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