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 "winauth.h"
36 #include "winmsg.h"
37 
38 /* Includes for authorization */
39 #include "securitysrv.h"
40 #include "os/osdep.h"
41 
42 #include <xcb/xcb.h>
43 
44 /*
45  * Constants
46  */
47 
48 #define AUTH_NAME	"MIT-MAGIC-COOKIE-1"
49 
50 /*
51  * Locals
52  */
53 
54 static XID g_authId = 0;
55 static unsigned int g_uiAuthDataLen = 0;
56 static char *g_pAuthData = NULL;
57 static xcb_auth_info_t auth_info;
58 
59 /*
60  * Code to generate a MIT-MAGIC-COOKIE-1, copied from under XCSECURITY
61  */
62 
63 #ifndef XCSECURITY
64 static XID
GenerateAuthorization(unsigned name_length,const char * name,unsigned data_length,const char * data,unsigned * data_length_return,char ** data_return)65 GenerateAuthorization(unsigned name_length,
66                       const char *name,
67                       unsigned data_length,
68                       const char *data,
69                       unsigned *data_length_return, char **data_return)
70 {
71     return MitGenerateCookie(data_length, data,
72                              FakeClientID(0), data_length_return, data_return);
73 }
74 #endif
75 
76 /*
77  * Generate authorization cookie for internal server clients
78  */
79 
80 BOOL
winGenerateAuthorization(void)81 winGenerateAuthorization(void)
82 {
83 #ifdef XCSECURITY
84     SecurityAuthorizationPtr pAuth = NULL;
85 #endif
86 
87     /* Call OS layer to generate authorization key */
88     g_authId = GenerateAuthorization(strlen(AUTH_NAME),
89                                      AUTH_NAME,
90                                      0, NULL, &g_uiAuthDataLen, &g_pAuthData);
91     if ((XID) ~0L == g_authId) {
92         ErrorF("winGenerateAuthorization - GenerateAuthorization failed\n");
93         return FALSE;
94     }
95 
96     else {
97         winDebug("winGenerateAuthorization - GenerateAuthorization success!\n"
98                  "AuthDataLen: %d AuthData: %s\n",
99                  g_uiAuthDataLen, g_pAuthData);
100     }
101 
102     auth_info.name = strdup(AUTH_NAME);
103     auth_info.namelen = strlen(AUTH_NAME);
104     auth_info.data = g_pAuthData;
105     auth_info.datalen = g_uiAuthDataLen;
106 
107 #ifdef XCSECURITY
108     /* Allocate structure for additional auth information */
109     pAuth = (SecurityAuthorizationPtr)
110         malloc(sizeof(SecurityAuthorizationRec));
111     if (!(pAuth)) {
112         ErrorF("winGenerateAuthorization - Failed allocating "
113                "SecurityAuthorizationPtr.\n");
114         return FALSE;
115     }
116 
117     /* Fill in the auth fields */
118     pAuth->id = g_authId;
119     pAuth->timeout = 0;         /* live for x seconds after refcnt == 0 */
120     pAuth->group = None;
121     pAuth->trustLevel = XSecurityClientTrusted;
122     pAuth->refcnt = 1;          /* this auth must stick around */
123     pAuth->secondsRemaining = 0;
124     pAuth->timer = NULL;
125     pAuth->eventClients = NULL;
126 
127     /* Add the authorization to the server's auth list */
128     if (!AddResource(g_authId, SecurityAuthorizationResType, pAuth)) {
129         ErrorF("winGenerateAuthorization - AddResource failed for auth.\n");
130         return FALSE;
131     }
132 #endif
133 
134     return TRUE;
135 }
136 
137 xcb_auth_info_t *
winGetXcbAuthInfo(void)138 winGetXcbAuthInfo(void)
139 {
140     if (g_pAuthData)
141         return &auth_info;
142 
143     return NULL;
144 }
145