3 Copyright 1993, 1994, 1998 The Open Group
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
11 The above copyright notice and this permission notice shall be included
12 in all copies or substantial portions of the Software.
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.
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
27 * Copyright 1993, 1994 NCR Corporation - Dayton, Ohio, USA
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.
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.
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.
58 #include <X11/Xthreads.h>
61 #include <X11/Xlibint.h>
62 #include <X11/Xwinsock.h>
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.
72 #define FamilyInternet 0 /* IPv4 */
73 #define FamilyDECnet 1
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
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.
90 TRANS(ConvertAddress)(int *familyp, int *addrlenp, Xtransaddr **addrp)
94 prmsg(2,"ConvertAddress(%d,%d,%p)\n",*familyp,*addrlenp,*addrp);
98 #if defined(TCPCONN) || defined(STREAMSCONN)
102 * Check for the BSD hack localhost address 127.0.0.1.
103 * In this case, we are really FamilyLocal.
106 struct sockaddr_in saddr;
107 int len = sizeof(saddr.sin_addr.s_addr);
108 char *cp = (char *) &saddr.sin_addr.s_addr;
110 memcpy (&saddr, *addrp, sizeof (struct sockaddr_in));
112 if ((len == 4) && (cp[0] == 127) && (cp[1] == 0) &&
113 (cp[2] == 0) && (cp[3] == 1))
115 *familyp=FamilyLocal;
119 *familyp=FamilyInternet;
121 memcpy(*addrp,&saddr.sin_addr,len);
126 #if defined(IPv6) && defined(AF_INET6)
129 struct sockaddr_in6 saddr6;
131 memcpy (&saddr6, *addrp, sizeof (struct sockaddr_in6));
133 if (IN6_IS_ADDR_LOOPBACK(&saddr6.sin6_addr))
135 *familyp=FamilyLocal;
137 else if (IN6_IS_ADDR_V4MAPPED(&(saddr6.sin6_addr))) {
138 char *cp = (char *) &saddr6.sin6_addr.s6_addr[12];
140 if ((cp[0] == 127) && (cp[1] == 0) &&
141 (cp[2] == 0) && (cp[3] == 1))
143 *familyp=FamilyLocal;
147 *familyp=FamilyInternet;
148 *addrlenp = sizeof (struct in_addr);
149 memcpy(*addrp,cp,*addrlenp);
154 *familyp=FamilyInternet6;
155 *addrlenp=sizeof(saddr6.sin6_addr);
156 memcpy(*addrp,&saddr6.sin6_addr,sizeof(saddr6.sin6_addr));
161 #endif /* defined(TCPCONN) || defined(STREAMSCONN) */
164 #if defined(UNIXCONN) || defined(LOCALCONN)
167 *familyp=FamilyLocal;
170 #endif /* defined(UNIXCONN) || defined(LOCALCONN) */
172 #if (defined(__SCO__) || defined(__UNIXWARE__)) && defined(LOCALCONN)
175 *familyp=FamilyLocal;
181 prmsg(1,"ConvertAddress: Unknown family type %d\n",
187 if (*familyp == FamilyLocal)
190 * In the case of a local connection, we need to get the
191 * host name for authentication.
194 char hostnamebuf[256];
195 int len = TRANS(GetHostname) (hostnamebuf, sizeof hostnamebuf);
198 if (*addrp && *addrlenp < (len + 1))
204 *addrp = malloc (len + 1);
206 strcpy ((char *) *addrp, hostnamebuf);
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>
238 TRANS(GetMyNetworkId) (XtransConnInfo ciptr)
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;
247 if (gethostname (hostnamebuf, sizeof (hostnamebuf)) < 0)
254 #if defined(UNIXCONN) || defined(STREAMSCONN) || defined(LOCALCONN)
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);
264 #endif /* defined(UNIXCONN) || defined(STREAMSCONN) || defined(LOCALCONN) */
266 #if defined(TCPCONN) || defined(STREAMSCONN)
268 #if defined(IPv6) && defined(AF_INET6)
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;
280 #if defined(IPv6) && defined(AF_INET6)
281 if (family == AF_INET6)
282 portnum = ntohs (saddr6->sin6_port);
285 portnum = ntohs (saddr->sin_port);
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);
293 #endif /* defined(TCPCONN) || defined(STREAMSCONN) */
307 static volatile int nameserver_timedout = 0;
310 #ifdef RETSIGTYPE /* set by autoconf AC_TYPE_SIGNAL */
313 #ifdef SIGNALRETURNSINT
319 nameserver_lost(int sig _X_UNUSED)
321 nameserver_timedout = 1;
324 #ifdef SIGNALRETURNSINT
325 return -1; /* for picky compilers */
328 #endif /* SIGALARM */
332 TRANS(GetPeerNetworkId) (XtransConnInfo ciptr)
335 int family = ciptr->family;
336 char *peer_addr = ciptr->peeraddr;
339 const char *addr = NULL;
344 #if defined(UNIXCONN) || defined(STREAMSCONN) || defined(LOCALCONN)
347 if (gethostname (addrbuf, sizeof (addrbuf)) == 0)
351 #endif /* defined(UNIXCONN) || defined(STREAMSCONN) || defined(LOCALCONN) */
353 #if defined(TCPCONN) || defined(STREAMSCONN)
355 #if defined(IPv6) && defined(AF_INET6)
359 struct sockaddr_in *saddr = (struct sockaddr_in *) peer_addr;
360 #if defined(IPv6) && defined(AF_INET6)
361 struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *) peer_addr;
365 #ifdef XTHREADS_NEEDS_BYNAMEPARAMS
366 _Xgethostbynameparams hparams;
368 struct hostent * volatile hostp = NULL;
370 #if defined(IPv6) && defined(AF_INET6)
371 if (family == AF_INET6)
373 address = (char *) &saddr6->sin6_addr;
374 addresslen = sizeof (saddr6->sin6_addr);
379 address = (char *) &saddr->sin_addr;
380 addresslen = sizeof (saddr->sin_addr);
385 * gethostbyaddr can take a LONG time if the host does not exist.
386 * Assume that if it does not respond in NAMESERVER_TIMEOUT seconds
387 * that something is wrong and do not make the user wait.
388 * gethostbyaddr will continue after a signal, so we have to
392 nameserver_timedout = 0;
393 signal (SIGALRM, nameserver_lost);
395 if (setjmp(env) == 0) {
397 hostp = _XGethostbyaddr (address, addresslen, family, hparams);
403 addr = hostp->h_name;
405 #if defined(IPv6) && defined(AF_INET6)
406 addr = inet_ntop (family, address, addrbuf, sizeof (addrbuf));
408 addr = inet_ntoa (saddr->sin_addr);
413 #endif /* defined(TCPCONN) || defined(STREAMSCONN) */
421 hostname = malloc (strlen (ciptr->transptr->TransName) + strlen (addr) + 2);
422 strcpy (hostname, ciptr->transptr->TransName);
423 strcat (hostname, "/");
425 strcat (hostname, addr);
433 #if defined(WIN32) && defined(TCPCONN)
435 TRANS(WSAStartup) (void)
437 static WSADATA wsadata;
439 prmsg (2,"WSAStartup()\n");
441 if (!wsadata.wVersion && WSAStartup(MAKEWORD(2,2), &wsadata))
450 is_numeric (const char *str)
454 for (i = 0; i < (int) strlen (str); i++)
455 if (!isdigit (str[i]))
462 #include <sys/types.h>
463 #include <sys/stat.h>
466 #if !defined(S_IFLNK) && !defined(S_ISLNK)
468 #define lstat(a,b) stat(a,b)
471 #define FAIL_IF_NOMODE 1
472 #define FAIL_IF_NOT_ROOT 2
473 #define WARN_NO_ACCESS 4
476 * We make the assumption that when the 'sticky' (t) bit is requested
477 * it's not save if the directory has non-root ownership or the sticky
478 * bit cannot be set and fail.
481 trans_mkdir(const char *path, int mode)
485 if (lstat(path, &buf) != 0) {
486 if (errno != ENOENT) {
487 prmsg(1, "mkdir: ERROR: (l)stat failed for %s (%d)\n",
491 /* Dir doesn't exist. Try to create it */
493 #if !defined(WIN32) && !defined(__CYGWIN__)
495 * 'sticky' bit requested: assume application makes
496 * certain security implications. If effective user ID
497 * is != 0: fail as we may not be able to meet them.
499 if (geteuid() != 0) {
501 prmsg(1, "mkdir: ERROR: euid != 0,"
502 "directory %s will not be created.\n",
508 prmsg(1, "mkdir: Cannot create %s with root ownership\n",
515 if (mkdir(path, mode) == 0) {
516 if (chmod(path, mode)) {
517 prmsg(1, "mkdir: ERROR: Mode of %s should be set to %04o\n",
524 if (mkdir(path) == 0) {
527 prmsg(1, "mkdir: ERROR: Cannot create %s\n",
535 if (S_ISDIR(buf.st_mode)) {
538 int updatedOwner = 0;
541 /* Check if the directory's ownership is OK. */
546 * Check if the directory's mode is OK. An exact match isn't
547 * required, just a mode that isn't more permissive than the
550 if ((~mode) & 0077 & buf.st_mode)
554 * If the directory is not writeable not everybody may
555 * be able to create sockets. Therefore warn if mode
558 if ((~buf.st_mode) & 0022 & mode) {
560 status |= WARN_NO_ACCESS;
564 * If 'sticky' bit is requested fail if owner isn't root
565 * as we assume the caller makes certain security implications
568 status |= FAIL_IF_NOT_ROOT;
569 if (!(buf.st_mode & 01000)) {
570 status |= FAIL_IF_NOMODE;
577 * If fchown(2) and fchmod(2) are available, try to correct the
578 * directory's owner and mode. Otherwise it isn't safe to attempt
581 if (updateMode || updateOwner) {
584 if ((fd = open(path, O_RDONLY)) != -1) {
585 if (fstat(fd, &fbuf) == -1) {
586 prmsg(1, "mkdir: ERROR: fstat failed for %s (%d)\n",
592 * Verify that we've opened the same directory as was
595 if (!S_ISDIR(fbuf.st_mode) ||
596 buf.st_dev != fbuf.st_dev ||
597 buf.st_ino != fbuf.st_ino) {
598 prmsg(1, "mkdir: ERROR: inode for %s changed\n",
603 if (updateOwner && fchown(fd, 0, 0) == 0)
605 if (updateMode && fchmod(fd, mode) == 0)
612 if (updateOwner && !updatedOwner) {
614 if (status & FAIL_IF_NOT_ROOT) {
615 prmsg(1, "mkdir: ERROR: Owner of %s must be set to root\n",
620 #if !defined(__APPLE_CC__) && !defined(__CYGWIN__)
621 prmsg(1, "mkdir: Owner of %s should be set to root\n",
626 if (updateMode && !updatedMode) {
628 if (status & FAIL_IF_NOMODE) {
629 prmsg(1, "mkdir: ERROR: Mode of %s must be set to %04o\n",
634 prmsg(1, "mkdir: Mode of %s should be set to %04o\n",
636 if (status & WARN_NO_ACCESS) {
637 prmsg(1, "mkdir: this may cause subsequent errors\n");
645 /* In all other cases, fail */
649 #endif /* TRANS_SERVER */