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 (c) 2007, 2010, Oracle and/or its affiliates. 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>
67 #include <libcfs/lucache.h>
69 #include <obd_class.h>
70 #include <obd_support.h>
71 #include <lustre_net.h>
72 #include <lustre_import.h>
73 #include <lustre_dlm.h>
74 #include <lustre_sec.h>
75 #include <lustre_lib.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); \
87 int mdt_init_sec_level(struct mdt_thread_info *info)
89 struct mdt_device *mdt = info->mti_mdt;
90 struct ptlrpc_request *req = mdt_info_req(info);
91 char *client = libcfs_nid2str(req->rq_peer.nid);
92 struct obd_export *exp = req->rq_export;
93 struct obd_device *obd = exp->exp_obd;
94 struct obd_connect_data *data, *reply;
98 data = req_capsule_client_get(info->mti_pill, &RMF_CONNECT_DATA);
99 reply = req_capsule_server_get(info->mti_pill, &RMF_CONNECT_DATA);
100 if (data == NULL || reply == NULL)
103 /* connection from MDT is always trusted */
104 if (req->rq_auth_usr_mdt) {
105 mdt_init_sec_none(reply, exp);
109 /* no GSS support case */
110 if (!req->rq_auth_gss) {
111 if (mdt->mdt_sec_level > LUSTRE_SEC_NONE) {
112 CWARN("client %s -> target %s does not user GSS, "
113 "can not run under security level %d.\n",
114 client, obd->obd_name, mdt->mdt_sec_level);
117 mdt_init_sec_none(reply, exp);
122 /* old version case */
123 if (unlikely(!(data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT) ||
124 !(data->ocd_connect_flags & OBD_CONNECT_MDS_CAPA) ||
125 !(data->ocd_connect_flags & OBD_CONNECT_OSS_CAPA))) {
126 if (mdt->mdt_sec_level > LUSTRE_SEC_NONE) {
127 CWARN("client %s -> target %s uses old version, "
128 "can not run under security level %d.\n",
129 client, obd->obd_name, mdt->mdt_sec_level);
132 CWARN("client %s -> target %s uses old version, "
133 "run under security level %d.\n",
134 client, obd->obd_name, mdt->mdt_sec_level);
135 mdt_init_sec_none(reply, exp);
140 remote = data->ocd_connect_flags & OBD_CONNECT_RMT_CLIENT_FORCE;
142 if (!req->rq_auth_remote)
143 CDEBUG(D_SEC, "client (local realm) %s -> target %s "
144 "asked to be remote.\n", client, obd->obd_name);
145 } else if (req->rq_auth_remote) {
147 CDEBUG(D_SEC, "client (remote realm) %s -> target %s is set "
148 "as remote by default.\n", client, obd->obd_name);
152 if (!mdt->mdt_opts.mo_oss_capa) {
153 CDEBUG(D_SEC, "client %s -> target %s is set as remote,"
154 " but OSS capabilities are not enabled: %d.\n",
155 client, obd->obd_name, mdt->mdt_opts.mo_oss_capa);
159 if (req->rq_auth_uid == INVALID_UID) {
160 CDEBUG(D_SEC, "client %s -> target %s: user is not "
161 "authenticated!\n", client, obd->obd_name);
166 switch (mdt->mdt_sec_level) {
167 case LUSTRE_SEC_NONE:
169 mdt_init_sec_none(reply, exp);
172 CDEBUG(D_SEC, "client %s -> target %s is set as remote, "
173 "can not run under security level %d.\n",
174 client, obd->obd_name, mdt->mdt_sec_level);
177 case LUSTRE_SEC_REMOTE:
179 mdt_init_sec_none(reply, exp);
183 reply->ocd_connect_flags &= ~(OBD_CONNECT_RMT_CLIENT |
184 OBD_CONNECT_RMT_CLIENT_FORCE);
185 if (!mdt->mdt_opts.mo_mds_capa)
186 reply->ocd_connect_flags &= ~OBD_CONNECT_MDS_CAPA;
187 if (!mdt->mdt_opts.mo_oss_capa)
188 reply->ocd_connect_flags &= ~OBD_CONNECT_OSS_CAPA;
198 int mdt_init_idmap(struct mdt_thread_info *info)
200 struct ptlrpc_request *req = mdt_info_req(info);
201 struct mdt_export_data *med = mdt_req2med(req);
202 struct obd_export *exp = req->rq_export;
203 char *client = libcfs_nid2str(req->rq_peer.nid);
204 struct obd_device *obd = exp->exp_obd;
208 if (exp_connect_rmtclient(exp)) {
209 cfs_down(&med->med_idmap_sem);
211 med->med_idmap = lustre_idmap_init();
212 cfs_up(&med->med_idmap_sem);
214 if (IS_ERR(med->med_idmap)) {
215 long err = PTR_ERR(med->med_idmap);
217 med->med_idmap = NULL;
218 CERROR("client %s -> target %s "
219 "failed to init idmap [%ld]!\n",
220 client, obd->obd_name, err);
222 } else if (!med->med_idmap) {
223 CERROR("client %s -> target %s "
224 "failed to init(2) idmap!\n",
225 client, obd->obd_name);
229 CDEBUG(D_SEC, "client %s -> target %s is remote.\n",
230 client, obd->obd_name);
231 /* NB, MDS_CONNECT establish root idmap too! */
232 rc = mdt_handle_idmap(info);
237 void mdt_cleanup_idmap(struct mdt_export_data *med)
239 cfs_down(&med->med_idmap_sem);
240 if (med->med_idmap != NULL) {
241 lustre_idmap_fini(med->med_idmap);
242 med->med_idmap = NULL;
244 cfs_up(&med->med_idmap_sem);
247 static inline void mdt_revoke_export_locks(struct obd_export *exp)
249 /* don't revoke locks during recovery */
250 if (exp->exp_obd->obd_recovering)
253 ldlm_revoke_export_locks(exp);
256 int mdt_handle_idmap(struct mdt_thread_info *info)
258 struct ptlrpc_request *req = mdt_info_req(info);
259 struct mdt_device *mdt = info->mti_mdt;
260 struct mdt_export_data *med;
261 struct ptlrpc_user_desc *pud = req->rq_user_desc;
262 struct md_identity *identity;
270 med = mdt_req2med(req);
271 if (!exp_connect_rmtclient(info->mti_exp))
274 opc = lustre_msg_get_opc(req->rq_reqmsg);
275 /* Bypass other opc */
276 if ((opc != SEC_CTX_INIT) && (opc != SEC_CTX_INIT_CONT) &&
277 (opc != SEC_CTX_FINI) && (opc != MDS_CONNECT))
280 LASSERT(med->med_idmap);
282 if (unlikely(!pud)) {
283 CDEBUG(D_SEC, "remote client must run with rq_user_desc "
288 if (req->rq_auth_mapped_uid == INVALID_UID) {
289 CDEBUG(D_SEC, "invalid authorized mapped uid, please check "
290 "/etc/lustre/idmap.conf!\n");
294 if (is_identity_get_disabled(mdt->mdt_identity_cache)) {
295 CDEBUG(D_SEC, "remote client must run with identity_get "
300 identity = mdt_identity_get(mdt->mdt_identity_cache,
301 req->rq_auth_mapped_uid);
302 if (IS_ERR(identity)) {
303 CDEBUG(D_SEC, "can't get mdt identity(%u), no mapping added\n",
304 req->rq_auth_mapped_uid);
310 case SEC_CTX_INIT_CONT:
312 rc = lustre_idmap_add(med->med_idmap,
313 pud->pud_uid, identity->mi_uid,
314 pud->pud_gid, identity->mi_gid);
317 rc = lustre_idmap_del(med->med_idmap,
318 pud->pud_uid, identity->mi_uid,
319 pud->pud_gid, identity->mi_gid);
323 mdt_identity_put(mdt->mdt_identity_cache, identity);
330 case SEC_CTX_INIT_CONT:
332 mdt_revoke_export_locks(req->rq_export);
339 int ptlrpc_user_desc_do_idmap(struct ptlrpc_request *req,
340 struct ptlrpc_user_desc *pud)
342 struct mdt_export_data *med = mdt_req2med(req);
343 struct lustre_idmap_table *idmap = med->med_idmap;
347 /* Only remote client need desc_to_idmap. */
348 if (!exp_connect_rmtclient(req->rq_export))
351 uid = lustre_idmap_lookup_uid(NULL, idmap, 0, pud->pud_uid);
352 if (uid == CFS_IDMAP_NOTFOUND) {
353 CDEBUG(D_SEC, "no mapping for uid %u\n", pud->pud_uid);
357 if (pud->pud_uid == pud->pud_fsuid) {
360 fsuid = lustre_idmap_lookup_uid(NULL, idmap, 0, pud->pud_fsuid);
361 if (fsuid == CFS_IDMAP_NOTFOUND) {
362 CDEBUG(D_SEC, "no mapping for fsuid %u\n",
368 gid = lustre_idmap_lookup_gid(NULL, idmap, 0, pud->pud_gid);
369 if (gid == CFS_IDMAP_NOTFOUND) {
370 CDEBUG(D_SEC, "no mapping for gid %u\n", pud->pud_gid);
374 if (pud->pud_gid == pud->pud_fsgid) {
377 fsgid = lustre_idmap_lookup_gid(NULL, idmap, 0, pud->pud_fsgid);
378 if (fsgid == CFS_IDMAP_NOTFOUND) {
379 CDEBUG(D_SEC, "no mapping for fsgid %u\n",
387 pud->pud_fsuid = fsuid;
388 pud->pud_fsgid = fsgid;
396 void mdt_body_reverse_idmap(struct mdt_thread_info *info, struct mdt_body *body)
398 struct ptlrpc_request *req = mdt_info_req(info);
399 struct md_ucred *uc = mdt_ucred(info);
400 struct mdt_export_data *med = mdt_req2med(req);
401 struct lustre_idmap_table *idmap = med->med_idmap;
403 if (!exp_connect_rmtclient(info->mti_exp))
406 if (body->valid & OBD_MD_FLUID) {
407 uid_t uid = lustre_idmap_lookup_uid(uc, idmap, 1, body->uid);
409 if (uid == CFS_IDMAP_NOTFOUND) {
411 if (body->valid & OBD_MD_FLMODE)
412 body->mode = (body->mode & ~S_IRWXU) |
413 ((body->mode & S_IRWXO) << 6);
419 if (body->valid & OBD_MD_FLGID) {
420 gid_t gid = lustre_idmap_lookup_gid(uc, idmap, 1, body->gid);
422 if (gid == CFS_IDMAP_NOTFOUND) {
424 if (body->valid & OBD_MD_FLMODE)
425 body->mode = (body->mode & ~S_IRWXG) |
426 ((body->mode & S_IRWXO) << 3);
433 /* Do not ignore root_squash for non-setattr case. */
434 int mdt_fix_attr_ucred(struct mdt_thread_info *info, __u32 op)
436 struct ptlrpc_request *req = mdt_info_req(info);
437 struct md_ucred *uc = mdt_ucred(info);
438 struct lu_attr *attr = &info->mti_attr.ma_attr;
439 struct mdt_export_data *med = mdt_req2med(req);
440 struct lustre_idmap_table *idmap = med->med_idmap;
442 if ((uc->mu_valid != UCRED_OLD) && (uc->mu_valid != UCRED_NEW))
445 if (op != REINT_SETATTR) {
446 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1))
447 attr->la_uid = uc->mu_fsuid;
448 /* for S_ISGID, inherit gid from his parent, such work will be
449 * done in cmm/mdd layer, here set all cases as uc->mu_fsgid. */
450 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1))
451 attr->la_gid = uc->mu_fsgid;
452 } else if (exp_connect_rmtclient(info->mti_exp)) {
453 /* NB: -1 case will be handled by mdt_fix_attr() later. */
454 if ((attr->la_valid & LA_UID) && (attr->la_uid != -1)) {
455 uid_t uid = lustre_idmap_lookup_uid(uc, idmap, 0,
458 if (uid == CFS_IDMAP_NOTFOUND) {
459 CDEBUG(D_SEC, "Deny chown to uid %u\n",
466 if ((attr->la_valid & LA_GID) && (attr->la_gid != -1)) {
467 gid_t gid = lustre_idmap_lookup_gid(uc, idmap, 0,
470 if (gid == CFS_IDMAP_NOTFOUND) {
471 CDEBUG(D_SEC, "Deny chown to gid %u\n",