aboutsummaryrefslogtreecommitdiff
path: root/xorg-server/os
diff options
context:
space:
mode:
authormarha <marha@users.sourceforge.net>2011-11-25 08:32:21 +0100
committermarha <marha@users.sourceforge.net>2011-11-25 08:32:21 +0100
commit849d328b25c2f3c87d539e1fe7af38e7580a0409 (patch)
tree349c425a87ddb3aaa1d7832d5a8e68252fc73ddb /xorg-server/os
parenta8a12d4c8be177f63cae7dc96c2b52f09e228a76 (diff)
parenta0b4a1330be6a36ad095222d2ea83927cd33514d (diff)
downloadvcxsrv-849d328b25c2f3c87d539e1fe7af38e7580a0409.tar.gz
vcxsrv-849d328b25c2f3c87d539e1fe7af38e7580a0409.tar.bz2
vcxsrv-849d328b25c2f3c87d539e1fe7af38e7580a0409.zip
Merge remote-tracking branch 'origin/released'
Conflicts: xorg-server/glx/glxdriswrast.c xorg-server/glx/single2.c xorg-server/hw/xwin/winconfig.c xorg-server/os/osinit.c xorg-server/os/xdmcp.c xorg-server/xkb/xkbInit.c
Diffstat (limited to 'xorg-server/os')
-rw-r--r--xorg-server/os/Makefile.am4
-rw-r--r--xorg-server/os/access.c10
-rw-r--r--xorg-server/os/client.c8
-rw-r--r--xorg-server/os/connection.c15
-rw-r--r--xorg-server/os/osinit.c622
-rw-r--r--xorg-server/os/rpcauth.c387
-rw-r--r--xorg-server/os/strndup.c49
-rw-r--r--xorg-server/os/utils.c14
-rw-r--r--xorg-server/os/xdmcp.c3514
9 files changed, 2333 insertions, 2290 deletions
diff --git a/xorg-server/os/Makefile.am b/xorg-server/os/Makefile.am
index ef9ecddcf..8dd809531 100644
--- a/xorg-server/os/Makefile.am
+++ b/xorg-server/os/Makefile.am
@@ -41,6 +41,10 @@ if NEED_STRLCAT
libos_la_SOURCES += $(STRLCAT_SRCS)
endif
+if NEED_STRNDUP
+libos_la_SOURCES += $(STRNDUP_SRCS)
+endif
+
EXTRA_DIST = $(SECURERPC_SRCS) $(INTERNALMALLOC_SRCS) \
$(XDMCP_SRCS) $(STRLCAT_SRCS)
diff --git a/xorg-server/os/access.c b/xorg-server/os/access.c
index 9f148e202..d8fd7e48a 100644
--- a/xorg-server/os/access.c
+++ b/xorg-server/os/access.c
@@ -698,7 +698,7 @@ DefineSelf (int fd)
ifn.lifn_family = AF_UNSPEC;
ifn.lifn_flags = 0;
if (ioctl (fd, SIOCGLIFNUM, (char *) &ifn) < 0)
- Error ("Getting interface count");
+ ErrorF ("Getting interface count: %s\n", strerror(errno));
if (len < (ifn.lifn_count * sizeof(struct lifreq))) {
len = ifn.lifn_count * sizeof(struct lifreq);
bufptr = malloc(len);
@@ -728,7 +728,7 @@ DefineSelf (int fd)
#endif
if (ifioctl (fd, IFC_IOCTL_REQ, (pointer) &ifc) < 0)
- Error ("Getting interface configuration (4)");
+ ErrorF ("Getting interface configuration (4): %s\n", strerror(errno));
cplim = (char *) IFC_IFC_REQ + IFC_IFC_LEN;
@@ -1934,8 +1934,7 @@ siHostnameAddrMatch(int family, pointer addr, int len,
if (siAddrLen >= sizeof(hostname))
return FALSE;
- strncpy(hostname, siAddr, siAddrLen);
- hostname[siAddrLen] = '\0';
+ strlcpy(hostname, siAddr, siAddrLen + 1);
memset( &hints, 0, sizeof(hints) );
if (family == FamilyInternet) hints.ai_family = AF_INET;
@@ -1967,8 +1966,7 @@ siHostnameAddrMatch(int family, pointer addr, int len,
if (siAddrLen >= sizeof(hostname))
return FALSE;
- strncpy(hostname, siAddr, siAddrLen);
- hostname[siAddrLen] = '\0';
+ strlcpy(hostname, siAddr, siAddrLen + 1);
if ((hp = _XGethostbyname(hostname, hparams)) != NULL) {
#ifdef h_addr /* new 4.3bsd version of gethostent */
diff --git a/xorg-server/os/client.c b/xorg-server/os/client.c
index b5349778b..4aec097e1 100644
--- a/xorg-server/os/client.c
+++ b/xorg-server/os/client.c
@@ -149,13 +149,7 @@ void DetermineClientCmd(pid_t pid, const char **cmdname, const char **cmdargs)
cmdsize = strlen(path) + 1;
if (cmdname)
{
- char *name = malloc(cmdsize);
- if (name)
- {
- strncpy(name, path, cmdsize);
- name[cmdsize - 1] = '\0';
- *cmdname = name;
- }
+ *cmdname = strdup(path);
}
/* Construct the arguments for client process. */
diff --git a/xorg-server/os/connection.c b/xorg-server/os/connection.c
index cc5a4d3f5..bff81051e 100644
--- a/xorg-server/os/connection.c
+++ b/xorg-server/os/connection.c
@@ -412,7 +412,7 @@ CreateWellKnownSockets(void)
FD_ZERO (&WellKnownConnections);
- sprintf (port, "%d", atoi (display));
+ snprintf (port, sizeof(port), "%d", atoi (display));
if ((_XSERVTransMakeAllCOTSServerListeners (port, &partial,
&ListenTransCount, &ListenTransConns) >= 0) &&
@@ -528,7 +528,6 @@ AuthAudit (ClientPtr client, Bool letin,
unsigned int proto_n, char *auth_proto, int auth_id)
{
char addr[128];
- char *out = addr;
char client_uid_string[64];
LocalClientCredRec *lcc;
#ifdef XSERVER_DTRACE
@@ -537,7 +536,7 @@ AuthAudit (ClientPtr client, Bool letin,
#endif
if (!len)
- strcpy(out, "local host");
+ strlcpy(addr, "local host", sizeof(addr));
else
switch (saddr->sa_family)
{
@@ -545,11 +544,11 @@ AuthAudit (ClientPtr client, Bool letin,
#if defined(UNIXCONN) || defined(LOCALCONN)
case AF_UNIX:
#endif
- strcpy(out, "local host");
+ strlcpy(addr, "local host", sizeof(addr));
break;
#if defined(TCPCONN) || defined(STREAMSCONN)
case AF_INET:
- sprintf(out, "IP %s",
+ snprintf(addr, sizeof(addr), "IP %s",
inet_ntoa(((struct sockaddr_in *) saddr)->sin_addr));
break;
#if defined(IPv6) && defined(AF_INET6)
@@ -557,13 +556,13 @@ AuthAudit (ClientPtr client, Bool letin,
char ipaddr[INET6_ADDRSTRLEN];
inet_ntop(AF_INET6, &((struct sockaddr_in6 *) saddr)->sin6_addr,
ipaddr, sizeof(ipaddr));
- sprintf(out, "IP %s", ipaddr);
+ snprintf(addr, sizeof(addr), "IP %s", ipaddr);
}
break;
#endif
#endif
default:
- strcpy(out, "unknown address");
+ strlcpy(addr, "unknown address", sizeof(addr));
}
if (GetLocalClientCreds(client, &lcc) != -1) {
@@ -1307,7 +1306,7 @@ void ListenOnOpenFD(int fd, int noxauth) {
strcpy(port, display_env);
} else {
/* Just some default so things don't break and die. */
- sprintf(port, ":%d", atoi(display));
+ snprintf(port, sizeof(port), ":%d", atoi(display));
}
/* Make our XtransConnInfo
diff --git a/xorg-server/os/osinit.c b/xorg-server/os/osinit.c
index 2080cb782..f7b2ca443 100644
--- a/xorg-server/os/osinit.c
+++ b/xorg-server/os/osinit.c
@@ -1,311 +1,311 @@
-/***********************************************************
-
-Copyright 1987, 1998 The Open Group
-
-Permission to use, copy, modify, distribute, and sell this software and its
-documentation for any purpose is hereby granted without fee, provided that
-the above copyright notice appear in all copies and that both that
-copyright notice and this permission notice appear in supporting
-documentation.
-
-The above copyright notice and this permission notice shall be included in
-all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
-AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
-CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
-
-Except as contained in this notice, the name of The Open Group shall not be
-used in advertising or otherwise to promote the sale, use or other dealings
-in this Software without prior written authorization from The Open Group.
-
-
-Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
-
- All Rights Reserved
-
-Permission to use, copy, modify, and distribute this software and its
-documentation for any purpose and without fee is hereby granted,
-provided that the above copyright notice appear in all copies and that
-both that copyright notice and this permission notice appear in
-supporting documentation, and that the name of Digital not be
-used in advertising or publicity pertaining to distribution of the
-software without specific, written prior permission.
-
-DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
-ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
-DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
-ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
-WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
-ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
-SOFTWARE.
-
-******************************************************************/
-
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
-#include <stdio.h>
-#include <X11/X.h>
-#include "os.h"
-#include "osdep.h"
-#include <X11/Xos.h>
-#include <signal.h>
-#include <errno.h>
-#ifdef HAVE_DLFCN_H
-# include <dlfcn.h>
-#endif
-#ifdef HAVE_BACKTRACE
-#include <execinfo.h>
-#endif
-
-#include "misc.h"
-
-#include "dixstruct.h"
-
-
-#if !defined(SYSV) && !defined(WIN32)
-#include <sys/resource.h>
-#endif
-
-#ifndef ADMPATH
-#define ADMPATH "/usr/adm/X%smsgs"
-#endif
-
-extern char *display;
-#ifdef RLIMIT_DATA
-int limitDataSpace = -1;
-#endif
-#ifdef RLIMIT_STACK
-int limitStackSpace = -1;
-#endif
-#ifdef RLIMIT_NOFILE
-int limitNoFile = -1;
-#endif
-
-static OsSigWrapperPtr OsSigWrapper = NULL;
-
-OsSigWrapperPtr
-OsRegisterSigWrapper(OsSigWrapperPtr newSigWrapper)
-{
- OsSigWrapperPtr oldSigWrapper = OsSigWrapper;
-
- OsSigWrapper = newSigWrapper;
-
- return oldSigWrapper;
-}
-
-/*
- * OsSigHandler --
- * Catch unexpected signals and exit or continue cleanly.
- */
-static void
-#ifdef SA_SIGINFO
-OsSigHandler(int signo, siginfo_t *sip, void *unused)
-#else
-OsSigHandler(int signo)
-#endif
-{
-#ifdef RTLD_DI_SETSIGNAL
- const char *dlerr = dlerror();
-
- if (dlerr) {
- LogMessage(X_ERROR, "Dynamic loader error: %s\n", dlerr);
- }
-#endif /* RTLD_DI_SETSIGNAL */
-
- if (OsSigWrapper != NULL) {
- if (OsSigWrapper(signo) == 0) {
- /* ddx handled signal and wants us to continue */
- return;
- }
- }
-
- /* log, cleanup, and abort */
- xorg_backtrace();
-
-#ifdef SA_SIGINFO
- if (sip->si_code == SI_USER) {
- ErrorF("Recieved signal %d sent by process %ld, uid %ld\n",
- signo, (long) sip->si_pid, (long) sip->si_uid);
- } else {
- switch (signo) {
- case SIGSEGV:
- case SIGBUS:
- case SIGILL:
- case SIGFPE:
- ErrorF("%s at address %p\n", strsignal(signo), sip->si_addr);
- }
- }
-#endif
-
- FatalError("Caught signal %d (%s). Server aborting\n",
- signo, strsignal(signo));
-}
-
-void
-OsInit(void)
-{
- static Bool been_here = FALSE;
- static char* devnull = "/dev/null";
- char fname[PATH_MAX];
-
- if (!been_here) {
-#ifndef _MSC_VER
- struct sigaction act, oact;
- int i;
- int siglist[] = { SIGSEGV, SIGQUIT, SIGILL, SIGFPE, SIGBUS,
- SIGSYS,
- SIGXCPU,
- SIGXFSZ,
-#ifdef SIGEMT
- SIGEMT,
-#endif
- 0 /* must be last */ };
- sigemptyset(&act.sa_mask);
-#ifdef SA_SIGINFO
- act.sa_sigaction = OsSigHandler;
- act.sa_flags = SA_SIGINFO;
-#else
- act.sa_handler = OsSigHandler;
- act.sa_flags = 0;
-#endif
- for (i = 0; siglist[i] != 0; i++) {
- if (sigaction(siglist[i], &act, &oact)) {
- ErrorF("failed to install signal handler for signal %d: %s\n",
- siglist[i], strerror(errno));
- }
- }
-#ifdef HAVE_BACKTRACE
- /*
- * initialize the backtracer, since the ctor calls dlopen(), which
- * calls malloc(), which isn't signal-safe.
- */
- do {
- void *array;
- backtrace(&array, 1);
- } while (0);
-#endif
-
-#ifdef RTLD_DI_SETSIGNAL
- /* Tell runtime linker to send a signal we can catch instead of SIGKILL
- * for failures to load libraries/modules at runtime so we can clean up
- * after ourselves.
- */
- int failure_signal = SIGQUIT;
- dlinfo(RTLD_SELF, RTLD_DI_SETSIGNAL, &failure_signal);
-#endif
-#endif
-#if !defined(__CYGWIN__)
- fclose(stdin);
- fclose(stdout);
-#endif
- /*
- * If a write of zero bytes to stderr returns non-zero, i.e. -1,
- * then writing to stderr failed, and we'll write somewhere else
- * instead. (Apparently this never happens in the Real World.)
- */
- if (write (2, fname, 0) == -1)
- {
- FILE *err;
-
- if (strlen (display) + strlen (ADMPATH) + 1 < sizeof fname)
- sprintf (fname, ADMPATH, display);
- else
- strcpy (fname, devnull);
- /*
- * uses stdio to avoid os dependencies here,
- * a real os would use
- * open (fname, O_WRONLY|O_APPEND|O_CREAT, 0666)
- */
- if (!(err = fopen (fname, "a+")))
- err = fopen (devnull, "w");
- if (err && (fileno(err) != 2)) {
- dup2 (fileno (err), 2);
- fclose (err);
- }
-#if defined(SYSV) || defined(SVR4) || defined(WIN32) || defined(__CYGWIN__)
- {
- static char buf[BUFSIZ];
- setvbuf (stderr, buf, _IOLBF, BUFSIZ);
- }
-#else
- setlinebuf(stderr);
-#endif
- }
-
-#ifndef _MSC_VER
- if (getpgrp () == 0)
- setpgid (0, 0);
-#endif
-
-#ifdef RLIMIT_DATA
- if (limitDataSpace >= 0)
- {
- struct rlimit rlim;
-
- if (!getrlimit(RLIMIT_DATA, &rlim))
- {
- if ((limitDataSpace > 0) && (limitDataSpace < rlim.rlim_max))
- rlim.rlim_cur = limitDataSpace;
- else
- rlim.rlim_cur = rlim.rlim_max;
- (void)setrlimit(RLIMIT_DATA, &rlim);
- }
- }
-#endif
-#ifdef RLIMIT_STACK
- if (limitStackSpace >= 0)
- {
- struct rlimit rlim;
-
- if (!getrlimit(RLIMIT_STACK, &rlim))
- {
- if ((limitStackSpace > 0) && (limitStackSpace < rlim.rlim_max))
- rlim.rlim_cur = limitStackSpace;
- else
- rlim.rlim_cur = rlim.rlim_max;
- (void)setrlimit(RLIMIT_STACK, &rlim);
- }
- }
-#endif
-#ifdef RLIMIT_NOFILE
- if (limitNoFile >= 0)
- {
- struct rlimit rlim;
-
- if (!getrlimit(RLIMIT_NOFILE, &rlim))
- {
- if ((limitNoFile > 0) && (limitNoFile < rlim.rlim_max))
- rlim.rlim_cur = limitNoFile;
- else
- rlim.rlim_cur = rlim.rlim_max;
- (void)setrlimit(RLIMIT_NOFILE, &rlim);
- }
- }
-#endif
- LockServer();
- been_here = TRUE;
- }
- TimerInit();
- OsVendorInit();
- /*
- * No log file by default. OsVendorInit() should call LogInit() with the
- * log file name if logging to a file is desired.
- */
- LogInit(NULL, NULL);
- SmartScheduleInit ();
-}
-
-void
-OsCleanup(Bool terminating)
-{
- if (terminating)
- {
- UnlockServer();
- }
-}
+/***********************************************************
+
+Copyright 1987, 1998 The Open Group
+
+Permission to use, copy, modify, distribute, and sell this software and its
+documentation for any purpose is hereby granted without fee, provided that
+the above copyright notice appear in all copies and that both that
+copyright notice and this permission notice appear in supporting
+documentation.
+
+The above copyright notice and this permission notice shall be included in
+all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
+AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+
+Except as contained in this notice, the name of The Open Group shall not be
+used in advertising or otherwise to promote the sale, use or other dealings
+in this Software without prior written authorization from The Open Group.
+
+
+Copyright 1987 by Digital Equipment Corporation, Maynard, Massachusetts.
+
+ All Rights Reserved
+
+Permission to use, copy, modify, and distribute this software and its
+documentation for any purpose and without fee is hereby granted,
+provided that the above copyright notice appear in all copies and that
+both that copyright notice and this permission notice appear in
+supporting documentation, and that the name of Digital not be
+used in advertising or publicity pertaining to distribution of the
+software without specific, written prior permission.
+
+DIGITAL DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
+ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
+DIGITAL BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
+ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
+WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
+ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
+SOFTWARE.
+
+******************************************************************/
+
+#ifdef HAVE_DIX_CONFIG_H
+#include <dix-config.h>
+#endif
+
+#include <stdio.h>
+#include <X11/X.h>
+#include "os.h"
+#include "osdep.h"
+#include <X11/Xos.h>
+#include <signal.h>
+#include <errno.h>
+#ifdef HAVE_DLFCN_H
+# include <dlfcn.h>
+#endif
+#ifdef HAVE_BACKTRACE
+#include <execinfo.h>
+#endif
+
+#include "misc.h"
+
+#include "dixstruct.h"
+
+
+#if !defined(SYSV) && !defined(WIN32)
+#include <sys/resource.h>
+#endif
+
+#ifndef ADMPATH
+#define ADMPATH "/usr/adm/X%smsgs"
+#endif
+
+extern char *display;
+#ifdef RLIMIT_DATA
+int limitDataSpace = -1;
+#endif
+#ifdef RLIMIT_STACK
+int limitStackSpace = -1;
+#endif
+#ifdef RLIMIT_NOFILE
+int limitNoFile = -1;
+#endif
+
+static OsSigWrapperPtr OsSigWrapper = NULL;
+
+OsSigWrapperPtr
+OsRegisterSigWrapper(OsSigWrapperPtr newSigWrapper)
+{
+ OsSigWrapperPtr oldSigWrapper = OsSigWrapper;
+
+ OsSigWrapper = newSigWrapper;
+
+ return oldSigWrapper;
+}
+
+/*
+ * OsSigHandler --
+ * Catch unexpected signals and exit or continue cleanly.
+ */
+static void
+#ifdef SA_SIGINFO
+OsSigHandler(int signo, siginfo_t *sip, void *unused)
+#else
+OsSigHandler(int signo)
+#endif
+{
+#ifdef RTLD_DI_SETSIGNAL
+ const char *dlerr = dlerror();
+
+ if (dlerr) {
+ LogMessage(X_ERROR, "Dynamic loader error: %s\n", dlerr);
+ }
+#endif /* RTLD_DI_SETSIGNAL */
+
+ if (OsSigWrapper != NULL) {
+ if (OsSigWrapper(signo) == 0) {
+ /* ddx handled signal and wants us to continue */
+ return;
+ }
+ }
+
+ /* log, cleanup, and abort */
+ xorg_backtrace();
+
+#ifdef SA_SIGINFO
+ if (sip->si_code == SI_USER) {
+ ErrorF("Recieved signal %d sent by process %ld, uid %ld\n",
+ signo, (long) sip->si_pid, (long) sip->si_uid);
+ } else {
+ switch (signo) {
+ case SIGSEGV:
+ case SIGBUS:
+ case SIGILL:
+ case SIGFPE:
+ ErrorF("%s at address %p\n", strsignal(signo), sip->si_addr);
+ }
+ }
+#endif
+
+ FatalError("Caught signal %d (%s). Server aborting\n",
+ signo, strsignal(signo));
+}
+
+void
+OsInit(void)
+{
+ static Bool been_here = FALSE;
+ static char* devnull = "/dev/null";
+ char fname[PATH_MAX];
+
+ if (!been_here) {
+#ifndef _MSC_VER
+ struct sigaction act, oact;
+ int i;
+ int siglist[] = { SIGSEGV, SIGQUIT, SIGILL, SIGFPE, SIGBUS,
+ SIGSYS,
+ SIGXCPU,
+ SIGXFSZ,
+#ifdef SIGEMT
+ SIGEMT,
+#endif
+ 0 /* must be last */ };
+ sigemptyset(&act.sa_mask);
+#ifdef SA_SIGINFO
+ act.sa_sigaction = OsSigHandler;
+ act.sa_flags = SA_SIGINFO;
+#else
+ act.sa_handler = OsSigHandler;
+ act.sa_flags = 0;
+#endif
+ for (i = 0; siglist[i] != 0; i++) {
+ if (sigaction(siglist[i], &act, &oact)) {
+ ErrorF("failed to install signal handler for signal %d: %s\n",
+ siglist[i], strerror(errno));
+ }
+ }
+#ifdef HAVE_BACKTRACE
+ /*
+ * initialize the backtracer, since the ctor calls dlopen(), which
+ * calls malloc(), which isn't signal-safe.
+ */
+ do {
+ void *array;
+ backtrace(&array, 1);
+ } while (0);
+#endif
+
+#ifdef RTLD_DI_SETSIGNAL
+ /* Tell runtime linker to send a signal we can catch instead of SIGKILL
+ * for failures to load libraries/modules at runtime so we can clean up
+ * after ourselves.
+ */
+ int failure_signal = SIGQUIT;
+ dlinfo(RTLD_SELF, RTLD_DI_SETSIGNAL, &failure_signal);
+#endif
+#endif
+#if !defined(__CYGWIN__)
+ fclose(stdin);
+ fclose(stdout);
+#endif
+ /*
+ * If a write of zero bytes to stderr returns non-zero, i.e. -1,
+ * then writing to stderr failed, and we'll write somewhere else
+ * instead. (Apparently this never happens in the Real World.)
+ */
+ if (write (2, fname, 0) == -1)
+ {
+ FILE *err;
+
+ if (strlen (display) + strlen (ADMPATH) + 1 < sizeof fname)
+ snprintf (fname, sizeof(fname), ADMPATH, display);
+ else
+ strcpy (fname, devnull);
+ /*
+ * uses stdio to avoid os dependencies here,
+ * a real os would use
+ * open (fname, O_WRONLY|O_APPEND|O_CREAT, 0666)
+ */
+ if (!(err = fopen (fname, "a+")))
+ err = fopen (devnull, "w");
+ if (err && (fileno(err) != 2)) {
+ dup2 (fileno (err), 2);
+ fclose (err);
+ }
+#if defined(SYSV) || defined(SVR4) || defined(WIN32) || defined(__CYGWIN__)
+ {
+ static char buf[BUFSIZ];
+ setvbuf (stderr, buf, _IOLBF, BUFSIZ);
+ }
+#else
+ setlinebuf(stderr);
+#endif
+ }
+
+#ifndef _MSC_VER
+ if (getpgrp () == 0)
+ setpgid (0, 0);
+#endif
+
+#ifdef RLIMIT_DATA
+ if (limitDataSpace >= 0)
+ {
+ struct rlimit rlim;
+
+ if (!getrlimit(RLIMIT_DATA, &rlim))
+ {
+ if ((limitDataSpace > 0) && (limitDataSpace < rlim.rlim_max))
+ rlim.rlim_cur = limitDataSpace;
+ else
+ rlim.rlim_cur = rlim.rlim_max;
+ (void)setrlimit(RLIMIT_DATA, &rlim);
+ }
+ }
+#endif
+#ifdef RLIMIT_STACK
+ if (limitStackSpace >= 0)
+ {
+ struct rlimit rlim;
+
+ if (!getrlimit(RLIMIT_STACK, &rlim))
+ {
+ if ((limitStackSpace > 0) && (limitStackSpace < rlim.rlim_max))
+ rlim.rlim_cur = limitStackSpace;
+ else
+ rlim.rlim_cur = rlim.rlim_max;
+ (void)setrlimit(RLIMIT_STACK, &rlim);
+ }
+ }
+#endif
+#ifdef RLIMIT_NOFILE
+ if (limitNoFile >= 0)
+ {
+ struct rlimit rlim;
+
+ if (!getrlimit(RLIMIT_NOFILE, &rlim))
+ {
+ if ((limitNoFile > 0) && (limitNoFile < rlim.rlim_max))
+ rlim.rlim_cur = limitNoFile;
+ else
+ rlim.rlim_cur = rlim.rlim_max;
+ (void)setrlimit(RLIMIT_NOFILE, &rlim);
+ }
+ }
+#endif
+ LockServer();
+ been_here = TRUE;
+ }
+ TimerInit();
+ OsVendorInit();
+ /*
+ * No log file by default. OsVendorInit() should call LogInit() with the
+ * log file name if logging to a file is desired.
+ */
+ LogInit(NULL, NULL);
+ SmartScheduleInit ();
+}
+
+void
+OsCleanup(Bool terminating)
+{
+ if (terminating)
+ {
+ UnlockServer();
+ }
+}
diff --git a/xorg-server/os/rpcauth.c b/xorg-server/os/rpcauth.c
index b40f9359a..989a49a06 100644
--- a/xorg-server/os/rpcauth.c
+++ b/xorg-server/os/rpcauth.c
@@ -1,193 +1,194 @@
-/*
-
-Copyright 1991, 1998 The Open Group
-
-Permission to use, copy, modify, distribute, and sell this software and its
-documentation for any purpose is hereby granted without fee, provided that
-the above copyright notice appear in all copies and that both that
-copyright notice and this permission notice appear in supporting
-documentation.
-
-The above copyright notice and this permission notice shall be included
-in all copies or substantial portions of the Software.
-
-THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
-OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
-IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
-OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
-ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
-OTHER DEALINGS IN THE SOFTWARE.
-
-Except as contained in this notice, the name of The Open Group shall
-not be used in advertising or otherwise to promote the sale, use or
-other dealings in this Software without prior written authorization
-from The Open Group.
-
-*/
-
-/*
- * SUN-DES-1 authentication mechanism
- * Author: Mayank Choudhary, Sun Microsystems
- */
-
-
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
-#ifdef SECURE_RPC
-
-#include <X11/X.h>
-#include <X11/Xauth.h>
-#include "misc.h"
-#include "os.h"
-#include "osdep.h"
-#include "dixstruct.h"
-
-#include <rpc/rpc.h>
-
-#ifdef sun
-/* <rpc/auth.h> only includes this if _KERNEL is #defined... */
-extern bool_t xdr_opaque_auth(XDR *, struct opaque_auth *);
-#endif
-
-static enum auth_stat why;
-
-static char *
-authdes_ezdecode(const char *inmsg, int len)
-{
- struct rpc_msg msg;
- char cred_area[MAX_AUTH_BYTES];
- char verf_area[MAX_AUTH_BYTES];
- char *temp_inmsg;
- struct svc_req r;
- bool_t res0, res1;
- XDR xdr;
- SVCXPRT xprt;
-
- temp_inmsg = malloc(len);
- memmove(temp_inmsg, inmsg, len);
-
- memset((char *)&msg, 0, sizeof(msg));
- memset((char *)&r, 0, sizeof(r));
- memset(cred_area, 0, sizeof(cred_area));
- memset(verf_area, 0, sizeof(verf_area));
-
- msg.rm_call.cb_cred.oa_base = cred_area;
- msg.rm_call.cb_verf.oa_base = verf_area;
- why = AUTH_FAILED;
- xdrmem_create(&xdr, temp_inmsg, len, XDR_DECODE);
-
- if ((r.rq_clntcred = malloc(MAX_AUTH_BYTES)) == NULL)
- goto bad1;
- r.rq_xprt = &xprt;
-
- /* decode into msg */
- res0 = xdr_opaque_auth(&xdr, &(msg.rm_call.cb_cred));
- res1 = xdr_opaque_auth(&xdr, &(msg.rm_call.cb_verf));
- if ( ! (res0 && res1) )
- goto bad2;
-
- /* do the authentication */
-
- r.rq_cred = msg.rm_call.cb_cred; /* read by opaque stuff */
- if (r.rq_cred.oa_flavor != AUTH_DES) {
- why = AUTH_TOOWEAK;
- goto bad2;
- }
-#ifdef SVR4
- if ((why = __authenticate(&r, &msg)) != AUTH_OK) {
-#else
- if ((why = _authenticate(&r, &msg)) != AUTH_OK) {
-#endif
- goto bad2;
- }
- return (((struct authdes_cred *) r.rq_clntcred)->adc_fullname.name);
-
-bad2:
- free(r.rq_clntcred);
-bad1:
- return ((char *)0); /* ((struct authdes_cred *) NULL); */
-}
-
-static XID rpc_id = (XID) ~0L;
-
-static Bool
-CheckNetName (
- unsigned char *addr,
- short len,
- pointer closure
-)
-{
- return (len == strlen ((char *) closure) &&
- strncmp ((char *) addr, (char *) closure, len) == 0);
-}
-
-static char rpc_error[MAXNETNAMELEN+50];
-
-_X_HIDDEN XID
-SecureRPCCheck (unsigned short data_length, const char *data,
- ClientPtr client, char **reason)
-{
- char *fullname;
-
- if (rpc_id == (XID) ~0L) {
- *reason = "Secure RPC authorization not initialized";
- } else {
- fullname = authdes_ezdecode(data, data_length);
- if (fullname == (char *)0) {
- sprintf(rpc_error, "Unable to authenticate secure RPC client (why=%d)", why);
- *reason = rpc_error;
- } else {
- if (ForEachHostInFamily (FamilyNetname, CheckNetName, fullname))
- return rpc_id;
- sprintf(rpc_error, "Principal \"%s\" is not authorized to connect",
- fullname);
- *reason = rpc_error;
- }
- }
- return (XID) ~0L;
-}
-
-_X_HIDDEN void
-SecureRPCInit (void)
-{
- if (rpc_id == ~0L)
- AddAuthorization (9, "SUN-DES-1", 0, (char *) 0);
-}
-
-_X_HIDDEN int
-SecureRPCAdd (unsigned short data_length, const char *data, XID id)
-{
- if (data_length)
- AddHost ((pointer) 0, FamilyNetname, data_length, data);
- rpc_id = id;
- return 1;
-}
-
-_X_HIDDEN int
-SecureRPCReset (void)
-{
- rpc_id = (XID) ~0L;
- return 1;
-}
-
-_X_HIDDEN XID
-SecureRPCToID (unsigned short data_length, char *data)
-{
- return rpc_id;
-}
-
-_X_HIDDEN int
-SecureRPCFromID (XID id, unsigned short *data_lenp, char **datap)
-{
- return 0;
-}
-
-_X_HIDDEN int
-SecureRPCRemove (unsigned short data_length, const char *data)
-{
- return 0;
-}
-#endif /* SECURE_RPC */
+/*
+
+Copyright 1991, 1998 The Open Group
+
+Permission to use, copy, modify, distribute, and sell this software and its
+documentation for any purpose is hereby granted without fee, provided that
+the above copyright notice appear in all copies and that both that
+copyright notice and this permission notice appear in supporting
+documentation.
+
+The above copyright notice and this permission notice shall be included
+in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
+OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
+ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
+OTHER DEALINGS IN THE SOFTWARE.
+
+Except as contained in this notice, the name of The Open Group shall
+not be used in advertising or otherwise to promote the sale, use or
+other dealings in this Software without prior written authorization
+from The Open Group.
+
+*/
+
+/*
+ * SUN-DES-1 authentication mechanism
+ * Author: Mayank Choudhary, Sun Microsystems
+ */
+
+
+#ifdef HAVE_DIX_CONFIG_H
+#include <dix-config.h>
+#endif
+
+#ifdef SECURE_RPC
+
+#include <X11/X.h>
+#include <X11/Xauth.h>
+#include "misc.h"
+#include "os.h"
+#include "osdep.h"
+#include "dixstruct.h"
+
+#include <rpc/rpc.h>
+
+#ifdef sun
+/* <rpc/auth.h> only includes this if _KERNEL is #defined... */
+extern bool_t xdr_opaque_auth(XDR *, struct opaque_auth *);
+#endif
+
+static enum auth_stat why;
+
+static char *
+authdes_ezdecode(const char *inmsg, int len)
+{
+ struct rpc_msg msg;
+ char cred_area[MAX_AUTH_BYTES];
+ char verf_area[MAX_AUTH_BYTES];
+ char *temp_inmsg;
+ struct svc_req r;
+ bool_t res0, res1;
+ XDR xdr;
+ SVCXPRT xprt;
+
+ temp_inmsg = malloc(len);
+ memmove(temp_inmsg, inmsg, len);
+
+ memset((char *)&msg, 0, sizeof(msg));
+ memset((char *)&r, 0, sizeof(r));
+ memset(cred_area, 0, sizeof(cred_area));
+ memset(verf_area, 0, sizeof(verf_area));
+
+ msg.rm_call.cb_cred.oa_base = cred_area;
+ msg.rm_call.cb_verf.oa_base = verf_area;
+ why = AUTH_FAILED;
+ xdrmem_create(&xdr, temp_inmsg, len, XDR_DECODE);
+
+ if ((r.rq_clntcred = malloc(MAX_AUTH_BYTES)) == NULL)
+ goto bad1;
+ r.rq_xprt = &xprt;
+
+ /* decode into msg */
+ res0 = xdr_opaque_auth(&xdr, &(msg.rm_call.cb_cred));
+ res1 = xdr_opaque_auth(&xdr, &(msg.rm_call.cb_verf));
+ if ( ! (res0 && res1) )
+ goto bad2;
+
+ /* do the authentication */
+
+ r.rq_cred = msg.rm_call.cb_cred; /* read by opaque stuff */
+ if (r.rq_cred.oa_flavor != AUTH_DES) {
+ why = AUTH_TOOWEAK;
+ goto bad2;
+ }
+#ifdef SVR4
+ if ((why = __authenticate(&r, &msg)) != AUTH_OK) {
+#else
+ if ((why = _authenticate(&r, &msg)) != AUTH_OK) {
+#endif
+ goto bad2;
+ }
+ return (((struct authdes_cred *) r.rq_clntcred)->adc_fullname.name);
+
+bad2:
+ free(r.rq_clntcred);
+bad1:
+ return ((char *)0); /* ((struct authdes_cred *) NULL); */
+}
+
+static XID rpc_id = (XID) ~0L;
+
+static Bool
+CheckNetName (
+ unsigned char *addr,
+ short len,
+ pointer closure
+)
+{
+ return (len == strlen ((char *) closure) &&
+ strncmp ((char *) addr, (char *) closure, len) == 0);
+}
+
+static char rpc_error[MAXNETNAMELEN+50];
+
+_X_HIDDEN XID
+SecureRPCCheck (unsigned short data_length, const char *data,
+ ClientPtr client, char **reason)
+{
+ char *fullname;
+
+ if (rpc_id == (XID) ~0L) {
+ *reason = "Secure RPC authorization not initialized";
+ } else {
+ fullname = authdes_ezdecode(data, data_length);
+ if (fullname == (char *)0) {
+ snprintf(rpc_error, sizeof(rpc_error),
+ "Unable to authenticate secure RPC client (why=%d)", why);
+ *reason = rpc_error;
+ } else {
+ if (ForEachHostInFamily (FamilyNetname, CheckNetName, fullname))
+ return rpc_id;
+ snprintf(rpc_error, sizeof(rpc_error),
+ "Principal \"%s\" is not authorized to connect", fullname);
+ *reason = rpc_error;
+ }
+ }
+ return (XID) ~0L;
+}
+
+_X_HIDDEN void
+SecureRPCInit (void)
+{
+ if (rpc_id == ~0L)
+ AddAuthorization (9, "SUN-DES-1", 0, (char *) 0);
+}
+
+_X_HIDDEN int
+SecureRPCAdd (unsigned short data_length, const char *data, XID id)
+{
+ if (data_length)
+ AddHost ((pointer) 0, FamilyNetname, data_length, data);
+ rpc_id = id;
+ return 1;
+}
+
+_X_HIDDEN int
+SecureRPCReset (void)
+{
+ rpc_id = (XID) ~0L;
+ return 1;
+}
+
+_X_HIDDEN XID
+SecureRPCToID (unsigned short data_length, char *data)
+{
+ return rpc_id;
+}
+
+_X_HIDDEN int
+SecureRPCFromID (XID id, unsigned short *data_lenp, char **datap)
+{
+ return 0;
+}
+
+_X_HIDDEN int
+SecureRPCRemove (unsigned short data_length, const char *data)
+{
+ return 0;
+}
+#endif /* SECURE_RPC */
diff --git a/xorg-server/os/strndup.c b/xorg-server/os/strndup.c
new file mode 100644
index 000000000..bf8e982d4
--- /dev/null
+++ b/xorg-server/os/strndup.c
@@ -0,0 +1,49 @@
+/*
+ * Copyright (c) 1988, 1993
+ * The Regents of the University of California. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ * notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ * notice, this list of conditions and the following disclaimer in the
+ * documentation and/or other materials provided with the distribution.
+ * 4. Neither the name of the University nor the names of its contributors
+ * may be used to endorse or promote products derived from this software
+ * without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ */
+
+#include <stddef.h>
+#include <stdlib.h>
+#include <string.h>
+#include "os.h"
+
+char *
+strndup(const char *str, size_t n)
+{
+ size_t len;
+ char *copy;
+
+ for (len = 0; len < n && str[len]; len++)
+ continue;
+
+ if ((copy = malloc(len + 1)) == NULL)
+ return (NULL);
+ memcpy(copy, str, len);
+ copy[len] = '\0';
+ return (copy);
+}
diff --git a/xorg-server/os/utils.c b/xorg-server/os/utils.c
index 30ffb4373..27275248d 100644
--- a/xorg-server/os/utils.c
+++ b/xorg-server/os/utils.c
@@ -279,7 +279,7 @@ LockServer(void)
*/
tmppath = LOCK_DIR;
- sprintf(port, "%d", atoi(display));
+ snprintf(port, sizeof(port), "%d", atoi(display));
len = strlen(LOCK_PREFIX) > strlen(LOCK_TMP_PREFIX) ? strlen(LOCK_PREFIX) :
strlen(LOCK_TMP_PREFIX);
len += strlen(tmppath) + strlen(port) + strlen(LOCK_SUFFIX) + 1;
@@ -316,7 +316,7 @@ LockServer(void)
}
if (lfd < 0)
FatalError("Could not create lock file in %s\n", tmp);
- (void) sprintf(pid_str, "%10ld\n", (long)getpid());
+ snprintf(pid_str, sizeof(pid_str), "%10ld\n", (long)getpid());
(void) write(lfd, pid_str, 11);
(void) fchmod(lfd, 0444);
(void) close(lfd);
@@ -1308,7 +1308,7 @@ OsAbort (void)
*/
int
-System(char *command)
+System(const char *command)
{
int pid, p;
void (*csig)(int);
@@ -1358,7 +1358,7 @@ static struct pid {
OsSigHandlerPtr old_alarm = NULL; /* XXX horrible awful hack */
pointer
-Popen(char *command, char *type)
+Popen(const char *command, const char *type)
{
struct pid *cur;
FILE *iop;
@@ -1444,7 +1444,7 @@ Popen(char *command, char *type)
/* fopen that drops privileges */
pointer
-Fopen(char *file, char *type)
+Fopen(const char *file, const char *type)
{
FILE *iop;
#ifndef HAS_SAVED_IDS_AND_SETEUID
@@ -1722,13 +1722,11 @@ CheckUserParameters(int argc, char **argv, char **envp)
if (!eq)
continue;
len = eq - envp[i];
- e = malloc(len + 1);
+ e = strndup(envp[i], len);
if (!e) {
bad = InternalError;
break;
}
- strncpy(e, envp[i], len);
- e[len] = 0;
if (len >= 4 &&
(strcmp(e + len - 4, "PATH") == 0 ||
strcmp(e, "TERMCAP") == 0)) {
diff --git a/xorg-server/os/xdmcp.c b/xorg-server/os/xdmcp.c
index 3958f06dc..9e79ea702 100644
--- a/xorg-server/os/xdmcp.c
+++ b/xorg-server/os/xdmcp.c
@@ -1,1757 +1,1757 @@
-/*
- * Copyright 1989 Network Computing Devices, Inc., Mountain View, California.
- *
- * Permission to use, copy, modify, and distribute this software and its
- * documentation for any purpose and without fee is hereby granted, provided
- * that the above copyright notice appear in all copies and that both that
- * copyright notice and this permission notice appear in supporting
- * documentation, and that the name of N.C.D. not be used in advertising or
- * publicity pertaining to distribution of the software without specific,
- * written prior permission. N.C.D. makes no representations about the
- * suitability of this software for any purpose. It is provided "as is"
- * without express or implied warranty.
- *
- */
-
-#ifdef HAVE_DIX_CONFIG_H
-#include <dix-config.h>
-#endif
-
-#ifdef WIN32
-#include <X11/Xwinsock.h>
-#endif
-
-#include <X11/Xos.h>
-
-#if !defined(WIN32)
-#include <sys/param.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <netdb.h>
-#endif
-
-#include <stdio.h>
-#include <stdlib.h>
-#include <X11/X.h>
-#include <X11/Xmd.h>
-#include "misc.h"
-#include <X11/Xpoll.h>
-#include "osdep.h"
-#include "input.h"
-#include "dixstruct.h"
-#include "opaque.h"
-#include "site.h"
-
-#ifdef STREAMSCONN
-#include <tiuser.h>
-#include <netconfig.h>
-#include <netdir.h>
-#endif
-
-#ifdef XDMCP
-#undef REQUEST
-
-#ifdef XDMCP_NO_IPV6
-#undef IPv6
-#endif
-
-#include <X11/Xdmcp.h>
-
-#define X_INCLUDE_NETDB_H
-#include <X11/Xos_r.h>
-
-static char *defaultDisplayClass = COMPILEDDISPLAYCLASS;
-
-extern void match_interface(u_long u_lQuery);
-
-static int xdmcpSocket, sessionSocket;
-static xdmcp_states state;
-#if defined(IPv6) && defined(AF_INET6)
-static int xdmcpSocket6;
-static struct sockaddr_storage req_sockaddr;
-#else
-static struct sockaddr_in req_sockaddr;
-#endif
-static int req_socklen;
-static CARD32 SessionID;
-static CARD32 timeOutTime;
-static int timeOutRtx;
-static CARD32 defaultKeepaliveDormancy = XDM_DEF_DORMANCY;
-static CARD32 keepaliveDormancy = XDM_DEF_DORMANCY;
-static CARD16 DisplayNumber;
-static xdmcp_states XDM_INIT_STATE = XDM_OFF;
-#ifdef HASXDMAUTH
-static char *xdmAuthCookie;
-#endif
-
-static XdmcpBuffer buffer;
-
-#if defined(IPv6) && defined(AF_INET6)
-
-static struct addrinfo *mgrAddr;
-static struct addrinfo *mgrAddrFirst;
-
-#define SOCKADDR_TYPE struct sockaddr_storage
-#define SOCKADDR_FAMILY(s) ((struct sockaddr *)&(s))->sa_family
-
-#ifdef BSD44SOCKETS
-#define SOCKLEN_FIELD(s) ((struct sockaddr *)&(s))->sa_len
-#define SOCKLEN_TYPE unsigned char
-#else
-#define SOCKLEN_TYPE unsigned int
-#endif
-
-#else
-
-#define SOCKADDR_TYPE struct sockaddr_in
-#define SOCKADDR_FAMILY(s) (s).sin_family
-
-#ifdef BSD44SOCKETS
-#define SOCKLEN_FIELD(s) (s).sin_len
-#define SOCKLEN_TYPE unsigned char
-#else
-#define SOCKLEN_TYPE size_t
-#endif
-
-#endif
-
-static SOCKADDR_TYPE ManagerAddress;
-static SOCKADDR_TYPE FromAddress;
-
-#ifdef SOCKLEN_FIELD
-#define ManagerAddressLen SOCKLEN_FIELD(ManagerAddress)
-#define FromAddressLen SOCKLEN_FIELD(FromAddress)
-#else
-static SOCKLEN_TYPE ManagerAddressLen, FromAddressLen;
-#endif
-
-#if defined(IPv6) && defined(AF_INET6)
-static struct multicastinfo {
- struct multicastinfo *next;
- struct addrinfo *ai;
- int hops;
-} *mcastlist;
-#endif
-
-static void XdmcpAddHost(
- const struct sockaddr *from,
- int fromlen,
- ARRAY8Ptr AuthenticationName,
- ARRAY8Ptr hostname,
- ARRAY8Ptr status);
-
-static void XdmcpSelectHost(
- const struct sockaddr *host_sockaddr,
- int host_len,
- ARRAY8Ptr AuthenticationName);
-
-static void get_xdmcp_sock(void);
-
-static void send_query_msg(void);
-
-static void recv_willing_msg(
- struct sockaddr * /*from*/,
- int /*fromlen*/,
- unsigned /*length*/);
-
-static void send_request_msg(void);
-
-static void recv_accept_msg(unsigned /*length*/);
-
-static void recv_decline_msg(unsigned /*length*/);
-
-static void send_manage_msg(void);
-
-static void recv_refuse_msg(unsigned /*length*/);
-
-static void recv_failed_msg(unsigned /*length*/);
-
-static void send_keepalive_msg(void);
-
-static void recv_alive_msg(unsigned /*length*/);
-
-static void XdmcpFatal(
- const char * /*type*/,
- ARRAY8Ptr /*status*/);
-
-static void XdmcpWarning(const char * /*str*/);
-
-static void get_manager_by_name(
- int /*argc*/,
- char ** /*argv*/,
- int /*i*/);
-
-static void get_fromaddr_by_name(int /*argc*/, char ** /*argv*/, int /*i*/);
-
-#if defined(IPv6) && defined(AF_INET6)
-static int get_mcast_options(int /*argc*/, char ** /*argv*/, int /*i*/);
-#endif
-
-static void receive_packet(int /*socketfd*/);
-
-static void send_packet(void);
-
-static void timeout(void);
-
-static void restart(void);
-
-static void XdmcpBlockHandler(
- pointer /*data*/,
- struct timeval ** /*wt*/,
- pointer /*LastSelectMask*/);
-
-static void XdmcpWakeupHandler(
- pointer /*data*/,
- int /*i*/,
- pointer /*LastSelectMask*/);
-
-#define XSERV_t
-#define TRANS_SERVER
-#define TRANS_REOPEN
-#include <X11/Xtrans/Xtrans.h>
-
-/*
- * Register the Manufacturer display ID
- */
-
-static ARRAY8 ManufacturerDisplayID;
-
-static void
-XdmcpRegisterManufacturerDisplayID (const char *name, int length)
-{
- int i;
-
- XdmcpDisposeARRAY8 (&ManufacturerDisplayID);
- if (!XdmcpAllocARRAY8 (&ManufacturerDisplayID, length))
- return;
- for (i = 0; i < length; i++)
- ManufacturerDisplayID.data[i] = (CARD8) name[i];
-}
-
-static unsigned short xdm_udp_port = XDM_UDP_PORT;
-static Bool OneSession = FALSE;
-static const char *xdm_from = NULL;
-
-void
-XdmcpUseMsg (void)
-{
- ErrorF("-query host-name contact named host for XDMCP\n");
- ErrorF("-broadcast broadcast for XDMCP\n");
-#if defined(IPv6) && defined(AF_INET6)
- ErrorF("-multicast [addr [hops]] IPv6 multicast for XDMCP\n");
-#endif
- ErrorF("-indirect host-name contact named host for indirect XDMCP\n");
- ErrorF("-port port-num UDP port number to send messages to\n");
- ErrorF("-from local-address specify the local address to connect from\n");
- ErrorF("-once Terminate server after one session\n");
- ErrorF("-class display-class specify display class to send in manage\n");
-#ifdef HASXDMAUTH
- ErrorF("-cookie xdm-auth-bits specify the magic cookie for XDMCP\n");
-#endif
- ErrorF("-displayID display-id manufacturer display ID for request\n");
-}
-
-int
-XdmcpOptions(int argc, char **argv, int i)
-{
- if (strcmp(argv[i], "-query") == 0) {
- get_manager_by_name(argc, argv, i++);
- XDM_INIT_STATE = XDM_QUERY;
- AccessUsingXdmcp ();
- return i + 1;
- }
- if (strcmp(argv[i], "-broadcast") == 0) {
- XDM_INIT_STATE = XDM_BROADCAST;
- AccessUsingXdmcp ();
- return i + 1;
- }
-#if defined(IPv6) && defined(AF_INET6)
- if (strcmp(argv[i], "-multicast") == 0) {
- i = get_mcast_options(argc, argv, ++i);
- XDM_INIT_STATE = XDM_MULTICAST;
- AccessUsingXdmcp ();
- return i + 1;
- }
-#endif
- if (strcmp(argv[i], "-indirect") == 0) {
- get_manager_by_name(argc, argv, i++);
- XDM_INIT_STATE = XDM_INDIRECT;
- AccessUsingXdmcp ();
- return i + 1;
- }
- if (strcmp(argv[i], "-port") == 0) {
- if (++i == argc) {
- FatalError("Xserver: missing port number in command line\n");
- }
- xdm_udp_port = (unsigned short) atoi(argv[i]);
- return i + 1;
- }
- if (strcmp(argv[i], "-from") == 0) {
- get_fromaddr_by_name(argc, argv, ++i);
- return i + 1;
- }
- if (strcmp(argv[i], "-once") == 0) {
- OneSession = TRUE;
- return i + 1;
- }
- if (strcmp(argv[i], "-class") == 0) {
- if (++i == argc) {
- FatalError("Xserver: missing class name in command line\n");
- }
- defaultDisplayClass = argv[i];
- return i + 1;
- }
-#ifdef HASXDMAUTH
- if (strcmp(argv[i], "-cookie") == 0) {
- if (++i == argc) {
- FatalError("Xserver: missing cookie data in command line\n");
- }
- xdmAuthCookie = argv[i];
- return i + 1;
- }
-#endif
- if (strcmp(argv[i], "-displayID") == 0) {
- if (++i == argc) {
- FatalError("Xserver: missing displayID in command line\n");
- }
- XdmcpRegisterManufacturerDisplayID (argv[i], strlen (argv[i]));
- return i + 1;
- }
- return i;
-}
-
-/*
- * This section is a collection of routines for
- * registering server-specific data with the XDMCP
- * state machine.
- */
-
-
-/*
- * Save all broadcast addresses away so BroadcastQuery
- * packets get sent everywhere
- */
-
-#define MAX_BROADCAST 10
-
-/* This stays sockaddr_in since IPv6 doesn't support broadcast */
-static struct sockaddr_in BroadcastAddresses[MAX_BROADCAST];
-static int NumBroadcastAddresses;
-
-void
-XdmcpRegisterBroadcastAddress (const struct sockaddr_in *addr)
-{
- struct sockaddr_in *bcast;
- if (NumBroadcastAddresses >= MAX_BROADCAST)
- return;
- bcast = &BroadcastAddresses[NumBroadcastAddresses++];
- memset(bcast, 0, sizeof (struct sockaddr_in));
-#ifdef BSD44SOCKETS
- bcast->sin_len = addr->sin_len;
-#endif
- bcast->sin_family = addr->sin_family;
- bcast->sin_port = htons (xdm_udp_port);
- bcast->sin_addr = addr->sin_addr;
-}
-
-/*
- * Each authentication type is registered here; Validator
- * will be called to check all access attempts using
- * the specified authentication type
- */
-
-static ARRAYofARRAY8 AuthenticationNames, AuthenticationDatas;
-typedef struct _AuthenticationFuncs {
- ValidatorFunc Validator;
- GeneratorFunc Generator;
- AddAuthorFunc AddAuth;
-} AuthenticationFuncsRec, *AuthenticationFuncsPtr;
-
-static AuthenticationFuncsPtr AuthenticationFuncsList;
-
-void
-XdmcpRegisterAuthentication (
- const char *name,
- int namelen,
- const char *data,
- int datalen,
- ValidatorFunc Validator,
- GeneratorFunc Generator,
- AddAuthorFunc AddAuth)
-{
- int i;
- ARRAY8 AuthenticationName, AuthenticationData;
- static AuthenticationFuncsPtr newFuncs;
-
- if (!XdmcpAllocARRAY8 (&AuthenticationName, namelen))
- return;
- if (!XdmcpAllocARRAY8 (&AuthenticationData, datalen))
- {
- XdmcpDisposeARRAY8 (&AuthenticationName);
- return;
- }
- for (i = 0; i < namelen; i++)
- AuthenticationName.data[i] = name[i];
- for (i = 0; i < datalen; i++)
- AuthenticationData.data[i] = data[i];
- if (!(XdmcpReallocARRAYofARRAY8 (&AuthenticationNames,
- AuthenticationNames.length + 1) &&
- XdmcpReallocARRAYofARRAY8 (&AuthenticationDatas,
- AuthenticationDatas.length + 1) &&
- (newFuncs = malloc((AuthenticationNames.length + 1) * sizeof (AuthenticationFuncsRec)))))
- {
- XdmcpDisposeARRAY8 (&AuthenticationName);
- XdmcpDisposeARRAY8 (&AuthenticationData);
- return;
- }
- for (i = 0; i < AuthenticationNames.length - 1; i++)
- newFuncs[i] = AuthenticationFuncsList[i];
- newFuncs[AuthenticationNames.length-1].Validator = Validator;
- newFuncs[AuthenticationNames.length-1].Generator = Generator;
- newFuncs[AuthenticationNames.length-1].AddAuth = AddAuth;
- free(AuthenticationFuncsList);
- AuthenticationFuncsList = newFuncs;
- AuthenticationNames.data[AuthenticationNames.length-1] = AuthenticationName;
- AuthenticationDatas.data[AuthenticationDatas.length-1] = AuthenticationData;
-}
-
-/*
- * Select the authentication type to be used; this is
- * set by the manager of the host to be connected to.
- */
-
-static ARRAY8 noAuthenticationName = {(CARD16) 0, (CARD8Ptr) 0};
-static ARRAY8 noAuthenticationData = {(CARD16) 0, (CARD8Ptr) 0};
-static ARRAY8Ptr AuthenticationName = &noAuthenticationName;
-static ARRAY8Ptr AuthenticationData = &noAuthenticationData;
-static AuthenticationFuncsPtr AuthenticationFuncs;
-
-static void
-XdmcpSetAuthentication (const ARRAY8Ptr name)
-{
- int i;
-
- for (i = 0; i < AuthenticationNames.length; i++)
- if (XdmcpARRAY8Equal (&AuthenticationNames.data[i], name))
- {
- AuthenticationName = &AuthenticationNames.data[i];
- AuthenticationData = &AuthenticationDatas.data[i];
- AuthenticationFuncs = &AuthenticationFuncsList[i];
- break;
- }
-}
-
-/*
- * Register the host address for the display
- */
-
-static ARRAY16 ConnectionTypes;
-static ARRAYofARRAY8 ConnectionAddresses;
-static long xdmcpGeneration;
-
-void
-XdmcpRegisterConnection (
- int type,
- const char *address,
- int addrlen)
-{
- int i;
- CARD8 *newAddress;
-
- if (xdmcpGeneration != serverGeneration)
- {
- XdmcpDisposeARRAY16 (&ConnectionTypes);
- XdmcpDisposeARRAYofARRAY8 (&ConnectionAddresses);
- xdmcpGeneration = serverGeneration;
- }
- if (xdm_from != NULL) { /* Only register the requested address */
- const void *regAddr = address;
- const void *fromAddr = NULL;
- int regAddrlen = addrlen;
-
- if (addrlen == sizeof(struct in_addr)) {
- if (SOCKADDR_FAMILY(FromAddress) == AF_INET) {
- fromAddr = &((struct sockaddr_in *)&FromAddress)->sin_addr;
- }
-#if defined(IPv6) && defined(AF_INET6)
- else if ((SOCKADDR_FAMILY(FromAddress) == AF_INET6) &&
- IN6_IS_ADDR_V4MAPPED(
- &((struct sockaddr_in6 *)&FromAddress)->sin6_addr)) {
- fromAddr = &((struct sockaddr_in6 *)&FromAddress)->sin6_addr.s6_addr[12];
- }
-#endif
- }
-#if defined(IPv6) && defined(AF_INET6)
- else if (addrlen == sizeof(struct in6_addr)) {
- if (SOCKADDR_FAMILY(FromAddress) == AF_INET6) {
- fromAddr = &((struct sockaddr_in6 *)&FromAddress)->sin6_addr;
- } else if ((SOCKADDR_FAMILY(FromAddress) == AF_INET) &&
- IN6_IS_ADDR_V4MAPPED((struct in6_addr *) address)) {
- fromAddr = &((struct sockaddr_in *)&FromAddress)->sin_addr;
- regAddr = &((struct sockaddr_in6 *)&address)->sin6_addr.s6_addr[12];
- regAddrlen = sizeof(struct in_addr);
- }
- }
-#endif
- if (!fromAddr || memcmp(regAddr, fromAddr, regAddrlen) != 0) {
- return;
- }
- }
- if (ConnectionAddresses.length + 1 == 256)
- return;
- newAddress = malloc(addrlen * sizeof (CARD8));
- if (!newAddress)
- return;
- if (!XdmcpReallocARRAY16 (&ConnectionTypes, ConnectionTypes.length + 1))
- {
- free(newAddress);
- return;
- }
- if (!XdmcpReallocARRAYofARRAY8 (&ConnectionAddresses,
- ConnectionAddresses.length + 1))
- {
- free(newAddress);
- return;
- }
- ConnectionTypes.data[ConnectionTypes.length - 1] = (CARD16) type;
- for (i = 0; i < addrlen; i++)
- newAddress[i] = address[i];
- ConnectionAddresses.data[ConnectionAddresses.length-1].data = newAddress;
- ConnectionAddresses.data[ConnectionAddresses.length-1].length = addrlen;
-}
-
-/*
- * Register an Authorization Name. XDMCP advertises this list
- * to the manager.
- */
-
-static ARRAYofARRAY8 AuthorizationNames;
-
-void
-XdmcpRegisterAuthorizations (void)
-{
- XdmcpDisposeARRAYofARRAY8 (&AuthorizationNames);
- RegisterAuthorizations ();
-}
-
-void
-XdmcpRegisterAuthorization (const char *name, int namelen)
-{
- ARRAY8 authName;
- int i;
-
- authName.data = malloc(namelen * sizeof (CARD8));
- if (!authName.data)
- return;
- if (!XdmcpReallocARRAYofARRAY8 (&AuthorizationNames, AuthorizationNames.length +1))
- {
- free(authName.data);
- return;
- }
- for (i = 0; i < namelen; i++)
- authName.data[i] = (CARD8) name[i];
- authName.length = namelen;
- AuthorizationNames.data[AuthorizationNames.length-1] = authName;
-}
-
-/*
- * Register the DisplayClass string
- */
-
-static ARRAY8 DisplayClass;
-
-static void
-XdmcpRegisterDisplayClass (const char *name, int length)
-{
- int i;
-
- XdmcpDisposeARRAY8 (&DisplayClass);
- if (!XdmcpAllocARRAY8 (&DisplayClass, length))
- return;
- for (i = 0; i < length; i++)
- DisplayClass.data[i] = (CARD8) name[i];
-}
-
-/*
- * initialize XDMCP; create the socket, compute the display
- * number, set up the state machine
- */
-
-void
-XdmcpInit(void)
-{
- state = XDM_INIT_STATE;
-#ifdef HASXDMAUTH
- if (xdmAuthCookie)
- XdmAuthenticationInit (xdmAuthCookie, strlen (xdmAuthCookie));
-#endif
- if (state != XDM_OFF)
- {
- XdmcpRegisterAuthorizations();
- XdmcpRegisterDisplayClass (defaultDisplayClass, strlen (defaultDisplayClass));
- AccessUsingXdmcp();
- RegisterBlockAndWakeupHandlers (XdmcpBlockHandler, XdmcpWakeupHandler,
- (pointer) 0);
- timeOutRtx = 0;
- DisplayNumber = (CARD16) atoi(display);
- if (ConnectionTypes.length>1 && xdm_from==NULL)
- {
- unsigned i=0;
- char ErrorMessage[1024];
- sprintf(ErrorMessage,"Multiple ip-addresses detected:\n");
- for (i=0; i<ConnectionTypes.length; i++)
- {
- int AddrLen=ConnectionAddresses.data[i].length;
- if (AddrLen==4)
- sprintf(ErrorMessage+strlen(ErrorMessage)," %d.%d.%d.%d\n",
- ConnectionAddresses.data[i].data[0],
- ConnectionAddresses.data[i].data[1],
- ConnectionAddresses.data[i].data[2],
- ConnectionAddresses.data[i].data[3]);
- else
- {
- int j;
- sprintf(ErrorMessage+strlen(ErrorMessage)," ");
- for (j=0; j<AddrLen; j++)
- sprintf(ErrorMessage+strlen(ErrorMessage),"%02x",ConnectionAddresses.data[i].data[j]);
- sprintf(ErrorMessage+strlen(ErrorMessage),"\n");
- }
- }
- sprintf(ErrorMessage+strlen(ErrorMessage),"Please specify the ip-address you want to use with -from\n");
- FatalError(ErrorMessage);
- }
- get_xdmcp_sock();
- send_packet();
- }
-}
-
-void
-XdmcpReset (void)
-{
- state = XDM_INIT_STATE;
- if (state != XDM_OFF)
- {
- RegisterBlockAndWakeupHandlers (XdmcpBlockHandler, XdmcpWakeupHandler,
- (pointer) 0);
- timeOutRtx = 0;
- send_packet();
- }
-}
-
-/*
- * Called whenever a new connection is created; notices the
- * first connection and saves it to terminate the session
- * when it is closed
- */
-
-void
-XdmcpOpenDisplay(int sock)
-{
- if (state != XDM_AWAIT_MANAGE_RESPONSE)
- return;
- state = XDM_RUN_SESSION;
- sessionSocket = sock;
-}
-
-void
-XdmcpCloseDisplay(int sock)
-{
- if ((state != XDM_RUN_SESSION && state != XDM_AWAIT_ALIVE_RESPONSE)
- || sessionSocket != sock)
- return;
- state = XDM_INIT_STATE;
- if (OneSession)
- dispatchException |= DE_TERMINATE;
- else
- dispatchException |= DE_RESET;
- isItTimeToYield = TRUE;
-}
-
-/*
- * called before going to sleep, this routine
- * may modify the timeout value about to be sent
- * to select; in this way XDMCP can do appropriate things
- * dynamically while starting up
- */
-
-/*ARGSUSED*/
-static void
-XdmcpBlockHandler(
- pointer data, /* unused */
- struct timeval **wt,
- pointer pReadmask)
-{
- fd_set *LastSelectMask = (fd_set*)pReadmask;
- CARD32 millisToGo;
-
- if (state == XDM_OFF)
- return;
- FD_SET(xdmcpSocket, LastSelectMask);
-#if defined(IPv6) && defined(AF_INET6)
- if (xdmcpSocket6 >= 0)
- FD_SET(xdmcpSocket6, LastSelectMask);
-#endif
- if (timeOutTime == 0)
- return;
- millisToGo = timeOutTime - GetTimeInMillis();
- if ((int) millisToGo <= 0)
- millisToGo = 1;
- AdjustWaitForDelay (wt, millisToGo);
-}
-
-/*
- * called after select returns; this routine will
- * recognise when XDMCP packets await and
- * process them appropriately
- */
-
-/*ARGSUSED*/
-static void
-XdmcpWakeupHandler(
- pointer data, /* unused */
- int i,
- pointer pReadmask)
-{
- fd_set* LastSelectMask = (fd_set*)pReadmask;
- fd_set devicesReadable;
-
- if (state == XDM_OFF)
- return;
- if (i > 0)
- {
- if (FD_ISSET(xdmcpSocket, LastSelectMask))
- {
- receive_packet(xdmcpSocket);
- FD_CLR(xdmcpSocket, LastSelectMask);
- }
-#if defined(IPv6) && defined(AF_INET6)
- if (xdmcpSocket6 >= 0 && FD_ISSET(xdmcpSocket6, LastSelectMask))
- {
- receive_packet(xdmcpSocket6);
- FD_CLR(xdmcpSocket6, LastSelectMask);
- }
-#endif
- XFD_ANDSET(&devicesReadable, LastSelectMask, &EnabledDevices);
- if (XFD_ANYSET(&devicesReadable))
- {
- if (state == XDM_AWAIT_USER_INPUT)
- restart();
- else if (state == XDM_RUN_SESSION)
- keepaliveDormancy = defaultKeepaliveDormancy;
- }
- }
-}
-
-/*
- * This routine should be called from the routine that drives the
- * user's host menu when the user selects a host
- */
-
-static void
-XdmcpSelectHost(
- const struct sockaddr *host_sockaddr,
- int host_len,
- ARRAY8Ptr AuthenticationName)
-{
- state = XDM_START_CONNECTION;
- memmove(&req_sockaddr, host_sockaddr, host_len);
- req_socklen = host_len;
- XdmcpSetAuthentication (AuthenticationName);
- send_packet();
-}
-
-/*
- * !!! this routine should be replaced by a routine that adds
- * the host to the user's host menu. the current version just
- * selects the first host to respond with willing message.
- */
-
-#ifdef _MSC_VER
-void DisplayXdmcpHostsDialog(void);
-int XdmcpHostAddName(const char *HostName, int HostIndex);
-
-struct hostinfo
-{
- struct sockaddr *from;
- int fromlen;
- ARRAY8 AuthenticationName;
-};
-static int g_NrHosts;
-static struct hostinfo *g_Hosts;
-
-void XdmcpHostSelected(int HostIdx)
-{
- int i;
-
- /* Connect to the selected host */
- XdmcpSelectHost(g_Hosts[HostIdx].from, g_Hosts[HostIdx].fromlen, &g_Hosts[HostIdx].AuthenticationName);
-
- for (i=0; i<g_NrHosts; i++)
- {
- free(g_Hosts[i].from);
- free(g_Hosts[i].AuthenticationName.data);
- }
- free(g_Hosts);
- g_Hosts=NULL;
- g_NrHosts=0;
-}
-#endif
-
-/*ARGSUSED*/
-static void
-XdmcpAddHost(
- const struct sockaddr *from,
- int fromlen,
- ARRAY8Ptr AuthenticationName,
- ARRAY8Ptr hostname,
- ARRAY8Ptr status)
-{
-#ifdef _MSC_VER
- char szHostName[100];
- int HostIdx;
-
- memcpy(szHostName,hostname->data,hostname->length);
- szHostName[hostname->length]=0;
-
- DisplayXdmcpHostsDialog(); /* Display the dialog if not already displayed */
-
- HostIdx=XdmcpHostAddName(szHostName, g_NrHosts);
- if (HostIdx==-1)
- {
- HostIdx=g_NrHosts;
- g_NrHosts++;
- g_Hosts=realloc(g_Hosts,g_NrHosts*sizeof(*g_Hosts));
- g_Hosts[HostIdx].AuthenticationName.data=NULL;
- g_Hosts[HostIdx].from=NULL;
- }
-
- g_Hosts[HostIdx].fromlen=fromlen;
- g_Hosts[HostIdx].from=realloc(g_Hosts[HostIdx].from,g_Hosts[HostIdx].fromlen);
- memcpy(g_Hosts[HostIdx].from,from,fromlen);
-
- g_Hosts[HostIdx].AuthenticationName.length=AuthenticationName->length;
- g_Hosts[HostIdx].AuthenticationName.data=realloc(g_Hosts[HostIdx].AuthenticationName.data,AuthenticationName->length);
- memcpy(g_Hosts[HostIdx].AuthenticationName.data,AuthenticationName->data,AuthenticationName->length);
-#else
- XdmcpSelectHost(from, fromlen, AuthenticationName);
-#endif
-}
-
-/*
- * A message is queued on the socket; read it and
- * do the appropriate thing
- */
-
-static ARRAY8 UnwillingMessage = { (CARD8) 14, (CARD8 *) "Host unwilling" };
-
-static void
-receive_packet(int socketfd)
-{
-#if defined(IPv6) && defined(AF_INET6)
- struct sockaddr_storage from;
-#else
- struct sockaddr_in from;
-#endif
- int fromlen = sizeof(from);
- XdmcpHeader header;
-
- /* read message off socket */
- if (!XdmcpFill (socketfd, &buffer, (XdmcpNetaddr) &from, &fromlen))
- return;
-
- /* reset retransmission backoff */
- timeOutRtx = 0;
-
- if (!XdmcpReadHeader (&buffer, &header))
- return;
-
- if (header.version != XDM_PROTOCOL_VERSION)
- return;
-
- switch (header.opcode) {
- case WILLING:
- recv_willing_msg((struct sockaddr *) &from, fromlen, header.length);
- break;
- case UNWILLING:
- XdmcpFatal("Manager unwilling", &UnwillingMessage);
- break;
- case ACCEPT:
- recv_accept_msg(header.length);
- break;
- case DECLINE:
- recv_decline_msg(header.length);
- break;
- case REFUSE:
- recv_refuse_msg(header.length);
- break;
- case FAILED:
- recv_failed_msg(header.length);
- break;
- case ALIVE:
- recv_alive_msg(header.length);
- break;
- }
-}
-
-/*
- * send the appropriate message given the current state
- */
-
-static void
-send_packet(void)
-{
- int rtx;
- switch (state) {
- case XDM_QUERY:
- case XDM_BROADCAST:
- case XDM_INDIRECT:
-#if defined(IPv6) && defined(AF_INET6)
- case XDM_MULTICAST:
-#endif
- send_query_msg();
- break;
- case XDM_START_CONNECTION:
- send_request_msg();
- break;
- case XDM_MANAGE:
- send_manage_msg();
- break;
- case XDM_KEEPALIVE:
- send_keepalive_msg();
- break;
- default:
- break;
- }
- rtx = (XDM_MIN_RTX << timeOutRtx);
- if (rtx > XDM_MAX_RTX)
- rtx = XDM_MAX_RTX;
- timeOutTime = GetTimeInMillis() + rtx * 1000;
-}
-
-/*
- * The session is declared dead for some reason; too many
- * timeouts, or Keepalive failure.
- */
-
-static void
-XdmcpDeadSession (const char *reason)
-{
- ErrorF ("XDM: %s, declaring session dead\n", reason);
- state = XDM_INIT_STATE;
- isItTimeToYield = TRUE;
- dispatchException |= DE_RESET;
- timeOutTime = 0;
- timeOutRtx = 0;
- send_packet();
-}
-
-/*
- * Timeout waiting for an XDMCP response.
- */
-
-static void
-timeout(void)
-{
- timeOutRtx++;
- if (state == XDM_AWAIT_ALIVE_RESPONSE && timeOutRtx >= XDM_KA_RTX_LIMIT )
- {
- XdmcpDeadSession ("too many keepalive retransmissions");
- return;
- }
- else if (timeOutRtx >= XDM_RTX_LIMIT)
- {
- /* Quit if "-once" specified, otherwise reset and try again. */
- if (OneSession) {
- dispatchException |= DE_TERMINATE;
- ErrorF("XDM: too many retransmissions\n");
- } else {
- XdmcpDeadSession("too many retransmissions");
- }
- return;
- }
-
-#if defined(IPv6) && defined(AF_INET6)
- if (state == XDM_COLLECT_QUERY || state == XDM_COLLECT_INDIRECT_QUERY) {
- /* Try next address */
- for (mgrAddr = mgrAddr->ai_next; ; mgrAddr = mgrAddr->ai_next) {
- if (mgrAddr == NULL) {
- mgrAddr = mgrAddrFirst;
- }
- if (mgrAddr->ai_family == AF_INET
- || mgrAddr->ai_family == AF_INET6)
- break;
- }
-#ifndef SIN6_LEN
- ManagerAddressLen = mgrAddr->ai_addrlen;
-#endif
- memcpy(&ManagerAddress, mgrAddr->ai_addr, mgrAddr->ai_addrlen);
- }
-#endif
-
- switch (state) {
- case XDM_COLLECT_QUERY:
- state = XDM_QUERY;
- break;
- case XDM_COLLECT_BROADCAST_QUERY:
- state = XDM_BROADCAST;
- break;
-#if defined(IPv6) && defined(AF_INET6)
- case XDM_COLLECT_MULTICAST_QUERY:
- state = XDM_MULTICAST;
- break;
-#endif
- case XDM_COLLECT_INDIRECT_QUERY:
- state = XDM_INDIRECT;
- break;
- case XDM_AWAIT_REQUEST_RESPONSE:
- state = XDM_START_CONNECTION;
- break;
- case XDM_AWAIT_MANAGE_RESPONSE:
- state = XDM_MANAGE;
- break;
- case XDM_AWAIT_ALIVE_RESPONSE:
- state = XDM_KEEPALIVE;
- break;
- default:
- break;
- }
- send_packet();
-}
-
-static void
-restart(void)
-{
- state = XDM_INIT_STATE;
- timeOutRtx = 0;
- send_packet();
-}
-
-static int
-XdmcpCheckAuthentication (ARRAY8Ptr Name, ARRAY8Ptr Data, int packet_type)
-{
- return (XdmcpARRAY8Equal (Name, AuthenticationName) &&
- (AuthenticationName->length == 0 ||
- (*AuthenticationFuncs->Validator) (AuthenticationData, Data, packet_type)));
-}
-
-static int
-XdmcpAddAuthorization (ARRAY8Ptr name, ARRAY8Ptr data)
-{
- AddAuthorFunc AddAuth;
-
- if (AuthenticationFuncs && AuthenticationFuncs->AddAuth)
- AddAuth = AuthenticationFuncs->AddAuth;
- else
- AddAuth = AddAuthorization;
- return (*AddAuth) ((unsigned short)name->length,
- (char *)name->data,
- (unsigned short)data->length,
- (char *)data->data);
-}
-
-/*
- * from here to the end of this file are routines private
- * to the state machine.
- */
-
-static void
-get_xdmcp_sock(void)
-{
-#ifdef STREAMSCONN
- struct netconfig *nconf;
-
- if ((xdmcpSocket = t_open("/dev/udp", O_RDWR, 0)) < 0) {
- XdmcpWarning("t_open() of /dev/udp failed");
- return;
- }
-
- if( t_bind(xdmcpSocket,NULL,NULL) < 0 ) {
- XdmcpWarning("UDP socket creation failed");
- t_error("t_bind(xdmcpSocket) failed" );
- t_close(xdmcpSocket);
- return;
- }
-
- /*
- * This part of the code looks contrived. It will actually fit in nicely
- * when the CLTS part of Xtrans is implemented.
- */
-
- if( (nconf=getnetconfigent("udp")) == NULL ) {
- XdmcpWarning("UDP socket creation failed: getnetconfigent()");
- t_unbind(xdmcpSocket);
- t_close(xdmcpSocket);
- return;
- }
-
- if( netdir_options(nconf, ND_SET_BROADCAST, xdmcpSocket, NULL) ) {
- XdmcpWarning("UDP set broadcast option failed: netdir_options()");
- freenetconfigent(nconf);
- t_unbind(xdmcpSocket);
- t_close(xdmcpSocket);
- return;
- }
-
- freenetconfigent(nconf);
-#else
- int soopts = 1;
-
-#if defined(IPv6) && defined(AF_INET6)
- if ((xdmcpSocket6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
- XdmcpWarning("INET6 UDP socket creation failed");
-#endif
- if ((xdmcpSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
- XdmcpWarning("UDP socket creation failed");
-#ifdef SO_BROADCAST
- else if (setsockopt(xdmcpSocket, SOL_SOCKET, SO_BROADCAST, (char *)&soopts,
- sizeof(soopts)) < 0)
- XdmcpWarning("UDP set broadcast socket-option failed");
-#endif /* SO_BROADCAST */
- if (xdm_from)
- {
- if (xdmcpSocket >= 0 && SOCKADDR_FAMILY(FromAddress)==AF_INET) {
- if (bind(xdmcpSocket, (struct sockaddr *)&FromAddress,
- FromAddressLen) < 0) {
- FatalError("Xserver: failed to bind to -from address: %s error %d\n", xdm_from, WSAGetLastError());
- }
- }
- else if (xdmcpSocket6 >= 0 && SOCKADDR_FAMILY(FromAddress)==AF_INET6) {
- if (bind(xdmcpSocket6, (struct sockaddr *)&FromAddress,
- FromAddressLen) < 0) {
- FatalError("Xserver: failed to bind to -from address: %s error %d\n", xdm_from, WSAGetLastError());
- }
- }
- }
-#endif /* STREAMSCONN */
-}
-
-static void
-send_query_msg(void)
-{
- XdmcpHeader header;
- Bool broadcast = FALSE;
-#if defined(IPv6) && defined(AF_INET6)
- Bool multicast = FALSE;
-#endif
- int i;
- int socketfd = xdmcpSocket;
-
- header.version = XDM_PROTOCOL_VERSION;
- switch(state){
- case XDM_QUERY:
- header.opcode = (CARD16) QUERY;
- state = XDM_COLLECT_QUERY;
- break;
- case XDM_BROADCAST:
- header.opcode = (CARD16) BROADCAST_QUERY;
- state = XDM_COLLECT_BROADCAST_QUERY;
- broadcast = TRUE;
- break;
-#if defined(IPv6) && defined(AF_INET6)
- case XDM_MULTICAST:
- header.opcode = (CARD16) BROADCAST_QUERY;
- state = XDM_COLLECT_MULTICAST_QUERY;
- multicast = TRUE;
- break;
-#endif
- case XDM_INDIRECT:
- header.opcode = (CARD16) INDIRECT_QUERY;
- state = XDM_COLLECT_INDIRECT_QUERY;
- break;
- default:
- break;
- }
- header.length = 1;
- for (i = 0; i < AuthenticationNames.length; i++)
- header.length += 2 + AuthenticationNames.data[i].length;
-
- XdmcpWriteHeader (&buffer, &header);
- XdmcpWriteARRAYofARRAY8 (&buffer, &AuthenticationNames);
- if (broadcast)
- {
- int i;
-
- for (i = 0; i < NumBroadcastAddresses; i++)
- XdmcpFlush (xdmcpSocket, &buffer, (XdmcpNetaddr) &BroadcastAddresses[i],
- sizeof (struct sockaddr_in));
- }
-#if defined(IPv6) && defined(AF_INET6)
- else if (multicast)
- {
- struct multicastinfo *mcl;
- struct addrinfo *ai;
-
- for (mcl = mcastlist; mcl != NULL; mcl = mcl->next) {
- for (ai = mcl->ai ; ai != NULL; ai = ai->ai_next) {
- if (ai->ai_family == AF_INET) {
- int hopflag = mcl->hops;
- socketfd = xdmcpSocket;
- setsockopt(socketfd, IPPROTO_IP, IP_MULTICAST_TTL,
- (char*)&hopflag, sizeof(hopflag));
- } else if (ai->ai_family == AF_INET6) {
- int hopflag6 = mcl->hops;
- socketfd = xdmcpSocket6;
- setsockopt(socketfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
- (char*)&hopflag6, sizeof(hopflag6));
- } else {
- continue;
- }
- XdmcpFlush (socketfd, &buffer,
- (XdmcpNetaddr) ai->ai_addr, ai->ai_addrlen);
- break;
- }
- }
- }
-#endif
- else
- {
-#if defined(IPv6) && defined(AF_INET6)
- if (SOCKADDR_FAMILY(ManagerAddress) == AF_INET6)
- socketfd = xdmcpSocket6;
-#endif
- XdmcpFlush (socketfd, &buffer, (XdmcpNetaddr) &ManagerAddress,
- ManagerAddressLen);
- }
-}
-
-static void
-recv_willing_msg(
- struct sockaddr *from,
- int fromlen,
- unsigned length)
-{
- ARRAY8 authenticationName;
- ARRAY8 hostname;
- ARRAY8 status;
-
- authenticationName.data = 0;
- hostname.data = 0;
- status.data = 0;
- if (XdmcpReadARRAY8 (&buffer, &authenticationName) &&
- XdmcpReadARRAY8 (&buffer, &hostname) &&
- XdmcpReadARRAY8 (&buffer, &status))
- {
- if (length == 6 + authenticationName.length +
- hostname.length + status.length)
- {
- switch (state)
- {
- case XDM_COLLECT_QUERY:
- XdmcpSelectHost(from, fromlen, &authenticationName);
- break;
- case XDM_COLLECT_BROADCAST_QUERY:
-#if defined(IPv6) && defined(AF_INET6)
- case XDM_COLLECT_MULTICAST_QUERY:
-#endif
- case XDM_COLLECT_INDIRECT_QUERY:
- XdmcpAddHost(from, fromlen, &authenticationName, &hostname, &status);
- break;
- default:
- break;
- }
- }
- }
- XdmcpDisposeARRAY8 (&authenticationName);
- XdmcpDisposeARRAY8 (&hostname);
- XdmcpDisposeARRAY8 (&status);
-}
-
-static void
-send_request_msg(void)
-{
- XdmcpHeader header;
- int length;
- int i;
- CARD16 XdmcpConnectionType;
- ARRAY8 authenticationData;
- int socketfd = xdmcpSocket;
-
- switch (SOCKADDR_FAMILY(ManagerAddress))
- {
- case AF_INET: XdmcpConnectionType=FamilyInternet; break;
-#if defined(IPv6) && defined(AF_INET6)
- case AF_INET6: XdmcpConnectionType=FamilyInternet6; break;
-#endif
- default: XdmcpConnectionType=0xffff; break;
- }
-
- header.version = XDM_PROTOCOL_VERSION;
- header.opcode = (CARD16) REQUEST;
-
- length = 2; /* display number */
- length += 1 + 2 * ConnectionTypes.length; /* connection types */
- length += 1; /* connection addresses */
- for (i = 0; i < ConnectionAddresses.length; i++)
- length += 2 + ConnectionAddresses.data[i].length;
- authenticationData.length = 0;
- authenticationData.data = 0;
- if (AuthenticationFuncs)
- {
- (*AuthenticationFuncs->Generator) (AuthenticationData,
- &authenticationData,
- REQUEST);
- }
- length += 2 + AuthenticationName->length; /* authentication name */
- length += 2 + authenticationData.length; /* authentication data */
- length += 1; /* authorization names */
- for (i = 0; i < AuthorizationNames.length; i++)
- length += 2 + AuthorizationNames.data[i].length;
- length += 2 + ManufacturerDisplayID.length; /* display ID */
- header.length = length;
-
- if (!XdmcpWriteHeader (&buffer, &header))
- {
- XdmcpDisposeARRAY8 (&authenticationData);
- return;
- }
- XdmcpWriteCARD16 (&buffer, DisplayNumber);
- XdmcpWriteCARD8 (&buffer, ConnectionTypes.length);
-
- /* The connection array is send reordered, so that connections of */
- /* the same address type as the XDMCP manager connection are send */
- /* first. This works around a bug in xdm. mario@klebsch.de */
- for (i = 0; i < (int)ConnectionTypes.length; i++)
- if (ConnectionTypes.data[i]==XdmcpConnectionType)
- XdmcpWriteCARD16 (&buffer, ConnectionTypes.data[i]);
- for (i = 0; i < (int)ConnectionTypes.length; i++)
- if (ConnectionTypes.data[i]!=XdmcpConnectionType)
- XdmcpWriteCARD16 (&buffer, ConnectionTypes.data[i]);
-
- XdmcpWriteCARD8 (&buffer, ConnectionAddresses.length);
- for (i = 0; i < (int)ConnectionAddresses.length; i++)
- if ( (i<ConnectionTypes.length) &&
- (ConnectionTypes.data[i]==XdmcpConnectionType) )
- XdmcpWriteARRAY8 (&buffer, &ConnectionAddresses.data[i]);
- for (i = 0; i < (int)ConnectionAddresses.length; i++)
- if ( (i>=ConnectionTypes.length) ||
- (ConnectionTypes.data[i]!=XdmcpConnectionType) )
- XdmcpWriteARRAY8 (&buffer, &ConnectionAddresses.data[i]);
-
- XdmcpWriteARRAY8 (&buffer, AuthenticationName);
- XdmcpWriteARRAY8 (&buffer, &authenticationData);
- XdmcpDisposeARRAY8 (&authenticationData);
- XdmcpWriteARRAYofARRAY8 (&buffer, &AuthorizationNames);
- XdmcpWriteARRAY8 (&buffer, &ManufacturerDisplayID);
-#if defined(IPv6) && defined(AF_INET6)
- if (SOCKADDR_FAMILY(req_sockaddr) == AF_INET6)
- socketfd = xdmcpSocket6;
-#endif
- if (XdmcpFlush (socketfd, &buffer,
- (XdmcpNetaddr) &req_sockaddr, req_socklen))
- state = XDM_AWAIT_REQUEST_RESPONSE;
-}
-
-static void
-recv_accept_msg(unsigned length)
-{
- CARD32 AcceptSessionID;
- ARRAY8 AcceptAuthenticationName, AcceptAuthenticationData;
- ARRAY8 AcceptAuthorizationName, AcceptAuthorizationData;
-
- if (state != XDM_AWAIT_REQUEST_RESPONSE)
- return;
- AcceptAuthenticationName.data = 0;
- AcceptAuthenticationData.data = 0;
- AcceptAuthorizationName.data = 0;
- AcceptAuthorizationData.data = 0;
- if (XdmcpReadCARD32 (&buffer, &AcceptSessionID) &&
- XdmcpReadARRAY8 (&buffer, &AcceptAuthenticationName) &&
- XdmcpReadARRAY8 (&buffer, &AcceptAuthenticationData) &&
- XdmcpReadARRAY8 (&buffer, &AcceptAuthorizationName) &&
- XdmcpReadARRAY8 (&buffer, &AcceptAuthorizationData))
- {
- if (length == 12 + AcceptAuthenticationName.length +
- AcceptAuthenticationData.length +
- AcceptAuthorizationName.length +
- AcceptAuthorizationData.length)
- {
- if (!XdmcpCheckAuthentication (&AcceptAuthenticationName,
- &AcceptAuthenticationData, ACCEPT))
- {
- XdmcpFatal ("Authentication Failure", &AcceptAuthenticationName);
- }
- /* permit access control manipulations from this host */
- AugmentSelf (&req_sockaddr, req_socklen);
- /* if the authorization specified in the packet fails
- * to be acceptable, enable the local addresses
- */
- if (!XdmcpAddAuthorization (&AcceptAuthorizationName,
- &AcceptAuthorizationData))
- {
- AddLocalHosts ();
- }
- SessionID = AcceptSessionID;
- state = XDM_MANAGE;
- send_packet();
- }
- }
- XdmcpDisposeARRAY8 (&AcceptAuthenticationName);
- XdmcpDisposeARRAY8 (&AcceptAuthenticationData);
- XdmcpDisposeARRAY8 (&AcceptAuthorizationName);
- XdmcpDisposeARRAY8 (&AcceptAuthorizationData);
-}
-
-static void
-recv_decline_msg(unsigned length)
-{
- ARRAY8 status, DeclineAuthenticationName, DeclineAuthenticationData;
-
- status.data = 0;
- DeclineAuthenticationName.data = 0;
- DeclineAuthenticationData.data = 0;
- if (XdmcpReadARRAY8 (&buffer, &status) &&
- XdmcpReadARRAY8 (&buffer, &DeclineAuthenticationName) &&
- XdmcpReadARRAY8 (&buffer, &DeclineAuthenticationData))
- {
- if (length == 6 + status.length +
- DeclineAuthenticationName.length +
- DeclineAuthenticationData.length &&
- XdmcpCheckAuthentication (&DeclineAuthenticationName,
- &DeclineAuthenticationData, DECLINE))
- {
- XdmcpFatal ("Session declined", &status);
- }
- }
- XdmcpDisposeARRAY8 (&status);
- XdmcpDisposeARRAY8 (&DeclineAuthenticationName);
- XdmcpDisposeARRAY8 (&DeclineAuthenticationData);
-}
-
-static void
-send_manage_msg(void)
-{
- XdmcpHeader header;
- int socketfd = xdmcpSocket;
-
- header.version = XDM_PROTOCOL_VERSION;
- header.opcode = (CARD16) MANAGE;
- header.length = 8 + DisplayClass.length;
-
- if (!XdmcpWriteHeader (&buffer, &header))
- return;
- XdmcpWriteCARD32 (&buffer, SessionID);
- XdmcpWriteCARD16 (&buffer, DisplayNumber);
- XdmcpWriteARRAY8 (&buffer, &DisplayClass);
- state = XDM_AWAIT_MANAGE_RESPONSE;
-#if defined(IPv6) && defined(AF_INET6)
- if (SOCKADDR_FAMILY(req_sockaddr) == AF_INET6)
- socketfd = xdmcpSocket6;
-#endif
- XdmcpFlush (socketfd, &buffer, (XdmcpNetaddr) &req_sockaddr, req_socklen);
-}
-
-static void
-recv_refuse_msg(unsigned length)
-{
- CARD32 RefusedSessionID;
-
- if (state != XDM_AWAIT_MANAGE_RESPONSE)
- return;
- if (length != 4)
- return;
- if (XdmcpReadCARD32 (&buffer, &RefusedSessionID))
- {
- if (RefusedSessionID == SessionID)
- {
- state = XDM_START_CONNECTION;
- send_packet();
- }
- }
-}
-
-static void
-recv_failed_msg(unsigned length)
-{
- CARD32 FailedSessionID;
- ARRAY8 status;
-
- if (state != XDM_AWAIT_MANAGE_RESPONSE)
- return;
- status.data = 0;
- if (XdmcpReadCARD32 (&buffer, &FailedSessionID) &&
- XdmcpReadARRAY8 (&buffer, &status))
- {
- if (length == 6 + status.length &&
- SessionID == FailedSessionID)
- {
- XdmcpFatal ("Session failed", &status);
- }
- }
- XdmcpDisposeARRAY8 (&status);
-}
-
-static void
-send_keepalive_msg(void)
-{
- XdmcpHeader header;
- int socketfd = xdmcpSocket;
-
- header.version = XDM_PROTOCOL_VERSION;
- header.opcode = (CARD16) KEEPALIVE;
- header.length = 6;
-
- XdmcpWriteHeader (&buffer, &header);
- XdmcpWriteCARD16 (&buffer, DisplayNumber);
- XdmcpWriteCARD32 (&buffer, SessionID);
-
- state = XDM_AWAIT_ALIVE_RESPONSE;
-#if defined(IPv6) && defined(AF_INET6)
- if (SOCKADDR_FAMILY(req_sockaddr) == AF_INET6)
- socketfd = xdmcpSocket6;
-#endif
- XdmcpFlush (socketfd, &buffer, (XdmcpNetaddr) &req_sockaddr, req_socklen);
-}
-
-static void
-recv_alive_msg (unsigned length)
-{
- CARD8 SessionRunning;
- CARD32 AliveSessionID;
-
- if (state != XDM_AWAIT_ALIVE_RESPONSE)
- return;
- if (length != 5)
- return;
- if (XdmcpReadCARD8 (&buffer, &SessionRunning) &&
- XdmcpReadCARD32 (&buffer, &AliveSessionID))
- {
- if (SessionRunning && AliveSessionID == SessionID)
- {
- /* backoff dormancy period */
- state = XDM_RUN_SESSION;
- if ((GetTimeInMillis() - lastDeviceEventTime.milliseconds) >
- keepaliveDormancy * 1000)
- {
- keepaliveDormancy <<= 1;
- if (keepaliveDormancy > XDM_MAX_DORMANCY)
- keepaliveDormancy = XDM_MAX_DORMANCY;
- }
- timeOutTime = GetTimeInMillis() + keepaliveDormancy * 1000;
- }
- else
- {
- XdmcpDeadSession ("Alive response indicates session dead");
- }
- }
-}
-
-static void
-XdmcpFatal (
- const char *type,
- ARRAY8Ptr status)
-{
- FatalError ("XDMCP fatal error: %s %*.*s\n", type,
- status->length, status->length, status->data);
-}
-
-static void
-XdmcpWarning(const char *str)
-{
- ErrorF("XDMCP warning: %s\n", str);
-}
-
-static void
-get_addr_by_name(
- const char *argtype,
- const char *namestr,
- int port,
- int socktype,
- SOCKADDR_TYPE *addr,
- SOCKLEN_TYPE *addrlen
-#if defined(IPv6) && defined(AF_INET6)
- ,
- struct addrinfo **aip,
- struct addrinfo **aifirstp
-#endif
- )
-{
-#if defined(IPv6) && defined(AF_INET6)
- struct addrinfo *ai;
- struct addrinfo hints;
- char portstr[6];
- char *pport = portstr;
- int gaierr;
-
-#if defined(WIN32) && defined(TCPCONN)
- _XSERVTransWSAStartup();
-#endif
-
- memset(&hints, 0, sizeof(hints));
- hints.ai_socktype = socktype;
-
- if (port == 0) {
- pport = NULL;
- } else if (port > 0 && port < 65535) {
- sprintf(portstr, "%d", port);
- } else {
- FatalError("Xserver: port out of range: %d\n", port);
- }
-
- if (*aifirstp != NULL) {
- freeaddrinfo(*aifirstp);
- *aifirstp = NULL;
- }
-
- if ((gaierr = getaddrinfo(namestr, pport, &hints, aifirstp)) == 0) {
- for (ai = *aifirstp; ai != NULL; ai = ai->ai_next) {
- if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6)
- break;
- }
- if ((ai == NULL) || (ai->ai_addrlen > sizeof(SOCKADDR_TYPE))) {
- FatalError ("Xserver: %s host %s not on supported network type\n",
- argtype, namestr);
- } else {
- *aip = ai;
- *addrlen = ai->ai_addrlen;
- memcpy(addr, ai->ai_addr, ai->ai_addrlen);
- }
- } else {
- FatalError("Xserver: %s: %s %s\n", gai_strerror(gaierr), argtype, namestr);
- }
-#else
- struct hostent *hep;
-#ifdef XTHREADS_NEEDS_BYNAMEPARAMS
- _Xgethostbynameparams hparams;
-#endif
- if (!(hep = _XGethostbyname(namestr, hparams)))
- {
- FatalError("Xserver: %s unknown host: %s\n", argtype, namestr);
- }
- if (hep->h_length == sizeof (struct in_addr))
- {
- memmove(&addr->sin_addr, hep->h_addr, hep->h_length);
- *addrlen = sizeof(struct sockaddr_in);
- addr->sin_family = AF_INET;
- addr->sin_port = htons (port);
- }
- else
- {
- FatalError("Xserver: %s host on strange network %s\n", argtype, namestr);
- }
-#endif
-}
-
-static void
-get_manager_by_name(
- int argc,
- char **argv,
- int i)
-{
-
- PSOCKADDR_IN queryAddr = NULL;
- u_long u_lqueryAddr = 0;
-
- if ((i + 1) == argc)
- {
- FatalError("Xserver: missing %s host name in command line\n", argv[i]);
- }
-
- get_addr_by_name(argv[i], argv[i+1], xdm_udp_port, SOCK_DGRAM,
- &ManagerAddress, &ManagerAddressLen
-#if defined(IPv6) && defined(AF_INET6)
- , &mgrAddr, &mgrAddrFirst
-#endif
- );
- queryAddr = (PSOCKADDR_IN)&ManagerAddress;
- u_lqueryAddr = queryAddr->sin_addr.S_un.S_addr;
- match_interface(u_lqueryAddr);
-}
-
-
-static void
-get_fromaddr_by_name(
- int argc,
- char **argv,
- int i)
-{
-#if defined(IPv6) && defined(AF_INET6)
- struct addrinfo *ai = NULL;
- struct addrinfo *aifirst = NULL;
-#endif
- if (i == argc)
- {
- FatalError("Xserver: missing -from host name in command line\n");
- }
- get_addr_by_name("-from", argv[i], 0, 0, &FromAddress, &FromAddressLen
-#if defined(IPv6) && defined(AF_INET6)
- , &ai, &aifirst
-#endif
- );
-#if defined(IPv6) && defined(AF_INET6)
- if (aifirst != NULL)
- freeaddrinfo(aifirst);
-#endif
- xdm_from = argv[i];
-}
-
-
-#if defined(IPv6) && defined(AF_INET6)
-static int
-get_mcast_options(int argc, char **argv, int i)
-{
- char *address = XDM_DEFAULT_MCAST_ADDR6;
- int hopcount = 1;
- struct addrinfo hints;
- char portstr[6];
- int gaierr;
- struct addrinfo *ai, *firstai;
-
- if ((i < argc) && (argv[i][0] != '-') && (argv[i][0] != '+')) {
- address = argv[i++];
- if ((i < argc) && (argv[i][0] != '-') && (argv[i][0] != '+')) {
- hopcount = strtol(argv[i++], NULL, 10);
- if ((hopcount < 1) || (hopcount > 255)) {
- FatalError("Xserver: multicast hop count out of range: %d\n",
- hopcount);
- }
- }
- }
-
- if (xdm_udp_port > 0 && xdm_udp_port < 65535) {
- sprintf(portstr, "%d", xdm_udp_port);
- } else {
- FatalError("Xserver: port out of range: %d\n", xdm_udp_port);
- }
- memset(&hints, 0, sizeof(hints));
- hints.ai_socktype = SOCK_DGRAM;
-
- if ((gaierr = getaddrinfo(address, portstr, &hints, &firstai)) == 0) {
- for (ai = firstai; ai != NULL; ai = ai->ai_next) {
- if (((ai->ai_family == AF_INET) &&
- IN_MULTICAST(((struct sockaddr_in *) ai->ai_addr)
- ->sin_addr.s_addr))
- || ((ai->ai_family == AF_INET6) &&
- IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *) ai->ai_addr)
- ->sin6_addr)))
- break;
- }
- if (ai == NULL) {
- FatalError ("Xserver: address not supported multicast type %s\n",
- address);
- } else {
- struct multicastinfo *mcastinfo, *mcl;
-
- mcastinfo = malloc(sizeof(struct multicastinfo));
- mcastinfo->next = NULL;
- mcastinfo->ai = firstai;
- mcastinfo->hops = hopcount;
-
- if (mcastlist == NULL) {
- mcastlist = mcastinfo;
- } else {
- for (mcl = mcastlist; mcl->next != NULL; mcl = mcl->next) {
- /* Do nothing - just find end of list */
- }
- mcl->next = mcastinfo;
- }
- }
- } else {
- FatalError("Xserver: %s: %s\n", gai_strerror(gaierr), address);
- }
- return i;
-}
-#endif
-
-#else
-static int xdmcp_non_empty; /* avoid complaint by ranlib */
-#endif /* XDMCP */
+/*
+ * Copyright 1989 Network Computing Devices, Inc., Mountain View, California.
+ *
+ * Permission to use, copy, modify, and distribute this software and its
+ * documentation for any purpose and without fee is hereby granted, provided
+ * that the above copyright notice appear in all copies and that both that
+ * copyright notice and this permission notice appear in supporting
+ * documentation, and that the name of N.C.D. not be used in advertising or
+ * publicity pertaining to distribution of the software without specific,
+ * written prior permission. N.C.D. makes no representations about the
+ * suitability of this software for any purpose. It is provided "as is"
+ * without express or implied warranty.
+ *
+ */
+
+#ifdef HAVE_DIX_CONFIG_H
+#include <dix-config.h>
+#endif
+
+#ifdef WIN32
+#include <X11/Xwinsock.h>
+#endif
+
+#include <X11/Xos.h>
+
+#if !defined(WIN32)
+#include <sys/param.h>
+#include <sys/socket.h>
+#include <netinet/in.h>
+#include <netdb.h>
+#endif
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <X11/X.h>
+#include <X11/Xmd.h>
+#include "misc.h"
+#include <X11/Xpoll.h>
+#include "osdep.h"
+#include "input.h"
+#include "dixstruct.h"
+#include "opaque.h"
+#include "site.h"
+
+#ifdef STREAMSCONN
+#include <tiuser.h>
+#include <netconfig.h>
+#include <netdir.h>
+#endif
+
+#ifdef XDMCP
+#undef REQUEST
+
+#ifdef XDMCP_NO_IPV6
+#undef IPv6
+#endif
+
+#include <X11/Xdmcp.h>
+
+#define X_INCLUDE_NETDB_H
+#include <X11/Xos_r.h>
+
+static char *defaultDisplayClass = COMPILEDDISPLAYCLASS;
+
+extern void match_interface(u_long u_lQuery);
+
+static int xdmcpSocket, sessionSocket;
+static xdmcp_states state;
+#if defined(IPv6) && defined(AF_INET6)
+static int xdmcpSocket6;
+static struct sockaddr_storage req_sockaddr;
+#else
+static struct sockaddr_in req_sockaddr;
+#endif
+static int req_socklen;
+static CARD32 SessionID;
+static CARD32 timeOutTime;
+static int timeOutRtx;
+static CARD32 defaultKeepaliveDormancy = XDM_DEF_DORMANCY;
+static CARD32 keepaliveDormancy = XDM_DEF_DORMANCY;
+static CARD16 DisplayNumber;
+static xdmcp_states XDM_INIT_STATE = XDM_OFF;
+#ifdef HASXDMAUTH
+static char *xdmAuthCookie;
+#endif
+
+static XdmcpBuffer buffer;
+
+#if defined(IPv6) && defined(AF_INET6)
+
+static struct addrinfo *mgrAddr;
+static struct addrinfo *mgrAddrFirst;
+
+#define SOCKADDR_TYPE struct sockaddr_storage
+#define SOCKADDR_FAMILY(s) ((struct sockaddr *)&(s))->sa_family
+
+#ifdef BSD44SOCKETS
+#define SOCKLEN_FIELD(s) ((struct sockaddr *)&(s))->sa_len
+#define SOCKLEN_TYPE unsigned char
+#else
+#define SOCKLEN_TYPE unsigned int
+#endif
+
+#else
+
+#define SOCKADDR_TYPE struct sockaddr_in
+#define SOCKADDR_FAMILY(s) (s).sin_family
+
+#ifdef BSD44SOCKETS
+#define SOCKLEN_FIELD(s) (s).sin_len
+#define SOCKLEN_TYPE unsigned char
+#else
+#define SOCKLEN_TYPE size_t
+#endif
+
+#endif
+
+static SOCKADDR_TYPE ManagerAddress;
+static SOCKADDR_TYPE FromAddress;
+
+#ifdef SOCKLEN_FIELD
+#define ManagerAddressLen SOCKLEN_FIELD(ManagerAddress)
+#define FromAddressLen SOCKLEN_FIELD(FromAddress)
+#else
+static SOCKLEN_TYPE ManagerAddressLen, FromAddressLen;
+#endif
+
+#if defined(IPv6) && defined(AF_INET6)
+static struct multicastinfo {
+ struct multicastinfo *next;
+ struct addrinfo *ai;
+ int hops;
+} *mcastlist;
+#endif
+
+static void XdmcpAddHost(
+ const struct sockaddr *from,
+ int fromlen,
+ ARRAY8Ptr AuthenticationName,
+ ARRAY8Ptr hostname,
+ ARRAY8Ptr status);
+
+static void XdmcpSelectHost(
+ const struct sockaddr *host_sockaddr,
+ int host_len,
+ ARRAY8Ptr AuthenticationName);
+
+static void get_xdmcp_sock(void);
+
+static void send_query_msg(void);
+
+static void recv_willing_msg(
+ struct sockaddr * /*from*/,
+ int /*fromlen*/,
+ unsigned /*length*/);
+
+static void send_request_msg(void);
+
+static void recv_accept_msg(unsigned /*length*/);
+
+static void recv_decline_msg(unsigned /*length*/);
+
+static void send_manage_msg(void);
+
+static void recv_refuse_msg(unsigned /*length*/);
+
+static void recv_failed_msg(unsigned /*length*/);
+
+static void send_keepalive_msg(void);
+
+static void recv_alive_msg(unsigned /*length*/);
+
+static void XdmcpFatal(
+ const char * /*type*/,
+ ARRAY8Ptr /*status*/);
+
+static void XdmcpWarning(const char * /*str*/);
+
+static void get_manager_by_name(
+ int /*argc*/,
+ char ** /*argv*/,
+ int /*i*/);
+
+static void get_fromaddr_by_name(int /*argc*/, char ** /*argv*/, int /*i*/);
+
+#if defined(IPv6) && defined(AF_INET6)
+static int get_mcast_options(int /*argc*/, char ** /*argv*/, int /*i*/);
+#endif
+
+static void receive_packet(int /*socketfd*/);
+
+static void send_packet(void);
+
+static void timeout(void);
+
+static void restart(void);
+
+static void XdmcpBlockHandler(
+ pointer /*data*/,
+ struct timeval ** /*wt*/,
+ pointer /*LastSelectMask*/);
+
+static void XdmcpWakeupHandler(
+ pointer /*data*/,
+ int /*i*/,
+ pointer /*LastSelectMask*/);
+
+#define XSERV_t
+#define TRANS_SERVER
+#define TRANS_REOPEN
+#include <X11/Xtrans/Xtrans.h>
+
+/*
+ * Register the Manufacturer display ID
+ */
+
+static ARRAY8 ManufacturerDisplayID;
+
+static void
+XdmcpRegisterManufacturerDisplayID (const char *name, int length)
+{
+ int i;
+
+ XdmcpDisposeARRAY8 (&ManufacturerDisplayID);
+ if (!XdmcpAllocARRAY8 (&ManufacturerDisplayID, length))
+ return;
+ for (i = 0; i < length; i++)
+ ManufacturerDisplayID.data[i] = (CARD8) name[i];
+}
+
+static unsigned short xdm_udp_port = XDM_UDP_PORT;
+static Bool OneSession = FALSE;
+static const char *xdm_from = NULL;
+
+void
+XdmcpUseMsg (void)
+{
+ ErrorF("-query host-name contact named host for XDMCP\n");
+ ErrorF("-broadcast broadcast for XDMCP\n");
+#if defined(IPv6) && defined(AF_INET6)
+ ErrorF("-multicast [addr [hops]] IPv6 multicast for XDMCP\n");
+#endif
+ ErrorF("-indirect host-name contact named host for indirect XDMCP\n");
+ ErrorF("-port port-num UDP port number to send messages to\n");
+ ErrorF("-from local-address specify the local address to connect from\n");
+ ErrorF("-once Terminate server after one session\n");
+ ErrorF("-class display-class specify display class to send in manage\n");
+#ifdef HASXDMAUTH
+ ErrorF("-cookie xdm-auth-bits specify the magic cookie for XDMCP\n");
+#endif
+ ErrorF("-displayID display-id manufacturer display ID for request\n");
+}
+
+int
+XdmcpOptions(int argc, char **argv, int i)
+{
+ if (strcmp(argv[i], "-query") == 0) {
+ get_manager_by_name(argc, argv, i++);
+ XDM_INIT_STATE = XDM_QUERY;
+ AccessUsingXdmcp ();
+ return i + 1;
+ }
+ if (strcmp(argv[i], "-broadcast") == 0) {
+ XDM_INIT_STATE = XDM_BROADCAST;
+ AccessUsingXdmcp ();
+ return i + 1;
+ }
+#if defined(IPv6) && defined(AF_INET6)
+ if (strcmp(argv[i], "-multicast") == 0) {
+ i = get_mcast_options(argc, argv, ++i);
+ XDM_INIT_STATE = XDM_MULTICAST;
+ AccessUsingXdmcp ();
+ return i + 1;
+ }
+#endif
+ if (strcmp(argv[i], "-indirect") == 0) {
+ get_manager_by_name(argc, argv, i++);
+ XDM_INIT_STATE = XDM_INDIRECT;
+ AccessUsingXdmcp ();
+ return i + 1;
+ }
+ if (strcmp(argv[i], "-port") == 0) {
+ if (++i == argc) {
+ FatalError("Xserver: missing port number in command line\n");
+ }
+ xdm_udp_port = (unsigned short) atoi(argv[i]);
+ return i + 1;
+ }
+ if (strcmp(argv[i], "-from") == 0) {
+ get_fromaddr_by_name(argc, argv, ++i);
+ return i + 1;
+ }
+ if (strcmp(argv[i], "-once") == 0) {
+ OneSession = TRUE;
+ return i + 1;
+ }
+ if (strcmp(argv[i], "-class") == 0) {
+ if (++i == argc) {
+ FatalError("Xserver: missing class name in command line\n");
+ }
+ defaultDisplayClass = argv[i];
+ return i + 1;
+ }
+#ifdef HASXDMAUTH
+ if (strcmp(argv[i], "-cookie") == 0) {
+ if (++i == argc) {
+ FatalError("Xserver: missing cookie data in command line\n");
+ }
+ xdmAuthCookie = argv[i];
+ return i + 1;
+ }
+#endif
+ if (strcmp(argv[i], "-displayID") == 0) {
+ if (++i == argc) {
+ FatalError("Xserver: missing displayID in command line\n");
+ }
+ XdmcpRegisterManufacturerDisplayID (argv[i], strlen (argv[i]));
+ return i + 1;
+ }
+ return i;
+}
+
+/*
+ * This section is a collection of routines for
+ * registering server-specific data with the XDMCP
+ * state machine.
+ */
+
+
+/*
+ * Save all broadcast addresses away so BroadcastQuery
+ * packets get sent everywhere
+ */
+
+#define MAX_BROADCAST 10
+
+/* This stays sockaddr_in since IPv6 doesn't support broadcast */
+static struct sockaddr_in BroadcastAddresses[MAX_BROADCAST];
+static int NumBroadcastAddresses;
+
+void
+XdmcpRegisterBroadcastAddress (const struct sockaddr_in *addr)
+{
+ struct sockaddr_in *bcast;
+ if (NumBroadcastAddresses >= MAX_BROADCAST)
+ return;
+ bcast = &BroadcastAddresses[NumBroadcastAddresses++];
+ memset(bcast, 0, sizeof (struct sockaddr_in));
+#ifdef BSD44SOCKETS
+ bcast->sin_len = addr->sin_len;
+#endif
+ bcast->sin_family = addr->sin_family;
+ bcast->sin_port = htons (xdm_udp_port);
+ bcast->sin_addr = addr->sin_addr;
+}
+
+/*
+ * Each authentication type is registered here; Validator
+ * will be called to check all access attempts using
+ * the specified authentication type
+ */
+
+static ARRAYofARRAY8 AuthenticationNames, AuthenticationDatas;
+typedef struct _AuthenticationFuncs {
+ ValidatorFunc Validator;
+ GeneratorFunc Generator;
+ AddAuthorFunc AddAuth;
+} AuthenticationFuncsRec, *AuthenticationFuncsPtr;
+
+static AuthenticationFuncsPtr AuthenticationFuncsList;
+
+void
+XdmcpRegisterAuthentication (
+ const char *name,
+ int namelen,
+ const char *data,
+ int datalen,
+ ValidatorFunc Validator,
+ GeneratorFunc Generator,
+ AddAuthorFunc AddAuth)
+{
+ int i;
+ ARRAY8 AuthenticationName, AuthenticationData;
+ static AuthenticationFuncsPtr newFuncs;
+
+ if (!XdmcpAllocARRAY8 (&AuthenticationName, namelen))
+ return;
+ if (!XdmcpAllocARRAY8 (&AuthenticationData, datalen))
+ {
+ XdmcpDisposeARRAY8 (&AuthenticationName);
+ return;
+ }
+ for (i = 0; i < namelen; i++)
+ AuthenticationName.data[i] = name[i];
+ for (i = 0; i < datalen; i++)
+ AuthenticationData.data[i] = data[i];
+ if (!(XdmcpReallocARRAYofARRAY8 (&AuthenticationNames,
+ AuthenticationNames.length + 1) &&
+ XdmcpReallocARRAYofARRAY8 (&AuthenticationDatas,
+ AuthenticationDatas.length + 1) &&
+ (newFuncs = malloc((AuthenticationNames.length + 1) * sizeof (AuthenticationFuncsRec)))))
+ {
+ XdmcpDisposeARRAY8 (&AuthenticationName);
+ XdmcpDisposeARRAY8 (&AuthenticationData);
+ return;
+ }
+ for (i = 0; i < AuthenticationNames.length - 1; i++)
+ newFuncs[i] = AuthenticationFuncsList[i];
+ newFuncs[AuthenticationNames.length-1].Validator = Validator;
+ newFuncs[AuthenticationNames.length-1].Generator = Generator;
+ newFuncs[AuthenticationNames.length-1].AddAuth = AddAuth;
+ free(AuthenticationFuncsList);
+ AuthenticationFuncsList = newFuncs;
+ AuthenticationNames.data[AuthenticationNames.length-1] = AuthenticationName;
+ AuthenticationDatas.data[AuthenticationDatas.length-1] = AuthenticationData;
+}
+
+/*
+ * Select the authentication type to be used; this is
+ * set by the manager of the host to be connected to.
+ */
+
+static ARRAY8 noAuthenticationName = {(CARD16) 0, (CARD8Ptr) 0};
+static ARRAY8 noAuthenticationData = {(CARD16) 0, (CARD8Ptr) 0};
+static ARRAY8Ptr AuthenticationName = &noAuthenticationName;
+static ARRAY8Ptr AuthenticationData = &noAuthenticationData;
+static AuthenticationFuncsPtr AuthenticationFuncs;
+
+static void
+XdmcpSetAuthentication (const ARRAY8Ptr name)
+{
+ int i;
+
+ for (i = 0; i < AuthenticationNames.length; i++)
+ if (XdmcpARRAY8Equal (&AuthenticationNames.data[i], name))
+ {
+ AuthenticationName = &AuthenticationNames.data[i];
+ AuthenticationData = &AuthenticationDatas.data[i];
+ AuthenticationFuncs = &AuthenticationFuncsList[i];
+ break;
+ }
+}
+
+/*
+ * Register the host address for the display
+ */
+
+static ARRAY16 ConnectionTypes;
+static ARRAYofARRAY8 ConnectionAddresses;
+static long xdmcpGeneration;
+
+void
+XdmcpRegisterConnection (
+ int type,
+ const char *address,
+ int addrlen)
+{
+ int i;
+ CARD8 *newAddress;
+
+ if (xdmcpGeneration != serverGeneration)
+ {
+ XdmcpDisposeARRAY16 (&ConnectionTypes);
+ XdmcpDisposeARRAYofARRAY8 (&ConnectionAddresses);
+ xdmcpGeneration = serverGeneration;
+ }
+ if (xdm_from != NULL) { /* Only register the requested address */
+ const void *regAddr = address;
+ const void *fromAddr = NULL;
+ int regAddrlen = addrlen;
+
+ if (addrlen == sizeof(struct in_addr)) {
+ if (SOCKADDR_FAMILY(FromAddress) == AF_INET) {
+ fromAddr = &((struct sockaddr_in *)&FromAddress)->sin_addr;
+ }
+#if defined(IPv6) && defined(AF_INET6)
+ else if ((SOCKADDR_FAMILY(FromAddress) == AF_INET6) &&
+ IN6_IS_ADDR_V4MAPPED(
+ &((struct sockaddr_in6 *)&FromAddress)->sin6_addr)) {
+ fromAddr = &((struct sockaddr_in6 *)&FromAddress)->sin6_addr.s6_addr[12];
+ }
+#endif
+ }
+#if defined(IPv6) && defined(AF_INET6)
+ else if (addrlen == sizeof(struct in6_addr)) {
+ if (SOCKADDR_FAMILY(FromAddress) == AF_INET6) {
+ fromAddr = &((struct sockaddr_in6 *)&FromAddress)->sin6_addr;
+ } else if ((SOCKADDR_FAMILY(FromAddress) == AF_INET) &&
+ IN6_IS_ADDR_V4MAPPED((struct in6_addr *) address)) {
+ fromAddr = &((struct sockaddr_in *)&FromAddress)->sin_addr;
+ regAddr = &((struct sockaddr_in6 *)&address)->sin6_addr.s6_addr[12];
+ regAddrlen = sizeof(struct in_addr);
+ }
+ }
+#endif
+ if (!fromAddr || memcmp(regAddr, fromAddr, regAddrlen) != 0) {
+ return;
+ }
+ }
+ if (ConnectionAddresses.length + 1 == 256)
+ return;
+ newAddress = malloc(addrlen * sizeof (CARD8));
+ if (!newAddress)
+ return;
+ if (!XdmcpReallocARRAY16 (&ConnectionTypes, ConnectionTypes.length + 1))
+ {
+ free(newAddress);
+ return;
+ }
+ if (!XdmcpReallocARRAYofARRAY8 (&ConnectionAddresses,
+ ConnectionAddresses.length + 1))
+ {
+ free(newAddress);
+ return;
+ }
+ ConnectionTypes.data[ConnectionTypes.length - 1] = (CARD16) type;
+ for (i = 0; i < addrlen; i++)
+ newAddress[i] = address[i];
+ ConnectionAddresses.data[ConnectionAddresses.length-1].data = newAddress;
+ ConnectionAddresses.data[ConnectionAddresses.length-1].length = addrlen;
+}
+
+/*
+ * Register an Authorization Name. XDMCP advertises this list
+ * to the manager.
+ */
+
+static ARRAYofARRAY8 AuthorizationNames;
+
+void
+XdmcpRegisterAuthorizations (void)
+{
+ XdmcpDisposeARRAYofARRAY8 (&AuthorizationNames);
+ RegisterAuthorizations ();
+}
+
+void
+XdmcpRegisterAuthorization (const char *name, int namelen)
+{
+ ARRAY8 authName;
+ int i;
+
+ authName.data = malloc(namelen * sizeof (CARD8));
+ if (!authName.data)
+ return;
+ if (!XdmcpReallocARRAYofARRAY8 (&AuthorizationNames, AuthorizationNames.length +1))
+ {
+ free(authName.data);
+ return;
+ }
+ for (i = 0; i < namelen; i++)
+ authName.data[i] = (CARD8) name[i];
+ authName.length = namelen;
+ AuthorizationNames.data[AuthorizationNames.length-1] = authName;
+}
+
+/*
+ * Register the DisplayClass string
+ */
+
+static ARRAY8 DisplayClass;
+
+static void
+XdmcpRegisterDisplayClass (const char *name, int length)
+{
+ int i;
+
+ XdmcpDisposeARRAY8 (&DisplayClass);
+ if (!XdmcpAllocARRAY8 (&DisplayClass, length))
+ return;
+ for (i = 0; i < length; i++)
+ DisplayClass.data[i] = (CARD8) name[i];
+}
+
+/*
+ * initialize XDMCP; create the socket, compute the display
+ * number, set up the state machine
+ */
+
+void
+XdmcpInit(void)
+{
+ state = XDM_INIT_STATE;
+#ifdef HASXDMAUTH
+ if (xdmAuthCookie)
+ XdmAuthenticationInit (xdmAuthCookie, strlen (xdmAuthCookie));
+#endif
+ if (state != XDM_OFF)
+ {
+ XdmcpRegisterAuthorizations();
+ XdmcpRegisterDisplayClass (defaultDisplayClass, strlen (defaultDisplayClass));
+ AccessUsingXdmcp();
+ RegisterBlockAndWakeupHandlers (XdmcpBlockHandler, XdmcpWakeupHandler,
+ (pointer) 0);
+ timeOutRtx = 0;
+ DisplayNumber = (CARD16) atoi(display);
+ if (ConnectionTypes.length>1 && xdm_from==NULL)
+ {
+ unsigned i=0;
+ char ErrorMessage[1024];
+ sprintf(ErrorMessage,"Multiple ip-addresses detected:\n");
+ for (i=0; i<ConnectionTypes.length; i++)
+ {
+ int AddrLen=ConnectionAddresses.data[i].length;
+ if (AddrLen==4)
+ sprintf(ErrorMessage+strlen(ErrorMessage)," %d.%d.%d.%d\n",
+ ConnectionAddresses.data[i].data[0],
+ ConnectionAddresses.data[i].data[1],
+ ConnectionAddresses.data[i].data[2],
+ ConnectionAddresses.data[i].data[3]);
+ else
+ {
+ int j;
+ sprintf(ErrorMessage+strlen(ErrorMessage)," ");
+ for (j=0; j<AddrLen; j++)
+ sprintf(ErrorMessage+strlen(ErrorMessage),"%02x",ConnectionAddresses.data[i].data[j]);
+ sprintf(ErrorMessage+strlen(ErrorMessage),"\n");
+ }
+ }
+ sprintf(ErrorMessage+strlen(ErrorMessage),"Please specify the ip-address you want to use with -from\n");
+ FatalError(ErrorMessage);
+ }
+ get_xdmcp_sock();
+ send_packet();
+ }
+}
+
+void
+XdmcpReset (void)
+{
+ state = XDM_INIT_STATE;
+ if (state != XDM_OFF)
+ {
+ RegisterBlockAndWakeupHandlers (XdmcpBlockHandler, XdmcpWakeupHandler,
+ (pointer) 0);
+ timeOutRtx = 0;
+ send_packet();
+ }
+}
+
+/*
+ * Called whenever a new connection is created; notices the
+ * first connection and saves it to terminate the session
+ * when it is closed
+ */
+
+void
+XdmcpOpenDisplay(int sock)
+{
+ if (state != XDM_AWAIT_MANAGE_RESPONSE)
+ return;
+ state = XDM_RUN_SESSION;
+ sessionSocket = sock;
+}
+
+void
+XdmcpCloseDisplay(int sock)
+{
+ if ((state != XDM_RUN_SESSION && state != XDM_AWAIT_ALIVE_RESPONSE)
+ || sessionSocket != sock)
+ return;
+ state = XDM_INIT_STATE;
+ if (OneSession)
+ dispatchException |= DE_TERMINATE;
+ else
+ dispatchException |= DE_RESET;
+ isItTimeToYield = TRUE;
+}
+
+/*
+ * called before going to sleep, this routine
+ * may modify the timeout value about to be sent
+ * to select; in this way XDMCP can do appropriate things
+ * dynamically while starting up
+ */
+
+/*ARGSUSED*/
+static void
+XdmcpBlockHandler(
+ pointer data, /* unused */
+ struct timeval **wt,
+ pointer pReadmask)
+{
+ fd_set *LastSelectMask = (fd_set*)pReadmask;
+ CARD32 millisToGo;
+
+ if (state == XDM_OFF)
+ return;
+ FD_SET(xdmcpSocket, LastSelectMask);
+#if defined(IPv6) && defined(AF_INET6)
+ if (xdmcpSocket6 >= 0)
+ FD_SET(xdmcpSocket6, LastSelectMask);
+#endif
+ if (timeOutTime == 0)
+ return;
+ millisToGo = timeOutTime - GetTimeInMillis();
+ if ((int) millisToGo <= 0)
+ millisToGo = 1;
+ AdjustWaitForDelay (wt, millisToGo);
+}
+
+/*
+ * called after select returns; this routine will
+ * recognise when XDMCP packets await and
+ * process them appropriately
+ */
+
+/*ARGSUSED*/
+static void
+XdmcpWakeupHandler(
+ pointer data, /* unused */
+ int i,
+ pointer pReadmask)
+{
+ fd_set* LastSelectMask = (fd_set*)pReadmask;
+ fd_set devicesReadable;
+
+ if (state == XDM_OFF)
+ return;
+ if (i > 0)
+ {
+ if (FD_ISSET(xdmcpSocket, LastSelectMask))
+ {
+ receive_packet(xdmcpSocket);
+ FD_CLR(xdmcpSocket, LastSelectMask);
+ }
+#if defined(IPv6) && defined(AF_INET6)
+ if (xdmcpSocket6 >= 0 && FD_ISSET(xdmcpSocket6, LastSelectMask))
+ {
+ receive_packet(xdmcpSocket6);
+ FD_CLR(xdmcpSocket6, LastSelectMask);
+ }
+#endif
+ XFD_ANDSET(&devicesReadable, LastSelectMask, &EnabledDevices);
+ if (XFD_ANYSET(&devicesReadable))
+ {
+ if (state == XDM_AWAIT_USER_INPUT)
+ restart();
+ else if (state == XDM_RUN_SESSION)
+ keepaliveDormancy = defaultKeepaliveDormancy;
+ }
+ }
+}
+
+/*
+ * This routine should be called from the routine that drives the
+ * user's host menu when the user selects a host
+ */
+
+static void
+XdmcpSelectHost(
+ const struct sockaddr *host_sockaddr,
+ int host_len,
+ ARRAY8Ptr AuthenticationName)
+{
+ state = XDM_START_CONNECTION;
+ memmove(&req_sockaddr, host_sockaddr, host_len);
+ req_socklen = host_len;
+ XdmcpSetAuthentication (AuthenticationName);
+ send_packet();
+}
+
+/*
+ * !!! this routine should be replaced by a routine that adds
+ * the host to the user's host menu. the current version just
+ * selects the first host to respond with willing message.
+ */
+
+#ifdef _MSC_VER
+void DisplayXdmcpHostsDialog(void);
+int XdmcpHostAddName(const char *HostName, int HostIndex);
+
+struct hostinfo
+{
+ struct sockaddr *from;
+ int fromlen;
+ ARRAY8 AuthenticationName;
+};
+static int g_NrHosts;
+static struct hostinfo *g_Hosts;
+
+void XdmcpHostSelected(int HostIdx)
+{
+ int i;
+
+ /* Connect to the selected host */
+ XdmcpSelectHost(g_Hosts[HostIdx].from, g_Hosts[HostIdx].fromlen, &g_Hosts[HostIdx].AuthenticationName);
+
+ for (i=0; i<g_NrHosts; i++)
+ {
+ free(g_Hosts[i].from);
+ free(g_Hosts[i].AuthenticationName.data);
+ }
+ free(g_Hosts);
+ g_Hosts=NULL;
+ g_NrHosts=0;
+}
+#endif
+
+/*ARGSUSED*/
+static void
+XdmcpAddHost(
+ const struct sockaddr *from,
+ int fromlen,
+ ARRAY8Ptr AuthenticationName,
+ ARRAY8Ptr hostname,
+ ARRAY8Ptr status)
+{
+#ifdef _MSC_VER
+ char szHostName[100];
+ int HostIdx;
+
+ memcpy(szHostName,hostname->data,hostname->length);
+ szHostName[hostname->length]=0;
+
+ DisplayXdmcpHostsDialog(); /* Display the dialog if not already displayed */
+
+ HostIdx=XdmcpHostAddName(szHostName, g_NrHosts);
+ if (HostIdx==-1)
+ {
+ HostIdx=g_NrHosts;
+ g_NrHosts++;
+ g_Hosts=realloc(g_Hosts,g_NrHosts*sizeof(*g_Hosts));
+ g_Hosts[HostIdx].AuthenticationName.data=NULL;
+ g_Hosts[HostIdx].from=NULL;
+ }
+
+ g_Hosts[HostIdx].fromlen=fromlen;
+ g_Hosts[HostIdx].from=realloc(g_Hosts[HostIdx].from,g_Hosts[HostIdx].fromlen);
+ memcpy(g_Hosts[HostIdx].from,from,fromlen);
+
+ g_Hosts[HostIdx].AuthenticationName.length=AuthenticationName->length;
+ g_Hosts[HostIdx].AuthenticationName.data=realloc(g_Hosts[HostIdx].AuthenticationName.data,AuthenticationName->length);
+ memcpy(g_Hosts[HostIdx].AuthenticationName.data,AuthenticationName->data,AuthenticationName->length);
+#else
+ XdmcpSelectHost(from, fromlen, AuthenticationName);
+#endif
+}
+
+/*
+ * A message is queued on the socket; read it and
+ * do the appropriate thing
+ */
+
+static ARRAY8 UnwillingMessage = { (CARD8) 14, (CARD8 *) "Host unwilling" };
+
+static void
+receive_packet(int socketfd)
+{
+#if defined(IPv6) && defined(AF_INET6)
+ struct sockaddr_storage from;
+#else
+ struct sockaddr_in from;
+#endif
+ int fromlen = sizeof(from);
+ XdmcpHeader header;
+
+ /* read message off socket */
+ if (!XdmcpFill (socketfd, &buffer, (XdmcpNetaddr) &from, &fromlen))
+ return;
+
+ /* reset retransmission backoff */
+ timeOutRtx = 0;
+
+ if (!XdmcpReadHeader (&buffer, &header))
+ return;
+
+ if (header.version != XDM_PROTOCOL_VERSION)
+ return;
+
+ switch (header.opcode) {
+ case WILLING:
+ recv_willing_msg((struct sockaddr *) &from, fromlen, header.length);
+ break;
+ case UNWILLING:
+ XdmcpFatal("Manager unwilling", &UnwillingMessage);
+ break;
+ case ACCEPT:
+ recv_accept_msg(header.length);
+ break;
+ case DECLINE:
+ recv_decline_msg(header.length);
+ break;
+ case REFUSE:
+ recv_refuse_msg(header.length);
+ break;
+ case FAILED:
+ recv_failed_msg(header.length);
+ break;
+ case ALIVE:
+ recv_alive_msg(header.length);
+ break;
+ }
+}
+
+/*
+ * send the appropriate message given the current state
+ */
+
+static void
+send_packet(void)
+{
+ int rtx;
+ switch (state) {
+ case XDM_QUERY:
+ case XDM_BROADCAST:
+ case XDM_INDIRECT:
+#if defined(IPv6) && defined(AF_INET6)
+ case XDM_MULTICAST:
+#endif
+ send_query_msg();
+ break;
+ case XDM_START_CONNECTION:
+ send_request_msg();
+ break;
+ case XDM_MANAGE:
+ send_manage_msg();
+ break;
+ case XDM_KEEPALIVE:
+ send_keepalive_msg();
+ break;
+ default:
+ break;
+ }
+ rtx = (XDM_MIN_RTX << timeOutRtx);
+ if (rtx > XDM_MAX_RTX)
+ rtx = XDM_MAX_RTX;
+ timeOutTime = GetTimeInMillis() + rtx * 1000;
+}
+
+/*
+ * The session is declared dead for some reason; too many
+ * timeouts, or Keepalive failure.
+ */
+
+static void
+XdmcpDeadSession (const char *reason)
+{
+ ErrorF ("XDM: %s, declaring session dead\n", reason);
+ state = XDM_INIT_STATE;
+ isItTimeToYield = TRUE;
+ dispatchException |= DE_RESET;
+ timeOutTime = 0;
+ timeOutRtx = 0;
+ send_packet();
+}
+
+/*
+ * Timeout waiting for an XDMCP response.
+ */
+
+static void
+timeout(void)
+{
+ timeOutRtx++;
+ if (state == XDM_AWAIT_ALIVE_RESPONSE && timeOutRtx >= XDM_KA_RTX_LIMIT )
+ {
+ XdmcpDeadSession ("too many keepalive retransmissions");
+ return;
+ }
+ else if (timeOutRtx >= XDM_RTX_LIMIT)
+ {
+ /* Quit if "-once" specified, otherwise reset and try again. */
+ if (OneSession) {
+ dispatchException |= DE_TERMINATE;
+ ErrorF("XDM: too many retransmissions\n");
+ } else {
+ XdmcpDeadSession("too many retransmissions");
+ }
+ return;
+ }
+
+#if defined(IPv6) && defined(AF_INET6)
+ if (state == XDM_COLLECT_QUERY || state == XDM_COLLECT_INDIRECT_QUERY) {
+ /* Try next address */
+ for (mgrAddr = mgrAddr->ai_next; ; mgrAddr = mgrAddr->ai_next) {
+ if (mgrAddr == NULL) {
+ mgrAddr = mgrAddrFirst;
+ }
+ if (mgrAddr->ai_family == AF_INET
+ || mgrAddr->ai_family == AF_INET6)
+ break;
+ }
+#ifndef SIN6_LEN
+ ManagerAddressLen = mgrAddr->ai_addrlen;
+#endif
+ memcpy(&ManagerAddress, mgrAddr->ai_addr, mgrAddr->ai_addrlen);
+ }
+#endif
+
+ switch (state) {
+ case XDM_COLLECT_QUERY:
+ state = XDM_QUERY;
+ break;
+ case XDM_COLLECT_BROADCAST_QUERY:
+ state = XDM_BROADCAST;
+ break;
+#if defined(IPv6) && defined(AF_INET6)
+ case XDM_COLLECT_MULTICAST_QUERY:
+ state = XDM_MULTICAST;
+ break;
+#endif
+ case XDM_COLLECT_INDIRECT_QUERY:
+ state = XDM_INDIRECT;
+ break;
+ case XDM_AWAIT_REQUEST_RESPONSE:
+ state = XDM_START_CONNECTION;
+ break;
+ case XDM_AWAIT_MANAGE_RESPONSE:
+ state = XDM_MANAGE;
+ break;
+ case XDM_AWAIT_ALIVE_RESPONSE:
+ state = XDM_KEEPALIVE;
+ break;
+ default:
+ break;
+ }
+ send_packet();
+}
+
+static void
+restart(void)
+{
+ state = XDM_INIT_STATE;
+ timeOutRtx = 0;
+ send_packet();
+}
+
+static int
+XdmcpCheckAuthentication (ARRAY8Ptr Name, ARRAY8Ptr Data, int packet_type)
+{
+ return (XdmcpARRAY8Equal (Name, AuthenticationName) &&
+ (AuthenticationName->length == 0 ||
+ (*AuthenticationFuncs->Validator) (AuthenticationData, Data, packet_type)));
+}
+
+static int
+XdmcpAddAuthorization (ARRAY8Ptr name, ARRAY8Ptr data)
+{
+ AddAuthorFunc AddAuth;
+
+ if (AuthenticationFuncs && AuthenticationFuncs->AddAuth)
+ AddAuth = AuthenticationFuncs->AddAuth;
+ else
+ AddAuth = AddAuthorization;
+ return (*AddAuth) ((unsigned short)name->length,
+ (char *)name->data,
+ (unsigned short)data->length,
+ (char *)data->data);
+}
+
+/*
+ * from here to the end of this file are routines private
+ * to the state machine.
+ */
+
+static void
+get_xdmcp_sock(void)
+{
+#ifdef STREAMSCONN
+ struct netconfig *nconf;
+
+ if ((xdmcpSocket = t_open("/dev/udp", O_RDWR, 0)) < 0) {
+ XdmcpWarning("t_open() of /dev/udp failed");
+ return;
+ }
+
+ if( t_bind(xdmcpSocket,NULL,NULL) < 0 ) {
+ XdmcpWarning("UDP socket creation failed");
+ t_error("t_bind(xdmcpSocket) failed" );
+ t_close(xdmcpSocket);
+ return;
+ }
+
+ /*
+ * This part of the code looks contrived. It will actually fit in nicely
+ * when the CLTS part of Xtrans is implemented.
+ */
+
+ if( (nconf=getnetconfigent("udp")) == NULL ) {
+ XdmcpWarning("UDP socket creation failed: getnetconfigent()");
+ t_unbind(xdmcpSocket);
+ t_close(xdmcpSocket);
+ return;
+ }
+
+ if( netdir_options(nconf, ND_SET_BROADCAST, xdmcpSocket, NULL) ) {
+ XdmcpWarning("UDP set broadcast option failed: netdir_options()");
+ freenetconfigent(nconf);
+ t_unbind(xdmcpSocket);
+ t_close(xdmcpSocket);
+ return;
+ }
+
+ freenetconfigent(nconf);
+#else
+ int soopts = 1;
+
+#if defined(IPv6) && defined(AF_INET6)
+ if ((xdmcpSocket6 = socket(AF_INET6, SOCK_DGRAM, 0)) < 0)
+ XdmcpWarning("INET6 UDP socket creation failed");
+#endif
+ if ((xdmcpSocket = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
+ XdmcpWarning("UDP socket creation failed");
+#ifdef SO_BROADCAST
+ else if (setsockopt(xdmcpSocket, SOL_SOCKET, SO_BROADCAST, (char *)&soopts,
+ sizeof(soopts)) < 0)
+ XdmcpWarning("UDP set broadcast socket-option failed");
+#endif /* SO_BROADCAST */
+ if (xdm_from)
+ {
+ if (xdmcpSocket >= 0 && SOCKADDR_FAMILY(FromAddress)==AF_INET) {
+ if (bind(xdmcpSocket, (struct sockaddr *)&FromAddress,
+ FromAddressLen) < 0) {
+ FatalError("Xserver: failed to bind to -from address: %s error %d\n", xdm_from, WSAGetLastError());
+ }
+ }
+ else if (xdmcpSocket6 >= 0 && SOCKADDR_FAMILY(FromAddress)==AF_INET6) {
+ if (bind(xdmcpSocket6, (struct sockaddr *)&FromAddress,
+ FromAddressLen) < 0) {
+ FatalError("Xserver: failed to bind to -from address: %s error %d\n", xdm_from, WSAGetLastError());
+ }
+ }
+ }
+#endif /* STREAMSCONN */
+}
+
+static void
+send_query_msg(void)
+{
+ XdmcpHeader header;
+ Bool broadcast = FALSE;
+#if defined(IPv6) && defined(AF_INET6)
+ Bool multicast = FALSE;
+#endif
+ int i;
+ int socketfd = xdmcpSocket;
+
+ header.version = XDM_PROTOCOL_VERSION;
+ switch(state){
+ case XDM_QUERY:
+ header.opcode = (CARD16) QUERY;
+ state = XDM_COLLECT_QUERY;
+ break;
+ case XDM_BROADCAST:
+ header.opcode = (CARD16) BROADCAST_QUERY;
+ state = XDM_COLLECT_BROADCAST_QUERY;
+ broadcast = TRUE;
+ break;
+#if defined(IPv6) && defined(AF_INET6)
+ case XDM_MULTICAST:
+ header.opcode = (CARD16) BROADCAST_QUERY;
+ state = XDM_COLLECT_MULTICAST_QUERY;
+ multicast = TRUE;
+ break;
+#endif
+ case XDM_INDIRECT:
+ header.opcode = (CARD16) INDIRECT_QUERY;
+ state = XDM_COLLECT_INDIRECT_QUERY;
+ break;
+ default:
+ break;
+ }
+ header.length = 1;
+ for (i = 0; i < AuthenticationNames.length; i++)
+ header.length += 2 + AuthenticationNames.data[i].length;
+
+ XdmcpWriteHeader (&buffer, &header);
+ XdmcpWriteARRAYofARRAY8 (&buffer, &AuthenticationNames);
+ if (broadcast)
+ {
+ int i;
+
+ for (i = 0; i < NumBroadcastAddresses; i++)
+ XdmcpFlush (xdmcpSocket, &buffer, (XdmcpNetaddr) &BroadcastAddresses[i],
+ sizeof (struct sockaddr_in));
+ }
+#if defined(IPv6) && defined(AF_INET6)
+ else if (multicast)
+ {
+ struct multicastinfo *mcl;
+ struct addrinfo *ai;
+
+ for (mcl = mcastlist; mcl != NULL; mcl = mcl->next) {
+ for (ai = mcl->ai ; ai != NULL; ai = ai->ai_next) {
+ if (ai->ai_family == AF_INET) {
+ int hopflag = mcl->hops;
+ socketfd = xdmcpSocket;
+ setsockopt(socketfd, IPPROTO_IP, IP_MULTICAST_TTL,
+ (char*)&hopflag, sizeof(hopflag));
+ } else if (ai->ai_family == AF_INET6) {
+ int hopflag6 = mcl->hops;
+ socketfd = xdmcpSocket6;
+ setsockopt(socketfd, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
+ (char*)&hopflag6, sizeof(hopflag6));
+ } else {
+ continue;
+ }
+ XdmcpFlush (socketfd, &buffer,
+ (XdmcpNetaddr) ai->ai_addr, ai->ai_addrlen);
+ break;
+ }
+ }
+ }
+#endif
+ else
+ {
+#if defined(IPv6) && defined(AF_INET6)
+ if (SOCKADDR_FAMILY(ManagerAddress) == AF_INET6)
+ socketfd = xdmcpSocket6;
+#endif
+ XdmcpFlush (socketfd, &buffer, (XdmcpNetaddr) &ManagerAddress,
+ ManagerAddressLen);
+ }
+}
+
+static void
+recv_willing_msg(
+ struct sockaddr *from,
+ int fromlen,
+ unsigned length)
+{
+ ARRAY8 authenticationName;
+ ARRAY8 hostname;
+ ARRAY8 status;
+
+ authenticationName.data = 0;
+ hostname.data = 0;
+ status.data = 0;
+ if (XdmcpReadARRAY8 (&buffer, &authenticationName) &&
+ XdmcpReadARRAY8 (&buffer, &hostname) &&
+ XdmcpReadARRAY8 (&buffer, &status))
+ {
+ if (length == 6 + authenticationName.length +
+ hostname.length + status.length)
+ {
+ switch (state)
+ {
+ case XDM_COLLECT_QUERY:
+ XdmcpSelectHost(from, fromlen, &authenticationName);
+ break;
+ case XDM_COLLECT_BROADCAST_QUERY:
+#if defined(IPv6) && defined(AF_INET6)
+ case XDM_COLLECT_MULTICAST_QUERY:
+#endif
+ case XDM_COLLECT_INDIRECT_QUERY:
+ XdmcpAddHost(from, fromlen, &authenticationName, &hostname, &status);
+ break;
+ default:
+ break;
+ }
+ }
+ }
+ XdmcpDisposeARRAY8 (&authenticationName);
+ XdmcpDisposeARRAY8 (&hostname);
+ XdmcpDisposeARRAY8 (&status);
+}
+
+static void
+send_request_msg(void)
+{
+ XdmcpHeader header;
+ int length;
+ int i;
+ CARD16 XdmcpConnectionType;
+ ARRAY8 authenticationData;
+ int socketfd = xdmcpSocket;
+
+ switch (SOCKADDR_FAMILY(ManagerAddress))
+ {
+ case AF_INET: XdmcpConnectionType=FamilyInternet; break;
+#if defined(IPv6) && defined(AF_INET6)
+ case AF_INET6: XdmcpConnectionType=FamilyInternet6; break;
+#endif
+ default: XdmcpConnectionType=0xffff; break;
+ }
+
+ header.version = XDM_PROTOCOL_VERSION;
+ header.opcode = (CARD16) REQUEST;
+
+ length = 2; /* display number */
+ length += 1 + 2 * ConnectionTypes.length; /* connection types */
+ length += 1; /* connection addresses */
+ for (i = 0; i < ConnectionAddresses.length; i++)
+ length += 2 + ConnectionAddresses.data[i].length;
+ authenticationData.length = 0;
+ authenticationData.data = 0;
+ if (AuthenticationFuncs)
+ {
+ (*AuthenticationFuncs->Generator) (AuthenticationData,
+ &authenticationData,
+ REQUEST);
+ }
+ length += 2 + AuthenticationName->length; /* authentication name */
+ length += 2 + authenticationData.length; /* authentication data */
+ length += 1; /* authorization names */
+ for (i = 0; i < AuthorizationNames.length; i++)
+ length += 2 + AuthorizationNames.data[i].length;
+ length += 2 + ManufacturerDisplayID.length; /* display ID */
+ header.length = length;
+
+ if (!XdmcpWriteHeader (&buffer, &header))
+ {
+ XdmcpDisposeARRAY8 (&authenticationData);
+ return;
+ }
+ XdmcpWriteCARD16 (&buffer, DisplayNumber);
+ XdmcpWriteCARD8 (&buffer, ConnectionTypes.length);
+
+ /* The connection array is send reordered, so that connections of */
+ /* the same address type as the XDMCP manager connection are send */
+ /* first. This works around a bug in xdm. mario@klebsch.de */
+ for (i = 0; i < (int)ConnectionTypes.length; i++)
+ if (ConnectionTypes.data[i]==XdmcpConnectionType)
+ XdmcpWriteCARD16 (&buffer, ConnectionTypes.data[i]);
+ for (i = 0; i < (int)ConnectionTypes.length; i++)
+ if (ConnectionTypes.data[i]!=XdmcpConnectionType)
+ XdmcpWriteCARD16 (&buffer, ConnectionTypes.data[i]);
+
+ XdmcpWriteCARD8 (&buffer, ConnectionAddresses.length);
+ for (i = 0; i < (int)ConnectionAddresses.length; i++)
+ if ( (i<ConnectionTypes.length) &&
+ (ConnectionTypes.data[i]==XdmcpConnectionType) )
+ XdmcpWriteARRAY8 (&buffer, &ConnectionAddresses.data[i]);
+ for (i = 0; i < (int)ConnectionAddresses.length; i++)
+ if ( (i>=ConnectionTypes.length) ||
+ (ConnectionTypes.data[i]!=XdmcpConnectionType) )
+ XdmcpWriteARRAY8 (&buffer, &ConnectionAddresses.data[i]);
+
+ XdmcpWriteARRAY8 (&buffer, AuthenticationName);
+ XdmcpWriteARRAY8 (&buffer, &authenticationData);
+ XdmcpDisposeARRAY8 (&authenticationData);
+ XdmcpWriteARRAYofARRAY8 (&buffer, &AuthorizationNames);
+ XdmcpWriteARRAY8 (&buffer, &ManufacturerDisplayID);
+#if defined(IPv6) && defined(AF_INET6)
+ if (SOCKADDR_FAMILY(req_sockaddr) == AF_INET6)
+ socketfd = xdmcpSocket6;
+#endif
+ if (XdmcpFlush (socketfd, &buffer,
+ (XdmcpNetaddr) &req_sockaddr, req_socklen))
+ state = XDM_AWAIT_REQUEST_RESPONSE;
+}
+
+static void
+recv_accept_msg(unsigned length)
+{
+ CARD32 AcceptSessionID;
+ ARRAY8 AcceptAuthenticationName, AcceptAuthenticationData;
+ ARRAY8 AcceptAuthorizationName, AcceptAuthorizationData;
+
+ if (state != XDM_AWAIT_REQUEST_RESPONSE)
+ return;
+ AcceptAuthenticationName.data = 0;
+ AcceptAuthenticationData.data = 0;
+ AcceptAuthorizationName.data = 0;
+ AcceptAuthorizationData.data = 0;
+ if (XdmcpReadCARD32 (&buffer, &AcceptSessionID) &&
+ XdmcpReadARRAY8 (&buffer, &AcceptAuthenticationName) &&
+ XdmcpReadARRAY8 (&buffer, &AcceptAuthenticationData) &&
+ XdmcpReadARRAY8 (&buffer, &AcceptAuthorizationName) &&
+ XdmcpReadARRAY8 (&buffer, &AcceptAuthorizationData))
+ {
+ if (length == 12 + AcceptAuthenticationName.length +
+ AcceptAuthenticationData.length +
+ AcceptAuthorizationName.length +
+ AcceptAuthorizationData.length)
+ {
+ if (!XdmcpCheckAuthentication (&AcceptAuthenticationName,
+ &AcceptAuthenticationData, ACCEPT))
+ {
+ XdmcpFatal ("Authentication Failure", &AcceptAuthenticationName);
+ }
+ /* permit access control manipulations from this host */
+ AugmentSelf (&req_sockaddr, req_socklen);
+ /* if the authorization specified in the packet fails
+ * to be acceptable, enable the local addresses
+ */
+ if (!XdmcpAddAuthorization (&AcceptAuthorizationName,
+ &AcceptAuthorizationData))
+ {
+ AddLocalHosts ();
+ }
+ SessionID = AcceptSessionID;
+ state = XDM_MANAGE;
+ send_packet();
+ }
+ }
+ XdmcpDisposeARRAY8 (&AcceptAuthenticationName);
+ XdmcpDisposeARRAY8 (&AcceptAuthenticationData);
+ XdmcpDisposeARRAY8 (&AcceptAuthorizationName);
+ XdmcpDisposeARRAY8 (&AcceptAuthorizationData);
+}
+
+static void
+recv_decline_msg(unsigned length)
+{
+ ARRAY8 status, DeclineAuthenticationName, DeclineAuthenticationData;
+
+ status.data = 0;
+ DeclineAuthenticationName.data = 0;
+ DeclineAuthenticationData.data = 0;
+ if (XdmcpReadARRAY8 (&buffer, &status) &&
+ XdmcpReadARRAY8 (&buffer, &DeclineAuthenticationName) &&
+ XdmcpReadARRAY8 (&buffer, &DeclineAuthenticationData))
+ {
+ if (length == 6 + status.length +
+ DeclineAuthenticationName.length +
+ DeclineAuthenticationData.length &&
+ XdmcpCheckAuthentication (&DeclineAuthenticationName,
+ &DeclineAuthenticationData, DECLINE))
+ {
+ XdmcpFatal ("Session declined", &status);
+ }
+ }
+ XdmcpDisposeARRAY8 (&status);
+ XdmcpDisposeARRAY8 (&DeclineAuthenticationName);
+ XdmcpDisposeARRAY8 (&DeclineAuthenticationData);
+}
+
+static void
+send_manage_msg(void)
+{
+ XdmcpHeader header;
+ int socketfd = xdmcpSocket;
+
+ header.version = XDM_PROTOCOL_VERSION;
+ header.opcode = (CARD16) MANAGE;
+ header.length = 8 + DisplayClass.length;
+
+ if (!XdmcpWriteHeader (&buffer, &header))
+ return;
+ XdmcpWriteCARD32 (&buffer, SessionID);
+ XdmcpWriteCARD16 (&buffer, DisplayNumber);
+ XdmcpWriteARRAY8 (&buffer, &DisplayClass);
+ state = XDM_AWAIT_MANAGE_RESPONSE;
+#if defined(IPv6) && defined(AF_INET6)
+ if (SOCKADDR_FAMILY(req_sockaddr) == AF_INET6)
+ socketfd = xdmcpSocket6;
+#endif
+ XdmcpFlush (socketfd, &buffer, (XdmcpNetaddr) &req_sockaddr, req_socklen);
+}
+
+static void
+recv_refuse_msg(unsigned length)
+{
+ CARD32 RefusedSessionID;
+
+ if (state != XDM_AWAIT_MANAGE_RESPONSE)
+ return;
+ if (length != 4)
+ return;
+ if (XdmcpReadCARD32 (&buffer, &RefusedSessionID))
+ {
+ if (RefusedSessionID == SessionID)
+ {
+ state = XDM_START_CONNECTION;
+ send_packet();
+ }
+ }
+}
+
+static void
+recv_failed_msg(unsigned length)
+{
+ CARD32 FailedSessionID;
+ ARRAY8 status;
+
+ if (state != XDM_AWAIT_MANAGE_RESPONSE)
+ return;
+ status.data = 0;
+ if (XdmcpReadCARD32 (&buffer, &FailedSessionID) &&
+ XdmcpReadARRAY8 (&buffer, &status))
+ {
+ if (length == 6 + status.length &&
+ SessionID == FailedSessionID)
+ {
+ XdmcpFatal ("Session failed", &status);
+ }
+ }
+ XdmcpDisposeARRAY8 (&status);
+}
+
+static void
+send_keepalive_msg(void)
+{
+ XdmcpHeader header;
+ int socketfd = xdmcpSocket;
+
+ header.version = XDM_PROTOCOL_VERSION;
+ header.opcode = (CARD16) KEEPALIVE;
+ header.length = 6;
+
+ XdmcpWriteHeader (&buffer, &header);
+ XdmcpWriteCARD16 (&buffer, DisplayNumber);
+ XdmcpWriteCARD32 (&buffer, SessionID);
+
+ state = XDM_AWAIT_ALIVE_RESPONSE;
+#if defined(IPv6) && defined(AF_INET6)
+ if (SOCKADDR_FAMILY(req_sockaddr) == AF_INET6)
+ socketfd = xdmcpSocket6;
+#endif
+ XdmcpFlush (socketfd, &buffer, (XdmcpNetaddr) &req_sockaddr, req_socklen);
+}
+
+static void
+recv_alive_msg (unsigned length)
+{
+ CARD8 SessionRunning;
+ CARD32 AliveSessionID;
+
+ if (state != XDM_AWAIT_ALIVE_RESPONSE)
+ return;
+ if (length != 5)
+ return;
+ if (XdmcpReadCARD8 (&buffer, &SessionRunning) &&
+ XdmcpReadCARD32 (&buffer, &AliveSessionID))
+ {
+ if (SessionRunning && AliveSessionID == SessionID)
+ {
+ /* backoff dormancy period */
+ state = XDM_RUN_SESSION;
+ if ((GetTimeInMillis() - lastDeviceEventTime.milliseconds) >
+ keepaliveDormancy * 1000)
+ {
+ keepaliveDormancy <<= 1;
+ if (keepaliveDormancy > XDM_MAX_DORMANCY)
+ keepaliveDormancy = XDM_MAX_DORMANCY;
+ }
+ timeOutTime = GetTimeInMillis() + keepaliveDormancy * 1000;
+ }
+ else
+ {
+ XdmcpDeadSession ("Alive response indicates session dead");
+ }
+ }
+}
+
+static void
+XdmcpFatal (
+ const char *type,
+ ARRAY8Ptr status)
+{
+ FatalError ("XDMCP fatal error: %s %*.*s\n", type,
+ status->length, status->length, status->data);
+}
+
+static void
+XdmcpWarning(const char *str)
+{
+ ErrorF("XDMCP warning: %s\n", str);
+}
+
+static void
+get_addr_by_name(
+ const char *argtype,
+ const char *namestr,
+ int port,
+ int socktype,
+ SOCKADDR_TYPE *addr,
+ SOCKLEN_TYPE *addrlen
+#if defined(IPv6) && defined(AF_INET6)
+ ,
+ struct addrinfo **aip,
+ struct addrinfo **aifirstp
+#endif
+ )
+{
+#if defined(IPv6) && defined(AF_INET6)
+ struct addrinfo *ai;
+ struct addrinfo hints;
+ char portstr[6];
+ char *pport = portstr;
+ int gaierr;
+
+#if defined(WIN32) && defined(TCPCONN)
+ _XSERVTransWSAStartup();
+#endif
+
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_socktype = socktype;
+
+ if (port == 0) {
+ pport = NULL;
+ } else if (port > 0 && port < 65535) {
+ snprintf(portstr, sizeof(portstr), "%d", port);
+ } else {
+ FatalError("Xserver: port out of range: %d\n", port);
+ }
+
+ if (*aifirstp != NULL) {
+ freeaddrinfo(*aifirstp);
+ *aifirstp = NULL;
+ }
+
+ if ((gaierr = getaddrinfo(namestr, pport, &hints, aifirstp)) == 0) {
+ for (ai = *aifirstp; ai != NULL; ai = ai->ai_next) {
+ if (ai->ai_family == AF_INET || ai->ai_family == AF_INET6)
+ break;
+ }
+ if ((ai == NULL) || (ai->ai_addrlen > sizeof(SOCKADDR_TYPE))) {
+ FatalError ("Xserver: %s host %s not on supported network type\n",
+ argtype, namestr);
+ } else {
+ *aip = ai;
+ *addrlen = ai->ai_addrlen;
+ memcpy(addr, ai->ai_addr, ai->ai_addrlen);
+ }
+ } else {
+ FatalError("Xserver: %s: %s %s\n", gai_strerror(gaierr), argtype, namestr);
+ }
+#else
+ struct hostent *hep;
+#ifdef XTHREADS_NEEDS_BYNAMEPARAMS
+ _Xgethostbynameparams hparams;
+#endif
+ if (!(hep = _XGethostbyname(namestr, hparams)))
+ {
+ FatalError("Xserver: %s unknown host: %s\n", argtype, namestr);
+ }
+ if (hep->h_length == sizeof (struct in_addr))
+ {
+ memmove(&addr->sin_addr, hep->h_addr, hep->h_length);
+ *addrlen = sizeof(struct sockaddr_in);
+ addr->sin_family = AF_INET;
+ addr->sin_port = htons (port);
+ }
+ else
+ {
+ FatalError("Xserver: %s host on strange network %s\n", argtype, namestr);
+ }
+#endif
+}
+
+static void
+get_manager_by_name(
+ int argc,
+ char **argv,
+ int i)
+{
+
+ PSOCKADDR_IN queryAddr = NULL;
+ u_long u_lqueryAddr = 0;
+
+ if ((i + 1) == argc)
+ {
+ FatalError("Xserver: missing %s host name in command line\n", argv[i]);
+ }
+
+ get_addr_by_name(argv[i], argv[i+1], xdm_udp_port, SOCK_DGRAM,
+ &ManagerAddress, &ManagerAddressLen
+#if defined(IPv6) && defined(AF_INET6)
+ , &mgrAddr, &mgrAddrFirst
+#endif
+ );
+ queryAddr = (PSOCKADDR_IN)&ManagerAddress;
+ u_lqueryAddr = queryAddr->sin_addr.S_un.S_addr;
+ match_interface(u_lqueryAddr);
+}
+
+
+static void
+get_fromaddr_by_name(
+ int argc,
+ char **argv,
+ int i)
+{
+#if defined(IPv6) && defined(AF_INET6)
+ struct addrinfo *ai = NULL;
+ struct addrinfo *aifirst = NULL;
+#endif
+ if (i == argc)
+ {
+ FatalError("Xserver: missing -from host name in command line\n");
+ }
+ get_addr_by_name("-from", argv[i], 0, 0, &FromAddress, &FromAddressLen
+#if defined(IPv6) && defined(AF_INET6)
+ , &ai, &aifirst
+#endif
+ );
+#if defined(IPv6) && defined(AF_INET6)
+ if (aifirst != NULL)
+ freeaddrinfo(aifirst);
+#endif
+ xdm_from = argv[i];
+}
+
+
+#if defined(IPv6) && defined(AF_INET6)
+static int
+get_mcast_options(int argc, char **argv, int i)
+{
+ char *address = XDM_DEFAULT_MCAST_ADDR6;
+ int hopcount = 1;
+ struct addrinfo hints;
+ char portstr[6];
+ int gaierr;
+ struct addrinfo *ai, *firstai;
+
+ if ((i < argc) && (argv[i][0] != '-') && (argv[i][0] != '+')) {
+ address = argv[i++];
+ if ((i < argc) && (argv[i][0] != '-') && (argv[i][0] != '+')) {
+ hopcount = strtol(argv[i++], NULL, 10);
+ if ((hopcount < 1) || (hopcount > 255)) {
+ FatalError("Xserver: multicast hop count out of range: %d\n",
+ hopcount);
+ }
+ }
+ }
+
+ if (xdm_udp_port > 0 && xdm_udp_port < 65535) {
+ snprintf(portstr, sizeof(portstr), "%d", xdm_udp_port);
+ } else {
+ FatalError("Xserver: port out of range: %d\n", xdm_udp_port);
+ }
+ memset(&hints, 0, sizeof(hints));
+ hints.ai_socktype = SOCK_DGRAM;
+
+ if ((gaierr = getaddrinfo(address, portstr, &hints, &firstai)) == 0) {
+ for (ai = firstai; ai != NULL; ai = ai->ai_next) {
+ if (((ai->ai_family == AF_INET) &&
+ IN_MULTICAST(((struct sockaddr_in *) ai->ai_addr)
+ ->sin_addr.s_addr))
+ || ((ai->ai_family == AF_INET6) &&
+ IN6_IS_ADDR_MULTICAST(&((struct sockaddr_in6 *) ai->ai_addr)
+ ->sin6_addr)))
+ break;
+ }
+ if (ai == NULL) {
+ FatalError ("Xserver: address not supported multicast type %s\n",
+ address);
+ } else {
+ struct multicastinfo *mcastinfo, *mcl;
+
+ mcastinfo = malloc(sizeof(struct multicastinfo));
+ mcastinfo->next = NULL;
+ mcastinfo->ai = firstai;
+ mcastinfo->hops = hopcount;
+
+ if (mcastlist == NULL) {
+ mcastlist = mcastinfo;
+ } else {
+ for (mcl = mcastlist; mcl->next != NULL; mcl = mcl->next) {
+ /* Do nothing - just find end of list */
+ }
+ mcl->next = mcastinfo;
+ }
+ }
+ } else {
+ FatalError("Xserver: %s: %s\n", gai_strerror(gaierr), address);
+ }
+ return i;
+}
+#endif
+
+#else
+static int xdmcp_non_empty; /* avoid complaint by ranlib */
+#endif /* XDMCP */