2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
6 * These libraries and programs are free software; you can
7 * redistribute them and/or modify them under the terms of the GNU
8 * Lesser General Public License as published by the Free Software
9 * Foundation; either version 2 of the License, or (at your option)
12 * These libraries and programs are distributed in the hope that
13 * they will be useful, but WITHOUT ANY WARRANTY; without even the
14 * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15 * PURPOSE. See the GNU Lesser General Public License for more
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with these libraries and programs; if not, write
20 * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21 * Floor, Boston, MA 02110-1301 USA
23 /* $TOG: xdmauth.c /main/4 1997/03/14 13:45:35 barstow $ */
24 /* (c) Copyright 1997 The Open Group */
26 * (c) Copyright 1993, 1994 Hewlett-Packard Company *
27 * (c) Copyright 1993, 1994 International Business Machines Corp. *
28 * (c) Copyright 1993, 1994 Sun Microsystems, Inc. *
29 * (c) Copyright 1993, 1994 Novell, Inc. *
38 * Revision 1.1.2.3 1995/06/06 20:25:50 Chris_Beute
39 * Code snapshot merge from March 15 and SIA changes
40 * [1995/05/31 20:17:31 Chris_Beute]
42 * Revision 1.1.2.2 1995/04/21 13:05:43 Peter_Derr
43 * dtlogin auth key fixes from deltacde
44 * [1995/04/12 19:21:36 Peter_Derr]
46 * xdm R6 version used to handle XDM-AUTHORIZATION-1
47 * [1995/04/12 18:06:02 Peter_Derr]
53 Copyright (c) 1988 X Consortium
55 Permission is hereby granted, free of charge, to any person obtaining
56 a copy of this software and associated documentation files (the
57 "Software"), to deal in the Software without restriction, including
58 without limitation the rights to use, copy, modify, merge, publish,
59 distribute, sublicense, and/or sell copies of the Software, and to
60 permit persons to whom the Software is furnished to do so, subject to
61 the following conditions:
63 The above copyright notice and this permission notice shall be included
64 in all copies or substantial portions of the Software.
66 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
67 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
68 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
69 IN NO EVENT SHALL THE X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR
70 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
71 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
72 OTHER DEALINGS IN THE SOFTWARE.
74 Except as contained in this notice, the name of the X Consortium shall
75 not be used in advertising or otherwise to promote the sale, use or
76 other dealings in this Software without prior written authorization
77 from the X Consortium.
82 * xdm - display manager daemon
83 * Author: Keith Packard, MIT X Consortium
87 * generate authorization data for XDM-AUTHORIZATION-1 as per XDMCP spec
94 static char auth_name[256];
95 static int auth_name_len;
98 XdmPrintDataHex(const char *s, const char *a, int l)
103 for (i = 0; i < l; i++)
104 Debug (" %02x", a[i] & 0xff);
110 XdmPrintArray8Hex(const char *s, ARRAY8Ptr a)
112 XdmPrintDataHex (s, (char *) a->data, a->length);
116 #if NeedWidePrototypes
118 XdmInitAuth (unsigned int name_len, char *name)
121 XdmInitAuth (unsigned short name_len, char *name)
122 #endif /* NeedWidePrototypes */
126 auth_name_len = name_len;
127 memmove( auth_name, name, name_len);
131 * Generate authorization for XDM-AUTHORIZATION-1
133 * When being used with XDMCP, 8 bytes are generated for the session key
134 * (sigma), as the random number (rho) is already shared between xdm and
135 * the server. Otherwise, we'll prepend a random number to pass in the file
136 * between xdm and the server (16 bytes total)
140 XdmGetAuthHelper (unsigned short namelen, char *name, int includeRho)
143 new = (Xauth *) malloc (sizeof (Xauth));
147 new->family = FamilyWild;
148 new->address_length = 0;
150 new->number_length = 0;
153 new->data_length = 16;
155 new->data_length = 8;
157 new->data = (char *) malloc (new->data_length);
163 new->name = (char *) malloc (namelen);
166 free ((char *) new->data);
170 memmove( (char *)new->name, name, namelen);
171 new->name_length = namelen;
172 GenerateAuthData ((char *)new->data, new->data_length);
174 * set the first byte of the session key to zero as it
175 * is a DES key and only uses 56 bits
177 ((char *)new->data)[new->data_length - 8] = '\0';
178 XdmPrintDataHex ("Local server auth", (char *)new->data, new->data_length);
182 #if NeedWidePrototypes
184 XdmGetAuth (unsigned int namelen, char *name)
187 XdmGetAuth (unsigned short namelen, char *name)
188 #endif /* NeedWidePrototypes */
190 return XdmGetAuthHelper (namelen, name, TRUE);
195 #if NeedWidePrototypes
196 void XdmGetXdmcpAuth (struct protoDisplay *pdpy, unsigned int authorizationNameLen, char *authorizationName)
198 void XdmGetXdmcpAuth (struct protoDisplay *pdpy, unsigned short authorizationNameLen, char *authorizationName)
199 #endif /* NeedWidePrototypes */
201 Xauth *fileauth, *xdmcpauth;
203 if (pdpy->fileAuthorization && pdpy->xdmcpAuthorization)
205 xdmcpauth = XdmGetAuthHelper (authorizationNameLen, authorizationName, FALSE);
208 fileauth = (Xauth *) malloc (sizeof (Xauth));
211 XauDisposeAuth(xdmcpauth);
214 /* build the file auth from the XDMCP auth */
215 *fileauth = *xdmcpauth;
216 fileauth->name = malloc (xdmcpauth->name_length);
217 fileauth->data = malloc (16);
218 fileauth->data_length = 16;
219 if (!fileauth->name || !fileauth->data)
221 XauDisposeAuth (xdmcpauth);
223 free ((char *) fileauth->name);
225 free ((char *) fileauth->data);
226 free ((char *) fileauth);
230 * for the file authorization, prepend the random number (rho)
231 * which is simply the number we've been passing back and
234 memmove( fileauth->name, xdmcpauth->name, xdmcpauth->name_length);
235 memmove( fileauth->data, pdpy->authenticationData.data, 8);
236 memmove( fileauth->data + 8, xdmcpauth->data, 8);
237 XdmPrintDataHex ("Accept packet auth", xdmcpauth->data, xdmcpauth->data_length);
238 XdmPrintDataHex ("Auth file auth", fileauth->data, fileauth->data_length);
239 /* encrypt the session key for its trip back to the server */
240 XdmcpWrap (xdmcpauth->data, &pdpy->key, xdmcpauth->data, 8);
241 pdpy->fileAuthorization = fileauth;
242 pdpy->xdmcpAuthorization = xdmcpauth;
245 #define atox(c) ('0' <= c && c <= '9' ? c - '0' : \
246 'a' <= c && c <= 'f' ? c - 'a' + 10 : \
247 'A' <= c && c <= 'F' ? c - 'A' + 10 : -1)
250 HexToBinary (char *key)
257 while (in[0] && in[1])
262 bottom = atox(in[1]);
265 *out++ = (top << 4) | bottom;
275 * Search the Keys file for the entry matching this display. This
276 * routine accepts either plain ascii strings for keys, or hex-encoded numbers
279 XdmGetKey (struct protoDisplay *pdpy, ARRAY8Ptr displayID)
282 char line[1024], id[1024], key[1024];
285 Debug ("Lookup key for %*.*s\n", displayID->length, displayID->length, displayID->data);
286 keys = fopen (keyFile, "r");
289 while (fgets (line, sizeof (line) - 1, keys))
291 if (line[0] == '#' || sscanf (line, "%s %s", id, key) != 2)
293 bzero(line, sizeof(line));
294 Debug ("Key entry \"%s\" \"%s\"\n", id, key);
295 if (strlen (id) == displayID->length &&
296 !strncmp (id, (char *)displayID->data, displayID->length))
298 if (!strncmp (key, "0x", 2) || !strncmp (key, "0X", 2))
299 if (!HexToBinary (key))
301 keylen = strlen (key);
303 key[keylen++] = '\0';
304 pdpy->key.data[0] = '\0';
305 memmove( pdpy->key.data + 1, key, 7);
306 bzero(key, sizeof(key));
311 bzero(line, sizeof(line));
312 bzero(key, sizeof(key));
318 XdmCheckAuthentication (struct protoDisplay *pdpy, ARRAY8Ptr displayID,
319 ARRAY8Ptr authenticationName, ARRAY8Ptr authenticationData)
321 XdmAuthKeyPtr incoming;
323 if (!XdmGetKey (pdpy, displayID))
325 if (authenticationData->length != 8)
327 XdmcpUnwrap (authenticationData->data, &pdpy->key,
328 authenticationData->data, 8);
329 XdmPrintArray8Hex ("Request packet auth", authenticationData);
330 if (!XdmcpCopyARRAY8(authenticationData, &pdpy->authenticationData))
332 incoming = (XdmAuthKeyPtr) authenticationData->data;
333 XdmcpIncrementKey (incoming);
334 XdmcpWrap (authenticationData->data, &pdpy->key,
335 authenticationData->data, 8);
340 #endif /* HASXDMAUTH (covering the entire file) */