Make setthetime() static per the prototype.
[dragonfly.git] / contrib / isc-dhcp / omapip / auth.c
1 /* auth.c
2
3    Subroutines having to do with authentication. */
4
5 /*
6  * Copyright (c) 1998-2001 Internet Software Consortium.
7  * All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  * 3. Neither the name of The Internet Software Consortium nor the names
19  *    of its contributors may be used to endorse or promote products derived
20  *    from this software without specific prior written permission.
21  *
22  * THIS SOFTWARE IS PROVIDED BY THE INTERNET SOFTWARE CONSORTIUM AND
23  * CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
24  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
25  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
26  * DISCLAIMED.  IN NO EVENT SHALL THE INTERNET SOFTWARE CONSORTIUM OR
27  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
30  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
31  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
32  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
33  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34  * SUCH DAMAGE.
35  *
36  * This software has been written for the Internet Software Consortium
37  * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
38  * To learn more about the Internet Software Consortium, see
39  * ``http://www.isc.org/''.  To learn more about Vixie Enterprises,
40  * see ``http://www.vix.com''.   To learn more about Nominum, Inc., see
41  * ``http://www.nominum.com''.
42  */
43
44 #ifndef lint
45 static char ocopyright[] =
46 "$Id: auth.c,v 1.3.2.3 2001/10/17 03:27:56 mellon Exp $ Copyright 1998-2000 The Internet Software Consortium.";
47 #endif
48
49 #include <omapip/omapip_p.h>
50
51 OMAPI_OBJECT_ALLOC (omapi_auth_key, omapi_auth_key_t, omapi_type_auth_key)
52 typedef struct hash omapi_auth_hash_t;
53 HASH_FUNCTIONS_DECL (omapi_auth_key, const char *,
54                      omapi_auth_key_t, omapi_auth_hash_t)
55 omapi_auth_hash_t *auth_key_hash;
56 HASH_FUNCTIONS (omapi_auth_key, const char *, omapi_auth_key_t,
57                 omapi_auth_hash_t,
58                 omapi_auth_key_reference, omapi_auth_key_dereference)
59
60 isc_result_t omapi_auth_key_new (omapi_auth_key_t **o, const char *file,
61                                  int line)
62 {
63         return omapi_auth_key_allocate (o, file, line);
64 }
65
66 isc_result_t omapi_auth_key_destroy (omapi_object_t *h,
67                                      const char *file, int line)
68 {
69         omapi_auth_key_t *a;
70
71         if (h -> type != omapi_type_auth_key)
72                 return ISC_R_INVALIDARG;
73         a = (omapi_auth_key_t *)h;
74
75         if (auth_key_hash)
76                 omapi_auth_key_hash_delete (auth_key_hash, a -> name, 0, MDL);
77
78         if (a -> name)
79                 dfree (a -> name, MDL);
80         if (a -> algorithm)
81                 dfree (a -> algorithm, MDL);
82         if (a -> key)
83                 omapi_data_string_dereference (&a -> key, MDL);
84         
85         return ISC_R_SUCCESS;
86 }
87
88 isc_result_t omapi_auth_key_enter (omapi_auth_key_t *a)
89 {
90         omapi_auth_key_t *tk;
91
92         if (a -> type != omapi_type_auth_key)
93                 return ISC_R_INVALIDARG;
94
95         tk = (omapi_auth_key_t *)0;
96         if (auth_key_hash) {
97                 omapi_auth_key_hash_lookup (&tk, auth_key_hash,
98                                             a -> name, 0, MDL);
99                 if (tk == a) {
100                         omapi_auth_key_dereference (&tk, MDL);
101                         return ISC_R_SUCCESS;
102                 }
103                 if (tk) {
104                         omapi_auth_key_hash_delete (auth_key_hash,
105                                                     tk -> name, 0, MDL);
106                         omapi_auth_key_dereference (&tk, MDL);
107                 }
108         } else {
109                 if (!omapi_auth_key_new_hash (&auth_key_hash, 1, MDL))
110                         return ISC_R_NOMEMORY;
111         }
112         omapi_auth_key_hash_add (auth_key_hash, a -> name, 0, a, MDL);
113         return ISC_R_SUCCESS;
114         
115 }
116
117 isc_result_t omapi_auth_key_lookup_name (omapi_auth_key_t **a,
118                                          const char *name)
119 {
120         if (!auth_key_hash)
121                 return ISC_R_NOTFOUND;
122         if (!omapi_auth_key_hash_lookup (a, auth_key_hash, name, 0, MDL))
123                 return ISC_R_NOTFOUND;
124         return ISC_R_SUCCESS;
125 }
126
127 isc_result_t omapi_auth_key_lookup (omapi_object_t **h,
128                                     omapi_object_t *id,
129                                     omapi_object_t *ref)
130 {
131         isc_result_t status;
132         omapi_value_t *name = (omapi_value_t *)0;
133         omapi_value_t *algorithm = (omapi_value_t *)0;
134
135         if (!auth_key_hash)
136                 return ISC_R_NOTFOUND;
137
138         if (!ref)
139                 return ISC_R_NOKEYS;
140
141         status = omapi_get_value_str (ref, id, "name", &name);
142         if (status != ISC_R_SUCCESS)
143                 return status;
144
145         if ((name -> value -> type != omapi_datatype_string) &&
146             (name -> value -> type != omapi_datatype_data)) {
147                 omapi_value_dereference (&name, MDL);
148                 return ISC_R_NOTFOUND;
149         }
150
151         status = omapi_get_value_str (ref, id, "algorithm", &algorithm);
152         if (status != ISC_R_SUCCESS) {
153                 omapi_value_dereference (&name, MDL);
154                 return status;
155         }
156
157         if ((algorithm -> value -> type != omapi_datatype_string) &&
158             (algorithm -> value -> type != omapi_datatype_data)) {
159                 omapi_value_dereference (&name, MDL);
160                 omapi_value_dereference (&algorithm, MDL);
161                 return ISC_R_NOTFOUND;
162         }
163
164
165         if (!omapi_auth_key_hash_lookup ((omapi_auth_key_t **)h, auth_key_hash,
166                                          (const char *)
167                                          name -> value -> u.buffer.value,
168                                          name -> value -> u.buffer.len, MDL)) {
169                 omapi_value_dereference (&name, MDL);
170                 omapi_value_dereference (&algorithm, MDL);
171                 return ISC_R_NOTFOUND;
172         }
173
174         if (omapi_td_strcasecmp (algorithm -> value,
175                                  ((omapi_auth_key_t *)*h) -> algorithm) != 0) {
176                 omapi_value_dereference (&name, MDL);
177                 omapi_value_dereference (&algorithm, MDL);
178                 omapi_object_dereference (h, MDL);
179                 return ISC_R_NOTFOUND;
180         }
181
182         omapi_value_dereference (&name, MDL);
183         omapi_value_dereference (&algorithm, MDL);
184
185         return ISC_R_SUCCESS;
186 }
187
188 isc_result_t omapi_auth_key_stuff_values (omapi_object_t *c,
189                                           omapi_object_t *id,
190                                           omapi_object_t *h)
191 {
192         omapi_auth_key_t *a;
193         isc_result_t status;
194
195         if (h -> type != omapi_type_auth_key)
196                 return ISC_R_INVALIDARG;
197         a = (omapi_auth_key_t *)h;
198
199         /* Write only the name and algorithm -- not the secret! */
200         if (a -> name) {
201                 status = omapi_connection_put_name (c, "name");
202                 if (status != ISC_R_SUCCESS)
203                         return status;
204                 status = omapi_connection_put_string (c, a -> name);
205                 if (status != ISC_R_SUCCESS)
206                         return status;
207         }
208         if (a -> algorithm) {
209                 status = omapi_connection_put_name (c, "algorithm");
210                 if (status != ISC_R_SUCCESS)
211                         return status;
212                 status = omapi_connection_put_string (c, a -> algorithm);
213                 if (status != ISC_R_SUCCESS)
214                         return status;
215         }
216
217         return ISC_R_SUCCESS;
218 }
219
220 isc_result_t omapi_auth_key_get_value (omapi_object_t *h,
221                                        omapi_object_t *id,
222                                        omapi_data_string_t *name,
223                                        omapi_value_t **value)
224 {
225         omapi_auth_key_t *a;
226         isc_result_t status;
227
228         if (h -> type != omapi_type_auth_key)
229                 return ISC_R_UNEXPECTED;
230         a = (omapi_auth_key_t *)h;
231
232         if (omapi_ds_strcmp (name, "name") == 0) {
233                 if (a -> name)
234                         return omapi_make_string_value
235                                 (value, name, a -> name, MDL);
236                 else
237                         return ISC_R_NOTFOUND;
238         } else if (omapi_ds_strcmp (name, "key") == 0) {
239                 if (a -> key) {
240                         status = omapi_value_new (value, MDL);
241                         if (status != ISC_R_SUCCESS)
242                                 return status;
243
244                         status = omapi_data_string_reference
245                                 (&(*value) -> name, name, MDL);
246                         if (status != ISC_R_SUCCESS) {
247                                 omapi_value_dereference (value, MDL);
248                                 return status;
249                         }
250
251                         status = omapi_typed_data_new (MDL, &(*value) -> value,
252                                                        omapi_datatype_data,
253                                                        a -> key -> len);
254                         if (status != ISC_R_SUCCESS) {
255                                 omapi_value_dereference (value, MDL);
256                                 return status;
257                         }
258
259                         memcpy ((*value) -> value -> u.buffer.value,
260                                 a -> key -> value, a -> key -> len);
261                         return ISC_R_SUCCESS;
262                 } else
263                         return ISC_R_NOTFOUND;
264         } else if (omapi_ds_strcmp (name, "algorithm") == 0) {
265                 if (a -> algorithm)
266                         return omapi_make_string_value
267                                 (value, name, a -> algorithm, MDL);
268                 else
269                         return ISC_R_NOTFOUND;
270         }
271
272         return ISC_R_SUCCESS;
273 }