PageRenderTime 1396ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/src/netbsd/src/dist/dhcp/dhcpctl/remote.c

https://bitbucket.org/killerpenguinassassins/open_distrib_devel
C | 366 lines | 255 code | 46 blank | 65 comment | 75 complexity | 2c36d50311a03b890a1cdea637cca509 MD5 | raw file
Possible License(s): CC0-1.0, MIT, LGPL-2.0, LGPL-3.0, WTFPL, GPL-2.0, BSD-2-Clause, AGPL-3.0, CC-BY-SA-3.0, MPL-2.0, JSON, BSD-3-Clause-No-Nuclear-License-2014, LGPL-2.1, CPL-1.0, AGPL-1.0, 0BSD, ISC, Apache-2.0, GPL-3.0, IPL-1.0, MPL-2.0-no-copyleft-exception, BSD-3-Clause
  1. /* remote.c
  2. The dhcpctl remote object. */
  3. /*
  4. * Copyright (c) 2004 by Internet Systems Consortium, Inc. ("ISC")
  5. * Copyright (c) 1999-2003 by Internet Software Consortium
  6. *
  7. * Permission to use, copy, modify, and distribute this software for any
  8. * purpose with or without fee is hereby granted, provided that the above
  9. * copyright notice and this permission notice appear in all copies.
  10. *
  11. * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
  12. * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
  13. * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR
  14. * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  15. * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  16. * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  17. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  18. *
  19. * Internet Systems Consortium, Inc.
  20. * 950 Charter Street
  21. * Redwood City, CA 94063
  22. * <info@isc.org>
  23. * http://www.isc.org/
  24. *
  25. * This software has been written for Internet Systems Consortium
  26. * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
  27. * To learn more about Internet Systems Consortium, see
  28. * ``http://www.isc.org/''. To learn more about Vixie Enterprises,
  29. * see ``http://www.vix.com''. To learn more about Nominum, Inc., see
  30. * ``http://www.nominum.com''.
  31. */
  32. #ifndef lint
  33. static char copyright[] =
  34. "$Id: remote.c,v 1.5 2005/08/11 17:13:21 drochner Exp $ Copyright (c) 2004 Internet Systems Consortium. All rights reserved.\n";
  35. #endif /* not lint */
  36. #include <omapip/omapip_p.h>
  37. #include "dhcpctl.h"
  38. /* dhcpctl_new_authenticator
  39. synchronous - creates an authenticator object.
  40. returns nonzero status code if the object couldn't be created
  41. stores handle to authenticator through h if successful, and returns zero.
  42. name is the authenticator name (NUL-terminated string).
  43. algorithm is the NUL-terminated string name of the algorithm to use
  44. (currently, only "hmac-md5" is supported).
  45. secret and secret_len is the key secret. */
  46. dhcpctl_status dhcpctl_new_authenticator (dhcpctl_handle *h,
  47. const char *name,
  48. const char *algorithm,
  49. const unsigned char *secret,
  50. unsigned secret_len)
  51. {
  52. struct auth_key *key = (struct auth_key *)0;
  53. isc_result_t status;
  54. status = omapi_auth_key_new (&key, MDL);
  55. if (status != ISC_R_SUCCESS)
  56. return status;
  57. key -> name = dmalloc (strlen (name) + 1, MDL);
  58. if (!key -> name) {
  59. omapi_auth_key_dereference (&key, MDL);
  60. return ISC_R_NOMEMORY;
  61. }
  62. strcpy (key -> name, name);
  63. /* If the algorithm name isn't an FQDN, tack on the
  64. .SIG-ALG.REG.NET. domain. */
  65. if (strchr (algorithm, '.') == 0) {
  66. static char add[] = ".SIG-ALG.REG.INT.";
  67. key -> algorithm = dmalloc (strlen (algorithm) +
  68. sizeof (add), MDL);
  69. if (!key -> algorithm) {
  70. omapi_auth_key_dereference (&key, MDL);
  71. return ISC_R_NOMEMORY;
  72. }
  73. strcpy (key -> algorithm, algorithm);
  74. strcat (key -> algorithm, add);
  75. } else {
  76. key -> algorithm = dmalloc (strlen (algorithm) + 1, MDL);
  77. if (!key -> algorithm) {
  78. omapi_auth_key_dereference (&key, MDL);
  79. return ISC_R_NOMEMORY;
  80. }
  81. strcpy (key -> algorithm, algorithm);
  82. }
  83. status = omapi_data_string_new (&key -> key, secret_len, MDL);
  84. if (status != ISC_R_SUCCESS) {
  85. omapi_auth_key_dereference (&key, MDL);
  86. return status;
  87. }
  88. memcpy (key -> key -> value, secret, secret_len);
  89. key -> key -> len = secret_len;
  90. *h = (dhcpctl_handle) key;
  91. return ISC_R_SUCCESS;
  92. }
  93. /* dhcpctl_new_object
  94. synchronous - creates a local handle for a host entry.
  95. returns nonzero status code if the local host entry couldn't
  96. be created
  97. stores handle to host through h if successful, and returns zero.
  98. object_type is a pointer to a NUL-terminated string containing
  99. the ascii name of the type of object being accessed - e.g., "host" */
  100. dhcpctl_status dhcpctl_new_object (dhcpctl_handle *h,
  101. dhcpctl_handle connection,
  102. const char *object_type)
  103. {
  104. dhcpctl_remote_object_t *m;
  105. omapi_object_t *g;
  106. isc_result_t status;
  107. m = (dhcpctl_remote_object_t *)0;
  108. status = omapi_object_allocate ((void *)&m,
  109. dhcpctl_remote_type, 0, MDL);
  110. if (status != ISC_R_SUCCESS)
  111. return status;
  112. g = (omapi_object_t *)0;
  113. status = omapi_generic_new (&g, MDL);
  114. if (status != ISC_R_SUCCESS) {
  115. dfree (m, MDL);
  116. return status;
  117. }
  118. status = omapi_object_reference (&m -> inner, g, MDL);
  119. if (status != ISC_R_SUCCESS) {
  120. omapi_object_dereference ((void *)&m, MDL);
  121. omapi_object_dereference (&g, MDL);
  122. return status;
  123. }
  124. status = omapi_object_reference (&g -> outer,
  125. (omapi_object_t *)m, MDL);
  126. if (status != ISC_R_SUCCESS) {
  127. omapi_object_dereference ((void *)&m, MDL);
  128. omapi_object_dereference (&g, MDL);
  129. return status;
  130. }
  131. status = omapi_typed_data_new (MDL, &m -> rtype,
  132. omapi_datatype_string,
  133. object_type);
  134. if (status != ISC_R_SUCCESS) {
  135. omapi_object_dereference ((void *)&m, MDL);
  136. omapi_object_dereference (&g, MDL);
  137. return status;
  138. }
  139. status = omapi_object_reference (h, (omapi_object_t *)m, MDL);
  140. omapi_object_dereference ((void *)&m, MDL);
  141. omapi_object_dereference (&g, MDL);
  142. if (status != ISC_R_SUCCESS)
  143. return status;
  144. return status;
  145. }
  146. /* asynchronous - just queues the request
  147. returns nonzero status code if open couldn't be queued
  148. returns zero if open was queued
  149. h is a handle to an object created by dhcpctl_new_object
  150. connection is a connection to a DHCP server
  151. flags include:
  152. DHCPCTL_CREATE - if the object doesn't exist, create it
  153. DHCPCTL_UPDATE - update the object on the server using the
  154. attached parameters
  155. DHCPCTL_EXCL - error if the object exists and DHCPCTL_CREATE
  156. was also specified */
  157. dhcpctl_status dhcpctl_open_object (dhcpctl_handle h,
  158. dhcpctl_handle connection,
  159. int flags)
  160. {
  161. isc_result_t status;
  162. omapi_object_t *message = (omapi_object_t *)0;
  163. dhcpctl_remote_object_t *remote;
  164. if (h -> type != dhcpctl_remote_type)
  165. return ISC_R_INVALIDARG;
  166. remote = (dhcpctl_remote_object_t *)h;
  167. status = omapi_message_new (&message, MDL);
  168. if (status != ISC_R_SUCCESS)
  169. return status;
  170. status = omapi_set_int_value (message, (omapi_object_t *)0,
  171. "op", OMAPI_OP_OPEN);
  172. if (status != ISC_R_SUCCESS) {
  173. omapi_object_dereference (&message, MDL);
  174. return status;
  175. }
  176. status = omapi_set_object_value (message, (omapi_object_t *)0,
  177. "object", h);
  178. if (status != ISC_R_SUCCESS) {
  179. omapi_object_dereference (&message, MDL);
  180. return status;
  181. }
  182. if (flags & DHCPCTL_CREATE) {
  183. status = omapi_set_boolean_value (message, (omapi_object_t *)0,
  184. "create", 1);
  185. if (status != ISC_R_SUCCESS) {
  186. omapi_object_dereference (&message, MDL);
  187. return status;
  188. }
  189. }
  190. if (flags & DHCPCTL_UPDATE) {
  191. status = omapi_set_boolean_value (message, (omapi_object_t *)0,
  192. "update", 1);
  193. if (status != ISC_R_SUCCESS) {
  194. omapi_object_dereference (&message, MDL);
  195. return status;
  196. }
  197. }
  198. if (flags & DHCPCTL_EXCL) {
  199. status = omapi_set_boolean_value (message, (omapi_object_t *)0,
  200. "exclusive", 1);
  201. if (status != ISC_R_SUCCESS) {
  202. omapi_object_dereference (&message, MDL);
  203. return status;
  204. }
  205. }
  206. if (remote -> rtype) {
  207. status = omapi_set_value_str (message, (omapi_object_t *)0,
  208. "type", remote -> rtype);
  209. if (status != ISC_R_SUCCESS) {
  210. omapi_object_dereference (&message, MDL);
  211. return status;
  212. }
  213. }
  214. status = omapi_message_register (message);
  215. if (status != ISC_R_SUCCESS) {
  216. omapi_object_dereference (&message, MDL);
  217. return status;
  218. }
  219. status = omapi_protocol_send_message (connection -> outer,
  220. (omapi_object_t *)0,
  221. message, (omapi_object_t *)0);
  222. if (status != ISC_R_SUCCESS)
  223. omapi_message_unregister (message);
  224. omapi_object_dereference (&message, MDL);
  225. return status;
  226. }
  227. /* Callback methods (not meant to be called directly) */
  228. isc_result_t dhcpctl_remote_set_value (omapi_object_t *h,
  229. omapi_object_t *id,
  230. omapi_data_string_t *name,
  231. omapi_typed_data_t *value)
  232. {
  233. dhcpctl_remote_object_t *ro;
  234. unsigned long rh;
  235. isc_result_t status;
  236. if (h -> type != dhcpctl_remote_type)
  237. return ISC_R_INVALIDARG;
  238. ro = (dhcpctl_remote_object_t *)h;
  239. if (!omapi_ds_strcmp (name, "remote-handle")) {
  240. status = omapi_get_int_value (&rh, value);
  241. if (status == ISC_R_SUCCESS)
  242. ro -> remote_handle = rh;
  243. return status;
  244. }
  245. if (h -> inner && h -> inner -> type -> set_value)
  246. return (*(h -> inner -> type -> set_value))
  247. (h -> inner, id, name, value);
  248. return ISC_R_NOTFOUND;
  249. }
  250. isc_result_t dhcpctl_remote_get_value (omapi_object_t *h,
  251. omapi_object_t *id,
  252. omapi_data_string_t *name,
  253. omapi_value_t **value)
  254. {
  255. if (h -> type != dhcpctl_remote_type)
  256. return ISC_R_INVALIDARG;
  257. if (h -> inner && h -> inner -> type -> get_value)
  258. return (*(h -> inner -> type -> get_value))
  259. (h -> inner, id, name, value);
  260. return ISC_R_NOTFOUND;
  261. }
  262. isc_result_t dhcpctl_remote_signal_handler (omapi_object_t *o,
  263. const char *name, va_list ap)
  264. {
  265. dhcpctl_remote_object_t *p;
  266. omapi_typed_data_t *tv;
  267. if (o -> type != dhcpctl_remote_type)
  268. return ISC_R_INVALIDARG;
  269. p = (dhcpctl_remote_object_t *)o;
  270. if (!strcmp (name, "updated")) {
  271. p -> waitstatus = ISC_R_SUCCESS;
  272. if (o -> inner -> type == omapi_type_generic)
  273. omapi_generic_clear_flags (o -> inner);
  274. return omapi_signal_in (o -> inner, "ready");
  275. }
  276. if (!strcmp (name, "status")) {
  277. p -> waitstatus = va_arg (ap, isc_result_t);
  278. if (p -> message)
  279. omapi_typed_data_dereference (&p -> message, MDL);
  280. tv = va_arg (ap, omapi_typed_data_t *);
  281. if (tv)
  282. omapi_typed_data_reference (&p -> message, tv, MDL);
  283. return omapi_signal_in (o -> inner, "ready");
  284. }
  285. if (p -> inner && p -> inner -> type -> signal_handler)
  286. return (*(p -> inner -> type -> signal_handler))
  287. (p -> inner, name, ap);
  288. return ISC_R_SUCCESS;
  289. }
  290. isc_result_t dhcpctl_remote_destroy (omapi_object_t *h,
  291. const char *file, int line)
  292. {
  293. dhcpctl_remote_object_t *p;
  294. if (h -> type != dhcpctl_remote_type)
  295. return ISC_R_INVALIDARG;
  296. p = (dhcpctl_remote_object_t *)h;
  297. if (p -> handle)
  298. omapi_object_dereference ((omapi_object_t **)&p -> handle,
  299. file, line);
  300. if (p -> rtype)
  301. omapi_typed_data_dereference ((omapi_typed_data_t **)&p->rtype,
  302. file, line);
  303. return ISC_R_SUCCESS;
  304. }
  305. /* Write all the published values associated with the object through the
  306. specified connection. */
  307. isc_result_t dhcpctl_remote_stuff_values (omapi_object_t *c,
  308. omapi_object_t *id,
  309. omapi_object_t *p)
  310. {
  311. if (p -> type != dhcpctl_remote_type)
  312. return ISC_R_INVALIDARG;
  313. if (p -> inner && p -> inner -> type -> stuff_values)
  314. return (*(p -> inner -> type -> stuff_values)) (c, id,
  315. p -> inner);
  316. return ISC_R_SUCCESS;
  317. }