date and time created 83/10/01 12:21:05 by sam
[unix-history] / usr / src / usr.bin / login / login.c.1
CommitLineData
22d4760e 1#ifndef lint
2e355d9e 2static char *sccsid = "@(#)login.c.1 4.33 (Berkeley) 83/09/02";
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;
4cf9fc9e 153 } else
3b8dd95e 154 getloginname(&utmp);
f570e1ff
BJ
155 if (!strcmp(pwd->pw_shell, "/bin/csh")) {
156 ldisc = NTTYDISC;
157 ioctl(0, TIOCSETD, &ldisc);
158 }
3b8dd95e
SL
159 /*
160 * If no remote login authentication and
161 * a password exists for this user, prompt
162 * for one and verify it.
163 */
164 if (!rflag && *pwd->pw_passwd != '\0') {
165 char *pp;
166
167 setpriority(PRIO_PROCESS, 0, -4);
168 pp = getpass("Password:");
169 namep = crypt(pp, pwd->pw_passwd);
170 setpriority(PRIO_PROCESS, 0, 0);
171 if (strcmp(namep, pwd->pw_passwd))
172 invalid = TRUE;
f570e1ff 173 }
3b8dd95e
SL
174 /*
175 * If user not super-user, check for logins disabled.
176 */
f570e1ff 177 if (pwd->pw_uid != 0 && (nlfd = fopen(nolog, "r")) > 0) {
f570e1ff
BJ
178 while ((c = getc(nlfd)) != EOF)
179 putchar(c);
180 fflush(stdout);
181 sleep(5);
182 exit(0);
183 }
3b8dd95e
SL
184 /*
185 * If valid so far and root is logging in,
186 * see if root logins on this terminal are permitted.
187 */
f570e1ff
BJ
188 if (!invalid && pwd->pw_uid == 0 &&
189 !rootterm(ttyn+sizeof("/dev/")-1)) {
4f8d3876
BJ
190 logerr("ROOT LOGIN REFUSED %s",
191 ttyn+sizeof("/dev/")-1);
f570e1ff
BJ
192 invalid = TRUE;
193 }
194 if (invalid) {
88a01c09 195 printf("Login incorrect\n");
4f8d3876 196 if (ttyn[sizeof("/dev/tty")-1] == 'd')
ea87b78e 197 logerr("BADDIALUP %s %s",
4f8d3876 198 ttyn+sizeof("/dev/")-1, utmp.ut_name);
88a01c09 199 }
f570e1ff
BJ
200 if (*pwd->pw_shell == '\0')
201 pwd->pw_shell = "/bin/sh";
202 i = strlen(pwd->pw_shell);
203 if (chdir(pwd->pw_dir) < 0 && !invalid ) {
204 if (chdir("/") < 0) {
205 printf("No directory!\n");
206 invalid = TRUE;
207 } else {
3b8dd95e
SL
208 printf("No directory! %s\n",
209 "Logging in with home=/");
f570e1ff
BJ
210 pwd->pw_dir = "/";
211 }
88a01c09 212 }
3b8dd95e
SL
213 /*
214 * Remote login invalid must have been because
215 * of a restriction of some sort, no extra chances.
216 */
86eb6c9e
BJ
217 if (rflag && invalid)
218 exit(1);
f570e1ff 219 } while (invalid);
3b8dd95e
SL
220/* committed to login turn off timeout */
221 alarm(0);
88a01c09 222
22d4760e
SL
223 if (quota(Q_SETUID, pwd->pw_uid, 0, 0) < 0) {
224 if (errno == EUSERS)
225 printf("%s.\n%s.\n",
226 "Too many users logged on already",
227 "Try again later");
228 else if (errno == EPROCLIM)
229 printf("You have too many processes running.\n");
230 else
231 perror("setuid");
232 sleep(5);
233 exit(0);
234 }
88a01c09
BJ
235 time(&utmp.ut_time);
236 t = ttyslot();
3b8dd95e 237 if (t > 0 && (f = open("/etc/utmp", 1)) >= 0) {
88a01c09
BJ
238 lseek(f, (long)(t*sizeof(utmp)), 0);
239 SCPYN(utmp.ut_line, rindex(ttyn, '/')+1);
240 write(f, (char *)&utmp, sizeof(utmp));
241 close(f);
242 }
3b8dd95e 243 if (t > 0 && (f = open("/usr/adm/wtmp", 1)) >= 0) {
88a01c09
BJ
244 lseek(f, 0L, 2);
245 write(f, (char *)&utmp, sizeof(utmp));
246 close(f);
247 }
3b8dd95e 248 quietlog = access(qlog, 0) == 0;
4f8d3876 249 if ((f = open(lastlog, 2)) >= 0) {
f570e1ff
BJ
250 struct lastlog ll;
251
252 lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0);
253 if (read(f, (char *) &ll, sizeof ll) == sizeof ll &&
3b8dd95e
SL
254 ll.ll_time != 0 && !quietlog) {
255 printf("Last login: %.*s ",
256 24-5, (char *)ctime(&ll.ll_time));
257 if (*ll.ll_host != '\0')
258 printf("from %.*s\n",
259 sizeof (ll.ll_host), ll.ll_host);
260 else
261 printf("on %.*s\n",
262 sizeof (ll.ll_line), ll.ll_line);
f570e1ff
BJ
263 }
264 lseek(f, (long)pwd->pw_uid * sizeof (struct lastlog), 0);
265 time(&ll.ll_time);
266 SCPYN(ll.ll_line, rindex(ttyn, '/')+1);
3b8dd95e 267 SCPYN(ll.ll_host, utmp.ut_host);
f570e1ff
BJ
268 write(f, (char *) &ll, sizeof ll);
269 close(f);
270 }
88a01c09 271 chown(ttyn, pwd->pw_uid, pwd->pw_gid);
3479a16a 272 chmod(ttyn, 0622);
88a01c09 273 setgid(pwd->pw_gid);
e5321f7b
KM
274 strncpy(name, utmp.ut_name, NMAX);
275 name[NMAX] = '\0';
b1198826 276 initgroups(name, pwd->pw_gid);
22d4760e 277 quota(Q_DOWARN, pwd->pw_uid, (dev_t)-1, 0);
88a01c09 278 setuid(pwd->pw_uid);
88a01c09
BJ
279 environ = envinit;
280 strncat(homedir, pwd->pw_dir, sizeof(homedir)-6);
281 strncat(shell, pwd->pw_shell, sizeof(shell)-7);
4f8d3876 282 if (term[strlen("TERM=")] == 0)
86eb6c9e 283 strncat(term, stypeof(ttyn), sizeof(term)-6);
f570e1ff 284 strncat(user, pwd->pw_name, sizeof(user)-6);
88a01c09
BJ
285 if ((namep = rindex(pwd->pw_shell, '/')) == NULL)
286 namep = pwd->pw_shell;
287 else
288 namep++;
289 strcat(minusnam, namep);
b4389814 290 umask(022);
4f8d3876 291 if (ttyn[sizeof("/dev/tty")-1] == 'd')
ea87b78e
SL
292 logerr("DIALUP %s %s",
293 ttyn+sizeof("/dev/")-1, pwd->pw_name);
4f8d3876 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 320 while (up->ut_name[0] == '\0') {
d910ab7f 321 namep = up->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 }
d910ab7f
EW
332 strncpy(lusername, up->ut_name, NMAX);
333 lusername[NMAX] = 0;
3b8dd95e 334 setpwent();
d910ab7f 335 if ((pwd = getpwnam(lusername)) == NULL)
3b8dd95e
SL
336 pwd = &nouser;
337 endpwent();
338}
339
340timedout()
341{
342
343 printf("Login timed out after %d seconds\n", timeout);
344 exit(0);
345}
346
88a01c09
BJ
347int stopmotd;
348catch()
349{
1886582e 350
88a01c09
BJ
351 signal(SIGINT, SIG_IGN);
352 stopmotd++;
353}
354
f570e1ff 355rootterm(tty)
1886582e 356 char *tty;
f570e1ff
BJ
357{
358 register FILE *fd;
1886582e 359 char buf[100];
f570e1ff
BJ
360
361 if ((fd = fopen(securetty, "r")) == NULL)
362 return(1);
363 while (fgets(buf, sizeof buf, fd) != NULL) {
364 buf[strlen(buf)-1] = '\0';
365 if (strcmp(tty, buf) == 0) {
366 fclose(fd);
367 return(1);
368 }
369 }
370 fclose(fd);
371 return(0);
372}
373
88a01c09
BJ
374showmotd()
375{
376 FILE *mf;
377 register c;
378
379 signal(SIGINT, catch);
f570e1ff
BJ
380 if ((mf = fopen("/etc/motd","r")) != NULL) {
381 while ((c = getc(mf)) != EOF && stopmotd == 0)
88a01c09
BJ
382 putchar(c);
383 fclose(mf);
384 }
385 signal(SIGINT, SIG_IGN);
386}
387
f570e1ff 388#undef UNKNOWN
88a01c09
BJ
389#define UNKNOWN "su"
390
391char *
392stypeof(ttyid)
3b8dd95e 393 char *ttyid;
88a01c09 394{
3b8dd95e
SL
395 static char typebuf[16];
396 char buf[50];
397 register FILE *f;
398 register char *p, *t, *q;
88a01c09
BJ
399
400 if (ttyid == NULL)
401 return (UNKNOWN);
402 f = fopen("/etc/ttytype", "r");
403 if (f == NULL)
404 return (UNKNOWN);
405 /* split off end of name */
406 for (p = q = ttyid; *p != 0; p++)
407 if (*p == '/')
408 q = p + 1;
409
410 /* scan the file */
3b8dd95e
SL
411 while (fgets(buf, sizeof buf, f) != NULL) {
412 for (t = buf; *t != ' ' && *t != '\t'; t++)
88a01c09
BJ
413 ;
414 *t++ = 0;
f570e1ff
BJ
415 while (*t == ' ' || *t == '\t')
416 t++;
3b8dd95e 417 for (p = t; *p > ' '; p++)
88a01c09
BJ
418 ;
419 *p = 0;
3b8dd95e 420 if (strcmp(q,t) == 0) {
88a01c09
BJ
421 strcpy(typebuf, buf);
422 fclose(f);
423 return (typebuf);
424 }
425 }
426 fclose (f);
427 return (UNKNOWN);
428}
86eb6c9e 429
3b8dd95e
SL
430doremotelogin(host)
431 char *host;
432{
433 FILE *hostf;
434 int first = 1;
435
436 getstr(rusername, sizeof (rusername), "remuser");
437 getstr(lusername, sizeof (lusername), "locuser");
438 getstr(term+5, sizeof(term)-5, "Terminal type");
4cf9fc9e
SL
439 if (getuid()) {
440 pwd = &nouser;
3b8dd95e 441 goto bad;
4cf9fc9e 442 }
3b8dd95e
SL
443 setpwent();
444 pwd = getpwnam(lusername);
445 endpwent();
4cf9fc9e
SL
446 if (pwd == NULL) {
447 pwd = &nouser;
3b8dd95e 448 goto bad;
4cf9fc9e 449 }
3b8dd95e
SL
450 hostf = pwd->pw_uid ? fopen("/etc/hosts.equiv", "r") : 0;
451again:
452 if (hostf) {
453 char ahost[32];
454
455 while (fgets(ahost, sizeof (ahost), hostf)) {
456 char *user;
457
458 if ((user = index(ahost, '\n')) != 0)
459 *user++ = '\0';
460 if ((user = index(ahost, ' ')) != 0)
461 *user++ = '\0';
462 if (!strcmp(host, ahost) &&
463 !strcmp(rusername, user ? user : lusername)) {
464 fclose(hostf);
465 return (1);
466 }
467 }
468 fclose(hostf);
469 }
470 if (first == 1) {
471 char *rhosts = ".rhosts";
472 struct stat sbuf;
473
474 first = 0;
475 if (chdir(pwd->pw_dir) < 0)
476 goto again;
477 if (lstat(rhosts, &sbuf) < 0)
478 goto again;
479 if ((sbuf.st_mode & S_IFMT) == S_IFLNK) {
480 printf("login: .rhosts is a soft link.\r\n");
481 goto bad;
482 }
483 hostf = fopen(rhosts, "r");
484 fstat(fileno(hostf), &sbuf);
485 if (sbuf.st_uid && sbuf.st_uid != pwd->pw_uid) {
486 printf("login: Bad .rhosts ownership.\r\n");
487 fclose(hostf);
488 goto bad;
489 }
490 goto again;
491 }
492bad:
493 return (-1);
494}
495
86eb6c9e
BJ
496getstr(buf, cnt, err)
497 char *buf;
498 int cnt;
499 char *err;
500{
501 char c;
502
503 do {
504 if (read(0, &c, 1) != 1)
505 exit(1);
506 if (--cnt < 0) {
507 printf("%s too long\r\n", err);
508 exit(1);
509 }
510 *buf++ = c;
511 } while (c != 0);
512}
4f8d3876 513
3b8dd95e
SL
514char *speeds[] =
515 { "0", "50", "75", "110", "134", "150", "200", "300",
516 "600", "1200", "1800", "2400", "4800", "9600", "19200", "38400" };
517#define NSPEEDS (sizeof (speeds) / sizeof (speeds[0]))
518
519doremoteterm(term, tp)
520 char *term;
521 struct sgttyb *tp;
522{
523 char *cp = index(term, '/');
524 register int i;
525
526 if (cp) {
527 *cp++ = 0;
528 for (i = 0; i < NSPEEDS; i++)
529 if (!strcmp(speeds[i], cp)) {
530 tp->sg_ispeed = tp->sg_ospeed = i;
531 break;
532 }
533 }
534 tp->sg_flags = ECHO|CRMOD|ANYP|XTABS;
535}
536
4f8d3876
BJ
537logerr(fmt, a1, a2, a3)
538 char *fmt, *a1, *a2, *a3;
539{
da9c63f2
SL
540#ifdef LOGERR
541 FILE *cons = fopen("/dev/console", "w");
4f8d3876 542
da9c63f2
SL
543 if (cons != NULL) {
544 fprintf(cons, fmt, a1, a2, a3);
ea87b78e 545 fprintf(cons, "\n\r");
da9c63f2
SL
546 fclose(cons);
547 }
548#endif
4f8d3876 549}