dtprintinfo: Coverity 89561
[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 libraries 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 void
98 XdmPrintDataHex(const char *s, const char *a, int l)
99 {
100     int i;
101
102     Debug ("%s", s);
103     for (i = 0; i < l; i++)
104         Debug (" %02x", a[i] & 0xff);
105     Debug ("\n");
106 }
107
108 #ifdef notdef                   /* not used */
109 XdmPrintKey (s, k)
110     char            *s;
111     XdmAuthKeyRec   *k;
112 {
113     XdmPrintDataHex (s, (char *) k->data, 8);
114 }
115 #endif
116
117 #ifdef XDMCP
118 void
119 XdmPrintArray8Hex(const char *s, ARRAY8Ptr a)
120 {
121     XdmPrintDataHex (s, (char *) a->data, a->length);
122 }
123 #endif
124
125 #if NeedWidePrototypes
126 void
127 XdmInitAuth (unsigned int name_len, char *name)
128 #else
129 void
130 XdmInitAuth (unsigned short name_len, char *name)
131 #endif /* NeedWidePrototypes */
132 {
133     if (name_len > 256)
134         name_len = 256;
135     auth_name_len = name_len;
136     memmove( auth_name, name, name_len);
137 }
138
139 /*
140  * Generate authorization for XDM-AUTHORIZATION-1 
141  *
142  * When being used with XDMCP, 8 bytes are generated for the session key
143  * (sigma), as the random number (rho) is already shared between xdm and
144  * the server.  Otherwise, we'll prepend a random number to pass in the file
145  * between xdm and the server (16 bytes total)
146  */
147
148 Xauth *
149 XdmGetAuthHelper (unsigned short namelen, char *name, int includeRho)
150 {
151     Xauth   *new;
152     new = (Xauth *) malloc (sizeof (Xauth));
153
154     if (!new)
155         return (Xauth *) 0;
156     new->family = FamilyWild;
157     new->address_length = 0;
158     new->address = 0;
159     new->number_length = 0;
160     new->number = 0;
161     if (includeRho)
162         new->data_length = 16;
163     else
164         new->data_length = 8;
165
166     new->data = (char *) malloc (new->data_length);
167     if (!new->data)
168     {
169         free ((char *) new);
170         return (Xauth *) 0;
171     }
172     new->name = (char *) malloc (namelen);
173     if (!new->name)
174     {
175         free ((char *) new->data);
176         free ((char *) new);
177         return (Xauth *) 0;
178     }
179     memmove( (char *)new->name, name, namelen);
180     new->name_length = namelen;
181     GenerateAuthData ((char *)new->data, new->data_length);
182     /*
183      * set the first byte of the session key to zero as it
184      * is a DES key and only uses 56 bits
185      */
186     ((char *)new->data)[new->data_length - 8] = '\0';
187     XdmPrintDataHex ("Local server auth", (char *)new->data, new->data_length);
188     return new;
189 }
190
191 #if NeedWidePrototypes
192 Xauth *
193 XdmGetAuth (unsigned int namelen, char *name)
194 #else
195 Xauth *
196 XdmGetAuth (unsigned short namelen, char *name)
197 #endif /* NeedWidePrototypes */
198 {
199     return XdmGetAuthHelper (namelen, name, TRUE);
200 }
201
202 #ifdef XDMCP
203
204 #if NeedWidePrototypes
205 void XdmGetXdmcpAuth (struct protoDisplay *pdpy, unsigned int authorizationNameLen, char *authorizationName)
206 #else
207 void XdmGetXdmcpAuth (struct protoDisplay *pdpy, unsigned short authorizationNameLen, char *authorizationName)
208 #endif /* NeedWidePrototypes */
209 {
210     Xauth   *fileauth, *xdmcpauth;
211
212     if (pdpy->fileAuthorization && pdpy->xdmcpAuthorization)
213         return;
214     xdmcpauth = XdmGetAuthHelper (authorizationNameLen, authorizationName, FALSE);
215     if (!xdmcpauth)
216         return;
217     fileauth = (Xauth *) malloc (sizeof (Xauth));
218     if (!fileauth)
219     {
220         XauDisposeAuth(xdmcpauth);
221         return;
222     }
223     /* build the file auth from the XDMCP auth */
224     *fileauth = *xdmcpauth;
225     fileauth->name = malloc (xdmcpauth->name_length);
226     fileauth->data = malloc (16);
227     fileauth->data_length = 16;
228     if (!fileauth->name || !fileauth->data)
229     {
230         XauDisposeAuth (xdmcpauth);
231         if (fileauth->name)
232             free ((char *) fileauth->name);
233         if (fileauth->data)
234             free ((char *) fileauth->data);
235         free ((char *) fileauth);
236         return;
237     }
238     /*
239      * for the file authorization, prepend the random number (rho)
240      * which is simply the number we've been passing back and
241      * forth via XDMCP
242      */
243     memmove( fileauth->name, xdmcpauth->name, xdmcpauth->name_length);
244     memmove( fileauth->data, pdpy->authenticationData.data, 8);
245     memmove( fileauth->data + 8, xdmcpauth->data, 8);
246     XdmPrintDataHex ("Accept packet auth", xdmcpauth->data, xdmcpauth->data_length);
247     XdmPrintDataHex ("Auth file auth", fileauth->data, fileauth->data_length);
248     /* encrypt the session key for its trip back to the server */
249     XdmcpWrap (xdmcpauth->data, &pdpy->key, xdmcpauth->data, 8);
250     pdpy->fileAuthorization = fileauth;
251     pdpy->xdmcpAuthorization = xdmcpauth;
252 }
253
254 #define atox(c) ('0' <= c && c <= '9' ? c - '0' : \
255                  'a' <= c && c <= 'f' ? c - 'a' + 10 : \
256                  'A' <= c && c <= 'F' ? c - 'A' + 10 : -1)
257
258 static
259 HexToBinary (char *key)
260 {
261     char    *out, *in;
262     int     top, bottom;
263
264     in = key + 2;
265     out= key;
266     while (in[0] && in[1])
267     {
268         top = atox(in[0]);
269         if (top == -1)
270             return 0;
271         bottom = atox(in[1]);
272         if (bottom == -1)
273             return 0;
274         *out++ = (top << 4) | bottom;
275         in += 2;
276     }
277     if (in[0])
278         return 0;
279     *out++ = '\0';
280     return 1;
281 }
282
283 /*
284  * Search the Keys file for the entry matching this display.  This
285  * routine accepts either plain ascii strings for keys, or hex-encoded numbers
286  */
287
288 XdmGetKey (struct protoDisplay *pdpy, ARRAY8Ptr displayID)
289 {
290     FILE    *keys;
291     char    line[1024], id[1024], key[1024];
292     int     keylen;
293
294     Debug ("Lookup key for %*.*s\n", displayID->length, displayID->length, displayID->data);
295     keys = fopen (keyFile, "r");
296     if (!keys)
297         return FALSE;
298     while (fgets (line, sizeof (line) -  1, keys))
299     {
300         if (line[0] == '#' || sscanf (line, "%s %s", id, key) != 2)
301             continue;
302         bzero(line, sizeof(line));
303         Debug ("Key entry \"%s\" \"%s\"\n", id, key);
304         if (strlen (id) == displayID->length &&
305             !strncmp (id, (char *)displayID->data, displayID->length))
306         {
307             if (!strncmp (key, "0x", 2) || !strncmp (key, "0X", 2))
308                 if (!HexToBinary (key))
309                     break;
310             keylen = strlen (key);
311             while (keylen < 7)
312                 key[keylen++] = '\0';
313             pdpy->key.data[0] = '\0';
314             memmove( pdpy->key.data + 1, key, 7);
315             bzero(key, sizeof(key));
316             fclose (keys);
317             return TRUE;
318         }
319     }
320     bzero(line, sizeof(line));
321     bzero(key, sizeof(key));
322     fclose (keys);
323     return FALSE;
324 }
325
326 /*ARGSUSED*/
327 XdmCheckAuthentication (struct protoDisplay *pdpy, ARRAY8Ptr displayID,
328                         ARRAY8Ptr authenticationName, ARRAY8Ptr authenticationData)
329 {
330     XdmAuthKeyPtr   incoming;
331
332     if (!XdmGetKey (pdpy, displayID))
333         return FALSE;
334     if (authenticationData->length != 8)
335         return FALSE;
336     XdmcpUnwrap (authenticationData->data, &pdpy->key,
337                   authenticationData->data, 8);
338     XdmPrintArray8Hex ("Request packet auth", authenticationData);
339     if (!XdmcpCopyARRAY8(authenticationData, &pdpy->authenticationData))
340         return FALSE;
341     incoming = (XdmAuthKeyPtr) authenticationData->data;
342     XdmcpIncrementKey (incoming);
343     XdmcpWrap (authenticationData->data, &pdpy->key,
344                   authenticationData->data, 8);
345     return TRUE;
346 }
347
348 #endif /* XDMCP */
349 #endif /* HASXDMAUTH (covering the entire file) */