1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
6 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 only,
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License version 2 for more details (a copy is included
16 * in the LICENSE file that accompanied this code).
18 * You should have received a copy of the GNU General Public License
19 * version 2 along with this program; If not, see
20 * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
22 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
23 * CA 95054 USA or visit www.sun.com if you need additional information or
29 * Copyright 2008 Sun Microsystems, Inc. All rights reserved
30 * Use is subject to license terms.
33 * This file is part of Lustre, http://www.lustre.org/
34 * Lustre is a trademark of Sun Microsystems, Inc.
36 * lustre/mdt/mdt_idmap.c
38 * Author: Lai Siyao <lsy@clusterfs.com>
39 * Author: Fan Yong <fanyong@clusterfs.com>
45 #define DEBUG_SUBSYSTEM S_MDS
47 #ifndef AUTOCONF_INCLUDED
48 #include <linux/config.h>
50 #include <linux/module.h>
51 #include <linux/kernel.h>
53 #include <linux/kmod.h>
54 #include <linux/string.h>
55 #include <linux/stat.h>
56 #include <linux/errno.h>
57 #include <linux/version.h>
58 #include <linux/unistd.h>
59 #include <asm/system.h>
60 #include <asm/uaccess.h>
62 #include <linux/stat.h>
63 #include <asm/uaccess.h>
64 #include <linux/slab.h>
66 #include <libcfs/libcfs.h>
68 #include <obd_class.h>
69 #include <obd_support.h>
70 #include <lustre_net.h>
71 #include <lustre_import.h>
72 #include <lustre_dlm.h>
73 #include <lustre_sec.h>
74 #include <lustre_lib.h>
75 #include <lustre_ucache.h>
77 #include "mdt_internal.h"
79 #define mdt_init_sec_none(reply, exp) \
81 reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT | \
82 OBD_CONNECT_RMT_CLIENT_FORCE | \
83 OBD_CONNECT_MDS_CAPA | \
84 OBD_CONNECT_OSS_CAPA); \
85 spin_lock(&exp->exp_lock); \
86 exp->exp_connect_flags = reply->ocd_connect_flags; \
87 spin_unlock(&exp->exp_lock); \
90 int mdt_init_sec_level(struct mdt_thread_info *info)
92 struct mdt_device *mdt = info->mti_mdt;
93 struct ptlrpc_request *req = mdt_info_req(info);
94 char *client = libcfs_nid2str(req->rq_peer.nid);
95 struct obd_export *exp = req->rq_export;
96 struct obd_device *obd = exp->exp_obd;
97 struct obd_connect_data *data, *reply;
101 data = req_capsule_client_get(info->mti_pill, &RMF_CONNECT_DATA);
102 reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
103 if (data == NULL || reply == NULL)
106 /* connection from MDT is always trusted */
107 if (req->rq_auth_usr_mdt) {
108 mdt_init_sec_none(reply, exp);
112 /* no GSS support case */
113 if (!req->rq_auth_gss) {
114 if (mdt->mdt_sec_level > LUSTRE_SEC_NONE) {
115 CWARN("client %s -> target %s does not user GSS, "
116 "can not run under security level %d.\n",
117 client, obd->obd_name, mdt->mdt_sec_level);
120 mdt_init_sec_none(reply, exp);
125 /* old version case */
126 if (unlikely(!(data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT) ||
127 !(data->ocd_connect_flags & OBD_CONNECT_MDS_CAPA) ||
128 !(data->ocd_connect_flags & OBD_CONNECT_OSS_CAPA))) {
129 if (mdt->mdt_sec_level > LUSTRE_SEC_NONE) {
130 CWARN("client %s -> target %s uses old version, "
131 "can not run under security level %d.\n",
132 client, obd->obd_name, mdt->mdt_sec_level);
135 CWARN("client %s -> target %s uses old version, "
136 "run under security level %d.\n",
137 client, obd->obd_name, mdt->mdt_sec_level);
138 mdt_init_sec_none(reply, exp);
143 remote = data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT_FORCE;
145 if (!req->rq_auth_remote)
146 CDEBUG(D_SEC, "client (local realm) %s -> target %s "
147 "asked to be remote.\n", client, obd->obd_name);
148 } else if (req->rq_auth_remote) {
150 CDEBUG(D_SEC, "client (remote realm) %s -> target %s is set "
151 "as remote by default.\n", client, obd->obd_name);
155 if (!mdt->mdt_opts.mo_oss_capa) {
156 CDEBUG(D_SEC, "client %s -> target %s is set as remote,"
157 " but OSS capabilities are not enabled: %d.\n",
158 client, obd->obd_name, mdt->mdt_opts.mo_oss_capa);
162 if (req->rq_auth_uid == INVALID_UID) {
163 CDEBUG(D_SEC, "client %s -> target %s: user is not "
164 "authenticated!\n", client, obd->obd_name);
169 switch (mdt->mdt_sec_level) {
170 case LUSTRE_SEC_NONE:
172 mdt_init_sec_none(reply, exp);
175 CDEBUG(D_SEC, "client %s -> target %s is set as remote, "
176 "can not run under security level %d.\n",
177 client, obd->obd_name, mdt->mdt_sec_level);
180 case LUSTRE_SEC_REMOTE:
182 mdt_init_sec_none(reply, exp);
186 reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT |
187 OBD_CONNECT_RMT_CLIENT_FORCE);
188 if (!mdt->mdt_opts.mo_mds_capa)
189 reply->ocd_connect_flags &= ~OBD_CONNECT_MDS_CAPA;
190 if (!mdt->mdt_opts.mo_oss_capa)
191 reply->ocd_connect_flags &= ~OBD_CONNECT_OSS_CAPA;
193 spin_lock(&exp->exp_lock);
194 exp->exp_connect_flags = reply->ocd_connect_flags;
195 spin_unlock(&exp->exp_lock);
205 int mdt_init_idmap(struct mdt_thread_info *info)
207 struct ptlrpc_request *req = mdt_info_req(info);
208 struct mdt_export_data *med = mdt_req2med(req);
209 struct obd_export *exp = req->rq_export;
210 char *client = libcfs_nid2str(req->rq_peer.nid);
211 struct obd_device *obd = exp->exp_obd;
215 if (exp_connect_rmtclient(exp)) {
216 down(&med->med_idmap_sem);
218 med->med_idmap = lustre_idmap_init();
219 up(&med->med_idmap_sem);
221 if (IS_ERR(med->med_idmap)) {
222 long err = PTR_ERR(med->med_idmap);
224 med->med_idmap = NULL;
225 CERROR("client %s -> target %s "
226 "failed to init idmap [%ld]!\n",
227 client, obd->obd_name, err);
229 } else if (!med->med_idmap) {
230 CERROR("client %s -> target %s "
231 "failed to init(2) idmap!\n",
232 client, obd->obd_name);
236 CDEBUG(D_SEC, "client %s -> target %s is remote.\n",
237 client, obd->obd_name);
238 /* NB, MDS_CONNECT establish root idmap too! */
239 rc = mdt_handle_idmap(info);
244 void mdt_cleanup_idmap(struct mdt_export_data *med)
246 down(&med->med_idmap_sem);
247 if (med->med_idmap != NULL) {
248 lustre_idmap_fini(med->med_idmap);
249 med->med_idmap = NULL;
251 up(&med->med_idmap_sem);
254 static inline void mdt_revoke_export_locks(struct obd_export *exp)
256 /* don't revoke locks during recovery */
257 if (exp->exp_obd->obd_recovering)
260 ldlm_revoke_export_locks(exp);
263 int mdt_handle_idmap(struct mdt_thread_info *info)
265 struct ptlrpc_request *req = mdt_info_req(info);
266 struct mdt_device *mdt = info->mti_mdt;
267 struct mdt_export_data *med;
268 struct ptlrpc_user_desc *pud = req->rq_user_desc;
269 struct md_identity *identity;
277 med = mdt_req2med(req);
278 if (!exp_connect_rmtclient(info->mti_exp))
281 opc = lustre_msg_get_opc(req->rq_reqmsg);
282 /* Bypass other opc */
283 if ((opc != SEC_CTX_INIT) && (opc != SEC_CTX_INIT_CONT) &&
284 (opc != SEC_CTX_FINI) && (opc != MDS_CONNECT))
287 LASSERT(med->med_idmap);
289 if (unlikely(!pud)) {
290 CDEBUG(D_SEC, "remote client must run with rq_user_desc "
295 if (req->rq_auth_mapped_uid == INVALID_UID) {
296 CDEBUG(D_SEC, "invalid authorized mapped uid, please check "
297 "/etc/lustre/idmap.conf!\n");
301 if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
302 CDEBUG(D_SEC, "remote client must run with identity_get "
307 identity = mdt_identity_get(mdt->mdt_identity_cache,
308 req->rq_auth_mapped_uid);
309 if (IS_ERR(identity)) {
310 CDEBUG(D_SEC, "can't get mdt identity(%u), no mapping added\n",
311 req->rq_auth_mapped_uid);
317 case SEC_CTX_INIT_CONT:
319 rc = lustre_idmap_add(med->med_idmap,
320 pud->pud_uid, identity->mi_uid,
321 pud->pud_gid, identity->mi_gid);
324 rc = lustre_idmap_del(med->med_idmap,
325 pud->pud_uid, identity->mi_uid,
326 pud->pud_gid, identity->mi_gid);
330 mdt_identity_put(mdt->mdt_identity_cache, identity);
337 case SEC_CTX_INIT_CONT:
339 mdt_revoke_export_locks(req->rq_export);
346 int ptlrpc_user_desc_do_idmap(struct ptlrpc_request *req,
347 struct ptlrpc_user_desc *pud)
349 struct mdt_export_data *med = mdt_req2med(req);
350 struct lustre_idmap_table *idmap = med->med_idmap;
354 /* Only remote client need desc_to_idmap. */
355 if (!exp_connect_rmtclient(req->rq_export))
358 uid = lustre_idmap_lookup_uid(NULL, idmap, 0, pud->pud_uid);
359 if (uid == CFS_IDMAP_NOTFOUND) {
360 CDEBUG(D_SEC, "no mapping for uid %u\n", pud->pud_uid);
364 if (pud->pud_uid == pud->pud_fsuid) {
367 fsuid = lustre_idmap_lookup_uid(NULL, idmap, 0, pud->pud_fsuid);
368 if (fsuid == CFS_IDMAP_NOTFOUND) {
369 CDEBUG(D_SEC, "no mapping for fsuid %u\n",
375 gid = lustre_idmap_lookup_gid(NULL, idmap, 0, pud->pud_gid);
376 if (gid == CFS_IDMAP_NOTFOUND) {
377 CDEBUG(D_SEC, "no mapping for gid %u\n", pud->pud_gid);
381 if (pud->pud_gid == pud->pud_fsgid) {
384 fsgid = lustre_idmap_lookup_gid(NULL, idmap, 0, pud->pud_fsgid);
385 if (fsgid == CFS_IDMAP_NOTFOUND) {
386 CDEBUG(D_SEC, "no mapping for fsgid %u\n",
394 pud->pud_fsuid = fsuid;
395 pud->pud_fsgid = fsgid;
403 void mdt_body_reverse_idmap(struct mdt_thread_info *info, struct mdt_body *body)
405 struct ptlrpc_request *req = mdt_info_req(info);
406 struct md_ucred *uc = mdt_ucred(info);
407 struct mdt_export_data *med = mdt_req2med(req);
408 struct lustre_idmap_table *idmap = med->med_idmap;
410 if (!exp_connect_rmtclient(info->mti_exp))
413 if (body->valid & OBD_MD_FLUID) {
414 uid_t uid = lustre_idmap_lookup_uid(uc, idmap, 1, body->uid);
416 if (uid == CFS_IDMAP_NOTFOUND) {
418 if (body->valid & OBD_MD_FLMODE)
419 body->mode = (body->mode & ~S_IRWXU) |
420 ((body->mode & S_IRWXO) << 6);
426 if (body->valid & OBD_MD_FLGID) {
427 gid_t gid = lustre_idmap_lookup_gid(uc, idmap, 1, body->gid);
429 if (gid == CFS_IDMAP_NOTFOUND) {
431 if (body->valid & OBD_MD_FLMODE)
432 body->mode = (body->mode & ~S_IRWXG) |
433 ((body->mode & S_IRWXO) << 3);
440 /* Do not ignore root_squash for non-setattr case. */
441 int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
443 struct ptlrpc_request *req = mdt_info_req(info);
444 struct md_ucred *uc = mdt_ucred(info);
445 struct lu_attr *attr = &info->mti_attr.ma_attr;
446 struct mdt_export_data *med = mdt_req2med(req);
447 struct lustre_idmap_table *idmap = med->med_idmap;
449 if ((uc->mu_valid != UCRED_OLD) && (uc->mu_valid != UCRED_NEW))
452 if (op != REINT_SETATTR) {
453 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
454 attr->la_uid = uc->mu_fsuid;
455 /* for S_ISGID, inherit gid from his parent, such work will be
456 * done in cmm/mdd layer, here set all cases as uc->mu_fsgid. */
457 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
458 attr->la_gid = uc->mu_fsgid;
459 } else if (exp_connect_rmtclient(info->mti_exp)) {
460 /* NB: -1 case will be handled by mdt_fix_attr() later. */
461 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1)) {
462 uid_t uid = lustre_idmap_lookup_uid(uc, idmap, 0,
465 if (uid == CFS_IDMAP_NOTFOUND) {
466 CDEBUG(D_SEC, "Deny chown to uid %u\n",
473 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1)) {
474 gid_t gid = lustre_idmap_lookup_gid(uc, idmap, 0,
477 if (gid == CFS_IDMAP_NOTFOUND) {
478 CDEBUG(D_SEC, "Deny chown to gid %u\n",