Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <pwd.h>
- #include <signal.h>
- #include <unistd.h>
- main(int argc, char **argv)
- {
- struct passwd *userlist;
- int count, usernumber, len1;
- FILE *tmp, *stmp, *profile;
- char *commentfield, *username, *userdir, *home;
- char reply;
- commentfield = (char *)malloc(1024*sizeof(char));
- username = (char *)malloc(8*sizeof(char));
- userdir = (char *)malloc(256*sizeof(char));
- home = (char *)malloc(256*sizeof(char));
- if (argc!=4)
- {
- printf("jak korzystac : %s [directory] [nazwa uzytkownika] [komentarz]\n", argv[0]);
- exit(1);
- }
- if( (strlen(argv[2]) < 5) || (strlen(argv[2]) > 8) )
- {
- printf("Nazwa uzytkownika musi miescic sie w zakresie od 5 do 8 znakow :( \n");
- exit(1);
- }
- signal(SIGHUP, SIG_IGN);
- signal(SIGINT, SIG_IGN);
- setpwent();
- count = 0;
- while((userlist = getpwent()) != NULL)
- {
- if(count < userlist->pw_uid)
- {
- count = userlist->pw_uid ;
- usernumber = count + 1;
- }
- }
- printf("usernumber : %d\n", usernumber);
- endpwent();
- sprintf(commentfield,"%s", argv[3]);
- sprintf(username, "%s", argv[2]);
- sprintf(userdir, "%s", argv[1]);
- sprintf(home, "/%s/%s", argv[1], argv[2]);
- printf("\n Sprawdz czy wszystko gra: \n");
- printf("----------------------------------------------------");
- printf("\n Nazwa uzytkownika :\t %s", username);
- printf("\n Directory :\t %s", home);
- printf("\n Komentarz :\t %s", commentfield);
- printf("\n----------------------------------------------------\n\n");
- printf("Czy wszystko sie zgadza? t/n: ");
- scanf("%c", &reply);
- if(reply != 't')
- {
- printf("\n Opuszczam program. Nie wpisano 't'\n");
- exit(1);
- }
- tmp = fopen("/etc/passwd", "a");
- if (tmp == NULL)
- {
- printf("\nBrak dostepu\n");
- exit(1);
- }
- fprintf(tmp, "%s:x:%d:1:%s:%s:/bin/bash\n", username, usernumber, commentfield, home);
- fclose(tmp);
- stmp = fopen("/etc/shadow", "a");
- if (stmp == NULL)
- {
- printf("\nBrak dostepu\n");
- exit(1);
- }
- fprintf(stmp, "%s:*LK*:::::::\n", username);
- fclose(stmp);
- mkdir(home, 0755);
- chdir(home);
- profile = fopen(".profile", "a");
- fprintf(profile, "stty istrip\n");
- fprintf(profile, "PATH=/bin:/usr/bin:/usr/local/bin:/usr/share/bin:.\n");
- fprintf(profile, "export PATH\n");
- fprintf(profile, "\n\n");
- fclose(profile);
- chown(home, usernumber, 1);
- chown(".profile", usernumber, 1);
- chmod(".profile", 0644);
- printf("\n\nSukces!!\n Teraz ustaw haslo: ");
- execl("/usr/bin/passwd", "passwd", username, NULL);
- printf("\n\n Sukces!! Haslo ustawione :)\n\n");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement