]> andersk Git - moira.git/blame - clients/stella/stella.c
Make this build under Win32 again; remove unused variable.
[moira.git] / clients / stella / stella.c
CommitLineData
80e8aa14 1/*
2 * Command line oriented Moira host tool.
3 *
4 * kolya@MIT.EDU, January 2000
5 *
6 * Somewhat based on blanche
7 *
8d138b83 8 * Copyright (C) 2000, 2001 by the Massachusetts Institute of Technology.
80e8aa14 9 * For copying and distribution information, please see the file
10 * <mit-copyright.h>.
11 */
12
13#include <mit-copyright.h>
14#include <moira.h>
15#include <moira_site.h>
16#include <mrclient.h>
17
18#include <ctype.h>
19#include <errno.h>
20#include <stdio.h>
21#include <stdlib.h>
22#include <string.h>
23
0aebabe7 24#ifdef _WIN32
25typedef unsigned long in_addr_t;
26#else
27#include <sys/types.h>
28#include <sys/socket.h>
29
30#include <netinet/in.h>
31#include <arpa/inet.h>
32#endif
33
80e8aa14 34RCSID("$Header$");
35
36struct owner_type {
37 int type;
38 char *name;
39};
40
41struct mqelem {
42 struct mqelem *q_forw;
43 struct mqelem *q_back;
44 void *q_data;
45};
46
47struct string_list {
48 char *string;
49 struct string_list *next;
50};
51
80e8aa14 52#define M_ANY 0
53#define M_USER 1
54#define M_LIST 2
a547d259 55#define M_KERBEROS 3
56#define M_NONE 4
80e8aa14 57
58/* argument parsing macro */
59#define argis(a, b) (!strcmp(*arg + 1, a) || !strcmp(*arg + 1, b))
60
61/* flags from command line */
62int info_flag, update_flag, create_flag, delete_flag, list_map_flag;
7e689fac 63int update_alias_flag, update_map_flag, verbose, noauth;
7b6a099a 64int list_container_flag, update_container_flag, unformatted_flag;
80e8aa14 65
66struct string_list *alias_add_queue, *alias_remove_queue;
67struct string_list *map_add_queue, *map_remove_queue;
9cd2d193 68struct string_list *container_add_queue, *container_remove_queue;
80e8aa14 69
70char *hostname, *whoami;
71
72char *newname, *address, *network, *h_status, *vendor, *model;
080e5e2c 73char *os, *location, *contact, *billing_contact, *adm_cmt, *op_cmt;
80e8aa14 74
8d138b83 75in_addr_t ipaddress;
80e8aa14 76struct owner_type *owner;
77
78void usage(char **argv);
79int store_host_info(int argc, char **argv, void *hint);
80void show_host_info(char **argv);
7b6a099a 81void show_host_info_unformatted(char **argv);
80e8aa14 82int show_machine_in_cluster(int argc, char **argv, void *hint);
9cd2d193 83int show_machine_in_container(int argc, char **argv, void *hint);
80e8aa14 84struct owner_type *parse_member(char *s);
85struct string_list *add_to_string_list(struct string_list *old_list, char *s);
86int wrap_mr_query(char *handle, int argc, char **argv,
87 int (*callback)(int, char **, void *), void *callarg);
88void print_query(char *query_name, int argc, char **argv);
89
90int main(int argc, char **argv)
91{
92 int status, success;
93 char **arg = argv;
533bacb3 94 char *server = NULL;
80e8aa14 95
96 /* clear all flags & lists */
97 info_flag = update_flag = create_flag = list_map_flag = update_map_flag = 0;
7e689fac 98 update_alias_flag = verbose = noauth = 0;
9cd2d193 99 list_container_flag = update_container_flag = 0;
80e8aa14 100 newname = address = network = h_status = vendor = model = NULL;
080e5e2c 101 os = location = contact = billing_contact = adm_cmt = op_cmt = NULL;
80e8aa14 102 owner = NULL;
103 alias_add_queue = alias_remove_queue = NULL;
104 map_add_queue = map_remove_queue = NULL;
9cd2d193 105 container_add_queue = container_remove_queue = NULL;
80e8aa14 106 whoami = argv[0];
107
108 success = 1;
109
110 /* parse args, building addlist, dellist, & synclist */
111 while (++arg - argv < argc)
112 {
113 if (**arg == '-')
114 {
115 if (argis("i", "info"))
116 info_flag++;
117 else if (argis("C", "create"))
118 create_flag++;
119 else if (argis("D", "delete"))
120 delete_flag++;
121 else if (argis("R", "rename")) {
122 if (arg - argv < argc - 1) {
123 arg++;
124 update_flag++;
125 newname = *arg;
126 } else
127 usage(argv);
128 }
129 else if (argis("A", "address")) {
130 if (arg - argv < argc - 1) {
131 arg++;
132 update_flag++;
133 address = *arg;
134 } else
135 usage(argv);
136 }
137 else if (argis("O", "owner")) {
138 if (arg - argv < argc - 1) {
139 arg++;
140 update_flag++;
141 owner = parse_member(*arg);
142 } else
143 usage(argv);
144 }
145 else if (argis("N", "network")) {
146 if (arg - argv < argc - 1) {
147 arg++;
148 update_flag++;
149 network = *arg;
150 } else
151 usage(argv);
152 }
153 else if (argis("S", "status")) {
154 if (arg - argv < argc - 1) {
155 int i;
533bacb3 156 int len;
80e8aa14 157
158 arg++;
159 update_flag++;
160 h_status = *arg;
161
533bacb3 162 len = strlen(h_status);
163 for(i = 0; i < len; i++) {
80e8aa14 164 if(!isdigit(h_status[i])) {
165 printf("Error: status code %s is not numeric.\n", h_status);
166 exit(1);
167 }
168 }
169 } else
170 usage(argv);
171 }
172 else if (argis("V", "vendor")) {
173 if (arg - argv < argc - 1) {
174 arg++;
175 update_flag++;
176 vendor = *arg;
177 } else
178 usage(argv);
179 }
180 else if (argis("M", "model")) {
181 if (arg - argv < argc - 1) {
182 arg++;
183 update_flag++;
184 model = *arg;
185 } else
186 usage(argv);
187 }
188 else if (argis("o", "os")) {
189 if (arg - argv < argc - 1) {
190 arg++;
191 update_flag++;
192 os = *arg;
193 } else
194 usage(argv);
195 }
196 else if (argis("L", "location")) {
197 if (arg - argv < argc - 1) {
198 arg++;
199 update_flag++;
200 location = *arg;
201 } else
202 usage(argv);
203 }
204 else if (argis("c", "contact")) {
205 if (arg - argv < argc - 1) {
206 arg++;
207 update_flag++;
208 contact = *arg;
209 } else
210 usage(argv);
211 }
080e5e2c 212 else if (argis("bc", "billingcontact")) {
213 if (arg - argv < argc - 1) {
214 arg++;
215 update_flag++;
216 billing_contact = *arg;
217 } else
218 usage(argv);
219 }
80e8aa14 220 else if (argis("ac", "admcmt")) {
221 if (arg - argv < argc - 1) {
222 arg++;
223 update_flag++;
224 adm_cmt = *arg;
225 } else
226 usage(argv);
227 }
228 else if (argis("oc", "opcmt")) {
229 if (arg - argv < argc - 1) {
230 arg++;
231 update_flag++;
232 op_cmt = *arg;
233 } else
234 usage(argv);
235 }
236 else if (argis("a", "aliasadd")) {
237 if (arg - argv < argc - 1) {
238 arg++;
239 alias_add_queue=add_to_string_list(alias_add_queue, *arg);
240 } else
241 usage(argv);
7e689fac 242 update_alias_flag++;
80e8aa14 243 }
244 else if (argis("d", "aliasdelete")) {
245 if (arg - argv < argc - 1) {
246 arg++;
247 alias_remove_queue=add_to_string_list(alias_remove_queue, *arg);
248 } else
249 usage(argv);
7e689fac 250 update_alias_flag++;
80e8aa14 251 }
252 else if (argis("am", "addmap")) {
253 if (arg - argv < argc - 1) {
254 arg++;
255 map_add_queue=add_to_string_list(map_add_queue, *arg);
256 } else
257 usage(argv);
258 update_map_flag++;
259 }
260 else if (argis("dm", "deletemap")) {
261 if (arg - argv < argc - 1) {
262 arg++;
263 map_remove_queue=add_to_string_list(map_remove_queue, *arg);
264 } else
265 usage(argv);
266 update_map_flag++;
267 }
268 else if (argis("lm", "listmap"))
269 list_map_flag++;
9cd2d193 270 else if (argis("acn", "addcontainer")) {
271 if (arg - argv < argc - 1) {
272 arg++;
273 container_add_queue =
274 add_to_string_list(container_add_queue, *arg);
275 } else
276 usage(argv);
277 update_container_flag++;
278 }
279 else if (argis("dcn", "deletecontainer")) {
280 if (arg - argv < argc - 1) {
281 arg++;
282 container_remove_queue =
283 add_to_string_list(container_remove_queue, *arg);
284 } else
285 usage(argv);
286 update_container_flag++;
287 }
288 else if (argis("lcn", "listcontainer"))
289 list_container_flag++;
7b6a099a 290 else if (argis("u", "unformatted"))
291 unformatted_flag++;
80e8aa14 292 else if (argis("n", "noauth"))
293 noauth++;
294 else if (argis("v", "verbose"))
295 verbose++;
296 else if (argis("db", "database"))
297 {
298 if (arg - argv < argc - 1)
299 {
300 ++arg;
301 server = *arg;
302 }
303 else
304 usage(argv);
305 }
306 else
307 usage(argv);
308 }
309 else if (hostname == NULL)
310 hostname = *arg;
311 else
312 usage(argv);
313 }
314 if (hostname == NULL)
315 usage(argv);
316
317 /* default to info_flag if nothing else was specified */
318 if(!(info_flag || update_flag || create_flag || \
7e689fac 319 delete_flag || list_map_flag || update_map_flag || \
9cd2d193 320 update_alias_flag || update_container_flag || \
321 list_container_flag)) {
80e8aa14 322 info_flag++;
323 }
324
325 /* fire up Moira */
9cd2d193 326 status = mrcl_connect(server, "stella", 7, !noauth);
80e8aa14 327 if (status == MRCL_AUTH_ERROR)
328 {
329 com_err(whoami, 0, "Try the -noauth flag if you don't "
330 "need authentication.");
331 }
332 if (status)
333 exit(2);
334
8d138b83 335 /* Perform the lookup by IP address if that's what we've been handed */
336 if ((ipaddress=inet_addr(hostname)) != -1) {
337 char *args[5];
338 char *argv[30];
339
340 args[1] = strdup(hostname);
341 args[0] = args[2] = args[3] = "*";
342 status = wrap_mr_query("get_host", 4, args, store_host_info, argv);
343
344 if (status) {
345 com_err(whoami, status, "while looking up IP address.");
346 } else {
347 hostname = argv[0];
348 }
349 }
350
80e8aa14 351 /* create if needed */
352 if (create_flag)
353 {
354 char *argv[30];
355 int cnt;
356
080e5e2c 357 for (cnt = 0; cnt < 16; cnt++) {
80e8aa14 358 argv[cnt] = "";
359 }
360
361 argv[0] = canonicalize_hostname(strdup(hostname));
362
363 if (vendor)
364 argv[1] = vendor;
365 if (model)
366 argv[2] = model;
367 if (os)
368 argv[3] = os;
369 if (location)
370 argv[4] = location;
371 if (contact)
372 argv[5] = contact;
080e5e2c 373 if (billing_contact)
374 argv[6] = billing_contact;
80e8aa14 375 /* The use field always gets set to "0" */
080e5e2c 376 argv[7] = "0";
80e8aa14 377 if (h_status)
080e5e2c 378 argv[8] = h_status;
80e8aa14 379 else
080e5e2c 380 argv[8] = "1";
80e8aa14 381 if (network)
080e5e2c 382 argv[9] = network;
80e8aa14 383 if (address)
080e5e2c 384 argv[10] = address;
d55f933a 385 else
080e5e2c 386 argv[10] = "unique";
80e8aa14 387 if (adm_cmt)
080e5e2c 388 argv[13] = adm_cmt;
80e8aa14 389 if (op_cmt)
080e5e2c 390 argv[14] = op_cmt;
80e8aa14 391
392 if (owner)
393 {
080e5e2c 394 argv[12] = owner->name;
80e8aa14 395 switch (owner->type)
396 {
397 case M_ANY:
398 case M_USER:
080e5e2c 399 argv[11] = "USER";
400 status = wrap_mr_query("add_host", 15, argv, NULL, NULL);
80e8aa14 401 if (owner->type != M_ANY || status != MR_USER)
402 break;
403
404 case M_LIST:
080e5e2c 405 argv[11] = "LIST";
406 status = wrap_mr_query("add_host", 15, argv, NULL, NULL);
80e8aa14 407 break;
408
409 case M_KERBEROS:
080e5e2c 410 argv[11] = "KERBEROS";
df183ce8 411 status = mrcl_validate_kerberos_member(argv[12], &argv[12]);
412 if (mrcl_get_message())
413 mrcl_com_err(whoami);
080e5e2c 414 status = wrap_mr_query("add_host", 15, argv, NULL, NULL);
80e8aa14 415 break;
416
417 case M_NONE:
080e5e2c 418 argv[11] = "NONE";
419 status = wrap_mr_query("add_host", 15, argv, NULL, NULL);
80e8aa14 420 break;
421 }
422 }
423 else
424 {
80e8aa14 425 argv[11] = "NONE";
080e5e2c 426 argv[12] = "NONE";
80e8aa14 427
080e5e2c 428 status = wrap_mr_query("add_host", 15, argv, NULL, NULL);
80e8aa14 429 }
430
431 if (status)
432 {
433 com_err(whoami, status, "while creating host.");
434 exit(1);
435 }
436
437 }
438 else if (update_flag)
439 {
440 char *old_argv[30];
441 char *argv[16];
442 char *args[5];
80e8aa14 443
444 args[0] = canonicalize_hostname(strdup(hostname));
445 args[1] = args[2] = args[3] = "*";
446
447 status = wrap_mr_query("get_host", 4, args, store_host_info, old_argv);
448 if (status)
449 {
450 com_err(whoami, status, "while getting list information");
451 exit(1);
452 }
453
454 argv[1] = old_argv[0];
455 argv[2] = old_argv[1];
456 argv[3] = old_argv[2];
457 argv[4] = old_argv[3];
458 argv[5] = old_argv[4];
459 argv[6] = old_argv[5];
460 argv[7] = old_argv[6];
461 argv[8] = old_argv[7];
080e5e2c 462 argv[9] = old_argv[8];
80e8aa14 463 argv[10] = old_argv[10];
464 argv[11] = old_argv[11];
465 argv[12] = old_argv[12];
466 argv[13] = old_argv[13];
467 argv[14] = old_argv[14];
080e5e2c 468 argv[15] = old_argv[15];
80e8aa14 469
470 argv[0] = canonicalize_hostname(strdup(hostname));
471 if (newname)
6c114a34 472 argv[1] = canonicalize_hostname(strdup(newname));
80e8aa14 473 if (vendor)
474 argv[2] = vendor;
475 if (model)
476 argv[3] = model;
477 if (os)
478 argv[4] = os;
479 if (location)
480 argv[5] = location;
481 if (contact)
482 argv[6] = contact;
080e5e2c 483 if (billing_contact)
484 argv[7] = billing_contact;
80e8aa14 485 if (h_status)
080e5e2c 486 argv[9] = h_status;
80e8aa14 487 if (network)
080e5e2c 488 argv[10] = network;
80e8aa14 489 if (address)
080e5e2c 490 argv[11] = address;
80e8aa14 491 if (adm_cmt)
080e5e2c 492 argv[14] = adm_cmt;
80e8aa14 493 if (op_cmt)
080e5e2c 494 argv[15] = op_cmt;
80e8aa14 495
496 if (owner)
497 {
080e5e2c 498 argv[13] = owner->name;
80e8aa14 499 switch (owner->type)
500 {
501 case M_ANY:
502 case M_USER:
080e5e2c 503 argv[12] = "USER";
504 status = wrap_mr_query("update_host", 16, argv, NULL, NULL);
80e8aa14 505 if (owner->type != M_ANY || status != MR_USER)
506 break;
507
508 case M_LIST:
080e5e2c 509 argv[12] = "LIST";
510 status = wrap_mr_query("update_host", 16, argv, NULL, NULL);
80e8aa14 511 break;
512
513 case M_KERBEROS:
080e5e2c 514 argv[12] = "KERBEROS";
df183ce8 515 status = mrcl_validate_kerberos_member(argv[13], &argv[13]);
516 if (mrcl_get_message())
517 mrcl_com_err(whoami);
080e5e2c 518 status = wrap_mr_query("update_host", 16, argv, NULL, NULL);
80e8aa14 519 break;
520
521 case M_NONE:
080e5e2c 522 argv[12] = "NONE";
523 status = wrap_mr_query("update_host", 16, argv, NULL, NULL);
80e8aa14 524 break;
525 }
526 }
527 else
080e5e2c 528 status = wrap_mr_query("update_host", 16, argv, NULL, NULL);
80e8aa14 529
530 if (status)
531 com_err(whoami, status, "while updating host.");
532 else if (newname)
533 hostname = newname;
534 }
535
536 /* create aliases if necessary */
537 if (alias_add_queue) {
538 struct string_list *q = alias_add_queue;
539
540 while(q) {
541 char *alias = q->string;
542 char *args[2];
543
5aa7fdfd 544 args[0] = partial_canonicalize_hostname(strdup(alias));
80e8aa14 545 args[1] = canonicalize_hostname(strdup(hostname));
546 status = wrap_mr_query("add_hostalias", 2, args, NULL, NULL);
547 if (status) {
548 com_err(whoami, status, "while adding host alias");
549 exit(1);
550 }
551
552 q = q->next;
553 }
554 }
555
556 /* delete aliases if necessary */
557 if (alias_remove_queue) {
558 struct string_list *q = alias_remove_queue;
559
560 while(q) {
561 char *alias = q->string;
562 char *args[2];
563
5aa7fdfd 564 args[0] = partial_canonicalize_hostname(strdup(alias));
80e8aa14 565 args[1] = canonicalize_hostname(strdup(hostname));
566 status = wrap_mr_query("delete_hostalias", 2, args, NULL, NULL);
567 if (status) {
568 com_err(whoami, status, "while deleting host alias");
569 exit(1);
570 }
571
572 q = q->next;
573 }
574 }
575
576 /* create cluster mappings */
577 if (map_add_queue) {
578 struct string_list *q = map_add_queue;
579
580 while(q) {
581 char *clustername = q->string;
582 char *args[2];
583
584 args[0] = canonicalize_hostname(strdup(hostname));
585 args[1] = clustername;
586 status = wrap_mr_query("add_machine_to_cluster", 2, args, NULL, NULL);
587 if (status) {
588 com_err(whoami, status, "while adding cluster mapping");
589 exit(1);
590 }
591
592 q = q->next;
593 }
594 }
595
596 /* delete cluster mappings */
597 if (map_remove_queue) {
598 struct string_list *q = map_remove_queue;
599
600 while(q) {
601 char *clustername = q->string;
602 char *args[2];
603
604 args[0] = canonicalize_hostname(strdup(hostname));
605 args[1] = clustername;
606 status = wrap_mr_query("delete_machine_from_cluster", 2, args,
607 NULL, NULL);
608 if (status) {
609 com_err(whoami, status, "while deleting cluster mapping");
610 exit(1);
611 }
612
613 q = q->next;
614 }
615 }
616
9cd2d193 617 /* add container mappings */
618 if (container_add_queue) {
619 struct string_list *q = container_add_queue;
620
621 while (q) {
622 char *containername = q->string;
623 char *args[2];
624
625 args[0] = canonicalize_hostname(strdup(hostname));
626 args[1] = containername;
627 status = wrap_mr_query("add_machine_to_container", 2, args,
628 NULL, NULL);
629
630 if (status) {
631 com_err(whoami, status, "while adding container mapping");
632 exit(1);
633 }
634
635 q = q->next;
636 }
637 }
638
639 /* delete container mappings */
640 if (container_remove_queue) {
641 struct string_list *q = container_remove_queue;
642
643 while (q) {
644 char *containername = q->string;
645 char *args[2];
646
647 args[0] = canonicalize_hostname(strdup(hostname));
648 args[1] = containername;
649 status = wrap_mr_query("delete_machine_from_container", 2, args,
650 NULL, NULL);
651
652 if (status) {
653 com_err(whoami, status, "while deleting container mapping");
654 exit(1);
655 }
656
657 q = q->next;
658 }
659 }
660
80e8aa14 661 /* display list info if requested to */
662 if (info_flag) {
663 struct mqelem *elem = NULL;
664 char *args[5];
665 char *argv[30];
666
667 args[0] = canonicalize_hostname(strdup(hostname));
668 args[1] = args[2] = args[3] = "*";
669 status = wrap_mr_query("get_host", 4, args, store_host_info, argv);
670 if (status) {
671 com_err(whoami, status, "while getting host information");
672 exit(1);
673 }
7b6a099a 674 if (unformatted_flag)
675 show_host_info_unformatted(argv);
676 else
677 show_host_info(argv);
80e8aa14 678 }
679
680 /* list cluster mappings if needed */
681 if (list_map_flag) {
682 char *args[3];
683
684 args[0] = canonicalize_hostname(strdup(hostname));
685 args[1] = "*";
686 status = wrap_mr_query("get_machine_to_cluster_map", 2, args,
687 show_machine_in_cluster, NULL);
688 if (status)
689 if (status != MR_NO_MATCH) {
690 com_err(whoami, status, "while getting cluster mappings");
691 exit(1);
692 }
693 }
694
9cd2d193 695 /* list container mappings if needed */
696 if (list_container_flag) {
697 char *argv[1];
698
699 argv[0] = canonicalize_hostname(strdup(hostname));
700 status = wrap_mr_query("get_machine_to_container_map", 1, argv,
701 show_machine_in_container, NULL);
702
703 if (status)
704 if (status != MR_NO_MATCH) {
705 com_err(whoami, status, "while getting container mappings");
706 exit(1);
707 }
708 }
709
80e8aa14 710 if (delete_flag) {
711 char *argv[1];
712
713 argv[0] = canonicalize_hostname(strdup(hostname));
714 status = wrap_mr_query("delete_host", 1, argv, NULL, NULL);
715 if (status) {
716 com_err(whoami, status, "while deleting host");
717 exit(1);
718 }
719 }
720
721 /* We're done! */
722 mr_disconnect();
723 exit(success ? 0 : 1);
724}
725
726void usage(char **argv)
727{
533bacb3 728#define USAGE_OPTIONS_FORMAT " %-39s%s\n"
80e8aa14 729 fprintf(stderr, "Usage: %s hostname [options]\n", argv[0]);
730 fprintf(stderr, "Options are\n");
533bacb3 731 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-C | -create",
80e8aa14 732 "-O | -owner owner");
533bacb3 733 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-D | -delete",
80e8aa14 734 "-S | -status status");
533bacb3 735 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-R | -rename newname",
80e8aa14 736 "-V | -vendor vendor");
533bacb3 737 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-a | -addalias alias",
80e8aa14 738 "-M | -model model");
533bacb3 739 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-d | -deletealias alias",
80e8aa14 740 "-L | -location location");
533bacb3 741 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-i | -info",
80e8aa14 742 "-o | -os os");
533bacb3 743 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-oc | -opcmt op_cmt",
80e8aa14 744 "-c | -contact contact");
533bacb3 745 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-ac | -admcmt adm_cmt",
080e5e2c 746 "-bc | -billingcontact billing_contact");
747 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-A | -address address",
80e8aa14 748 "-N | -network network");
080e5e2c 749 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-am | -addmap cluster",
750 "-dm | deletemap cluster");
9cd2d193 751 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-acn | -addcontainer container",
752 "-dcn | -deletecontainer container");
080e5e2c 753 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-lm | -listmap",
9cd2d193 754 "-lcn | -listcontainer");
7b6a099a 755 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-u | -unformatted",
756 "-v | -verbose");
757 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-n | -noauth",
758 "-db | -database host[:port]");
80e8aa14 759 exit(1);
760}
761
762/* Show alias information */
763
764static int show_has_aliases;
765
766int show_alias_info(int argc, char **argv, void *hint)
767{
768 if(!show_has_aliases++)
769 printf("Aliases: %s", argv[0]);
770 else
771 printf(", %s", argv[0]);
772
773 return MR_CONT;
774}
775
7b6a099a 776int show_alias_info_unformatted(int argc, char **argv, void *hint)
777{
778 if(!show_has_aliases++)
779 printf("Alias: %s", argv[0]);
780 else
781 printf(", %s", argv[0]);
782
783 return MR_CONT;
784}
785
80e8aa14 786static char *states[] = {
787 "Reserved (0)",
788 "Active (1)",
789 "None (2)",
790 "Deleted (3)"
791};
792
793static char *MacState(int state)
794{
795 static char buf[BUFSIZ];
796
797 if (state < 0 || state > 3)
798 {
799 sprintf(buf, "Unknown (%d)", state);
800 return buf;
801 }
802 return states[state];
803}
804
805/* Retrieve information about a host */
806
807int store_host_info(int argc, char **argv, void *hint)
808{
809 int i;
810 char **nargv = hint;
811
812 for(i=0; i<argc; i++)
813 nargv[i] = strdup(argv[i]);
814
815 return MR_CONT;
816}
817
818void show_host_info(char **argv)
819{
820 char tbuf[256];
821 char *args[3];
822 struct mqelem *elem = NULL;
823 int stat;
824
825 printf("Machine: %s\n", argv[M_NAME]);
826 args[0] = "*";
827 args[1] = argv[M_NAME];
828 show_has_aliases = 0;
829 stat = wrap_mr_query("get_hostalias", 2, args, show_alias_info, &elem);
830 printf("\n");
831 if (stat) {
832 if (stat != MR_NO_MATCH)
833 com_err(whoami, stat, "while getting aliases");
834 } else {
835 printf("\n");
836 }
837 sprintf(tbuf, "%s %s", argv[M_OWNER_TYPE],
838 strcmp(argv[M_OWNER_TYPE], "NONE") ? argv[M_OWNER_NAME] : "");
839 printf("Address: %-16s Network: %-16s\n",
840 argv[M_ADDR], argv[M_SUBNET]);
841 printf("Owner: %-16s Use data: %s\n", tbuf, argv[M_INUSE]);
842 printf("Status: %-16s Changed: %s\n",
843 MacState(atoi(argv[M_STAT])), argv[M_STAT_CHNG]);
844 printf("\n");
080e5e2c 845 printf("Vendor: %-16s Location: %s\n", argv[M_VENDOR],
846 argv[M_LOC]);
847 printf("Model: %-16s Contact: %s\n", argv[M_MODEL],
848 argv[M_CONTACT]);
849 printf("OS: %-16s Billing Contact: %s\n", argv[M_OS],
850 argv[M_BILL_CONTACT]);
851 printf("\nOpt: %s\n", argv[M_USE]);
80e8aa14 852 printf("\nAdm cmt: %s\n", argv[M_ACOMMENT]);
853 printf("Op cmt: %s\n", argv[M_OCOMMENT]);
854 printf("\n");
855 printf("Created by %s on %s\n", argv[M_CREATOR], argv[M_CREATED]);
856 printf("Last mod by %s at %s with %s.\n", argv[M_MODBY], argv[M_MODTIME], argv[M_MODWITH]);
857}
858
7b6a099a 859void show_host_info_unformatted(char **argv)
860{
7b6a099a 861 char *args[3];
862 struct mqelem *elem = NULL;
863 int stat;
864
865 printf("Machine: %s\n", argv[M_NAME]);
866 args[0] = "*";
867 args[1] = argv[M_NAME];
868 show_has_aliases = 0;
869 stat = wrap_mr_query("get_hostalias", 2, args, show_alias_info_unformatted,
870 &elem);
871 if (stat && stat != MR_NO_MATCH)
872 com_err(whoami, stat, "while getting aliases");
873 else
874 printf("\n");
875 printf("Address: %s\n", argv[M_ADDR]);
876 printf("Network: %s\n", argv[M_SUBNET]);
877 printf("Owner Type: %s\n", argv[M_OWNER_TYPE]);
878 printf("Owner: %s\n", argv[M_OWNER_NAME]);
879 printf("Status: %s\n", MacState(atoi(argv[M_STAT])));
880 printf("Changed: %s\n", argv[M_STAT_CHNG]);
881 printf("Use data: %s\n", argv[M_INUSE]);
882 printf("Vendor: %s\n", argv[M_VENDOR]);
883 printf("Model: %s\n", argv[M_MODEL]);
884 printf("OS: %s\n", argv[M_OS]);
885 printf("Location: %s\n", argv[M_LOC]);
886 printf("Contact: %s\n", argv[M_CONTACT]);
887 printf("Billing Contact: %s\n", argv[M_BILL_CONTACT]);
888 printf("Opt: %s\n", argv[M_USE]);
889 printf("Adm cmt: %s\n", argv[M_ACOMMENT]);
890 printf("Op cmt: %s\n", argv[M_OCOMMENT]);
891 printf("Created by: %s\n", argv[M_CREATOR]);
892 printf("Created on: %s\n", argv[M_CREATED]);
893 printf("Last mod by: %s\n", argv[M_MODBY]);
894 printf("Last mod on: %s\n", argv[M_MODTIME]);
895 printf("Last mod with: %s\n", argv[M_MODWITH]);
896}
897
80e8aa14 898int show_machine_in_cluster(int argc, char **argv, void *hint)
899{
900 printf("Machine: %-30s Cluster: %-30s\n", argv[0], argv[1]);
901
902 return MR_CONT;
903}
904
9cd2d193 905int show_machine_in_container(int argc, char **argv, void *hint)
906{
907 printf("Machine: %-30s Container: %-25s\n", argv[0], argv[1]);
908
909 return MR_CONT;
910}
911
80e8aa14 912/* Parse a line of input, fetching a member. NULL is returned if a member
913 * is not found. ';' is a comment character.
914 */
915
916struct owner_type *parse_member(char *s)
917{
918 struct owner_type *m;
919 char *p, *lastchar;
920
921 while (*s && isspace(*s))
922 s++;
923 lastchar = p = s;
924 while (*p && *p != '\n' && *p != ';')
925 {
926 if (isprint(*p) && !isspace(*p))
927 lastchar = p++;
928 else
929 p++;
930 }
931 lastchar++;
932 *lastchar = '\0';
933 if (p == s || strlen(s) == 0)
934 return NULL;
935
936 if (!(m = malloc(sizeof(struct owner_type))))
937 return NULL;
938
939 if ((p = strchr(s, ':')))
940 {
941 *p = '\0';
942 m->name = ++p;
943 if (!strcasecmp("user", s))
944 m->type = M_USER;
945 else if (!strcasecmp("list", s))
946 m->type = M_LIST;
80e8aa14 947 else if (!strcasecmp("kerberos", s))
948 m->type = M_KERBEROS;
949 else if (!strcasecmp("none", s))
950 m->type = M_NONE;
951 else
952 {
953 m->type = M_ANY;
954 *(--p) = ':';
955 m->name = s;
956 }
957 m->name = strdup(m->name);
958 }
959 else
960 {
961 m->name = strdup(s);
962 m->type = strcasecmp(s, "none") ? M_ANY : M_NONE;
963 }
964 return m;
965}
966
967struct string_list *add_to_string_list(struct string_list *old_list, char *s) {
968 struct string_list *new_list;
969
970 new_list = (struct string_list *)malloc(sizeof(struct string_list *));
971 new_list->next = old_list;
972 new_list->string = s;
973
974 return new_list;
975}
976
977int wrap_mr_query(char *handle, int argc, char **argv,
978 int (*callback)(int, char **, void *), void *callarg) {
979 if (verbose)
980 print_query(handle, argc, argv);
981
982 return mr_query(handle, argc, argv, callback, callarg);
983}
984
985void print_query(char *query_name, int argc, char **argv) {
986 int cnt;
987
988 printf("qy %s", query_name);
989 for(cnt=0; cnt<argc; cnt++)
990 printf(" <%s>", argv[cnt]);
991 printf("\n");
992}
This page took 0.377394 seconds and 5 git commands to generate.