groupadd.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*++
  2. Copyright (c) 2015 Minoca Corp. All Rights Reserved
  3. Module Name:
  4. groupadd.c
  5. Abstract:
  6. This module implements support for the groupadd utility, which adds a new
  7. group to the system.
  8. Author:
  9. Evan Green 11-Mar-2015
  10. Environment:
  11. POSIX
  12. --*/
  13. //
  14. // ------------------------------------------------------------------- Includes
  15. //
  16. #include <minoca/lib/types.h>
  17. #include <assert.h>
  18. #include <errno.h>
  19. #include <getopt.h>
  20. #include <stdlib.h>
  21. #include <string.h>
  22. #include <unistd.h>
  23. #include "../swlib.h"
  24. #include "lutil.h"
  25. //
  26. // ---------------------------------------------------------------- Definitions
  27. //
  28. #define GROUPADD_VERSION_MAJOR 1
  29. #define GROUPADD_VERSION_MINOR 0
  30. #define GROUPADD_USAGE \
  31. "usage: groupadd [options] groupname\n" \
  32. "The groupadd utility adds a new group to the system. Options are:\n" \
  33. " -f, --force -- Exit successfully if the group already exists, and \n" \
  34. " cancel -g if the given GID is already in use.\n" \
  35. " -g, --gid=gid -- Use the given group ID number for the new group.\n" \
  36. " -o, --non-unique -- Succeed even if a group with the same ID exists.\n" \
  37. " -p, --password - Sets the password for the group.\n" \
  38. " -r, --system -- Sets this as a system group.\n" \
  39. " -R, --root=dir -- Chroot into the given directory before operating.\n" \
  40. " --help -- Displays this help text and exits.\n" \
  41. " --version -- Displays the application version and exits.\n"
  42. #define GROUPADD_OPTIONS_STRING "fg:op:rR:HV"
  43. #define GROUPADD_DEFAULT_PASSWORD "x"
  44. #define GROUPADD_MAX_ID 0x7FFFFFFE
  45. //
  46. // Define application options.
  47. //
  48. //
  49. // Set this option to succeed if the group already exists, and cancel -g if
  50. // the GID is in use.
  51. //
  52. #define GROUPADD_OPTION_FORCE 0x00000001
  53. //
  54. // Set this option to create a system account.
  55. //
  56. #define GROUPADD_OPTION_SYSTEM 0x00000002
  57. //
  58. // Set this option to allow non-unique user IDs.
  59. //
  60. #define GROUPADD_OPTION_NON_UNIQUE 0x00000004
  61. //
  62. // ------------------------------------------------------ Data Type Definitions
  63. //
  64. //
  65. // ----------------------------------------------- Internal Function Prototypes
  66. //
  67. //
  68. // -------------------------------------------------------------------- Globals
  69. //
  70. struct option GroupaddLongOptions[] = {
  71. {"force", no_argument, 0, 'f'},
  72. {"gid", required_argument, 0, 'g'},
  73. {"non-unique", no_argument, 0, 'o'},
  74. {"password", required_argument, 0, 'p'},
  75. {"root", required_argument, 0, 'R'},
  76. {"system", no_argument, 0, 'r'},
  77. {"help", no_argument, 0, 'H'},
  78. {"version", no_argument, 0, 'V'},
  79. {NULL, 0, 0, 0},
  80. };
  81. //
  82. // ------------------------------------------------------------------ Functions
  83. //
  84. INT
  85. GroupaddMain (
  86. INT ArgumentCount,
  87. CHAR **Arguments
  88. )
  89. /*++
  90. Routine Description:
  91. This routine is the main entry point for the groupadd utility.
  92. Arguments:
  93. ArgumentCount - Supplies the number of command line arguments the program
  94. was invoked with.
  95. Arguments - Supplies a tokenized array of command line arguments.
  96. Return Value:
  97. Returns an integer exit code. 0 for success, nonzero otherwise.
  98. --*/
  99. {
  100. PSTR AfterScan;
  101. ULONG ArgumentIndex;
  102. struct group Group;
  103. PSTR GroupName;
  104. INT Option;
  105. ULONG Options;
  106. PSTR Password;
  107. PSTR RootDirectory;
  108. int Status;
  109. memset(&Group, 0, sizeof(Group));
  110. Group.gr_gid = -1;
  111. Group.gr_passwd = GROUPADD_DEFAULT_PASSWORD;
  112. GroupName = NULL;
  113. Options = 0;
  114. Password = NULL;
  115. RootDirectory = NULL;
  116. //
  117. // Process the control arguments.
  118. //
  119. while (TRUE) {
  120. Option = getopt_long(ArgumentCount,
  121. Arguments,
  122. GROUPADD_OPTIONS_STRING,
  123. GroupaddLongOptions,
  124. NULL);
  125. if (Option == -1) {
  126. break;
  127. }
  128. if ((Option == '?') || (Option == ':')) {
  129. Status = 1;
  130. goto MainEnd;
  131. }
  132. switch (Option) {
  133. case 'f':
  134. Options |= GROUPADD_OPTION_FORCE;
  135. break;
  136. case 'g':
  137. Group.gr_gid = strtoul(optarg, &AfterScan, 10);
  138. if (AfterScan == optarg) {
  139. SwPrintError(0, optarg, "Invalid group ID");
  140. Status = 1;
  141. goto MainEnd;
  142. }
  143. break;
  144. case 'o':
  145. Options |= GROUPADD_OPTION_NON_UNIQUE;
  146. break;
  147. case 'p':
  148. Password = optarg;
  149. break;
  150. case 'R':
  151. RootDirectory = optarg;
  152. break;
  153. case 'r':
  154. Options |= GROUPADD_OPTION_SYSTEM;
  155. break;
  156. case 'V':
  157. SwPrintVersion(GROUPADD_VERSION_MAJOR, GROUPADD_VERSION_MINOR);
  158. return 1;
  159. case 'H':
  160. printf(GROUPADD_USAGE);
  161. return 1;
  162. default:
  163. assert(FALSE);
  164. Status = 1;
  165. goto MainEnd;
  166. }
  167. }
  168. ArgumentIndex = optind;
  169. if (ArgumentIndex > ArgumentCount) {
  170. ArgumentIndex = ArgumentCount;
  171. }
  172. if (ArgumentIndex >= ArgumentCount) {
  173. SwPrintError(0, NULL, "Argument expected. Try --help for usage");
  174. return 1;
  175. }
  176. GroupName = Arguments[ArgumentIndex];
  177. ArgumentIndex += 1;
  178. if (ArgumentIndex != ArgumentCount) {
  179. SwPrintError(0, NULL, "Unexpected additional arguments");
  180. return 1;
  181. }
  182. //
  183. // Enforce a valid user name.
  184. //
  185. if (SwIsValidUserName(GroupName) == FALSE) {
  186. SwPrintError(0, GroupName, "Invalid group name");
  187. Status = 1;
  188. goto MainEnd;
  189. }
  190. //
  191. // If the hoards are clamoring for it, implement passwords on groups.
  192. //
  193. if (Password != NULL) {
  194. SwPrintError(0,
  195. NULL,
  196. "Group passwords currently not implemented. Let us know "
  197. "that you want it.");
  198. Status = 1;
  199. goto MainEnd;
  200. }
  201. //
  202. // Chroot if requested.
  203. //
  204. if (RootDirectory != NULL) {
  205. Status = chroot(RootDirectory);
  206. if (Status != 0) {
  207. Status = errno;
  208. SwPrintError(Status, RootDirectory, "Failed to chroot");
  209. goto MainEnd;
  210. }
  211. Status = chdir("/");
  212. if (Status != 0) {
  213. Status = errno;
  214. SwPrintError(Status, RootDirectory, "Failed to chdir");
  215. goto MainEnd;
  216. }
  217. }
  218. Group.gr_name = GroupName;
  219. //
  220. // Ensure there are no duplicates in the group name.
  221. //
  222. if (getgrnam(GroupName) != NULL) {
  223. if ((Options & GROUPADD_OPTION_FORCE) == 0) {
  224. SwPrintError(0, GroupName, "Group already exists");
  225. Status = 1;
  226. goto MainEnd;
  227. } else {
  228. Status = 0;
  229. goto MainEnd;
  230. }
  231. }
  232. if (Group.gr_gid != (gid_t)-1) {
  233. if ((Options & GROUPADD_OPTION_NON_UNIQUE) == 0) {
  234. if (getgrgid(Group.gr_gid) != NULL) {
  235. //
  236. // If force is enabled and the GID already exists, just cancel
  237. // it.
  238. //
  239. if ((Options & GROUPADD_OPTION_FORCE) != 0) {
  240. Group.gr_gid = -1;
  241. } else {
  242. SwPrintError(0, NULL, "Group ID %d in use", Group.gr_gid);
  243. Status = 1;
  244. goto MainEnd;
  245. }
  246. }
  247. }
  248. }
  249. //
  250. // Find a group ID.
  251. //
  252. if (Group.gr_gid == -1) {
  253. if ((Options & GROUPADD_OPTION_SYSTEM) != 0) {
  254. Group.gr_gid = BASE_SYSTEM_GID;
  255. } else {
  256. Group.gr_gid = BASE_NON_SYSTEM_GID;
  257. }
  258. while (Group.gr_gid < GROUPADD_MAX_ID) {
  259. if (getgrgid(Group.gr_gid) == NULL) {
  260. break;
  261. }
  262. Group.gr_gid += 1;
  263. }
  264. if (Group.gr_gid >= GROUPADD_MAX_ID) {
  265. SwPrintError(0, NULL, "Group IDs exhausted");
  266. Status = 1;
  267. goto MainEnd;
  268. }
  269. }
  270. //
  271. // Create the group.
  272. //
  273. Status = SwUpdateGroupLine(&Group, UpdatePasswordAddLine);
  274. if (Status != 0) {
  275. SwPrintError(Status, GroupName, "Failed to add group");
  276. goto MainEnd;
  277. }
  278. MainEnd:
  279. return Status;
  280. }
  281. //
  282. // --------------------------------------------------------- Internal Functions
  283. //