1519bb60aSIngo Weinhold/*
2519bb60aSIngo Weinhold * Copyright 2013, Ingo Weinhold, ingo_weinhold@gmx.de.
3519bb60aSIngo Weinhold * Distributed under the terms of the MIT License.
4519bb60aSIngo Weinhold */
5519bb60aSIngo Weinhold
6519bb60aSIngo Weinhold#include <getopt.h>
7519bb60aSIngo Weinhold#include <grp.h>
8519bb60aSIngo Weinhold#include <stdio.h>
9519bb60aSIngo Weinhold#include <stdlib.h>
10519bb60aSIngo Weinhold#include <unistd.h>
11519bb60aSIngo Weinhold
12519bb60aSIngo Weinhold#include <OS.h>
13519bb60aSIngo Weinhold
14519bb60aSIngo Weinhold#include <RegistrarDefs.h>
15519bb60aSIngo Weinhold#include <user_group.h>
16519bb60aSIngo Weinhold#include <util/KMessage.h>
17519bb60aSIngo Weinhold
18519bb60aSIngo Weinhold#include "multiuser_utils.h"
19519bb60aSIngo Weinhold
20519bb60aSIngo Weinhold
21519bb60aSIngo Weinholdextern const char *__progname;
22519bb60aSIngo Weinhold
23519bb60aSIngo Weinhold
24519bb60aSIngo Weinholdstatic const char* kUsage =
25519bb60aSIngo Weinhold	"Usage: %s [ <options> ] <group name>\n"
26519bb60aSIngo Weinhold	"Deletes the specified group.\n"
27519bb60aSIngo Weinhold	"\n"
28519bb60aSIngo Weinhold	"Options:\n"
29519bb60aSIngo Weinhold	"  -h, --help\n"
30519bb60aSIngo Weinhold	"    Print usage info.\n"
31519bb60aSIngo Weinhold	;
32519bb60aSIngo Weinhold
33519bb60aSIngo Weinholdstatic void
34519bb60aSIngo Weinholdprint_usage_and_exit(bool error)
35519bb60aSIngo Weinhold{
36519bb60aSIngo Weinhold	fprintf(error ? stderr : stdout, kUsage, __progname);
37519bb60aSIngo Weinhold	exit(error ? 1 : 0);
38519bb60aSIngo Weinhold}
39519bb60aSIngo Weinhold
40519bb60aSIngo Weinhold
41519bb60aSIngo Weinholdint
42519bb60aSIngo Weinholdmain(int argc, const char* const* argv)
43519bb60aSIngo Weinhold{
44519bb60aSIngo Weinhold	while (true) {
45519bb60aSIngo Weinhold		static struct option sLongOptions[] = {
46519bb60aSIngo Weinhold			{ "help", no_argument, 0, 'h' },
47519bb60aSIngo Weinhold			{ 0, 0, 0, 0 }
48519bb60aSIngo Weinhold		};
49519bb60aSIngo Weinhold
50519bb60aSIngo Weinhold		opterr = 0; // don't print errors
51519bb60aSIngo Weinhold		int c = getopt_long(argc, (char**)argv, "h", sLongOptions, NULL);
52519bb60aSIngo Weinhold		if (c == -1)
53519bb60aSIngo Weinhold			break;
54519bb60aSIngo Weinhold
55519bb60aSIngo Weinhold
56519bb60aSIngo Weinhold		switch (c) {
57519bb60aSIngo Weinhold			case 'h':
58519bb60aSIngo Weinhold				print_usage_and_exit(false);
59519bb60aSIngo Weinhold				break;
60519bb60aSIngo Weinhold
61519bb60aSIngo Weinhold			default:
62519bb60aSIngo Weinhold				print_usage_and_exit(true);
63519bb60aSIngo Weinhold				break;
64519bb60aSIngo Weinhold		}
65519bb60aSIngo Weinhold	}
66519bb60aSIngo Weinhold
67519bb60aSIngo Weinhold	if (optind != argc - 1)
68519bb60aSIngo Weinhold		print_usage_and_exit(true);
69519bb60aSIngo Weinhold
70519bb60aSIngo Weinhold	const char* group = argv[optind];
71519bb60aSIngo Weinhold
72519bb60aSIngo Weinhold	if (geteuid() != 0) {
73519bb60aSIngo Weinhold		fprintf(stderr, "Error: Only root may delete groups.\n");
74519bb60aSIngo Weinhold		exit(1);
75519bb60aSIngo Weinhold	}
76519bb60aSIngo Weinhold
77519bb60aSIngo Weinhold	if (getgrnam(group) == NULL) {
78519bb60aSIngo Weinhold		fprintf(stderr, "Error: Group \"%s\" doesn't exists.\n", group);
79519bb60aSIngo Weinhold		exit(1);
80519bb60aSIngo Weinhold	}
81519bb60aSIngo Weinhold
82519bb60aSIngo Weinhold	// prepare request for the registrar
83519bb60aSIngo Weinhold	KMessage message(BPrivate::B_REG_DELETE_GROUP);
84519bb60aSIngo Weinhold	if (message.AddString("name", group) != B_OK) {
85519bb60aSIngo Weinhold		fprintf(stderr, "Error: Out of memory!\n");
86519bb60aSIngo Weinhold		exit(1);
87519bb60aSIngo Weinhold	}
88519bb60aSIngo Weinhold
89519bb60aSIngo Weinhold	// send the request
90519bb60aSIngo Weinhold	KMessage reply;
91519bb60aSIngo Weinhold	status_t error = send_authentication_request_to_registrar(message, reply);
92519bb60aSIngo Weinhold	if (error != B_OK) {
93519bb60aSIngo Weinhold		fprintf(stderr, "Error: Failed to delete group: %s\n", strerror(error));
94519bb60aSIngo Weinhold		exit(1);
95519bb60aSIngo Weinhold	}
96519bb60aSIngo Weinhold
97519bb60aSIngo Weinhold	return 0;
98519bb60aSIngo Weinhold}