Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Newer
Older
100644 445 lines (389 sloc) 11.812 kB
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
1 /* vi: set sw=4 ts=4: */
801ab14 Add one-line GPL boilerplate to numerous (but not all yet) source files.
"Robert P. J. Day" authored
2 /*
0ef64bd *: make GNU licensing statement forms more regular
Denys Vlasenko authored
3 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
801ab14 Add one-line GPL boilerplate to numerous (but not all yet) source files.
"Robert P. J. Day" authored
4 */
b6adbf1 usage.c: remove reference to busybox.h
Denis Vlasenko authored
5 #include "libbb.h"
f470196 @aldot - fix conflicting decls of syslog related facilitynames and priorityn…
aldot authored
6 #include <syslog.h>
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
7 #include <sys/resource.h>
8
c0415a9 login: nuke nonblock() - we have it in libbb; -400 bytes bss.
Denis Vlasenko authored
9 #if ENABLE_SELINUX
37f5bef libbb: split update_utmp from login/getty in preparation to use it fo…
Denys Vlasenko authored
10 # include <selinux/selinux.h> /* for is_selinux_enabled() */
11 # include <selinux/get_context_list.h> /* for get_default_context() */
12 # include <selinux/flask.h> /* for security class definitions */
9e48045 Patch from Russell Coker:
Eric Andersen authored
13 #endif
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
14
d6e81c7 login: optional support for PAM
Denis Vlasenko authored
15 #if ENABLE_PAM
c6c2345 login: fixes for PAM build
Denis Vlasenko authored
16 /* PAM may include <locale.h>. We may need to undefine bbox's stub define: */
37f5bef libbb: split update_utmp from login/getty in preparation to use it fo…
Denys Vlasenko authored
17 # undef setlocale
c6c2345 login: fixes for PAM build
Denis Vlasenko authored
18 /* For some obscure reason, PAM is not in pam/xxx, but in security/xxx.
19 * Apparently they like to confuse people. */
37f5bef libbb: split update_utmp from login/getty in preparation to use it fo…
Denys Vlasenko authored
20 # include <security/pam_appl.h>
21 # include <security/pam_misc.h>
d6e81c7 login: optional support for PAM
Denis Vlasenko authored
22 static const struct pam_conv conv = {
23 misc_conv,
24 NULL
25 };
26 #endif
27
942e429 login: previous commit comment was wrong :)
Denis Vlasenko authored
28 enum {
29 TIMEOUT = 60,
30 EMPTY_USERNAME_COUNT = 10,
31 USERNAME_SIZE = 32,
9a9edf2 login: make it saner and smaller by ~0.5k.
Denis Vlasenko authored
32 TTYNAME_SIZE = 32,
942e429 login: previous commit comment was wrong :)
Denis Vlasenko authored
33 };
0fbff13 Several login cleanups from vodz
Eric Andersen authored
34
c0415a9 login: nuke nonblock() - we have it in libbb; -400 bytes bss.
Denis Vlasenko authored
35 static char* short_tty;
7ccf5cc login: eliminate forward decls and #ifdefs
Denis Vlasenko authored
36
f312e32 login: make /etc/nologin support configurable. -240 bytes if not sele…
Denis Vlasenko authored
37 #if ENABLE_FEATURE_NOLOGIN
68404f1 *: add -Wunused-parameter; fix resulting breakage
Denis Vlasenko authored
38 static void die_if_nologin(void)
7ccf5cc login: eliminate forward decls and #ifdefs
Denis Vlasenko authored
39 {
40 FILE *fp;
41 int c;
694b514 login: fix /etc/nologin support
Denis Vlasenko authored
42 int empty = 1;
7ccf5cc login: eliminate forward decls and #ifdefs
Denis Vlasenko authored
43
694b514 login: fix /etc/nologin support
Denis Vlasenko authored
44 fp = fopen_for_read("/etc/nologin");
45 if (!fp) /* assuming it does not exist */
7ccf5cc login: eliminate forward decls and #ifdefs
Denis Vlasenko authored
46 return;
47
694b514 login: fix /etc/nologin support
Denis Vlasenko authored
48 while ((c = getc(fp)) != EOF) {
49 if (c == '\n')
50 bb_putchar('\r');
51 bb_putchar(c);
52 empty = 0;
53 }
54 if (empty)
7ccf5cc login: eliminate forward decls and #ifdefs
Denis Vlasenko authored
55 puts("\r\nSystem closed for routine maintenance\r");
694b514 login: fix /etc/nologin support
Denis Vlasenko authored
56
57 fclose(fp);
8131eea *: introduce and use ffulsh_all()
Denys Vlasenko authored
58 fflush_all();
694b514 login: fix /etc/nologin support
Denis Vlasenko authored
59 /* Users say that they do need this prior to exit: */
60 tcdrain(STDOUT_FILENO);
636a1f8 @aldot - use EXIT_{SUCCESS,FAILURE}. No object-code changes
aldot authored
61 exit(EXIT_FAILURE);
7ccf5cc login: eliminate forward decls and #ifdefs
Denis Vlasenko authored
62 }
f312e32 login: make /etc/nologin support configurable. -240 bytes if not sele…
Denis Vlasenko authored
63 #else
37f5bef libbb: split update_utmp from login/getty in preparation to use it fo…
Denys Vlasenko authored
64 # define die_if_nologin() ((void)0)
f312e32 login: make /etc/nologin support configurable. -240 bytes if not sele…
Denis Vlasenko authored
65 #endif
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
66
d6e81c7 login: optional support for PAM
Denis Vlasenko authored
67 #if ENABLE_FEATURE_SECURETTY && !ENABLE_PAM
7ccf5cc login: eliminate forward decls and #ifdefs
Denis Vlasenko authored
68 static int check_securetty(void)
69 {
084266e fix several problems with config parser:
Denis Vlasenko authored
70 char *buf = (char*)"/etc/securetty"; /* any non-NULL is ok */
5415c85 libbb: [x]fopen_for_{read,write} introduced and used.
Denis Vlasenko authored
71 parser_t *parser = config_open2("/etc/securetty", fopen_for_read);
084266e fix several problems with config parser:
Denis Vlasenko authored
72 while (config_read(parser, &buf, 1, 1, "# \t", PARSE_NORMAL)) {
73 if (strcmp(buf, short_tty) == 0)
74 break;
75 buf = NULL;
7ccf5cc login: eliminate forward decls and #ifdefs
Denis Vlasenko authored
76 }
084266e fix several problems with config parser:
Denis Vlasenko authored
77 config_close(parser);
78 /* buf != NULL here if config file was not found, empty
79 * or line was found which equals short_tty */
80 return buf != NULL;
7ccf5cc login: eliminate forward decls and #ifdefs
Denis Vlasenko authored
81 }
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
82 #else
f312e32 login: make /etc/nologin support configurable. -240 bytes if not sele…
Denis Vlasenko authored
83 static ALWAYS_INLINE int check_securetty(void) { return 1; }
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
84 #endif
85
4eff8ef login: cleanup work by Walter Harms. No logic changes.
Denis Vlasenko authored
86 #if ENABLE_SELINUX
87 static void initselinux(char *username, char *full_tty,
88 security_context_t *user_sid)
89 {
90 security_context_t old_tty_sid, new_tty_sid;
91
92 if (!is_selinux_enabled())
93 return;
94
95 if (get_default_context(username, NULL, user_sid)) {
6331cf0 *: use "can't" instead of "cannot"
Denys Vlasenko authored
96 bb_error_msg_and_die("can't get SID for %s", username);
4eff8ef login: cleanup work by Walter Harms. No logic changes.
Denis Vlasenko authored
97 }
98 if (getfilecon(full_tty, &old_tty_sid) < 0) {
99 bb_perror_msg_and_die("getfilecon(%s) failed", full_tty);
100 }
8e5de2a login: fix buglet introduced by "simplification"
Denis Vlasenko authored
101 if (security_compute_relabel(*user_sid, old_tty_sid,
4eff8ef login: cleanup work by Walter Harms. No logic changes.
Denis Vlasenko authored
102 SECCLASS_CHR_FILE, &new_tty_sid) != 0) {
103 bb_perror_msg_and_die("security_change_sid(%s) failed", full_tty);
104 }
105 if (setfilecon(full_tty, new_tty_sid) != 0) {
106 bb_perror_msg_and_die("chsid(%s, %s) failed", full_tty, new_tty_sid);
107 }
108 }
109 #endif
110
111 #if ENABLE_LOGIN_SCRIPTS
112 static void run_login_script(struct passwd *pw, char *full_tty)
113 {
114 char *t_argv[2];
115
116 t_argv[0] = getenv("LOGIN_PRE_SUID_SCRIPT");
117 if (t_argv[0]) {
118 t_argv[1] = NULL;
119 xsetenv("LOGIN_TTY", full_tty);
120 xsetenv("LOGIN_USER", pw->pw_name);
121 xsetenv("LOGIN_UID", utoa(pw->pw_uid));
122 xsetenv("LOGIN_GID", utoa(pw->pw_gid));
123 xsetenv("LOGIN_SHELL", pw->pw_shell);
e4dcba1 *: whitespace fixes
Denys Vlasenko authored
124 spawn_and_wait(t_argv); /* NOMMU-friendly */
4eff8ef login: cleanup work by Walter Harms. No logic changes.
Denis Vlasenko authored
125 unsetenv("LOGIN_TTY");
126 unsetenv("LOGIN_USER");
127 unsetenv("LOGIN_UID");
128 unsetenv("LOGIN_GID");
129 unsetenv("LOGIN_SHELL");
130 }
131 }
132 #else
133 void run_login_script(struct passwd *pw, char *full_tty);
134 #endif
135
7ccf5cc login: eliminate forward decls and #ifdefs
Denis Vlasenko authored
136 static void get_username_or_die(char *buf, int size_buf)
137 {
138 int c, cntdown;
d8540f7 init: make sure fd 0,1,2 are not closed, + related optimizations.
Denis Vlasenko authored
139
7ccf5cc login: eliminate forward decls and #ifdefs
Denis Vlasenko authored
140 cntdown = EMPTY_USERNAME_COUNT;
d8540f7 init: make sure fd 0,1,2 are not closed, + related optimizations.
Denis Vlasenko authored
141 prompt:
7ccf5cc login: eliminate forward decls and #ifdefs
Denis Vlasenko authored
142 print_login_prompt();
d8540f7 init: make sure fd 0,1,2 are not closed, + related optimizations.
Denis Vlasenko authored
143 /* skip whitespace */
7ccf5cc login: eliminate forward decls and #ifdefs
Denis Vlasenko authored
144 do {
145 c = getchar();
f2cbb03 *: optimize most of isXXXXX() macros
Denys Vlasenko authored
146 if (c == EOF)
147 exit(EXIT_FAILURE);
7ccf5cc login: eliminate forward decls and #ifdefs
Denis Vlasenko authored
148 if (c == '\n') {
f2cbb03 *: optimize most of isXXXXX() macros
Denys Vlasenko authored
149 if (!--cntdown)
150 exit(EXIT_FAILURE);
7ccf5cc login: eliminate forward decls and #ifdefs
Denis Vlasenko authored
151 goto prompt;
152 }
f2cbb03 *: optimize most of isXXXXX() macros
Denys Vlasenko authored
153 } while (isspace(c)); /* maybe isblank? */
7ccf5cc login: eliminate forward decls and #ifdefs
Denis Vlasenko authored
154
155 *buf++ = c;
156 if (!fgets(buf, size_buf-2, stdin))
636a1f8 @aldot - use EXIT_{SUCCESS,FAILURE}. No object-code changes
aldot authored
157 exit(EXIT_FAILURE);
7ccf5cc login: eliminate forward decls and #ifdefs
Denis Vlasenko authored
158 if (!strchr(buf, '\n'))
636a1f8 @aldot - use EXIT_{SUCCESS,FAILURE}. No object-code changes
aldot authored
159 exit(EXIT_FAILURE);
f2cbb03 *: optimize most of isXXXXX() macros
Denys Vlasenko authored
160 while ((unsigned char)*buf > ' ')
161 buf++;
7ccf5cc login: eliminate forward decls and #ifdefs
Denis Vlasenko authored
162 *buf = '\0';
163 }
164
165 static void motd(void)
166 {
0de3c55 login: remove setpgrp call (makes it work from shell prompt again).
Denis Vlasenko authored
167 int fd;
7ccf5cc login: eliminate forward decls and #ifdefs
Denis Vlasenko authored
168
0de3c55 login: remove setpgrp call (makes it work from shell prompt again).
Denis Vlasenko authored
169 fd = open(bb_path_motd_file, O_RDONLY);
5281630 login: clear dangerous environment variables if started by non-root
Denis Vlasenko authored
170 if (fd >= 0) {
8131eea *: introduce and use ffulsh_all()
Denys Vlasenko authored
171 fflush_all();
0de3c55 login: remove setpgrp call (makes it work from shell prompt again).
Denis Vlasenko authored
172 bb_copyfd_eof(fd, STDOUT_FILENO);
173 close(fd);
7ccf5cc login: eliminate forward decls and #ifdefs
Denis Vlasenko authored
174 }
175 }
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
176
a60f84e *: rename ATTRIBUTE_XXX to just XXX.
Denis Vlasenko authored
177 static void alarm_handler(int sig UNUSED_PARAM)
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
178 {
2f50aa4 login: use some ideas from util-linux's login.
Denis Vlasenko authored
179 /* This is the escape hatch! Poor serial line users and the like
180 * arrive here when their connection is broken.
181 * We don't want to block here */
c0415a9 login: nuke nonblock() - we have it in libbb; -400 bytes bss.
Denis Vlasenko authored
182 ndelay_on(1);
0de3c55 login: remove setpgrp call (makes it work from shell prompt again).
Denis Vlasenko authored
183 printf("\r\nLogin timed out after %d seconds\r\n", TIMEOUT);
8131eea *: introduce and use ffulsh_all()
Denys Vlasenko authored
184 fflush_all();
5281630 login: clear dangerous environment variables if started by non-root
Denis Vlasenko authored
185 /* unix API is brain damaged regarding O_NONBLOCK,
186 * we should undo it, or else we can affect other processes */
187 ndelay_off(1);
400d8bb less,klogd,syslogd,nc,tcpudp: exit on signal by killing itself, not e…
Denis Vlasenko authored
188 _exit(EXIT_SUCCESS);
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
189 }
190
9b49a5e add -fvisibility=hidden to CC flags, mark XXX_main functions
Denis Vlasenko authored
191 int login_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
a60f84e *: rename ATTRIBUTE_XXX to just XXX.
Denis Vlasenko authored
192 int login_main(int argc UNUSED_PARAM, char **argv)
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
193 {
66fabdb login: apply fixes + getopt_ulflag'ification by Bernhard
Denis Vlasenko authored
194 enum {
195 LOGIN_OPT_f = (1<<0),
196 LOGIN_OPT_h = (1<<1),
197 LOGIN_OPT_p = (1<<2),
198 };
d6e81c7 login: optional support for PAM
Denis Vlasenko authored
199 char *fromhost;
0fbff13 Several login cleanups from vodz
Eric Andersen authored
200 char username[USERNAME_SIZE];
2ec94a7 login: fix /etc/nologin handling (should prohibit non-root LOGINS,
Denis Vlasenko authored
201 int run_by_root;
67b23e6 getopt_ulflags -> getopt32.
Denis Vlasenko authored
202 unsigned opt;
942e429 login: previous commit comment was wrong :)
Denis Vlasenko authored
203 int count = 0;
9a9edf2 login: make it saner and smaller by ~0.5k.
Denis Vlasenko authored
204 struct passwd *pw;
37f5bef libbb: split update_utmp from login/getty in preparation to use it fo…
Denys Vlasenko authored
205 char *opt_host = NULL;
cdf6277 dos2unix: tiny shrink
Denis Vlasenko authored
206 char *opt_user = opt_user; /* for compiler */
512c8ae login: small simplification by Walter Harms. -10 bytes
Denis Vlasenko authored
207 char *full_tty;
5e34ff2 *: mass renaming of USE_XXXX to IF_XXXX
Denis Vlasenko authored
208 IF_SELINUX(security_context_t user_sid = NULL;)
a29a5e4 login: fix PAM login (was unable to complete Kerberos login)
Denis Vlasenko authored
209 #if ENABLE_PAM
210 int pamret;
211 pam_handle_t *pamh;
212 const char *pamuser;
213 const char *failed_msg;
214 struct passwd pwdstruct;
215 char pwdbuf[256];
260fb55 login: apply PAM environment
Ian Wienand authored
216 char **pamenv;
a29a5e4 login: fix PAM login (was unable to complete Kerberos login)
Denis Vlasenko authored
217 #endif
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
218
942e429 login: previous commit comment was wrong :)
Denis Vlasenko authored
219 username[0] = '\0';
220 signal(SIGALRM, alarm_handler);
221 alarm(TIMEOUT);
c7bda1c Remove trailing whitespace. Update copyright to include 2004.
Eric Andersen authored
222
2ec94a7 login: fix /etc/nologin handling (should prohibit non-root LOGINS,
Denis Vlasenko authored
223 /* More of suid paranoia if called by non-root: */
224 /* Clear dangerous stuff, set PATH */
225 run_by_root = !sanitize_env_if_suid();
c9ca0a3 mount: recognize "dirsync" (closes bug 835)
Denis Vlasenko authored
226
0de3c55 login: remove setpgrp call (makes it work from shell prompt again).
Denis Vlasenko authored
227 /* Mandatory paranoia for suid applet:
228 * ensure that fd# 0,1,2 are opened (at least to /dev/null)
229 * and any extra open fd's are closed.
230 * (The name of the function is misleading. Not daemonizing here.) */
231 bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE | DAEMON_CLOSE_EXTRA_FDS, NULL);
232
fe7cd64 don't pass argc in getopt32, it's superfluous
Denis Vlasenko authored
233 opt = getopt32(argv, "f:h:p", &opt_user, &opt_host);
66fabdb login: apply fixes + getopt_ulflag'ification by Bernhard
Denis Vlasenko authored
234 if (opt & LOGIN_OPT_f) {
2ec94a7 login: fix /etc/nologin handling (should prohibit non-root LOGINS,
Denis Vlasenko authored
235 if (!run_by_root)
66fabdb login: apply fixes + getopt_ulflag'ification by Bernhard
Denis Vlasenko authored
236 bb_error_msg_and_die("-f is for root only");
22f6dcb login: fix getopt_ulflags fallout (wasn't taking username supplied by…
Denis Vlasenko authored
237 safe_strncpy(username, opt_user, sizeof(username));
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
238 }
1d42665 *: shrink: use Vladimir's "o+" specifier instead of xatou(opt_param)
Denis Vlasenko authored
239 argv += optind;
240 if (argv[0]) /* user from command line (getty) */
241 safe_strncpy(username, argv[0], sizeof(username));
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
242
9a9edf2 login: make it saner and smaller by ~0.5k.
Denis Vlasenko authored
243 /* Let's find out and memorize our tty */
512c8ae login: small simplification by Walter Harms. -10 bytes
Denis Vlasenko authored
244 if (!isatty(STDIN_FILENO) || !isatty(STDOUT_FILENO) || !isatty(STDERR_FILENO))
e4dcba1 *: whitespace fixes
Denys Vlasenko authored
245 return EXIT_FAILURE; /* Must be a terminal */
512c8ae login: small simplification by Walter Harms. -10 bytes
Denis Vlasenko authored
246 full_tty = xmalloc_ttyname(STDIN_FILENO);
247 if (!full_tty)
248 full_tty = xstrdup("UNKNOWN");
f8d8aa1 libbb: add skip_dev_pfx()
Denys Vlasenko authored
249 short_tty = skip_dev_pfx(full_tty);
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
250
37f5bef libbb: split update_utmp from login/getty in preparation to use it fo…
Denys Vlasenko authored
251 if (opt_host) {
d6e81c7 login: optional support for PAM
Denis Vlasenko authored
252 fromhost = xasprintf(" on '%s' from '%s'", short_tty, opt_host);
4e12b1a libbb: introduce and use xmalloc_ttyname (-32 in bss).
Denis Vlasenko authored
253 } else {
d6e81c7 login: optional support for PAM
Denis Vlasenko authored
254 fromhost = xasprintf(" on '%s'", short_tty);
4e12b1a libbb: introduce and use xmalloc_ttyname (-32 in bss).
Denis Vlasenko authored
255 }
c7bda1c Remove trailing whitespace. Update copyright to include 2004.
Eric Andersen authored
256
d6e81c7 login: optional support for PAM
Denis Vlasenko authored
257 /* Was breaking "login <username>" from shell command line: */
258 /*bb_setpgrp();*/
0fbff13 Several login cleanups from vodz
Eric Andersen authored
259
54ac03a *: remove LOG_NOWAIT
Denis Vlasenko authored
260 openlog(applet_name, LOG_PID | LOG_CONS, LOG_AUTH);
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
261
942e429 login: previous commit comment was wrong :)
Denis Vlasenko authored
262 while (1) {
6e1b62b flush typeahead before prompt
Paul Fox authored
263 /* flush away any type-ahead (as getty does) */
f812eac @jeremie-koenig init,loginutils: termios portability fixes
jeremie-koenig authored
264 tcflush(0, TCIFLUSH);
6e1b62b flush typeahead before prompt
Paul Fox authored
265
942e429 login: previous commit comment was wrong :)
Denis Vlasenko authored
266 if (!username[0])
9a9edf2 login: make it saner and smaller by ~0.5k.
Denis Vlasenko authored
267 get_username_or_die(username, sizeof(username));
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
268
d6e81c7 login: optional support for PAM
Denis Vlasenko authored
269 #if ENABLE_PAM
270 pamret = pam_start("login", username, &conv, &pamh);
271 if (pamret != PAM_SUCCESS) {
a29a5e4 login: fix PAM login (was unable to complete Kerberos login)
Denis Vlasenko authored
272 failed_msg = "start";
d6e81c7 login: optional support for PAM
Denis Vlasenko authored
273 goto pam_auth_failed;
274 }
275 /* set TTY (so things like securetty work) */
276 pamret = pam_set_item(pamh, PAM_TTY, short_tty);
277 if (pamret != PAM_SUCCESS) {
a29a5e4 login: fix PAM login (was unable to complete Kerberos login)
Denis Vlasenko authored
278 failed_msg = "set_item(TTY)";
d6e81c7 login: optional support for PAM
Denis Vlasenko authored
279 goto pam_auth_failed;
280 }
281 pamret = pam_authenticate(pamh, 0);
82f3b16 login: do reject wrong passwords with PAM auth
Denis Vlasenko authored
282 if (pamret != PAM_SUCCESS) {
a29a5e4 login: fix PAM login (was unable to complete Kerberos login)
Denis Vlasenko authored
283 failed_msg = "authenticate";
82f3b16 login: do reject wrong passwords with PAM auth
Denis Vlasenko authored
284 goto pam_auth_failed;
285 /* TODO: or just "goto auth_failed"
286 * since user seems to enter wrong password
287 * (in this case pamret == 7)
288 */
289 }
290 /* check that the account is healthy */
291 pamret = pam_acct_mgmt(pamh, 0);
292 if (pamret != PAM_SUCCESS) {
a29a5e4 login: fix PAM login (was unable to complete Kerberos login)
Denis Vlasenko authored
293 failed_msg = "acct_mgmt";
82f3b16 login: do reject wrong passwords with PAM auth
Denis Vlasenko authored
294 goto pam_auth_failed;
295 }
296 /* read user back */
a29a5e4 login: fix PAM login (was unable to complete Kerberos login)
Denis Vlasenko authored
297 pamuser = NULL;
298 /* gcc: "dereferencing type-punned pointer breaks aliasing rules..."
299 * thus we cast to (void*) */
300 if (pam_get_item(pamh, PAM_USER, (void*)&pamuser) != PAM_SUCCESS) {
301 failed_msg = "get_item(USER)";
302 goto pam_auth_failed;
d6e81c7 login: optional support for PAM
Denis Vlasenko authored
303 }
a29a5e4 login: fix PAM login (was unable to complete Kerberos login)
Denis Vlasenko authored
304 if (!pamuser || !pamuser[0])
305 goto auth_failed;
306 safe_strncpy(username, pamuser, sizeof(username));
307 /* Don't use "pw = getpwnam(username);",
308 * PAM is said to be capable of destroying static storage
309 * used by getpwnam(). We are using safe(r) function */
310 pw = NULL;
311 getpwnam_r(username, &pwdstruct, pwdbuf, sizeof(pwdbuf), &pw);
312 if (!pw)
313 goto auth_failed;
314 pamret = pam_open_session(pamh, 0);
315 if (pamret != PAM_SUCCESS) {
316 failed_msg = "open_session";
317 goto pam_auth_failed;
318 }
319 pamret = pam_setcred(pamh, PAM_ESTABLISH_CRED);
320 if (pamret != PAM_SUCCESS) {
321 failed_msg = "setcred";
322 goto pam_auth_failed;
323 }
324 break; /* success, continue login process */
325
d6e81c7 login: optional support for PAM
Denis Vlasenko authored
326 pam_auth_failed:
c297ea9 login: log PAM errors to syslog, not stderr
Denys Vlasenko authored
327 /* syslog, because we don't want potential attacker
328 * to know _why_ login failed */
329 syslog(LOG_WARNING, "pam_%s call failed: %s (%d)", failed_msg,
a29a5e4 login: fix PAM login (was unable to complete Kerberos login)
Denis Vlasenko authored
330 pam_strerror(pamh, pamret), pamret);
d6e81c7 login: optional support for PAM
Denis Vlasenko authored
331 safe_strncpy(username, "UNKNOWN", sizeof(username));
332 #else /* not PAM */
942e429 login: previous commit comment was wrong :)
Denis Vlasenko authored
333 pw = getpwnam(username);
334 if (!pw) {
65e14b4 login: ask passwords even for wrong usernames.
Denis Vlasenko authored
335 strcpy(username, "UNKNOWN");
336 goto fake_it;
9a9edf2 login: make it saner and smaller by ~0.5k.
Denis Vlasenko authored
337 }
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
338
9a9edf2 login: make it saner and smaller by ~0.5k.
Denis Vlasenko authored
339 if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*')
340 goto auth_failed;
c7bda1c Remove trailing whitespace. Update copyright to include 2004.
Eric Andersen authored
341
66fabdb login: apply fixes + getopt_ulflag'ification by Bernhard
Denis Vlasenko authored
342 if (opt & LOGIN_OPT_f)
9a9edf2 login: make it saner and smaller by ~0.5k.
Denis Vlasenko authored
343 break; /* -f USER: success without asking passwd */
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
344
9a9edf2 login: make it saner and smaller by ~0.5k.
Denis Vlasenko authored
345 if (pw->pw_uid == 0 && !check_securetty())
346 goto auth_failed;
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
347
348 /* Don't check the password if password entry is empty (!) */
942e429 login: previous commit comment was wrong :)
Denis Vlasenko authored
349 if (!pw->pw_passwd[0])
66fabdb login: apply fixes + getopt_ulflag'ification by Bernhard
Denis Vlasenko authored
350 break;
65e14b4 login: ask passwords even for wrong usernames.
Denis Vlasenko authored
351 fake_it:
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
352 /* authorization takes place here */
942e429 login: previous commit comment was wrong :)
Denis Vlasenko authored
353 if (correct_password(pw))
66fabdb login: apply fixes + getopt_ulflag'ification by Bernhard
Denis Vlasenko authored
354 break;
d6e81c7 login: optional support for PAM
Denis Vlasenko authored
355 #endif /* ENABLE_PAM */
0de3c55 login: remove setpgrp call (makes it work from shell prompt again).
Denis Vlasenko authored
356 auth_failed:
66fabdb login: apply fixes + getopt_ulflag'ification by Bernhard
Denis Vlasenko authored
357 opt &= ~LOGIN_OPT_f;
84cb767 @landley patch from tito: consolidate delay functions as bb_do_delay()
landley authored
358 bb_do_delay(FAIL_DELAY);
82f3b16 login: do reject wrong passwords with PAM auth
Denis Vlasenko authored
359 /* TODO: doesn't sound like correct English phrase to me */
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
360 puts("Login incorrect");
942e429 login: previous commit comment was wrong :)
Denis Vlasenko authored
361 if (++count == 3) {
89f0b34 rodata cleanup. "unable to" == "cannot". -300 bytes
Denis Vlasenko authored
362 syslog(LOG_WARNING, "invalid password for '%s'%s",
9a9edf2 login: make it saner and smaller by ~0.5k.
Denis Vlasenko authored
363 username, fromhost);
78b286f @virtuoso login: free fromhost upon cleaning up
virtuoso authored
364
365 if (ENABLE_FEATURE_CLEAN_UP)
366 free(fromhost);
367
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
368 return EXIT_FAILURE;
942e429 login: previous commit comment was wrong :)
Denis Vlasenko authored
369 }
9a9edf2 login: make it saner and smaller by ~0.5k.
Denis Vlasenko authored
370 username[0] = '\0';
2ec94a7 login: fix /etc/nologin handling (should prohibit non-root LOGINS,
Denis Vlasenko authored
371 } /* while (1) */
c7bda1c Remove trailing whitespace. Update copyright to include 2004.
Eric Andersen authored
372
942e429 login: previous commit comment was wrong :)
Denis Vlasenko authored
373 alarm(0);
2ec94a7 login: fix /etc/nologin handling (should prohibit non-root LOGINS,
Denis Vlasenko authored
374 /* We can ignore /etc/nologin if we are logging in as root,
375 * it doesn't matter whether we are run by root or not */
376 if (pw->pw_uid != 0)
68404f1 *: add -Wunused-parameter; fix resulting breakage
Denis Vlasenko authored
377 die_if_nologin();
60158cb @landley A patch from Takeharu KATO to update/fix SE-Linux support.
landley authored
378
5e34ff2 *: mass renaming of USE_XXXX to IF_XXXX
Denis Vlasenko authored
379 IF_SELINUX(initselinux(username, full_tty, &user_sid));
9e48045 Patch from Russell Coker:
Eric Andersen authored
380
2f50aa4 login: use some ideas from util-linux's login.
Denis Vlasenko authored
381 /* Try these, but don't complain if they fail.
382 * _f_chown is safe wrt race t=ttyname(0);...;chown(t); */
383 fchown(0, pw->pw_uid, pw->pw_gid);
384 fchmod(0, 0600);
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
385
3a41611 telnetd: write LOGIN/DEAD_PROCESS utmp records. Closes bug 1363
Denys Vlasenko authored
386 update_utmp(getpid(), USER_PROCESS, short_tty, username, run_by_root ? opt_host : NULL);
37f5bef libbb: split update_utmp from login/getty in preparation to use it fo…
Denys Vlasenko authored
387
5281630 login: clear dangerous environment variables if started by non-root
Denis Vlasenko authored
388 /* We trust environment only if we run by root */
3266aa9 trailing whitespace removal
Denis Vlasenko authored
389 if (ENABLE_LOGIN_SCRIPTS && run_by_root)
4eff8ef login: cleanup work by Walter Harms. No logic changes.
Denis Vlasenko authored
390 run_login_script(pw, full_tty);
2e50291 login: style fixes
Denis Vlasenko authored
391
942e429 login: previous commit comment was wrong :)
Denis Vlasenko authored
392 change_identity(pw);
bd74e3d libbb/login/su: do not sanitize shell name twice
Denys Vlasenko authored
393 setup_environment(pw->pw_shell,
fd686a2 setup_environment(): eliminate one parameter
Denys Vlasenko authored
394 (!(opt & LOGIN_OPT_p) * SETUP_ENV_CLEARENV) + SETUP_ENV_CHANGEENV,
395 pw);
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
396
260fb55 login: apply PAM environment
Ian Wienand authored
397 #if ENABLE_PAM
398 /* Modules such as pam_env will setup the PAM environment,
399 * which should be copied into the new environment. */
400 pamenv = pam_getenvlist(pamh);
401 if (pamenv) while (*pamenv) {
402 putenv(*pamenv);
403 pamenv++;
404 }
405 #endif
406
942e429 login: previous commit comment was wrong :)
Denis Vlasenko authored
407 motd();
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
408
942e429 login: previous commit comment was wrong :)
Denis Vlasenko authored
409 if (pw->pw_uid == 0)
2f50aa4 login: use some ideas from util-linux's login.
Denis Vlasenko authored
410 syslog(LOG_INFO, "root login%s", fromhost);
4eff8ef login: cleanup work by Walter Harms. No logic changes.
Denis Vlasenko authored
411
78b286f @virtuoso login: free fromhost upon cleaning up
virtuoso authored
412 if (ENABLE_FEATURE_CLEAN_UP)
413 free(fromhost);
414
d1f8c1c @landley From Jan Kiszka: This patch fixes the security labelling of the login…
landley authored
415 /* well, a simple setexeccon() here would do the job as well,
416 * but let's play the game for now */
5e34ff2 *: mass renaming of USE_XXXX to IF_XXXX
Denis Vlasenko authored
417 IF_SELINUX(set_current_security_context(user_sid);)
6ae8079 login: re-enable Ctrl-^C before execing shell.
Denis Vlasenko authored
418
419 // util-linux login also does:
420 // /* start new session */
421 // setsid();
422 // /* TIOCSCTTY: steal tty from other process group */
423 // if (ioctl(0, TIOCSCTTY, 1)) error_msg...
0de3c55 login: remove setpgrp call (makes it work from shell prompt again).
Denis Vlasenko authored
424 // BBox login used to do this (see above):
425 // bb_setpgrp();
426 // If this stuff is really needed, add it and explain why!
6ae8079 login: re-enable Ctrl-^C before execing shell.
Denis Vlasenko authored
427
2ec94a7 login: fix /etc/nologin handling (should prohibit non-root LOGINS,
Denis Vlasenko authored
428 /* Set signals to defaults */
3fa36e2 *: a bit of code shrink
Denis Vlasenko authored
429 /* Non-ignored signals revert to SIG_DFL on exec anyway */
430 /*signal(SIGALRM, SIG_DFL);*/
2ec94a7 login: fix /etc/nologin handling (should prohibit non-root LOGINS,
Denis Vlasenko authored
431
8c76487 login: add big fat comment about SIGINT - prevent
Denis Vlasenko authored
432 /* Is this correct? This way user can ctrl-c out of /etc/profile,
433 * potentially creating security breach (tested with bash 3.0).
434 * But without this, bash 3.0 will not enable ctrl-c either.
435 * Maybe bash is buggy?
436 * Need to find out what standards say about /bin/login -
2ec94a7 login: fix /etc/nologin handling (should prohibit non-root LOGINS,
Denis Vlasenko authored
437 * should we leave SIGINT etc enabled or disabled? */
6ae8079 login: re-enable Ctrl-^C before execing shell.
Denis Vlasenko authored
438 signal(SIGINT, SIG_DFL);
439
a2f6101 setup_environment: code shrink
Denis Vlasenko authored
440 /* Exec login shell with no additional parameters */
bd74e3d libbb/login/su: do not sanitize shell name twice
Denys Vlasenko authored
441 run_shell(pw->pw_shell, 1, NULL, NULL);
c7bda1c Remove trailing whitespace. Update copyright to include 2004.
Eric Andersen authored
442
a2f6101 setup_environment: code shrink
Denis Vlasenko authored
443 /* return EXIT_FAILURE; - not reached */
1fca558 Bigger patch for (partial) tinylogin integration
Robert Griebl authored
444 }
Something went wrong with that request. Please try again.