winauth.c revision f7df2e56
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/*
42 * Constants
43 */
44
45#define AUTH_NAME	"MIT-MAGIC-COOKIE-1"
46
47/*
48 * Locals
49 */
50
51static XID g_authId = 0;
52static unsigned int g_uiAuthDataLen = 0;
53static char *g_pAuthData = NULL;
54
55/*
56 * Code to generate a MIT-MAGIC-COOKIE-1, copied from under XCSECURITY
57 */
58
59#ifndef XCSECURITY
60void
61GenerateRandomData(int len, char *buf)
62{
63    int fd;
64
65    fd = open("/dev/urandom", O_RDONLY);
66    read(fd, buf, len);
67    close(fd);
68}
69
70static char cookie[16];         /* 128 bits */
71
72XID
73MitGenerateCookie(unsigned data_length,
74                  const char *data,
75                  XID id, unsigned *data_length_return, char **data_return)
76{
77    int i = 0;
78    int status;
79
80    while (data_length--) {
81        cookie[i++] += *data++;
82        if (i >= sizeof(cookie))
83            i = 0;
84    }
85    GenerateRandomData(sizeof(cookie), cookie);
86    status = MitAddCookie(sizeof(cookie), cookie, id);
87    if (!status) {
88        id = -1;
89    }
90    else {
91        *data_return = cookie;
92        *data_length_return = sizeof(cookie);
93    }
94    return id;
95}
96
97static
98    XID
99GenerateAuthorization(unsigned name_length,
100                      const char *name,
101                      unsigned data_length,
102                      const char *data,
103                      unsigned *data_length_return, char **data_return)
104{
105    return MitGenerateCookie(data_length, data,
106                             FakeClientID(0), data_length_return, data_return);
107}
108#endif
109
110/*
111 * Generate authorization cookie for internal server clients
112 */
113
114Bool
115winGenerateAuthorization(void)
116{
117    SecurityAuthorizationPtr pAuth = NULL;
118
119    /* Call OS layer to generate authorization key */
120    g_authId = GenerateAuthorization(strlen(AUTH_NAME),
121                                     AUTH_NAME,
122                                     0, NULL, &g_uiAuthDataLen, &g_pAuthData);
123    if ((XID) ~0L == g_authId) {
124        ErrorF("winGenerateAuthorization - GenerateAuthorization failed\n");
125        return FALSE;
126    }
127
128    else {
129        winDebug("winGenerateAuthorization - GenerateAuthorization success!\n"
130                 "AuthDataLen: %d AuthData: %s\n",
131                 g_uiAuthDataLen, g_pAuthData);
132    }
133
134#ifdef XCSECURITY
135    /* Allocate structure for additional auth information */
136    pAuth = (SecurityAuthorizationPtr)
137        malloc(sizeof(SecurityAuthorizationRec));
138    if (!(pAuth)) {
139        ErrorF("winGenerateAuthorization - Failed allocating "
140               "SecurityAuthorizationPtr.\n");
141        return FALSE;
142    }
143
144    /* Fill in the auth fields */
145    pAuth->id = g_authId;
146    pAuth->timeout = 0;         /* live for x seconds after refcnt == 0 */
147    pAuth->group = None;
148    pAuth->trustLevel = XSecurityClientTrusted;
149    pAuth->refcnt = 1;          /* this auth must stick around */
150    pAuth->secondsRemaining = 0;
151    pAuth->timer = NULL;
152    pAuth->eventClients = NULL;
153
154    /* Add the authorization to the server's auth list */
155    if (!AddResource(g_authId, SecurityAuthorizationResType, pAuth)) {
156        ErrorF("winGenerateAuthorization - AddResource failed for auth.\n");
157        return FALSE;
158    }
159#endif
160
161    return TRUE;
162}
163
164/* Use our generated cookie for authentication */
165void
166winSetAuthorization(void)
167{
168    XSetAuthorization(AUTH_NAME,
169                      strlen(AUTH_NAME), g_pAuthData, g_uiAuthDataLen);
170}
171