Whamcloud - gitweb
Branch HEAD
[fs/lustre-release.git] / lustre / mdt / mdt_idmap.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  *
9  *   This file is part of Lustre, http://www.lustre.org.
10  *
11  *   Lustre is free software; you can redistribute it and/or
12  *   modify it under the terms of version 2 of the GNU General Public
13  *   License as published by the Free Software Foundation.
14  *
15  *   Lustre is distributed in the hope that it will be useful,
16  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
17  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  *   GNU General Public License for more details.
19  *
20  *   You should have received a copy of the GNU General Public License
21  *   along with Lustre; if not, write to the Free Software
22  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23  */
24
25 #ifndef EXPORT_SYMTAB
26 #define EXPORT_SYMTAB
27 #endif
28 #define DEBUG_SUBSYSTEM S_MDS
29
30 #ifndef AUTOCONF_INCLUDED
31 #include <linux/config.h>
32 #endif
33 #include <linux/module.h>
34 #include <linux/kernel.h>
35 #include <linux/mm.h>
36 #include <linux/kmod.h>
37 #include <linux/string.h>
38 #include <linux/stat.h>
39 #include <linux/errno.h>
40 #include <linux/version.h>
41 #include <linux/unistd.h>
42 #include <asm/system.h>
43 #include <asm/uaccess.h>
44 #include <linux/fs.h>
45 #include <linux/stat.h>
46 #include <asm/uaccess.h>
47 #include <linux/slab.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_sec.h>
57 #include <lustre_lib.h>
58 #include <lustre_ucache.h>
59
60 #include "mdt_internal.h"
61
62 int mdt_init_idmap(struct mdt_thread_info *info)
63 {
64         struct ptlrpc_request *req = mdt_info_req(info);
65         char *client = libcfs_nid2str(req->rq_peer.nid);
66         struct mdt_export_data *med = mdt_req2med(req);
67         struct obd_device *obd = req->rq_export->exp_obd;
68         struct obd_connect_data *data, *reply;
69         int rc = 0, remote;
70         ENTRY;
71
72         data = req_capsule_client_get(info->mti_pill, &RMF_CONNECT_DATA);
73         reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
74         if (data == NULL || reply == NULL)
75                 RETURN(-EFAULT);
76
77         if (!req->rq_auth_gss || req->rq_auth_usr_mdt) {
78                 med->med_rmtclient = 0;
79                 reply->ocd_connect_flags &= ~OBD_CONNECT_RMT_CLIENT;
80                 RETURN(0);
81         }
82
83         remote = data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT;
84
85         if (remote) {
86                 med->med_rmtclient = 1;
87                 if (!req->rq_auth_remote)
88                         CDEBUG(D_SEC, "client (local realm) %s -> target %s "
89                                "asked to be remote!\n", client, obd->obd_name);
90         } else if (req->rq_auth_remote) {
91                 med->med_rmtclient = 1;
92                 CDEBUG(D_SEC, "client (remote realm) %s -> target %s forced "
93                        "to be remote!\n", client, obd->obd_name);
94         }
95
96         if (med->med_rmtclient) {
97                 down(&med->med_idmap_sem);
98                 if (!med->med_idmap)
99                         med->med_idmap = lustre_idmap_init();
100                 up(&med->med_idmap_sem);
101
102                 if (IS_ERR(med->med_idmap)) {
103                         long err = PTR_ERR(med->med_idmap);
104
105                         med->med_idmap = NULL;
106                         CERROR("client %s -> target %s "
107                                "failed to init idmap [%ld]!\n",
108                                client, obd->obd_name, err);
109                         RETURN(err);
110                 } else if (!med->med_idmap) {
111                         CERROR("client %s -> target %s "
112                                "failed to init(2) idmap!\n",
113                                client, obd->obd_name);
114                         RETURN(-ENOMEM);
115                 }
116
117                 reply->ocd_connect_flags &= ~OBD_CONNECT_LCL_CLIENT;
118                 CDEBUG(D_SEC, "client %s -> target %s is remote.\n",
119                        client, obd->obd_name);
120
121                 /* NB, MDS_CONNECT establish root idmap too! */
122                 rc = mdt_handle_idmap(info);
123         } else {
124                 if (req->rq_auth_uid == INVALID_UID) {
125                         CDEBUG(D_SEC, "client %s -> target %s: user is not "
126                                "authenticated!\n", client, obd->obd_name);
127                         RETURN(-EACCES);
128                 }
129                 reply->ocd_connect_flags &= ~OBD_CONNECT_RMT_CLIENT;
130         }
131
132         RETURN(rc);
133 }
134
135 void mdt_cleanup_idmap(struct mdt_export_data *med)
136 {
137         LASSERT(med->med_rmtclient);
138
139         down(&med->med_idmap_sem);
140         if (med->med_idmap != NULL) {
141                 lustre_idmap_fini(med->med_idmap);
142                 med->med_idmap = NULL;
143         }
144         up(&med->med_idmap_sem);
145 }
146
147 static inline void mdt_revoke_export_locks(struct obd_export *exp)
148 {
149         /* don't revoke locks during recovery */
150         if (exp->exp_obd->obd_recovering)
151                 return;
152
153         ldlm_revoke_export_locks(exp);
154 }
155
156 int mdt_handle_idmap(struct mdt_thread_info *info)
157 {
158         struct ptlrpc_request *req = mdt_info_req(info);
159         struct mdt_device *mdt = info->mti_mdt;
160         struct mdt_export_data *med;
161         struct ptlrpc_user_desc *pud = req->rq_user_desc;
162         struct md_identity *identity;
163         __u32 opc;
164         int rc = 0;
165         ENTRY;
166
167         if (!req->rq_export)
168                 RETURN(0);
169
170         med = mdt_req2med(req);
171         if (!med->med_rmtclient)
172                 RETURN(0);
173
174         opc = lustre_msg_get_opc(req->rq_reqmsg);
175         /* Bypass other opc */
176         if ((opc != SEC_CTX_INIT) && (opc != SEC_CTX_INIT_CONT) &&
177             (opc != SEC_CTX_FINI) && (opc != MDS_CONNECT))
178                 RETURN(0);
179
180         LASSERT(med->med_idmap);
181
182         if (unlikely(!pud)) {
183                 CDEBUG(D_SEC, "remote client must run with rq_user_desc "
184                        "present\n");
185                 RETURN(-EACCES);
186         }
187
188         if (req->rq_auth_mapped_uid == INVALID_UID) {
189                 CDEBUG(D_SEC, "invalid authorized mapped uid, please check "
190                        "/etc/lustre/idmap.conf!\n");
191                 RETURN(-EACCES);
192         }
193
194         if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
195                 CDEBUG(D_SEC, "remote client must run with identity_get "
196                        "enabled!\n");
197                 RETURN(-EACCES);
198         }
199
200         identity = mdt_identity_get(mdt->mdt_identity_cache,
201                                     req->rq_auth_mapped_uid);
202         if (IS_ERR(identity)) {
203                 CDEBUG(D_SEC, "can't get mdt identity(%u), no mapping added\n",
204                        req->rq_auth_mapped_uid);
205                 RETURN(-EACCES);
206         }
207
208         switch (opc) {
209                 case SEC_CTX_INIT:
210                 case SEC_CTX_INIT_CONT:
211                 case MDS_CONNECT:
212                         rc = lustre_idmap_add(med->med_idmap,
213                                               pud->pud_uid, identity->mi_uid,
214                                               pud->pud_gid, identity->mi_gid);
215                         break;
216                 case SEC_CTX_FINI:
217                         rc = lustre_idmap_del(med->med_idmap,
218                                               pud->pud_uid, identity->mi_uid,
219                                               pud->pud_gid, identity->mi_gid);
220                         break;
221         }
222
223         mdt_identity_put(mdt->mdt_identity_cache, identity);
224
225         if (rc)
226                 RETURN(rc);
227
228         switch (opc) {
229                 case SEC_CTX_INIT:
230                 case SEC_CTX_INIT_CONT:
231                 case SEC_CTX_FINI:
232                         mdt_revoke_export_locks(req->rq_export);
233                         break;
234         }
235
236         RETURN(0);
237 }
238
239 int ptlrpc_user_desc_do_idmap(struct ptlrpc_request *req,
240                               struct ptlrpc_user_desc *pud)
241 {
242         struct mdt_export_data    *med = mdt_req2med(req);
243         struct lustre_idmap_table *idmap = med->med_idmap;
244         uid_t uid, fsuid;
245         gid_t gid, fsgid;
246
247         /* Only remote client need desc_to_idmap. */
248         if (!med->med_rmtclient)
249                 return 0;
250
251         uid = lustre_idmap_lookup_uid(NULL, idmap, 0, pud->pud_uid);
252         if (uid == CFS_IDMAP_NOTFOUND) {
253                 CDEBUG(D_SEC, "no mapping for uid %u\n", pud->pud_uid);
254                 return -EACCES;
255         }
256
257         if (pud->pud_uid == pud->pud_fsuid) {
258                 fsuid = uid;
259         } else {
260                 fsuid = lustre_idmap_lookup_uid(NULL, idmap, 0, pud->pud_fsuid);
261                 if (fsuid == CFS_IDMAP_NOTFOUND) {
262                         CDEBUG(D_SEC, "no mapping for fsuid %u\n",
263                                pud->pud_fsuid);
264                         return -EACCES;
265                 }
266         }
267
268         gid = lustre_idmap_lookup_gid(NULL, idmap, 0, pud->pud_gid);
269         if (gid == CFS_IDMAP_NOTFOUND) {
270                 CDEBUG(D_SEC, "no mapping for gid %u\n", pud->pud_gid);
271                 return -EACCES;
272         }
273
274         if (pud->pud_gid == pud->pud_fsgid) {
275                 fsgid = gid;
276         } else {
277                 fsgid = lustre_idmap_lookup_gid(NULL, idmap, 0, pud->pud_fsgid);
278                 if (fsgid == CFS_IDMAP_NOTFOUND) {
279                         CDEBUG(D_SEC, "no mapping for fsgid %u\n",
280                                pud->pud_fsgid);
281                         return -EACCES;
282                 }
283         }
284
285         pud->pud_uid = uid;
286         pud->pud_gid = gid;
287         pud->pud_fsuid = fsuid;
288         pud->pud_fsgid = fsgid;
289
290         return 0;
291 }
292
293 /*
294  * Reverse mapping
295  */
296 void mdt_body_reverse_idmap(struct mdt_thread_info *info, struct mdt_body *body)
297 {
298         struct ptlrpc_request     *req = mdt_info_req(info);
299         struct md_ucred           *uc = mdt_ucred(info);
300         struct mdt_export_data    *med = mdt_req2med(req);
301         struct lustre_idmap_table *idmap = med->med_idmap;
302
303         if (!med->med_rmtclient)
304                 return;
305
306         if (body->valid & OBD_MD_FLUID) {
307                 uid_t uid = lustre_idmap_lookup_uid(uc, idmap, 1, body->uid);
308
309                 if (uid == CFS_IDMAP_NOTFOUND) {
310                         uid = NOBODY_UID;
311                         if (body->valid & OBD_MD_FLMODE)
312                                 body->mode = (body->mode & ~S_IRWXU) |
313                                              ((body->mode & S_IRWXO) << 6);
314                 }
315
316                 body->uid = uid;
317         }
318
319         if (body->valid & OBD_MD_FLGID) {
320                 gid_t gid = lustre_idmap_lookup_gid(uc, idmap, 1, body->gid);
321
322                 if (gid == CFS_IDMAP_NOTFOUND) {
323                         gid = NOBODY_GID;
324                         if (body->valid & OBD_MD_FLMODE)
325                                 body->mode = (body->mode & ~S_IRWXG) |
326                                              ((body->mode & S_IRWXO) << 3);
327                 }
328
329                 body->gid = gid;
330         }
331 }
332
333 /* Do not ignore root_squash for non-setattr case. */
334 int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
335 {
336         struct ptlrpc_request     *req = mdt_info_req(info);
337         struct md_ucred           *uc = mdt_ucred(info);
338         struct lu_attr            *attr = &info->mti_attr.ma_attr;
339         struct mdt_export_data    *med = mdt_req2med(req);
340         struct lustre_idmap_table *idmap = med->med_idmap;
341
342         if ((uc->mu_valid != UCRED_OLD) && (uc->mu_valid != UCRED_NEW))
343                 return -EINVAL;
344
345         if (op != REINT_SETATTR) {
346                 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
347                         attr->la_uid = uc->mu_fsuid;
348                 /* for S_ISGID, inherit gid from his parent, such work will be
349                  * done in cmm/mdd layer, here set all cases as uc->mu_fsgid. */
350                 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
351                         attr->la_gid = uc->mu_fsgid;
352         } else if (med->med_rmtclient) {
353                 /* NB: -1 case will be handled by mdt_fix_attr() later. */
354                 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1)) {
355                         uid_t uid = lustre_idmap_lookup_uid(uc, idmap, 0,
356                                                             attr->la_uid);
357
358                         if (uid == CFS_IDMAP_NOTFOUND) {
359                                 CDEBUG(D_SEC, "Deny chown to uid %u\n",
360                                        attr->la_uid);
361                                 return -EPERM;
362                         }
363
364                         attr->la_uid = uid;
365                 }
366                 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1)) {
367                         gid_t gid = lustre_idmap_lookup_gid(uc, idmap, 0,
368                                                             attr->la_gid);
369
370                         if (gid == CFS_IDMAP_NOTFOUND) {
371                                 CDEBUG(D_SEC, "Deny chown to gid %u\n",
372                                        attr->la_gid);
373                                 return -EPERM;
374                         }
375
376                         attr->la_gid = gid;
377                 }
378         }
379
380         return 0;
381 }