]>
Commit | Line | Data |
---|---|---|
4bce547e | 1 | /* omapictl.c |
d142e03f | 2 | |
4bce547e | 3 | Examine and modify omapi objects. */ |
d142e03f TL |
4 | |
5 | /* | |
4bce547e | 6 | * Copyright (c) 2001 Internet Software Consortium. |
49733f31 | 7 | * All rights reserved. |
d142e03f | 8 | * |
49733f31 TL |
9 | * Redistribution and use in source and binary forms, with or without |
10 | * modification, are permitted provided that the following conditions | |
11 | * are met: | |
d142e03f | 12 | * |
49733f31 TL |
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. | |
d142e03f | 21 | * |
49733f31 TL |
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''. | |
d142e03f TL |
42 | */ |
43 | ||
6a4c4be8 | 44 | #include <time.h> |
0f6bb7e1 | 45 | #include <sys/time.h> |
6a4c4be8 TL |
46 | #include <stdio.h> |
47 | #include <stdlib.h> | |
48 | #include <stdarg.h> | |
06f0ed06 | 49 | #include <string.h> |
6a4c4be8 | 50 | #include <isc/result.h> |
d142e03f | 51 | #include "dhcpctl.h" |
4bce547e | 52 | #include "dhcpd.h" |
d142e03f | 53 | |
4bce547e TL |
54 | /* Fixups */ |
55 | isc_result_t find_class (struct class **c, const char *n, const char *f, int l) | |
56 | { | |
57 | return 0; | |
58 | } | |
59 | int parse_allow_deny (struct option_cache **oc, struct parse *cfile, int flag) | |
60 | { | |
61 | return 0; | |
62 | } | |
63 | void dhcp (struct packet *packet) { } | |
64 | void bootp (struct packet *packet) { } | |
65 | int check_collection (struct packet *p, struct lease *l, struct collection *c) | |
66 | { | |
67 | return 0; | |
68 | } | |
69 | void classify (struct packet *packet, struct class *class) { } | |
d142e03f | 70 | |
58941de4 DN |
71 | static void usage (char *s) { |
72 | fprintf (stderr, | |
73 | "Usage: %s [-n <username>] [-p <password>] " | |
74 | "[-a <algorithm>]\n", s); | |
75 | exit (1); | |
76 | } | |
77 | ||
031d30b7 DN |
78 | static void check (isc_result_t status, const char *func) { |
79 | if (status != ISC_R_SUCCESS) { | |
80 | fprintf (stderr, "%s: %s\n", func, isc_result_totext (status)); | |
81 | exit (1); | |
82 | } | |
83 | } | |
84 | ||
4bce547e | 85 | int main (int argc, char **argv, char **envp) |
d142e03f TL |
86 | { |
87 | isc_result_t status, waitstatus; | |
88 | dhcpctl_handle connection; | |
58941de4 | 89 | dhcpctl_handle authenticator; |
4bce547e | 90 | dhcpctl_handle oh; |
bfacc53f | 91 | dhcpctl_data_string cid, ip_addr; |
e6e9b9e8 | 92 | dhcpctl_data_string result, groupname, identifier; |
58941de4 | 93 | const char *name = 0, *pass = 0, *algorithm = "hmac-md5"; |
031d30b7 | 94 | int i, j; |
4bce547e TL |
95 | int port = 7911; |
96 | const char *server = "127.0.0.1"; | |
97 | struct parse *cfile; | |
98 | enum dhcp_token token; | |
99 | const char *val; | |
031d30b7 DN |
100 | char buf[1024]; |
101 | char s1[1024]; | |
d142e03f | 102 | |
58941de4 DN |
103 | for (i = 1; i < argc; i++) { |
104 | if (!strcmp (argv[i], "-n")) { | |
105 | if (++i == argc) | |
106 | usage(argv[0]); | |
107 | name = argv[i]; | |
108 | } else if (!strcmp (argv[i], "-p")) { | |
109 | if (++i == argc) | |
110 | usage(argv[0]); | |
111 | pass = argv[i]; | |
112 | } else if (!strcmp (argv[i], "-a")) { | |
113 | if (++i == argc) | |
114 | usage(argv[0]); | |
115 | algorithm = argv[i]; | |
4bce547e TL |
116 | } else if (!strcmp (argv[i], "-s")) { |
117 | if (++i == argc) | |
118 | usage(argv[0]); | |
119 | server = argv[i]; | |
120 | } else if (!strcmp (argv[i], "-P")) { | |
121 | if (++i == argc) | |
122 | usage(argv[0]); | |
123 | port = atoi (argv[i]); | |
58941de4 DN |
124 | } else { |
125 | usage(argv[0]); | |
126 | } | |
127 | } | |
128 | ||
129 | if ((name || pass) && !(name && pass)) | |
130 | usage(argv[0]); | |
131 | ||
d142e03f TL |
132 | status = dhcpctl_initialize (); |
133 | if (status != ISC_R_SUCCESS) { | |
134 | fprintf (stderr, "dhcpctl_initialize: %s\n", | |
135 | isc_result_totext (status)); | |
136 | exit (1); | |
137 | } | |
138 | ||
58941de4 DN |
139 | authenticator = dhcpctl_null_handle; |
140 | ||
141 | if (name) { | |
142 | status = dhcpctl_new_authenticator (&authenticator, | |
143 | name, algorithm, pass, | |
06f0ed06 | 144 | strlen (pass) + 1); |
58941de4 DN |
145 | if (status != ISC_R_SUCCESS) { |
146 | fprintf (stderr, "Cannot create authenticator: %s\n", | |
147 | isc_result_totext (status)); | |
148 | exit (1); | |
149 | } | |
150 | } | |
151 | ||
d142e03f | 152 | memset (&connection, 0, sizeof connection); |
4bce547e | 153 | status = dhcpctl_connect (&connection, server, port, authenticator); |
d142e03f TL |
154 | if (status != ISC_R_SUCCESS) { |
155 | fprintf (stderr, "dhcpctl_connect: %s\n", | |
156 | isc_result_totext (status)); | |
157 | exit (1); | |
158 | } | |
159 | ||
4bce547e TL |
160 | memset (&oh, 0, sizeof oh); |
161 | ||
4bce547e | 162 | do { |
031d30b7 DN |
163 | printf ("obj: "); |
164 | if (oh == NULL) { | |
165 | printf ("<null>\n"); | |
166 | } else { | |
167 | dhcpctl_remote_object_t *r = | |
168 | (dhcpctl_remote_object_t *)oh; | |
169 | omapi_generic_object_t *g = | |
170 | (omapi_generic_object_t *)(r -> inner); | |
171 | ||
172 | if (r -> rtype -> type != omapi_datatype_string) { | |
173 | printf ("?\n"); | |
174 | } else { | |
175 | printf ("%.*s\n", | |
7c6a9da0 | 176 | (int)(r -> rtype -> u . buffer . len), |
031d30b7 DN |
177 | r -> rtype -> u . buffer . value); |
178 | } | |
179 | ||
180 | for (i = 0; i < g -> nvalues; i++) { | |
181 | omapi_value_t *v = g -> values [i]; | |
182 | ||
7c6a9da0 TL |
183 | printf ("%.*s = ", (int)v -> name -> len, |
184 | v -> name -> value); | |
031d30b7 DN |
185 | |
186 | switch (v -> value -> type) { | |
187 | case omapi_datatype_int: | |
188 | printf ("%d\n", | |
189 | v -> value -> u . integer); | |
190 | break; | |
191 | ||
192 | case omapi_datatype_string: | |
193 | printf ("\"%.*s\"\n", | |
7c6a9da0 | 194 | (int)v -> value -> u.buffer.len, |
031d30b7 DN |
195 | v -> value -> u.buffer.value); |
196 | break; | |
197 | ||
198 | case omapi_datatype_data: | |
199 | printf ("%s\n", | |
200 | print_hex_1 | |
201 | (v -> value -> u.buffer.len, | |
202 | v -> value -> u.buffer.value, | |
203 | 60)); | |
204 | break; | |
205 | ||
206 | case omapi_datatype_object: | |
207 | printf ("<obj>\n"); | |
208 | break; | |
209 | } | |
210 | } | |
211 | } | |
212 | ||
213 | fputs ("> ", stdout); | |
214 | fflush (stdout); | |
215 | if (fgets (buf, sizeof(buf), stdin) == NULL) | |
216 | break; | |
217 | ||
218 | status = new_parse (&cfile, 0, buf, strlen(buf), "<STDIN>"); | |
219 | check(status, "new_parse()"); | |
220 | ||
4bce547e TL |
221 | token = next_token (&val, cfile); |
222 | switch (token) { | |
223 | default: | |
224 | parse_warn (cfile, "unknown token: %s", val); | |
031d30b7 DN |
225 | break; |
226 | ||
227 | case EOF: | |
228 | break; | |
229 | ||
230 | case TOKEN_HELP: | |
231 | case '?': | |
232 | printf ("Commands:\n"); | |
233 | printf (" new <object-type>\n"); | |
234 | printf (" set <name> = <value>\n"); | |
235 | printf (" create\n"); | |
236 | printf (" open\n"); | |
4bce547e TL |
237 | break; |
238 | ||
239 | case TOKEN_NEW: | |
031d30b7 DN |
240 | token = next_token (&val, cfile); |
241 | if ((!is_identifier (token) && token != STRING) || | |
242 | next_token (NULL, cfile) != EOF) | |
243 | { | |
244 | printf ("usage: new <object-type>\n"); | |
4bce547e TL |
245 | break; |
246 | } | |
031d30b7 DN |
247 | |
248 | if (oh) { | |
249 | printf ("an object is already open.\n"); | |
4bce547e TL |
250 | break; |
251 | } | |
031d30b7 | 252 | |
4bce547e TL |
253 | status = dhcpctl_new_object (&oh, connection, val); |
254 | if (status != ISC_R_SUCCESS) { | |
031d30b7 DN |
255 | printf ("can't create object: %s\n", |
256 | isc_result_totext (status)); | |
257 | break; | |
4bce547e | 258 | } |
d142e03f | 259 | |
031d30b7 | 260 | break; |
bfacc53f | 261 | |
031d30b7 DN |
262 | case TOKEN_CLOSE: |
263 | if (next_token (NULL, cfile) != EOF) { | |
264 | printf ("usage: close\n"); | |
265 | } | |
d142e03f | 266 | |
031d30b7 | 267 | omapi_object_dereference (&oh, MDL); |
d142e03f | 268 | |
031d30b7 | 269 | break; |
d142e03f | 270 | |
031d30b7 DN |
271 | case TOKEN_SET: |
272 | token = next_token (&val, cfile); | |
d142e03f | 273 | |
031d30b7 DN |
274 | if ((!is_identifier (token) && token != STRING) || |
275 | next_token (NULL, cfile) != '=') | |
276 | { | |
277 | printf ("usage: set <name> = <value>\n"); | |
278 | break; | |
279 | } | |
032aca19 | 280 | |
031d30b7 DN |
281 | if (oh == NULL) { |
282 | printf ("no open object.\n"); | |
283 | break; | |
284 | } | |
032aca19 | 285 | |
031d30b7 DN |
286 | s1[0] = '\0'; |
287 | strncat (s1, val, sizeof(s1)-1); | |
032aca19 | 288 | |
031d30b7 DN |
289 | token = next_token (&val, cfile); |
290 | switch (token) { | |
291 | case STRING: | |
292 | dhcpctl_set_string_value (oh, val, s1); | |
293 | break; | |
472c048a | 294 | |
031d30b7 DN |
295 | case NUMBER: |
296 | dhcpctl_set_int_value (oh, atoi (val), s1); | |
297 | break; | |
472c048a | 298 | |
031d30b7 DN |
299 | default: |
300 | printf ("invalid value.\n"); | |
301 | } | |
472c048a | 302 | |
031d30b7 | 303 | break; |
e6e9b9e8 | 304 | |
031d30b7 DN |
305 | case TOKEN_CREATE: |
306 | case TOKEN_OPEN: | |
307 | if (next_token (NULL, cfile) != EOF) { | |
308 | printf ("usage: %s\n", val); | |
309 | } | |
eadee396 | 310 | |
031d30b7 DN |
311 | i = 0; |
312 | if (token == TOKEN_CREATE) | |
313 | i = DHCPCTL_CREATE | DHCPCTL_EXCL; | |
e6e9b9e8 | 314 | |
031d30b7 DN |
315 | status = dhcpctl_open_object (oh, connection, i); |
316 | if (status == ISC_R_SUCCESS) | |
317 | status = dhcpctl_wait_for_completion | |
318 | (oh, &waitstatus); | |
319 | if (status == ISC_R_SUCCESS) | |
320 | status = waitstatus; | |
321 | if (status != ISC_R_SUCCESS) { | |
322 | printf ("can't open object: %s\n", | |
323 | isc_result_totext (status)); | |
324 | break; | |
325 | } | |
eadee396 | 326 | |
031d30b7 | 327 | break; |
e6e9b9e8 | 328 | |
031d30b7 DN |
329 | case UPDATE: |
330 | if (next_token (NULL, cfile) != EOF) { | |
331 | printf ("usage: %s\n", val); | |
332 | } | |
e6e9b9e8 | 333 | |
031d30b7 DN |
334 | status = dhcpctl_object_update(connection, oh); |
335 | if (status == ISC_R_SUCCESS) | |
336 | status = dhcpctl_wait_for_completion | |
337 | (oh, &waitstatus); | |
338 | if (status == ISC_R_SUCCESS) | |
339 | status = waitstatus; | |
340 | if (status != ISC_R_SUCCESS) { | |
341 | printf ("can't update object: %s\n", | |
342 | isc_result_totext (status)); | |
343 | break; | |
344 | } | |
e6e9b9e8 | 345 | |
031d30b7 DN |
346 | break; |
347 | } | |
348 | } while (1); | |
eadee396 | 349 | |
d142e03f TL |
350 | exit (0); |
351 | } |