Debian Codegraph @

nx-X11/lib/src/ConnDis.c

raw
1 /**************************************************************************/
2 /* */
3 /* Copyright (c) 2001, 2011 NoMachine (http://www.nomachine.com) */
4 /* Copyright (c) 2008-2017 Oleksandr Shneyder <[email protected]> */
5 /* Copyright (c) 2011-2022 Mike Gabriel <[email protected]>*/
6 /* Copyright (c) 2014-2019 Mihai Moldovan <[email protected]> */
7 /* Copyright (c) 2014-2022 Ulrich Sibiller <[email protected]> */
8 /* Copyright (c) 2015-2016 Qindel Group (http://www.qindel.com) */
9 /* */
10 /* nx-X11, NX protocol compression and NX extensions to this software */
11 /* are copyright of the aforementioned persons and companies. */
12 /* */
13 /* Redistribution and use of the present software is allowed according */
14 /* to terms specified in the file LICENSE which comes in the source */
15 /* distribution. */
16 /* */
17 /* All rights reserved. */
18 /* */
19 /* NOTE: This software has received contributions from various other */
20 /* contributors, only the core maintainers and supporters are listed as */
21 /* copyright holders. Please contact us, if you feel you should be listed */
22 /* as copyright holder, as well. */
23 /* */
24 /**************************************************************************/
25
26 /*
27
28 Copyright 1989, 1998 The Open Group
29
30 Permission to use, copy, modify, distribute, and sell this software and its
31 documentation for any purpose is hereby granted without fee, provided that
32 the above copyright notice appear in all copies and that both that
33 copyright notice and this permission notice appear in supporting
34 documentation.
35
36 The above copyright notice and this permission notice shall be included in
37 all copies or substantial portions of the Software.
38
39 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
40 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
41 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
42 OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
43 AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
44 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
45
46 Except as contained in this notice, the name of The Open Group shall not be
47 used in advertising or otherwise to promote the sale, use or other dealings
48 in this Software without prior written authorization from The Open Group.
49
50 */
51
52 /*
53 * This file contains operating system dependencies.
54 */
55
56 #ifdef HAVE_CONFIG_H
57 #include <config.h>
58 #endif
59 #include <nx-X11/Xlibint.h>
60 #include <nx-X11/Xtrans/Xtrans.h>
61 #include <nx-X11/Xauth.h>
62 #ifdef HASXDMAUTH
63 #include <X11/Xdmcp.h>
64 #endif
65 #include <stdio.h>
66 #include <ctype.h>
67 #include <unistd.h>
68
69 #if !defined(WIN32)
70 #include <sys/socket.h>
71 #else
72 #include <nx-X11/Xwindows.h>
73 #endif
74
75 #ifndef X_CONNECTION_RETRIES /* number retries on ECONNREFUSED */
76 #define X_CONNECTION_RETRIES 5
77 #endif
78
79 #include "Xintconn.h"
80
81 /* prototypes */
82 static void GetAuthorization(
83 XtransConnInfo trans_conn,
84 int family,
85 char *saddr,
86 int saddrlen,
87 int idisplay,
88 char **auth_namep,
89 int *auth_namelenp,
90 char **auth_datap,
91 int *auth_datalenp);
92
93 /* functions */
94 static char *copystring (const char *src, int len)
95 {
96 char *dst = Xmalloc (len + 1);
97
98 if (dst) {
99 strncpy (dst, src, len);
100 dst[len] = '\0';
101 }
102
103 return dst;
104 }
105
106 #define Xstrdup(s) copystring(s, strlen(s))
107
108 #ifdef TCPCONN
109 # define TCP_TRANS "tcp"
110 #endif
111 #ifdef UNIXCONN
112 # define UNIX_TRANS "unix"
113 #endif
114 #if defined(LOCALCONN) || defined(UNIXCONN)
115 # define LOCAL_TRANS "local"
116 #endif
117
118 /* FIXME: This should be handled through Makefile/configure logic. See configure.ac in upstream libX11 */
119
120 #ifdef UNIXCONN
121 # ifdef TCPCONN
122 # define LOCAL_TRANSPORT_LIST LOCAL_TRANS,UNIX_TRANS,TCP_TRANS
123 # else
124 # define LOCAL_TRANSPORT_LIST LOCAL_TRANS,UNIX_TRANS
125 # endif
126 #else
127 # ifdef TCPCONN
128 # define LOCAL_TRANSPORT_LIST TCP_TRANS
129 # else
130 # define LOCAL_TRANSPORT_LIST
131 # endif
132 #endif
133
134
135
136
137 /*
138 * Attempts to connect to server, given display name. Returns file descriptor
139 * (network socket) or -1 if connection fails. Display names may be of the
140 * following format:
141 *
142 * [protocol/] [hostname] : [:] displaynumber [.screennumber]
143 *
144 * A string with exactly two colons seperating hostname from the
145 * display indicates a (now unsupported) DECnet style name. Colons in
146 * the hostname may occur if an IPv6 numeric address is used as the
147 * hostname. An IPv6 numeric address may also end in a double colon,
148 * so three colons in a row indicates an IPv6 address ending in ::
149 * followed by :display. To make it easier for people to read, an
150 * IPv6 numeric address hostname may be surrounded by [ ] in a similar
151 * fashion to the IPv6 numeric address URL syntax defined by IETF RFC
152 * 2732.
153 *
154 * If no hostname and no protocol is specified, the string is interpreted
155 * as the most efficient local connection to a server on the same machine.
156 * This is usually:
157 *
158 * o shared memory
159 * o local stream
160 * o UNIX domain socket
161 * o TCP to local host
162 *
163 * This function will eventually call the X Transport Interface functions
164 * which expects the hostname in the format:
165 *
166 * [protocol/] [hostname] : [:] displaynumber
167 *
168 */
169 XtransConnInfo
170 _X11TransConnectDisplay (
171 char *display_name,
172 char **fullnamep, /* RETURN */
173 int *dpynump, /* RETURN */
174 int *screenp, /* RETURN */
175 char **auth_namep, /* RETURN */
176 int *auth_namelenp, /* RETURN */
177 char **auth_datap, /* RETURN */
178 int *auth_datalenp) /* RETURN */
179 {
180 int family;
181 int saddrlen;
182 Xtransaddr *saddr;
183 char *lastp, *lastc, *p; /* char pointers */
184 char *pprotocol = NULL; /* start of protocol name */
185 char *phostname = NULL; /* start of host of display */
186 char *pdpynum = NULL; /* start of dpynum of display */
187 char *pscrnum = NULL; /* start of screen of display */
188 int idisplay = 0; /* required display number */
189 int iscreen = 0; /* optional screen number */
190 /* int (*connfunc)(); */ /* method to create connection */
191 int len, hostlen; /* length tmp variable */
192 int retry; /* retry counter */
193 char addrbuf[128]; /* final address passed to
194 X Transport Interface */
195 char* address = addrbuf;
196 XtransConnInfo trans_conn = NULL; /* transport connection object */
197 int connect_stat;
198 #if defined(LOCALCONN) || defined(UNIXCONN) || defined(TCPCONN)
199 Bool reset_hostname = False; /* Reset hostname? */
200 char *original_hostname = NULL;
201 int local_transport_index = -1;
202 const char *local_transport[] = { LOCAL_TRANSPORT_LIST, NULL };
203 #endif
204
205 p = display_name;
206
207 saddrlen = 0; /* set so that we can clear later */
208 saddr = NULL;
209
210 #if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST)
211 fprintf(stderr, "_X11TransConnectDisplay: Called with display_name [%s].\n", display_name);
212 #endif
213
214 #ifdef NX_TRANS_SOCKET
215
216 /*
217 * Check if user selected the "nx"
218 * protocol or an "nx" hostname.
219 */
220
221 if (!strncasecmp(p, "nx/", 3) || !strcasecmp(p, "nx") ||
222 !strncasecmp(p, "nx:", 3) || !strncasecmp(p, "nx,", 3))
223 {
224 if (*(display_name + 2) == '/')
225 {
226 p += 3;
227 }
228
229 if (pprotocol) XFree(pprotocol);
230 pprotocol = copystring ("nx", 2);
231
232 if (!pprotocol) goto bad;
233
234 #ifdef NX_TRANS_TEST
235 fprintf(stderr, "_X11TransConnectDisplay: Forced protocol to [%s].\n", pprotocol);
236 #endif
237 }
238 else
239 {
240
241 #endif
242
243 /*
244 * Step 0, find the protocol. This is delimited by the optional
245 * slash ('/').
246 */
247 for (lastp = p; *p && *p != ':' && *p != '/'; p++) ;
248 if (!*p) /* must have a colon */
249 {
250 if (pprotocol) XFree(pprotocol);
251 return NULL;
252 }
253
254 if (p != lastp && *p != ':') { /* protocol given? */
255 if (pprotocol) XFree(pprotocol);
256 pprotocol = copystring (lastp, p - lastp);
257 if (!pprotocol) goto bad; /* no memory */
258 p++; /* skip the '/' */
259 } else
260 p = display_name; /* reset the pointer in
261 case no protocol was given */
262 #ifdef NX_TRANS_SOCKET
263
264 } /* End of step 0. */
265
266 /*
267 * Check if user specified the "nx" protocol or
268 * hostname is "nx" or in the form "nx,...".
269 */
270
271 if (pprotocol && !strcasecmp(pprotocol, "nx"))
272 {
273
274 #ifdef NX_TRANS_TEST
275 fprintf(stderr, "_X11TransConnectDisplay: Checking hostname [%s].\n", p);
276 #endif
277
278 /*
279 * Options can include a "display=" tuple so
280 * need to scan right to left.
281 */
282
283 lastp = p;
284 lastc = NULL;
285
286 for (; *p; p++)
287 if (*p == ':')
288 lastc = p;
289
290 /*
291 * Don't complain if no screen was provided.
292 */
293
294 if (lastc)
295 {
296 if (phostname) XFree(phostname);
297 phostname = copystring (lastp, lastc - lastp);
298
299 p = lastc;
300 }
301 else
302 {
303 if (phostname) XFree(phostname);
304 phostname = copystring (lastp, strlen(lastp));
305 }
306
307 if (!phostname) goto bad;
308
309 #ifdef NX_TRANS_TEST
310 fprintf(stderr, "_X11TransConnectDisplay: Forced hostname [%s].\n", phostname);
311 #endif
312
313 }
314 else
315 {
316
317 #endif
318
319 /*
320 * Step 1, find the hostname. This is delimited by either one colon,
321 * or two colons in the case of DECnet (DECnet Phase V allows a single
322 * colon in the hostname). (See note above regarding IPv6 numeric
323 * addresses with triple colons or [] brackets.)
324 * FIXME: we do not support DECnet anymore, so maybe remove these checks?
325 */
326
327 lastp = p;
328 lastc = NULL;
329 for (; *p; p++)
330 if (*p == ':')
331 lastc = p;
332
333 if (!lastc) /* must have a colon */
334 {
335 if (pprotocol) XFree(pprotocol);
336 return NULL;
337 }
338
339 if ((lastp != lastc) && (*(lastc - 1) == ':')
340 #if defined(IPv6) && defined(AF_INET6)
341 && ( ((lastc - 1) == lastp) || (*(lastc - 2) != ':'))
342 #endif
343 ) {
344 /* DECnet display specified */
345
346 goto bad;
347 }
348 else
349 hostlen = lastc - lastp;
350
351 if (hostlen > 0) { /* hostname given? */
352 if (phostname) XFree(phostname);
353 phostname = copystring (lastp, hostlen);
354 if (!phostname) goto bad; /* no memory */
355 }
356
357 p = lastc;
358
359 #if defined(LOCALCONN) || defined(UNIXCONN) || defined(TCPCONN)
360 /* check if phostname == localnodename AND protocol not specified */
361 if (!pprotocol && phostname) {
362 char localhostname[256];
363
364 if ((_XGetHostname (localhostname, sizeof localhostname) > 0)
365 && (strcmp(phostname, localhostname) == 0)) {
366 original_hostname = phostname;
367 phostname = NULL;
368 reset_hostname = True;
369 }
370 }
371 #endif
372
373 #ifdef NX_TRANS_SOCKET
374
375 } /* End of step 1. */
376
377 /*
378 * Check if no display was specified. In this case
379 * search the "port=n" option in NX host string.
380 */
381
382 if (*p)
383 {
384
385 #endif
386
387
388 /*
389 * Step 2, find the display number. This field is required and is
390 * delimited either by a nul or a period, depending on whether or not
391 * a screen number is present.
392 */
393
394 for (lastp = ++p; *p && isascii(*p) && isdigit(*p); p++) ;
395 if ((p == lastp) || /* required field */
396 (*p != '\0' && *p != '.') || /* invalid non-digit terminator */
397 !(pdpynum = copystring (lastp, p - lastp))) /* no memory */
398 goto bad;
399 idisplay = atoi (pdpynum);
400
401 #ifdef NX_TRANS_SOCKET
402
403 }
404 else
405 {
406 char *host = NULL;
407 char *name = NULL;
408 char *value = NULL;
409
410 #ifdef NX_TRANS_TEST
411 fprintf(stderr, "_X11TransConnectDisplay: Searching port in port [%s].\n", phostname);
412 #endif
413
414 if (!strncasecmp(phostname, "nx,", 3))
415 {
416 host = copystring(phostname + 3, strlen(phostname) - 3);
417 }
418
419 if (!host) goto bad;
420
421 idisplay = -1;
422
423 name = strtok(host, "=");
424
425 while (name)
426 {
427 value = strtok(NULL, ",");
428
429 if (value == NULL || strstr(value, "=") != NULL ||
430 strstr(name, ",") != NULL || strlen(value) >= 128)
431 {
432 if (host) Xfree(host);
433
434 goto bad;
435 }
436 else if (strcasecmp(name, "port") == 0)
437 {
438 idisplay = atoi(value);
439
440 if (pdpynum) XFree(pdpynum);
441 pdpynum = copystring(value, strlen(value));
442
443 if (!pdpynum)
444 {
445 if (host) Xfree(host);
446 goto bad;
447 }
448 break;
449 }
450
451 name = strtok(NULL, "=");
452 }
453
454 if (host) Xfree(host);
455
456 if (idisplay == -1)
457 {
458 goto bad;
459 }
460
461 } /* End of step 2. */
462
463 #endif
464
465
466 /*
467 * Step 3, find the screen number. This field is optional. It is
468 * present only if the display number was followed by a period (which
469 * we've already verified is the only non-nul character).
470 */
471
472 if (*p) {
473 for (lastp = ++p; *p && isascii(*p) && isdigit (*p); p++) ;
474 if (p != lastp) {
475 if (*p || /* non-digits */
476 !(pscrnum = copystring (lastp, p - lastp))) /* no memory */
477 goto bad;
478 iscreen = atoi (lastp);
479 }
480 }
481
482 /*
483 * At this point, we know the following information:
484 *
485 * pprotocol protocol string or NULL
486 * phostname hostname string or NULL
487 * idisplay display number
488 * iscreen screen number
489 *
490 * We can now decide which transport to use based on the ConnectionFlags
491 * build parameter the hostname string. If phostname is NULL or equals
492 * the string "local", then choose the best transport. If phostname
493 * is "unix", then choose BSD UNIX domain sockets (if configured).
494 */
495
496 #ifdef NX_TRANS_SOCKET
497
498 /*
499 * If user selected the "nx" protocol
500 * force "local" transport.
501 */
502
503 if (pprotocol && !strcasecmp(pprotocol, "nx"))
504 {
505 Xfree(pprotocol);
506 pprotocol = copystring ("local", 5);
507
508 if (!pprotocol) goto bad;
509
510 #ifdef NX_TRANS_TEST
511 fprintf(stderr, "_X11TransConnectDisplay: Converted protocol to [%s].\n", pprotocol);
512 #endif
513
514 }
515
516 #endif
517
518 #if defined(TCPCONN) || defined(UNIXCONN) || defined(LOCALCONN)
519 if (!pprotocol) {
520 #if defined(UNIXCONN)
521 if (phostname && (strcmp (phostname, "unix") == 0)) {
522 pprotocol = copystring ("unix", 4);
523 } else
524 #endif
525 #ifdef HAVE_LAUNCHD
526 if (phostname && phostname[0]=='/') {
527 pprotocol = copystring ("local", 5);
528 }
529 #endif
530 if (!phostname)
531 {
532 if (local_transport[0] != NULL) {
533 if (pprotocol) Xfree(pprotocol);
534 pprotocol = Xstrdup(local_transport[0]);
535 local_transport_index = 0;
536 }
537 }
538
539 if (!pprotocol) { /* if still not found one, tcp is our last resort */
540 pprotocol = copystring ("tcp", 3);
541 }
542 }
543 #endif
544
545
546 connect:
547 /*
548 * This seems kind of backwards, but we need to put the protocol,
549 * host, and port back together to pass to _X11TransOpenCOTSClient().
550 */
551
552 {
553 int olen = 3 + (pprotocol ? strlen(pprotocol) : 0) +
554 (phostname ? strlen(phostname) : 0) +
555 (pdpynum ? strlen(pdpynum) : 0);
556 if (olen > sizeof addrbuf) address = Xmalloc (olen);
557 }
558 if (!address) goto bad;
559
560 sprintf(address,"%s/%s:%d",
561 pprotocol ? pprotocol : "",
562 phostname ? phostname : "",
563 idisplay );
564
565 /*
566 * Make the connection, also need to get the auth address info for
567 * the connection. Do retries in case server host has hit its
568 * backlog (which, unfortunately, isn't distinguishable from there not
569 * being a server listening at all, which is why we have to not retry
570 * too many times).
571 */
572 #if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST)
573 fprintf(stderr, "_X11TransConnectDisplay: Entering connection loop.\n");
574 #endif
575 for(retry=X_CONNECTION_RETRIES; retry>=0; retry-- )
576 {
577 #if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST)
578 fprintf(stderr, "_X11TransConnectDisplay: Going to call _X11TransOpenCOTSClient(address) with address [%s].\n", address);
579 #endif
580 if ( (trans_conn = _X11TransOpenCOTSClient(address)) == NULL )
581 {
582 break;
583 }
584 #if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST)
585 fprintf(stderr, "_X11TransConnectDisplay: Going to call _X11TransConnect(trans_conn,address).\n");
586 #endif
587 if ((connect_stat = _X11TransConnect(trans_conn,address)) < 0 )
588 {
589 #if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST)
590 fprintf(stderr, "_X11TransConnectDisplay: Going to call _X11TransClose(trans_conn).\n");
591 #endif
592 _X11TransClose(trans_conn);
593 trans_conn = NULL;
594
595 if (connect_stat == TRANS_TRY_CONNECT_AGAIN)
596 continue;
597 else
598 break;
599 }
600
601 #if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST)
602 fprintf(stderr, "_X11TransConnectDisplay: Going to call _X11TransGetPeerAddr(trans_conn, &family, &saddrlen, &saddr).\n");
603 #endif
604 _X11TransGetPeerAddr(trans_conn, &family, &saddrlen, &saddr);
605
606 /*
607 * The family is given in a socket format (ie AF_INET). This
608 * will convert it to the format used by the authorization and
609 * X protocol (ie FamilyInternet).
610 */
611
612 #if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST)
613 fprintf(stderr, "_X11TransConnectDisplay: Going to call _X11TransConvertAddress(&family, &saddrlen, &saddr).\n");
614 #endif
615 if( _X11TransConvertAddress(&family, &saddrlen, &saddr) < 0 )
616 {
617 _X11TransClose(trans_conn);
618 trans_conn = NULL;
619 if (saddr)
620 {
621 free ((char *) saddr);
622 saddr = NULL;
623 }
624 continue;
625 }
626
627 break;
628 }
629
630 #if defined(NX_TRANS_SOCKET) && defined(NX_TRANS_TEST)
631 fprintf(stderr, "_X11TransConnectDisplay: Out of connection loop.\n");
632 #endif
633 if (address != addrbuf) Xfree (address);
634 address = addrbuf;
635
636 if( trans_conn == NULL )
637 goto bad;
638
639 /*
640 * Set close-on-exec so that programs that fork() doesn't get confused.
641 */
642
643 _X11TransSetOption(trans_conn,TRANS_CLOSEONEXEC,1);
644
645 /*
646 * Build the expanded display name:
647 *
648 * [host] : [:] dpy . scr \0
649 */
650 #if defined(LOCALCONN) || defined(TCPCONN) || defined(UNIXCONN)
651 /*
652 * If we computed the host name, get rid of it so that
653 * XDisplayString() and XDisplayName() agree.
654 */
655 if (reset_hostname && (phostname != original_hostname)) {
656 if (phostname) Xfree (phostname);
657 phostname = original_hostname;
658 original_hostname = NULL;
659 }
660 #endif
661 len = ((phostname ? strlen(phostname) : 0) + 1 +
662 strlen(pdpynum) + 1 + (pscrnum ? strlen(pscrnum) : 1) + 1);
663 *fullnamep = (char *) Xmalloc (len);
664 if (!*fullnamep) goto bad;
665
666 #ifdef HAVE_LAUNCHD
667 if (phostname && strlen(phostname) > 11 && !strncmp(phostname, "/tmp/launch", 11))
668 sprintf (*fullnamep, "%s%s%d",
669 (phostname ? phostname : ""),
670 ":",
671 idisplay);
672 else
673 #endif
674 sprintf (*fullnamep, "%s%s%d.%d",
675 (phostname ? phostname : ""),
676 ":",
677 idisplay, iscreen);
678
679 *dpynump = idisplay;
680 *screenp = iscreen;
681 if (pprotocol) Xfree (pprotocol);
682 if (phostname) Xfree (phostname);
683 if (pdpynum) Xfree (pdpynum);
684 if (pscrnum) Xfree (pscrnum);
685 #if defined(LOCALCONN) || defined(UNIXCONN) || defined(TCPCONN)
686 if (original_hostname) Xfree (original_hostname);
687 #endif
688 GetAuthorization(trans_conn, family, (char *) saddr, saddrlen, idisplay,
689 auth_namep, auth_namelenp, auth_datap, auth_datalenp);
690 return trans_conn;
691
692
693 /*
694 * error return; make sure everything is cleaned up.
695 */
696 bad:
697 if (trans_conn) (void)_X11TransClose(trans_conn);
698 if (saddr) free ((char *) saddr);
699 if (pprotocol) Xfree (pprotocol);
700 if (phostname) Xfree (phostname);
701 if (address && address != addrbuf) { Xfree(address); address = addrbuf; }
702
703 #if defined(LOCALCONN) || defined(UNIXCONN) || defined(TCPCONN)
704 /* If connecting to the local machine, and we failed, try again with
705 * the next transport type available, if there is one.
706 */
707 if (local_transport_index >= 0) {
708 if (local_transport[++local_transport_index] != NULL) {
709 pprotocol = Xstrdup(local_transport[local_transport_index]);
710 #ifdef TCPCONN
711 if (strcmp(pprotocol, "tcp") == 0) {
712 if (original_hostname != NULL) {
713 phostname = original_hostname;
714 original_hostname = NULL;
715 } else {
716 phostname = copystring("localhost", 9);
717 }
718 } else
719 #endif /* TCPCONN */
720 {
721 if ((phostname != NULL) && (original_hostname == NULL)) {
722 original_hostname = phostname;
723 }
724 phostname = NULL;
725 }
726 goto connect;
727 }
728 }
729
730 /* No more to try, we've failed all available local transports */
731 if (original_hostname) Xfree(original_hostname);
732 #endif /* LOCALCONN || UNIXCONN || TCPCONN */
733
734 if (pdpynum) Xfree (pdpynum);
735 if (pscrnum) Xfree (pscrnum);
736 return NULL;
737
738 }
739
740 /*
741 * This is gross, but we need it for compatiblity.
742 * The test suite relies on the following interface.
743 *
744 */
745
746 int _XConnectDisplay (
747 char *display_name,
748 char **fullnamep, /* RETURN */
749 int *dpynump, /* RETURN */
750 int *screenp, /* RETURN */
751 char **auth_namep, /* RETURN */
752 int *auth_namelenp, /* RETURN */
753 char **auth_datap, /* RETURN */
754 int *auth_datalenp) /* RETURN */
755 {
756 XtransConnInfo trans_conn;
757
758 trans_conn = _X11TransConnectDisplay (
759 display_name, fullnamep, dpynump, screenp,
760 auth_namep, auth_namelenp, auth_datap, auth_datalenp);
761
762 if (trans_conn)
763 {
764 int fd = _X11TransGetConnectionNumber (trans_conn);
765 _X11TransFreeConnInfo (trans_conn);
766 return (fd);
767 }
768 else
769 return (-1);
770 }
771
772
773 /*****************************************************************************
774 * *
775 * Connection Utility Routines *
776 * *
777 *****************************************************************************/
778
779 /*
780 * Disconnect from server.
781 */
782
783 int _XDisconnectDisplay (XtransConnInfo trans_conn)
784 {
785 _X11TransDisconnect(trans_conn);
786 _X11TransClose(trans_conn);
787 return 0;
788 }
789
790
791
792 Bool
793 _XSendClientPrefix(
794 Display *dpy,
795 xConnClientPrefix *client, /* contains count for auth_* */
796 char *auth_proto, char *auth_string, /* NOT null-terminated */
797 xConnSetupPrefix *prefix) /* prefix information */
798 {
799 int auth_length = client->nbytesAuthProto;
800 int auth_strlen = client->nbytesAuthString;
801 static char padbuf[3]; /* for padding to 4x bytes */
802 int pad;
803 struct iovec iovarray[5], *iov = iovarray;
804 int niov = 0;
805 int len = 0;
806
807 #define add_to_iov(b,l) \
808 { iov->iov_base = (b); iov->iov_len = (l); iov++, niov++; len += (l); }
809
810 add_to_iov ((caddr_t) client, SIZEOF(xConnClientPrefix));
811
812 /*
813 * write authorization protocol name and data
814 */
815 if (auth_length > 0) {
816 add_to_iov (auth_proto, auth_length);
817 pad = -auth_length & 3; /* pad auth_length to a multiple of 4 */
818 if (pad) add_to_iov (padbuf, pad);
819 }
820 if (auth_strlen > 0) {
821 add_to_iov (auth_string, auth_strlen);
822 pad = -auth_strlen & 3; /* pad auth_strlen to a multiple of 4 */
823 if (pad) add_to_iov (padbuf, pad);
824 }
825
826 #undef add_to_iov
827
828 len -= _X11TransWritev (dpy->trans_conn, iovarray, niov);
829
830 /*
831 * Set the connection non-blocking since we use select() to block.
832 */
833
834 _X11TransSetOption(dpy->trans_conn, TRANS_NONBLOCKING, 1);
835
836 if (len != 0)
837 return -1;
838 #ifdef NX_TRANS_SOCKET
839 if (_NXDisplayWriteFunction != NULL) {
840 (*_NXDisplayWriteFunction)(dpy, len);
841 }
842 #ifdef NX_TRANS_CHANGE
843 if (_NXDisplayCongestionFunction != NULL &&
844 _X11TransSocketCongestionChange(dpy->trans_conn, &congestion) == 1) {
845 (*_NXDisplayCongestionFunction)(dpy, congestion);
846 }
847 #endif
848 #endif
849
850 #ifdef K5AUTH
851 if (auth_length == 14 &&
852 !strncmp(auth_proto, "MIT-KERBEROS-5", 14))
853 {
854 return k5_clientauth(dpy, prefix);
855 } else
856 #endif
857 return 0;
858 }
859
860
861
862 #ifdef SECURE_RPC
863 #include <rpc/rpc.h>
864 #ifdef ultrix
865 #include <time.h>
866 #include <rpc/auth_des.h>
867 #endif
868 #endif
869
870 #ifdef HASXDMAUTH
871 #include <time.h>
872 #define Time_t time_t
873 #endif
874
875 /*
876 * First, a routine for setting authorization data
877 */
878 static int xauth_namelen = 0;
879 static char *xauth_name = NULL; /* NULL means use default mechanism */
880 static int xauth_datalen = 0;
881 static char *xauth_data = NULL; /* NULL means get default data */
882
883 /*
884 * This is a list of the authorization names which Xlib currently supports.
885 * Xau will choose the file entry which matches the earliest entry in this
886 * array, allowing us to prioritize these in terms of the most secure first
887 */
888
889 static char *default_xauth_names[] = {
890 #ifdef K5AUTH
891 "MIT-KERBEROS-5",
892 #endif
893 #ifdef SECURE_RPC
894 "SUN-DES-1",
895 #endif
896 #ifdef HASXDMAUTH
897 "XDM-AUTHORIZATION-1",
898 #endif
899 "MIT-MAGIC-COOKIE-1"
900 };
901
902 static _Xconst int default_xauth_lengths[] = {
903 #ifdef K5AUTH
904 14, /* strlen ("MIT-KERBEROS-5") */
905 #endif
906 #ifdef SECURE_RPC
907 9, /* strlen ("SUN-DES-1") */
908 #endif
909 #ifdef HASXDMAUTH
910 19, /* strlen ("XDM-AUTHORIZATION-1") */
911 #endif
912 18 /* strlen ("MIT-MAGIC-COOKIE-1") */
913 };
914
915 #define NUM_DEFAULT_AUTH (sizeof (default_xauth_names) / sizeof (default_xauth_names[0]))
916
917 static char **xauth_names = default_xauth_names;
918 static _Xconst int *xauth_lengths = default_xauth_lengths;
919
920 static int xauth_names_length = NUM_DEFAULT_AUTH;
921
922 void XSetAuthorization (
923 char *name, int namelen, /* *len are lengths of name and data */
924 char *data, int datalen) /* name/data are NULL or arbitrary array of bytes */
925 {
926 char *tmpname, *tmpdata;
927
928 _XLockMutex(_Xglobal_lock);
929 if (xauth_name) Xfree (xauth_name); /* free any existing data */
930 if (xauth_data) Xfree (xauth_data);
931
932 xauth_name = xauth_data = NULL; /* mark it no longer valid */
933 xauth_namelen = xauth_datalen = 0;
934 _XUnlockMutex(_Xglobal_lock);
935
936 if (namelen < 0) namelen = 0; /* check for bogus inputs */
937 if (datalen < 0) datalen = 0; /* maybe should return? */
938
939 if (namelen > 0) { /* try to allocate space */
940 tmpname = Xmalloc ((unsigned) namelen);
941 if (!tmpname) return;
942 memcpy (tmpname, name, namelen);
943 } else {
944 tmpname = NULL;
945 }
946
947 if (datalen > 0) {
948 tmpdata = Xmalloc ((unsigned) datalen);
949 if (!tmpdata) {
950 if (tmpname) (void) Xfree (tmpname);
951 return;
952 }
953 memcpy (tmpdata, data, datalen);
954 } else {
955 tmpdata = NULL;
956 }
957
958 _XLockMutex(_Xglobal_lock);
959 xauth_name = tmpname; /* and store the suckers */
960 xauth_namelen = namelen;
961 if (tmpname)
962 {
963 xauth_names = &xauth_name;
964 xauth_lengths = &xauth_namelen;
965 xauth_names_length = 1;
966 }
967 else
968 {
969 xauth_names = default_xauth_names;
970 xauth_lengths = default_xauth_lengths;
971 xauth_names_length = NUM_DEFAULT_AUTH;
972 }
973 xauth_data = tmpdata;
974 xauth_datalen = datalen;
975 _XUnlockMutex(_Xglobal_lock);
976 return;
977 }
978
979 #ifdef SECURE_RPC
980 /*
981 * Create a credential that we can send to the X server.
982 */
983 static int
984 auth_ezencode(
985 char *servername,
986 int window,
987 char *cred_out,
988 int *len)
989 {
990 AUTH *a;
991 XDR xdr;
992
993 #if defined(SVR4) && defined(sun)
994 a = authdes_seccreate(servername, window, NULL, NULL);
995 #else
996 a = (AUTH *)authdes_create(servername, window, NULL, NULL);
997 #endif
998 if (a == (AUTH *)NULL) {
999 perror("auth_create");
1000 return 0;
1001 }
1002 xdrmem_create(&xdr, cred_out, *len, XDR_ENCODE);
1003 if (AUTH_MARSHALL(a, &xdr) == FALSE) {
1004 perror("auth_marshall");
1005 AUTH_DESTROY(a);
1006 return 0;
1007 }
1008 *len = xdr_getpos(&xdr);
1009 AUTH_DESTROY(a);
1010 return 1;
1011 }
1012 #endif
1013
1014 #ifdef K5AUTH
1015 #include <com_err.h>
1016
1017 extern krb5_flags krb5_kdc_default_options;
1018
1019 /*
1020 * k5_clientauth
1021 *
1022 * Returns non-zero if the setup prefix has been read,
1023 * so we can tell XOpenDisplay to not bother looking for it by
1024 * itself.
1025 */
1026 static int k5_clientauth(dpy, sprefix)
1027 Display *dpy;
1028 xConnSetupPrefix *sprefix;
1029 {
1030 krb5_error_code retval;
1031 xReq prefix;
1032 char *buf;
1033 CARD16 plen, tlen;
1034 krb5_data kbuf;
1035 krb5_ccache cc;
1036 krb5_creds creds;
1037 krb5_principal cprinc, sprinc;
1038 krb5_ap_rep_enc_part *repl;
1039
1040 krb5_init_ets();
1041 /*
1042 * stage 0: get encoded principal and tgt from server
1043 */
1044 _XRead(dpy, (char *)&prefix, sz_xReq);
1045 if (prefix.reqType != 2 && prefix.reqType != 3)
1046 /* not an auth packet... so deal */
1047 if (prefix.reqType == 0 || prefix.reqType == 1)
1048 {
1049 memcpy((char *)sprefix, (char *)&prefix, sz_xReq);
1050 _XRead(dpy, (char *)sprefix + sz_xReq,
1051 sz_xConnSetupPrefix - sz_xReq); /* ewww... gross */
1052 return 1;
1053 }
1054 else
1055 {
1056 fprintf(stderr,
1057 "Xlib: Krb5 stage 0: got illegal connection setup success code %d\n",
1058 prefix.reqType);
1059 return -1;
1060 }
1061 if (prefix.data != 0)
1062 {
1063 fprintf(stderr, "Xlib: got out of sequence (%d) packet in Krb5 auth\n",
1064 prefix.data);
1065 return -1;
1066 }
1067 buf = (char *)malloc((prefix.length << 2) - sz_xReq);
1068 if (buf == NULL) /* malloc failed. Run away! */
1069 {
1070 fprintf(stderr, "Xlib: malloc bombed in Krb5 auth\n");
1071 return -1;
1072 }
1073 tlen = (prefix.length << 2) - sz_xReq;
1074 _XRead(dpy, buf, tlen);
1075 if (prefix.reqType == 2 && tlen < 6)
1076 {
1077 fprintf(stderr, "Xlib: Krb5 stage 0 reply from server too short\n");
1078 free(buf);
1079 return -1;
1080 }
1081 if (prefix.reqType == 2)
1082 {
1083 plen = *(CARD16 *)buf;
1084 kbuf.data = buf + 2;
1085 kbuf.length = (plen > tlen) ? tlen : plen;
1086 }
1087 else
1088 {
1089 kbuf.data = buf;
1090 kbuf.length = tlen;
1091 }
1092 if (XauKrb5Decode(kbuf, &sprinc))
1093 {
1094 free(buf);
1095 fprintf(stderr, "Xlib: XauKrb5Decode bombed\n");
1096 return -1;
1097 }
1098 if (prefix.reqType == 3) /* do some special stuff here */
1099 {
1100 char *sname, *hostname = NULL;
1101
1102 sname = (char *)malloc(krb5_princ_component(sprinc, 0)->length + 1);
1103 if (sname == NULL)
1104 {
1105 free(buf);
1106 krb5_free_principal(sprinc);
1107 fprintf(stderr, "Xlib: malloc bombed in Krb5 auth\n");
1108 return -1;
1109 }
1110 memcpy(sname, krb5_princ_component(sprinc, 0)->data,
1111 krb5_princ_component(sprinc, 0)->length);
1112 sname[krb5_princ_component(sprinc, 0)->length] = '\0';
1113 krb5_free_principal(sprinc);
1114 if (dpy->display_name[0] != ':') /* hunt for a hostname */
1115 {
1116 char *t;
1117
1118 if ((hostname = (char *)malloc(strlen(dpy->display_name)))
1119 == NULL)
1120 {
1121 free(buf);
1122 free(sname);
1123 fprintf(stderr, "Xlib: malloc bombed in Krb5 auth\n");
1124 return -1;
1125 }
1126 strcpy(hostname, dpy->display_name);
1127 t = strchr(hostname, ':');
1128 if (t == NULL)
1129 {
1130 free(buf);
1131 free(sname);
1132 free(hostname);
1133 fprintf(stderr,
1134 "Xlib: shouldn't get here! malformed display name.");
1135 return -1;
1136 }
1137 if ((t - hostname + 1 < strlen(hostname)) && t[1] == ':')
1138 t++;
1139 *t = '\0'; /* truncate the dpy number out */
1140 }
1141 retval = krb5_sname_to_principal(hostname, sname,
1142 KRB5_NT_SRV_HST, &sprinc);
1143 free(sname);
1144 if (hostname)
1145 free(hostname);
1146 if (retval)
1147 {
1148 free(buf);
1149 fprintf(stderr, "Xlib: krb5_sname_to_principal failed: %s\n",
1150 error_message(retval));
1151 return -1;
1152 }
1153 }
1154 if (retval = krb5_cc_default(&cc))
1155 {
1156 free(buf);
1157 krb5_free_principal(sprinc);
1158 fprintf(stderr, "Xlib: krb5_cc_default failed: %s\n",
1159 error_message(retval));
1160 return -1;
1161 }
1162 if (retval = krb5_cc_get_principal(cc, &cprinc))
1163 {
1164 free(buf);
1165 krb5_free_principal(sprinc);
1166 fprintf(stderr, "Xlib: cannot get Kerberos principal from \"%s\": %s\n",
1167 krb5_cc_default_name(), error_message(retval));
1168 return -1;
1169 }
1170 bzero((char *)&creds, sizeof(creds));
1171 creds.server = sprinc;
1172 creds.client = cprinc;
1173 if (prefix.reqType == 2)
1174 {
1175 creds.second_ticket.length = tlen - plen - 2;
1176 creds.second_ticket.data = buf + 2 + plen;
1177 retval = krb5_get_credentials(KRB5_GC_USER_USER |
1178 krb5_kdc_default_options,
1179 cc, &creds);
1180 }
1181 else
1182 retval = krb5_get_credentials(krb5_kdc_default_options,
1183 cc, &creds);
1184 if (retval)
1185 {
1186 free(buf);
1187 krb5_free_cred_contents(&creds);
1188 fprintf(stderr, "Xlib: cannot get Kerberos credentials: %s\n",
1189 error_message(retval));
1190 return -1;
1191 }
1192 /*
1193 * now format the ap_req to send to the server
1194 */
1195 if (prefix.reqType == 2)
1196 retval = krb5_mk_req_extended(AP_OPTS_USE_SESSION_KEY |
1197 AP_OPTS_MUTUAL_REQUIRED, NULL,
1198 0, 0, NULL, cc,
1199 &creds, NULL, &kbuf);
1200 else
1201 retval = krb5_mk_req_extended(AP_OPTS_MUTUAL_REQUIRED, NULL,
1202 0, 0, NULL, cc, &creds, NULL,
1203 &kbuf);
1204 free(buf);
1205 if (retval) /* Some manner of Kerberos lossage */
1206 {
1207 krb5_free_cred_contents(&creds);
1208 fprintf(stderr, "Xlib: krb5_mk_req_extended failed: %s\n",
1209 error_message(retval));
1210 return -1;
1211 }
1212 prefix.reqType = 1;
1213 prefix.data = 0;
1214 prefix.length = (kbuf.length + sz_xReq + 3) >> 2;
1215 /*
1216 * stage 1: send ap_req to server
1217 */
1218 _XSend(dpy, (char *)&prefix, sz_xReq);
1219 _XSend(dpy, (char *)kbuf.data, kbuf.length);
1220 free(kbuf.data);
1221 /*
1222 * stage 2: get ap_rep from server to mutually authenticate
1223 */
1224 _XRead(dpy, (char *)&prefix, sz_xReq);
1225 if (prefix.reqType != 2)
1226 if (prefix.reqType == 0 || prefix.reqType == 1)
1227 {
1228 memcpy((char *)sprefix, (char *)&prefix, sz_xReq);
1229 _XRead(dpy, (char *)sprefix + sz_xReq,
1230 sz_xConnSetupPrefix - sz_xReq);
1231 return 1;
1232 }
1233 else
1234 {
1235 fprintf(stderr,
1236 "Xlib: Krb5 stage 2: got illegal connection setup success code %d\n",
1237 prefix.reqType);
1238 return -1;
1239 }
1240 if (prefix.data != 2)
1241 return -1;
1242 kbuf.length = (prefix.length << 2) - sz_xReq;
1243 kbuf.data = (char *)malloc(kbuf.length);
1244 if (kbuf.data == NULL)
1245 {
1246 fprintf(stderr, "Xlib: malloc bombed in Krb5 auth\n");
1247 return -1;
1248 }
1249 _XRead(dpy, (char *)kbuf.data, kbuf.length);
1250 retval = krb5_rd_rep(&kbuf, &creds.keyblock, &repl);
1251 if (retval)
1252 {
1253 free(kbuf.data);
1254 fprintf(stderr, "Xlib: krb5_rd_rep failed: %s\n",
1255 error_message(retval));
1256 return -1;
1257 }
1258 free(kbuf.data);
1259 /*
1260 * stage 3: send a short ack to the server and return
1261 */
1262 prefix.reqType = 3;
1263 prefix.data = 0;
1264 prefix.length = sz_xReq >> 2;
1265 _XSend(dpy, (char *)&prefix, sz_xReq);
1266 return 0;
1267 }
1268 #endif /* K5AUTH */
1269
1270 static void
1271 GetAuthorization(
1272 XtransConnInfo trans_conn,
1273 int family,
1274 char *saddr,
1275 int saddrlen,
1276 int idisplay,
1277 char **auth_namep, /* RETURN */
1278 int *auth_namelenp, /* RETURN */
1279 char **auth_datap, /* RETURN */
1280 int *auth_datalenp) /* RETURN */
1281 {
1282 #ifdef SECURE_RPC
1283 char rpc_cred[MAX_AUTH_BYTES];
1284 #endif
1285 #ifdef HASXDMAUTH
1286 unsigned char xdmcp_data[192/8];
1287 #endif
1288 char *auth_name;
1289 int auth_namelen;
1290 unsigned char *auth_data;
1291 int auth_datalen;
1292 Xauth *authptr = NULL;
1293
1294 /*
1295 * Look up the authorization protocol name and data if necessary.
1296 */
1297 if (xauth_name && xauth_data) {
1298 auth_namelen = xauth_namelen;
1299 auth_name = xauth_name;
1300 auth_datalen = xauth_datalen;
1301 auth_data = (unsigned char *) xauth_data;
1302 } else {
1303 char dpynumbuf[40]; /* big enough to hold 2^64 and more */
1304 (void) sprintf (dpynumbuf, "%d", idisplay);
1305
1306 authptr = XauGetBestAuthByAddr ((unsigned short) family,
1307 (unsigned short) saddrlen,
1308 saddr,
1309 (unsigned short) strlen (dpynumbuf),
1310 dpynumbuf,
1311 xauth_names_length,
1312 xauth_names,
1313 xauth_lengths);
1314 if (authptr) {
1315 auth_namelen = authptr->name_length;
1316 auth_name = (char *)authptr->name;
1317 auth_datalen = authptr->data_length;
1318 auth_data = (unsigned char *) authptr->data;
1319 } else {
1320 auth_namelen = 0;
1321 auth_name = NULL;
1322 auth_datalen = 0;
1323 auth_data = NULL;
1324 }
1325 }
1326 #ifdef HASXDMAUTH
1327 /*
1328 * build XDM-AUTHORIZATION-1 data
1329 */
1330 if (auth_namelen == 19 && !strncmp (auth_name, "XDM-AUTHORIZATION-1", 19))
1331 {
1332 int i, j;
1333 Time_t now;
1334 int family, addrlen;
1335 Xtransaddr *addr = NULL;
1336
1337 for (j = 0; j < 8; j++)
1338 xdmcp_data[j] = auth_data[j];
1339
1340 _X11TransGetMyAddr(trans_conn, &family, &addrlen, &addr);
1341
1342 switch( family )
1343 {
1344 #ifdef AF_INET
1345 case AF_INET:
1346 {
1347 /*
1348 * addr will contain a sockaddr_in with all
1349 * of the members already in network byte order.
1350 */
1351
1352 for(i=4; i<8; i++) /* do sin_addr */
1353 xdmcp_data[j++] = ((char *)addr)[i];
1354 for(i=2; i<4; i++) /* do sin_port */
1355 xdmcp_data[j++] = ((char *)addr)[i];
1356 break;
1357 }
1358 #endif /* AF_INET */
1359 #if defined(IPv6) && defined(AF_INET6)
1360 case AF_INET6:
1361 /* XXX This should probably never happen */
1362 {
1363 unsigned char ipv4mappedprefix[] = {
1364 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff };
1365
1366 /* In the case of v4 mapped addresses send the v4
1367 part of the address - addr is already in network byte order */
1368 if (memcmp((char*)addr+8, ipv4mappedprefix, 12) == 0) {
1369 for (i = 20 ; i < 24; i++)
1370 xdmcp_data[j++] = ((char *)addr)[i];
1371
1372 /* Port number */
1373 for (i=2; i<4; i++)
1374 xdmcp_data[j++] = ((char *)addr)[i];
1375 } else {
1376 /* Fake data to keep the data aligned. Otherwise the
1377 the server will bail about incorrect timing data */
1378 for (i = 0; i < 6; i++) {
1379 xdmcp_data[j++] = 0;
1380 }
1381 }
1382 break;
1383 }
1384 #endif /* AF_INET6 */
1385 #ifdef AF_UNIX
1386 case AF_UNIX:
1387 {
1388 /*
1389 * We don't use the sockaddr_un for this encoding.
1390 * Instead, we create a sockaddr_in filled with
1391 * a decreasing counter for the address, and the
1392 * pid for the port.
1393 */
1394
1395 static unsigned long unix_addr = 0xFFFFFFFF;
1396 unsigned long the_addr;
1397 unsigned short the_port;
1398 unsigned long the_utime;
1399 struct timeval tp;
1400
1401 X_GETTIMEOFDAY(&tp);
1402 _XLockMutex(_Xglobal_lock);
1403 the_addr = unix_addr--;
1404 _XUnlockMutex(_Xglobal_lock);
1405 the_utime = (unsigned long) tp.tv_usec;
1406 the_port = getpid ();
1407
1408 xdmcp_data[j++] = (the_utime >> 24) & 0xFF;
1409 xdmcp_data[j++] = (the_utime >> 16) & 0xFF;
1410 xdmcp_data[j++] = ((the_utime >> 8) & 0xF0)
1411 | ((the_addr >> 8) & 0x0F);
1412 xdmcp_data[j++] = (the_addr >> 0) & 0xFF;
1413 xdmcp_data[j++] = (the_port >> 8) & 0xFF;
1414 xdmcp_data[j++] = (the_port >> 0) & 0xFF;
1415 break;
1416 }
1417 #endif /* AF_UNIX */
1418 default:
1419 /*
1420 * Need to return some kind of errro status here.
1421 * maybe a NULL auth??
1422 */
1423 break;
1424 } /* switch */
1425
1426 if (addr)
1427 free ((char *) addr);
1428
1429 time (&now);
1430 xdmcp_data[j++] = (now >> 24) & 0xFF;
1431 xdmcp_data[j++] = (now >> 16) & 0xFF;
1432 xdmcp_data[j++] = (now >> 8) & 0xFF;
1433 xdmcp_data[j++] = (now >> 0) & 0xFF;
1434 while (j < 192 / 8)
1435 xdmcp_data[j++] = 0;
1436 _XLockMutex(_Xglobal_lock);
1437 /* this function might use static data, hence the lock around it */
1438 XdmcpWrap (xdmcp_data, auth_data + 8,
1439 xdmcp_data, j);
1440 _XUnlockMutex(_Xglobal_lock);
1441 auth_data = xdmcp_data;
1442 auth_datalen = j;
1443 }
1444 #endif /* HASXDMAUTH */
1445 #ifdef SECURE_RPC
1446 /*
1447 * The SUN-DES-1 authorization protocol uses the
1448 * "secure RPC" mechanism in SunOS 4.0+.
1449 */
1450 if (auth_namelen == 9 && !strncmp(auth_name, "SUN-DES-1", 9)) {
1451 char servernetname[MAXNETNAMELEN + 1];
1452
1453 /*
1454 * Copy over the server's netname from the authorization
1455 * data field filled in by XauGetAuthByAddr().
1456 */
1457 if (auth_datalen > MAXNETNAMELEN) {
1458 auth_datalen = 0;
1459 auth_data = NULL;
1460 } else {
1461 memcpy(servernetname, auth_data, auth_datalen);
1462 servernetname[auth_datalen] = '\0';
1463
1464 auth_datalen = sizeof (rpc_cred);
1465 if (auth_ezencode(servernetname, 100, rpc_cred,
1466 &auth_datalen))
1467 auth_data = (unsigned char *) rpc_cred;
1468 else {
1469 auth_datalen = 0;
1470 auth_data = NULL;
1471 }
1472 }
1473 }
1474 #endif
1475 if (saddr) free ((char *) saddr);
1476 if ((*auth_namelenp = auth_namelen))
1477 {
1478 if ((*auth_namep = Xmalloc(auth_namelen)))
1479 memcpy(*auth_namep, auth_name, auth_namelen);
1480 else
1481 *auth_namelenp = 0;
1482 }
1483 else
1484 *auth_namep = NULL;
1485 if ((*auth_datalenp = auth_datalen))
1486 {
1487 if ((*auth_datap = Xmalloc(auth_datalen)))
1488 memcpy(*auth_datap, auth_data, auth_datalen);
1489 else
1490 *auth_datalenp = 0;
1491 }
1492 else
1493 *auth_datap = NULL;
1494 if (authptr) XauDisposeAuth (authptr);
1495 }