openbmc/build/tmp/deploy/sdk/witherspoon-2019-08-08/sysroots/armv6-openbmc-linux-gnueabi/usr/src/debug/shadow/4.6-r0/shadow-4.6/src/newusers.c
static int add_group(const char* name, const char* gid, gid_t* ngid, uid_t uid)
{
    const struct group* grp;
    struct group grent;
    char* members[1];
#ifdef SHADOWGRP
    const struct sgrp* sg;
#endif

    /*
     * Start by seeing if the named group already exists. This will be
     * very easy to deal with if it does.
     */
    grp = getgrnam(gid);
    if (NULL == grp)
    {
        grp = gr_locate(gid);
    }
    if (NULL != grp)
    {
        /* The user will use this ID for her primary group */
        *ngid = grp->gr_gid;
        /* Don't check gshadow */
        return 0;
    }

    if (isdigit(gid[0]))
    {
        /*
         * The GID is a number, which means either this is a brand
         * new group, or an existing group.
         */

        if (get_gid(gid, &grent.gr_gid) == 0)
        {
            fprintf(stderr, _("%s: invalid group ID '%s'\n"), Prog, gid);
            return -1;
        }

        /* Look in both the system database (getgrgid) and in the
         * internal database (gr_locate_gid), which may contain
         * uncommitted changes */
        if ((getgrgid((gid_t)grent.gr_gid) != NULL) ||
            (gr_locate_gid((gid_t)grent.gr_gid) != NULL))
        {
            /* The user will use this ID for her
             * primary group */
            *ngid = (gid_t)grent.gr_gid;
            return 0;
        }

        /* Do not create groups with GID == (gid_t)-1 */
        if (grent.gr_gid == (gid_t)-1)
        {
            fprintf(stderr, _("%s: invalid group ID '%s'\n"), Prog, gid);
            return -1;
        }
    }
    else
    {
        /* The gid parameter can be "" or a name which is not
         * already the name of an existing group.
         * In both cases, figure out what group ID can be used.
         */
        if (find_new_gid(rflg, &grent.gr_gid, &uid) < 0)
        {
            return -1;
        }
    }

    /*
     * Now I have all of the fields required to create the new group.
     */
    if (('\0' != gid[0]) && (!isdigit(gid[0])))
    {
        grent.gr_name = xstrdup(gid);
    }
    else
    {
        grent.gr_name = xstrdup(name);
        /* FIXME: check if the group exists */
    }

    /* Check if this is a valid group name */
    if (!is_valid_group_name(grent.gr_name))
    {
        fprintf(stderr, _("%s: invalid group name '%s'\n"), Prog,
                grent.gr_name);
        if (grent.gr_name)
            free(grent.gr_name);
        return -1;
    }

    grent.gr_passwd = "*"; /* XXX warning: const */
    members[0] = NULL;
    grent.gr_mem = members;

    *ngid = grent.gr_gid;

#ifdef SHADOWGRP
    if (is_shadow_grp)
    {
        sg = sgr_locate(grent.gr_name);

        if (NULL != sg)
        {
            fprintf(stderr,
                    _("%s: group '%s' is a shadow group, but does not exist in "
                      "/etc/group\n"),
                    Prog, grent.gr_name);
            return -1;
        }
    }
#endif

#ifdef SHADOWGRP
    if (is_shadow_grp)
    {
        struct sgrp sgrent;
        char* admins[1];
        sgrent.sg_name = grent.gr_name;
        sgrent.sg_passwd = "*"; /* XXX warning: const */
        grent.gr_passwd = "x";  /* XXX warning: const */
        admins[0] = NULL;
        sgrent.sg_adm = admins;
        sgrent.sg_mem = members;

        if (sgr_update(&sgrent) == 0)
        {
            return -1;
        }
    }
#endif

    if (gr_update(&grent) == 0)
    {
        return -1;
    }

    return 0;
}
/*
 * add_group - create a new group or add a user to an existing group
 */
create a new group or add a user to an existing group
create a new group or add a user to an existing group
static int add_group(const char* name, const char* gid, gid_t* ngid, uid_t uid)
{
    const struct group* grp;
    struct group grent;
    char* members[1];
#ifdef SHADOWGRP
    const struct sgrp* sg;
#endif
    grp = getgrnam(gid);
    if (NULL == grp)
    {
        grp = gr_locate(gid);
    }
    if (NULL != grp)
    {
        *ngid = grp->gr_gid;
        return 0;
    }
    if (isdigit(gid[0]))
    {
        if (get_gid(gid, &grent.gr_gid) == 0)
        {
            fprintf(stderr, _("%s: invalid group ID '%s'\n"), Prog, gid);
            return -1;
        }
        if ((getgrgid((gid_t)grent.gr_gid) != NULL) ||
            (gr_locate_gid((gid_t)grent.gr_gid) != NULL))
        {
            *ngid = (gid_t)grent.gr_gid;
            return 0;
        }
        if (grent.gr_gid == (gid_t)-1)
        {
            fprintf(stderr, _("%s: invalid group ID '%s'\n"), Prog, gid);
            return -1;
        }
    }
    else
    {
        if (find_new_gid(rflg, &grent.gr_gid, &uid) < 0)
        {
            return -1;
        }
    }
    if (('\0' != gid[0]) && (!isdigit(gid[0])))
    {
        grent.gr_name = xstrdup(gid);
    }
    else
    {
        grent.gr_name = xstrdup(name);
    }
    if (!is_valid_group_name(grent.gr_name))
    {
        fprintf(stderr, _("%s: invalid group name '%s'\n"), Prog,
                grent.gr_name);
        if (grent.gr_name)
            free(grent.gr_name);
        return -1;
    }
    grent.gr_passwd = "*"; 
    members[0] = NULL;
    grent.gr_mem = members;
    *ngid = grent.gr_gid;
#ifdef SHADOWGRP
    if (is_shadow_grp)
    {
        sg = sgr_locate(grent.gr_name);
        if (NULL != sg)
        {
            fprintf(stderr,
                    _("%s: group '%s' is a shadow group, but does not exist in "
                      "/etc/group\n"),
                    Prog, grent.gr_name);
            return -1;
        }
    }
#endif
#ifdef SHADOWGRP
    if (is_shadow_grp)
    {
        struct sgrp sgrent;
        char* admins[1];
        sgrent.sg_name = grent.gr_name;
        sgrent.sg_passwd = "*"; 
        grent.gr_passwd = "x";  
        admins[0] = NULL;
        sgrent.sg_adm = admins;
        sgrent.sg_mem = members;
        if (sgr_update(&sgrent) == 0)
        {
            return -1;
        }
    }
#endif
    if (gr_update(&grent) == 0)
    {
        return -1;
    }
    return 0;
}
static int add_group ( const char * name , const char * gid , gid_t * ngid , uid_t uid ) { const struct group * grp ; struct group grent ; char * members [ 1 ] ; #ifdef SHADOWGRP const struct sgrp * sg ; #endif grp = getgrnam ( gid ) ; if ( NULL == grp ) { grp = gr_locate ( gid ) ; } if ( NULL != grp ) { * ngid = grp -> gr_gid ; return 0 ; } if ( isdigit ( gid [ 0 ] ) ) { if ( get_gid ( gid , & grent . gr_gid ) == 0 ) { fprintf ( stderr , _ ( " \n " ) , Prog , gid ) ; return -1 ; } if ( ( getgrgid ( ( gid_t ) grent . gr_gid ) != NULL ) || ( gr_locate_gid ( ( gid_t ) grent . gr_gid ) != NULL ) ) { * ngid = ( gid_t ) grent . gr_gid ; return 0 ; } if ( grent . gr_gid == ( gid_t ) - 1 ) { fprintf ( stderr , _ ( " \n " ) , Prog , gid ) ; return -1 ; } } else { if ( find_new_gid ( rflg , & grent . gr_gid , & uid ) < 0 ) { return -1 ; } } if ( ( ' \0 ' != gid [ 0 ] ) && ( ! isdigit ( gid [ 0 ] ) ) ) { grent . gr_name = xstrdup ( gid ) ; } else { grent . gr_name = xstrdup ( name ) ; } if ( ! is_valid_group_name ( grent . gr_name ) ) { fprintf ( stderr , _ ( " \n " ) , Prog , grent . gr_name ) ; if ( grent . gr_name ) free ( grent . gr_name ) ; return -1 ; } grent . gr_passwd = " " ; members [ 0 ] = NULL ; grent . gr_mem = members ; * ngid = grent . gr_gid ; #ifdef SHADOWGRP if ( is_shadow_grp ) { sg = sgr_locate ( grent . gr_name ) ; if ( NULL != sg ) { fprintf ( stderr , _ ( " " " \n " ) , Prog , grent . gr_name ) ; return -1 ; } } #endif #ifdef SHADOWGRP if ( is_shadow_grp ) { struct sgrp sgrent ; char * admins [ 1 ] ; sgrent . sg_name = grent . gr_name ; sgrent . sg_passwd = " " ; grent . gr_passwd = " " ; admins [ 0 ] = NULL ; sgrent . sg_adm = admins ; sgrent . sg_mem = members ; if ( sgr_update ( & sgrent ) == 0 ) { return -1 ; } } #endif if ( gr_update ( & grent ) == 0 ) { return -1 ; } return 0 ; }
add_group - create a new group or add a user to an existing group
add_group - create a new group or add a user to an existing group
/*
     * Start by seeing if the named group already exists. This will be
     * very easy to deal with if it does.
     */ /* The user will use this ID for her primary group */ /* Don't check gshadow */ /*
         * The GID is a number, which means either this is a brand
         * new group, or an existing group.
         */ /* Look in both the system database (getgrgid) and in the
         * internal database (gr_locate_gid), which may contain
         * uncommitted changes */ /* The user will use this ID for her
             * primary group */ /* Do not create groups with GID == (gid_t)-1 */ /* The gid parameter can be "" or a name which is not
         * already the name of an existing group.
         * In both cases, figure out what group ID can be used.
         */ /*
     * Now I have all of the fields required to create the new group.
     */ /* FIXME: check if the group exists */ /* Check if this is a valid group name */ /* XXX warning: const */ /* XXX warning: const */ /* XXX warning: const */