aboutsummaryrefslogtreecommitdiff
path: root/dhcpctl/omshell.c
blob: bb489d8456dc5cd65ca4ccb9222d534ed09efc95 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
/* omshell.c

   Examine and modify omapi objects. */

/*
 * Copyright (c) 2009-2011 by Internet Systems Consortium, Inc. ("ISC")
 * Copyright (c) 2004-2007 by Internet Systems Consortium, Inc. ("ISC")
 * Copyright (c) 2001-2003 by Internet Software Consortium
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
 * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 *
 *   Internet Systems Consortium, Inc.
 *   950 Charter Street
 *   Redwood City, CA 94063
 *   <info@isc.org>
 *   https://www.isc.org/
 *
 * This software has been written for Internet Systems Consortium
 * by Ted Lemon in cooperation with Vixie Enterprises and Nominum, Inc.
 * To learn more about Internet Systems Consortium, see
 * ``https://www.isc.org/''.  To learn more about Vixie Enterprises,
 * see ``http://www.vix.com''.   To learn more about Nominum, Inc., see
 * ``http://www.nominum.com''.
 */

#include "config.h"

#include <time.h>
#include <sys/time.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
//#include "result.h"
#include <syslog.h>
#include "dhcpctl.h"
#include "dhcpd.h"

/* Fixups */
isc_result_t find_class (struct class **c, const char *n, const char *f, int l)
{
	return 0;
}
int parse_allow_deny (struct option_cache **oc, struct parse *cfile, int flag)
{
	return 0;
}
void dhcp (struct packet *packet) { }
void bootp (struct packet *packet) { }

#ifdef DHCPv6
/* XXX: should we warn or something here? */
void dhcpv6(struct packet *packet) { }
#endif /* DHCPv6 */

int check_collection (struct packet *p, struct lease *l, struct collection *c)
{
	return 0;
}
void classify (struct packet *packet, struct class *class) { }

static void usage (char *s) {
	fprintf (stderr, "Usage: %s\n", s);
	exit (1);
}

static void check (isc_result_t status, const char *func) {
	if (status != ISC_R_SUCCESS) {
		fprintf (stderr, "%s: %s\n", func, isc_result_totext (status));
		exit (1);
	}
}

int 
main(int argc, char **argv) {
	isc_result_t status, waitstatus;
	dhcpctl_handle connection;
	dhcpctl_handle authenticator;
	dhcpctl_handle oh;
	struct data_string secret;
	const char *name = 0, *algorithm = "hmac-md5";
	int i;
	int port = 7911;
	const char *server = "127.0.0.1";
	struct parse *cfile;
	enum dhcp_token token;
	const char *val;
	char *s;
	char buf[1024];
	char s1[1024];
	int connected = 0;
	char hex_buf[1025];

	for (i = 1; i < argc; i++) {
		usage(argv[0]);
	}

	/* Initially, log errors to stderr as well as to syslogd. */
	openlog ("omshell", LOG_NDELAY, DHCPD_LOG_FACILITY);
	status = dhcpctl_initialize ();
	if (status != ISC_R_SUCCESS) {
		fprintf (stderr, "dhcpctl_initialize: %s\n",
			 isc_result_totext (status));
		exit (1);
	}

	memset (&oh, 0, sizeof oh);

	do {
	    if (!connected) {
	    } else if (oh == NULL) {
		printf ("obj: <null>\n");
	    } else {
		dhcpctl_remote_object_t *r = (dhcpctl_remote_object_t *)oh;
		omapi_generic_object_t *g =
			(omapi_generic_object_t *)(r -> inner);
		
		printf ("obj: ");

		if (r -> rtype -> type != omapi_datatype_string) {
			printf ("?\n");
		} else {
			printf ("%.*s\n",
				(int)(r -> rtype -> u . buffer . len),
				r -> rtype -> u . buffer . value);
		}
		
		for (i = 0; i < g -> nvalues; i++) {
		    omapi_value_t *v = g -> values [i];
			
		    if (!g -> values [i])
			    continue;

		    printf ("%.*s = ", (int)v -> name -> len,
			    v -> name -> value);
			
		    if (!v -> value) {
			printf ("<null>\n");
			continue;
		    }
		    switch (v -> value -> type) {
			  case omapi_datatype_int:
			    printf ("%d\n",
				    v -> value -> u . integer);
			    break;
			 
			  case omapi_datatype_string:
			    printf ("\"%.*s\"\n",
				    (int) v -> value -> u.buffer.len,
				    v -> value -> u.buffer.value);
			    break;
				
			  case omapi_datatype_data:
			    print_hex_or_string(v->value->u.buffer.len,
						v->value->u.buffer.value,
						sizeof(hex_buf), hex_buf);
			    printf("%s\n", hex_buf);
			    break;
			    
			  case omapi_datatype_object:
			    printf ("<obj>\n");
			    break;
		    }
		}
	    }

	    fputs ("> ", stdout);
	    fflush (stdout);
	    if (fgets (buf, sizeof(buf), stdin) == NULL)
		break;

	    status = new_parse (&cfile, -1, buf, strlen(buf), "<STDIN>", 1);
	    check(status, "new_parse()");
	    
	    token = next_token (&val, (unsigned *)0, cfile);
	    switch (token) {
		  default:
		    parse_warn (cfile, "unknown token: %s", val);
		    skip_to_semi (cfile);
		    break;
		    
		  case END_OF_FILE:
		  case ENDOFLINE: /* EOL: */
		    break;
		    
		  case TOKEN_HELP:
	          case QUESTIONMARK: /* '?': */
		    printf ("Commands:\n");
		    printf ("  port <server omapi port>\n");
		    printf ("  server <server address>\n");
		    printf ("  key <key name> <key value>\n");
		    printf ("  connect\n");
		    printf ("  new <object-type>\n");
		    printf ("  set <name> = <value>\n");
		    printf ("  create\n");
		    printf ("  open\n");
		    printf ("  update\n");
		    printf ("  unset <name>\n");
		    printf ("  refresh\n");
		    printf ("  remove\n");
		    skip_to_semi (cfile);
		    break;
		    
		  case PORT:
		    token = next_token (&val, (unsigned *)0, cfile);
		    if (is_identifier (token)) {
			    struct servent *se;
			    se = getservbyname (val, "tcp");
			    if (se)
				    port = ntohs (se -> s_port);
			    else {
				    printf ("unknown service name: %s\n", val);
				    break;
			    }
		    } else if (token == NUMBER) {
			    port = atoi (val);
		    } else {
			    skip_to_semi (cfile);
			    printf ("usage: port <port>\n");
			    break;
		    }
		    token = next_token (&val, (unsigned *)0, cfile);
		    if (token != END_OF_FILE && token != EOL) {
			    printf ("usage: port <server>\n");
			    skip_to_semi (cfile);
			    break;
		    }
		    break;

		  case TOKEN_SERVER:
		    token = next_token (&val, (unsigned *)0, cfile);
		    if (token == NUMBER) {
			    int alen = (sizeof buf) - 1;
			    int len;

			    s = &buf [0];
			    len = strlen (val);
			    if (len + 1 > alen) {
			      baddq:
				printf ("usage: server <server>\n");
				skip_to_semi (cfile);
				break;
			    }			    strcpy (buf, val);
			    s += len;
			    token = next_token (&val, (unsigned *)0, cfile);
			    if (token != DOT)
				    goto baddq;
			    *s++ = '.';
			    token = next_token (&val, (unsigned *)0, cfile);
			    if (token != NUMBER)
				    goto baddq;
			    len = strlen (val);
			    if (len + 1 > alen)
				    goto baddq;
			    strcpy (s, val);
			    s += len;
			    token = next_token (&val, (unsigned *)0, cfile);
			    if (token != DOT)
				    goto baddq;
			    *s++ = '.';
			    token = next_token (&val, (unsigned *)0, cfile);
			    if (token != NUMBER)
				    goto baddq;
			    len = strlen (val);
			    if (len + 1 > alen)
				    goto baddq;
			    strcpy (s, val);
			    s += len;
			    token = next_token (&val, (unsigned *)0, cfile);
			    if (token != DOT)
				    goto baddq;
			    *s++ = '.';
			    token = next_token (&val, (unsigned *)0, cfile);
			    if (token != NUMBER)
				    goto baddq;
			    len = strlen (val);
			    if (len + 1 > alen)
				    goto baddq;
			    strcpy (s, val);
			    val = &buf [0];
		    } else if (is_identifier (token)) {
			    /* Use val directly. */
		    } else {
			    printf ("usage: server <server>\n");
			    skip_to_semi (cfile);
			    break;
		    }

		    s = dmalloc (strlen (val) + 1, MDL);
		    if (!server) {
			    printf ("no memory to store server name.\n");
			    skip_to_semi (cfile);
			    break;
		    }
		    strcpy (s, val);
		    server = s;

		    token = next_token (&val, (unsigned *)0, cfile);
		    if (token != END_OF_FILE && token != EOL) {
			    printf ("usage: server <server>\n");
			    skip_to_semi (cfile);
			    break;
		    }
		    break;

		  case KEY:
		    token = peek_token(&val, (unsigned *)0, cfile);
		    if (token == STRING) {
			    token = next_token (&val, (unsigned *)0, cfile);
			    if (!is_identifier (token)) {
				    printf ("usage: key <name> <value>\n");
				    skip_to_semi (cfile);
				    break;
			    }
			    s = dmalloc (strlen (val) + 1, MDL);
			    if (!s) {
				    printf ("no memory for key name.\n");
				    skip_to_semi (cfile);
				    break;
			    }
			    strcpy (s, val);
		    } else {
			    s = parse_host_name(cfile);
			    if (s == NULL) {
				    printf ("usage: key <name> <value>\n");
				    skip_to_semi(cfile);
				    break;
			    }
		    }
		    name = s;

		    memset (&secret, 0, sizeof secret);
		    if (!parse_base64 (&secret, cfile)) {
			    skip_to_semi (cfile);
			    break;
		    }
		    token = next_token (&val, (unsigned *)0, cfile);
		    if (token != END_OF_FILE && token != EOL) {
			    printf ("usage: key <name> <secret>\n");
			    skip_to_semi (cfile);
			    break;
		    }
		    break;

		  case CONNECT:
		    token = next_token (&val, (unsigned *)0, cfile);
		    if (token != END_OF_FILE && token != EOL) {
			    printf ("usage: connect\n");
			    skip_to_semi (cfile);
			    break;
		    }

		    authenticator = dhcpctl_null_handle;

		    if (name) {
			status = dhcpctl_new_authenticator (&authenticator,
							    name, algorithm,
							    secret.data,
							    secret.len);

			if (status != ISC_R_SUCCESS) {
			    fprintf (stderr,
				     "Cannot create authenticator: %s\n",
				     isc_result_totext (status));
			    break;
			}
		    }

		    memset (&connection, 0, sizeof connection);
		    status = dhcpctl_connect (&connection,
					      server, port, authenticator);
		    if (status != ISC_R_SUCCESS) {
			    fprintf (stderr, "dhcpctl_connect: %s\n",
				     isc_result_totext (status));
			    break;
		    }
		    connected = 1;
		    break;

		  case TOKEN_NEW:
		    token = next_token (&val, (unsigned *)0, cfile);
		    if ((!is_identifier (token) && token != STRING)) {
			    printf ("usage: new <object-type>\n");
			    break;
		    }
		    
		    if (oh) {
			    printf ("an object is already open.\n");
			    skip_to_semi (cfile);
			    break;
		    }
		    
		    if (!connected) {
			    printf ("not connected.\n");
			    skip_to_semi (cfile);
			    break;
		    }

		    status = dhcpctl_new_object (&oh, connection, val);
		    if (status != ISC_R_SUCCESS) {
			    printf ("can't create object: %s\n",
				    isc_result_totext (status));
			    break;
		    }
		    
		    token = next_token (&val, (unsigned *)0, cfile);
		    if (token != END_OF_FILE && token != EOL) {
			    printf ("usage: new <object-type>\n");
			    skip_to_semi (cfile);
			    break;
		    }
		    break;

		  case TOKEN_CLOSE:
		    token = next_token (&val, (unsigned *)0, cfile);
		    if (token != END_OF_FILE && token != EOL) {
			    printf ("usage: close\n");
			    skip_to_semi (cfile);
			    break;
		    }

		    if (!connected) {
			    printf ("not connected.\n");
			    skip_to_semi (cfile);
			    break;
		    }

		    if (!oh) {
			    printf ("not open.\n");
			    skip_to_semi (cfile);
			    break;
		    }
		    omapi_object_dereference (&oh, MDL);
		    
		    break;

		  case TOKEN_SET:
		    token = next_token (&val, (unsigned *)0, cfile);

		    if ((!is_identifier (token) && token != STRING)) {
			  set_usage:
			    printf ("usage: set <name> = <value>\n");
			    skip_to_semi (cfile);
			    break;
		    }
		    
		    if (oh == NULL) {
			    printf ("no open object.\n");
			    skip_to_semi (cfile);
			    break;
		    }
		    
		    if (!connected) {
			    printf ("not connected.\n");
			    skip_to_semi (cfile);
			    break;
		    }

		    s1[0] = '\0';
		    strncat (s1, val, sizeof(s1)-1);
		    
		    token = next_token (&val, (unsigned *)0, cfile);
		    if (token != EQUAL)
			    goto set_usage;

		    token = next_token (&val, (unsigned *)0, cfile);
		    switch (token) {
			  case STRING:
			    dhcpctl_set_string_value (oh, val, s1);
			    token = next_token (&val, (unsigned *)0, cfile);
			    break;
			    
			  case NUMBER:
			    strcpy (buf, val);
			    token = peek_token (&val, (unsigned *)0, cfile);
			    /* Colon-separated hex list? */
			    if (token == COLON)
				goto cshl;
			    else if (token == DOT) {
				s = buf;
				val = buf;
				do {
				    int intval = atoi (val);
				    if (intval > 255) {
					parse_warn (cfile,
						    "dotted octet > 255: %s",
						    val);
					skip_to_semi (cfile);
					goto badnum;
				    }
				    *s++ = intval;
				    token = next_token (&val,
							(unsigned *)0, cfile);
				    if (token != DOT)
					    break;
				    /* DOT is zero. */
				    while ((token = next_token (&val,
					(unsigned *)0, cfile)) == DOT)
					*s++ = 0;
				} while (token == NUMBER);
				dhcpctl_set_data_value (oh, buf,
							(unsigned)(s - buf),
							s1);
				break;
			    }
			    dhcpctl_set_int_value (oh, atoi (buf), s1);
			    token = next_token (&val, (unsigned *)0, cfile);
			  badnum:
			    break;
			    
			  case NUMBER_OR_NAME:
			    strcpy (buf, val);
			  cshl:
			    s = buf;
			    val = buf;
			    do {
				convert_num (cfile, (unsigned char *)s,
					     val, 16, 8);
				++s;
				token = next_token (&val,
						    (unsigned *)0, cfile);
				if (token != COLON)
				    break;
				token = next_token (&val,
						    (unsigned *)0, cfile);
			    } while (token == NUMBER ||
				     token == NUMBER_OR_NAME);
			    dhcpctl_set_data_value (oh, buf,
						    (unsigned)(s - buf), s1);
			    break;

			  default:
			    printf ("invalid value.\n");
			    skip_to_semi (cfile);
		    }
		    
		    if (token != END_OF_FILE && token != EOL)
			    goto set_usage;
		    break;
		    
		  case UNSET:
		    token = next_token (&val, (unsigned *)0, cfile);

		    if ((!is_identifier (token) && token != STRING)) {
			  unset_usage:
			    printf ("usage: unset <name>\n");
			    skip_to_semi (cfile);
			    break;
		    }
		    
		    if (!oh) {
			    printf ("no open object.\n");
			    skip_to_semi (cfile);
			    break;
		    }
		    
		    if (!connected) {
			    printf ("not connected.\n");
			    skip_to_semi (cfile);
			    break;
		    }

		    s1[0] = '\0';
		    strncat (s1, val, sizeof(s1)-1);
		    
		    token = next_token (&val, (unsigned *)0, cfile);
		    if (token != END_OF_FILE && token != EOL)
			    goto unset_usage;

		    dhcpctl_set_null_value (oh, s1);
		    break;

			    
		  case TOKEN_CREATE:
		  case TOKEN_OPEN:
		    i = token;
		    token = next_token (&val, (unsigned *)0, cfile);
		    if (token != END_OF_FILE && token != EOL) {
			    printf ("usage: %s\n", val);
			    skip_to_semi (cfile);
			    break;
		    }
		    
		    if (!connected) {
			    printf ("not connected.\n");
			    skip_to_semi (cfile);
			    break;
		    }

		    if (!oh) {
			    printf ("you must make a new object first!\n");
			    skip_to_semi (cfile);
			    break;
		    }

		    if (i == TOKEN_CREATE)
			    i = DHCPCTL_CREATE | DHCPCTL_EXCL;
		    else
			    i = 0;
		    
		    status = dhcpctl_open_object (oh, connection, i);
		    if (status == ISC_R_SUCCESS)
			    status = dhcpctl_wait_for_completion
				    (oh, &waitstatus);
		    if (status == ISC_R_SUCCESS)
			    status = waitstatus;
		    if (status != ISC_R_SUCCESS) {
			    printf ("can't open object: %s\n",
				    isc_result_totext (status));
			    break;
		    }
		    
		    break;

		  case UPDATE:
		    token = next_token (&val, (unsigned *)0, cfile);
		    if (token != END_OF_FILE && token != EOL) {
			    printf ("usage: %s\n", val);
			    skip_to_semi (cfile);
			    break;
		    }
		    
		    if (!connected) {
			    printf ("not connected.\n");
			    skip_to_semi (cfile);
			    break;
		    }

		    if (!oh) {
			    printf ("you haven't opened an object yet!\n");
			    skip_to_semi (cfile);
			    break;
		    }

		    status = dhcpctl_object_update(connection, oh);
		    if (status == ISC_R_SUCCESS)
			    status = dhcpctl_wait_for_completion
				    (oh, &waitstatus);
		    if (status == ISC_R_SUCCESS)
			    status = waitstatus;
		    if (status != ISC_R_SUCCESS) {
			    printf ("can't update object: %s\n",
				    isc_result_totext (status));
			    break;
		    }
		    
		    break;

		  case REMOVE:
		    token = next_token (&val, (unsigned *)0, cfile);
		    if (token != END_OF_FILE && token != EOL) {
			    printf ("usage: remove\n");
			    skip_to_semi (cfile);
			    break;
		    }
		    
		    if (!connected) {
			    printf ("not connected.\n");
			    break;
		    }

		    if (!oh) {
			    printf ("no object.\n");
			    break;
		    }

		    status = dhcpctl_object_remove(connection, oh);
		    if (status == ISC_R_SUCCESS)
			    status = dhcpctl_wait_for_completion
				    (oh, &waitstatus);
		    if (status == ISC_R_SUCCESS)
			    status = waitstatus;
		    if (status != ISC_R_SUCCESS) {
			    printf ("can't destroy object: %s\n",
				    isc_result_totext (status));
			    break;
		    }
		    omapi_object_dereference (&oh, MDL);
		    break;

		  case REFRESH:
		    token = next_token (&val, (unsigned *)0, cfile);
		    if (token != END_OF_FILE && token != EOL) {
			    printf ("usage: refresh\n");
			    skip_to_semi (cfile);
			    break;
		    }
		    
		    if (!connected) {
			    printf ("not connected.\n");
			    break;
		    }

		    if (!oh) {
			    printf ("no object.\n");
			    break;
		    }

		    status = dhcpctl_object_refresh(connection, oh);
		    if (status == ISC_R_SUCCESS)
			    status = dhcpctl_wait_for_completion
				    (oh, &waitstatus);
		    if (status == ISC_R_SUCCESS)
			    status = waitstatus;
		    if (status != ISC_R_SUCCESS) {
			    printf ("can't refresh object: %s\n",
				    isc_result_totext (status));
			    break;
		    }
		    
		    break;
	    }
	    end_parse (&cfile);
	} while (1);

	exit (0);
}

/* Sigh */
isc_result_t dhcp_set_control_state (control_object_state_t oldstate,
				     control_object_state_t newstate)
{
	return ISC_R_SUCCESS;
}