Whamcloud - gitweb
LU-6496 ptlrpc: Fix wrong code indentation in plain_authorize
[fs/lustre-release.git] / lustre / ptlrpc / gss / gss_rawobj.c
1 /*
2  * GPL HEADER START
3  *
4  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 only,
8  * as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License version 2 for more details (a copy is included
14  * in the LICENSE file that accompanied this code).
15  *
16  * You should have received a copy of the GNU General Public License
17  * version 2 along with this program; If not, see
18  * http://www.sun.com/software/products/lustre/docs/GPLv2.pdf
19  *
20  * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
21  * CA 95054 USA or visit www.sun.com if you need additional information or
22  * have any questions.
23  *
24  * GPL HEADER END
25  */
26 /*
27  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
28  * Use is subject to license terms.
29  *
30  * Copyright (c) 2011, Intel Corporation.
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_rawobj.c
37  *
38  * Author: Eric Mei <ericm@clusterfs.com>
39  */
40
41 #define DEBUG_SUBSYSTEM S_SEC
42
43 #include <linux/mutex.h>
44
45 #include <obd.h>
46 #include <obd_class.h>
47 #include <obd_support.h>
48 #include <lustre_sec.h>
49
50 #include "gss_internal.h"
51
52 int rawobj_empty(rawobj_t *obj)
53 {
54         LASSERT(equi(obj->len, obj->data));
55         return (obj->len == 0);
56 }
57
58 int rawobj_alloc(rawobj_t *obj, char *buf, int len)
59 {
60         LASSERT(obj);
61         LASSERT(len >= 0);
62
63         obj->len = len;
64         if (len) {
65                 OBD_ALLOC_LARGE(obj->data, len);
66                 if (!obj->data) {
67                         obj->len = 0;
68                         RETURN(-ENOMEM);
69                 }
70                 memcpy(obj->data, buf, len);
71         } else
72                 obj->data = NULL;
73         return 0;
74 }
75
76 void rawobj_free(rawobj_t *obj)
77 {
78         LASSERT(obj);
79
80         if (obj->len) {
81                 LASSERT(obj->data);
82                 OBD_FREE_LARGE(obj->data, obj->len);
83                 obj->len = 0;
84                 obj->data = NULL;
85         } else
86                 LASSERT(!obj->data);
87 }
88
89 int rawobj_equal(rawobj_t *a, rawobj_t *b)
90 {
91         LASSERT(a && b);
92
93         return (a->len == b->len &&
94                 (!a->len || !memcmp(a->data, b->data, a->len)));
95 }
96
97 int rawobj_dup(rawobj_t *dest, rawobj_t *src)
98 {
99         LASSERT(src && dest);
100
101         dest->len = src->len;
102         if (dest->len) {
103                 OBD_ALLOC_LARGE(dest->data, dest->len);
104                 if (!dest->data) {
105                         dest->len = 0;
106                         return -ENOMEM;
107                 }
108                 memcpy(dest->data, src->data, dest->len);
109         } else
110                 dest->data = NULL;
111         return 0;
112 }
113
114 int rawobj_serialize(rawobj_t *obj, __u32 **buf, __u32 *buflen)
115 {
116         __u32 len;
117
118         LASSERT(obj);
119         LASSERT(buf);
120         LASSERT(buflen);
121
122         len = cfs_size_round4(obj->len);
123
124         if (*buflen < 4 + len) {
125                 CERROR("buflen %u <  %u\n", *buflen, 4 + len);
126                 return -EINVAL;
127         }
128
129         *(*buf)++ = cpu_to_le32(obj->len);
130         memcpy(*buf, obj->data, obj->len);
131         *buf += (len >> 2);
132         *buflen -= (4 + len);
133
134         return 0;
135 }
136
137 static int __rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen,
138                             int alloc, int local)
139 {
140         __u32 len;
141
142         if (*buflen < sizeof(__u32)) {
143                 CERROR("buflen %u\n", *buflen);
144                 return -EINVAL;
145         }
146
147         obj->len = *(*buf)++;
148         if (!local)
149                 obj->len = le32_to_cpu(obj->len);
150         *buflen -= sizeof(__u32);
151
152         if (!obj->len) {
153                 obj->data = NULL;
154                 return 0;
155         }
156
157         len = local ? obj->len : cfs_size_round4(obj->len);
158         if (*buflen < len) {
159                 CERROR("buflen %u < %u\n", *buflen, len);
160                 obj->len = 0;
161                 return -EINVAL;
162         }
163
164         if (!alloc)
165                 obj->data = (__u8 *) *buf;
166         else {
167                 OBD_ALLOC_LARGE(obj->data, obj->len);
168                 if (!obj->data) {
169                         CERROR("fail to alloc %u bytes\n", obj->len);
170                         obj->len = 0;
171                         return -ENOMEM;
172                 }
173                 memcpy(obj->data, *buf, obj->len);
174         }
175
176         *((char **)buf) += len;
177         *buflen -= len;
178
179         return 0;
180 }
181
182 int rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen)
183 {
184         return __rawobj_extract(obj, buf, buflen, 0, 0);
185 }
186
187 int rawobj_extract_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen)
188 {
189         return __rawobj_extract(obj, buf, buflen, 1, 0);
190 }
191
192 int rawobj_extract_local(rawobj_t *obj, __u32 **buf, __u32 *buflen)
193 {
194         return __rawobj_extract(obj, buf, buflen, 0, 1);
195 }
196
197 int rawobj_extract_local_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen)
198 {
199         return __rawobj_extract(obj, buf, buflen, 1, 1);
200 }
201
202 int rawobj_from_netobj(rawobj_t *rawobj, netobj_t *netobj)
203 {
204         rawobj->len = netobj->len;
205         rawobj->data = netobj->data;
206         return 0;
207 }
208
209 int rawobj_from_netobj_alloc(rawobj_t *rawobj, netobj_t *netobj)
210 {
211         rawobj->len = 0;
212         rawobj->data = NULL;
213
214         if (netobj->len == 0)
215                 return 0;
216                 
217         OBD_ALLOC_LARGE(rawobj->data, netobj->len);
218         if (rawobj->data == NULL)
219                 return -ENOMEM;
220
221         rawobj->len = netobj->len;
222         memcpy(rawobj->data, netobj->data, netobj->len);
223         return 0;
224 }
225
226 /****************************************
227  * misc more                            *
228  ****************************************/
229
230 int buffer_extract_bytes(const void **buf, __u32 *buflen,
231                          void *res, __u32 reslen)
232 {
233         if (*buflen < reslen) {
234                 CERROR("buflen %u < %u\n", *buflen, reslen);
235                 return -EINVAL;
236         }
237
238         memcpy(res, *buf, reslen);
239         *buf += reslen;
240         *buflen -= reslen;
241         return 0;
242 }