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