3 Copyright 1988, 1998 The Open Group
4 Copyright 2001,2003 Oswald Buddenhagen <ossi@kde.org>
6 Permission to use, copy, modify, distribute, and sell this software and its
7 documentation for any purpose is hereby granted without fee, provided that
8 the above copyright notice appear in all copies and that both that
9 copyright notice and this permission notice appear in supporting
12 The above copyright notice and this permission notice shall be included
13 in all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
16 OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
19 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
20 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
21 OTHER DEALINGS IN THE SOFTWARE.
23 Except as contained in this notice, the name of a copyright holder shall
24 not be used in advertising or otherwise to promote the sale, use or
25 other dealings in this Software without prior written authorization
26 from the copyright holder.
31 * xdm - display manager daemon
32 * Author: Keith Packard, MIT X Consortium
34 * generate authorization data for XDM-AUTHORIZATION-1 as per XDMCP spec
41 static char authName
[256];
42 static int authNameLen
;
45 xdmInitAuth( unsigned short name_len
, const char *name
)
49 authNameLen
= name_len
;
50 memmove( authName
, name
, name_len
);
54 * Generate authorization for XDM-AUTHORIZATION-1
56 * When being used with XDMCP, 8 bytes are generated for the session key
57 * (sigma), as the random number (rho) is already shared between xdm and
58 * the server. Otherwise, we'll prepend a random number to pass in the file
59 * between xdm and the server (16 bytes total)
63 xdmGetAuthHelper( unsigned short namelen
, const char *name
, int includeRho
)
67 if (!(new = (Xauth
*)Malloc( sizeof(Xauth
) )))
69 new->family
= FamilyWild
;
70 new->address_length
= 0;
72 new->number_length
= 0;
75 new->data_length
= 16;
79 new->data
= (char *)Malloc( new->data_length
);
84 new->name
= (char *)Malloc( namelen
);
86 free( (char *)new->data
);
90 memmove( (char *)new->name
, name
, namelen
);
91 new->name_length
= namelen
;
92 if (!generateAuthData( (char *)new->data
, new->data_length
)) {
93 free( (char *)new->name
);
94 free( (char *)new->data
);
99 * set the first byte of the session key to zero as it
100 * is a DES key and only uses 56 bits
102 ((char *)new->data
)[new->data_length
- 8] = '\0';
103 debug( "local server auth %02[*hhx\n", new->data_length
, new->data
);
108 xdmGetAuth( unsigned short namelen
, const char *name
)
110 return xdmGetAuthHelper( namelen
, name
, True
);
116 xdmGetXdmcpAuth( struct protoDisplay
*pdpy
,
117 unsigned short authorizationNameLen
,
118 const char *authorizationName
)
120 Xauth
*fileauth
, *xdmcpauth
;
122 if (pdpy
->fileAuthorization
&& pdpy
->xdmcpAuthorization
)
124 xdmcpauth
= xdmGetAuthHelper( authorizationNameLen
, authorizationName
,
128 fileauth
= (Xauth
*)Malloc( sizeof(Xauth
) );
130 XauDisposeAuth( xdmcpauth
);
133 /* build the file auth from the XDMCP auth */
134 *fileauth
= *xdmcpauth
;
135 fileauth
->name
= Malloc( xdmcpauth
->name_length
);
136 fileauth
->data
= Malloc( 16 );
137 fileauth
->data_length
= 16;
138 if (!fileauth
->name
|| !fileauth
->data
) {
139 XauDisposeAuth( xdmcpauth
);
141 free( (char *)fileauth
->name
);
143 free( (char *)fileauth
->data
);
144 free( (char *)fileauth
);
148 * for the file authorization, prepend the random number (rho)
149 * which is simply the number we've been passing back and
152 memmove( fileauth
->name
, xdmcpauth
->name
, xdmcpauth
->name_length
);
153 memmove( fileauth
->data
, pdpy
->authenticationData
.data
, 8 );
154 memmove( fileauth
->data
+ 8, xdmcpauth
->data
, 8 );
155 debug( "accept packet auth %02[*hhx\nauth file auth %02[*hhx\n",
156 xdmcpauth
->data_length
, xdmcpauth
->data
,
157 fileauth
->data_length
, fileauth
->data
);
158 /* encrypt the session key for its trip back to the server */
159 XdmcpWrap( (unsigned char *)xdmcpauth
->data
, (unsigned char *)&pdpy
->key
,
160 (unsigned char *)xdmcpauth
->data
, 8 );
161 pdpy
->fileAuthorization
= fileauth
;
162 pdpy
->xdmcpAuthorization
= xdmcpauth
;
165 #define atox(c) ('0' <= c && c <= '9' ? c - '0' : \
166 'a' <= c && c <= 'f' ? c - 'a' + 10 : \
167 'A' <= c && c <= 'F' ? c - 'A' + 10 : -1)
170 hexToBinary( char *key
)
177 while (in
[0] && in
[1]) {
181 bottom
= atox( in
[1] );
184 *out
++ = (top
<< 4) | bottom
;
194 * Search the Keys file for the entry matching this display. This
195 * routine accepts either plain ascii strings for keys, or hex-encoded numbers
199 xdmGetKey( struct protoDisplay
*pdpy
, ARRAY8Ptr displayID
)
202 char line
[1024], id
[1024], key
[1024];
205 debug( "lookup key for %.*s\n", displayID
->length
, displayID
->data
);
206 keys
= fopen( keyFile
, "r" );
209 while (fgets( line
, sizeof(line
), keys
)) {
210 if (line
[0] == '#' || sscanf( line
, "%s %s", id
, key
) != 2)
212 bzero( line
, sizeof(line
) );
213 debug( "key entry for %\"s %d bytes\n", id
, strlen( key
) );
214 if (strlen( id
) == displayID
->length
&&
215 !strncmp( id
, (char *)displayID
->data
, displayID
->length
))
217 if (!strncmp( key
, "0x", 2 ) || !strncmp( key
, "0X", 2 ))
218 if (!hexToBinary( key
))
220 keylen
= strlen( key
);
222 key
[keylen
++] = '\0';
223 pdpy
->key
.data
[0] = '\0';
224 memmove( pdpy
->key
.data
+ 1, key
, 7 );
225 bzero( key
, sizeof(key
) );
230 bzero( line
, sizeof(line
) );
231 bzero( key
, sizeof(key
) );
238 xdmcheckAuthentication( struct protoDisplay
*pdpy
,
240 ARRAY8Ptr authenticationName ATTR_UNUSED
,
241 ARRAY8Ptr authenticationData
)
243 XdmAuthKeyPtr incoming
;
245 if (!xdmGetKey( pdpy
, displayID
))
247 if (authenticationData
->length
!= 8)
249 XdmcpUnwrap( authenticationData
->data
, (unsigned char *)&pdpy
->key
,
250 authenticationData
->data
, 8 );
251 debug( "request packet auth %02[*hhx\n",
252 authenticationData
->length
, authenticationData
->data
);
253 if (!XdmcpCopyARRAY8( authenticationData
, &pdpy
->authenticationData
))
255 incoming
= (XdmAuthKeyPtr
)authenticationData
->data
;
256 XdmcpIncrementKey( incoming
);
257 XdmcpWrap( authenticationData
->data
, (unsigned char *)&pdpy
->key
,
258 authenticationData
->data
, 8 );