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