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