BSD 3 development
[unix-history] / usr / src / cmd / chfn.c
CommitLineData
6295589e
MH
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
8char passwd[] = "/etc/passwd";
9char temp[] = "/etc/ptmp";
10struct passwd *pwd;
11struct passwd *getpwent();
12int endpwent();
13char *crypt();
14char *getpass();
15char *pw;
16char pwbuf[10];
17char buf[BUFSIZ];
18
19main(argc, argv)
20char *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
106out:
107 unlink(temp);
108
109bex:
110 exit(1);
111}