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