Xreset.src: Do not hardcode /usr/dt.
[oweals/cde.git] / cde / programs / dtlogin / xdmauth.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
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)
10  * any later version.
11  *
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
16  * details.
17  *
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
22  */
23 /* $TOG: xdmauth.c /main/4 1997/03/14 13:45:35 barstow $ */
24 /* (c) Copyright 1997 The Open Group */
25 /*                                                                      *
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.                                *
30  */
31
32 /*
33  * @DEC_COPYRIGHT@
34  */
35 /*
36  * HISTORY
37  * $Log$
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]
41  *
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]
45  * 
46  *      xdm R6 version used to handle XDM-AUTHORIZATION-1
47  *      [1995/04/12  18:06:02  Peter_Derr]
48  * 
49  * $EndLog$
50  */
51 /*
52
53 Copyright (c) 1988  X Consortium
54
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:
62
63 The above copyright notice and this permission notice shall be included
64 in all copies or substantial portions of the Software.
65
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.
73
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.
78
79 */
80
81 /*
82  * xdm - display manager daemon
83  * Author:  Keith Packard, MIT X Consortium
84  *
85  * xdmauth
86  *
87  * generate authorization data for XDM-AUTHORIZATION-1 as per XDMCP spec
88  */
89
90 #include "dm.h"
91
92 #ifdef HASXDMAUTH
93
94 static char     auth_name[256];
95 static int      auth_name_len;
96
97 XdmPrintDataHex (s, a, l)
98     char            *s;
99     char            *a;
100     int             l;
101 {
102     int i;
103
104     Debug ("%s", s);
105     for (i = 0; i < l; i++)
106         Debug (" %02x", a[i] & 0xff);
107     Debug ("\n");
108 }
109
110 #ifdef notdef                   /* not used */
111 XdmPrintKey (s, k)
112     char            *s;
113     XdmAuthKeyRec   *k;
114 {
115     XdmPrintDataHex (s, (char *) k->data, 8);
116 }
117 #endif
118
119 #ifdef XDMCP
120 XdmPrintArray8Hex (s, a)
121     char        *s;
122     ARRAY8Ptr   a;
123 {
124     XdmPrintDataHex (s, (char *) a->data, a->length);
125 }
126 #endif
127
128 XdmInitAuth (name_len, name)
129 #if NeedWidePrototypes
130     unsigned int name_len;
131 #else
132     unsigned short name_len;
133 #endif /* NeedWidePrototypes */
134     char            *name;
135 {
136     if (name_len > 256)
137         name_len = 256;
138     auth_name_len = name_len;
139     memmove( auth_name, name, name_len);
140 }
141
142 /*
143  * Generate authorization for XDM-AUTHORIZATION-1 
144  *
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)
149  */
150
151 Xauth *
152 XdmGetAuthHelper (namelen, name, includeRho)
153     unsigned short  namelen;
154     char            *name;
155     int     includeRho;
156 {
157     Xauth   *new;
158     new = (Xauth *) malloc (sizeof (Xauth));
159
160     if (!new)
161         return (Xauth *) 0;
162     new->family = FamilyWild;
163     new->address_length = 0;
164     new->address = 0;
165     new->number_length = 0;
166     new->number = 0;
167     if (includeRho)
168         new->data_length = 16;
169     else
170         new->data_length = 8;
171
172     new->data = (char *) malloc (new->data_length);
173     if (!new->data)
174     {
175         free ((char *) new);
176         return (Xauth *) 0;
177     }
178     new->name = (char *) malloc (namelen);
179     if (!new->name)
180     {
181         free ((char *) new->data);
182         free ((char *) new);
183         return (Xauth *) 0;
184     }
185     memmove( (char *)new->name, name, namelen);
186     new->name_length = namelen;
187     GenerateAuthData ((char *)new->data, new->data_length);
188     /*
189      * set the first byte of the session key to zero as it
190      * is a DES key and only uses 56 bits
191      */
192     ((char *)new->data)[new->data_length - 8] = '\0';
193     XdmPrintDataHex ("Local server auth", (char *)new->data, new->data_length);
194     return new;
195 }
196
197 Xauth *
198 XdmGetAuth (namelen, name)
199 #if NeedWidePrototypes
200     unsigned int namelen;
201 #else
202     unsigned short namelen;
203 #endif /* NeedWidePrototypes */
204     char            *name;
205 {
206     return XdmGetAuthHelper (namelen, name, TRUE);
207 }
208
209 #ifdef XDMCP
210
211 XdmGetXdmcpAuth (pdpy,authorizationNameLen, authorizationName)
212     struct protoDisplay *pdpy;
213 #if NeedWidePrototypes
214     unsigned int authorizationNameLen;
215 #else
216     unsigned short authorizationNameLen;
217 #endif /* NeedWidePrototypes */
218     char                *authorizationName;
219 {
220     Xauth   *fileauth, *xdmcpauth;
221
222     if (pdpy->fileAuthorization && pdpy->xdmcpAuthorization)
223         return;
224     xdmcpauth = XdmGetAuthHelper (authorizationNameLen, authorizationName, FALSE);
225     if (!xdmcpauth)
226         return;
227     fileauth = (Xauth *) malloc (sizeof (Xauth));
228     if (!fileauth)
229     {
230         XauDisposeAuth(xdmcpauth);
231         return;
232     }
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)
239     {
240         XauDisposeAuth (xdmcpauth);
241         if (fileauth->name)
242             free ((char *) fileauth->name);
243         if (fileauth->data)
244             free ((char *) fileauth->data);
245         free ((char *) fileauth);
246         return;
247     }
248     /*
249      * for the file authorization, prepend the random number (rho)
250      * which is simply the number we've been passing back and
251      * forth via XDMCP
252      */
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;
262 }
263
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)
267
268 static
269 HexToBinary (key)
270     char    *key;
271 {
272     char    *out, *in;
273     int     top, bottom;
274
275     in = key + 2;
276     out= key;
277     while (in[0] && in[1])
278     {
279         top = atox(in[0]);
280         if (top == -1)
281             return 0;
282         bottom = atox(in[1]);
283         if (bottom == -1)
284             return 0;
285         *out++ = (top << 4) | bottom;
286         in += 2;
287     }
288     if (in[0])
289         return 0;
290     *out++ = '\0';
291     return 1;
292 }
293
294 /*
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
297  */
298
299 XdmGetKey (pdpy, displayID)
300     struct protoDisplay *pdpy;
301     ARRAY8Ptr           displayID;
302 {
303     FILE    *keys;
304     char    line[1024], id[1024], key[1024];
305     int     keylen;
306
307     Debug ("Lookup key for %*.*s\n", displayID->length, displayID->length, displayID->data);
308     keys = fopen (keyFile, "r");
309     if (!keys)
310         return FALSE;
311     while (fgets (line, sizeof (line) -  1, keys))
312     {
313         if (line[0] == '#' || sscanf (line, "%s %s", id, key) != 2)
314             continue;
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))
319         {
320             if (!strncmp (key, "0x", 2) || !strncmp (key, "0X", 2))
321                 if (!HexToBinary (key))
322                     break;
323             keylen = strlen (key);
324             while (keylen < 7)
325                 key[keylen++] = '\0';
326             pdpy->key.data[0] = '\0';
327             memmove( pdpy->key.data + 1, key, 7);
328             bzero(key, sizeof(key));
329             fclose (keys);
330             return TRUE;
331         }
332     }
333     bzero(line, sizeof(line));
334     bzero(key, sizeof(key));
335     fclose (keys);
336     return FALSE;
337 }
338
339 /*ARGSUSED*/
340 XdmCheckAuthentication (pdpy, displayID, authenticationName, authenticationData)
341     struct protoDisplay *pdpy;
342     ARRAY8Ptr           displayID, authenticationName, authenticationData;
343 {
344     XdmAuthKeyPtr   incoming;
345
346     if (!XdmGetKey (pdpy, displayID))
347         return FALSE;
348     if (authenticationData->length != 8)
349         return FALSE;
350     XdmcpUnwrap (authenticationData->data, &pdpy->key,
351                   authenticationData->data, 8);
352     XdmPrintArray8Hex ("Request packet auth", authenticationData);
353     if (!XdmcpCopyARRAY8(authenticationData, &pdpy->authenticationData))
354         return FALSE;
355     incoming = (XdmAuthKeyPtr) authenticationData->data;
356     XdmcpIncrementKey (incoming);
357     XdmcpWrap (authenticationData->data, &pdpy->key,
358                   authenticationData->data, 8);
359     return TRUE;
360 }
361
362 #endif /* XDMCP */
363 #endif /* HASXDMAUTH (covering the entire file) */