lasdns.cpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409
  1. /** BEGIN COPYRIGHT BLOCK
  2. * This Program is free software; you can redistribute it and/or modify it under
  3. * the terms of the GNU General Public License as published by the Free Software
  4. * Foundation; version 2 of the License.
  5. *
  6. * This Program is distributed in the hope that it will be useful, but WITHOUT
  7. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  8. * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  9. *
  10. * You should have received a copy of the GNU General Public License along with
  11. * this Program; if not, write to the Free Software Foundation, Inc., 59 Temple
  12. * Place, Suite 330, Boston, MA 02111-1307 USA.
  13. *
  14. * In addition, as a special exception, Red Hat, Inc. gives You the additional
  15. * right to link the code of this Program with code not covered under the GNU
  16. * General Public License ("Non-GPL Code") and to distribute linked combinations
  17. * including the two, subject to the limitations in this paragraph. Non-GPL Code
  18. * permitted under this exception must only link to the code of this Program
  19. * through those well defined interfaces identified in the file named EXCEPTION
  20. * found in the source code files (the "Approved Interfaces"). The files of
  21. * Non-GPL Code may instantiate templates or use macros or inline functions from
  22. * the Approved Interfaces without causing the resulting work to be covered by
  23. * the GNU General Public License. Only Red Hat, Inc. may make changes or
  24. * additions to the list of Approved Interfaces. You must obey the GNU General
  25. * Public License in all respects for all of the Program code and other code used
  26. * in conjunction with the Program except the Non-GPL Code covered by this
  27. * exception. If you modify this file, you may extend this exception to your
  28. * version of the file, but you are not obligated to do so. If you do not wish to
  29. * provide this exception without modification, you must delete this exception
  30. * statement from your version and license this file solely under the GPL without
  31. * exception.
  32. *
  33. *
  34. * Copyright (C) 2001 Sun Microsystems, Inc. Used by permission.
  35. * Copyright (C) 2005 Red Hat, Inc.
  36. * All rights reserved.
  37. * END COPYRIGHT BLOCK **/
  38. /* lasdns.c
  39. * This file contains the DNS LAS code.
  40. */
  41. #include <stdio.h>
  42. #include <string.h>
  43. #ifdef XP_WIN32
  44. /* #include <winsock2.h> */
  45. #include <winsock.h>
  46. #else
  47. #include <sys/types.h>
  48. #include <sys/socket.h>
  49. #include <netinet/in.h>
  50. #include <arpa/inet.h>
  51. #include <netdb.h>
  52. #endif
  53. #include <netsite.h>
  54. extern "C" {
  55. #include <prnetdb.h>
  56. }
  57. #include <base/plist.h>
  58. #include <base/pool.h>
  59. #include <libaccess/nserror.h>
  60. #include <libaccess/nsauth.h>
  61. #include <libaccess/acl.h>
  62. #include "aclpriv.h"
  63. #include <libaccess/aclproto.h>
  64. #include <libaccess/las.h>
  65. #include "lasdns.h"
  66. #include "aclutil.h"
  67. #include "aclcache.h"
  68. #include "permhash.h"
  69. #include <libaccess/dbtlibaccess.h>
  70. #include <libaccess/aclerror.h>
  71. #include "access_plhash.h"
  72. extern "C" {
  73. #include <nspr.h>
  74. }
  75. #ifdef UTEST
  76. extern int LASDnsGetDns(char **dnsv);
  77. #endif
  78. /* LASDnsMatch
  79. * Given an array of fully-qualified dns names, tries to match them
  80. * against a given hash table.
  81. * INPUT
  82. * dns DNS string
  83. * context pointer to an LAS DNS context structure
  84. *
  85. * In our usage, this context is derived from
  86. * an acllist cache, which is a representation of
  87. * of some global acis--in other words it's a global thing
  88. * shared between threads--hence the use
  89. * of the "read-only" hash lookup routines here.
  90. */
  91. int
  92. LASDnsMatch(char *token, LASDnsContext_t *context)
  93. {
  94. /* Test for the unusual case where "*" is allowed */
  95. if (ACL_HashTableLookup_const(context->Table, "*"))
  96. return LAS_EVAL_TRUE;
  97. /* Start with the full name. Then strip off each component
  98. * leaving the remainder starting with a period. E.g.
  99. * splash.mcom.com
  100. * .mcom.com
  101. * .com
  102. * Search the hash table for each remaining portion. Remember that
  103. * wildcards were put in with the leading period intact.
  104. */
  105. do {
  106. if (ACL_HashTableLookup_const(context->Table, token))
  107. return LAS_EVAL_TRUE;
  108. token = strchr(&token[1], '.');
  109. } while (token != NULL);
  110. return LAS_EVAL_FALSE;
  111. }
  112. /* LASDNSBuild
  113. * Builds a hash table of all the hostnames provided (plus their aliases
  114. * if aliasflg is true). Wildcards are only permitted in the leftmost
  115. * field. They're represented in the hash table by a leading period.
  116. * E.g. ".mcom.com".
  117. *
  118. * RETURNS Zero on success, else LAS_EVAL_INVALID
  119. */
  120. int
  121. LASDnsBuild(NSErr_t *errp, char *attr_pattern, LASDnsContext_t *context, int aliasflg)
  122. {
  123. size_t delimiter; /* length of valid token */
  124. char token[256]; /* max length dns name */
  125. int i;
  126. int ipcnt;
  127. char **p;
  128. unsigned long *ipaddrs=0;
  129. pool_handle_t *pool;
  130. PRStatus error=PR_SUCCESS;
  131. char buffer[PR_NETDB_BUF_SIZE];
  132. #ifdef UTEST
  133. struct hostent *he, host;
  134. #else
  135. PRHostEnt *he, host;
  136. #endif
  137. context->Table = PR_NewHashTable(0,
  138. PR_HashCaseString,
  139. PR_CompareCaseStrings,
  140. PR_CompareValues,
  141. &ACLPermAllocOps,
  142. NULL);
  143. pool = pool_create();
  144. context->pool = pool;
  145. if ((!context->Table) || (!context->pool)) {
  146. nserrGenerate(errp, ACLERRNOMEM, ACLERR4700, ACL_Program, 1,
  147. XP_GetAdminStr(DBT_lasdnsbuildUnableToAllocateHashT_));
  148. return LAS_EVAL_INVALID;
  149. }
  150. do {
  151. size_t maxsize = sizeof(token);
  152. /* Get a single hostname from the pattern string */
  153. delimiter = strcspn(attr_pattern, ", \t");
  154. if (delimiter >= maxsize) {
  155. delimiter = maxsize-1;
  156. }
  157. PL_strncpyz(token, attr_pattern, delimiter + 1);
  158. token[delimiter] = '\0';
  159. /* Skip any white space after the token */
  160. attr_pattern += delimiter;
  161. attr_pattern += strspn(attr_pattern, ", \t");
  162. /* If there's a wildcard, strip it off but leave the "."
  163. * Can't have aliases for a wildcard pattern.
  164. * Treat "*" as a special case. If so, go ahead and hash it.
  165. */
  166. if (token[0] == '*') {
  167. if (token[1] != '\0') {
  168. if (!PR_HashTableAdd(context->Table, pool_strdup(pool, &token[1]), (void *)-1)) {
  169. nserrGenerate(errp, ACLERRFAIL, ACLERR4710, ACL_Program, 2,
  170. XP_GetAdminStr(DBT_lasdnsbuildUnableToAddKeySN_), token);
  171. return LAS_EVAL_INVALID;
  172. }
  173. } else {
  174. if (!PR_HashTableAdd(context->Table, pool_strdup(pool, token), (void *)-1)) {
  175. nserrGenerate(errp, ACLERRFAIL, ACLERR4720, ACL_Program, 2, XP_GetAdminStr(DBT_lasdnsbuildUnableToAddKeySN_), token);
  176. return LAS_EVAL_INVALID;
  177. }
  178. }
  179. } else {
  180. /* This is a single hostname add it to the hash table */
  181. if (!PR_HashTableAdd(context->Table, pool_strdup(pool, &token[0]), (void *)-1)) {
  182. nserrGenerate(errp, ACLERRFAIL, ACLERR4730, ACL_Program, 2, XP_GetAdminStr(DBT_lasdnsbuildUnableToAddKeySN_), token);
  183. return LAS_EVAL_INVALID;
  184. }
  185. if (aliasflg) {
  186. /* memset(buffer, '\0', PR_NETDB_BUF_SIZE);
  187. */
  188. #ifdef UTEST
  189. he = gethostbyname(token);
  190. #else
  191. error = PR_GetHostByName(token,
  192. buffer,
  193. PR_NETDB_BUF_SIZE,
  194. &host);
  195. if (error == PR_SUCCESS) he = &host;
  196. else he = NULL;
  197. #endif
  198. if (he) {
  199. /* Make a copy of the list of IP addresses if any */
  200. if (he->h_addr_list && he->h_addr_list[0]) {
  201. /* Count the IP addresses */
  202. for (p = he->h_addr_list, ipcnt = 0; *p; ++p) {
  203. ++ipcnt;
  204. }
  205. /* Allocate space */
  206. ipaddrs = (unsigned long *)PERM_MALLOC(ipcnt * sizeof(unsigned long));
  207. /* Copy IP addresses */
  208. for (i = 0; i < ipcnt; ++i) {
  209. ipaddrs[i] = 0;
  210. memcpy((void *)&ipaddrs[i], he->h_addr_list[i], 4);
  211. }
  212. }
  213. /* Add each of the aliases to the list */
  214. if (he->h_aliases && he->h_aliases[0]) {
  215. for (p = he->h_aliases; *p; ++p) {
  216. /* Add it to the hash table */
  217. if (!PR_HashTableAdd(context->Table, pool_strdup(pool, *p), (void*)-1)) {
  218. nserrGenerate(errp, ACLERRFAIL, ACLERR4740, ACL_Program, 2,
  219. XP_GetAdminStr(DBT_lasdnsbuildUnableToAddKeySN_), *p);
  220. PERM_FREE(ipaddrs);
  221. return LAS_EVAL_INVALID;
  222. }
  223. }
  224. }
  225. for (i = 0; i < ipcnt; ++i) {
  226. #ifdef UTEST
  227. he = gethostbyaddr((char *)&ipaddrs[i], 4, AF_INET);
  228. #else
  229. error = PR_GetHostByAddr((PRNetAddr *)&ipaddrs[i],
  230. buffer,
  231. PR_NETDB_BUF_SIZE,
  232. &host);
  233. if (error == PR_SUCCESS) he = &host;
  234. else he = NULL;
  235. #endif
  236. if (he) {
  237. if (he->h_name) {
  238. /* Add it to the hash table */
  239. if (!PR_HashTableAdd(context->Table, pool_strdup(pool, he->h_name),
  240. (void *)-1)) {
  241. nserrGenerate(errp, ACLERRFAIL, ACLERR4750, ACL_Program, 2,
  242. XP_GetAdminStr(DBT_lasdnsbuildUnableToAddKeySN_), he->h_name);
  243. PERM_FREE(ipaddrs);
  244. return LAS_EVAL_INVALID;
  245. }
  246. }
  247. if (he->h_aliases && he->h_aliases[0]) {
  248. for (p = he->h_aliases; *p; ++p) {
  249. /* Add it to the hash table */
  250. if (!PR_HashTableAdd(context->Table, pool_strdup(pool, *p), (void *)-1)) {
  251. nserrGenerate(errp, ACLERRFAIL, ACLERR4760, ACL_Program, 2,
  252. XP_GetAdminStr(DBT_lasdnsbuildUnableToAddKeySN_), *p);
  253. PERM_FREE(ipaddrs);
  254. return LAS_EVAL_INVALID;
  255. }
  256. }
  257. }
  258. }
  259. }
  260. PERM_FREE(ipaddrs);
  261. } /* if he */
  262. } /* if aliasflg */
  263. } /* else - single hostname */
  264. } while ((attr_pattern != NULL) &&
  265. (attr_pattern[0] != '\0') &&
  266. (delimiter != 0));
  267. return 0;
  268. }
  269. /* LASDnsFlush
  270. * Given the address of a las_cookie for a DNS expression entry, frees up
  271. * all allocated memory for it. This includes the hash table, plus the
  272. * context structure.
  273. */
  274. void
  275. LASDnsFlush(void **las_cookie)
  276. {
  277. if (*las_cookie == NULL)
  278. return;
  279. pool_destroy(((LASDnsContext_t *)*las_cookie)->pool);
  280. PR_HashTableDestroy(((LASDnsContext_t *)*las_cookie)->Table);
  281. PERM_FREE(*las_cookie);
  282. *las_cookie = NULL;
  283. return;
  284. }
  285. /*
  286. * LASDnsEval
  287. * INPUT
  288. * attr_name The string "dns" - in lower case.
  289. * comparator CMP_OP_EQ or CMP_OP_NE only
  290. * attr_pattern A comma-separated list of DNS names
  291. * Any segment(s) in a DNS name can be wildcarded using
  292. * "*". Note that this is not a true Regular Expression
  293. * form.
  294. * *cachable Always set to ACL_INDEF_CACHE
  295. * subject Subject property list
  296. * resource Resource property list
  297. * auth_info Authentication info, if any
  298. * RETURNS
  299. * ret code The usual LAS return codes.
  300. */
  301. int LASDnsEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator,
  302. char *attr_pattern, ACLCachable_t *cachable, void **LAS_cookie,
  303. PList_t subject, PList_t resource,
  304. PList_t auth_info, PList_t global_auth)
  305. {
  306. int result;
  307. int aliasflg;
  308. char *my_dns;
  309. LASDnsContext_t *context;
  310. int rv;
  311. *cachable = ACL_INDEF_CACHABLE;
  312. if (strcmp(attr_name, "dns") == 0)
  313. aliasflg = 0;
  314. else if (strcmp(attr_name, "dnsalias") == 0)
  315. aliasflg = 1;
  316. else {
  317. nserrGenerate(errp, ACLERRINVAL, ACLERR4800, ACL_Program, 2, XP_GetAdminStr(DBT_lasDnsBuildReceivedRequestForAtt_), attr_name);
  318. return LAS_EVAL_INVALID;
  319. }
  320. if ((comparator != CMP_OP_EQ) && (comparator != CMP_OP_NE)) {
  321. nserrGenerate(errp, ACLERRINVAL, ACLERR4810, ACL_Program, 2, XP_GetAdminStr(DBT_lasdnsevalIllegalComparatorDN_), comparator_string(comparator));
  322. return LAS_EVAL_INVALID;
  323. }
  324. /* If this is the first time through, build the pattern tree first. */
  325. if (*LAS_cookie == NULL) {
  326. ACL_CritEnter();
  327. if (*LAS_cookie == NULL) { /* Must check again */
  328. *LAS_cookie = context =
  329. (LASDnsContext_t *)PERM_MALLOC(sizeof(LASDnsContext_t));
  330. if (context == NULL) {
  331. nserrGenerate(errp, ACLERRNOMEM, ACLERR4820, ACL_Program, 1, XP_GetAdminStr(DBT_lasdnsevalUnableToAllocateContex_));
  332. ACL_CritExit();
  333. return LAS_EVAL_FAIL;
  334. }
  335. context->Table = NULL;
  336. LASDnsBuild(errp, attr_pattern, context, aliasflg);
  337. }
  338. ACL_CritExit();
  339. } else
  340. context = (LASDnsContext *) *LAS_cookie;
  341. /* Call the DNS attribute getter */
  342. #ifdef UTEST
  343. LASDnsGetDns(&my_dns); /* gets stuffed on return */
  344. #else
  345. rv = ACL_GetAttribute(errp, ACL_ATTR_DNS, (void **)&my_dns,
  346. subject, resource, auth_info, global_auth);
  347. if (rv != LAS_EVAL_TRUE) {
  348. if (subject || resource) {
  349. char rv_str[16];
  350. /* Don't ereport if called from ACL_CachableAclList */
  351. sprintf(rv_str, "%d", rv);
  352. nserrGenerate(errp, ACLERRINVAL, ACLERR4830, ACL_Program, 2, XP_GetAdminStr(DBT_lasdnsevalUnableToGetDnsErrorDN_), rv_str);
  353. }
  354. return LAS_EVAL_FAIL;
  355. }
  356. #endif
  357. result = LASDnsMatch(my_dns, context);
  358. if (comparator == CMP_OP_NE) {
  359. if (result == LAS_EVAL_FALSE)
  360. return LAS_EVAL_TRUE;
  361. else if (result == LAS_EVAL_TRUE)
  362. return LAS_EVAL_FALSE;
  363. }
  364. return (result);
  365. }