#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <crypt.h>
#include <ctype.h>
#include <assert.h>
struct user {
char* username;
char* name;
char* saltandhash;
char* state;
};
int main(int argc, char **argv)
{
if ( argc != 3 )
{
printf("usage: %s passwd-path shadow-path", argv[0]);
exit(EXIT_FAILURE);
}
char* passwd = argv[1];
char* shadow = argv[2];
FILE* fp_shadow;
FILE* fp_passwd;
char* line_shadow = NULL;
char* line_passwd = NULL;
size_t len_shadow = 0;
size_t len_passwd = 0;
ssize_t read_shadow;
ssize_t read_passwd;
FILE* fp_dict;
char* line_dict = NULL;
size_t len_dict = 0;
ssize_t read_dict;
const char* dict = "top250short.txt";
fp_shadow = fopen(shadow, "r");
if (fp_shadow == NULL)
exit(EXIT_FAILURE);
fp_passwd = fopen(passwd, "r");
if(fp_passwd == NULL)
exit(EXIT_FAILURE);
int first_time = 1;
int found = 0;
int counter = 0;
struct user users[8196];
struct user u = {NULL, NULL, NULL, NULL};
char salt[6];
char* username = NULL;
char* saltandhash = NULL;
char* name = NULL;
while ((read_shadow = getline(&line_shadow, &len_shadow, fp_shadow)) != -1) {
username = strdup(strsep(&line_shadow, ":"));
saltandhash = strdup(strsep(&line_shadow, ":"));
if(first_time){
int i;
for (i = 0; i < 6; ++i){
salt[i] = saltandhash[i];
}
first_time = 0;
}
read_passwd = getline(&line_passwd, &len_passwd, fp_passwd);
if(read_passwd == -1)
exit(EXIT_FAILURE);
strsep(&line_passwd, ":");
strsep(&line_passwd, ":");
strsep(&line_passwd, ":");
strsep(&line_passwd, ":");
name = strdup(strsep(&line_passwd, ":"));
u.username = strdup(username);
u.name = strdup(name);
u.saltandhash = strdup(saltandhash);
u.state = strdup("NF");
users[counter] = u;
printf("%s***%s***%s\n", users[0].username, users[0].saltandhash, users[0].state);
counter++;
}
return 0;
}
In this code, I am trying to read passwd and shadow from files and store the values ββthat I analyze in an array of structures.
The passwd file is as follows:
mly460:x:1002:1002:Michael Lily,,,:/home/mly460:/bin/sh
(and similar lines)
the shadow file is as follows:
mly460:$1$R5$yo1/aN8VtX54qAf1W8Rhe0:16458::::::
(and similar lines)
The output of this program at startup. / executable passwd-path shadow-path
mly460***$1$R5$yo1/aN8VtX54qAf1W8Rhe0***NF
mly460***$1$R5$yo1/aN8VtX54qAf1W8Rhe0***NF
mly460***$1$R5$yo1/aN8VtX54qAf1W8Rhe0***NF
tcsh
***$1$R5$yo1/aN8VtX54qAf1W8Rhe0***NF
***$1$R5$yo1/aN8VtX54qAf1W8Rhe0***NF
***59:/bin/tcsh
***NF
***011***NF
***011***Calvert,,,
***011***Calvert,,,
:::
***011***Calvert,,,
1$R5$Z8T2h9IbKpHEhVouZOXIt1***011***Calvert,,,
1$R5$Z8T2h9IbKpHEhVouZOXIt1***16415::::::
***Calvert,,,
1$R5$Z8T2h9IbKpHEhVouZOXIt1***bwm505***
1$R5$Z8T2h9IbKpHEhVouZOXIt1***bwm505***$kMLZJaGeDjfu0e3d5XZM/1
1$R5$Z8T2h9IbKpHEhVouZOXIt1***bwm505***$kMLZJaGeDjfu0e3d5XZM/1
1$R5$Z8T2h9IbKpHEhVouZOXIt1***bwm505***$kMLZJaGeDjfu0e3d5XZM/1
(and the rest is the same line.)
As you can see at the end, I always print the contents of the structure, which is stored at the 0th position in the users array.
What is strange is that the contents of the 0-th CHANGE element as a while loop progresses, but after a certain point it stabilizes to a certain value.
. "saltandhash", , printf. ( , .)
- ?
( ): strsep line_shadow line_paswd. char * , :
char* copy_to_parse = strdup(line_shadow);