]>
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 tty
.c_iflag
&= ~(IGNBRK
| INLCR
| IGNCR
| IXOFF
| IUCLC
| IXANY
| ISTRIP
);
105 tty
.c_oflag
|= (OPOST
| ONLCR
| NL0
| CR0
| TAB0
| BS0
| VT0
| FF0
);
106 tty
.c_oflag
&= ~(OLCUC
| OCRNL
| ONOCR
| ONLRET
| OFILL
| OFDEL
|\
107 NLDLY
|CRDLY
|TABDLY
|BSDLY
|VTDLY
|FFDLY
);
108 tty
.c_lflag
|= (ISIG
| ICANON
| IEXTEN
| ECHO
|ECHOE
|ECHOK
|ECHOCTL
|ECHOKE
);
109 tty
.c_lflag
&= ~(ECHONL
| NOFLSH
| XCASE
| TOSTOP
| ECHOPRT
);
110 tty
.c_cflag
|= (CREAD
| CS8
| B9600
);
111 tty
.c_cflag
&= ~(PARENB
);
113 /* VTIME and VMIN can overlap with VEOF and VEOL since they are
114 * only used for non-canonical mode. We just set the at the
115 * beginning, so nothing bad should happen.
119 tty
.c_cc
[VINTR
] = CINTR
;
120 tty
.c_cc
[VQUIT
] = CQUIT
;
121 tty
.c_cc
[VERASE
] = CERASE
; /* ASCII DEL (0177) */
122 tty
.c_cc
[VKILL
] = CKILL
;
123 tty
.c_cc
[VEOF
] = CEOF
;
124 tty
.c_cc
[VSWTC
] = _POSIX_VDISABLE
;
125 tty
.c_cc
[VSTART
] = CSTART
;
126 tty
.c_cc
[VSTOP
] = CSTOP
;
127 tty
.c_cc
[VSUSP
] = CSUSP
;
128 tty
.c_cc
[VEOL
] = _POSIX_VDISABLE
;
129 tty
.c_cc
[VREPRINT
] = CREPRINT
;
130 tty
.c_cc
[VDISCARD
] = CDISCARD
;
131 tty
.c_cc
[VWERASE
] = CWERASE
;
132 tty
.c_cc
[VLNEXT
] = CLNEXT
;
133 tty
.c_cc
[VEOL2
] = _POSIX_VDISABLE
;
135 tcsetattr(0, TCSANOW
, &tty
);
147 void alrm_handler(int sig
__attribute__((unused
)))
149 void alrm_handler(int sig
)
155 * See if an encrypted password is valid. The encrypted
156 * password is checked for traditional-style DES and
157 * FreeBSD-style MD5 encryption.
160 int valid(const char *pass
)
167 if (pass
[0] == 0) return 1;
169 if (pass
[0] != '$') goto check_des
;
172 * up to 4 bytes for the signature e.g. $1$
174 for(s
= pass
+1; *s
&& *s
!= '$'; s
++)
176 if (*s
++ != '$') return 0;
177 if ((off
= (off_t
)(s
-pass
)) > 4 || off
< 3) return 0;
179 memset(id
, '\0', sizeof(id
));
180 strncpy(id
, pass
, off
);
183 * up to 16 bytes for the salt
185 for(; *s
&& *s
!= '$'; s
++)
187 if (*s
++ != '$') return 0;
188 if ((off_t
)(s
-pass
) > 16) return 0;
192 * the MD5 hash (128 bits or 16 bytes) encoded in base64 = 22 bytes
194 if ((strcmp(id
, "$1$") == 0) && (len
< 22 || len
> 24)) return 0;
197 * the SHA-256 hash 43 bytes
199 if ((strcmp(id
, "$5$") == 0) && (len
< 42 || len
> 44)) return 0;
202 * the SHA-512 hash 86 bytes
204 if ((strcmp(id
, "$6$") == 0) && (len
< 85 || len
> 87)) return 0;
213 if (strlen(pass
) != 13) return 0;
214 for (s
= pass
; *s
; s
++) {
215 if ((*s
< '0' || *s
> '9') &&
216 (*s
< 'a' || *s
> 'z') &&
217 (*s
< 'A' || *s
> 'Z') &&
218 *s
!= '.' && *s
!= '/') return 0;
225 * Set a variable if the value is not NULL.
228 void set(char **var
, char *val
)
234 * Get the root password entry.
237 struct passwd
*getrootpwent(int try_manually
)
239 static struct passwd pwd
;
243 static char line
[256];
244 static char sline
[256];
248 * First, we try to get the password the standard
249 * way using normal library calls.
251 if ((pw
= getpwnam("root")) &&
252 !strcmp(pw
->pw_passwd
, "x") &&
253 (spw
= getspnam("root")))
254 pw
->pw_passwd
= spw
->sp_pwdp
;
255 if (pw
|| !try_manually
) return pw
;
258 * If we come here, we could not retrieve the root
259 * password through library calls and we try to
260 * read the password and shadow files manually.
262 pwd
.pw_name
= "root";
264 pwd
.pw_gecos
= "Super User";
270 if ((fp
= fopen(F_PASSWD
, "r")) == NULL
) {
276 * Find root in the password file.
278 while((p
= fgets(line
, 256, fp
)) != NULL
) {
279 if (strncmp(line
, "root:", 5) != 0)
282 set(&pwd
.pw_passwd
, strsep(&p
, ":"));
283 (void)strsep(&p
, ":");
284 (void)strsep(&p
, ":");
285 set(&pwd
.pw_gecos
, strsep(&p
, ":"));
286 set(&pwd
.pw_dir
, strsep(&p
, ":"));
287 set(&pwd
.pw_shell
, strsep(&p
, "\n"));
294 * If the encrypted password is valid
295 * or not found, return.
298 fprintf(stderr
, "%s: no entry for root\n", F_PASSWD
);
301 if (valid(pwd
.pw_passwd
)) return &pwd
;
304 * The password is invalid. If there is a
305 * shadow password, try it.
307 strcpy(pwd
.pw_passwd
, "");
308 if ((fp
= fopen(F_SHADOW
, "r")) == NULL
) {
309 fprintf(stderr
, "%s: root password garbled\n", F_PASSWD
);
312 while((p
= fgets(sline
, 256, fp
)) != NULL
) {
313 if (strncmp(sline
, "root:", 5) != 0)
316 set(&pwd
.pw_passwd
, strsep(&p
, ":"));
322 * If the password is still invalid,
323 * NULL it, and return.
326 fprintf(stderr
, "%s: no entry for root\n", F_SHADOW
);
327 strcpy(pwd
.pw_passwd
, "");
329 if (!valid(pwd
.pw_passwd
)) {
330 fprintf(stderr
, "%s: root password garbled\n", F_SHADOW
);
331 strcpy(pwd
.pw_passwd
, ""); }
336 * Ask for the password. Note that there is no
337 * default timeout as we normally skip this during boot.
340 char *getpasswd(char *crypted
)
343 struct termios old
, tty
;
344 static char pass
[128];
347 #if defined(USE_ONELINE)
349 printf("Give root password for login: ");
351 printf("Press enter for login: ");
354 printf("Give root password for maintenance\n");
356 printf("Press enter for maintenance");
357 printf("(or type Control-D to continue): ");
363 tty
.c_iflag
&= ~(IUCLC
|IXON
|IXOFF
|IXANY
);
364 tty
.c_lflag
&= ~(ECHO
|ECHOE
|ECHOK
|ECHONL
|TOSTOP
);
365 tcsetattr(0, TCSANOW
, &tty
);
367 pass
[sizeof(pass
) - 1] = 0;
369 sa
.sa_handler
= alrm_handler
;
371 sigaction(SIGALRM
, &sa
, NULL
);
372 if (timeout
) alarm(timeout
);
374 if (read(0, pass
, sizeof(pass
) - 1) <= 0)
377 for(i
= 0; i
< (int)sizeof(pass
) && pass
[i
]; i
++)
378 if (pass
[i
] == '\r' || pass
[i
] == '\n') {
384 tcsetattr(0, TCSANOW
, &old
);
391 * Password was OK, execute a shell.
394 void sushell(struct passwd
*pwd
)
402 * Set directory and shell.
404 (void)chdir(pwd
->pw_dir
);
405 if ((p
= getenv("SUSHELL")) != NULL
)
407 else if ((p
= getenv("sushell")) != NULL
)
410 if (pwd
->pw_shell
[0])
411 sushell
= pwd
->pw_shell
;
415 if ((p
= strrchr(sushell
, '/')) == NULL
)
419 snprintf(shell
, sizeof(shell
), profile
? "-%s" : "%s", p
);
422 * Set some important environment variables.
424 getcwd(home
, sizeof(home
));
425 setenv("HOME", home
, 1);
426 setenv("LOGNAME", "root", 1);
427 setenv("USER", "root", 1);
429 setenv("SHLVL","0",1);
432 * Try to execute a shell.
434 setenv("SHELL", sushell
, 1);
435 signal(SIGINT
, saved_sigint
);
436 signal(SIGTSTP
, saved_sigtstp
);
437 signal(SIGQUIT
, saved_sigquit
);
439 if (is_selinux_enabled() > 0) {
440 security_context_t scon
=NULL
;
443 if (getseuserbyname("root", &seuser
, &level
) == 0)
444 if (get_default_context_with_level(seuser
, level
, 0, &scon
) == 0) {
445 if (setexeccon(scon
) != 0)
446 fprintf(stderr
, "setexeccon faile\n");
453 execl(sushell
, shell
, NULL
);
456 setenv("SHELL", BINSH
, 1);
457 execl(BINSH
, profile
? "-sh" : "sh", NULL
);
460 /* Fall back to staticly linked shell if both the users shell
461 and /bin/sh failed to execute. */
462 setenv("SHELL", STATICSH
, 1);
463 execl(STATICSH
, STATICSH
, NULL
);
470 fprintf(stderr
, "Usage: sulogin [-e] [-p] [-t timeout] [tty device]\n");
473 int main(int argc
, char **argv
)
480 pid_t pid
, pgrp
, ppgrp
, ttypgrp
;
483 * See if we have a timeout flag.
486 while((c
= getopt(argc
, argv
, "ept:")) != EOF
) switch(c
) {
488 timeout
= atoi(optarg
);
502 if (geteuid() != 0) {
503 fprintf(stderr
, "sulogin: only root can run sulogin.\n");
508 * See if we need to open an other tty device.
510 saved_sigint
= signal(SIGINT
, SIG_IGN
);
511 saved_sigtstp
= signal(SIGQUIT
, SIG_IGN
);
512 saved_sigquit
= signal(SIGTSTP
, SIG_IGN
);
513 if (optind
< argc
) tty
= argv
[optind
];
515 if (tty
|| (tty
= getenv("CONSOLE"))) {
517 if ((fd
= open(tty
, O_RDWR
)) < 0) {
523 fprintf(stderr
, "%s: not a tty\n", tty
);
528 * Only go through this trouble if the new
529 * tty doesn't fall in this process group.
533 ppgrp
= getpgid(getppid());
534 ttypgrp
= tcgetpgrp(fd
);
536 if (pgrp
!= ttypgrp
&& ppgrp
!= ttypgrp
) {
537 if (pid
!= getsid(0)) {
538 if (pid
== getpgid(0))
539 setpgid(0, getpgid(getppid()));
543 signal(SIGHUP
, SIG_IGN
);
545 ioctl(0, TIOCNOTTY
, (char *)1);
546 signal(SIGHUP
, SIG_DFL
);
552 if ((fd
= open(tty
, O_RDWR
|O_NOCTTY
)) < 0) {
555 ioctl(0, TIOCSCTTY
, (char *)1);
556 tcsetpgrp(fd
, ppgrp
);
567 } else if (getpid() == 1) {
568 /* We are init. We hence need to set a session anyway */
570 if (ioctl(0, TIOCSCTTY
, (char *)1))
571 perror("ioctl(TIOCSCTTY)");
574 #if defined(SANE_TIO) && (SANE_TIO == 1)
579 * Get the root password.
581 if ((pwd
= getrootpwent(opt_e
)) == NULL
) {
582 fprintf(stderr
, "sulogin: cannot open password database!\n");
587 * Ask for the password.
590 if ((p
= getpasswd(pwd
->pw_passwd
)) == NULL
) break;
591 if (pwd
->pw_passwd
[0] == 0 ||
592 strcmp(crypt(p
, pwd
->pw_passwd
), pwd
->pw_passwd
) == 0)
594 saved_sigquit
= signal(SIGQUIT
, SIG_IGN
);
595 saved_sigtstp
= signal(SIGTSTP
, SIG_IGN
);
596 saved_sigint
= signal(SIGINT
, SIG_IGN
);
597 printf("Login incorrect.\n");
601 * User pressed Control-D.