Debian Codegraph @

nx-X11/lib/include/xtrans/Xtransutil.c

raw
1 /*
2
3 Copyright 1993, 1994, 1998 The Open Group
4
5 Permission to use, copy, modify, distribute, and sell this software and its
6 documentation for any purpose is hereby granted without fee, provided that
7 the above copyright notice appear in all copies and that both that
8 copyright notice and this permission notice appear in supporting
9 documentation.
10
11 The above copyright notice and this permission notice shall be included
12 in all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17 IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20 OTHER DEALINGS IN THE SOFTWARE.
21
22 Except as contained in this notice, the name of The Open Group shall
23 not be used in advertising or otherwise to promote the sale, use or
24 other dealings in this Software without prior written authorization
25 from The Open Group.
26
27 * Copyright 1993, 1994 NCR Corporation - Dayton, Ohio, USA
28 *
29 * All Rights Reserved
30 *
31 * Permission to use, copy, modify, and distribute this software and its
32 * documentation for any purpose and without fee is hereby granted, 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 name NCR not be used in advertising
36 * or publicity pertaining to distribution of the software without specific,
37 * written prior permission. NCR makes no representations about the
38 * suitability of this software for any purpose. It is provided "as is"
39 * without express or implied warranty.
40 *
41 * NCRS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
42 * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
43 * NO EVENT SHALL NCR BE LIABLE FOR ANY SPECIAL, INDIRECT OR
44 * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
45 * OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
46 * NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
47 * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
48 */
49
50 /*
51 * These are some utility functions created for convenience or to provide
52 * an interface that is similar to an existing interface. These are built
53 * only using the Transport Independant API, and have no knowledge of
54 * the internal implementation.
55 */
56
57 #ifdef XTHREADS
58 #include <X11/Xthreads.h>
59 #endif
60 #ifdef WIN32
61 #include <X11/Xlibint.h>
62 #include <X11/Xwinsock.h>
63 #endif
64
65 #ifdef X11_t
66
67 /*
68 * These values come from X.h and Xauth.h, and MUST match them. Some
69 * of these values are also defined by the ChangeHost protocol message.
70 */
71
72 #define FamilyInternet 0 /* IPv4 */
73 #define FamilyDECnet 1
74 #define FamilyChaos 2
75 #define FamilyInternet6 6
76 #define FamilyAmoeba 33
77 #define FamilyLocalHost 252
78 #define FamilyKrb5Principal 253
79 #define FamilyNetname 254
80 #define FamilyLocal 256
81 #define FamilyWild 65535
82
83 /*
84 * TRANS(ConvertAddress) converts a sockaddr based address to an
85 * X authorization based address. Some of this is defined as part of
86 * the ChangeHost protocol. The rest is just done in a consistent manner.
87 */
88
89 int
90 TRANS(ConvertAddress)(int *familyp, int *addrlenp, Xtransaddr **addrp)
91
92 {
93
94 prmsg(2,"ConvertAddress(%d,%d,%p)\n",*familyp,*addrlenp,(void *) *addrp);
95
96 switch( *familyp )
97 {
98 #if defined(TCPCONN)
99 case AF_INET:
100 {
101 /*
102 * Check for the BSD hack localhost address 127.0.0.1.
103 * In this case, we are really FamilyLocal.
104 */
105
106 struct sockaddr_in saddr;
107 int len = sizeof(saddr.sin_addr.s_addr);
108 char *cp = (char *) &saddr.sin_addr.s_addr;
109
110 memcpy (&saddr, *addrp, sizeof (struct sockaddr_in));
111
112 if ((len == 4) && (cp[0] == 127) && (cp[1] == 0) &&
113 (cp[2] == 0) && (cp[3] == 1))
114 {
115 *familyp=FamilyLocal;
116 }
117 else
118 {
119 *familyp=FamilyInternet;
120 *addrlenp=len;
121 memcpy(*addrp,&saddr.sin_addr,len);
122 }
123 break;
124 }
125
126 #if defined(IPv6) && defined(AF_INET6)
127 case AF_INET6:
128 {
129 struct sockaddr_in6 saddr6;
130
131 memcpy (&saddr6, *addrp, sizeof (struct sockaddr_in6));
132
133 if (IN6_IS_ADDR_LOOPBACK(&saddr6.sin6_addr))
134 {
135 *familyp=FamilyLocal;
136 }
137 else if (IN6_IS_ADDR_V4MAPPED(&(saddr6.sin6_addr))) {
138 char *cp = (char *) &saddr6.sin6_addr.s6_addr[12];
139
140 if ((cp[0] == 127) && (cp[1] == 0) &&
141 (cp[2] == 0) && (cp[3] == 1))
142 {
143 *familyp=FamilyLocal;
144 }
145 else
146 {
147 *familyp=FamilyInternet;
148 *addrlenp = sizeof (struct in_addr);
149 memcpy(*addrp,cp,*addrlenp);
150 }
151 }
152 else
153 {
154 *familyp=FamilyInternet6;
155 *addrlenp=sizeof(saddr6.sin6_addr);
156 memcpy(*addrp,&saddr6.sin6_addr,sizeof(saddr6.sin6_addr));
157 }
158 break;
159 }
160 #endif /* IPv6 */
161 #endif /* defined(TCPCONN) */
162
163
164 #if defined(UNIXCONN) || defined(LOCALCONN)
165 case AF_UNIX:
166 {
167 *familyp=FamilyLocal;
168 break;
169 }
170 #endif /* defined(UNIXCONN) || defined(LOCALCONN) */
171
172 #if (defined(__SCO__) || defined(__UNIXWARE__)) && defined(LOCALCONN)
173 case 0:
174 {
175 *familyp=FamilyLocal;
176 break;
177 }
178 #endif
179
180 default:
181 prmsg(1,"ConvertAddress: Unknown family type %d\n",
182 *familyp);
183 return -1;
184 }
185
186
187 if (*familyp == FamilyLocal)
188 {
189 /*
190 * In the case of a local connection, we need to get the
191 * host name for authentication.
192 */
193
194 char hostnamebuf[256];
195 int len = TRANS(GetHostname) (hostnamebuf, sizeof hostnamebuf);
196
197 if (len > 0) {
198 if (*addrp && *addrlenp < (len + 1))
199 {
200 free (*addrp);
201 *addrp = NULL;
202 }
203 if (!*addrp)
204 *addrp = malloc (len + 1);
205 if (*addrp) {
206 strcpy ((char *) *addrp, hostnamebuf);
207 *addrlenp = len;
208 } else {
209 *addrlenp = 0;
210 }
211 }
212 else
213 {
214 if (*addrp)
215 free (*addrp);
216 *addrp = NULL;
217 *addrlenp = 0;
218 }
219 }
220
221 return 0;
222 }
223
224 #endif /* X11_t */
225
226 #ifdef ICE_t
227
228 /* Needed for _XGethostbyaddr usage in TRANS(GetPeerNetworkId) */
229 # if defined(TCPCONN) || defined(UNIXCONN)
230 # define X_INCLUDE_NETDB_H
231 # define XOS_USE_NO_LOCKING
232 # include <X11/Xos_r.h>
233 # endif
234
235 #include <signal.h>
236
237 char *
238 TRANS(GetMyNetworkId) (XtransConnInfo ciptr)
239
240 {
241 int family = ciptr->family;
242 char *addr = ciptr->addr;
243 char hostnamebuf[256];
244 char *networkId = NULL;
245 const char *transName = ciptr->transptr->TransName;
246
247 if (gethostname (hostnamebuf, sizeof (hostnamebuf)) < 0)
248 {
249 return (NULL);
250 }
251
252 switch (family)
253 {
254 #if defined(UNIXCONN) || defined(LOCALCONN)
255 case AF_UNIX:
256 {
257 struct sockaddr_un *saddr = (struct sockaddr_un *) addr;
258 networkId = malloc (3 + strlen (transName) +
259 strlen (hostnamebuf) + strlen (saddr->sun_path));
260 sprintf (networkId, "%s/%s:%s", transName,
261 hostnamebuf, saddr->sun_path);
262 break;
263 }
264 #endif /* defined(UNIXCONN) || defined(LOCALCONN) */
265
266 #if defined(TCPCONN)
267 case AF_INET:
268 #if defined(IPv6) && defined(AF_INET6)
269 case AF_INET6:
270 #endif
271 {
272 struct sockaddr_in *saddr = (struct sockaddr_in *) addr;
273 #if defined(IPv6) && defined(AF_INET6)
274 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *) addr;
275 #endif
276 int portnum;
277 char portnumbuf[10];
278
279
280 #if defined(IPv6) && defined(AF_INET6)
281 if (family == AF_INET6)
282 portnum = ntohs (saddr6->sin6_port);
283 else
284 #endif
285 portnum = ntohs (saddr->sin_port);
286
287 snprintf (portnumbuf, sizeof(portnumbuf), "%d", portnum);
288 networkId = malloc (3 + strlen (transName) +
289 strlen (hostnamebuf) + strlen (portnumbuf));
290 sprintf (networkId, "%s/%s:%s", transName, hostnamebuf, portnumbuf);
291 break;
292 }
293 #endif /* defined(TCPCONN) */
294
295
296 default:
297 break;
298 }
299
300 return (networkId);
301 }
302
303 #include <setjmp.h>
304 static jmp_buf env;
305
306 #ifdef SIGALRM
307 static volatile int nameserver_timedout = 0;
308
309 static void
310 nameserver_lost(int sig _X_UNUSED)
311 {
312 nameserver_timedout = 1;
313 longjmp (env, -1);
314 /* NOTREACHED */
315 }
316 #endif /* SIGALARM */
317
318
319 char *
320 TRANS(GetPeerNetworkId) (XtransConnInfo ciptr)
321
322 {
323 int family = ciptr->family;
324 char *peer_addr = ciptr->peeraddr;
325 char *hostname;
326 char addrbuf[256];
327 const char *addr = NULL;
328
329 switch (family)
330 {
331 case AF_UNSPEC:
332 #if defined(UNIXCONN) || defined(LOCALCONN)
333 case AF_UNIX:
334 {
335 if (gethostname (addrbuf, sizeof (addrbuf)) == 0)
336 addr = addrbuf;
337 break;
338 }
339 #endif /* defined(UNIXCONN) || defined(LOCALCONN) */
340
341 #if defined(TCPCONN)
342 case AF_INET:
343 #if defined(IPv6) && defined(AF_INET6)
344 case AF_INET6:
345 #endif
346 {
347 struct sockaddr_in *saddr = (struct sockaddr_in *) peer_addr;
348 #if defined(IPv6) && defined(AF_INET6)
349 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *) peer_addr;
350 #endif
351 char *address;
352 int addresslen;
353 #ifdef XTHREADS_NEEDS_BYNAMEPARAMS
354 _Xgethostbynameparams hparams;
355 #endif
356 struct hostent * volatile hostp = NULL;
357
358 #if defined(IPv6) && defined(AF_INET6)
359 if (family == AF_INET6)
360 {
361 address = (char *) &saddr6->sin6_addr;
362 addresslen = sizeof (saddr6->sin6_addr);
363 }
364 else
365 #endif
366 {
367 address = (char *) &saddr->sin_addr;
368 addresslen = sizeof (saddr->sin_addr);
369 }
370
371 #ifdef SIGALRM
372 /*
373 * gethostbyaddr can take a LONG time if the host does not exist.
374 * Assume that if it does not respond in NAMESERVER_TIMEOUT seconds
375 * that something is wrong and do not make the user wait.
376 * gethostbyaddr will continue after a signal, so we have to
377 * jump out of it.
378 */
379
380 nameserver_timedout = 0;
381 signal (SIGALRM, nameserver_lost);
382 alarm (4);
383 if (setjmp(env) == 0) {
384 #endif
385 hostp = _XGethostbyaddr (address, addresslen, family, hparams);
386 #ifdef SIGALRM
387 }
388 alarm (0);
389 #endif
390 if (hostp != NULL)
391 addr = hostp->h_name;
392 else
393 #if defined(IPv6) && defined(AF_INET6)
394 addr = inet_ntop (family, address, addrbuf, sizeof (addrbuf));
395 #else
396 addr = inet_ntoa (saddr->sin_addr);
397 #endif
398 break;
399 }
400
401 #endif /* defined(TCPCONN) */
402
403
404 default:
405 return (NULL);
406 }
407
408
409 hostname = malloc (strlen (ciptr->transptr->TransName) + strlen (addr) + 2);
410 strcpy (hostname, ciptr->transptr->TransName);
411 strcat (hostname, "/");
412 if (addr)
413 strcat (hostname, addr);
414
415 return (hostname);
416 }
417
418 #endif /* ICE_t */
419
420
421 #if defined(WIN32) && defined(TCPCONN)
422 int
423 TRANS(WSAStartup) (void)
424 {
425 static WSADATA wsadata;
426
427 prmsg (2,"WSAStartup()\n");
428
429 if (!wsadata.wVersion && WSAStartup(MAKEWORD(2,2), &wsadata))
430 return 1;
431 return 0;
432 }
433 #endif
434
435 #include <ctype.h>
436
437 #if defined(X11_t)
438 static int
439 is_numeric (const char *str)
440 {
441 int i;
442
443 for (i = 0; i < (int) strlen (str); i++)
444 if (!isdigit (str[i]))
445 return (0);
446
447 return (1);
448 }
449 #endif
450
451 #ifdef TRANS_SERVER
452 #include <sys/types.h>
453 #include <sys/stat.h>
454 #include <errno.h>
455
456 #if !defined(S_IFLNK) && !defined(S_ISLNK)
457 #undef lstat
458 #define lstat(a,b) stat(a,b)
459 #endif
460
461 #define FAIL_IF_NOMODE 1
462 #define FAIL_IF_NOT_ROOT 2
463 #define WARN_NO_ACCESS 4
464
465 /*
466 * We make the assumption that when the 'sticky' (t) bit is requested
467 * it's not save if the directory has non-root ownership or the sticky
468 * bit cannot be set and fail.
469 */
470 static int
471 trans_mkdir(const char *path, int mode)
472 {
473 struct stat buf;
474
475 if (lstat(path, &buf) != 0) {
476 if (errno != ENOENT) {
477 prmsg(1, "mkdir: ERROR: (l)stat failed for %s (%d)\n",
478 path, errno);
479 return -1;
480 }
481 /* Dir doesn't exist. Try to create it */
482
483 #if !defined(WIN32) && !defined(__CYGWIN__)
484 /*
485 * 'sticky' bit requested: assume application makes
486 * certain security implications. If effective user ID
487 * is != 0: fail as we may not be able to meet them.
488 */
489 if (geteuid() != 0) {
490 if (mode & 01000) {
491 prmsg(1, "mkdir: ERROR: euid != 0,"
492 "directory %s will not be created.\n",
493 path);
494 #ifdef FAIL_HARD
495 return -1;
496 #endif
497 } else {
498 prmsg(1, "mkdir: Cannot create %s with root ownership\n",
499 path);
500 }
501 }
502 #endif
503
504 #ifndef WIN32
505 if (mkdir(path, mode) == 0) {
506 if (chmod(path, mode)) {
507 prmsg(1, "mkdir: ERROR: Mode of %s should be set to %04o\n",
508 path, mode);
509 #ifdef FAIL_HARD
510 return -1;
511 #endif
512 }
513 #else
514 if (mkdir(path) == 0) {
515 #endif
516 } else {
517 prmsg(1, "mkdir: ERROR: Cannot create %s\n",
518 path);
519 return -1;
520 }
521
522 return 0;
523
524 } else {
525 if (S_ISDIR(buf.st_mode)) {
526 int updateOwner = 0;
527 int updateMode = 0;
528 int updatedOwner = 0;
529 int updatedMode = 0;
530 int status = 0;
531 /* Check if the directory's ownership is OK. */
532 if (buf.st_uid != 0)
533 updateOwner = 1;
534
535 /*
536 * Check if the directory's mode is OK. An exact match isn't
537 * required, just a mode that isn't more permissive than the
538 * one requested.
539 */
540 if ((~mode) & 0077 & buf.st_mode)
541 updateMode = 1;
542
543 /*
544 * If the directory is not writeable not everybody may
545 * be able to create sockets. Therefore warn if mode
546 * cannot be fixed.
547 */
548 if ((~buf.st_mode) & 0022 & mode) {
549 updateMode = 1;
550 status |= WARN_NO_ACCESS;
551 }
552
553 /*
554 * If 'sticky' bit is requested fail if owner isn't root
555 * as we assume the caller makes certain security implications
556 */
557 if (mode & 01000) {
558 status |= FAIL_IF_NOT_ROOT;
559 if (!(buf.st_mode & 01000)) {
560 status |= FAIL_IF_NOMODE;
561 updateMode = 1;
562 }
563 }
564
565 #ifdef HAS_FCHOWN
566 /*
567 * If fchown(2) and fchmod(2) are available, try to correct the
568 * directory's owner and mode. Otherwise it isn't safe to attempt
569 * to do this.
570 */
571 if (updateMode || updateOwner) {
572 int fd = -1;
573 struct stat fbuf;
574 if ((fd = open(path, O_RDONLY)) != -1) {
575 if (fstat(fd, &fbuf) == -1) {
576 prmsg(1, "mkdir: ERROR: fstat failed for %s (%d)\n",
577 path, errno);
578 close(fd);
579 return -1;
580 }
581 /*
582 * Verify that we've opened the same directory as was
583 * checked above.
584 */
585 if (!S_ISDIR(fbuf.st_mode) ||
586 buf.st_dev != fbuf.st_dev ||
587 buf.st_ino != fbuf.st_ino) {
588 prmsg(1, "mkdir: ERROR: inode for %s changed\n",
589 path);
590 close(fd);
591 return -1;
592 }
593 if (updateOwner && fchown(fd, 0, 0) == 0)
594 updatedOwner = 1;
595 if (updateMode && fchmod(fd, mode) == 0)
596 updatedMode = 1;
597 close(fd);
598 }
599 }
600 #endif
601
602 if (updateOwner && !updatedOwner) {
603 #ifdef FAIL_HARD
604 if (status & FAIL_IF_NOT_ROOT) {
605 prmsg(1, "mkdir: ERROR: Owner of %s must be set to root\n",
606 path);
607 return -1;
608 }
609 #endif
610 #if !defined(__APPLE_CC__) && !defined(__CYGWIN__)
611 prmsg(1, "mkdir: Owner of %s should be set to root\n",
612 path);
613 #endif
614 }
615
616 if (updateMode && !updatedMode) {
617 #ifdef FAIL_HARD
618 if (status & FAIL_IF_NOMODE) {
619 prmsg(1, "mkdir: ERROR: Mode of %s must be set to %04o\n",
620 path, mode);
621 return -1;
622 }
623 #endif
624 prmsg(1, "mkdir: Mode of %s should be set to %04o\n",
625 path, mode);
626 if (status & WARN_NO_ACCESS) {
627 prmsg(1, "mkdir: this may cause subsequent errors\n");
628 }
629 }
630 return 0;
631 }
632 return -1;
633 }
634
635 /* In all other cases, fail */
636 return -1;
637 }
638
639 #endif /* TRANS_SERVER */