change over to new error message format
[unix-history] / usr / src / usr.bin / passwd / chfn.sh
CommitLineData
64504187
BJ
1static char *sccsid = "@(#)chfn.sh 4.1 (Berkeley) %G%";
2/*
3 * chfn - change full name (or other info in gecos field)
4 */
5#include <stdio.h>
6#include <signal.h>
7#include <pwd.h>
8
9char passwd[] = "/etc/passwd";
10char temp[] = "/etc/ptmp";
11struct passwd *pwd;
12struct passwd *getpwent();
13int endpwent();
14char *crypt();
15char *getpass();
16char *pw;
17char pwbuf[10];
18char buf[BUFSIZ];
19
20main(argc, argv)
21char *argv[];
22{
23 char *p;
24 int i;
25 char saltc[2];
26 long salt;
27 int u,fi,fo;
28 int insist;
29 int ok, flags;
30 int c;
31 int pwlen;
32 FILE *tf;
33
34 insist = 0;
35 if (argc != 3) {
36 printf("Usage: chfn user full-name\n");
37 goto bex;
38 }
39 if (index(argv[2], ':') || index(argv[2], '\n')) {
40 printf("Illegal character in new string\n");
41 exit(1);
42 }
43 while((pwd=getpwent()) != NULL){
44 if(strcmp(pwd->pw_name,argv[1]) == 0){
45 u = getuid();
46 if(u!=0 && u != pwd->pw_uid){
47 printf("Permission denied.\n");
48 goto bex;
49 }
50 break;
51 }
52 }
53 endpwent();
54 signal(SIGHUP, 1);
55 signal(SIGINT, 1);
56 signal(SIGQUIT, 1);
57 signal(SIGTSTP, 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:%d:%d:%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
108out:
109 unlink(temp);
110
111bex:
112 exit(1);
113}