static int
extendline(char **buf, int * buflen, int needed)
{
if (needed > *buflen) {
char *tmp = realloc(*buf, needed);
if (tmp == NULL)
return -1;
*buf = tmp;
*buflen = needed;
}
return *buflen;
}
static int
extendarray(char ***buf, int * buflen, int needed)
{
if (needed > *buflen) {
char **tmp = realloc(*buf, needed * sizeof(char *));
if (tmp == NULL)
return -1;
*buf = tmp;
*buflen = needed;
}
return *buflen;
}
static char * get_password(const char * user) {
static char pwd[MAX_STRING_LEN];
char line[MAX_STRING_LEN];
FILE * fpw=NULL;
char * token;
fpw = fopen (getpwpath(), "r");
if (fpw == NULL) {
return NULL;
}
memset(pwd,0,MAX_STRING_LEN);
while (!(getline (line, MAX_STRING_LEN, fpw))) {
if (line[0] == '#') {
continue;
}
token = strtok(line,":");
if (token == NULL) {
continue;
}
if (strcmp (user, token) != 0) {
continue;
}
/* User found */
token = strtok(NULL,":");
strcpy(pwd,token);
fclose(fpw);
return pwd;
}
fclose(fpw);
return NULL;
}
static char * get_alias(const char * user) {
static char alias[MAX_STRING_LEN];
char line[MAX_STRING_LEN];
FILE * fpw;
char * token;
fpw = fopen (getpwpath(), "r");
while (!(getline (line, sizeof (line), fpw))) {
if (line[0] == '#') {
continue;
}
token = strtok(line,":");
if (strcmp (user, token) != 0) {
continue;
}
/* User found */
token = strtok(NULL,":");
token = strtok(NULL,":");
if (token == NULL) {
return NULL;
}
strcpy(alias,token);
fclose(fpw);
return alias;
}
fclose(fpw);
return NULL;
}
/*
* Reads and verifies user's password from an input
* Return password for the user
*/
#define safe_string(a) a ? a : "NULL"
static char * read_user_password(const char * user, int pwd_read_mode) {
char prompt[128];
char * pwd, * pwd_verify;
static char password[_PASSWORD_LEN];
memset(password,0,_PASSWORD_LEN);
switch (pwd_read_mode) {
case PRM_NEW:
sprintf(prompt,"Enter New password for user '%s':",user);
/* We need to strdup because GETPASS uses static object */
pwd_verify = GETPASS(prompt);
verify_password_phrase(pwd_verify,TRUE);
pwd = strdup(pwd_verify);
memset(pwd_verify,0,strlen(pwd_verify));
sprintf(prompt,"Re-type New password for user '%s':",user);
pwd_verify = GETPASS(prompt);
verify_password_phrase(pwd_verify,FALSE);
if (strcmp(pwd,pwd_verify)) {
free(pwd);
error(1,0,"Passwords are different");
} else {
free(pwd);
strcpy(password,pwd_verify);
memset(pwd_verify,0,strlen(pwd_verify));
}
break;
case PRM_CHECK:
pwd = get_password(user);
sprintf(prompt,"Enter current password for user '%s': ",user);
pwd_verify = GETPASS(prompt);
verify_password_phrase(pwd_verify,FALSE);
if (strcmp(pwd,crypt(pwd_verify,pwd))==0) {
strcpy(password,pwd);
} else {
error(1,0,"Wrong password",user);
}
break;
case PRM_ADMIN:
pwd = get_password(CVS_ADMIN_USER);
if (pwd == NULL)
error(1,0,"Administrator's user accout not found. Please contact your CVS admin.");
if (strcmp(pwd,"*")==0)
error(1,0,"Password for Administrator's user accout is not set. Please contact
your CVS admin.");
pwd_verify = GETPASS("Enter CVS Administrator password: ");
verify_password_phrase(pwd_verify,FALSE);
if (strcmp(pwd,crypt(pwd_verify,pwd))==0) {
strcpy(password,pwd);
} else {
error(1,0,"Administrator password is invalid");
}
break;
}
return password;
}