1 /* 2 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 #include <getopt.h> 7 #include <grp.h> 8 #include <stdio.h> 9 #include <stdlib.h> 10 #include <unistd.h> 11 12 #include <OS.h> 13 14 #include <RegistrarDefs.h> 15 #include <user_group.h> 16 #include <util/KMessage.h> 17 18 #include "multiuser_utils.h" 19 20 21 extern const char *__progname; 22 23 24 static const char* kUsage = 25 "Usage: %s [ <options> ] <group name>\n" 26 "Deletes the specified group.\n" 27 "\n" 28 "Options:\n" 29 " -h, --help\n" 30 " Print usage info.\n" 31 ; 32 33 static void 34 print_usage_and_exit(bool error) 35 { 36 fprintf(error ? stderr : stdout, kUsage, __progname); 37 exit(error ? 1 : 0); 38 } 39 40 41 int 42 main(int argc, const char* const* argv) 43 { 44 while (true) { 45 static struct option sLongOptions[] = { 46 { "help", no_argument, 0, 'h' }, 47 { 0, 0, 0, 0 } 48 }; 49 50 opterr = 0; // don't print errors 51 int c = getopt_long(argc, (char**)argv, "h", sLongOptions, NULL); 52 if (c == -1) 53 break; 54 55 56 switch (c) { 57 case 'h': 58 print_usage_and_exit(false); 59 break; 60 61 default: 62 print_usage_and_exit(true); 63 break; 64 } 65 } 66 67 if (optind != argc - 1) 68 print_usage_and_exit(true); 69 70 const char* group = argv[optind]; 71 72 if (geteuid() != 0) { 73 fprintf(stderr, "Error: Only root may delete groups.\n"); 74 exit(1); 75 } 76 77 if (getgrnam(group) == NULL) { 78 fprintf(stderr, "Error: Group \"%s\" doesn't exists.\n", group); 79 exit(1); 80 } 81 82 // prepare request for the registrar 83 KMessage message(BPrivate::B_REG_DELETE_GROUP); 84 if (message.AddString("name", group) != B_OK) { 85 fprintf(stderr, "Error: Out of memory!\n"); 86 exit(1); 87 } 88 89 // send the request 90 KMessage reply; 91 status_t error = send_authentication_request_to_registrar(message, reply); 92 if (error != B_OK) { 93 fprintf(stderr, "Error: Failed to delete group: %s\n", strerror(error)); 94 exit(1); 95 } 96 97 return 0; 98 } 99