dhcpctl/remote.c

Go to the documentation of this file.
00001 /* remote.c
00002 
00003    The dhcpctl remote object. */
00004 
00005 /*
00006  * Copyright (c) 2004,2007,2009,2014 by Internet Systems Consortium, Inc. ("ISC")
00007  * Copyright (c) 1999-2003 by Internet Software Consortium
00008  *
00009  * Permission to use, copy, modify, and distribute this software for any
00010  * purpose with or without fee is hereby granted, provided that the above
00011  * copyright notice and this permission notice appear in all copies.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
00014  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00015  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
00016  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00017  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00018  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
00019  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00020  *
00021  *   Internet Systems Consortium, Inc.
00022  *   950 Charter Street
00023  *   Redwood City, CA 94063
00024  *   <info@isc.org>
00025  *   https://www.isc.org/
00026  *
00027  */
00028 
00029 #include "dhcpd.h"
00030 #include <omapip/omapip_p.h>
00031 #include "dhcpctl.h"
00032 
00033 /* dhcpctl_new_authenticator
00034 
00035    synchronous - creates an authenticator object.
00036    returns nonzero status code if the object couldn't be created
00037    stores handle to authenticator through h if successful, and returns zero.
00038    name is the authenticator name (NUL-terminated string).
00039    algorithm is the NUL-terminated string name of the algorithm to use
00040    (currently, only "hmac-md5" is supported).
00041    secret and secret_len is the key secret. */
00042 
00043 dhcpctl_status dhcpctl_new_authenticator (dhcpctl_handle *h,
00044                                           const char *name,
00045                                           const char *algorithm,
00046                                           const unsigned char *secret,
00047                                           unsigned secret_len)
00048 {
00049         struct auth_key *key = (struct auth_key *)0;
00050         isc_result_t status;
00051 
00052         status = omapi_auth_key_new (&key, MDL);
00053         if (status != ISC_R_SUCCESS)
00054                 return status;
00055 
00056         key -> name = dmalloc (strlen (name) + 1, MDL);
00057         if (!key -> name) {
00058                 omapi_auth_key_dereference (&key, MDL);
00059                 return ISC_R_NOMEMORY;
00060         }
00061         strcpy (key -> name, name);
00062 
00063         /* If the algorithm name isn't an FQDN, tack on the
00064            .SIG-ALG.REG.NET. domain. */
00065         if (strchr (algorithm, '.') == 0) {
00066                 static char add[] = ".SIG-ALG.REG.INT.";
00067                 key -> algorithm = dmalloc (strlen (algorithm) +
00068                                             sizeof (add), MDL);
00069                 if (!key -> algorithm) {
00070                         omapi_auth_key_dereference (&key, MDL);
00071                         return ISC_R_NOMEMORY;
00072                 }
00073                 strcpy (key -> algorithm, algorithm);
00074                 strcat (key -> algorithm, add);
00075         } else {
00076                 key -> algorithm = dmalloc (strlen (algorithm) + 1, MDL);
00077                 if (!key -> algorithm) {
00078                         omapi_auth_key_dereference (&key, MDL);
00079                         return ISC_R_NOMEMORY;
00080                 }
00081                 strcpy (key -> algorithm, algorithm);
00082         }
00083 
00084         status = omapi_data_string_new (&key -> key, secret_len, MDL);
00085         if (status != ISC_R_SUCCESS) {
00086                 omapi_auth_key_dereference (&key, MDL);
00087                 return status;
00088         }
00089         memcpy (key -> key -> value, secret, secret_len);
00090         key -> key -> len = secret_len;
00091 
00092         *h = (dhcpctl_handle) key;
00093         return ISC_R_SUCCESS;
00094 }
00095 
00096 
00097 /* dhcpctl_new_object
00098 
00099    synchronous - creates a local handle for a host entry.
00100    returns nonzero status code if the local host entry couldn't
00101    be created
00102    stores handle to host through h if successful, and returns zero.
00103    object_type is a pointer to a NUL-terminated string containing
00104    the ascii name of the type of object being accessed - e.g., "host" */
00105 
00106 dhcpctl_status dhcpctl_new_object (dhcpctl_handle *h,
00107                                    dhcpctl_handle connection,
00108                                    const char *object_type)
00109 {
00110         dhcpctl_remote_object_t *m;
00111         omapi_object_t *g;
00112         isc_result_t status;
00113 
00114         m = (dhcpctl_remote_object_t *)0;
00115         status = omapi_object_allocate((omapi_object_t **)&m,
00116                                        dhcpctl_remote_type, 0, MDL);
00117         if (status != ISC_R_SUCCESS)
00118                 return status;
00119 
00120         g = (omapi_object_t *)0;
00121         status = omapi_generic_new (&g, MDL);
00122         if (status != ISC_R_SUCCESS) {
00123                 dfree (m, MDL);
00124                 return status;
00125         }
00126         status = omapi_object_reference (&m -> inner, g, MDL);
00127         if (status != ISC_R_SUCCESS) {
00128                 omapi_object_dereference ((omapi_object_t **)&m, MDL);
00129                 omapi_object_dereference (&g, MDL);
00130                 return status;
00131         }
00132         status = omapi_object_reference (&g -> outer,
00133                                          (omapi_object_t *)m, MDL);
00134 
00135         if (status != ISC_R_SUCCESS) {
00136                 omapi_object_dereference ((omapi_object_t **)&m, MDL);
00137                 omapi_object_dereference (&g, MDL);
00138                 return status;
00139         }
00140 
00141         status = omapi_typed_data_new (MDL, &m -> rtype,
00142                                        omapi_datatype_string,
00143                                        object_type);
00144         if (status != ISC_R_SUCCESS) {
00145                 omapi_object_dereference ((omapi_object_t **)&m, MDL);
00146                 omapi_object_dereference (&g, MDL);
00147                 return status;
00148         }
00149 
00150         status = omapi_object_reference (h, (omapi_object_t *)m, MDL);
00151         omapi_object_dereference ((omapi_object_t **)&m, MDL);
00152         omapi_object_dereference (&g, MDL);
00153         if (status != ISC_R_SUCCESS)
00154                 return status;
00155 
00156         return status;
00157 }
00158 
00159 /* asynchronous - just queues the request
00160    returns nonzero status code if open couldn't be queued
00161    returns zero if open was queued
00162    h is a handle to an object created by dhcpctl_new_object
00163    connection is a connection to a DHCP server
00164    flags include:
00165      DHCPCTL_CREATE - if the object doesn't exist, create it
00166      DHCPCTL_UPDATE - update the object on the server using the
00167                       attached parameters 
00168      DHCPCTL_EXCL - error if the object exists and DHCPCTL_CREATE
00169                       was also specified */
00170 
00171 dhcpctl_status dhcpctl_open_object (dhcpctl_handle h,
00172                                     dhcpctl_handle connection,
00173                                     int flags)
00174 {
00175         isc_result_t status;
00176         omapi_object_t *message = (omapi_object_t *)0;
00177         dhcpctl_remote_object_t *remote;
00178 
00179         if (h -> type != dhcpctl_remote_type)
00180                 return DHCP_R_INVALIDARG;
00181         remote = (dhcpctl_remote_object_t *)h;
00182 
00183         status = omapi_message_new (&message, MDL);
00184         if (status != ISC_R_SUCCESS)
00185                 return status;
00186         status = omapi_set_int_value (message, (omapi_object_t *)0,
00187                                       "op", OMAPI_OP_OPEN);
00188         if (status != ISC_R_SUCCESS) {
00189                 omapi_object_dereference (&message, MDL);
00190                 return status;
00191         }
00192         status = omapi_set_object_value (message, (omapi_object_t *)0,
00193                                          "object", h);
00194         if (status != ISC_R_SUCCESS) {
00195                 omapi_object_dereference (&message, MDL);
00196                 return status;
00197         }
00198         if (flags & DHCPCTL_CREATE) {
00199                 status = omapi_set_boolean_value (message, (omapi_object_t *)0,
00200                                                   "create", 1);
00201                 if (status != ISC_R_SUCCESS) {
00202                         omapi_object_dereference (&message, MDL);
00203                         return status;
00204                 }
00205         }
00206         if (flags & DHCPCTL_UPDATE) {
00207                 status = omapi_set_boolean_value (message, (omapi_object_t *)0,
00208                                                   "update", 1);
00209                 if (status != ISC_R_SUCCESS) {
00210                         omapi_object_dereference (&message, MDL);
00211                         return status;
00212                 }
00213         }
00214         if (flags & DHCPCTL_EXCL) {
00215                 status = omapi_set_boolean_value (message, (omapi_object_t *)0,
00216                                                   "exclusive", 1);
00217                 if (status != ISC_R_SUCCESS) {
00218                         omapi_object_dereference (&message, MDL);
00219                         return status;
00220                 }
00221         }
00222 
00223         if (remote -> rtype) {
00224                 status = omapi_set_value_str (message, (omapi_object_t *)0,
00225                                               "type", remote -> rtype);
00226                 if (status != ISC_R_SUCCESS) {
00227                         omapi_object_dereference (&message, MDL);
00228                         return status;
00229                 }
00230         }
00231 
00232         status = omapi_message_register (message);
00233         if (status != ISC_R_SUCCESS) {
00234                 omapi_object_dereference (&message, MDL);
00235                 return status;
00236         }
00237 
00238         status = omapi_protocol_send_message (connection -> outer,
00239                                             (omapi_object_t *)0,
00240                                             message, (omapi_object_t *)0);
00241 
00242         if (status != ISC_R_SUCCESS)
00243                 omapi_message_unregister (message);
00244 
00245         omapi_object_dereference (&message, MDL);
00246         return status;
00247 }
00248 
00249 /* Callback methods (not meant to be called directly) */
00250 
00251 isc_result_t dhcpctl_remote_set_value (omapi_object_t *h,
00252                                        omapi_object_t *id,
00253                                        omapi_data_string_t *name,
00254                                        omapi_typed_data_t *value)
00255 {
00256         dhcpctl_remote_object_t *ro;
00257         unsigned long rh;
00258         isc_result_t status;
00259 
00260         if (h -> type != dhcpctl_remote_type)
00261                 return DHCP_R_INVALIDARG;
00262         ro = (dhcpctl_remote_object_t *)h;
00263 
00264         if (!omapi_ds_strcmp (name, "remote-handle")) {
00265                 status = omapi_get_int_value (&rh, value);
00266                 if (status == ISC_R_SUCCESS)
00267                         ro -> remote_handle = rh;
00268                 return status;
00269         }
00270 
00271         if (h -> inner && h -> inner -> type -> set_value)
00272                 return (*(h -> inner -> type -> set_value))
00273                         (h -> inner, id, name, value);
00274         return ISC_R_NOTFOUND;
00275 }
00276 
00277 isc_result_t dhcpctl_remote_get_value (omapi_object_t *h,
00278                                        omapi_object_t *id,
00279                                        omapi_data_string_t *name,
00280                                        omapi_value_t **value)
00281 {
00282         if (h -> type != dhcpctl_remote_type)
00283                 return DHCP_R_INVALIDARG;
00284         
00285         if (h -> inner && h -> inner -> type -> get_value)
00286                 return (*(h -> inner -> type -> get_value))
00287                         (h -> inner, id, name, value);
00288         return ISC_R_NOTFOUND;
00289 }
00290 
00291 isc_result_t dhcpctl_remote_signal_handler (omapi_object_t *o,
00292                                             const char *name, va_list ap)
00293 {
00294         dhcpctl_remote_object_t *p;
00295         omapi_typed_data_t *tv;
00296 
00297         if (o -> type != dhcpctl_remote_type)
00298                 return DHCP_R_INVALIDARG;
00299         p = (dhcpctl_remote_object_t *)o;
00300 
00301         if (!strcmp (name, "updated")) {
00302                 p -> waitstatus = ISC_R_SUCCESS;
00303                 if (o -> inner -> type == omapi_type_generic)
00304                         omapi_generic_clear_flags (o -> inner);
00305                 return omapi_signal_in (o -> inner, "ready");
00306         }
00307         if (!strcmp (name, "status")) {
00308                 p -> waitstatus = va_arg (ap, isc_result_t);
00309                 if (p -> message)
00310                         omapi_typed_data_dereference (&p -> message, MDL);
00311                 tv = va_arg (ap, omapi_typed_data_t *);
00312                 if (tv)
00313                         omapi_typed_data_reference (&p -> message, tv, MDL);
00314                 return omapi_signal_in (o -> inner, "ready");
00315         }
00316 
00317         if (p -> inner && p -> inner -> type -> signal_handler)
00318                 return (*(p -> inner -> type -> signal_handler))
00319                         (p -> inner, name, ap);
00320 
00321         return ISC_R_SUCCESS;
00322 }
00323 
00324 isc_result_t dhcpctl_remote_destroy (omapi_object_t *h,
00325                                      const char *file, int line)
00326 {
00327         dhcpctl_remote_object_t *p;
00328         if (h -> type != dhcpctl_remote_type)
00329                 return DHCP_R_INVALIDARG;
00330         p = (dhcpctl_remote_object_t *)h;
00331         if (p -> handle)
00332                 omapi_object_dereference ((omapi_object_t **)&p -> handle,
00333                                           file, line);
00334         if (p -> rtype)
00335                 omapi_typed_data_dereference ((omapi_typed_data_t **)&p->rtype,
00336                                               file, line);
00337         return ISC_R_SUCCESS;
00338 }
00339 
00340 /* Write all the published values associated with the object through the
00341    specified connection. */
00342 
00343 isc_result_t dhcpctl_remote_stuff_values (omapi_object_t *c,
00344                                           omapi_object_t *id,
00345                                           omapi_object_t *p)
00346 {
00347         if (p -> type != dhcpctl_remote_type)
00348                 return DHCP_R_INVALIDARG;
00349 
00350         if (p -> inner && p -> inner -> type -> stuff_values)
00351                 return (*(p -> inner -> type -> stuff_values)) (c, id,
00352                                                                 p -> inner);
00353         return ISC_R_SUCCESS;
00354 }
00355 

Generated on 5 Apr 2014 for ISC DHCP by  doxygen 1.6.1