Skip to content

Commit

Permalink
pw.c (passwords) refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
sai2791 committed Apr 27, 2021
1 parent a8d913d commit da06f48
Showing 1 changed file with 22 additions and 18 deletions.
40 changes: 22 additions & 18 deletions pw.c
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
/*
* Password file management for aund.
* Current format is
User:Password:URD:Priv:Opt4
* User:Password:URD:Priv:Opt4
*/

#include "fs_proto.h"
Expand Down Expand Up @@ -132,7 +132,7 @@ static int
pw_read_line(char **user, char **pw, char **urd, char **priv, int *opt4)
{
static char buffer[16384];
char *p, *q, *r, *s;
char *password, *directory_name, *r, *s;

errno = 0;
if (!fgets(buffer, sizeof(buffer), fp)) {
Expand All @@ -144,15 +144,15 @@ pw_read_line(char **user, char **pw, char **urd, char **priv, int *opt4)

buffer[strcspn(buffer, "\r\n")] = '\0';

if ((p = strchr(buffer, ':')) == NULL ||
(q = strchr(p+1, ':')) == NULL ||
(s = strchr(q+1, ':')) == NULL) {
if ((password = strchr(buffer, ':')) == NULL ||
(directory_name = strchr(password+1, ':')) == NULL ||
(s = strchr(directory_name+1, ':')) == NULL) {
warnx("%s:%d: malformatted line\n", pwfile, pwline);
return -1;
}

*p++ = '\0';
*q++ = '\0';
*password++ = '\0';
*directory_name++ = '\0';
*s++ = '\0';

r = strchr(s, ':');
Expand All @@ -164,8 +164,8 @@ pw_read_line(char **user, char **pw, char **urd, char **priv, int *opt4)
}

*user = buffer;
*pw = p;
*urd = q;
*pw = password;
*urd = directory_name;
*priv = s;

return 0;
Expand Down Expand Up @@ -215,16 +215,18 @@ pw_validate(char *user, const char *pw, int *opt4)
static char *
pw_urd(char const *user)
{
char *u, *p, *d, *s;
int o4;
char *user_name, *user_password, *directory_name;
char *privilege;
int opt4;

if (pw_open(1) < 0)
return NULL;

while (pw_read_line(&u, &p, &d, &s, &o4) == 0) {
if (!strcasecmp(user, u)) {
while (pw_read_line(&user_name, &user_password,
&directory_name, &privilege, &opt4) == 0) {
if (!strcasecmp(user, user_name)) {
pw_close();
return strdup(d);
return strdup(directory_name);
}
}
pw_close();
Expand Down Expand Up @@ -477,17 +479,19 @@ static int
pw_del_user(char *user)
{
bool found=false;
char *u, *p, *d, *s;
char *user_name, *password, *directory_name;
char *privilege;
int opt4;
int match;
int result;

if (pw_open(1) < 0)
return -1;

while (pw_read_line(&u, &p, &d, &s, &opt4) == 0)
while (pw_read_line(&user_name, &password, &directory_name,
&privilege, &opt4) == 0)
{
match = strncmp(user, u, strlen(u));
match = strncmp(user, user_name, strlen(user_name));
if (match == 0)
{
// We found a match
Expand All @@ -499,7 +503,7 @@ pw_del_user(char *user)
{
// not a match so we write this back to the password
// file.
pw_write_line(u, p, d, s, opt4);
pw_write_line(user_name, password, directory_name, privilege, opt4);
}

}
Expand Down

0 comments on commit da06f48

Please sign in to comment.