xdmauth.c 9.8 KB

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