Whamcloud - gitweb
b=18751 Move prng.c to libcfs
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_cli_upcall.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * GPL HEADER START
5  *
6  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7  *
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.
11  *
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).
17  *
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
21  *
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
24  * have any questions.
25  *
26  * GPL HEADER END
27  */
28 /*
29  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
30  * Use is subject to license terms.
31  */
32 /*
33  * This file is part of Lustre, http://www.lustre.org/
34  * Lustre is a trademark of Sun Microsystems, Inc.
35  *
36  * lustre/ptlrpc/gss/gss_cli_upcall.c
37  *
38  * Author: Eric Mei <ericm@clusterfs.com>
39  */
40
41 #ifndef EXPORT_SYMTAB
42 # define EXPORT_SYMTAB
43 #endif
44 #define DEBUG_SUBSYSTEM S_SEC
45 #ifdef __KERNEL__
46 #include <linux/init.h>
47 #include <linux/module.h>
48 #include <linux/slab.h>
49 #include <linux/dcache.h>
50 #include <linux/fs.h>
51 #include <linux/mutex.h>
52 #else
53 #include <liblustre.h>
54 #endif
55
56 #include <obd.h>
57 #include <obd_class.h>
58 #include <obd_support.h>
59 #include <lustre/lustre_idl.h>
60 #include <lustre_net.h>
61 #include <lustre_import.h>
62 #include <lustre_sec.h>
63
64 #include "gss_err.h"
65 #include "gss_internal.h"
66 #include "gss_api.h"
67
68 /**********************************************
69  * gss context init/fini helper               *
70  **********************************************/
71
72 static
73 int ctx_init_pack_request(struct obd_import *imp,
74                           struct ptlrpc_request *req,
75                           int lustre_srv,
76                           uid_t uid, gid_t gid,
77                           long token_size,
78                           char __user *token)
79 {
80         struct lustre_msg       *msg = req->rq_reqbuf;
81         struct gss_sec          *gsec;
82         struct gss_header       *ghdr;
83         struct ptlrpc_user_desc *pud;
84         __u32                   *p, size, offset = 2;
85         rawobj_t                 obj;
86
87         LASSERT(msg->lm_bufcount <= 4);
88         LASSERT(req->rq_cli_ctx);
89         LASSERT(req->rq_cli_ctx->cc_sec);
90
91         /* gss hdr */
92         ghdr = lustre_msg_buf(msg, 0, sizeof(*ghdr));
93         ghdr->gh_version = PTLRPC_GSS_VERSION;
94         ghdr->gh_flags = 0;
95         ghdr->gh_proc = PTLRPC_GSS_PROC_INIT;
96         ghdr->gh_seq = 0;
97         ghdr->gh_svc = SPTLRPC_SVC_NULL;
98         ghdr->gh_handle.len = 0;
99
100         /* fix the user desc */
101         if (req->rq_pack_udesc) {
102                 ghdr->gh_flags |= LUSTRE_GSS_PACK_USER;
103
104                 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
105                 LASSERT(pud);
106                 pud->pud_uid = pud->pud_fsuid = uid;
107                 pud->pud_gid = pud->pud_fsgid = gid;
108                 pud->pud_cap = 0;
109                 pud->pud_ngroups = 0;
110                 offset++;
111         }
112
113         /* security payload */
114         p = lustre_msg_buf(msg, offset, 0);
115         size = msg->lm_buflens[offset];
116         LASSERT(p);
117
118         /* 1. lustre svc type */
119         LASSERT(size > 4);
120         *p++ = cpu_to_le32(lustre_srv);
121         size -= 4;
122
123         /* 2. target uuid */
124         obj.len = strlen(imp->imp_obd->u.cli.cl_target_uuid.uuid) + 1;
125         obj.data = imp->imp_obd->u.cli.cl_target_uuid.uuid;
126         if (rawobj_serialize(&obj, &p, &size))
127                 LBUG();
128
129         /* 3. reverse context handle. actually only needed by root user,
130          *    but we send it anyway. */
131         gsec = sec2gsec(req->rq_cli_ctx->cc_sec);
132         obj.len = sizeof(gsec->gs_rvs_hdl);
133         obj.data = (__u8 *) &gsec->gs_rvs_hdl;
134         if (rawobj_serialize(&obj, &p, &size))
135                 LBUG();
136
137         /* 4. now the token */
138         LASSERT(size >= (sizeof(__u32) + token_size));
139         *p++ = cpu_to_le32(((__u32) token_size));
140         if (cfs_copy_from_user(p, token, token_size)) {
141                 CERROR("can't copy token\n");
142                 return -EFAULT;
143         }
144         size -= sizeof(__u32) + cfs_size_round4(token_size);
145
146         req->rq_reqdata_len = lustre_shrink_msg(req->rq_reqbuf, offset,
147                                                 msg->lm_buflens[offset] - size, 0);
148         return 0;
149 }
150
151 static
152 int ctx_init_parse_reply(struct lustre_msg *msg, int swabbed,
153                          char __user *outbuf, long outlen)
154 {
155         struct gss_rep_header   *ghdr;
156         __u32                    obj_len, round_len;
157         __u32                    status, effective = 0;
158
159         if (msg->lm_bufcount != 3) {
160                 CERROR("unexpected bufcount %u\n", msg->lm_bufcount);
161                 return -EPROTO;
162         }
163
164         ghdr = (struct gss_rep_header *) gss_swab_header(msg, 0, swabbed);
165         if (ghdr == NULL) {
166                 CERROR("unable to extract gss reply header\n");
167                 return -EPROTO;
168         }
169
170         if (ghdr->gh_version != PTLRPC_GSS_VERSION) {
171                 CERROR("invalid gss version %u\n", ghdr->gh_version);
172                 return -EPROTO;
173         }
174
175         if (outlen < (4 + 2) * 4 + cfs_size_round4(ghdr->gh_handle.len) +
176                      cfs_size_round4(msg->lm_buflens[2])) {
177                 CERROR("output buffer size %ld too small\n", outlen);
178                 return -EFAULT;
179         }
180
181         status = 0;
182         effective = 0;
183
184         if (cfs_copy_to_user(outbuf, &status, 4))
185                 return -EFAULT;
186         outbuf += 4;
187         if (cfs_copy_to_user(outbuf, &ghdr->gh_major, 4))
188                 return -EFAULT;
189         outbuf += 4;
190         if (cfs_copy_to_user(outbuf, &ghdr->gh_minor, 4))
191                 return -EFAULT;
192         outbuf += 4;
193         if (cfs_copy_to_user(outbuf, &ghdr->gh_seqwin, 4))
194                 return -EFAULT;
195         outbuf += 4;
196         effective += 4 * 4;
197
198         /* handle */
199         obj_len = ghdr->gh_handle.len;
200         round_len = (obj_len + 3) & ~ 3;
201         if (cfs_copy_to_user(outbuf, &obj_len, 4))
202                 return -EFAULT;
203         outbuf += 4;
204         if (cfs_copy_to_user(outbuf, (char *) ghdr->gh_handle.data, round_len))
205                 return -EFAULT;
206         outbuf += round_len;
207         effective += 4 + round_len;
208
209         /* out token */
210         obj_len = msg->lm_buflens[2];
211         round_len = (obj_len + 3) & ~ 3;
212         if (cfs_copy_to_user(outbuf, &obj_len, 4))
213                 return -EFAULT;
214         outbuf += 4;
215         if (cfs_copy_to_user(outbuf, lustre_msg_buf(msg, 2, 0), round_len))
216                 return -EFAULT;
217         outbuf += round_len;
218         effective += 4 + round_len;
219
220         return effective;
221 }
222
223 /* XXX move to where lgssd could see */
224 struct lgssd_ioctl_param {
225         int             version;        /* in   */
226         int             secid;          /* in   */
227         char           *uuid;           /* in   */
228         int             lustre_svc;     /* in   */
229         uid_t           uid;            /* in   */
230         gid_t           gid;            /* in   */
231         long            send_token_size;/* in   */
232         char           *send_token;     /* in   */
233         long            reply_buf_size; /* in   */
234         char           *reply_buf;      /* in   */
235         long            status;         /* out  */
236         long            reply_length;   /* out  */
237 };
238
239 int gss_do_ctx_init_rpc(__user char *buffer, unsigned long count)
240 {
241         struct obd_import        *imp;
242         struct ptlrpc_request    *req;
243         struct lgssd_ioctl_param  param;
244         struct obd_device        *obd;
245         char                      obdname[64];
246         long                      lsize;
247         int                       rc;
248
249         if (count != sizeof(param)) {
250                 CERROR("ioctl size %lu, expect %lu, please check lgss_keyring "
251                        "version\n", count, (unsigned long) sizeof(param));
252                 RETURN(-EINVAL);
253         }
254         if (cfs_copy_from_user(&param, buffer, sizeof(param))) {
255                 CERROR("failed copy data from lgssd\n");
256                 RETURN(-EFAULT);
257         }
258
259         if (param.version != GSSD_INTERFACE_VERSION) {
260                 CERROR("gssd interface version %d (expect %d)\n",
261                         param.version, GSSD_INTERFACE_VERSION);
262                 RETURN(-EINVAL);
263         }
264
265         /* take name */
266         if (strncpy_from_user(obdname, param.uuid, sizeof(obdname)) <= 0) {
267                 CERROR("Invalid obdname pointer\n");
268                 RETURN(-EFAULT);
269         }
270
271         obd = class_name2obd(obdname);
272         if (!obd) {
273                 CERROR("no such obd %s\n", obdname);
274                 RETURN(-EINVAL);
275         }
276
277         if (unlikely(!obd->obd_set_up)) {
278                 CERROR("obd %s not setup\n", obdname);
279                 RETURN(-EINVAL);
280         }
281
282         cfs_spin_lock(&obd->obd_dev_lock);
283         if (obd->obd_stopping) {
284                 CERROR("obd %s has stopped\n", obdname);
285                 cfs_spin_unlock(&obd->obd_dev_lock);
286                 RETURN(-EINVAL);
287         }
288
289         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
290             strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) &&
291             strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME)) {
292                 CERROR("obd %s is not a client device\n", obdname);
293                 cfs_spin_unlock(&obd->obd_dev_lock);
294                 RETURN(-EINVAL);
295         }
296         cfs_spin_unlock(&obd->obd_dev_lock);
297
298         cfs_down_read(&obd->u.cli.cl_sem);
299         if (obd->u.cli.cl_import == NULL) {
300                 CERROR("obd %s: import has gone\n", obd->obd_name);
301                 RETURN(-EINVAL);
302         }
303         imp = class_import_get(obd->u.cli.cl_import);
304         cfs_up_read(&obd->u.cli.cl_sem);
305
306         if (imp->imp_deactive) {
307                 CERROR("import has been deactivated\n");
308                 class_import_put(imp);
309                 RETURN(-EINVAL);
310         }
311
312         req = ptlrpc_request_alloc_pack(imp, &RQF_SEC_CTX, LUSTRE_OBD_VERSION,
313                                         SEC_CTX_INIT);
314         if (req == NULL) {
315                 param.status = -ENOMEM;
316                 goto out_copy;
317         }
318
319         if (req->rq_cli_ctx->cc_sec->ps_id != param.secid) {
320                 CWARN("original secid %d, now has changed to %d, "
321                       "cancel this negotiation\n", param.secid,
322                       req->rq_cli_ctx->cc_sec->ps_id);
323                 param.status = -EINVAL;
324                 goto out_copy;
325         }
326
327         /* get token */
328         rc = ctx_init_pack_request(imp, req,
329                                    param.lustre_svc,
330                                    param.uid, param.gid,
331                                    param.send_token_size,
332                                    param.send_token);
333         if (rc) {
334                 param.status = rc;
335                 goto out_copy;
336         }
337
338         ptlrpc_request_set_replen(req);
339
340         rc = ptlrpc_queue_wait(req);
341         if (rc) {
342                 /* If any _real_ denial be made, we expect server return
343                  * -EACCES reply or return success but indicate gss error
344                  * inside reply messsage. All other errors are treated as
345                  * timeout, caller might try the negotiation repeatedly,
346                  * leave recovery decisions to general ptlrpc layer.
347                  *
348                  * FIXME maybe some other error code shouldn't be treated
349                  * as timeout. */
350                 param.status = rc;
351                 if (rc != -EACCES)
352                         param.status = -ETIMEDOUT;
353                 goto out_copy;
354         }
355
356         LASSERT(req->rq_repdata);
357         lsize = ctx_init_parse_reply(req->rq_repdata,
358                                      ptlrpc_rep_need_swab(req),
359                                      param.reply_buf, param.reply_buf_size);
360         if (lsize < 0) {
361                 param.status = (int) lsize;
362                 goto out_copy;
363         }
364
365         param.status = 0;
366         param.reply_length = lsize;
367
368 out_copy:
369         if (cfs_copy_to_user(buffer, &param, sizeof(param)))
370                 rc = -EFAULT;
371         else
372                 rc = 0;
373
374         class_import_put(imp);
375         ptlrpc_req_finished(req);
376         RETURN(rc);
377 }
378
379 int gss_do_ctx_fini_rpc(struct gss_cli_ctx *gctx)
380 {
381         struct ptlrpc_cli_ctx   *ctx = &gctx->gc_base;
382         struct obd_import       *imp = ctx->cc_sec->ps_import;
383         struct ptlrpc_request   *req;
384         struct ptlrpc_user_desc *pud;
385         int                      rc;
386         ENTRY;
387
388         LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
389
390         if (cli_ctx_is_error(ctx) || !cli_ctx_is_uptodate(ctx)) {
391                 CDEBUG(D_SEC, "ctx %p(%u->%s) not uptodate, "
392                        "don't send destroy rpc\n", ctx,
393                        ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
394                 RETURN(0);
395         }
396
397         cfs_might_sleep();
398
399         CWARN("%s ctx %p idx "LPX64" (%u->%s)\n",
400               sec_is_reverse(ctx->cc_sec) ?
401               "server finishing reverse" : "client finishing forward",
402               ctx, gss_handle_to_u64(&gctx->gc_handle),
403               ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
404
405         gctx->gc_proc = PTLRPC_GSS_PROC_DESTROY;
406
407         req = ptlrpc_request_alloc(imp, &RQF_SEC_CTX);
408         if (req == NULL) {
409                 CWARN("ctx %p(%u): fail to prepare rpc, destroy locally\n",
410                       ctx, ctx->cc_vcred.vc_uid);
411                 GOTO(out, rc = -ENOMEM);
412         }
413
414         rc = ptlrpc_request_bufs_pack(req, LUSTRE_OBD_VERSION, SEC_CTX_FINI,
415                                       NULL, ctx);
416         if (rc) {
417                 ptlrpc_request_free(req);
418                 GOTO(out_ref, rc);
419         }
420
421         /* fix the user desc */
422         if (req->rq_pack_udesc) {
423                 /* we rely the fact that this request is in AUTH mode,
424                  * and user_desc at offset 2. */
425                 pud = lustre_msg_buf(req->rq_reqbuf, 2, sizeof(*pud));
426                 LASSERT(pud);
427                 pud->pud_uid = pud->pud_fsuid = ctx->cc_vcred.vc_uid;
428                 pud->pud_gid = pud->pud_fsgid = ctx->cc_vcred.vc_gid;
429                 pud->pud_cap = 0;
430                 pud->pud_ngroups = 0;
431         }
432
433         req->rq_phase = RQ_PHASE_RPC;
434         rc = ptl_send_rpc(req, 1);
435         if (rc)
436                 CWARN("ctx %p(%u->%s): rpc error %d, destroy locally\n", ctx,
437                       ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec), rc);
438
439 out_ref:
440         ptlrpc_req_finished(req);
441 out:
442         RETURN(rc);
443 }
444
445 int __init gss_init_cli_upcall(void)
446 {
447         return 0;
448 }
449
450 void __exit gss_exit_cli_upcall(void)
451 {
452 }