Whamcloud - gitweb
branch: HEAD
[fs/lustre-release.git] / lustre / mdt / mdt_identity.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2004-2006 Cluster File Systems, Inc.
5  *   Author: Lai Siyao <lsy@clusterfs.com>
6  *   Author: Fan Yong <fanyong@clusterfs.com>
7  *
8  *   This file is part of Lustre, http://www.lustre.org.
9  *
10  *   Lustre is free software; you can redistribute it and/or
11  *   modify it under the terms of version 2 of the GNU General Public
12  *   License as published by the Free Software Foundation.
13  *
14  *   Lustre is distributed in the hope that it will be useful,
15  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
16  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  *   GNU General Public License for more details.
18  *
19  *   You should have received a copy of the GNU General Public License
20  *   along with Lustre; if not, write to the Free Software
21  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22  */
23
24 #ifndef EXPORT_SYMTAB
25 #define EXPORT_SYMTAB
26 #endif
27 #define DEBUG_SUBSYSTEM S_MDS
28
29 #ifndef AUTOCONF_INCLUDED
30 #include <linux/config.h>
31 #endif
32 #include <linux/module.h>
33 #include <linux/kernel.h>
34 #include <linux/mm.h>
35 #include <linux/kmod.h>
36 #include <linux/string.h>
37 #include <linux/stat.h>
38 #include <linux/errno.h>
39 #include <linux/version.h>
40 #include <linux/unistd.h>
41 #include <asm/system.h>
42 #include <asm/uaccess.h>
43 #include <linux/fs.h>
44 #include <linux/stat.h>
45 #include <asm/uaccess.h>
46 #include <linux/slab.h>
47
48 #include <libcfs/kp30.h>
49 #include <obd.h>
50 #include <obd_class.h>
51 #include <obd_support.h>
52 #include <lustre_net.h>
53 #include <lustre_import.h>
54 #include <lustre_dlm.h>
55 #include <lustre_lib.h>
56 #include <lustre_ucache.h>
57
58 #include "mdt_internal.h"
59
60 static void mdt_identity_entry_init(struct upcall_cache_entry *entry,
61                                     void *unused)
62 {
63         entry->u.identity.mi_uc_entry = entry;
64 }
65
66 static void mdt_identity_entry_free(struct upcall_cache *cache,
67                                     struct upcall_cache_entry *entry)
68 {
69         struct md_identity *identity = &entry->u.identity;
70
71         if (identity->mi_ginfo) {
72                 groups_free(identity->mi_ginfo);
73                 identity->mi_ginfo = NULL;
74         }
75
76         if (identity->mi_nperms) {
77                 LASSERT(identity->mi_perms);
78                 OBD_FREE(identity->mi_perms,
79                          identity->mi_nperms * sizeof(struct md_perm));
80                 identity->mi_nperms = 0;
81         }
82 }
83
84 static int mdt_identity_do_upcall(struct upcall_cache *cache,
85                                   struct upcall_cache_entry *entry)
86 {
87         char *upcall, keystr[16];
88         char *argv[] = {
89                   [0] = cache->uc_upcall,
90                   [1] = cache->uc_name,
91                   [2] = keystr,
92                   [3] = NULL
93         };
94         char *envp[] = {
95                   [0] = "HOME=/",
96                   [1] = "PATH=/sbin:/usr/sbin",
97                   [2] = NULL
98         };
99         int size, rc;
100         ENTRY;
101
102         /* There is race condition:
103          * "uc_upcall" was changed just after "is_identity_get_disabled" check.
104          */
105         size = strlen(cache->uc_upcall) + 1;
106         OBD_ALLOC(upcall, size);
107         if (unlikely(!upcall))
108                 RETURN(-ENOMEM);
109
110         read_lock(&cache->uc_upcall_rwlock);
111         memcpy(upcall, cache->uc_upcall, size - 1);
112         read_unlock(&cache->uc_upcall_rwlock);
113         upcall[size - 1] = 0;
114         if (unlikely(!strcmp(upcall, "NONE"))) {
115                 CERROR("no upcall set\n");
116                 GOTO(out, rc = -EREMCHG);
117         }
118
119         argv[0] = upcall;
120
121         snprintf(keystr, sizeof(keystr), LPU64, entry->ue_key);
122
123         CDEBUG(D_INFO, "The upcall is: %s \n", cache->uc_upcall);
124
125         rc = USERMODEHELPER(argv[0], argv, envp);
126         if (rc < 0) {
127                 CERROR("%s: error invoking upcall %s %s %s: rc %d; "
128                        "check /proc/fs/lustre/mdt/%s/identity_upcall\n",
129                        cache->uc_name, argv[0], argv[1], argv[2], rc,
130                        cache->uc_name);
131         } else {
132                 CDEBUG(D_HA, "%s: invoked upcall %s %s %s\n", cache->uc_name,
133                        argv[0], argv[1], argv[2]);
134                 rc = 0;
135         }
136         EXIT;
137 out:
138         OBD_FREE(upcall, size);
139         return rc;
140 }
141
142 static int mdt_identity_parse_downcall(struct upcall_cache *cache,
143                                        struct upcall_cache_entry *entry,
144                                        void *args)
145 {
146         struct md_identity *identity = &entry->u.identity;
147         struct identity_downcall_data *data = args;
148         struct group_info *ginfo;
149         struct md_perm *perms = NULL;
150         int size, i;
151         ENTRY;
152
153         LASSERT(data);
154         if (data->idd_ngroups > NGROUPS_MAX)
155                 RETURN(-E2BIG);
156
157         ginfo = groups_alloc(data->idd_ngroups);
158         if (!ginfo) {
159                 CERROR("failed to alloc %d groups\n", data->idd_ngroups);
160                 RETURN(-ENOMEM);
161         }
162
163         lustre_groups_from_list(ginfo, data->idd_groups);
164         lustre_groups_sort(ginfo);
165
166         if (data->idd_nperms) {
167                 size = data->idd_nperms * sizeof(*perms);
168                 OBD_ALLOC(perms, size);
169                 if (!perms) {
170                         CERROR("failed to alloc %d permissions\n",
171                                data->idd_nperms);
172                         groups_free(ginfo);
173                         RETURN(-ENOMEM);
174                 }
175
176                 for (i = 0; i < data->idd_nperms; i++) {
177                         perms[i].mp_nid = data->idd_perms[i].pdd_nid;
178                         perms[i].mp_perm = data->idd_perms[i].pdd_perm;
179                 }
180         }
181
182         identity->mi_uid = data->idd_uid;
183         identity->mi_gid = data->idd_gid;
184         identity->mi_ginfo = ginfo;
185         identity->mi_nperms = data->idd_nperms;
186         identity->mi_perms = perms;
187
188         CDEBUG(D_OTHER, "parse mdt identity@%p: %d:%d, ngroups %u, nperms %u\n",
189                identity, identity->mi_uid, identity->mi_gid,
190                identity->mi_ginfo->ngroups, identity->mi_nperms);
191
192         RETURN(0);
193 }
194
195 struct md_identity *mdt_identity_get(struct upcall_cache *cache, __u32 uid)
196 {
197         struct upcall_cache_entry *entry;
198
199         if (!cache)
200                 return ERR_PTR(-ENOENT);
201
202         entry = upcall_cache_get_entry(cache, (__u64)uid, NULL);
203         if (IS_ERR(entry))
204                 return ERR_PTR(PTR_ERR(entry));
205         else if (unlikely(!entry))
206                 return ERR_PTR(-ENOENT);
207         else
208                 return &entry->u.identity;
209 }
210
211 void mdt_identity_put(struct upcall_cache *cache, struct md_identity *identity)
212 {
213         if (!cache)
214                 return;
215
216         LASSERT(identity);
217         upcall_cache_put_entry(cache, identity->mi_uc_entry);
218 }
219
220 struct upcall_cache_ops mdt_identity_upcall_cache_ops = {
221         .init_entry     = mdt_identity_entry_init,
222         .free_entry     = mdt_identity_entry_free,
223         .do_upcall      = mdt_identity_do_upcall,
224         .parse_downcall = mdt_identity_parse_downcall,
225 };
226
227 void mdt_flush_identity(struct upcall_cache *cache, int uid)
228 {
229         if (uid < 0)
230                 upcall_cache_flush_idle(cache);
231         else
232                 upcall_cache_flush_one(cache, (__u64)uid, NULL);
233 }
234
235 /*
236  * If there is LNET_NID_ANY in perm[i].mp_nid,
237  * it must be perm[0].mp_nid, and act as default perm.
238  */
239 __u32 mdt_identity_get_perm(struct md_identity *identity,
240                             __u32 is_rmtclient, lnet_nid_t nid)
241 {
242         struct md_perm *perm;
243         int i;
244
245         if (!identity) {
246                 LASSERT(is_rmtclient == 0);
247                 return CFS_SETGRP_PERM;
248         }
249
250         perm = identity->mi_perms;
251         /* check exactly matched nid first */
252         for (i = identity->mi_nperms - 1; i > 0; i--) {
253                 if (perm[i].mp_nid != nid)
254                         continue;
255                 return perm[i].mp_perm;
256         }
257
258         /* check LNET_NID_ANY then */
259         if ((identity->mi_nperms > 0) &&
260             ((perm[0].mp_nid == nid) || (perm[0].mp_nid == LNET_NID_ANY)))
261                 return perm[0].mp_perm;
262
263         /* return default last */
264         return is_rmtclient ? 0 : CFS_SETGRP_PERM;
265 }
266
267 int mdt_pack_remote_perm(struct mdt_thread_info *info, struct mdt_object *o,
268                          void *buf)
269 {
270         struct ptlrpc_request   *req = mdt_info_req(info);
271         struct md_ucred         *uc = mdt_ucred(info);
272         struct md_object        *next = mdt_object_child(o);
273         struct mdt_export_data  *med = mdt_req2med(req);
274         struct mdt_remote_perm  *perm = buf;
275
276         ENTRY;
277
278         /* remote client request always pack ptlrpc_user_desc! */
279         LASSERT(perm);
280
281         if (!med->med_rmtclient)
282                 RETURN(-EBADE);
283
284         if ((uc->mu_valid != UCRED_OLD) && (uc->mu_valid != UCRED_NEW))
285                 RETURN(-EINVAL);
286
287         perm->rp_uid = uc->mu_o_uid;
288         perm->rp_gid = uc->mu_o_gid;
289         perm->rp_fsuid = uc->mu_o_fsuid;
290         perm->rp_fsgid = uc->mu_o_fsgid;
291
292         perm->rp_access_perm = 0;
293         if (mo_permission(info->mti_env, NULL, next, NULL, MAY_READ) == 0)
294                 perm->rp_access_perm |= MAY_READ;
295         if (mo_permission(info->mti_env, NULL, next, NULL, MAY_WRITE) == 0)
296                 perm->rp_access_perm |= MAY_WRITE;
297         if (mo_permission(info->mti_env, NULL, next, NULL, MAY_EXEC) == 0)
298                 perm->rp_access_perm |= MAY_EXEC;
299
300         RETURN(0);
301 }