id: unsigned long is excessive for option bitmask
[oweals/busybox.git] / loginutils / addgroup.c
index b29b66527bd906dfa35ced6e61c57df6dae52b5a..89414d7381fbfadedb165efe5fc61fae49c7b20f 100644 (file)
 
 static void xgroup_study(struct group *g)
 {
-       enum { max = 65000 };
-       /* Use a particular gid. */
-       int desired = (g->gr_gid > 0);
-
        /* Make sure gr_name is unused */
        if (getgrnam(g->gr_name)) {
                goto error;
        }
 
-       /* Check if the desired gid is free or
-          find the first free one */
-       do {
-               if (g->gr_gid == max) { /* out of bounds: exit */
-                       bb_error_msg_and_die("no gids left");
-               }
+       /* Check if the desired gid is free
+        * or find the first free one */
+       while (1) {
                if (!getgrgid(g->gr_gid)) {
-                       return; /* ok */
+                       return; /* found free group: return */
                }
-               if (desired) { /* the desired gid is already in use: exit */
+               if (option_mask32) {
+                       /* -g N, cannot pick gid other than N: error */
                        g->gr_name = itoa(g->gr_gid);
                        goto error;
                }
                g->gr_gid++;
-       } while (1);
+               if (g->gr_gid <= 0) {
+                       /* overflowed: error */
+                       bb_error_msg_and_die("no gids left");
+               }
+       }
 
-error:
+ error:
        /* exit */
-       bb_error_msg_and_die("%s: already in use", g->gr_name);
+       bb_error_msg_and_die("group %s already exists", g->gr_name);
 }
 
 /* append a new user to the passwd file */
@@ -53,12 +51,12 @@ static void new_group(char *group, gid_t gid)
        /* make sure gid and group haven't already been allocated */
        gr.gr_gid = gid;
        gr.gr_name = group;
-       xgroup_study( &gr);
+       xgroup_study(&gr);
 
        /* add entry to group */
        file = xfopen(bb_path_group_file, "a");
        /* group:passwd:gid:userlist */
-       fprintf(file, "%s:x:%d:\n", group, gr.gr_gid);
+       fprintf(file, "%s:x:%u:\n", group, (unsigned)gr.gr_gid);
        if (ENABLE_FEATURE_CLEAN_UP)
                fclose(file);
 #if ENABLE_FEATURE_SHADOWPASSWDS
@@ -74,7 +72,7 @@ static void new_group(char *group, gid_t gid)
 #if ENABLE_FEATURE_ADDUSER_TO_GROUP
 static void add_user_to_group(char **args,
                const char *path,
-               FILE *(*fopen_func)(const char *fileName, const char *mode))
+               FILE* FAST_FUNC (*fopen_func)(const char *fileName, const char *mode))
 {
        char *line;
        int len = strlen(args[1]);
@@ -85,10 +83,10 @@ static void add_user_to_group(char **args,
 
        if (!group_file) return;
 
-       while ((line = xmalloc_getline(group_file))) {
+       while ((line = xmalloc_fgetline(group_file)) != NULL) {
                /* Find the group */
                if (!strncmp(line, args[1], len)
-               && line[len] == ':'
+                && line[len] == ':'
                ) {
                        /* Add the new user */
                        line = xasprintf("%s%s%s", line,
@@ -121,33 +119,43 @@ static void add_user_to_group(char **args,
  * addgroup will take a login_name as its first parameter.
  *
  * gid can be customized via command-line parameters.
- * If  called with two non-option arguments, addgroup
+ * If called with two non-option arguments, addgroup
  * will add an existing user to an existing group.
  */
-int addgroup_main(int argc, char **argv);
-int addgroup_main(int argc, char **argv)
+int addgroup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
+int addgroup_main(int argc UNUSED_PARAM, char **argv)
 {
        char *group;
        gid_t gid = 0;
 
-       /* check for min, max and missing args and exit on error */
-       opt_complementary = "-1:?2:?";
-       if (getopt32(argc, argv, "g:", &group)) {
-               gid = xatoul_range(group, 0, (gid_t)ULONG_MAX);
-       }
-       /* move past the commandline options */
-       argv += optind;
-       argc -= optind;
-
        /* need to be root */
        if (geteuid()) {
                bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
        }
 
+       /* Syntax:
+        *  addgroup group
+        *  addgroup -g num group
+        *  addgroup user group
+        * Check for min, max and missing args */
+       opt_complementary = "-1:?2";
+       if (getopt32(argv, "g:", &group)) {
+               gid = xatoul_range(group, 0, ((unsigned long)(gid_t)ULONG_MAX) >> 1);
+       }
+       /* move past the commandline options */
+       argv += optind;
+       //argc -= optind;
+
 #if ENABLE_FEATURE_ADDUSER_TO_GROUP
-       if (argc == 2) {
+       if (argv[1]) {
                struct group *gr;
 
+               if (option_mask32) {
+                       /* -g was there, but "addgroup -g num user group"
+                        * is a no-no */
+                       bb_show_usage();
+               }
+
                /* check if group and user exist */
                xuname2uid(argv[0]); /* unknown user: exit */
                xgroup2gid(argv[1]); /* unknown group: exit */
@@ -162,11 +170,14 @@ int addgroup_main(int argc, char **argv)
                add_user_to_group(argv, bb_path_group_file, xfopen);
 #if ENABLE_FEATURE_SHADOWPASSWDS
                add_user_to_group(argv, bb_path_gshadow_file, fopen_or_warn);
-#endif /* ENABLE_FEATURE_SHADOWPASSWDS */
+#endif
        } else
 #endif /* ENABLE_FEATURE_ADDUSER_TO_GROUP */
+       {
+               die_if_bad_username(argv[0]);
                new_group(argv[0], gid);
 
+       }
        /* Reached only on success */
        return EXIT_SUCCESS;
 }