BSD 3 development
[unix-history] / usr / src / cmd / chsh.c
CommitLineData
c57b0c8f
BJ
1/*
2 * chsh
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 < 2 || argc > 3) {
35 printf("Usage: chsh user [ /bin/csh ]\n");
36 goto bex;
37 }
38 if (argc == 2)
39 argv[2] = "";
40 else if (strcmp(argv[2], "/bin/csh") && getuid()) {
41 printf("Only /bin/csh may be specified\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_shell = 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}