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/ptlrpc/gss/gss_cli_upcall.c
38 * Author: Eric Mei <ericm@clusterfs.com>
42 # define EXPORT_SYMTAB
44 #define DEBUG_SUBSYSTEM S_SEC
46 #include <linux/init.h>
47 #include <linux/module.h>
48 #include <linux/slab.h>
49 #include <linux/dcache.h>
51 #include <linux/mutex.h>
52 #include <linux/random.h>
54 #include <liblustre.h>
58 #include <obd_class.h>
59 #include <obd_support.h>
60 #include <lustre/lustre_idl.h>
61 #include <lustre_net.h>
62 #include <lustre_import.h>
63 #include <lustre_sec.h>
66 #include "gss_internal.h"
69 /**********************************************
70 * gss context init/fini helper *
71 **********************************************/
74 int ctx_init_pack_request(struct obd_import *imp,
75 struct ptlrpc_request *req,
81 struct lustre_msg *msg = req->rq_reqbuf;
83 struct gss_header *ghdr;
84 struct ptlrpc_user_desc *pud;
85 __u32 *p, size, offset = 2;
88 LASSERT(msg->lm_bufcount <= 4);
89 LASSERT(req->rq_cli_ctx);
90 LASSERT(req->rq_cli_ctx->cc_sec);
93 ghdr = lustre_msg_buf(msg, 0, sizeof(*ghdr));
94 ghdr->gh_version = PTLRPC_GSS_VERSION;
96 ghdr->gh_proc = PTLRPC_GSS_PROC_INIT;
98 ghdr->gh_svc = SPTLRPC_SVC_NULL;
99 ghdr->gh_handle.len = 0;
101 /* fix the user desc */
102 if (req->rq_pack_udesc) {
103 ghdr->gh_flags |= LUSTRE_GSS_PACK_USER;
105 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
107 pud->pud_uid = pud->pud_fsuid = uid;
108 pud->pud_gid = pud->pud_fsgid = gid;
110 pud->pud_ngroups = 0;
114 /* security payload */
115 p = lustre_msg_buf(msg, offset, 0);
116 size = msg->lm_buflens[offset];
119 /* 1. lustre svc type */
121 *p++ = cpu_to_le32(lustre_srv);
125 obj.len = strlen(imp->imp_obd->u.cli.cl_target_uuid.uuid) + 1;
126 obj.data = imp->imp_obd->u.cli.cl_target_uuid.uuid;
127 if (rawobj_serialize(&obj, &p, &size))
130 /* 3. reverse context handle. actually only needed by root user,
131 * but we send it anyway. */
132 gsec = sec2gsec(req->rq_cli_ctx->cc_sec);
133 obj.len = sizeof(gsec->gs_rvs_hdl);
134 obj.data = (__u8 *) &gsec->gs_rvs_hdl;
135 if (rawobj_serialize(&obj, &p, &size))
138 /* 4. now the token */
139 LASSERT(size >= (sizeof(__u32) + token_size));
140 *p++ = cpu_to_le32(((__u32) token_size));
141 if (copy_from_user(p, token, token_size)) {
142 CERROR("can't copy token\n");
145 size -= sizeof(__u32) + size_round4(token_size);
147 req->rq_reqdata_len = lustre_shrink_msg(req->rq_reqbuf, offset,
148 msg->lm_buflens[offset] - size, 0);
153 int ctx_init_parse_reply(struct lustre_msg *msg,
154 char __user *outbuf, long outlen)
156 struct gss_rep_header *ghdr;
157 __u32 obj_len, round_len;
158 __u32 status, effective = 0;
160 if (msg->lm_bufcount != 3) {
161 CERROR("unexpected bufcount %u\n", msg->lm_bufcount);
165 ghdr = (struct gss_rep_header *) gss_swab_header(msg, 0);
167 CERROR("unable to extract gss reply header\n");
171 if (ghdr->gh_version != PTLRPC_GSS_VERSION) {
172 CERROR("invalid gss version %u\n", ghdr->gh_version);
176 if (outlen < (4 + 2) * 4 + size_round4(ghdr->gh_handle.len) +
177 size_round4(msg->lm_buflens[2])) {
178 CERROR("output buffer size %ld too small\n", outlen);
185 if (copy_to_user(outbuf, &status, 4))
188 if (copy_to_user(outbuf, &ghdr->gh_major, 4))
191 if (copy_to_user(outbuf, &ghdr->gh_minor, 4))
194 if (copy_to_user(outbuf, &ghdr->gh_seqwin, 4))
200 obj_len = ghdr->gh_handle.len;
201 round_len = (obj_len + 3) & ~ 3;
202 if (copy_to_user(outbuf, &obj_len, 4))
205 if (copy_to_user(outbuf, (char *) ghdr->gh_handle.data, round_len))
208 effective += 4 + round_len;
211 obj_len = msg->lm_buflens[2];
212 round_len = (obj_len + 3) & ~ 3;
213 if (copy_to_user(outbuf, &obj_len, 4))
216 if (copy_to_user(outbuf, lustre_msg_buf(msg, 2, 0), round_len))
219 effective += 4 + round_len;
224 /* XXX move to where lgssd could see */
225 struct lgssd_ioctl_param {
226 int version; /* in */
229 int lustre_svc; /* in */
232 long send_token_size;/* in */
233 char *send_token; /* in */
234 long reply_buf_size; /* in */
235 char *reply_buf; /* in */
236 long status; /* out */
237 long reply_length; /* out */
240 int gss_do_ctx_init_rpc(__user char *buffer, unsigned long count)
242 struct obd_import *imp;
243 struct ptlrpc_request *req;
244 struct lgssd_ioctl_param param;
245 struct obd_device *obd;
250 if (count != sizeof(param)) {
251 CERROR("ioctl size %lu, expect %lu, please check lgssd version\n",
252 count, (unsigned long) sizeof(param));
255 if (copy_from_user(¶m, buffer, sizeof(param))) {
256 CERROR("failed copy data from lgssd\n");
260 if (param.version != GSSD_INTERFACE_VERSION) {
261 CERROR("gssd interface version %d (expect %d)\n",
262 param.version, GSSD_INTERFACE_VERSION);
267 if (strncpy_from_user(obdname, param.uuid, sizeof(obdname)) <= 0) {
268 CERROR("Invalid obdname pointer\n");
272 obd = class_name2obd(obdname);
274 CERROR("no such obd %s\n", obdname);
278 imp = class_import_get(obd->u.cli.cl_import);
279 LASSERT(imp->imp_sec);
281 /* force this import to use v2 msg */
282 imp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
284 req = ptlrpc_request_alloc_pack(imp, &RQF_SEC_CTX, LUSTRE_OBD_VERSION,
287 param.status = -ENOMEM;
291 if (req->rq_cli_ctx->cc_sec->ps_id != param.secid) {
292 CWARN("original secid %d, now has changed to %d, "
293 "cancel this negotiation\n", param.secid,
294 req->rq_cli_ctx->cc_sec->ps_id);
295 param.status = -EINVAL;
300 rc = ctx_init_pack_request(imp, req,
302 param.uid, param.gid,
303 param.send_token_size,
310 ptlrpc_request_set_replen(req);
312 rc = ptlrpc_queue_wait(req);
314 /* If any _real_ denial be made, we expect server return
315 * -EACCES reply or return success but indicate gss error
316 * inside reply messsage. All other errors are treated as
317 * timeout, caller might try the negotiation repeatedly,
318 * leave recovery decisions to general ptlrpc layer.
320 * FIXME maybe some other error code shouldn't be treated
324 param.status = -ETIMEDOUT;
328 LASSERT(req->rq_repdata);
329 lsize = ctx_init_parse_reply(req->rq_repdata,
330 param.reply_buf, param.reply_buf_size);
332 param.status = (int) lsize;
337 param.reply_length = lsize;
340 if (copy_to_user(buffer, ¶m, sizeof(param)))
345 class_import_put(imp);
346 ptlrpc_req_finished(req);
350 int gss_do_ctx_fini_rpc(struct gss_cli_ctx *gctx)
352 struct ptlrpc_cli_ctx *ctx = &gctx->gc_base;
353 struct obd_import *imp = ctx->cc_sec->ps_import;
354 struct ptlrpc_request *req;
355 struct ptlrpc_user_desc *pud;
359 LASSERT(atomic_read(&ctx->cc_refcount) > 0);
361 if (cli_ctx_is_error(ctx) || !cli_ctx_is_uptodate(ctx)) {
362 CDEBUG(D_SEC, "ctx %p(%u->%s) not uptodate, "
363 "don't send destroy rpc\n", ctx,
364 ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
370 CWARN("%s ctx %p idx "LPX64" (%u->%s)\n",
371 sec_is_reverse(ctx->cc_sec) ?
372 "server finishing reverse" : "client finishing forward",
373 ctx, gss_handle_to_u64(&gctx->gc_handle),
374 ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
376 gctx->gc_proc = PTLRPC_GSS_PROC_DESTROY;
378 req = ptlrpc_request_alloc(imp, &RQF_SEC_CTX);
380 CWARN("ctx %p(%u): fail to prepare rpc, destroy locally\n",
381 ctx, ctx->cc_vcred.vc_uid);
382 GOTO(out, rc = -ENOMEM);
385 rc = ptlrpc_request_bufs_pack(req, LUSTRE_OBD_VERSION, SEC_CTX_FINI,
388 ptlrpc_request_free(req);
392 /* fix the user desc */
393 if (req->rq_pack_udesc) {
394 /* we rely the fact that this request is in AUTH mode,
395 * and user_desc at offset 2. */
396 pud = lustre_msg_buf(req->rq_reqbuf, 2, sizeof(*pud));
398 pud->pud_uid = pud->pud_fsuid = ctx->cc_vcred.vc_uid;
399 pud->pud_gid = pud->pud_fsgid = ctx->cc_vcred.vc_gid;
401 pud->pud_ngroups = 0;
404 req->rq_phase = RQ_PHASE_RPC;
405 rc = ptl_send_rpc(req, 1);
407 CWARN("ctx %p(%u->%s): rpc error %d, destroy locally\n", ctx,
408 ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec), rc);
411 ptlrpc_req_finished(req);
416 int __init gss_init_cli_upcall(void)
421 void __exit gss_exit_cli_upcall(void)