iceauth.c revision 266e564d
1/* $Xorg: iceauth.c,v 1.4 2001/02/09 02:03:26 xorgcvs Exp $ */
2/******************************************************************************
3
4
5Copyright 1993, 1998  The Open Group
6
7Permission to use, copy, modify, distribute, and sell this software and its
8documentation for any purpose is hereby granted without fee, provided that
9the above copyright notice appear in all copies and that both that
10copyright notice and this permission notice appear in supporting
11documentation.
12
13The above copyright notice and this permission notice shall be included in
14all copies or substantial portions of the Software.
15
16THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
19OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
20AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
23Except as contained in this notice, the name of The Open Group shall not be
24used in advertising or otherwise to promote the sale, use or other dealings
25in this Software without prior written authorization from The Open Group.
26
27Author: Ralph Mor, X Consortium
28******************************************************************************/
29/* $XFree86: xc/lib/ICE/iceauth.c,v 3.5 2001/12/14 19:53:36 dawes Exp $ */
30
31#ifdef HAVE_CONFIG_H
32#include <config.h>
33#endif
34#include <X11/ICE/ICElib.h>
35#include "ICElibint.h"
36#include <X11/ICE/ICEutil.h>
37
38#include <time.h>
39#define Time_t time_t
40
41static int binaryEqual (const char *a, const char *b, unsigned len);
42
43static int was_called_state;
44
45/*
46 * MIT-MAGIC-COOKIE-1 is a sample authentication method implemented by
47 * the SI.  It is not part of standard ICElib.
48 */
49
50
51char *
52IceGenerateMagicCookie (len)
53
54int len;
55
56{
57    char    *auth;
58    long    ldata[2];
59    int	    seed;
60    int	    value;
61    int	    i;
62
63    if ((auth = (char *) malloc (len + 1)) == NULL)
64	return (NULL);
65
66#ifdef ITIMER_REAL
67    {
68	struct timeval  now;
69	X_GETTIMEOFDAY (&now);
70	ldata[0] = now.tv_sec;
71	ldata[1] = now.tv_usec;
72    }
73#else
74    {
75#ifndef __UNIXOS2__
76	long    time ();
77#endif
78	ldata[0] = time ((long *) 0);
79	ldata[1] = getpid ();
80    }
81#endif
82    seed = (ldata[0]) + (ldata[1] << 16);
83    srand (seed);
84    for (i = 0; i < len; i++)
85    {
86	value = rand ();
87	auth[i] = value & 0xff;
88    }
89    auth[len] = '\0';
90
91    return (auth);
92}
93
94
95
96IcePoAuthStatus
97_IcePoMagicCookie1Proc (iceConn, authStatePtr, cleanUp, swap,
98    authDataLen, authData, replyDataLenRet, replyDataRet, errorStringRet)
99
100IceConn		iceConn;
101IcePointer	*authStatePtr;
102Bool 		cleanUp;
103Bool		swap;
104int     	authDataLen;
105IcePointer	authData;
106int 		*replyDataLenRet;
107IcePointer	*replyDataRet;
108char    	**errorStringRet;
109
110{
111    if (cleanUp)
112    {
113	/*
114	 * We didn't allocate any state.  We're done.
115	 */
116
117	return (IcePoAuthDoneCleanup);
118    }
119
120    *errorStringRet = NULL;
121
122    if (*authStatePtr == NULL)
123    {
124	/*
125	 * This is the first time we're being called.  Search the
126	 * authentication data for the first occurence of
127	 * MIT-MAGIC-COOKIE-1 that matches iceConn->connection_string.
128	 */
129
130	unsigned short  length;
131	char		*data;
132
133	_IceGetPoAuthData ("ICE", iceConn->connection_string,
134	    "MIT-MAGIC-COOKIE-1", &length, &data);
135
136	if (!data)
137	{
138	    const char *tempstr =
139		"Could not find correct MIT-MAGIC-COOKIE-1 authentication";
140
141	    *errorStringRet = strdup(tempstr);
142
143	    return (IcePoAuthFailed);
144	}
145	else
146	{
147	    *authStatePtr = (IcePointer) &was_called_state;
148
149	    *replyDataLenRet = length;
150	    *replyDataRet = data;
151
152	    return (IcePoAuthHaveReply);
153	}
154    }
155    else
156    {
157	/*
158	 * We should never get here for MIT-MAGIC-COOKIE-1 since it is
159	 * a single pass authentication method.
160	 */
161
162	const char *tempstr =
163	    "MIT-MAGIC-COOKIE-1 authentication internal error";
164
165	*errorStringRet = strdup(tempstr);
166
167	return (IcePoAuthFailed);
168    }
169}
170
171
172
173IcePaAuthStatus
174_IcePaMagicCookie1Proc (iceConn, authStatePtr, swap,
175    authDataLen, authData, replyDataLenRet, replyDataRet, errorStringRet)
176
177IceConn		iceConn;
178IcePointer	*authStatePtr;
179Bool		swap;
180int     	authDataLen;
181IcePointer	authData;
182int 		*replyDataLenRet;
183IcePointer	*replyDataRet;
184char    	**errorStringRet;
185
186{
187    *errorStringRet = NULL;
188    *replyDataLenRet = 0;
189    *replyDataRet = NULL;
190
191    if (*authStatePtr == NULL)
192    {
193	/*
194	 * This is the first time we're being called.  We don't have
195	 * any data to pass to the other client.
196	 */
197
198	*authStatePtr = (IcePointer) &was_called_state;
199
200	return (IcePaAuthContinue);
201    }
202    else
203    {
204	/*
205	 * Search the authentication data for the first occurence of
206	 * MIT-MAGIC-COOKIE-1 that matches iceConn->connection_string.
207	 */
208
209	unsigned short  length;
210	char		*data;
211
212	_IceGetPaAuthData ("ICE", iceConn->connection_string,
213	    "MIT-MAGIC-COOKIE-1", &length, &data);
214
215	if (data)
216	{
217	    IcePaAuthStatus stat;
218
219	    if (authDataLen == length &&
220	        binaryEqual ((char *) authData, data, authDataLen))
221	    {
222		stat = IcePaAuthAccepted;
223	    }
224	    else
225	    {
226		const char *tempstr
227		    = "MIT-MAGIC-COOKIE-1 authentication rejected";
228
229		*errorStringRet = strdup(tempstr);
230
231		stat = IcePaAuthRejected;
232	    }
233
234	    free (data);
235	    return (stat);
236	}
237	else
238	{
239	    /*
240	     * We should never get here because in the ConnectionReply
241	     * we should have passed all the valid methods.  So we should
242	     * always find a valid entry.
243	     */
244
245	    const char *tempstr =
246		"MIT-MAGIC-COOKIE-1 authentication internal error";
247
248	    *errorStringRet = strdup(tempstr);
249
250	    return (IcePaAuthFailed);
251	}
252    }
253}
254
255
256
257/*
258 * local routines
259 */
260
261static int
262binaryEqual (const char *a, const char *b, unsigned len)
263
264{
265    while (len--)
266	if (*a++ != *b++)
267	    return 0;
268    return 1;
269}
270