tcp: Fix 64 bit build with debugging features enabled.
[haiku.git] / src / bin / multiuser / userdel.cpp
blob7acdb7ed81c58d6b47595b5a359e91454f78e70b
1 /*
2 * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3 * Distributed under the terms of the MIT License.
4 */
6 #include <getopt.h>
7 #include <pwd.h>
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <unistd.h>
12 #include <OS.h>
14 #include <RegistrarDefs.h>
15 #include <user_group.h>
16 #include <util/KMessage.h>
18 #include "multiuser_utils.h"
21 extern const char *__progname;
24 static const char* kUsage =
25 "Usage: %s [ <options> ] <user name>\n"
26 "Deletes the specified user.\n"
27 "\n"
28 "Options:\n"
29 " -h, --help\n"
30 " Print usage info.\n"
33 static void
34 print_usage_and_exit(bool error)
36 fprintf(error ? stderr : stdout, kUsage, __progname);
37 exit(error ? 1 : 0);
41 int
42 main(int argc, const char* const* argv)
44 while (true) {
45 static struct option sLongOptions[] = {
46 { "help", no_argument, 0, 'h' },
47 { 0, 0, 0, 0 }
50 opterr = 0; // don't print errors
51 int c = getopt_long(argc, (char**)argv, "h", sLongOptions, NULL);
52 if (c == -1)
53 break;
56 switch (c) {
57 case 'h':
58 print_usage_and_exit(false);
59 break;
61 default:
62 print_usage_and_exit(true);
63 break;
67 if (optind != argc - 1)
68 print_usage_and_exit(true);
70 const char* user = argv[optind];
72 if (geteuid() != 0) {
73 fprintf(stderr, "Error: Only root may delete users.\n");
74 exit(1);
77 if (getpwnam(user) == NULL) {
78 fprintf(stderr, "Error: User \"%s\" doesn't exist.\n", user);
79 exit(1);
82 // prepare request for the registrar
83 KMessage message(BPrivate::B_REG_DELETE_USER);
84 if (message.AddString("name", user) != B_OK) {
85 fprintf(stderr, "Error: Out of memory!\n");
86 exit(1);
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 user: %s\n", strerror(error));
94 exit(1);
97 return 0;