Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Newer
Older
100644 515 lines (454 sloc) 14.372 kb
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
1 /* vi: set sw=4 ts=4: */
801ab140 »
2006-07-12 Add one-line GPL boilerplate to numerous (but not all yet) source files.
2 /*
3 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
4 */
5
b6adbf1b »
2007-05-26 usage.c: remove reference to busybox.h
6 #include "libbb.h"
f470196a »
2008-01-27 - fix conflicting decls of syslog related facilitynames and priorityn…
7 #include <syslog.h>
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
8 #include <utmp.h>
9 #include <sys/resource.h>
10
c0415a98 »
2007-03-24 login: nuke nonblock() - we have it in libbb; -400 bytes bss.
11 #if ENABLE_SELINUX
60158cb9 »
2005-05-03 A patch from Takeharu KATO to update/fix SE-Linux support.
12 #include <selinux/selinux.h> /* for is_selinux_enabled() */
13 #include <selinux/get_context_list.h> /* for get_default_context() */
14 #include <selinux/flask.h> /* for security class definitions */
9e48045e »
2003-07-03 Patch from Russell Coker:
15 #endif
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
16
d6e81c77 »
2007-08-21 login: optional support for PAM
17 #if ENABLE_PAM
c6c2345c »
2007-08-22 login: fixes for PAM build
18 /* PAM may include <locale.h>. We may need to undefine bbox's stub define: */
19 #undef setlocale
20 /* For some obscure reason, PAM is not in pam/xxx, but in security/xxx.
21 * Apparently they like to confuse people. */
22 #include <security/pam_appl.h>
23 #include <security/pam_misc.h>
d6e81c77 »
2007-08-21 login: optional support for PAM
24 static const struct pam_conv conv = {
25 misc_conv,
26 NULL
27 };
28 #endif
29
942e4291 »
2006-09-08 login: previous commit comment was wrong :)
30 enum {
31 TIMEOUT = 60,
32 EMPTY_USERNAME_COUNT = 10,
33 USERNAME_SIZE = 32,
9a9edf20 »
2006-09-08 login: make it saner and smaller by ~0.5k.
34 TTYNAME_SIZE = 32,
942e4291 »
2006-09-08 login: previous commit comment was wrong :)
35 };
0fbff134 »
2002-06-22 Several login cleanups from vodz
36
c0415a98 »
2007-03-24 login: nuke nonblock() - we have it in libbb; -400 bytes bss.
37 static char* short_tty;
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
38
39 #if ENABLE_FEATURE_UTMP
40 /* vv Taken from tinylogin utmp.c vv */
41 /*
42 * read_or_build_utent - see if utmp file is correct for this process
43 *
44 * System V is very picky about the contents of the utmp file
45 * and requires that a slot for the current process exist.
46 * The utmp file is scanned for an entry with the same process
47 * ID. If no entry exists the process exits with a message.
48 *
49 * The "picky" flag is for network and other logins that may
50 * use special flags. It allows the pid checks to be overridden.
51 * This means that getty should never invoke login with any
52 * command line flags.
53 */
66fabdb6 »
2006-09-17 login: apply fixes + getopt_ulflag'ification by Bernhard
54
2ec94a7e »
2008-11-07 login: fix /etc/nologin handling (should prohibit non-root LOGINS,
55 static void read_or_build_utent(struct utmp *utptr, int run_by_root)
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
56 {
57 struct utmp *ut;
58 pid_t pid = getpid();
59
60 setutent();
61
62 /* First, try to find a valid utmp entry for this process. */
2ec94a7e »
2008-11-07 login: fix /etc/nologin handling (should prohibit non-root LOGINS,
63 /* If there is one, just use it. */
64 while ((ut = getutent()) != NULL)
65 if (ut->ut_pid == pid && ut->ut_line[0] && ut->ut_id[0]
66 && (ut->ut_type == LOGIN_PROCESS || ut->ut_type == USER_PROCESS)
67 ) {
68 *utptr = *ut; /* struct copy */
69 if (run_by_root) /* why only for root? */
70 memset(utptr->ut_host, 0, sizeof(utptr->ut_host));
71 return;
72 }
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
73
2ec94a7e »
2008-11-07 login: fix /etc/nologin handling (should prohibit non-root LOGINS,
74 // Why? Do we require non-root to exec login from another
75 // former login process (e.g. login shell)? Some login's have
76 // login shells as children, so it won't work...
77 // if (!run_by_root)
78 // bb_error_msg_and_die("no utmp entry found");
79
80 /* Otherwise create a new one. */
81 memset(utptr, 0, sizeof(*utptr));
82 utptr->ut_type = LOGIN_PROCESS;
83 utptr->ut_pid = pid;
84 strncpy(utptr->ut_line, short_tty, sizeof(utptr->ut_line));
85 /* This one is only 4 chars wide. Try to fit something
86 * remotely meaningful by skipping "tty"... */
87 strncpy(utptr->ut_id, short_tty + 3, sizeof(utptr->ut_id));
88 strncpy(utptr->ut_user, "LOGIN", sizeof(utptr->ut_user));
89 utptr->ut_tv.tv_sec = time(NULL);
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
90 }
91
92 /*
93 * write_utent - put a USER_PROCESS entry in the utmp file
94 *
95 * write_utent changes the type of the current utmp entry to
96 * USER_PROCESS. the wtmp file will be updated as well.
97 */
c0415a98 »
2007-03-24 login: nuke nonblock() - we have it in libbb; -400 bytes bss.
98 static void write_utent(struct utmp *utptr, const char *username)
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
99 {
c0415a98 »
2007-03-24 login: nuke nonblock() - we have it in libbb; -400 bytes bss.
100 utptr->ut_type = USER_PROCESS;
101 strncpy(utptr->ut_user, username, sizeof(utptr->ut_user));
62d85035 »
2008-06-01 - use ut_user rather than ut_name (Cristian Ionescu-Idbohrn)
102 utptr->ut_tv.tv_sec = time(NULL);
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
103 /* other fields already filled in by read_or_build_utent above */
104 setutent();
c0415a98 »
2007-03-24 login: nuke nonblock() - we have it in libbb; -400 bytes bss.
105 pututline(utptr);
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
106 endutent();
107 #if ENABLE_FEATURE_WTMP
108 if (access(bb_path_wtmp_file, R_OK|W_OK) == -1) {
109 close(creat(bb_path_wtmp_file, 0664));
110 }
c0415a98 »
2007-03-24 login: nuke nonblock() - we have it in libbb; -400 bytes bss.
111 updwtmp(bb_path_wtmp_file, utptr);
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
112 #endif
113 }
66fabdb6 »
2006-09-17 login: apply fixes + getopt_ulflag'ification by Bernhard
114 #else /* !ENABLE_FEATURE_UTMP */
2ec94a7e »
2008-11-07 login: fix /etc/nologin handling (should prohibit non-root LOGINS,
115 #define read_or_build_utent(utptr, run_by_root) ((void)0)
c0415a98 »
2007-03-24 login: nuke nonblock() - we have it in libbb; -400 bytes bss.
116 #define write_utent(utptr, username) ((void)0)
66fabdb6 »
2006-09-17 login: apply fixes + getopt_ulflag'ification by Bernhard
117 #endif /* !ENABLE_FEATURE_UTMP */
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
118
f312e326 »
2007-06-12 login: make /etc/nologin support configurable. -240 bytes if not sele…
119 #if ENABLE_FEATURE_NOLOGIN
68404f13 »
2008-03-17 *: add -Wunused-parameter; fix resulting breakage
120 static void die_if_nologin(void)
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
121 {
122 FILE *fp;
123 int c;
694b5141 »
2008-11-07 login: fix /etc/nologin support
124 int empty = 1;
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
125
694b5141 »
2008-11-07 login: fix /etc/nologin support
126 fp = fopen_for_read("/etc/nologin");
127 if (!fp) /* assuming it does not exist */
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
128 return;
129
694b5141 »
2008-11-07 login: fix /etc/nologin support
130 while ((c = getc(fp)) != EOF) {
131 if (c == '\n')
132 bb_putchar('\r');
133 bb_putchar(c);
134 empty = 0;
135 }
136 if (empty)
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
137 puts("\r\nSystem closed for routine maintenance\r");
694b5141 »
2008-11-07 login: fix /etc/nologin support
138
139 fclose(fp);
140 fflush(NULL);
141 /* Users say that they do need this prior to exit: */
142 tcdrain(STDOUT_FILENO);
636a1f85 »
2008-05-19 - use EXIT_{SUCCESS,FAILURE}. No object-code changes
143 exit(EXIT_FAILURE);
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
144 }
f312e326 »
2007-06-12 login: make /etc/nologin support configurable. -240 bytes if not sele…
145 #else
68404f13 »
2008-03-17 *: add -Wunused-parameter; fix resulting breakage
146 static ALWAYS_INLINE void die_if_nologin(void) {}
f312e326 »
2007-06-12 login: make /etc/nologin support configurable. -240 bytes if not sele…
147 #endif
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
148
d6e81c77 »
2007-08-21 login: optional support for PAM
149 #if ENABLE_FEATURE_SECURETTY && !ENABLE_PAM
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
150 static int check_securetty(void)
151 {
084266ed »
2008-07-26 fix several problems with config parser:
152 char *buf = (char*)"/etc/securetty"; /* any non-NULL is ok */
5415c856 »
2008-07-21 libbb: [x]fopen_for_{read,write} introduced and used.
153 parser_t *parser = config_open2("/etc/securetty", fopen_for_read);
084266ed »
2008-07-26 fix several problems with config parser:
154 while (config_read(parser, &buf, 1, 1, "# \t", PARSE_NORMAL)) {
155 if (strcmp(buf, short_tty) == 0)
156 break;
157 buf = NULL;
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
158 }
084266ed »
2008-07-26 fix several problems with config parser:
159 config_close(parser);
160 /* buf != NULL here if config file was not found, empty
161 * or line was found which equals short_tty */
162 return buf != NULL;
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
163 }
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
164 #else
f312e326 »
2007-06-12 login: make /etc/nologin support configurable. -240 bytes if not sele…
165 static ALWAYS_INLINE int check_securetty(void) { return 1; }
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
166 #endif
167
4eff8efa »
2009-02-02 login: cleanup work by Walter Harms. No logic changes.
168 #if ENABLE_SELINUX
169 static void initselinux(char *username, char *full_tty,
170 security_context_t *user_sid)
171 {
172 security_context_t old_tty_sid, new_tty_sid;
173
174 if (!is_selinux_enabled())
175 return;
176
177 if (get_default_context(username, NULL, user_sid)) {
178 bb_error_msg_and_die("cannot get SID for %s", username);
179 }
180 if (getfilecon(full_tty, &old_tty_sid) < 0) {
181 bb_perror_msg_and_die("getfilecon(%s) failed", full_tty);
182 }
183 if (security_compute_relabel(user_sid, old_tty_sid,
184 SECCLASS_CHR_FILE, &new_tty_sid) != 0) {
185 bb_perror_msg_and_die("security_change_sid(%s) failed", full_tty);
186 }
187 if (setfilecon(full_tty, new_tty_sid) != 0) {
188 bb_perror_msg_and_die("chsid(%s, %s) failed", full_tty, new_tty_sid);
189 }
190 }
191 #endif
192
193 #if ENABLE_LOGIN_SCRIPTS
194 static void run_login_script(struct passwd *pw, char *full_tty)
195 {
196 char *t_argv[2];
197
198 t_argv[0] = getenv("LOGIN_PRE_SUID_SCRIPT");
199 if (t_argv[0]) {
200 t_argv[1] = NULL;
201 xsetenv("LOGIN_TTY", full_tty);
202 xsetenv("LOGIN_USER", pw->pw_name);
203 xsetenv("LOGIN_UID", utoa(pw->pw_uid));
204 xsetenv("LOGIN_GID", utoa(pw->pw_gid));
205 xsetenv("LOGIN_SHELL", pw->pw_shell);
206 spawn_and_wait(t_argv); /* NOMMU-friendly */
207 unsetenv("LOGIN_TTY");
208 unsetenv("LOGIN_USER");
209 unsetenv("LOGIN_UID");
210 unsetenv("LOGIN_GID");
211 unsetenv("LOGIN_SHELL");
212 }
213 }
214 #else
215 void run_login_script(struct passwd *pw, char *full_tty);
216 #endif
217
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
218 static void get_username_or_die(char *buf, int size_buf)
219 {
220 int c, cntdown;
d8540f71 »
2007-06-14 init: make sure fd 0,1,2 are not closed, + related optimizations.
221
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
222 cntdown = EMPTY_USERNAME_COUNT;
d8540f71 »
2007-06-14 init: make sure fd 0,1,2 are not closed, + related optimizations.
223 prompt:
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
224 print_login_prompt();
d8540f71 »
2007-06-14 init: make sure fd 0,1,2 are not closed, + related optimizations.
225 /* skip whitespace */
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
226 do {
227 c = getchar();
636a1f85 »
2008-05-19 - use EXIT_{SUCCESS,FAILURE}. No object-code changes
228 if (c == EOF) exit(EXIT_FAILURE);
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
229 if (c == '\n') {
636a1f85 »
2008-05-19 - use EXIT_{SUCCESS,FAILURE}. No object-code changes
230 if (!--cntdown) exit(EXIT_FAILURE);
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
231 goto prompt;
232 }
233 } while (isspace(c));
234
235 *buf++ = c;
236 if (!fgets(buf, size_buf-2, stdin))
636a1f85 »
2008-05-19 - use EXIT_{SUCCESS,FAILURE}. No object-code changes
237 exit(EXIT_FAILURE);
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
238 if (!strchr(buf, '\n'))
636a1f85 »
2008-05-19 - use EXIT_{SUCCESS,FAILURE}. No object-code changes
239 exit(EXIT_FAILURE);
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
240 while (isgraph(*buf)) buf++;
241 *buf = '\0';
242 }
243
244 static void motd(void)
245 {
0de3c55b »
2007-04-12 login: remove setpgrp call (makes it work from shell prompt again).
246 int fd;
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
247
0de3c55b »
2007-04-12 login: remove setpgrp call (makes it work from shell prompt again).
248 fd = open(bb_path_motd_file, O_RDONLY);
52816302 »
2007-11-06 login: clear dangerous environment variables if started by non-root
249 if (fd >= 0) {
0de3c55b »
2007-04-12 login: remove setpgrp call (makes it work from shell prompt again).
250 fflush(stdout);
251 bb_copyfd_eof(fd, STDOUT_FILENO);
252 close(fd);
7ccf5cc0 »
2006-09-14 login: eliminate forward decls and #ifdefs
253 }
254 }
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
255
a60f84eb »
2008-07-05 *: rename ATTRIBUTE_XXX to just XXX.
256 static void alarm_handler(int sig UNUSED_PARAM)
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
257 {
2f50aa42 »
2006-09-08 login: use some ideas from util-linux's login.
258 /* This is the escape hatch! Poor serial line users and the like
259 * arrive here when their connection is broken.
260 * We don't want to block here */
c0415a98 »
2007-03-24 login: nuke nonblock() - we have it in libbb; -400 bytes bss.
261 ndelay_on(1);
0de3c55b »
2007-04-12 login: remove setpgrp call (makes it work from shell prompt again).
262 printf("\r\nLogin timed out after %d seconds\r\n", TIMEOUT);
400d8bb4 »
2008-02-24 less,klogd,syslogd,nc,tcpudp: exit on signal by killing itself, not e…
263 fflush(stdout);
52816302 »
2007-11-06 login: clear dangerous environment variables if started by non-root
264 /* unix API is brain damaged regarding O_NONBLOCK,
265 * we should undo it, or else we can affect other processes */
266 ndelay_off(1);
400d8bb4 »
2008-02-24 less,klogd,syslogd,nc,tcpudp: exit on signal by killing itself, not e…
267 _exit(EXIT_SUCCESS);
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
268 }
269
9b49a5ed »
2007-10-11 add -fvisibility=hidden to CC flags, mark XXX_main functions
270 int login_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
a60f84eb »
2008-07-05 *: rename ATTRIBUTE_XXX to just XXX.
271 int login_main(int argc UNUSED_PARAM, char **argv)
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
272 {
66fabdb6 »
2006-09-17 login: apply fixes + getopt_ulflag'ification by Bernhard
273 enum {
274 LOGIN_OPT_f = (1<<0),
275 LOGIN_OPT_h = (1<<1),
276 LOGIN_OPT_p = (1<<2),
277 };
d6e81c77 »
2007-08-21 login: optional support for PAM
278 char *fromhost;
0fbff134 »
2002-06-22 Several login cleanups from vodz
279 char username[USERNAME_SIZE];
dc4e75ef »
2003-09-02 move all "-/bin/sh" "/bin/sh" and "sh" to libbb/messages.c file as one
280 const char *tmp;
2ec94a7e »
2008-11-07 login: fix /etc/nologin handling (should prohibit non-root LOGINS,
281 int run_by_root;
67b23e60 »
2006-10-03 getopt_ulflags -> getopt32.
282 unsigned opt;
942e4291 »
2006-09-08 login: previous commit comment was wrong :)
283 int count = 0;
9a9edf20 »
2006-09-08 login: make it saner and smaller by ~0.5k.
284 struct passwd *pw;
cdf62770 »
2008-03-17 dos2unix: tiny shrink
285 char *opt_host = opt_host; /* for compiler */
286 char *opt_user = opt_user; /* for compiler */
c0415a98 »
2007-03-24 login: nuke nonblock() - we have it in libbb; -400 bytes bss.
287 char full_tty[TTYNAME_SIZE];
66fabdb6 »
2006-09-17 login: apply fixes + getopt_ulflag'ification by Bernhard
288 USE_SELINUX(security_context_t user_sid = NULL;)
c0415a98 »
2007-03-24 login: nuke nonblock() - we have it in libbb; -400 bytes bss.
289 USE_FEATURE_UTMP(struct utmp utent;)
a29a5e40 »
2007-11-07 login: fix PAM login (was unable to complete Kerberos login)
290 #if ENABLE_PAM
291 int pamret;
292 pam_handle_t *pamh;
293 const char *pamuser;
294 const char *failed_msg;
295 struct passwd pwdstruct;
296 char pwdbuf[256];
297 #endif
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
298
c0415a98 »
2007-03-24 login: nuke nonblock() - we have it in libbb; -400 bytes bss.
299 short_tty = full_tty;
942e4291 »
2006-09-08 login: previous commit comment was wrong :)
300 username[0] = '\0';
301 signal(SIGALRM, alarm_handler);
302 alarm(TIMEOUT);
c7bda1ce »
2004-03-15 Remove trailing whitespace. Update copyright to include 2004.
303
2ec94a7e »
2008-11-07 login: fix /etc/nologin handling (should prohibit non-root LOGINS,
304 /* More of suid paranoia if called by non-root: */
305 /* Clear dangerous stuff, set PATH */
306 run_by_root = !sanitize_env_if_suid();
c9ca0a32 »
2008-02-18 mount: recognize "dirsync" (closes bug 835)
307
0de3c55b »
2007-04-12 login: remove setpgrp call (makes it work from shell prompt again).
308 /* Mandatory paranoia for suid applet:
309 * ensure that fd# 0,1,2 are opened (at least to /dev/null)
310 * and any extra open fd's are closed.
311 * (The name of the function is misleading. Not daemonizing here.) */
312 bb_daemonize_or_rexec(DAEMON_ONLY_SANITIZE | DAEMON_CLOSE_EXTRA_FDS, NULL);
313
fe7cd642 »
2007-08-18 don't pass argc in getopt32, it's superfluous
314 opt = getopt32(argv, "f:h:p", &opt_user, &opt_host);
66fabdb6 »
2006-09-17 login: apply fixes + getopt_ulflag'ification by Bernhard
315 if (opt & LOGIN_OPT_f) {
2ec94a7e »
2008-11-07 login: fix /etc/nologin handling (should prohibit non-root LOGINS,
316 if (!run_by_root)
66fabdb6 »
2006-09-17 login: apply fixes + getopt_ulflag'ification by Bernhard
317 bb_error_msg_and_die("-f is for root only");
22f6dcb4 »
2006-09-26 login: fix getopt_ulflags fallout (wasn't taking username supplied by…
318 safe_strncpy(username, opt_user, sizeof(username));
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
319 }
1d42665b »
2008-03-17 *: shrink: use Vladimir's "o+" specifier instead of xatou(opt_param)
320 argv += optind;
321 if (argv[0]) /* user from command line (getty) */
322 safe_strncpy(username, argv[0], sizeof(username));
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
323
9a9edf20 »
2006-09-08 login: make it saner and smaller by ~0.5k.
324 /* Let's find out and memorize our tty */
942e4291 »
2006-09-08 login: previous commit comment was wrong :)
325 if (!isatty(0) || !isatty(1) || !isatty(2))
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
326 return EXIT_FAILURE; /* Must be a terminal */
9a9edf20 »
2006-09-08 login: make it saner and smaller by ~0.5k.
327 safe_strncpy(full_tty, "UNKNOWN", sizeof(full_tty));
4e12b1a2 »
2008-12-23 libbb: introduce and use xmalloc_ttyname (-32 in bss).
328 tmp = xmalloc_ttyname(STDIN_FILENO);
9a9edf20 »
2006-09-08 login: make it saner and smaller by ~0.5k.
329 if (tmp) {
330 safe_strncpy(full_tty, tmp, sizeof(full_tty));
331 if (strncmp(full_tty, "/dev/", 5) == 0)
332 short_tty = full_tty + 5;
333 }
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
334
2ec94a7e »
2008-11-07 login: fix /etc/nologin handling (should prohibit non-root LOGINS,
335 read_or_build_utent(&utent, run_by_root);
c7bda1ce »
2004-03-15 Remove trailing whitespace. Update copyright to include 2004.
336
cdf62770 »
2008-03-17 dos2unix: tiny shrink
337 if (opt & LOGIN_OPT_h) {
4eff8efa »
2009-02-02 login: cleanup work by Walter Harms. No logic changes.
338 USE_FEATURE_UTMP(safe_strncpy(utent.ut_host, opt_host, sizeof(utent.ut_host));)
d6e81c77 »
2007-08-21 login: optional support for PAM
339 fromhost = xasprintf(" on '%s' from '%s'", short_tty, opt_host);
4e12b1a2 »
2008-12-23 libbb: introduce and use xmalloc_ttyname (-32 in bss).
340 } else {
d6e81c77 »
2007-08-21 login: optional support for PAM
341 fromhost = xasprintf(" on '%s'", short_tty);
4e12b1a2 »
2008-12-23 libbb: introduce and use xmalloc_ttyname (-32 in bss).
342 }
c7bda1ce »
2004-03-15 Remove trailing whitespace. Update copyright to include 2004.
343
d6e81c77 »
2007-08-21 login: optional support for PAM
344 /* Was breaking "login <username>" from shell command line: */
345 /*bb_setpgrp();*/
0fbff134 »
2002-06-22 Several login cleanups from vodz
346
8f8f268c »
2006-10-03 bb_applet_name -> applet_name
347 openlog(applet_name, LOG_PID | LOG_CONS | LOG_NOWAIT, LOG_AUTH);
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
348
942e4291 »
2006-09-08 login: previous commit comment was wrong :)
349 while (1) {
6e1b62b1 »
2007-11-07 flush typeahead before prompt
350 /* flush away any type-ahead (as getty does) */
abbd3632 »
2007-11-08 xreadlink: code shrink
351 ioctl(0, TCFLSH, TCIFLUSH);
6e1b62b1 »
2007-11-07 flush typeahead before prompt
352
942e4291 »
2006-09-08 login: previous commit comment was wrong :)
353 if (!username[0])
9a9edf20 »
2006-09-08 login: make it saner and smaller by ~0.5k.
354 get_username_or_die(username, sizeof(username));
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
355
d6e81c77 »
2007-08-21 login: optional support for PAM
356 #if ENABLE_PAM
357 pamret = pam_start("login", username, &conv, &pamh);
358 if (pamret != PAM_SUCCESS) {
a29a5e40 »
2007-11-07 login: fix PAM login (was unable to complete Kerberos login)
359 failed_msg = "start";
d6e81c77 »
2007-08-21 login: optional support for PAM
360 goto pam_auth_failed;
361 }
362 /* set TTY (so things like securetty work) */
363 pamret = pam_set_item(pamh, PAM_TTY, short_tty);
364 if (pamret != PAM_SUCCESS) {
a29a5e40 »
2007-11-07 login: fix PAM login (was unable to complete Kerberos login)
365 failed_msg = "set_item(TTY)";
d6e81c77 »
2007-08-21 login: optional support for PAM
366 goto pam_auth_failed;
367 }
368 pamret = pam_authenticate(pamh, 0);
82f3b167 »
2007-09-03 login: do reject wrong passwords with PAM auth
369 if (pamret != PAM_SUCCESS) {
a29a5e40 »
2007-11-07 login: fix PAM login (was unable to complete Kerberos login)
370 failed_msg = "authenticate";
82f3b167 »
2007-09-03 login: do reject wrong passwords with PAM auth
371 goto pam_auth_failed;
372 /* TODO: or just "goto auth_failed"
373 * since user seems to enter wrong password
374 * (in this case pamret == 7)
375 */
376 }
377 /* check that the account is healthy */
378 pamret = pam_acct_mgmt(pamh, 0);
379 if (pamret != PAM_SUCCESS) {
a29a5e40 »
2007-11-07 login: fix PAM login (was unable to complete Kerberos login)
380 failed_msg = "acct_mgmt";
82f3b167 »
2007-09-03 login: do reject wrong passwords with PAM auth
381 goto pam_auth_failed;
382 }
383 /* read user back */
a29a5e40 »
2007-11-07 login: fix PAM login (was unable to complete Kerberos login)
384 pamuser = NULL;
385 /* gcc: "dereferencing type-punned pointer breaks aliasing rules..."
386 * thus we cast to (void*) */
387 if (pam_get_item(pamh, PAM_USER, (void*)&pamuser) != PAM_SUCCESS) {
388 failed_msg = "get_item(USER)";
389 goto pam_auth_failed;
d6e81c77 »
2007-08-21 login: optional support for PAM
390 }
a29a5e40 »
2007-11-07 login: fix PAM login (was unable to complete Kerberos login)
391 if (!pamuser || !pamuser[0])
392 goto auth_failed;
393 safe_strncpy(username, pamuser, sizeof(username));
394 /* Don't use "pw = getpwnam(username);",
395 * PAM is said to be capable of destroying static storage
396 * used by getpwnam(). We are using safe(r) function */
397 pw = NULL;
398 getpwnam_r(username, &pwdstruct, pwdbuf, sizeof(pwdbuf), &pw);
399 if (!pw)
400 goto auth_failed;
401 pamret = pam_open_session(pamh, 0);
402 if (pamret != PAM_SUCCESS) {
403 failed_msg = "open_session";
404 goto pam_auth_failed;
405 }
406 pamret = pam_setcred(pamh, PAM_ESTABLISH_CRED);
407 if (pamret != PAM_SUCCESS) {
408 failed_msg = "setcred";
409 goto pam_auth_failed;
410 }
411 break; /* success, continue login process */
412
d6e81c77 »
2007-08-21 login: optional support for PAM
413 pam_auth_failed:
a29a5e40 »
2007-11-07 login: fix PAM login (was unable to complete Kerberos login)
414 bb_error_msg("pam_%s call failed: %s (%d)", failed_msg,
415 pam_strerror(pamh, pamret), pamret);
d6e81c77 »
2007-08-21 login: optional support for PAM
416 safe_strncpy(username, "UNKNOWN", sizeof(username));
417 #else /* not PAM */
942e4291 »
2006-09-08 login: previous commit comment was wrong :)
418 pw = getpwnam(username);
419 if (!pw) {
65e14b45 »
2007-06-08 login: ask passwords even for wrong usernames.
420 strcpy(username, "UNKNOWN");
421 goto fake_it;
9a9edf20 »
2006-09-08 login: make it saner and smaller by ~0.5k.
422 }
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
423
9a9edf20 »
2006-09-08 login: make it saner and smaller by ~0.5k.
424 if (pw->pw_passwd[0] == '!' || pw->pw_passwd[0] == '*')
425 goto auth_failed;
c7bda1ce »
2004-03-15 Remove trailing whitespace. Update copyright to include 2004.
426
66fabdb6 »
2006-09-17 login: apply fixes + getopt_ulflag'ification by Bernhard
427 if (opt & LOGIN_OPT_f)
9a9edf20 »
2006-09-08 login: make it saner and smaller by ~0.5k.
428 break; /* -f USER: success without asking passwd */
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
429
9a9edf20 »
2006-09-08 login: make it saner and smaller by ~0.5k.
430 if (pw->pw_uid == 0 && !check_securetty())
431 goto auth_failed;
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
432
433 /* Don't check the password if password entry is empty (!) */
942e4291 »
2006-09-08 login: previous commit comment was wrong :)
434 if (!pw->pw_passwd[0])
66fabdb6 »
2006-09-17 login: apply fixes + getopt_ulflag'ification by Bernhard
435 break;
65e14b45 »
2007-06-08 login: ask passwords even for wrong usernames.
436 fake_it:
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
437 /* authorization takes place here */
942e4291 »
2006-09-08 login: previous commit comment was wrong :)
438 if (correct_password(pw))
66fabdb6 »
2006-09-17 login: apply fixes + getopt_ulflag'ification by Bernhard
439 break;
d6e81c77 »
2007-08-21 login: optional support for PAM
440 #endif /* ENABLE_PAM */
0de3c55b »
2007-04-12 login: remove setpgrp call (makes it work from shell prompt again).
441 auth_failed:
66fabdb6 »
2006-09-17 login: apply fixes + getopt_ulflag'ification by Bernhard
442 opt &= ~LOGIN_OPT_f;
84cb7673 »
2006-01-06 patch from tito: consolidate delay functions as bb_do_delay()
443 bb_do_delay(FAIL_DELAY);
82f3b167 »
2007-09-03 login: do reject wrong passwords with PAM auth
444 /* TODO: doesn't sound like correct English phrase to me */
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
445 puts("Login incorrect");
942e4291 »
2006-09-08 login: previous commit comment was wrong :)
446 if (++count == 3) {
89f0b348 »
2006-11-18 rodata cleanup. "unable to" == "cannot". -300 bytes
447 syslog(LOG_WARNING, "invalid password for '%s'%s",
9a9edf20 »
2006-09-08 login: make it saner and smaller by ~0.5k.
448 username, fromhost);
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
449 return EXIT_FAILURE;
942e4291 »
2006-09-08 login: previous commit comment was wrong :)
450 }
9a9edf20 »
2006-09-08 login: make it saner and smaller by ~0.5k.
451 username[0] = '\0';
2ec94a7e »
2008-11-07 login: fix /etc/nologin handling (should prohibit non-root LOGINS,
452 } /* while (1) */
c7bda1ce »
2004-03-15 Remove trailing whitespace. Update copyright to include 2004.
453
942e4291 »
2006-09-08 login: previous commit comment was wrong :)
454 alarm(0);
2ec94a7e »
2008-11-07 login: fix /etc/nologin handling (should prohibit non-root LOGINS,
455 /* We can ignore /etc/nologin if we are logging in as root,
456 * it doesn't matter whether we are run by root or not */
457 if (pw->pw_uid != 0)
68404f13 »
2008-03-17 *: add -Wunused-parameter; fix resulting breakage
458 die_if_nologin();
60158cb9 »
2005-05-03 A patch from Takeharu KATO to update/fix SE-Linux support.
459
c0415a98 »
2007-03-24 login: nuke nonblock() - we have it in libbb; -400 bytes bss.
460 write_utent(&utent, username);
60158cb9 »
2005-05-03 A patch from Takeharu KATO to update/fix SE-Linux support.
461
4eff8efa »
2009-02-02 login: cleanup work by Walter Harms. No logic changes.
462 USE_SELINUX(initselinux(username, full_tty, &user_sid));
9e48045e »
2003-07-03 Patch from Russell Coker:
463
2f50aa42 »
2006-09-08 login: use some ideas from util-linux's login.
464 /* Try these, but don't complain if they fail.
465 * _f_chown is safe wrt race t=ttyname(0);...;chown(t); */
466 fchown(0, pw->pw_uid, pw->pw_gid);
467 fchmod(0, 0600);
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
468
52816302 »
2007-11-06 login: clear dangerous environment variables if started by non-root
469 /* We trust environment only if we run by root */
4eff8efa »
2009-02-02 login: cleanup work by Walter Harms. No logic changes.
470 if (ENABLE_LOGIN_SCRIPTS && run_by_root)
471 run_login_script(pw, full_tty);
2e502914 »
2006-09-08 login: style fixes
472
942e4291 »
2006-09-08 login: previous commit comment was wrong :)
473 change_identity(pw);
474 tmp = pw->pw_shell;
475 if (!tmp || !*tmp)
dc4e75ef »
2003-09-02 move all "-/bin/sh" "/bin/sh" and "sh" to libbb/messages.c file as one
476 tmp = DEFAULT_SHELL;
cdf62770 »
2008-03-17 dos2unix: tiny shrink
477 /* setup_environment params: shell, clear_env, change_env, pw */
478 setup_environment(tmp, !(opt & LOGIN_OPT_p), 1, pw);
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
479
942e4291 »
2006-09-08 login: previous commit comment was wrong :)
480 motd();
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
481
942e4291 »
2006-09-08 login: previous commit comment was wrong :)
482 if (pw->pw_uid == 0)
2f50aa42 »
2006-09-08 login: use some ideas from util-linux's login.
483 syslog(LOG_INFO, "root login%s", fromhost);
4eff8efa »
2009-02-02 login: cleanup work by Walter Harms. No logic changes.
484
d1f8c1c1 »
2006-03-27 From Jan Kiszka: This patch fixes the security labelling of the login…
485 /* well, a simple setexeccon() here would do the job as well,
486 * but let's play the game for now */
4eff8efa »
2009-02-02 login: cleanup work by Walter Harms. No logic changes.
487 USE_SELINUX(set_current_security_context(user_sid);)
6ae8079e »
2006-10-31 login: re-enable Ctrl-^C before execing shell.
488
489 // util-linux login also does:
490 // /* start new session */
491 // setsid();
492 // /* TIOCSCTTY: steal tty from other process group */
493 // if (ioctl(0, TIOCSCTTY, 1)) error_msg...
0de3c55b »
2007-04-12 login: remove setpgrp call (makes it work from shell prompt again).
494 // BBox login used to do this (see above):
495 // bb_setpgrp();
496 // If this stuff is really needed, add it and explain why!
6ae8079e »
2006-10-31 login: re-enable Ctrl-^C before execing shell.
497
2ec94a7e »
2008-11-07 login: fix /etc/nologin handling (should prohibit non-root LOGINS,
498 /* Set signals to defaults */
3fa36e23 »
2008-11-09 *: a bit of code shrink
499 /* Non-ignored signals revert to SIG_DFL on exec anyway */
500 /*signal(SIGALRM, SIG_DFL);*/
2ec94a7e »
2008-11-07 login: fix /etc/nologin handling (should prohibit non-root LOGINS,
501
8c76487f »
2006-10-31 login: add big fat comment about SIGINT - prevent
502 /* Is this correct? This way user can ctrl-c out of /etc/profile,
503 * potentially creating security breach (tested with bash 3.0).
504 * But without this, bash 3.0 will not enable ctrl-c either.
505 * Maybe bash is buggy?
506 * Need to find out what standards say about /bin/login -
2ec94a7e »
2008-11-07 login: fix /etc/nologin handling (should prohibit non-root LOGINS,
507 * should we leave SIGINT etc enabled or disabled? */
6ae8079e »
2006-10-31 login: re-enable Ctrl-^C before execing shell.
508 signal(SIGINT, SIG_DFL);
509
a2f61012 »
2007-09-10 setup_environment: code shrink
510 /* Exec login shell with no additional parameters */
511 run_shell(tmp, 1, NULL, NULL);
c7bda1ce »
2004-03-15 Remove trailing whitespace. Update copyright to include 2004.
512
a2f61012 »
2007-09-10 setup_environment: code shrink
513 /* return EXIT_FAILURE; - not reached */
1fca5587 »
2002-06-04 Bigger patch for (partial) tinylogin integration
514 }
Something went wrong with that request. Please try again.