1 /*
2 * SPDX-License-Identifier: ISC
3 *
4 * Copyright (c) 2013-2020 Todd C. Miller <Todd.Miller@sudo.ws>
5 *
6 * Permission to use, copy, modify, and distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <config.h>
20
21 #include <sys/types.h>
22 #include <sys/stat.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <unistd.h>
27 #include <limits.h>
28 #include <errno.h>
29
30 #include "sudo_compat.h"
31 #include "sudo_fatal.h"
32 #include "sudo_util.h"
33 #include "sudo_debug.h"
34
35 sudo_dso_public int main(int argc, char *argv[]);
36
37 int sudo_debug_instance = SUDO_DEBUG_INSTANCE_INITIALIZER;
38 extern char *get_process_ttyname(char *name, size_t namelen);
39
40 static int
match_ttys(const char * tty1,const char * tty2)41 match_ttys(const char *tty1, const char *tty2)
42 {
43 struct stat sb1, sb2;
44
45 if (tty1 != NULL && tty2 != NULL) {
46 if (strcmp(tty1, tty2) == 0)
47 return 0;
48 /* Could be the same device with a different name. */
49 if (stat(tty1, &sb1) == 0 && S_ISCHR(sb1.st_mode) &&
50 stat(tty2, &sb2) == 0 && S_ISCHR(sb2.st_mode)) {
51 if (sb1.st_rdev == sb2.st_rdev)
52 return 0;
53 }
54 } else if (tty1 == NULL && tty2 == NULL) {
55 return 0;
56 }
57
58 return 1;
59 }
60
61
62 int
main(int argc,char * argv[])63 main(int argc, char *argv[])
64 {
65 char *tty_libc = NULL, *tty_sudo = NULL;
66 char pathbuf[PATH_MAX];
67 int ret = 1;
68
69 initprogname(argc > 0 ? argv[0] : "check_ttyname");
70
71 /* Lookup tty name using kernel info if possible. */
72 if (get_process_ttyname(pathbuf, sizeof(pathbuf)) != NULL)
73 tty_sudo = pathbuf;
74
75 #if defined(HAVE_KINFO_PROC2_NETBSD) || \
76 defined(HAVE_KINFO_PROC_OPENBSD) || \
77 defined(HAVE_KINFO_PROC_FREEBSD) || \
78 defined(HAVE_KINFO_PROC_44BSD) || \
79 defined(HAVE__TTYNAME_DEV) || defined(HAVE_STRUCT_PSINFO_PR_TTYDEV) || \
80 defined(HAVE_PSTAT_GETPROC) || defined(__linux__)
81
82 /* Lookup tty name attached to stdin via libc. */
83 tty_libc = ttyname(STDIN_FILENO);
84 #endif
85
86 /* Compare libc and kernel ttys. */
87 ret = match_ttys(tty_libc, tty_sudo);
88 if (ret == 0) {
89 printf("%s: OK (%s)\n", getprogname(), tty_sudo ? tty_sudo : "none");
90 } else if (tty_libc == NULL) {
91 printf("%s: SKIP (%s)\n", getprogname(), tty_sudo ? tty_sudo : "none");
92 ret = 0;
93 } else {
94 printf("%s: FAIL %s (sudo) vs. %s (libc)\n", getprogname(),
95 tty_sudo ? tty_sudo : "none", tty_libc ? tty_libc : "none");
96 }
97
98 return ret;
99 }
100