Whamcloud - gitweb
d829beb2739a1b1882144a5862f19d77e34dec71
[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  * Copyright (c) 2012, 2013, Intel Corporation.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/mdt/mdt_idmap.c
37  *
38  * Author: Lai Siyao <lsy@clusterfs.com>
39  * Author: Fan Yong <fanyong@clusterfs.com>
40  */
41
42 #define DEBUG_SUBSYSTEM S_MDS
43
44 #include <linux/module.h>
45 #include <linux/kernel.h>
46 #include <linux/mm.h>
47 #include <linux/kmod.h>
48 #include <linux/string.h>
49 #include <linux/stat.h>
50 #include <linux/errno.h>
51 #include <linux/version.h>
52 #include <linux/unistd.h>
53 #include <asm/uaccess.h>
54 #include <linux/fs.h>
55 #include <linux/stat.h>
56 #include <asm/uaccess.h>
57 #include <linux/slab.h>
58
59 #include <libcfs/libcfs.h>
60 #include <obd.h>
61 #include <obd_class.h>
62 #include <obd_support.h>
63 #include <lustre_net.h>
64 #include <lustre_import.h>
65 #include <lustre_dlm.h>
66 #include <lustre_sec.h>
67 #include <lustre_lib.h>
68
69 #include "mdt_internal.h"
70
71 int mdt_init_idmap(struct tgt_session_info *tsi)
72 {
73         struct ptlrpc_request   *req = tgt_ses_req(tsi);
74         struct mdt_export_data *med = mdt_req2med(req);
75         struct obd_export *exp = req->rq_export;
76         char *client = libcfs_nid2str(req->rq_peer.nid);
77         int rc = 0;
78         ENTRY;
79
80         if (exp_connect_rmtclient(exp)) {
81                 mutex_lock(&med->med_idmap_mutex);
82                 if (!med->med_idmap)
83                         med->med_idmap = lustre_idmap_init();
84                 mutex_unlock(&med->med_idmap_mutex);
85
86                 if (IS_ERR(med->med_idmap)) {
87                         long err = PTR_ERR(med->med_idmap);
88
89                         med->med_idmap = NULL;
90                         CERROR("%s: client %s -> target %s "
91                                "failed to init idmap [%ld]!\n",
92                                tgt_name(tsi->tsi_tgt), client,
93                                tgt_name(tsi->tsi_tgt), err);
94                         RETURN(err);
95                 } else if (!med->med_idmap) {
96                         CERROR("%s: client %s -> target %s "
97                                "failed to init(2) idmap!\n",
98                                tgt_name(tsi->tsi_tgt), client,
99                                tgt_name(tsi->tsi_tgt));
100                         RETURN(-ENOMEM);
101                 }
102
103                 CDEBUG(D_SEC, "%s: client %s -> target %s is remote.\n",
104                         tgt_name(tsi->tsi_tgt), client,
105                         tgt_name(tsi->tsi_tgt));
106                 /* NB, MDS_CONNECT establish root idmap too! */
107                 rc = mdt_handle_idmap(tsi);
108         }
109         RETURN(rc);
110 }
111
112 void mdt_cleanup_idmap(struct mdt_export_data *med)
113 {
114         mutex_lock(&med->med_idmap_mutex);
115         if (med->med_idmap != NULL) {
116                 lustre_idmap_fini(med->med_idmap);
117                 med->med_idmap = NULL;
118         }
119         mutex_unlock(&med->med_idmap_mutex);
120 }
121
122 static inline void mdt_revoke_export_locks(struct obd_export *exp)
123 {
124         /* don't revoke locks during recovery */
125         if (exp->exp_obd->obd_recovering)
126                 return;
127
128         ldlm_revoke_export_locks(exp);
129 }
130
131 int mdt_handle_idmap(struct tgt_session_info *tsi)
132 {
133         struct ptlrpc_request   *req = tgt_ses_req(tsi);
134         struct mdt_device       *mdt = mdt_exp2dev(req->rq_export);
135         struct mdt_export_data *med;
136         struct ptlrpc_user_desc *pud = req->rq_user_desc;
137         struct md_identity *identity;
138         __u32 opc;
139         int rc = 0;
140         ENTRY;
141
142         if (!req->rq_export)
143                 RETURN(0);
144
145         med = mdt_req2med(req);
146         if (!exp_connect_rmtclient(req->rq_export))
147                 RETURN(0);
148
149         opc = lustre_msg_get_opc(req->rq_reqmsg);
150         /* Bypass other opc */
151         if ((opc != SEC_CTX_INIT) && (opc != SEC_CTX_INIT_CONT) &&
152             (opc != SEC_CTX_FINI) && (opc != MDS_CONNECT))
153                 RETURN(0);
154
155         LASSERT(med->med_idmap);
156
157         if (unlikely(!pud)) {
158                 CDEBUG(D_SEC, "remote client must run with rq_user_desc "
159                        "present\n");
160                 RETURN(-EACCES);
161         }
162
163         if (!uid_valid(make_kuid(&init_user_ns, req->rq_auth_mapped_uid))) {
164                 CDEBUG(D_SEC, "invalid authorized mapped uid, please check "
165                        "/etc/lustre/idmap.conf!\n");
166                 RETURN(-EACCES);
167         }
168
169         if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
170                 CDEBUG(D_SEC, "remote client must run with identity_get "
171                        "enabled!\n");
172                 RETURN(-EACCES);
173         }
174
175         identity = mdt_identity_get(mdt->mdt_identity_cache,
176                                     req->rq_auth_mapped_uid);
177         if (IS_ERR(identity)) {
178                 CDEBUG(D_SEC, "can't get mdt identity(%u), no mapping added\n",
179                        req->rq_auth_mapped_uid);
180                 RETURN(-EACCES);
181         }
182
183         switch (opc) {
184                 case SEC_CTX_INIT:
185                 case SEC_CTX_INIT_CONT:
186                 case MDS_CONNECT:
187                         rc = lustre_idmap_add(med->med_idmap,
188                                               pud->pud_uid, identity->mi_uid,
189                                               pud->pud_gid, identity->mi_gid);
190                         break;
191                 case SEC_CTX_FINI:
192                         rc = lustre_idmap_del(med->med_idmap,
193                                               pud->pud_uid, identity->mi_uid,
194                                               pud->pud_gid, identity->mi_gid);
195                         break;
196         }
197
198         mdt_identity_put(mdt->mdt_identity_cache, identity);
199
200         if (rc)
201                 RETURN(rc);
202
203         switch (opc) {
204                 case SEC_CTX_INIT:
205                 case SEC_CTX_INIT_CONT:
206                 case SEC_CTX_FINI:
207                         mdt_revoke_export_locks(req->rq_export);
208                         break;
209         }
210
211         RETURN(0);
212 }
213
214 int ptlrpc_user_desc_do_idmap(struct ptlrpc_request *req,
215                               struct ptlrpc_user_desc *pud)
216 {
217         struct mdt_export_data    *med = mdt_req2med(req);
218         struct lustre_idmap_table *idmap = med->med_idmap;
219         uid_t uid, fsuid;
220         gid_t gid, fsgid;
221
222         /* Only remote client need desc_to_idmap. */
223         if (!exp_connect_rmtclient(req->rq_export))
224                 return 0;
225
226         uid = lustre_idmap_lookup_uid(NULL, idmap, 0, pud->pud_uid);
227         if (uid == CFS_IDMAP_NOTFOUND) {
228                 CDEBUG(D_SEC, "no mapping for uid %u\n", pud->pud_uid);
229                 return -EACCES;
230         }
231
232         if (pud->pud_uid == pud->pud_fsuid) {
233                 fsuid = uid;
234         } else {
235                 fsuid = lustre_idmap_lookup_uid(NULL, idmap, 0, pud->pud_fsuid);
236                 if (fsuid == CFS_IDMAP_NOTFOUND) {
237                         CDEBUG(D_SEC, "no mapping for fsuid %u\n",
238                                pud->pud_fsuid);
239                         return -EACCES;
240                 }
241         }
242
243         gid = lustre_idmap_lookup_gid(NULL, idmap, 0, pud->pud_gid);
244         if (gid == CFS_IDMAP_NOTFOUND) {
245                 CDEBUG(D_SEC, "no mapping for gid %u\n", pud->pud_gid);
246                 return -EACCES;
247         }
248
249         if (pud->pud_gid == pud->pud_fsgid) {
250                 fsgid = gid;
251         } else {
252                 fsgid = lustre_idmap_lookup_gid(NULL, idmap, 0, pud->pud_fsgid);
253                 if (fsgid == CFS_IDMAP_NOTFOUND) {
254                         CDEBUG(D_SEC, "no mapping for fsgid %u\n",
255                                pud->pud_fsgid);
256                         return -EACCES;
257                 }
258         }
259
260         pud->pud_uid = uid;
261         pud->pud_gid = gid;
262         pud->pud_fsuid = fsuid;
263         pud->pud_fsgid = fsgid;
264
265         return 0;
266 }
267
268 /*
269  * Reverse mapping
270  */
271 void mdt_body_reverse_idmap(struct mdt_thread_info *info, struct mdt_body *body)
272 {
273         struct ptlrpc_request     *req = mdt_info_req(info);
274         struct lu_ucred           *uc = mdt_ucred(info);
275         struct mdt_export_data    *med = mdt_req2med(req);
276         struct lustre_idmap_table *idmap = med->med_idmap;
277
278         if (!exp_connect_rmtclient(info->mti_exp))
279                 return;
280
281         if (body->mbo_valid & OBD_MD_FLUID) {
282                 uid_t uid;
283
284                 uid = lustre_idmap_lookup_uid(uc, idmap, 1, body->mbo_uid);
285
286                 if (uid == CFS_IDMAP_NOTFOUND) {
287                         uid = NOBODY_UID;
288                         if (body->mbo_valid & OBD_MD_FLMODE)
289                                 body->mbo_mode = (body->mbo_mode & ~S_IRWXU) |
290                                              ((body->mbo_mode & S_IRWXO) << 6);
291                 }
292
293                 body->mbo_uid = uid;
294         }
295
296         if (body->mbo_valid & OBD_MD_FLGID) {
297                 gid_t gid;
298
299                 gid = lustre_idmap_lookup_gid(uc, idmap, 1, body->mbo_gid);
300
301                 if (gid == CFS_IDMAP_NOTFOUND) {
302                         gid = NOBODY_GID;
303                         if (body->mbo_valid & OBD_MD_FLMODE)
304                                 body->mbo_mode = (body->mbo_mode & ~S_IRWXG) |
305                                              ((body->mbo_mode & S_IRWXO) << 3);
306                 }
307
308                 body->mbo_gid = gid;
309         }
310 }
311
312 /* Do not ignore root_squash for non-setattr case. */
313 int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
314 {
315         struct ptlrpc_request     *req = mdt_info_req(info);
316         struct lu_ucred           *uc = mdt_ucred_check(info);
317         struct lu_attr            *attr = &info->mti_attr.ma_attr;
318         struct mdt_export_data    *med = mdt_req2med(req);
319         struct lustre_idmap_table *idmap = med->med_idmap;
320
321         if (uc == NULL)
322                 return -EINVAL;
323
324         if (op != REINT_SETATTR) {
325                 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
326                         attr->la_uid = uc->uc_fsuid;
327                 /* for S_ISGID, inherit gid from his parent, such work will be
328                  * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
329                 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
330                         attr->la_gid = uc->uc_fsgid;
331         } else if (exp_connect_rmtclient(info->mti_exp)) {
332                 /* NB: -1 case will be handled by mdt_fix_attr() later. */
333                 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1)) {
334                         uid_t uid = lustre_idmap_lookup_uid(uc, idmap, 0,
335                                                             attr->la_uid);
336
337                         if (uid == CFS_IDMAP_NOTFOUND) {
338                                 CDEBUG(D_SEC, "Deny chown to uid %u\n",
339                                        attr->la_uid);
340                                 return -EPERM;
341                         }
342
343                         attr->la_uid = uid;
344                 }
345                 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1)) {
346                         gid_t gid = lustre_idmap_lookup_gid(uc, idmap, 0,
347                                                             attr->la_gid);
348
349                         if (gid == CFS_IDMAP_NOTFOUND) {
350                                 CDEBUG(D_SEC, "Deny chown to gid %u\n",
351                                        attr->la_gid);
352                                 return -EPERM;
353                         }
354
355                         attr->la_gid = gid;
356                 }
357         }
358
359         return 0;
360 }