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;
42 /* It is important to membercmp that M_USER < M_LIST < M_STRING */
50 /* argument parsing macro */
51 #define argis(a, b) (!strcmp(*arg + 1, a) || !strcmp(*arg + 1, b))
53 /* flags from command line */
54 int info_flag, update_flag, create_flag, delete_flag, list_map_flag;
55 int update_map_flag, verbose, noauth;
57 struct string_list *alias_add_queue, *alias_remove_queue;
58 struct string_list *map_add_queue, *map_remove_queue;
60 char *hostname, *whoami;
62 char *newname, *address, *network, *h_status, *vendor, *model;
63 char *os, *location, *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 struct owner_type *parse_member(char *s);
72 struct string_list *add_to_string_list(struct string_list *old_list, char *s);
73 int wrap_mr_query(char *handle, int argc, char **argv,
74 int (*callback)(int, char **, void *), void *callarg);
75 void print_query(char *query_name, int argc, char **argv);
77 int main(int argc, char **argv)
83 /* clear all flags & lists */
84 info_flag = update_flag = create_flag = list_map_flag = update_map_flag = 0;
86 newname = address = network = h_status = vendor = model = NULL;
87 os = location = contact = adm_cmt = op_cmt = NULL;
89 alias_add_queue = alias_remove_queue = NULL;
90 map_add_queue = map_remove_queue = NULL;
95 /* parse args, building addlist, dellist, & synclist */
96 while (++arg - argv < argc)
100 if (argis("i", "info"))
102 else if (argis("C", "create"))
104 else if (argis("D", "delete"))
106 else if (argis("R", "rename")) {
107 if (arg - argv < argc - 1) {
114 else if (argis("A", "address")) {
115 if (arg - argv < argc - 1) {
122 else if (argis("O", "owner")) {
123 if (arg - argv < argc - 1) {
126 owner = parse_member(*arg);
130 else if (argis("N", "network")) {
131 if (arg - argv < argc - 1) {
138 else if (argis("S", "status")) {
139 if (arg - argv < argc - 1) {
147 len = strlen(h_status);
148 for(i = 0; i < len; i++) {
149 if(!isdigit(h_status[i])) {
150 printf("Error: status code %s is not numeric.\n", h_status);
157 else if (argis("V", "vendor")) {
158 if (arg - argv < argc - 1) {
165 else if (argis("M", "model")) {
166 if (arg - argv < argc - 1) {
173 else if (argis("o", "os")) {
174 if (arg - argv < argc - 1) {
181 else if (argis("L", "location")) {
182 if (arg - argv < argc - 1) {
189 else if (argis("c", "contact")) {
190 if (arg - argv < argc - 1) {
197 else if (argis("ac", "admcmt")) {
198 if (arg - argv < argc - 1) {
205 else if (argis("oc", "opcmt")) {
206 if (arg - argv < argc - 1) {
213 else if (argis("a", "aliasadd")) {
214 if (arg - argv < argc - 1) {
216 alias_add_queue=add_to_string_list(alias_add_queue, *arg);
220 else if (argis("d", "aliasdelete")) {
221 if (arg - argv < argc - 1) {
223 alias_remove_queue=add_to_string_list(alias_remove_queue, *arg);
227 else if (argis("am", "addmap")) {
228 if (arg - argv < argc - 1) {
230 map_add_queue=add_to_string_list(map_add_queue, *arg);
235 else if (argis("dm", "deletemap")) {
236 if (arg - argv < argc - 1) {
238 map_remove_queue=add_to_string_list(map_remove_queue, *arg);
243 else if (argis("lm", "listmap"))
245 else if (argis("n", "noauth"))
247 else if (argis("v", "verbose"))
249 else if (argis("db", "database"))
251 if (arg - argv < argc - 1)
262 else if (hostname == NULL)
267 if (hostname == NULL)
270 /* default to info_flag if nothing else was specified */
271 if(!(info_flag || update_flag || create_flag || \
272 delete_flag || list_map_flag || update_map_flag )) {
277 status = mrcl_connect(server, "stella", 2, !noauth);
278 if (status == MRCL_AUTH_ERROR)
280 com_err(whoami, 0, "Try the -noauth flag if you don't "
281 "need authentication.");
286 /* create if needed */
292 for (cnt = 0; cnt < 15; cnt++) {
296 argv[0] = canonicalize_hostname(strdup(hostname));
308 /* The use field always gets set to "0" */
325 argv[11] = owner->name;
331 status = wrap_mr_query("add_host", 14, argv, NULL, NULL);
332 if (owner->type != M_ANY || status != MR_USER)
337 status = wrap_mr_query("add_host", 14, argv, NULL, NULL);
341 argv[10] = "KERBEROS";
342 status = wrap_mr_query("add_host", 14, argv, NULL, NULL);
347 status = wrap_mr_query("add_host", 14, argv, NULL, NULL);
356 status = wrap_mr_query("add_host", 14, argv, NULL, NULL);
361 com_err(whoami, status, "while creating host.");
366 else if (update_flag)
372 args[0] = canonicalize_hostname(strdup(hostname));
373 args[1] = args[2] = args[3] = "*";
375 status = wrap_mr_query("get_host", 4, args, store_host_info, old_argv);
378 com_err(whoami, status, "while getting list information");
382 argv[1] = old_argv[0];
383 argv[2] = old_argv[1];
384 argv[3] = old_argv[2];
385 argv[4] = old_argv[3];
386 argv[5] = old_argv[4];
387 argv[6] = old_argv[5];
388 argv[7] = old_argv[6];
389 argv[8] = old_argv[7];
390 argv[9] = old_argv[9];
391 argv[10] = old_argv[10];
392 argv[11] = old_argv[11];
393 argv[12] = old_argv[12];
394 argv[13] = old_argv[13];
395 argv[14] = old_argv[14];
397 argv[0] = canonicalize_hostname(strdup(hostname));
399 argv[1] = canonicalize_hostname(strdup(newname));
423 argv[12] = owner->name;
429 status = wrap_mr_query("update_host", 15, argv, NULL, NULL);
430 if (owner->type != M_ANY || status != MR_USER)
435 status = wrap_mr_query("update_host", 15, argv, NULL, NULL);
439 argv[11] = "KERBEROS";
440 status = wrap_mr_query("update_host", 15, argv, NULL, NULL);
445 status = wrap_mr_query("update_host", 15, argv, NULL, NULL);
450 status = wrap_mr_query("update_host", 15, argv, NULL, NULL);
453 com_err(whoami, status, "while updating host.");
458 /* create aliases if necessary */
459 if (alias_add_queue) {
460 struct string_list *q = alias_add_queue;
463 char *alias = q->string;
466 args[0] = canonicalize_hostname(strdup(alias));
467 args[1] = canonicalize_hostname(strdup(hostname));
468 status = wrap_mr_query("add_hostalias", 2, args, NULL, NULL);
470 com_err(whoami, status, "while adding host alias");
478 /* delete aliases if necessary */
479 if (alias_remove_queue) {
480 struct string_list *q = alias_remove_queue;
483 char *alias = q->string;
486 args[0] = canonicalize_hostname(strdup(alias));
487 args[1] = canonicalize_hostname(strdup(hostname));
488 status = wrap_mr_query("delete_hostalias", 2, args, NULL, NULL);
490 com_err(whoami, status, "while deleting host alias");
498 /* create cluster mappings */
500 struct string_list *q = map_add_queue;
503 char *clustername = q->string;
506 args[0] = canonicalize_hostname(strdup(hostname));
507 args[1] = clustername;
508 status = wrap_mr_query("add_machine_to_cluster", 2, args, NULL, NULL);
510 com_err(whoami, status, "while adding cluster mapping");
518 /* delete cluster mappings */
519 if (map_remove_queue) {
520 struct string_list *q = map_remove_queue;
523 char *clustername = q->string;
526 args[0] = canonicalize_hostname(strdup(hostname));
527 args[1] = clustername;
528 status = wrap_mr_query("delete_machine_from_cluster", 2, args,
531 com_err(whoami, status, "while deleting cluster mapping");
539 /* display list info if requested to */
541 struct mqelem *elem = NULL;
545 args[0] = canonicalize_hostname(strdup(hostname));
546 args[1] = args[2] = args[3] = "*";
547 status = wrap_mr_query("get_host", 4, args, store_host_info, argv);
549 com_err(whoami, status, "while getting host information");
552 show_host_info(argv);
555 /* list cluster mappings if needed */
559 args[0] = canonicalize_hostname(strdup(hostname));
561 status = wrap_mr_query("get_machine_to_cluster_map", 2, args,
562 show_machine_in_cluster, NULL);
564 if (status != MR_NO_MATCH) {
565 com_err(whoami, status, "while getting cluster mappings");
573 argv[0] = canonicalize_hostname(strdup(hostname));
574 status = wrap_mr_query("delete_host", 1, argv, NULL, NULL);
576 com_err(whoami, status, "while deleting host");
583 exit(success ? 0 : 1);
586 void usage(char **argv)
588 #define USAGE_OPTIONS_FORMAT " %-39s%s\n"
589 fprintf(stderr, "Usage: %s hostname [options]\n", argv[0]);
590 fprintf(stderr, "Options are\n");
591 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-C | -create",
592 "-O | -owner owner");
593 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-D | -delete",
594 "-S | -status status");
595 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-R | -rename newname",
596 "-V | -vendor vendor");
597 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-a | -addalias alias",
598 "-M | -model model");
599 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-d | -deletealias alias",
600 "-L | -location location");
601 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-i | -info",
603 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-oc | -opcmt op_cmt",
604 "-c | -contact contact");
605 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-ac | -admcmt adm_cmt",
606 "-A | -address address");
607 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-am | -addmap cluster",
608 "-N | -network network");
609 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-dm | -deletemap cluster",
611 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-db | -database host[:port]",
613 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-v | -verbose",
618 /* Show alias information */
620 static int show_has_aliases;
622 int show_alias_info(int argc, char **argv, void *hint)
624 if(!show_has_aliases++)
625 printf("Aliases: %s", argv[0]);
627 printf(", %s", argv[0]);
632 static char *states[] = {
639 static char *MacState(int state)
641 static char buf[BUFSIZ];
643 if (state < 0 || state > 3)
645 sprintf(buf, "Unknown (%d)", state);
648 return states[state];
651 /* Retrieve information about a host */
653 int store_host_info(int argc, char **argv, void *hint)
658 for(i=0; i<argc; i++)
659 nargv[i] = strdup(argv[i]);
664 void show_host_info(char **argv)
668 struct mqelem *elem = NULL;
671 printf("Machine: %s\n", argv[M_NAME]);
673 args[1] = argv[M_NAME];
674 show_has_aliases = 0;
675 stat = wrap_mr_query("get_hostalias", 2, args, show_alias_info, &elem);
678 if (stat != MR_NO_MATCH)
679 com_err(whoami, stat, "while getting aliases");
683 sprintf(tbuf, "%s %s", argv[M_OWNER_TYPE],
684 strcmp(argv[M_OWNER_TYPE], "NONE") ? argv[M_OWNER_NAME] : "");
685 printf("Address: %-16s Network: %-16s\n",
686 argv[M_ADDR], argv[M_SUBNET]);
687 printf("Owner: %-16s Use data: %s\n", tbuf, argv[M_INUSE]);
688 printf("Status: %-16s Changed: %s\n",
689 MacState(atoi(argv[M_STAT])), argv[M_STAT_CHNG]);
692 printf("Vendor: %-16s Model: %-20s OS: %s\n",
693 argv[M_VENDOR], argv[M_MODEL], argv[M_OS]);
694 printf("Location: %-16s Contact: %-20s Opt: %s\n",
695 argv[M_LOC], argv[M_CONTACT], argv[M_USE]);
696 printf("\nAdm cmt: %s\n", argv[M_ACOMMENT]);
697 printf("Op cmt: %s\n", argv[M_OCOMMENT]);
699 printf("Created by %s on %s\n", argv[M_CREATOR], argv[M_CREATED]);
700 printf("Last mod by %s at %s with %s.\n", argv[M_MODBY], argv[M_MODTIME], argv[M_MODWITH]);
703 int show_machine_in_cluster(int argc, char **argv, void *hint)
705 printf("Machine: %-30s Cluster: %-30s\n", argv[0], argv[1]);
710 /* Parse a line of input, fetching a member. NULL is returned if a member
711 * is not found. ';' is a comment character.
714 struct owner_type *parse_member(char *s)
716 struct owner_type *m;
719 while (*s && isspace(*s))
722 while (*p && *p != '\n' && *p != ';')
724 if (isprint(*p) && !isspace(*p))
731 if (p == s || strlen(s) == 0)
734 if (!(m = malloc(sizeof(struct owner_type))))
737 if ((p = strchr(s, ':')))
741 if (!strcasecmp("user", s))
743 else if (!strcasecmp("list", s))
745 else if (!strcasecmp("string", s))
747 else if (!strcasecmp("kerberos", s))
748 m->type = M_KERBEROS;
749 else if (!strcasecmp("none", s))
757 m->name = strdup(m->name);
762 m->type = strcasecmp(s, "none") ? M_ANY : M_NONE;
767 struct string_list *add_to_string_list(struct string_list *old_list, char *s) {
768 struct string_list *new_list;
770 new_list = (struct string_list *)malloc(sizeof(struct string_list *));
771 new_list->next = old_list;
772 new_list->string = s;
777 int wrap_mr_query(char *handle, int argc, char **argv,
778 int (*callback)(int, char **, void *), void *callarg) {
780 print_query(handle, argc, argv);
782 return mr_query(handle, argc, argv, callback, callarg);
785 void print_query(char *query_name, int argc, char **argv) {
788 printf("qy %s", query_name);
789 for(cnt=0; cnt<argc; cnt++)
790 printf(" <%s>", argv[cnt]);