Whamcloud - gitweb
LU-645 llite: Avoid unnecessary dentry rehashing
[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 /*
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 #else
53 #include <liblustre.h>
54 #endif
55
56 #include <obd.h>
57 #include <obd_class.h>
58 #include <obd_support.h>
59 #include <lustre/lustre_idl.h>
60 #include <lustre_net.h>
61 #include <lustre_import.h>
62 #include <lustre_sec.h>
63
64 #include "gss_err.h"
65 #include "gss_internal.h"
66 #include "gss_api.h"
67
68 /**********************************************
69  * gss context init/fini helper               *
70  **********************************************/
71
72 static
73 int ctx_init_pack_request(struct obd_import *imp,
74                           struct ptlrpc_request *req,
75                           int lustre_srv,
76                           uid_t uid, gid_t gid,
77                           long token_size,
78                           char __user *token)
79 {
80         struct lustre_msg       *msg = req->rq_reqbuf;
81         struct gss_sec          *gsec;
82         struct gss_header       *ghdr;
83         struct ptlrpc_user_desc *pud;
84         __u32                   *p, size, offset = 2;
85         rawobj_t                 obj;
86
87         LASSERT(msg->lm_bufcount <= 4);
88         LASSERT(req->rq_cli_ctx);
89         LASSERT(req->rq_cli_ctx->cc_sec);
90
91         /* gss hdr */
92         ghdr = lustre_msg_buf(msg, 0, sizeof(*ghdr));
93         ghdr->gh_version = PTLRPC_GSS_VERSION;
94         ghdr->gh_sp = (__u8) imp->imp_sec->ps_part;
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 (cfs_copy_from_user(p, token, token_size)) {
142                 CERROR("can't copy token\n");
143                 return -EFAULT;
144         }
145         size -= sizeof(__u32) + cfs_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, int swabbed,
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, swabbed);
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 + cfs_size_round4(ghdr->gh_handle.len) +
177                      cfs_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 (cfs_copy_to_user(outbuf, &status, 4))
186                 return -EFAULT;
187         outbuf += 4;
188         if (cfs_copy_to_user(outbuf, &ghdr->gh_major, 4))
189                 return -EFAULT;
190         outbuf += 4;
191         if (cfs_copy_to_user(outbuf, &ghdr->gh_minor, 4))
192                 return -EFAULT;
193         outbuf += 4;
194         if (cfs_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 (cfs_copy_to_user(outbuf, &obj_len, 4))
203                 return -EFAULT;
204         outbuf += 4;
205         if (cfs_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 (cfs_copy_to_user(outbuf, &obj_len, 4))
214                 return -EFAULT;
215         outbuf += 4;
216         if (cfs_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 lgss_keyring "
252                        "version\n", count, (unsigned long) sizeof(param));
253                 RETURN(-EINVAL);
254         }
255         if (cfs_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         if (unlikely(!obd->obd_set_up)) {
279                 CERROR("obd %s not setup\n", obdname);
280                 RETURN(-EINVAL);
281         }
282
283         cfs_spin_lock(&obd->obd_dev_lock);
284         if (obd->obd_stopping) {
285                 CERROR("obd %s has stopped\n", obdname);
286                 cfs_spin_unlock(&obd->obd_dev_lock);
287                 RETURN(-EINVAL);
288         }
289
290         if (strcmp(obd->obd_type->typ_name, LUSTRE_MDC_NAME) &&
291             strcmp(obd->obd_type->typ_name, LUSTRE_OSC_NAME) &&
292             strcmp(obd->obd_type->typ_name, LUSTRE_MGC_NAME)) {
293                 CERROR("obd %s is not a client device\n", obdname);
294                 cfs_spin_unlock(&obd->obd_dev_lock);
295                 RETURN(-EINVAL);
296         }
297         cfs_spin_unlock(&obd->obd_dev_lock);
298
299         cfs_down_read(&obd->u.cli.cl_sem);
300         if (obd->u.cli.cl_import == NULL) {
301                 CERROR("obd %s: import has gone\n", obd->obd_name);
302                 RETURN(-EINVAL);
303         }
304         imp = class_import_get(obd->u.cli.cl_import);
305         cfs_up_read(&obd->u.cli.cl_sem);
306
307         if (imp->imp_deactive) {
308                 CERROR("import has been deactivated\n");
309                 class_import_put(imp);
310                 RETURN(-EINVAL);
311         }
312
313         req = ptlrpc_request_alloc_pack(imp, &RQF_SEC_CTX, LUSTRE_OBD_VERSION,
314                                         SEC_CTX_INIT);
315         if (req == NULL) {
316                 param.status = -ENOMEM;
317                 goto out_copy;
318         }
319
320         if (req->rq_cli_ctx->cc_sec->ps_id != param.secid) {
321                 CWARN("original secid %d, now has changed to %d, "
322                       "cancel this negotiation\n", param.secid,
323                       req->rq_cli_ctx->cc_sec->ps_id);
324                 param.status = -EINVAL;
325                 goto out_copy;
326         }
327
328         /* get token */
329         rc = ctx_init_pack_request(imp, req,
330                                    param.lustre_svc,
331                                    param.uid, param.gid,
332                                    param.send_token_size,
333                                    param.send_token);
334         if (rc) {
335                 param.status = rc;
336                 goto out_copy;
337         }
338
339         ptlrpc_request_set_replen(req);
340
341         rc = ptlrpc_queue_wait(req);
342         if (rc) {
343                 /* If any _real_ denial be made, we expect server return
344                  * -EACCES reply or return success but indicate gss error
345                  * inside reply messsage. All other errors are treated as
346                  * timeout, caller might try the negotiation repeatedly,
347                  * leave recovery decisions to general ptlrpc layer.
348                  *
349                  * FIXME maybe some other error code shouldn't be treated
350                  * as timeout. */
351                 param.status = rc;
352                 if (rc != -EACCES)
353                         param.status = -ETIMEDOUT;
354                 goto out_copy;
355         }
356
357         LASSERT(req->rq_repdata);
358         lsize = ctx_init_parse_reply(req->rq_repdata,
359                                      ptlrpc_rep_need_swab(req),
360                                      param.reply_buf, param.reply_buf_size);
361         if (lsize < 0) {
362                 param.status = (int) lsize;
363                 goto out_copy;
364         }
365
366         param.status = 0;
367         param.reply_length = lsize;
368
369 out_copy:
370         if (cfs_copy_to_user(buffer, &param, sizeof(param)))
371                 rc = -EFAULT;
372         else
373                 rc = 0;
374
375         class_import_put(imp);
376         ptlrpc_req_finished(req);
377         RETURN(rc);
378 }
379
380 int gss_do_ctx_fini_rpc(struct gss_cli_ctx *gctx)
381 {
382         struct ptlrpc_cli_ctx   *ctx = &gctx->gc_base;
383         struct obd_import       *imp = ctx->cc_sec->ps_import;
384         struct ptlrpc_request   *req;
385         struct ptlrpc_user_desc *pud;
386         int                      rc;
387         ENTRY;
388
389         LASSERT(cfs_atomic_read(&ctx->cc_refcount) > 0);
390
391         if (cli_ctx_is_error(ctx) || !cli_ctx_is_uptodate(ctx)) {
392                 CDEBUG(D_SEC, "ctx %p(%u->%s) not uptodate, "
393                        "don't send destroy rpc\n", ctx,
394                        ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
395                 RETURN(0);
396         }
397
398         cfs_might_sleep();
399
400         CWARN("%s ctx %p idx "LPX64" (%u->%s)\n",
401               sec_is_reverse(ctx->cc_sec) ?
402               "server finishing reverse" : "client finishing forward",
403               ctx, gss_handle_to_u64(&gctx->gc_handle),
404               ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec));
405
406         gctx->gc_proc = PTLRPC_GSS_PROC_DESTROY;
407
408         req = ptlrpc_request_alloc(imp, &RQF_SEC_CTX);
409         if (req == NULL) {
410                 CWARN("ctx %p(%u): fail to prepare rpc, destroy locally\n",
411                       ctx, ctx->cc_vcred.vc_uid);
412                 GOTO(out, rc = -ENOMEM);
413         }
414
415         rc = ptlrpc_request_bufs_pack(req, LUSTRE_OBD_VERSION, SEC_CTX_FINI,
416                                       NULL, ctx);
417         if (rc) {
418                 ptlrpc_request_free(req);
419                 GOTO(out_ref, rc);
420         }
421
422         /* fix the user desc */
423         if (req->rq_pack_udesc) {
424                 /* we rely the fact that this request is in AUTH mode,
425                  * and user_desc at offset 2. */
426                 pud = lustre_msg_buf(req->rq_reqbuf, 2, sizeof(*pud));
427                 LASSERT(pud);
428                 pud->pud_uid = pud->pud_fsuid = ctx->cc_vcred.vc_uid;
429                 pud->pud_gid = pud->pud_fsgid = ctx->cc_vcred.vc_gid;
430                 pud->pud_cap = 0;
431                 pud->pud_ngroups = 0;
432         }
433
434         req->rq_phase = RQ_PHASE_RPC;
435         rc = ptl_send_rpc(req, 1);
436         if (rc)
437                 CWARN("ctx %p(%u->%s): rpc error %d, destroy locally\n", ctx,
438                       ctx->cc_vcred.vc_uid, sec2target_str(ctx->cc_sec), rc);
439
440 out_ref:
441         ptlrpc_req_finished(req);
442 out:
443         RETURN(rc);
444 }
445
446 int __init gss_init_cli_upcall(void)
447 {
448         return 0;
449 }
450
451 void __exit gss_exit_cli_upcall(void)
452 {
453 }