105b261ecSmrg/*
205b261ecSmrg *Copyright (C) 2003-2004 Harold L Hunt II All Rights Reserved.
305b261ecSmrg *
405b261ecSmrg *Permission is hereby granted, free of charge, to any person obtaining
505b261ecSmrg * a copy of this software and associated documentation files (the
605b261ecSmrg *"Software"), to deal in the Software without restriction, including
705b261ecSmrg *without limitation the rights to use, copy, modify, merge, publish,
805b261ecSmrg *distribute, sublicense, and/or sell copies of the Software, and to
905b261ecSmrg *permit persons to whom the Software is furnished to do so, subject to
1005b261ecSmrg *the following conditions:
1105b261ecSmrg *
1205b261ecSmrg *The above copyright notice and this permission notice shall be
1305b261ecSmrg *included in all copies or substantial portions of the Software.
1405b261ecSmrg *
1505b261ecSmrg *THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
1605b261ecSmrg *EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
1705b261ecSmrg *MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
1805b261ecSmrg *NONINFRINGEMENT. IN NO EVENT SHALL HAROLD L HUNT II BE LIABLE FOR
1905b261ecSmrg *ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
2005b261ecSmrg *CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
2105b261ecSmrg *WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2205b261ecSmrg *
2305b261ecSmrg *Except as contained in this notice, the name of Harold L Hunt II
2405b261ecSmrg *shall not be used in advertising or otherwise to promote the sale, use
2505b261ecSmrg *or other dealings in this Software without prior written authorization
2605b261ecSmrg *from Harold L Hunt II.
2705b261ecSmrg *
2805b261ecSmrg * Authors:	Harold L Hunt II
2905b261ecSmrg */
3005b261ecSmrg
314202a189Smrg#ifdef HAVE_XWIN_CONFIG_H
324202a189Smrg#include <xwin-config.h>
334202a189Smrg#endif
344202a189Smrg
355a112b11Smrg#include "winauth.h"
365a112b11Smrg#include "winmsg.h"
3705b261ecSmrg
3805b261ecSmrg/* Includes for authorization */
3905b261ecSmrg#include "securitysrv.h"
40f7df2e56Smrg#include "os/osdep.h"
4105b261ecSmrg
427e31ba66Smrg#include <xcb/xcb.h>
437e31ba66Smrg
4405b261ecSmrg/*
4505b261ecSmrg * Constants
4605b261ecSmrg */
4705b261ecSmrg
4805b261ecSmrg#define AUTH_NAME	"MIT-MAGIC-COOKIE-1"
4905b261ecSmrg
5005b261ecSmrg/*
514202a189Smrg * Locals
524202a189Smrg */
534202a189Smrg
544202a189Smrgstatic XID g_authId = 0;
554202a189Smrgstatic unsigned int g_uiAuthDataLen = 0;
564202a189Smrgstatic char *g_pAuthData = NULL;
577e31ba66Smrgstatic xcb_auth_info_t auth_info;
584202a189Smrg
594202a189Smrg/*
604202a189Smrg * Code to generate a MIT-MAGIC-COOKIE-1, copied from under XCSECURITY
6105b261ecSmrg */
6205b261ecSmrg
634202a189Smrg#ifndef XCSECURITY
645a112b11Smrgstatic XID
65f7df2e56SmrgGenerateAuthorization(unsigned name_length,
66f7df2e56Smrg                      const char *name,
67f7df2e56Smrg                      unsigned data_length,
68f7df2e56Smrg                      const char *data,
69f7df2e56Smrg                      unsigned *data_length_return, char **data_return)
704202a189Smrg{
714202a189Smrg    return MitGenerateCookie(data_length, data,
724202a189Smrg                             FakeClientID(0), data_length_return, data_return);
734202a189Smrg}
744202a189Smrg#endif
7505b261ecSmrg
7605b261ecSmrg/*
7705b261ecSmrg * Generate authorization cookie for internal server clients
7805b261ecSmrg */
7905b261ecSmrg
805a112b11SmrgBOOL
81f7df2e56SmrgwinGenerateAuthorization(void)
8205b261ecSmrg{
835a112b11Smrg#ifdef XCSECURITY
84f7df2e56Smrg    SecurityAuthorizationPtr pAuth = NULL;
855a112b11Smrg#endif
86f7df2e56Smrg
87f7df2e56Smrg    /* Call OS layer to generate authorization key */
88f7df2e56Smrg    g_authId = GenerateAuthorization(strlen(AUTH_NAME),
89f7df2e56Smrg                                     AUTH_NAME,
90f7df2e56Smrg                                     0, NULL, &g_uiAuthDataLen, &g_pAuthData);
91f7df2e56Smrg    if ((XID) ~0L == g_authId) {
92f7df2e56Smrg        ErrorF("winGenerateAuthorization - GenerateAuthorization failed\n");
93f7df2e56Smrg        return FALSE;
9405b261ecSmrg    }
954202a189Smrg
96f7df2e56Smrg    else {
97f7df2e56Smrg        winDebug("winGenerateAuthorization - GenerateAuthorization success!\n"
98f7df2e56Smrg                 "AuthDataLen: %d AuthData: %s\n",
99f7df2e56Smrg                 g_uiAuthDataLen, g_pAuthData);
10005b261ecSmrg    }
1014202a189Smrg
1025a112b11Smrg    auth_info.name = strdup(AUTH_NAME);
1037e31ba66Smrg    auth_info.namelen = strlen(AUTH_NAME);
1047e31ba66Smrg    auth_info.data = g_pAuthData;
1057e31ba66Smrg    auth_info.datalen = g_uiAuthDataLen;
1067e31ba66Smrg
1074202a189Smrg#ifdef XCSECURITY
108f7df2e56Smrg    /* Allocate structure for additional auth information */
109f7df2e56Smrg    pAuth = (SecurityAuthorizationPtr)
110f7df2e56Smrg        malloc(sizeof(SecurityAuthorizationRec));
111f7df2e56Smrg    if (!(pAuth)) {
112f7df2e56Smrg        ErrorF("winGenerateAuthorization - Failed allocating "
113f7df2e56Smrg               "SecurityAuthorizationPtr.\n");
114f7df2e56Smrg        return FALSE;
11505b261ecSmrg    }
116f7df2e56Smrg
117f7df2e56Smrg    /* Fill in the auth fields */
118f7df2e56Smrg    pAuth->id = g_authId;
119f7df2e56Smrg    pAuth->timeout = 0;         /* live for x seconds after refcnt == 0 */
120f7df2e56Smrg    pAuth->group = None;
121f7df2e56Smrg    pAuth->trustLevel = XSecurityClientTrusted;
122f7df2e56Smrg    pAuth->refcnt = 1;          /* this auth must stick around */
123f7df2e56Smrg    pAuth->secondsRemaining = 0;
124f7df2e56Smrg    pAuth->timer = NULL;
125f7df2e56Smrg    pAuth->eventClients = NULL;
126f7df2e56Smrg
127f7df2e56Smrg    /* Add the authorization to the server's auth list */
128f7df2e56Smrg    if (!AddResource(g_authId, SecurityAuthorizationResType, pAuth)) {
129f7df2e56Smrg        ErrorF("winGenerateAuthorization - AddResource failed for auth.\n");
130f7df2e56Smrg        return FALSE;
13105b261ecSmrg    }
1324202a189Smrg#endif
1334202a189Smrg
134f7df2e56Smrg    return TRUE;
13505b261ecSmrg}
1364202a189Smrg
1377e31ba66Smrgxcb_auth_info_t *
1387e31ba66SmrgwinGetXcbAuthInfo(void)
1397e31ba66Smrg{
1407e31ba66Smrg    if (g_pAuthData)
1417e31ba66Smrg        return &auth_info;
1427e31ba66Smrg
1437e31ba66Smrg    return NULL;
1447e31ba66Smrg}
145