Whamcloud - gitweb
don't let ctx obj share storage with request buffer, which might be
[fs/lustre-release.git] / lustre / sec / gss / rawobj.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 #include <linux/crypto.h>
31 #else
32 #include <liblustre.h>
33 #endif
34
35 #include <libcfs/kp30.h>
36 #include <linux/obd.h>
37 #include <linux/obd_class.h>
38 #include <linux/obd_support.h>
39 #include <linux/lustre_idl.h>
40 #include <linux/lustre_sec.h>
41
42 #include "gss_internal.h"
43
44 int rawobj_alloc(rawobj_t *obj, char *buf, int len)
45 {
46         LASSERT(obj);
47         LASSERT(len >= 0);
48
49         obj->len = len;
50         if (len) {
51                 OBD_ALLOC(obj->data, len);
52                 if (!obj->data) {
53                         obj->len = 0;
54                         RETURN(-ENOMEM);
55                 }
56                 memcpy(obj->data, buf, len);
57         } else
58                 obj->data = NULL;
59         return 0;
60 }
61
62 void rawobj_free(rawobj_t *obj)
63 {
64         LASSERT(obj);
65
66         if (obj->len) {
67                 LASSERT(obj->data);
68                 OBD_FREE(obj->data, obj->len);
69                 obj->len = 0;
70                 obj->data = NULL;
71         } else
72                 LASSERT(!obj->data);
73 }
74
75 int rawobj_equal(rawobj_t *a, rawobj_t *b)
76 {
77         LASSERT(a && b);
78
79         return (a->len == b->len &&
80                 (!a->len || !memcmp(a->data, b->data, a->len)));
81 }
82
83 int rawobj_dup(rawobj_t *dest, rawobj_t *src)
84 {
85         LASSERT(src && dest);
86
87         dest->len = src->len;
88         if (dest->len) {
89                 OBD_ALLOC(dest->data, dest->len);
90                 if (!dest->data) {
91                         dest->len = 0;
92                         return -ENOMEM;
93                 }
94                 memcpy(dest->data, src->data, dest->len);
95         } else
96                 dest->data = NULL;
97         return 0;
98 }
99
100 int rawobj_serialize(rawobj_t *obj, __u32 **buf, __u32 *buflen)
101 {
102         __u32 len;
103
104         LASSERT(obj);
105         LASSERT(buf);
106         LASSERT(buflen);
107
108         len = size_round4(obj->len);
109
110         if (*buflen < 4 + len) {
111                 CERROR("buflen %u <  %u\n", *buflen, 4 + len);
112                 return -EINVAL;
113         }
114
115         *(*buf)++ = cpu_to_le32(obj->len);
116         memcpy(*buf, obj->data, obj->len);
117         *buf += (len >> 2);
118         *buflen -= (4 + len);
119
120         return 0;
121 }
122
123 static int __rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen,
124                         int alloc, int local)
125 {
126         __u32 len;
127
128         if (*buflen < sizeof(__u32)) {
129                 CERROR("buflen %u\n", *buflen);
130                 return -EINVAL;
131         }
132
133         obj->len = *(*buf)++;
134         if (!local)
135                 obj->len = le32_to_cpu(obj->len);
136         *buflen -= sizeof(__u32);
137
138         if (!obj->len) {
139                 obj->data = NULL;
140                 return 0;
141         }
142
143         len = local ? obj->len : size_round4(obj->len);
144         if (*buflen < len) {
145                 CERROR("buflen %u < %u\n", *buflen, len);
146                 return -EINVAL;
147         }
148
149         if (!alloc)
150                 obj->data = (__u8 *) *buf;
151         else {
152                 OBD_ALLOC(obj->data, obj->len);
153                 if (!obj->data) {
154                         CERROR("fail to alloc %u bytes\n", obj->len);
155                         return -ENOMEM;
156                 }
157                 memcpy(obj->data, *buf, obj->len);
158         }
159
160         *((char **)buf) += len;
161         *buflen -= len;
162
163         return 0;
164 }
165
166 int rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen)
167 {
168         return __rawobj_extract(obj, buf, buflen, 0, 0);
169 }
170
171 int rawobj_extract_alloc(rawobj_t *obj, __u32 **buf, __u32 *buflen)
172 {
173         return __rawobj_extract(obj, buf, buflen, 1, 0);
174 }
175
176 int rawobj_extract_local(rawobj_t *obj, __u32 **buf, __u32 *buflen)
177 {
178         return __rawobj_extract(obj, buf, buflen, 0, 1);
179 }