1 /*
2  *Copyright (C) 2003-2004 Harold L Hunt II All Rights Reserved.
3  *
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:
11  *
12  *The above copyright notice and this permission notice shall be
13  *included in all copies or substantial portions of the Software.
14  *
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.
22  *
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.
27  *
28  * Authors:	Harold L Hunt II
29  */
30 
31 #ifdef HAVE_XWIN_CONFIG_H
32 #include <xwin-config.h>
33 #endif
34 
35 #include "win.h"
36 
37 /* Includes for authorization */
38 #include "securitysrv.h"
39 #include "os/osdep.h"
40 
41 #include <xcb/xcb.h>
42 
43 /* Need to get this from Xlib.h */
44 extern void XSetAuthorization(
45     const char *                /* name */,
46     int                         /* namelen */,
47     const char *                /* data */,
48     int                         /* datalen */
49 );
50 
51 /*
52  * Constants
53  */
54 
55 #define AUTH_NAME	"MIT-MAGIC-COOKIE-1"
56 
57 /*
58  * Locals
59  */
60 
61 static XID g_authId = 0;
62 static unsigned int g_uiAuthDataLen = 0;
63 static char *g_pAuthData = NULL;
64 static xcb_auth_info_t auth_info;
65 
66 /*
67  * Code to generate a MIT-MAGIC-COOKIE-1, copied from under XCSECURITY
68  */
69 
70 #ifndef XCSECURITY
71 
72 static
73     XID
GenerateAuthorization(unsigned name_length,const char * name,unsigned data_length,const char * data,unsigned * data_length_return,char ** data_return)74 GenerateAuthorization(unsigned name_length,
75                       const char *name,
76                       unsigned data_length,
77                       const char *data,
78                       unsigned *data_length_return, char **data_return)
79 {
80     return MitGenerateCookie(data_length, data,
81                              FakeClientID(0), data_length_return, data_return);
82 }
83 #endif
84 
85 /*
86  * Generate authorization cookie for internal server clients
87  */
88 
89 Bool
winGenerateAuthorization(void)90 winGenerateAuthorization(void)
91 {
92     SecurityAuthorizationPtr pAuth = NULL;
93 
94     /* Call OS layer to generate authorization key */
95     g_authId = GenerateAuthorization(strlen(AUTH_NAME),
96                                      AUTH_NAME,
97                                      0, NULL, &g_uiAuthDataLen, &g_pAuthData);
98     if ((XID) ~0L == g_authId) {
99         ErrorF("winGenerateAuthorization - GenerateAuthorization failed\n");
100         return FALSE;
101     }
102 
103     else {
104         winDebug("winGenerateAuthorization - GenerateAuthorization success!\n"
105                  "AuthDataLen: %d AuthData: %s\n",
106                  g_uiAuthDataLen, g_pAuthData);
107     }
108 
109     auth_info.name = AUTH_NAME;
110     auth_info.namelen = strlen(AUTH_NAME);
111     auth_info.data = g_pAuthData;
112     auth_info.datalen = g_uiAuthDataLen;
113 
114 #ifdef XCSECURITY
115     /* Allocate structure for additional auth information */
116     pAuth = (SecurityAuthorizationPtr)
117         malloc(sizeof(SecurityAuthorizationRec));
118     if (!(pAuth)) {
119         ErrorF("winGenerateAuthorization - Failed allocating "
120                "SecurityAuthorizationPtr.\n");
121         return FALSE;
122     }
123 
124     /* Fill in the auth fields */
125     pAuth->id = g_authId;
126     pAuth->timeout = 0;         /* live for x seconds after refcnt == 0 */
127     pAuth->group = None;
128     pAuth->trustLevel = XSecurityClientTrusted;
129     pAuth->refcnt = 1;          /* this auth must stick around */
130     pAuth->secondsRemaining = 0;
131     pAuth->timer = NULL;
132     pAuth->eventClients = NULL;
133 
134     /* Add the authorization to the server's auth list */
135     if (!AddResource(g_authId, SecurityAuthorizationResType, pAuth)) {
136         ErrorF("winGenerateAuthorization - AddResource failed for auth.\n");
137         return FALSE;
138     }
139 #endif
140 
141     return TRUE;
142 }
143 
144 /* Use our generated cookie for authentication */
145 void
winSetAuthorization(void)146 winSetAuthorization(void)
147 {
148     XSetAuthorization(AUTH_NAME,
149                       strlen(AUTH_NAME), g_pAuthData, g_uiAuthDataLen);
150 }
151 
152 xcb_auth_info_t *
winGetXcbAuthInfo(void)153 winGetXcbAuthInfo(void)
154 {
155     if (g_pAuthData)
156         return &auth_info;
157 
158     return NULL;
159 }
160