3 * This is the file cluster.c for the Moira Client, which allows users
4 * to quickly and easily maintain most parts of the Moira database.
8 * By: Chris D. Peterson
10 * Copyright (C) 1988-1998 by the Massachusetts Institute of Technology.
11 * For copying and distribution information, please see the file
15 /* BTW: for anyone who cares MCD is short for Machine, Cluster, Data. */
17 #include <mit-copyright.h>
19 #include <moira_site.h>
26 #include <sys/types.h>
29 #include <sys/utsname.h>
33 #include <netinet/in.h>
34 #include <arpa/inet.h>
43 void PrintAliases(char **info);
44 static void PrintMachine(char **info);
45 struct mqelem *GetMCInfo(int type, char *name1, char *name2);
46 struct mqelem *GetMachineByOwner(char *type, char *name);
47 char **AskMCDInfo(char **info, int type, Bool name);
48 int CheckAndRemoveFromCluster(char *name, Bool ask_user);
49 int CheckAndRemoveCnames(char *name, Bool ask_user);
50 int CheckAndRemoveMachines(char *name, Bool ask_first);
52 static char *PrintContainerInfo(char **info);
53 static void RealUpdateContainer(char **info, Bool junk);
54 static void RealDeleteContainer(char **info, Bool one_container);
55 static void RealRemoveMachineFromContainer(char **info, Bool one_contmap);
66 #define M_DEFAULT_TYPE DEFAULT_NONE
68 #define C_DEFAULT_DESCRIPT DEFAULT_NONE
69 #define C_DEFAULT_LOCATION DEFAULT_NONE
71 #define CON_DEFAULT_TYPE DEFAULT_NONE
73 #define CD_DEFAULT_LABEL DEFAULT_NONE
74 #define CD_DEFAULT_DATA DEFAULT_NONE
76 #define S_DEFAULT_LOW "18.0.0.20"
77 #define S_DEFAULT_HIGH "18.0.2.249"
79 static char *states[] = {
86 static char *subnet_states[] = {
94 static char *MacState(int state)
96 static char buf[BUFSIZ];
98 if (state < 0 || state > 3)
100 sprintf(buf, "Unknown (%d)", state);
103 return states[state];
106 static char *SubnetState(int state)
108 static char buf[BUFSIZ];
110 if (state < 0 || state > 4)
112 sprintf(buf, "Unknown (%d)", state);
115 return subnet_states[state];
118 /* -------------------- Set Defaults -------------------- */
120 /* Function Name: SetMachineDefaults
121 * Description: sets machine defaults.
122 * Arguments: info - an array to put the defaults into.
123 * name - Canonacalized name of the machine.
124 * Returns: info - the array.
127 static char **SetMachineDefaults(char **info, char *name)
129 info[M_NAME] = strdup(name);
130 info[M_VENDOR] = strdup(M_DEFAULT_TYPE);
131 info[M_MODEL] = strdup(M_DEFAULT_TYPE);
132 info[M_OS] = strdup(M_DEFAULT_TYPE);
133 info[M_LOC] = strdup(M_DEFAULT_TYPE);
134 info[M_CONTACT] = strdup(M_DEFAULT_TYPE);
135 info[M_BILL_CONTACT] = strdup(M_DEFAULT_TYPE);
136 info[M_ACCT_NUMBER] = strdup("");
137 info[M_USE] = strdup("0");
138 info[M_STAT] = strdup("1");
139 info[M_SUBNET] = strdup("NONE");
140 info[M_ADDR] = strdup("unique");
141 info[M_OWNER_TYPE] = strdup("NONE");
142 info[M_OWNER_NAME] = strdup("NONE");
143 info[M_ACOMMENT] = strdup("");
144 info[M_OCOMMENT] = strdup("");
145 info[17] = info[18] = NULL;
149 /* Function Name: SetClusterDefaults
150 * Description: sets Cluster defaults.
151 * Arguments: info - an array to put the defaults into.
152 * name - name of the Cluster.
153 * Returns: info - the array.
156 static char **SetClusterDefaults(char **info, char *name)
158 info[C_NAME] = strdup(name);
159 info[C_DESCRIPT] = strdup(C_DEFAULT_DESCRIPT);
160 info[C_LOCATION] = strdup(C_DEFAULT_LOCATION);
161 info[C_MODBY] = info[C_MODTIME] = info[C_MODWITH] = info[C_END] = NULL;
165 static char **SetContainerDefaults(char **info, char *name)
167 info[CON_NAME] = strdup(name);
168 info[CON_DESCRIPT] = strdup(CON_DEFAULT_TYPE);
169 info[CON_LOCATION] = strdup(CON_DEFAULT_TYPE);
170 info[CON_CONTACT] = strdup(CON_DEFAULT_TYPE);
171 info[CON_OWNER_TYPE] = strdup("NONE");
172 info[CON_OWNER_NAME] = strdup("NONE");
173 info[CON_MEMACE_TYPE] = strdup("NONE");
174 info[CON_MEMACE_NAME] = strdup("NONE");
175 info[CON_MODBY] = info[CON_MODTIME] = info[CON_MODWITH] = NULL;
176 info[CON_END] = NULL;
180 /* Function Name: SetSubnetDefaults
181 * Description: sets Subnet defaults.
182 * Arguments: info - an array to put the defaults into.
183 * name - name of the Subnet.
184 * Returns: info - the array.
187 static char **SetSubnetDefaults(char **info, char *name)
191 info[SN_NAME] = strdup(name);
192 info[SN_DESC] = strdup("");
193 info[SN_STATUS] = strdup("1");
194 info[SN_CONTACT] = strdup(DEFAULT_NONE);
195 info[SN_ACCT_NUMBER] = strdup("");
196 sprintf(buf, "%ld", ntohl(inet_addr("18.255.0.0")));
197 info[SN_ADDRESS] = strdup(buf);
198 sprintf(buf, "%ld", ntohl(inet_addr("255.255.0.0")));
199 info[SN_MASK] = strdup(buf);
200 sprintf(buf, "%ld", ntohl(inet_addr(S_DEFAULT_LOW)));
201 info[SN_LOW] = strdup(buf);
202 sprintf(buf, "%ld", ntohl(inet_addr(S_DEFAULT_HIGH)));
203 info[SN_HIGH] = strdup(buf);
204 info[SN_PREFIX] = strdup("");
205 info[SN_ACE_TYPE] = strdup("LIST");
206 info[SN_ACE_NAME] = strdup("network");
207 info[SN_MODBY] = info[SN_MODTIME] = info[SN_MODWITH] = info[SN_END] = NULL;
211 /* -------------------- General Functions -------------------- */
213 static char aliasbuf[256];
215 void PrintAliases(char **info)
217 if (strlen(aliasbuf) == 0)
218 sprintf(aliasbuf, "Aliases: %s", info[0]);
221 strcat(aliasbuf, ", ");
222 strcat(aliasbuf, info[0]);
227 /* Function Name: PrintMachInfo
228 * Description: This function Prints out the Machine info in
230 * Arguments: info - array of information about a machine.
231 * Returns: The name of the Machine
234 static char *PrintMachInfo(char **info)
236 char buf[BUFSIZ], tbuf[256];
238 struct mqelem *elem = NULL;
242 sprintf(buf, "Machine: %s", info[M_NAME]);
245 args[1] = info[M_NAME];
246 if ((stat = do_mr_query("get_hostalias", 2, args, StoreInfo, &elem)))
248 if (stat != MR_NO_MATCH)
249 com_err(program_name, stat, " looking up aliases");
254 Loop(QueueTop(elem), (void (*)(char **)) PrintAliases);
256 Put_message(aliasbuf);
258 sprintf(tbuf, "%s %s", info[M_OWNER_TYPE],
259 strcmp(info[M_OWNER_TYPE], "NONE") ? info[M_OWNER_NAME] : "");
260 sprintf(buf, "Address: %-16s Network: %-16s",
261 info[M_ADDR], info[M_SUBNET]);
263 sprintf(buf, "Owner: %-16s Use data: %s", tbuf, info[M_INUSE]);
265 sprintf(buf, "Status: %-16s Changed: %s",
266 MacState(atoi(info[M_STAT])), info[M_STAT_CHNG]);
269 sprintf(buf, "Vendor: %-16s Location: %s", info[M_VENDOR],
272 sprintf(buf, "Model: %-16s Contact: %s", info[M_MODEL],
275 sprintf(buf, "OS: %-16s Billing Contact: %s", info[M_OS],
276 info[M_BILL_CONTACT]);
278 sprintf(buf, "Opt: %-16s Account Number: %s", info[M_USE],
279 info[M_ACCT_NUMBER]);
282 sprintf(buf, "Adm cmt: %s", info[M_ACOMMENT]);
284 sprintf(buf, "Op cmt: %s", info[M_OCOMMENT]);
287 sprintf(buf, "Created by %s on %s", info[M_CREATOR], info[M_CREATED]);
289 sprintf(buf, MOD_FORMAT, info[M_MODBY], info[M_MODTIME], info[M_MODWITH]);
291 /* Do a get_subnet for the machine's subnet. We need to know if it's
294 args[0] = info[M_SUBNET];
295 stat = do_mr_query("get_subnet", 1, args, StoreInfo, &elem);
297 com_err(program_name, stat, " looking up subnet info");
298 if (atoi(((char **)elem->q_data)[2]) == SNET_STATUS_PRIVATE)
301 sprintf(buf, "Warning: This host is on a private subnet.");
303 sprintf(buf, "Billing information shown is superceded by billing information for the subnet.");
310 /* Function Name: PrintMachine
311 * Description: Prints the name of a machine record
312 * Arguments: info - array of information about the machine.
316 static void PrintMachine(char **info)
320 sprintf(buf, "Machine: %s", info[M_NAME]);
324 /* Function Name: PrintCname
325 * Description: Prints the Data on a host alias
326 * Arguments: info a pointer to the data array.
327 * Returns: The name of the alias.
330 static char *PrintCname(char **info)
334 sprintf(buf, "Alias: %-32s Canonical Name: %s", info[0], info[1]);
339 /* Function Name: PrintClusterInfo
340 * Description: This function Prints out the cluster info
341 * in a coherent form.
342 * Arguments: info - array of information about a cluster.
343 * Returns: The name of the cluster.
346 static char *PrintClusterInfo(char **info)
351 sprintf(buf, "Cluster: %s", info[C_NAME]);
353 sprintf(buf, "Description: %s", info[C_DESCRIPT]);
355 sprintf(buf, "Location: %s", info[C_LOCATION]);
357 sprintf(buf, MOD_FORMAT, info[C_MODBY], info[C_MODTIME], info[C_MODWITH]);
362 /* Function Name: PrintClusterData
363 * Description: Prints the Data on a cluster
364 * Arguments: info a pointer to the data array.
365 * Returns: The name of the cluster.
368 static char *PrintClusterData(char **info)
373 sprintf(buf, "Cluster: %-20s Label: %-15s Data: %s",
374 info[CD_NAME], info[CD_LABEL], info[CD_DATA]);
376 return info[CD_NAME];
379 /* Function Name: PrintMCMap
380 * Description: Prints the data about a machine to cluster mapping.
381 * Arguments: info a pointer to the data array.
385 static char *PrintMCMap(char **info)
388 sprintf(buf, "Cluster: %-30s Machine: %-20s",
389 info[MAP_CLUSTER], info[MAP_MACHINE]);
391 return ""; /* Used by QueryLoop(). */
394 /* Function Name: PrintSubnetInfo
395 * Description: This function Prints out the subnet info
396 * in a coherent form.
397 * Arguments: info - array of information about a subnet.
398 * Returns: The name of the subnet.
401 static char *PrintSubnetInfo(char **info)
404 struct in_addr addr, mask, low, high;
407 sprintf(buf, " Network: %s", info[SN_NAME]);
409 sprintf(buf, " Description: %s", info[SN_DESC]);
411 sprintf(buf, " Status: %s", SubnetState(atoi(info[SN_STATUS])));
413 sprintf(buf, " Contact: %s", info[SN_CONTACT]);
415 sprintf(buf, " Account Number: %s", info[SN_ACCT_NUMBER]);
417 addr.s_addr = htonl(atoi(info[SN_ADDRESS]));
418 mask.s_addr = htonl(atoi(info[SN_MASK]));
419 low.s_addr = htonl(atoi(info[SN_LOW]));
420 high.s_addr = htonl(atoi(info[SN_HIGH]));
421 /* screwy sequence is here because inet_ntoa returns a pointer to
422 a static buf. If it were all one sprintf, the last value would
424 sprintf(buf, " Address: %s Mask: ", inet_ntoa(addr));
425 strcat(buf, inet_ntoa(mask));
426 strcat(buf, "\n High: ");
427 strcat(buf, inet_ntoa(high));
428 strcat(buf, " Low: ");
429 strcat(buf, inet_ntoa(low));
431 sprintf(buf, "Hostname prefix: %s", info[SN_PREFIX]);
433 sprintf(buf, " Owner: %s %s\n", info[SN_ACE_TYPE],
434 strcmp(info[SN_ACE_TYPE], "NONE") ? info[SN_ACE_NAME] : "");
436 sprintf(buf, MOD_FORMAT, info[SN_MODBY], info[SN_MODTIME], info[SN_MODWITH]);
438 return info[SN_NAME];
441 /* Function Name: GetMCInfo.
442 * Description: This function stores info about a machine.
443 * type - type of data we are trying to retrieve.
444 * name1 - the name of argv[0] for the call.
445 * name2 - the name of argv[1] for the call.
446 * Returns: the top element of a queue containing the data or NULL.
449 struct mqelem *GetMCInfo(int type, char *name1, char *name2)
452 struct mqelem *elem = NULL;
459 args[1] = args[2] = args[3] = "*";
460 if ((stat = do_mr_query("get_host", 4, args, StoreInfo, &elem)))
462 if (stat == MR_NO_MATCH)
465 sprintf(buf, "Machine '%s' is not in the database.", name1);
469 com_err(program_name, stat, " in get_machine.");
476 if ((stat = do_mr_query("get_hostalias", 2, args, StoreInfo, &elem)))
478 com_err(program_name, stat, " in get_hostalias.");
483 if ((stat = do_mr_query("get_subnet", 1, &name1, StoreInfo, &elem)))
485 if (stat == MR_NO_MATCH)
488 sprintf(buf, "Network '%s' is not in the database.", name1);
492 com_err(program_name, stat, " in get_subnet.");
497 if ((stat = do_mr_query("get_cluster", 1, &name1, StoreInfo, &elem)))
499 com_err(program_name, stat, " in get_cluster.");
504 args[MAP_MACHINE] = name1;
505 args[MAP_CLUSTER] = name2;
506 if ((stat = do_mr_query("get_machine_to_cluster_map", 2, args,
509 com_err(program_name, stat, " in get_machine_to_cluster_map.");
514 args[CD_NAME] = name1;
515 args[CD_LABEL] = name2;
516 if ((stat = do_mr_query("get_cluster_data", 2, args, StoreInfo, &elem)))
518 com_err(program_name, stat, " in get_cluster_data.");
523 args[CON_NAME] = name1;
524 if ((stat = do_mr_query("get_container", 1, &name1, StoreInfo, &elem)))
526 com_err(program_name, stat, " in get_container.");
532 if ((stat = do_mr_query("get_machine_to_container_map", 1, &name1,
535 com_err(program_name, stat, " in get_machine_to_container_map.");
539 return QueueTop(elem);
542 /* Function Name: AskMCDInfo.
543 * Description: This function askes the user for information about a
544 * machine and saves it into a structure.
545 * Arguments: info - a pointer the information to ask about
546 * type - type of information - MACHINE
550 * name - T/F : change the name of this type.
554 char **AskMCDInfo(char **info, int type, Bool name)
556 char temp_buf[BUFSIZ], *newname, *oldnewname;
561 sprintf(temp_buf, "\nSetting the information for the Machine %s...",
565 sprintf(temp_buf, "Setting the information for the Network %s...",
569 sprintf(temp_buf, "Setting the information for the Cluster %s...",
573 sprintf(temp_buf, "Setting the Data for the Cluster %s...",
577 sprintf(temp_buf, "Setting the Data for the Container %s...",
581 Put_message(temp_buf);
588 newname = strdup(info[M_NAME]);
589 if (GetValueFromUser("The new name for this machine? ", &newname) ==
592 oldnewname = strdup(newname);
593 newname = canonicalize_hostname(newname);
594 if (strcasecmp(newname, oldnewname) && *oldnewname != '"')
596 sprintf(temp_buf, "Warning: '%s' canonicalized to '%s'\n",
597 oldnewname, newname);
598 Put_message(temp_buf);
603 newname = strdup(info[SN_NAME]);
604 if (GetValueFromUser("The new name for this network? ", &newname) ==
609 newname = strdup(info[C_NAME]);
610 if (GetValueFromUser("The new name for this cluster? ", &newname) ==
615 newname = strdup(info[CON_NAME]);
616 if (GetValueFromUser("The new name for this container? ", &newname)
621 Put_message("Unknown type in AskMCDInfo, programmer botch");
629 if (GetValueFromUser("Machine's vendor", &info[M_VENDOR]) == SUB_ERROR)
631 if (GetValueFromUser("Machine's model", &info[M_MODEL]) == SUB_ERROR)
633 if (GetValueFromUser("Machine's operating system", &info[M_OS]) ==
636 if (GetValueFromUser("Machine's location", &info[M_LOC]) == SUB_ERROR)
638 if (GetValueFromUser("Machine's contact", &info[M_CONTACT]) ==
641 if (GetValueFromUser("Machine's billing contact",
642 &info[M_BILL_CONTACT]) == SUB_ERROR)
644 if (GetValueFromUser("Machine's billing account number",
645 &info[M_ACCT_NUMBER]) == SUB_ERROR)
650 if (GetValueFromUser("Machine's status (? for help)",
651 &info[M_STAT]) == SUB_ERROR)
653 if (isdigit(info[M_STAT][0]))
655 Put_message("Valid status numbers:");
656 for (i = 0; i < 4; i++)
657 Put_message(states[i]);
660 /* there appears to be some argument mismatch between the client
661 * and the server.. so here is this argument shuffler.
662 * I have since modified this to always shuffle the arguments..
663 * not just do so when performing a modify all fields request.
664 * The SetMachinedefaults() has been changed to reflect this.
665 * pray for us and may we attain enlightenment through structures.
670 /* info did not come from SetMachineDefaults(), which does not
671 * initialize entry 10 (M_STAT_CHNG), therefore we can
674 /* This is an update of an existing machine and the structure
675 * was filled in thru a query to the db which does fill in this
681 info[10] = info[M_SUBNET];
682 info[11] = info[M_ADDR];
683 info[12] = info[M_OWNER_TYPE];
684 info[13] = info[M_OWNER_NAME];
685 info[14] = info[M_ACOMMENT];
686 info[15] = info[M_OCOMMENT];
690 if (GetValueFromUser("Machine's network (or 'none')", &info[10])
694 if (GetValueFromUser("Machine's address (or 'unassigned' or 'unique')",
695 &info[11]) == SUB_ERROR)
697 if (GetTypeFromUser("Machine's owner type", "ace_type", &info[12]) ==
700 if (strcmp(info[12], "NONE") &&
701 GetValueFromUser("Owner's Name", &info[13]) == SUB_ERROR)
703 if (!strcmp(info[12], "KERBEROS"))
707 mrcl_validate_kerberos_member(info[13], &canon);
708 if (mrcl_get_message())
709 Put_message(mrcl_get_message());
713 if (GetValueFromUser("Administrative comment", &info[14]) == SUB_ERROR)
715 if (GetValueFromUser("Operational comment", &info[15]) == SUB_ERROR)
718 FreeAndClear(&info[17], TRUE);
719 FreeAndClear(&info[18], TRUE);
722 if (GetValueFromUser("Network description", &info[SN_DESC]) == SUB_ERROR)
727 if (GetValueFromUser("Network's status (? for help)",
728 &info[SN_STATUS]) == SUB_ERROR)
730 if (isdigit(info[SN_STATUS][0]))
732 Put_message("Valid status numbers:");
733 for (i = 0; i < 5; i++)
734 Put_message(subnet_states[i]);
736 if (GetValueFromUser("Network's contact", &info[SN_CONTACT]) == SUB_ERROR)
738 if (GetValueFromUser("Network's billing account number",
739 &info[SN_ACCT_NUMBER]) == SUB_ERROR)
741 if (GetAddressFromUser("Network address", &info[SN_ADDRESS]) == SUB_ERROR)
743 if (GetAddressFromUser("Network mask", &info[SN_MASK]) == SUB_ERROR)
745 if (atoi(info[SN_LOW]) == (int)ntohl(inet_addr(S_DEFAULT_LOW)))
748 unsigned long mask, addr;
750 addr = atoi(info[SN_ADDRESS]);
751 mask = atoi(info[SN_MASK]);
752 low.s_addr = atoi(info[SN_LOW]);
753 low.s_addr = (low.s_addr & ~mask) | (addr & mask);
755 sprintf(temp_buf, "%ld", low.s_addr);
756 info[SN_LOW] = strdup(temp_buf);
758 if (GetAddressFromUser("Lowest assignable address", &info[SN_LOW]) ==
761 if (atoi(info[SN_HIGH]) == (int)ntohl(inet_addr(S_DEFAULT_HIGH)))
764 unsigned long mask, addr;
766 addr = atoi(info[SN_ADDRESS]);
767 mask = atoi(info[SN_MASK]);
768 high.s_addr = atoi(info[SN_HIGH]);
769 high.s_addr = (high.s_addr & ~mask) | (addr & mask);
771 sprintf(temp_buf, "%ld", high.s_addr);
772 info[SN_HIGH] = strdup(temp_buf);
774 if (GetAddressFromUser("Highest assignable address", &info[SN_HIGH]) ==
777 if (GetValueFromUser("Hostname prefix", &info[SN_PREFIX]) == SUB_ERROR)
779 if (GetTypeFromUser("Owner type", "ace_type", &info[SN_ACE_TYPE]) ==
782 if (strcmp(info[SN_ACE_TYPE], "NONE") &&
783 GetValueFromUser("Owner name", &info[SN_ACE_NAME]) == SUB_ERROR)
785 if (!strcmp(info[SN_ACE_TYPE], "KERBEROS"))
789 mrcl_validate_kerberos_member(info[SN_ACE_NAME], &canon);
790 if (mrcl_get_message())
791 Put_message(mrcl_get_message());
792 free(info[SN_ACE_NAME]);
793 info[SN_ACE_NAME] = canon;
795 FreeAndClear(&info[SN_MODTIME], TRUE);
796 FreeAndClear(&info[SN_MODBY], TRUE);
797 FreeAndClear(&info[SN_MODWITH], TRUE);
800 if (GetValueFromUser("Cluster's Description:", &info[C_DESCRIPT]) ==
803 if (GetValueFromUser("Cluster's Location:", &info[C_LOCATION]) ==
806 FreeAndClear(&info[C_MODTIME], TRUE);
807 FreeAndClear(&info[C_MODBY], TRUE);
808 FreeAndClear(&info[C_MODWITH], TRUE);
811 if (GetValueFromUser("Label defining this data?", &info[CD_LABEL]) ==
814 if (GetValueFromUser("The data itself ? ", &info[CD_DATA]) == SUB_ERROR)
818 if (GetValueFromUser("Container's Description:", &info[CON_DESCRIPT]) ==
821 if (GetValueFromUser("Container's Location:", &info[CON_LOCATION]) ==
824 if (GetValueFromUser("Container's Contact:", &info[CON_CONTACT]) ==
827 if (GetTypeFromUser("Container's owner type", "ace_type",
828 &info[CON_OWNER_TYPE]) == SUB_ERROR)
830 if (strcmp(info[CON_OWNER_TYPE], "NONE") &&
831 GetValueFromUser("Owner's Name", &info[CON_OWNER_NAME]) == SUB_ERROR)
833 if (!strcmp(info[CON_OWNER_TYPE], "KERBEROS"))
837 mrcl_validate_kerberos_member(info[CON_OWNER_NAME], &canon);
838 if (mrcl_get_message())
839 Put_message(mrcl_get_message());
840 free(info[CON_OWNER_NAME]);
841 info[CON_OWNER_NAME] = canon;
843 if (GetTypeFromUser("Container's Membership ACL", "ace_type",
844 &info[CON_MEMACE_TYPE]) == SUB_ERROR)
846 if (strcmp(info[CON_MEMACE_TYPE], "NONE") &&
847 GetValueFromUser("Membership ACL", &info[CON_MEMACE_NAME])
850 if (!strcmp(info[CON_MEMACE_TYPE], "KERBEROS"))
854 mrcl_validate_kerberos_member(info[CON_MEMACE_NAME], &canon);
855 if (mrcl_get_message())
856 Put_message(mrcl_get_message());
857 free(info[CON_MEMACE_NAME]);
858 info[CON_MEMACE_NAME] = canon;
860 FreeAndClear(&info[CON_MODTIME], TRUE);
861 FreeAndClear(&info[CON_MODBY], TRUE);
862 FreeAndClear(&info[CON_MODWITH], TRUE);
867 * Slide the newname into the #2 slot, this screws up all future references
871 SlipInNewName(info, newname);
876 /* ----------- Machine Menu ----------- */
878 /* Function Name: ShowMachineInfo
879 * Description: This function shows the information about a machine.
880 * Arguments: argc, argv - the name of the machine in argv[1].
881 * Returns: DM_NORMAL.
884 int ShowMachineInfo(int argc, char **argv)
889 tmpname = canonicalize_hostname(strdup(argv[1]));
890 top = GetMCInfo(MACHINE, tmpname, NULL);
891 Loop(top, ((void (*)(char **)) PrintMachInfo));
896 /* Function Name: ShowMachineQuery
897 * Description: This function shows the information about a machine.
898 * or group of machines, which may be selected through a
899 * number of criteria.
900 * Arguments: argc, argv - the name of the machine in argv[1],
901 * the address of the machine in argv[2],
902 * the location of the machine in argv[3],
903 * and the contact name in argv[4].
904 * any of these may be wildcards.
905 * Returns: DM_NORMAL.
908 int ShowMachineQuery(int argc, char **argv)
911 struct mqelem *top, *elem = NULL;
914 if (!strcmp(argv[1], "") && !strcmp(argv[2], "") &&
915 !strcmp(argv[3], "") && !strcmp(argv[4], ""))
917 Put_message("You must specify at least one parameter of the query.");
922 args[0] = canonicalize_hostname(strdup(argv[1]));
938 if ((stat = do_mr_query("get_host", 4, args, StoreInfo, &elem)))
940 if (stat == MR_NO_MATCH)
941 Put_message("No machine(s) found matching query in the database.");
943 com_err(program_name, stat, " in get_machine.");
946 top = QueueTop(elem);
947 Loop(top, ((void (*)(char **)) PrintMachInfo));
952 /* Function Name: AddMachine
953 * Description: This function adds a new machine to the database.
954 * Arguments: argc, argv - the name of the network in argv[1].
955 * Returns: DM_NORMAL.
958 int AddMachine(int argc, char **argv)
960 char **args, *info[MAX_ARGS_SIZE], *name, buf[256], *xargs[5];
962 struct mqelem *elem = NULL;
965 if (!ValidName(argv[1])) /* Checks for wildcards. */
969 * get the network record
972 if (strcasecmp(argv[1], "none") &&
973 (stat = do_mr_query("get_subnet", 1, &argv[1], StoreInfo, &elem)))
975 if (stat == MR_NO_MATCH)
978 sprintf(buf, "Network '%s' is not in the database.", argv[1]);
981 com_err(program_name, stat, " in get_subnet.");
986 * Check to see if this machine already exists.
989 name = strdup(""); /* want to put prefix here */
990 if (GetValueFromUser("Machine name", &name) == SUB_ERROR)
993 name = canonicalize_hostname(strdup(name));
996 xargs[1] = xargs[2] = xargs[3] = "*";
997 if (!(stat = do_mr_query("get_host", 4, xargs, NULL, NULL)))
999 sprintf(buf, "The machine '%s' already exists.", name);
1004 else if (stat != MR_NO_MATCH)
1006 com_err(program_name, stat,
1007 " while checking machine '%s' in AddMachine.", name);
1011 rinfo = SetMachineDefaults(info, name);
1012 rinfo[M_SUBNET] = strdup(argv[1]);
1013 if (!(args = AskMCDInfo(rinfo, MACHINE, FALSE)))
1015 Put_message("Aborted.");
1020 * Actually create the new Machine.
1023 if ((stat = do_mr_query("add_host", CountArgs(args), args, NULL, NULL)))
1024 com_err(program_name, stat, " in AddMachine.");
1031 /* Function Name: RealUpdateMachine
1032 * Description: Performs the actual update of the machine data.
1033 * Arguments: info - the information on the machine to update.
1034 * junk - an UNUSED Boolean.
1038 static void RealUpdateMachine(char **info, Bool junk)
1041 char **args = AskMCDInfo(info, MACHINE, TRUE);
1044 Put_message("Aborted.");
1047 if ((stat = do_mr_query("update_host", CountArgs(args), args, NULL, NULL)))
1048 com_err(program_name, stat, " in UpdateMachine.");
1050 Put_message("Machine successfully updated.");
1053 /* Function Name: UpdateMachine
1054 * Description: This function adds a new machine to the database.
1055 * Arguments: argc, argv - the name of the machine in argv[1].
1056 * Returns: DM_NORMAL.
1059 int UpdateMachine(int argc, char **argv)
1064 tmpname = canonicalize_hostname(strdup(argv[1]));
1065 top = GetMCInfo(MACHINE, tmpname, NULL);
1066 QueryLoop(top, NullPrint, RealUpdateMachine, "Update the machine");
1073 /* Function Name: CheckAndRemoveFromCluster
1074 * Description: This func tests to see if a machine is in a cluster.
1075 * and if so then removes it
1076 * Arguments: name - name of the machine (already Canonicalized).
1077 * ask_user- query the user before removing if from clusters?
1078 * Returns: MR_ERROR if machine left in a cluster, or mr_error.
1081 int CheckAndRemoveFromCluster(char *name, Bool ask_user)
1083 int stat, ret_value;
1085 char *args[10], temp_buf[BUFSIZ], *ptr;
1086 struct mqelem *top, *elem = NULL;
1088 ret_value = SUB_NORMAL; /* initialize ret_value. */
1091 stat = do_mr_query("get_machine_to_cluster_map", 2, args, StoreInfo, &elem);
1092 if (stat && stat != MR_NO_MATCH)
1094 com_err(program_name, stat, " in get_machine_to_cluster_map.");
1097 if (stat == MR_SUCCESS)
1099 elem = top = QueueTop(elem);
1102 sprintf(temp_buf, "%s is assigned to the following clusters.", name);
1103 Put_message(temp_buf);
1104 Loop(top, (void (*)(char **)) PrintMCMap);
1105 ptr = "Remove this machine from ** ALL ** these clusters?";
1106 if (YesNoQuestion(ptr, FALSE) == TRUE) /* may return -1. */
1110 Put_message("Aborting...");
1122 char **info = elem->q_data;
1123 if ((stat = do_mr_query("delete_machine_from_cluster",
1124 2, info, NULL, NULL)))
1126 ret_value = SUB_ERROR;
1127 com_err(program_name, stat,
1128 " in delete_machine_from_cluster.");
1130 "Machine %s ** NOT ** removed from cluster %s.",
1131 info[MAP_MACHINE], info[MAP_CLUSTER]);
1132 Put_message(temp_buf);
1134 elem = elem->q_forw;
1141 /* Function Name: CheckAndRemoveCnames
1142 * Description: This func tests to see if a machine has cnames,
1143 * and if so then removes them.
1144 * Arguments: name - name of the machine (already Canonicalized).
1145 * ask_user- query the user before removing cnames?
1146 * Returns: MR_ERROR if machine left with a cname, or mr_error.
1149 int CheckAndRemoveCnames(char *name, Bool ask_user)
1151 int stat, ret_value;
1153 char *args[10], temp_buf[BUFSIZ], *ptr;
1154 struct mqelem *top, *elem = NULL;
1156 ret_value = SUB_NORMAL;
1159 stat = do_mr_query("get_hostalias", 2, args, StoreInfo, &elem);
1160 if (stat && stat != MR_NO_MATCH)
1162 com_err(program_name, stat, " in get_hostalias.");
1165 if (stat == MR_SUCCESS)
1167 elem = top = QueueTop(elem);
1170 sprintf(temp_buf, "%s has the following cnames.", name);
1171 Put_message(temp_buf);
1172 Loop(top, (void (*)(char **)) PrintCname);
1173 ptr = "Remove ** ALL ** these cnames?";
1174 if (YesNoQuestion(ptr, FALSE) == TRUE) /* may return -1. */
1178 Put_message("Aborting...");
1190 char **info = elem->q_data;
1191 if ((stat = do_mr_query("delete_hostalias", 2, info,
1194 ret_value = SUB_ERROR;
1195 com_err(program_name, stat, " in delete_hostalias.");
1197 "Cname %s ** NOT ** removed from host %s.",
1199 Put_message(temp_buf);
1201 elem = elem->q_forw;
1208 /* Function Name: RealDeleteMachine
1209 * Description: Actually Deletes the Machine.
1210 * Arguments: info - nescessary information stored as an array of char *'s
1211 * one_machine - a boolean, true if there is only one item in
1216 static void RealDeleteMachine(char **info, Bool one_machine)
1219 char temp_buf[BUFSIZ];
1221 sprintf(temp_buf, "Are you sure you want to delete the machine %s (y/n)? ",
1223 if (!one_machine || Confirm(temp_buf))
1225 if (CheckAndRemoveFromCluster(info[M_NAME], TRUE) != SUB_ERROR)
1227 if (CheckAndRemoveCnames(info[M_NAME], TRUE) != SUB_ERROR)
1229 if ((stat = do_mr_query("delete_host", 1,
1230 &info[M_NAME], NULL, NULL)))
1232 com_err(program_name, stat, " in DeleteMachine.");
1233 sprintf(temp_buf, "%s ** NOT ** deleted.",
1235 Put_message(temp_buf);
1239 sprintf(temp_buf, "%s successfully Deleted.", info[M_NAME]);
1240 Put_message(temp_buf);
1247 /* Function Name: DeleteMachine
1248 * Description: This function removes a machine from the data base.
1249 * Arguments: argc, argv - the machines name int argv[1].
1250 * Returns: DM_NORMAL.
1253 /* Perhaps we should remove the cluster if it has no machine now. */
1255 int DeleteMachine(int argc, char **argv)
1260 tmpname = canonicalize_hostname(strdup(argv[1]));
1261 top = GetMCInfo(MACHINE, tmpname, (char *) NULL);
1262 QueryLoop(top, PrintMachInfo, RealDeleteMachine, "Delete the machine");
1269 /* Function Name: ShowCname
1270 * Description: This function shows machine aliases
1271 * Arguments: argc, argv - the alias argv[1], the real name in argv[2]
1272 * Returns: DM_NORMAL.
1275 int ShowCname(int argc, char **argv)
1278 char *tmpalias, *tmpname;
1280 tmpalias = partial_canonicalize_hostname(strdup(argv[1]));
1281 tmpname = canonicalize_hostname(strdup(argv[2]));
1282 top = GetMCInfo(CNAME, tmpalias, tmpname);
1283 Put_message(""); /* blank line on screen */
1284 Loop(top, ((void (*)(char **)) PrintCname));
1290 int AddCname(int argc, char **argv)
1295 args[0] = partial_canonicalize_hostname(strdup(argv[1]));
1296 args[1] = canonicalize_hostname(strdup(argv[2]));
1297 stat = do_mr_query("add_hostalias", 2, args, NULL, NULL);
1303 Put_message("That alias name is already in use.");
1306 Put_message("Permission denied. "
1307 "(Regular users can only add two aliases to a host.");
1310 com_err(program_name, stat, " in add_hostalias");
1316 int DeleteCname(int argc, char **argv)
1321 args[0] = partial_canonicalize_hostname(strdup(argv[1]));
1322 args[1] = canonicalize_hostname(strdup(argv[2]));
1323 stat = do_mr_query("delete_hostalias", 2, args, NULL, NULL);
1325 com_err(program_name, stat, " in delete_hostalias");
1330 /* Function Name: AddMachineToCluster
1331 * Description: This function adds a machine to a cluster
1332 * Arguments: argc, argv - The machine name is argv[1].
1333 * The cluster name in argv[2].
1334 * Returns: DM_NORMAL.
1337 int AddMachineToCluster(int argc, char **argv)
1340 char *machine, *cluster, temp_buf[BUFSIZ], *args[10];
1341 Bool add_it, one_machine, one_cluster;
1342 struct mqelem *melem, *mtop, *celem, *ctop;
1344 machine = canonicalize_hostname(strdup(argv[1]));
1345 if (strcasecmp(machine, argv[1]) && *argv[1] != '"')
1347 sprintf(temp_buf, "Warning: '%s' canonicalized to '%s'.",
1349 Put_message(temp_buf);
1353 celem = ctop = GetMCInfo(CLUSTER, cluster, NULL);
1354 melem = mtop = GetMCInfo(MACHINE, machine, NULL);
1357 one_machine = (QueueCount(mtop) == 1);
1358 one_cluster = (QueueCount(ctop) == 1);
1360 /* No good way to use QueryLoop() here, sigh */
1364 char **minfo = melem->q_data;
1367 char **cinfo = celem->q_data;
1368 if (one_machine && one_cluster)
1372 sprintf(temp_buf, "Add machine %s to cluster %s (y/n/q) ?",
1373 minfo[M_NAME], cinfo[C_NAME]);
1374 switch (YesNoQuitQuestion(temp_buf, FALSE))
1383 Put_message("Aborting...");
1391 args[0] = minfo[M_NAME];
1392 args[1] = cinfo[C_NAME];
1393 stat = do_mr_query("add_machine_to_cluster", 2, args,
1400 sprintf(temp_buf, "%s is already in cluster %s",
1401 minfo[M_NAME], cinfo[C_NAME]);
1402 Put_message(temp_buf);
1405 com_err(program_name, stat, " in AddMachineToCluster.");
1409 celem = celem->q_forw;
1411 celem = ctop; /* reset cluster element. */
1412 melem = melem->q_forw;
1419 /* Function Name: RealRemoveMachineFromCluster
1420 * Description: This function actually removes the machine from its
1422 * Arguments: info - all information nescessary to perform the removal.
1423 * one_map - True if there is only one case, and we should
1428 static void RealRemoveMachineFromCluster(char **info, Bool one_map)
1430 char temp_buf[BUFSIZ];
1433 sprintf(temp_buf, "Remove %s from the cluster %s",
1434 info[MAP_MACHINE], info[MAP_CLUSTER]);
1435 if (!one_map || Confirm(temp_buf))
1437 if ((stat = do_mr_query("delete_machine_from_cluster", 2,
1439 com_err(program_name, stat, " in delete_machine_from_cluster");
1442 sprintf(temp_buf, "%s has been removed from the cluster %s.",
1443 info[MAP_MACHINE], info[MAP_CLUSTER]);
1444 Put_message(temp_buf);
1448 Put_message("Machine not removed.");
1451 /* Function Name: RemoveMachineFromCluster
1452 * Description: Removes this machine form a specific cluster.
1453 * Arguments: argc, argv - Name of machine in argv[1].
1454 * Name of cluster in argv[2].
1458 int RemoveMachineFromCluster(int argc, char **argv)
1460 struct mqelem *elem = NULL;
1461 char buf[BUFSIZ], *args[10];
1464 args[MAP_MACHINE] = canonicalize_hostname(strdup(argv[1]));
1465 if (strcasecmp(args[MAP_MACHINE], argv[1]) && *argv[1] != '"')
1467 sprintf(buf, "Warning: '%s' canonicalized to '%s'.",
1468 argv[1], args[MAP_MACHINE]);
1471 args[MAP_CLUSTER] = argv[2];
1472 args[MAP_END] = NULL;
1474 stat = do_mr_query("get_machine_to_cluster_map", CountArgs(args), args,
1476 if (stat == MR_NO_MATCH)
1478 sprintf(buf, "The machine %s is not in the cluster %s.",
1479 args[MAP_MACHINE], args[MAP_CLUSTER]);
1481 free(args[MAP_MACHINE]);
1484 if (stat != MR_SUCCESS)
1485 com_err(program_name, stat, " in delete_machine_from_cluster");
1487 elem = QueueTop(elem);
1488 QueryLoop(elem, PrintMCMap, RealRemoveMachineFromCluster,
1489 "Remove this machine from this cluster");
1492 free(args[MAP_MACHINE]);
1496 /* ---------- Subnet Menu -------- */
1498 /* Function Name: ShowSubnetInfo
1499 * Description: Gets information about a subnet given its name.
1500 * Arguments: argc, argc - the name of the subnet in in argv[1].
1501 * Returns: DM_NORMAL.
1504 int ShowSubnetInfo(int argc, char **argv)
1508 top = GetMCInfo(SUBNET, argv[1], (char *) NULL);
1509 Loop(top, (void (*)(char **)) PrintSubnetInfo);
1514 /* Function Name: AddSubnet
1515 * Description: Creates a new subnet.
1516 * Arguments: argc, argv - the name of the new subnet is argv[1].
1517 * Returns: DM_NORMAL.
1520 int AddSubnet(int argc, char **argv)
1522 char **args, *info[MAX_ARGS_SIZE], *name = argv[1];
1526 * Check to see if this subnet already exists.
1528 if (!ValidName(name))
1531 if ((stat = do_mr_query("get_subnet", 1, &name, NULL, NULL)) == MR_SUCCESS)
1533 Put_message("This subnet already exists.");
1536 else if (stat != MR_NO_MATCH)
1538 com_err(program_name, stat, " in AddSubnet.");
1541 if (!(args = AskMCDInfo(SetSubnetDefaults(info, name), SUBNET, FALSE)))
1543 Put_message("Aborted.");
1549 * Actually create the new Subnet.
1551 if ((stat = do_mr_query("add_subnet", CountArgs(args), args, NULL, NULL)))
1552 com_err(program_name, stat, " in AddSubnet.");
1558 /* Function Name: RealUpdateSubnet
1559 * Description: This function actually performs the subnet update.
1560 * Arguments: info - all information nesc. for updating the subnet.
1561 * junk - an UNUSED boolean.
1565 static void RealUpdateSubnet(char **info, Bool junk)
1568 char **args = AskMCDInfo(info, SUBNET, TRUE);
1571 Put_message("Aborted.");
1574 if ((stat = do_mr_query("update_subnet", CountArgs(args), args, NULL, NULL)))
1575 com_err(program_name, stat, " in UpdateSubnet.");
1577 Put_message("Subnet successfully updated.");
1580 /* Function Name: UpdateSubnet
1581 * Description: This Function Updates a subnet
1582 * Arguments: name of the subnet in argv[1].
1583 * Returns: DM_NORMAL.
1586 int UpdateSubnet(int argc, char **argv)
1589 top = GetMCInfo(SUBNET, argv[1], NULL);
1590 QueryLoop(top, NullPrint, RealUpdateSubnet, "Update the subnet");
1596 /* Function Name: RealDeleteSubnet
1597 * Description: Actually performs the subnet deletion.
1598 * Arguments: info - all information about this subnet.
1599 * one_subnet - If true then there was only one subnet in
1600 * the queue, and we should confirm.
1604 static void RealDeleteSubnet(char **info, Bool one_subnet)
1607 char temp_buf[BUFSIZ];
1610 "Are you sure the you want to delete the subnet %s (y/n) ?",
1612 if (!one_subnet || Confirm(temp_buf))
1614 if ((stat = do_mr_query("delete_subnet", 1, &info[C_NAME], NULL, NULL)))
1616 com_err(program_name, stat, " in delete_subnet.");
1617 sprintf(temp_buf, "Subnet %s ** NOT ** deleted.", info[C_NAME]);
1618 Put_message(temp_buf);
1622 sprintf(temp_buf, "subnet %s successfully deleted.",
1624 Put_message(temp_buf);
1629 /* Function Name: DeleteSubnet
1630 * Description: This function removes a subnet from the database.
1631 * Arguments: argc, argv - the name of the subnet is stored in argv[1].
1632 * Returns: DM_NORMAL.
1635 int DeleteSubnet(int argc, char **argv)
1639 top = GetMCInfo(SUBNET, argv[1], NULL);
1640 QueryLoop(top, PrintSubnetInfo, RealDeleteSubnet, "Delete the subnet");
1646 /* ---------- Cluster Menu -------- */
1648 /* Function Name: ShowClusterInfo
1649 * Description: Gets information about a cluser given its name.
1650 * Arguments: argc, argc - the name of the cluster in in argv[1].
1651 * Returns: DM_NORMAL.
1654 int ShowClusterInfo(int argc, char **argv)
1658 top = GetMCInfo(CLUSTER, argv[1], NULL);
1659 Loop(top, (void (*)(char **)) PrintClusterInfo);
1664 /* Function Name: AddCluster
1665 * Description: Creates a new cluster.
1666 * Arguments: argc, argv - the name of the new cluster is argv[1].
1667 * Returns: DM_NORMAL.
1670 int AddCluster(int argc, char **argv)
1672 char **args, *info[MAX_ARGS_SIZE], *name = argv[1];
1676 * Check to see if this cluster already exists.
1678 if (!ValidName(name))
1681 if ((stat = do_mr_query("get_cluster", 1, &name, NULL, NULL)) == MR_SUCCESS)
1683 Put_message("This cluster already exists.");
1686 else if (stat != MR_NO_MATCH)
1688 com_err(program_name, stat, " in AddCluster.");
1691 if (!(args = AskMCDInfo(SetClusterDefaults(info, name), CLUSTER, FALSE)))
1693 Put_message("Aborted.");
1699 * Actually create the new Cluster.
1701 if ((stat = do_mr_query("add_cluster", CountArgs(args), args, NULL, NULL)))
1702 com_err(program_name, stat, " in AddCluster.");
1708 /* Function Name: RealUpdateCluster
1709 * Description: This function actually performs the cluster update.
1710 * Arguments: info - all information nesc. for updating the cluster.
1711 * junk - an UNUSED boolean.
1715 static void RealUpdateCluster(char **info, Bool junk)
1718 char **args = AskMCDInfo(info, CLUSTER, TRUE);
1722 Put_message("Aborted.");
1725 if ((stat = do_mr_query("update_cluster", CountArgs(args),
1727 com_err(program_name, stat, " in UpdateCluster.");
1729 Put_message("Cluster successfully updated.");
1732 /* Function Name: UpdateCluster
1733 * Description: This Function Updates a cluster
1734 * Arguments: name of the cluster in argv[1].
1735 * Returns: DM_NORMAL.
1738 int UpdateCluster(int argc, char **argv)
1741 top = GetMCInfo(CLUSTER, argv[1], NULL);
1742 QueryLoop(top, NullPrint, RealUpdateCluster, "Update the cluster");
1748 /* Function Name: CheckAndRemoveMachine
1749 * Description: This function checks and removes all machines from a
1751 * Arguments: name - name of the cluster.
1752 * ask_first - if TRUE, then we will query the user, before
1754 * Returns: SUB_ERROR if all machines not removed.
1757 int CheckAndRemoveMachines(char *name, Bool ask_first)
1759 int stat, ret_value;
1761 char *args[10], temp_buf[BUFSIZ], *ptr;
1762 struct mqelem *top, *elem = NULL;
1764 ret_value = SUB_NORMAL;
1765 args[MAP_MACHINE] = "*";
1766 args[MAP_CLUSTER] = name;
1767 stat = do_mr_query("get_machine_to_cluster_map", 2, args, StoreInfo, &elem);
1768 if (stat && stat != MR_NO_MATCH)
1770 com_err(program_name, stat, " in get_machine_to_cluster_map.");
1773 if (stat == MR_SUCCESS)
1775 elem = top = QueueTop(elem);
1778 sprintf(temp_buf, "The cluster %s has the following machines in it:",
1780 Put_message(temp_buf);
1783 char **info = elem->q_data;
1784 Print(1, &info[MAP_MACHINE], (char *) NULL);
1785 elem = elem->q_forw;
1787 ptr = "Remove ** ALL ** these machines from this cluster?";
1789 if (YesNoQuestion(ptr, FALSE) == TRUE) /* may return -1. */
1793 Put_message("Aborting...");
1806 char **info = elem->q_data;
1807 if ((stat = do_mr_query("delete_machine_from_cluster",
1808 2, info, NULL, NULL)))
1810 ret_value = SUB_ERROR;
1811 com_err(program_name, stat,
1812 " in delete_machine_from_cluster.");
1814 "Machine %s ** NOT ** removed from cluster %s.",
1815 info[MAP_MACHINE], info[MAP_CLUSTER]);
1816 Put_message(temp_buf);
1818 elem = elem->q_forw;
1825 /* Function Name: RealDeleteCluster
1826 * Description: Actually performs the cluster deletion.
1827 * Arguments: info - all information about this cluster.
1828 * one_cluster - If true then there was only one cluster in
1829 * the queue, and we should confirm.
1833 static void RealDeleteCluster(char **info, Bool one_cluster)
1836 char temp_buf[BUFSIZ];
1839 "Are you sure the you want to delete the cluster %s (y/n) ?",
1841 if (!one_cluster || Confirm(temp_buf))
1843 if (CheckAndRemoveMachines(info[C_NAME], TRUE) != SUB_ERROR)
1845 if ((stat = do_mr_query("delete_cluster", 1,
1846 &info[C_NAME], NULL, NULL)))
1848 com_err(program_name, stat, " in delete_cluster.");
1849 sprintf(temp_buf, "Cluster %s ** NOT ** deleted.", info[C_NAME]);
1850 Put_message(temp_buf);
1854 sprintf(temp_buf, "cluster %s successfully deleted.",
1856 Put_message(temp_buf);
1862 /* Function Name: DeleteCluster
1863 * Description: This function removes a cluster from the database.
1864 * Arguments: argc, argv - the name of the cluster is stored in argv[1].
1865 * Returns: DM_NORMAL.
1868 int DeleteCluster(int argc, char **argv)
1872 top = GetMCInfo(CLUSTER, argv[1], NULL);
1873 QueryLoop(top, PrintClusterInfo, RealDeleteCluster, "Delete the cluster");
1879 /* ----------- Cluster Data Menu -------------- */
1881 /* Function Name: ShowClusterData
1882 * Description: This function shows the services for one cluster.
1883 * Arguments: argc, argv - The name of the cluster is argv[1].
1884 * The label of the data in argv[2].
1885 * Returns: DM_NORMAL.
1888 int ShowClusterData(int argc, char **argv)
1890 struct mqelem *elem, *top;
1893 top = elem = GetMCInfo(DATA, argv[1], argv[2]);
1896 info = elem->q_data;
1897 PrintClusterData(info);
1898 elem = elem->q_forw;
1904 /* Function Name: AddClusterData
1905 * Description: This function adds some data to the cluster.
1906 * Arguments: argv, argc: argv[1] - the name of the cluster.
1907 * argv[2] - the label of the data.
1908 * argv[3] - the data.
1909 * Returns: DM_NORMAL.
1912 int AddClusterData(int argc, char **argv)
1916 for (i = 1; i < 4; i++)
1918 if (IS_EMPTY(argv[i]))
1920 Put_message("Cluster data cannot be an empty string.");
1924 if ((stat = do_mr_query("add_cluster_data", 3, argv + 1, NULL, NULL)))
1925 com_err(program_name, stat, " in AddClusterData.");
1929 /* Function Name: RealRemoveClusterData
1930 * Description: actually removes the cluster data.
1931 * Arguments: info - all info necessary to remove the cluster, in an array
1933 * one_item - if true then the queue has only one elem and we
1938 static void RealRemoveClusterData(char **info, Bool one_item)
1944 temp_ptr = "Are you sure that you want to remove this cluster data (y/n) ?";
1945 PrintClusterData(info);
1946 if (!one_item || Confirm(temp_ptr))
1948 if ((stat = do_mr_query("delete_cluster_data", 3, info, NULL, NULL)))
1950 com_err(program_name, stat, " in DeleteClusterData.");
1951 Put_message("Data not removed.");
1954 Put_message("Removal successful.");
1958 /* Function Name: RemoveClusterData
1959 * Description: This function removes data on a given cluster.
1960 * Arguments: argv, argc: argv[1] - the name of the cluster.
1961 * argv[2] - the label of the data.
1962 * argv[3] - the data.
1963 * Returns: DM_NORMAL.
1966 int RemoveClusterData(int argc, char **argv)
1970 top = GetMCInfo(DATA, argv[1], argv[2]);
1971 QueryLoop(top, PrintClusterData, RealRemoveClusterData,
1972 "Remove data from cluster");
1978 /* Function Name: MachineToClusterMap
1979 * Description: This Retrieves the mapping between machine and cluster
1980 * Arguments: argc, argv - argv[1] -> machine name or wildcard.
1981 * argv[2] -> cluster name or wildcard.
1985 int MachineToClusterMap(int argc, char **argv)
1987 struct mqelem *elem, *top;
1988 char *tmpname, temp_buf[256];
1990 tmpname = canonicalize_hostname(strdup(argv[1]));
1991 if (strcasecmp(tmpname, argv[1]) && *argv[1] != '"')
1993 sprintf(temp_buf, "Warning: '%s' canonicalized to '%s'.",
1995 Put_message(temp_buf);
1997 top = elem = GetMCInfo(MAP, tmpname, argv[2]);
1999 Put_message(""); /* blank line on screen */
2002 char **info = elem->q_data;
2004 elem = elem->q_forw;
2012 /* Function Name: MachineByOwner
2013 * Description: This function prints all machines which are owned by
2014 * a given user or group.
2016 * Returns: DM_NORMAL.
2019 int MachineByOwner(int argc, char **argv)
2021 char buf[BUFSIZ], temp_buf[BUFSIZ], *type, *name;
2024 type = strdup("USER");
2025 if (GetTypeFromUser("Type of owner", "ace_type", &type) == SUB_ERROR)
2028 sprintf(buf, "Name of %s", type);
2029 name = strdup(user);
2030 if (GetValueFromUser(buf, &name) == SUB_ERROR)
2033 switch (YesNoQuestion("Do you want a recursive search (y/n)", FALSE))
2036 sprintf(temp_buf, "R%s", type); /* "USER to "RUSER", etc. */
2038 type = strdup(temp_buf);
2046 top = GetMachineByOwner(type, name);
2047 Loop(top, PrintMachine);
2053 /* Function Name: GetMachineByOwner
2054 * Description: This function stores information retrieved by
2055 * the get_host_by_owner query
2056 * Arguments: type - an ace_type, argv[0] for the query
2057 * name - name of machine, argv[1] for the query
2058 * Returns: the top element of a queue returning the data, or NULL.
2061 struct mqelem *GetMachineByOwner(char *type, char *name)
2064 struct mqelem *elem = NULL;
2069 if ((status = do_mr_query("get_host_by_owner", 2, args, StoreInfo, &elem)))
2071 com_err(program_name, status, " in get_host_by_owner");
2074 return QueueTop(elem);
2077 int ShowContainerInfo(int argc, char **argv)
2081 top = GetMCInfo(CONTAINER, argv[1], NULL);
2082 Loop(top, (void (*)(char **)) PrintContainerInfo);
2087 static char *PrintContainerInfo(char **info)
2089 char buf[BUFSIZ], tbuf[256];
2092 sprintf(buf, "Container: %-16s", info[CON_NAME]);
2094 sprintf(buf, "Description: %-16s", info[CON_DESCRIPT]);
2096 sprintf(buf, "Location: %-16s Contact: %s", info[CON_LOCATION],
2099 sprintf(tbuf, "%s %s", info[CON_OWNER_TYPE],
2100 strcmp(info[CON_OWNER_TYPE], "NONE") ? info[CON_OWNER_NAME] : "");
2101 sprintf(buf, "Owner: %-16s", tbuf);
2103 sprintf(tbuf, "%s %s", info[CON_MEMACE_TYPE],
2104 strcmp(info[CON_MEMACE_TYPE], "NONE") ? info[CON_MEMACE_NAME] : "");
2105 sprintf(buf, "Membership ACL: %-16s", tbuf);
2108 sprintf(buf, MOD_FORMAT, info[CON_MODBY], info[CON_MODTIME],
2111 return info[CON_NAME];
2114 static char *PrintContainer(char **info)
2118 sprintf(buf, "Container: %s", info[CON_NAME]);
2122 static char *PrintMContMap(char **info)
2125 sprintf(buf, "Container: %-30s Machine: %-20s",
2131 int AddContainer(int argc, char **argv)
2133 char **args, *info[MAX_ARGS_SIZE], *name = argv[1];
2136 /* Can't use ValidName() because spaces are allowed in container names */
2139 Put_message("Please use a non-empty name.");
2143 if (strchr(name, '*') || strchr(name, '?'))
2145 Put_message("Wildcards not accepted here.");
2149 /* Check if this cluster already exists. */
2150 if ((stat = do_mr_query("get_container", 1, &name, NULL, NULL))
2153 Put_message("This container already exists.");
2156 else if (stat != MR_NO_MATCH)
2158 com_err(program_name, stat, " in AddContainer.");
2161 if (!(args = AskMCDInfo(SetContainerDefaults(info, name), CONTAINER, FALSE)))
2163 Put_message("Aborted.");
2168 /* Create the new container. */
2169 if ((stat = do_mr_query("add_container", CountArgs(args), args, NULL, NULL)))
2170 com_err(program_name, stat, " in AddContainer.");
2176 int UpdateContainer(int argc, char **argv)
2179 top = GetMCInfo(CONTAINER, argv[1], NULL);
2180 QueryLoop(top, NullPrint, RealUpdateContainer, "Update the container");
2186 static void RealUpdateContainer(char **info, Bool junk)
2189 char **args = AskMCDInfo(info, CONTAINER, TRUE);
2193 Put_message("Aborted.");
2196 if ((stat = do_mr_query("update_container", CountArgs(args), args,
2198 com_err(program_name, stat, " in UpdateContainer.");
2200 Put_message("Container successfully updated.");
2203 int DeleteContainer(int argc, char **argv)
2207 top = GetMCInfo(CONTAINER, argv[1], NULL);
2208 QueryLoop(top, PrintClusterInfo, RealDeleteContainer,
2209 "Delete the container");
2215 static void RealDeleteContainer(char **info, Bool one_container)
2218 char temp_buf[BUFSIZ];
2221 "Are you sure you want to delete the container %s (y/n) ?",
2223 if (!one_container || Confirm(temp_buf))
2225 if (CheckAndRemoveMachinesFromContainer(info[CON_NAME], TRUE)
2228 if ((stat = do_mr_query("delete_container", 1, &info[CON_NAME],
2231 com_err(program_name, stat, " in delete_container.");
2232 sprintf(temp_buf, "Container %s ** NOT ** deleted.",
2234 Put_message(temp_buf);
2238 sprintf(temp_buf, "Container %s successfully deleted.",
2240 Put_message(temp_buf);
2246 int CheckAndRemoveMachinesFromContainer(char *name, Bool ask_first)
2248 int stat, ret_value;
2250 char *args[10], temp_buf[BUFSIZ], *ptr;
2251 struct mqelem *top, *elem = NULL;
2253 ret_value = SUB_NORMAL;
2256 stat = do_mr_query("get_machines_of_container", 2, args, StoreInfo,
2258 if (stat && stat != MR_NO_MATCH)
2260 com_err(program_name, stat, " in get_machines_of_container");
2263 if (stat == MR_SUCCESS)
2265 elem = top = QueueTop(elem);
2269 "The container %s has the following machines in it:", name);
2270 Put_message(temp_buf);
2273 char **info = elem->q_data;
2274 Print(1, &info[0], (char *) NULL);
2275 elem = elem->q_forw;
2277 ptr = "Remove ** ALL ** these machines from this container?";
2279 if (YesNoQuestion(ptr, FALSE) == TRUE) /* may return -1. */
2283 Put_message("Aborting...");
2296 char **info = elem->q_data;
2297 if ((stat = do_mr_query("delete_machine_from_container",
2298 2, info, NULL, NULL)))
2300 ret_value = SUB_ERROR;
2301 com_err(program_name, stat,
2302 " in delete_machine_from_container.");
2304 "Machine %s ** NOT ** removed from container %s.",
2306 Put_message(temp_buf);
2308 elem = elem->q_forw;
2315 int GetSubContainers(int argc, char **argv)
2318 struct mqelem *elem = NULL, *top = NULL;
2323 if (YesNoQuestion("Do you want a recursive search?", TRUE) == TRUE)
2328 if (stat = do_mr_query("get_subcontainers_of_container", 2, args,
2330 com_err(program_name, stat, " in get_subcontainers_of_container");
2332 top = QueueTop(elem);
2333 Loop(top, ((void (*)(char **)) PrintContainer));
2338 int MachineToContainerMap(int argc, char **argv)
2340 struct mqelem *elem, *top;
2341 char *tmpname, temp_buf[256];
2343 tmpname = canonicalize_hostname(strdup(argv[1]));
2344 if (strcasecmp(tmpname, argv[1]) && *argv[1] != '"')
2346 sprintf(temp_buf, "Warning: '%s' canonicalized to '%s'.",
2348 Put_message(temp_buf);
2350 top = elem = GetMCInfo(CONTMAP, tmpname, NULL);
2355 char **info = elem->q_data;
2356 PrintMContMap(info);
2357 elem = elem->q_forw;
2365 int AddMachineToContainer(int argc, char **argv)
2368 char *machine, *container, temp_buf[BUFSIZ], *args[10];
2369 Bool add_it, one_machine, one_container;
2370 struct mqelem *melem, *mtop, *celem, *ctop;
2372 machine = canonicalize_hostname(strdup(argv[1]));
2373 if (strcasecmp(machine, argv[1]) && *argv[1] != '"')
2375 sprintf(temp_buf, "Warning: '%s' canonicalized to '%s'.",
2377 Put_message(temp_buf);
2379 container = argv[2];
2381 celem = ctop = GetMCInfo(CONTAINER, container, NULL);
2382 melem = mtop = GetMCInfo(MACHINE, machine, NULL);
2385 one_machine = (QueueCount(mtop) == 1);
2386 one_container = (QueueCount(ctop) == 1);
2390 char **minfo = melem->q_data;
2393 char **cinfo = celem->q_data;
2394 if (one_machine && one_container)
2398 sprintf(temp_buf, "Add machine %s to container %s (y/n/q) ?",
2399 minfo[M_NAME], cinfo[CON_NAME]);
2400 switch (YesNoQuestion(temp_buf, FALSE))
2409 Put_message("Aborting...");
2417 args[0] = minfo[M_NAME];
2418 args[1] = cinfo[CON_NAME];
2419 stat = do_mr_query("add_machine_to_container", 2, args, NULL,
2426 sprintf(temp_buf, "%s is already in container %s",
2427 minfo[M_NAME], cinfo[CON_NAME]);
2428 Put_message(temp_buf);
2431 com_err(program_name, stat, " in AddMachineToContainer.");
2435 celem = celem->q_forw;
2438 melem = melem->q_forw;
2445 int RemoveMachineFromContainer(int argc, char **argv)
2447 struct mqelem *elem = NULL;
2448 char buf[BUFSIZ], *args[10];
2451 args[0] = canonicalize_hostname(strdup(argv[1]));
2452 if (strcasecmp(args[0], argv[1]) && *argv[1] != '"')
2454 sprintf(buf, "Warning: '%s' canonicalized to '%s'.",
2461 stat = do_mr_query("get_machine_to_container_map", 1, args, StoreInfo,
2463 if (stat == MR_NO_MATCH)
2465 sprintf(buf, "The machine %s is not in the container %s.",
2471 if (stat != MR_SUCCESS)
2472 com_err(program_name, stat, " in deleter_machine_from_container");
2474 elem = QueueTop(elem);
2475 QueryLoop(elem, PrintMContMap, RealRemoveMachineFromContainer,
2476 "Remove this machine from this container");
2483 static void RealRemoveMachineFromContainer(char **info, Bool one_contmap)
2485 char temp_buf[BUFSIZ];
2488 sprintf(temp_buf, "Remove %s from the container %s",
2490 if (!one_contmap || Confirm(temp_buf))
2492 if ((stat = do_mr_query("delete_machine_from_container", 2,
2494 com_err(program_name, stat, " in delete_machine_from_container");
2497 sprintf(temp_buf, "%s has been removed from the container %s.",
2499 Put_message(temp_buf);
2503 Put_message("Machine not removed.");
2506 int GetMachinesOfContainer(int argc, char **argv)
2509 struct mqelem *elem = NULL, *top = NULL;
2514 if (YesNoQuestion("Do you want a recursive search?", TRUE) == TRUE)
2519 if (stat = do_mr_query("get_machines_of_container", 2, args,
2521 com_err(program_name, stat, " in get_machines_of_container");
2523 top = QueueTop(elem);
2524 Loop(top, ((void (*)(char **)) PrintMContMap));