1
/* $Xorg: Xtransutil.c,v 1.4 2001/02/09 02:04:07 xorgcvs Exp $ */
4
Copyright 1993, 1994, 1998 The Open Group
6
Permission to use, copy, modify, distribute, and sell this software and its
7
documentation for any purpose is hereby granted without fee, provided that
8
the above copyright notice appear in all copies and that both that
9
copyright notice and this permission notice appear in supporting
12
The above copyright notice and this permission notice shall be included
13
in all copies or substantial portions of the Software.
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
19
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21
OTHER DEALINGS IN THE SOFTWARE.
23
Except as contained in this notice, the name of The Open Group shall
24
not be used in advertising or otherwise to promote the sale, use or
25
other dealings in this Software without prior written authorization
29
/* $XFree86: xc/lib/xtrans/Xtransutil.c,v 3.23 2003/02/12 15:01:38 alanh Exp $ */
31
/* Copyright 1993, 1994 NCR Corporation - Dayton, Ohio, USA
35
* Permission to use, copy, modify, and distribute this software and its
36
* documentation for any purpose and without fee is hereby granted, provided
37
* that the above copyright notice appear in all copies and that both that
38
* copyright notice and this permission notice appear in supporting
39
* documentation, and that the name NCR not be used in advertising
40
* or publicity pertaining to distribution of the software without specific,
41
* written prior permission. NCR makes no representations about the
42
* suitability of this software for any purpose. It is provided "as is"
43
* without express or implied warranty.
45
* NCRS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
46
* INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN
47
* NO EVENT SHALL NCR BE LIABLE FOR ANY SPECIAL, INDIRECT OR
48
* CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS
49
* OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT,
50
* NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
51
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
55
* These are some utility functions created for convenience or to provide
56
* an interface that is similar to an existing interface. These are built
57
* only using the Transport Independant API, and have no knowledge of
58
* the internal implementation.
62
#include <X11/Xthreads.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
73
#define FamilyDECnet 1
75
#define FamilyAmoeba 33
76
#define FamilyLocalHost 252
77
#define FamilyKrb5Principal 253
78
#define FamilyNetname 254
79
#define FamilyLocal 256
80
#define FamilyWild 65535
83
* TRANS(ConvertAddress) converts a sockaddr based address to an
84
* X authorization based address. Some of this is defined as part of
85
* the ChangeHost protocol. The rest is just doen in a consistent manner.
89
TRANS(ConvertAddress)(int *familyp, int *addrlenp, Xtransaddr **addrp)
93
PRMSG(2,"ConvertAddress(%d,%d,%x)\n",*familyp,*addrlenp,*addrp);
97
#if defined(TCPCONN) || defined(STREAMSCONN)
101
* Check for the BSD hack localhost address 127.0.0.1.
102
* In this case, we are really FamilyLocal.
105
struct sockaddr_in saddr;
108
int len = sizeof(saddr.sin_addr);
110
int len = SIZEOF_in_addr;
112
char *cp = (char *) &saddr.sin_addr;
113
#else /* else not CRAY */
114
int len = sizeof(saddr.sin_addr.s_addr);
115
char *cp = (char *) &saddr.sin_addr.s_addr;
118
memcpy (&saddr, *addrp, sizeof (struct sockaddr_in));
120
if ((len == 4) && (cp[0] == 127) && (cp[1] == 0) &&
121
(cp[2] == 0) && (cp[3] == 1))
123
*familyp=FamilyLocal;
127
*familyp=FamilyInternet;
129
memcpy(*addrp,&saddr.sin_addr,len);
133
#endif /* defined(TCPCONN) || defined(STREAMSCONN) */
135
#if defined(DNETCONN)
138
struct sockaddr_dn saddr;
140
memcpy (&saddr, *addrp, sizeof (struct sockaddr_dn));
142
*familyp=FamilyDECnet;
143
*addrlenp=sizeof(struct dn_naddr);
144
memcpy(*addrp,&saddr.sdn_add,*addrlenp);
148
#endif /* defined(DNETCONN) */
150
#if defined(UNIXCONN) || defined(LOCALCONN)
153
*familyp=FamilyLocal;
156
#endif /* defined(UNIXCONN) || defined(LOCALCONN) */
160
PRMSG(1,"ConvertAddress: Unknown family type %d\n",
166
if (*familyp == FamilyLocal)
169
* In the case of a local connection, we need to get the
170
* host name for authentication.
173
char hostnamebuf[256];
174
int len = TRANS(GetHostname) (hostnamebuf, sizeof hostnamebuf);
177
if (*addrp && *addrlenp < (len + 1))
179
xfree ((char *) *addrp);
183
*addrp = (Xtransaddr *) xalloc (len + 1);
185
strcpy ((char *) *addrp, hostnamebuf);
194
xfree ((char *) *addrp);
210
TRANS(GetMyNetworkId) (XtransConnInfo ciptr)
213
int family = ciptr->family;
214
char *addr = ciptr->addr;
215
char hostnamebuf[256];
216
char *networkId = NULL;
217
char *transName = ciptr->transptr->TransName;
219
if (gethostname (hostnamebuf, sizeof (hostnamebuf)) < 0)
226
#if defined(UNIXCONN) || defined(STREAMSCONN) || defined(LOCALCONN)
229
struct sockaddr_un *saddr = (struct sockaddr_un *) addr;
230
networkId = (char *) xalloc (3 + strlen (transName) +
231
strlen (hostnamebuf) + strlen (saddr->sun_path));
232
sprintf (networkId, "%s/%s:%s", transName,
233
hostnamebuf, saddr->sun_path);
236
#endif /* defined(UNIXCONN) || defined(STREAMSCONN) || defined(LOCALCONN) */
238
#if defined(TCPCONN) || defined(STREAMSCONN)
241
struct sockaddr_in *saddr = (struct sockaddr_in *) addr;
244
sprintf (portnumbuf, "%d", ntohs (saddr->sin_port));
245
networkId = (char *) xalloc (3 + strlen (transName) +
246
strlen (hostnamebuf) + strlen (portnumbuf));
247
sprintf (networkId, "%s/%s:%s", transName, hostnamebuf, portnumbuf);
250
#endif /* defined(TCPCONN) || defined(STREAMSCONN) */
252
#if defined(DNETCONN)
255
struct sockaddr_dn *saddr = (struct sockaddr_dn *) addr;
257
networkId = (char *) xalloc (
258
13 + strlen (hostnamebuf) + saddr->sdn_objnamel);
259
sprintf (networkId, "dnet/%s::%s",
260
hostnamebuf, saddr->sdn_objname);
263
#endif /* defined(DNETCONN) */
276
static int nameserver_timedout = 0;
279
#ifdef SIGNALRETURNSINT
284
nameserver_lost(int sig)
286
nameserver_timedout = 1;
289
#ifdef SIGNALRETURNSINT
290
return -1; /* for picky compilers */
293
#endif /* SIGALARM */
297
TRANS(GetPeerNetworkId) (XtransConnInfo ciptr)
300
int family = ciptr->family;
301
char *peer_addr = ciptr->peeraddr;
309
#if defined(UNIXCONN) || defined(STREAMSCONN) || defined(LOCALCONN)
312
if (gethostname (addrbuf, sizeof (addrbuf)) == 0)
316
#endif /* defined(UNIXCONN) || defined(STREAMSCONN) || defined(LOCALCONN) */
318
#if defined(TCPCONN) || defined(STREAMSCONN)
321
struct sockaddr_in *saddr = (struct sockaddr_in *) peer_addr;
322
#ifdef XTHREADS_NEEDS_BYNAMEPARAMS
323
_Xgethostbynameparams hparams;
325
struct hostent * volatile hostp = NULL;
329
* gethostbyaddr can take a LONG time if the host does not exist.
330
* Assume that if it does not respond in NAMESERVER_TIMEOUT seconds
331
* that something is wrong and do not make the user wait.
332
* gethostbyaddr will continue after a signal, so we have to
336
nameserver_timedout = 0;
337
signal (SIGALRM, nameserver_lost);
339
if (setjmp(env) == 0) {
341
hostp = _XGethostbyaddr ((char *) &saddr->sin_addr,
342
sizeof (saddr->sin_addr), AF_INET, hparams);
348
addr = hostp->h_name;
350
addr = inet_ntoa (saddr->sin_addr);
354
#endif /* defined(TCPCONN) || defined(STREAMSCONN) */
356
#if defined(DNETCONN)
359
struct sockaddr_dn *saddr = (struct sockaddr_dn *) peer_addr;
362
if (np = getnodebyaddr(saddr->sdn_add.a_addr,
363
saddr->sdn_add.a_len, AF_DECnet)) {
364
sprintf(addrbuf, "%s:", np->n_name);
366
sprintf(addrbuf, "%s:", dnet_htoa(&saddr->sdn_add));
371
#endif /* defined(DNETCONN) */
378
hostname = (char *) xalloc (
379
strlen (ciptr->transptr->TransName) + strlen (addr) + 2);
380
strcpy (hostname, ciptr->transptr->TransName);
381
strcat (hostname, "/");
383
strcat (hostname, addr);
391
#if defined(WIN32) && (defined(TCPCONN) || defined(DNETCONN))
393
TRANS(WSAStartup) (void)
395
static WSADATA wsadata;
397
PRMSG (2,"WSAStartup()\n", 0, 0, 0);
399
if (!wsadata.wVersion && WSAStartup(MAKEWORD(1,1), &wsadata))
407
is_numeric (char *str)
412
for (i = 0; i < (int) strlen (str); i++)
413
if (!isdigit (str[i]))
420
#include <sys/types.h>
421
#include <sys/stat.h>
424
#if !defined(S_IFLNK) && !defined(S_ISLNK)
425
#define lstat(a,b) stat(a,b)
429
trans_mkdir(char *path, int mode)
433
if (mkdir(path, mode) == 0) {
437
/* If mkdir failed with EEXIST, test if it is a directory with
438
the right modes, else fail */
439
if (errno == EEXIST) {
440
if (lstat(path, &buf) != 0) {
441
PRMSG(1, "mkdir: (l)stat failed for %s (%d)\n",
445
if (S_ISDIR(buf.st_mode)) {
448
int updatedOwner = 0;
450
/* Check if the directory's ownership is OK. */
454
* Check if the directory's mode is OK. An exact match isn't
455
* required, just a mode that isn't more permissive than the
458
if ((~mode) & 0077 & buf.st_mode)
460
if ((mode & 01000) &&
461
(buf.st_mode & 0002) && !(buf.st_mode & 01000))
465
* If fchown(2) and fchmod(2) are available, try to correct the
466
* directory's owner and mode. Otherwise it isn't safe to attempt
469
if (updateMode || updateOwner) {
472
if ((fd = open(path, O_RDONLY)) != -1) {
473
if (fstat(fd, &fbuf) == -1) {
474
PRMSG(1, "mkdir: fstat failed for %s (%d)\n",
479
* Verify that we've opened the same directory as was
482
if (!S_ISDIR(fbuf.st_mode) ||
483
buf.st_dev != fbuf.st_dev ||
484
buf.st_ino != fbuf.st_ino) {
485
PRMSG(1, "mkdir: inode for %s changed\n",
489
if (updateOwner && fchown(fd, 0, 0) == 0)
491
if (updateMode && fchmod(fd, mode) == 0)
497
if (updateOwner && !updatedOwner) {
498
PRMSG(1, "mkdir: Owner of %s should be set to root\n",
500
#if !defined(__CYGWIN__)
504
if (updateMode && !updatedMode) {
505
PRMSG(1, "mkdir: Mode of %s should be set to %04o\n",
507
#if !defined(__CYGWIN__)
514
/* In all other cases, fail */
518
#endif /* TRANS_SERVER */