]>
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(const char *pass
)
170 if (pass
[0] == 0) return 1;
172 if (pass
[0] != '$') goto check_des
;
175 * up to 4 bytes for the signature e.g. $1$
177 for(s
= pass
+1; *s
&& *s
!= '$'; s
++)
179 if (*s
++ != '$') return 0;
180 if ((off
= (off_t
)(s
-pass
)) > 4 || off
< 3) return 0;
182 memset(id
, '\0', sizeof(id
));
183 strncpy(id
, pass
, off
);
186 * up to 16 bytes for the salt
188 for(; *s
&& *s
!= '$'; s
++)
190 if (*s
++ != '$') return 0;
191 if ((off_t
)(s
-pass
) > 16) return 0;
195 * the MD5 hash (128 bits or 16 bytes) encoded in base64 = 22 bytes
197 if ((strcmp(id
, "$1$") == 0) && (len
< 22 || len
> 24)) return 0;
200 * the SHA-256 hash 43 bytes
202 if ((strcmp(id
, "$5$") == 0) && (len
< 42 || len
> 44)) return 0;
205 * the SHA-512 hash 86 bytes
207 if ((strcmp(id
, "$6$") == 0) && (len
< 85 || len
> 87)) return 0;
216 if (strlen(pass
) != 13) return 0;
217 for (s
= pass
; *s
; s
++) {
218 if ((*s
< '0' || *s
> '9') &&
219 (*s
< 'a' || *s
> 'z') &&
220 (*s
< 'A' || *s
> 'Z') &&
221 *s
!= '.' && *s
!= '/') return 0;
228 * Set a variable if the value is not NULL.
231 void set(char **var
, char *val
)
237 * Get the root password entry.
240 struct passwd
*getrootpwent(int try_manually
)
242 static struct passwd pwd
;
246 static char line
[256];
247 static char sline
[256];
251 * First, we try to get the password the standard
252 * way using normal library calls.
254 if ((pw
= getpwnam("root")) &&
255 !strcmp(pw
->pw_passwd
, "x") &&
256 (spw
= getspnam("root")))
257 pw
->pw_passwd
= spw
->sp_pwdp
;
258 if (pw
|| !try_manually
) return pw
;
261 * If we come here, we could not retrieve the root
262 * password through library calls and we try to
263 * read the password and shadow files manually.
265 pwd
.pw_name
= "root";
267 pwd
.pw_gecos
= "Super User";
273 if ((fp
= fopen(F_PASSWD
, "r")) == NULL
) {
279 * Find root in the password file.
281 while((p
= fgets(line
, 256, fp
)) != NULL
) {
282 if (strncmp(line
, "root:", 5) != 0)
285 set(&pwd
.pw_passwd
, strsep(&p
, ":"));
286 (void)strsep(&p
, ":");
287 (void)strsep(&p
, ":");
288 set(&pwd
.pw_gecos
, strsep(&p
, ":"));
289 set(&pwd
.pw_dir
, strsep(&p
, ":"));
290 set(&pwd
.pw_shell
, strsep(&p
, "\n"));
297 * If the encrypted password is valid
298 * or not found, return.
301 fprintf(stderr
, "%s: no entry for root\n", F_PASSWD
);
304 if (valid(pwd
.pw_passwd
)) return &pwd
;
307 * The password is invalid. If there is a
308 * shadow password, try it.
310 strcpy(pwd
.pw_passwd
, "");
311 if ((fp
= fopen(F_SHADOW
, "r")) == NULL
) {
312 fprintf(stderr
, "%s: root password garbled\n", F_PASSWD
);
315 while((p
= fgets(sline
, 256, fp
)) != NULL
) {
316 if (strncmp(sline
, "root:", 5) != 0)
319 set(&pwd
.pw_passwd
, strsep(&p
, ":"));
325 * If the password is still invalid,
326 * NULL it, and return.
329 fprintf(stderr
, "%s: no entry for root\n", F_SHADOW
);
330 strcpy(pwd
.pw_passwd
, "");
332 if (!valid(pwd
.pw_passwd
)) {
333 fprintf(stderr
, "%s: root password garbled\n", F_SHADOW
);
334 strcpy(pwd
.pw_passwd
, ""); }
339 * Ask for the password. Note that there is no
340 * default timeout as we normally skip this during boot.
343 char *getpasswd(char *crypted
)
346 struct termios old
, tty
;
347 static char pass
[128];
350 #if defined(USE_ONELINE)
352 printf("Give root password for login: ");
354 printf("Press enter for login: ");
357 printf("Give root password for maintenance\n");
359 printf("Press enter for maintenance");
360 printf("(or type Control-D to continue): ");
366 tty
.c_iflag
&= ~(IUCLC
|IXON
|IXOFF
|IXANY
);
367 tty
.c_lflag
&= ~(ECHO
|ECHOE
|ECHOK
|ECHONL
|TOSTOP
);
368 tcsetattr(0, TCSANOW
, &tty
);
370 pass
[sizeof(pass
) - 1] = 0;
372 sa
.sa_handler
= alrm_handler
;
374 sigaction(SIGALRM
, &sa
, NULL
);
375 if (timeout
) alarm(timeout
);
377 if (read(0, pass
, sizeof(pass
) - 1) <= 0)
380 for(i
= 0; i
< (int)sizeof(pass
) && pass
[i
]; i
++)
381 if (pass
[i
] == '\r' || pass
[i
] == '\n') {
387 tcsetattr(0, TCSANOW
, &old
);
394 * Password was OK, execute a shell.
397 void sushell(struct passwd
*pwd
)
405 * Set directory and shell.
407 (void)chdir(pwd
->pw_dir
);
408 if ((p
= getenv("SUSHELL")) != NULL
)
410 else if ((p
= getenv("sushell")) != NULL
)
413 if (pwd
->pw_shell
[0])
414 sushell
= pwd
->pw_shell
;
418 if ((p
= strrchr(sushell
, '/')) == NULL
)
422 snprintf(shell
, sizeof(shell
), profile
? "-%s" : "%s", p
);
425 * Set some important environment variables.
427 getcwd(home
, sizeof(home
));
428 setenv("HOME", home
, 1);
429 setenv("LOGNAME", "root", 1);
430 setenv("USER", "root", 1);
432 setenv("SHLVL","0",1);
435 * Try to execute a shell.
437 setenv("SHELL", sushell
, 1);
438 signal(SIGINT
, saved_sigint
);
439 signal(SIGTSTP
, saved_sigtstp
);
440 signal(SIGQUIT
, saved_sigquit
);
442 if (is_selinux_enabled() > 0) {
443 security_context_t scon
=NULL
;
446 if (getseuserbyname("root", &seuser
, &level
) == 0)
447 if (get_default_context_with_level(seuser
, level
, 0, &scon
) > 0) {
448 if (setexeccon(scon
) != 0)
449 fprintf(stderr
, "setexeccon faile\n");
456 execl(sushell
, shell
, NULL
);
459 setenv("SHELL", BINSH
, 1);
460 execl(BINSH
, profile
? "-sh" : "sh", NULL
);
463 /* Fall back to staticly linked shell if both the users shell
464 and /bin/sh failed to execute. */
465 setenv("SHELL", STATICSH
, 1);
466 execl(STATICSH
, STATICSH
, NULL
);
473 fprintf(stderr
, "Usage: sulogin [-e] [-p] [-t timeout] [tty device]\n");
476 int main(int argc
, char **argv
)
483 pid_t pid
, pgrp
, ppgrp
, ttypgrp
;
486 * See if we have a timeout flag.
489 while((c
= getopt(argc
, argv
, "ept:")) != EOF
) switch(c
) {
491 timeout
= atoi(optarg
);
505 if (geteuid() != 0) {
506 fprintf(stderr
, "sulogin: only root can run sulogin.\n");
511 * See if we need to open an other tty device.
513 saved_sigint
= signal(SIGINT
, SIG_IGN
);
514 saved_sigtstp
= signal(SIGQUIT
, SIG_IGN
);
515 saved_sigquit
= signal(SIGTSTP
, SIG_IGN
);
516 if (optind
< argc
) tty
= argv
[optind
];
518 if (tty
|| (tty
= getenv("CONSOLE"))) {
520 if ((fd
= open(tty
, O_RDWR
)) < 0) {
526 fprintf(stderr
, "%s: not a tty\n", tty
);
531 * Only go through this trouble if the new
532 * tty doesn't fall in this process group.
536 ppgrp
= getpgid(getppid());
537 ttypgrp
= tcgetpgrp(fd
);
539 if (pgrp
!= ttypgrp
&& ppgrp
!= ttypgrp
) {
540 if (pid
!= getsid(0)) {
541 if (pid
== getpgid(0))
542 setpgid(0, getpgid(getppid()));
546 signal(SIGHUP
, SIG_IGN
);
548 ioctl(0, TIOCNOTTY
, (char *)1);
549 signal(SIGHUP
, SIG_DFL
);
555 if ((fd
= open(tty
, O_RDWR
|O_NOCTTY
)) < 0) {
558 ioctl(0, TIOCSCTTY
, (char *)1);
559 tcsetpgrp(fd
, ppgrp
);
570 } else if (getpid() == 1) {
571 /* We are init. We hence need to set a session anyway */
573 if (ioctl(0, TIOCSCTTY
, (char *)1))
574 perror("ioctl(TIOCSCTTY)");
577 #if defined(SANE_TIO) && (SANE_TIO == 1)
582 * Get the root password.
584 if ((pwd
= getrootpwent(opt_e
)) == NULL
) {
585 fprintf(stderr
, "sulogin: cannot open password database!\n");
590 * Ask for the password.
593 if ((p
= getpasswd(pwd
->pw_passwd
)) == NULL
) break;
594 if (pwd
->pw_passwd
[0] == 0 ||
595 strcmp(crypt(p
, pwd
->pw_passwd
), pwd
->pw_passwd
) == 0)
597 saved_sigquit
= signal(SIGQUIT
, SIG_IGN
);
598 saved_sigtstp
= signal(SIGTSTP
, SIG_IGN
);
599 saved_sigint
= signal(SIGINT
, SIG_IGN
);
600 printf("Login incorrect.\n");
604 * User pressed Control-D.