4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
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.
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).
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
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
27 * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28 * Use is subject to license terms.
30 * Copyright (c) 2012, Intel Corporation.
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>
42 #define DEBUG_SUBSYSTEM S_MDS
44 #include <linux/module.h>
45 #include <linux/kernel.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>
55 #include <linux/stat.h>
56 #include <asm/uaccess.h>
57 #include <linux/slab.h>
59 #include <libcfs/libcfs.h>
60 #include <libcfs/lucache.h>
62 #include <obd_class.h>
63 #include <obd_support.h>
64 #include <lustre_net.h>
65 #include <lustre_import.h>
66 #include <lustre_dlm.h>
67 #include <lustre_sec.h>
68 #include <lustre_lib.h>
70 #include "mdt_internal.h"
72 #define mdt_init_sec_none(reply, exp) \
74 reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT | \
75 OBD_CONNECT_RMT_CLIENT_FORCE | \
76 OBD_CONNECT_MDS_CAPA | \
77 OBD_CONNECT_OSS_CAPA); \
80 int mdt_init_sec_level(struct mdt_thread_info *info)
82 struct mdt_device *mdt = info->mti_mdt;
83 struct ptlrpc_request *req = mdt_info_req(info);
84 char *client = libcfs_nid2str(req->rq_peer.nid);
85 struct obd_export *exp = req->rq_export;
86 struct obd_device *obd = exp->exp_obd;
87 struct obd_connect_data *data, *reply;
91 data = req_capsule_client_get(info->mti_pill, &RMF_CONNECT_DATA);
92 reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
93 if (data == NULL || reply == NULL)
96 /* connection from MDT is always trusted */
97 if (req->rq_auth_usr_mdt) {
98 mdt_init_sec_none(reply, exp);
102 /* no GSS support case */
103 if (!req->rq_auth_gss) {
104 if (mdt->mdt_sec_level > LUSTRE_SEC_NONE) {
105 CWARN("client %s -> target %s does not user GSS, "
106 "can not run under security level %d.\n",
107 client, obd->obd_name, mdt->mdt_sec_level);
110 mdt_init_sec_none(reply, exp);
115 /* old version case */
116 if (unlikely(!(data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT) ||
117 !(data->ocd_connect_flags & OBD_CONNECT_MDS_CAPA) ||
118 !(data->ocd_connect_flags & OBD_CONNECT_OSS_CAPA))) {
119 if (mdt->mdt_sec_level > LUSTRE_SEC_NONE) {
120 CWARN("client %s -> target %s uses old version, "
121 "can not run under security level %d.\n",
122 client, obd->obd_name, mdt->mdt_sec_level);
125 CWARN("client %s -> target %s uses old version, "
126 "run under security level %d.\n",
127 client, obd->obd_name, mdt->mdt_sec_level);
128 mdt_init_sec_none(reply, exp);
133 remote = data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT_FORCE;
135 if (!req->rq_auth_remote)
136 CDEBUG(D_SEC, "client (local realm) %s -> target %s "
137 "asked to be remote.\n", client, obd->obd_name);
138 } else if (req->rq_auth_remote) {
140 CDEBUG(D_SEC, "client (remote realm) %s -> target %s is set "
141 "as remote by default.\n", client, obd->obd_name);
145 if (!mdt->mdt_opts.mo_oss_capa) {
146 CDEBUG(D_SEC, "client %s -> target %s is set as remote,"
147 " but OSS capabilities are not enabled: %d.\n",
148 client, obd->obd_name, mdt->mdt_opts.mo_oss_capa);
152 if (req->rq_auth_uid == INVALID_UID) {
153 CDEBUG(D_SEC, "client %s -> target %s: user is not "
154 "authenticated!\n", client, obd->obd_name);
159 switch (mdt->mdt_sec_level) {
160 case LUSTRE_SEC_NONE:
162 mdt_init_sec_none(reply, exp);
165 CDEBUG(D_SEC, "client %s -> target %s is set as remote, "
166 "can not run under security level %d.\n",
167 client, obd->obd_name, mdt->mdt_sec_level);
170 case LUSTRE_SEC_REMOTE:
172 mdt_init_sec_none(reply, exp);
176 reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT |
177 OBD_CONNECT_RMT_CLIENT_FORCE);
178 if (!mdt->mdt_opts.mo_mds_capa)
179 reply->ocd_connect_flags &= ~OBD_CONNECT_MDS_CAPA;
180 if (!mdt->mdt_opts.mo_oss_capa)
181 reply->ocd_connect_flags &= ~OBD_CONNECT_OSS_CAPA;
191 int mdt_init_idmap(struct mdt_thread_info *info)
193 struct ptlrpc_request *req = mdt_info_req(info);
194 struct mdt_export_data *med = mdt_req2med(req);
195 struct obd_export *exp = req->rq_export;
196 char *client = libcfs_nid2str(req->rq_peer.nid);
197 struct obd_device *obd = exp->exp_obd;
201 if (exp_connect_rmtclient(exp)) {
202 mutex_lock(&med->med_idmap_mutex);
204 med->med_idmap = lustre_idmap_init();
205 mutex_unlock(&med->med_idmap_mutex);
207 if (IS_ERR(med->med_idmap)) {
208 long err = PTR_ERR(med->med_idmap);
210 med->med_idmap = NULL;
211 CERROR("client %s -> target %s "
212 "failed to init idmap [%ld]!\n",
213 client, obd->obd_name, err);
215 } else if (!med->med_idmap) {
216 CERROR("client %s -> target %s "
217 "failed to init(2) idmap!\n",
218 client, obd->obd_name);
222 CDEBUG(D_SEC, "client %s -> target %s is remote.\n",
223 client, obd->obd_name);
224 /* NB, MDS_CONNECT establish root idmap too! */
225 rc = mdt_handle_idmap(info);
230 void mdt_cleanup_idmap(struct mdt_export_data *med)
232 mutex_lock(&med->med_idmap_mutex);
233 if (med->med_idmap != NULL) {
234 lustre_idmap_fini(med->med_idmap);
235 med->med_idmap = NULL;
237 mutex_unlock(&med->med_idmap_mutex);
240 static inline void mdt_revoke_export_locks(struct obd_export *exp)
242 /* don't revoke locks during recovery */
243 if (exp->exp_obd->obd_recovering)
246 ldlm_revoke_export_locks(exp);
249 int mdt_handle_idmap(struct mdt_thread_info *info)
251 struct ptlrpc_request *req = mdt_info_req(info);
252 struct mdt_device *mdt = info->mti_mdt;
253 struct mdt_export_data *med;
254 struct ptlrpc_user_desc *pud = req->rq_user_desc;
255 struct md_identity *identity;
263 med = mdt_req2med(req);
264 if (!exp_connect_rmtclient(info->mti_exp))
267 opc = lustre_msg_get_opc(req->rq_reqmsg);
268 /* Bypass other opc */
269 if ((opc != SEC_CTX_INIT) && (opc != SEC_CTX_INIT_CONT) &&
270 (opc != SEC_CTX_FINI) && (opc != MDS_CONNECT))
273 LASSERT(med->med_idmap);
275 if (unlikely(!pud)) {
276 CDEBUG(D_SEC, "remote client must run with rq_user_desc "
281 if (req->rq_auth_mapped_uid == INVALID_UID) {
282 CDEBUG(D_SEC, "invalid authorized mapped uid, please check "
283 "/etc/lustre/idmap.conf!\n");
287 if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
288 CDEBUG(D_SEC, "remote client must run with identity_get "
293 identity = mdt_identity_get(mdt->mdt_identity_cache,
294 req->rq_auth_mapped_uid);
295 if (IS_ERR(identity)) {
296 CDEBUG(D_SEC, "can't get mdt identity(%u), no mapping added\n",
297 req->rq_auth_mapped_uid);
303 case SEC_CTX_INIT_CONT:
305 rc = lustre_idmap_add(med->med_idmap,
306 pud->pud_uid, identity->mi_uid,
307 pud->pud_gid, identity->mi_gid);
310 rc = lustre_idmap_del(med->med_idmap,
311 pud->pud_uid, identity->mi_uid,
312 pud->pud_gid, identity->mi_gid);
316 mdt_identity_put(mdt->mdt_identity_cache, identity);
323 case SEC_CTX_INIT_CONT:
325 mdt_revoke_export_locks(req->rq_export);
332 int ptlrpc_user_desc_do_idmap(struct ptlrpc_request *req,
333 struct ptlrpc_user_desc *pud)
335 struct mdt_export_data *med = mdt_req2med(req);
336 struct lustre_idmap_table *idmap = med->med_idmap;
340 /* Only remote client need desc_to_idmap. */
341 if (!exp_connect_rmtclient(req->rq_export))
344 uid = lustre_idmap_lookup_uid(NULL, idmap, 0, pud->pud_uid);
345 if (uid == CFS_IDMAP_NOTFOUND) {
346 CDEBUG(D_SEC, "no mapping for uid %u\n", pud->pud_uid);
350 if (pud->pud_uid == pud->pud_fsuid) {
353 fsuid = lustre_idmap_lookup_uid(NULL, idmap, 0, pud->pud_fsuid);
354 if (fsuid == CFS_IDMAP_NOTFOUND) {
355 CDEBUG(D_SEC, "no mapping for fsuid %u\n",
361 gid = lustre_idmap_lookup_gid(NULL, idmap, 0, pud->pud_gid);
362 if (gid == CFS_IDMAP_NOTFOUND) {
363 CDEBUG(D_SEC, "no mapping for gid %u\n", pud->pud_gid);
367 if (pud->pud_gid == pud->pud_fsgid) {
370 fsgid = lustre_idmap_lookup_gid(NULL, idmap, 0, pud->pud_fsgid);
371 if (fsgid == CFS_IDMAP_NOTFOUND) {
372 CDEBUG(D_SEC, "no mapping for fsgid %u\n",
380 pud->pud_fsuid = fsuid;
381 pud->pud_fsgid = fsgid;
389 void mdt_body_reverse_idmap(struct mdt_thread_info *info, struct mdt_body *body)
391 struct ptlrpc_request *req = mdt_info_req(info);
392 struct lu_ucred *uc = mdt_ucred(info);
393 struct mdt_export_data *med = mdt_req2med(req);
394 struct lustre_idmap_table *idmap = med->med_idmap;
396 if (!exp_connect_rmtclient(info->mti_exp))
399 if (body->valid & OBD_MD_FLUID) {
400 uid_t uid = lustre_idmap_lookup_uid(uc, idmap, 1, body->uid);
402 if (uid == CFS_IDMAP_NOTFOUND) {
404 if (body->valid & OBD_MD_FLMODE)
405 body->mode = (body->mode & ~S_IRWXU) |
406 ((body->mode & S_IRWXO) << 6);
412 if (body->valid & OBD_MD_FLGID) {
413 gid_t gid = lustre_idmap_lookup_gid(uc, idmap, 1, body->gid);
415 if (gid == CFS_IDMAP_NOTFOUND) {
417 if (body->valid & OBD_MD_FLMODE)
418 body->mode = (body->mode & ~S_IRWXG) |
419 ((body->mode & S_IRWXO) << 3);
426 /* Do not ignore root_squash for non-setattr case. */
427 int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
429 struct ptlrpc_request *req = mdt_info_req(info);
430 struct lu_ucred *uc = mdt_ucred_check(info);
431 struct lu_attr *attr = &info->mti_attr.ma_attr;
432 struct mdt_export_data *med = mdt_req2med(req);
433 struct lustre_idmap_table *idmap = med->med_idmap;
438 if (op != REINT_SETATTR) {
439 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
440 attr->la_uid = uc->uc_fsuid;
441 /* for S_ISGID, inherit gid from his parent, such work will be
442 * done in cmm/mdd layer, here set all cases as uc->uc_fsgid. */
443 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
444 attr->la_gid = uc->uc_fsgid;
445 } else if (exp_connect_rmtclient(info->mti_exp)) {
446 /* NB: -1 case will be handled by mdt_fix_attr() later. */
447 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1)) {
448 uid_t uid = lustre_idmap_lookup_uid(uc, idmap, 0,
451 if (uid == CFS_IDMAP_NOTFOUND) {
452 CDEBUG(D_SEC, "Deny chown to uid %u\n",
459 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1)) {
460 gid_t gid = lustre_idmap_lookup_gid(uc, idmap, 0,
463 if (gid == CFS_IDMAP_NOTFOUND) {
464 CDEBUG(D_SEC, "Deny chown to gid %u\n",