source: trunk/minix/commands/simple/passwd.c@ 10

Last change on this file since 10 was 9, checked in by Mattia Monga, 13 years ago

Minix 3.1.2a

File size: 5.0 KB
Line 
1/* passwd - change a passwd Author: Adri Koppes */
2
3/* chfn, chsh - change full name, shell Added by: Kees J. Bot */
4
5#include <sys/types.h>
6#include <fcntl.h>
7#include <string.h>
8#include <signal.h>
9#include <pwd.h>
10#include <errno.h>
11#include <stdlib.h>
12#include <unistd.h>
13#include <time.h>
14#include <sys/stat.h>
15#include <minix/minlib.h>
16#include <stdio.h>
17
18_PROTOTYPE(void report, (char *label));
19_PROTOTYPE(void quit, (int ex_stat));
20_PROTOTYPE(void fatal, (char *label));
21_PROTOTYPE(void usage, (void));
22_PROTOTYPE(int goodchars, (char *s));
23_PROTOTYPE(int main, (int argc, char **argv));
24
25char pw_file[] = "/etc/passwd";
26char sh_file[] = "/etc/shadow";
27char pw_tmp[] = "/etc/ptmp";
28char bad[] = "Permission denied\n";
29char buf[1024];
30
31enum action {
32 PASSWD, CHFN, CHSH
33} action = PASSWD;
34
35char *arg0;
36
37void report(label)
38char *label;
39{
40 int e = errno;
41 fprintf(stderr, "%s: ", arg0);
42 fflush(stderr);
43 errno = e;
44 perror(label);
45}
46
47void quit(ex_stat)
48int ex_stat;
49{
50 if (unlink(pw_tmp) < 0 && errno != ENOENT) {
51 report(pw_tmp);
52 ex_stat = 1;
53 }
54 exit(ex_stat);
55}
56
57void fatal(label)
58char *label;
59{
60 report(label);
61 quit(1);
62}
63
64void usage()
65{
66 static char *usages[] = {
67 "passwd [user]\n",
68 "chfn [user] fullname\n",
69 "chsh [user] shell\n"
70 };
71 std_err(usages[(int) action]);
72 exit(1);
73}
74
75int goodchars(s)
76char *s;
77{
78 int c;
79
80 while ((c = *s++) != 0) {
81 if (c == ':' || c < ' ' || c >= 127) return(0);
82 }
83 return(1);
84}
85
86int main(argc, argv)
87int argc;
88char *argv[];
89{
90 int uid, cn, n;
91 int fd_pwd, fd_tmp;
92 FILE *fp_tmp;
93 time_t salt;
94 struct passwd *pwd;
95 char *name, pwname[9], oldpwd[9], newpwd[9], newcrypted[14], sl[2];
96 char *argn;
97 int shadow = 0;
98
99 if ((arg0 = strrchr(argv[0], '/')) != 0)
100 arg0++;
101 else
102 arg0 = argv[0];
103
104 if (strcmp(arg0, "chfn") == 0)
105 action = CHFN;
106 else if (strcmp(arg0, "chsh") == 0)
107 action = CHSH;
108
109 uid = getuid();
110
111 n = action == PASSWD ? 1 : 2;
112
113 if (argc != n && argc != n + 1) usage();
114
115 if (argc == n) {
116 pwd = getpwuid(uid);
117 strcpy(pwname, pwd->pw_name);
118 name = pwname;
119 } else {
120 name = argv[1];
121 pwd = getpwnam(name);
122 }
123 if (pwd == NULL || ((uid != pwd->pw_uid) && uid != 0)) {
124 std_err(bad);
125 exit(1);
126 }
127
128 switch (action) {
129 case PASSWD:
130 if (pwd->pw_passwd[0] == '#' && pwd->pw_passwd[1] == '#') {
131 /* The password is found in the shadow password file. */
132 shadow = 1;
133 strncpy(pwname, pwd->pw_passwd + 2, 8);
134 pwname[8] = 0;
135 name = pwname;
136 setpwfile(sh_file);
137 if ((pwd= getpwnam(name)) == NULL) {
138 std_err(bad);
139 exit(1);
140 }
141 printf("Changing the shadow password of %s\n", name);
142 } else {
143 printf("Changing the password of %s\n", name);
144 }
145
146 oldpwd[0] = 0;
147 if (pwd->pw_passwd[0] != '\0' && uid != 0) {
148 strcpy(oldpwd, getpass("Old password:"));
149 if (strcmp(pwd->pw_passwd, crypt(oldpwd, pwd->pw_passwd)) != 0)
150 {
151 std_err(bad);
152 exit(1);
153 }
154 }
155 for (;;) {
156 strcpy(newpwd, getpass("New password:"));
157
158 if (newpwd[0] == '\0')
159 std_err("Password cannot be null");
160 else if (strcmp(newpwd, getpass("Retype password:")) != 0)
161 std_err("Passwords don't match");
162 else
163 break;
164
165 std_err(", try again\n");
166 }
167 time(&salt);
168 sl[0] = (salt & 077) + '.';
169 sl[1] = ((salt >> 6) & 077) + '.';
170 for (cn = 0; cn < 2; cn++) {
171 if (sl[cn] > '9') sl[cn] += 7;
172 if (sl[cn] > 'Z') sl[cn] += 6;
173 }
174 strcpy(newcrypted, crypt(newpwd, sl));
175 break;
176
177 case CHFN:
178 case CHSH:
179 argn = argv[argc - 1];
180
181 if (strlen(argn) > (action == CHFN ? 80 : 60) || !goodchars(argn)) {
182 std_err(bad);
183 exit(1);
184 }
185 }
186
187 signal(SIGHUP, SIG_IGN);
188 signal(SIGINT, SIG_IGN);
189 signal(SIGQUIT, SIG_IGN);
190 signal(SIGTERM, SIG_IGN);
191
192 umask(0);
193 n = 10;
194 while ((fd_tmp = open(pw_tmp, O_RDWR | O_CREAT | O_EXCL, 0400)) < 0) {
195 if (errno != EEXIST) fatal("Can't create temporary file");
196
197 if (n-- > 0) {
198 sleep(2);
199 } else {
200 fprintf(stderr, "Password file busy, try again later.\n");
201 exit(1);
202 }
203 }
204
205 if ((fp_tmp = fdopen(fd_tmp, "w+")) == NULL) fatal(pw_tmp);
206
207 setpwent();
208 while ((pwd = getpwent()) != 0) {
209 if (strcmp(name, pwd->pw_name) == 0) {
210 switch (action) {
211 case PASSWD:
212 pwd->pw_passwd = newcrypted;
213 break;
214 case CHFN:
215 pwd->pw_gecos = argn;
216 break;
217 case CHSH:
218 pwd->pw_shell = argn;
219 break;
220 }
221 }
222 if (strcmp(pwd->pw_shell, "/bin/sh") == 0
223 || strcmp(pwd->pw_shell, "/usr/bin/sh") == 0
224 )
225 pwd->pw_shell = "";
226
227 fprintf(fp_tmp, "%s:%s:%s:",
228 pwd->pw_name,
229 pwd->pw_passwd,
230 itoa(pwd->pw_uid)
231 );
232 if (ferror(fp_tmp)) fatal(pw_tmp);
233
234 fprintf(fp_tmp, "%s:%s:%s:%s\n",
235 itoa(pwd->pw_gid),
236 pwd->pw_gecos,
237 pwd->pw_dir,
238 pwd->pw_shell
239 );
240 if (ferror(fp_tmp)) fatal(pw_tmp);
241 }
242 endpwent();
243 if (fflush(fp_tmp) == EOF) fatal(pw_tmp);
244
245 if (lseek(fd_tmp, (off_t) 0, SEEK_SET) != 0)
246 fatal("Can't reread temp file");
247
248 if ((fd_pwd = open(shadow ? sh_file : pw_file, O_WRONLY | O_TRUNC)) < 0)
249 fatal("Can't recreate password file");
250
251 while ((n = read(fd_tmp, buf, sizeof(buf))) != 0) {
252 if (n < 0 || write(fd_pwd, buf, n) != n) {
253 report("Error rewriting password file, tell root!");
254 exit(1);
255 }
256 }
257 close(fd_tmp);
258 close(fd_pwd);
259 quit(0);
260}
Note: See TracBrowser for help on using the repository browser.