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