avoid losing alarm clock
[unix-history] / usr / src / usr.bin / login / login.c.1
CommitLineData
22d4760e 1#ifndef lint
714accc5 2static char *sccsid = "@(#)login.c.1 4.30 (Berkeley) 83/06/13";
22d4760e
SL
3#endif
4
88a01c09
BJ
5/*
6 * login [ name ]
3b8dd95e
SL
7 * login -r hostname (for rlogind)
8 * login -h hostname (for telnetd, etc.)
88a01c09
BJ
9 */
10
7a625b73 11#include <sys/param.h>
3b8dd95e
SL
12#include <sys/quota.h>
13#include <sys/stat.h>
14#include <sys/time.h>
15#include <sys/resource.h>
16
88a01c09
BJ
17#include <sgtty.h>
18#include <utmp.h>
19#include <signal.h>
20#include <pwd.h>
21#include <stdio.h>
88a01c09 22#include <lastlog.h>
22d4760e 23#include <errno.h>
f570e1ff
BJ
24
25#define SCPYN(a, b) strncpy(a, b, sizeof(a))
88a01c09 26
b4389814 27#define NMAX sizeof(utmp.ut_name)
88a01c09 28
f570e1ff
BJ
29#define FALSE 0
30#define TRUE -1
31
32char nolog[] = "/etc/nologin";
33char qlog[] = ".hushlogin";
34char securetty[] = "/etc/securetty";
88a01c09
BJ
35char maildir[30] = "/usr/spool/mail/";
36char lastlog[] = "/usr/adm/lastlog";
3479a16a 37struct passwd nouser = {"", "nope", -1, -1, -1, "", "", "", "" };
88a01c09
BJ
38struct sgttyb ttyb;
39struct utmp utmp;
40char minusnam[16] = "-";
3b8dd95e
SL
41/*
42 * This bounds the time given to login. We initialize it here
43 * so it can be patched on machines where it's too small.
44 */
45int timeout = 60;
86eb6c9e 46
88a01c09
BJ
47char homedir[64] = "HOME=";
48char shell[64] = "SHELL=";
49char term[64] = "TERM=";
f570e1ff 50char user[20] = "USER=";
86eb6c9e
BJ
51
52char *envinit[] =
3b8dd95e 53 { homedir, shell, "PATH=:/usr/ucb:/bin:/usr/bin", term, user, 0 };
88a01c09 54
86eb6c9e 55struct passwd *pwd;
88a01c09 56struct passwd *getpwnam();
86eb6c9e 57char *strcat(), *rindex(), *index();
88a01c09 58int setpwent();
3b8dd95e 59int timedout();
88a01c09
BJ
60char *ttyname();
61char *crypt();
62char *getpass();
88a01c09
BJ
63char *stypeof();
64extern char **environ;
22d4760e 65extern int errno;
88a01c09 66
714accc5
SL
67struct tchars tc = {
68 CINTR, CQUIT, CSTART, CSTOP, CEOT, CBRK
69};
70struct ltchars ltc = {
71 CSUSP, CDSUSP, CRPRNT, CFLUSH, CWERASE, CLNEXT
841d84b0
BJ
72};
73
86eb6c9e 74int rflag;
b4389814 75char rusername[NMAX+1], lusername[NMAX+1];
86eb6c9e 76char rpassword[NMAX+1];
e5321f7b 77char name[NMAX+1];
b4389814 78char *rhost;
86eb6c9e 79
88a01c09 80main(argc, argv)
3b8dd95e 81 char *argv[];
88a01c09
BJ
82{
83 register char *namep;
3b8dd95e
SL
84 int t, f, c, i;
85 int invalid, quietlog;
f570e1ff 86 FILE *nlfd;
88a01c09 87 char *ttyn;
b4389814 88 int ldisc = 0, zero = 0;
88a01c09 89
3b8dd95e
SL
90 signal(SIGALRM, timedout);
91 alarm(timeout);
88a01c09
BJ
92 signal(SIGQUIT, SIG_IGN);
93 signal(SIGINT, SIG_IGN);
3b8dd95e 94 setpriority(PRIO_PROCESS, 0, 0);
22d4760e 95 quota(Q_SETUID, 0, 0, 0);
3b8dd95e
SL
96 /*
97 * -r is used by rlogind to cause the autologin protocol;
98 * -h is used by other servers to pass the name of the
99 * remote host to login so that it may be placed in utmp and wtmp
100 */
101 if (argc > 1) {
102 if (strcmp(argv[1], "-r") == 0) {
103 rflag = doremotelogin(argv[2]);
104 SCPYN(utmp.ut_host, argv[2]);
105 argc = 0;
b4389814 106 }
3b8dd95e
SL
107 if (strcmp(argv[1], "-h") == 0 && getuid() == 0) {
108 SCPYN(utmp.ut_host, argv[2]);
109 argc = 0;
b4389814 110 }
86eb6c9e 111 }
714accc5 112 ioctl(0, TIOCLSET, &zero);
c95ed2b2 113 ioctl(0, TIOCNXCL, 0);
4f8d3876
BJ
114 ioctl(0, FIONBIO, &zero);
115 ioctl(0, FIOASYNC, &zero);
714accc5 116 ioctl(0, TIOCGETP, &ttyb);
3b8dd95e
SL
117 /*
118 * If talking to an rlogin process,
119 * propagate the terminal type and
120 * baud rate across the network.
121 */
122 if (rflag)
123 doremoteterm(term, &ttyb);
714accc5
SL
124 ioctl(0, TIOCSLTC, &ltc);
125 ioctl(0, TIOCSETC, &tc);
126 ioctl(0, TIOCSETP, &ttyb);
3b8dd95e 127 for (t = getdtablesize(); t > 3; t--)
88a01c09
BJ
128 close(t);
129 ttyn = ttyname(0);
f570e1ff 130 if (ttyn==(char *)0)
88a01c09 131 ttyn = "/dev/tty??";
f570e1ff
BJ
132 do {
133 ldisc = 0;
c95ed2b2 134 ioctl(0, TIOCSETD, &ldisc);
f570e1ff
BJ
135 invalid = FALSE;
136 SCPYN(utmp.ut_name, "");
3b8dd95e
SL
137 /*
138 * Name specified, take it.
139 */
140 if (argc > 1) {
f570e1ff
BJ
141 SCPYN(utmp.ut_name, argv[1]);
142 argc = 0;
143 }
3b8dd95e
SL
144 /*
145 * If remote login take given name,
146 * otherwise prompt user for something.
147 */
4f8d3876 148 if (rflag) {
3479a16a 149 SCPYN(utmp.ut_name, lusername);
3b8dd95e 150 /* autologin failed, prompt for passwd */
4f8d3876
BJ
151 if (rflag == -1)
152 rflag = 0;
3b8dd95e
SL
153 } else {
154 getloginname(&utmp);
f570e1ff 155 }
f570e1ff
BJ
156 if (!strcmp(pwd->pw_shell, "/bin/csh")) {
157 ldisc = NTTYDISC;
158 ioctl(0, TIOCSETD, &ldisc);
159 }
3b8dd95e
SL
160 /*
161 * If no remote login authentication and
162 * a password exists for this user, prompt
163 * for one and verify it.
164 */
165 if (!rflag && *pwd->pw_passwd != '\0') {
166 char *pp;
167
168 setpriority(PRIO_PROCESS, 0, -4);
169 pp = getpass("Password:");
170 namep = crypt(pp, pwd->pw_passwd);
171 setpriority(PRIO_PROCESS, 0, 0);
172 if (strcmp(namep, pwd->pw_passwd))
173 invalid = TRUE;
f570e1ff 174 }
3b8dd95e
SL
175 /*
176 * If user not super-user, check for logins disabled.
177 */
f570e1ff 178 if (pwd->pw_uid != 0 && (nlfd = fopen(nolog, "r")) > 0) {
f570e1ff
BJ
179 while ((c = getc(nlfd)) != EOF)
180 putchar(c);
181 fflush(stdout);
182 sleep(5);
183 exit(0);
184 }
3b8dd95e
SL
185 /*
186 * If valid so far and root is logging in,
187 * see if root logins on this terminal are permitted.
188 */
f570e1ff
BJ
189 if (!invalid && pwd->pw_uid == 0 &&
190 !rootterm(ttyn+sizeof("/dev/")-1)) {
4f8d3876
BJ
191 logerr("ROOT LOGIN REFUSED %s",
192 ttyn+sizeof("/dev/")-1);
f570e1ff
BJ
193 invalid = TRUE;
194 }
195 if (invalid) {
88a01c09 196 printf("Login incorrect\n");
4f8d3876
BJ
197 if (ttyn[sizeof("/dev/tty")-1] == 'd')
198 logerr("BADDIALUP %s %s\n",
199 ttyn+sizeof("/dev/")-1, utmp.ut_name);
88a01c09 200 }
f570e1ff
BJ
201 if (*pwd->pw_shell == '\0')
202 pwd->pw_shell = "/bin/sh";
203 i = strlen(pwd->pw_shell);
204 if (chdir(pwd->pw_dir) < 0 && !invalid ) {
205 if (chdir("/") < 0) {
206 printf("No directory!\n");
207 invalid = TRUE;
208 } else {
3b8dd95e
SL
209 printf("No directory! %s\n",
210 "Logging in with home=/");
f570e1ff
BJ
211 pwd->pw_dir = "/";
212 }
88a01c09 213 }
3b8dd95e
SL
214 /*
215 * Remote login invalid must have been because
216 * of a restriction of some sort, no extra chances.
217 */
86eb6c9e
BJ
218 if (rflag && invalid)
219 exit(1);
f570e1ff 220 } while (invalid);
3b8dd95e
SL
221/* committed to login turn off timeout */
222 alarm(0);
88a01c09 223
22d4760e
SL
224 if (quota(Q_SETUID, pwd->pw_uid, 0, 0) < 0) {
225 if (errno == EUSERS)
226 printf("%s.\n%s.\n",
227 "Too many users logged on already",
228 "Try again later");
229 else if (errno == EPROCLIM)
230 printf("You have too many processes running.\n");
231 else
232 perror("setuid");
233 sleep(5);
234 exit(0);
235 }
88a01c09
BJ
236 time(&utmp.ut_time);
237 t = ttyslot();
3b8dd95e 238 if (t > 0 && (f = open("/etc/utmp", 1)) >= 0) {
88a01c09
BJ
239 lseek(f, (long)(t*sizeof(utmp)), 0);
240 SCPYN(utmp.ut_line, rindex(ttyn, '/')+1);
241 write(f, (char *)&utmp, sizeof(utmp));
242 close(f);
243 }
3b8dd95e 244 if (t > 0 && (f = open("/usr/adm/wtmp", 1)) >= 0) {
88a01c09
BJ
245 lseek(f, 0L, 2);
246 write(f, (char *)&utmp, sizeof(utmp));
247 close(f);
248 }
3b8dd95e 249 quietlog = access(qlog, 0) == 0;
4f8d3876 250 if ((f = open(lastlog, 2)) >= 0) {
f570e1ff
BJ
251 struct lastlog ll;
252
253 lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0);
254 if (read(f, (char *) &ll, sizeof ll) == sizeof ll &&
3b8dd95e
SL
255 ll.ll_time != 0 && !quietlog) {
256 printf("Last login: %.*s ",
257 24-5, (char *)ctime(&ll.ll_time));
258 if (*ll.ll_host != '\0')
259 printf("from %.*s\n",
260 sizeof (ll.ll_host), ll.ll_host);
261 else
262 printf("on %.*s\n",
263 sizeof (ll.ll_line), ll.ll_line);
f570e1ff
BJ
264 }
265 lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0);
266 time(&ll.ll_time);
267 SCPYN(ll.ll_line, rindex(ttyn, '/')+1);
3b8dd95e 268 SCPYN(ll.ll_host, utmp.ut_host);
f570e1ff
BJ
269 write(f, (char *) &ll, sizeof ll);
270 close(f);
271 }
88a01c09 272 chown(ttyn, pwd->pw_uid, pwd->pw_gid);
3479a16a 273 chmod(ttyn, 0622);
88a01c09 274 setgid(pwd->pw_gid);
e5321f7b
KM
275 strncpy(name, utmp.ut_name, NMAX);
276 name[NMAX] = '\0';
b1198826 277 initgroups(name, pwd->pw_gid);
22d4760e 278 quota(Q_DOWARN, pwd->pw_uid, (dev_t)-1, 0);
88a01c09 279 setuid(pwd->pw_uid);
88a01c09
BJ
280 environ = envinit;
281 strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
282 strncat(shell, pwd->pw_shell, sizeof(shell)-7);
4f8d3876 283 if (term[strlen("TERM=")] == 0)
86eb6c9e 284 strncat(term, stypeof(ttyn), sizeof(term)-6);
f570e1ff 285 strncat(user, pwd->pw_name, sizeof(user)-6);
88a01c09
BJ
286 if ((namep = rindex(pwd->pw_shell, '/')) == NULL)
287 namep = pwd->pw_shell;
288 else
289 namep++;
290 strcat(minusnam, namep);
b4389814 291 umask(022);
4f8d3876
BJ
292 if (ttyn[sizeof("/dev/tty")-1] == 'd')
293 logerr("DIALUP %s %s\n", ttyn+sizeof("/dev/")-1, pwd->pw_name);
294 if (!quietlog) {
f570e1ff
BJ
295 showmotd();
296 strcat(maildir, pwd->pw_name);
297 if (access(maildir,4)==0) {
298 struct stat statb;
299 stat(maildir, &statb);
300 if (statb.st_size)
301 printf("You have mail.\n");
302 }
303 }
3b8dd95e 304 signal(SIGALRM, SIG_DFL);
88a01c09
BJ
305 signal(SIGQUIT, SIG_DFL);
306 signal(SIGINT, SIG_DFL);
5f87416f 307 signal(SIGTSTP, SIG_IGN);
88a01c09 308 execlp(pwd->pw_shell, minusnam, 0);
f570e1ff 309 perror(pwd->pw_shell);
88a01c09
BJ
310 printf("No shell\n");
311 exit(0);
312}
313
3b8dd95e
SL
314getloginname(up)
315 register struct utmp *up;
316{
317 register char *namep;
5a786176 318 char c;
3b8dd95e 319
3b8dd95e
SL
320 while (up->ut_name[0] == '\0') {
321 namep = utmp.ut_name;
5a786176 322 printf("login: ");
3b8dd95e
SL
323 while ((c = getchar()) != '\n') {
324 if (c == ' ')
325 c = '_';
326 if (c == EOF)
327 exit(0);
328 if (namep < up->ut_name+NMAX)
329 *namep++ = c;
330 }
331 }
332 setpwent();
333 if ((pwd = getpwnam(utmp.ut_name)) == NULL)
334 pwd = &nouser;
335 endpwent();
336}
337
338timedout()
339{
340
341 printf("Login timed out after %d seconds\n", timeout);
342 exit(0);
343}
344
88a01c09
BJ
345int stopmotd;
346catch()
347{
1886582e 348
88a01c09
BJ
349 signal(SIGINT, SIG_IGN);
350 stopmotd++;
351}
352
f570e1ff 353rootterm(tty)
1886582e 354 char *tty;
f570e1ff
BJ
355{
356 register FILE *fd;
1886582e 357 char buf[100];
f570e1ff
BJ
358
359 if ((fd = fopen(securetty, "r")) == NULL)
360 return(1);
361 while (fgets(buf, sizeof buf, fd) != NULL) {
362 buf[strlen(buf)-1] = '\0';
363 if (strcmp(tty, buf) == 0) {
364 fclose(fd);
365 return(1);
366 }
367 }
368 fclose(fd);
369 return(0);
370}
371
88a01c09
BJ
372showmotd()
373{
374 FILE *mf;
375 register c;
376
377 signal(SIGINT, catch);
f570e1ff
BJ
378 if ((mf = fopen("/etc/motd","r")) != NULL) {
379 while ((c = getc(mf)) != EOF && stopmotd == 0)
88a01c09
BJ
380 putchar(c);
381 fclose(mf);
382 }
383 signal(SIGINT, SIG_IGN);
384}
385
f570e1ff 386#undef UNKNOWN
88a01c09
BJ
387#define UNKNOWN "su"
388
389char *
390stypeof(ttyid)
3b8dd95e 391 char *ttyid;
88a01c09 392{
3b8dd95e
SL
393 static char typebuf[16];
394 char buf[50];
395 register FILE *f;
396 register char *p, *t, *q;
88a01c09
BJ
397
398 if (ttyid == NULL)
399 return (UNKNOWN);
400 f = fopen("/etc/ttytype", "r");
401 if (f == NULL)
402 return (UNKNOWN);
403 /* split off end of name */
404 for (p = q = ttyid; *p != 0; p++)
405 if (*p == '/')
406 q = p + 1;
407
408 /* scan the file */
3b8dd95e
SL
409 while (fgets(buf, sizeof buf, f) != NULL) {
410 for (t = buf; *t != ' ' && *t != '\t'; t++)
88a01c09
BJ
411 ;
412 *t++ = 0;
f570e1ff
BJ
413 while (*t == ' ' || *t == '\t')
414 t++;
3b8dd95e 415 for (p = t; *p > ' '; p++)
88a01c09
BJ
416 ;
417 *p = 0;
3b8dd95e 418 if (strcmp(q,t) == 0) {
88a01c09
BJ
419 strcpy(typebuf, buf);
420 fclose(f);
421 return (typebuf);
422 }
423 }
424 fclose (f);
425 return (UNKNOWN);
426}
86eb6c9e 427
3b8dd95e
SL
428doremotelogin(host)
429 char *host;
430{
431 FILE *hostf;
432 int first = 1;
433
434 getstr(rusername, sizeof (rusername), "remuser");
435 getstr(lusername, sizeof (lusername), "locuser");
436 getstr(term+5, sizeof(term)-5, "Terminal type");
437 if (getuid())
438 goto bad;
439 setpwent();
440 pwd = getpwnam(lusername);
441 endpwent();
442 if (pwd == NULL)
443 goto bad;
444 hostf = pwd->pw_uid ? fopen("/etc/hosts.equiv", "r") : 0;
445again:
446 if (hostf) {
447 char ahost[32];
448
449 while (fgets(ahost, sizeof (ahost), hostf)) {
450 char *user;
451
452 if ((user = index(ahost, '\n')) != 0)
453 *user++ = '\0';
454 if ((user = index(ahost, ' ')) != 0)
455 *user++ = '\0';
456 if (!strcmp(host, ahost) &&
457 !strcmp(rusername, user ? user : lusername)) {
458 fclose(hostf);
459 return (1);
460 }
461 }
462 fclose(hostf);
463 }
464 if (first == 1) {
465 char *rhosts = ".rhosts";
466 struct stat sbuf;
467
468 first = 0;
469 if (chdir(pwd->pw_dir) < 0)
470 goto again;
471 if (lstat(rhosts, &sbuf) < 0)
472 goto again;
473 if ((sbuf.st_mode & S_IFMT) == S_IFLNK) {
474 printf("login: .rhosts is a soft link.\r\n");
475 goto bad;
476 }
477 hostf = fopen(rhosts, "r");
478 fstat(fileno(hostf), &sbuf);
479 if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) {
480 printf("login: Bad .rhosts ownership.\r\n");
481 fclose(hostf);
482 goto bad;
483 }
484 goto again;
485 }
486bad:
487 return (-1);
488}
489
86eb6c9e
BJ
490getstr(buf, cnt, err)
491 char *buf;
492 int cnt;
493 char *err;
494{
495 char c;
496
497 do {
498 if (read(0, &c, 1) != 1)
499 exit(1);
500 if (--cnt < 0) {
501 printf("%s too long\r\n", err);
502 exit(1);
503 }
504 *buf++ = c;
505 } while (c != 0);
506}
4f8d3876 507
3b8dd95e
SL
508char *speeds[] =
509 { "0", "50", "75", "110", "134", "150", "200", "300",
510 "600", "1200", "1800", "2400", "4800", "9600", "19200", "38400" };
511#define NSPEEDS (sizeof (speeds) / sizeof (speeds[0]))
512
513doremoteterm(term, tp)
514 char *term;
515 struct sgttyb *tp;
516{
517 char *cp = index(term, '/');
518 register int i;
519
520 if (cp) {
521 *cp++ = 0;
522 for (i = 0; i < NSPEEDS; i++)
523 if (!strcmp(speeds[i], cp)) {
524 tp->sg_ispeed = tp->sg_ospeed = i;
525 break;
526 }
527 }
528 tp->sg_flags = ECHO|CRMOD|ANYP|XTABS;
529}
530
4f8d3876
BJ
531logerr(fmt, a1, a2, a3)
532 char *fmt, *a1, *a2, *a3;
533{
da9c63f2
SL
534#ifdef LOGERR
535 FILE *cons = fopen("/dev/console", "w");
4f8d3876 536
da9c63f2
SL
537 if (cons != NULL) {
538 fprintf(cons, fmt, a1, a2, a3);
539 fputc('\r', cons);
540 fclose(cons);
541 }
542#endif
4f8d3876 543}