|
|
1.1 ! root 1: /* ! 2: * chfn - change full name (or other info in gecos field) ! 3: */ ! 4: #include <stdio.h> ! 5: #include <signal.h> ! 6: #include <pwd.h> ! 7: ! 8: char passwd[] = "/etc/passwd"; ! 9: char temp[] = "/etc/ptmp"; ! 10: struct passwd *pwd; ! 11: struct passwd *getpwent(); ! 12: int endpwent(); ! 13: char *crypt(); ! 14: char *getpass(); ! 15: char *pw; ! 16: char pwbuf[10]; ! 17: char buf[BUFSIZ]; ! 18: ! 19: main(argc, argv) ! 20: char *argv[]; ! 21: { ! 22: char *p; ! 23: int i; ! 24: char saltc[2]; ! 25: long salt; ! 26: int u,fi,fo; ! 27: int insist; ! 28: int ok, flags; ! 29: int c; ! 30: int pwlen; ! 31: FILE *tf; ! 32: ! 33: insist = 0; ! 34: if (argc != 3) { ! 35: printf("Usage: chfn user full-name\n"); ! 36: goto bex; ! 37: } ! 38: if (index(argv[2], ':') || index(argv[2], '\n')) { ! 39: printf("Illegal character in new string\n"); ! 40: exit(1); ! 41: } ! 42: while((pwd=getpwent()) != NULL){ ! 43: if(strcmp(pwd->pw_name,argv[1]) == 0){ ! 44: u = getuid(); ! 45: if(u!=0 && u != pwd->pw_uid){ ! 46: printf("Permission denied.\n"); ! 47: goto bex; ! 48: } ! 49: break; ! 50: } ! 51: } ! 52: endpwent(); ! 53: signal(SIGHUP, 1); ! 54: signal(SIGINT, 1); ! 55: signal(SIGQUIT, 1); ! 56: ! 57: if(access(temp, 0) >= 0) { ! 58: printf("Temporary file busy -- try again\n"); ! 59: goto bex; ! 60: } ! 61: if((tf=fopen(temp,"w")) == NULL) { ! 62: printf("Cannot create temporary file\n"); ! 63: goto bex; ! 64: } ! 65: ! 66: /* ! 67: * copy passwd to temp, replacing matching lines ! 68: * with new shell. ! 69: */ ! 70: ! 71: while((pwd=getpwent()) != NULL) { ! 72: if(strcmp(pwd->pw_name,argv[1]) == 0) { ! 73: u = getuid(); ! 74: if(u != 0 && u != pwd->pw_uid) { ! 75: printf("Permission denied.\n"); ! 76: goto out; ! 77: } ! 78: pwd->pw_gecos = argv[2]; ! 79: } ! 80: fprintf(tf,"%s:%s:%d:%d:%s:%s:%s\n", ! 81: pwd->pw_name, ! 82: pwd->pw_passwd, ! 83: pwd->pw_uid, ! 84: pwd->pw_gid, ! 85: pwd->pw_gecos, ! 86: pwd->pw_dir, ! 87: pwd->pw_shell); ! 88: } ! 89: endpwent(); ! 90: fclose(tf); ! 91: ! 92: /* ! 93: * copy temp back to passwd file ! 94: */ ! 95: ! 96: if((fi=open(temp,0)) < 0) { ! 97: printf("Temp file disappeared!\n"); ! 98: goto out; ! 99: } ! 100: if((fo=creat(passwd, 0644)) < 0) { ! 101: printf("Cannot recreat passwd file.\n"); ! 102: goto out; ! 103: } ! 104: while((u=read(fi,buf,sizeof(buf))) > 0) write(fo,buf,u); ! 105: ! 106: out: ! 107: unlink(temp); ! 108: ! 109: bex: ! 110: exit(1); ! 111: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.