1 #if (!defined(lint) && !defined(SABER))
2 static char rcsid_module_c[] = "$Header$";
5 /* This is the file delete.c for the MOIRA Client, which allows a nieve
6 * user to quickly and easily maintain most parts of the MOIRA database.
7 * It Contains: functions for deleting users and lists.
10 * By: Chris D. Peterson
16 * Copyright 1988 by the Massachusetts Institute of Technology.
18 * For further information on copyright and distribution
19 * see the file mit-copyright.h
25 #include <moira_site.h>
28 #include "mit-copyright.h"
33 /* Function Name: CheckListForDeletion
34 * Description: Check one of the lists in which we just removed a member.
35 * if the list is empty then it will delete it.
36 * Arguments: name - name of the list to check.
37 * verbose - verbose mode?
42 CheckListForDeletion(name, verbose)
46 struct qelem *elem = NULL;
48 char *args[2], buf[BUFSIZ], **info;
50 if ( (status = do_mr_query("count_members_of_list", 1, &name, StoreInfo,
51 (char *) &elem)) != 0) {
52 com_err(program_name, status,
53 " in DeleteList (count_members_of_list).");
56 info = (char **) elem->q_data;
57 if ( strcmp(info[NAME],"0") == 0) {
59 sprintf(buf, "Delete the empty list %s? ", name);
60 if (YesNoQuestion(buf, FALSE) != TRUE) {
61 Put_message("Aborting Deletion!");
66 args[0] = "foo"; /* not used. */
73 /* Function Name: CheckAce
74 * Description: Checks an ace to see of we should delete it.
75 * Arguments: type - the type of this ace.
76 * name - the name of the ace.
77 * verbose - query user?
82 CheckAce(type, name, verbose)
86 char *args[2], buf[BUFSIZ];
89 if ( strcmp(type, "LIST") != 0 )
90 return; /* If the ace is not a list the ignore it. */
94 status = do_mr_query("get_ace_use", 2, args, NullFunc, (char *) NULL);
95 if (status != MR_NO_MATCH)
96 return; /* If this query fails the ace will
97 not be deleted even if it is empty. */
99 sprintf(buf, "Delete the unused Access Control Entity (ACE) %s? ",
101 if ( YesNoQuestion(buf, FALSE) != TRUE) {
102 Put_message("Aborting Deletion!");
109 * NOTE: Delete list expects only the name of the list to delete in argv[1].
110 * since, 'args' already satisfies this, there is no need to create
111 * a special argument list.
117 /* Function Name: CheckIfAce
118 * Description: Checks to see if this is an ace of another data object.
119 * Arguments: name - name of the object.
120 * Returns: SUB_ERROR if this list is an ace, or if the query did not
125 CheckIfAce(name, type, verbose)
129 char * args[2], buf[BUFSIZ], **info;
130 struct qelem *local, *elem;
136 switch (status = do_mr_query("get_ace_use", 2, args,
137 StoreInfo, (char *) &elem)) {
141 local = elem = QueueTop(elem);
142 info = (char **) local->q_data;
143 if (QueueCount(elem) == 1 &&
144 !strcmp(info[0], "LIST") &&
145 !strcmp(info[1], name)) {
150 sprintf(buf, "%s %s %s", type, name,
151 "is the ACE for the following data objects:");
154 while (local != NULL) {
155 info = (char **) local->q_data;
156 if (!strcmp(info[0], "LIST") &&
157 !strcmp(info[1], name))
159 Print(CountArgs(info), info, NULL);
160 local = local->q_forw;
164 "The ACE for each of these items must be changed before");
165 sprintf(buf,"the %s %s can be deleted.\n", type, name);
170 com_err(program_name, status, " in CheckIfAce (get_ace_use).");
177 /* Function Name: RemoveItemFromLists
178 * Description: this function removes a list from all other lists of
179 * which it is a member.
180 * Arguments: name - name of the item
181 * elem - a pointer to a queue element. RETURNED
182 * verbose - verbose mode.
183 * Returns: SUB_ERROR if there is an error.
187 RemoveItemFromLists(name, type, elem, verbose)
189 struct qelem ** elem;
193 char *args[10], temp_buf[BUFSIZ];
202 * Get all list of which this item is a member, and store them in a queue.
205 status = do_mr_query("get_lists_of_member", 2, args, StoreInfo,
208 if (status == MR_NO_MATCH)
211 if (status != MR_SUCCESS) {
212 com_err(program_name, status, " in DeleteList (get_lists_of_member).");
217 * If verbose mode, ask user of we should remove our list from
221 local = *elem = QueueTop(*elem);
222 lists = QueueCount(*elem);
226 sprintf(temp_buf, "%s %s is a member of %d other list%s.\n", type,
227 name, lists, ((lists == 1) ? "" : "s") );
228 Put_message(temp_buf);
229 while (local != NULL) {
230 char ** info = (char **) local->q_data;
231 Print( 1, &info[GLOM_NAME], (char *) NULL);
232 local = local->q_forw;
234 Put_message(" "); /* Blank Line. */
235 sprintf(temp_buf,"Remove %s %s from these lists? ", type, name);
236 if (YesNoQuestion(temp_buf, FALSE) != TRUE) {
237 Put_message("Aborting...");
245 * Remove this list from all lists that it is a member of.
249 args[DM_MEMBER] = name;
250 args[DM_TYPE] = type;
251 while (local != NULL) {
252 char ** info = (char **) local->q_data;
253 args[DM_LIST] = info[GLOM_NAME];
254 if ( (status = do_mr_query("delete_member_from_list",
255 3, args, Scream, NULL)) != 0) {
256 com_err(program_name, status, " in delete_member\nAborting\n");
260 local = local->q_forw;
265 /* Function Name: RemoveMembersOfList
266 * Description: Deletes the members of the list.
267 * Arguments: name - name of the list.
268 * verbose - query user, about deletion?
269 * Returns: SUB_ERROR - if we could not delete, or the user abouted.
273 RemoveMembersOfList(name, verbose)
277 char buf[BUFSIZ], *args[10];
278 struct qelem *local, *elem = NULL;
281 * Get the members of this list.
283 status = do_mr_query("get_members_of_list", 1, &name, StoreInfo,
285 if (status == MR_NO_MATCH)
289 com_err(program_name, status, " in DeleteList (get_members_of_list).");
293 * If verbose mode, then ask the user if we should delete.
295 local = elem = QueueTop(elem);
296 if ( (members = QueueCount(elem)) == 0)
299 sprintf(buf, "List %s has %d member%s:", name, QueueCount(elem),
300 ((members == 1) ? "" : "s") );
302 Put_message(" "); /* Blank Line. */
303 while (local != NULL) {
304 char ** info = (char **) local->q_data;
305 Print( CountArgs(info), info, NULL);
306 local = local->q_forw;
308 Put_message(" "); /* Blank Line. */
309 sprintf(buf, "Remove th%s member%s from list %s? ",
310 ((members == 1) ? "is" : "ese"),
311 ((members == 1) ? "" : "s"), name );
312 if ( YesNoQuestion(buf, FALSE) != TRUE) {
313 Put_message("Aborting...");
319 * Perform The Removal.
323 while (local != NULL) {
324 char ** info = (char **) local->q_data;
327 if ( (status = do_mr_query("delete_member_from_list",
328 3, args, Scream, NULL)) != 0) {
329 com_err(program_name, status, " in delete_member\nAborting\n");
333 local = local->q_forw;
338 /* Function Name: DeleteUserGroup
339 * Description: Deletes the list given by name if it exists.
340 * intended to be used to delete user groups
341 * Arguments: name - the name of the list to delete.
342 * verbose - flag that if TRUE queries the user to
343 * ask if list should be deleted.
344 * Returns: MR_ERROR if there is an error.
348 DeleteUserGroup(name, verbose)
353 char buf[BUFSIZ], *args[10];
355 status = do_mr_query("get_list_info", 1, &name, NullFunc, (char *) NULL);
358 sprintf(buf, "There is also a list named %s, delete it?", name);
359 ans = YesNoQuestion(buf, FALSE);
361 Put_message("Leaving group alone.");
365 Put_message("Aborting...\n");
369 /* ans == TRUE || ~verbose */
370 args[0] = "foo"; /* not used. */
374 else if (status != MR_NO_MATCH) {
375 com_err(program_name, status, " Aborting Delete User.");
381 /* Function Name: DeleteHomeFilesys
382 * Description: Delete the home filesystem for the named user.
383 * Arguments: name - name of the user (and filsystem) to delete.
384 * verbose - if TRUE query user.
385 * Returns: SUB_NORMAL if home filesystem deleted, or nonexistant.
389 DeleteHomeFilesys(name, verbose)
396 switch (status = do_mr_query("get_filesys_by_label", 1, &name, NullFunc,
402 sprintf(buf, "Delete the filesystem named %s (y/n)?", name);
403 switch (YesNoQuestion(buf, FALSE)) {
405 Put_message("Filesystem Not Deleted, continuing...\n");
410 Put_message("Filesystem Not Deleted, aborting...\n\n");
414 if ( (status = do_mr_query("delete_filesys", 1, &name, Scream,
415 (char *) NULL) ) != MR_SUCCESS) {
416 com_err(program_name, status, " in delete_filesys.");
420 Put_message("Filesystem Successfully Deleted.");
423 com_err(program_name, status, " in get_filesystem_by_label).");
430 /* Function Name: RealDeleteUser
431 * Description: Just Deletes the user.
432 * Arguments: name - name of User to delete
433 * Returns: SUB_ERROR if the deletion failed.
443 if ( (status = do_mr_query("delete_user", 1, &name, Scream,
444 (char *) NULL)) != MR_SUCCESS) {
445 com_err(program_name, status, ": user not deleted");
448 (void) sprintf(buf, "User %s deleted.", name);
454 /* Function Name: RealDeleteList
455 * Description: Just Deletes the list.
456 * Arguments: name - name of list to delete
457 * Returns: SUB_ERROR if the deletion failed.
467 if ( (status = do_mr_query("delete_list", 1, &name, Scream,
468 (char *) NULL)) != MR_SUCCESS) {
469 com_err(program_name, status, ": list not deleted");
472 (void) sprintf(buf, "List %s deleted.", name);
478 /* Function Name: AttemptToDeleteList
479 * Description: Atempts to delete list, in the following manner:
480 * 1) try to delet it, if this fails in a known error then
481 * a) try to clean up each of those known methods, or
482 * at least explain why we failed.
483 * Arguments: list_info - info about this list.
484 * ask_first - (T/F) query user before preparing for deletion,
486 * Returns: none - all is taken care of and error messages printed
487 * one way or the other.
491 AttemptToDeleteList(list_info, ask_first)
496 struct qelem *local, *member_of;
497 char *name = list_info[L_NAME];
501 * Attempt delete. - will only work if:
502 * 1) This list has no members.
503 * 2) This list in a member of no other lists.
504 * 3) This list is not an ace of another object.
507 switch (status = do_mr_query("delete_list", 1, &name,
508 Scream, (char *) NULL)) {
510 Put_message("List Sucessfully Deleted.");
511 CheckAce(list_info[L_ACE_TYPE], list_info[L_ACE_NAME], ask_first);
515 * This list is in use. Try to find out why,
516 * and for the cases where we have a good idea of
517 * what to do we will query and then do it.
520 if ((CheckIfAce(name, "list", ask_first) != SUB_NORMAL) ||
521 (RemoveItemFromLists(name, "list",
522 &member_of, ask_first) != SUB_NORMAL))
525 * If the list is it's own ACL, then make the person performing
526 * the delete the owner before removing this person from the list
528 if (!strcmp(list_info[L_ACE_TYPE], "LIST") &&
529 !strcmp(list_info[L_ACE_NAME], list_info[L_NAME])) {
530 free(list_info[L_ACE_TYPE]);
531 free(list_info[L_ACE_NAME]);
532 list_info[L_ACE_TYPE] = Strsave("USER");
533 list_info[L_ACE_NAME] = Strsave(user);
534 SlipInNewName(list_info, Strsave(list_info[L_NAME]));
535 if ((status = do_mr_query("update_list", CountArgs(list_info)-3,
536 list_info, Scream, (char *) NULL))
538 com_err(program_name, status, " while updating list owner");
539 Put_message("List may be only partly deleted.");
542 if ((RemoveMembersOfList(name, ask_first) == SUB_NORMAL) &&
543 (RealDeleteList(name) == SUB_NORMAL) )
545 CheckAce(list_info[L_ACE_TYPE], list_info[L_ACE_NAME], ask_first);
547 local = QueueTop(member_of);
548 while (local != NULL) {
549 char ** info = (char **) local->q_data;
550 CheckListForDeletion(info[LM_LIST], ask_first);
551 local = local->q_forw;
553 FreeQueue(member_of);
557 com_err(program_name, status, " in DeleteList (delete_list).");
562 /* Function Name: DeleteList
563 * Description: deletes a list
564 * Arguments: argc, argv - standard MR argc and argv.
565 * Returns: DM Status Code.
570 DeleteList(argc, argv)
575 struct qelem *top, *list;
581 switch(status = do_mr_query("get_list_info", 1, argv + 1,
582 StoreInfo, (char *) &list)){
585 /* case MR_NO_WILDCARD:
586 Put_message("Wildcards are not accepted here.");
590 Put_message("There is no list that matches that name.");
593 com_err(program_name, status, " in DeleteList (get_list_info).");
597 top = list = QueueTop(list);
598 one_list = (QueueCount(list) == 1);
599 while (list != NULL) {
600 char ** info = (char**) list->q_data;
602 sprintf( buf, "Are you sure that you want to delete the list %s",
604 if ( Confirm(buf) ) AttemptToDeleteList(info, TRUE);
607 sprintf(buf, "Delete the list %s", info[L_NAME]);
608 switch( YesNoQuestion( buf, FALSE ) ) {
610 AttemptToDeleteList(info, TRUE);
615 Put_message ("Aborting...");
626 /* Function Name: DeleteUser
627 * Description: Deletes a user from the database.
628 * Arguments: argc, argv - name of the user in argv[1].
629 * Returns: DM_NORMAL.
634 DeleteUser(argc, argv)
640 char * name = argv[1]; /* name of the user we are deleting. */
642 struct qelem *local, *member_of = NULL;
645 if (!ValidName(name))
648 if (!Confirm("Are you sure that you want to delete this user?"))
651 status = do_mr_query("delete_user", 1, &name, Scream, (char *) NULL);
652 if (status != MR_IN_USE && status != 0) {
653 com_err(program_name, status, ": user not deleted");
657 sprintf(buf,"User %s deleted.", name);
660 /* delete this return if the policy decision below is reversed */
665 /* Design decision not to allow registered users to be deleted.
667 Put_message("Sorry, registered users cannot be deleted from the database.");
668 Put_message("Deactivate the user now, and the system manager will expunge later.");
670 else if (status == MR_IN_USE) {
674 * 1) Query - Delete home filesytem.
675 * 2) Query - Delete user Group.
676 * 2) Is the user an ACE of any object in the database?
677 * 3) Query - Remove user from all list of which he is a member.
679 * If all these have been accomplished, then attempt to delete the user again.
681 if ( (DeleteHomeFilesys(name, TRUE) == SUB_ERROR) ||
682 (DeleteUserGroup(name, TRUE) == SUB_ERROR) ||
683 (CheckIfAce(name, "user", TRUE) == SUB_ERROR) ||
684 (RemoveItemFromLists(name, "user",
685 &member_of, TRUE) == SUB_ERROR) ||
686 (RealDeleteUser(name) == SUB_ERROR) ) {
692 * Query - Delete all empty lists created by removing this user from them.
696 while (local != NULL) {
697 char ** info = (char **) local->q_data;
698 CheckListForDeletion(info[0], TRUE);
699 local = local->q_forw;
702 FreeQueue(member_of); /* Free memory and return. */