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_XBSD_ENV)
28 #define quota(a,b,c,d) 0
30 #include <afs/afsutil.h>
36 static gid_t tty_gid(int default_gid);
37 static void getloginname(struct utmp *up);
39 #define TTYGRPNAME "tty" /* name of group to own ttys */
40 #define TTYGID(gid) tty_gid(gid) /* gid that owns all ttys */
42 #define SCMPN(a, b) strncmp(a, b, sizeof(a))
43 #define SCPYN(a, b) strncpy(a, b, sizeof(a))
45 #define NMAX sizeof(utmp.ut_name)
46 #define HMAX sizeof(utmp.ut_host)
51 char nolog[] = "/etc/nologin";
52 char qlog[] = ".hushlogin";
53 char maildir[30] = "/usr/spool/mail/";
54 char lastlog[] = "/usr/adm/lastlog";
58 char minusnam[16] = "-";
59 char *envinit[] = { 0 }; /* now set by setenv calls */
62 * This bounds the time given to login. We initialize it here
63 * so it can be patched on machines where it's too small.
70 char *strcat(), *malloc(), *realloc();
71 static void timedout(void);
72 static void showmotd(void);
73 static void doremoteterm(char *term, struct sgttyb *tp);
74 static void setenv(char *var, char *value, int clobber);
79 extern char **environ;
83 CINTR, CQUIT, CSTART, CSTOP, CEOT, CBRK
85 struct ltchars ltc = {
86 CSUSP, CDSUSP, CRPRNT, CFLUSH, CWERASE, CLNEXT
89 struct winsize win = { 0, 0, 0, 0 };
93 char rusername[NMAX + 1], lusername[NMAX + 1];
94 char rpassword[NMAX + 1];
101 /* this sucks but it works for now.
107 #include "AFS_component_version_number.c"
110 main(int argc, char **argv)
113 int pflag = 0, hflag = 0, t, f, c;
114 int invalid, quietlog;
117 int ldisc = 0, zero = 0, i;
122 * The following signal action for AIX is necessary so that in case of a
123 * crash (i.e. core is generated) we can include the user's data section
124 * in the core dump. Unfortunately, by default, only a partial core is
125 * generated which, in many cases, isn't too useful.
127 struct sigaction nsa;
129 sigemptyset(&nsa.sa_mask);
130 nsa.sa_handler = SIG_DFL;
131 nsa.sa_flags = SA_FULLDUMP;
132 sigaction(SIGSEGV, &nsa, NULL);
134 signal(SIGALRM, timedout);
136 signal(SIGQUIT, SIG_IGN);
137 signal(SIGINT, SIG_IGN);
138 setpriority(PRIO_PROCESS, 0, 0);
139 quota(Q_SETUID, 0, 0, 0);
141 /* create a dummy user */
142 memset(&nouser, 0, sizeof(nouser));
144 nouser.pw_passwd = "*";
145 nouser.pw_dir = nouser.pw_shell = "";
146 nouser.pw_uid = nouser.pw_gid = -1;
149 * -p is used by getty to tell login not to destroy the environment
150 * -r is used by rlogind to cause the autologin protocol;
151 * -h is used by other servers to pass the name of the
152 * remote host to login so that it may be placed in utmp and wtmp
155 if (strcmp(argv[1], "-r") == 0) {
156 if (rflag || hflag) {
157 printf("Only one of -r and -h allowed\n");
161 usererr = doremotelogin(argv[2]);
162 SCPYN(utmp.ut_host, argv[2]);
167 if (strcmp(argv[1], "-h") == 0 && getuid() == 0) {
168 if (rflag || hflag) {
169 printf("Only one of -r and -h allowed\n");
173 SCPYN(utmp.ut_host, argv[2]);
178 if (strcmp(argv[1], "-p") == 0) {
186 ioctl(0, TIOCLSET, &zero);
187 ioctl(0, TIOCNXCL, 0);
188 ioctl(0, FIONBIO, &zero);
189 ioctl(0, FIOASYNC, &zero);
190 ioctl(0, TIOCGETP, &ttyb);
192 * If talking to an rlogin process,
193 * propagate the terminal type and
194 * baud rate across the network.
197 doremoteterm(term, &ttyb);
198 ttyb.sg_erase = CERASE;
199 ttyb.sg_kill = CKILL;
200 ioctl(0, TIOCSLTC, <c);
201 ioctl(0, TIOCSETC, &tc);
202 ioctl(0, TIOCSETP, &ttyb);
203 for (t = getdtablesize(); t > 2; t--)
206 if (ttyn == (char *)0 || *ttyn == '\0')
208 tty = strrchr(ttyn, '/');
213 openlog("login", LOG_ODELAY, LOG_AUTH);
218 ioctl(0, TIOCSETD, &ldisc);
219 SCPYN(utmp.ut_name, "");
221 * Name specified, take it.
224 SCPYN(utmp.ut_name, argv[1]);
228 * If remote login take given name,
229 * otherwise prompt user for something.
231 if (rflag && !invalid)
232 SCPYN(utmp.ut_name, lusername);
236 if (!strcmp(pwd->pw_shell, "/bin/csh")) {
238 ioctl(0, TIOCSETD, &ldisc);
241 * If no remote login authentication and
242 * a password exists for this user, prompt
243 * for one and verify it.
245 if (usererr == -1 && *pwd->pw_passwd != '\0') {
247 char password[BUFSIZ];
249 if (ka_UserReadPassword
250 ("Password:", password, sizeof(password), &reason)) {
251 printf("Unable to login because %s,\n", reason);
254 if (ka_UserAuthenticate
255 (pwd->pw_name, /*inst */ 0, /*realm */ 0, password,
256 /*sepag */ 1, &reason)) {
257 printf("Unable to authenticate to AFS because %s.\n", reason);
258 printf(" proceeding with local authentication... \n");
259 /* try local login */
260 namep = crypt(password, pwd->pw_passwd);
261 if (strcmp(namep, pwd->pw_passwd))
266 setpriority(PRIO_PROCESS, 0, -4);
267 pp = getpass("Password:");
268 namep = crypt(pp, pwd->pw_passwd);
269 setpriority(PRIO_PROCESS, 0, 0);
270 if (strcmp(namep, pwd->pw_passwd))
275 * If user not super-user, check for logins disabled.
277 if (pwd->pw_uid != 0 && (nlfd = fopen(nolog, "r")) != 0) {
278 while ((c = getc(nlfd)) != EOF)
285 * If valid so far and root is logging in,
286 * see if root logins on this terminal are permitted.
288 if (!invalid && pwd->pw_uid == 0 && !rootterm(tty)) {
290 syslog(LOG_CRIT, "ROOT LOGIN REFUSED ON %s FROM %.*s", tty,
293 syslog(LOG_CRIT, "ROOT LOGIN REFUSED ON %s", tty);
297 printf("Login incorrect\n");
301 "REPEATED LOGIN FAILURES ON %s FROM %.*s, %.*s",
302 tty, HMAX, utmp.ut_host, NMAX, utmp.ut_name);
304 syslog(LOG_CRIT, "REPEATED LOGIN FAILURES ON %s, %.*s",
305 tty, NMAX, utmp.ut_name);
306 ioctl(0, TIOCHPCL, NULL);
307 close(0), close(1), close(2);
312 if (*pwd->pw_shell == '\0')
313 pwd->pw_shell = "/bin/sh";
314 if (chdir(pwd->pw_dir) < 0 && !invalid) {
315 if (chdir("/") < 0) {
316 printf("No directory!\n");
319 printf("No directory! %s\n", "Logging in with home=/");
324 * Remote login invalid must have been because
325 * of a restriction of some sort, no extra chances.
327 if (!usererr && invalid)
330 /* committed to login turn off timeout */
333 if (quota(Q_SETUID, pwd->pw_uid, 0, 0) < 0 && errno != EINVAL) {
335 printf("%s.\n%s.\n", "Too many users logged on already",
337 else if (errno == EPROCLIM)
338 printf("You have too many processes running.\n");
340 perror("quota (Q_SETUID)");
346 if (t > 0 && (f = open("/etc/utmp", O_WRONLY)) >= 0) {
347 lseek(f, (afs_int32) (t * sizeof(utmp)), 0);
348 SCPYN(utmp.ut_line, tty);
349 write(f, (char *)&utmp, sizeof(utmp));
352 if ((f = open("/usr/adm/wtmp", O_WRONLY | O_APPEND)) >= 0) {
353 write(f, (char *)&utmp, sizeof(utmp));
356 quietlog = access(qlog, F_OK) == 0;
357 if ((f = open(lastlog, O_RDWR)) >= 0) {
360 lseek(f, (afs_int32) pwd->pw_uid * sizeof(struct lastlog), 0);
361 if (read(f, (char *)&ll, sizeof ll) == sizeof ll && ll.ll_time != 0
363 printf("Last login: %.*s ", 24 - 5, (char *)ctime(&ll.ll_time));
364 if (*ll.ll_host != '\0')
365 printf("from %.*s\n", sizeof(ll.ll_host), ll.ll_host);
367 printf("on %.*s\n", sizeof(ll.ll_line), ll.ll_line);
369 lseek(f, (afs_int32) pwd->pw_uid * sizeof(struct lastlog), 0);
371 SCPYN(ll.ll_line, tty);
372 SCPYN(ll.ll_host, utmp.ut_host);
373 write(f, (char *)&ll, sizeof ll);
376 chown(ttyn, pwd->pw_uid, TTYGID(pwd->pw_gid));
377 if (!hflag && !rflag) /* XXX */
378 ioctl(0, TIOCSWINSZ, &win);
381 strncpy(name, utmp.ut_name, NMAX);
383 initgroups(name, pwd->pw_gid);
384 quota(Q_DOWARN, pwd->pw_uid, (dev_t) - 1, 0);
386 /* destroy environment unless user has asked to preserve it */
390 /* set up environment, this time without destruction */
391 /* copy the environment before setenving */
393 while (environ[i] != NULL)
395 envnew = (char **)malloc(sizeof(char *) * (i + 1));
397 envnew[i] = environ[i];
400 setenv("HOME=", pwd->pw_dir, 1);
401 setenv("SHELL=", pwd->pw_shell, 1);
403 strncpy(term, stypeof(tty), sizeof(term));
404 setenv("TERM=", term, 0);
405 setenv("USER=", pwd->pw_name, 1);
406 setenv("PATH=", ":/usr/ucb:/bin:/usr/bin", 0);
408 if ((namep = strrchr(pwd->pw_shell, '/')) == NULL)
409 namep = pwd->pw_shell;
412 strcat(minusnam, namep);
413 if (tty[sizeof("tty") - 1] == 'd')
414 syslog(LOG_INFO, "DIALUP %s, %s", tty, pwd->pw_name);
415 if (pwd->pw_uid == 0)
417 syslog(LOG_NOTICE, "ROOT LOGIN %s FROM %.*s", tty, HMAX,
420 syslog(LOG_NOTICE, "ROOT LOGIN %s", tty);
425 strcat(maildir, pwd->pw_name);
426 if (stat(maildir, &st) == 0 && st.st_size != 0)
427 printf("You have %smail.\n",
428 (st.st_mtime > st.st_atime) ? "new " : "");
430 signal(SIGALRM, SIG_DFL);
431 signal(SIGQUIT, SIG_DFL);
432 signal(SIGINT, SIG_DFL);
433 signal(SIGTSTP, SIG_IGN);
434 execlp(pwd->pw_shell, minusnam, 0);
435 perror(pwd->pw_shell);
436 printf("No shell\n");
441 getloginname(struct utmp *up)
446 while (up->ut_name[0] == '\0') {
449 while ((c = getchar()) != '\n') {
454 if (namep < up->ut_name + NMAX)
458 strncpy(lusername, up->ut_name, NMAX);
460 if ((pwd = getpwnam(lusername)) == NULL)
468 printf("Login timed out after %d seconds\n", timeout);
477 signal(SIGINT, SIG_IGN);
486 if ((t = getttynam(tty)) != NULL) {
487 if (t->ty_status & TTY_SECURE)
499 signal(SIGINT, catch);
500 if ((mf = fopen("/etc/motd", "r")) != NULL) {
501 while ((c = getc(mf)) != EOF && stopmotd == 0)
505 signal(SIGINT, SIG_IGN);
516 if (ttyid == NULL || (t = getttynam(ttyid)) == NULL)
522 doremotelogin(char *host)
524 getstr(rusername, sizeof(rusername), "remuser");
525 getstr(lusername, sizeof(lusername), "locuser");
526 getstr(term, sizeof(term), "Terminal type");
531 pwd = getpwnam(lusername);
536 return (ruserok(host, (pwd->pw_uid == 0), rusername, lusername));
540 getstr(char *buf, int cnt, char *err)
545 if (read(0, &c, 1) != 1)
548 printf("%s too long\r\n", err);
555 char *speeds[] = { "0", "50", "75", "110", "134", "150", "200", "300",
556 "600", "1200", "1800", "2400", "4800", "9600", "19200", "38400"
559 #define NSPEEDS (sizeof (speeds) / sizeof (speeds[0]))
562 doremoteterm(char *term, struct sgttyb *tp)
564 char *cp = strchr(term, '/'), **cpp;
570 cp = strchr(speed, '/');
573 for (cpp = speeds; cpp < &speeds[NSPEEDS]; cpp++)
574 if (strcmp(*cpp, speed) == 0) {
575 tp->sg_ispeed = tp->sg_ospeed = cpp - speeds;
579 tp->sg_flags = ECHO | CRMOD | ANYP | XTABS;
583 * Set the value of var to be arg in the Unix 4.2 BSD environment env.
584 * Var should end with '='.
585 * (bindings are of the form "var=value")
586 * This procedure assumes the memory for the first level of environ
587 * was allocated using malloc.
590 setenv(char *var, char *value, int clobber)
592 extern char **environ;
594 int varlen = strlen(var);
595 int vallen = strlen(value);
597 for (index = 0; environ[index] != NULL; index++) {
598 if (strncmp(environ[index], var, varlen) == 0) {
602 environ[index] = malloc(varlen + vallen + 1);
603 strcpy(environ[index], var);
604 strcat(environ[index], value);
608 environ = (char **)realloc(environ, sizeof(char *) * (index + 2));
609 if (environ == NULL) {
610 fprintf(stderr, "login: malloc out of memory\n");
613 environ[index] = malloc(varlen + vallen + 1);
614 strcpy(environ[index], var);
615 strcat(environ[index], value);
616 environ[++index] = NULL;
620 tty_gid(int default_gid)
622 struct group *getgrnam(), *gr;
623 gid_t gid = default_gid;
625 gr = getgrnam(TTYGRPNAME);
635 #include "AFS_component_version_number.c"
640 #endif /*!defined(AFS_SUN_ENV) && !defined(AFS_AIX_ENV) && !defined(AFS_HPUX_ENV) */