1 /* login.c - Start a session on the system.
3 * Copyright 2012 Elie De Brauwer <eliedebrauwer@gmail.com>
5 * No support for PAM/securetty/selinux/login script/issue/utmp
6 * Relies on libcrypt for hash calculation.
8 * TODO: this command predates "pending" but needs cleanup. It #defines
9 * random stuff, calls exit() form a signal handler... yeah.
11 USE_LOGIN(NEWTOY(login, ">1f:ph:", TOYFLAG_BIN|TOYFLAG_NEEDROOT))
16 depends on TOYBOX_SHADOW
18 usage: login [-p] [-h host] [-f USERNAME] [USERNAME]
20 Log in as a user, prompting for username and password if necessary.
22 -p Preserve environment
23 -h The name of the remote host for this login
24 -f login as USERNAME without authentication
34 int login_timeout, login_fail_timeout;
37 static void login_timeout_handler(int sig __attribute__((unused)))
39 printf("\nLogin timed out after %d seconds.\n", TT.login_timeout);
46 "BASH_ENV", "ENV", "HOME", "IFS", "LD_LIBRARY_PATH", "LD_PRELOAD",
47 "LD_TRACE_LOADED_OBJECTS", "LD_BIND_NOW", "LD_AOUT_LIBRARY_PATH",
48 "LD_AOUT_PRELOAD", "LD_NOWARN", "LD_KEEPDIR", "SHELL"
50 int hh = toys.optflags&FLAG_h, count, tty;
51 char uu[33], *username, *pass = 0, *ss;
52 struct passwd *pwd = 0;
54 for (tty=0; tty<3; tty++) if (isatty(tty)) break;
55 if (tty == 3) error_exit("no tty");
57 for (count = 0; count < ARRAY_LEN(forbid); count++) unsetenv(forbid[count]);
59 openlog("login", LOG_PID | LOG_CONS, LOG_AUTH);
60 xsignal(SIGALRM, login_timeout_handler);
62 if (TT.username) username = TT.username;
63 else username = *toys.optargs;
64 for (count = 0; count < 3; count++) {
65 alarm(TT.login_timeout = 60);
71 memset(username = uu, 0, sizeof(uu));
72 gethostname(uu, sizeof(uu)-1);
73 printf("%s%slogin: ", *uu ? uu : "", *uu ? " " : "");
76 if(!fgets(uu, sizeof(uu)-1, stdin)) _exit(1);
78 // Remove trailing \n and so on
79 for (i = 0; i<sizeof(uu); i++) if (uu[i]<=' ' || uu[i]==':') uu[i]=0;
86 // If user exists and isn't locked
87 pwd = getpwnam(username);
88 if (pwd && *pwd->pw_passwd != '!' && *pwd->pw_passwd != '*') {
90 // Pre-authenticated or passwordless
91 if (TT.username || !*pwd->pw_passwd) break;
93 // fetch shadow password if necessary
94 if (*(pass = pwd->pw_passwd) == 'x') {
95 struct spwd *spwd = getspnam (username);
97 if (spwd) pass = spwd->sp_pwdp;
99 } else if (TT.username) error_exit("bad -f '%s'", TT.username);
101 // Verify password. (Prompt for password _before_ checking disable state.)
102 if (!read_password(toybuf, sizeof(toybuf), "Password: ")) {
103 int x = pass && (ss = crypt(toybuf, pass)) && !strcmp(pass, ss);
105 // password go bye-bye now.
106 memset(toybuf, 0, sizeof(toybuf));
110 syslog(LOG_WARNING, "invalid password for '%s' on %s %s%s", pwd->pw_name,
111 ttyname(tty), hh ? "from " : "", hh ? TT.hostname : "");
114 puts("Login incorrect");
121 // This had password data in it, and we reuse for motd below
122 memset(toybuf, 0, sizeof(toybuf));
124 if (!pwd) error_exit("max retries (3)");
126 // Check twice because "this file exists" is a security test, and in
127 // theory filehandle exhaustion or other error could make open/read fail.
128 if (pwd->pw_uid && !access("/etc/nologin", R_OK)) {
129 ss = readfile("/etc/nologin", toybuf, sizeof(toybuf));
130 puts ((ss && *ss) ? ss : "nologin");
139 if (chdir(pwd->pw_dir)) printf("bad $HOME: %s\n", pwd->pw_dir);
141 if (!(toys.optflags&FLAG_p)) {
142 char *term = getenv("TERM");
145 if (term) setenv("TERM", term, 1);
148 setenv("USER", pwd->pw_name, 1);
149 setenv("LOGNAME", pwd->pw_name, 1);
150 setenv("HOME", pwd->pw_dir, 1);
151 setenv("SHELL", pwd->pw_shell, 1);
153 // Message of the day
154 if ((ss = readfile("/etc/motd", 0, 0))) {
159 syslog(LOG_INFO, "%s logged in on %s %s %s", pwd->pw_name,
160 ttyname(tty), hh ? "from" : "", hh ? TT.hostname : "");
162 // not using xexec(), login calls absolute path from filesystem so must exec()
163 execl(pwd->pw_shell, xmprintf("-%s", pwd->pw_shell), (char *)0);
164 perror_exit("exec shell '%s'", pwd->pw_shell);