1 /*
2  * misc utility routines
3  */
4 /*
5 
6 Copyright 1990, 1991, 1998  The Open Group
7 
8 Permission to use, copy, modify, distribute, and sell this software and its
9 documentation for any purpose is hereby granted without fee, provided that
10 the above copyright notice appear in all copies and that both that
11 copyright notice and this permission notice appear in supporting
12 documentation.
13 
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16 
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
20 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
21 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 
24 Except as contained in this notice, the name of The Open Group shall not be
25 used in advertising or otherwise to promote the sale, use or other dealings
26 in this Software without prior written authorization from The Open Group.
27 
28  * Copyright 1990, 1991 Network Computing Devices;
29  * Portions Copyright 1987 by Digital Equipment Corporation
30  *
31  * Permission to use, copy, modify, distribute, and sell this software and
32  * its documentation for any purpose is hereby granted without fee, provided
33  * that the above copyright notice appear in all copies and that both that
34  * copyright notice and this permission notice appear in supporting
35  * documentation, and that the names of Network Computing Devices, or Digital
36  * not be used in advertising or publicity pertaining to distribution
37  * of the software without specific, written prior permission.
38  *
39  * NETWORK COMPUTING DEVICES, AND DIGITAL DISCLAIM ALL WARRANTIES WITH
40  * REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
41  * MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL NETWORK COMPUTING DEVICES,
42  * OR DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
43  * DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
44  * PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS
45  * ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF
46  * THIS SOFTWARE.
47  */
48 
49 #include	"config.h"
50 
51 #define	XK_LATIN1
52 
53 #include	<difsutils.h>
54 
55 #include	<stdio.h>
56 #include	<ctype.h>
57 #include	"misc.h"
58 #include	"globals.h"
59 #include	"clientstr.h"
60 #include	<X11/fonts/fontstruct.h>
61 #include	<X11/keysymdef.h>
62 
63 #include	"authstr.h"
64 #include	"auth.h"
65 #include	"client.h"
66 #include	"dispatch.h"
67 
68 static FontResolutionPtr default_resolutions;
69 static int  num_resolutions;
70 static int  default_point_size = 120;
71 
72 AuthContextPtr
GetClientAuthorization(void)73 GetClientAuthorization(void)
74 {
75     return currentClient->auth;
76 }
77 
78 void
SetDefaultPointSize(int ps)79 SetDefaultPointSize(int ps)
80 {
81     int         i;
82 
83     default_point_size = ps;
84     for (i = 0; i < num_resolutions; i++)
85 	default_resolutions[i].point_size = ps;
86 }
87 
88 int
SetDefaultResolutions(char * str)89 SetDefaultResolutions(char *str)
90 {
91     int         num,
92                 numr = 0,
93                 n;
94     char       *s;
95     FontResolutionPtr new,
96                 nr;
97     int         state;
98 
99     s = str;
100     while (*s) {		/* count commas */
101 	if (*s == ',')
102 	    numr++;
103 	s++;
104     }
105 
106     if ((numr % 2) != 1) {	/* must be multiple of 2 + 1 */
107 	return FSBadResolution;
108     }
109     numr = (numr + 1) / 2;
110     nr = new = (FontResolutionPtr) fsalloc(sizeof(FontResolutionRec)
111 					   * numr);
112     if (!new)
113 	return FSBadAlloc;
114     s = str;
115     num = 0;
116     state = 0;
117     while (*s) {
118 	if (*s == ',') {
119 	    if (state == 0) {
120 		nr->x_resolution = num;
121 		state++;
122 	    } else {
123 		state = 0;
124 		nr->y_resolution = num;
125 		nr->point_size = default_point_size;
126 		nr++;
127 	    }
128 	    num = 0;
129 	    s++;
130 	    continue;
131 	}
132 	if (!isdigit(*s)) {
133 	    fsfree((char *) new);
134 	    return FSBadResolution;
135 	}
136 	n = *s - '0';
137 	num = num * 10 + n;
138 	s++;
139     }
140 
141     /* do the last one */
142     assert(state == 1);
143     nr->y_resolution = num;
144     nr->point_size = default_point_size;
145 
146     if (default_resolutions) {
147 	fsfree((char *) default_resolutions);
148     }
149     default_resolutions = new;
150     num_resolutions = numr;
151     return FSSuccess;
152 }
153 
154 FontResolutionPtr
GetClientResolutions(int * num)155 GetClientResolutions(int *num)
156 {
157     /* return the client's if it has them, otherwise the default values */
158     if (currentClient->num_resolutions) {
159 	*num = currentClient->num_resolutions;
160 	return (FontResolutionPtr) currentClient->resolutions;
161     } else {
162 	*num = num_resolutions;
163 	return default_resolutions;
164     }
165 }
166 
167 int
GetDefaultPointSize(void)168 GetDefaultPointSize(void)
169 {
170     FontResolutionPtr res;
171     int         num;
172 
173     res = GetClientResolutions(&num);
174     if (res)
175 	return res->point_size;
176     else
177 	return default_point_size;
178 }
179 
180 int
strncmpnocase(const char * first,const char * second,int n)181 strncmpnocase(
182     const char *first,
183     const char *second,
184     int         n)
185 {
186     register const unsigned char *ap,
187                *bp;
188 
189     for (ap = (const unsigned char *) first,
190 	    bp = (const unsigned char *) second;
191     /* SUPPRESS 112 */
192 	    n > 0 && *ap && *bp; n--, ap++, bp++) {
193 	register unsigned char a,
194 	            b;
195 
196 	/* SUPPRESS 112 */
197 	if ((a = *ap) != (b = *bp)) {
198 	    /* try lowercasing and try again */
199 
200 	    if ((a >= XK_A) && (a <= XK_Z))
201 		a += (XK_a - XK_A);
202 	    else if ((a >= XK_Agrave) && (a <= XK_Odiaeresis))
203 		a += (XK_agrave - XK_Agrave);
204 	    else if ((a >= XK_Ooblique) && (a <= XK_Thorn))
205 		a += (XK_oslash - XK_Ooblique);
206 
207 	    if ((b >= XK_A) && (b <= XK_Z))
208 		b += (XK_a - XK_A);
209 	    else if ((b >= XK_Agrave) && (b <= XK_Odiaeresis))
210 		b += (XK_agrave - XK_Agrave);
211 	    else if ((b >= XK_Ooblique) && (b <= XK_Thorn))
212 		b += (XK_oslash - XK_Ooblique);
213 
214 	    if (a != b)
215 		break;
216 	}
217     }
218     /* SUPPRESS 112 */
219     return (n ? (((int) *ap) - ((int) *bp)) : 0);
220 }
221 
222 void
NoopDDA(void)223 NoopDDA(void)
224 {
225 }
226 
227 /* block & wakeup handlers */
228 
229 typedef struct _BlockHandler {
230     FontBlockHandlerProcPtr	BlockHandler;
231     DifsWakeupFunc	WakeupHandler;
232     pointer     	blockData;
233     Bool        	deleted;
234 }           BlockHandlerRec, *BlockHandlerPtr;
235 
236 static BlockHandlerPtr handlers;
237 static int  numHandlers;
238 static int  sizeHandlers;
239 static Bool inHandler;
240 static Bool handlerDeleted;
241 
242 /* called from the OS layer */
243 void
BlockHandler(OSTimePtr pTimeout,pointer pReadmask)244 BlockHandler(
245     OSTimePtr   pTimeout,	/* DIX doesn't want to know how OS represents
246 				 * time */
247     pointer     pReadmask)	/* nor how it represents the set of
248 				 * descriptors */
249 {
250     register int i,
251                 j;
252 
253     ++inHandler;
254     for (i = 0; i < numHandlers; i++)
255 	(*handlers[i].BlockHandler) (handlers[i].blockData);
256     if (handlerDeleted) {
257 	for (i = 0; i < numHandlers;)
258 	    if (handlers[i].deleted) {
259 		for (j = i; j < numHandlers - 1; j++)
260 		    handlers[j] = handlers[j + 1];
261 		numHandlers--;
262 	    } else
263 		i++;
264     }
265     --inHandler;
266 }
267 
268 
269 void
WakeupHandler(int result,unsigned long * pReadmask)270 WakeupHandler(
271     int		result,		/* result from the wait */
272     unsigned long * pReadmask)	/* the resulting descriptor mask */
273 {
274     register int i,
275                 j;
276 
277     ++inHandler;
278     for (i = numHandlers - 1; i >= 0; i--)
279 	(*handlers[i].WakeupHandler) (handlers[i].blockData,
280 				      result, pReadmask);
281     if (handlerDeleted) {
282 	for (i = 0; i < numHandlers;)
283 	    if (handlers[i].deleted) {
284 		for (j = i; j < numHandlers - 1; j++)
285 		    handlers[j] = handlers[j + 1];
286 		numHandlers--;
287 	    } else
288 		i++;
289     }
290     --inHandler;
291 }
292 
293 /* Reentrant with BlockHandler and WakeupHandler, except wakeup won't
294  * get called until next time
295  */
296 
297 Bool
RegisterBlockAndWakeupHandlers(FontBlockHandlerProcPtr blockHandler,DifsWakeupFunc wakeupHandler,pointer blockData)298 RegisterBlockAndWakeupHandlers(
299     FontBlockHandlerProcPtr blockHandler,
300     DifsWakeupFunc wakeupHandler,
301     pointer     blockData)
302 {
303     BlockHandlerPtr new;
304 
305     if (numHandlers >= sizeHandlers) {
306 	new = (BlockHandlerPtr) fsrealloc(handlers, (numHandlers + 1) *
307 					  sizeof(BlockHandlerRec));
308 	if (!new)
309 	    return FALSE;
310 	handlers = new;
311 	sizeHandlers = numHandlers + 1;
312     }
313     handlers[numHandlers].BlockHandler = blockHandler;
314     handlers[numHandlers].WakeupHandler = wakeupHandler;
315     handlers[numHandlers].blockData = blockData;
316     numHandlers = numHandlers + 1;
317     return TRUE;
318 }
319 
320 void
RemoveBlockAndWakeupHandlers(FontBlockHandlerProcPtr blockHandler,DifsWakeupFunc wakeupHandler,pointer blockData)321 RemoveBlockAndWakeupHandlers(
322     FontBlockHandlerProcPtr blockHandler,
323     DifsWakeupFunc wakeupHandler,
324     pointer     blockData)
325 {
326     int         i;
327 
328     for (i = 0; i < numHandlers; i++)
329 	if (handlers[i].BlockHandler == blockHandler &&
330 		handlers[i].WakeupHandler == wakeupHandler &&
331 		handlers[i].blockData == blockData) {
332 	    if (inHandler) {
333 		handlerDeleted = TRUE;
334 		handlers[i].deleted = TRUE;
335 	    } else {
336 		for (; i < numHandlers - 1; i++)
337 		    handlers[i] = handlers[i + 1];
338 		numHandlers--;
339 	    }
340 	    break;
341 	}
342 }
343 
344 void
InitBlockAndWakeupHandlers(void)345 InitBlockAndWakeupHandlers(void)
346 {
347     fsfree(handlers);
348     handlers = (BlockHandlerPtr) 0;
349     numHandlers = 0;
350     sizeHandlers = 0;
351 }
352 
353 /*
354  * A general work queue.  Perform some task before the server
355  * sleeps for input.
356  */
357 
358 WorkQueuePtr workQueue;
359 static WorkQueuePtr *workQueueLast = &workQueue;
360 
361 /* ARGSUSED */
362 void
ProcessWorkQueue(void)363 ProcessWorkQueue(void)
364 {
365     WorkQueuePtr q,
366                 n,
367                 p;
368 
369     p = NULL;
370     /*
371      * Scan the work queue once, calling each function.  Those which return
372      * TRUE are removed from the queue, otherwise they will be called again.
373      * This must be reentrant with QueueWorkProc, hence the crufty usage of
374      * variables.
375      */
376     for (q = workQueue; q; q = n) {
377 	if ((*q->function) (q->client, q->closure)) {
378 	    /* remove q from the list */
379 	    n = q->next;	/* don't fetch until after func called */
380 	    if (p)
381 		p->next = n;
382 	    else
383 		workQueue = n;
384 	    fsfree(q);
385 	} else {
386 	    n = q->next;	/* don't fetch until after func called */
387 	    p = q;
388 	}
389     }
390     if (p)
391 	workQueueLast = &p->next;
392     else {
393 	workQueueLast = &workQueue;
394     }
395 }
396 
397 Bool
QueueWorkProc(Bool (* function)(ClientPtr,pointer),ClientPtr client,pointer data)398 QueueWorkProc(
399     Bool        (*function) (ClientPtr, pointer),
400     ClientPtr   client,
401     pointer     data)
402 {
403     WorkQueuePtr q;
404 
405     q = (WorkQueuePtr) fsalloc(sizeof *q);
406     if (!q)
407 	return FALSE;
408     q->function = function;
409     q->client = client;
410     q->closure = data;
411     q->next = NULL;
412     *workQueueLast = q;
413     workQueueLast = &q->next;
414     return TRUE;
415 }
416 
417 /*
418  * Manage a queue of sleeping clients, awakening them
419  * when requested, by using the OS functions IgnoreClient
420  * and AttendClient.  Note that this *ignores* the troubles
421  * with request data interleaving itself with events, but
422  * we'll leave that until a later time.
423  */
424 
425 typedef struct _SleepQueue {
426     struct _SleepQueue *next;
427     ClientPtr   client;
428     Bool        (*function) (ClientPtr, pointer);
429     pointer     closure;
430 }           SleepQueueRec, *SleepQueuePtr;
431 
432 static SleepQueuePtr sleepQueue = NULL;
433 
434 Bool
ClientSleep(ClientPtr client,Bool (* function)(ClientPtr,pointer),pointer data)435 ClientSleep(
436     ClientPtr   client,
437     Bool        (*function) (ClientPtr, pointer),
438     pointer     data)
439 {
440     SleepQueuePtr q;
441 
442     q = (SleepQueuePtr) fsalloc(sizeof *q);
443     if (!q)
444 	return FALSE;
445 
446     IgnoreClient(client);
447     q->next = sleepQueue;
448     q->client = client;
449     q->function = function;
450     q->closure = data;
451     sleepQueue = q;
452     return TRUE;
453 }
454 
455 Bool
ClientSignal(ClientPtr client)456 ClientSignal(ClientPtr client)
457 {
458     SleepQueuePtr q;
459 
460     for (q = sleepQueue; q; q = q->next)
461 	if (q->client == client) {
462 	    return QueueWorkProc(q->function, q->client, q->closure);
463 	}
464     return FALSE;
465 }
466 
467 void
ClientWakeup(ClientPtr client)468 ClientWakeup(ClientPtr client)
469 {
470     SleepQueuePtr q,
471                *prev;
472 
473     prev = &sleepQueue;
474     while ((q = *prev) != (SleepQueuePtr) 0) {
475 	if (q->client == client) {
476 	    *prev = q->next;
477 	    fsfree(q);
478 	    if (client->clientGone == CLIENT_GONE)
479 		CloseDownClient(client);
480 	    else
481 		AttendClient(client);
482 	    break;
483 	}
484 	prev = &q->next;
485     }
486 }
487 
488 Bool
ClientIsAsleep(ClientPtr client)489 ClientIsAsleep(ClientPtr client)
490 {
491     SleepQueuePtr q;
492 
493     for (q = sleepQueue; q; q = q->next)
494 	if (q->client == client)
495 	    return TRUE;
496     return FALSE;
497 }
498 
499 pointer
Xalloc(unsigned long m)500 Xalloc(unsigned long m)
501 {
502     return fsalloc(m);
503 }
504 
505 pointer
Xrealloc(pointer n,unsigned long m)506 Xrealloc(pointer n, unsigned long m)
507 {
508     return fsrealloc(n, m);
509 }
510 
511 void
Xfree(unsigned long * n)512 Xfree(unsigned long *n)
513 {
514     fsfree(n);
515 }
516 
517 pointer
Xcalloc(unsigned long n)518 Xcalloc(unsigned long n)
519 {
520     pointer ret;
521 
522     ret = fsalloc(n);
523     if (ret && n)
524 	bzero(ret, n);
525     return ret;
526 }
527 
528 int
set_font_authorizations(char ** authorizations,int * authlen,void * _client)529 set_font_authorizations(char **authorizations, int *authlen, void *_client)
530 {
531     ClientPtr client = _client;
532 #define AUTH1_NAME "hp-hostname-1"
533 #define AUTH2_NAME "hp-printername-1"
534     static char result[1024];
535     char *p;
536     AuthContextPtr acp = client->auth;
537     int len1, len2;
538 
539     if (acp != NULL && acp->authname != NULL && acp->authdata != NULL &&
540 	(!strcmp(AUTH1_NAME, acp->authname) ||
541 	 !strcmp(AUTH2_NAME, acp->authname)) &&
542 	(len1 = strlen(acp->authname) + 1) +
543 	(len2 = strlen(acp->authdata) + 1) + 2 * sizeof(short) <= 1024)
544     {
545 	p = result;
546 	*p++ = len1 >> 8;
547 	*p++ = len1 &0xff;
548 	*p++ = len2 >> 8;
549 	*p++ = len2 & 0xff;
550 	memmove( p, acp->authname, len1);
551 	p += len1;
552 	memmove( p, acp->authdata, len2);
553 	p += len2;
554 	*authlen = p - result;
555 	*authorizations = result;
556 	return 1;
557     }
558 
559     *authlen = 0;
560     return 0;
561 }
562 
563 int
client_auth_generation(ClientPtr client)564 client_auth_generation(ClientPtr client)
565 {
566     return client->auth_generation;
567 }
568