~ubuntu-branches/ubuntu/trusty/xdm/trusty

« back to all changes in this revision

Viewing changes to xdmauth.c

  • Committer: Package Import Robot
  • Author(s): Micah Gersten
  • Date: 2011-12-09 03:00:26 UTC
  • mfrom: (9.1.5 sid)
  • Revision ID: package-import@ubuntu.com-20111209030026-baa9vul2hhtujjxh
Tags: 1:1.1.11-1ubuntu1
* Merge from Debian testing.  Remaining changes:
  - debian/{rules, xdm.install, local/ubuntu*}:
    + Add Ubuntu graphics and configure xdm to use them by default.
  - debian/patches/ubuntu_no_whiteglass.diff: Don't hardcode
    the default Xcursor theme to whiteglass. Use the Ubuntu
    default x-cursor-theme instead.

* Drop debian/patches/ftbfs_binutils-gold.diff: Fixed upstream
* Change Vcs-* to XS-Debian-Vcs-*
  - update debian/control

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 
3
 
Copyright 1988, 1998  The Open Group
4
 
 
5
 
Permission to use, copy, modify, distribute, and sell this software and its
6
 
documentation for any purpose is hereby granted without fee, provided that
7
 
the above copyright notice appear in all copies and that both that
8
 
copyright notice and this permission notice appear in supporting
9
 
documentation.
10
 
 
11
 
The above copyright notice and this permission notice shall be included
12
 
in all copies or substantial portions of the Software.
13
 
 
14
 
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
15
 
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
 
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17
 
IN NO EVENT SHALL THE OPEN GROUP BE LIABLE FOR ANY CLAIM, DAMAGES OR
18
 
OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19
 
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20
 
OTHER DEALINGS IN THE SOFTWARE.
21
 
 
22
 
Except as contained in this notice, the name of The Open Group shall
23
 
not be used in advertising or otherwise to promote the sale, use or
24
 
other dealings in this Software without prior written authorization
25
 
from The Open Group.
26
 
 
27
 
*/
28
 
/*
29
 
 * xdm - display manager daemon
30
 
 * Author:  Keith Packard, MIT X Consortium
31
 
 *
32
 
 * xdmauth
33
 
 *
34
 
 * generate authorization data for XDM-AUTHORIZATION-1 as per XDMCP spec
35
 
 */
36
 
 
37
 
#include "dm.h"
38
 
#include "dm_auth.h"
39
 
#include "dm_error.h"
40
 
 
41
 
#ifdef HASXDMAUTH
42
 
 
43
 
static char     auth_name[256];
44
 
static int      auth_name_len;
45
 
 
46
 
static void
47
 
XdmPrintDataHex (char *s, char *a, int l)
48
 
{
49
 
    int i;
50
 
 
51
 
    Debug ("%s", s);
52
 
    for (i = 0; i < l; i++)
53
 
        Debug (" %02x", a[i] & 0xff);
54
 
    Debug ("\n");
55
 
}
56
 
 
57
 
# ifdef XDMCP
58
 
static void
59
 
XdmPrintArray8Hex (char *s, ARRAY8Ptr a)
60
 
{
61
 
    XdmPrintDataHex (s, (char *) a->data, a->length);
62
 
}
63
 
# endif
64
 
 
65
 
void
66
 
XdmInitAuth (unsigned short name_len, char *name)
67
 
{
68
 
    if (name_len > 256)
69
 
        name_len = 256;
70
 
    auth_name_len = name_len;
71
 
    memmove( auth_name, name, name_len);
72
 
}
73
 
 
74
 
/*
75
 
 * Generate authorization for XDM-AUTHORIZATION-1
76
 
 *
77
 
 * When being used with XDMCP, 8 bytes are generated for the session key
78
 
 * (sigma), as the random number (rho) is already shared between xdm and
79
 
 * the server.  Otherwise, we'll prepend a random number to pass in the file
80
 
 * between xdm and the server (16 bytes total)
81
 
 */
82
 
 
83
 
static Xauth *
84
 
XdmGetAuthHelper (unsigned short namelen, char *name, int includeRho)
85
 
{
86
 
    Xauth   *new;
87
 
    new = (Xauth *) malloc (sizeof (Xauth));
88
 
 
89
 
    if (!new)
90
 
        return (Xauth *) 0;
91
 
    new->family = FamilyWild;
92
 
    new->address_length = 0;
93
 
    new->address = NULL;
94
 
    new->number_length = 0;
95
 
    new->number = NULL;
96
 
    if (includeRho)
97
 
        new->data_length = 16;
98
 
    else
99
 
        new->data_length = 8;
100
 
 
101
 
    new->data = (char *) malloc (new->data_length);
102
 
    if (!new->data)
103
 
    {
104
 
        free ((char *) new);
105
 
        return (Xauth *) 0;
106
 
    }
107
 
    new->name = (char *) malloc (namelen);
108
 
    if (!new->name)
109
 
    {
110
 
        free ((char *) new->data);
111
 
        free ((char *) new);
112
 
        return (Xauth *) 0;
113
 
    }
114
 
    memmove( (char *)new->name, name, namelen);
115
 
    new->name_length = namelen;
116
 
    if (!GenerateAuthData ((char *)new->data, new->data_length))
117
 
    {
118
 
        free ((char *) new->name);
119
 
        free ((char *) new->data);
120
 
        free ((char *) new);
121
 
        return (Xauth *) 0;
122
 
    }
123
 
    /*
124
 
     * set the first byte of the session key to zero as it
125
 
     * is a DES key and only uses 56 bits
126
 
     */
127
 
    ((char *)new->data)[new->data_length - 8] = '\0';
128
 
    XdmPrintDataHex ("Local server auth", (char *)new->data, new->data_length);
129
 
    return new;
130
 
}
131
 
 
132
 
Xauth *
133
 
XdmGetAuth (unsigned short namelen, char *name)
134
 
{
135
 
    return XdmGetAuthHelper (namelen, name, TRUE);
136
 
}
137
 
 
138
 
# ifdef XDMCP
139
 
 
140
 
void
141
 
XdmGetXdmcpAuth (struct protoDisplay *pdpy,
142
 
    unsigned short authorizationNameLen, char *authorizationName)
143
 
{
144
 
    Xauth   *fileauth, *xdmcpauth;
145
 
 
146
 
    if (pdpy->fileAuthorization && pdpy->xdmcpAuthorization)
147
 
        return;
148
 
    xdmcpauth = XdmGetAuthHelper (authorizationNameLen, authorizationName, FALSE);
149
 
    if (!xdmcpauth)
150
 
        return;
151
 
    fileauth = (Xauth *) malloc (sizeof (Xauth));
152
 
    if (!fileauth)
153
 
    {
154
 
        XauDisposeAuth(xdmcpauth);
155
 
        return;
156
 
    }
157
 
    /* build the file auth from the XDMCP auth */
158
 
    *fileauth = *xdmcpauth;
159
 
    fileauth->name = malloc (xdmcpauth->name_length);
160
 
    fileauth->data = malloc (16);
161
 
    fileauth->data_length = 16;
162
 
    if (!fileauth->name || !fileauth->data)
163
 
    {
164
 
        XauDisposeAuth (xdmcpauth);
165
 
        if (fileauth->name)
166
 
            free ((char *) fileauth->name);
167
 
        if (fileauth->data)
168
 
            free ((char *) fileauth->data);
169
 
        free ((char *) fileauth);
170
 
        return;
171
 
    }
172
 
    /*
173
 
     * for the file authorization, prepend the random number (rho)
174
 
     * which is simply the number we've been passing back and
175
 
     * forth via XDMCP
176
 
     */
177
 
    memmove( fileauth->name, xdmcpauth->name, xdmcpauth->name_length);
178
 
    memmove( fileauth->data, pdpy->authenticationData.data, 8);
179
 
    memmove( fileauth->data + 8, xdmcpauth->data, 8);
180
 
    XdmPrintDataHex ("Accept packet auth", xdmcpauth->data, xdmcpauth->data_length);
181
 
    XdmPrintDataHex ("Auth file auth", fileauth->data, fileauth->data_length);
182
 
    /* encrypt the session key for its trip back to the server */
183
 
    XdmcpWrap ((unsigned char *)xdmcpauth->data, (unsigned char *)&pdpy->key,
184
 
               (unsigned char *)xdmcpauth->data, 8);
185
 
    pdpy->fileAuthorization = fileauth;
186
 
    pdpy->xdmcpAuthorization = xdmcpauth;
187
 
}
188
 
 
189
 
#  define atox(c)       ('0' <= c && c <= '9' ? c - '0' : \
190
 
                 'a' <= c && c <= 'f' ? c - 'a' + 10 : \
191
 
                 'A' <= c && c <= 'F' ? c - 'A' + 10 : -1)
192
 
 
193
 
static int
194
 
HexToBinary(char *key)
195
 
{
196
 
    char    *out, *in;
197
 
    int     top, bottom;
198
 
 
199
 
    in = key + 2;
200
 
    out= key;
201
 
    while (in[0] && in[1])
202
 
    {
203
 
        top = atox(in[0]);
204
 
        if (top == -1)
205
 
            return 0;
206
 
        bottom = atox(in[1]);
207
 
        if (bottom == -1)
208
 
            return 0;
209
 
        *out++ = (top << 4) | bottom;
210
 
        in += 2;
211
 
    }
212
 
    if (in[0])
213
 
        return 0;
214
 
    *out++ = '\0';
215
 
    return 1;
216
 
}
217
 
 
218
 
/*
219
 
 * Search the Keys file for the entry matching this display.  This
220
 
 * routine accepts either plain ascii strings for keys, or hex-encoded numbers
221
 
 */
222
 
 
223
 
static int
224
 
XdmGetKey(struct protoDisplay *pdpy, ARRAY8Ptr displayID)
225
 
{
226
 
    FILE    *keys;
227
 
    char    line[1024], id[1024], key[1024];
228
 
    int     keylen;
229
 
 
230
 
    Debug ("Lookup key for %*.*s\n", displayID->length, displayID->length, displayID->data);
231
 
    keys = fopen (keyFile, "r");
232
 
    if (!keys)
233
 
        return FALSE;
234
 
    while (fgets (line, sizeof (line) -  1, keys))
235
 
    {
236
 
        if (line[0] == '#' || sscanf (line, "%s %s", id, key) != 2)
237
 
            continue;
238
 
        bzero(line, sizeof(line));
239
 
        Debug ("Key entry for \"%s\" %d bytes\n", id, strlen(key));
240
 
        if (strlen (id) == displayID->length &&
241
 
            !strncmp (id, (char *)displayID->data, displayID->length))
242
 
        {
243
 
            if (!strncmp (key, "0x", 2) || !strncmp (key, "0X", 2))
244
 
                if (!HexToBinary (key))
245
 
                    break;
246
 
            keylen = strlen (key);
247
 
            while (keylen < 7)
248
 
                key[keylen++] = '\0';
249
 
            pdpy->key.data[0] = '\0';
250
 
            memmove( pdpy->key.data + 1, key, 7);
251
 
            bzero(key, sizeof(key));
252
 
            fclose (keys);
253
 
            return TRUE;
254
 
        }
255
 
    }
256
 
    bzero(line, sizeof(line));
257
 
    bzero(key, sizeof(key));
258
 
    fclose (keys);
259
 
    return FALSE;
260
 
}
261
 
 
262
 
/*ARGSUSED*/
263
 
int
264
 
XdmCheckAuthentication(struct protoDisplay *pdpy, ARRAY8Ptr displayID,
265
 
    ARRAY8Ptr authenticationName, ARRAY8Ptr authenticationData)
266
 
{
267
 
    XdmAuthKeyPtr   incoming;
268
 
 
269
 
    if (!XdmGetKey (pdpy, displayID))
270
 
        return FALSE;
271
 
    if (authenticationData->length != 8)
272
 
        return FALSE;
273
 
    XdmcpUnwrap (authenticationData->data, (unsigned char *)&pdpy->key,
274
 
                  authenticationData->data, 8);
275
 
    XdmPrintArray8Hex ("Request packet auth", authenticationData);
276
 
    if (!XdmcpCopyARRAY8(authenticationData, &pdpy->authenticationData))
277
 
        return FALSE;
278
 
    incoming = (XdmAuthKeyPtr) authenticationData->data;
279
 
    XdmcpIncrementKey (incoming);
280
 
    XdmcpWrap (authenticationData->data, (unsigned char *)&pdpy->key,
281
 
                  authenticationData->data, 8);
282
 
    return TRUE;
283
 
}
284
 
 
285
 
# endif /* XDMCP */
286
 
#endif /* HASXDMAUTH (covering the entire file) */