PageRenderTime 51ms CodeModel.GetById 25ms RepoModel.GetById 0ms app.codeStats 0ms

/uClinux-dist/user/dhcp-isc/dhcpctl/remote.c

https://bitbucket.org/__wp__/mb-linux-msli
C | 361 lines | 252 code | 44 blank | 65 comment | 75 complexity | 34d8450fed75cfcf08b9b3b7a550e5a3 MD5 | raw file
Possible License(s): AGPL-3.0, GPL-2.0, LGPL-2.0, MPL-2.0, ISC, BSD-3-Clause, LGPL-2.1, MPL-2.0-no-copyleft-exception, 0BSD, CC-BY-SA-3.0, GPL-3.0, LGPL-3.0, AGPL-1.0, Unlicense
  1. /* remote.c
  2. The dhcpctl remote object. */
  3. /*
  4. * Copyright (c) 2004,2007 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. #include "dhcpd.h"
  33. #include <omapip/omapip_p.h>
  34. #include "dhcpctl.h"
  35. /* dhcpctl_new_authenticator
  36. synchronous - creates an authenticator object.
  37. returns nonzero status code if the object couldn't be created
  38. stores handle to authenticator through h if successful, and returns zero.
  39. name is the authenticator name (NUL-terminated string).
  40. algorithm is the NUL-terminated string name of the algorithm to use
  41. (currently, only "hmac-md5" is supported).
  42. secret and secret_len is the key secret. */
  43. dhcpctl_status dhcpctl_new_authenticator (dhcpctl_handle *h,
  44. const char *name,
  45. const char *algorithm,
  46. const unsigned char *secret,
  47. unsigned secret_len)
  48. {
  49. struct auth_key *key = (struct auth_key *)0;
  50. isc_result_t status;
  51. status = omapi_auth_key_new (&key, MDL);
  52. if (status != ISC_R_SUCCESS)
  53. return status;
  54. key -> name = dmalloc (strlen (name) + 1, MDL);
  55. if (!key -> name) {
  56. omapi_auth_key_dereference (&key, MDL);
  57. return ISC_R_NOMEMORY;
  58. }
  59. strcpy (key -> name, name);
  60. /* If the algorithm name isn't an FQDN, tack on the
  61. .SIG-ALG.REG.NET. domain. */
  62. if (strchr (algorithm, '.') == 0) {
  63. static char add[] = ".SIG-ALG.REG.INT.";
  64. key -> algorithm = dmalloc (strlen (algorithm) +
  65. sizeof (add), MDL);
  66. if (!key -> algorithm) {
  67. omapi_auth_key_dereference (&key, MDL);
  68. return ISC_R_NOMEMORY;
  69. }
  70. strcpy (key -> algorithm, algorithm);
  71. strcat (key -> algorithm, add);
  72. } else {
  73. key -> algorithm = dmalloc (strlen (algorithm) + 1, MDL);
  74. if (!key -> algorithm) {
  75. omapi_auth_key_dereference (&key, MDL);
  76. return ISC_R_NOMEMORY;
  77. }
  78. strcpy (key -> algorithm, algorithm);
  79. }
  80. status = omapi_data_string_new (&key -> key, secret_len, MDL);
  81. if (status != ISC_R_SUCCESS) {
  82. omapi_auth_key_dereference (&key, MDL);
  83. return status;
  84. }
  85. memcpy (key -> key -> value, secret, secret_len);
  86. key -> key -> len = secret_len;
  87. *h = (dhcpctl_handle) key;
  88. return ISC_R_SUCCESS;
  89. }
  90. /* dhcpctl_new_object
  91. synchronous - creates a local handle for a host entry.
  92. returns nonzero status code if the local host entry couldn't
  93. be created
  94. stores handle to host through h if successful, and returns zero.
  95. object_type is a pointer to a NUL-terminated string containing
  96. the ascii name of the type of object being accessed - e.g., "host" */
  97. dhcpctl_status dhcpctl_new_object (dhcpctl_handle *h,
  98. dhcpctl_handle connection,
  99. const char *object_type)
  100. {
  101. dhcpctl_remote_object_t *m;
  102. omapi_object_t *g;
  103. isc_result_t status;
  104. m = (dhcpctl_remote_object_t *)0;
  105. status = omapi_object_allocate((omapi_object_t **)&m,
  106. dhcpctl_remote_type, 0, MDL);
  107. if (status != ISC_R_SUCCESS)
  108. return status;
  109. g = (omapi_object_t *)0;
  110. status = omapi_generic_new (&g, MDL);
  111. if (status != ISC_R_SUCCESS) {
  112. dfree (m, MDL);
  113. return status;
  114. }
  115. status = omapi_object_reference (&m -> inner, g, MDL);
  116. if (status != ISC_R_SUCCESS) {
  117. omapi_object_dereference ((omapi_object_t **)&m, MDL);
  118. omapi_object_dereference (&g, MDL);
  119. return status;
  120. }
  121. status = omapi_object_reference (&g -> outer,
  122. (omapi_object_t *)m, MDL);
  123. if (status != ISC_R_SUCCESS) {
  124. omapi_object_dereference ((omapi_object_t **)&m, MDL);
  125. omapi_object_dereference (&g, MDL);
  126. return status;
  127. }
  128. status = omapi_typed_data_new (MDL, &m -> rtype,
  129. omapi_datatype_string,
  130. object_type);
  131. if (status != ISC_R_SUCCESS) {
  132. omapi_object_dereference ((omapi_object_t **)&m, MDL);
  133. omapi_object_dereference (&g, MDL);
  134. return status;
  135. }
  136. status = omapi_object_reference (h, (omapi_object_t *)m, MDL);
  137. omapi_object_dereference ((omapi_object_t **)&m, MDL);
  138. omapi_object_dereference (&g, MDL);
  139. if (status != ISC_R_SUCCESS)
  140. return status;
  141. return status;
  142. }
  143. /* asynchronous - just queues the request
  144. returns nonzero status code if open couldn't be queued
  145. returns zero if open was queued
  146. h is a handle to an object created by dhcpctl_new_object
  147. connection is a connection to a DHCP server
  148. flags include:
  149. DHCPCTL_CREATE - if the object doesn't exist, create it
  150. DHCPCTL_UPDATE - update the object on the server using the
  151. attached parameters
  152. DHCPCTL_EXCL - error if the object exists and DHCPCTL_CREATE
  153. was also specified */
  154. dhcpctl_status dhcpctl_open_object (dhcpctl_handle h,
  155. dhcpctl_handle connection,
  156. int flags)
  157. {
  158. isc_result_t status;
  159. omapi_object_t *message = (omapi_object_t *)0;
  160. dhcpctl_remote_object_t *remote;
  161. if (h -> type != dhcpctl_remote_type)
  162. return ISC_R_INVALIDARG;
  163. remote = (dhcpctl_remote_object_t *)h;
  164. status = omapi_message_new (&message, MDL);
  165. if (status != ISC_R_SUCCESS)
  166. return status;
  167. status = omapi_set_int_value (message, (omapi_object_t *)0,
  168. "op", OMAPI_OP_OPEN);
  169. if (status != ISC_R_SUCCESS) {
  170. omapi_object_dereference (&message, MDL);
  171. return status;
  172. }
  173. status = omapi_set_object_value (message, (omapi_object_t *)0,
  174. "object", h);
  175. if (status != ISC_R_SUCCESS) {
  176. omapi_object_dereference (&message, MDL);
  177. return status;
  178. }
  179. if (flags & DHCPCTL_CREATE) {
  180. status = omapi_set_boolean_value (message, (omapi_object_t *)0,
  181. "create", 1);
  182. if (status != ISC_R_SUCCESS) {
  183. omapi_object_dereference (&message, MDL);
  184. return status;
  185. }
  186. }
  187. if (flags & DHCPCTL_UPDATE) {
  188. status = omapi_set_boolean_value (message, (omapi_object_t *)0,
  189. "update", 1);
  190. if (status != ISC_R_SUCCESS) {
  191. omapi_object_dereference (&message, MDL);
  192. return status;
  193. }
  194. }
  195. if (flags & DHCPCTL_EXCL) {
  196. status = omapi_set_boolean_value (message, (omapi_object_t *)0,
  197. "exclusive", 1);
  198. if (status != ISC_R_SUCCESS) {
  199. omapi_object_dereference (&message, MDL);
  200. return status;
  201. }
  202. }
  203. if (remote -> rtype) {
  204. status = omapi_set_value_str (message, (omapi_object_t *)0,
  205. "type", remote -> rtype);
  206. if (status != ISC_R_SUCCESS) {
  207. omapi_object_dereference (&message, MDL);
  208. return status;
  209. }
  210. }
  211. status = omapi_message_register (message);
  212. if (status != ISC_R_SUCCESS) {
  213. omapi_object_dereference (&message, MDL);
  214. return status;
  215. }
  216. status = omapi_protocol_send_message (connection -> outer,
  217. (omapi_object_t *)0,
  218. message, (omapi_object_t *)0);
  219. if (status != ISC_R_SUCCESS)
  220. omapi_message_unregister (message);
  221. omapi_object_dereference (&message, MDL);
  222. return status;
  223. }
  224. /* Callback methods (not meant to be called directly) */
  225. isc_result_t dhcpctl_remote_set_value (omapi_object_t *h,
  226. omapi_object_t *id,
  227. omapi_data_string_t *name,
  228. omapi_typed_data_t *value)
  229. {
  230. dhcpctl_remote_object_t *ro;
  231. unsigned long rh;
  232. isc_result_t status;
  233. if (h -> type != dhcpctl_remote_type)
  234. return ISC_R_INVALIDARG;
  235. ro = (dhcpctl_remote_object_t *)h;
  236. if (!omapi_ds_strcmp (name, "remote-handle")) {
  237. status = omapi_get_int_value (&rh, value);
  238. if (status == ISC_R_SUCCESS)
  239. ro -> remote_handle = rh;
  240. return status;
  241. }
  242. if (h -> inner && h -> inner -> type -> set_value)
  243. return (*(h -> inner -> type -> set_value))
  244. (h -> inner, id, name, value);
  245. return ISC_R_NOTFOUND;
  246. }
  247. isc_result_t dhcpctl_remote_get_value (omapi_object_t *h,
  248. omapi_object_t *id,
  249. omapi_data_string_t *name,
  250. omapi_value_t **value)
  251. {
  252. if (h -> type != dhcpctl_remote_type)
  253. return ISC_R_INVALIDARG;
  254. if (h -> inner && h -> inner -> type -> get_value)
  255. return (*(h -> inner -> type -> get_value))
  256. (h -> inner, id, name, value);
  257. return ISC_R_NOTFOUND;
  258. }
  259. isc_result_t dhcpctl_remote_signal_handler (omapi_object_t *o,
  260. const char *name, va_list ap)
  261. {
  262. dhcpctl_remote_object_t *p;
  263. omapi_typed_data_t *tv;
  264. if (o -> type != dhcpctl_remote_type)
  265. return ISC_R_INVALIDARG;
  266. p = (dhcpctl_remote_object_t *)o;
  267. if (!strcmp (name, "updated")) {
  268. p -> waitstatus = ISC_R_SUCCESS;
  269. if (o -> inner -> type == omapi_type_generic)
  270. omapi_generic_clear_flags (o -> inner);
  271. return omapi_signal_in (o -> inner, "ready");
  272. }
  273. if (!strcmp (name, "status")) {
  274. p -> waitstatus = va_arg (ap, isc_result_t);
  275. if (p -> message)
  276. omapi_typed_data_dereference (&p -> message, MDL);
  277. tv = va_arg (ap, omapi_typed_data_t *);
  278. if (tv)
  279. omapi_typed_data_reference (&p -> message, tv, MDL);
  280. return omapi_signal_in (o -> inner, "ready");
  281. }
  282. if (p -> inner && p -> inner -> type -> signal_handler)
  283. return (*(p -> inner -> type -> signal_handler))
  284. (p -> inner, name, ap);
  285. return ISC_R_SUCCESS;
  286. }
  287. isc_result_t dhcpctl_remote_destroy (omapi_object_t *h,
  288. const char *file, int line)
  289. {
  290. dhcpctl_remote_object_t *p;
  291. if (h -> type != dhcpctl_remote_type)
  292. return ISC_R_INVALIDARG;
  293. p = (dhcpctl_remote_object_t *)h;
  294. if (p -> handle)
  295. omapi_object_dereference ((omapi_object_t **)&p -> handle,
  296. file, line);
  297. if (p -> rtype)
  298. omapi_typed_data_dereference ((omapi_typed_data_t **)&p->rtype,
  299. file, line);
  300. return ISC_R_SUCCESS;
  301. }
  302. /* Write all the published values associated with the object through the
  303. specified connection. */
  304. isc_result_t dhcpctl_remote_stuff_values (omapi_object_t *c,
  305. omapi_object_t *id,
  306. omapi_object_t *p)
  307. {
  308. if (p -> type != dhcpctl_remote_type)
  309. return ISC_R_INVALIDARG;
  310. if (p -> inner && p -> inner -> type -> stuff_values)
  311. return (*(p -> inner -> type -> stuff_values)) (c, id,
  312. p -> inner);
  313. return ISC_R_SUCCESS;
  314. }