1 #if (!defined(lint) && !defined(SABER))
2 static char rcsid_module_c[] = "$Header$";
5 /* This is the file delete.c for the SMS Client, which allows a nieve
6 * user to quickly and easily maintain most parts of the SMS 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
27 #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 = sms_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 = sms_query("get_ace_use", 2, args, NullFunc, (char *) NULL);
95 if (status != SMS_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 noe
125 CheckIfAce(name, type, verbose)
129 char * args[2], buf[BUFSIZ];
130 struct qelem *local, *elem;
136 switch (status = sms_query("get_ace_use", 2, args,
137 StoreInfo, (char *) &elem)) {
141 local = elem = QueueTop(elem);
143 sprintf(buf, "%s %s %s %s", type, name,
144 "is the Access Control List (ACE) for the following data",
147 while (local != NULL) {
148 char ** info = (char **) local->q_data;
149 Print( CountArgs(info), info, NULL);
150 local = local->q_forw;
153 "The ACE for each of these items must be changed before");
154 sprintf(buf,"the %s %s can be deleted.\n", type, name);
159 com_err(program_name, status, " in CheckIfAce (get_ace_use).");
166 /* Function Name: RemoveItemFromLists
167 * Description: this function removes a list from all other lists of
168 * which it is a member.
169 * Arguments: name - name of the item
170 * elem - a pointer to a queue element. RETURNED
171 * verbose - verbose mode.
172 * Returns: SUB_ERROR if there is an error.
176 RemoveItemFromLists(name, type, elem, verbose)
178 struct qelem ** elem;
182 char *args[10], temp_buf[BUFSIZ];
191 * Get all list of which this item is a member, and store them in a queue.
194 status = sms_query("get_lists_of_member", 2, args, StoreInfo,
197 if (status == SMS_NO_MATCH)
200 if (status != SMS_SUCCESS) {
201 com_err(program_name, status, " in DeleteList (get_lists_of_member).");
206 * If verbose mode, ask user of we should remove our list from
210 local = *elem = QueueTop(*elem);
211 lists = QueueCount(*elem);
215 sprintf(temp_buf, "%s %s is a member of %d other list%s.\n", type,
216 name, lists, ((lists == 1) ? "" : "s") );
217 Put_message(temp_buf);
218 while (local != NULL) {
219 char ** info = (char **) local->q_data;
220 Print( 1, &info[GLOM_NAME], (char *) NULL);
221 local = local->q_forw;
223 Put_message(" "); /* Blank Line. */
224 sprintf(temp_buf,"Remove %s %s from these lists? ", type, name);
225 if (YesNoQuestion(temp_buf, FALSE) != TRUE) {
226 Put_message("Aborting...");
234 * Remove this list from all lists that it is a member of.
238 args[DM_MEMBER] = name;
239 args[DM_TYPE] = type;
240 while (local != NULL) {
241 char ** info = (char **) local->q_data;
242 args[DM_LIST] = info[GLOM_NAME];
243 if ( (status = sms_query("delete_member_from_list",
244 3, args, Scream, NULL)) != 0) {
245 com_err(program_name, status, " in delete_member\nAborting\n");
249 local = local->q_forw;
254 /* Function Name: RemoveMembersOfList
255 * Description: Deletes the members of the list.
256 * Arguments: name - name of the list.
257 * verbose - query user, about deletion?
258 * Returns: SUB_ERROR - if we could not delete, or the user abouted.
262 RemoveMembersOfList(name, verbose)
266 char buf[BUFSIZ], *args[10];
267 struct qelem *local, *elem = NULL;
270 * Get the members of this list.
272 status = sms_query("get_members_of_list", 1, &name, StoreInfo,
274 if (status == SMS_NO_MATCH)
278 com_err(program_name, status, " in DeleteList (get_members_of_list).");
282 * If verbose mode, then ask the user if we should delete.
284 local = elem = QueueTop(elem);
285 if ( (members = QueueCount(elem)) == 0)
288 sprintf(buf, "List %s has %d member%s:", name, QueueCount(elem),
289 ((members == 1) ? "" : "s") );
291 Put_message(" "); /* Blank Line. */
292 while (local != NULL) {
293 char ** info = (char **) local->q_data;
294 Print( CountArgs(info), info, NULL);
295 local = local->q_forw;
297 Put_message(" "); /* Blank Line. */
298 sprintf(buf, "Remove th%s member%s from list %s? ",
299 ((members == 1) ? "is" : "ese"),
300 ((members == 1) ? "" : "s", name) );
301 if ( YesNoQuestion(buf, FALSE) != TRUE) {
302 Put_message("Aborting...");
308 * Perform The Removal.
312 while (local != NULL) {
313 char ** info = (char **) local->q_data;
316 if ( (status = sms_query("delete_member_from_list",
317 3, args, Scream, NULL)) != 0) {
318 com_err(program_name, status, " in delete_member\nAborting\n");
322 local = local->q_forw;
327 /* Function Name: DeleteUserGroup
328 * Description: Deletes the list given by name if it exists.
329 * intended to be used to delete user groups
330 * Arguments: name - the name of the list to delete.
331 * verbose - flag that if TRUE queries the user to
332 * ask if list should be deleted.
333 * Returns: SMS_ERROR if there is an error.
337 DeleteUserGroup(name, verbose)
342 char buf[BUFSIZ], *args[10];
344 status = sms_query("get_list_info", 1, &name, NullFunc, (char *) NULL);
347 sprintf(buf, "There is also a list named %s, delete it?", name);
348 ans = YesNoQuestion(buf, FALSE);
350 Put_message("Leaving group alone.");
354 Put_message("Aborting...\n");
358 /* ans == TRUE || ~verbose */
359 args[0] = "foo"; /* not used. */
363 else if (status != SMS_NO_MATCH) {
364 com_err(program_name, status, " Aborting Delete User.");
370 /* Function Name: DeleteHomeFilesys
371 * Description: Delete the home filesystem for the named user.
372 * Arguments: name - name of the user (and filsystem) to delete.
373 * verbose - if TRUE query user.
374 * Returns: SUB_NORMAL if home filesystem deleted, or nonexistant.
378 DeleteHomeFilesys(name, verbose)
385 switch (status = sms_query("get_filesys_by_label", 1, &name, NullFunc,
391 sprintf(buf, "Delete the filesystem named %s (y/n)?", name);
392 switch (YesNoQuestion(buf, FALSE)) {
394 Put_message("Filesystem Not Deleted, continuing...\n");
399 Put_message("Filesystem Not Deleted, aborting...\n\n");
403 if ( (status = sms_query("delete_filesys", 1, &name, Scream,
404 (char *) NULL) ) != SMS_SUCCESS) {
405 com_err(program_name, status, " in delete_filesys.");
409 Put_message("Filesystem Successfully Deleted.");
412 com_err(program_name, status, " in get_filesystem_by_label).");
418 /* Function Name: RealDeleteUser
419 * Description: Just Deletes the user.
420 * Arguments: name - name of User to delete
421 * Returns: SUB_ERROR if the deletion failed.
431 if ( (status = sms_query("delete_user", 1, &name, Scream,
432 (char *) NULL)) != SMS_SUCCESS) {
433 com_err(program_name, status, ": user not deleted");
436 (void) sprintf(buf, "User %s deleted.", name);
441 /* Function Name: RealDeleteList
442 * Description: Just Deletes the list.
443 * Arguments: name - name of list to delete
444 * Returns: SUB_ERROR if the deletion failed.
454 if ( (status = sms_query("delete_list", 1, &name, Scream,
455 (char *) NULL)) != SMS_SUCCESS) {
456 com_err(program_name, status, ": list not deleted");
459 (void) sprintf(buf, "List %s deleted.", name);
465 /* Function Name: AttemptToDeleteList
466 * Description: Atempts to delete list, in the following manner:
467 * 1) try to delet it, if this fails in a known error then
468 * a) try to clean up each of those known methods, or
469 * at least explain why we failed.
470 * Arguments: list_info - info about this list.
471 * ask_first - (T/F) query user before preparing for deletion,
473 * Returns: none - all is taken care of and error messages printed
474 * one way or the other.
478 AttemptToDeleteList(list_info, ask_first)
483 struct qelem *local, *member_of;
484 char *name = list_info[L_NAME];
488 * Attempt delete. - will only work if:
489 * 1) This list has no members.
490 * 2) This list in a member of no other lists.
491 * 3) This list is not an ace of another object.
494 switch (status = sms_query("delete_list", 1, &name,
495 Scream, (char *) NULL)) {
497 Put_message("List Sucessfully Deleted.");
498 CheckAce(list_info[L_ACE_TYPE], list_info[L_ACE_NAME], ask_first);
502 * This list is in use. Try to find out why,
503 * and for the cases where we have a good idea of
504 * what to do we will query and then do it.
507 if ( (CheckIfAce(name, "list", ask_first) == SUB_NORMAL) &&
508 (RemoveMembersOfList(name, ask_first) == SUB_NORMAL) &&
509 (RemoveItemFromLists(name, "list",
510 &member_of, ask_first) == SUB_NORMAL) &&
511 (RealDeleteList(name) == SUB_NORMAL) )
513 CheckAce(list_info[L_ACE_TYPE], list_info[L_ACE_NAME], ask_first);
515 local = QueueTop(member_of);
516 while (local != NULL) {
517 char ** info = (char **) local->q_data;
518 CheckListForDeletion(info[LM_LIST], ask_first);
519 local = local->q_forw;
521 FreeQueue(member_of);
525 com_err(program_name, status, " in DeleteList (delete_list).");
530 /* Function Name: DeleteList
531 * Description: deletes a list
532 * Arguments: argc, argv - standard SMS argc and argv.
533 * Returns: DM Status Code.
538 DeleteList(argc, argv)
543 struct qelem *top, *list;
549 switch(status = sms_query("get_list_info", 1, argv + 1, StoreInfo, &list)){
552 /* case SMS_NO_WILDCARD:
553 Put_message("Wildcards are not accepted here.");
555 */ case SMS_NO_MATCH:
557 Put_message("There is no list that matches that name.");
560 com_err(program_name, status, " in DeleteList (get_list_info).");
564 top = list = QueueTop(list);
565 one_list = (QueueCount(list) == 1);
566 while (list != NULL) {
567 char ** info = (char**) list->q_data;
569 sprintf( buf, "Are you sure that you want to delete the list %s",
571 if ( Confirm(buf) ) AttemptToDeleteList(info, TRUE);
574 sprintf(buf, "Delete the list %s", info[L_NAME]);
575 switch( YesNoQuestion( buf, FALSE ) ) {
577 AttemptToDeleteList(info, TRUE);
582 Put_message ("Aborting...");
593 /* Function Name: DeleteUser
594 * Description: Deletes a user from the database.
595 * Arguments: argc, argv - name of the user in argv[1].
596 * Returns: DM_NORMAL.
601 DeleteUser(argc, argv)
607 char * name = argv[1]; /* name of the user we are deleting. */
608 struct qelem *local, *member_of = NULL;
610 if (!ValidName(name))
613 if (!Confirm("Are you sure that you want to delete this user?"))
616 status = sms_query("delete_user", 1, &name, Scream, (char *) NULL);
617 if (status != SMS_IN_USE && status != 0) {
618 com_err(program_name, status, ": user not deleted");
622 sprintf(buf,"User %s deleted.", name);
625 else if (status == SMS_IN_USE) {
629 * 1) Query - Delete home filesytem.
630 * 2) Query - Delete user Group.
631 * 2) Is the user an ACE of any object in the database?
632 * 3) Query - Remove user from all list of which he is a member.
634 * If all these have been accomplished, then attempt to delete the user again.
636 if ( (DeleteHomeFilesys(name, TRUE) == SUB_ERROR) ||
637 (DeleteUserGroup(name, TRUE) == SUB_ERROR) ||
638 (CheckIfAce(name, "user", TRUE) == SUB_ERROR) ||
639 (RemoveItemFromLists(name, "user",
640 &member_of, TRUE) == SUB_ERROR) ||
641 (RealDeleteUser(name) == SUB_ERROR) ) {
647 * Query - Delete all empty lists created by removing this user from them.
651 while (local != NULL) {
652 char ** info = (char **) local->q_data;
653 CheckListForDeletion(info[0], TRUE);
654 local = local->q_forw;
657 FreeQueue(member_of); /* Free memory and return. */
665 * c-continued-statement-offset: 4
667 * c-argdecl-indent: 4