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;
55 struct string_list *alias_add_queue, *alias_remove_queue;
56 struct string_list *map_add_queue, *map_remove_queue;
58 char *hostname, *whoami;
60 char *newname, *address, *network, *h_status, *vendor, *model;
61 char *os, *location, *contact, *adm_cmt, *op_cmt;
63 struct owner_type *owner;
65 void usage(char **argv);
66 int store_host_info(int argc, char **argv, void *hint);
67 void show_host_info(char **argv);
68 int show_machine_in_cluster(int argc, char **argv, void *hint);
69 struct owner_type *parse_member(char *s);
70 struct string_list *add_to_string_list(struct string_list *old_list, char *s);
71 int wrap_mr_query(char *handle, int argc, char **argv,
72 int (*callback)(int, char **, void *), void *callarg);
73 void print_query(char *query_name, int argc, char **argv);
75 int main(int argc, char **argv)
81 /* clear all flags & lists */
82 info_flag = update_flag = create_flag = list_map_flag = update_map_flag = 0;
83 update_alias_flag = verbose = noauth = 0;
84 newname = address = network = h_status = vendor = model = NULL;
85 os = location = contact = adm_cmt = op_cmt = NULL;
87 alias_add_queue = alias_remove_queue = NULL;
88 map_add_queue = map_remove_queue = NULL;
93 /* parse args, building addlist, dellist, & synclist */
94 while (++arg - argv < argc)
98 if (argis("i", "info"))
100 else if (argis("C", "create"))
102 else if (argis("D", "delete"))
104 else if (argis("R", "rename")) {
105 if (arg - argv < argc - 1) {
112 else if (argis("A", "address")) {
113 if (arg - argv < argc - 1) {
120 else if (argis("O", "owner")) {
121 if (arg - argv < argc - 1) {
124 owner = parse_member(*arg);
128 else if (argis("N", "network")) {
129 if (arg - argv < argc - 1) {
136 else if (argis("S", "status")) {
137 if (arg - argv < argc - 1) {
145 len = strlen(h_status);
146 for(i = 0; i < len; i++) {
147 if(!isdigit(h_status[i])) {
148 printf("Error: status code %s is not numeric.\n", h_status);
155 else if (argis("V", "vendor")) {
156 if (arg - argv < argc - 1) {
163 else if (argis("M", "model")) {
164 if (arg - argv < argc - 1) {
171 else if (argis("o", "os")) {
172 if (arg - argv < argc - 1) {
179 else if (argis("L", "location")) {
180 if (arg - argv < argc - 1) {
187 else if (argis("c", "contact")) {
188 if (arg - argv < argc - 1) {
195 else if (argis("ac", "admcmt")) {
196 if (arg - argv < argc - 1) {
203 else if (argis("oc", "opcmt")) {
204 if (arg - argv < argc - 1) {
211 else if (argis("a", "aliasadd")) {
212 if (arg - argv < argc - 1) {
214 alias_add_queue=add_to_string_list(alias_add_queue, *arg);
219 else if (argis("d", "aliasdelete")) {
220 if (arg - argv < argc - 1) {
222 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 || \
273 update_alias_flag)) {
278 status = mrcl_connect(server, "stella", 2, !noauth);
279 if (status == MRCL_AUTH_ERROR)
281 com_err(whoami, 0, "Try the -noauth flag if you don't "
282 "need authentication.");
287 /* create if needed */
293 for (cnt = 0; cnt < 15; cnt++) {
297 argv[0] = canonicalize_hostname(strdup(hostname));
309 /* The use field always gets set to "0" */
328 argv[11] = owner->name;
334 status = wrap_mr_query("add_host", 14, argv, NULL, NULL);
335 if (owner->type != M_ANY || status != MR_USER)
340 status = wrap_mr_query("add_host", 14, argv, NULL, NULL);
344 argv[10] = "KERBEROS";
345 status = wrap_mr_query("add_host", 14, argv, NULL, NULL);
350 status = wrap_mr_query("add_host", 14, argv, NULL, NULL);
359 status = wrap_mr_query("add_host", 14, argv, NULL, NULL);
364 com_err(whoami, status, "while creating host.");
369 else if (update_flag)
375 args[0] = canonicalize_hostname(strdup(hostname));
376 args[1] = args[2] = args[3] = "*";
378 status = wrap_mr_query("get_host", 4, args, store_host_info, old_argv);
381 com_err(whoami, status, "while getting list information");
385 argv[1] = old_argv[0];
386 argv[2] = old_argv[1];
387 argv[3] = old_argv[2];
388 argv[4] = old_argv[3];
389 argv[5] = old_argv[4];
390 argv[6] = old_argv[5];
391 argv[7] = old_argv[6];
392 argv[8] = old_argv[7];
393 argv[9] = old_argv[9];
394 argv[10] = old_argv[10];
395 argv[11] = old_argv[11];
396 argv[12] = old_argv[12];
397 argv[13] = old_argv[13];
398 argv[14] = old_argv[14];
400 argv[0] = canonicalize_hostname(strdup(hostname));
402 argv[1] = canonicalize_hostname(strdup(newname));
426 argv[12] = owner->name;
432 status = wrap_mr_query("update_host", 15, argv, NULL, NULL);
433 if (owner->type != M_ANY || status != MR_USER)
438 status = wrap_mr_query("update_host", 15, argv, NULL, NULL);
442 argv[11] = "KERBEROS";
443 status = wrap_mr_query("update_host", 15, argv, NULL, NULL);
448 status = wrap_mr_query("update_host", 15, argv, NULL, NULL);
453 status = wrap_mr_query("update_host", 15, argv, NULL, NULL);
456 com_err(whoami, status, "while updating host.");
461 /* create aliases if necessary */
462 if (alias_add_queue) {
463 struct string_list *q = alias_add_queue;
466 char *alias = q->string;
469 args[0] = partial_canonicalize_hostname(strdup(alias));
470 args[1] = canonicalize_hostname(strdup(hostname));
471 status = wrap_mr_query("add_hostalias", 2, args, NULL, NULL);
473 com_err(whoami, status, "while adding host alias");
481 /* delete aliases if necessary */
482 if (alias_remove_queue) {
483 struct string_list *q = alias_remove_queue;
486 char *alias = q->string;
489 args[0] = partial_canonicalize_hostname(strdup(alias));
490 args[1] = canonicalize_hostname(strdup(hostname));
491 status = wrap_mr_query("delete_hostalias", 2, args, NULL, NULL);
493 com_err(whoami, status, "while deleting host alias");
501 /* create cluster mappings */
503 struct string_list *q = map_add_queue;
506 char *clustername = q->string;
509 args[0] = canonicalize_hostname(strdup(hostname));
510 args[1] = clustername;
511 status = wrap_mr_query("add_machine_to_cluster", 2, args, NULL, NULL);
513 com_err(whoami, status, "while adding cluster mapping");
521 /* delete cluster mappings */
522 if (map_remove_queue) {
523 struct string_list *q = map_remove_queue;
526 char *clustername = q->string;
529 args[0] = canonicalize_hostname(strdup(hostname));
530 args[1] = clustername;
531 status = wrap_mr_query("delete_machine_from_cluster", 2, args,
534 com_err(whoami, status, "while deleting cluster mapping");
542 /* display list info if requested to */
544 struct mqelem *elem = NULL;
548 args[0] = canonicalize_hostname(strdup(hostname));
549 args[1] = args[2] = args[3] = "*";
550 status = wrap_mr_query("get_host", 4, args, store_host_info, argv);
552 com_err(whoami, status, "while getting host information");
555 show_host_info(argv);
558 /* list cluster mappings if needed */
562 args[0] = canonicalize_hostname(strdup(hostname));
564 status = wrap_mr_query("get_machine_to_cluster_map", 2, args,
565 show_machine_in_cluster, NULL);
567 if (status != MR_NO_MATCH) {
568 com_err(whoami, status, "while getting cluster mappings");
576 argv[0] = canonicalize_hostname(strdup(hostname));
577 status = wrap_mr_query("delete_host", 1, argv, NULL, NULL);
579 com_err(whoami, status, "while deleting host");
586 exit(success ? 0 : 1);
589 void usage(char **argv)
591 #define USAGE_OPTIONS_FORMAT " %-39s%s\n"
592 fprintf(stderr, "Usage: %s hostname [options]\n", argv[0]);
593 fprintf(stderr, "Options are\n");
594 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-C | -create",
595 "-O | -owner owner");
596 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-D | -delete",
597 "-S | -status status");
598 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-R | -rename newname",
599 "-V | -vendor vendor");
600 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-a | -addalias alias",
601 "-M | -model model");
602 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-d | -deletealias alias",
603 "-L | -location location");
604 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-i | -info",
606 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-oc | -opcmt op_cmt",
607 "-c | -contact contact");
608 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-ac | -admcmt adm_cmt",
609 "-A | -address address");
610 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-am | -addmap cluster",
611 "-N | -network network");
612 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-dm | -deletemap cluster",
614 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-db | -database host[:port]",
616 fprintf(stderr, USAGE_OPTIONS_FORMAT, "-v | -verbose",
621 /* Show alias information */
623 static int show_has_aliases;
625 int show_alias_info(int argc, char **argv, void *hint)
627 if(!show_has_aliases++)
628 printf("Aliases: %s", argv[0]);
630 printf(", %s", argv[0]);
635 static char *states[] = {
642 static char *MacState(int state)
644 static char buf[BUFSIZ];
646 if (state < 0 || state > 3)
648 sprintf(buf, "Unknown (%d)", state);
651 return states[state];
654 /* Retrieve information about a host */
656 int store_host_info(int argc, char **argv, void *hint)
661 for(i=0; i<argc; i++)
662 nargv[i] = strdup(argv[i]);
667 void show_host_info(char **argv)
671 struct mqelem *elem = NULL;
674 printf("Machine: %s\n", argv[M_NAME]);
676 args[1] = argv[M_NAME];
677 show_has_aliases = 0;
678 stat = wrap_mr_query("get_hostalias", 2, args, show_alias_info, &elem);
681 if (stat != MR_NO_MATCH)
682 com_err(whoami, stat, "while getting aliases");
686 sprintf(tbuf, "%s %s", argv[M_OWNER_TYPE],
687 strcmp(argv[M_OWNER_TYPE], "NONE") ? argv[M_OWNER_NAME] : "");
688 printf("Address: %-16s Network: %-16s\n",
689 argv[M_ADDR], argv[M_SUBNET]);
690 printf("Owner: %-16s Use data: %s\n", tbuf, argv[M_INUSE]);
691 printf("Status: %-16s Changed: %s\n",
692 MacState(atoi(argv[M_STAT])), argv[M_STAT_CHNG]);
695 printf("Vendor: %-16s Model: %-20s OS: %s\n",
696 argv[M_VENDOR], argv[M_MODEL], argv[M_OS]);
697 printf("Location: %-16s Contact: %-20s Opt: %s\n",
698 argv[M_LOC], argv[M_CONTACT], argv[M_USE]);
699 printf("\nAdm cmt: %s\n", argv[M_ACOMMENT]);
700 printf("Op cmt: %s\n", argv[M_OCOMMENT]);
702 printf("Created by %s on %s\n", argv[M_CREATOR], argv[M_CREATED]);
703 printf("Last mod by %s at %s with %s.\n", argv[M_MODBY], argv[M_MODTIME], argv[M_MODWITH]);
706 int show_machine_in_cluster(int argc, char **argv, void *hint)
708 printf("Machine: %-30s Cluster: %-30s\n", argv[0], argv[1]);
713 /* Parse a line of input, fetching a member. NULL is returned if a member
714 * is not found. ';' is a comment character.
717 struct owner_type *parse_member(char *s)
719 struct owner_type *m;
722 while (*s && isspace(*s))
725 while (*p && *p != '\n' && *p != ';')
727 if (isprint(*p) && !isspace(*p))
734 if (p == s || strlen(s) == 0)
737 if (!(m = malloc(sizeof(struct owner_type))))
740 if ((p = strchr(s, ':')))
744 if (!strcasecmp("user", s))
746 else if (!strcasecmp("list", s))
748 else if (!strcasecmp("kerberos", s))
749 m->type = M_KERBEROS;
750 else if (!strcasecmp("none", s))
758 m->name = strdup(m->name);
763 m->type = strcasecmp(s, "none") ? M_ANY : M_NONE;
768 struct string_list *add_to_string_list(struct string_list *old_list, char *s) {
769 struct string_list *new_list;
771 new_list = (struct string_list *)malloc(sizeof(struct string_list *));
772 new_list->next = old_list;
773 new_list->string = s;
778 int wrap_mr_query(char *handle, int argc, char **argv,
779 int (*callback)(int, char **, void *), void *callarg) {
781 print_query(handle, argc, argv);
783 return mr_query(handle, argc, argv, callback, callarg);
786 void print_query(char *query_name, int argc, char **argv) {
789 printf("qy %s", query_name);
790 for(cnt=0; cnt<argc; cnt++)
791 printf(" <%s>", argv[cnt]);