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 librararies 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;
97 XdmPrintDataHex (s, a, l)
105 for (i = 0; i < l; i++)
106 Debug (" %02x", a[i] & 0xff);
110 #ifdef notdef /* not used */
115 XdmPrintDataHex (s, (char *) k->data, 8);
120 XdmPrintArray8Hex (s, a)
124 XdmPrintDataHex (s, (char *) a->data, a->length);
128 XdmInitAuth (name_len, name)
129 #if NeedWidePrototypes
130 unsigned int name_len;
132 unsigned short name_len;
133 #endif /* NeedWidePrototypes */
138 auth_name_len = name_len;
139 memmove( auth_name, name, name_len);
143 * Generate authorization for XDM-AUTHORIZATION-1
145 * When being used with XDMCP, 8 bytes are generated for the session key
146 * (sigma), as the random number (rho) is already shared between xdm and
147 * the server. Otherwise, we'll prepend a random number to pass in the file
148 * between xdm and the server (16 bytes total)
152 XdmGetAuthHelper (namelen, name, includeRho)
153 unsigned short namelen;
158 new = (Xauth *) malloc (sizeof (Xauth));
162 new->family = FamilyWild;
163 new->address_length = 0;
165 new->number_length = 0;
168 new->data_length = 16;
170 new->data_length = 8;
172 new->data = (char *) malloc (new->data_length);
178 new->name = (char *) malloc (namelen);
181 free ((char *) new->data);
185 memmove( (char *)new->name, name, namelen);
186 new->name_length = namelen;
187 GenerateAuthData ((char *)new->data, new->data_length);
189 * set the first byte of the session key to zero as it
190 * is a DES key and only uses 56 bits
192 ((char *)new->data)[new->data_length - 8] = '\0';
193 XdmPrintDataHex ("Local server auth", (char *)new->data, new->data_length);
198 XdmGetAuth (namelen, name)
199 #if NeedWidePrototypes
200 unsigned int namelen;
202 unsigned short namelen;
203 #endif /* NeedWidePrototypes */
206 return XdmGetAuthHelper (namelen, name, TRUE);
211 XdmGetXdmcpAuth (pdpy,authorizationNameLen, authorizationName)
212 struct protoDisplay *pdpy;
213 #if NeedWidePrototypes
214 unsigned int authorizationNameLen;
216 unsigned short authorizationNameLen;
217 #endif /* NeedWidePrototypes */
218 char *authorizationName;
220 Xauth *fileauth, *xdmcpauth;
222 if (pdpy->fileAuthorization && pdpy->xdmcpAuthorization)
224 xdmcpauth = XdmGetAuthHelper (authorizationNameLen, authorizationName, FALSE);
227 fileauth = (Xauth *) malloc (sizeof (Xauth));
230 XauDisposeAuth(xdmcpauth);
233 /* build the file auth from the XDMCP auth */
234 *fileauth = *xdmcpauth;
235 fileauth->name = malloc (xdmcpauth->name_length);
236 fileauth->data = malloc (16);
237 fileauth->data_length = 16;
238 if (!fileauth->name || !fileauth->data)
240 XauDisposeAuth (xdmcpauth);
242 free ((char *) fileauth->name);
244 free ((char *) fileauth->data);
245 free ((char *) fileauth);
249 * for the file authorization, prepend the random number (rho)
250 * which is simply the number we've been passing back and
253 memmove( fileauth->name, xdmcpauth->name, xdmcpauth->name_length);
254 memmove( fileauth->data, pdpy->authenticationData.data, 8);
255 memmove( fileauth->data + 8, xdmcpauth->data, 8);
256 XdmPrintDataHex ("Accept packet auth", xdmcpauth->data, xdmcpauth->data_length);
257 XdmPrintDataHex ("Auth file auth", fileauth->data, fileauth->data_length);
258 /* encrypt the session key for its trip back to the server */
259 XdmcpWrap (xdmcpauth->data, &pdpy->key, xdmcpauth->data, 8);
260 pdpy->fileAuthorization = fileauth;
261 pdpy->xdmcpAuthorization = xdmcpauth;
264 #define atox(c) ('0' <= c && c <= '9' ? c - '0' : \
265 'a' <= c && c <= 'f' ? c - 'a' + 10 : \
266 'A' <= c && c <= 'F' ? c - 'A' + 10 : -1)
277 while (in[0] && in[1])
282 bottom = atox(in[1]);
285 *out++ = (top << 4) | bottom;
295 * Search the Keys file for the entry matching this display. This
296 * routine accepts either plain ascii strings for keys, or hex-encoded numbers
299 XdmGetKey (pdpy, displayID)
300 struct protoDisplay *pdpy;
304 char line[1024], id[1024], key[1024];
307 Debug ("Lookup key for %*.*s\n", displayID->length, displayID->length, displayID->data);
308 keys = fopen (keyFile, "r");
311 while (fgets (line, sizeof (line) - 1, keys))
313 if (line[0] == '#' || sscanf (line, "%s %s", id, key) != 2)
315 bzero(line, sizeof(line));
316 Debug ("Key entry \"%s\" \"%s\"\n", id, key);
317 if (strlen (id) == displayID->length &&
318 !strncmp (id, (char *)displayID->data, displayID->length))
320 if (!strncmp (key, "0x", 2) || !strncmp (key, "0X", 2))
321 if (!HexToBinary (key))
323 keylen = strlen (key);
325 key[keylen++] = '\0';
326 pdpy->key.data[0] = '\0';
327 memmove( pdpy->key.data + 1, key, 7);
328 bzero(key, sizeof(key));
333 bzero(line, sizeof(line));
334 bzero(key, sizeof(key));
340 XdmCheckAuthentication (pdpy, displayID, authenticationName, authenticationData)
341 struct protoDisplay *pdpy;
342 ARRAY8Ptr displayID, authenticationName, authenticationData;
344 XdmAuthKeyPtr incoming;
346 if (!XdmGetKey (pdpy, displayID))
348 if (authenticationData->length != 8)
350 XdmcpUnwrap (authenticationData->data, &pdpy->key,
351 authenticationData->data, 8);
352 XdmPrintArray8Hex ("Request packet auth", authenticationData);
353 if (!XdmcpCopyARRAY8(authenticationData, &pdpy->authenticationData))
355 incoming = (XdmAuthKeyPtr) authenticationData->data;
356 XdmcpIncrementKey (incoming);
357 XdmcpWrap (authenticationData->data, &pdpy->key,
358 authenticationData->data, 8);
363 #endif /* HASXDMAUTH (covering the entire file) */