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.
31 * This file is part of Lustre, http://www.lustre.org/
32 * Lustre is a trademark of Sun Microsystems, Inc.
34 * lustre/mdt/mdt_idmap.c
36 * Author: Lai Siyao <lsy@clusterfs.com>
37 * Author: Fan Yong <fanyong@clusterfs.com>
40 #define DEBUG_SUBSYSTEM S_MDS
42 #include <linux/module.h>
43 #include <linux/kernel.h>
45 #include <linux/kmod.h>
46 #include <linux/string.h>
47 #include <linux/stat.h>
48 #include <linux/errno.h>
49 #include <linux/version.h>
50 #include <linux/unistd.h>
51 #include <asm/uaccess.h>
53 #include <linux/stat.h>
54 #include <asm/uaccess.h>
55 #include <linux/slab.h>
57 #include <libcfs/libcfs.h>
58 #include <libcfs/lucache.h>
60 #include <obd_class.h>
61 #include <obd_support.h>
62 #include <lustre_net.h>
63 #include <lustre_import.h>
64 #include <lustre_dlm.h>
65 #include <lustre_sec.h>
66 #include <lustre_lib.h>
68 #include "mdt_internal.h"
70 #define mdt_init_sec_none(reply, exp) \
72 reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT | \
73 OBD_CONNECT_RMT_CLIENT_FORCE | \
74 OBD_CONNECT_MDS_CAPA | \
75 OBD_CONNECT_OSS_CAPA); \
78 int mdt_init_sec_level(struct mdt_thread_info *info)
80 struct mdt_device *mdt = info->mti_mdt;
81 struct ptlrpc_request *req = mdt_info_req(info);
82 char *client = libcfs_nid2str(req->rq_peer.nid);
83 struct obd_export *exp = req->rq_export;
84 struct obd_device *obd = exp->exp_obd;
85 struct obd_connect_data *data, *reply;
89 data = req_capsule_client_get(info->mti_pill, &RMF_CONNECT_DATA);
90 reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
91 if (data == NULL || reply == NULL)
94 /* connection from MDT is always trusted */
95 if (req->rq_auth_usr_mdt) {
96 mdt_init_sec_none(reply, exp);
100 /* no GSS support case */
101 if (!req->rq_auth_gss) {
102 if (mdt->mdt_sec_level > LUSTRE_SEC_NONE) {
103 CWARN("client %s -> target %s does not user GSS, "
104 "can not run under security level %d.\n",
105 client, obd->obd_name, mdt->mdt_sec_level);
108 mdt_init_sec_none(reply, exp);
113 /* old version case */
114 if (unlikely(!(data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT) ||
115 !(data->ocd_connect_flags & OBD_CONNECT_MDS_CAPA) ||
116 !(data->ocd_connect_flags & OBD_CONNECT_OSS_CAPA))) {
117 if (mdt->mdt_sec_level > LUSTRE_SEC_NONE) {
118 CWARN("client %s -> target %s uses old version, "
119 "can not run under security level %d.\n",
120 client, obd->obd_name, mdt->mdt_sec_level);
123 CWARN("client %s -> target %s uses old version, "
124 "run under security level %d.\n",
125 client, obd->obd_name, mdt->mdt_sec_level);
126 mdt_init_sec_none(reply, exp);
131 remote = data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT_FORCE;
133 if (!req->rq_auth_remote)
134 CDEBUG(D_SEC, "client (local realm) %s -> target %s "
135 "asked to be remote.\n", client, obd->obd_name);
136 } else if (req->rq_auth_remote) {
138 CDEBUG(D_SEC, "client (remote realm) %s -> target %s is set "
139 "as remote by default.\n", client, obd->obd_name);
143 if (!mdt->mdt_opts.mo_oss_capa) {
144 CDEBUG(D_SEC, "client %s -> target %s is set as remote,"
145 " but OSS capabilities are not enabled: %d.\n",
146 client, obd->obd_name, mdt->mdt_opts.mo_oss_capa);
150 if (req->rq_auth_uid == INVALID_UID) {
151 CDEBUG(D_SEC, "client %s -> target %s: user is not "
152 "authenticated!\n", client, obd->obd_name);
157 switch (mdt->mdt_sec_level) {
158 case LUSTRE_SEC_NONE:
160 mdt_init_sec_none(reply, exp);
163 CDEBUG(D_SEC, "client %s -> target %s is set as remote, "
164 "can not run under security level %d.\n",
165 client, obd->obd_name, mdt->mdt_sec_level);
168 case LUSTRE_SEC_REMOTE:
170 mdt_init_sec_none(reply, exp);
174 reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT |
175 OBD_CONNECT_RMT_CLIENT_FORCE);
176 if (!mdt->mdt_opts.mo_mds_capa)
177 reply->ocd_connect_flags &= ~OBD_CONNECT_MDS_CAPA;
178 if (!mdt->mdt_opts.mo_oss_capa)
179 reply->ocd_connect_flags &= ~OBD_CONNECT_OSS_CAPA;
189 int mdt_init_idmap(struct mdt_thread_info *info)
191 struct ptlrpc_request *req = mdt_info_req(info);
192 struct mdt_export_data *med = mdt_req2med(req);
193 struct obd_export *exp = req->rq_export;
194 char *client = libcfs_nid2str(req->rq_peer.nid);
195 struct obd_device *obd = exp->exp_obd;
199 if (exp_connect_rmtclient(exp)) {
200 cfs_mutex_lock(&med->med_idmap_mutex);
202 med->med_idmap = lustre_idmap_init();
203 cfs_mutex_unlock(&med->med_idmap_mutex);
205 if (IS_ERR(med->med_idmap)) {
206 long err = PTR_ERR(med->med_idmap);
208 med->med_idmap = NULL;
209 CERROR("client %s -> target %s "
210 "failed to init idmap [%ld]!\n",
211 client, obd->obd_name, err);
213 } else if (!med->med_idmap) {
214 CERROR("client %s -> target %s "
215 "failed to init(2) idmap!\n",
216 client, obd->obd_name);
220 CDEBUG(D_SEC, "client %s -> target %s is remote.\n",
221 client, obd->obd_name);
222 /* NB, MDS_CONNECT establish root idmap too! */
223 rc = mdt_handle_idmap(info);
228 void mdt_cleanup_idmap(struct mdt_export_data *med)
230 cfs_mutex_lock(&med->med_idmap_mutex);
231 if (med->med_idmap != NULL) {
232 lustre_idmap_fini(med->med_idmap);
233 med->med_idmap = NULL;
235 cfs_mutex_unlock(&med->med_idmap_mutex);
238 static inline void mdt_revoke_export_locks(struct obd_export *exp)
240 /* don't revoke locks during recovery */
241 if (exp->exp_obd->obd_recovering)
244 ldlm_revoke_export_locks(exp);
247 int mdt_handle_idmap(struct mdt_thread_info *info)
249 struct ptlrpc_request *req = mdt_info_req(info);
250 struct mdt_device *mdt = info->mti_mdt;
251 struct mdt_export_data *med;
252 struct ptlrpc_user_desc *pud = req->rq_user_desc;
253 struct md_identity *identity;
261 med = mdt_req2med(req);
262 if (!exp_connect_rmtclient(info->mti_exp))
265 opc = lustre_msg_get_opc(req->rq_reqmsg);
266 /* Bypass other opc */
267 if ((opc != SEC_CTX_INIT) && (opc != SEC_CTX_INIT_CONT) &&
268 (opc != SEC_CTX_FINI) && (opc != MDS_CONNECT))
271 LASSERT(med->med_idmap);
273 if (unlikely(!pud)) {
274 CDEBUG(D_SEC, "remote client must run with rq_user_desc "
279 if (req->rq_auth_mapped_uid == INVALID_UID) {
280 CDEBUG(D_SEC, "invalid authorized mapped uid, please check "
281 "/etc/lustre/idmap.conf!\n");
285 if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
286 CDEBUG(D_SEC, "remote client must run with identity_get "
291 identity = mdt_identity_get(mdt->mdt_identity_cache,
292 req->rq_auth_mapped_uid);
293 if (IS_ERR(identity)) {
294 CDEBUG(D_SEC, "can't get mdt identity(%u), no mapping added\n",
295 req->rq_auth_mapped_uid);
301 case SEC_CTX_INIT_CONT:
303 rc = lustre_idmap_add(med->med_idmap,
304 pud->pud_uid, identity->mi_uid,
305 pud->pud_gid, identity->mi_gid);
308 rc = lustre_idmap_del(med->med_idmap,
309 pud->pud_uid, identity->mi_uid,
310 pud->pud_gid, identity->mi_gid);
314 mdt_identity_put(mdt->mdt_identity_cache, identity);
321 case SEC_CTX_INIT_CONT:
323 mdt_revoke_export_locks(req->rq_export);
330 int ptlrpc_user_desc_do_idmap(struct ptlrpc_request *req,
331 struct ptlrpc_user_desc *pud)
333 struct mdt_export_data *med = mdt_req2med(req);
334 struct lustre_idmap_table *idmap = med->med_idmap;
338 /* Only remote client need desc_to_idmap. */
339 if (!exp_connect_rmtclient(req->rq_export))
342 uid = lustre_idmap_lookup_uid(NULL, idmap, 0, pud->pud_uid);
343 if (uid == CFS_IDMAP_NOTFOUND) {
344 CDEBUG(D_SEC, "no mapping for uid %u\n", pud->pud_uid);
348 if (pud->pud_uid == pud->pud_fsuid) {
351 fsuid = lustre_idmap_lookup_uid(NULL, idmap, 0, pud->pud_fsuid);
352 if (fsuid == CFS_IDMAP_NOTFOUND) {
353 CDEBUG(D_SEC, "no mapping for fsuid %u\n",
359 gid = lustre_idmap_lookup_gid(NULL, idmap, 0, pud->pud_gid);
360 if (gid == CFS_IDMAP_NOTFOUND) {
361 CDEBUG(D_SEC, "no mapping for gid %u\n", pud->pud_gid);
365 if (pud->pud_gid == pud->pud_fsgid) {
368 fsgid = lustre_idmap_lookup_gid(NULL, idmap, 0, pud->pud_fsgid);
369 if (fsgid == CFS_IDMAP_NOTFOUND) {
370 CDEBUG(D_SEC, "no mapping for fsgid %u\n",
378 pud->pud_fsuid = fsuid;
379 pud->pud_fsgid = fsgid;
387 void mdt_body_reverse_idmap(struct mdt_thread_info *info, struct mdt_body *body)
389 struct ptlrpc_request *req = mdt_info_req(info);
390 struct md_ucred *uc = mdt_ucred(info);
391 struct mdt_export_data *med = mdt_req2med(req);
392 struct lustre_idmap_table *idmap = med->med_idmap;
394 if (!exp_connect_rmtclient(info->mti_exp))
397 if (body->valid & OBD_MD_FLUID) {
398 uid_t uid = lustre_idmap_lookup_uid(uc, idmap, 1, body->uid);
400 if (uid == CFS_IDMAP_NOTFOUND) {
402 if (body->valid & OBD_MD_FLMODE)
403 body->mode = (body->mode & ~S_IRWXU) |
404 ((body->mode & S_IRWXO) << 6);
410 if (body->valid & OBD_MD_FLGID) {
411 gid_t gid = lustre_idmap_lookup_gid(uc, idmap, 1, body->gid);
413 if (gid == CFS_IDMAP_NOTFOUND) {
415 if (body->valid & OBD_MD_FLMODE)
416 body->mode = (body->mode & ~S_IRWXG) |
417 ((body->mode & S_IRWXO) << 3);
424 /* Do not ignore root_squash for non-setattr case. */
425 int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
427 struct ptlrpc_request *req = mdt_info_req(info);
428 struct md_ucred *uc = mdt_ucred(info);
429 struct lu_attr *attr = &info->mti_attr.ma_attr;
430 struct mdt_export_data *med = mdt_req2med(req);
431 struct lustre_idmap_table *idmap = med->med_idmap;
433 if ((uc->mu_valid != UCRED_OLD) && (uc->mu_valid != UCRED_NEW))
436 if (op != REINT_SETATTR) {
437 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
438 attr->la_uid = uc->mu_fsuid;
439 /* for S_ISGID, inherit gid from his parent, such work will be
440 * done in cmm/mdd layer, here set all cases as uc->mu_fsgid. */
441 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
442 attr->la_gid = uc->mu_fsgid;
443 } else if (exp_connect_rmtclient(info->mti_exp)) {
444 /* NB: -1 case will be handled by mdt_fix_attr() later. */
445 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1)) {
446 uid_t uid = lustre_idmap_lookup_uid(uc, idmap, 0,
449 if (uid == CFS_IDMAP_NOTFOUND) {
450 CDEBUG(D_SEC, "Deny chown to uid %u\n",
457 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1)) {
458 gid_t gid = lustre_idmap_lookup_gid(uc, idmap, 0,
461 if (gid == CFS_IDMAP_NOTFOUND) {
462 CDEBUG(D_SEC, "Deny chown to gid %u\n",