]>
git.wh0rd.org - sysvinit.git/blob - src/sulogin.c
2 * sulogin This program gives Linux machines a reasonable
3 * secure way to boot single user. It forces the
4 * user to supply the root password before a
7 * If there is a shadow password file and the
8 * encrypted root password is "x" the shadow
9 * password will be used.
11 * Version: @(#)sulogin 2.85-3 23-Apr-2003 miquels@cistron.nl
13 * Copyright (C) 1998-2003 Miquel van Smoorenburg.
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
25 * You should have received a copy of the GNU General Public License
26 * along with this program; if not, write to the Free Software
27 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
31 #include <sys/types.h>
42 #include <sys/ttydefaults.h>
44 #include <sys/ioctl.h>
45 #if defined(__GLIBC__)
50 # include <selinux/selinux.h>
51 # include <selinux/get_context_list.h>
57 #define F_PASSWD "/etc/passwd"
58 #define F_SHADOW "/etc/shadow"
59 #define BINSH "/bin/sh"
60 #define STATICSH "/bin/sash"
62 char *Version
= "@(#)sulogin 2.85-3 23-Apr-2003 miquels@cistron.nl";
67 static void (*saved_sigint
) = SIG_DFL
;
68 static void (*saved_sigtstp
) = SIG_DFL
;
69 static void (*saved_sigquit
) = SIG_DFL
;
75 #if defined(SANE_TIO) && (SANE_TIO == 1)
77 * Fix the tty modes and set reasonable defaults.
78 * (I'm not sure if this is needed under Linux, but..)
86 /* Skip serial console */
87 if (ioctl (0, TIOCMGET
, (char*)&serial
) == 0)
94 /* Use defaults of <sys/ttydefaults.h> for base settings */
95 tty
.c_iflag
|= TTYDEF_IFLAG
;
96 tty
.c_oflag
|= TTYDEF_OFLAG
;
97 tty
.c_lflag
|= TTYDEF_LFLAG
;
98 tty
.c_cflag
|= (TTYDEF_SPEED
| TTYDEF_CFLAG
);
100 /* Sane setting, allow eight bit characters, no carriage return delay
101 * the same result as `stty sane cr0 pass8'
103 tty
.c_iflag
|= (BRKINT
| ICRNL
| IMAXBEL
);
104 #ifdef IUTF8 /* Not defined on FreeBSD */
105 tty
.c_iflag
|= IUTF8
;
107 tty
.c_iflag
&= ~(IGNBRK
| INLCR
| IGNCR
| IXOFF
| IUCLC
| IXANY
| ISTRIP
);
108 tty
.c_oflag
|= (OPOST
| ONLCR
| NL0
| CR0
| TAB0
| BS0
| VT0
| FF0
);
109 tty
.c_oflag
&= ~(OLCUC
| OCRNL
| ONOCR
| ONLRET
| OFILL
| OFDEL
|\
110 NLDLY
|CRDLY
|TABDLY
|BSDLY
|VTDLY
|FFDLY
);
111 tty
.c_lflag
|= (ISIG
| ICANON
| IEXTEN
| ECHO
|ECHOE
|ECHOK
|ECHOCTL
|ECHOKE
);
112 tty
.c_lflag
&= ~(ECHONL
| NOFLSH
| XCASE
| TOSTOP
| ECHOPRT
);
113 tty
.c_cflag
|= (CREAD
| CS8
| B9600
);
114 tty
.c_cflag
&= ~(PARENB
);
116 /* VTIME and VMIN can overlap with VEOF and VEOL since they are
117 * only used for non-canonical mode. We just set the at the
118 * beginning, so nothing bad should happen.
122 tty
.c_cc
[VINTR
] = CINTR
;
123 tty
.c_cc
[VQUIT
] = CQUIT
;
124 tty
.c_cc
[VERASE
] = CERASE
; /* ASCII DEL (0177) */
125 tty
.c_cc
[VKILL
] = CKILL
;
126 tty
.c_cc
[VEOF
] = CEOF
;
127 tty
.c_cc
[VSWTC
] = _POSIX_VDISABLE
;
128 tty
.c_cc
[VSTART
] = CSTART
;
129 tty
.c_cc
[VSTOP
] = CSTOP
;
130 tty
.c_cc
[VSUSP
] = CSUSP
;
131 tty
.c_cc
[VEOL
] = _POSIX_VDISABLE
;
132 tty
.c_cc
[VREPRINT
] = CREPRINT
;
133 tty
.c_cc
[VDISCARD
] = CDISCARD
;
134 tty
.c_cc
[VWERASE
] = CWERASE
;
135 tty
.c_cc
[VLNEXT
] = CLNEXT
;
136 tty
.c_cc
[VEOL2
] = _POSIX_VDISABLE
;
138 tcsetattr(0, TCSANOW
, &tty
);
150 void alrm_handler(int sig
__attribute__((unused
)))
152 void alrm_handler(int sig
)
158 * See if an encrypted password is valid. The encrypted
159 * password is checked for traditional-style DES and
160 * FreeBSD-style MD5 encryption.
163 int valid(char *pass
)
168 if (pass
[0] == 0) return 1;
171 * 3 bytes for the signature $1$
172 * up to 8 bytes for the salt
174 * the MD5 hash (128 bits or 16 bytes) encoded in base64 = 22 bytes
176 if (strncmp(pass
, "$1$", 3) == 0) {
177 for(s
= pass
+ 3; *s
&& *s
!= '$'; s
++)
179 if (*s
++ != '$') return 0;
181 if (len
< 22 || len
> 24) return 0;
187 if (strlen(pass
) != 13) return 0;
188 for (s
= pass
; *s
; s
++) {
189 if ((*s
< '0' || *s
> '9') &&
190 (*s
< 'a' || *s
> 'z') &&
191 (*s
< 'A' || *s
> 'Z') &&
192 *s
!= '.' && *s
!= '/') return 0;
199 * Set a variable if the value is not NULL.
202 void set(char **var
, char *val
)
208 * Get the root password entry.
211 struct passwd
*getrootpwent(int try_manually
)
213 static struct passwd pwd
;
217 static char line
[256];
218 static char sline
[256];
222 * First, we try to get the password the standard
223 * way using normal library calls.
225 if ((pw
= getpwnam("root")) &&
226 !strcmp(pw
->pw_passwd
, "x") &&
227 (spw
= getspnam("root")))
228 pw
->pw_passwd
= spw
->sp_pwdp
;
229 if (pw
|| !try_manually
) return pw
;
232 * If we come here, we could not retrieve the root
233 * password through library calls and we try to
234 * read the password and shadow files manually.
236 pwd
.pw_name
= "root";
238 pwd
.pw_gecos
= "Super User";
244 if ((fp
= fopen(F_PASSWD
, "r")) == NULL
) {
250 * Find root in the password file.
252 while((p
= fgets(line
, 256, fp
)) != NULL
) {
253 if (strncmp(line
, "root:", 5) != 0)
256 set(&pwd
.pw_passwd
, strsep(&p
, ":"));
257 (void)strsep(&p
, ":");
258 (void)strsep(&p
, ":");
259 set(&pwd
.pw_gecos
, strsep(&p
, ":"));
260 set(&pwd
.pw_dir
, strsep(&p
, ":"));
261 set(&pwd
.pw_shell
, strsep(&p
, "\n"));
268 * If the encrypted password is valid
269 * or not found, return.
272 fprintf(stderr
, "%s: no entry for root\n", F_PASSWD
);
275 if (valid(pwd
.pw_passwd
)) return &pwd
;
278 * The password is invalid. If there is a
279 * shadow password, try it.
281 strcpy(pwd
.pw_passwd
, "");
282 if ((fp
= fopen(F_SHADOW
, "r")) == NULL
) {
283 fprintf(stderr
, "%s: root password garbled\n", F_PASSWD
);
286 while((p
= fgets(sline
, 256, fp
)) != NULL
) {
287 if (strncmp(sline
, "root:", 5) != 0)
290 set(&pwd
.pw_passwd
, strsep(&p
, ":"));
296 * If the password is still invalid,
297 * NULL it, and return.
300 fprintf(stderr
, "%s: no entry for root\n", F_SHADOW
);
301 strcpy(pwd
.pw_passwd
, "");
303 if (!valid(pwd
.pw_passwd
)) {
304 fprintf(stderr
, "%s: root password garbled\n", F_SHADOW
);
305 strcpy(pwd
.pw_passwd
, ""); }
310 * Ask for the password. Note that there is no
311 * default timeout as we normally skip this during boot.
314 char *getpasswd(char *crypted
)
317 struct termios old
, tty
;
318 static char pass
[128];
323 printf("Give root password for login: ");
325 printf("Press enter for login: ");
330 tty
.c_iflag
&= ~(IUCLC
|IXON
|IXOFF
|IXANY
);
331 tty
.c_lflag
&= ~(ECHO
|ECHOE
|ECHOK
|ECHONL
|TOSTOP
);
332 tcsetattr(0, TCSANOW
, &tty
);
334 pass
[sizeof(pass
) - 1] = 0;
336 sa
.sa_handler
= alrm_handler
;
338 sigaction(SIGALRM
, &sa
, NULL
);
339 if (timeout
) alarm(timeout
);
341 if (read(0, pass
, sizeof(pass
) - 1) <= 0)
344 for(i
= 0; i
< (int)sizeof(pass
) && pass
[i
]; i
++)
345 if (pass
[i
] == '\r' || pass
[i
] == '\n') {
351 tcsetattr(0, TCSANOW
, &old
);
358 * Password was OK, execute a shell.
361 void sushell(struct passwd
*pwd
)
369 * Set directory and shell.
371 (void)chdir(pwd
->pw_dir
);
372 if ((p
= getenv("SUSHELL")) != NULL
)
374 else if ((p
= getenv("sushell")) != NULL
)
377 if (pwd
->pw_shell
[0])
378 sushell
= pwd
->pw_shell
;
382 if ((p
= strrchr(sushell
, '/')) == NULL
)
386 snprintf(shell
, sizeof(shell
), profile
? "-%s" : "%s", p
);
389 * Set some important environment variables.
391 getcwd(home
, sizeof(home
));
392 setenv("HOME", home
, 1);
393 setenv("LOGNAME", "root", 1);
394 setenv("USER", "root", 1);
396 setenv("SHLVL","0",1);
399 * Try to execute a shell.
401 setenv("SHELL", sushell
, 1);
402 signal(SIGINT
, saved_sigint
);
403 signal(SIGTSTP
, saved_sigtstp
);
404 signal(SIGQUIT
, saved_sigquit
);
406 if (is_selinux_enabled
> 0) {
407 security_context_t scon
=NULL
;
410 if (getseuserbyname("root", &seuser
, &level
) == 0)
411 if (get_default_context_with_level(seuser
, level
, 0, &scon
) > 0) {
412 if (setexeccon(scon
) != 0)
413 fprintf(stderr
, "setexeccon faile\n");
420 execl(sushell
, shell
, NULL
);
423 setenv("SHELL", BINSH
, 1);
424 execl(BINSH
, profile
? "-sh" : "sh", NULL
);
427 /* Fall back to staticly linked shell if both the users shell
428 and /bin/sh failed to execute. */
429 setenv("SHELL", STATICSH
, 1);
430 execl(STATICSH
, STATICSH
, NULL
);
437 fprintf(stderr
, "Usage: sulogin [-e] [-p] [-t timeout] [tty device]\n");
440 int main(int argc
, char **argv
)
447 pid_t pid
, pgrp
, ppgrp
, ttypgrp
;
450 * See if we have a timeout flag.
453 while((c
= getopt(argc
, argv
, "ept:")) != EOF
) switch(c
) {
455 timeout
= atoi(optarg
);
469 if (geteuid() != 0) {
470 fprintf(stderr
, "sulogin: only root can run sulogin.\n");
475 * See if we need to open an other tty device.
477 saved_sigint
= signal(SIGINT
, SIG_IGN
);
478 saved_sigtstp
= signal(SIGQUIT
, SIG_IGN
);
479 saved_sigquit
= signal(SIGTSTP
, SIG_IGN
);
480 if (optind
< argc
) tty
= argv
[optind
];
482 if (tty
|| (tty
= getenv("CONSOLE"))) {
484 if ((fd
= open(tty
, O_RDWR
)) < 0) {
490 fprintf(stderr
, "%s: not a tty\n", tty
);
495 * Only go through this trouble if the new
496 * tty doesn't fall in this process group.
500 ppgrp
= getpgid(getppid());
501 ttypgrp
= tcgetpgrp(fd
);
503 if (pgrp
!= ttypgrp
&& ppgrp
!= ttypgrp
) {
504 if (pid
!= getsid(0)) {
505 if (pid
== getpgid(0))
506 setpgid(0, getpgid(getppid()));
510 signal(SIGHUP
, SIG_IGN
);
512 ioctl(0, TIOCNOTTY
, (char *)1);
513 signal(SIGHUP
, SIG_DFL
);
519 if ((fd
= open(tty
, O_RDWR
)) < 0) {
522 ioctl(0, TIOCSCTTY
, (char *)1);
523 tcsetpgrp(fd
, ppgrp
);
534 } else if (getpid() == 1) {
535 /* We are init. We hence need to set a session anyway */
537 if (ioctl(0, TIOCSCTTY
, (char *)1))
538 perror("ioctl(TIOCSCTTY)");
541 #if defined(SANE_TIO) && (SANE_TIO == 1)
546 * Get the root password.
548 if ((pwd
= getrootpwent(opt_e
)) == NULL
) {
549 fprintf(stderr
, "sulogin: cannot open password database!\n");
554 * Ask for the password.
557 if ((p
= getpasswd(pwd
->pw_passwd
)) == NULL
) break;
558 if (pwd
->pw_passwd
[0] == 0 ||
559 strcmp(crypt(p
, pwd
->pw_passwd
), pwd
->pw_passwd
) == 0)
561 saved_sigquit
= signal(SIGQUIT
, SIG_IGN
);
562 saved_sigtstp
= signal(SIGTSTP
, SIG_IGN
);
563 saved_sigint
= signal(SIGINT
, SIG_IGN
);
564 printf("Login incorrect.\n");
568 * User pressed Control-D.