winauth.c revision 05b261ec
1#ifdef HAVE_XWIN_CONFIG_H
2#include <xwin-config.h>
3#endif
4#if defined(XCSECURITY)
5/*
6 *Copyright (C) 2003-2004 Harold L Hunt II All Rights Reserved.
7 *
8 *Permission is hereby granted, free of charge, to any person obtaining
9 * a copy of this software and associated documentation files (the
10 *"Software"), to deal in the Software without restriction, including
11 *without limitation the rights to use, copy, modify, merge, publish,
12 *distribute, sublicense, and/or sell copies of the Software, and to
13 *permit persons to whom the Software is furnished to do so, subject to
14 *the following conditions:
15 *
16 *The above copyright notice and this permission notice shall be
17 *included in all copies or substantial portions of the Software.
18 *
19 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 *NONINFRINGEMENT. IN NO EVENT SHALL HAROLD L HUNT II BE LIABLE FOR
23 *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
24 *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
26 *
27 *Except as contained in this notice, the name of Harold L Hunt II
28 *shall not be used in advertising or otherwise to promote the sale, use
29 *or other dealings in this Software without prior written authorization
30 *from Harold L Hunt II.
31 *
32 * Authors:	Harold L Hunt II
33 */
34
35#include "win.h"
36
37/* Includes for authorization */
38#include <X11/Xauth.h>
39#include "securitysrv.h"
40#include <X11/extensions/securstr.h>
41
42
43/*
44 * Constants
45 */
46
47#define AUTH_NAME	"MIT-MAGIC-COOKIE-1"
48
49
50/*
51 * Globals
52 */
53
54XID		g_authId = 0;
55unsigned int	g_uiAuthDataLen = 0;
56char		*g_pAuthData = NULL;
57
58
59/*
60 * Generate authorization cookie for internal server clients
61 */
62
63Bool
64winGenerateAuthorization ()
65{
66  Bool				fFreeAuth = FALSE;
67  SecurityAuthorizationPtr	pAuth = NULL;
68
69  /* Call OS layer to generate authorization key */
70  g_authId = GenerateAuthorization (strlen (AUTH_NAME),
71				    AUTH_NAME,
72				    0,
73				    NULL,
74				    &g_uiAuthDataLen,
75				    &g_pAuthData);
76  if ((XID) ~0L == g_authId)
77    {
78      ErrorF ("winGenerateAuthorization - GenerateAuthorization failed\n");
79      goto auth_bailout;
80    }
81#if 0
82  else
83    {
84      ErrorF ("winGenerateAuthorization - GenerateAuthorization success!\n"
85	      "AuthDataLen: %d AuthData: %s\n",
86	      g_uiAuthDataLen, g_pAuthData);
87    }
88#endif
89
90  /* Allocate structure for additional auth information */
91  pAuth = (SecurityAuthorizationPtr)
92    xalloc (sizeof (SecurityAuthorizationRec));
93  if (!(pAuth))
94    {
95      ErrorF ("winGenerateAuthorization - Failed allocating "
96	      "SecurityAuthorizationPtr.\n");
97      goto auth_bailout;
98    }
99
100  /* Fill in the auth fields */
101  pAuth->id = g_authId;
102  pAuth->timeout = 0; /* live for x seconds after refcnt == 0 */
103  pAuth->group = None;
104  pAuth->trustLevel = XSecurityClientTrusted;
105  pAuth->refcnt = 1; /* this auth must stick around */
106  pAuth->secondsRemaining = 0;
107  pAuth->timer = NULL;
108  pAuth->eventClients = NULL;
109
110  /* Add the authorization to the server's auth list */
111  if (!AddResource (g_authId,
112		    SecurityAuthorizationResType,
113		    pAuth))
114    {
115      ErrorF ("winGenerateAuthorization - AddResource failed for auth.\n");
116      fFreeAuth = TRUE;
117      goto auth_bailout;
118    }
119
120  /* Don't free the auth data, since it is still used internally */
121  pAuth = NULL;
122
123  return TRUE;
124
125 auth_bailout:
126  if (fFreeAuth)
127    xfree (pAuth);
128
129  return FALSE;
130}
131#endif
132