X-Git-Url: http://git.demorecorder.com/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fxcb_auth.c;h=93a6f6839bdbe8f709dca299c77279ffad56a0f3;hb=6e2e87d0bbdff87f127986a0666445160d52e6a5;hp=46c85fd70e1ecafc4610ff7f452525c30ab2311e;hpb=e6132aff2fdcd0ab1ef5d7ac84a3c597dc07d370;p=free-sw%2Fxcb%2Flibxcb diff --git a/src/xcb_auth.c b/src/xcb_auth.c index 46c85fd..93a6f68 100644 --- a/src/xcb_auth.c +++ b/src/xcb_auth.c @@ -34,21 +34,13 @@ #include #include -/* FIXME: for the moment Xdmcp.h must be included before xcb.h because - * it includes Xmd.h, but config.h (normally included from xcbint.h - * which must be included after xcb.h) must be included before Xdmcp.h. - * Ow. */ -#ifdef HAVE_CONFIG_H -#include "config.h" -#endif +#include "xcb.h" +#include "xcbint.h" #ifdef HASXDMAUTH #include #endif -#include "xcb.h" -#include "xcbint.h" - enum auth_protos { #ifdef HASXDMAUTH AUTH_XA1, @@ -76,7 +68,7 @@ static size_t memdup(char **dst, void *src, size_t len) return len; } -static int authname_match(enum auth_protos kind, char *name, int namelen) +static int authname_match(enum auth_protos kind, char *name, size_t namelen) { if(strlen(authnames[kind]) != namelen) return 0; @@ -85,46 +77,50 @@ static int authname_match(enum auth_protos kind, char *name, int namelen) return 1; } -static Xauth *get_authptr(struct sockaddr *sockname, unsigned int socknamelen) +#define SIN6_ADDR(s) (&((struct sockaddr_in6 *)s)->sin6_addr) + +static Xauth *get_authptr(struct sockaddr *sockname, unsigned int socknamelen, + int display) { char *addr = 0; int addrlen = 0; unsigned short family; char hostnamebuf[256]; /* big enough for max hostname */ char dispbuf[40]; /* big enough to hold more than 2^64 base 10 */ - char *display; int authnamelens[N_AUTH_PROTOS]; int i; family = FamilyLocal; /* 256 */ - switch (sockname->sa_family) { - case AF_INET: - /*block*/ { - struct sockaddr_in *si = (struct sockaddr_in *) sockname; - assert(sizeof(*si) == socknamelen); - addr = (char *) &si->sin_addr; - addrlen = 4; - if (ntohl(si->sin_addr.s_addr) != 0x7f000001) - family = XCBFamilyInternet; - snprintf(dispbuf, sizeof(dispbuf), "%d", ntohs(si->sin_port) - X_TCP_PORT); - display = dispbuf; + switch(sockname->sa_family) + { +#ifdef AF_INET6 + case AF_INET6: + addr = (char *) SIN6_ADDR(sockname); + addrlen = sizeof(*SIN6_ADDR(sockname)); + if(!IN6_IS_ADDR_V4MAPPED(SIN6_ADDR(sockname))) + { + if(!IN6_IS_ADDR_LOOPBACK(SIN6_ADDR(sockname))) + family = XCB_FAMILY_INTERNET_6; + break; } - break; + addr += 12; + /* if v4-mapped, fall through. */ +#endif + case AF_INET: + if(!addr) + addr = (char *) &((struct sockaddr_in *)sockname)->sin_addr; + addrlen = sizeof(((struct sockaddr_in *)sockname)->sin_addr); + if(*(in_addr_t *) addr != htonl(INADDR_LOOPBACK)) + family = XCB_FAMILY_INTERNET; + break; case AF_UNIX: - /*block*/ { - struct sockaddr_un *su = (struct sockaddr_un *) sockname; - char *sockbuf = (char *) sockname; - assert(sizeof(*su) >= socknamelen); - sockbuf[socknamelen] = 0; /* null-terminate path */ - display = strrchr(su->sun_path, 'X'); - if (display == 0) - return 0; /* sockname is mangled somehow */ - display++; - } - break; + break; default: return 0; /* cannot authenticate this family */ } + + snprintf(dispbuf, sizeof(dispbuf), "%d", display); + if (family == FamilyLocal) { if (gethostname(hostnamebuf, sizeof(hostnamebuf)) == -1) return 0; /* do not know own hostname */ @@ -136,7 +132,7 @@ static Xauth *get_authptr(struct sockaddr *sockname, unsigned int socknamelen) authnamelens[i] = strlen(authnames[i]); return XauGetBestAuthByAddr (family, (unsigned short) addrlen, addr, - (unsigned short) strlen(display), display, + (unsigned short) strlen(dispbuf), dispbuf, N_AUTH_PROTOS, authnames, authnamelens); } @@ -158,7 +154,7 @@ static void do_append(char *buf, int *idxp, void *val, size_t valsize) { } #endif -static int compute_auth(XCBAuthInfo *info, Xauth *authptr, struct sockaddr *sockname) +static int compute_auth(xcb_auth_info_t *info, Xauth *authptr, struct sockaddr *sockname) { if (authname_match(AUTH_MC1, authptr->name, authptr->name_length)) { info->datalen = memdup(&info->data, authptr->data, authptr->data_length); @@ -185,10 +181,32 @@ static int compute_auth(XCBAuthInfo *info, Xauth *authptr, struct sockaddr *sock APPEND(info->data, j, si->sin_port); } break; +#ifdef AF_INET6 + case AF_INET6: + /*block*/ { + struct sockaddr_in6 *si6 = (struct sockaddr_in6 *) sockname; + if(IN6_IS_ADDR_V4MAPPED(SIN6_ADDR(sockname))) + { + do_append(info->data, &j, &si6->sin6_addr.s6_addr[12], 4); + APPEND(info->data, j, si6->sin6_port); + } + else + { + /* XDM-AUTHORIZATION-1 does not handle IPv6 correctly. Do the + same thing Xlib does: use all zeroes for the 4-byte address + and 2-byte port number. */ + uint32_t fakeaddr = 0; + uint16_t fakeport = 0; + APPEND(info->data, j, fakeaddr); + APPEND(info->data, j, fakeport); + } + } + break; +#endif case AF_UNIX: /*block*/ { - long fakeaddr = htonl(0xffffffff - next_nonce()); - short fakeport = htons(getpid()); + uint32_t fakeaddr = htonl(0xffffffff - next_nonce()); + uint16_t fakeport = htons(getpid()); APPEND(info->data, j, fakeaddr); APPEND(info->data, j, fakeport); } @@ -198,9 +216,7 @@ static int compute_auth(XCBAuthInfo *info, Xauth *authptr, struct sockaddr *sock return 0; /* do not know how to build this */ } { - long now; - time(&now); - now = htonl(now); + uint32_t now = htonl(time(0)); APPEND(info->data, j, now); } assert(j <= 192 / 8); @@ -216,7 +232,7 @@ static int compute_auth(XCBAuthInfo *info, Xauth *authptr, struct sockaddr *sock return 0; /* Unknown authorization type */ } -int _xcb_get_auth_info(int fd, XCBAuthInfo *info) +int _xcb_get_auth_info(int fd, xcb_auth_info_t *info, int display) { /* code adapted from Xlib/ConnDis.c, xtrans/Xtranssocket.c, xtrans/Xtransutils.c */ @@ -226,17 +242,17 @@ int _xcb_get_auth_info(int fd, XCBAuthInfo *info) Xauth *authptr = 0; int ret = 1; - /* ensure info has reasonable contents */ - /* XXX This should be removed, but Jamey depends on it - somehow but can't remember how. Principle: don't touch - someone else's data if you're borken. */ - info->namelen = info->datalen = 0; - info->name = info->data = 0; - if (getpeername(fd, sockname, &socknamelen) == -1) - return 0; /* can only authenticate sockets */ + { + if (getsockname(fd, sockname, &socknamelen) == -1) + return 0; /* can only authenticate sockets */ + if (sockname->sa_family != AF_UNIX) + return 0; + /* Some systems like hpux or Hurd do not expose peer names + * for UNIX Domain Sockets. We do not need it anyway. */ + } - authptr = get_authptr(sockname, socknamelen); + authptr = get_authptr(sockname, socknamelen, display); if (authptr == 0) return 0; /* cannot find good auth data */