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
76         /* gss hdr */
77         ghdr = lustre_msg_buf(msg, 0, sizeof(*ghdr));
78         ghdr->gh_version = PTLRPC_GSS_VERSION;
79         ghdr->gh_flags = 0;
80         ghdr->gh_proc = PTLRPC_GSS_PROC_INIT;
81         ghdr->gh_seq = 0;
82         ghdr->gh_svc = SPTLRPC_SVC_NULL;
83         ghdr->gh_handle.len = 0;
84
85         /* fix the user desc */
86         if (SEC_FLAVOR_HAS_USER(req->rq_sec_flavor)) {
87                 pud = lustre_msg_buf(msg, offset, sizeof(*pud));
88                 LASSERT(pud);
89                 pud->pud_uid = pud->pud_fsuid = uid;
90                 pud->pud_gid = pud->pud_fsgid = gid;
91                 pud->pud_cap = 0;
92                 pud->pud_ngroups = 0;
93                 offset++;
94         }
95
96         /* security payload */
97         p = lustre_msg_buf(msg, offset, 0);
98         size = msg->lm_buflens[offset];
99
100         /* 1. lustre svc type */
101         LASSERT(size > 4);
102         *p++ = cpu_to_le32(lustre_srv);
103         size -= 4;
104
105         /* 2. target uuid */
106         obj.len = strlen(imp->imp_obd->u.cli.cl_target_uuid.uuid) + 1;
107         obj.data = imp->imp_obd->u.cli.cl_target_uuid.uuid;
108         if (rawobj_serialize(&obj, &p, &size))
109                 LBUG();
110
111         /* 3. reverse context handle. actually only needed by root user,
112          *    but we send it anyway.
113          */
114         gsec = container_of(imp->imp_sec, struct gss_sec, gs_base);
115         obj.len = sizeof(gsec->gs_rvs_hdl);
116         obj.data = (__u8 *) &gsec->gs_rvs_hdl;
117         if (rawobj_serialize(&obj, &p, &size))
118                 LBUG();
119
120         /* 4. now the token */
121         LASSERT(size >= (sizeof(__u32) + token_size));
122         *p++ = cpu_to_le32(((__u32) token_size));
123         if (copy_from_user(p, token, token_size)) {
124                 CERROR("can't copy token\n");
125                 return -EFAULT;
126         }
127         size -= sizeof(__u32) + size_round4(token_size);
128
129         req->rq_reqdata_len = lustre_shrink_msg(req->rq_reqbuf, offset,
130                                                 msg->lm_buflens[offset] - size, 0);
131         return 0;
132 }
133
134 static
135 int ctx_init_parse_reply(struct lustre_msg *msg,
136                          char __user *outbuf, long outlen)
137 {
138         struct gss_rep_header   *ghdr;
139         __u32                    obj_len, round_len;
140         __u32                    status, effective = 0;
141
142         if (msg->lm_bufcount != 3) {
143                 CERROR("unexpected bufcount %u\n", msg->lm_bufcount);
144                 return -EPROTO;
145         }
146
147         ghdr = (struct gss_rep_header *) gss_swab_header(msg, 0);
148         if (ghdr == NULL) {
149                 CERROR("unable to extract gss reply header\n");
150                 return -EPROTO;
151         }
152
153         if (ghdr->gh_version != PTLRPC_GSS_VERSION) {
154                 CERROR("invalid gss version %u\n", ghdr->gh_version);
155                 return -EPROTO;
156         }
157
158         if (outlen < (4 + 2) * 4 + size_round4(ghdr->gh_handle.len) +
159                      size_round4(msg->lm_buflens[2])) {
160                 CERROR("output buffer size %ld too small\n", outlen);
161                 return -EFAULT;
162         }
163
164         status = 0;
165         effective = 0;
166
167         if (copy_to_user(outbuf, &status, 4))
168                 return -EFAULT;
169         outbuf += 4;
170         if (copy_to_user(outbuf, &ghdr->gh_major, 4))
171                 return -EFAULT;
172         outbuf += 4;
173         if (copy_to_user(outbuf, &ghdr->gh_minor, 4))
174                 return -EFAULT;
175         outbuf += 4;
176         if (copy_to_user(outbuf, &ghdr->gh_seqwin, 4))
177                 return -EFAULT;
178         outbuf += 4;
179         effective += 4 * 4;
180
181         /* handle */
182         obj_len = ghdr->gh_handle.len;
183         round_len = (obj_len + 3) & ~ 3;
184         if (copy_to_user(outbuf, &obj_len, 4))
185                 return -EFAULT;
186         outbuf += 4;
187         if (copy_to_user(outbuf, (char *) ghdr->gh_handle.data, round_len))
188                 return -EFAULT;
189         outbuf += round_len;
190         effective += 4 + round_len;
191
192         /* out token */
193         obj_len = msg->lm_buflens[2];
194         round_len = (obj_len + 3) & ~ 3;
195         if (copy_to_user(outbuf, &obj_len, 4))
196                 return -EFAULT;
197         outbuf += 4;
198         if (copy_to_user(outbuf, lustre_msg_buf(msg, 2, 0), round_len))
199                 return -EFAULT;
200         outbuf += round_len;
201         effective += 4 + round_len;
202
203         return effective;
204 }
205
206 /* XXX move to where lgssd could see */
207 struct lgssd_ioctl_param {
208         int             version;        /* in   */
209         char           *uuid;           /* in   */
210         int             lustre_svc;     /* in   */
211         uid_t           uid;            /* in   */
212         gid_t           gid;            /* in   */
213         long            send_token_size;/* in   */
214         char           *send_token;     /* in   */
215         long            reply_buf_size; /* in   */
216         char           *reply_buf;      /* in   */
217         long            status;         /* out  */
218         long            reply_length;   /* out  */
219 };
220
221 int gss_do_ctx_init_rpc(__user char *buffer, unsigned long count)
222 {
223         struct obd_import        *imp;
224         struct ptlrpc_request    *req;
225         struct lgssd_ioctl_param  param;
226         struct obd_device        *obd;
227         char                      obdname[64];
228         long                      lsize;
229         int                       lmsg_size = sizeof(struct ptlrpc_body);
230         int                       rc;
231
232         if (count != sizeof(param)) {
233                 CERROR("ioctl size %lu, expect %lu, please check lgssd version\n",
234                         count, (unsigned long) sizeof(param));
235                 RETURN(-EINVAL);
236         }
237         if (copy_from_user(&param, buffer, sizeof(param))) {
238                 CERROR("failed copy data from lgssd\n");
239                 RETURN(-EFAULT);
240         }
241
242         if (param.version != GSSD_INTERFACE_VERSION) {
243                 CERROR("gssd interface version %d (expect %d)\n",
244                         param.version, GSSD_INTERFACE_VERSION);
245                 RETURN(-EINVAL);
246         }
247
248         /* take name */
249         if (strncpy_from_user(obdname, param.uuid, sizeof(obdname)) <= 0) {
250                 CERROR("Invalid obdname pointer\n");
251                 RETURN(-EFAULT);
252         }
253
254         obd = class_name2obd(obdname);
255         if (!obd) {
256                 CERROR("no such obd %s\n", obdname);
257                 RETURN(-EINVAL);
258         }
259
260         imp = class_import_get(obd->u.cli.cl_import);
261         LASSERT(imp->imp_sec);
262
263         /* force this import to use v2 msg */
264         imp->imp_msg_magic = LUSTRE_MSG_MAGIC_V2;
265
266         req = ptlrpc_prep_req(imp, LUSTRE_OBD_VERSION, SEC_CTX_INIT,
267                               1, &lmsg_size, NULL);
268         if (!req) {
269                 param.status = -ENOMEM;
270                 goto out_copy;
271         }
272
273         /* get token */
274         rc = ctx_init_pack_request(imp, req,
275                                    param.lustre_svc,
276                                    param.uid, param.gid,
277                                    param.send_token_size,
278                                    param.send_token);
279         if (rc) {
280                 param.status = rc;
281                 goto out_copy;
282         }
283
284         req->rq_replen = lustre_msg_size_v2(1, &lmsg_size);
285
286         rc = ptlrpc_queue_wait(req);
287         if (rc) {
288                 /* If any _real_ denial be made, we expect server return
289                  * -EACCES reply or return success but indicate gss error
290                  * inside reply messsage. All other errors are treated as
291                  * timeout, caller might try the negotiation repeatedly,
292                  * leave recovery decisions to general ptlrpc layer.
293                  *
294                  * FIXME maybe some other error code shouldn't be treated
295                  * as timeout.
296                  */
297                 param.status = rc;
298                 if (rc != -EACCES)
299                         param.status = -ETIMEDOUT;
300                 goto out_copy;
301         }
302
303         lsize = ctx_init_parse_reply(req->rq_repbuf,
304                                      param.reply_buf, param.reply_buf_size);
305         if (lsize < 0) {
306                 param.status = (int) lsize;
307                 goto out_copy;
308         }
309
310         param.status = 0;
311         param.reply_length = lsize;
312
313 out_copy:
314         if (copy_to_user(buffer, &param, sizeof(param)))
315                 rc = -EFAULT;
316         else
317                 rc = 0;
318
319         class_import_put(imp);
320         ptlrpc_req_finished(req);
321         RETURN(rc);
322 }
323
324 int gss_do_ctx_fini_rpc(struct gss_cli_ctx *gctx)
325 {
326         struct ptlrpc_cli_ctx   *ctx = &gctx->gc_base;
327         struct obd_import       *imp = ctx->cc_sec->ps_import;
328         struct ptlrpc_request   *req;
329         struct ptlrpc_user_desc *pud;
330         int                      buflens = sizeof(struct ptlrpc_body);
331         int                      rc;
332         ENTRY;
333
334         if (cli_ctx_is_error(ctx) || !cli_ctx_is_uptodate(ctx)) {
335                 CDEBUG(D_SEC, "ctx %p(%u->%s) not uptodate, "
336                        "don't send destroy rpc\n", ctx,
337                        ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
338                 RETURN(0);
339         }
340
341         might_sleep();
342
343         CDEBUG(D_SEC, "%s ctx %p(%u->%s)\n",
344                sec_is_reverse(ctx->cc_sec) ?
345                "server finishing reverse" : "client finishing forward",
346                ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
347
348         /* context's refcount could be 0, steal one */
349         atomic_inc(&ctx->cc_refcount);
350
351         gctx->gc_proc = PTLRPC_GSS_PROC_DESTROY;
352
353         req = ptlrpc_prep_req_pool(imp, LUSTRE_OBD_VERSION, SEC_CTX_FINI,
354                                    1, &buflens, NULL, NULL, ctx);
355         if (!req) {
356                 CWARN("ctx %p(%u): fail to prepare rpc, destroy locally\n",
357                       ctx, ctx->cc_vcred.vc_uid);
358                 GOTO(out_ref, rc = -ENOMEM);
359         }
360
361         /* fix the user desc */
362         if (SEC_FLAVOR_HAS_USER(req->rq_sec_flavor)) {
363                 /* we rely the fact that this request is in AUTH mode,
364                  * and user_desc at offset 2.
365                  */
366                 pud = lustre_msg_buf(req->rq_reqbuf, 2, sizeof(*pud));
367                 LASSERT(pud);
368                 pud->pud_uid = pud->pud_fsuid = ctx->cc_vcred.vc_uid;
369                 pud->pud_gid = pud->pud_fsgid = ctx->cc_vcred.vc_gid;
370                 pud->pud_cap = 0;
371                 pud->pud_ngroups = 0;
372         }
373
374         req->rq_phase = RQ_PHASE_RPC;
375         rc = ptl_send_rpc(req, 1);
376         if (rc) {
377                 CWARN("ctx %p(%u->%s): rpc error %d, destroy locally\n",
378                       ctx, ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec),
379                       rc);
380         }
381
382         ptlrpc_req_finished(req);
383 out_ref:
384         atomic_dec(&ctx->cc_refcount);
385         RETURN(rc);
386 }
387
388 int __init gss_init_cli_upcall(void)
389 {
390         return 0;
391 }
392
393 void __exit gss_exit_cli_upcall(void)
394 {
395 }