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