1 | /* |
---|
2 | * xdm - display manager daemon |
---|
3 | * |
---|
4 | * $XConsortium: resource.c,v 1.44 91/07/24 00:06:52 keith Exp $ |
---|
5 | * |
---|
6 | * Copyright 1988 Massachusetts Institute of Technology |
---|
7 | * |
---|
8 | * Permission to use, copy, modify, and distribute this software and its |
---|
9 | * documentation for any purpose and without fee is hereby granted, provided |
---|
10 | * that the above copyright notice appear in all copies and that both that |
---|
11 | * copyright notice and this permission notice appear in supporting |
---|
12 | * documentation, and that the name of M.I.T. not be used in advertising or |
---|
13 | * publicity pertaining to distribution of the software without specific, |
---|
14 | * written prior permission. M.I.T. makes no representations about the |
---|
15 | * suitability of this software for any purpose. It is provided "as is" |
---|
16 | * without express or implied warranty. |
---|
17 | * |
---|
18 | * Author: Keith Packard, MIT X Consortium |
---|
19 | */ |
---|
20 | |
---|
21 | /* |
---|
22 | * resource.c |
---|
23 | */ |
---|
24 | |
---|
25 | # include "dm.h" |
---|
26 | # include <X11/Intrinsic.h> |
---|
27 | # include <X11/Xmu/CharSet.h> |
---|
28 | |
---|
29 | char *config; |
---|
30 | |
---|
31 | char *servers; |
---|
32 | int request_port; |
---|
33 | int debugLevel; |
---|
34 | char *errorLogFile; |
---|
35 | int daemonMode; |
---|
36 | char *pidFile; |
---|
37 | int lockPidFile; |
---|
38 | char *authDir; |
---|
39 | int autoRescan; |
---|
40 | int removeDomainname; |
---|
41 | char *keyFile; |
---|
42 | char *accessFile; |
---|
43 | char **exportList; |
---|
44 | char *randomFile; |
---|
45 | |
---|
46 | # define DM_STRING 0 |
---|
47 | # define DM_INT 1 |
---|
48 | # define DM_BOOL 2 |
---|
49 | # define DM_ARGV 3 |
---|
50 | |
---|
51 | /* |
---|
52 | * the following constants are supposed to be set in the makefile from |
---|
53 | * parameters set util/imake.includes/site.def (or *.macros in that directory |
---|
54 | * if it is server-specific). DO NOT CHANGE THESE DEFINITIONS! |
---|
55 | */ |
---|
56 | #ifndef DEF_SERVER_LINE |
---|
57 | #define DEF_SERVER_LINE ":0 local /usr/bin/X11/X :0" |
---|
58 | #endif |
---|
59 | #ifndef XRDB_PROGRAM |
---|
60 | #define XRDB_PROGRAM "/usr/bin/X11/xrdb" |
---|
61 | #endif |
---|
62 | #ifndef DEF_SESSION |
---|
63 | #define DEF_SESSION "/usr/bin/X11/xterm -ls" |
---|
64 | #endif |
---|
65 | #ifndef DEF_USER_PATH |
---|
66 | #define DEF_USER_PATH ":/bin:/usr/bin:/usr/bin/X11:/usr/ucb" |
---|
67 | #endif |
---|
68 | #ifndef DEF_SYSTEM_PATH |
---|
69 | #define DEF_SYSTEM_PATH "/etc:/bin:/usr/bin:/usr/bin/X11:/usr/ucb" |
---|
70 | #endif |
---|
71 | #ifndef DEF_SYSTEM_SHELL |
---|
72 | #define DEF_SYSTEM_SHELL "/bin/sh" |
---|
73 | #endif |
---|
74 | #ifndef DEF_FAILSAFE_CLIENT |
---|
75 | #define DEF_FAILSAFE_CLIENT "/usr/bin/X11/xterm" |
---|
76 | #endif |
---|
77 | #ifndef DEF_XDM_CONFIG |
---|
78 | #define DEF_XDM_CONFIG "/usr/lib/X11/xdm/xdm-config" |
---|
79 | #endif |
---|
80 | #ifndef DEF_CHOOSER |
---|
81 | #define DEF_CHOOSER "/usr/lib/X11/xdm/chooser" |
---|
82 | #endif |
---|
83 | #ifndef DEF_AUTH_NAME |
---|
84 | #ifdef HASXDMAUTH |
---|
85 | #define DEF_AUTH_NAME "XDM-AUTHORIZATION-1 MIT-MAGIC-COOKIE-1" |
---|
86 | #else |
---|
87 | #define DEF_AUTH_NAME "MIT-MAGIC-COOKIE-1" |
---|
88 | #endif |
---|
89 | #endif |
---|
90 | #ifndef DEF_AUTH_DIR |
---|
91 | #define DEF_AUTH_DIR "/usr/lib/X11/xdm" |
---|
92 | #endif |
---|
93 | #ifndef DEF_USER_AUTH_DIR |
---|
94 | #define DEF_USER_AUTH_DIR "/tmp" |
---|
95 | #endif |
---|
96 | #ifndef DEF_KEY_FILE |
---|
97 | #define DEF_KEY_FILE "" |
---|
98 | #endif |
---|
99 | #ifndef DEF_ACCESS_FILE |
---|
100 | #define DEF_ACCESS_FILE "" |
---|
101 | #endif |
---|
102 | #ifndef DEF_RANDOM_FILE |
---|
103 | #define DEF_RANDOM_FILE "/dev/mem" |
---|
104 | #endif |
---|
105 | |
---|
106 | #define DEF_UDP_PORT "177" /* registered XDMCP port, dont change */ |
---|
107 | |
---|
108 | struct dmResources { |
---|
109 | char *name, *class; |
---|
110 | int type; |
---|
111 | char **dm_value; |
---|
112 | char *default_value; |
---|
113 | } DmResources[] = { |
---|
114 | "servers", "Servers", DM_STRING, &servers, |
---|
115 | DEF_SERVER_LINE, |
---|
116 | "requestPort", "RequestPort", DM_INT, (char **) &request_port, |
---|
117 | DEF_UDP_PORT, |
---|
118 | "debugLevel", "DebugLevel", DM_INT, (char **) &debugLevel, |
---|
119 | "0", |
---|
120 | "errorLogFile", "ErrorLogFile", DM_STRING, &errorLogFile, |
---|
121 | "", |
---|
122 | "daemonMode", "DaemonMode", DM_BOOL, (char **) &daemonMode, |
---|
123 | "true", |
---|
124 | "pidFile", "PidFile", DM_STRING, &pidFile, |
---|
125 | "", |
---|
126 | "lockPidFile", "LockPidFile", DM_BOOL, (char **) &lockPidFile, |
---|
127 | "true", |
---|
128 | "authDir", "authDir", DM_STRING, &authDir, |
---|
129 | DEF_AUTH_DIR, |
---|
130 | "autoRescan", "AutoRescan", DM_BOOL, (char **) &autoRescan, |
---|
131 | "true", |
---|
132 | "removeDomainname","RemoveDomainname",DM_BOOL, (char **) &removeDomainname, |
---|
133 | "true", |
---|
134 | "keyFile", "KeyFile", DM_STRING, &keyFile, |
---|
135 | DEF_KEY_FILE, |
---|
136 | "accessFile", "AccessFile", DM_STRING, &accessFile, |
---|
137 | DEF_ACCESS_FILE, |
---|
138 | "exportList", "ExportList", DM_ARGV, (char **) &exportList, |
---|
139 | "", |
---|
140 | "randomFile", "RandomFile", DM_STRING, &randomFile, |
---|
141 | DEF_RANDOM_FILE, |
---|
142 | }; |
---|
143 | |
---|
144 | # define NUM_DM_RESOURCES (sizeof DmResources / sizeof DmResources[0]) |
---|
145 | |
---|
146 | # define boffset(f) XtOffsetOf(struct display, f) |
---|
147 | |
---|
148 | struct displayResource { |
---|
149 | char *name, *class; |
---|
150 | int type; |
---|
151 | int offset; |
---|
152 | char *default_value; |
---|
153 | }; |
---|
154 | |
---|
155 | /* resources for managing the server */ |
---|
156 | |
---|
157 | struct displayResource serverResources[] = { |
---|
158 | "serverAttempts","ServerAttempts",DM_INT, boffset(serverAttempts), |
---|
159 | "1", |
---|
160 | "openDelay", "OpenDelay", DM_INT, boffset(openDelay), |
---|
161 | "15", |
---|
162 | "openRepeat", "OpenRepeat", DM_INT, boffset(openRepeat), |
---|
163 | "5", |
---|
164 | "openTimeout", "OpenTimeout", DM_INT, boffset(openTimeout), |
---|
165 | "30", |
---|
166 | "startAttempts","StartAttempts",DM_INT, boffset(startAttempts), |
---|
167 | "4", |
---|
168 | "pingInterval", "PingInterval", DM_INT, boffset(pingInterval), |
---|
169 | "5", |
---|
170 | "pingTimeout", "PingTimeout", DM_INT, boffset(pingTimeout), |
---|
171 | "5", |
---|
172 | "terminateServer","TerminateServer",DM_BOOL, boffset(terminateServer), |
---|
173 | "false", |
---|
174 | "grabServer", "GrabServer", DM_BOOL, boffset(grabServer), |
---|
175 | "false", |
---|
176 | "grabTimeout", "GrabTimeout", DM_INT, boffset(grabTimeout), |
---|
177 | "3", |
---|
178 | "resetSignal", "Signal", DM_INT, boffset(resetSignal), |
---|
179 | "1", /* SIGHUP */ |
---|
180 | "termSignal", "Signal", DM_INT, boffset(termSignal), |
---|
181 | "15", /* SIGTERM */ |
---|
182 | "resetForAuth", "ResetForAuth", DM_BOOL, boffset(resetForAuth), |
---|
183 | "false", |
---|
184 | "authorize", "Authorize", DM_BOOL, boffset(authorize), |
---|
185 | "true", |
---|
186 | "authComplain", "AuthComplain", DM_BOOL, boffset(authComplain), |
---|
187 | "true", |
---|
188 | "authName", "AuthName", DM_ARGV, boffset(authNames), |
---|
189 | DEF_AUTH_NAME, |
---|
190 | "authFile", "AuthFile", DM_STRING, boffset(clientAuthFile), |
---|
191 | "", |
---|
192 | }; |
---|
193 | |
---|
194 | # define NUM_SERVER_RESOURCES (sizeof serverResources/\ |
---|
195 | sizeof serverResources[0]) |
---|
196 | |
---|
197 | /* resources which control the session behaviour */ |
---|
198 | |
---|
199 | struct displayResource sessionResources[] = { |
---|
200 | "resources", "Resources", DM_STRING, boffset(resources), |
---|
201 | "", |
---|
202 | "xrdb", "Xrdb", DM_STRING, boffset(xrdb), |
---|
203 | XRDB_PROGRAM, |
---|
204 | "setup", "Setup", DM_STRING, boffset(setup), |
---|
205 | "", |
---|
206 | "startup", "Startup", DM_STRING, boffset(startup), |
---|
207 | "", |
---|
208 | "reset", "Reset", DM_STRING, boffset(reset), |
---|
209 | "", |
---|
210 | "session", "Session", DM_STRING, boffset(session), |
---|
211 | DEF_SESSION, |
---|
212 | "userPath", "Path", DM_STRING, boffset(userPath), |
---|
213 | DEF_USER_PATH, |
---|
214 | "systemPath", "Path", DM_STRING, boffset(systemPath), |
---|
215 | DEF_SYSTEM_PATH, |
---|
216 | "systemShell", "Shell", DM_STRING, boffset(systemShell), |
---|
217 | DEF_SYSTEM_SHELL, |
---|
218 | "failsafeClient","FailsafeClient", DM_STRING, boffset(failsafeClient), |
---|
219 | DEF_FAILSAFE_CLIENT, |
---|
220 | "userAuthDir", "UserAuthDir", DM_STRING, boffset(userAuthDir), |
---|
221 | DEF_AUTH_DIR, |
---|
222 | "chooser", "Chooser", DM_STRING, boffset(chooser), |
---|
223 | DEF_CHOOSER, |
---|
224 | }; |
---|
225 | |
---|
226 | # define NUM_SESSION_RESOURCES (sizeof sessionResources/\ |
---|
227 | sizeof sessionResources[0]) |
---|
228 | |
---|
229 | XrmDatabase DmResourceDB; |
---|
230 | |
---|
231 | GetResource (name, class, valueType, valuep, default_value) |
---|
232 | char *name, *class; |
---|
233 | int valueType; |
---|
234 | char **valuep; |
---|
235 | char *default_value; |
---|
236 | { |
---|
237 | char *type; |
---|
238 | XrmValue value; |
---|
239 | char *string, *new_string; |
---|
240 | char str_buf[50]; |
---|
241 | int len; |
---|
242 | extern char **parseArgs(); |
---|
243 | |
---|
244 | if (DmResourceDB && XrmGetResource (DmResourceDB, |
---|
245 | name, class, |
---|
246 | &type, &value)) |
---|
247 | { |
---|
248 | string = value.addr; |
---|
249 | len = value.size; |
---|
250 | } |
---|
251 | else |
---|
252 | { |
---|
253 | string = default_value; |
---|
254 | len = strlen (string); |
---|
255 | } |
---|
256 | |
---|
257 | Debug ("%s/%s value %*.*s\n", name, class, len, len, string); |
---|
258 | |
---|
259 | if (valueType == DM_STRING && *valuep) |
---|
260 | { |
---|
261 | if (strlen (*valuep) == len && !strncmp (*valuep, string, len)) |
---|
262 | return; |
---|
263 | else |
---|
264 | free (*valuep); |
---|
265 | } |
---|
266 | |
---|
267 | switch (valueType) { |
---|
268 | case DM_STRING: |
---|
269 | new_string = malloc ((unsigned) (len+1)); |
---|
270 | if (!new_string) { |
---|
271 | LogOutOfMem ("GetResource"); |
---|
272 | return; |
---|
273 | } |
---|
274 | strncpy (new_string, string, len); |
---|
275 | new_string[len] = '\0'; |
---|
276 | *(valuep) = new_string; |
---|
277 | break; |
---|
278 | case DM_INT: |
---|
279 | strncpy (str_buf, string, sizeof (str_buf)); |
---|
280 | str_buf[sizeof (str_buf)-1] = '\0'; |
---|
281 | *((int *) valuep) = atoi (str_buf); |
---|
282 | break; |
---|
283 | case DM_BOOL: |
---|
284 | strncpy (str_buf, string, sizeof (str_buf)); |
---|
285 | str_buf[sizeof (str_buf)-1] = '\0'; |
---|
286 | XmuCopyISOLatin1Lowered (str_buf, str_buf); |
---|
287 | if (!strcmp (str_buf, "true") || |
---|
288 | !strcmp (str_buf, "on") || |
---|
289 | !strcmp (str_buf, "yes")) |
---|
290 | *((int *) valuep) = 1; |
---|
291 | else if (!strcmp (str_buf, "false") || |
---|
292 | !strcmp (str_buf, "off") || |
---|
293 | !strcmp (str_buf, "no")) |
---|
294 | *((int *) valuep) = 0; |
---|
295 | break; |
---|
296 | case DM_ARGV: |
---|
297 | freeArgs (*(char ***) valuep); |
---|
298 | *((char ***) valuep) = parseArgs ((char **) 0, string); |
---|
299 | break; |
---|
300 | } |
---|
301 | } |
---|
302 | |
---|
303 | XrmOptionDescRec configTable [] = { |
---|
304 | {"-server", NULL, XrmoptionSkipArg, (caddr_t) NULL }, |
---|
305 | {"-udpPort", NULL, XrmoptionSkipArg, (caddr_t) NULL }, |
---|
306 | {"-error", NULL, XrmoptionSkipArg, (caddr_t) NULL }, |
---|
307 | {"-resources", NULL, XrmoptionSkipArg, (caddr_t) NULL }, |
---|
308 | {"-session", NULL, XrmoptionSkipArg, (caddr_t) NULL }, |
---|
309 | {"-debug", NULL, XrmoptionSkipArg, (caddr_t) NULL }, |
---|
310 | {"-xrm", NULL, XrmoptionSkipArg, (caddr_t) NULL }, |
---|
311 | {"-config", ".configFile", XrmoptionSepArg, (caddr_t) NULL } |
---|
312 | }; |
---|
313 | |
---|
314 | XrmOptionDescRec optionTable [] = { |
---|
315 | {"-server", ".servers", XrmoptionSepArg, (caddr_t) NULL }, |
---|
316 | {"-udpPort", ".requestPort", XrmoptionSepArg, (caddr_t) NULL }, |
---|
317 | {"-error", ".errorLogFile", XrmoptionSepArg, (caddr_t) NULL }, |
---|
318 | {"-resources", "*resources", XrmoptionSepArg, (caddr_t) NULL }, |
---|
319 | {"-session", "*session", XrmoptionSepArg, (caddr_t) NULL }, |
---|
320 | {"-debug", "*debugLevel", XrmoptionSepArg, (caddr_t) NULL }, |
---|
321 | {"-xrm", NULL, XrmoptionResArg, (caddr_t) NULL }, |
---|
322 | {"-daemon", ".daemonMode", XrmoptionNoArg, "true" }, |
---|
323 | {"-nodaemon", ".daemonMode", XrmoptionNoArg, "false" } |
---|
324 | }; |
---|
325 | |
---|
326 | static int originalArgc; |
---|
327 | static char **originalArgv; |
---|
328 | |
---|
329 | InitResources (argc, argv) |
---|
330 | int argc; |
---|
331 | char **argv; |
---|
332 | { |
---|
333 | XrmInitialize (); |
---|
334 | originalArgc = argc; |
---|
335 | originalArgv = argv; |
---|
336 | ReinitResources (); |
---|
337 | } |
---|
338 | |
---|
339 | ReinitResources () |
---|
340 | { |
---|
341 | int argc; |
---|
342 | char **a; |
---|
343 | char **argv; |
---|
344 | XrmDatabase newDB; |
---|
345 | |
---|
346 | argv = (char **) malloc ((originalArgc + 1) * sizeof (char *)); |
---|
347 | if (!argv) |
---|
348 | LogPanic ("no space for argument realloc\n"); |
---|
349 | for (argc = 0; argc < originalArgc; argc++) |
---|
350 | argv[argc] = originalArgv[argc]; |
---|
351 | argv[argc] = 0; |
---|
352 | if (DmResourceDB) |
---|
353 | XrmDestroyDatabase (DmResourceDB); |
---|
354 | DmResourceDB = XrmGetStringDatabase (""); |
---|
355 | /* pre-parse the command line to get the -config option, if any */ |
---|
356 | XrmParseCommand (&DmResourceDB, configTable, |
---|
357 | sizeof (configTable) / sizeof (configTable[0]), |
---|
358 | "DisplayManager", &argc, argv); |
---|
359 | GetResource ("DisplayManager.configFile", "DisplayManager.ConfigFile", |
---|
360 | DM_STRING, &config, DEF_XDM_CONFIG); |
---|
361 | newDB = XrmGetFileDatabase ( config ); |
---|
362 | if (newDB) |
---|
363 | { |
---|
364 | if (DmResourceDB) |
---|
365 | XrmDestroyDatabase (DmResourceDB); |
---|
366 | DmResourceDB = newDB; |
---|
367 | } |
---|
368 | else if (argc != originalArgc) |
---|
369 | LogError ("Can't open configuration file %s\n", config ); |
---|
370 | XrmParseCommand (&DmResourceDB, optionTable, |
---|
371 | sizeof (optionTable) / sizeof (optionTable[0]), |
---|
372 | "DisplayManager", &argc, argv); |
---|
373 | if (argc > 1) |
---|
374 | { |
---|
375 | LogError ("extra arguments on command line:"); |
---|
376 | for (a = argv + 1; *a; a++) |
---|
377 | LogError (" \"%s\"", *a); |
---|
378 | LogError ("\n"); |
---|
379 | } |
---|
380 | free (argv); |
---|
381 | } |
---|
382 | |
---|
383 | LoadDMResources () |
---|
384 | { |
---|
385 | int i; |
---|
386 | char name[1024], class[1024]; |
---|
387 | |
---|
388 | for (i = 0; i < NUM_DM_RESOURCES; i++) { |
---|
389 | sprintf (name, "DisplayManager.%s", DmResources[i].name); |
---|
390 | sprintf (class, "DisplayManager.%s", DmResources[i].class); |
---|
391 | GetResource (name, class, DmResources[i].type, |
---|
392 | (char **) DmResources[i].dm_value, |
---|
393 | DmResources[i].default_value); |
---|
394 | } |
---|
395 | } |
---|
396 | |
---|
397 | static |
---|
398 | CleanUpName (src, dst, len) |
---|
399 | char *src, *dst; |
---|
400 | int len; |
---|
401 | { |
---|
402 | while (*src) { |
---|
403 | if (--len <= 0) |
---|
404 | break; |
---|
405 | switch (*src) |
---|
406 | { |
---|
407 | case ':': |
---|
408 | case '.': |
---|
409 | *dst++ = '_'; |
---|
410 | break; |
---|
411 | default: |
---|
412 | *dst++ = *src; |
---|
413 | } |
---|
414 | ++src; |
---|
415 | } |
---|
416 | *dst = '\0'; |
---|
417 | } |
---|
418 | |
---|
419 | LoadDisplayResources (d, resources, numResources) |
---|
420 | struct display *d; |
---|
421 | struct displayResource *resources; |
---|
422 | int numResources; |
---|
423 | { |
---|
424 | int i; |
---|
425 | char name[1024], class[1024]; |
---|
426 | char dpyName[512], dpyClass[512]; |
---|
427 | |
---|
428 | CleanUpName (d->name, dpyName, sizeof (dpyName)); |
---|
429 | CleanUpName (d->class ? d->class : d->name, dpyClass, sizeof (dpyClass)); |
---|
430 | for (i = 0; i < numResources; i++) { |
---|
431 | sprintf (name, "DisplayManager.%s.%s", |
---|
432 | dpyName, resources[i].name); |
---|
433 | sprintf (class, "DisplayManager.%s.%s", |
---|
434 | dpyClass, resources[i].class); |
---|
435 | GetResource (name, class, resources[i].type, |
---|
436 | (char **) (((char *) d) + resources[i].offset), |
---|
437 | resources[i].default_value); |
---|
438 | } |
---|
439 | } |
---|
440 | |
---|
441 | LoadServerResources (d) |
---|
442 | struct display *d; |
---|
443 | { |
---|
444 | LoadDisplayResources (d, serverResources, NUM_SERVER_RESOURCES); |
---|
445 | } |
---|
446 | |
---|
447 | LoadSessionResources (d) |
---|
448 | struct display *d; |
---|
449 | { |
---|
450 | LoadDisplayResources (d, sessionResources, NUM_SESSION_RESOURCES); |
---|
451 | } |
---|