do case insensitive comparison on domain name
[unix-history] / usr / src / usr.bin / login / login.c
CommitLineData
bcf1365c 1/*
ede75793
KB
2 * Copyright (c) 1980, 1987, 1988 The Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms are permitted
6 * provided that the above copyright notice and this paragraph are
7 * duplicated in all such forms and that any documentation,
8 * advertising materials, and other materials related to such
9 * distribution and use acknowledge that the software was developed
10 * by the University of California, Berkeley. The name of the
11 * University may not be used to endorse or promote products derived
12 * from this software without specific prior written permission.
13 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14 * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15 * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
bcf1365c
DF
16 */
17
18#ifndef lint
19char copyright[] =
ede75793 20"@(#) Copyright (c) 1980, 1987, 1988 The Regents of the University of California.\n\
bcf1365c 21 All rights reserved.\n";
ede75793 22#endif /* not lint */
bcf1365c 23
22d4760e 24#ifndef lint
abc4056e 25static char sccsid[] = "@(#)login.c 5.28 (Berkeley) %G%";
ede75793 26#endif /* not lint */
22d4760e 27
88a01c09
BJ
28/*
29 * login [ name ]
5dbe2745
MK
30 * login -h hostname (for telnetd, etc.)
31 * login -f name (for pre-authenticated login: datakit, xterm, etc.)
88a01c09
BJ
32 */
33
7a625b73 34#include <sys/param.h>
3b8dd95e
SL
35#include <sys/quota.h>
36#include <sys/stat.h>
37#include <sys/time.h>
38#include <sys/resource.h>
9479aa87 39#include <sys/file.h>
80f91e3f 40#include <sys/ioctl.h>
3b8dd95e 41
88a01c09
BJ
42#include <utmp.h>
43#include <signal.h>
88a01c09 44#include <lastlog.h>
22d4760e 45#include <errno.h>
9479aa87
BJ
46#include <ttyent.h>
47#include <syslog.h>
c89291e2 48#include <grp.h>
80f91e3f 49#include <pwd.h>
ede75793 50#include <setjmp.h>
80f91e3f
KB
51#include <stdio.h>
52#include <strings.h>
c89291e2 53
80f91e3f 54#define TTYGRPNAME "tty" /* name of group to own ttys */
88a01c09 55
80f91e3f
KB
56#define MOTDFILE "/etc/motd"
57#define MAILDIR "/usr/spool/mail"
58#define NOLOGIN "/etc/nologin"
59#define HUSHLOGIN ".hushlogin"
60#define LASTLOG "/usr/adm/lastlog"
61#define BSHELL "/bin/sh"
f570e1ff 62
3b8dd95e 63/*
80f91e3f
KB
64 * This bounds the time given to login. Not a define so it can
65 * be patched on machines where it's too small.
3b8dd95e 66 */
a9b031fe 67int timeout = 300;
86eb6c9e 68
80f91e3f 69struct passwd *pwd;
f0c0c252
KB
70int repeatcnt;
71char term[64], *hostname, *username, *tty;
88a01c09 72
ede75793 73struct sgttyb sgttyb;
80f91e3f 74struct tchars tc = {
714accc5
SL
75 CINTR, CQUIT, CSTART, CSTOP, CEOT, CBRK
76};
80f91e3f 77struct ltchars ltc = {
714accc5 78 CSUSP, CDSUSP, CRPRNT, CFLUSH, CWERASE, CLNEXT
841d84b0
BJ
79};
80
88a01c09 81main(argc, argv)
80f91e3f
KB
82 int argc;
83 char **argv;
88a01c09 84{
80f91e3f
KB
85 extern int errno, optind;
86 extern char *optarg, **environ;
87 struct group *gr;
88 register int ch;
89 register char *p;
f0c0c252 90 int ask, fflag, hflag, pflag, cnt;
80f91e3f 91 int quietlog, passwd_req, ioctlval, timedout();
f0c0c252 92 char *domain, *salt, *envinit[1], *ttyn;
80f91e3f
KB
93 char tbuf[MAXPATHLEN + 2];
94 char *ttyname(), *stypeof(), *crypt(), *getpass();
95 time_t time();
96 off_t lseek();
97
98 (void)signal(SIGALRM, timedout);
99 (void)alarm((u_int)timeout);
100 (void)signal(SIGQUIT, SIG_IGN);
101 (void)signal(SIGINT, SIG_IGN);
102 (void)setpriority(PRIO_PROCESS, 0, 0);
103 (void)quota(Q_SETUID, 0, 0, 0);
104
3b8dd95e 105 /*
d3737d51 106 * -p is used by getty to tell login not to destroy the environment
5dbe2745 107 * -f is used to skip a second login authentication
ee1eff74
MK
108 * -h is used by other servers to pass the name of the remote
109 * host to login so that it may be placed in utmp and wtmp
3b8dd95e 110 */
80f91e3f
KB
111 (void)gethostname(tbuf, sizeof(tbuf));
112 domain = index(tbuf, '.');
113
ee1eff74 114 fflag = hflag = pflag = 0;
80f91e3f 115 passwd_req = 1;
ee1eff74 116 while ((ch = getopt(argc, argv, "fh:p")) != EOF)
ede75793 117 switch (ch) {
80f91e3f 118 case 'f':
80f91e3f 119 fflag = 1;
80f91e3f
KB
120 break;
121 case 'h':
122 if (getuid()) {
123 fprintf(stderr,
124 "login: -h for super-user only.\n");
b6f7cade 125 exit(1);
5dbe2745 126 }
80f91e3f
KB
127 hflag = 1;
128 if (domain && (p = index(optarg, '.')) &&
abc4056e 129 strcasecmp(p, domain) == 0)
80f91e3f
KB
130 *p = 0;
131 hostname = optarg;
132 break;
133 case 'p':
d3737d51 134 pflag = 1;
80f91e3f 135 break;
80f91e3f
KB
136 case '?':
137 default:
ede75793 138 fprintf(stderr, "usage: login [-fp] [username]\n");
80f91e3f 139 exit(1);
d3737d51 140 }
80f91e3f
KB
141 argc -= optind;
142 argv += optind;
f0c0c252
KB
143 if (*argv) {
144 ask = 0;
80f91e3f 145 username = *argv;
f0c0c252
KB
146 }
147 else
148 ask = 1;
80f91e3f
KB
149
150 ioctlval = 0;
151 (void)ioctl(0, TIOCLSET, &ioctlval);
152 (void)ioctl(0, TIOCNXCL, 0);
ede75793
KB
153 (void)fcntl(0, F_SETFL, ioctlval);
154 (void)ioctl(0, TIOCGETP, &sgttyb);
ede75793
KB
155 sgttyb.sg_erase = CERASE;
156 sgttyb.sg_kill = CKILL;
80f91e3f
KB
157 (void)ioctl(0, TIOCSLTC, &ltc);
158 (void)ioctl(0, TIOCSETC, &tc);
ede75793 159 (void)ioctl(0, TIOCSETP, &sgttyb);
80f91e3f
KB
160
161 for (cnt = getdtablesize(); cnt > 2; cnt--)
162 close(cnt);
163
88a01c09 164 ttyn = ttyname(0);
80f91e3f 165 if (ttyn == NULL || *ttyn == '\0')
88a01c09 166 ttyn = "/dev/tty??";
80f91e3f
KB
167 if (tty = rindex(ttyn, '/'))
168 ++tty;
9479aa87 169 else
80f91e3f
KB
170 tty = ttyn;
171
076ae92c 172 openlog("login", LOG_ODELAY, LOG_AUTH);
80f91e3f 173
f0c0c252 174 for (cnt = 0;; ask = 1) {
80f91e3f
KB
175 ioctlval = 0;
176 (void)ioctl(0, TIOCSETD, &ioctlval);
177
f0c0c252 178 if (ask) {
ede75793 179 fflag = 0;
80f91e3f 180 getloginname();
ede75793 181 }
f0c0c252
KB
182 /* note if trying multiple login's */
183 if (repeatcnt) {
184 if (strcmp(tbuf, username)) {
185 badlogin(tbuf);
186 repeatcnt = 1;
187 (void)strcpy(tbuf, username);
188 }
189 else
190 ++repeatcnt;
191 }
192 else {
193 repeatcnt = 1;
194 (void)strcpy(tbuf, username);
195 }
ede75793
KB
196 if (pwd = getpwnam(username))
197 salt = pwd->pw_passwd;
198 else
199 salt = "xx";
80f91e3f
KB
200
201 /* if user not super-user, check for disabled logins */
ede75793 202 if (pwd == NULL || pwd->pw_uid)
80f91e3f
KB
203 checknologin();
204
3b8dd95e 205 /*
ede75793
KB
206 * Disallow automatic login to root; if not invoked by
207 * root, disallow if the uid's differ.
3b8dd95e 208 */
ede75793 209 if (fflag && pwd) {
5dbe2745
MK
210 int uid = getuid();
211
ede75793
KB
212 passwd_req = pwd->pw_uid == 0 ||
213 (uid && uid != pwd->pw_uid);
f570e1ff 214 }
80f91e3f 215
3b8dd95e 216 /*
ee1eff74 217 * If no pre-authentication and a password exists
80f91e3f 218 * for this user, prompt for one and verify it.
3b8dd95e 219 */
ede75793 220 if (!passwd_req || pwd && !*pwd->pw_passwd)
80f91e3f
KB
221 break;
222
223 setpriority(PRIO_PROCESS, 0, -4);
ede75793 224 p = crypt(getpass("Password:"), salt);
80f91e3f 225 setpriority(PRIO_PROCESS, 0, 0);
ede75793 226 if (pwd && !strcmp(p, pwd->pw_passwd))
80f91e3f
KB
227 break;
228
229 printf("Login incorrect\n");
f0c0c252
KB
230 /* we allow 10 tries, but after 3 we start backing off */
231 if (++cnt > 3) {
232 if (cnt >= 10) {
233 badlogin(username);
234 (void)ioctl(0, TIOCHPCL, (struct sgttyb *)NULL);
235 sleepexit(1);
236 }
237 sleep((u_int)((cnt - 3) * 5));
f570e1ff 238 }
80f91e3f 239 }
88a01c09 240
80f91e3f
KB
241 /* committed to login -- turn off timeout */
242 (void)alarm((u_int)0);
243
f0c0c252
KB
244 /* log any mistakes -- don't count last one */
245 --repeatcnt;
246 badlogin(username);
247
80f91e3f
KB
248 /*
249 * If valid so far and root is logging in, see if root logins on
250 * this terminal are permitted.
251 */
f0c0c252 252 if (pwd->pw_uid == 0 && !rootterm()) {
80f91e3f 253 if (hostname)
f0c0c252
KB
254 syslog(LOG_ERR, "ROOT LOGIN REFUSED ON %s FROM %s",
255 tty, hostname);
22d4760e 256 else
ede75793 257 syslog(LOG_ERR, "ROOT LOGIN REFUSED ON %s", tty);
80f91e3f
KB
258 printf("Login incorrect\n");
259 sleepexit(1);
22d4760e 260 }
80f91e3f 261
80f91e3f
KB
262 if (quota(Q_SETUID, pwd->pw_uid, 0, 0) < 0 && errno != EINVAL) {
263 switch(errno) {
264 case EUSERS:
265 fprintf(stderr,
266 "Too many users logged on already.\nTry again later.\n");
267 break;
268 case EPROCLIM:
269 fprintf(stderr,
270 "You have too many processes running.\n");
271 break;
272 default:
273 perror("quota (Q_SETUID)");
f570e1ff 274 }
80f91e3f
KB
275 sleepexit(0);
276 }
277
ede75793
KB
278 if (chdir(pwd->pw_dir) < 0) {
279 printf("No directory %s!\n", pwd->pw_dir);
280 if (chdir("/"))
281 exit(0);
282 pwd->pw_dir = "/";
283 printf("Logging in with home = \"/\".\n");
284 }
285
286 /* nothing else left to fail -- really log in */
80f91e3f
KB
287 {
288 struct utmp utmp;
289
290 (void)time(&utmp.ut_time);
291 strncpy(utmp.ut_name, username, sizeof(utmp.ut_name));
292 strncpy(utmp.ut_host, hostname, sizeof(utmp.ut_host));
293 strncpy(utmp.ut_line, tty, sizeof(utmp.ut_line));
294 login(&utmp);
f570e1ff 295 }
80f91e3f
KB
296
297 quietlog = access(HUSHLOGIN, F_OK) == 0;
f0c0c252 298 dolastlog(quietlog);
80f91e3f 299
ee1eff74 300 if (!hflag) { /* XXX */
80f91e3f
KB
301 static struct winsize win = { 0, 0, 0, 0 };
302
303 (void)ioctl(0, TIOCSWINSZ, &win);
304 }
305
306 (void)chown(ttyn, pwd->pw_uid,
307 (gr = getgrnam(TTYGRPNAME)) ? gr->gr_gid : pwd->pw_gid);
308 (void)chmod(ttyn, 0620);
309 (void)setgid(pwd->pw_gid);
310
311 initgroups(username, pwd->pw_gid);
312
22d4760e 313 quota(Q_DOWARN, pwd->pw_uid, (dev_t)-1, 0);
80f91e3f 314 (void)setuid(pwd->pw_uid);
3daca631 315
ede75793
KB
316 if (*pwd->pw_shell == '\0')
317 pwd->pw_shell = BSHELL;
318 /* turn on new line discipline for the csh */
319 else if (!strcmp(pwd->pw_shell, "/bin/csh")) {
320 ioctlval = NTTYDISC;
321 (void)ioctl(0, TIOCSETD, &ioctlval);
322 }
323
80f91e3f 324 /* destroy environment unless user has requested preservation */
d3737d51
SL
325 if (!pflag)
326 environ = envinit;
ede75793
KB
327 (void)setenv("HOME", pwd->pw_dir, 1);
328 (void)setenv("SHELL", pwd->pw_shell, 1);
d3737d51 329 if (term[0] == '\0')
f0c0c252 330 strncpy(term, stypeof(), sizeof(term));
ede75793
KB
331 (void)setenv("TERM", term, 0);
332 (void)setenv("USER", pwd->pw_name, 1);
333 (void)setenv("PATH", "/usr/ucb:/bin:/usr/bin:", 0);
d3737d51 334
9479aa87 335 if (tty[sizeof("tty")-1] == 'd')
d3737d51
SL
336 syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
337 if (pwd->pw_uid == 0)
80f91e3f 338 if (hostname)
f0c0c252
KB
339 syslog(LOG_NOTICE, "ROOT LOGIN %s FROM %s",
340 tty, hostname);
df9d9536
KM
341 else
342 syslog(LOG_NOTICE, "ROOT LOGIN %s", tty);
80f91e3f 343
4f8d3876 344 if (!quietlog) {
6821e9c5 345 struct stat st;
d3737d51 346
80f91e3f
KB
347 motd();
348 (void)sprintf(tbuf, "%s/%s", MAILDIR, pwd->pw_name);
349 if (stat(tbuf, &st) == 0 && st.st_size != 0)
6821e9c5 350 printf("You have %smail.\n",
80f91e3f 351 (st.st_mtime > st.st_atime) ? "new " : "");
f570e1ff 352 }
80f91e3f
KB
353
354 (void)signal(SIGALRM, SIG_DFL);
355 (void)signal(SIGQUIT, SIG_DFL);
356 (void)signal(SIGINT, SIG_DFL);
357 (void)signal(SIGTSTP, SIG_IGN);
358
359 tbuf[0] = '-';
360 strcpy(tbuf + 1, (p = rindex(pwd->pw_shell, '/')) ?
361 p + 1 : pwd->pw_shell);
362 execlp(pwd->pw_shell, tbuf, 0);
ede75793 363 fprintf(stderr, "login: no shell: ");
f570e1ff 364 perror(pwd->pw_shell);
88a01c09
BJ
365 exit(0);
366}
367
80f91e3f 368getloginname()
3b8dd95e 369{
80f91e3f
KB
370 register int ch;
371 register char *p;
372 static char nbuf[UT_NAMESIZE + 1];
3b8dd95e 373
80f91e3f 374 for (;;) {
5a786176 375 printf("login: ");
ede75793 376 for (p = nbuf; (ch = getchar()) != '\n'; ) {
f0c0c252
KB
377 if (ch == EOF) {
378 badlogin(username);
3b8dd95e 379 exit(0);
f0c0c252 380 }
ede75793 381 if (p < nbuf + UT_NAMESIZE)
80f91e3f 382 *p++ = ch;
3b8dd95e 383 }
80f91e3f
KB
384 if (p > nbuf)
385 if (nbuf[0] == '-')
386 fprintf(stderr,
387 "login names may not start with '-'.\n");
388 else {
389 *p = '\0';
390 username = nbuf;
ede75793 391 break;
80f91e3f 392 }
3b8dd95e 393 }
3b8dd95e
SL
394}
395
396timedout()
397{
80f91e3f 398 fprintf(stderr, "Login timed out after %d seconds\n", timeout);
3b8dd95e
SL
399 exit(0);
400}
401
f0c0c252 402rootterm()
88a01c09 403{
80f91e3f 404 struct ttyent *t;
1886582e 405
80f91e3f 406 return((t = getttynam(tty)) && t->ty_status&TTY_SECURE);
88a01c09
BJ
407}
408
ede75793
KB
409jmp_buf motdinterrupt;
410
80f91e3f 411motd()
f570e1ff 412{
ede75793 413 register int fd, nchars;
80f91e3f 414 int (*oldint)(), sigint();
ede75793 415 char tbuf[8192];
80f91e3f 416
ede75793 417 if ((fd = open(MOTDFILE, O_RDONLY, 0)) < 0)
80f91e3f
KB
418 return;
419 oldint = signal(SIGINT, sigint);
ede75793
KB
420 if (setjmp(motdinterrupt) == 0)
421 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
422 (void)write(fileno(stdout), tbuf, nchars);
80f91e3f 423 (void)signal(SIGINT, oldint);
ede75793 424 (void)close(fd);
80f91e3f 425}
9479aa87 426
80f91e3f
KB
427sigint()
428{
ede75793 429 longjmp(motdinterrupt, 1);
80f91e3f
KB
430}
431
432checknologin()
433{
434 register int fd, nchars;
ede75793 435 char tbuf[8192];
80f91e3f
KB
436
437 if ((fd = open(NOLOGIN, O_RDONLY, 0)) >= 0) {
438 while ((nchars = read(fd, tbuf, sizeof(tbuf))) > 0)
439 (void)write(fileno(stdout), tbuf, nchars);
440 sleepexit(0);
f570e1ff 441 }
f570e1ff
BJ
442}
443
f0c0c252 444dolastlog(quiet)
80f91e3f 445 int quiet;
88a01c09 446{
80f91e3f
KB
447 struct lastlog ll;
448 int fd;
449
450 if ((fd = open(LASTLOG, O_RDWR, 0)) >= 0) {
ede75793 451 (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
80f91e3f
KB
452 if (!quiet) {
453 if (read(fd, (char *)&ll, sizeof(ll)) == sizeof(ll) &&
454 ll.ll_time != 0) {
455 printf("Last login: %.*s ",
456 24-5, (char *)ctime(&ll.ll_time));
457 if (*ll.ll_host != '\0')
458 printf("from %.*s\n",
459 sizeof(ll.ll_host), ll.ll_host);
460 else
461 printf("on %.*s\n",
462 sizeof(ll.ll_line), ll.ll_line);
463 }
ede75793 464 (void)lseek(fd, (off_t)pwd->pw_uid * sizeof(ll), L_SET);
80f91e3f
KB
465 }
466 (void)time(&ll.ll_time);
467 strncpy(ll.ll_line, tty, sizeof(ll.ll_line));
468 strncpy(ll.ll_host, hostname, sizeof(ll.ll_host));
469 (void)write(fd, (char *)&ll, sizeof(ll));
470 (void)close(fd);
88a01c09 471 }
88a01c09
BJ
472}
473
f0c0c252
KB
474badlogin(name)
475 char *name;
476{
477 if (!repeatcnt)
478 return;
479 if (hostname)
480 syslog(LOG_ERR, "%d LOGIN FAILURE%s ON %s FROM %s, %s",
481 repeatcnt, repeatcnt > 1 ? "S" : "", tty, hostname, name);
482 else
483 syslog(LOG_ERR, "%d LOGIN FAILURE%s ON %s, %s",
484 repeatcnt, repeatcnt > 1 ? "S" : "", tty, name);
485}
486
f570e1ff 487#undef UNKNOWN
80f91e3f 488#define UNKNOWN "su"
88a01c09
BJ
489
490char *
f0c0c252 491stypeof()
88a01c09 492{
80f91e3f 493 struct ttyent *t;
88a01c09 494
f0c0c252 495 return(tty && (t = getttynam(tty)) ? t->ty_type : UNKNOWN);
88a01c09 496}
86eb6c9e
BJ
497
498getstr(buf, cnt, err)
80f91e3f 499 char *buf, *err;
86eb6c9e 500 int cnt;
86eb6c9e 501{
80f91e3f 502 char ch;
86eb6c9e
BJ
503
504 do {
80f91e3f 505 if (read(0, &ch, sizeof(ch)) != sizeof(ch))
86eb6c9e
BJ
506 exit(1);
507 if (--cnt < 0) {
80f91e3f
KB
508 fprintf(stderr, "%s too long\r\n", err);
509 sleepexit(1);
86eb6c9e 510 }
80f91e3f
KB
511 *buf++ = ch;
512 } while (ch);
86eb6c9e 513}
4f8d3876 514
80f91e3f
KB
515sleepexit(eval)
516 int eval;
c89291e2 517{
80f91e3f
KB
518 sleep((u_int)5);
519 exit(eval);
c89291e2 520}