Whamcloud - gitweb
315fff13f2107aea7e4d03b8c3ce7042a3090a9a
[fs/lustre-release.git] / lustre / mdt / mdt_idmap.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  */
30 /*
31  * This file is part of Lustre, http://www.lustre.org/
32  * Lustre is a trademark of Sun Microsystems, Inc.
33  *
34  * lustre/mdt/mdt_idmap.c
35  *
36  * Author: Lai Siyao <lsy@clusterfs.com>
37  * Author: Fan Yong <fanyong@clusterfs.com>
38  */
39
40 #ifndef EXPORT_SYMTAB
41 #define EXPORT_SYMTAB
42 #endif
43 #define DEBUG_SUBSYSTEM S_MDS
44
45 #ifndef AUTOCONF_INCLUDED
46 #include <linux/config.h>
47 #endif
48 #include <linux/module.h>
49 #include <linux/kernel.h>
50 #include <linux/mm.h>
51 #include <linux/kmod.h>
52 #include <linux/string.h>
53 #include <linux/stat.h>
54 #include <linux/errno.h>
55 #include <linux/version.h>
56 #include <linux/unistd.h>
57 #include <asm/system.h>
58 #include <asm/uaccess.h>
59 #include <linux/fs.h>
60 #include <linux/stat.h>
61 #include <asm/uaccess.h>
62 #include <linux/slab.h>
63
64 #include <libcfs/libcfs.h>
65 #include <libcfs/lucache.h>
66 #include <obd.h>
67 #include <obd_class.h>
68 #include <obd_support.h>
69 #include <lustre_net.h>
70 #include <lustre_import.h>
71 #include <lustre_dlm.h>
72 #include <lustre_sec.h>
73 #include <lustre_lib.h>
74
75 #include "mdt_internal.h"
76
77 #define mdt_init_sec_none(reply, exp)                                   \
78 do {                                                                    \
79         reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT |          \
80                                       OBD_CONNECT_RMT_CLIENT_FORCE |    \
81                                       OBD_CONNECT_MDS_CAPA |            \
82                                       OBD_CONNECT_OSS_CAPA);            \
83         cfs_spin_lock(&exp->exp_lock);                                  \
84         exp->exp_connect_flags = reply->ocd_connect_flags;              \
85         cfs_spin_unlock(&exp->exp_lock);                                \
86 } while (0)
87
88 int mdt_init_sec_level(struct mdt_thread_info *info)
89 {
90         struct mdt_device *mdt = info->mti_mdt;
91         struct ptlrpc_request *req = mdt_info_req(info);
92         char *client = libcfs_nid2str(req->rq_peer.nid);
93         struct obd_export *exp = req->rq_export;
94         struct obd_device *obd = exp->exp_obd;
95         struct obd_connect_data *data, *reply;
96         int rc = 0, remote;
97         ENTRY;
98
99         data = req_capsule_client_get(info->mti_pill, &RMF_CONNECT_DATA);
100         reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
101         if (data == NULL || reply == NULL)
102                 RETURN(-EFAULT);
103
104         /* connection from MDT is always trusted */
105         if (req->rq_auth_usr_mdt) {
106                 mdt_init_sec_none(reply, exp);
107                 RETURN(0);
108         }
109
110         /* no GSS support case */
111         if (!req->rq_auth_gss) {
112                 if (mdt->mdt_sec_level > LUSTRE_SEC_NONE) {
113                         CWARN("client %s -> target %s does not user GSS, "
114                               "can not run under security level %d.\n",
115                               client, obd->obd_name, mdt->mdt_sec_level);
116                         RETURN(-EACCES);
117                 } else {
118                         mdt_init_sec_none(reply, exp);
119                         RETURN(0);
120                 }
121         }
122
123         /* old version case */
124         if (unlikely(!(data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT) ||
125                      !(data->ocd_connect_flags & OBD_CONNECT_MDS_CAPA) ||
126                      !(data->ocd_connect_flags & OBD_CONNECT_OSS_CAPA))) {
127                 if (mdt->mdt_sec_level > LUSTRE_SEC_NONE) {
128                         CWARN("client %s -> target %s uses old version, "
129                               "can not run under security level %d.\n",
130                               client, obd->obd_name, mdt->mdt_sec_level);
131                         RETURN(-EACCES);
132                 } else {
133                         CWARN("client %s -> target %s uses old version, "
134                               "run under security level %d.\n",
135                               client, obd->obd_name, mdt->mdt_sec_level);
136                         mdt_init_sec_none(reply, exp);
137                         RETURN(0);
138                 }
139         }
140
141         remote = data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT_FORCE;
142         if (remote) {
143                 if (!req->rq_auth_remote)
144                         CDEBUG(D_SEC, "client (local realm) %s -> target %s "
145                                "asked to be remote.\n", client, obd->obd_name);
146         } else if (req->rq_auth_remote) {
147                 remote = 1;
148                 CDEBUG(D_SEC, "client (remote realm) %s -> target %s is set "
149                        "as remote by default.\n", client, obd->obd_name);
150         }
151
152         if (remote) {
153                 if (!mdt->mdt_opts.mo_oss_capa) {
154                         CDEBUG(D_SEC, "client %s -> target %s is set as remote,"
155                                " but OSS capabilities are not enabled: %d.\n",
156                                client, obd->obd_name, mdt->mdt_opts.mo_oss_capa);
157                         RETURN(-EACCES);
158                 }
159         } else {
160                 if (req->rq_auth_uid == INVALID_UID) {
161                         CDEBUG(D_SEC, "client %s -> target %s: user is not "
162                                "authenticated!\n", client, obd->obd_name);
163                         RETURN(-EACCES);
164                 }
165         }
166
167         switch (mdt->mdt_sec_level) {
168         case LUSTRE_SEC_NONE:
169                 if (!remote) {
170                         mdt_init_sec_none(reply, exp);
171                         break;
172                 } else {
173                         CDEBUG(D_SEC, "client %s -> target %s is set as remote, "
174                                "can not run under security level %d.\n",
175                                client, obd->obd_name, mdt->mdt_sec_level);
176                         RETURN(-EACCES);
177                 }
178         case LUSTRE_SEC_REMOTE:
179                 if (!remote)
180                         mdt_init_sec_none(reply, exp);
181                 break;
182         case LUSTRE_SEC_ALL:
183                 if (!remote) {
184                         reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT |
185                                                       OBD_CONNECT_RMT_CLIENT_FORCE);
186                         if (!mdt->mdt_opts.mo_mds_capa)
187                                 reply->ocd_connect_flags &= ~OBD_CONNECT_MDS_CAPA;
188                         if (!mdt->mdt_opts.mo_oss_capa)
189                                 reply->ocd_connect_flags &= ~OBD_CONNECT_OSS_CAPA;
190
191                         cfs_spin_lock(&exp->exp_lock);
192                         exp->exp_connect_flags = reply->ocd_connect_flags;
193                         cfs_spin_unlock(&exp->exp_lock);
194                 }
195                 break;
196         default:
197                 RETURN(-EINVAL);
198         }
199
200         RETURN(rc);
201 }
202
203 int mdt_init_idmap(struct mdt_thread_info *info)
204 {
205         struct ptlrpc_request *req = mdt_info_req(info);
206         struct mdt_export_data *med = mdt_req2med(req);
207         struct obd_export *exp = req->rq_export;
208         char *client = libcfs_nid2str(req->rq_peer.nid);
209         struct obd_device *obd = exp->exp_obd;
210         int rc = 0;
211         ENTRY;
212
213         if (exp_connect_rmtclient(exp)) {
214                 cfs_mutex_lock(&med->med_idmap_mutex);
215                 if (!med->med_idmap)
216                         med->med_idmap = lustre_idmap_init();
217                 cfs_mutex_unlock(&med->med_idmap_mutex);
218
219                 if (IS_ERR(med->med_idmap)) {
220                         long err = PTR_ERR(med->med_idmap);
221
222                         med->med_idmap = NULL;
223                         CERROR("client %s -> target %s "
224                                "failed to init idmap [%ld]!\n",
225                                client, obd->obd_name, err);
226                         RETURN(err);
227                 } else if (!med->med_idmap) {
228                         CERROR("client %s -> target %s "
229                                "failed to init(2) idmap!\n",
230                                client, obd->obd_name);
231                         RETURN(-ENOMEM);
232                 }
233
234                 CDEBUG(D_SEC, "client %s -> target %s is remote.\n",
235                        client, obd->obd_name);
236                 /* NB, MDS_CONNECT establish root idmap too! */
237                 rc = mdt_handle_idmap(info);
238         }
239         RETURN(rc);
240 }
241
242 void mdt_cleanup_idmap(struct mdt_export_data *med)
243 {
244         cfs_mutex_lock(&med->med_idmap_mutex);
245         if (med->med_idmap != NULL) {
246                 lustre_idmap_fini(med->med_idmap);
247                 med->med_idmap = NULL;
248         }
249         cfs_mutex_unlock(&med->med_idmap_mutex);
250 }
251
252 static inline void mdt_revoke_export_locks(struct obd_export *exp)
253 {
254         /* don't revoke locks during recovery */
255         if (exp->exp_obd->obd_recovering)
256                 return;
257
258         ldlm_revoke_export_locks(exp);
259 }
260
261 int mdt_handle_idmap(struct mdt_thread_info *info)
262 {
263         struct ptlrpc_request *req = mdt_info_req(info);
264         struct mdt_device *mdt = info->mti_mdt;
265         struct mdt_export_data *med;
266         struct ptlrpc_user_desc *pud = req->rq_user_desc;
267         struct md_identity *identity;
268         __u32 opc;
269         int rc = 0;
270         ENTRY;
271
272         if (!req->rq_export)
273                 RETURN(0);
274
275         med = mdt_req2med(req);
276         if (!exp_connect_rmtclient(info->mti_exp))
277                 RETURN(0);
278
279         opc = lustre_msg_get_opc(req->rq_reqmsg);
280         /* Bypass other opc */
281         if ((opc != SEC_CTX_INIT) && (opc != SEC_CTX_INIT_CONT) &&
282             (opc != SEC_CTX_FINI) && (opc != MDS_CONNECT))
283                 RETURN(0);
284
285         LASSERT(med->med_idmap);
286
287         if (unlikely(!pud)) {
288                 CDEBUG(D_SEC, "remote client must run with rq_user_desc "
289                        "present\n");
290                 RETURN(-EACCES);
291         }
292
293         if (req->rq_auth_mapped_uid == INVALID_UID) {
294                 CDEBUG(D_SEC, "invalid authorized mapped uid, please check "
295                        "/etc/lustre/idmap.conf!\n");
296                 RETURN(-EACCES);
297         }
298
299         if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
300                 CDEBUG(D_SEC, "remote client must run with identity_get "
301                        "enabled!\n");
302                 RETURN(-EACCES);
303         }
304
305         identity = mdt_identity_get(mdt->mdt_identity_cache,
306                                     req->rq_auth_mapped_uid);
307         if (IS_ERR(identity)) {
308                 CDEBUG(D_SEC, "can't get mdt identity(%u), no mapping added\n",
309                        req->rq_auth_mapped_uid);
310                 RETURN(-EACCES);
311         }
312
313         switch (opc) {
314                 case SEC_CTX_INIT:
315                 case SEC_CTX_INIT_CONT:
316                 case MDS_CONNECT:
317                         rc = lustre_idmap_add(med->med_idmap,
318                                               pud->pud_uid, identity->mi_uid,
319                                               pud->pud_gid, identity->mi_gid);
320                         break;
321                 case SEC_CTX_FINI:
322                         rc = lustre_idmap_del(med->med_idmap,
323                                               pud->pud_uid, identity->mi_uid,
324                                               pud->pud_gid, identity->mi_gid);
325                         break;
326         }
327
328         mdt_identity_put(mdt->mdt_identity_cache, identity);
329
330         if (rc)
331                 RETURN(rc);
332
333         switch (opc) {
334                 case SEC_CTX_INIT:
335                 case SEC_CTX_INIT_CONT:
336                 case SEC_CTX_FINI:
337                         mdt_revoke_export_locks(req->rq_export);
338                         break;
339         }
340
341         RETURN(0);
342 }
343
344 int ptlrpc_user_desc_do_idmap(struct ptlrpc_request *req,
345                               struct ptlrpc_user_desc *pud)
346 {
347         struct mdt_export_data    *med = mdt_req2med(req);
348         struct lustre_idmap_table *idmap = med->med_idmap;
349         uid_t uid, fsuid;
350         gid_t gid, fsgid;
351
352         /* Only remote client need desc_to_idmap. */
353         if (!exp_connect_rmtclient(req->rq_export))
354                 return 0;
355
356         uid = lustre_idmap_lookup_uid(NULL, idmap, 0, pud->pud_uid);
357         if (uid == CFS_IDMAP_NOTFOUND) {
358                 CDEBUG(D_SEC, "no mapping for uid %u\n", pud->pud_uid);
359                 return -EACCES;
360         }
361
362         if (pud->pud_uid == pud->pud_fsuid) {
363                 fsuid = uid;
364         } else {
365                 fsuid = lustre_idmap_lookup_uid(NULL, idmap, 0, pud->pud_fsuid);
366                 if (fsuid == CFS_IDMAP_NOTFOUND) {
367                         CDEBUG(D_SEC, "no mapping for fsuid %u\n",
368                                pud->pud_fsuid);
369                         return -EACCES;
370                 }
371         }
372
373         gid = lustre_idmap_lookup_gid(NULL, idmap, 0, pud->pud_gid);
374         if (gid == CFS_IDMAP_NOTFOUND) {
375                 CDEBUG(D_SEC, "no mapping for gid %u\n", pud->pud_gid);
376                 return -EACCES;
377         }
378
379         if (pud->pud_gid == pud->pud_fsgid) {
380                 fsgid = gid;
381         } else {
382                 fsgid = lustre_idmap_lookup_gid(NULL, idmap, 0, pud->pud_fsgid);
383                 if (fsgid == CFS_IDMAP_NOTFOUND) {
384                         CDEBUG(D_SEC, "no mapping for fsgid %u\n",
385                                pud->pud_fsgid);
386                         return -EACCES;
387                 }
388         }
389
390         pud->pud_uid = uid;
391         pud->pud_gid = gid;
392         pud->pud_fsuid = fsuid;
393         pud->pud_fsgid = fsgid;
394
395         return 0;
396 }
397
398 /*
399  * Reverse mapping
400  */
401 void mdt_body_reverse_idmap(struct mdt_thread_info *info, struct mdt_body *body)
402 {
403         struct ptlrpc_request     *req = mdt_info_req(info);
404         struct md_ucred           *uc = mdt_ucred(info);
405         struct mdt_export_data    *med = mdt_req2med(req);
406         struct lustre_idmap_table *idmap = med->med_idmap;
407
408         if (!exp_connect_rmtclient(info->mti_exp))
409                 return;
410
411         if (body->valid & OBD_MD_FLUID) {
412                 uid_t uid = lustre_idmap_lookup_uid(uc, idmap, 1, body->uid);
413
414                 if (uid == CFS_IDMAP_NOTFOUND) {
415                         uid = NOBODY_UID;
416                         if (body->valid & OBD_MD_FLMODE)
417                                 body->mode = (body->mode & ~S_IRWXU) |
418                                              ((body->mode & S_IRWXO) << 6);
419                 }
420
421                 body->uid = uid;
422         }
423
424         if (body->valid & OBD_MD_FLGID) {
425                 gid_t gid = lustre_idmap_lookup_gid(uc, idmap, 1, body->gid);
426
427                 if (gid == CFS_IDMAP_NOTFOUND) {
428                         gid = NOBODY_GID;
429                         if (body->valid & OBD_MD_FLMODE)
430                                 body->mode = (body->mode & ~S_IRWXG) |
431                                              ((body->mode & S_IRWXO) << 3);
432                 }
433
434                 body->gid = gid;
435         }
436 }
437
438 /* Do not ignore root_squash for non-setattr case. */
439 int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
440 {
441         struct ptlrpc_request     *req = mdt_info_req(info);
442         struct md_ucred           *uc = mdt_ucred(info);
443         struct lu_attr            *attr = &info->mti_attr.ma_attr;
444         struct mdt_export_data    *med = mdt_req2med(req);
445         struct lustre_idmap_table *idmap = med->med_idmap;
446
447         if ((uc->mu_valid != UCRED_OLD) && (uc->mu_valid != UCRED_NEW))
448                 return -EINVAL;
449
450         if (op != REINT_SETATTR) {
451                 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
452                         attr->la_uid = uc->mu_fsuid;
453                 /* for S_ISGID, inherit gid from his parent, such work will be
454                  * done in cmm/mdd layer, here set all cases as uc->mu_fsgid. */
455                 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
456                         attr->la_gid = uc->mu_fsgid;
457         } else if (exp_connect_rmtclient(info->mti_exp)) {
458                 /* NB: -1 case will be handled by mdt_fix_attr() later. */
459                 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1)) {
460                         uid_t uid = lustre_idmap_lookup_uid(uc, idmap, 0,
461                                                             attr->la_uid);
462
463                         if (uid == CFS_IDMAP_NOTFOUND) {
464                                 CDEBUG(D_SEC, "Deny chown to uid %u\n",
465                                        attr->la_uid);
466                                 return -EPERM;
467                         }
468
469                         attr->la_uid = uid;
470                 }
471                 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1)) {
472                         gid_t gid = lustre_idmap_lookup_gid(uc, idmap, 0,
473                                                             attr->la_gid);
474
475                         if (gid == CFS_IDMAP_NOTFOUND) {
476                                 CDEBUG(D_SEC, "Deny chown to gid %u\n",
477                                        attr->la_gid);
478                                 return -EPERM;
479                         }
480
481                         attr->la_gid = gid;
482                 }
483         }
484
485         return 0;
486 }