From d364e141c112efba37882001a49da77d90da28a4 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Sun, 18 Aug 2013 14:01:46 -0500 Subject: Whitespace changes to passwd. --- toys/lsb/passwd.c | 107 ++++++++++++++++++++++++------------------------------ 1 file changed, 47 insertions(+), 60 deletions(-) (limited to 'toys/lsb/passwd.c') diff --git a/toys/lsb/passwd.c b/toys/lsb/passwd.c index ff109f14..167d5730 100644 --- a/toys/lsb/passwd.c +++ b/toys/lsb/passwd.c @@ -39,6 +39,7 @@ char *strcasestr(const char *haystack, const char *needle); unsigned int random_number_generator(int fd) { unsigned int randnum; + xreadall(fd, &randnum, sizeof(randnum)); return randnum; } @@ -47,6 +48,7 @@ char inttoc(int i) { // salt value uses 64 chracters in "./0-9a-zA-Z" const char character_set[]="./0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; + i &= 0x3f; // masking for using 10 bits only return character_set[i]; } @@ -55,20 +57,21 @@ int get_salt(char *salt) { int i, salt_length = 0; int randfd; - if(!strncmp(TT.algo,"des",3)){ + + if (!strncmp(TT.algo,"des",3)) { // 2 bytes salt value is used in des salt_length = 2; } else { *salt++ = '$'; - if(!strncmp(TT.algo,"md5",3)){ + if (!strncmp(TT.algo,"md5",3)) { *salt++ = '1'; // 8 bytes salt value is used in md5 salt_length = 8; - } else if(!strncmp(TT.algo,"sha256",6)){ + } else if (!strncmp(TT.algo,"sha256",6)) { *salt++ = '5'; // 16 bytes salt value is used in sha256 salt_length = 16; - } else if(!strncmp(TT.algo,"sha512",6)){ + } else if (!strncmp(TT.algo,"sha512",6)) { *salt++ = '6'; // 16 bytes salt value is used in sha512 salt_length = 16; @@ -78,7 +81,7 @@ int get_salt(char *salt) } randfd = xopen(URANDOM_PATH, O_RDONLY); - for(i=0; ipw_name); + if (toys.optargs[0]) name = toys.optargs[0]; + else name = xstrdup(pw->pw_name); pw = getpwnam(name); - if(!pw) error_exit("Unknown user '%s'",name); + if (!pw) error_exit("Unknown user '%s'",name); - if(myuid != 0 && (myuid != pw->pw_uid)) + if (myuid != 0 && (myuid != pw->pw_uid)) error_exit("You need to be root to change '%s' password\n", name); pass = pw->pw_passwd; - if(pw->pw_passwd[0] == 'x') { + if (pw->pw_passwd[0] == 'x') { /*get shadow passwd */ sp = getspnam(name); - if(sp) - pass = sp->sp_pwdp; + if (sp) pass = sp->sp_pwdp; } - if(!(toys.optflags & (FLAG_l | FLAG_u | FLAG_d))) { + if (!(toys.optflags & (FLAG_l | FLAG_u | FLAG_d))) { printf("Changing password for %s\n",name); - if(pass[0] == '!') + if (pass[0] == '!') error_exit("Can't change, password is locked for %s",name); - if(myuid != 0) { + if (myuid != 0) { /*Validate user */ - if(read_password(toybuf, sizeof(toybuf), "Origial password:")) { - if(!toys.optargs[0]) free(name); + if (read_password(toybuf, sizeof(toybuf), "Origial password:")) { + if (!toys.optargs[0]) free(name); return; } orig = toybuf; - if(verify_passwd(pass)) - error_exit("Authentication failed\n"); + if (verify_passwd(pass)) error_exit("Authentication failed\n"); } orig = xstrdup(orig); /*Get new password */ newp = new_password(orig, name); - if(!newp) { + if (!newp) { free(orig); - if(!toys.optargs[0]) free(name); + if (!toys.optargs[0]) free(name); return; //new password is not set well. } /*Encrypt the passwd */ - if(!(toys.optflags & FLAG_a)) TT.algo = "des"; + if (!(toys.optflags & FLAG_a)) TT.algo = "des"; - if(get_salt(salt)) - error_exit("Error: Unkown encryption algorithm\n"); + if (get_salt(salt)) error_exit("Error: Unkown encryption algorithm\n"); encrypted = crypt(newp, salt); free(newp); free(orig); - } - else if(toys.optflags & FLAG_l) { - if(pass[0] == '!') + } else if (toys.optflags & FLAG_l) { + if (pass[0] == '!') error_exit("password is already locked for %s",name); printf("Locking password for %s\n",name); encrypted = xmsprintf("!%s",pass); - } - else if(toys.optflags & FLAG_u) { - if(pass[0] != '!') + } else if (toys.optflags & FLAG_u) { + if (pass[0] != '!') error_exit("password is already unlocked for %s",name); printf("Unlocking password for %s\n",name); encrypted = xstrdup(&pass[1]); - } - else if(toys.optflags & FLAG_d) { + } else if (toys.optflags & FLAG_d) { printf("Deleting password for %s\n",name); encrypted = (char*)xzalloc(sizeof(char)*2); //1 = "", 2 = '\0' } /*Update the passwd */ - if(pw->pw_passwd[0] == 'x') + if (pw->pw_passwd[0] == 'x') ret = update_password("/etc/shadow", name, encrypted); - else - ret = update_password("/etc/passwd", name, encrypted); + else ret = update_password("/etc/passwd", name, encrypted); - if((toys.optflags & (FLAG_l | FLAG_u | FLAG_d))) - free(encrypted); + if ((toys.optflags & (FLAG_l | FLAG_u | FLAG_d))) free(encrypted); - if(!toys.optargs[0]) free(name); - if(!ret) - error_msg("Success"); - else - error_msg("Failure"); + if (!toys.optargs[0]) free(name); + if (!ret) error_msg("Success"); + else error_msg("Failure"); } -- cgit v1.2.3