1 /* -*- mode: c; c-basic-offset: 8; indent-tabs-mode: nil; -*-
2 * vim:expandtab:shiftwidth=8:tabstop=8:
4 * Copyright (C) 2004 Cluster File Systems, Inc.
6 * This file is part of Lustre, http://www.lustre.org.
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.
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.
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.
23 # define EXPORT_SYMTAB
25 #define DEBUG_SUBSYSTEM S_SEC
27 #include <linux/init.h>
28 #include <linux/module.h>
29 #include <linux/slab.h>
31 #include <liblustre.h>
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>
40 static spinlock_t svcsecs_lock = SPIN_LOCK_UNLOCKED;
41 static struct ptlrpc_svcsec *svcsecs[PTLRPC_SEC_MAX_FLAVORS] = {
45 int svcsec_register(struct ptlrpc_svcsec *sec)
47 __u32 flavor = sec->pss_flavor.flavor;
49 if (flavor >= PTLRPC_SEC_MAX_FLAVORS)
52 spin_lock(&svcsecs_lock);
53 if (svcsecs[flavor]) {
54 spin_unlock(&svcsecs_lock);
57 svcsecs[flavor] = sec;
58 spin_unlock(&svcsecs_lock);
60 CDEBUG(D_SEC, "Registered svc security module %s\n", sec->pss_name);
64 int svcsec_unregister(struct ptlrpc_svcsec *sec)
66 __u32 flavor = sec->pss_flavor.flavor;
68 if (flavor >= PTLRPC_SEC_MAX_FLAVORS)
71 spin_lock(&svcsecs_lock);
72 if (!svcsecs[flavor]) {
73 spin_unlock(&svcsecs_lock);
77 LASSERT(svcsecs[flavor] == sec);
79 CDEBUG(D_SEC, "Unregistered svc security module %s\n", sec->pss_name);
80 svcsecs[flavor] = NULL;
81 spin_unlock(&svcsecs_lock);
87 struct ptlrpc_svcsec * flavor2svcsec(__u32 flavor)
89 struct ptlrpc_svcsec *sec;
91 if (flavor >= PTLRPC_SEC_MAX_FLAVORS)
94 spin_lock(&svcsecs_lock);
95 sec = svcsecs[flavor];
96 if (sec && !try_module_get(sec->pss_owner))
98 spin_unlock(&svcsecs_lock);
102 struct ptlrpc_svcsec * svcsec_get(struct ptlrpc_svcsec *sec)
106 rc = try_module_get(sec->pss_owner);
111 void svcsec_put(struct ptlrpc_svcsec *sec)
113 module_put(sec->pss_owner);
117 * common code to allocate reply_state buffer.
119 int svcsec_alloc_reply_state(struct ptlrpc_request *req,
120 int msgsize, int secsize)
122 struct ptlrpc_reply_state *rs;
124 int repsize, bufsize;
127 LASSERT(msgsize % 8 == 0);
128 LASSERT(secsize % 8 == 0);
130 repsize = sizeof(struct ptlrpcs_wire_hdr) + msgsize + secsize;
131 bufsize = repsize + sizeof(struct ptlrpc_reply_state);
133 OBD_ALLOC(buf, bufsize);
135 CERROR("can't alloc %d\n", bufsize);
139 /* req->rq_repbuf is not used on server side */
140 rs = (struct ptlrpc_reply_state *) (buf + repsize);
142 rs->rs_buf_len = bufsize;
144 rs->rs_repbuf_len = repsize;
145 /* current known data length is hdr + msg, security payload
146 * will be added on later.
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));
152 req->rq_reply_state = rs;
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);
160 void svcsec_free_reply_state(struct ptlrpc_reply_state *rs)
165 /* for work around memory-alloc debug poison */
168 OBD_FREE(p, rs->rs_buf_len);
172 int svcsec_alloc_repbuf(struct ptlrpc_svcsec *svcsec,
173 struct ptlrpc_request *req,
177 LASSERT(msgsize % 8 == 0);
179 if (svcsec->alloc_repbuf)
180 return svcsec->alloc_repbuf(svcsec, req, msgsize);
182 return svcsec_alloc_reply_state(req, msgsize, 0);
185 int svcsec_accept(struct ptlrpc_request *req, enum ptlrpcs_error *res)
187 struct ptlrpc_svcsec *sec;
188 struct ptlrpcs_wire_hdr *sec_hdr;
192 LASSERT(req->rq_reqbuf);
193 LASSERT(!req->rq_reqmsg);
194 LASSERT(!req->rq_svcsec);
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);
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);
209 switch (sec_hdr->sectype) {
210 case PTLRPC_SEC_TYPE_NONE:
211 case PTLRPC_SEC_TYPE_AUTH:
212 case PTLRPC_SEC_TYPE_PRIV:
215 CERROR("unknown security type %d\n", sec_hdr->sectype);
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);
226 req->rq_svcsec = sec = flavor2svcsec(sec_hdr->flavor);
228 CERROR("drop msg: unsupported flavor %d\n", sec_hdr->flavor);
231 LASSERT(sec->accept);
233 rc = sec->accept(req, res);
238 req->rq_svcsec = NULL;
243 LASSERT(req->rq_reqmsg);
250 int svcsec_authorize(struct ptlrpc_request *req)
252 LASSERT(req->rq_svcsec);
253 LASSERT(req->rq_svcsec->authorize);
255 return (req->rq_svcsec->authorize(req));
258 void svcsec_cleanup_req(struct ptlrpc_request *req)
260 struct ptlrpc_svcsec *svcsec = req->rq_svcsec;
264 LASSERT(svcsec->cleanup_req || !req->rq_sec_svcdata);
266 if (svcsec->cleanup_req)
267 svcsec->cleanup_req(svcsec, req);