Whamcloud - gitweb
branch: HEAD
[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  *  Copyright (C) 2006 Cluster File Systems, Inc.
5  *   Author: Eric Mei <ericm@clusterfs.com>
6  *
7  *   This file is part of the Lustre file system, http://www.lustre.org
8  *   Lustre is a trademark of Cluster File Systems, Inc.
9  *
10  *   You may have signed or agreed to another license before downloading
11  *   this software.  If so, you are bound by the terms and conditions
12  *   of that agreement, and the following does not apply to you.  See the
13  *   LICENSE file included with this distribution for more information.
14  *
15  *   If you did not agree to a different license, then this copy of Lustre
16  *   is open source software; you can redistribute it and/or modify it
17  *   under the terms of version 2 of the GNU General Public License as
18  *   published by the Free Software Foundation.
19  *
20  *   In either case, Lustre is distributed in the hope that it will be
21  *   useful, but WITHOUT ANY WARRANTY; without even the implied warranty
22  *   of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23  *   license text for more details.
24  *
25  */
26
27 #ifndef EXPORT_SYMTAB
28 # define EXPORT_SYMTAB
29 #endif
30 #define DEBUG_SUBSYSTEM S_SEC
31 #ifdef __KERNEL__
32 #include <linux/init.h>
33 #include <linux/module.h>
34 #include <linux/slab.h>
35 #include <linux/dcache.h>
36 #include <linux/fs.h>
37 #include <linux/mutex.h>
38 #include <linux/random.h>
39 #else
40 #include <liblustre.h>
41 #endif
42
43 #include <obd.h>
44 #include <obd_class.h>
45 #include <obd_support.h>
46 #include <lustre/lustre_idl.h>
47 #include <lustre_net.h>
48 #include <lustre_import.h>
49 #include <lustre_sec.h>
50
51 #include "gss_err.h"
52 #include "gss_internal.h"
53 #include "gss_api.h"
54
55 /**********************************************
56  * gss context init/fini helper               *
57  **********************************************/
58
59 static
60 int ctx_init_pack_request(struct obd_import *imp,
61                           struct ptlrpc_request *req,
62                           int lustre_srv,
63                           uid_t uid, gid_t gid,
64                           long token_size,
65                           char __user *token)
66 {
67         struct lustre_msg       *msg = req->rq_reqbuf;
68         struct gss_sec          *gsec;
69         struct gss_header       *ghdr;
70         struct ptlrpc_user_desc *pud;
71         __u32                   *p, size, offset = 2;
72         rawobj_t                 obj;
73
74         LASSERT(msg->lm_bufcount <= 4);
75         LASSERT(req->rq_cli_ctx);
76         LASSERT(req->rq_cli_ctx->cc_sec);
77
78         /* gss hdr */
79         ghdr = lustre_msg_buf(msg, 0, sizeof(*ghdr));
80         ghdr->gh_version = PTLRPC_GSS_VERSION;
81         ghdr->gh_flags = 0;
82         ghdr->gh_proc = PTLRPC_GSS_PROC_INIT;
83         ghdr->gh_seq = 0;
84         ghdr->gh_svc = SPTLRPC_SVC_NULL;
85         ghdr->gh_handle.len = 0;
86
87         /* fix the user desc */
88         if (req->rq_pack_udesc) {
89                 ghdr->gh_flags |= LUSTRE_GSS_PACK_USER;
90
91                 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
92                 LASSERT(pud);
93                 pud->pud_uid = pud->pud_fsuid = uid;
94                 pud->pud_gid = pud->pud_fsgid = gid;
95                 pud->pud_cap = 0;
96                 pud->pud_ngroups = 0;
97                 offset++;
98         }
99
100         /* security payload */
101         p = lustre_msg_buf(msg, offset, 0);
102         size = msg->lm_buflens[offset];
103         LASSERT(p);
104
105         /* 1. lustre svc type */
106         LASSERT(size > 4);
107         *p++ = cpu_to_le32(lustre_srv);
108         size -= 4;
109
110         /* 2. target uuid */
111         obj.len = strlen(imp->imp_obd->u.cli.cl_target_uuid.uuid) + 1;
112         obj.data = imp->imp_obd->u.cli.cl_target_uuid.uuid;
113         if (rawobj_serialize(&obj, &p, &size))
114                 LBUG();
115
116         /* 3. reverse context handle. actually only needed by root user,
117          *    but we send it anyway. */
118         gsec = sec2gsec(req->rq_cli_ctx->cc_sec);
119         obj.len = sizeof(gsec->gs_rvs_hdl);
120         obj.data = (__u8 *) &gsec->gs_rvs_hdl;
121         if (rawobj_serialize(&obj, &p, &size))
122                 LBUG();
123
124         /* 4. now the token */
125         LASSERT(size >= (sizeof(__u32) + token_size));
126         *p++ = cpu_to_le32(((__u32) token_size));
127         if (copy_from_user(p, token, token_size)) {
128                 CERROR("can't copy token\n");
129                 return -EFAULT;
130         }
131         size -= sizeof(__u32) + size_round4(token_size);
132
133         req->rq_reqdata_len = lustre_shrink_msg(req->rq_reqbuf, offset,
134                                                 msg->lm_buflens[offset] - size, 0);
135         return 0;
136 }
137
138 static
139 int ctx_init_parse_reply(struct lustre_msg *msg,
140                          char __user *outbuf, long outlen)
141 {
142         struct gss_rep_header   *ghdr;
143         __u32                    obj_len, round_len;
144         __u32                    status, effective = 0;
145
146         if (msg->lm_bufcount != 3) {
147                 CERROR("unexpected bufcount %u\n", msg->lm_bufcount);
148                 return -EPROTO;
149         }
150
151         ghdr = (struct gss_rep_header *) gss_swab_header(msg, 0);
152         if (ghdr == NULL) {
153                 CERROR("unable to extract gss reply header\n");
154                 return -EPROTO;
155         }
156
157         if (ghdr->gh_version != PTLRPC_GSS_VERSION) {
158                 CERROR("invalid gss version %u\n", ghdr->gh_version);
159                 return -EPROTO;
160         }
161
162         if (outlen < (4 + 2) * 4 + size_round4(ghdr->gh_handle.len) +
163                      size_round4(msg->lm_buflens[2])) {
164                 CERROR("output buffer size %ld too small\n", outlen);
165                 return -EFAULT;
166         }
167
168         status = 0;
169         effective = 0;
170
171         if (copy_to_user(outbuf, &status, 4))
172                 return -EFAULT;
173         outbuf += 4;
174         if (copy_to_user(outbuf, &ghdr->gh_major, 4))
175                 return -EFAULT;
176         outbuf += 4;
177         if (copy_to_user(outbuf, &ghdr->gh_minor, 4))
178                 return -EFAULT;
179         outbuf += 4;
180         if (copy_to_user(outbuf, &ghdr->gh_seqwin, 4))
181                 return -EFAULT;
182         outbuf += 4;
183         effective += 4 * 4;
184
185         /* handle */
186         obj_len = ghdr->gh_handle.len;
187         round_len = (obj_len + 3) & ~ 3;
188         if (copy_to_user(outbuf, &obj_len, 4))
189                 return -EFAULT;
190         outbuf += 4;
191         if (copy_to_user(outbuf, (char *) ghdr->gh_handle.data, round_len))
192                 return -EFAULT;
193         outbuf += round_len;
194         effective += 4 + round_len;
195
196         /* out token */
197         obj_len = msg->lm_buflens[2];
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, lustre_msg_buf(msg, 2, 0), round_len))
203                 return -EFAULT;
204         outbuf += round_len;
205         effective += 4 + round_len;
206
207         return effective;
208 }
209
210 /* XXX move to where lgssd could see */
211 struct lgssd_ioctl_param {
212         int             version;        /* in   */
213         int             secid;          /* in   */
214         char           *uuid;           /* in   */
215         int             lustre_svc;     /* in   */
216         uid_t           uid;            /* in   */
217         gid_t           gid;            /* in   */
218         long            send_token_size;/* in   */
219         char           *send_token;     /* in   */
220         long            reply_buf_size; /* in   */
221         char           *reply_buf;      /* in   */
222         long            status;         /* out  */
223         long            reply_length;   /* out  */
224 };
225
226 int gss_do_ctx_init_rpc(__user char *buffer, unsigned long count)
227 {
228         struct obd_import        *imp;
229         struct ptlrpc_request    *req;
230         struct lgssd_ioctl_param  param;
231         struct obd_device        *obd;
232         char                      obdname[64];
233         long                      lsize;
234         int                       rc;
235
236         if (count != sizeof(param)) {
237                 CERROR("ioctl size %lu, expect %lu, please check lgssd version\n",
238                         count, (unsigned long) sizeof(param));
239                 RETURN(-EINVAL);
240         }
241         if (copy_from_user(&param, buffer, sizeof(param))) {
242                 CERROR("failed copy data from lgssd\n");
243                 RETURN(-EFAULT);
244         }
245
246         if (param.version != GSSD_INTERFACE_VERSION) {
247                 CERROR("gssd interface version %d (expect %d)\n",
248                         param.version, GSSD_INTERFACE_VERSION);
249                 RETURN(-EINVAL);
250         }
251
252         /* take name */
253         if (strncpy_from_user(obdname, param.uuid, sizeof(obdname)) <= 0) {
254                 CERROR("Invalid obdname pointer\n");
255                 RETURN(-EFAULT);
256         }
257
258         obd = class_name2obd(obdname);
259         if (!obd) {
260                 CERROR("no such obd %s\n", obdname);
261                 RETURN(-EINVAL);
262         }
263
264         imp = class_import_get(obd->u.cli.cl_import);
265         LASSERT(imp->imp_sec);
266
267         /* force this import to use v2 msg */
268         imp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
269
270         req = ptlrpc_request_alloc_pack(imp, &RQF_SEC_CTX, LUSTRE_OBD_VERSION,
271                                         SEC_CTX_INIT);
272         if (req == NULL) {
273                 param.status = -ENOMEM;
274                 goto out_copy;
275         }
276
277         if (req->rq_cli_ctx->cc_sec->ps_id != param.secid) {
278                 CWARN("original secid %d, now has changed to %d, "
279                       "cancel this negotiation\n", param.secid,
280                       req->rq_cli_ctx->cc_sec->ps_id);
281                 param.status = -EINVAL;
282                 goto out_copy;
283         }
284
285         /* get token */
286         rc = ctx_init_pack_request(imp, req,
287                                    param.lustre_svc,
288                                    param.uid, param.gid,
289                                    param.send_token_size,
290                                    param.send_token);
291         if (rc) {
292                 param.status = rc;
293                 goto out_copy;
294         }
295
296         ptlrpc_request_set_replen(req);
297
298         rc = ptlrpc_queue_wait(req);
299         if (rc) {
300                 /* If any _real_ denial be made, we expect server return
301                  * -EACCES reply or return success but indicate gss error
302                  * inside reply messsage. All other errors are treated as
303                  * timeout, caller might try the negotiation repeatedly,
304                  * leave recovery decisions to general ptlrpc layer.
305                  *
306                  * FIXME maybe some other error code shouldn't be treated
307                  * as timeout. */
308                 param.status = rc;
309                 if (rc != -EACCES)
310                         param.status = -ETIMEDOUT;
311                 goto out_copy;
312         }
313
314         lsize = ctx_init_parse_reply(req->rq_repbuf,
315                                      param.reply_buf, param.reply_buf_size);
316         if (lsize < 0) {
317                 param.status = (int) lsize;
318                 goto out_copy;
319         }
320
321         param.status = 0;
322         param.reply_length = lsize;
323
324 out_copy:
325         if (copy_to_user(buffer, &param, sizeof(param)))
326                 rc = -EFAULT;
327         else
328                 rc = 0;
329
330         class_import_put(imp);
331         ptlrpc_req_finished(req);
332         RETURN(rc);
333 }
334
335 int gss_do_ctx_fini_rpc(struct gss_cli_ctx *gctx)
336 {
337         struct ptlrpc_cli_ctx   *ctx = &gctx->gc_base;
338         struct obd_import       *imp = ctx->cc_sec->ps_import;
339         struct ptlrpc_request   *req;
340         struct ptlrpc_user_desc *pud;
341         int                      rc;
342         ENTRY;
343
344         LASSERT(atomic_read(&ctx->cc_refcount) > 0);
345
346         if (cli_ctx_is_error(ctx) || !cli_ctx_is_uptodate(ctx)) {
347                 CDEBUG(D_SEC, "ctx %p(%u->%s) not uptodate, "
348                        "don't send destroy rpc\n", ctx,
349                        ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
350                 RETURN(0);
351         }
352
353         might_sleep();
354
355         CWARN("%s ctx %p idx "LPX64" (%u->%s)\n",
356               sec_is_reverse(ctx->cc_sec) ?
357               "server finishing reverse" : "client finishing forward",
358               ctx, gss_handle_to_u64(&gctx->gc_handle),
359               ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
360
361         gctx->gc_proc = PTLRPC_GSS_PROC_DESTROY;
362
363         req = ptlrpc_request_alloc(imp, &RQF_SEC_CTX);
364         if (req == NULL) {
365                 CWARN("ctx %p(%u): fail to prepare rpc, destroy locally\n",
366                       ctx, ctx->cc_vcred.vc_uid);
367                 GOTO(out, rc = -ENOMEM);
368         }
369
370         rc = ptlrpc_request_bufs_pack(req, LUSTRE_OBD_VERSION, SEC_CTX_FINI,
371                                       NULL, ctx);
372         if (rc) {
373                 ptlrpc_request_free(req);
374                 GOTO(out_ref, rc);
375         }
376
377         /* fix the user desc */
378         if (req->rq_pack_udesc) {
379                 /* we rely the fact that this request is in AUTH mode,
380                  * and user_desc at offset 2. */
381                 pud = lustre_msg_buf(req->rq_reqbuf, 2, sizeof(*pud));
382                 LASSERT(pud);
383                 pud->pud_uid = pud->pud_fsuid = ctx->cc_vcred.vc_uid;
384                 pud->pud_gid = pud->pud_fsgid = ctx->cc_vcred.vc_gid;
385                 pud->pud_cap = 0;
386                 pud->pud_ngroups = 0;
387         }
388
389         req->rq_phase = RQ_PHASE_RPC;
390         rc = ptl_send_rpc(req, 1);
391         if (rc) {
392                 CWARN("ctx %p(%u->%s): rpc error %d, destroy locally\n",
393                       ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec),
394                       rc);
395         }
396
397 out_ref:
398         ptlrpc_req_finished(req);
399 out:
400         RETURN(rc);
401 }
402
403 int __init gss_init_cli_upcall(void)
404 {
405         return 0;
406 }
407
408 void __exit gss_exit_cli_upcall(void)
409 {
410 }