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) 2011, 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/ptlrpc/gss/gss_cli_upcall.c
38 * Author: Eric Mei <ericm@clusterfs.com>
41 #define DEBUG_SUBSYSTEM S_SEC
43 #include <linux/init.h>
44 #include <linux/module.h>
45 #include <linux/slab.h>
46 #include <linux/dcache.h>
48 #include <linux/mutex.h>
50 #include <liblustre.h>
54 #include <obd_class.h>
55 #include <obd_support.h>
56 #include <lustre/lustre_idl.h>
57 #include <lustre_net.h>
58 #include <lustre_import.h>
59 #include <lustre_sec.h>
62 #include "gss_internal.h"
65 /**********************************************
66 * gss context init/fini helper *
67 **********************************************/
70 int ctx_init_pack_request(struct obd_import *imp,
71 struct ptlrpc_request *req,
77 struct lustre_msg *msg = req->rq_reqbuf;
79 struct gss_header *ghdr;
80 struct ptlrpc_user_desc *pud;
81 __u32 *p, size, offset = 2;
84 LASSERT(msg->lm_bufcount <= 4);
85 LASSERT(req->rq_cli_ctx);
86 LASSERT(req->rq_cli_ctx->cc_sec);
89 ghdr = lustre_msg_buf(msg, 0, sizeof(*ghdr));
90 ghdr->gh_version = PTLRPC_GSS_VERSION;
91 ghdr->gh_sp = (__u8) imp->imp_sec->ps_part;
93 ghdr->gh_proc = PTLRPC_GSS_PROC_INIT;
95 ghdr->gh_svc = SPTLRPC_SVC_NULL;
96 ghdr->gh_handle.len = 0;
98 /* fix the user desc */
99 if (req->rq_pack_udesc) {
100 ghdr->gh_flags |= LUSTRE_GSS_PACK_USER;
102 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
104 pud->pud_uid = pud->pud_fsuid = uid;
105 pud->pud_gid = pud->pud_fsgid = gid;
107 pud->pud_ngroups = 0;
111 /* security payload */
112 p = lustre_msg_buf(msg, offset, 0);
113 size = msg->lm_buflens[offset];
116 /* 1. lustre svc type */
118 *p++ = cpu_to_le32(lustre_srv);
122 obj.len = strlen(imp->imp_obd->u.cli.cl_target_uuid.uuid) + 1;
123 obj.data = imp->imp_obd->u.cli.cl_target_uuid.uuid;
124 if (rawobj_serialize(&obj, &p, &size))
127 /* 3. reverse context handle. actually only needed by root user,
128 * but we send it anyway. */
129 gsec = sec2gsec(req->rq_cli_ctx->cc_sec);
130 obj.len = sizeof(gsec->gs_rvs_hdl);
131 obj.data = (__u8 *) &gsec->gs_rvs_hdl;
132 if (rawobj_serialize(&obj, &p, &size))
135 /* 4. now the token */
136 LASSERT(size >= (sizeof(__u32) + token_size));
137 *p++ = cpu_to_le32(((__u32) token_size));
138 if (copy_from_user(p, token, token_size)) {
139 CERROR("can't copy token\n");
142 size -= sizeof(__u32) + cfs_size_round4(token_size);
144 req->rq_reqdata_len = lustre_shrink_msg(req->rq_reqbuf, offset,
145 msg->lm_buflens[offset] - size, 0);
150 int ctx_init_parse_reply(struct lustre_msg *msg, int swabbed,
151 char __user *outbuf, long outlen)
153 struct gss_rep_header *ghdr;
154 __u32 obj_len, round_len;
155 __u32 status, effective = 0;
157 if (msg->lm_bufcount != 3) {
158 CERROR("unexpected bufcount %u\n", msg->lm_bufcount);
162 ghdr = (struct gss_rep_header *) gss_swab_header(msg, 0, swabbed);
164 CERROR("unable to extract gss reply header\n");
168 if (ghdr->gh_version != PTLRPC_GSS_VERSION) {
169 CERROR("invalid gss version %u\n", ghdr->gh_version);
173 if (outlen < (4 + 2) * 4 + cfs_size_round4(ghdr->gh_handle.len) +
174 cfs_size_round4(msg->lm_buflens[2])) {
175 CERROR("output buffer size %ld too small\n", outlen);
182 if (copy_to_user(outbuf, &status, 4))
185 if (copy_to_user(outbuf, &ghdr->gh_major, 4))
188 if (copy_to_user(outbuf, &ghdr->gh_minor, 4))
191 if (copy_to_user(outbuf, &ghdr->gh_seqwin, 4))
197 obj_len = ghdr->gh_handle.len;
198 round_len = (obj_len + 3) & ~3;
199 if (copy_to_user(outbuf, &obj_len, 4))
202 if (copy_to_user(outbuf, (char *) ghdr->gh_handle.data, round_len))
205 effective += 4 + round_len;
208 obj_len = msg->lm_buflens[2];
209 round_len = (obj_len + 3) & ~3;
210 if (copy_to_user(outbuf, &obj_len, 4))
213 if (copy_to_user(outbuf, lustre_msg_buf(msg, 2, 0), round_len))
216 effective += 4 + round_len;
221 /* XXX move to where lgssd could see */
222 struct lgssd_ioctl_param {
223 int version; /* in */
226 int lustre_svc; /* in */
229 long send_token_size;/* in */
230 char *send_token; /* in */
231 long reply_buf_size; /* in */
232 char *reply_buf; /* in */
233 long status; /* out */
234 long reply_length; /* out */
237 int gss_do_ctx_init_rpc(__user char *buffer, unsigned long count)
239 struct obd_import *imp;
240 struct ptlrpc_request *req;
241 struct lgssd_ioctl_param param;
242 struct obd_device *obd;
247 if (count != sizeof(param)) {
248 CERROR("ioctl size %lu, expect %lu, please check lgss_keyring "
249 "version\n", count, (unsigned long) sizeof(param));
252 if (copy_from_user(¶m, buffer, sizeof(param))) {
253 CERROR("failed copy data from lgssd\n");
257 if (param.version != GSSD_INTERFACE_VERSION) {
258 CERROR("gssd interface version %d (expect %d)\n",
259 param.version, GSSD_INTERFACE_VERSION);
264 if (strncpy_from_user(obdname, param.uuid, sizeof(obdname)) <= 0) {
265 CERROR("Invalid obdname pointer\n");
269 obd = class_name2obd(obdname);
271 CERROR("no such obd %s\n", obdname);
275 if (unlikely(!obd->obd_set_up)) {
276 CERROR("obd %s not setup\n", obdname);
280 spin_lock(&obd->obd_dev_lock);
281 if (obd->obd_stopping) {
282 CERROR("obd %s has stopped\n", obdname);
283 spin_unlock(&obd->obd_dev_lock);
287 if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
288 strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) &&
289 strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME)) {
290 CERROR("obd %s is not a client device\n", obdname);
291 spin_unlock(&obd->obd_dev_lock);
294 spin_unlock(&obd->obd_dev_lock);
296 down_read(&obd->u.cli.cl_sem);
297 if (obd->u.cli.cl_import == NULL) {
298 CERROR("obd %s: import has gone\n", obd->obd_name);
299 up_read(&obd->u.cli.cl_sem);
302 imp = class_import_get(obd->u.cli.cl_import);
303 up_read(&obd->u.cli.cl_sem);
305 if (imp->imp_deactive) {
306 CERROR("import has been deactivated\n");
307 class_import_put(imp);
311 req = ptlrpc_request_alloc_pack(imp, &RQF_SEC_CTX, LUSTRE_OBD_VERSION,
314 param.status = -ENOMEM;
318 if (req->rq_cli_ctx->cc_sec->ps_id != param.secid) {
319 CWARN("original secid %d, now has changed to %d, "
320 "cancel this negotiation\n", param.secid,
321 req->rq_cli_ctx->cc_sec->ps_id);
322 param.status = -EINVAL;
327 rc = ctx_init_pack_request(imp, req,
329 param.uid, param.gid,
330 param.send_token_size,
337 ptlrpc_request_set_replen(req);
339 rc = ptlrpc_queue_wait(req);
341 /* If any _real_ denial be made, we expect server return
342 * -EACCES reply or return success but indicate gss error
343 * inside reply messsage. All other errors are treated as
344 * timeout, caller might try the negotiation repeatedly,
345 * leave recovery decisions to general ptlrpc layer.
347 * FIXME maybe some other error code shouldn't be treated
351 param.status = -ETIMEDOUT;
355 LASSERT(req->rq_repdata);
356 lsize = ctx_init_parse_reply(req->rq_repdata,
357 ptlrpc_rep_need_swab(req),
358 param.reply_buf, param.reply_buf_size);
360 param.status = (int) lsize;
365 param.reply_length = lsize;
368 if (copy_to_user(buffer, ¶m, sizeof(param)))
373 class_import_put(imp);
374 ptlrpc_req_finished(req);
378 int gss_do_ctx_fini_rpc(struct gss_cli_ctx *gctx)
380 struct ptlrpc_cli_ctx *ctx = &gctx->gc_base;
381 struct obd_import *imp = ctx->cc_sec->ps_import;
382 struct ptlrpc_request *req;
383 struct ptlrpc_user_desc *pud;
387 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
389 if (cli_ctx_is_error(ctx) || !cli_ctx_is_uptodate(ctx)) {
390 CDEBUG(D_SEC, "ctx %p(%u->%s) not uptodate, "
391 "don't send destroy rpc\n", ctx,
392 ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
398 CWARN("%s ctx %p idx "LPX64" (%u->%s)\n",
399 sec_is_reverse(ctx->cc_sec) ?
400 "server finishing reverse" : "client finishing forward",
401 ctx, gss_handle_to_u64(&gctx->gc_handle),
402 ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
404 gctx->gc_proc = PTLRPC_GSS_PROC_DESTROY;
406 req = ptlrpc_request_alloc(imp, &RQF_SEC_CTX);
408 CWARN("ctx %p(%u): fail to prepare rpc, destroy locally\n",
409 ctx, ctx->cc_vcred.vc_uid);
410 GOTO(out, rc = -ENOMEM);
413 rc = ptlrpc_request_bufs_pack(req, LUSTRE_OBD_VERSION, SEC_CTX_FINI,
416 ptlrpc_request_free(req);
420 /* fix the user desc */
421 if (req->rq_pack_udesc) {
422 /* we rely the fact that this request is in AUTH mode,
423 * and user_desc at offset 2. */
424 pud = lustre_msg_buf(req->rq_reqbuf, 2, sizeof(*pud));
426 pud->pud_uid = pud->pud_fsuid = ctx->cc_vcred.vc_uid;
427 pud->pud_gid = pud->pud_fsgid = ctx->cc_vcred.vc_gid;
429 pud->pud_ngroups = 0;
432 req->rq_phase = RQ_PHASE_RPC;
433 rc = ptl_send_rpc(req, 1);
435 CWARN("ctx %p(%u->%s): rpc error %d, destroy locally\n", ctx,
436 ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec), rc);
439 ptlrpc_req_finished(req);
444 int __init gss_init_cli_upcall(void)
449 void gss_exit_cli_upcall(void)