xref: /original-bsd/usr.bin/passwd/chsh.sh (revision d25e1985)
1static char *sccsid = "@(#)chsh.sh	4.1 (Berkeley) 10/01/80";
2/*
3 * chsh
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 < 2 || argc > 3) {
36		printf("Usage: chsh user [ /bin/oldcsh ] [ /bin/csh ]\n");
37		goto bex;
38	}
39	if (argc == 2)
40		argv[2] = "";
41	else if (strcmp(argv[2], "/bin/oldcsh") && strcmp(argv[2], "/bin/csh") && getuid()) {
42		printf("Only /bin/oldcsh or /bin/csh may be specified\n");
43		exit(1);
44	}
45	while((pwd=getpwent()) != NULL){
46		if(strcmp(pwd->pw_name,argv[1]) == 0){
47			u = getuid();
48			if(u!=0 && u != pwd->pw_uid){
49				printf("Permission denied.\n");
50				goto bex;
51				}
52			break;
53			}
54		}
55	endpwent();
56	signal(SIGHUP, 1);
57	signal(SIGINT, 1);
58	signal(SIGQUIT, 1);
59	signal(SIGTSTP, 1);
60
61	if(access(temp, 0) >= 0) {
62		printf("Temporary file busy -- try again\n");
63		goto bex;
64	}
65	if((tf=fopen(temp,"w")) == NULL) {
66		printf("Cannot create temporary file\n");
67		goto bex;
68	}
69
70/*
71 *	copy passwd to temp, replacing matching lines
72 *	with new shell.
73 */
74
75	while((pwd=getpwent()) != NULL) {
76		if(strcmp(pwd->pw_name,argv[1]) == 0) {
77			u = getuid();
78			if(u != 0 && u != pwd->pw_uid) {
79				printf("Permission denied.\n");
80				goto out;
81			}
82			pwd->pw_shell = argv[2];
83		}
84		fprintf(tf,"%s:%s:%d:%d:%s:%s:%s\n",
85			pwd->pw_name,
86			pwd->pw_passwd,
87			pwd->pw_uid,
88			pwd->pw_gid,
89			pwd->pw_gecos,
90			pwd->pw_dir,
91			pwd->pw_shell);
92	}
93	endpwent();
94	fclose(tf);
95
96/*
97 *	copy temp back to passwd file
98 */
99
100	if((fi=open(temp,0)) < 0) {
101		printf("Temp file disappeared!\n");
102		goto out;
103	}
104	if((fo=creat(passwd, 0644)) < 0) {
105		printf("Cannot recreat passwd file.\n");
106		goto out;
107	}
108	while((u=read(fi,buf,sizeof(buf))) > 0) write(fo,buf,u);
109
110out:
111	unlink(temp);
112
113bex:
114	exit(1);
115}
116