Whamcloud - gitweb
6c6edc4216a94812aa04e3b0fe0b408ba6be9177
[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                         RETURN(-ENOMEM);
54                 memcpy(obj->data, buf, len);
55         } else
56                 obj->data = NULL;
57         return 0;
58 }
59
60 void rawobj_free(rawobj_t *obj)
61 {
62         LASSERT(obj);
63
64         if (obj->len) {
65                 LASSERT(obj->data);
66                 OBD_FREE(obj->data, obj->len);
67                 obj->len = 0;
68                 obj->data = NULL;
69         } else
70                 LASSERT(!obj->data);
71 }
72
73 int rawobj_equal(rawobj_t *a, rawobj_t *b)
74 {
75         LASSERT(a && b);
76
77         return (a->len == b->len &&
78                 !memcmp(a->data, b->data, a->len));
79 }
80
81 int rawobj_dup(rawobj_t *dest, rawobj_t *src)
82 {
83         LASSERT(src && dest);
84
85         dest->len = src->len;
86         if (dest->len) {
87                 OBD_ALLOC(dest->data, dest->len);
88                 if (!dest->data)
89                         return -ENOMEM;
90                 memcpy(dest->data, src->data, dest->len);
91         } else
92                 dest->data = NULL;
93         return 0;
94 }
95
96 int rawobj_serialize(rawobj_t *obj, __u32 **buf, __u32 *buflen)
97 {
98         __u32 len;
99
100         LASSERT(obj);
101         LASSERT(buf);
102         LASSERT(buflen);
103
104         len = size_round4(obj->len);
105
106         if (*buflen < 4 + len) {
107                 CERROR("buflen %u <  %u\n", *buflen, 4 + len);
108                 return -EINVAL;
109         }
110
111         *(*buf)++ = cpu_to_le32(obj->len);
112         memcpy(*buf, obj->data, obj->len);
113         *buf += (len >> 2);
114         *buflen -= (4 + len);
115
116         return 0;
117 }
118
119 static int __rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen,
120                         int alloc, int local)
121 {
122         __u32 len;
123
124         if (*buflen < sizeof(__u32)) {
125                 CERROR("buflen %u\n", *buflen);
126                 return -EINVAL;
127         }
128
129         obj->len = *(*buf)++;
130         if (!local)
131                 obj->len = le32_to_cpu(obj->len);
132         *buflen -= sizeof(__u32);
133
134         if (!obj->len) {
135                 obj->data = NULL;
136                 return 0;
137         }
138
139         len = local ? obj->len : size_round4(obj->len);
140         if (*buflen < len) {
141                 CERROR("buflen %u < %u\n", *buflen, len);
142                 return -EINVAL;
143         }
144
145         if (!alloc)
146                 obj->data = (__u8 *) *buf;
147         else {
148                 OBD_ALLOC(obj->data, obj->len);
149                 if (!obj->data) {
150                         CERROR("fail to alloc %u bytes\n", obj->len);
151                         return -ENOMEM;
152                 }
153                 memcpy(obj->data, *buf, obj->len);
154         }
155
156         *((char **)buf) += len;
157         *buflen -= len;
158
159         return 0;
160 }
161
162 int rawobj_extract(rawobj_t *obj, __u32 **buf, __u32 *buflen)
163 {
164         return __rawobj_extract(obj, buf, buflen, 0, 0);
165 }
166
167 int rawobj_extract_local(rawobj_t *obj, __u32 **buf, __u32 *buflen)
168 {
169         return __rawobj_extract(obj, buf, buflen, 0, 1);
170 }