Whamcloud - gitweb
b36c99ae5f7439ef134b709465e84fa562069134
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_cli_upcall.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
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.
9  *
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).
15  *
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
19  *
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
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, 2012, Intel Corporation.
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 #define DEBUG_SUBSYSTEM S_SEC
42 #ifdef __KERNEL__
43 #include <linux/init.h>
44 #include <linux/module.h>
45 #include <linux/slab.h>
46 #include <linux/dcache.h>
47 #include <linux/fs.h>
48 #include <linux/mutex.h>
49 #else
50 #include <liblustre.h>
51 #endif
52
53 #include <obd.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>
60
61 #include "gss_err.h"
62 #include "gss_internal.h"
63 #include "gss_api.h"
64
65 /**********************************************
66  * gss context init/fini helper               *
67  **********************************************/
68
69 static
70 int ctx_init_pack_request(struct obd_import *imp,
71                           struct ptlrpc_request *req,
72                           int lustre_srv,
73                           uid_t uid, gid_t gid,
74                           long token_size,
75                           char __user *token)
76 {
77         struct lustre_msg       *msg = req->rq_reqbuf;
78         struct gss_sec          *gsec;
79         struct gss_header       *ghdr;
80         struct ptlrpc_user_desc *pud;
81         __u32                   *p, size, offset = 2;
82         rawobj_t                 obj;
83
84         LASSERT(msg->lm_bufcount <= 4);
85         LASSERT(req->rq_cli_ctx);
86         LASSERT(req->rq_cli_ctx->cc_sec);
87
88         /* gss hdr */
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;
92         ghdr->gh_flags = 0;
93         ghdr->gh_proc = PTLRPC_GSS_PROC_INIT;
94         ghdr->gh_seq = 0;
95         ghdr->gh_svc = SPTLRPC_SVC_NULL;
96         ghdr->gh_handle.len = 0;
97
98         /* fix the user desc */
99         if (req->rq_pack_udesc) {
100                 ghdr->gh_flags |= LUSTRE_GSS_PACK_USER;
101
102                 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
103                 LASSERT(pud);
104                 pud->pud_uid = pud->pud_fsuid = uid;
105                 pud->pud_gid = pud->pud_fsgid = gid;
106                 pud->pud_cap = 0;
107                 pud->pud_ngroups = 0;
108                 offset++;
109         }
110
111         /* security payload */
112         p = lustre_msg_buf(msg, offset, 0);
113         size = msg->lm_buflens[offset];
114         LASSERT(p);
115
116         /* 1. lustre svc type */
117         LASSERT(size > 4);
118         *p++ = cpu_to_le32(lustre_srv);
119         size -= 4;
120
121         /* 2. target uuid */
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))
125                 LBUG();
126
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))
133                 LBUG();
134
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");
140                 return -EFAULT;
141         }
142         size -= sizeof(__u32) + cfs_size_round4(token_size);
143
144         req->rq_reqdata_len = lustre_shrink_msg(req->rq_reqbuf, offset,
145                                                 msg->lm_buflens[offset] - size, 0);
146         return 0;
147 }
148
149 static
150 int ctx_init_parse_reply(struct lustre_msg *msg, int swabbed,
151                          char __user *outbuf, long outlen)
152 {
153         struct gss_rep_header   *ghdr;
154         __u32                    obj_len, round_len;
155         __u32                    status, effective = 0;
156
157         if (msg->lm_bufcount != 3) {
158                 CERROR("unexpected bufcount %u\n", msg->lm_bufcount);
159                 return -EPROTO;
160         }
161
162         ghdr = (struct gss_rep_header *) gss_swab_header(msg, 0, swabbed);
163         if (ghdr == NULL) {
164                 CERROR("unable to extract gss reply header\n");
165                 return -EPROTO;
166         }
167
168         if (ghdr->gh_version != PTLRPC_GSS_VERSION) {
169                 CERROR("invalid gss version %u\n", ghdr->gh_version);
170                 return -EPROTO;
171         }
172
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);
176                 return -EFAULT;
177         }
178
179         status = 0;
180         effective = 0;
181
182         if (copy_to_user(outbuf, &status, 4))
183                 return -EFAULT;
184         outbuf += 4;
185         if (copy_to_user(outbuf, &ghdr->gh_major, 4))
186                 return -EFAULT;
187         outbuf += 4;
188         if (copy_to_user(outbuf, &ghdr->gh_minor, 4))
189                 return -EFAULT;
190         outbuf += 4;
191         if (copy_to_user(outbuf, &ghdr->gh_seqwin, 4))
192                 return -EFAULT;
193         outbuf += 4;
194         effective += 4 * 4;
195
196         /* handle */
197         obj_len = ghdr->gh_handle.len;
198         round_len = (obj_len + 3) & ~3;
199         if (copy_to_user(outbuf, &obj_len, 4))
200                 return -EFAULT;
201         outbuf += 4;
202         if (copy_to_user(outbuf, (char *) ghdr->gh_handle.data, round_len))
203                 return -EFAULT;
204         outbuf += round_len;
205         effective += 4 + round_len;
206
207         /* out token */
208         obj_len = msg->lm_buflens[2];
209         round_len = (obj_len + 3) & ~3;
210         if (copy_to_user(outbuf, &obj_len, 4))
211                 return -EFAULT;
212         outbuf += 4;
213         if (copy_to_user(outbuf, lustre_msg_buf(msg, 2, 0), round_len))
214                 return -EFAULT;
215         outbuf += round_len;
216         effective += 4 + round_len;
217
218         return effective;
219 }
220
221 /* XXX move to where lgssd could see */
222 struct lgssd_ioctl_param {
223         int             version;        /* in   */
224         int             secid;          /* in   */
225         char           *uuid;           /* in   */
226         int             lustre_svc;     /* in   */
227         uid_t           uid;            /* in   */
228         gid_t           gid;            /* 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  */
235 };
236
237 int gss_do_ctx_init_rpc(__user char *buffer, unsigned long count)
238 {
239         struct obd_import        *imp;
240         struct ptlrpc_request    *req;
241         struct lgssd_ioctl_param  param;
242         struct obd_device        *obd;
243         char                      obdname[64];
244         long                      lsize;
245         int                       rc;
246
247         if (count != sizeof(param)) {
248                 CERROR("ioctl size %lu, expect %lu, please check lgss_keyring "
249                        "version\n", count, (unsigned long) sizeof(param));
250                 RETURN(-EINVAL);
251         }
252         if (copy_from_user(&param, buffer, sizeof(param))) {
253                 CERROR("failed copy data from lgssd\n");
254                 RETURN(-EFAULT);
255         }
256
257         if (param.version != GSSD_INTERFACE_VERSION) {
258                 CERROR("gssd interface version %d (expect %d)\n",
259                         param.version, GSSD_INTERFACE_VERSION);
260                 RETURN(-EINVAL);
261         }
262
263         /* take name */
264         if (strncpy_from_user(obdname, param.uuid, sizeof(obdname)) <= 0) {
265                 CERROR("Invalid obdname pointer\n");
266                 RETURN(-EFAULT);
267         }
268
269         obd = class_name2obd(obdname);
270         if (!obd) {
271                 CERROR("no such obd %s\n", obdname);
272                 RETURN(-EINVAL);
273         }
274
275         if (unlikely(!obd->obd_set_up)) {
276                 CERROR("obd %s not setup\n", obdname);
277                 RETURN(-EINVAL);
278         }
279
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);
284                 RETURN(-EINVAL);
285         }
286
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);
292                 RETURN(-EINVAL);
293         }
294         spin_unlock(&obd->obd_dev_lock);
295
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);
300                 RETURN(-EINVAL);
301         }
302         imp = class_import_get(obd->u.cli.cl_import);
303         up_read(&obd->u.cli.cl_sem);
304
305         if (imp->imp_deactive) {
306                 CERROR("import has been deactivated\n");
307                 class_import_put(imp);
308                 RETURN(-EINVAL);
309         }
310
311         req = ptlrpc_request_alloc_pack(imp, &RQF_SEC_CTX, LUSTRE_OBD_VERSION,
312                                         SEC_CTX_INIT);
313         if (req == NULL) {
314                 param.status = -ENOMEM;
315                 goto out_copy;
316         }
317
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;
323                 goto out_copy;
324         }
325
326         /* get token */
327         rc = ctx_init_pack_request(imp, req,
328                                    param.lustre_svc,
329                                    param.uid, param.gid,
330                                    param.send_token_size,
331                                    param.send_token);
332         if (rc) {
333                 param.status = rc;
334                 goto out_copy;
335         }
336
337         ptlrpc_request_set_replen(req);
338
339         rc = ptlrpc_queue_wait(req);
340         if (rc) {
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.
346                  *
347                  * FIXME maybe some other error code shouldn't be treated
348                  * as timeout. */
349                 param.status = rc;
350                 if (rc != -EACCES)
351                         param.status = -ETIMEDOUT;
352                 goto out_copy;
353         }
354
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);
359         if (lsize < 0) {
360                 param.status = (int) lsize;
361                 goto out_copy;
362         }
363
364         param.status = 0;
365         param.reply_length = lsize;
366
367 out_copy:
368         if (copy_to_user(buffer, &param, sizeof(param)))
369                 rc = -EFAULT;
370         else
371                 rc = 0;
372
373         class_import_put(imp);
374         ptlrpc_req_finished(req);
375         RETURN(rc);
376 }
377
378 int gss_do_ctx_fini_rpc(struct gss_cli_ctx *gctx)
379 {
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;
384         int                      rc;
385         ENTRY;
386
387         LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
388
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));
393                 RETURN(0);
394         }
395
396         might_sleep();
397
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));
403
404         gctx->gc_proc = PTLRPC_GSS_PROC_DESTROY;
405
406         req = ptlrpc_request_alloc(imp, &RQF_SEC_CTX);
407         if (req == NULL) {
408                 CWARN("ctx %p(%u): fail to prepare rpc, destroy locally\n",
409                       ctx, ctx->cc_vcred.vc_uid);
410                 GOTO(out, rc = -ENOMEM);
411         }
412
413         rc = ptlrpc_request_bufs_pack(req, LUSTRE_OBD_VERSION, SEC_CTX_FINI,
414                                       NULL, ctx);
415         if (rc) {
416                 ptlrpc_request_free(req);
417                 GOTO(out_ref, rc);
418         }
419
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));
425                 LASSERT(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;
428                 pud->pud_cap = 0;
429                 pud->pud_ngroups = 0;
430         }
431
432         req->rq_phase = RQ_PHASE_RPC;
433         rc = ptl_send_rpc(req, 1);
434         if (rc)
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);
437
438 out_ref:
439         ptlrpc_req_finished(req);
440 out:
441         RETURN(rc);
442 }
443
444 int __init gss_init_cli_upcall(void)
445 {
446         return 0;
447 }
448
449 void __exit gss_exit_cli_upcall(void)
450 {
451 }