winauth.c revision 05b261ec
105b261ecSmrg#ifdef HAVE_XWIN_CONFIG_H
205b261ecSmrg#include <xwin-config.h>
305b261ecSmrg#endif
405b261ecSmrg#if defined(XCSECURITY)
505b261ecSmrg/*
605b261ecSmrg *Copyright (C) 2003-2004 Harold L Hunt II All Rights Reserved.
705b261ecSmrg *
805b261ecSmrg *Permission is hereby granted, free of charge, to any person obtaining
905b261ecSmrg * a copy of this software and associated documentation files (the
1005b261ecSmrg *"Software"), to deal in the Software without restriction, including
1105b261ecSmrg *without limitation the rights to use, copy, modify, merge, publish,
1205b261ecSmrg *distribute, sublicense, and/or sell copies of the Software, and to
1305b261ecSmrg *permit persons to whom the Software is furnished to do so, subject to
1405b261ecSmrg *the following conditions:
1505b261ecSmrg *
1605b261ecSmrg *The above copyright notice and this permission notice shall be
1705b261ecSmrg *included in all copies or substantial portions of the Software.
1805b261ecSmrg *
1905b261ecSmrg *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
2005b261ecSmrg *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
2105b261ecSmrg *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
2205b261ecSmrg *NONINFRINGEMENT. IN NO EVENT SHALL HAROLD L HUNT II BE LIABLE FOR
2305b261ecSmrg *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
2405b261ecSmrg *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2505b261ecSmrg *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2605b261ecSmrg *
2705b261ecSmrg *Except as contained in this notice, the name of Harold L Hunt II
2805b261ecSmrg *shall not be used in advertising or otherwise to promote the sale, use
2905b261ecSmrg *or other dealings in this Software without prior written authorization
3005b261ecSmrg *from Harold L Hunt II.
3105b261ecSmrg *
3205b261ecSmrg * Authors:	Harold L Hunt II
3305b261ecSmrg */
3405b261ecSmrg
3505b261ecSmrg#include "win.h"
3605b261ecSmrg
3705b261ecSmrg/* Includes for authorization */
3805b261ecSmrg#include <X11/Xauth.h>
3905b261ecSmrg#include "securitysrv.h"
4005b261ecSmrg#include <X11/extensions/securstr.h>
4105b261ecSmrg
4205b261ecSmrg
4305b261ecSmrg/*
4405b261ecSmrg * Constants
4505b261ecSmrg */
4605b261ecSmrg
4705b261ecSmrg#define AUTH_NAME	"MIT-MAGIC-COOKIE-1"
4805b261ecSmrg
4905b261ecSmrg
5005b261ecSmrg/*
5105b261ecSmrg * Globals
5205b261ecSmrg */
5305b261ecSmrg
5405b261ecSmrgXID		g_authId = 0;
5505b261ecSmrgunsigned int	g_uiAuthDataLen = 0;
5605b261ecSmrgchar		*g_pAuthData = NULL;
5705b261ecSmrg
5805b261ecSmrg
5905b261ecSmrg/*
6005b261ecSmrg * Generate authorization cookie for internal server clients
6105b261ecSmrg */
6205b261ecSmrg
6305b261ecSmrgBool
6405b261ecSmrgwinGenerateAuthorization ()
6505b261ecSmrg{
6605b261ecSmrg  Bool				fFreeAuth = FALSE;
6705b261ecSmrg  SecurityAuthorizationPtr	pAuth = NULL;
6805b261ecSmrg
6905b261ecSmrg  /* Call OS layer to generate authorization key */
7005b261ecSmrg  g_authId = GenerateAuthorization (strlen (AUTH_NAME),
7105b261ecSmrg				    AUTH_NAME,
7205b261ecSmrg				    0,
7305b261ecSmrg				    NULL,
7405b261ecSmrg				    &g_uiAuthDataLen,
7505b261ecSmrg				    &g_pAuthData);
7605b261ecSmrg  if ((XID) ~0L == g_authId)
7705b261ecSmrg    {
7805b261ecSmrg      ErrorF ("winGenerateAuthorization - GenerateAuthorization failed\n");
7905b261ecSmrg      goto auth_bailout;
8005b261ecSmrg    }
8105b261ecSmrg#if 0
8205b261ecSmrg  else
8305b261ecSmrg    {
8405b261ecSmrg      ErrorF ("winGenerateAuthorization - GenerateAuthorization success!\n"
8505b261ecSmrg	      "AuthDataLen: %d AuthData: %s\n",
8605b261ecSmrg	      g_uiAuthDataLen, g_pAuthData);
8705b261ecSmrg    }
8805b261ecSmrg#endif
8905b261ecSmrg
9005b261ecSmrg  /* Allocate structure for additional auth information */
9105b261ecSmrg  pAuth = (SecurityAuthorizationPtr)
9205b261ecSmrg    xalloc (sizeof (SecurityAuthorizationRec));
9305b261ecSmrg  if (!(pAuth))
9405b261ecSmrg    {
9505b261ecSmrg      ErrorF ("winGenerateAuthorization - Failed allocating "
9605b261ecSmrg	      "SecurityAuthorizationPtr.\n");
9705b261ecSmrg      goto auth_bailout;
9805b261ecSmrg    }
9905b261ecSmrg
10005b261ecSmrg  /* Fill in the auth fields */
10105b261ecSmrg  pAuth->id = g_authId;
10205b261ecSmrg  pAuth->timeout = 0; /* live for x seconds after refcnt == 0 */
10305b261ecSmrg  pAuth->group = None;
10405b261ecSmrg  pAuth->trustLevel = XSecurityClientTrusted;
10505b261ecSmrg  pAuth->refcnt = 1; /* this auth must stick around */
10605b261ecSmrg  pAuth->secondsRemaining = 0;
10705b261ecSmrg  pAuth->timer = NULL;
10805b261ecSmrg  pAuth->eventClients = NULL;
10905b261ecSmrg
11005b261ecSmrg  /* Add the authorization to the server's auth list */
11105b261ecSmrg  if (!AddResource (g_authId,
11205b261ecSmrg		    SecurityAuthorizationResType,
11305b261ecSmrg		    pAuth))
11405b261ecSmrg    {
11505b261ecSmrg      ErrorF ("winGenerateAuthorization - AddResource failed for auth.\n");
11605b261ecSmrg      fFreeAuth = TRUE;
11705b261ecSmrg      goto auth_bailout;
11805b261ecSmrg    }
11905b261ecSmrg
12005b261ecSmrg  /* Don't free the auth data, since it is still used internally */
12105b261ecSmrg  pAuth = NULL;
12205b261ecSmrg
12305b261ecSmrg  return TRUE;
12405b261ecSmrg
12505b261ecSmrg auth_bailout:
12605b261ecSmrg  if (fFreeAuth)
12705b261ecSmrg    xfree (pAuth);
12805b261ecSmrg
12905b261ecSmrg  return FALSE;
13005b261ecSmrg}
13105b261ecSmrg#endif
132