15 // Compile with: gcc -std=c99 -lcrypt
17 /** Deliberately make this smaller so we can test for buffer overflow problems **/
21 bool EnterTheShadowRealm(const char * shadow, const char * salt, const char * username, const char * passwd)
24 if (strlen(username) + strlen(passwd) >= BUFSIZ-1)
26 fprintf(stderr, "User/Password too long!\n");
30 FILE * f = fopen(shadow, "r");
33 fprintf(stderr, "Can't open %s - %s\n", shadow, strerror(errno));
38 int passwd_index = -1;
39 int garbage_index = -1;
40 while (fgets(buffer, BUFSIZ, f) != NULL) // NOTE: Restrict username+password strings to BUFSIZ... what could possibly go wrong?
43 printf("Scanning %d: %s", strlen(buffer), buffer);
45 for (int i = 0; i < BUFSIZ-1; ++i)
55 if (strcmp(username,buffer) == 0)
57 printf("User matches! %s\n", buffer);
63 if (passwd_index <= 0)
65 fprintf(stderr, "No user found matching %s\n", username);
69 for (int i = passwd_index; i < BUFSIZ-1; ++i)
71 if (buffer[i] == ':' || buffer[i] == '\n')
78 printf("Salted Entry: %s\n", buffer+passwd_index);
79 printf("Salted Attempt: %s\n", crypt(passwd, salt));
81 return (strcmp(crypt(passwd, salt), passwd) == 0);
86 int main(int argc, char ** argv)
88 char * shadow = "shadow";
97 // Get the username and password
98 // Need to get these passed through HTTPS at some point
100 char username[BUFSIZ];
101 if (fgets(username, BUFSIZ, stdin) != username)
103 fprintf(stderr, "Username too long!\n");
107 username[strlen(username)-1] = '\0';
109 char * password = getpass("Password: "); //NOTE: getpass is deprecated. Just here for testing.
110 password[strlen(password)-1] = '\0';
112 printf("Could we enter the shadow realm? %d\n", EnterTheShadowRealm(shadow, "A9", username, password));