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