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 <errno.h>
7519bb60aSIngo Weinhold#include <getopt.h>
8519bb60aSIngo Weinhold#include <grp.h>
9519bb60aSIngo Weinhold#include <stdio.h>
10519bb60aSIngo Weinhold#include <stdlib.h>
11519bb60aSIngo Weinhold#include <string.h>
12519bb60aSIngo Weinhold#include <unistd.h>
13519bb60aSIngo Weinhold
14519bb60aSIngo Weinhold#include <OS.h>
15519bb60aSIngo Weinhold
16519bb60aSIngo Weinhold#include <RegistrarDefs.h>
17519bb60aSIngo Weinhold#include <user_group.h>
18519bb60aSIngo Weinhold#include <util/KMessage.h>
19519bb60aSIngo Weinhold
20519bb60aSIngo Weinhold#include "multiuser_utils.h"
21519bb60aSIngo Weinhold
22519bb60aSIngo Weinhold
23519bb60aSIngo Weinholdextern const char *__progname;
24519bb60aSIngo Weinhold
25519bb60aSIngo Weinhold
26519bb60aSIngo Weinholdstatic const char* kUsage =
27519bb60aSIngo Weinhold	"Usage: %s [ <options> ] <group name>\n"
28519bb60aSIngo Weinhold	"Creates a new group <group name>.\n"
29519bb60aSIngo Weinhold	"\n"
30519bb60aSIngo Weinhold	"Options:\n"
31519bb60aSIngo Weinhold	"  -h, --help\n"
32519bb60aSIngo Weinhold	"    Print usage info.\n"
33519bb60aSIngo Weinhold	;
34519bb60aSIngo Weinhold
35519bb60aSIngo Weinholdstatic void
36519bb60aSIngo Weinholdprint_usage_and_exit(bool error)
37519bb60aSIngo Weinhold{
38519bb60aSIngo Weinhold	fprintf(error ? stderr : stdout, kUsage, __progname);
39519bb60aSIngo Weinhold	exit(error ? 1 : 0);
40519bb60aSIngo Weinhold}
41519bb60aSIngo Weinhold
42519bb60aSIngo Weinhold
43519bb60aSIngo Weinholdint
44519bb60aSIngo Weinholdmain(int argc, const char* const* argv)
45519bb60aSIngo Weinhold{
46519bb60aSIngo Weinhold	while (true) {
47519bb60aSIngo Weinhold		static struct option sLongOptions[] = {
48519bb60aSIngo Weinhold			{ "help", no_argument, 0, 'h' },
49519bb60aSIngo Weinhold			{ 0, 0, 0, 0 }
50519bb60aSIngo Weinhold		};
51519bb60aSIngo Weinhold
52519bb60aSIngo Weinhold		opterr = 0; // don't print errors
53519bb60aSIngo Weinhold		int c = getopt_long(argc, (char**)argv, "h", sLongOptions, NULL);
54519bb60aSIngo Weinhold		if (c == -1)
55519bb60aSIngo Weinhold			break;
56519bb60aSIngo Weinhold
57519bb60aSIngo Weinhold
58519bb60aSIngo Weinhold		switch (c) {
59519bb60aSIngo Weinhold			case 'h':
60519bb60aSIngo Weinhold				print_usage_and_exit(false);
61519bb60aSIngo Weinhold				break;
62519bb60aSIngo Weinhold
63519bb60aSIngo Weinhold			default:
64519bb60aSIngo Weinhold				print_usage_and_exit(true);
65519bb60aSIngo Weinhold				break;
66519bb60aSIngo Weinhold		}
67519bb60aSIngo Weinhold	}
68519bb60aSIngo Weinhold
69519bb60aSIngo Weinhold	if (optind != argc - 1)
70519bb60aSIngo Weinhold		print_usage_and_exit(true);
71519bb60aSIngo Weinhold
72519bb60aSIngo Weinhold	const char* group = argv[optind];
73519bb60aSIngo Weinhold
74519bb60aSIngo Weinhold	if (geteuid() != 0) {
75519bb60aSIngo Weinhold		fprintf(stderr, "Error: Only root may add groups.\n");
76519bb60aSIngo Weinhold		exit(1);
77519bb60aSIngo Weinhold	}
78519bb60aSIngo Weinhold
79519bb60aSIngo Weinhold	// check, if group already exists
80519bb60aSIngo Weinhold	if (getgrnam(group) != NULL) {
81519bb60aSIngo Weinhold		fprintf(stderr, "Error: Group \"%s\" already exists.\n", group);
82519bb60aSIngo Weinhold		exit(1);
83519bb60aSIngo Weinhold	}
84519bb60aSIngo Weinhold
85519bb60aSIngo Weinhold	// find an unused GID
86519bb60aSIngo Weinhold	gid_t gid = 100;
87519bb60aSIngo Weinhold	while (getgrgid(gid) != NULL)
88519bb60aSIngo Weinhold		gid++;
89519bb60aSIngo Weinhold
90519bb60aSIngo Weinhold	// prepare request for the registrar
91519bb60aSIngo Weinhold	KMessage message(BPrivate::B_REG_UPDATE_GROUP);
92519bb60aSIngo Weinhold	if (message.AddInt32("gid", gid) != B_OK
93519bb60aSIngo Weinhold		|| message.AddString("name", group) != B_OK
94519bb60aSIngo Weinhold		|| message.AddString("password", "x") != B_OK
95519bb60aSIngo Weinhold		|| message.AddBool("add group", true) != B_OK) {
96519bb60aSIngo Weinhold		fprintf(stderr, "Error: Out of memory!\n");
97519bb60aSIngo Weinhold		exit(1);
98519bb60aSIngo Weinhold	}
99519bb60aSIngo Weinhold
100519bb60aSIngo Weinhold	// send the request
101519bb60aSIngo Weinhold	KMessage reply;
102519bb60aSIngo Weinhold	status_t error = send_authentication_request_to_registrar(message, reply);
103519bb60aSIngo Weinhold	if (error != B_OK) {
104519bb60aSIngo Weinhold		fprintf(stderr, "Error: Failed to create group: %s\n", strerror(error));
105519bb60aSIngo Weinhold		exit(1);
106519bb60aSIngo Weinhold	}
107519bb60aSIngo Weinhold
108519bb60aSIngo Weinhold	return 0;
109519bb60aSIngo Weinhold}