#ifdef HAVE_XWIN_CONFIG_H
#include <xwin-config.h>
#endif
#include "win.h"
#include "securitysrv.h"
#define AUTH_NAME "MIT-MAGIC-COOKIE-1"
static XID g_authId = 0;
static unsigned int g_uiAuthDataLen = 0;
static char *g_pAuthData = NULL;
#ifndef XCSECURITY
static
void
GenerateRandomData (int len, char *buf)
{
int fd;
fd = open("/dev/urandom", O_RDONLY);
read(fd, buf, len);
close(fd);
}
static char cookie[16];
XID
static MitGenerateCookie (
unsigned data_length,
char *data,
XID id,
unsigned *data_length_return,
char **data_return)
{
int i = 0;
int status;
while (data_length--)
{
cookie[i++] += *data++;
if (i >= sizeof (cookie)) i = 0;
}
GenerateRandomData(sizeof (cookie), cookie);
status = MitAddCookie(sizeof (cookie), cookie, id);
if (!status)
{
id = -1;
}
else
{
*data_return = cookie;
*data_length_return = sizeof (cookie);
}
return id;
}
static
XID
GenerateAuthorization(
unsigned name_length,
char *name,
unsigned data_length,
char *data,
unsigned *data_length_return,
char **data_return)
{
return MitGenerateCookie(data_length, data,
FakeClientID(0), data_length_return, data_return);
}
#endif
Bool
winGenerateAuthorization (void)
{
Bool fFreeAuth = FALSE;
SecurityAuthorizationPtr pAuth = NULL;
g_authId = GenerateAuthorization (strlen (AUTH_NAME),
AUTH_NAME,
0,
NULL,
&g_uiAuthDataLen,
&g_pAuthData);
if ((XID) ~0L == g_authId)
{
ErrorF ("winGenerateAuthorization - GenerateAuthorization failed\n");
goto auth_bailout;
}
else
{
winDebug("winGenerateAuthorization - GenerateAuthorization success!\n"
"AuthDataLen: %d AuthData: %s\n",
g_uiAuthDataLen, g_pAuthData);
}
#ifdef XCSECURITY
pAuth = (SecurityAuthorizationPtr)
malloc(sizeof (SecurityAuthorizationRec));
if (!(pAuth))
{
ErrorF ("winGenerateAuthorization - Failed allocating "
"SecurityAuthorizationPtr.\n");
goto auth_bailout;
}
pAuth->id = g_authId;
pAuth->timeout = 0;
pAuth->group = None;
pAuth->trustLevel = XSecurityClientTrusted;
pAuth->refcnt = 1;
pAuth->secondsRemaining = 0;
pAuth->timer = NULL;
pAuth->eventClients = NULL;
if (!AddResource (g_authId,
SecurityAuthorizationResType,
pAuth))
{
ErrorF ("winGenerateAuthorization - AddResource failed for auth.\n");
fFreeAuth = TRUE;
goto auth_bailout;
}
pAuth = NULL;
#endif
return TRUE;
auth_bailout:
if (fFreeAuth)
free(pAuth);
return FALSE;
}
void
winSetAuthorization(void)
{
XSetAuthorization (AUTH_NAME,
strlen (AUTH_NAME),
g_pAuthData,
g_uiAuthDataLen);
}