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
|
$OpenBSD$
Index: xconsole.c
--- xconsole.c.orig
+++ xconsole.c
@@ -65,7 +65,7 @@ extern char *_XawTextGetSTRING(TextWidget ctx, XawText
#include <X11/Shell.h>
#include <ctype.h>
#include <stdlib.h>
-#ifdef HAS_OPENPTY
+#ifdef HAVE_OPENPTY
# ifdef HAVE_UTIL_H
# include <util.h>
# endif
@@ -77,11 +77,20 @@ extern char *_XawTextGetSTRING(TextWidget ctx, XawText
# endif
#endif
+#define XCONSOLE_MAX_LINE_LENGTH 1024
+
+#ifdef USE_PRIVSEP
+#include <err.h>
+#include <pwd.h>
+#include "xconsole.h"
+#endif
+
static void inputReady(XtPointer w, int *source, XtInputId *id);
static long TextLength(Widget w);
static void TextReplace(Widget w, int start, int end, XawTextBlock *block);
static void TextAppend(Widget w, char *s, int len);
static void TextInsert(Widget w, char *s, int len);
+static Bool ExceededLineLength(Widget w);
static Bool ExceededMaxLines(Widget w);
static void ScrollLine(Widget w);
@@ -168,7 +177,7 @@ static XrmOptionDescRec options[] = {
# endif
# if defined (SVR4) || defined (USE_PTS)
# include <termios.h>
-# ifndef HAS_OPENPTY
+# ifndef HAVE_OPENPTY
# include <sys/stropts.h> /* for I_PUSH */
# endif
# ifdef sun
@@ -247,8 +256,13 @@ OpenConsole(void)
{
# ifdef TIOCCONS
int on = 1;
+# ifdef USE_PRIVSEP
+ if (priv_set_console(tty_fd) != -1)
+ input = fdopen (pty_fd, "r");
+# else
if (ioctl (tty_fd, TIOCCONS, (char *) &on) != -1)
input = fdopen (pty_fd, "r");
+# endif
# else
int consfd = open("/dev/console", O_RDONLY);
if (consfd >= 0)
@@ -614,6 +628,9 @@ main(int argc, char *argv[])
{
Arg arglist[10];
Cardinal num_args;
+#ifdef USE_PRIVSEP
+ struct passwd *pw;
+#endif
XtSetLanguageProc(NULL,NULL,NULL);
top = XtInitialize ("xconsole", "XConsole", options, XtNumber (options),
@@ -621,6 +638,26 @@ main(int argc, char *argv[])
XtGetApplicationResources (top, (XtPointer)&app_resources, resources,
XtNumber (resources), NULL, 0);
+#ifdef USE_PRIVSEP
+ /* Revoke privileges if any */
+ if (getuid() == 0) {
+ /* Running as root */
+ pw = getpwnam(XCONSOLE_USER);
+ if (!pw) {
+ fprintf(stderr, "%s user not found\n", XCONSOLE_USER);
+ exit(2);
+ }
+ if (priv_init(pw->pw_uid, pw->pw_gid) < 0) {
+ fprintf(stderr, "priv_init failed\n");
+ exit(2);
+ }
+ } else
+ if (priv_init(-1, -1) < 0) {
+ fprintf(stderr, "priv_init failed\n");
+ exit(2);
+ }
+#endif
+
if (app_resources.daemon)
if (fork ()) exit (0);
XtAddActions (actions, XtNumber (actions));
@@ -659,6 +696,12 @@ main(int argc, char *argv[])
#ifdef USE_OSM
ioerror = XSetIOErrorHandler(IOError);
#endif
+
+#ifdef USE_PRIVSEP
+ if (pledge("stdio rpath sendfd recvfd", NULL) == -1)
+ err(1, "pledge");
+#endif
+
XtMainLoop ();
return 0;
}
@@ -710,6 +753,13 @@ TextAppend(Widget w, char *s, int len)
TextReplace (w, last, last, &block);
if (current == last)
XawTextSetInsertionPoint (w, last + block.length);
+ if (ExceededLineLength(w)) {
+ block.ptr = "\n";
+ block.firstPos = 0;
+ block.length = 1;
+ block.format = FMT8BIT;
+ TextReplace(w, last, last, &block);
+ }
if (ExceededMaxLines(w))
ScrollLine(w);
}
@@ -733,6 +783,21 @@ TextInsert(Widget w, char *s, int len)
}
static Bool
+ExceededLineLength(Widget w)
+{
+ XawTextPosition last_end_of_line;
+ long length = TextLength(w);
+
+ last_end_of_line = XawTextSourceScan (XawTextGetSource(w),
+ (XawTextPosition)length,
+ XawstEOL, XawsdLeft,
+ 1, TRUE);
+ if (length - last_end_of_line > XCONSOLE_MAX_LINE_LENGTH)
+ return True;
+ return False;
+}
+
+static Bool
ExceededMaxLines(Widget w)
{
XawTextPosition end_of_last_line;
@@ -791,7 +856,12 @@ ScrollLine(Widget w)
static int
get_pty(int *pty, int *tty, char *ttydev, char *ptydev)
{
-#ifdef HAS_OPENPTY
+#ifdef USE_PRIVSEP
+ if (priv_openpty(pty, tty) < 0) {
+ return 1;
+ }
+ return 0;
+#elif HAVE_OPENPTY
if (openpty(pty, tty, NULL, NULL, NULL) == -1) {
return 1;
}
|