Ignore:
Timestamp:
Feb 25, 2011, 9:26:54 PM (13 years ago)
Author:
Bruno Cornec
Message:
  • Update mindi-busybox to 1.18.3 to avoid problems with the tar command which is now failing on recent versions with busybox 1.7.3
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/2.2.9/mindi-busybox/loginutils/addgroup.c

    r1765 r2725  
    77 * Copyright (C) 2007 by Tito Ragusa <farmatito@tiscali.it>
    88 *
    9  * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
     9 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
    1010 *
    1111 */
    12 
    1312#include "libbb.h"
    1413
     14#if CONFIG_LAST_SYSTEM_ID < CONFIG_FIRST_SYSTEM_ID
     15#error Bad LAST_SYSTEM_ID or FIRST_SYSTEM_ID in .config
     16#endif
     17
     18#define OPT_GID                       (1 << 0)
     19#define OPT_SYSTEM_ACCOUNT            (1 << 1)
     20
     21/* We assume GID_T_MAX == INT_MAX */
    1522static void xgroup_study(struct group *g)
    1623{
     24    unsigned max = INT_MAX;
     25
    1726    /* Make sure gr_name is unused */
    1827    if (getgrnam(g->gr_name)) {
    19         goto error;
     28        bb_error_msg_and_die("%s '%s' in use", "group", g->gr_name);
     29        /* these format strings are reused in adduser and addgroup */
    2030    }
    2131
     32    /* if a specific gid is requested, the --system switch and */
     33    /* min and max values are overridden, and the range of valid */
     34    /* gid values is set to [0, INT_MAX] */
     35    if (!(option_mask32 & OPT_GID)) {
     36        if (option_mask32 & OPT_SYSTEM_ACCOUNT) {
     37            g->gr_gid = CONFIG_FIRST_SYSTEM_ID;
     38            max = CONFIG_LAST_SYSTEM_ID;
     39        } else {
     40            g->gr_gid = CONFIG_LAST_SYSTEM_ID + 1;
     41            max = 64999;
     42        }
     43    }
    2244    /* Check if the desired gid is free
    2345     * or find the first free one */
     
    2648            return; /* found free group: return */
    2749        }
    28         if (option_mask32) {
     50        if (option_mask32 & OPT_GID) {
    2951            /* -g N, cannot pick gid other than N: error */
    30             g->gr_name = itoa(g->gr_gid);
    31             goto error;
     52            bb_error_msg_and_die("%s '%s' in use", "gid", itoa(g->gr_gid));
     53            /* this format strings is reused in adduser and addgroup */
     54        }
     55        if (g->gr_gid == max) {
     56            /* overflowed: error */
     57            bb_error_msg_and_die("no %cids left", 'g');
     58            /* this format string is reused in adduser and addgroup */
    3259        }
    3360        g->gr_gid++;
    34         if (g->gr_gid <= 0) {
    35             /* overflowed: error */
    36             bb_error_msg_and_die("no gids left");
    37         }
    3861    }
    39 
    40  error:
    41     /* exit */
    42     bb_error_msg_and_die("group %s already exists", g->gr_name);
    4362}
    4463
     
    4665static void new_group(char *group, gid_t gid)
    4766{
    48     FILE *file;
    4967    struct group gr;
     68    char *p;
    5069
    5170    /* make sure gid and group haven't already been allocated */
     
    5574
    5675    /* add entry to group */
    57     file = xfopen(bb_path_group_file, "a");
    58     /* group:passwd:gid:userlist */
    59     fprintf(file, "%s:x:%d:\n", group, gr.gr_gid);
     76    p = xasprintf("x:%u:", (unsigned) gr.gr_gid);
     77    if (update_passwd(bb_path_group_file, group, p, NULL) < 0)
     78        exit(EXIT_FAILURE);
    6079    if (ENABLE_FEATURE_CLEAN_UP)
    61         fclose(file);
     80        free(p);
    6281#if ENABLE_FEATURE_SHADOWPASSWDS
    63     file = fopen_or_warn(bb_path_gshadow_file, "a");
    64     if (file) {
    65         fprintf(file, "%s:!::\n", group);
    66         if (ENABLE_FEATURE_CLEAN_UP)
    67             fclose(file);
    68     }
     82    /* /etc/gshadow fields:
     83     * 1. Group name.
     84     * 2. Encrypted password.
     85     *    If set, non-members of the group can join the group
     86     *    by typing the password for that group using the newgrp command.
     87     *    If the value is of this field ! then no user is allowed
     88     *    to access the group using the newgrp command. A value of !!
     89     *    is treated the same as a value of ! only it indicates
     90     *    that a password has never been set before. If the value is null,
     91     *    only group members can log into the group.
     92     * 3. Group administrators (comma delimited list).
     93     *    Group members listed here can add or remove group members
     94     *    using the gpasswd command.
     95     * 4. Group members (comma delimited list).
     96     */
     97    /* Ignore errors: if file is missing we assume admin doesn't want it */
     98    update_passwd(bb_path_gshadow_file, group, "!::", NULL);
    6999#endif
    70100}
    71101
    72 #if ENABLE_FEATURE_ADDUSER_TO_GROUP
    73 static void add_user_to_group(char **args,
    74         const char *path,
    75         FILE *(*fopen_func)(const char *fileName, const char *mode))
    76 {
    77     char *line;
    78     int len = strlen(args[1]);
    79     llist_t *plist = NULL;
    80     FILE *group_file;
    81 
    82     group_file = fopen_func(path, "r");
    83 
    84     if (!group_file) return;
    85 
    86     while ((line = xmalloc_getline(group_file))) {
    87         /* Find the group */
    88         if (!strncmp(line, args[1], len)
    89          && line[len] == ':'
    90         ) {
    91             /* Add the new user */
    92             line = xasprintf("%s%s%s", line,
    93                         last_char_is(line, ':') ? "" : ",",
    94                         args[0]);
    95         }
    96         llist_add_to_end(&plist, line);
    97     }
    98 
    99     if (ENABLE_FEATURE_CLEAN_UP) {
    100         fclose(group_file);
    101         group_file = fopen_func(path, "w");
    102         while ((line = llist_pop(&plist))) {
    103             if (group_file)
    104                 fprintf(group_file, "%s\n", line);
    105             free(line);
    106         }
    107         if (group_file)
    108             fclose(group_file);
    109     } else {
    110         group_file = fopen_func(path, "w");
    111         if (group_file)
    112             while ((line = llist_pop(&plist)))
    113                 fprintf(group_file, "%s\n", line);
    114     }
    115 }
     102#if ENABLE_FEATURE_ADDGROUP_LONG_OPTIONS
     103static const char addgroup_longopts[] ALIGN1 =
     104        "gid\0"                 Required_argument "g"
     105        "system\0"              No_argument       "S"
     106        ;
    116107#endif
    117108
     
    123114 * will add an existing user to an existing group.
    124115 */
    125 int addgroup_main(int argc, char **argv);
    126 int addgroup_main(int argc, char **argv)
     116int addgroup_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
     117int addgroup_main(int argc UNUSED_PARAM, char **argv)
    127118{
    128     char *group;
    129     gid_t gid = 0;
     119    unsigned opts;
     120    unsigned gid = 0;
    130121
    131122    /* need to be root */
     
    133124        bb_error_msg_and_die(bb_msg_perm_denied_are_you_root);
    134125    }
    135 
     126#if ENABLE_FEATURE_ADDGROUP_LONG_OPTIONS
     127    applet_long_options = addgroup_longopts;
     128#endif
    136129    /* Syntax:
    137130     *  addgroup group
     
    139132     *  addgroup user group
    140133     * Check for min, max and missing args */
    141     opt_complementary = "-1:?2";
    142     if (getopt32(argv, "g:", &group)) {
    143         gid = xatoul_range(group, 0, ((unsigned long)(gid_t)ULONG_MAX) >> 1);
    144     }
     134    opt_complementary = "-1:?2:g+";
     135    opts = getopt32(argv, "g:S", &gid);
    145136    /* move past the commandline options */
    146137    argv += optind;
    147     argc -= optind;
     138    //argc -= optind;
    148139
    149140#if ENABLE_FEATURE_ADDUSER_TO_GROUP
    150     if (argc == 2) {
     141    if (argv[1]) {
    151142        struct group *gr;
    152143
    153         if (option_mask32) {
     144        if (opts & OPT_GID) {
    154145            /* -g was there, but "addgroup -g num user group"
    155146             * is a no-no */
     
    159150        /* check if group and user exist */
    160151        xuname2uid(argv[0]); /* unknown user: exit */
    161         xgroup2gid(argv[1]); /* unknown group: exit */
     152        gr = xgetgrnam(argv[1]); /* unknown group: exit */
    162153        /* check if user is already in this group */
    163         gr = getgrnam(argv[1]);
    164154        for (; *(gr->gr_mem) != NULL; (gr->gr_mem)++) {
    165155            if (!strcmp(argv[0], *(gr->gr_mem))) {
     
    168158            }
    169159        }
    170         add_user_to_group(argv, bb_path_group_file, xfopen);
    171 #if ENABLE_FEATURE_SHADOWPASSWDS
    172         add_user_to_group(argv, bb_path_gshadow_file, fopen_or_warn);
    173 #endif /* ENABLE_FEATURE_SHADOWPASSWDS */
     160        if (update_passwd(bb_path_group_file, argv[1], NULL, argv[0]) < 0) {
     161            return EXIT_FAILURE;
     162        }
     163# if ENABLE_FEATURE_SHADOWPASSWDS
     164        update_passwd(bb_path_gshadow_file, argv[1], NULL, argv[0]);
     165# endif
    174166    } else
    175167#endif /* ENABLE_FEATURE_ADDUSER_TO_GROUP */
     168    {
     169        die_if_bad_username(argv[0]);
    176170        new_group(argv[0], gid);
    177 
     171    }
    178172    /* Reached only on success */
    179173    return EXIT_SUCCESS;
Note: See TracChangeset for help on using the changeset viewer.