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