2 *Copyright (C) 2003-2004 Harold L Hunt II All Rights Reserved.
4 *Permission is hereby granted, free of charge, to any person obtaining
5 * a copy of this software and associated documentation files (the
6 *"Software"), to deal in the Software without restriction, including
7 *without limitation the rights to use, copy, modify, merge, publish,
8 *distribute, sublicense, and/or sell copies of the Software, and to
9 *permit persons to whom the Software is furnished to do so, subject to
10 *the following conditions:
12 *The above copyright notice and this permission notice shall be
13 *included in all copies or substantial portions of the Software.
15 *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 *NONINFRINGEMENT. IN NO EVENT SHALL HAROLD L HUNT II BE LIABLE FOR
19 *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
20 *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21 *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23 *Except as contained in this notice, the name of Harold L Hunt II
24 *shall not be used in advertising or otherwise to promote the sale, use
25 *or other dealings in this Software without prior written authorization
26 *from Harold L Hunt II.
28 * Authors: Harold L Hunt II
31 #ifdef HAVE_XWIN_CONFIG_H
32 #include <xwin-config.h>
37 /* Includes for authorization */
38 #include "securitysrv.h"
45 #define AUTH_NAME "MIT-MAGIC-COOKIE-1"
51 static XID g_authId
= 0;
52 static unsigned int g_uiAuthDataLen
= 0;
53 static char *g_pAuthData
= NULL
;
56 * Code to generate a MIT-MAGIC-COOKIE-1, copied from under XCSECURITY
61 GenerateRandomData(int len
, char *buf
)
65 fd
= open("/dev/urandom", O_RDONLY
);
70 static char cookie
[16]; /* 128 bits */
73 MitGenerateCookie(unsigned data_length
,
75 XID id
, unsigned *data_length_return
, char **data_return
)
80 while (data_length
--) {
81 cookie
[i
++] += *data
++;
82 if (i
>= sizeof(cookie
))
85 GenerateRandomData(sizeof(cookie
), cookie
);
86 status
= MitAddCookie(sizeof(cookie
), cookie
, id
);
91 *data_return
= cookie
;
92 *data_length_return
= sizeof(cookie
);
99 GenerateAuthorization(unsigned name_length
,
101 unsigned data_length
,
103 unsigned *data_length_return
, char **data_return
)
105 return MitGenerateCookie(data_length
, data
,
106 FakeClientID(0), data_length_return
, data_return
);
111 * Generate authorization cookie for internal server clients
115 winGenerateAuthorization(void)
117 Bool fFreeAuth
= FALSE
;
118 SecurityAuthorizationPtr pAuth
= NULL
;
120 /* Call OS layer to generate authorization key */
121 g_authId
= GenerateAuthorization(strlen(AUTH_NAME
),
123 0, NULL
, &g_uiAuthDataLen
, &g_pAuthData
);
124 if ((XID
) ~0L == g_authId
) {
125 ErrorF("winGenerateAuthorization - GenerateAuthorization failed\n");
130 winDebug("winGenerateAuthorization - GenerateAuthorization success!\n"
131 "AuthDataLen: %d AuthData: %s\n",
132 g_uiAuthDataLen
, g_pAuthData
);
136 /* Allocate structure for additional auth information */
137 pAuth
= (SecurityAuthorizationPtr
)
138 malloc(sizeof(SecurityAuthorizationRec
));
140 ErrorF("winGenerateAuthorization - Failed allocating "
141 "SecurityAuthorizationPtr.\n");
145 /* Fill in the auth fields */
146 pAuth
->id
= g_authId
;
147 pAuth
->timeout
= 0; /* live for x seconds after refcnt == 0 */
149 pAuth
->trustLevel
= XSecurityClientTrusted
;
150 pAuth
->refcnt
= 1; /* this auth must stick around */
151 pAuth
->secondsRemaining
= 0;
153 pAuth
->eventClients
= NULL
;
155 /* Add the authorization to the server's auth list */
156 if (!AddResource(g_authId
, SecurityAuthorizationResType
, pAuth
)) {
157 ErrorF("winGenerateAuthorization - AddResource failed for auth.\n");
162 /* Don't free the auth data, since it is still used internally */
175 /* Use our generated cookie for authentication */
177 winSetAuthorization(void)
179 XSetAuthorization(AUTH_NAME
,
180 strlen(AUTH_NAME
), g_pAuthData
, g_uiAuthDataLen
);