no more subclasses of manual pages
[unix-history] / usr / src / usr.bin / login / login.c
CommitLineData
bcf1365c 1/*
5dbe2745 2 * Copyright (c) 1980,1987 Regents of the University of California.
bcf1365c
DF
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8char copyright[] =
9"@(#) Copyright (c) 1980 Regents of the University of California.\n\
10 All rights reserved.\n";
11#endif not lint
12
22d4760e 13#ifndef lint
133ce0bc 14static char sccsid[] = "@(#)login.c 5.23 (Berkeley) %G%";
bcf1365c 15#endif not lint
22d4760e 16
88a01c09
BJ
17/*
18 * login [ name ]
5dbe2745
MK
19 * login -r hostname (for rlogind)
20 * login -h hostname (for telnetd, etc.)
21 * login -f name (for pre-authenticated login: datakit, xterm, etc.)
88a01c09
BJ
22 */
23
7a625b73 24#include <sys/param.h>
3b8dd95e
SL
25#include <sys/quota.h>
26#include <sys/stat.h>
27#include <sys/time.h>
28#include <sys/resource.h>
9479aa87 29#include <sys/file.h>
3b8dd95e 30
88a01c09
BJ
31#include <sgtty.h>
32#include <utmp.h>
33#include <signal.h>
34#include <pwd.h>
35#include <stdio.h>
88a01c09 36#include <lastlog.h>
22d4760e 37#include <errno.h>
9479aa87
BJ
38#include <ttyent.h>
39#include <syslog.h>
c89291e2
KM
40#include <grp.h>
41
9382c11c
MK
42#define TTYGRPNAME "tty" /* name of group to own ttys */
43#define TTYGID(gid) tty_gid(gid) /* gid that owns all ttys */
f570e1ff 44
9479aa87 45#define SCMPN(a, b) strncmp(a, b, sizeof(a))
f570e1ff 46#define SCPYN(a, b) strncpy(a, b, sizeof(a))
88a01c09 47
b4389814 48#define NMAX sizeof(utmp.ut_name)
df9d9536 49#define HMAX sizeof(utmp.ut_host)
88a01c09 50
f570e1ff
BJ
51#define FALSE 0
52#define TRUE -1
53
54char nolog[] = "/etc/nologin";
55char qlog[] = ".hushlogin";
88a01c09
BJ
56char maildir[30] = "/usr/spool/mail/";
57char lastlog[] = "/usr/adm/lastlog";
3479a16a 58struct passwd nouser = {"", "nope", -1, -1, -1, "", "", "", "" };
88a01c09
BJ
59struct sgttyb ttyb;
60struct utmp utmp;
61char minusnam[16] = "-";
3daca631 62char *envinit[1]; /* now set by setenv calls */
3b8dd95e
SL
63/*
64 * This bounds the time given to login. We initialize it here
65 * so it can be patched on machines where it's too small.
66 */
a9b031fe 67int timeout = 300;
86eb6c9e 68
d3737d51 69char term[64];
88a01c09 70
86eb6c9e 71struct passwd *pwd;
3daca631 72char *strcat(), *rindex(), *index();
3b8dd95e 73int timedout();
88a01c09
BJ
74char *ttyname();
75char *crypt();
76char *getpass();
88a01c09 77char *stypeof();
22d4760e 78extern int errno;
88a01c09 79
714accc5
SL
80struct tchars tc = {
81 CINTR, CQUIT, CSTART, CSTOP, CEOT, CBRK
82};
83struct ltchars ltc = {
84 CSUSP, CDSUSP, CRPRNT, CFLUSH, CWERASE, CLNEXT
841d84b0
BJ
85};
86
d3737d51
SL
87struct winsize win = { 0, 0, 0, 0 };
88
86eb6c9e 89int rflag;
95a2c263 90int usererr = -1;
b4389814 91char rusername[NMAX+1], lusername[NMAX+1];
86eb6c9e 92char rpassword[NMAX+1];
e5321f7b 93char name[NMAX+1];
53b3a5b2 94char me[MAXHOSTNAMELEN];
b4389814 95char *rhost;
86eb6c9e 96
88a01c09 97main(argc, argv)
3b8dd95e 98 char *argv[];
88a01c09 99{
3daca631 100 extern char **environ;
88a01c09 101 register char *namep;
5dbe2745 102 int pflag = 0, hflag = 0, fflag = 0, t, f, c;
3b8dd95e 103 int invalid, quietlog;
f570e1ff 104 FILE *nlfd;
9479aa87 105 char *ttyn, *tty;
9ad71fdb 106 int ldisc = 0, zero = 0;
53b3a5b2 107 char *p, *domain, *index();
88a01c09 108
3b8dd95e
SL
109 signal(SIGALRM, timedout);
110 alarm(timeout);
88a01c09
BJ
111 signal(SIGQUIT, SIG_IGN);
112 signal(SIGINT, SIG_IGN);
3b8dd95e 113 setpriority(PRIO_PROCESS, 0, 0);
22d4760e 114 quota(Q_SETUID, 0, 0, 0);
3b8dd95e 115 /*
d3737d51 116 * -p is used by getty to tell login not to destroy the environment
3b8dd95e 117 * -r is used by rlogind to cause the autologin protocol;
5dbe2745 118 * -f is used to skip a second login authentication
3b8dd95e
SL
119 * -h is used by other servers to pass the name of the
120 * remote host to login so that it may be placed in utmp and wtmp
121 */
53b3a5b2
MK
122 (void) gethostname(me, sizeof(me));
123 domain = index(me, '.');
95a2c263 124 while (argc > 1) {
3b8dd95e 125 if (strcmp(argv[1], "-r") == 0) {
5dbe2745
MK
126 if (rflag || hflag || fflag) {
127 printf("Other options not allowed with -r\n");
95a2c263
JB
128 exit(1);
129 }
b6f7cade
KB
130 if (argv[2] == 0)
131 exit(1);
95a2c263
JB
132 rflag = 1;
133 usererr = doremotelogin(argv[2]);
53b3a5b2
MK
134 if ((p = index(argv[2], '.')) && strcmp(p, domain) == 0)
135 *p = 0;
3b8dd95e 136 SCPYN(utmp.ut_host, argv[2]);
95a2c263
JB
137 argc -= 2;
138 argv += 2;
139 continue;
b4389814 140 }
5dbe2745
MK
141 if (strcmp(argv[1], "-h") == 0) {
142 if (getuid() == 0) {
143 if (rflag || hflag) {
144 printf("Only one of -r and -h allowed\n");
145 exit(1);
146 }
147 hflag = 1;
148 if ((p = index(argv[2], '.')) &&
149 strcmp(p, domain) == 0)
150 *p = 0;
151 SCPYN(utmp.ut_host, argv[2]);
152 }
153 argc -= 2;
154 argv += 2;
155 continue;
156 }
157 if (strcmp(argv[1], "-f") == 0 && argc > 2) {
158 if (rflag) {
159 printf("Only one of -r and -f allowed\n");
95a2c263
JB
160 exit(1);
161 }
5dbe2745
MK
162 fflag = 1;
163 SCPYN(utmp.ut_name, argv[2]);
95a2c263
JB
164 argc -= 2;
165 argv += 2;
166 continue;
b4389814 167 }
d3737d51
SL
168 if (strcmp(argv[1], "-p") == 0) {
169 argc--;
170 argv++;
171 pflag = 1;
95a2c263 172 continue;
d3737d51 173 }
95a2c263 174 break;
86eb6c9e 175 }
714accc5 176 ioctl(0, TIOCLSET, &zero);
c95ed2b2 177 ioctl(0, TIOCNXCL, 0);
4f8d3876
BJ
178 ioctl(0, FIONBIO, &zero);
179 ioctl(0, FIOASYNC, &zero);
714accc5 180 ioctl(0, TIOCGETP, &ttyb);
3b8dd95e
SL
181 /*
182 * If talking to an rlogin process,
183 * propagate the terminal type and
184 * baud rate across the network.
185 */
186 if (rflag)
187 doremoteterm(term, &ttyb);
568353d4
MK
188 ttyb.sg_erase = CERASE;
189 ttyb.sg_kill = CKILL;
714accc5
SL
190 ioctl(0, TIOCSLTC, &ltc);
191 ioctl(0, TIOCSETC, &tc);
192 ioctl(0, TIOCSETP, &ttyb);
c32fb087 193 for (t = getdtablesize(); t > 2; t--)
88a01c09
BJ
194 close(t);
195 ttyn = ttyname(0);
6fc1474b 196 if (ttyn == (char *)0 || *ttyn == '\0')
88a01c09 197 ttyn = "/dev/tty??";
9479aa87
BJ
198 tty = rindex(ttyn, '/');
199 if (tty == NULL)
200 tty = ttyn;
201 else
202 tty++;
076ae92c 203 openlog("login", LOG_ODELAY, LOG_AUTH);
9479aa87 204 t = 0;
96aec705 205 invalid = FALSE;
f570e1ff
BJ
206 do {
207 ldisc = 0;
c95ed2b2 208 ioctl(0, TIOCSETD, &ldisc);
5dbe2745
MK
209 if (fflag == 0)
210 SCPYN(utmp.ut_name, "");
3b8dd95e
SL
211 /*
212 * Name specified, take it.
213 */
214 if (argc > 1) {
f570e1ff
BJ
215 SCPYN(utmp.ut_name, argv[1]);
216 argc = 0;
217 }
3b8dd95e
SL
218 /*
219 * If remote login take given name,
220 * otherwise prompt user for something.
221 */
96aec705 222 if (rflag && !invalid)
3479a16a 223 SCPYN(utmp.ut_name, lusername);
b6f7cade 224 else {
3b8dd95e 225 getloginname(&utmp);
b6f7cade
KB
226 if (utmp.ut_name[0] == '-') {
227 puts("login names may not start with '-'.");
228 invalid = TRUE;
229 continue;
230 }
231 }
96aec705 232 invalid = FALSE;
f570e1ff
BJ
233 if (!strcmp(pwd->pw_shell, "/bin/csh")) {
234 ldisc = NTTYDISC;
235 ioctl(0, TIOCSETD, &ldisc);
236 }
5dbe2745
MK
237 if (fflag) {
238 int uid = getuid();
239
240 if (uid != 0 && uid != pwd->pw_uid)
241 fflag = 0;
242 /*
243 * Disallow automatic login for root.
244 */
245 if (pwd->pw_uid == 0)
246 fflag = 0;
247 }
3b8dd95e
SL
248 /*
249 * If no remote login authentication and
250 * a password exists for this user, prompt
251 * for one and verify it.
252 */
5dbe2745 253 if (usererr == -1 && fflag == 0 && *pwd->pw_passwd != '\0') {
3b8dd95e
SL
254 char *pp;
255
256 setpriority(PRIO_PROCESS, 0, -4);
257 pp = getpass("Password:");
258 namep = crypt(pp, pwd->pw_passwd);
259 setpriority(PRIO_PROCESS, 0, 0);
260 if (strcmp(namep, pwd->pw_passwd))
261 invalid = TRUE;
f570e1ff 262 }
3b8dd95e
SL
263 /*
264 * If user not super-user, check for logins disabled.
265 */
133ce0bc 266 if (pwd->pw_uid != 0 && (nlfd = fopen(nolog, "r"))) {
f570e1ff
BJ
267 while ((c = getc(nlfd)) != EOF)
268 putchar(c);
269 fflush(stdout);
270 sleep(5);
271 exit(0);
272 }
3b8dd95e
SL
273 /*
274 * If valid so far and root is logging in,
275 * see if root logins on this terminal are permitted.
276 */
9479aa87 277 if (!invalid && pwd->pw_uid == 0 && !rootterm(tty)) {
df9d9536
KM
278 if (utmp.ut_host[0])
279 syslog(LOG_CRIT,
280 "ROOT LOGIN REFUSED ON %s FROM %.*s",
281 tty, HMAX, utmp.ut_host);
282 else
283 syslog(LOG_CRIT,
284 "ROOT LOGIN REFUSED ON %s", tty);
f570e1ff
BJ
285 invalid = TRUE;
286 }
287 if (invalid) {
88a01c09 288 printf("Login incorrect\n");
9479aa87 289 if (++t >= 5) {
df9d9536 290 if (utmp.ut_host[0])
5dbe2745
MK
291 syslog(LOG_ERR,
292 "REPEATED LOGIN FAILURES ON %s FROM %.*s, %.*s",
df9d9536
KM
293 tty, HMAX, utmp.ut_host,
294 NMAX, utmp.ut_name);
295 else
5dbe2745
MK
296 syslog(LOG_ERR,
297 "REPEATED LOGIN FAILURES ON %s, %.*s",
df9d9536 298 tty, NMAX, utmp.ut_name);
9479aa87 299 ioctl(0, TIOCHPCL, (struct sgttyb *) 0);
d3737d51 300 close(0), close(1), close(2);
9479aa87
BJ
301 sleep(10);
302 exit(1);
303 }
88a01c09 304 }
f570e1ff
BJ
305 if (*pwd->pw_shell == '\0')
306 pwd->pw_shell = "/bin/sh";
f570e1ff
BJ
307 if (chdir(pwd->pw_dir) < 0 && !invalid ) {
308 if (chdir("/") < 0) {
309 printf("No directory!\n");
310 invalid = TRUE;
311 } else {
3b8dd95e
SL
312 printf("No directory! %s\n",
313 "Logging in with home=/");
f570e1ff
BJ
314 pwd->pw_dir = "/";
315 }
88a01c09 316 }
3b8dd95e
SL
317 /*
318 * Remote login invalid must have been because
319 * of a restriction of some sort, no extra chances.
320 */
95a2c263 321 if (!usererr && invalid)
86eb6c9e 322 exit(1);
f570e1ff 323 } while (invalid);
3b8dd95e
SL
324/* committed to login turn off timeout */
325 alarm(0);
88a01c09 326
790c9c8b 327 if (quota(Q_SETUID, pwd->pw_uid, 0, 0) < 0 && errno != EINVAL) {
22d4760e
SL
328 if (errno == EUSERS)
329 printf("%s.\n%s.\n",
330 "Too many users logged on already",
331 "Try again later");
332 else if (errno == EPROCLIM)
333 printf("You have too many processes running.\n");
334 else
6821e9c5 335 perror("quota (Q_SETUID)");
22d4760e
SL
336 sleep(5);
337 exit(0);
338 }
88a01c09 339 time(&utmp.ut_time);
41060cea
KB
340 SCPYN(utmp.ut_line, tty);
341 login(&utmp);
9479aa87
BJ
342 quietlog = access(qlog, F_OK) == 0;
343 if ((f = open(lastlog, O_RDWR)) >= 0) {
f570e1ff
BJ
344 struct lastlog ll;
345
346 lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0);
347 if (read(f, (char *) &ll, sizeof ll) == sizeof ll &&
3b8dd95e
SL
348 ll.ll_time != 0 && !quietlog) {
349 printf("Last login: %.*s ",
350 24-5, (char *)ctime(&ll.ll_time));
351 if (*ll.ll_host != '\0')
352 printf("from %.*s\n",
353 sizeof (ll.ll_host), ll.ll_host);
354 else
355 printf("on %.*s\n",
356 sizeof (ll.ll_line), ll.ll_line);
f570e1ff
BJ
357 }
358 lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0);
359 time(&ll.ll_time);
9479aa87 360 SCPYN(ll.ll_line, tty);
3b8dd95e 361 SCPYN(ll.ll_host, utmp.ut_host);
f570e1ff
BJ
362 write(f, (char *) &ll, sizeof ll);
363 close(f);
364 }
9382c11c 365 chown(ttyn, pwd->pw_uid, TTYGID(pwd->pw_gid));
c32fb087 366 if (!hflag && !rflag) /* XXX */
d3737d51 367 ioctl(0, TIOCSWINSZ, &win);
c89291e2 368 chmod(ttyn, 0620);
88a01c09 369 setgid(pwd->pw_gid);
e5321f7b
KM
370 strncpy(name, utmp.ut_name, NMAX);
371 name[NMAX] = '\0';
b1198826 372 initgroups(name, pwd->pw_gid);
22d4760e 373 quota(Q_DOWARN, pwd->pw_uid, (dev_t)-1, 0);
88a01c09 374 setuid(pwd->pw_uid);
3daca631 375
d3737d51
SL
376 /* destroy environment unless user has asked to preserve it */
377 if (!pflag)
378 environ = envinit;
3daca631
KB
379 setenv("HOME", pwd->pw_dir, 1);
380 setenv("SHELL", pwd->pw_shell, 1);
d3737d51
SL
381 if (term[0] == '\0')
382 strncpy(term, stypeof(tty), sizeof(term));
3daca631
KB
383 setenv("TERM", term, 0);
384 setenv("USER", pwd->pw_name, 1);
385 setenv("PATH", ":/usr/ucb:/bin:/usr/bin", 0);
d3737d51 386
88a01c09
BJ
387 if ((namep = rindex(pwd->pw_shell, '/')) == NULL)
388 namep = pwd->pw_shell;
389 else
390 namep++;
391 strcat(minusnam, namep);
9479aa87 392 if (tty[sizeof("tty")-1] == 'd')
d3737d51
SL
393 syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
394 if (pwd->pw_uid == 0)
df9d9536
KM
395 if (utmp.ut_host[0])
396 syslog(LOG_NOTICE, "ROOT LOGIN %s FROM %.*s",
397 tty, HMAX, utmp.ut_host);
398 else
399 syslog(LOG_NOTICE, "ROOT LOGIN %s", tty);
4f8d3876 400 if (!quietlog) {
6821e9c5 401 struct stat st;
d3737d51 402
f570e1ff
BJ
403 showmotd();
404 strcat(maildir, pwd->pw_name);
6821e9c5
S
405 if (stat(maildir, &st) == 0 && st.st_size != 0)
406 printf("You have %smail.\n",
d3737d51 407 (st.st_mtime > st.st_atime) ? "new " : "");
f570e1ff 408 }
3b8dd95e 409 signal(SIGALRM, SIG_DFL);
88a01c09
BJ
410 signal(SIGQUIT, SIG_DFL);
411 signal(SIGINT, SIG_DFL);
5f87416f 412 signal(SIGTSTP, SIG_IGN);
88a01c09 413 execlp(pwd->pw_shell, minusnam, 0);
f570e1ff 414 perror(pwd->pw_shell);
88a01c09
BJ
415 printf("No shell\n");
416 exit(0);
417}
418
3b8dd95e
SL
419getloginname(up)
420 register struct utmp *up;
421{
422 register char *namep;
5a786176 423 char c;
3b8dd95e 424
3b8dd95e 425 while (up->ut_name[0] == '\0') {
d910ab7f 426 namep = up->ut_name;
5a786176 427 printf("login: ");
3b8dd95e
SL
428 while ((c = getchar()) != '\n') {
429 if (c == ' ')
430 c = '_';
431 if (c == EOF)
432 exit(0);
433 if (namep < up->ut_name+NMAX)
434 *namep++ = c;
435 }
436 }
d910ab7f
EW
437 strncpy(lusername, up->ut_name, NMAX);
438 lusername[NMAX] = 0;
d910ab7f 439 if ((pwd = getpwnam(lusername)) == NULL)
3b8dd95e 440 pwd = &nouser;
3b8dd95e
SL
441}
442
443timedout()
444{
445
446 printf("Login timed out after %d seconds\n", timeout);
447 exit(0);
448}
449
88a01c09
BJ
450int stopmotd;
451catch()
452{
1886582e 453
88a01c09
BJ
454 signal(SIGINT, SIG_IGN);
455 stopmotd++;
456}
457
f570e1ff 458rootterm(tty)
1886582e 459 char *tty;
f570e1ff 460{
9479aa87
BJ
461 register struct ttyent *t;
462
463 if ((t = getttynam(tty)) != NULL) {
464 if (t->ty_status & TTY_SECURE)
465 return (1);
f570e1ff 466 }
9479aa87 467 return (0);
f570e1ff
BJ
468}
469
88a01c09
BJ
470showmotd()
471{
472 FILE *mf;
473 register c;
474
475 signal(SIGINT, catch);
9479aa87 476 if ((mf = fopen("/etc/motd", "r")) != NULL) {
f570e1ff 477 while ((c = getc(mf)) != EOF && stopmotd == 0)
88a01c09
BJ
478 putchar(c);
479 fclose(mf);
480 }
481 signal(SIGINT, SIG_IGN);
482}
483
f570e1ff 484#undef UNKNOWN
88a01c09
BJ
485#define UNKNOWN "su"
486
487char *
488stypeof(ttyid)
3b8dd95e 489 char *ttyid;
88a01c09 490{
9479aa87 491 register struct ttyent *t;
88a01c09 492
9479aa87 493 if (ttyid == NULL || (t = getttynam(ttyid)) == NULL)
88a01c09 494 return (UNKNOWN);
9479aa87 495 return (t->ty_type);
88a01c09 496}
86eb6c9e 497
3b8dd95e
SL
498doremotelogin(host)
499 char *host;
500{
3b8dd95e
SL
501 getstr(rusername, sizeof (rusername), "remuser");
502 getstr(lusername, sizeof (lusername), "locuser");
d3737d51 503 getstr(term, sizeof(term), "Terminal type");
4cf9fc9e
SL
504 if (getuid()) {
505 pwd = &nouser;
95a2c263 506 return(-1);
4cf9fc9e 507 }
3b8dd95e 508 pwd = getpwnam(lusername);
4cf9fc9e
SL
509 if (pwd == NULL) {
510 pwd = &nouser;
95a2c263 511 return(-1);
3b8dd95e 512 }
95a2c263 513 return(ruserok(host, (pwd->pw_uid == 0), rusername, lusername));
3b8dd95e
SL
514}
515
86eb6c9e
BJ
516getstr(buf, cnt, err)
517 char *buf;
518 int cnt;
519 char *err;
520{
521 char c;
522
523 do {
524 if (read(0, &c, 1) != 1)
525 exit(1);
526 if (--cnt < 0) {
527 printf("%s too long\r\n", err);
528 exit(1);
529 }
530 *buf++ = c;
531 } while (c != 0);
532}
4f8d3876 533
3b8dd95e
SL
534char *speeds[] =
535 { "0", "50", "75", "110", "134", "150", "200", "300",
536 "600", "1200", "1800", "2400", "4800", "9600", "19200", "38400" };
537#define NSPEEDS (sizeof (speeds) / sizeof (speeds[0]))
538
539doremoteterm(term, tp)
540 char *term;
541 struct sgttyb *tp;
542{
d3737d51
SL
543 register char *cp = index(term, '/'), **cpp;
544 char *speed;
3b8dd95e
SL
545
546 if (cp) {
d3737d51
SL
547 *cp++ = '\0';
548 speed = cp;
549 cp = index(speed, '/');
550 if (cp)
551 *cp++ = '\0';
552 for (cpp = speeds; cpp < &speeds[NSPEEDS]; cpp++)
553 if (strcmp(*cpp, speed) == 0) {
554 tp->sg_ispeed = tp->sg_ospeed = cpp-speeds;
3b8dd95e
SL
555 break;
556 }
557 }
558 tp->sg_flags = ECHO|CRMOD|ANYP|XTABS;
559}
d3737d51 560
9382c11c 561tty_gid(default_gid)
5576108a 562 int default_gid;
c89291e2
KM
563{
564 struct group *getgrnam(), *gr;
5576108a 565 int gid = default_gid;
c89291e2 566
9382c11c 567 gr = getgrnam(TTYGRPNAME);
c89291e2
KM
568 if (gr != (struct group *) 0)
569 gid = gr->gr_gid;
570
571 endgrent();
572
5576108a 573 return (gid);
c89291e2 574}