Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <string.h>
- #ifdef __linux__
- #include <crypt.h>
- #elif __APPLE__
- #include <unistd.h>
- #endif
- #include "mycrypt.h"
- #define DEBUG
- int main(int argc, char* argv[]) {
- char *pw1, *pw2, *entry;
- char linebuf[MAXLINE];
- FILE *pwfile;
- int ret;
- // Check number of arguments.
- if (argc<2) {
- printf("Usage: %s <cmd>\n", argv[0]);
- exit(EXIT_FAILURE);
- }
- // Check, whether file exists.
- pwfile = fopen(PWFILE, "r");
- if (pwfile == NULL) {
- perror("open pwfile");
- exit(EXIT_FAILURE);
- }
- // Search for user in passwords.txt.
- while (!feof(pwfile)) {
- fgets(linebuf, MAXLINE, pwfile);
- ret = memcmp(linebuf, argv[1], strlen(argv[1]));
- if (ret == 0) {
- // Quit, if user already exists.
- printf("User already exists.\n");
- exit(EXIT_FAILURE);
- }
- }
- fclose(pwfile);
- // Continue, if user name is unique.
- if (ret != 0) {
- printf("User not found\n\n");
- }
- // Get user password and hash it immediatly.
- pw1 = strdup(crypt(getpass("Enter your password: "), "ab"));
- pw2 = strdup(crypt(getpass("Re-enter your password: "), "ab"));
- // Compare user passwords and compare them.
- if (strcmp(pw1, pw2) != 0) {
- printf("\nYour provided passwords didn't match.\n");
- exit(EXIT_FAILURE);
- }
- // Concatenate user:hash(password) to char.
- entry = (char *)malloc((strlen(argv[1]) + strlen(pw1) + 1)*sizeof(char));
- strcpy(entry, argv[1]);
- strcat(entry, ":");
- strcat(entry, pw1);
- strcat(entry, "\n");
- // Append char to file.
- pwfile = fopen(PWFILE, "a");
- fprintf(pwfile, "%s", entry);
- fclose(pwfile);
- free(pw1);
- free(pw2);
- free(entry);
- exit(EXIT_SUCCESS);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement