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/chsh.c
static void update_shell(const char* user, char* newshell)
{
    const struct passwd* pw; /* Password entry from /etc/passwd   */
    struct passwd pwent;     /* New password entry                */

    /*
     * Before going any further, raise the ulimit to prevent
     * colliding into a lowered ulimit, and set the real UID
     * to root to protect against unexpected signals. Any
     * keyboard signals are set to be ignored.
     */
    if (setuid(0) != 0)
    {
        SYSLOG((LOG_ERR, "can't setuid(0)"));
        fputs(_("Cannot change ID to root.\n"), stderr);
        fail_exit(1);
    }
    pwd_init();

    /*
     * The passwd entry is now ready to be committed back to
     * the password file. Get a lock on the file and open it.
     */
    if (pw_lock() == 0)
    {
        fprintf(stderr, _("%s: cannot lock %s; try again later.\n"), Prog,
                pw_dbname());
        fail_exit(1);
    }
    pw_locked = true;
    if (pw_open(O_CREAT | O_RDWR) == 0)
    {
        fprintf(stderr, _("%s: cannot open %s\n"), Prog, pw_dbname());
        SYSLOG((LOG_WARN, "cannot open %s", pw_dbname()));
        fail_exit(1);
    }

    /*
     * Get the entry to update using pw_locate() - we want the real
     * one from /etc/passwd, not the one from getpwnam() which could
     * contain the shadow password if (despite the warnings) someone
     * enables AUTOSHADOW (or SHADOW_COMPAT in libc).  --marekm
     */
    pw = pw_locate(user);
    if (NULL == pw)
    {
        fprintf(stderr, _("%s: user '%s' does not exist in %s\n"), Prog, user,
                pw_dbname());
        fail_exit(1);
    }

    /*
     * Make a copy of the entry, then change the shell field. The other
     * fields remain unchanged.
     */
    pwent = *pw;
    pwent.pw_shell = newshell;

    /*
     * Update the passwd file entry. If there is a DBM file, update
     * that entry as well.
     */
    if (pw_update(&pwent) == 0)
    {
        fprintf(stderr, _("%s: failed to prepare the new %s entry '%s'\n"),
                Prog, pw_dbname(), pwent.pw_name);
        fail_exit(1);
    }

    /*
     * Changes have all been made, so commit them and unlock the file.
     */
    if (pw_close() == 0)
    {
        fprintf(stderr, _("%s: failure while writing changes to %s\n"), Prog,
                pw_dbname());
        SYSLOG((LOG_ERR, "failure while writing changes to %s", pw_dbname()));
        fail_exit(1);
    }
    if (pw_unlock() == 0)
    {
        fprintf(stderr, _("%s: failed to unlock %s\n"), Prog, pw_dbname());
        SYSLOG((LOG_ERR, "failed to unlock %s", pw_dbname()));
        /* continue */
    }
    pw_locked = false;
}
/*
 * update_shell - update the user's shell in the passwd database
 *
 *	Commit the user's entry after changing her shell field.
 *
 *	It will not return in case of error.
 */
update the user's shell in the passwd database
Commit the user's entry after changing her shell field.

It will not return in case of error.
update the user ' s shell in the passwd database Commit the user ' s entry after changing her shell field . It will not return in case of error .
static void update_shell(const char* user, char* newshell)
{
    const struct passwd* pw; 
    struct passwd pwent;     
    if (setuid(0) != 0)
    {
        SYSLOG((LOG_ERR, "can't setuid(0)"));
        fputs(_("Cannot change ID to root.\n"), stderr);
        fail_exit(1);
    }
    pwd_init();
    if (pw_lock() == 0)
    {
        fprintf(stderr, _("%s: cannot lock %s; try again later.\n"), Prog,
                pw_dbname());
        fail_exit(1);
    }
    pw_locked = true;
    if (pw_open(O_CREAT | O_RDWR) == 0)
    {
        fprintf(stderr, _("%s: cannot open %s\n"), Prog, pw_dbname());
        SYSLOG((LOG_WARN, "cannot open %s", pw_dbname()));
        fail_exit(1);
    }
    pw = pw_locate(user);
    if (NULL == pw)
    {
        fprintf(stderr, _("%s: user '%s' does not exist in %s\n"), Prog, user,
                pw_dbname());
        fail_exit(1);
    }
    pwent = *pw;
    pwent.pw_shell = newshell;
    if (pw_update(&pwent) == 0)
    {
        fprintf(stderr, _("%s: failed to prepare the new %s entry '%s'\n"),
                Prog, pw_dbname(), pwent.pw_name);
        fail_exit(1);
    }
    if (pw_close() == 0)
    {
        fprintf(stderr, _("%s: failure while writing changes to %s\n"), Prog,
                pw_dbname());
        SYSLOG((LOG_ERR, "failure while writing changes to %s", pw_dbname()));
        fail_exit(1);
    }
    if (pw_unlock() == 0)
    {
        fprintf(stderr, _("%s: failed to unlock %s\n"), Prog, pw_dbname());
        SYSLOG((LOG_ERR, "failed to unlock %s", pw_dbname()));
    }
    pw_locked = false;
}
static void update_shell ( const char * user , char * newshell ) { const struct passwd * pw ; struct passwd pwent ; if ( setuid ( 0 ) != 0 ) { SYSLOG ( ( LOG_ERR , " " ) ) ; fputs ( _ ( " \n " ) , stderr ) ; fail_exit ( 1 ) ; } pwd_init ( ) ; if ( pw_lock ( ) == 0 ) { fprintf ( stderr , _ ( " \n " ) , Prog , pw_dbname ( ) ) ; fail_exit ( 1 ) ; } pw_locked = true ; if ( pw_open ( O_CREAT | O_RDWR ) == 0 ) { fprintf ( stderr , _ ( " \n " ) , Prog , pw_dbname ( ) ) ; SYSLOG ( ( LOG_WARN , " " , pw_dbname ( ) ) ) ; fail_exit ( 1 ) ; } pw = pw_locate ( user ) ; if ( NULL == pw ) { fprintf ( stderr , _ ( " \n " ) , Prog , user , pw_dbname ( ) ) ; fail_exit ( 1 ) ; } pwent = * pw ; pwent . pw_shell = newshell ; if ( pw_update ( & pwent ) == 0 ) { fprintf ( stderr , _ ( " \n " ) , Prog , pw_dbname ( ) , pwent . pw_name ) ; fail_exit ( 1 ) ; } if ( pw_close ( ) == 0 ) { fprintf ( stderr , _ ( " \n " ) , Prog , pw_dbname ( ) ) ; SYSLOG ( ( LOG_ERR , " " , pw_dbname ( ) ) ) ; fail_exit ( 1 ) ; } if ( pw_unlock ( ) == 0 ) { fprintf ( stderr , _ ( " \n " ) , Prog , pw_dbname ( ) ) ; SYSLOG ( ( LOG_ERR , " " , pw_dbname ( ) ) ) ; } pw_locked = false ; }
update_shell - update the user's shell in the passwd database
      Commit the user's entry after changing her shell field.
update_shell - update the user ' s shell in the passwd database Commit the user ' s entry after changing her shell field .
/* Password entry from /etc/passwd   */ /* New password entry                */ /*
     * Before going any further, raise the ulimit to prevent
     * colliding into a lowered ulimit, and set the real UID
     * to root to protect against unexpected signals. Any
     * keyboard signals are set to be ignored.
     */ /*
     * The passwd entry is now ready to be committed back to
     * the password file. Get a lock on the file and open it.
     */ /*
     * Get the entry to update using pw_locate() - we want the real
     * one from /etc/passwd, not the one from getpwnam() which could
     * contain the shadow password if (despite the warnings) someone
     * enables AUTOSHADOW (or SHADOW_COMPAT in libc).  --marekm
     */ /*
     * Make a copy of the entry, then change the shell field. The other
     * fields remain unchanged.
     */ /*
     * Update the passwd file entry. If there is a DBM file, update
     * that entry as well.
     */ /*
     * Changes have all been made, so commit them and unlock the file.
     */ /* continue */