Annotation of embedaddon/dhcp/omapip/auth.c, revision 1.1.1.1

1.1       misho       1: /* auth.c
                      2: 
                      3:    Subroutines having to do with authentication. */
                      4: 
                      5: /*
                      6:  * Copyright (c) 2004,2007,2009 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:  *   https://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:  * ``https://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: #include "dhcpd.h"
                     36: 
                     37: #include <omapip/omapip_p.h>
                     38: 
                     39: OMAPI_OBJECT_ALLOC (omapi_auth_key, omapi_auth_key_t, omapi_type_auth_key)
                     40: typedef struct hash omapi_auth_hash_t;
                     41: HASH_FUNCTIONS_DECL (omapi_auth_key, const char *,
                     42:                     omapi_auth_key_t, omapi_auth_hash_t)
                     43: omapi_auth_hash_t *auth_key_hash;
                     44: HASH_FUNCTIONS (omapi_auth_key, const char *, omapi_auth_key_t,
                     45:                omapi_auth_hash_t,
                     46:                omapi_auth_key_reference, omapi_auth_key_dereference,
                     47:                do_case_hash)
                     48: 
                     49: isc_result_t omapi_auth_key_new (omapi_auth_key_t **o, const char *file,
                     50:                                 int line)
                     51: {
                     52:        return omapi_auth_key_allocate (o, file, line);
                     53: }
                     54: 
                     55: isc_result_t omapi_auth_key_destroy (omapi_object_t *h,
                     56:                                     const char *file, int line)
                     57: {
                     58:        omapi_auth_key_t *a;
                     59: 
                     60:        if (h -> type != omapi_type_auth_key)
                     61:                return ISC_R_INVALIDARG;
                     62:        a = (omapi_auth_key_t *)h;
                     63: 
                     64:        if (auth_key_hash)
                     65:                omapi_auth_key_hash_delete (auth_key_hash, a -> name, 0, MDL);
                     66: 
                     67:        if (a -> name)
                     68:                dfree (a -> name, MDL);
                     69:        if (a -> algorithm)
                     70:                dfree (a -> algorithm, MDL);
                     71:        if (a -> key)
                     72:                omapi_data_string_dereference (&a -> key, MDL);
                     73:        
                     74:        return ISC_R_SUCCESS;
                     75: }
                     76: 
                     77: isc_result_t omapi_auth_key_enter (omapi_auth_key_t *a)
                     78: {
                     79:        omapi_auth_key_t *tk;
                     80: 
                     81:        if (a -> type != omapi_type_auth_key)
                     82:                return ISC_R_INVALIDARG;
                     83: 
                     84:        tk = (omapi_auth_key_t *)0;
                     85:        if (auth_key_hash) {
                     86:                omapi_auth_key_hash_lookup (&tk, auth_key_hash,
                     87:                                            a -> name, 0, MDL);
                     88:                if (tk == a) {
                     89:                        omapi_auth_key_dereference (&tk, MDL);
                     90:                        return ISC_R_SUCCESS;
                     91:                }
                     92:                if (tk) {
                     93:                        omapi_auth_key_hash_delete (auth_key_hash,
                     94:                                                    tk -> name, 0, MDL);
                     95:                        omapi_auth_key_dereference (&tk, MDL);
                     96:                }
                     97:        } else {
                     98:                if (!omapi_auth_key_new_hash(&auth_key_hash,
                     99:                                             KEY_HASH_SIZE, MDL))
                    100:                        return ISC_R_NOMEMORY;
                    101:        }
                    102:        omapi_auth_key_hash_add (auth_key_hash, a -> name, 0, a, MDL);
                    103:        return ISC_R_SUCCESS;
                    104:        
                    105: }
                    106: 
                    107: isc_result_t omapi_auth_key_lookup_name (omapi_auth_key_t **a,
                    108:                                         const char *name)
                    109: {
                    110:        if (!auth_key_hash)
                    111:                return ISC_R_NOTFOUND;
                    112:        if (!omapi_auth_key_hash_lookup (a, auth_key_hash, name, 0, MDL))
                    113:                return ISC_R_NOTFOUND;
                    114:        return ISC_R_SUCCESS;
                    115: }
                    116: 
                    117: isc_result_t omapi_auth_key_lookup (omapi_object_t **h,
                    118:                                    omapi_object_t *id,
                    119:                                    omapi_object_t *ref)
                    120: {
                    121:        isc_result_t status;
                    122:        omapi_value_t *name = (omapi_value_t *)0;
                    123:        omapi_value_t *algorithm = (omapi_value_t *)0;
                    124: 
                    125:        if (!auth_key_hash)
                    126:                return ISC_R_NOTFOUND;
                    127: 
                    128:        if (!ref)
                    129:                return ISC_R_NOKEYS;
                    130: 
                    131:        status = omapi_get_value_str (ref, id, "name", &name);
                    132:        if (status != ISC_R_SUCCESS)
                    133:                return status;
                    134: 
                    135:        if ((name -> value -> type != omapi_datatype_string) &&
                    136:            (name -> value -> type != omapi_datatype_data)) {
                    137:                omapi_value_dereference (&name, MDL);
                    138:                return ISC_R_NOTFOUND;
                    139:        }
                    140: 
                    141:        status = omapi_get_value_str (ref, id, "algorithm", &algorithm);
                    142:        if (status != ISC_R_SUCCESS) {
                    143:                omapi_value_dereference (&name, MDL);
                    144:                return status;
                    145:        }
                    146: 
                    147:        if ((algorithm -> value -> type != omapi_datatype_string) &&
                    148:            (algorithm -> value -> type != omapi_datatype_data)) {
                    149:                omapi_value_dereference (&name, MDL);
                    150:                omapi_value_dereference (&algorithm, MDL);
                    151:                return ISC_R_NOTFOUND;
                    152:        }
                    153: 
                    154: 
                    155:        if (!omapi_auth_key_hash_lookup ((omapi_auth_key_t **)h, auth_key_hash,
                    156:                                         (const char *)
                    157:                                         name -> value -> u.buffer.value,
                    158:                                         name -> value -> u.buffer.len, MDL)) {
                    159:                omapi_value_dereference (&name, MDL);
                    160:                omapi_value_dereference (&algorithm, MDL);
                    161:                return ISC_R_NOTFOUND;
                    162:        }
                    163: 
                    164:        if (omapi_td_strcasecmp (algorithm -> value,
                    165:                                 ((omapi_auth_key_t *)*h) -> algorithm) != 0) {
                    166:                omapi_value_dereference (&name, MDL);
                    167:                omapi_value_dereference (&algorithm, MDL);
                    168:                omapi_object_dereference (h, MDL);
                    169:                return ISC_R_NOTFOUND;
                    170:        }
                    171: 
                    172:        omapi_value_dereference (&name, MDL);
                    173:        omapi_value_dereference (&algorithm, MDL);
                    174: 
                    175:        return ISC_R_SUCCESS;
                    176: }
                    177: 
                    178: isc_result_t omapi_auth_key_stuff_values (omapi_object_t *c,
                    179:                                          omapi_object_t *id,
                    180:                                          omapi_object_t *h)
                    181: {
                    182:        omapi_auth_key_t *a;
                    183:        isc_result_t status;
                    184: 
                    185:        if (h -> type != omapi_type_auth_key)
                    186:                return ISC_R_INVALIDARG;
                    187:        a = (omapi_auth_key_t *)h;
                    188: 
                    189:        /* Write only the name and algorithm -- not the secret! */
                    190:        if (a -> name) {
                    191:                status = omapi_connection_put_name (c, "name");
                    192:                if (status != ISC_R_SUCCESS)
                    193:                        return status;
                    194:                status = omapi_connection_put_string (c, a -> name);
                    195:                if (status != ISC_R_SUCCESS)
                    196:                        return status;
                    197:        }
                    198:        if (a -> algorithm) {
                    199:                status = omapi_connection_put_name (c, "algorithm");
                    200:                if (status != ISC_R_SUCCESS)
                    201:                        return status;
                    202:                status = omapi_connection_put_string (c, a -> algorithm);
                    203:                if (status != ISC_R_SUCCESS)
                    204:                        return status;
                    205:        }
                    206: 
                    207:        return ISC_R_SUCCESS;
                    208: }
                    209: 
                    210: isc_result_t omapi_auth_key_get_value (omapi_object_t *h,
                    211:                                       omapi_object_t *id,
                    212:                                       omapi_data_string_t *name,
                    213:                                       omapi_value_t **value)
                    214: {
                    215:        omapi_auth_key_t *a;
                    216:        isc_result_t status;
                    217: 
                    218:        if (h -> type != omapi_type_auth_key)
                    219:                return ISC_R_UNEXPECTED;
                    220:        a = (omapi_auth_key_t *)h;
                    221: 
                    222:        if (omapi_ds_strcmp (name, "name") == 0) {
                    223:                if (a -> name)
                    224:                        return omapi_make_string_value
                    225:                                (value, name, a -> name, MDL);
                    226:                else
                    227:                        return ISC_R_NOTFOUND;
                    228:        } else if (omapi_ds_strcmp (name, "key") == 0) {
                    229:                if (a -> key) {
                    230:                        status = omapi_value_new (value, MDL);
                    231:                        if (status != ISC_R_SUCCESS)
                    232:                                return status;
                    233: 
                    234:                        status = omapi_data_string_reference
                    235:                                (&(*value) -> name, name, MDL);
                    236:                        if (status != ISC_R_SUCCESS) {
                    237:                                omapi_value_dereference (value, MDL);
                    238:                                return status;
                    239:                        }
                    240: 
                    241:                        status = omapi_typed_data_new (MDL, &(*value) -> value,
                    242:                                                       omapi_datatype_data,
                    243:                                                       a -> key -> len);
                    244:                        if (status != ISC_R_SUCCESS) {
                    245:                                omapi_value_dereference (value, MDL);
                    246:                                return status;
                    247:                        }
                    248: 
                    249:                        memcpy ((*value) -> value -> u.buffer.value,
                    250:                                a -> key -> value, a -> key -> len);
                    251:                        return ISC_R_SUCCESS;
                    252:                } else
                    253:                        return ISC_R_NOTFOUND;
                    254:        } else if (omapi_ds_strcmp (name, "algorithm") == 0) {
                    255:                if (a -> algorithm)
                    256:                        return omapi_make_string_value
                    257:                                (value, name, a -> algorithm, MDL);
                    258:                else
                    259:                        return ISC_R_NOTFOUND;
                    260:        }
                    261: 
                    262:        return ISC_R_SUCCESS;
                    263: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>