1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
|
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* or http://www.opensolaris.org/os/licensing.
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright 2013 Nexenta Systems, Inc. All rights reserved.
* Copyright (c) 2016 by Delphix. All rights reserved.
*/
/*
* Copyright 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
/*
* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
* All rights reserved.
*
* Copyright (c) 1987, 1988 Microsoft Corporation.
* All rights reserved.
*/
/*
* sulogin - special login program exec'd from init to let user
* come up single user, or go to default init state straight away.
*
* Explain the scoop to the user, prompt for an authorized user
* name or ^D and then prompt for password or ^D. If the password
* is correct, check if the user is authorized, if so enter
* single user. ^D exits sulogin, and init will go to default init state.
*
* If /etc/passwd is missing, or there's no entry for root,
* go single user, no questions asked.
*/
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <sys/sysmsg_impl.h>
#include <sys/mkdev.h>
#include <sys/resource.h>
#include <sys/uadmin.h>
#include <sys/wait.h>
#include <sys/stermio.h>
#include <fcntl.h>
#include <termio.h>
#include <pwd.h>
#include <shadow.h>
#include <stdlib.h>
#include <stdio.h>
#include <signal.h>
#include <siginfo.h>
#include <utmpx.h>
#include <unistd.h>
#include <ucontext.h>
#include <string.h>
#include <strings.h>
#include <deflt.h>
#include <limits.h>
#include <errno.h>
#include <crypt.h>
#include <auth_attr.h>
#include <auth_list.h>
#include <nss_dbdefs.h>
#include <user_attr.h>
#include <sys/vt.h>
#include <sys/kd.h>
/*
* Intervals to sleep after failed login
*/
#ifndef SLEEPTIME
#define SLEEPTIME 4 /* sleeptime before login incorrect msg */
#endif
#define SLEEPTIME_MAX 5 /* maximum sleeptime */
/*
* the name of the file containing the login defaults we deliberately
* use the same file as login(1)
*/
#define DEFAULT_LOGIN "/etc/default/login"
#define DEFAULT_SULOGIN "/etc/default/sulogin"
#define DEFAULT_CONSOLE "/dev/console"
static char shell[] = "/sbin/sh";
static char su[] = "/sbin/su.static";
static int sleeptime = SLEEPTIME;
static int nchild = 0;
static pid_t pidlist[10];
static pid_t masterpid = 0;
static pid_t originalpid = 0;
static struct sigaction sa;
static struct termio ttymodes;
static char *findttyname(int fd);
static char *stripttyname(char *);
static char *sulogin_getinput(char *, int);
static void noop(int);
static void single(const char *, char *);
static void main_loop(char *, boolean_t);
static void parenthandler();
static void termhandler(int);
static void setupsigs(void);
static int pathcmp(char *, char *);
static void doit(char *, char *);
static void childcleanup(int);
#define ECHOON 0
#define ECHOOFF 1
/* ARGSUSED */
int
main(int argc, char **argv)
{
struct spwd *shpw;
int passreq = B_TRUE;
int flags;
int fd;
char *infop, *ptr, *p;
pid_t pid;
int bufsize;
struct stat st;
char cttyname[100];
char namedlist[500];
char scratchlist[500];
dev_t cttyd;
if (geteuid() != 0) {
(void) fprintf(stderr, "%s: must be root\n", argv[0]);
return (EXIT_FAILURE);
}
/* Do the magic to determine the children */
if ((fd = open(SYSMSG, 0)) < 0)
return (EXIT_FAILURE);
/*
* If the console supports the CIOCTTYCONSOLE ioctl, then fetch
* its console device list. If not, then we use the default
* console name.
*/
if (ioctl(fd, CIOCTTYCONSOLE, &cttyd) == 0) {
if ((bufsize = ioctl(fd, CIOCGETCONSOLE, NULL)) < 0)
return (EXIT_FAILURE);
if (bufsize > 0) {
if ((infop = calloc(bufsize, sizeof (char))) == NULL)
return (EXIT_FAILURE);
if (ioctl(fd, CIOCGETCONSOLE, infop) < 0)
return (EXIT_FAILURE);
(void) snprintf(namedlist, sizeof (namedlist), "%s %s",
DEFAULT_CONSOLE, infop);
} else
(void) snprintf(namedlist, sizeof (namedlist), "%s",
DEFAULT_CONSOLE);
} else {
(void) snprintf(namedlist, sizeof (namedlist), "%s",
DEFAULT_CONSOLE);
cttyd = NODEV;
}
/*
* The attempt to turn the controlling terminals dev_t into a string
* may not be successful, thus leaving the variable cttyname as a
* NULL. This occurs if during boot we find
* the root partition (or some other partition)
* requires manual fsck, thus resulting in sulogin
* getting invoked. The ioctl for CIOCTTYCONSOLE
* called above returned NODEV for cttyd
* in these cases. NODEV gets returned when the vnode pointer
* in our session structure is NULL. In these cases it
* must be assumed that the default console is used.
*
* See uts/common/os/session.c:cttydev().
*/
(void) strcpy(cttyname, DEFAULT_CONSOLE);
(void) strcpy(scratchlist, namedlist);
ptr = scratchlist;
while (ptr != NULL) {
p = strchr(ptr, ' ');
if (p == NULL) {
if (stat(ptr, &st))
return (EXIT_FAILURE);
if (st.st_rdev == cttyd)
(void) strcpy(cttyname, ptr);
break;
}
*p++ = '\0';
if (stat(ptr, &st))
return (EXIT_FAILURE);
if (st.st_rdev == cttyd) {
(void) strcpy(cttyname, ptr);
break;
}
ptr = p;
}
/*
* Use the same value of SLEEPTIME that login(1) uses. This
* is obtained by reading the file /etc/default/login using
* the def*() functions.
*/
if (defopen(DEFAULT_LOGIN) == 0) {
/* ignore case */
flags = defcntl(DC_GETFLAGS, 0);
TURNOFF(flags, DC_CASE);
(void) defcntl(DC_SETFLAGS, flags);
if ((ptr = defread("SLEEPTIME=")) != NULL)
sleeptime = atoi(ptr);
if (sleeptime < 0 || sleeptime > SLEEPTIME_MAX)
sleeptime = SLEEPTIME;
(void) defopen(NULL); /* closes DEFAULT_LOGIN */
}
/*
* Use our own value of PASSREQ, separate from the one login(1) uses.
* This is obtained by reading the file /etc/default/sulogin using
* the def*() functions.
*/
if (defopen(DEFAULT_SULOGIN) == 0) {
if ((ptr = defread("PASSREQ=")) != NULL)
if (strcmp("NO", ptr) == 0)
passreq = B_FALSE;
(void) defopen(NULL); /* closes DEFAULT_SULOGIN */
}
if (passreq == B_FALSE)
single(shell, NULL);
/*
* if no 'root' entry in /etc/shadow, give maint. mode single
* user shell prompt
*/
setspent();
if ((shpw = getspnam("root")) == NULL) {
(void) fprintf(stderr, "\n*** Unable to retrieve `root' entry "
"in shadow password file ***\n\n");
single(shell, NULL);
}
endspent();
/*
* if no 'root' entry in /etc/passwd, give maint. mode single
* user shell prompt
*/
setpwent();
if (getpwnam("root") == NULL) {
(void) fprintf(stderr, "\n*** Unable to retrieve `root' entry "
"in password file ***\n\n");
single(shell, NULL);
}
endpwent();
/* process with controlling tty treated special */
if ((pid = fork()) != (pid_t)0) {
if (pid == -1)
return (EXIT_FAILURE);
else {
setupsigs();
masterpid = pid;
originalpid = getpid();
/*
* init() was invoked from a console that was not
* the default console, nor was it an auxiliary.
*/
if (cttyname[0] == '\0')
termhandler(0);
/* Never returns */
main_loop(cttyname, B_TRUE);
/* Never returns */
}
}
masterpid = getpid();
originalpid = getppid();
pidlist[nchild++] = originalpid;
sa.sa_handler = childcleanup;
sa.sa_flags = 0;
(void) sigemptyset(&sa.sa_mask);
(void) sigaction(SIGTERM, &sa, NULL);
(void) sigaction(SIGHUP, &sa, NULL);
sa.sa_handler = parenthandler;
sa.sa_flags = SA_SIGINFO;
(void) sigemptyset(&sa.sa_mask);
(void) sigaction(SIGUSR1, &sa, NULL);
sa.sa_handler = SIG_IGN;
sa.sa_flags = 0;
(void) sigemptyset(&sa.sa_mask);
(void) sigaction(SIGCHLD, &sa, NULL);
/*
* If there isn't a password on root, then don't permit
* the fanout capability of sulogin.
*/
if (*shpw->sp_pwdp != '\0') {
ptr = namedlist;
while (ptr != NULL) {
p = strchr(ptr, ' ');
if (p == NULL) {
doit(ptr, cttyname);
break;
}
*p++ = '\0';
doit(ptr, cttyname);
ptr = p;
}
}
if (pathcmp(cttyname, DEFAULT_CONSOLE) != 0) {
if ((pid = fork()) == (pid_t)0) {
setupsigs();
main_loop(DEFAULT_CONSOLE, B_FALSE);
} else if (pid == -1)
return (EXIT_FAILURE);
pidlist[nchild++] = pid;
}
/*
* When parent is all done, it pauses until one of its children
* signals that its time to kill the underpriviledged.
*/
(void) wait(NULL);
return (0);
}
/*
* These flags are taken from stty's "sane" table entries in
* usr/src/cmd/ttymon/sttytable.c
*/
#define SET_IFLAG (BRKINT|IGNPAR|ISTRIP|ICRNL|IXON|IMAXBEL)
#define RESET_IFLAG (IGNBRK|PARMRK|INPCK|INLCR|IGNCR|IUCLC|IXOFF|IXANY)
#define SET_OFLAG (OPOST|ONLCR)
#define RESET_OFLAG (OLCUC|OCRNL|ONOCR|ONLRET|OFILL|OFDEL| \
NLDLY|CRDLY|TABDLY|BSDLY|VTDLY|FFDLY)
#define SET_LFLAG (ISIG|ICANON|IEXTEN|ECHO|ECHOK|ECHOE|ECHOKE|ECHOCTL)
#define RESET_LFLAG (XCASE|ECHONL|NOFLSH|STFLUSH|STWRAP|STAPPL)
/*
* Do the equivalent of 'stty sane' on the terminal since we don't know
* what state it was in on startup.
*/
static void
sanitize_tty(int fd)
{
(void) ioctl(fd, TCGETA, &ttymodes);
ttymodes.c_iflag &= ~RESET_IFLAG;
ttymodes.c_iflag |= SET_IFLAG;
ttymodes.c_oflag &= ~RESET_OFLAG;
ttymodes.c_oflag |= SET_OFLAG;
ttymodes.c_lflag &= ~RESET_LFLAG;
ttymodes.c_lflag |= SET_LFLAG;
ttymodes.c_cc[VERASE] = CERASE;
ttymodes.c_cc[VKILL] = CKILL;
ttymodes.c_cc[VQUIT] = CQUIT;
ttymodes.c_cc[VINTR] = CINTR;
ttymodes.c_cc[VEOF] = CEOF;
ttymodes.c_cc[VEOL] = CNUL;
(void) ioctl(fd, TCSETAF, &ttymodes);
}
/*
* Fork a child of sulogin for each of the auxiliary consoles.
*/
static void
doit(char *ptr, char *cttyname)
{
pid_t pid;
if (pathcmp(ptr, DEFAULT_CONSOLE) != 0 &&
pathcmp(ptr, cttyname) != 0) {
if ((pid = fork()) == (pid_t)0) {
setupsigs();
main_loop(ptr, B_FALSE);
} else if (pid == -1)
exit(EXIT_FAILURE);
pidlist[nchild++] = pid;
}
}
static int
pathcmp(char *adev, char *bdev)
{
struct stat st1;
struct stat st2;
if (adev == NULL || bdev == NULL)
return (1);
if (strcmp(adev, bdev) == 0)
return (0);
if (stat(adev, &st1) || !S_ISCHR(st1.st_mode))
return (1);
if (stat(bdev, &st2) || !S_ISCHR(st2.st_mode))
return (1);
if (st1.st_rdev == st2.st_rdev)
return (0);
return (1);
}
/* Handlers for the children at initialization */
static void
setupsigs()
{
sa.sa_handler = noop;
sa.sa_flags = 0;
(void) sigemptyset(&sa.sa_mask);
(void) sigaction(SIGINT, &sa, NULL);
(void) sigaction(SIGQUIT, &sa, NULL);
sa.sa_handler = termhandler;
sa.sa_flags = 0;
(void) sigemptyset(&sa.sa_mask);
(void) sigaction(SIGTERM, &sa, NULL);
(void) sigaction(SIGKILL, &sa, NULL);
(void) sigaction(SIGHUP, &sa, NULL);
}
static void
main_loop(char *devname, boolean_t cttyflag)
{
int fd, fb, i;
char *user = NULL; /* authorized user */
char *pass; /* password from user */
char *cpass; /* crypted password */
struct spwd spwd;
struct spwd *lshpw; /* local shadow */
char shadow[NSS_BUFLEN_SHADOW];
FILE *sysmsgfd;
for (i = 0; i < 3; i++)
(void) close(i);
if (cttyflag == B_FALSE) {
if (setsid() == -1)
exit(EXIT_FAILURE);
}
if ((fd = open(devname, O_RDWR)) < 0)
exit(EXIT_FAILURE);
/*
* In system maintenance mode, all virtual console instances
* of the svc:/system/console-login service are not available
* any more, and only the system console is available. So here
* we always switch to the system console in case at the moment
* the active console isn't it.
*/
(void) ioctl(fd, VT_ACTIVATE, 1);
if (fd != 0)
(void) dup2(fd, STDIN_FILENO);
if (fd != 1)
(void) dup2(fd, STDOUT_FILENO);
if (fd != 2)
(void) dup2(fd, STDERR_FILENO);
if (fd > 2)
(void) close(fd);
/* Stop progress bar and reset console mode to text */
if ((fb = open("/dev/fb", O_RDONLY)) >= 0) {
(void) ioctl(fb, KDSETMODE, KD_RESETTEXT);
(void) close(fb);
}
sysmsgfd = fopen("/dev/sysmsg", "w");
sanitize_tty(fileno(stdin));
for (;;) {
do {
(void) printf("\nEnter user name for system "
"maintenance (control-d to bypass): ");
user = sulogin_getinput(devname, ECHOON);
if (user == NULL) {
/* signal other children to exit */
(void) sigsend(P_PID, masterpid, SIGUSR1);
/* ^D, so straight to default init state */
exit(EXIT_FAILURE);
}
} while (user[0] == '\0');
(void) printf("Enter %s password (control-d to bypass): ",
user);
if ((pass = sulogin_getinput(devname, ECHOOFF)) == NULL) {
/* signal other children to exit */
(void) sigsend(P_PID, masterpid, SIGUSR1);
/* ^D, so straight to default init state */
free(user);
exit(EXIT_FAILURE);
}
lshpw = getspnam_r(user, &spwd, shadow, sizeof (shadow));
if (lshpw == NULL) {
/*
* the user entered doesn't exist, too bad.
*/
goto sorry;
}
/*
* There is a special case error to catch here:
* If the password is hashed with an algorithm
* other than the old unix crypt the call to crypt(3c)
* could fail if /usr is corrupt or not available
* since by default /etc/security/crypt.conf will
* have the crypt_ modules located under /usr/lib.
* Or it could happen if /etc/security/crypt.conf
* is corrupted.
*
* If this happens crypt(3c) will return NULL and
* set errno to ELIBACC for the former condition or
* EINVAL for the latter, in this case we bypass
* authentication and just verify that the user is
* authorized.
*/
errno = 0;
cpass = crypt(pass, lshpw->sp_pwdp);
if (((cpass == NULL) && (lshpw->sp_pwdp[0] == '$')) &&
((errno == ELIBACC) || (errno == EINVAL))) {
goto checkauth;
} else if ((cpass == NULL) ||
(strcmp(cpass, lshpw->sp_pwdp) != 0)) {
goto sorry;
}
checkauth:
/*
* There is a special case error here as well.
* If /etc/user_attr is corrupt, getusernam("root")
* returns NULL.
* In this case, we just give access because this is similar
* to the case of root not existing in /etc/passwd.
*/
if ((getusernam("root") != NULL) &&
(chkauthattr(MAINTENANCE_AUTH, user) != 1)) {
goto sorry;
}
(void) fprintf(sysmsgfd, "\nsingle-user privilege "
"assigned to %s on %s.\n", user, devname);
(void) sigsend(P_PID, masterpid, SIGUSR1);
(void) wait(NULL);
free(user);
free(pass);
single(su, devname);
/* single never returns */
sorry:
(void) printf("\nLogin incorrect or user %s not authorized\n",
user);
free(user);
free(pass);
(void) sleep(sleeptime);
}
}
/*
* single() - exec shell for single user mode
*/
static void
single(const char *cmd, char *ttyn)
{
struct utmpx *u;
char found = B_FALSE;
if (ttyn == NULL)
ttyn = findttyname(STDIN_FILENO);
/*
* utmpx records on the console device are expected to be "console"
* by other processes, such as dtlogin.
*/
ttyn = stripttyname(ttyn);
/* update the utmpx file. */
while ((u = getutxent()) != NULL) {
if (strcmp(u->ut_line, ttyn) == 0) {
u->ut_tv.tv_sec = time(NULL);
u->ut_type = USER_PROCESS;
u->ut_pid = getpid();
if (strcmp(u->ut_user, "root") != 0)
(void) strcpy(u->ut_user, "root");
(void) pututxline(u);
found = B_TRUE;
break;
}
}
if (!found) {
struct utmpx entryx;
entryx.ut_tv.tv_sec = time(NULL);
entryx.ut_type = USER_PROCESS;
entryx.ut_pid = getpid();
(void) strcpy(entryx.ut_user, "root");
(void) strcpy(entryx.ut_line, ttyn);
entryx.ut_tv.tv_usec = 0;
entryx.ut_session = 0;
entryx.ut_id[0] = 'c';
entryx.ut_id[1] = 'o';
entryx.ut_id[2] = 's';
entryx.ut_id[3] = 'u';
entryx.ut_syslen = 1;
entryx.ut_host[0] = '\0';
entryx.ut_exit.e_termination = WTERMSIG(0);
entryx.ut_exit.e_exit = WEXITSTATUS(0);
(void) pututxline(&entryx);
}
endutxent();
(void) printf("Entering System Maintenance Mode\n\n");
if (execl(cmd, cmd, "-", (char *)0) < 0)
exit(EXIT_FAILURE);
}
/*
* sulogin_getinput() - hacked from the standard PAM tty conversation
* function getpassphrase() library version
* so we can distinguish newline and EOF.
* also don't need this routine to give a prompt.
*
* returns the password string, or NULL if the used typed EOF.
*/
static char *
sulogin_getinput(char *devname, int echooff)
{
struct termio ttyb;
int c;
FILE *fi;
static char input[PASS_MAX + 1];
void (*saved_handler)();
char *rval = input;
int i = 0;
if ((fi = fopen(devname, "r")) == NULL) {
fi = stdin;
}
saved_handler = signal(SIGINT, SIG_IGN);
if (echooff) {
ttyb = ttymodes;
ttyb.c_lflag &= ~(ECHO | ECHOE | ECHONL);
(void) ioctl(fileno(fi), TCSETAF, &ttyb);
}
/* get characters up to PASS_MAX, but don't overflow */
while ((c = getc(fi)) != '\n' && (c != '\r')) {
if (c == EOF && i == 0) { /* ^D, no input */
rval = NULL;
break;
}
if (i < PASS_MAX) {
input[i++] = (char)c;
}
}
input[i] = '\0';
(void) fputc('\n', fi);
if (echooff) {
(void) ioctl(fileno(fi), TCSETAW, &ttymodes);
}
if (saved_handler != SIG_ERR)
(void) signal(SIGINT, saved_handler);
return (rval == NULL ? NULL : strdup(rval));
}
static char *
findttyname(int fd)
{
char *ttyn = ttyname(fd);
if (ttyn == NULL)
ttyn = "/dev/???";
else {
/*
* /dev/syscon and /dev/systty are usually links to
* /dev/console. prefer /dev/console.
*/
if (((strcmp(ttyn, "/dev/syscon") == 0) ||
(strcmp(ttyn, "/dev/systty") == 0)) &&
access("/dev/console", F_OK))
ttyn = "/dev/console";
}
return (ttyn);
}
static char *
stripttyname(char *ttyn)
{
/* saw off the /dev/ */
if (strncmp(ttyn, "/dev/", sizeof ("/dev/") -1) == 0)
return (ttyn + sizeof ("/dev/") - 1);
else
return (ttyn);
}
/* ARGSUSED */
static void
noop(int sig)
{
/*
* This signal handler does nothing except return. We use it
* as the signal disposition in this program instead of
* SIG_IGN so that we do not have to restore the disposition
* back to SIG_DFL. Instead we allow exec(2) to set the
* dispostion to SIG_DFL to avoid a race condition.
*/
}
/* ARGSUSED */
static void
parenthandler(int sig, siginfo_t *si, ucontext_t *uc)
{
int i;
/*
* We get here if someone has successfully entered a password
* from the auxiliary console and is getting the single-user shell.
* When this happens, the parent needs to kill the children
* that didn't get the shell.
*
*/
for (i = 0; i < nchild; i++) {
if (pidlist[i] != si->__data.__proc.__pid)
(void) sigsend(P_PID, pidlist[i], SIGTERM);
}
sa.sa_handler = SIG_IGN;
sa.sa_flags = 0;
(void) sigemptyset(&sa.sa_mask);
(void) sigaction(SIGINT, &sa, NULL);
(void) sigaction(SIGQUIT, &sa, NULL);
(void) sigaction(SIGTERM, &sa, NULL);
(void) wait(NULL);
}
/*
* The master pid will get SIGTERM or SIGHUP from init, and then
* has to make sure the shell isn't still running.
*/
/* ARGSUSED */
static void
childcleanup(int sig)
{
int i;
/* Only need to kill the child that became the shell. */
for (i = 0; i < nchild; i++) {
/* Don't kill grandparent before it's necessary */
if (pidlist[i] != getppid())
(void) sigsend(P_PID, pidlist[i], SIGHUP);
}
}
/* ARGSUSED */
static void
termhandler(int sig)
{
FILE *fi;
pid_t pid;
/* Processes come here when they fail to receive the password. */
if ((fi = fopen("/dev/tty", "r+")) == NULL)
fi = stdin;
else
setbuf(fi, NULL);
sanitize_tty(fileno(fi));
/* If you're the controlling tty, then just wait */
pid = getpid();
if (pid == originalpid || pid == masterpid) {
sa.sa_handler = SIG_IGN;
sa.sa_flags = 0;
(void) sigemptyset(&sa.sa_mask);
(void) sigaction(SIGINT, &sa, NULL);
(void) sigaction(SIGQUIT, &sa, NULL);
sa.sa_handler = SIG_DFL;
sa.sa_flags = 0;
(void) sigemptyset(&sa.sa_mask);
(void) sigaction(SIGTERM, &sa, NULL);
(void) sigaction(SIGHUP, &sa, NULL);
(void) wait(NULL);
}
exit(0);
}
|