PageRenderTime 50ms CodeModel.GetById 8ms RepoModel.GetById 1ms app.codeStats 0ms

/kdebase-3.5.10/kdm/backend/access.c

#
C | 468 lines | 373 code | 45 blank | 50 comment | 72 complexity | 071dea135162b1a3b1ec0138b77e80a0 MD5 | raw file
Possible License(s): LGPL-2.0, GPL-2.0, CC-BY-SA-3.0
  1. /*
  2. Copyright 1990, 1998 The Open Group
  3. Copyright 2001,2004 Oswald Buddenhagen <ossi@kde.org>
  4. Copyright 2002 Sun Microsystems, Inc. All rights reserved.
  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. The above copyright notice and this permission notice shall be included
  11. in all copies or substantial portions of the Software.
  12. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  13. OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  14. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  15. IN NO EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
  16. OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
  17. ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
  18. OTHER DEALINGS IN THE SOFTWARE.
  19. Except as contained in this notice, the name of a copyright holder shall
  20. not be used in advertising or otherwise to promote the sale, use or
  21. other dealings in this Software without prior written authorization
  22. from the copyright holder.
  23. */
  24. /*
  25. * xdm - display manager daemon
  26. * Author: Keith Packard, MIT X Consortium
  27. *
  28. * Access control for XDMCP - keep a database of allowable display addresses
  29. * and (potentially) a list of hosts to send ForwardQuery packets to
  30. */
  31. #include <config.h>
  32. #ifdef XDMCP
  33. #include "dm.h"
  34. #include "dm_error.h"
  35. #include "dm_socket.h"
  36. #include <stdio.h>
  37. #include <ctype.h>
  38. #include <netdb.h>
  39. #if defined(IPv6) && defined(AF_INET6)
  40. # include <arpa/inet.h>
  41. #endif
  42. typedef struct {
  43. short int type;
  44. union {
  45. char *aliasPattern;
  46. char *hostPattern;
  47. struct _displayAddress {
  48. CARD16 connectionType;
  49. ARRAY8 hostAddress;
  50. } displayAddress;
  51. } entry;
  52. } HostEntry;
  53. typedef struct {
  54. short int iface;
  55. short int mcasts;
  56. short int nmcasts;
  57. } ListenEntry;
  58. typedef struct {
  59. char *name;
  60. short int hosts;
  61. short int nhosts;
  62. } AliasEntry;
  63. typedef struct {
  64. short int entries;
  65. short int nentries;
  66. short int hosts;
  67. short int nhosts;
  68. short int flags;
  69. } AclEntry;
  70. typedef struct {
  71. HostEntry *hostList;
  72. ListenEntry *listenList;
  73. AliasEntry *aliasList;
  74. AclEntry *acList;
  75. short int nHosts, nListens, nAliases, nAcls;
  76. CfgDep dep;
  77. } AccArr;
  78. static AccArr accData[1];
  79. static ARRAY8 localAddress;
  80. ARRAY8Ptr
  81. getLocalAddress( void )
  82. {
  83. static int haveLocalAddress;
  84. if (!haveLocalAddress) {
  85. #if defined(IPv6) && defined(AF_INET6)
  86. struct addrinfo *ai;
  87. if (getaddrinfo( localHostname(), NULL, NULL, &ai )) {
  88. XdmcpAllocARRAY8( &localAddress, 4 );
  89. localAddress.data[0] = 127;
  90. localAddress.data[1] = 0;
  91. localAddress.data[2] = 0;
  92. localAddress.data[3] = 1;
  93. } else {
  94. if (ai->ai_family == AF_INET) {
  95. XdmcpAllocARRAY8( &localAddress, sizeof(struct in_addr) );
  96. memcpy( localAddress.data,
  97. &((struct sockaddr_in *)ai->ai_addr)->sin_addr,
  98. sizeof(struct in_addr) );
  99. } else /* if (ai->ai_family == AF_INET6) */ {
  100. XdmcpAllocARRAY8( &localAddress, sizeof(struct in6_addr) );
  101. memcpy( localAddress.data,
  102. &((struct sockaddr_in6 *)ai->ai_addr)->sin6_addr,
  103. sizeof(struct in6_addr) );
  104. }
  105. freeaddrinfo( ai );
  106. #else
  107. struct hostent *hostent;
  108. if ((hostent = gethostbyname( localHostname() ))) {
  109. XdmcpAllocARRAY8( &localAddress, hostent->h_length );
  110. memmove( localAddress.data, hostent->h_addr, hostent->h_length );
  111. #endif
  112. haveLocalAddress = 1;
  113. }
  114. }
  115. return &localAddress;
  116. }
  117. void
  118. ScanAccessDatabase( int force )
  119. {
  120. struct _displayAddress *da;
  121. char *cptr;
  122. int nChars, i;
  123. Debug( "ScanAccessDatabase\n" );
  124. if (Setjmp( cnftalk.errjmp ))
  125. return; /* may memleak */
  126. if (startConfig( GC_gXaccess, &accData->dep, force ) <= 0)
  127. return;
  128. if (accData->hostList)
  129. free( accData->hostList );
  130. accData->nHosts = GRecvInt();
  131. accData->nListens = GRecvInt();
  132. accData->nAliases = GRecvInt();
  133. accData->nAcls = GRecvInt();
  134. nChars = GRecvInt();
  135. if (!(accData->hostList = (HostEntry *)
  136. Malloc( accData->nHosts * sizeof(HostEntry) +
  137. accData->nListens * sizeof(ListenEntry) +
  138. accData->nAliases * sizeof(AliasEntry) +
  139. accData->nAcls * sizeof(AclEntry) +
  140. nChars )))
  141. {
  142. CloseGetter();
  143. return;
  144. }
  145. accData->listenList = (ListenEntry *)(accData->hostList + accData->nHosts);
  146. accData->aliasList = (AliasEntry *)(accData->listenList + accData->nListens);
  147. accData->acList = (AclEntry *)(accData->aliasList + accData->nAliases);
  148. cptr = (char *)(accData->acList + accData->nAcls);
  149. for (i = 0; i < accData->nHosts; i++) {
  150. switch ((accData->hostList[i].type = GRecvInt())) {
  151. case HOST_ALIAS:
  152. accData->hostList[i].entry.aliasPattern = cptr;
  153. cptr += GRecvStrBuf( cptr );
  154. break;
  155. case HOST_PATTERN:
  156. accData->hostList[i].entry.hostPattern = cptr;
  157. cptr += GRecvStrBuf( cptr );
  158. break;
  159. case HOST_ADDRESS:
  160. da = &accData->hostList[i].entry.displayAddress;
  161. da->hostAddress.data = (unsigned char *)cptr;
  162. cptr += (da->hostAddress.length = GRecvArrBuf( cptr ));
  163. switch (GRecvInt())
  164. {
  165. #ifdef AF_INET
  166. case AF_INET:
  167. da->connectionType = FamilyInternet;
  168. break;
  169. #endif
  170. #if defined(IPv6) && defined(AF_INET6)
  171. case AF_INET6:
  172. da->connectionType = FamilyInternet6;
  173. break;
  174. #endif
  175. #ifdef AF_DECnet
  176. case AF_DECnet:
  177. da->connectionType = FamilyDECnet;
  178. break;
  179. #endif
  180. /*#ifdef AF_UNIX
  181. case AF_UNIX:
  182. #endif*/
  183. default:
  184. da->connectionType = FamilyLocal;
  185. break;
  186. }
  187. break;
  188. case HOST_BROADCAST:
  189. break;
  190. default:
  191. LogError( "Received unknown host type %d from config reader\n", accData->hostList[i].type );
  192. return;
  193. }
  194. }
  195. for (i = 0; i < accData->nListens; i++) {
  196. accData->listenList[i].iface = GRecvInt();
  197. accData->listenList[i].mcasts = GRecvInt();
  198. accData->listenList[i].nmcasts = GRecvInt();
  199. }
  200. for (i = 0; i < accData->nAliases; i++) {
  201. accData->aliasList[i].name = cptr;
  202. cptr += GRecvStrBuf( cptr );
  203. accData->aliasList[i].hosts = GRecvInt();
  204. accData->aliasList[i].nhosts = GRecvInt();
  205. }
  206. for (i = 0; i < accData->nAcls; i++) {
  207. accData->acList[i].entries = GRecvInt();
  208. accData->acList[i].nentries = GRecvInt();
  209. accData->acList[i].hosts = GRecvInt();
  210. accData->acList[i].nhosts = GRecvInt();
  211. accData->acList[i].flags = GRecvInt();
  212. }
  213. }
  214. /* Returns non-0 if string is matched by pattern. Does case folding.
  215. */
  216. static int
  217. patternMatch( const char *string, const char *pattern )
  218. {
  219. int p, s;
  220. if (!string)
  221. string = "";
  222. for (;;) {
  223. s = *string++;
  224. switch (p = *pattern++) {
  225. case '*':
  226. if (!*pattern)
  227. return 1;
  228. for (string--; *string; string++)
  229. if (patternMatch( string, pattern ))
  230. return 1;
  231. return 0;
  232. case '?':
  233. if (s == '\0')
  234. return 0;
  235. break;
  236. case '\0':
  237. return s == '\0';
  238. case '\\':
  239. p = *pattern++;
  240. /* fall through */
  241. default:
  242. if (tolower( p ) != tolower( s ))
  243. return 0;
  244. }
  245. }
  246. }
  247. #define MAX_DEPTH 32
  248. static void
  249. scanHostlist( int fh, int nh,
  250. ARRAY8Ptr clientAddress, CARD16 connectionType,
  251. ChooserFunc function, char *closure,
  252. int broadcast, int *haveLocalhost )
  253. {
  254. HostEntry *h;
  255. AliasEntry *a;
  256. int na;
  257. for (h = accData->hostList + fh; nh; nh--, h++) {
  258. switch (h->type) {
  259. case HOST_ALIAS:
  260. for (a = accData->aliasList, na = accData->nAliases; na; na--, a++)
  261. if (patternMatch( a->name, h->entry.aliasPattern )) /* XXX originally swapped, no wildcards in alias name matching */
  262. scanHostlist( a->hosts, a->nhosts,
  263. clientAddress, connectionType,
  264. function, closure, broadcast,
  265. haveLocalhost );
  266. break;
  267. case HOST_ADDRESS:
  268. if (XdmcpARRAY8Equal( getLocalAddress(), &h->entry.displayAddress.hostAddress ))
  269. *haveLocalhost = 1;
  270. else if (function)
  271. (*function)( connectionType, &h->entry.displayAddress.hostAddress, closure );
  272. break;
  273. case HOST_BROADCAST:
  274. if (broadcast && function)
  275. (*function)( FamilyBroadcast, 0, closure );
  276. break;
  277. default:
  278. break;
  279. }
  280. }
  281. }
  282. static int
  283. scanEntrylist( int fh, int nh,
  284. ARRAY8Ptr clientAddress, CARD16 connectionType,
  285. char **clientName )
  286. {
  287. HostEntry *h;
  288. AliasEntry *a;
  289. int na;
  290. for (h = accData->hostList + fh; nh; nh--, h++) {
  291. switch (h->type) {
  292. case HOST_ALIAS:
  293. for (a = accData->aliasList, na = accData->nAliases; na; na--, a++)
  294. if (patternMatch( a->name, h->entry.aliasPattern ))
  295. if (scanEntrylist( a->hosts, a->nhosts,
  296. clientAddress, connectionType,
  297. clientName ))
  298. return 1;
  299. break;
  300. case HOST_PATTERN:
  301. if (!*clientName)
  302. *clientName = NetworkAddressToHostname( connectionType,
  303. clientAddress );
  304. if (patternMatch( *clientName, h->entry.hostPattern ))
  305. return 1;
  306. break;
  307. case HOST_ADDRESS:
  308. if (h->entry.displayAddress.connectionType == connectionType &&
  309. XdmcpARRAY8Equal( &h->entry.displayAddress.hostAddress,
  310. clientAddress ))
  311. return 1;
  312. break;
  313. default:
  314. break;
  315. }
  316. }
  317. return 0;
  318. }
  319. static AclEntry *
  320. matchAclEntry( ARRAY8Ptr clientAddress, CARD16 connectionType, int direct )
  321. {
  322. AclEntry *e, *re;
  323. char *clientName = 0;
  324. int ne;
  325. for (e = accData->acList, ne = accData->nAcls, re = 0; ne; ne--, e++)
  326. if (!e->nhosts == direct)
  327. if (scanEntrylist( e->entries, e->nentries,
  328. clientAddress, connectionType,
  329. &clientName ))
  330. {
  331. re = e;
  332. break;
  333. }
  334. if (clientName)
  335. free( clientName );
  336. return re;
  337. }
  338. /*
  339. * calls the given function for each valid indirect entry. Returns TRUE if
  340. * the local host exists on any of the lists, else FALSE
  341. */
  342. int
  343. ForEachMatchingIndirectHost( ARRAY8Ptr clientAddress,
  344. CARD16 connectionType,
  345. ChooserFunc function, char *closure )
  346. {
  347. AclEntry *e;
  348. int haveLocalhost = 0;
  349. e = matchAclEntry( clientAddress, connectionType, 0 );
  350. if (e && !(e->flags & a_notAllowed)) {
  351. if (e->flags & a_useChooser) {
  352. ARRAY8Ptr choice;
  353. choice = IndirectChoice( clientAddress, connectionType );
  354. if (!choice || XdmcpARRAY8Equal( getLocalAddress(), choice ))
  355. haveLocalhost = 1;
  356. else
  357. (*function)( connectionType, choice, closure );
  358. } else
  359. scanHostlist( e->hosts, e->nhosts, clientAddress, connectionType,
  360. function, closure, FALSE, &haveLocalhost );
  361. }
  362. return haveLocalhost;
  363. }
  364. int
  365. UseChooser( ARRAY8Ptr clientAddress, CARD16 connectionType )
  366. {
  367. AclEntry *e;
  368. e = matchAclEntry( clientAddress, connectionType, 0 );
  369. return e && !(e->flags & a_notAllowed) && (e->flags & a_useChooser) &&
  370. !IndirectChoice( clientAddress, connectionType );
  371. }
  372. void
  373. ForEachChooserHost( ARRAY8Ptr clientAddress, CARD16 connectionType,
  374. ChooserFunc function, char *closure )
  375. {
  376. AclEntry *e;
  377. int haveLocalhost = 0;
  378. e = matchAclEntry( clientAddress, connectionType, 0 );
  379. if (e && !(e->flags & a_notAllowed) && (e->flags & a_useChooser))
  380. scanHostlist( e->hosts, e->nhosts, clientAddress, connectionType,
  381. function, closure, TRUE, &haveLocalhost );
  382. if (haveLocalhost)
  383. (*function)( connectionType, getLocalAddress(), closure );
  384. }
  385. /*
  386. * returns TRUE if the given client is acceptable to the local host. The
  387. * given display client is acceptable if it occurs without a host list.
  388. */
  389. int
  390. AcceptableDisplayAddress( ARRAY8Ptr clientAddress, CARD16 connectionType,
  391. xdmOpCode type )
  392. {
  393. AclEntry *e;
  394. if (type == INDIRECT_QUERY)
  395. return 1;
  396. e = matchAclEntry( clientAddress, connectionType, 1 );
  397. return e && !(e->flags & a_notAllowed) &&
  398. (type != BROADCAST_QUERY || !(e->flags & a_notBroadcast));
  399. }
  400. void
  401. ForEachListenAddr( ListenFunc listenfunction, ListenFunc mcastfunction,
  402. void **closure )
  403. {
  404. int i, j, ifc, mc, nmc;
  405. for (i = 0; i < accData->nListens; i++) {
  406. ifc = accData->listenList[i].iface;
  407. (*listenfunction)( ifc < 0 ? 0 :
  408. &accData->hostList[ifc].entry.displayAddress.hostAddress,
  409. closure );
  410. mc = accData->listenList[i].mcasts;
  411. nmc = accData->listenList[i].nmcasts;
  412. for (j = 0; j < nmc; j++, mc++)
  413. (*mcastfunction)( &accData->hostList[mc].entry.displayAddress.hostAddress,
  414. closure );
  415. }
  416. }
  417. #endif /* XDMCP */