X-Git-Url: http://git.demorecorder.com/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fxcb_auth.c;h=29e2b6f84479641fd090621a1598529d710ef225;hb=c3cfa04b09ff042fed731b17dac3e40de1a10187;hp=6e0ff4666ff45c6b3935ba68466c475133e8180a;hpb=f0b29819749b769e5a8d313bf1bab80d6513208b;p=free-sw%2Fxcb%2Flibxcb diff --git a/src/xcb_auth.c b/src/xcb_auth.c index 6e0ff46..29e2b6f 100644 --- a/src/xcb_auth.c +++ b/src/xcb_auth.c @@ -25,15 +25,35 @@ /* Authorization systems for the X protocol. */ +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + #include #include -#include -#include -#include #include #include #include +#ifdef __INTERIX +/* _don't_ ask. interix has INADDR_LOOPBACK in here. */ +#include +#endif + +#ifdef _WIN32 +#ifdef HASXDMAUTH +/* We must include the wrapped windows.h before any system header which includes + it unwrapped, to avoid conflicts with types defined in X headers */ +#include +#endif +#include "xcb_windefs.h" +#else +#include +#include +#include +#include +#endif /* _WIN32 */ + #include "xcb.h" #include "xcbint.h" @@ -69,11 +89,11 @@ static int authnameslen[N_AUTH_PROTOS] = { static size_t memdup(char **dst, void *src, size_t len) { if(len) - *dst = malloc(len); + *dst = malloc(len); else - *dst = 0; + *dst = 0; if(!*dst) - return 0; + return 0; memcpy(*dst, src, len); return len; } @@ -81,16 +101,15 @@ static size_t memdup(char **dst, void *src, size_t len) static int authname_match(enum auth_protos kind, char *name, size_t namelen) { if(authnameslen[kind] != namelen) - return 0; + return 0; if(memcmp(authnames[kind], name, namelen)) - return 0; + return 0; return 1; } #define SIN6_ADDR(s) (&((struct sockaddr_in6 *)s)->sin6_addr) -static Xauth *get_authptr(struct sockaddr *sockname, unsigned int socknamelen, - int display) +static Xauth *get_authptr(struct sockaddr *sockname, int display) { char *addr = 0; int addrlen = 0; @@ -164,7 +183,7 @@ static void do_append(char *buf, int *idxp, void *val, size_t valsize) { *idxp += valsize; } #endif - + static int compute_auth(xcb_auth_info_t *info, Xauth *authptr, struct sockaddr *sockname) { if (authname_match(AUTH_MC1, authptr->name, authptr->name_length)) { @@ -176,22 +195,22 @@ static int compute_auth(xcb_auth_info_t *info, Xauth *authptr, struct sockaddr * #ifdef HASXDMAUTH #define APPEND(buf,idx,val) do_append((buf),&(idx),&(val),sizeof(val)) if (authname_match(AUTH_XA1, authptr->name, authptr->name_length)) { - int j; + int j; - info->data = malloc(192 / 8); - if(!info->data) - return 0; + info->data = malloc(192 / 8); + if(!info->data) + return 0; - for (j = 0; j < 8; j++) - info->data[j] = authptr->data[j]; - switch(sockname->sa_family) { + for (j = 0; j < 8; j++) + info->data[j] = authptr->data[j]; + switch(sockname->sa_family) { case AF_INET: /*block*/ { - struct sockaddr_in *si = (struct sockaddr_in *) sockname; - APPEND(info->data, j, si->sin_addr.s_addr); - APPEND(info->data, j, si->sin_port); - } - break; + struct sockaddr_in *si = (struct sockaddr_in *) sockname; + APPEND(info->data, j, si->sin_addr.s_addr); + APPEND(info->data, j, si->sin_port); + } + break; #ifdef AF_INET6 case AF_INET6: /*block*/ { @@ -216,26 +235,26 @@ static int compute_auth(xcb_auth_info_t *info, Xauth *authptr, struct sockaddr * #endif case AF_UNIX: /*block*/ { - uint32_t fakeaddr = htonl(0xffffffff - next_nonce()); - uint16_t fakeport = htons(getpid()); - APPEND(info->data, j, fakeaddr); - APPEND(info->data, j, fakeport); - } - break; + uint32_t fakeaddr = htonl(0xffffffff - next_nonce()); + uint16_t fakeport = htons(getpid()); + APPEND(info->data, j, fakeaddr); + APPEND(info->data, j, fakeport); + } + break; default: free(info->data); return 0; /* do not know how to build this */ - } - { - uint32_t now = htonl(time(0)); - APPEND(info->data, j, now); - } - assert(j <= 192 / 8); - while (j < 192 / 8) - info->data[j++] = 0; - info->datalen = j; - XdmcpWrap ((unsigned char *) info->data, (unsigned char *) authptr->data + 8, (unsigned char *) info->data, info->datalen); - return 1; + } + { + uint32_t now = htonl(time(0)); + APPEND(info->data, j, now); + } + assert(j <= 192 / 8); + while (j < 192 / 8) + info->data[j++] = 0; + info->datalen = j; + XdmcpWrap ((unsigned char *) info->data, (unsigned char *) authptr->data + 8, (unsigned char *) info->data, info->datalen); + return 1; } #undef APPEND #endif @@ -243,39 +262,118 @@ static int compute_auth(xcb_auth_info_t *info, Xauth *authptr, struct sockaddr * return 0; /* Unknown authorization type */ } +/* `sockaddr_un.sun_path' typical size usually ranges between 92 and 108 */ +#define INITIAL_SOCKNAME_SLACK 108 + +/* Return a dynamically allocated socket address structure according + to the value returned by either getpeername() or getsockname() + (according to POSIX, applications should not assume a particular + length for `sockaddr_un.sun_path') */ +static struct sockaddr *get_peer_sock_name(int (*socket_func)(int, + struct sockaddr *, + socklen_t *), + int fd) +{ + socklen_t socknamelen = sizeof(struct sockaddr) + INITIAL_SOCKNAME_SLACK; + socklen_t actual_socknamelen = socknamelen; + struct sockaddr *sockname = malloc(socknamelen); + + if (sockname == NULL) + return NULL; + + /* Both getpeername() and getsockname() truncates sockname if + there is not enough space and set the required length in + actual_socknamelen */ + if (socket_func(fd, sockname, &actual_socknamelen) == -1) + goto sock_or_realloc_error; + + if (actual_socknamelen > socknamelen) + { + struct sockaddr *new_sockname = NULL; + socknamelen = actual_socknamelen; + + if ((new_sockname = realloc(sockname, actual_socknamelen)) == NULL) + goto sock_or_realloc_error; + + sockname = new_sockname; + + if (socket_func(fd, sockname, &actual_socknamelen) == -1 || + actual_socknamelen > socknamelen) + goto sock_or_realloc_error; + } + + return sockname; + + sock_or_realloc_error: + free(sockname); + return NULL; +} + 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 */ - char sockbuf[sizeof(struct sockaddr) + MAXPATHLEN]; - unsigned int socknamelen = sizeof(sockbuf); /* need extra space */ - struct sockaddr *sockname = (struct sockaddr *) &sockbuf; + struct sockaddr *sockname = NULL; + int gotsockname = 0; Xauth *authptr = 0; int ret = 1; - if (getpeername(fd, sockname, &socknamelen) == -1) + /* Some systems like hpux or Hurd do not expose peer names + * for UNIX Domain Sockets, but this is irrelevant, + * since compute_auth() ignores the peer name in this + * case anyway.*/ + if ((sockname = get_peer_sock_name(getpeername, fd)) == NULL) { - if (getsockname(fd, sockname, &socknamelen) == -1) - return 0; /* can only authenticate sockets */ + if ((sockname = get_peer_sock_name(getsockname, fd)) == NULL) + 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. */ + { + free(sockname); + return 0; /* except for AF_UNIX, sockets should have peernames */ + } + gotsockname = 1; } - authptr = get_authptr(sockname, socknamelen, display); + authptr = get_authptr(sockname, display); if (authptr == 0) + { + free(sockname); return 0; /* cannot find good auth data */ + } info->namelen = memdup(&info->name, authptr->name, authptr->name_length); - if(info->namelen) - ret = compute_auth(info, authptr, sockname); + if (!info->namelen) + goto no_auth; /* out of memory */ + + if (!gotsockname) + { + free(sockname); + + if ((sockname = get_peer_sock_name(getsockname, fd)) == NULL) + { + free(info->name); + goto no_auth; /* can only authenticate sockets */ + } + } + + ret = compute_auth(info, authptr, sockname); if(!ret) { - free(info->name); - info->name = 0; - info->namelen = 0; + free(info->name); + goto no_auth; /* cannot build auth record */ } + + free(sockname); + sockname = NULL; + XauDisposeAuth(authptr); return ret; + + no_auth: + free(sockname); + + info->name = 0; + info->namelen = 0; + XauDisposeAuth(authptr); + return 0; }