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