2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
11 * Copyright (c) 1980 Regents of the University of California.
12 * All rights reserved. The Berkeley software License Agreement
13 * specifies the terms and conditions for redistribution.
18 * login -r hostname (for rlogind)
19 * login -h hostname (for telnetd, etc.)
21 #include <afsconfig.h>
22 #include <afs/param.h>
26 #if !defined(AFS_SUN_ENV) && !defined(AFS_AIX_ENV) && !defined(AFS_HPUX_ENV) && !defined(AFS_SGI_ENV) && !defined(AFS_SUN5_ENV) && !defined(AFS_LINUX20_ENV) && !defined(AFS_DARWIN_ENV) &&!defined(AFS_FBSD_ENV)
27 #include <sys/param.h>
29 #define quota(a,b,c,d) 0
33 #include <sys/resource.h>
35 #include <afs/afsutil.h>
50 static gid_t tty_gid(int default_gid);
51 static void getloginname(register struct utmp *up);
53 #define TTYGRPNAME "tty" /* name of group to own ttys */
54 #define TTYGID(gid) tty_gid(gid) /* gid that owns all ttys */
56 #define SCMPN(a, b) strncmp(a, b, sizeof(a))
57 #define SCPYN(a, b) strncpy(a, b, sizeof(a))
59 #define NMAX sizeof(utmp.ut_name)
60 #define HMAX sizeof(utmp.ut_host)
65 char nolog[] = "/etc/nologin";
66 char qlog[] = ".hushlogin";
67 char maildir[30] = "/usr/spool/mail/";
68 char lastlog[] = "/usr/adm/lastlog";
72 char minusnam[16] = "-";
73 char *envinit[] = { 0 }; /* now set by setenv calls */
75 * This bounds the time given to login. We initialize it here
76 * so it can be patched on machines where it's too small.
83 char *strcat(), *malloc(), *realloc();
84 static void timedout(void);
85 static void showmotd(void);
86 static void doremoteterm(char *term, struct sgttyb *tp);
87 static void setenv(char *var, char *value, int clobber);
92 extern char **environ;
96 CINTR, CQUIT, CSTART, CSTOP, CEOT, CBRK
98 struct ltchars ltc = {
99 CSUSP, CDSUSP, CRPRNT, CFLUSH, CWERASE, CLNEXT
102 struct winsize win = { 0, 0, 0, 0 };
106 char rusername[NMAX+1], lusername[NMAX+1];
107 char rpassword[NMAX+1];
113 /* this sucks but it works for now.
119 #include "AFS_component_version_number.c"
125 register char *namep;
126 int pflag = 0, hflag = 0, t, f, c;
127 int invalid, quietlog;
130 int ldisc = 0, zero = 0, i;
135 * The following signal action for AIX is necessary so that in case of a
136 * crash (i.e. core is generated) we can include the user's data section
137 * in the core dump. Unfortunately, by default, only a partial core is
138 * generated which, in many cases, isn't too useful.
140 struct sigaction nsa;
142 sigemptyset(&nsa.sa_mask);
143 nsa.sa_handler = SIG_DFL;
144 nsa.sa_flags = SA_FULLDUMP;
145 sigaction(SIGSEGV, &nsa, NULL);
147 signal(SIGALRM, timedout);
149 signal(SIGQUIT, SIG_IGN);
150 signal(SIGINT, SIG_IGN);
151 setpriority(PRIO_PROCESS, 0, 0);
152 quota(Q_SETUID, 0, 0, 0);
154 /* create a dummy user */
155 memset(&nouser, 0, sizeof(nouser));
157 nouser.pw_passwd = "*";
158 nouser.pw_dir = nouser.pw_shell = "";
159 nouser.pw_uid = nouser.pw_gid = -1;
162 * -p is used by getty to tell login not to destroy the environment
163 * -r is used by rlogind to cause the autologin protocol;
164 * -h is used by other servers to pass the name of the
165 * remote host to login so that it may be placed in utmp and wtmp
168 if (strcmp(argv[1], "-r") == 0) {
169 if (rflag || hflag) {
170 printf("Only one of -r and -h allowed\n");
174 usererr = doremotelogin(argv[2]);
175 SCPYN(utmp.ut_host, argv[2]);
180 if (strcmp(argv[1], "-h") == 0 && getuid() == 0) {
181 if (rflag || hflag) {
182 printf("Only one of -r and -h allowed\n");
186 SCPYN(utmp.ut_host, argv[2]);
191 if (strcmp(argv[1], "-p") == 0) {
199 ioctl(0, TIOCLSET, &zero);
200 ioctl(0, TIOCNXCL, 0);
201 ioctl(0, FIONBIO, &zero);
202 ioctl(0, FIOASYNC, &zero);
203 ioctl(0, TIOCGETP, &ttyb);
205 * If talking to an rlogin process,
206 * propagate the terminal type and
207 * baud rate across the network.
210 doremoteterm(term, &ttyb);
211 ttyb.sg_erase = CERASE;
212 ttyb.sg_kill = CKILL;
213 ioctl(0, TIOCSLTC, <c);
214 ioctl(0, TIOCSETC, &tc);
215 ioctl(0, TIOCSETP, &ttyb);
216 for (t = getdtablesize(); t > 2; t--)
219 if (ttyn == (char *)0 || *ttyn == '\0')
221 tty = strrchr(ttyn, '/');
226 openlog("login", LOG_ODELAY, LOG_AUTH);
231 ioctl(0, TIOCSETD, &ldisc);
232 SCPYN(utmp.ut_name, "");
234 * Name specified, take it.
237 SCPYN(utmp.ut_name, argv[1]);
241 * If remote login take given name,
242 * otherwise prompt user for something.
244 if (rflag && !invalid)
245 SCPYN(utmp.ut_name, lusername);
249 if (!strcmp(pwd->pw_shell, "/bin/csh")) {
251 ioctl(0, TIOCSETD, &ldisc);
254 * If no remote login authentication and
255 * a password exists for this user, prompt
256 * for one and verify it.
258 if (usererr == -1 && *pwd->pw_passwd != '\0') {
260 char password[BUFSIZ];
262 if (ka_UserReadPassword
263 ("Password:", password, sizeof(password), &reason)) {
264 printf("Unable to login because %s,\n", reason);
267 else if (ka_UserAuthenticate
268 (pwd->pw_name, /*inst*/0, /*realm*/0, password,
269 /*sepag*/1, &reason)) {
270 printf("Unable to authenticate to AFS because %s.\n",
272 printf(" proceeding with local authentication... \n");
273 /* try local login */
274 namep = crypt(password, pwd->pw_passwd);
275 if (strcmp(namep, pwd->pw_passwd)) invalid = TRUE;
279 setpriority(PRIO_PROCESS, 0, -4);
280 pp = getpass("Password:");
281 namep = crypt(pp, pwd->pw_passwd);
282 setpriority(PRIO_PROCESS, 0, 0);
283 if (strcmp(namep, pwd->pw_passwd)) invalid = TRUE;
287 * If user not super-user, check for logins disabled.
289 if (pwd->pw_uid != 0 && (nlfd = fopen(nolog, "r")) != 0) {
290 while ((c = getc(nlfd)) != EOF)
297 * If valid so far and root is logging in,
298 * see if root logins on this terminal are permitted.
300 if (!invalid && pwd->pw_uid == 0 && !rootterm(tty)) {
303 "ROOT LOGIN REFUSED ON %s FROM %.*s",
304 tty, HMAX, utmp.ut_host);
307 "ROOT LOGIN REFUSED ON %s", tty);
311 printf("Login incorrect\n");
315 "REPEATED LOGIN FAILURES ON %s FROM %.*s, %.*s",
316 tty, HMAX, utmp.ut_host,
320 "REPEATED LOGIN FAILURES ON %s, %.*s",
321 tty, NMAX, utmp.ut_name);
322 ioctl(0, TIOCHPCL, (struct sgttyb *) 0);
323 close(0), close(1), close(2);
328 if (*pwd->pw_shell == '\0')
329 pwd->pw_shell = "/bin/sh";
330 if (chdir(pwd->pw_dir) < 0 && !invalid ) {
331 if (chdir("/") < 0) {
332 printf("No directory!\n");
335 printf("No directory! %s\n",
336 "Logging in with home=/");
341 * Remote login invalid must have been because
342 * of a restriction of some sort, no extra chances.
344 if (!usererr && invalid)
347 /* committed to login turn off timeout */
350 if (quota(Q_SETUID, pwd->pw_uid, 0, 0) < 0 && errno != EINVAL) {
353 "Too many users logged on already",
355 else if (errno == EPROCLIM)
356 printf("You have too many processes running.\n");
358 perror("quota (Q_SETUID)");
364 if (t > 0 && (f = open("/etc/utmp", O_WRONLY)) >= 0) {
365 lseek(f, (afs_int32)(t*sizeof(utmp)), 0);
366 SCPYN(utmp.ut_line, tty);
367 write(f, (char *)&utmp, sizeof(utmp));
370 if ((f = open("/usr/adm/wtmp", O_WRONLY|O_APPEND)) >= 0) {
371 write(f, (char *)&utmp, sizeof(utmp));
374 quietlog = access(qlog, F_OK) == 0;
375 if ((f = open(lastlog, O_RDWR)) >= 0) {
378 lseek(f, (afs_int32)pwd->pw_uid * sizeof (struct lastlog), 0);
379 if (read(f, (char *) &ll, sizeof ll) == sizeof ll &&
380 ll.ll_time != 0 && !quietlog) {
381 printf("Last login: %.*s ",
382 24-5, (char *)ctime(&ll.ll_time));
383 if (*ll.ll_host != '\0')
384 printf("from %.*s\n",
385 sizeof (ll.ll_host), ll.ll_host);
388 sizeof (ll.ll_line), ll.ll_line);
390 lseek(f, (afs_int32)pwd->pw_uid * sizeof (struct lastlog), 0);
392 SCPYN(ll.ll_line, tty);
393 SCPYN(ll.ll_host, utmp.ut_host);
394 write(f, (char *) &ll, sizeof ll);
397 chown(ttyn, pwd->pw_uid, TTYGID(pwd->pw_gid));
398 if (!hflag && !rflag) /* XXX */
399 ioctl(0, TIOCSWINSZ, &win);
402 strncpy(name, utmp.ut_name, NMAX);
404 initgroups(name, pwd->pw_gid);
405 quota(Q_DOWARN, pwd->pw_uid, (dev_t)-1, 0);
407 /* destroy environment unless user has asked to preserve it */
411 /* set up environment, this time without destruction */
412 /* copy the environment before setenving */
414 while (environ[i] != NULL)
416 envnew = (char **) malloc(sizeof (char *) * (i + 1));
418 envnew[i] = environ[i];
421 setenv("HOME=", pwd->pw_dir, 1);
422 setenv("SHELL=", pwd->pw_shell, 1);
424 strncpy(term, stypeof(tty), sizeof(term));
425 setenv("TERM=", term, 0);
426 setenv("USER=", pwd->pw_name, 1);
427 setenv("PATH=", ":/usr/ucb:/bin:/usr/bin", 0);
429 if ((namep = strrchr(pwd->pw_shell, '/')) == NULL)
430 namep = pwd->pw_shell;
433 strcat(minusnam, namep);
434 if (tty[sizeof("tty")-1] == 'd')
435 syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
436 if (pwd->pw_uid == 0)
438 syslog(LOG_NOTICE, "ROOT LOGIN %s FROM %.*s",
439 tty, HMAX, utmp.ut_host);
441 syslog(LOG_NOTICE, "ROOT LOGIN %s", tty);
446 strcat(maildir, pwd->pw_name);
447 if (stat(maildir, &st) == 0 && st.st_size != 0)
448 printf("You have %smail.\n",
449 (st.st_mtime > st.st_atime) ? "new " : "");
451 signal(SIGALRM, SIG_DFL);
452 signal(SIGQUIT, SIG_DFL);
453 signal(SIGINT, SIG_DFL);
454 signal(SIGTSTP, SIG_IGN);
455 execlp(pwd->pw_shell, minusnam, 0);
456 perror(pwd->pw_shell);
457 printf("No shell\n");
461 static void getloginname(register struct utmp *up)
463 register char *namep;
466 while (up->ut_name[0] == '\0') {
469 while ((c = getchar()) != '\n') {
474 if (namep < up->ut_name+NMAX)
478 strncpy(lusername, up->ut_name, NMAX);
480 if ((pwd = getpwnam(lusername)) == NULL)
484 static void timedout(void)
487 printf("Login timed out after %d seconds\n", timeout);
492 static void catch(void)
495 signal(SIGINT, SIG_IGN);
499 int rootterm(char *tty)
501 register struct ttyent *t;
503 if ((t = getttynam(tty)) != NULL) {
504 if (t->ty_status & TTY_SECURE)
510 static void showmotd(void)
515 signal(SIGINT, catch);
516 if ((mf = fopen("/etc/motd", "r")) != NULL) {
517 while ((c = getc(mf)) != EOF && stopmotd == 0)
521 signal(SIGINT, SIG_IGN);
527 char *stypeof(char *ttyid)
529 register struct ttyent *t;
531 if (ttyid == NULL || (t = getttynam(ttyid)) == NULL)
536 int doremotelogin(char *host)
538 getstr(rusername, sizeof (rusername), "remuser");
539 getstr(lusername, sizeof (lusername), "locuser");
540 getstr(term, sizeof(term), "Terminal type");
545 pwd = getpwnam(lusername);
550 return(ruserok(host, (pwd->pw_uid == 0), rusername, lusername));
561 if (read(0, &c, 1) != 1)
564 printf("%s too long\r\n", err);
572 { "0", "50", "75", "110", "134", "150", "200", "300",
573 "600", "1200", "1800", "2400", "4800", "9600", "19200", "38400" };
574 #define NSPEEDS (sizeof (speeds) / sizeof (speeds[0]))
576 static void doremoteterm(
580 register char *cp = strchr(term, '/'), **cpp;
586 cp = strchr(speed, '/');
589 for (cpp = speeds; cpp < &speeds[NSPEEDS]; cpp++)
590 if (strcmp(*cpp, speed) == 0) {
591 tp->sg_ispeed = tp->sg_ospeed = cpp-speeds;
595 tp->sg_flags = ECHO|CRMOD|ANYP|XTABS;
599 * Set the value of var to be arg in the Unix 4.2 BSD environment env.
600 * Var should end with '='.
601 * (bindings are of the form "var=value")
602 * This procedure assumes the memory for the first level of environ
603 * was allocated using malloc.
610 extern char **environ;
612 int varlen = strlen(var);
613 int vallen = strlen(value);
615 for (index = 0; environ[index] != NULL; index++) {
616 if (strncmp(environ[index], var, varlen) == 0) {
620 environ[index] = malloc(varlen + vallen + 1);
621 strcpy(environ[index], var);
622 strcat(environ[index], value);
626 environ = (char **) realloc(environ, sizeof (char *) * (index + 2));
627 if (environ == NULL) {
628 fprintf(stderr, "login: malloc out of memory\n");
631 environ[index] = malloc(varlen + vallen + 1);
632 strcpy(environ[index], var);
633 strcat(environ[index], value);
634 environ[++index] = NULL;
637 static gid_t tty_gid(int default_gid)
639 struct group *getgrnam(), *gr;
640 gid_t gid = default_gid;
642 gr = getgrnam(TTYGRPNAME);
643 if (gr != (struct group *) 0)
652 #include "AFS_component_version_number.c"
655 #endif /*!defined(AFS_SUN_ENV) && !defined(AFS_AIX_ENV) && !defined(AFS_HPUX_ENV)*/