Advertisement
Guest User

Untitled

a guest
May 3rd, 2018
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.90 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <unistd.h>
  4. #include <string.h>
  5. #ifdef __linux__
  6. #include <crypt.h>
  7. #elif __APPLE__
  8. #include <unistd.h>
  9. #endif
  10. #include "mycrypt.h"
  11.  
  12. #define DEBUG
  13.  
  14. int main(int argc, char* argv[]) {
  15. char *pw1, *pw2, *entry;
  16. char linebuf[MAXLINE];
  17. FILE *pwfile;
  18. int ret;
  19.  
  20. // Check number of arguments.
  21. if (argc<2) {
  22. printf("Usage: %s <cmd>\n", argv[0]);
  23. exit(EXIT_FAILURE);
  24. }
  25.  
  26. // Check, whether file exists.
  27. pwfile = fopen(PWFILE, "r");
  28. if (pwfile == NULL) {
  29. perror("open pwfile");
  30. exit(EXIT_FAILURE);
  31. }
  32.  
  33. // Search for user in passwords.txt.
  34. while (!feof(pwfile)) {
  35. fgets(linebuf, MAXLINE, pwfile);
  36. ret = memcmp(linebuf, argv[1], strlen(argv[1]));
  37. if (ret == 0) {
  38. // Quit, if user already exists.
  39. printf("User already exists.\n");
  40. exit(EXIT_FAILURE);
  41. }
  42. }
  43. fclose(pwfile);
  44.  
  45. // Continue, if user name is unique.
  46. if (ret != 0) {
  47. printf("User not found\n\n");
  48. }
  49.  
  50. // Get user password and hash it immediatly.
  51. pw1 = strdup(crypt(getpass("Enter your password: "), "ab"));
  52. pw2 = strdup(crypt(getpass("Re-enter your password: "), "ab"));
  53.  
  54. // Compare user passwords and compare them.
  55. if (strcmp(pw1, pw2) != 0) {
  56. printf("\nYour provided passwords didn't match.\n");
  57. exit(EXIT_FAILURE);
  58. }
  59.  
  60. // Concatenate user:hash(password) to char.
  61. entry = (char *)malloc((strlen(argv[1]) + strlen(pw1) + 1)*sizeof(char));
  62. strcpy(entry, argv[1]);
  63. strcat(entry, ":");
  64. strcat(entry, pw1);
  65. strcat(entry, "\n");
  66.  
  67. // Append char to file.
  68. pwfile = fopen(PWFILE, "a");
  69. fprintf(pwfile, "%s", entry);
  70.  
  71. fclose(pwfile);
  72. free(pw1);
  73. free(pw2);
  74. free(entry);
  75.  
  76. exit(EXIT_SUCCESS);
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement