winauth.c revision 7e31ba66
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
3505b261ecSmrg#include "win.h"
3605b261ecSmrg
3705b261ecSmrg/* Includes for authorization */
3805b261ecSmrg#include "securitysrv.h"
39f7df2e56Smrg#include "os/osdep.h"
4005b261ecSmrg
417e31ba66Smrg#include <xcb/xcb.h>
427e31ba66Smrg
437e31ba66Smrg/* Need to get this from Xlib.h */
447e31ba66Smrgextern void XSetAuthorization(
457e31ba66Smrg    const char *                /* name */,
467e31ba66Smrg    int                         /* namelen */,
477e31ba66Smrg    const char *                /* data */,
487e31ba66Smrg    int                         /* datalen */
497e31ba66Smrg);
507e31ba66Smrg
5105b261ecSmrg/*
5205b261ecSmrg * Constants
5305b261ecSmrg */
5405b261ecSmrg
5505b261ecSmrg#define AUTH_NAME	"MIT-MAGIC-COOKIE-1"
5605b261ecSmrg
5705b261ecSmrg/*
584202a189Smrg * Locals
594202a189Smrg */
604202a189Smrg
614202a189Smrgstatic XID g_authId = 0;
624202a189Smrgstatic unsigned int g_uiAuthDataLen = 0;
634202a189Smrgstatic char *g_pAuthData = NULL;
647e31ba66Smrgstatic xcb_auth_info_t auth_info;
654202a189Smrg
664202a189Smrg/*
674202a189Smrg * Code to generate a MIT-MAGIC-COOKIE-1, copied from under XCSECURITY
6805b261ecSmrg */
6905b261ecSmrg
704202a189Smrg#ifndef XCSECURITY
7105b261ecSmrg
724202a189Smrgstatic
73f7df2e56Smrg    XID
74f7df2e56SmrgGenerateAuthorization(unsigned name_length,
75f7df2e56Smrg                      const char *name,
76f7df2e56Smrg                      unsigned data_length,
77f7df2e56Smrg                      const char *data,
78f7df2e56Smrg                      unsigned *data_length_return, char **data_return)
794202a189Smrg{
804202a189Smrg    return MitGenerateCookie(data_length, data,
814202a189Smrg                             FakeClientID(0), data_length_return, data_return);
824202a189Smrg}
834202a189Smrg#endif
8405b261ecSmrg
8505b261ecSmrg/*
8605b261ecSmrg * Generate authorization cookie for internal server clients
8705b261ecSmrg */
8805b261ecSmrg
8905b261ecSmrgBool
90f7df2e56SmrgwinGenerateAuthorization(void)
9105b261ecSmrg{
92f7df2e56Smrg    SecurityAuthorizationPtr pAuth = NULL;
93f7df2e56Smrg
94f7df2e56Smrg    /* Call OS layer to generate authorization key */
95f7df2e56Smrg    g_authId = GenerateAuthorization(strlen(AUTH_NAME),
96f7df2e56Smrg                                     AUTH_NAME,
97f7df2e56Smrg                                     0, NULL, &g_uiAuthDataLen, &g_pAuthData);
98f7df2e56Smrg    if ((XID) ~0L == g_authId) {
99f7df2e56Smrg        ErrorF("winGenerateAuthorization - GenerateAuthorization failed\n");
100f7df2e56Smrg        return FALSE;
10105b261ecSmrg    }
1024202a189Smrg
103f7df2e56Smrg    else {
104f7df2e56Smrg        winDebug("winGenerateAuthorization - GenerateAuthorization success!\n"
105f7df2e56Smrg                 "AuthDataLen: %d AuthData: %s\n",
106f7df2e56Smrg                 g_uiAuthDataLen, g_pAuthData);
10705b261ecSmrg    }
1084202a189Smrg
1097e31ba66Smrg    auth_info.name = AUTH_NAME;
1107e31ba66Smrg    auth_info.namelen = strlen(AUTH_NAME);
1117e31ba66Smrg    auth_info.data = g_pAuthData;
1127e31ba66Smrg    auth_info.datalen = g_uiAuthDataLen;
1137e31ba66Smrg
1144202a189Smrg#ifdef XCSECURITY
115f7df2e56Smrg    /* Allocate structure for additional auth information */
116f7df2e56Smrg    pAuth = (SecurityAuthorizationPtr)
117f7df2e56Smrg        malloc(sizeof(SecurityAuthorizationRec));
118f7df2e56Smrg    if (!(pAuth)) {
119f7df2e56Smrg        ErrorF("winGenerateAuthorization - Failed allocating "
120f7df2e56Smrg               "SecurityAuthorizationPtr.\n");
121f7df2e56Smrg        return FALSE;
12205b261ecSmrg    }
123f7df2e56Smrg
124f7df2e56Smrg    /* Fill in the auth fields */
125f7df2e56Smrg    pAuth->id = g_authId;
126f7df2e56Smrg    pAuth->timeout = 0;         /* live for x seconds after refcnt == 0 */
127f7df2e56Smrg    pAuth->group = None;
128f7df2e56Smrg    pAuth->trustLevel = XSecurityClientTrusted;
129f7df2e56Smrg    pAuth->refcnt = 1;          /* this auth must stick around */
130f7df2e56Smrg    pAuth->secondsRemaining = 0;
131f7df2e56Smrg    pAuth->timer = NULL;
132f7df2e56Smrg    pAuth->eventClients = NULL;
133f7df2e56Smrg
134f7df2e56Smrg    /* Add the authorization to the server's auth list */
135f7df2e56Smrg    if (!AddResource(g_authId, SecurityAuthorizationResType, pAuth)) {
136f7df2e56Smrg        ErrorF("winGenerateAuthorization - AddResource failed for auth.\n");
137f7df2e56Smrg        return FALSE;
13805b261ecSmrg    }
1394202a189Smrg#endif
1404202a189Smrg
141f7df2e56Smrg    return TRUE;
14205b261ecSmrg}
1434202a189Smrg
1444202a189Smrg/* Use our generated cookie for authentication */
1454202a189Smrgvoid
1464202a189SmrgwinSetAuthorization(void)
1474202a189Smrg{
148f7df2e56Smrg    XSetAuthorization(AUTH_NAME,
149f7df2e56Smrg                      strlen(AUTH_NAME), g_pAuthData, g_uiAuthDataLen);
1504202a189Smrg}
1517e31ba66Smrg
1527e31ba66Smrgxcb_auth_info_t *
1537e31ba66SmrgwinGetXcbAuthInfo(void)
1547e31ba66Smrg{
1557e31ba66Smrg    if (g_pAuthData)
1567e31ba66Smrg        return &auth_info;
1577e31ba66Smrg
1587e31ba66Smrg    return NULL;
1597e31ba66Smrg}
160