1*4882a593Smuzhiyun /*
2*4882a593Smuzhiyun * Copyright © 2014 Red Hat, Inc.
3*4882a593Smuzhiyun *
4*4882a593Smuzhiyun * Permission is hereby granted, free of charge, to any person obtaining a
5*4882a593Smuzhiyun * copy of this software and associated documentation files (the "Software"),
6*4882a593Smuzhiyun * to deal in the Software without restriction, including without limitation
7*4882a593Smuzhiyun * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8*4882a593Smuzhiyun * and/or sell copies of the Software, and to permit persons to whom the
9*4882a593Smuzhiyun * Software is furnished to do so, subject to the following conditions:
10*4882a593Smuzhiyun *
11*4882a593Smuzhiyun * The above copyright notice and this permission notice (including the next
12*4882a593Smuzhiyun * paragraph) shall be included in all copies or substantial portions of the
13*4882a593Smuzhiyun * Software.
14*4882a593Smuzhiyun *
15*4882a593Smuzhiyun * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16*4882a593Smuzhiyun * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17*4882a593Smuzhiyun * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
18*4882a593Smuzhiyun * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19*4882a593Smuzhiyun * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20*4882a593Smuzhiyun * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21*4882a593Smuzhiyun * DEALINGS IN THE SOFTWARE.
22*4882a593Smuzhiyun *
23*4882a593Smuzhiyun * Author: Hans de Goede <hdegoede@redhat.com>
24*4882a593Smuzhiyun */
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #include "dix-config.h"
27*4882a593Smuzhiyun #include "xorg-config.h"
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #include <errno.h>
30*4882a593Smuzhiyun #include <fcntl.h>
31*4882a593Smuzhiyun #include <limits.h>
32*4882a593Smuzhiyun #include <stdint.h>
33*4882a593Smuzhiyun #include <stdio.h>
34*4882a593Smuzhiyun #include <stdlib.h>
35*4882a593Smuzhiyun #include <string.h>
36*4882a593Smuzhiyun #include <sys/ioctl.h>
37*4882a593Smuzhiyun #include <sys/stat.h>
38*4882a593Smuzhiyun #ifdef HAVE_SYS_SYSMACROS_H
39*4882a593Smuzhiyun #include <sys/sysmacros.h>
40*4882a593Smuzhiyun #endif
41*4882a593Smuzhiyun #include <sys/types.h>
42*4882a593Smuzhiyun #if defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
43*4882a593Smuzhiyun #include <sys/consio.h>
44*4882a593Smuzhiyun #endif
45*4882a593Smuzhiyun #include <unistd.h>
46*4882a593Smuzhiyun #ifdef WITH_LIBDRM
47*4882a593Smuzhiyun #include <drm.h>
48*4882a593Smuzhiyun #include <xf86drm.h> /* For DRM_DEV_NAME */
49*4882a593Smuzhiyun #endif
50*4882a593Smuzhiyun
51*4882a593Smuzhiyun #include "misc.h"
52*4882a593Smuzhiyun
53*4882a593Smuzhiyun #define CONFIG_FILE SYSCONFDIR "/X11/Xwrapper.config"
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun static const char *progname;
56*4882a593Smuzhiyun
57*4882a593Smuzhiyun enum { ROOT_ONLY, CONSOLE_ONLY, ANYBODY };
58*4882a593Smuzhiyun
59*4882a593Smuzhiyun /* KISS non locale / LANG parsing isspace version */
is_space(char c)60*4882a593Smuzhiyun static int is_space(char c)
61*4882a593Smuzhiyun {
62*4882a593Smuzhiyun return c == ' ' || c == '\t' || c == '\n';
63*4882a593Smuzhiyun }
64*4882a593Smuzhiyun
strip(char * s)65*4882a593Smuzhiyun static char *strip(char *s)
66*4882a593Smuzhiyun {
67*4882a593Smuzhiyun int i;
68*4882a593Smuzhiyun
69*4882a593Smuzhiyun /* Strip leading whitespace */
70*4882a593Smuzhiyun while (s[0] && is_space(s[0]))
71*4882a593Smuzhiyun s++;
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun /* Strip trailing whitespace */
74*4882a593Smuzhiyun i = strlen(s) - 1;
75*4882a593Smuzhiyun while (i >= 0 && is_space(s[i])) {
76*4882a593Smuzhiyun s[i] = 0;
77*4882a593Smuzhiyun i--;
78*4882a593Smuzhiyun }
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun return s;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun
parse_config(int * allowed,int * needs_root_rights)83*4882a593Smuzhiyun static void parse_config(int *allowed, int *needs_root_rights)
84*4882a593Smuzhiyun {
85*4882a593Smuzhiyun FILE *f;
86*4882a593Smuzhiyun char buf[1024];
87*4882a593Smuzhiyun char *stripped, *equals, *key, *value;
88*4882a593Smuzhiyun int line = 0;
89*4882a593Smuzhiyun
90*4882a593Smuzhiyun f = fopen(CONFIG_FILE, "r");
91*4882a593Smuzhiyun if (!f)
92*4882a593Smuzhiyun return;
93*4882a593Smuzhiyun
94*4882a593Smuzhiyun while (fgets(buf, sizeof(buf), f)) {
95*4882a593Smuzhiyun line++;
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun /* Skip comments and empty lines */
98*4882a593Smuzhiyun stripped = strip(buf);
99*4882a593Smuzhiyun if (stripped[0] == '#' || stripped[0] == 0)
100*4882a593Smuzhiyun continue;
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /* Split in a key + value pair */
103*4882a593Smuzhiyun equals = strchr(stripped, '=');
104*4882a593Smuzhiyun if (!equals) {
105*4882a593Smuzhiyun fprintf(stderr, "%s: Syntax error at %s line %d\n", progname,
106*4882a593Smuzhiyun CONFIG_FILE, line);
107*4882a593Smuzhiyun exit(1);
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun *equals = 0;
110*4882a593Smuzhiyun key = strip(stripped); /* To remove trailing whitespace from key */
111*4882a593Smuzhiyun value = strip(equals + 1); /* To remove leading whitespace from val */
112*4882a593Smuzhiyun if (!key[0]) {
113*4882a593Smuzhiyun fprintf(stderr, "%s: Missing key at %s line %d\n", progname,
114*4882a593Smuzhiyun CONFIG_FILE, line);
115*4882a593Smuzhiyun exit(1);
116*4882a593Smuzhiyun }
117*4882a593Smuzhiyun if (!value[0]) {
118*4882a593Smuzhiyun fprintf(stderr, "%s: Missing value at %s line %d\n", progname,
119*4882a593Smuzhiyun CONFIG_FILE, line);
120*4882a593Smuzhiyun exit(1);
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun
123*4882a593Smuzhiyun /* And finally process */
124*4882a593Smuzhiyun if (strcmp(key, "allowed_users") == 0) {
125*4882a593Smuzhiyun if (strcmp(value, "rootonly") == 0)
126*4882a593Smuzhiyun *allowed = ROOT_ONLY;
127*4882a593Smuzhiyun else if (strcmp(value, "console") == 0)
128*4882a593Smuzhiyun *allowed = CONSOLE_ONLY;
129*4882a593Smuzhiyun else if (strcmp(value, "anybody") == 0)
130*4882a593Smuzhiyun *allowed = ANYBODY;
131*4882a593Smuzhiyun else {
132*4882a593Smuzhiyun fprintf(stderr,
133*4882a593Smuzhiyun "%s: Invalid value '%s' for 'allowed_users' at %s line %d\n",
134*4882a593Smuzhiyun progname, value, CONFIG_FILE, line);
135*4882a593Smuzhiyun exit(1);
136*4882a593Smuzhiyun }
137*4882a593Smuzhiyun }
138*4882a593Smuzhiyun else if (strcmp(key, "needs_root_rights") == 0) {
139*4882a593Smuzhiyun if (strcmp(value, "yes") == 0)
140*4882a593Smuzhiyun *needs_root_rights = 1;
141*4882a593Smuzhiyun else if (strcmp(value, "no") == 0)
142*4882a593Smuzhiyun *needs_root_rights = 0;
143*4882a593Smuzhiyun else if (strcmp(value, "auto") == 0)
144*4882a593Smuzhiyun *needs_root_rights = -1;
145*4882a593Smuzhiyun else {
146*4882a593Smuzhiyun fprintf(stderr,
147*4882a593Smuzhiyun "%s: Invalid value '%s' for 'needs_root_rights' at %s line %d\n",
148*4882a593Smuzhiyun progname, value, CONFIG_FILE, line);
149*4882a593Smuzhiyun exit(1);
150*4882a593Smuzhiyun }
151*4882a593Smuzhiyun }
152*4882a593Smuzhiyun else if (strcmp(key, "nice_value") == 0) {
153*4882a593Smuzhiyun /* Backward compatibility with older Debian Xwrapper, ignore */
154*4882a593Smuzhiyun }
155*4882a593Smuzhiyun else {
156*4882a593Smuzhiyun fprintf(stderr, "%s: Invalid key '%s' at %s line %d\n", key,
157*4882a593Smuzhiyun progname, CONFIG_FILE, line);
158*4882a593Smuzhiyun exit(1);
159*4882a593Smuzhiyun }
160*4882a593Smuzhiyun }
161*4882a593Smuzhiyun fclose(f);
162*4882a593Smuzhiyun }
163*4882a593Smuzhiyun
on_console(int fd)164*4882a593Smuzhiyun static int on_console(int fd)
165*4882a593Smuzhiyun {
166*4882a593Smuzhiyun #if defined(__linux__)
167*4882a593Smuzhiyun struct stat st;
168*4882a593Smuzhiyun int r;
169*4882a593Smuzhiyun
170*4882a593Smuzhiyun r = fstat(fd, &st);
171*4882a593Smuzhiyun if (r == 0 && S_ISCHR(st.st_mode) && major(st.st_rdev) == 4)
172*4882a593Smuzhiyun return 1;
173*4882a593Smuzhiyun #elif defined(__FreeBSD__) || defined(__FreeBSD_kernel__)
174*4882a593Smuzhiyun int idx;
175*4882a593Smuzhiyun
176*4882a593Smuzhiyun if (ioctl(fd, VT_GETINDEX, &idx) != -1)
177*4882a593Smuzhiyun return 1;
178*4882a593Smuzhiyun #else
179*4882a593Smuzhiyun #warning This program needs porting to your kernel.
180*4882a593Smuzhiyun static int seen;
181*4882a593Smuzhiyun
182*4882a593Smuzhiyun if (!seen) {
183*4882a593Smuzhiyun fprintf(stderr, "%s: Unable to determine if running on a console\n",
184*4882a593Smuzhiyun progname);
185*4882a593Smuzhiyun seen = 1;
186*4882a593Smuzhiyun }
187*4882a593Smuzhiyun #endif
188*4882a593Smuzhiyun
189*4882a593Smuzhiyun return 0;
190*4882a593Smuzhiyun }
191*4882a593Smuzhiyun
main(int argc,char * argv[])192*4882a593Smuzhiyun int main(int argc, char *argv[])
193*4882a593Smuzhiyun {
194*4882a593Smuzhiyun #ifdef WITH_LIBDRM
195*4882a593Smuzhiyun struct drm_mode_card_res res;
196*4882a593Smuzhiyun #endif
197*4882a593Smuzhiyun char buf[PATH_MAX];
198*4882a593Smuzhiyun int i, r, fd;
199*4882a593Smuzhiyun int kms_cards = 0;
200*4882a593Smuzhiyun int total_cards = 0;
201*4882a593Smuzhiyun int allowed = CONSOLE_ONLY;
202*4882a593Smuzhiyun int needs_root_rights = -1;
203*4882a593Smuzhiyun char *const empty_envp[1] = { NULL, };
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun progname = argv[0];
206*4882a593Smuzhiyun
207*4882a593Smuzhiyun parse_config(&allowed, &needs_root_rights);
208*4882a593Smuzhiyun
209*4882a593Smuzhiyun /* For non root users check if they are allowed to run the X server */
210*4882a593Smuzhiyun if (getuid() != 0) {
211*4882a593Smuzhiyun switch (allowed) {
212*4882a593Smuzhiyun case ROOT_ONLY:
213*4882a593Smuzhiyun /* Already checked above */
214*4882a593Smuzhiyun fprintf(stderr, "%s: Only root is allowed to run the X server\n", argv[0]);
215*4882a593Smuzhiyun exit(1);
216*4882a593Smuzhiyun break;
217*4882a593Smuzhiyun case CONSOLE_ONLY:
218*4882a593Smuzhiyun /* Some of stdin / stdout / stderr maybe redirected to a file */
219*4882a593Smuzhiyun for (i = STDIN_FILENO; i <= STDERR_FILENO; i++) {
220*4882a593Smuzhiyun if (on_console(i))
221*4882a593Smuzhiyun break;
222*4882a593Smuzhiyun }
223*4882a593Smuzhiyun if (i > STDERR_FILENO) {
224*4882a593Smuzhiyun fprintf(stderr, "%s: Only console users are allowed to run the X server\n", argv[0]);
225*4882a593Smuzhiyun exit(1);
226*4882a593Smuzhiyun }
227*4882a593Smuzhiyun break;
228*4882a593Smuzhiyun case ANYBODY:
229*4882a593Smuzhiyun break;
230*4882a593Smuzhiyun }
231*4882a593Smuzhiyun }
232*4882a593Smuzhiyun
233*4882a593Smuzhiyun #ifdef WITH_LIBDRM
234*4882a593Smuzhiyun /* Detect if we need root rights, except when overriden by the config */
235*4882a593Smuzhiyun if (needs_root_rights == -1) {
236*4882a593Smuzhiyun for (i = 0; i < 16; i++) {
237*4882a593Smuzhiyun snprintf(buf, sizeof(buf), DRM_DEV_NAME, DRM_DIR_NAME, i);
238*4882a593Smuzhiyun fd = open(buf, O_RDWR);
239*4882a593Smuzhiyun if (fd == -1)
240*4882a593Smuzhiyun continue;
241*4882a593Smuzhiyun
242*4882a593Smuzhiyun total_cards++;
243*4882a593Smuzhiyun
244*4882a593Smuzhiyun memset(&res, 0, sizeof(struct drm_mode_card_res));
245*4882a593Smuzhiyun r = ioctl(fd, DRM_IOCTL_MODE_GETRESOURCES, &res);
246*4882a593Smuzhiyun if (r == 0)
247*4882a593Smuzhiyun kms_cards++;
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun close(fd);
250*4882a593Smuzhiyun }
251*4882a593Smuzhiyun }
252*4882a593Smuzhiyun #endif
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun /* If we've found cards, and all cards support kms, drop root rights */
255*4882a593Smuzhiyun if (needs_root_rights == 0 || (total_cards && kms_cards == total_cards)) {
256*4882a593Smuzhiyun gid_t realgid = getgid();
257*4882a593Smuzhiyun uid_t realuid = getuid();
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun if (setresgid(-1, realgid, realgid) != 0) {
260*4882a593Smuzhiyun fprintf(stderr, "%s: Could not drop setgid privileges: %s\n",
261*4882a593Smuzhiyun progname, strerror(errno));
262*4882a593Smuzhiyun exit(1);
263*4882a593Smuzhiyun }
264*4882a593Smuzhiyun if (setresuid(-1, realuid, realuid) != 0) {
265*4882a593Smuzhiyun fprintf(stderr, "%s: Could not drop setuid privileges: %s\n",
266*4882a593Smuzhiyun progname, strerror(errno));
267*4882a593Smuzhiyun exit(1);
268*4882a593Smuzhiyun }
269*4882a593Smuzhiyun }
270*4882a593Smuzhiyun
271*4882a593Smuzhiyun snprintf(buf, sizeof(buf), "%s/Xorg", SUID_WRAPPER_DIR);
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun /* Check if the server is executable by our real uid */
274*4882a593Smuzhiyun if (access(buf, X_OK) != 0) {
275*4882a593Smuzhiyun fprintf(stderr, "%s: Missing execute permissions for %s: %s\n",
276*4882a593Smuzhiyun progname, buf, strerror(errno));
277*4882a593Smuzhiyun exit(1);
278*4882a593Smuzhiyun }
279*4882a593Smuzhiyun
280*4882a593Smuzhiyun argv[0] = buf;
281*4882a593Smuzhiyun if (getuid() == geteuid())
282*4882a593Smuzhiyun (void) execv(argv[0], argv);
283*4882a593Smuzhiyun else
284*4882a593Smuzhiyun (void) execve(argv[0], argv, empty_envp);
285*4882a593Smuzhiyun fprintf(stderr, "%s: Failed to execute %s: %s\n",
286*4882a593Smuzhiyun progname, buf, strerror(errno));
287*4882a593Smuzhiyun exit(1);
288*4882a593Smuzhiyun }
289