+
+/* Function Name: MachineByOwner
+ * Description: This function prints all machines which are owned by
+ * a given user or group.
+ * Arguments: none.
+ * Returns: DM_NORMAL.
+ */
+
+int MachineByOwner(int argc, char **argv)
+{
+ char buf[BUFSIZ], temp_buf[BUFSIZ], *type, *name;
+ struct mqelem *top;
+
+ type = strdup("USER");
+ if (GetTypeFromUser("Type of owner", "ace_type", &type) == SUB_ERROR)
+ return DM_NORMAL;
+
+ sprintf(buf, "Name of %s", type);
+ name = strdup(user);
+ if (GetValueFromUser(buf, &name) == SUB_ERROR)
+ return DM_NORMAL;
+
+ switch (YesNoQuestion("Do you want a recursive search (y/n)", FALSE))
+ {
+ case TRUE:
+ sprintf(temp_buf, "R%s", type); /* "USER to "RUSER", etc. */
+ free(type);
+ type = strdup(temp_buf);
+ break;
+ case FALSE:
+ break;
+ default:
+ return DM_NORMAL;
+ }
+
+ top = GetMachineByOwner(type, name);
+ Loop(top, PrintMachine);
+
+ FreeQueue(top);
+ return DM_NORMAL;
+}
+
+/* Function Name: GetMachineByOwner
+ * Description: This function stores information retrieved by
+ * the get_host_by_owner query
+ * Arguments: type - an ace_type, argv[0] for the query
+ * name - name of machine, argv[1] for the query
+ * Returns: the top element of a queue returning the data, or NULL.
+ */
+
+struct mqelem *GetMachineByOwner(char *type, char *name)
+{
+ char *args[2];
+ struct mqelem *elem = NULL;
+ int status;
+
+ args[0] = type;
+ args[1] = name;
+ if ((status = do_mr_query("get_host_by_owner", 2, args, StoreInfo, &elem)))
+ {
+ com_err(program_name, status, " in get_host_by_owner");
+ return NULL;
+ }
+ return QueueTop(elem);
+}
+
+int MachineByAcctNumber(int argc, char **argv)
+{
+ char *args[0], *account_number;
+ int status;
+ struct mqelem *elem = NULL;
+
+ if (GetValueFromUser("Account Number", &account_number) == SUB_ERROR)
+ return DM_NORMAL;
+
+ args[0] = account_number;
+ if (status = do_mr_query("get_host_by_account_number", 1, args, StoreInfo,
+ &elem))
+ {
+ com_err(program_name, status, " in get_host_by_account_number");
+ return DM_NORMAL;
+ }
+ Loop(QueueTop(elem), (void (*)(char **)) PrintMachInfo);
+ FreeQueue(elem);
+
+ return DM_NORMAL;
+}
+
+int ShowContainerInfo(int argc, char **argv)
+{
+ struct mqelem *top;
+
+ top = GetMCInfo(CONTAINER, argv[1], NULL);
+ Loop(top, (void (*)(char **)) PrintContainerInfo);
+ FreeQueue(top);
+ return DM_NORMAL;
+}
+
+static char *PrintContainerInfo(char **info)
+{
+ char buf[BUFSIZ], tbuf[256];
+
+ Put_message("");
+ sprintf(buf, "Container: %-16s", info[CON_NAME]);
+ Put_message(buf);
+ sprintf(buf, "Description: %-16s", info[CON_DESCRIPT]);
+ Put_message(buf);
+ sprintf(buf, "Location: %-16s Contact: %s", info[CON_LOCATION],
+ info[CON_CONTACT]);
+ Put_message(buf);
+ sprintf(tbuf, "%s %s", info[CON_OWNER_TYPE],
+ strcmp(info[CON_OWNER_TYPE], "NONE") ? info[CON_OWNER_NAME] : "");
+ sprintf(buf, "Owner: %-16s", tbuf);
+ Put_message(buf);
+ sprintf(tbuf, "%s %s", info[CON_MEMACE_TYPE],
+ strcmp(info[CON_MEMACE_TYPE], "NONE") ? info[CON_MEMACE_NAME] : "");
+ sprintf(buf, "Membership ACL: %-16s", tbuf);
+ Put_message(buf);
+ Put_message("");
+ sprintf(buf, MOD_FORMAT, info[CON_MODBY], info[CON_MODTIME],
+ info[CON_MODWITH]);
+ Put_message(buf);
+ return info[CON_NAME];
+}
+
+static char *PrintContainer(char **info)
+{
+ char buf[BUFSIZ];
+
+ sprintf(buf, "Container: %s", info[CON_NAME]);
+ Put_message(buf);
+}
+
+static char *PrintMContMap(char **info)
+{
+ char buf[BUFSIZ];
+ sprintf(buf, "Container: %-30s Machine: %-20s",
+ info[1], info[0]);
+ Put_message(buf);
+ return "";
+}
+
+int AddContainer(int argc, char **argv)
+{
+ char **args, *info[MAX_ARGS_SIZE], *name = argv[1];
+ int stat;
+
+ /* Can't use ValidName() because spaces are allowed in container names */
+ if (IS_EMPTY(name))
+ {
+ Put_message("Please use a non-empty name.");
+ return DM_NORMAL;
+ }
+
+ if (strchr(name, '*') || strchr(name, '?'))
+ {
+ Put_message("Wildcards not accepted here.");
+ return DM_NORMAL;
+ }
+
+ /* Check if this cluster already exists. */
+ if ((stat = do_mr_query("get_container", 1, &name, NULL, NULL))
+ == MR_SUCCESS)
+ {
+ Put_message("This container already exists.");
+ return DM_NORMAL;
+ }
+ else if (stat != MR_NO_MATCH)
+ {
+ com_err(program_name, stat, " in AddContainer.");
+ return DM_NORMAL;
+ }
+ if (!(args = AskMCDInfo(SetContainerDefaults(info, name), CONTAINER, FALSE)))
+ {
+ Put_message("Aborted.");
+ FreeInfo(info);
+ return DM_NORMAL;
+ }
+
+ /* Create the new container. */
+ if ((stat = do_mr_query("add_container", CountArgs(args), args, NULL, NULL)))
+ com_err(program_name, stat, " in AddContainer.");
+
+ FreeInfo(info);
+ return DM_NORMAL;
+}
+
+int UpdateContainer(int argc, char **argv)
+{
+ struct mqelem *top;
+ top = GetMCInfo(CONTAINER, argv[1], NULL);
+ QueryLoop(top, NullPrint, RealUpdateContainer, "Update the container");
+
+ FreeQueue(top);
+ return DM_NORMAL;
+}
+
+static void RealUpdateContainer(char **info, Bool junk)
+{
+ int stat;
+ char **args = AskMCDInfo(info, CONTAINER, TRUE);
+
+ if (!args)
+ {
+ Put_message("Aborted.");
+ return;
+ }
+ if ((stat = do_mr_query("update_container", CountArgs(args), args,
+ NULL, NULL)))
+ com_err(program_name, stat, " in UpdateContainer.");
+ else
+ Put_message("Container successfully updated.");
+}
+
+int DeleteContainer(int argc, char **argv)
+{
+ struct mqelem *top;
+
+ top = GetMCInfo(CONTAINER, argv[1], NULL);
+ QueryLoop(top, PrintClusterInfo, RealDeleteContainer,
+ "Delete the container");
+
+ FreeQueue(top);
+ return DM_NORMAL;
+}
+
+static void RealDeleteContainer(char **info, Bool one_container)
+{
+ int stat;
+ char temp_buf[BUFSIZ];
+
+ sprintf(temp_buf,
+ "Are you sure you want to delete the container %s (y/n) ?",
+ info[CON_NAME]);
+ if (!one_container || Confirm(temp_buf))
+ {
+ if (CheckAndRemoveMachinesFromContainer(info[CON_NAME], TRUE)
+ != SUB_ERROR)
+ {
+ if ((stat = do_mr_query("delete_container", 1, &info[CON_NAME],
+ NULL, NULL)))
+ {
+ com_err(program_name, stat, " in delete_container.");
+ sprintf(temp_buf, "Container %s ** NOT ** deleted.",
+ info[CON_NAME]);
+ Put_message(temp_buf);
+ }
+ else
+ {
+ sprintf(temp_buf, "Container %s successfully deleted.",
+ info[CON_NAME]);
+ Put_message(temp_buf);
+ }
+ }
+ }
+}
+
+int CheckAndRemoveMachinesFromContainer(char *name, Bool ask_first)
+{
+ int stat, ret_value;
+ Bool delete_it;
+ char *args[10], temp_buf[BUFSIZ], *ptr;
+ struct mqelem *top, *elem = NULL;
+
+ ret_value = SUB_NORMAL;
+ args[0] = name;
+ args[1] = "0";
+ stat = do_mr_query("get_machines_of_container", 2, args, StoreInfo,
+ &elem);
+ if (stat && stat != MR_NO_MATCH)
+ {
+ com_err(program_name, stat, " in get_machines_of_container");
+ return DM_NORMAL;
+ }
+ if (stat == MR_SUCCESS)
+ {
+ elem = top = QueueTop(elem);
+ if (ask_first)
+ {
+ sprintf(temp_buf,
+ "The container %s has the following machines in it:", name);
+ Put_message(temp_buf);
+ while (elem)
+ {
+ char **info = elem->q_data;
+ Print(1, &info[0], (char *) NULL);
+ elem = elem->q_forw;
+ }
+ ptr = "Remove ** ALL ** these machines from this container?";
+
+ if (YesNoQuestion(ptr, FALSE) == TRUE) /* may return -1. */
+ delete_it = TRUE;
+ else
+ {
+ Put_message("Aborting...");
+ FreeQueue(top);
+ return SUB_ERROR;
+ }
+ }
+ else
+ delete_it = TRUE;
+
+ if (delete_it)
+ {
+ elem = top;
+ while (elem)
+ {
+ char **info = elem->q_data;
+ if ((stat = do_mr_query("delete_machine_from_container",
+ 2, info, NULL, NULL)))
+ {
+ ret_value = SUB_ERROR;
+ com_err(program_name, stat,
+ " in delete_machine_from_container.");
+ sprintf(temp_buf,
+ "Machine %s ** NOT ** removed from container %s.",
+ info[0], info[1]);
+ Put_message(temp_buf);
+ }
+ elem = elem->q_forw;
+ }
+ }
+ }
+ return ret_value;
+}
+
+int GetSubContainers(int argc, char **argv)
+{
+ char *args[2];
+ struct mqelem *elem = NULL, *top = NULL;
+ int stat;
+
+ args[0] = argv[1];
+
+ if (YesNoQuestion("Do you want a recursive search?", TRUE) == TRUE)
+ args[1] = "1";
+ else
+ args[1] = "0";
+
+ if (stat = do_mr_query("get_subcontainers_of_container", 2, args,
+ StoreInfo, &elem))
+ com_err(program_name, stat, " in get_subcontainers_of_container");
+
+ top = QueueTop(elem);
+ Loop(top, ((void (*)(char **)) PrintContainer));
+ FreeQueue(top);
+ return DM_NORMAL;
+}
+
+int MachineToContainerMap(int argc, char **argv)
+{
+ struct mqelem *elem, *top;
+ char *tmpname, temp_buf[256];
+
+ tmpname = canonicalize_hostname(strdup(argv[1]));
+ if (strcasecmp(tmpname, argv[1]) && *argv[1] != '"')
+ {
+ sprintf(temp_buf, "Warning: '%s' canonicalized to '%s'.",
+ argv[1], tmpname);
+ Put_message(temp_buf);
+ }
+ top = elem = GetMCInfo(CONTMAP, tmpname, NULL);
+
+ Put_message("");
+ while (elem)
+ {
+ char **info = elem->q_data;
+ PrintMContMap(info);
+ elem = elem->q_forw;
+ }
+
+ FreeQueue(top);
+ free(tmpname);
+ return DM_NORMAL;
+}
+
+int AddMachineToContainer(int argc, char **argv)
+{
+ int stat;
+ char *machine, *container, temp_buf[BUFSIZ], *args[10];
+ Bool add_it, one_machine, one_container;
+ struct mqelem *melem, *mtop, *celem, *ctop;
+
+ machine = canonicalize_hostname(strdup(argv[1]));
+ if (strcasecmp(machine, argv[1]) && *argv[1] != '"')
+ {
+ sprintf(temp_buf, "Warning: '%s' canonicalized to '%s'.",
+ argv[1], machine);
+ Put_message(temp_buf);
+ }
+ container = argv[2];
+
+ celem = ctop = GetMCInfo(CONTAINER, container, NULL);
+ melem = mtop = GetMCInfo(MACHINE, machine, NULL);
+ free(machine);
+
+ one_machine = (QueueCount(mtop) == 1);
+ one_container = (QueueCount(ctop) == 1);
+
+ while (melem)
+ {
+ char **minfo = melem->q_data;
+ while (celem)
+ {
+ char **cinfo = celem->q_data;
+ if (one_machine && one_container)
+ add_it = TRUE;
+ else
+ {
+ sprintf(temp_buf, "Add machine %s to container %s (y/n/q) ?",
+ minfo[M_NAME], cinfo[CON_NAME]);
+ switch (YesNoQuestion(temp_buf, FALSE))
+ {
+ case TRUE:
+ add_it = TRUE;
+ break;
+ case FALSE:
+ add_it = FALSE;
+ break;
+ default:
+ Put_message("Aborting...");
+ FreeQueue(ctop);
+ FreeQueue(mtop);
+ return DM_NORMAL;
+ }
+ }
+ if (add_it)
+ {
+ args[0] = minfo[M_NAME];
+ args[1] = cinfo[CON_NAME];
+ stat = do_mr_query("add_machine_to_container", 2, args, NULL,
+ NULL);
+ switch (stat)
+ {
+ case MR_SUCCESS:
+ break;
+ case MR_EXISTS:
+ sprintf(temp_buf, "%s is already in container %s",
+ minfo[M_NAME], cinfo[CON_NAME]);
+ Put_message(temp_buf);
+ break;
+ default:
+ com_err(program_name, stat, " in AddMachineToContainer.");
+ break;
+ }
+ }
+ celem = celem->q_forw;
+ }
+ celem = ctop;
+ melem = melem->q_forw;
+ }
+ FreeQueue(ctop);
+ FreeQueue(mtop);
+ return DM_NORMAL;
+}
+
+int RemoveMachineFromContainer(int argc, char **argv)
+{
+ struct mqelem *elem = NULL;
+ char buf[BUFSIZ], *args[10];
+ int stat;
+
+ args[0] = canonicalize_hostname(strdup(argv[1]));
+ if (strcasecmp(args[0], argv[1]) && *argv[1] != '"')
+ {
+ sprintf(buf, "Warning: '%s' canonicalized to '%s'.",
+ argv[1], args[0]);
+ Put_message(buf);
+ }
+ args[1] = argv[2];
+ args[2] = NULL;
+
+ stat = do_mr_query("get_machine_to_container_map", 1, args, StoreInfo,
+ &elem);
+ if (stat == MR_NO_MATCH)
+ {
+ sprintf(buf, "The machine %s is not in the container %s.",
+ args[0], args[1]);
+ Put_message(buf);
+ free(args[0]);
+ return DM_NORMAL;
+ }
+ if (stat != MR_SUCCESS)
+ com_err(program_name, stat, " in deleter_machine_from_container");
+
+ elem = QueueTop(elem);
+ QueryLoop(elem, PrintMContMap, RealRemoveMachineFromContainer,
+ "Remove this machine from this container");
+
+ FreeQueue(elem);
+ free(args[0]);
+ return DM_NORMAL;
+}
+
+static void RealRemoveMachineFromContainer(char **info, Bool one_contmap)
+{
+ char temp_buf[BUFSIZ];
+ int stat;
+
+ sprintf(temp_buf, "Remove %s from the container %s",
+ info[0], info[1]);
+ if (!one_contmap || Confirm(temp_buf))
+ {
+ if ((stat = do_mr_query("delete_machine_from_container", 2,
+ info, NULL, NULL)))
+ com_err(program_name, stat, " in delete_machine_from_container");
+ else
+ {
+ sprintf(temp_buf, "%s has been removed from the container %s.",
+ info[0], info[1]);
+ Put_message(temp_buf);
+ }
+ }
+ else
+ Put_message("Machine not removed.");
+}
+
+int GetMachinesOfContainer(int argc, char **argv)
+{
+ char *args[2];
+ struct mqelem *elem = NULL, *top = NULL;
+ int stat;
+
+ args[0] = argv[1];
+
+ if (YesNoQuestion("Do you want a recursive search?", TRUE) == TRUE)
+ args[1] = "1";
+ else
+ args[1] = "0";
+
+ if (stat = do_mr_query("get_machines_of_container", 2, args,
+ StoreInfo, &elem))
+ com_err(program_name, stat, " in get_machines_of_container");
+
+ top = QueueTop(elem);
+ Loop(top, ((void (*)(char **)) PrintMContMap));
+ FreeQueue(top);
+ return DM_NORMAL;
+}