Commit | Line | Data |
---|---|---|
22d4760e SL |
1 | #ifndef lint |
2 | static char *sccsid = "@(#)login.c 4.24 83/05/22"; | |
3 | #endif | |
4 | ||
88a01c09 BJ |
5 | /* |
6 | * login [ name ] | |
86eb6c9e | 7 | * login -r |
88a01c09 BJ |
8 | */ |
9 | ||
10 | #include <sys/types.h> | |
11 | #include <sgtty.h> | |
12 | #include <utmp.h> | |
13 | #include <signal.h> | |
14 | #include <pwd.h> | |
15 | #include <stdio.h> | |
16 | #include <sys/stat.h> | |
17 | #include <lastlog.h> | |
22d4760e SL |
18 | #define QUOTA |
19 | #include <sys/quota.h> | |
20 | #include <errno.h> | |
f570e1ff BJ |
21 | |
22 | #define SCPYN(a, b) strncpy(a, b, sizeof(a)) | |
88a01c09 | 23 | |
b4389814 BJ |
24 | #define NMAX sizeof(utmp.ut_name) |
25 | #define LMAX sizeof(utmp.ut_line) | |
88a01c09 | 26 | |
f570e1ff BJ |
27 | #define FALSE 0 |
28 | #define TRUE -1 | |
29 | ||
30 | char nolog[] = "/etc/nologin"; | |
31 | char qlog[] = ".hushlogin"; | |
32 | char securetty[] = "/etc/securetty"; | |
88a01c09 BJ |
33 | char maildir[30] = "/usr/spool/mail/"; |
34 | char lastlog[] = "/usr/adm/lastlog"; | |
3479a16a | 35 | struct passwd nouser = {"", "nope", -1, -1, -1, "", "", "", "" }; |
88a01c09 BJ |
36 | struct sgttyb ttyb; |
37 | struct utmp utmp; | |
38 | char minusnam[16] = "-"; | |
86eb6c9e | 39 | |
88a01c09 BJ |
40 | char homedir[64] = "HOME="; |
41 | char shell[64] = "SHELL="; | |
42 | char term[64] = "TERM="; | |
f570e1ff | 43 | char user[20] = "USER="; |
86eb6c9e BJ |
44 | char *speeds[] = |
45 | { "0", "50", "75", "110", "134", "150", "200", "300", | |
46 | "600", "1200", "1800", "2400", "4800", "9600", "19200", "38400" }; | |
47 | #define NSPEEDS (sizeof (speeds) / sizeof (speeds[0])) | |
48 | ||
49 | char *envinit[] = | |
50 | {homedir, shell, "PATH=:/usr/ucb:/bin:/usr/bin", term, user, 0}; | |
88a01c09 | 51 | |
86eb6c9e | 52 | struct passwd *pwd; |
88a01c09 | 53 | struct passwd *getpwnam(); |
86eb6c9e | 54 | char *strcat(), *rindex(), *index(); |
88a01c09 BJ |
55 | int setpwent(); |
56 | char *ttyname(); | |
57 | char *crypt(); | |
58 | char *getpass(); | |
88a01c09 BJ |
59 | char *stypeof(); |
60 | extern char **environ; | |
22d4760e | 61 | extern int errno; |
88a01c09 | 62 | |
3479a16a SL |
63 | struct ttychars tc = { |
64 | CERASE, CKILL, CINTR, CQUIT, CSTART, | |
65 | CSTOP, CEOF, CBRK, CSUSP, CDSUSP, | |
66 | CRPRNT, CFLUSH, CWERASE,CLNEXT | |
841d84b0 BJ |
67 | }; |
68 | ||
86eb6c9e | 69 | int rflag; |
b4389814 | 70 | char rusername[NMAX+1], lusername[NMAX+1]; |
86eb6c9e | 71 | char rpassword[NMAX+1]; |
e5321f7b | 72 | char name[NMAX+1]; |
b4389814 | 73 | char *rhost; |
86eb6c9e | 74 | |
88a01c09 BJ |
75 | main(argc, argv) |
76 | char **argv; | |
77 | { | |
78 | register char *namep; | |
79 | int t, f, c; | |
f570e1ff BJ |
80 | int invalid; |
81 | int quietlog; | |
82 | int i; | |
83 | FILE *nlfd; | |
88a01c09 | 84 | char *ttyn; |
b4389814 BJ |
85 | int ldisc = 0, zero = 0; |
86 | FILE *hostf; int first = 1; | |
88a01c09 BJ |
87 | |
88 | alarm(60); | |
89 | signal(SIGQUIT, SIG_IGN); | |
90 | signal(SIGINT, SIG_IGN); | |
91 | nice(-100); | |
92 | nice(20); | |
93 | nice(0); | |
22d4760e | 94 | quota(Q_SETUID, 0, 0, 0); |
4f8d3876 | 95 | if (argc > 1 && !strcmp(argv[1], "-r")) { |
86eb6c9e | 96 | rflag++; |
4f8d3876 | 97 | rhost = argv[2]; |
b4389814 | 98 | argc = 1; |
4f8d3876 BJ |
99 | getstr(rusername, sizeof (rusername), "remuser"); |
100 | getstr(lusername, sizeof (lusername), "locuser"); | |
86eb6c9e | 101 | getstr(term+5, sizeof(term)-5, "Terminal type"); |
4f8d3876 BJ |
102 | if (getuid()) |
103 | goto abnormal; | |
b4389814 BJ |
104 | setpwent(); |
105 | pwd = getpwnam(lusername); | |
4f8d3876 | 106 | endpwent(); |
703f91ff | 107 | if (pwd == NULL) |
4f8d3876 | 108 | goto abnormal; |
1886582e | 109 | hostf = pwd->pw_uid ? fopen("/etc/hosts.equiv", "r") : 0; |
b4389814 BJ |
110 | again: |
111 | if (hostf) { | |
fd29004e CL |
112 | char ahost[32]; |
113 | ||
114 | while (fgets(ahost, sizeof (ahost), hostf)) { | |
115 | char *user; | |
116 | ||
32e13142 CL |
117 | if ((user = index(ahost, '\n')) != 0) |
118 | *user++ = '\0'; | |
119 | if ((user = index(ahost, ' ')) != 0) | |
120 | *user++ = '\0'; | |
fd29004e CL |
121 | if (!strcmp(rhost, ahost) && |
122 | !strcmp(rusername, user ? | |
123 | user : lusername)) { | |
124 | fclose(hostf); | |
125 | goto normal; | |
126 | } | |
4f8d3876 | 127 | } |
fd29004e | 128 | fclose(hostf); |
b4389814 BJ |
129 | } |
130 | if (first == 1) { | |
fd29004e CL |
131 | char *rhosts = ".rhosts"; |
132 | struct stat sbuf; | |
133 | ||
b4389814 BJ |
134 | first = 0; |
135 | if (chdir(pwd->pw_dir) < 0) | |
136 | goto again; | |
fd29004e CL |
137 | if (lstat(rhosts, &sbuf) < 0) |
138 | goto again; | |
139 | if ((sbuf.st_mode & S_IFMT) == S_IFLNK) { | |
140 | printf("login: .rhosts is a soft link.\r\n"); | |
fd29004e CL |
141 | goto abnormal; |
142 | } | |
143 | hostf = fopen(rhosts, "r"); | |
144 | fstat(fileno(hostf), &sbuf); | |
145 | if ((int) sbuf.st_uid != pwd->pw_uid && | |
146 | (int) sbuf.st_uid != 0) { | |
147 | printf("login: Bad .rhosts ownership.\r\n"); | |
148 | fclose(hostf); | |
149 | goto abnormal; | |
150 | } | |
151 | goto again; | |
b4389814 | 152 | } |
4f8d3876 | 153 | abnormal: |
b4389814 BJ |
154 | rhost = 0; |
155 | rflag = -1; | |
86eb6c9e | 156 | } |
b4389814 | 157 | normal: |
3479a16a | 158 | ioctl(0, TIOCLSET, &zero); /* XXX */ |
c95ed2b2 | 159 | ioctl(0, TIOCNXCL, 0); |
4f8d3876 BJ |
160 | ioctl(0, FIONBIO, &zero); |
161 | ioctl(0, FIOASYNC, &zero); | |
3479a16a | 162 | ioctl(0, TIOCGETP, &ttyb); /* XXX */ |
86eb6c9e BJ |
163 | if (rflag) { |
164 | char *cp = index(term, '/'); | |
165 | if (cp) { | |
166 | int i; | |
167 | *cp++ = 0; | |
168 | for (i = 0; i < NSPEEDS; i++) | |
169 | if (!strcmp(speeds[i], cp)) { | |
170 | ttyb.sg_ispeed = ttyb.sg_ospeed = i; | |
171 | break; | |
172 | } | |
173 | } | |
174 | ttyb.sg_flags = ECHO|CRMOD|ANYP|XTABS; | |
175 | } | |
3479a16a SL |
176 | ioctl(0, TIOCSETP, &ttyb); /* XXX */ |
177 | ioctl(0, TIOCCSET, &tc); | |
88a01c09 BJ |
178 | for (t=3; t<20; t++) |
179 | close(t); | |
180 | ttyn = ttyname(0); | |
f570e1ff | 181 | if (ttyn==(char *)0) |
88a01c09 | 182 | ttyn = "/dev/tty??"; |
f570e1ff BJ |
183 | do { |
184 | ldisc = 0; | |
c95ed2b2 | 185 | ioctl(0, TIOCSETD, &ldisc); |
f570e1ff BJ |
186 | invalid = FALSE; |
187 | SCPYN(utmp.ut_name, ""); | |
188 | if (argc>1) { | |
189 | SCPYN(utmp.ut_name, argv[1]); | |
190 | argc = 0; | |
191 | } | |
4f8d3876 | 192 | if (rflag) { |
3479a16a | 193 | SCPYN(utmp.ut_name, lusername); |
4f8d3876 BJ |
194 | if (rflag == -1) |
195 | rflag = 0; | |
196 | } else | |
b4389814 BJ |
197 | while (utmp.ut_name[0] == '\0') { |
198 | namep = utmp.ut_name; | |
199 | { char hostname[32]; | |
200 | gethostname(hostname, sizeof (hostname)); | |
201 | printf("%s login: ", hostname); } | |
202 | while ((c = getchar()) != '\n') { | |
203 | if (c == ' ') | |
204 | c = '_'; | |
205 | if (c == EOF) | |
206 | exit(0); | |
207 | if (namep < utmp.ut_name+NMAX) | |
208 | *namep++ = c; | |
209 | } | |
f570e1ff | 210 | } |
b4389814 BJ |
211 | if (rhost == 0) { |
212 | setpwent(); | |
213 | if ((pwd = getpwnam(utmp.ut_name)) == NULL) | |
214 | pwd = &nouser; | |
215 | endpwent(); | |
f570e1ff | 216 | } |
f570e1ff BJ |
217 | if (!strcmp(pwd->pw_shell, "/bin/csh")) { |
218 | ldisc = NTTYDISC; | |
219 | ioctl(0, TIOCSETD, &ldisc); | |
220 | } | |
b4389814 BJ |
221 | if (rhost == 0) { |
222 | if (*pwd->pw_passwd != '\0') { | |
223 | char *pp; | |
224 | nice(-4); | |
225 | if (rflag == 0) | |
226 | pp = getpass("Password:"); | |
227 | else | |
228 | pp = rpassword; | |
229 | namep = crypt(pp,pwd->pw_passwd); | |
230 | nice(4); | |
231 | if (strcmp(namep, pwd->pw_passwd)) | |
232 | invalid = TRUE; | |
233 | } | |
f570e1ff BJ |
234 | } |
235 | if (pwd->pw_uid != 0 && (nlfd = fopen(nolog, "r")) > 0) { | |
236 | /* logins are disabled except for root */ | |
237 | while ((c = getc(nlfd)) != EOF) | |
238 | putchar(c); | |
239 | fflush(stdout); | |
240 | sleep(5); | |
241 | exit(0); | |
242 | } | |
243 | if (!invalid && pwd->pw_uid == 0 && | |
244 | !rootterm(ttyn+sizeof("/dev/")-1)) { | |
4f8d3876 BJ |
245 | logerr("ROOT LOGIN REFUSED %s", |
246 | ttyn+sizeof("/dev/")-1); | |
f570e1ff BJ |
247 | invalid = TRUE; |
248 | } | |
249 | if (invalid) { | |
88a01c09 | 250 | printf("Login incorrect\n"); |
4f8d3876 BJ |
251 | if (ttyn[sizeof("/dev/tty")-1] == 'd') |
252 | logerr("BADDIALUP %s %s\n", | |
253 | ttyn+sizeof("/dev/")-1, utmp.ut_name); | |
88a01c09 | 254 | } |
f570e1ff BJ |
255 | if (*pwd->pw_shell == '\0') |
256 | pwd->pw_shell = "/bin/sh"; | |
257 | i = strlen(pwd->pw_shell); | |
258 | if (chdir(pwd->pw_dir) < 0 && !invalid ) { | |
259 | if (chdir("/") < 0) { | |
260 | printf("No directory!\n"); | |
261 | invalid = TRUE; | |
262 | } else { | |
263 | printf("No directory! Logging in with home=/\n"); | |
264 | pwd->pw_dir = "/"; | |
265 | } | |
88a01c09 | 266 | } |
86eb6c9e BJ |
267 | if (rflag && invalid) |
268 | exit(1); | |
f570e1ff | 269 | } while (invalid); |
88a01c09 | 270 | |
22d4760e SL |
271 | if (quota(Q_SETUID, pwd->pw_uid, 0, 0) < 0) { |
272 | if (errno == EUSERS) | |
273 | printf("%s.\n%s.\n", | |
274 | "Too many users logged on already", | |
275 | "Try again later"); | |
276 | else if (errno == EPROCLIM) | |
277 | printf("You have too many processes running.\n"); | |
278 | else | |
279 | perror("setuid"); | |
280 | sleep(5); | |
281 | exit(0); | |
282 | } | |
88a01c09 BJ |
283 | time(&utmp.ut_time); |
284 | t = ttyslot(); | |
285 | if (t>0 && (f = open("/etc/utmp", 1)) >= 0) { | |
286 | lseek(f, (long)(t*sizeof(utmp)), 0); | |
287 | SCPYN(utmp.ut_line, rindex(ttyn, '/')+1); | |
288 | write(f, (char *)&utmp, sizeof(utmp)); | |
289 | close(f); | |
290 | } | |
291 | if (t>0 && (f = open("/usr/adm/wtmp", 1)) >= 0) { | |
292 | lseek(f, 0L, 2); | |
293 | write(f, (char *)&utmp, sizeof(utmp)); | |
294 | close(f); | |
295 | } | |
4f8d3876 | 296 | quietlog = 0; |
f570e1ff | 297 | if (access(qlog, 0) == 0) |
4f8d3876 BJ |
298 | quietlog = 1; |
299 | if ((f = open(lastlog, 2)) >= 0) { | |
f570e1ff BJ |
300 | struct lastlog ll; |
301 | ||
302 | lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0); | |
303 | if (read(f, (char *) &ll, sizeof ll) == sizeof ll && | |
304 | ll.ll_time != 0) { | |
4f8d3876 | 305 | if (quietlog == 0) |
f570e1ff BJ |
306 | printf("Last login: %.*s on %.*s\n" |
307 | , 24-5 | |
308 | , (char *) ctime(&ll.ll_time) | |
309 | , sizeof(ll.ll_line) | |
310 | , ll.ll_line | |
311 | ); | |
312 | } | |
313 | lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0); | |
314 | time(&ll.ll_time); | |
315 | SCPYN(ll.ll_line, rindex(ttyn, '/')+1); | |
316 | write(f, (char *) &ll, sizeof ll); | |
317 | close(f); | |
318 | } | |
88a01c09 | 319 | chown(ttyn, pwd->pw_uid, pwd->pw_gid); |
3479a16a | 320 | chmod(ttyn, 0622); |
88a01c09 | 321 | setgid(pwd->pw_gid); |
e5321f7b KM |
322 | strncpy(name, utmp.ut_name, NMAX); |
323 | name[NMAX] = '\0'; | |
b1198826 | 324 | initgroups(name, pwd->pw_gid); |
22d4760e | 325 | quota(Q_DOWARN, pwd->pw_uid, (dev_t)-1, 0); |
88a01c09 | 326 | setuid(pwd->pw_uid); |
88a01c09 BJ |
327 | environ = envinit; |
328 | strncat(homedir, pwd->pw_dir, sizeof(homedir)-6); | |
329 | strncat(shell, pwd->pw_shell, sizeof(shell)-7); | |
4f8d3876 | 330 | if (term[strlen("TERM=")] == 0) |
86eb6c9e | 331 | strncat(term, stypeof(ttyn), sizeof(term)-6); |
f570e1ff | 332 | strncat(user, pwd->pw_name, sizeof(user)-6); |
88a01c09 BJ |
333 | if ((namep = rindex(pwd->pw_shell, '/')) == NULL) |
334 | namep = pwd->pw_shell; | |
335 | else | |
336 | namep++; | |
337 | strcat(minusnam, namep); | |
338 | alarm(0); | |
b4389814 | 339 | umask(022); |
4f8d3876 BJ |
340 | if (ttyn[sizeof("/dev/tty")-1] == 'd') |
341 | logerr("DIALUP %s %s\n", ttyn+sizeof("/dev/")-1, pwd->pw_name); | |
342 | if (!quietlog) { | |
f570e1ff BJ |
343 | showmotd(); |
344 | strcat(maildir, pwd->pw_name); | |
345 | if (access(maildir,4)==0) { | |
346 | struct stat statb; | |
347 | stat(maildir, &statb); | |
348 | if (statb.st_size) | |
349 | printf("You have mail.\n"); | |
350 | } | |
351 | } | |
352 | ||
88a01c09 BJ |
353 | signal(SIGQUIT, SIG_DFL); |
354 | signal(SIGINT, SIG_DFL); | |
5f87416f | 355 | signal(SIGTSTP, SIG_IGN); |
88a01c09 | 356 | execlp(pwd->pw_shell, minusnam, 0); |
f570e1ff | 357 | perror(pwd->pw_shell); |
88a01c09 BJ |
358 | printf("No shell\n"); |
359 | exit(0); | |
360 | } | |
361 | ||
362 | int stopmotd; | |
363 | catch() | |
364 | { | |
1886582e | 365 | |
88a01c09 BJ |
366 | signal(SIGINT, SIG_IGN); |
367 | stopmotd++; | |
368 | } | |
369 | ||
f570e1ff | 370 | rootterm(tty) |
1886582e | 371 | char *tty; |
f570e1ff BJ |
372 | { |
373 | register FILE *fd; | |
1886582e | 374 | char buf[100]; |
f570e1ff BJ |
375 | |
376 | if ((fd = fopen(securetty, "r")) == NULL) | |
377 | return(1); | |
378 | while (fgets(buf, sizeof buf, fd) != NULL) { | |
379 | buf[strlen(buf)-1] = '\0'; | |
380 | if (strcmp(tty, buf) == 0) { | |
381 | fclose(fd); | |
382 | return(1); | |
383 | } | |
384 | } | |
385 | fclose(fd); | |
386 | return(0); | |
387 | } | |
388 | ||
88a01c09 BJ |
389 | showmotd() |
390 | { | |
391 | FILE *mf; | |
392 | register c; | |
393 | ||
394 | signal(SIGINT, catch); | |
f570e1ff BJ |
395 | if ((mf = fopen("/etc/motd","r")) != NULL) { |
396 | while ((c = getc(mf)) != EOF && stopmotd == 0) | |
88a01c09 BJ |
397 | putchar(c); |
398 | fclose(mf); | |
399 | } | |
400 | signal(SIGINT, SIG_IGN); | |
401 | } | |
402 | ||
f570e1ff | 403 | #undef UNKNOWN |
88a01c09 BJ |
404 | #define UNKNOWN "su" |
405 | ||
406 | char * | |
407 | stypeof(ttyid) | |
408 | char *ttyid; | |
409 | { | |
410 | static char typebuf[16]; | |
411 | char buf[50]; | |
412 | register FILE *f; | |
413 | register char *p, *t, *q; | |
414 | ||
415 | if (ttyid == NULL) | |
416 | return (UNKNOWN); | |
417 | f = fopen("/etc/ttytype", "r"); | |
418 | if (f == NULL) | |
419 | return (UNKNOWN); | |
420 | /* split off end of name */ | |
421 | for (p = q = ttyid; *p != 0; p++) | |
422 | if (*p == '/') | |
423 | q = p + 1; | |
424 | ||
425 | /* scan the file */ | |
426 | while (fgets(buf, sizeof buf, f) != NULL) | |
427 | { | |
f570e1ff | 428 | for (t=buf; *t!=' ' && *t != '\t'; t++) |
88a01c09 BJ |
429 | ; |
430 | *t++ = 0; | |
f570e1ff BJ |
431 | while (*t == ' ' || *t == '\t') |
432 | t++; | |
88a01c09 BJ |
433 | for (p=t; *p>' '; p++) |
434 | ; | |
435 | *p = 0; | |
436 | if (strcmp(q,t)==0) { | |
437 | strcpy(typebuf, buf); | |
438 | fclose(f); | |
439 | return (typebuf); | |
440 | } | |
441 | } | |
442 | fclose (f); | |
443 | return (UNKNOWN); | |
444 | } | |
86eb6c9e BJ |
445 | |
446 | getstr(buf, cnt, err) | |
447 | char *buf; | |
448 | int cnt; | |
449 | char *err; | |
450 | { | |
451 | char c; | |
452 | ||
453 | do { | |
454 | if (read(0, &c, 1) != 1) | |
455 | exit(1); | |
456 | if (--cnt < 0) { | |
457 | printf("%s too long\r\n", err); | |
458 | exit(1); | |
459 | } | |
460 | *buf++ = c; | |
461 | } while (c != 0); | |
462 | } | |
4f8d3876 BJ |
463 | |
464 | logerr(fmt, a1, a2, a3) | |
465 | char *fmt, *a1, *a2, *a3; | |
466 | { | |
467 | ||
468 | } |