From d0f7935f637df5d4794aa782def96cc3db8779ed Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Wed, 16 Oct 2013 19:30:17 -0500 Subject: Prep work for useradd by Ashwini Sharma. --- lib/password.c | 172 +++++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 142 insertions(+), 30 deletions(-) (limited to 'lib/password.c') diff --git a/lib/password.c b/lib/password.c index d1489eb3..b340ef48 100644 --- a/lib/password.c +++ b/lib/password.c @@ -1,19 +1,87 @@ -/* pwdutils.c - password read/update helper functions. +/* password.c - password read/update helper functions. * * Copyright 2012 Ashwini Kumar */ #include "toys.h" +#include "xregcomp.h" #include +static unsigned int random_number_generator(int fd) +{ + unsigned int randnum; + + xreadall(fd, &randnum, sizeof(randnum)); + return randnum; +} + +static char inttoc(int i) +{ + // salt value uses 64 chracters in "./0-9a-zA-Z" + const char character_set[]="./0123456789abcdefghijklmnopqrstuvwxyz" + "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; + + i &= 0x3f; // masking for using 10 bits only + return character_set[i]; +} + +int get_salt(char *salt, char *algo) +{ + int i, randfd, salt_length = 0, offset; + + if (!strcmp(algo,"des")){ + // 2 bytes salt value is used in des + salt_length = 2; + offset = 0; + } else { + *salt++ = '$'; + if (!strcmp(algo,"md5")){ + *salt++ = '1'; + // 8 bytes salt value is used in md5 + salt_length = 8; + } else if (!strcmp(algo,"sha256")){ + *salt++ = '5'; + // 16 bytes salt value is used in sha256 + salt_length = 16; + } else if (!strcmp(algo,"sha512")){ + *salt++ = '6'; + // 16 bytes salt value is used in sha512 + salt_length = 16; + } else return -1; + + *salt++ = '$'; + offset = 3; + } + + randfd = xopen("/dev/urandom", O_RDONLY); + for (i=0; iname, "passwd")) { + fprintf(newfp, "%s%s:",namesfx, entry); + current_ptr = get_nextcolon(line, 2); //past passwd + if (shadow) { + fprintf(newfp, "%u:",(unsigned)(time(NULL))/(24*60*60)); + current_ptr = get_nextcolon(current_ptr, 1); + fprintf(newfp, "%s\n",current_ptr); + } else fprintf(newfp, "%s\n",current_ptr); + } else if (!strcmp(toys.which->name, "groupadd") || + !strcmp(toys.which->name, "addgroup")){ + current_ptr = get_nextcolon(line, 3); //past gid/admin list + *current_ptr = '\0'; + fprintf(newfp, "%s", line); + fprintf(newfp, "%s\n", entry); } - else fprintf(newfp, "%s\n",current_ptr); } - free(line); } free(namesfx); + if (!found) fprintf(newfp, "%s\n", entry); fcntl(fileno(exfp), F_SETLK, &lock); fclose(exfp); @@ -124,7 +210,7 @@ int update_password(char *filename, char* username, char* encrypted) fsync(fileno(newfp)); fclose(newfp); rename(filenamesfx, filename); - if(errno) { + if (errno) { perror_msg("File Writing/Saving failed: "); unlink(filenamesfx); ret = -1; @@ -134,3 +220,29 @@ free_storage: free(filenamesfx); return ret; } + +void is_valid_username(const char *name) +{ + regex_t rp; + regmatch_t rm[1]; + int eval; + char *regex = "^[_.A-Za-z0-9][-_.A-Za-z0-9]*"; //User name REGEX + + xregcomp(&rp, regex, REG_NEWLINE); + + /* compare string against pattern -- remember that patterns + are anchored to the beginning of the line */ + eval = regexec(&rp, name, 1, rm, 0); + regfree(&rp); + if (!eval && !rm[0].rm_so) { + int len = strlen(name); + if ((rm[0].rm_eo == len) || + (rm[0].rm_eo == len - 1 && name[len - 1] == '$')) { + if (len >= LOGIN_NAME_MAX) error_exit("name is too long"); + else return; + } + } + error_exit("'%s', not valid %sname",name, + (((toys.which->name[3] == 'g') || + (toys.which->name[0] == 'g'))? "group" : "user")); +} -- cgit v1.2.3