2 * Command line oriented Moira host tool.
4 * kolya@MIT.EDU, January 2000
6 * Somewhat based on blanche
8 * Copyright (C) 2000 by the Massachusetts Institute of Technology.
9 * For copying and distribution information, please see the file
13 #include <mit-copyright.h>
15 #include <moira_site.h>
32 struct mqelem *q_forw;
33 struct mqelem *q_back;
39 struct string_list *next;
48 /* argument parsing macro */
49 #define argis(a, b) (!strcmp(*arg + 1, a) || !strcmp(*arg + 1, b))
51 /* flags from command line */
52 int info_flag, update_flag, create_flag, delete_flag, list_map_flag;
53 int update_alias_flag, update_map_flag, verbose, noauth;
54 int list_container_flag, update_container_flag;
56 struct string_list *alias_add_queue, *alias_remove_queue;
57 struct string_list *map_add_queue, *map_remove_queue;
58 struct string_list *container_add_queue, *container_remove_queue;
60 char *hostname, *whoami;
62 char *newname, *address, *network, *h_status, *vendor, *model;
63 char *os, *location, *contact, *billing_contact, *adm_cmt, *op_cmt;
65 struct owner_type *owner;
67 void usage(char **argv);
68 int store_host_info(int argc, char **argv, void *hint);
69 void show_host_info(char **argv);
70 int show_machine_in_cluster(int argc, char **argv, void *hint);
71 int show_machine_in_container(int argc, char **argv, void *hint);
72 struct owner_type *parse_member(char *s);
73 struct string_list *add_to_string_list(struct string_list *old_list, char *s);
74 int wrap_mr_query(char *handle, int argc, char **argv,
75 int (*callback)(int, char **, void *), void *callarg);
76 void print_query(char *query_name, int argc, char **argv);
78 int main(int argc, char **argv)
84 /* clear all flags & lists */
85 info_flag = update_flag = create_flag = list_map_flag = update_map_flag = 0;
86 update_alias_flag = verbose = noauth = 0;
87 list_container_flag = update_container_flag = 0;
88 newname = address = network = h_status = vendor = model = NULL;
89 os = location = contact = billing_contact = adm_cmt = op_cmt = NULL;
91 alias_add_queue = alias_remove_queue = NULL;
92 map_add_queue = map_remove_queue = NULL;
93 container_add_queue = container_remove_queue = NULL;
98 /* parse args, building addlist, dellist, & synclist */
99 while (++arg - argv < argc)
103 if (argis("i", "info"))
105 else if (argis("C", "create"))
107 else if (argis("D", "delete"))
109 else if (argis("R", "rename")) {
110 if (arg - argv < argc - 1) {
117 else if (argis("A", "address")) {
118 if (arg - argv < argc - 1) {
125 else if (argis("O", "owner")) {
126 if (arg - argv < argc - 1) {
129 owner = parse_member(*arg);
133 else if (argis("N", "network")) {
134 if (arg - argv < argc - 1) {
141 else if (argis("S", "status")) {
142 if (arg - argv < argc - 1) {
150 len = strlen(h_status);
151 for(i = 0; i < len; i++) {
152 if(!isdigit(h_status[i])) {
153 printf("Error: status code %s is not numeric.\n", h_status);
160 else if (argis("V", "vendor")) {
161 if (arg - argv < argc - 1) {
168 else if (argis("M", "model")) {
169 if (arg - argv < argc - 1) {
176 else if (argis("o", "os")) {
177 if (arg - argv < argc - 1) {
184 else if (argis("L", "location")) {
185 if (arg - argv < argc - 1) {
192 else if (argis("c", "contact")) {
193 if (arg - argv < argc - 1) {
200 else if (argis("bc", "billingcontact")) {
201 if (arg - argv < argc - 1) {
204 billing_contact = *arg;
208 else if (argis("ac", "admcmt")) {
209 if (arg - argv < argc - 1) {
216 else if (argis("oc", "opcmt")) {
217 if (arg - argv < argc - 1) {
224 else if (argis("a", "aliasadd")) {
225 if (arg - argv < argc - 1) {
227 alias_add_queue=add_to_string_list(alias_add_queue, *arg);
232 else if (argis("d", "aliasdelete")) {
233 if (arg - argv < argc - 1) {
235 alias_remove_queue=add_to_string_list(alias_remove_queue, *arg);
240 else if (argis("am", "addmap")) {
241 if (arg - argv < argc - 1) {
243 map_add_queue=add_to_string_list(map_add_queue, *arg);
248 else if (argis("dm", "deletemap")) {
249 if (arg - argv < argc - 1) {
251 map_remove_queue=add_to_string_list(map_remove_queue, *arg);
256 else if (argis("lm", "listmap"))
258 else if (argis("acn", "addcontainer")) {
259 if (arg - argv < argc - 1) {
261 container_add_queue =
262 add_to_string_list(container_add_queue, *arg);
265 update_container_flag++;
267 else if (argis("dcn", "deletecontainer")) {
268 if (arg - argv < argc - 1) {
270 container_remove_queue =
271 add_to_string_list(container_remove_queue, *arg);
274 update_container_flag++;
276 else if (argis("lcn", "listcontainer"))
277 list_container_flag++;
278 else if (argis("n", "noauth"))
280 else if (argis("v", "verbose"))
282 else if (argis("db", "database"))
284 if (arg - argv < argc - 1)
295 else if (hostname == NULL)
300 if (hostname == NULL)
303 /* default to info_flag if nothing else was specified */
304 if(!(info_flag || update_flag || create_flag || \
305 delete_flag || list_map_flag || update_map_flag || \
306 update_alias_flag || update_container_flag || \
307 list_container_flag)) {
312 status = mrcl_connect(server, "stella", 7, !noauth);
313 if (status == MRCL_AUTH_ERROR)
315 com_err(whoami, 0, "Try the -noauth flag if you don't "
316 "need authentication.");
321 /* create if needed */
327 for (cnt = 0; cnt < 16; cnt++) {
331 argv[0] = canonicalize_hostname(strdup(hostname));
344 argv[6] = billing_contact;
345 /* The use field always gets set to "0" */
364 argv[12] = owner->name;
370 status = wrap_mr_query("add_host", 15, argv, NULL, NULL);
371 if (owner->type != M_ANY || status != MR_USER)
376 status = wrap_mr_query("add_host", 15, argv, NULL, NULL);
380 argv[11] = "KERBEROS";
381 status = mrcl_validate_kerberos_member(argv[12], &argv[12]);
382 if (mrcl_get_message())
383 mrcl_com_err(whoami);
384 status = wrap_mr_query("add_host", 15, argv, NULL, NULL);
389 status = wrap_mr_query("add_host", 15, argv, NULL, NULL);
398 status = wrap_mr_query("add_host", 15, argv, NULL, NULL);
403 com_err(whoami, status, "while creating host.");
408 else if (update_flag)
414 args[0] = canonicalize_hostname(strdup(hostname));
415 args[1] = args[2] = args[3] = "*";
417 status = wrap_mr_query("get_host", 4, args, store_host_info, old_argv);
420 com_err(whoami, status, "while getting list information");
424 argv[1] = old_argv[0];
425 argv[2] = old_argv[1];
426 argv[3] = old_argv[2];
427 argv[4] = old_argv[3];
428 argv[5] = old_argv[4];
429 argv[6] = old_argv[5];
430 argv[7] = old_argv[6];
431 argv[8] = old_argv[7];
432 argv[9] = old_argv[8];
433 argv[10] = old_argv[10];
434 argv[11] = old_argv[11];
435 argv[12] = old_argv[12];
436 argv[13] = old_argv[13];
437 argv[14] = old_argv[14];
438 argv[15] = old_argv[15];
440 argv[0] = canonicalize_hostname(strdup(hostname));
442 argv[1] = canonicalize_hostname(strdup(newname));
454 argv[7] = billing_contact;
468 argv[13] = owner->name;
474 status = wrap_mr_query("update_host", 16, argv, NULL, NULL);
475 if (owner->type != M_ANY || status != MR_USER)
480 status = wrap_mr_query("update_host", 16, argv, NULL, NULL);
484 argv[12] = "KERBEROS";
485 status = mrcl_validate_kerberos_member(argv[13], &argv[13]);
486 if (mrcl_get_message())
487 mrcl_com_err(whoami);
488 status = wrap_mr_query("update_host", 16, argv, NULL, NULL);
493 status = wrap_mr_query("update_host", 16, argv, NULL, NULL);
498 status = wrap_mr_query("update_host", 16, argv, NULL, NULL);
501 com_err(whoami, status, "while updating host.");
506 /* create aliases if necessary */
507 if (alias_add_queue) {
508 struct string_list *q = alias_add_queue;
511 char *alias = q->string;
514 args[0] = partial_canonicalize_hostname(strdup(alias));
515 args[1] = canonicalize_hostname(strdup(hostname));
516 status = wrap_mr_query("add_hostalias", 2, args, NULL, NULL);
518 com_err(whoami, status, "while adding host alias");
526 /* delete aliases if necessary */
527 if (alias_remove_queue) {
528 struct string_list *q = alias_remove_queue;
531 char *alias = q->string;
534 args[0] = partial_canonicalize_hostname(strdup(alias));
535 args[1] = canonicalize_hostname(strdup(hostname));
536 status = wrap_mr_query("delete_hostalias", 2, args, NULL, NULL);
538 com_err(whoami, status, "while deleting host alias");
546 /* create cluster mappings */
548 struct string_list *q = map_add_queue;
551 char *clustername = q->string;
554 args[0] = canonicalize_hostname(strdup(hostname));
555 args[1] = clustername;
556 status = wrap_mr_query("add_machine_to_cluster", 2, args, NULL, NULL);
558 com_err(whoami, status, "while adding cluster mapping");
566 /* delete cluster mappings */
567 if (map_remove_queue) {
568 struct string_list *q = map_remove_queue;
571 char *clustername = q->string;
574 args[0] = canonicalize_hostname(strdup(hostname));
575 args[1] = clustername;
576 status = wrap_mr_query("delete_machine_from_cluster", 2, args,
579 com_err(whoami, status, "while deleting cluster mapping");
587 /* add container mappings */
588 if (container_add_queue) {
589 struct string_list *q = container_add_queue;
592 char *containername = q->string;
595 args[0] = canonicalize_hostname(strdup(hostname));
596 args[1] = containername;
597 status = wrap_mr_query("add_machine_to_container", 2, args,
601 com_err(whoami, status, "while adding container mapping");
609 /* delete container mappings */
610 if (container_remove_queue) {
611 struct string_list *q = container_remove_queue;
614 char *containername = q->string;
617 args[0] = canonicalize_hostname(strdup(hostname));
618 args[1] = containername;
619 status = wrap_mr_query("delete_machine_from_container", 2, args,
623 com_err(whoami, status, "while deleting container mapping");
631 /* display list info if requested to */
633 struct mqelem *elem = NULL;
637 args[0] = canonicalize_hostname(strdup(hostname));
638 args[1] = args[2] = args[3] = "*";
639 status = wrap_mr_query("get_host", 4, args, store_host_info, argv);
641 com_err(whoami, status, "while getting host information");
644 show_host_info(argv);
647 /* list cluster mappings if needed */
651 args[0] = canonicalize_hostname(strdup(hostname));
653 status = wrap_mr_query("get_machine_to_cluster_map", 2, args,
654 show_machine_in_cluster, NULL);
656 if (status != MR_NO_MATCH) {
657 com_err(whoami, status, "while getting cluster mappings");
662 /* list container mappings if needed */
663 if (list_container_flag) {
666 argv[0] = canonicalize_hostname(strdup(hostname));
667 status = wrap_mr_query("get_machine_to_container_map", 1, argv,
668 show_machine_in_container, NULL);
671 if (status != MR_NO_MATCH) {
672 com_err(whoami, status, "while getting container mappings");
680 argv[0] = canonicalize_hostname(strdup(hostname));
681 status = wrap_mr_query("delete_host", 1, argv, NULL, NULL);
683 com_err(whoami, status, "while deleting host");
690 exit(success ? 0 : 1);
693 void usage(char **argv)
695 #define USAGE_OPTIONS_FORMAT " %-39s%s\n"
696 fprintf(stderr, "Usage: %s hostname [options]\n", argv[0]);
697 fprintf(stderr, "Options are\n");
698 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-C | -create",
699 "-O | -owner owner");
700 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-D | -delete",
701 "-S | -status status");
702 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-R | -rename newname",
703 "-V | -vendor vendor");
704 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-a | -addalias alias",
705 "-M | -model model");
706 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-d | -deletealias alias",
707 "-L | -location location");
708 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-i | -info",
710 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-oc | -opcmt op_cmt",
711 "-c | -contact contact");
712 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-ac | -admcmt adm_cmt",
713 "-bc | -billingcontact billing_contact");
714 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-A | -address address",
715 "-N | -network network");
716 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-am | -addmap cluster",
717 "-dm | deletemap cluster");
718 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-acn | -addcontainer container",
719 "-dcn | -deletecontainer container");
720 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-lm | -listmap",
721 "-lcn | -listcontainer");
722 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-v | -verbose",
724 fprintf(stderr, " %-39s\n" , "-db | -database host[:port]");
728 /* Show alias information */
730 static int show_has_aliases;
732 int show_alias_info(int argc, char **argv, void *hint)
734 if(!show_has_aliases++)
735 printf("Aliases: %s", argv[0]);
737 printf(", %s", argv[0]);
742 static char *states[] = {
749 static char *MacState(int state)
751 static char buf[BUFSIZ];
753 if (state < 0 || state > 3)
755 sprintf(buf, "Unknown (%d)", state);
758 return states[state];
761 /* Retrieve information about a host */
763 int store_host_info(int argc, char **argv, void *hint)
768 for(i=0; i<argc; i++)
769 nargv[i] = strdup(argv[i]);
774 void show_host_info(char **argv)
778 struct mqelem *elem = NULL;
781 printf("Machine: %s\n", argv[M_NAME]);
783 args[1] = argv[M_NAME];
784 show_has_aliases = 0;
785 stat = wrap_mr_query("get_hostalias", 2, args, show_alias_info, &elem);
788 if (stat != MR_NO_MATCH)
789 com_err(whoami, stat, "while getting aliases");
793 sprintf(tbuf, "%s %s", argv[M_OWNER_TYPE],
794 strcmp(argv[M_OWNER_TYPE], "NONE") ? argv[M_OWNER_NAME] : "");
795 printf("Address: %-16s Network: %-16s\n",
796 argv[M_ADDR], argv[M_SUBNET]);
797 printf("Owner: %-16s Use data: %s\n", tbuf, argv[M_INUSE]);
798 printf("Status: %-16s Changed: %s\n",
799 MacState(atoi(argv[M_STAT])), argv[M_STAT_CHNG]);
801 printf("Vendor: %-16s Location: %s\n", argv[M_VENDOR],
803 printf("Model: %-16s Contact: %s\n", argv[M_MODEL],
805 printf("OS: %-16s Billing Contact: %s\n", argv[M_OS],
806 argv[M_BILL_CONTACT]);
807 printf("\nOpt: %s\n", argv[M_USE]);
808 printf("\nAdm cmt: %s\n", argv[M_ACOMMENT]);
809 printf("Op cmt: %s\n", argv[M_OCOMMENT]);
811 printf("Created by %s on %s\n", argv[M_CREATOR], argv[M_CREATED]);
812 printf("Last mod by %s at %s with %s.\n", argv[M_MODBY], argv[M_MODTIME], argv[M_MODWITH]);
815 int show_machine_in_cluster(int argc, char **argv, void *hint)
817 printf("Machine: %-30s Cluster: %-30s\n", argv[0], argv[1]);
822 int show_machine_in_container(int argc, char **argv, void *hint)
824 printf("Machine: %-30s Container: %-25s\n", argv[0], argv[1]);
829 /* Parse a line of input, fetching a member. NULL is returned if a member
830 * is not found. ';' is a comment character.
833 struct owner_type *parse_member(char *s)
835 struct owner_type *m;
838 while (*s && isspace(*s))
841 while (*p && *p != '\n' && *p != ';')
843 if (isprint(*p) && !isspace(*p))
850 if (p == s || strlen(s) == 0)
853 if (!(m = malloc(sizeof(struct owner_type))))
856 if ((p = strchr(s, ':')))
860 if (!strcasecmp("user", s))
862 else if (!strcasecmp("list", s))
864 else if (!strcasecmp("kerberos", s))
865 m->type = M_KERBEROS;
866 else if (!strcasecmp("none", s))
874 m->name = strdup(m->name);
879 m->type = strcasecmp(s, "none") ? M_ANY : M_NONE;
884 struct string_list *add_to_string_list(struct string_list *old_list, char *s) {
885 struct string_list *new_list;
887 new_list = (struct string_list *)malloc(sizeof(struct string_list *));
888 new_list->next = old_list;
889 new_list->string = s;
894 int wrap_mr_query(char *handle, int argc, char **argv,
895 int (*callback)(int, char **, void *), void *callarg) {
897 print_query(handle, argc, argv);
899 return mr_query(handle, argc, argv, callback, callarg);
902 void print_query(char *query_name, int argc, char **argv) {
905 printf("qy %s", query_name);
906 for(cnt=0; cnt<argc; cnt++)
907 printf(" <%s>", argv[cnt]);