2 * Command line oriented Moira host tool.
4 * kolya@MIT.EDU, January 2000
6 * Somewhat based on blanche
8 * Copyright (C) 2000, 2001 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>
25 typedef unsigned long in_addr_t;
27 #include <sys/types.h>
28 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <arpa/inet.h>
42 struct mqelem *q_forw;
43 struct mqelem *q_back;
49 struct string_list *next;
58 /* argument parsing macro */
59 #define argis(a, b) (!strcmp(*arg + 1, a) || !strcmp(*arg + 1, b))
61 /* flags from command line */
62 int info_flag, update_flag, create_flag, delete_flag, list_map_flag;
63 int update_alias_flag, update_map_flag, verbose, noauth;
64 int list_container_flag, update_container_flag, unformatted_flag;
66 struct string_list *alias_add_queue, *alias_remove_queue;
67 struct string_list *map_add_queue, *map_remove_queue;
68 struct string_list *container_add_queue, *container_remove_queue;
70 char *hostname, *whoami;
72 char *newname, *address, *network, *h_status, *vendor, *model;
73 char *os, *location, *contact, *billing_contact, *account_number;
74 char *adm_cmt, *op_cmt;
77 struct owner_type *owner;
79 void usage(char **argv);
80 int store_host_info(int argc, char **argv, void *hint);
81 void show_host_info(char **argv);
82 void show_host_info_unformatted(char **argv);
83 int show_machine_in_cluster(int argc, char **argv, void *hint);
84 int show_machine_in_container(int argc, char **argv, void *hint);
85 struct owner_type *parse_member(char *s);
86 struct string_list *add_to_string_list(struct string_list *old_list, char *s);
87 int wrap_mr_query(char *handle, int argc, char **argv,
88 int (*callback)(int, char **, void *), void *callarg);
89 void print_query(char *query_name, int argc, char **argv);
91 int main(int argc, char **argv)
97 /* clear all flags & lists */
98 info_flag = update_flag = create_flag = list_map_flag = update_map_flag = 0;
99 update_alias_flag = verbose = noauth = 0;
100 list_container_flag = update_container_flag = 0;
101 newname = address = network = h_status = vendor = model = NULL;
102 os = location = contact = billing_contact = account_number = adm_cmt = NULL;
105 alias_add_queue = alias_remove_queue = NULL;
106 map_add_queue = map_remove_queue = NULL;
107 container_add_queue = container_remove_queue = NULL;
112 /* parse args, building addlist, dellist, & synclist */
113 while (++arg - argv < argc)
117 if (argis("i", "info"))
119 else if (argis("C", "create"))
121 else if (argis("D", "delete"))
123 else if (argis("R", "rename")) {
124 if (arg - argv < argc - 1) {
131 else if (argis("A", "address")) {
132 if (arg - argv < argc - 1) {
139 else if (argis("O", "owner")) {
140 if (arg - argv < argc - 1) {
143 owner = parse_member(*arg);
147 else if (argis("N", "network")) {
148 if (arg - argv < argc - 1) {
155 else if (argis("S", "status")) {
156 if (arg - argv < argc - 1) {
164 len = strlen(h_status);
165 for(i = 0; i < len; i++) {
166 if(!isdigit(h_status[i])) {
167 printf("Error: status code %s is not numeric.\n", h_status);
174 else if (argis("V", "vendor")) {
175 if (arg - argv < argc - 1) {
182 else if (argis("M", "model")) {
183 if (arg - argv < argc - 1) {
190 else if (argis("o", "os")) {
191 if (arg - argv < argc - 1) {
198 else if (argis("L", "location")) {
199 if (arg - argv < argc - 1) {
206 else if (argis("c", "contact")) {
207 if (arg - argv < argc - 1) {
214 else if (argis("bc", "billingcontact")) {
215 if (arg - argv < argc - 1) {
218 billing_contact = *arg;
222 else if (argis("an", "accountnumber")) {
223 if (arg - argv < argc - 1) {
226 account_number = *arg;
230 else if (argis("ac", "admcmt")) {
231 if (arg - argv < argc - 1) {
238 else if (argis("oc", "opcmt")) {
239 if (arg - argv < argc - 1) {
246 else if (argis("a", "aliasadd")) {
247 if (arg - argv < argc - 1) {
249 alias_add_queue=add_to_string_list(alias_add_queue, *arg);
254 else if (argis("d", "aliasdelete")) {
255 if (arg - argv < argc - 1) {
257 alias_remove_queue=add_to_string_list(alias_remove_queue, *arg);
262 else if (argis("am", "addmap")) {
263 if (arg - argv < argc - 1) {
265 map_add_queue=add_to_string_list(map_add_queue, *arg);
270 else if (argis("dm", "deletemap")) {
271 if (arg - argv < argc - 1) {
273 map_remove_queue=add_to_string_list(map_remove_queue, *arg);
278 else if (argis("lm", "listmap"))
280 else if (argis("acn", "addcontainer")) {
281 if (arg - argv < argc - 1) {
283 container_add_queue =
284 add_to_string_list(container_add_queue, *arg);
287 update_container_flag++;
289 else if (argis("dcn", "deletecontainer")) {
290 if (arg - argv < argc - 1) {
292 container_remove_queue =
293 add_to_string_list(container_remove_queue, *arg);
296 update_container_flag++;
298 else if (argis("lcn", "listcontainer"))
299 list_container_flag++;
300 else if (argis("u", "unformatted"))
302 else if (argis("n", "noauth"))
304 else if (argis("v", "verbose"))
306 else if (argis("db", "database"))
308 if (arg - argv < argc - 1)
319 else if (hostname == NULL)
324 if (hostname == NULL)
327 /* default to info_flag if nothing else was specified */
328 if(!(info_flag || update_flag || create_flag || \
329 delete_flag || list_map_flag || update_map_flag || \
330 update_alias_flag || update_container_flag || \
331 list_container_flag)) {
336 status = mrcl_connect(server, "stella", 8, !noauth);
337 if (status == MRCL_AUTH_ERROR)
339 com_err(whoami, 0, "Try the -noauth flag if you don't "
340 "need authentication.");
345 /* Perform the lookup by IP address if that's what we've been handed */
346 if ((ipaddress=inet_addr(hostname)) != -1) {
350 args[1] = strdup(hostname);
351 args[0] = args[2] = args[3] = "*";
352 status = wrap_mr_query("get_host", 4, args, store_host_info, argv);
355 com_err(whoami, status, "while looking up IP address.");
361 /* create if needed */
367 for (cnt = 0; cnt < 17; cnt++) {
371 argv[0] = canonicalize_hostname(strdup(hostname));
384 argv[6] = billing_contact;
386 argv[7] = account_number;
387 /* The use field always gets set to "0" */
406 argv[13] = owner->name;
412 status = wrap_mr_query("add_host", 16, argv, NULL, NULL);
413 if (owner->type != M_ANY || status != MR_USER)
418 status = wrap_mr_query("add_host", 16, argv, NULL, NULL);
422 argv[12] = "KERBEROS";
423 status = mrcl_validate_kerberos_member(argv[13], &argv[13]);
424 if (mrcl_get_message())
425 mrcl_com_err(whoami);
426 status = wrap_mr_query("add_host", 16, argv, NULL, NULL);
431 status = wrap_mr_query("add_host", 16, argv, NULL, NULL);
440 status = wrap_mr_query("add_host", 16, argv, NULL, NULL);
445 com_err(whoami, status, "while creating host.");
450 else if (update_flag)
456 args[0] = canonicalize_hostname(strdup(hostname));
457 args[1] = args[2] = args[3] = "*";
459 status = wrap_mr_query("get_host", 4, args, store_host_info, old_argv);
462 com_err(whoami, status, "while getting list information");
466 argv[1] = old_argv[0];
467 argv[2] = old_argv[1];
468 argv[3] = old_argv[2];
469 argv[4] = old_argv[3];
470 argv[5] = old_argv[4];
471 argv[6] = old_argv[5];
472 argv[7] = old_argv[6];
473 argv[8] = old_argv[7];
474 argv[9] = old_argv[8];
475 argv[10] = old_argv[9];
476 argv[11] = old_argv[11];
477 argv[12] = old_argv[12];
478 argv[13] = old_argv[13];
479 argv[14] = old_argv[14];
480 argv[15] = old_argv[15];
481 argv[16] = old_argv[16];
483 argv[0] = canonicalize_hostname(strdup(hostname));
485 argv[1] = canonicalize_hostname(strdup(newname));
497 argv[7] = billing_contact;
499 argv[8] = account_number;
513 argv[14] = owner->name;
519 status = wrap_mr_query("update_host", 17, argv, NULL, NULL);
520 if (owner->type != M_ANY || status != MR_USER)
525 status = wrap_mr_query("update_host", 17, argv, NULL, NULL);
529 argv[13] = "KERBEROS";
530 status = mrcl_validate_kerberos_member(argv[14], &argv[14]);
531 if (mrcl_get_message())
532 mrcl_com_err(whoami);
533 status = wrap_mr_query("update_host", 17, argv, NULL, NULL);
538 status = wrap_mr_query("update_host", 17, argv, NULL, NULL);
543 status = wrap_mr_query("update_host", 17, argv, NULL, NULL);
546 com_err(whoami, status, "while updating host.");
551 /* create aliases if necessary */
552 if (alias_add_queue) {
553 struct string_list *q = alias_add_queue;
556 char *alias = q->string;
559 args[0] = partial_canonicalize_hostname(strdup(alias));
560 args[1] = canonicalize_hostname(strdup(hostname));
561 status = wrap_mr_query("add_hostalias", 2, args, NULL, NULL);
563 com_err(whoami, status, "while adding host alias");
571 /* delete aliases if necessary */
572 if (alias_remove_queue) {
573 struct string_list *q = alias_remove_queue;
576 char *alias = q->string;
579 args[0] = partial_canonicalize_hostname(strdup(alias));
580 args[1] = canonicalize_hostname(strdup(hostname));
581 status = wrap_mr_query("delete_hostalias", 2, args, NULL, NULL);
583 com_err(whoami, status, "while deleting host alias");
591 /* create cluster mappings */
593 struct string_list *q = map_add_queue;
596 char *clustername = q->string;
599 args[0] = canonicalize_hostname(strdup(hostname));
600 args[1] = clustername;
601 status = wrap_mr_query("add_machine_to_cluster", 2, args, NULL, NULL);
603 com_err(whoami, status, "while adding cluster mapping");
611 /* delete cluster mappings */
612 if (map_remove_queue) {
613 struct string_list *q = map_remove_queue;
616 char *clustername = q->string;
619 args[0] = canonicalize_hostname(strdup(hostname));
620 args[1] = clustername;
621 status = wrap_mr_query("delete_machine_from_cluster", 2, args,
624 com_err(whoami, status, "while deleting cluster mapping");
632 /* add container mappings */
633 if (container_add_queue) {
634 struct string_list *q = container_add_queue;
637 char *containername = q->string;
640 args[0] = canonicalize_hostname(strdup(hostname));
641 args[1] = containername;
642 status = wrap_mr_query("add_machine_to_container", 2, args,
646 com_err(whoami, status, "while adding container mapping");
654 /* delete container mappings */
655 if (container_remove_queue) {
656 struct string_list *q = container_remove_queue;
659 char *containername = q->string;
662 args[0] = canonicalize_hostname(strdup(hostname));
663 args[1] = containername;
664 status = wrap_mr_query("delete_machine_from_container", 2, args,
668 com_err(whoami, status, "while deleting container mapping");
676 /* display list info if requested to */
678 struct mqelem *elem = NULL;
682 args[0] = canonicalize_hostname(strdup(hostname));
683 args[1] = args[2] = args[3] = "*";
684 status = wrap_mr_query("get_host", 4, args, store_host_info, argv);
686 com_err(whoami, status, "while getting host information");
689 if (unformatted_flag)
690 show_host_info_unformatted(argv);
692 show_host_info(argv);
693 args[0] = argv[M_SUBNET];
694 status = wrap_mr_query("get_subnet", 1, args, store_host_info, argv);
696 com_err(whoami, status, "while getting subnet information");
697 if (atoi(argv[SN_STATUS]) == SNET_STATUS_PRIVATE_10MBPS ||
698 atoi(argv[SN_STATUS]) == SNET_STATUS_PRIVATE_100MBPS ||
699 atoi(argv[SN_STATUS]) == SNET_STATUS_PRIVATE_1000MBPS)
701 fprintf(stderr, "\nWarning: This host is on a private subnet.\n");
702 fprintf(stderr, "Billing information shown is superseded by billing information for the subnet.\n");
706 /* list cluster mappings if needed */
710 args[0] = canonicalize_hostname(strdup(hostname));
712 status = wrap_mr_query("get_machine_to_cluster_map", 2, args,
713 show_machine_in_cluster, NULL);
715 if (status != MR_NO_MATCH) {
716 com_err(whoami, status, "while getting cluster mappings");
721 /* list container mappings if needed */
722 if (list_container_flag) {
725 argv[0] = canonicalize_hostname(strdup(hostname));
726 status = wrap_mr_query("get_machine_to_container_map", 1, argv,
727 show_machine_in_container, NULL);
730 if (status != MR_NO_MATCH) {
731 com_err(whoami, status, "while getting container mappings");
739 argv[0] = canonicalize_hostname(strdup(hostname));
740 status = wrap_mr_query("delete_host", 1, argv, NULL, NULL);
742 com_err(whoami, status, "while deleting host");
749 exit(success ? 0 : 1);
752 void usage(char **argv)
754 #define USAGE_OPTIONS_FORMAT " %-39s%s\n"
755 fprintf(stderr, "Usage: %s hostname [options]\n", argv[0]);
756 fprintf(stderr, "Options are\n");
757 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-C | -create",
758 "-O | -owner owner");
759 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-D | -delete",
760 "-S | -status status");
761 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-R | -rename newname",
762 "-V | -vendor vendor");
763 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-a | -addalias alias",
764 "-M | -model model");
765 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-d | -deletealias alias",
766 "-L | -location location");
767 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-i | -info",
769 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-oc | -opcmt op_cmt",
770 "-c | -contact contact");
771 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-ac | -admcmt adm_cmt",
772 "-bc | -billingcontact billing_contact");
773 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-an | -accountnumber account_number", "-A | -address address");
774 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-N | -network network",
775 "-am | -addmap cluster");
776 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-dm | deletemap cluster",
777 "-acn | -addcontainer container");
778 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-dcn | -deletecontainer container",
780 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-lcn | -listcontainer",
781 "-u | -unformatted");
782 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-v | -verbose",
784 fprintf(stderr, " %-39s\n" , "-db | -database host[:port]");
788 /* Show alias information */
790 static int show_has_aliases;
792 int show_alias_info(int argc, char **argv, void *hint)
794 if(!show_has_aliases++)
795 printf("Aliases: %s", argv[0]);
797 printf(", %s", argv[0]);
802 int show_alias_info_unformatted(int argc, char **argv, void *hint)
804 if(!show_has_aliases++)
805 printf("Alias: %s", argv[0]);
807 printf(", %s", argv[0]);
812 static char *states[] = {
819 static char *MacState(int state)
821 static char buf[BUFSIZ];
823 if (state < 0 || state > 3)
825 sprintf(buf, "Unknown (%d)", state);
828 return states[state];
831 /* Retrieve information about a host */
833 int store_host_info(int argc, char **argv, void *hint)
838 for(i=0; i<argc; i++)
839 nargv[i] = strdup(argv[i]);
844 void show_host_info(char **argv)
848 struct mqelem *elem = NULL;
851 printf("Machine: %s\n", argv[M_NAME]);
853 args[1] = argv[M_NAME];
854 show_has_aliases = 0;
855 stat = wrap_mr_query("get_hostalias", 2, args, show_alias_info, &elem);
858 if (stat != MR_NO_MATCH)
859 com_err(whoami, stat, "while getting aliases");
863 sprintf(tbuf, "%s %s", argv[M_OWNER_TYPE],
864 strcmp(argv[M_OWNER_TYPE], "NONE") ? argv[M_OWNER_NAME] : "");
865 printf("Address: %-16s Network: %-16s\n",
866 argv[M_ADDR], argv[M_SUBNET]);
867 printf("Owner: %-16s Use data: %s\n", tbuf, argv[M_INUSE]);
868 printf("Status: %-16s Changed: %s\n",
869 MacState(atoi(argv[M_STAT])), argv[M_STAT_CHNG]);
871 printf("Vendor: %-16s Location: %s\n", argv[M_VENDOR],
873 printf("Model: %-16s Contact: %s\n", argv[M_MODEL],
875 printf("OS: %-16s Billing Contact: %s\n", argv[M_OS],
876 argv[M_BILL_CONTACT]);
877 printf("Opt: %-16s Account Number: %s\n", argv[M_USE],
878 argv[M_ACCT_NUMBER]);
879 printf("\nAdm cmt: %s\n", argv[M_ACOMMENT]);
880 printf("Op cmt: %s\n", argv[M_OCOMMENT]);
882 printf("Created by %s on %s\n", argv[M_CREATOR], argv[M_CREATED]);
883 printf("Last mod by %s at %s with %s.\n", argv[M_MODBY], argv[M_MODTIME], argv[M_MODWITH]);
886 void show_host_info_unformatted(char **argv)
889 struct mqelem *elem = NULL;
892 printf("Machine: %s\n", argv[M_NAME]);
894 args[1] = argv[M_NAME];
895 show_has_aliases = 0;
896 stat = wrap_mr_query("get_hostalias", 2, args, show_alias_info_unformatted,
898 if (stat && stat != MR_NO_MATCH)
899 com_err(whoami, stat, "while getting aliases");
902 printf("Address: %s\n", argv[M_ADDR]);
903 printf("Network: %s\n", argv[M_SUBNET]);
904 printf("Owner Type: %s\n", argv[M_OWNER_TYPE]);
905 printf("Owner: %s\n", argv[M_OWNER_NAME]);
906 printf("Status: %s\n", MacState(atoi(argv[M_STAT])));
907 printf("Changed: %s\n", argv[M_STAT_CHNG]);
908 printf("Use data: %s\n", argv[M_INUSE]);
909 printf("Vendor: %s\n", argv[M_VENDOR]);
910 printf("Model: %s\n", argv[M_MODEL]);
911 printf("OS: %s\n", argv[M_OS]);
912 printf("Location: %s\n", argv[M_LOC]);
913 printf("Contact: %s\n", argv[M_CONTACT]);
914 printf("Billing Contact: %s\n", argv[M_BILL_CONTACT]);
915 printf("Account Number: %s\n", argv[M_ACCT_NUMBER]);
916 printf("Opt: %s\n", argv[M_USE]);
917 printf("Adm cmt: %s\n", argv[M_ACOMMENT]);
918 printf("Op cmt: %s\n", argv[M_OCOMMENT]);
919 printf("Created by: %s\n", argv[M_CREATOR]);
920 printf("Created on: %s\n", argv[M_CREATED]);
921 printf("Last mod by: %s\n", argv[M_MODBY]);
922 printf("Last mod on: %s\n", argv[M_MODTIME]);
923 printf("Last mod with: %s\n", argv[M_MODWITH]);
926 int show_machine_in_cluster(int argc, char **argv, void *hint)
928 printf("Machine: %-30s Cluster: %-30s\n", argv[0], argv[1]);
933 int show_machine_in_container(int argc, char **argv, void *hint)
935 printf("Machine: %-30s Container: %-25s\n", argv[0], argv[1]);
940 /* Parse a line of input, fetching a member. NULL is returned if a member
941 * is not found. ';' is a comment character.
944 struct owner_type *parse_member(char *s)
946 struct owner_type *m;
949 while (*s && isspace(*s))
952 while (*p && *p != '\n' && *p != ';')
954 if (isprint(*p) && !isspace(*p))
961 if (p == s || strlen(s) == 0)
964 if (!(m = malloc(sizeof(struct owner_type))))
967 if ((p = strchr(s, ':')))
971 if (!strcasecmp("user", s))
973 else if (!strcasecmp("list", s))
975 else if (!strcasecmp("kerberos", s))
976 m->type = M_KERBEROS;
977 else if (!strcasecmp("none", s))
985 m->name = strdup(m->name);
990 m->type = strcasecmp(s, "none") ? M_ANY : M_NONE;
995 struct string_list *add_to_string_list(struct string_list *old_list, char *s) {
996 struct string_list *new_list;
998 new_list = (struct string_list *)malloc(sizeof(struct string_list *));
999 new_list->next = old_list;
1000 new_list->string = s;
1005 int wrap_mr_query(char *handle, int argc, char **argv,
1006 int (*callback)(int, char **, void *), void *callarg) {
1008 print_query(handle, argc, argv);
1010 return mr_query(handle, argc, argv, callback, callarg);
1013 void print_query(char *query_name, int argc, char **argv) {
1016 printf("qy %s", query_name);
1017 for(cnt=0; cnt<argc; cnt++)
1018 printf(" <%s>", argv[cnt]);