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