Whamcloud - gitweb
a05485c9a78ad240306c9790dec9590dd1b1378a
[fs/lustre-release.git] / lustre / sec / sec_null.c
1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=8:tabstop=8:
3  *
4  * Copyright (C) 2004 Cluster File Systems, Inc.
5  *
6  *   This file is part of Lustre, http://www.lustre.org.
7  *
8  *   Lustre is free software; you can redistribute it and/or
9  *   modify it under the terms of version 2 of the GNU General Public
10  *   License as published by the Free Software Foundation.
11  *
12  *   Lustre is distributed in the hope that it will be useful,
13  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *   GNU General Public License for more details.
16  *
17  *   You should have received a copy of the GNU General Public License
18  *   along with Lustre; if not, write to the Free Software
19  *   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #ifndef EXPORT_SYMTAB
23 # define EXPORT_SYMTAB
24 #endif
25 #define DEBUG_SUBSYSTEM S_SEC
26 #ifdef __KERNEL__
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
30 #else
31 #include <liblustre.h>
32 #endif
33
34 #include <libcfs/kp30.h>
35 #include <linux/obd_support.h>
36 #include <linux/lustre_net.h>
37 #include <linux/lustre_sec.h>
38
39 static int null_cred_refresh(struct ptlrpc_cred *cred)
40 {
41         LASSERT(test_bit(PTLRPC_CRED_UPTODATE_BIT, &cred->pc_flags));
42         return 0;
43 }
44
45 static int null_cred_match(struct ptlrpc_cred *cred,
46                            struct vfs_cred *vcred)
47 {
48         return 1;
49 }
50
51 static int null_cred_sign(struct ptlrpc_cred *cred,
52                           struct ptlrpc_request *req)
53 {
54         struct ptlrpcs_wire_hdr *hdr = buf_to_sec_hdr(req->rq_reqbuf);
55
56         hdr->sec_len = cpu_to_le32(0);
57         return 0;
58 }
59
60 static int null_cred_verify(struct ptlrpc_cred *cred,
61                             struct ptlrpc_request *req)
62 {
63         struct ptlrpcs_wire_hdr *hdr = buf_to_sec_hdr(req->rq_repbuf);
64
65         if (hdr->sec_len != 0) {
66                 CERROR("security payload %u not zero\n", hdr->sec_len);
67                 return -EPROTO;
68         }
69
70         req->rq_repmsg = (struct lustre_msg *)(hdr + 1);
71         req->rq_replen = hdr->msg_len;
72         CDEBUG(D_SEC, "set repmsg at %p, len %d\n",
73                req->rq_repmsg, req->rq_replen);
74
75         return 0;
76 }
77
78 static void null_cred_destroy(struct ptlrpc_cred *cred)
79 {
80         LASSERT(!atomic_read(&cred->pc_refcount));
81
82         CDEBUG(D_SEC, "NULL_SEC: destroy cred %p\n", cred);
83         OBD_FREE(cred, sizeof(*cred));
84 }
85
86 static struct ptlrpc_credops null_credops = {
87         .refresh        = null_cred_refresh,
88         .match          = null_cred_match,
89         .sign           = null_cred_sign,
90         .verify         = null_cred_verify,
91         .destroy        = null_cred_destroy,
92 };
93
94 static
95 struct ptlrpc_sec* null_create_sec(ptlrpcs_flavor_t *flavor,
96                                    const char *pipe_dir,
97                                    void *pipe_data)
98 {
99         struct ptlrpc_sec *sec;
100         ENTRY;
101
102         LASSERT(flavor->flavor == PTLRPC_SEC_NULL);
103
104         OBD_ALLOC(sec, sizeof(*sec));
105         if (!sec)
106                 RETURN(ERR_PTR(-ENOMEM));
107
108         sec->ps_sectype = PTLRPC_SEC_TYPE_NONE;
109         sec->ps_expire = (-1UL >> 1); /* never expire */
110         sec->ps_nextgc = (-1UL >> 1);
111         sec->ps_flags = 0;
112
113         CDEBUG(D_SEC, "Create NULL security module at %p\n", sec);
114         RETURN(sec);
115 }
116
117 static
118 void null_destroy_sec(struct ptlrpc_sec *sec)
119 {
120         ENTRY;
121
122         CDEBUG(D_SEC, "Destroy NULL security module at %p\n", sec);
123
124         LASSERT(!atomic_read(&sec->ps_refcount));
125         OBD_FREE(sec, sizeof(*sec));
126         EXIT;
127 }
128
129 static
130 struct ptlrpc_cred* null_create_cred(struct ptlrpc_sec *sec,
131                                      struct vfs_cred *vcred)
132 {
133         struct ptlrpc_cred *cred;
134         ENTRY;
135
136         OBD_ALLOC(cred, sizeof(*cred));
137         if (!cred)
138                 RETURN(NULL);
139
140         INIT_LIST_HEAD(&cred->pc_hash);
141         atomic_set(&cred->pc_refcount, 0);
142         cred->pc_sec = sec;
143         cred->pc_ops = &null_credops;
144         cred->pc_expire = 0;
145         cred->pc_flags = PTLRPC_CRED_UPTODATE;
146         cred->pc_pag = vcred->vc_pag;
147         cred->pc_uid = vcred->vc_uid;
148         CDEBUG(D_SEC, "create a null cred at %p("LPU64"/%u)\n",
149                cred, vcred->vc_pag, vcred->vc_uid);
150
151         RETURN(cred);
152 }
153
154 static struct ptlrpc_secops null_secops = {
155         .create_sec     = null_create_sec,
156         .destroy_sec    = null_destroy_sec,
157         .create_cred    = null_create_cred,
158 };
159
160 static struct ptlrpc_sec_type null_type = {
161         .pst_owner      = THIS_MODULE,
162         .pst_name       = "NULL_SEC",
163         .pst_inst       = ATOMIC_INIT(0),
164         .pst_flavor     = {PTLRPC_SEC_NULL, 0},
165         .pst_ops        = &null_secops,
166 };
167
168 int ptlrpcs_null_init(void)
169 {
170         int rc;
171
172         rc = ptlrpcs_register(&null_type);
173         if (rc)
174                 CERROR("failed to register NULL security: %d\n", rc);
175
176         return rc;
177 }
178
179 int ptlrpcs_null_exit(void)
180 {
181         int rc;
182
183         rc = ptlrpcs_unregister(&null_type);
184         if (rc)
185                 CERROR("cannot unregister NULL security: %d\n", rc);
186
187         return rc;
188 }