Advertisement
Guest User

Untitled

a guest
Jun 20th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <sys/types.h>
  5. #include <pwd.h>
  6. #include <signal.h>
  7. #include <unistd.h>
  8.  
  9. main(int argc, char **argv)
  10. {
  11.  
  12. struct passwd *userlist;
  13. int count, usernumber, len1;
  14. FILE *tmp, *stmp, *profile;
  15. char *commentfield, *username, *userdir, *home;
  16. char reply;
  17.  
  18. commentfield = (char *)malloc(1024*sizeof(char));
  19. username = (char *)malloc(8*sizeof(char));
  20. userdir = (char *)malloc(256*sizeof(char));
  21. home = (char *)malloc(256*sizeof(char));
  22.  
  23. if (argc!=4)
  24. {
  25. printf("jak korzystac : %s [directory] [nazwa uzytkownika] [komentarz]\n", argv[0]);
  26. exit(1);
  27. }
  28.  
  29. if( (strlen(argv[2]) < 5) || (strlen(argv[2]) > 8) )
  30. {
  31. printf("Nazwa uzytkownika musi miescic sie w zakresie od 5 do 8 znakow :( \n");
  32. exit(1);
  33. }
  34.  
  35. signal(SIGHUP, SIG_IGN);
  36. signal(SIGINT, SIG_IGN);
  37.  
  38. setpwent();
  39.  
  40. count = 0;
  41.  
  42. while((userlist = getpwent()) != NULL)
  43. {
  44. if(count < userlist->pw_uid)
  45. {
  46. count = userlist->pw_uid ;
  47. usernumber = count + 1;
  48. }
  49. }
  50. printf("usernumber : %d\n", usernumber);
  51.  
  52. endpwent();
  53.  
  54. sprintf(commentfield,"%s", argv[3]);
  55. sprintf(username, "%s", argv[2]);
  56. sprintf(userdir, "%s", argv[1]);
  57. sprintf(home, "/%s/%s", argv[1], argv[2]);
  58.  
  59.  
  60.  
  61. printf("\n Sprawdz czy wszystko gra: \n");
  62. printf("----------------------------------------------------");
  63. printf("\n Nazwa uzytkownika :\t %s", username);
  64. printf("\n Directory :\t %s", home);
  65. printf("\n Komentarz :\t %s", commentfield);
  66. printf("\n----------------------------------------------------\n\n");
  67.  
  68. printf("Czy wszystko sie zgadza? t/n: ");
  69. scanf("%c", &reply);
  70.  
  71. if(reply != 't')
  72. {
  73. printf("\n Opuszczam program. Nie wpisano 't'\n");
  74. exit(1);
  75. }
  76.  
  77. tmp = fopen("/etc/passwd", "a");
  78. if (tmp == NULL)
  79. {
  80. printf("\nBrak dostepu\n");
  81. exit(1);
  82. }
  83. fprintf(tmp, "%s:x:%d:1:%s:%s:/bin/bash\n", username, usernumber, commentfield, home);
  84. fclose(tmp);
  85.  
  86.  
  87.  
  88. stmp = fopen("/etc/shadow", "a");
  89. if (stmp == NULL)
  90. {
  91. printf("\nBrak dostepu\n");
  92. exit(1);
  93. }
  94. fprintf(stmp, "%s:*LK*:::::::\n", username);
  95. fclose(stmp);
  96.  
  97. mkdir(home, 0755);
  98. chdir(home);
  99.  
  100.  
  101. profile = fopen(".profile", "a");
  102. fprintf(profile, "stty istrip\n");
  103. fprintf(profile, "PATH=/bin:/usr/bin:/usr/local/bin:/usr/share/bin:.\n");
  104. fprintf(profile, "export PATH\n");
  105. fprintf(profile, "\n\n");
  106. fclose(profile);
  107.  
  108. chown(home, usernumber, 1);
  109. chown(".profile", usernumber, 1);
  110. chmod(".profile", 0644);
  111.  
  112.  
  113. printf("\n\nSukces!!\n Teraz ustaw haslo: ");
  114. execl("/usr/bin/passwd", "passwd", username, NULL);
  115. printf("\n\n Sukces!! Haslo ustawione :)\n\n");
  116.  
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement