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 #ifdef HAVE_SEGMENT_H
48 #include <asm/segment.h>
49 #endif
50
51 #include <libcfs/kp30.h>
52 #include <obd.h>
53 #include <obd_class.h>
54 #include <obd_support.h>
55 #include <lustre_net.h>
56 #include <lustre_import.h>
57 #include <lustre_dlm.h>
58 #include <lustre_lib.h>
59 #include <lustre_ucache.h>
60
61 #include "mdt_internal.h"
62
63 static void mdt_identity_entry_init(struct upcall_cache_entry *entry,
64                                     void *unused)
65 {
66         entry->u.identity.mi_uc_entry = entry;
67 }
68
69 static void mdt_identity_entry_free(struct upcall_cache *cache,
70                                     struct upcall_cache_entry *entry)
71 {
72         struct md_identity *identity = &entry->u.identity;
73
74         if (identity->mi_ginfo) {
75                 groups_free(identity->mi_ginfo);
76                 identity->mi_ginfo = NULL;
77         }
78
79         if (identity->mi_nperms) {
80                 LASSERT(identity->mi_perms);
81                 OBD_FREE(identity->mi_perms,
82                          identity->mi_nperms * sizeof(struct md_perm));
83                 identity->mi_nperms = 0;
84         }
85 }
86
87 static int mdt_identity_do_upcall(struct upcall_cache *cache,
88                                   struct upcall_cache_entry *entry)
89 {
90         char *upcall, keystr[16];
91         char *argv[] = {
92                   [0] = cache->uc_upcall,
93                   [1] = cache->uc_name,
94                   [2] = keystr,
95                   [3] = NULL
96         };
97         char *envp[] = {
98                   [0] = "HOME=/",
99                   [1] = "PATH=/sbin:/usr/sbin",
100                   [2] = NULL
101         };
102         int size, rc;
103         ENTRY;
104
105         /* There is race condition:
106          * "uc_upcall" was changed just after "is_identity_get_disabled" check.
107          */
108         size = strlen(cache->uc_upcall) + 1;
109         OBD_ALLOC(upcall, size);
110         if (unlikely(!upcall))
111                 RETURN(-ENOMEM);
112
113         read_lock(&cache->uc_upcall_rwlock);
114         memcpy(upcall, cache->uc_upcall, size - 1);
115         read_unlock(&cache->uc_upcall_rwlock);
116         upcall[size - 1] = 0;
117         if (unlikely(!strcmp(upcall, "NONE"))) {
118                 CERROR("no upcall set\n");
119                 GOTO(out, rc = -EREMCHG);
120         }
121
122         argv[0] = upcall;
123
124         snprintf(keystr, sizeof(keystr), LPU64, entry->ue_key);
125
126         CDEBUG(D_INFO, "The upcall is: %s \n", cache->uc_upcall);
127
128         rc = USERMODEHELPER(argv[0], argv, envp);
129         if (rc < 0) {
130                 CERROR("%s: error invoking upcall %s %s %s: rc %d; "
131                        "check /proc/fs/lustre/mdt/%s/identity_upcall\n",
132                        cache->uc_name, argv[0], argv[1], argv[2], rc,
133                        cache->uc_name);
134         } else {
135                 CDEBUG(D_HA, "%s: invoked upcall %s %s %s\n", cache->uc_name,
136                        argv[0], argv[1], argv[2]);
137                 rc = 0;
138         }
139         EXIT;
140 out:
141         OBD_FREE(upcall, size);
142         return rc;
143 }
144
145 static int mdt_identity_parse_downcall(struct upcall_cache *cache,
146                                        struct upcall_cache_entry *entry,
147                                        void *args)
148 {
149         struct md_identity *identity = &entry->u.identity;
150         struct identity_downcall_data *data = args;
151         struct group_info *ginfo;
152         struct md_perm *perms = NULL;
153         int size, i;
154         ENTRY;
155
156         LASSERT(data);
157         if (data->idd_ngroups > NGROUPS_MAX)
158                 RETURN(-E2BIG);
159
160         ginfo = groups_alloc(data->idd_ngroups);
161         if (!ginfo) {
162                 CERROR("failed to alloc %d groups\n", data->idd_ngroups);
163                 RETURN(-ENOMEM);
164         }
165
166         lustre_groups_from_list(ginfo, data->idd_groups);
167         lustre_groups_sort(ginfo);
168
169         if (data->idd_nperms) {
170                 size = data->idd_nperms * sizeof(*perms);
171                 OBD_ALLOC(perms, size);
172                 if (!perms) {
173                         CERROR("failed to alloc %d permissions\n",
174                                data->idd_nperms);
175                         groups_free(ginfo);
176                         RETURN(-ENOMEM);
177                 }
178
179                 for (i = 0; i < data->idd_nperms; i++) {
180                         perms[i].mp_nid = data->idd_perms[i].pdd_nid;
181                         perms[i].mp_perm = data->idd_perms[i].pdd_perm;
182                 }
183         }
184
185         identity->mi_uid = data->idd_uid;
186         identity->mi_gid = data->idd_gid;
187         identity->mi_ginfo = ginfo;
188         identity->mi_nperms = data->idd_nperms;
189         identity->mi_perms = perms;
190
191         CDEBUG(D_OTHER, "parse mdt identity@%p: %d:%d, ngroups %u, nperms %u\n",
192                identity, identity->mi_uid, identity->mi_gid,
193                identity->mi_ginfo->ngroups, identity->mi_nperms);
194
195         RETURN(0);
196 }
197
198 struct md_identity *mdt_identity_get(struct upcall_cache *cache, __u32 uid)
199 {
200         struct upcall_cache_entry *entry;
201
202         if (!cache)
203                 return ERR_PTR(-ENOENT);
204
205         entry = upcall_cache_get_entry(cache, (__u64)uid, NULL);
206         if (IS_ERR(entry))
207                 return ERR_PTR(PTR_ERR(entry));
208         else if (unlikely(!entry))
209                 return ERR_PTR(-ENOENT);
210         else
211                 return &entry->u.identity;
212 }
213
214 void mdt_identity_put(struct upcall_cache *cache, struct md_identity *identity)
215 {
216         if (!cache)
217                 return;
218
219         LASSERT(identity);
220         upcall_cache_put_entry(cache, identity->mi_uc_entry);
221 }
222
223 struct upcall_cache_ops mdt_identity_upcall_cache_ops = {
224         .init_entry     = mdt_identity_entry_init,
225         .free_entry     = mdt_identity_entry_free,
226         .do_upcall      = mdt_identity_do_upcall,
227         .parse_downcall = mdt_identity_parse_downcall,
228 };
229
230 void mdt_flush_identity(struct upcall_cache *cache, int uid)
231 {
232         if (uid < 0)
233                 upcall_cache_flush_idle(cache);
234         else
235                 upcall_cache_flush_one(cache, (__u64)uid, NULL);
236 }
237
238 /*
239  * If there is LNET_NID_ANY in perm[i].mp_nid,
240  * it must be perm[0].mp_nid, and act as default perm.
241  */
242 __u32 mdt_identity_get_perm(struct md_identity *identity,
243                             __u32 is_rmtclient, lnet_nid_t nid)
244 {
245         struct md_perm *perm;
246         int i;
247
248         if (!identity) {
249                 LASSERT(is_rmtclient == 0);
250                 return CFS_SETGRP_PERM;
251         }
252
253         perm = identity->mi_perms;
254         /* check exactly matched nid first */
255         for (i = identity->mi_nperms - 1; i > 0; i--) {
256                 if (perm[i].mp_nid != nid)
257                         continue;
258                 return perm[i].mp_perm;
259         }
260
261         /* check LNET_NID_ANY then */
262         if ((identity->mi_nperms > 0) &&
263             ((perm[0].mp_nid == nid) || (perm[0].mp_nid == LNET_NID_ANY)))
264                 return perm[0].mp_perm;
265
266         /* return default last */
267         return is_rmtclient ? 0 : CFS_SETGRP_PERM;
268 }
269
270 int mdt_pack_remote_perm(struct mdt_thread_info *info, struct mdt_object *o,
271                          void *buf)
272 {
273         struct ptlrpc_request   *req = mdt_info_req(info);
274         struct md_ucred         *uc = mdt_ucred(info);
275         struct md_object        *next = mdt_object_child(o);
276         struct mdt_export_data  *med = mdt_req2med(req);
277         struct mdt_remote_perm  *perm = buf;
278
279         ENTRY;
280
281         /* remote client request always pack ptlrpc_user_desc! */
282         LASSERT(perm);
283
284         if (!med->med_rmtclient)
285                 RETURN(-EBADE);
286
287         if ((uc->mu_valid != UCRED_OLD) && (uc->mu_valid != UCRED_NEW))
288                 RETURN(-EINVAL);
289
290         perm->rp_uid = uc->mu_o_uid;
291         perm->rp_gid = uc->mu_o_gid;
292         perm->rp_fsuid = uc->mu_o_fsuid;
293         perm->rp_fsgid = uc->mu_o_fsgid;
294
295         perm->rp_access_perm = 0;
296         if (mo_permission(info->mti_env, NULL, next, NULL, MAY_READ) == 0)
297                 perm->rp_access_perm |= MAY_READ;
298         if (mo_permission(info->mti_env, NULL, next, NULL, MAY_WRITE) == 0)
299                 perm->rp_access_perm |= MAY_WRITE;
300         if (mo_permission(info->mti_env, NULL, next, NULL, MAY_EXEC) == 0)
301                 perm->rp_access_perm |= MAY_EXEC;
302
303         RETURN(0);
304 }