extern int errno considered harmful.
[dragonfly.git] / contrib / dhcp-3.0 / omapip / auth.c
1 /* auth.c
2
3    Subroutines having to do with authentication. */
4
5 /*
6  * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
7  * Copyright (c) 1998-2003 by Internet Software Consortium
8  *
9  * Permission to use, copy, modify, and distribute this software for any
10  * purpose with or without fee is hereby granted, provided that the above
11  * copyright notice and this permission notice appear in all copies.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
14  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
15  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
16  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
17  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
18  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
19  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
20  *
21  *   Internet Systems Consortium, Inc.
22  *   950 Charter Street
23  *   Redwood City, CA 94063
24  *   <info@isc.org>
25  *   http://www.isc.org/
26  *
27  * This software has been written for Internet Systems Consortium
28  * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
29  * To learn more about Internet Systems Consortium, see
30  * ``http://www.isc.org/''.  To learn more about Vixie Enterprises,
31  * see ``http://www.vix.com''.   To learn more about Nominum, Inc., see
32  * ``http://www.nominum.com''.
33  */
34
35 #ifndef lint
36 static char ocopyright[] =
37 "$Id: auth.c,v 1.3.2.4 2004/06/10 17:59:46 dhankins Exp $ Copyright 2004 Internet Systems Consortium.";
38 #endif
39
40 #include <omapip/omapip_p.h>
41
42 OMAPI_OBJECT_ALLOC (omapi_auth_key, omapi_auth_key_t, omapi_type_auth_key)
43 typedef struct hash omapi_auth_hash_t;
44 HASH_FUNCTIONS_DECL (omapi_auth_key, const char *,
45                      omapi_auth_key_t, omapi_auth_hash_t)
46 omapi_auth_hash_t *auth_key_hash;
47 HASH_FUNCTIONS (omapi_auth_key, const char *, omapi_auth_key_t,
48                 omapi_auth_hash_t,
49                 omapi_auth_key_reference, omapi_auth_key_dereference)
50
51 isc_result_t omapi_auth_key_new (omapi_auth_key_t **o, const char *file,
52                                  int line)
53 {
54         return omapi_auth_key_allocate (o, file, line);
55 }
56
57 isc_result_t omapi_auth_key_destroy (omapi_object_t *h,
58                                      const char *file, int line)
59 {
60         omapi_auth_key_t *a;
61
62         if (h -> type != omapi_type_auth_key)
63                 return ISC_R_INVALIDARG;
64         a = (omapi_auth_key_t *)h;
65
66         if (auth_key_hash)
67                 omapi_auth_key_hash_delete (auth_key_hash, a -> name, 0, MDL);
68
69         if (a -> name)
70                 dfree (a -> name, MDL);
71         if (a -> algorithm)
72                 dfree (a -> algorithm, MDL);
73         if (a -> key)
74                 omapi_data_string_dereference (&a -> key, MDL);
75         
76         return ISC_R_SUCCESS;
77 }
78
79 isc_result_t omapi_auth_key_enter (omapi_auth_key_t *a)
80 {
81         omapi_auth_key_t *tk;
82
83         if (a -> type != omapi_type_auth_key)
84                 return ISC_R_INVALIDARG;
85
86         tk = (omapi_auth_key_t *)0;
87         if (auth_key_hash) {
88                 omapi_auth_key_hash_lookup (&tk, auth_key_hash,
89                                             a -> name, 0, MDL);
90                 if (tk == a) {
91                         omapi_auth_key_dereference (&tk, MDL);
92                         return ISC_R_SUCCESS;
93                 }
94                 if (tk) {
95                         omapi_auth_key_hash_delete (auth_key_hash,
96                                                     tk -> name, 0, MDL);
97                         omapi_auth_key_dereference (&tk, MDL);
98                 }
99         } else {
100                 if (!omapi_auth_key_new_hash (&auth_key_hash, 1, MDL))
101                         return ISC_R_NOMEMORY;
102         }
103         omapi_auth_key_hash_add (auth_key_hash, a -> name, 0, a, MDL);
104         return ISC_R_SUCCESS;
105         
106 }
107
108 isc_result_t omapi_auth_key_lookup_name (omapi_auth_key_t **a,
109                                          const char *name)
110 {
111         if (!auth_key_hash)
112                 return ISC_R_NOTFOUND;
113         if (!omapi_auth_key_hash_lookup (a, auth_key_hash, name, 0, MDL))
114                 return ISC_R_NOTFOUND;
115         return ISC_R_SUCCESS;
116 }
117
118 isc_result_t omapi_auth_key_lookup (omapi_object_t **h,
119                                     omapi_object_t *id,
120                                     omapi_object_t *ref)
121 {
122         isc_result_t status;
123         omapi_value_t *name = (omapi_value_t *)0;
124         omapi_value_t *algorithm = (omapi_value_t *)0;
125
126         if (!auth_key_hash)
127                 return ISC_R_NOTFOUND;
128
129         if (!ref)
130                 return ISC_R_NOKEYS;
131
132         status = omapi_get_value_str (ref, id, "name", &name);
133         if (status != ISC_R_SUCCESS)
134                 return status;
135
136         if ((name -> value -> type != omapi_datatype_string) &&
137             (name -> value -> type != omapi_datatype_data)) {
138                 omapi_value_dereference (&name, MDL);
139                 return ISC_R_NOTFOUND;
140         }
141
142         status = omapi_get_value_str (ref, id, "algorithm", &algorithm);
143         if (status != ISC_R_SUCCESS) {
144                 omapi_value_dereference (&name, MDL);
145                 return status;
146         }
147
148         if ((algorithm -> value -> type != omapi_datatype_string) &&
149             (algorithm -> value -> type != omapi_datatype_data)) {
150                 omapi_value_dereference (&name, MDL);
151                 omapi_value_dereference (&algorithm, MDL);
152                 return ISC_R_NOTFOUND;
153         }
154
155
156         if (!omapi_auth_key_hash_lookup ((omapi_auth_key_t **)h, auth_key_hash,
157                                          (const char *)
158                                          name -> value -> u.buffer.value,
159                                          name -> value -> u.buffer.len, MDL)) {
160                 omapi_value_dereference (&name, MDL);
161                 omapi_value_dereference (&algorithm, MDL);
162                 return ISC_R_NOTFOUND;
163         }
164
165         if (omapi_td_strcasecmp (algorithm -> value,
166                                  ((omapi_auth_key_t *)*h) -> algorithm) != 0) {
167                 omapi_value_dereference (&name, MDL);
168                 omapi_value_dereference (&algorithm, MDL);
169                 omapi_object_dereference (h, MDL);
170                 return ISC_R_NOTFOUND;
171         }
172
173         omapi_value_dereference (&name, MDL);
174         omapi_value_dereference (&algorithm, MDL);
175
176         return ISC_R_SUCCESS;
177 }
178
179 isc_result_t omapi_auth_key_stuff_values (omapi_object_t *c,
180                                           omapi_object_t *id,
181                                           omapi_object_t *h)
182 {
183         omapi_auth_key_t *a;
184         isc_result_t status;
185
186         if (h -> type != omapi_type_auth_key)
187                 return ISC_R_INVALIDARG;
188         a = (omapi_auth_key_t *)h;
189
190         /* Write only the name and algorithm -- not the secret! */
191         if (a -> name) {
192                 status = omapi_connection_put_name (c, "name");
193                 if (status != ISC_R_SUCCESS)
194                         return status;
195                 status = omapi_connection_put_string (c, a -> name);
196                 if (status != ISC_R_SUCCESS)
197                         return status;
198         }
199         if (a -> algorithm) {
200                 status = omapi_connection_put_name (c, "algorithm");
201                 if (status != ISC_R_SUCCESS)
202                         return status;
203                 status = omapi_connection_put_string (c, a -> algorithm);
204                 if (status != ISC_R_SUCCESS)
205                         return status;
206         }
207
208         return ISC_R_SUCCESS;
209 }
210
211 isc_result_t omapi_auth_key_get_value (omapi_object_t *h,
212                                        omapi_object_t *id,
213                                        omapi_data_string_t *name,
214                                        omapi_value_t **value)
215 {
216         omapi_auth_key_t *a;
217         isc_result_t status;
218
219         if (h -> type != omapi_type_auth_key)
220                 return ISC_R_UNEXPECTED;
221         a = (omapi_auth_key_t *)h;
222
223         if (omapi_ds_strcmp (name, "name") == 0) {
224                 if (a -> name)
225                         return omapi_make_string_value
226                                 (value, name, a -> name, MDL);
227                 else
228                         return ISC_R_NOTFOUND;
229         } else if (omapi_ds_strcmp (name, "key") == 0) {
230                 if (a -> key) {
231                         status = omapi_value_new (value, MDL);
232                         if (status != ISC_R_SUCCESS)
233                                 return status;
234
235                         status = omapi_data_string_reference
236                                 (&(*value) -> name, name, MDL);
237                         if (status != ISC_R_SUCCESS) {
238                                 omapi_value_dereference (value, MDL);
239                                 return status;
240                         }
241
242                         status = omapi_typed_data_new (MDL, &(*value) -> value,
243                                                        omapi_datatype_data,
244                                                        a -> key -> len);
245                         if (status != ISC_R_SUCCESS) {
246                                 omapi_value_dereference (value, MDL);
247                                 return status;
248                         }
249
250                         memcpy ((*value) -> value -> u.buffer.value,
251                                 a -> key -> value, a -> key -> len);
252                         return ISC_R_SUCCESS;
253                 } else
254                         return ISC_R_NOTFOUND;
255         } else if (omapi_ds_strcmp (name, "algorithm") == 0) {
256                 if (a -> algorithm)
257                         return omapi_make_string_value
258                                 (value, name, a -> algorithm, MDL);
259                 else
260                         return ISC_R_NOTFOUND;
261         }
262
263         return ISC_R_SUCCESS;
264 }