Whamcloud - gitweb
land b_hd_sec onto HEAD:
[fs/lustre-release.git] / lustre / sec / svcsec.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_idl.h>
37 #include <linux/lustre_net.h>
38 #include <linux/lustre_sec.h>
39
40 static spinlock_t svcsecs_lock = SPIN_LOCK_UNLOCKED;
41 static struct ptlrpc_svcsec *svcsecs[PTLRPC_SEC_MAX_FLAVORS] = {
42         NULL,
43 };
44
45 int svcsec_register(struct ptlrpc_svcsec *sec)
46 {
47         __u32 flavor = sec->pss_flavor.flavor;
48
49         if (flavor >= PTLRPC_SEC_MAX_FLAVORS)
50                 return -EINVAL;
51
52         spin_lock(&svcsecs_lock);
53         if (svcsecs[flavor]) {
54                 spin_unlock(&svcsecs_lock);
55                 return -EALREADY;
56         }
57         svcsecs[flavor] = sec;
58         spin_unlock(&svcsecs_lock);
59
60         CDEBUG(D_SEC, "Registered svc security module %s\n", sec->pss_name);
61         return 0;
62 }
63
64 int svcsec_unregister(struct ptlrpc_svcsec *sec)
65 {
66         __u32 flavor = sec->pss_flavor.flavor;
67
68         if (flavor >= PTLRPC_SEC_MAX_FLAVORS)
69                 return -EINVAL;
70
71         spin_lock(&svcsecs_lock);
72         if (!svcsecs[flavor]) {
73                 spin_unlock(&svcsecs_lock);
74                 return -EINVAL;
75         }
76
77         LASSERT(svcsecs[flavor] == sec);
78
79         CDEBUG(D_SEC, "Unregistered svc security module %s\n", sec->pss_name);
80         svcsecs[flavor] = NULL;
81         spin_unlock(&svcsecs_lock);
82
83         return 0;
84 }
85
86 static
87 struct ptlrpc_svcsec * flavor2svcsec(__u32 flavor)
88 {
89         struct ptlrpc_svcsec *sec;
90
91         if (flavor >= PTLRPC_SEC_MAX_FLAVORS)
92                 return NULL;
93
94         spin_lock(&svcsecs_lock);
95         sec = svcsecs[flavor];
96         if (sec && !try_module_get(sec->pss_owner))
97                 sec = NULL;
98         spin_unlock(&svcsecs_lock);
99         return sec;
100 }
101
102 struct ptlrpc_svcsec * svcsec_get(struct ptlrpc_svcsec *sec)
103 {
104         int rc;
105
106         rc = try_module_get(sec->pss_owner);
107         LASSERT(rc);
108         return sec;
109 }
110
111 void svcsec_put(struct ptlrpc_svcsec *sec)
112 {
113         module_put(sec->pss_owner);
114 }
115
116 /*
117  * common code to allocate reply_state buffer.
118  */
119 int svcsec_alloc_reply_state(struct ptlrpc_request *req,
120                              int msgsize, int secsize)
121 {
122         struct ptlrpc_reply_state *rs;
123         char *buf;
124         int repsize, bufsize;
125         ENTRY;
126
127         LASSERT(msgsize % 8 == 0);
128         LASSERT(secsize % 8 == 0);
129
130         repsize = sizeof(struct ptlrpcs_wire_hdr) + msgsize + secsize;
131         bufsize = repsize + sizeof(struct ptlrpc_reply_state);
132
133         OBD_ALLOC(buf, bufsize);
134         if (!buf) {
135                 CERROR("can't alloc %d\n", bufsize);
136                 RETURN(-ENOMEM);
137         }
138
139         /* req->rq_repbuf is not used on server side */
140         rs = (struct ptlrpc_reply_state *) (buf + repsize);
141         rs->rs_buf = buf;
142         rs->rs_buf_len = bufsize;
143         rs->rs_repbuf = buf;
144         rs->rs_repbuf_len = repsize;
145         /* current known data length is hdr + msg, security payload
146          * will be added on later.
147          */
148         rs->rs_repdata_len = sizeof(struct ptlrpcs_wire_hdr) + msgsize;
149         req->rq_repmsg = rs->rs_msg = (struct lustre_msg *)
150                          (rs->rs_repbuf + sizeof(struct ptlrpcs_wire_hdr));
151
152         req->rq_reply_state = rs;
153
154         CDEBUG(D_SEC, "alloc rs buf at %p, len %d; repbuf at %p, len %d\n",
155                rs->rs_buf, rs->rs_buf_len, rs->rs_repbuf, rs->rs_repbuf_len);
156
157         RETURN(0);
158 }
159
160 void svcsec_free_reply_state(struct ptlrpc_reply_state *rs)
161 {
162         char *p;
163         ENTRY;
164
165         /* for work around memory-alloc debug poison */
166         LASSERT(rs);
167         p = rs->rs_buf;
168         OBD_FREE(p, rs->rs_buf_len);
169         EXIT;
170 }
171
172 int svcsec_alloc_repbuf(struct ptlrpc_svcsec *svcsec,
173                         struct ptlrpc_request *req,
174                         int msgsize)
175 {
176         LASSERT(svcsec);
177         LASSERT(msgsize % 8 == 0);
178
179         if (svcsec->alloc_repbuf)
180                 return svcsec->alloc_repbuf(svcsec, req, msgsize);
181         else
182                 return svcsec_alloc_reply_state(req, msgsize, 0);
183 }
184
185 int svcsec_accept(struct ptlrpc_request *req, enum ptlrpcs_error *res)
186 {
187         struct ptlrpc_svcsec           *sec;
188         struct ptlrpcs_wire_hdr        *sec_hdr;
189         int                             rc;
190         ENTRY;
191
192         LASSERT(req->rq_reqbuf);
193         LASSERT(!req->rq_reqmsg);
194         LASSERT(!req->rq_svcsec);
195
196         *res = PTLRPCS_BADCRED;
197         if (req->rq_reqbuf_len < sizeof(*sec_hdr)) {
198                 CERROR("drop too short msg (length: %d)\n", req->rq_reqbuf_len);
199                 RETURN(SVC_DROP);
200         }
201
202         sec_hdr = (struct ptlrpcs_wire_hdr *) req->rq_reqbuf;
203         sec_hdr->flavor = le32_to_cpu(sec_hdr->flavor);
204         sec_hdr->sectype = le32_to_cpu(sec_hdr->sectype);
205         sec_hdr->msg_len = le32_to_cpu(sec_hdr->msg_len);
206         sec_hdr->sec_len = le32_to_cpu(sec_hdr->sec_len);
207
208         /* sanity check */
209         switch (sec_hdr->sectype) {
210         case PTLRPC_SEC_TYPE_NONE:
211         case PTLRPC_SEC_TYPE_AUTH:
212         case PTLRPC_SEC_TYPE_PRIV:
213                 break;
214         default:
215                 CERROR("unknown security type %d\n", sec_hdr->sectype);
216                 RETURN(SVC_DROP);
217         }
218
219         if (sizeof(*sec_hdr) + sec_hdr->msg_len + sec_hdr->sec_len >
220             req->rq_reqbuf_len) {
221                 CERROR("received %d, msg %d, sec %d\n",
222                         req->rq_reqbuf_len, sec_hdr->msg_len, sec_hdr->sec_len);
223                 RETURN(SVC_DROP);
224         }
225
226         req->rq_svcsec = sec = flavor2svcsec(sec_hdr->flavor);
227         if (!sec) {
228                 CERROR("drop msg: unsupported flavor %d\n", sec_hdr->flavor);
229                 RETURN(SVC_DROP);
230         }
231         LASSERT(sec->accept);
232
233         rc = sec->accept(req, res);
234
235         switch (rc) {
236         case SVC_DROP:
237                 svcsec_put(sec);
238                 req->rq_svcsec = NULL;
239                 break;
240         case SVC_OK:
241         case SVC_LOGIN:
242         case SVC_LOGOUT:
243                 LASSERT(req->rq_reqmsg);
244                 break;
245         }
246
247         RETURN(rc);
248 }
249
250 int svcsec_authorize(struct ptlrpc_request *req)
251 {
252         LASSERT(req->rq_svcsec);
253         LASSERT(req->rq_svcsec->authorize);
254
255         return (req->rq_svcsec->authorize(req));
256 }
257
258 void svcsec_cleanup_req(struct ptlrpc_request *req)
259 {
260         struct ptlrpc_svcsec *svcsec = req->rq_svcsec;
261         ENTRY;
262
263         LASSERT(svcsec);
264         LASSERT(svcsec->cleanup_req || !req->rq_sec_svcdata);
265
266         if (svcsec->cleanup_req)
267                 svcsec->cleanup_req(svcsec, req);
268         EXIT;
269 }