lasip.cpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519
  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. /* aclip.c
  39. * This file contains the IP LAS code.
  40. */
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include <netsite.h>
  44. #include <base/plist.h>
  45. #include <libaccess/nserror.h>
  46. #include <libaccess/nsauth.h>
  47. #include <libaccess/acl.h>
  48. #include "aclpriv.h"
  49. #include <libaccess/aclproto.h>
  50. #include <libaccess/las.h>
  51. #include "lasip.h"
  52. #include "aclutil.h"
  53. #include "aclcache.h"
  54. #include <libaccess/dbtlibaccess.h>
  55. #include <libaccess/aclerror.h>
  56. #define LAS_IP_IS_CONSTANT(x) (((x) == (LASIpTree_t *)LAS_EVAL_TRUE) || ((x) == (LASIpTree_t *)LAS_EVAL_FALSE))
  57. #ifdef UTEST
  58. extern int LASIpGetIp();
  59. #endif
  60. static int
  61. LASIpAddPattern(NSErr_t *errp, int netmask, int pattern, LASIpTree_t **treetop);
  62. /* dotdecimal
  63. * Takes netmask and ip strings and returns the numeric values,
  64. * accounting for wildards in the ip specification. Wildcards in the
  65. * ip override the netmask where they conflict.
  66. * INPUT
  67. * ipstr e.g. "123.45.67.89"
  68. * netmaskstr e.g. "255.255.255.0"
  69. * RETURNS
  70. * *ip
  71. * *netmask e.g. 0xffffff00
  72. * result NULL on success or else one of the LAS_EVAL_* codes.
  73. */
  74. int
  75. dotdecimal(char *ipstr, char *netmaskstr, int *ip, int *netmask)
  76. {
  77. int i;
  78. char token[64];
  79. char *dotptr; /* location of the "." */
  80. int dotidx; /* index of the period char */
  81. /* Sanity check the patterns */
  82. /* Netmask can only have digits and periods. */
  83. if (strcspn(netmaskstr, "0123456789."))
  84. return LAS_EVAL_INVALID;
  85. /* IP can only have digits, periods and "*" */
  86. if (strcspn(ipstr, "0123456789.*"))
  87. return LAS_EVAL_INVALID;
  88. *netmask = *ip = 0; /* Start with "don't care" */
  89. for (i=0; i<4; i++) {
  90. dotptr = strchr(netmaskstr, '.');
  91. /* copy out the token, then point beyond it */
  92. if (dotptr == NULL)
  93. strcpy(token, netmaskstr);
  94. else {
  95. dotidx = dotptr-netmaskstr;
  96. strncpy(token, netmaskstr, dotidx);
  97. token[dotidx] = '\0';
  98. netmaskstr = ++dotptr; /* skip the period */
  99. }
  100. /* Turn into a number and shift left as appropriate */
  101. *netmask += (atoi(token))<<(8*(4-i-1));
  102. if (dotptr == NULL)
  103. break;
  104. }
  105. for (i=0; i<4; i++) {
  106. dotptr = strchr(ipstr, '.');
  107. /* copy out the token, then point beyond it */
  108. if (dotptr == NULL)
  109. strcpy(token, ipstr);
  110. else {
  111. dotidx = dotptr-ipstr;
  112. strncpy(token, ipstr, dotidx);
  113. token[dotidx] = '\0';
  114. ipstr = ++dotptr;
  115. }
  116. /* check for wildcard */
  117. if (strcmp(token, "*") == 0) {
  118. switch(i) {
  119. case 0:
  120. if (dotptr == NULL)
  121. *netmask &= 0x00000000;
  122. else
  123. *netmask &= 0x00ffffff;
  124. break;
  125. case 1:
  126. if (dotptr == NULL)
  127. *netmask &= 0xff000000;
  128. else
  129. *netmask &= 0xff00ffff;
  130. break;
  131. case 2:
  132. if (dotptr == NULL)
  133. *netmask &= 0xffff0000;
  134. else
  135. *netmask &= 0xffff00ff;
  136. break;
  137. case 3:
  138. *netmask &= 0xffffff00;
  139. break;
  140. }
  141. continue;
  142. } else {
  143. /* Turn into a number and shift left as appropriate */
  144. *ip += (atoi(token))<<(8*(4-i-1));
  145. }
  146. /* check for end of string */
  147. if (dotptr == NULL) {
  148. switch(i) {
  149. case 0:
  150. *netmask &= 0xff000000;
  151. break;
  152. case 1:
  153. *netmask &= 0xffff0000;
  154. break;
  155. case 2:
  156. *netmask &= 0xffffff00;
  157. break;
  158. }
  159. break;
  160. }
  161. }
  162. return 0;
  163. }
  164. /* LASIpTreeAlloc
  165. * Malloc a node and set the actions to LAS_EVAL_FALSE
  166. */
  167. static LASIpTree_t *
  168. LASIpTreeAllocNode(NSErr_t *errp)
  169. {
  170. LASIpTree_t *newnode;
  171. newnode = (LASIpTree_t *)PERM_MALLOC(sizeof(LASIpTree_t));
  172. if (newnode == NULL) {
  173. nserrGenerate(errp, ACLERRNOMEM, ACLERR5000, ACL_Program, 1, XP_GetAdminStr(DBT_lasiptreeallocNoMemoryN_));
  174. return NULL;
  175. }
  176. newnode->action[0] = (LASIpTree_t *)LAS_EVAL_FALSE;
  177. newnode->action[1] = (LASIpTree_t *)LAS_EVAL_FALSE;
  178. return newnode;
  179. }
  180. /* LASIpTreeDealloc
  181. * Deallocates a Tree starting from a given node down.
  182. * INPUT
  183. * startnode Starting node to remove. Could be a constant in
  184. * which case, just return success.
  185. * OUTPUT
  186. * N/A
  187. */
  188. static void
  189. LASIpTreeDealloc(LASIpTree_t *startnode)
  190. {
  191. int i;
  192. if (startnode == NULL)
  193. return;
  194. /* If this is just a constant then we're done */
  195. if (LAS_IP_IS_CONSTANT(startnode))
  196. return;
  197. /* Else recursively call ourself for each branch down */
  198. for (i=0; i<2; i++) {
  199. if (!(LAS_IP_IS_CONSTANT(startnode->action[i])))
  200. LASIpTreeDealloc(startnode->action[i]);
  201. }
  202. /* Now deallocate the local node */
  203. PERM_FREE(startnode);
  204. }
  205. /*
  206. * LASIpBuild
  207. * INPUT
  208. * attr_name The string "ip" - in lower case.
  209. * comparator CmpOpEQ or CmpOpNE only
  210. * attr_pattern A comma-separated list of IP addresses and netmasks
  211. * in dotted-decimal form. Netmasks are optionally
  212. * prepended to the IP address using a plus sign. E.g.
  213. * 255.255.255.0+123.45.67.89. Any byte in the IP address
  214. * (but not the netmask) can be wildcarded using "*"
  215. * context A pointer to the IP LAS context structure.
  216. * RETURNS
  217. * ret code The usual LAS return codes.
  218. */
  219. static int
  220. LASIpBuild(NSErr_t *errp, char *attr_name, CmpOp_t comparator, char *attr_pattern, LASIpTree_t **treetop)
  221. {
  222. unsigned int delimiter; /* length of valid token */
  223. char token[64], token2[64]; /* a single ip[+netmask] */
  224. char *curptr; /* current place in attr_pattern */
  225. int netmask, ip;
  226. char *plusptr;
  227. int retcode;
  228. /* ip address can be delimited by space, tab, comma, or carriage return
  229. * only.
  230. */
  231. curptr = attr_pattern;
  232. do {
  233. delimiter = strcspn(curptr, ", \t");
  234. delimiter = (delimiter <= strlen(curptr)) ? delimiter : strlen(curptr);
  235. strncpy(token, curptr, delimiter);
  236. token[delimiter] = '\0';
  237. /* skip all the white space after the token */
  238. curptr = strpbrk((curptr+delimiter), "1234567890+.*");
  239. /* Is there a netmask? */
  240. plusptr = strchr(token, '+');
  241. if (plusptr == NULL) {
  242. if (curptr && (*curptr == '+')) {
  243. /* There was a space before (and possibly after) the plus sign*/
  244. curptr = strpbrk((++curptr), "1234567890.*");
  245. delimiter = strcspn(curptr, ", \t");
  246. delimiter = (delimiter <= strlen(curptr)) ? delimiter : strlen(curptr);
  247. strncpy(token2, curptr, delimiter);
  248. token2[delimiter] = '\0';
  249. retcode = dotdecimal(token, token2, &ip, &netmask);
  250. if (retcode)
  251. return (retcode);
  252. curptr = strpbrk((++curptr), "1234567890+.*");
  253. } else {
  254. retcode =dotdecimal(token, "255.255.255.255", &ip, &netmask);
  255. if (retcode)
  256. return (retcode);
  257. }
  258. } else {
  259. /* token is the IP addr string in both cases */
  260. *plusptr ='\0'; /* truncate the string */
  261. retcode =dotdecimal(token, ++plusptr, &ip, &netmask);
  262. if (retcode)
  263. return (retcode);
  264. }
  265. if (LASIpAddPattern(errp, netmask, ip, treetop) != 0)
  266. return LAS_EVAL_INVALID;
  267. } while ((curptr != NULL) && (delimiter != 0));
  268. return 0;
  269. }
  270. /* LASIpAddPattern
  271. * Takes a netmask and IP address and a pointer to an existing IP
  272. * tree and adds nodes as appropriate to recognize the new pattern.
  273. * INPUT
  274. * netmask e.g. 0xffffff00
  275. * pattern IP address in 4 bytes
  276. * *treetop An existing IP tree or 0 if a new tree
  277. * RETURNS
  278. * ret code NULL on success, ACL_RES_ERROR on failure
  279. * **treetop If this is a new tree, the head of the tree.
  280. */
  281. static int
  282. LASIpAddPattern(NSErr_t *errp, int netmask, int pattern, LASIpTree_t **treetop)
  283. {
  284. int stopbit; /* Don't care after this point */
  285. int curbit; /* current bit we're working on */
  286. int curval; /* value of pattern[curbit] */
  287. LASIpTree_t *curptr; /* pointer to the current node */
  288. LASIpTree_t *newptr;
  289. /* stop at the first 1 in the netmask from low to high */
  290. for (stopbit=0; stopbit<32; stopbit++) {
  291. if ((netmask&(1<<stopbit)) != 0)
  292. break;
  293. }
  294. /* Special case if there's no tree. Allocate the first node */
  295. if (*treetop == (LASIpTree_t *)NULL) { /* No tree at all */
  296. curptr = LASIpTreeAllocNode(errp);
  297. if (curptr == NULL) {
  298. nserrGenerate(errp, ACLERRFAIL, ACLERR5100, ACL_Program, 1, XP_GetAdminStr(DBT_ipLasUnableToAllocateTreeNodeN_));
  299. return ACL_RES_ERROR;
  300. }
  301. *treetop = curptr;
  302. }
  303. /* Special case if the netmask is 0.
  304. */
  305. if (stopbit > 31) {
  306. curptr->action[0] = (LASIpTree_t *)LAS_EVAL_TRUE;
  307. curptr->action[1] = (LASIpTree_t *)LAS_EVAL_TRUE;
  308. return 0;
  309. }
  310. /* follow the tree down the pattern path bit by bit until the
  311. * end of the tree is reached (i.e. a constant).
  312. */
  313. for (curbit=31,curptr=*treetop; curbit >= 0; curbit--) {
  314. /* Is the current bit ON? If so set curval to 1 else 0 */
  315. curval = (pattern & (1<<curbit)) ? 1 : 0;
  316. /* Are we done, if so remove the rest of the tree */
  317. if (curbit == stopbit) {
  318. LASIpTreeDealloc(curptr->action[curval]);
  319. curptr->action[curval] =
  320. (LASIpTree_t *)LAS_EVAL_TRUE;
  321. /* This is the normal exit point. Most other
  322. * exits must be due to errors.
  323. */
  324. return 0;
  325. }
  326. /* Oops reached the end - must allocate */
  327. if (LAS_IP_IS_CONSTANT(curptr->action[curval])) {
  328. newptr = LASIpTreeAllocNode(errp);
  329. if (newptr == NULL) {
  330. LASIpTreeDealloc(*treetop);
  331. nserrGenerate(errp, ACLERRFAIL, ACLERR5110, ACL_Program, 1, XP_GetAdminStr(DBT_ipLasUnableToAllocateTreeNodeN_1));
  332. return ACL_RES_ERROR;
  333. }
  334. curptr->action[curval] = newptr;
  335. }
  336. /* Keep going down the tree */
  337. curptr = curptr->action[curval];
  338. }
  339. return ACL_RES_ERROR;
  340. }
  341. /* LASIpFlush
  342. * Deallocates any memory previously allocated by the LAS
  343. */
  344. void
  345. LASIpFlush(void **las_cookie)
  346. {
  347. if (*las_cookie == NULL)
  348. return;
  349. LASIpTreeDealloc(((LASIpContext_t *)*las_cookie)->treetop);
  350. PERM_FREE(*las_cookie);
  351. *las_cookie = NULL;
  352. return;
  353. }
  354. /*
  355. * LASIpEval
  356. * INPUT
  357. * attr_name The string "ip" - in lower case.
  358. * comparator CMP_OP_EQ or CMP_OP_NE only
  359. * attr_pattern A comma-separated list of IP addresses and netmasks
  360. * in dotted-decimal form. Netmasks are optionally
  361. * prepended to the IP address using a plus sign. E.g.
  362. * 255.255.255.0+123.45.67.89. Any byte in the IP address
  363. * (but not the netmask) can be wildcarded using "*"
  364. * *cachable Always set to ACL_INDEF_CACHABLE
  365. * subject Subject property list
  366. * resource Resource property list
  367. * auth_info The authentication info if any
  368. * RETURNS
  369. * ret code The usual LAS return codes.
  370. */
  371. int LASIpEval(NSErr_t *errp, char *attr_name, CmpOp_t comparator,
  372. char *attr_pattern, ACLCachable_t *cachable, void **LAS_cookie,
  373. PList_t subject, PList_t resource, PList_t auth_info,
  374. PList_t global_auth)
  375. {
  376. int bit;
  377. int value;
  378. IPAddr_t ip;
  379. int retcode;
  380. LASIpTree_t *node;
  381. LASIpContext_t *context;
  382. int rv;
  383. char ip_str[124];
  384. *cachable = ACL_INDEF_CACHABLE;
  385. if (strcmp(attr_name, "ip") != 0) {
  386. nserrGenerate(errp, ACLERRINVAL, ACLERR5200, ACL_Program, 2, XP_GetAdminStr(DBT_lasIpBuildReceivedRequestForAttr_), attr_name);
  387. return LAS_EVAL_INVALID;
  388. }
  389. if ((comparator != CMP_OP_EQ) && (comparator != CMP_OP_NE)) {
  390. nserrGenerate(errp, ACLERRINVAL, ACLERR5210, ACL_Program, 2, XP_GetAdminStr(DBT_lasipevalIllegalComparatorDN_), comparator_string(comparator));
  391. return LAS_EVAL_INVALID;
  392. }
  393. /* GET THE IP ADDR FROM THE SESSION CONTEXT AND STORE IT IN THE
  394. * VARIABLE ip.
  395. */
  396. #ifndef UTEST
  397. rv = ACL_GetAttribute(errp, ACL_ATTR_IP, (void **)&ip,
  398. subject, resource, auth_info, global_auth);
  399. if (rv != LAS_EVAL_TRUE) {
  400. if (subject || resource) {
  401. /* Don't ereport if called from ACL_CachableAclList */
  402. char rv_str[16];
  403. sprintf(rv_str, "%d", rv);
  404. nserrGenerate(errp, ACLERRINVAL, ACLERR5220, ACL_Program, 2, XP_GetAdminStr(DBT_lasipevalUnableToGetSessionAddre_), rv_str);
  405. }
  406. return LAS_EVAL_FAIL;
  407. }
  408. #else
  409. ip = (IPAddr_t)LASIpGetIp();
  410. #endif
  411. /* If this is the first time through, build the pattern tree first.
  412. */
  413. if (*LAS_cookie == NULL) {
  414. if (strcspn(attr_pattern, "0123456789.*,+ \t")) {
  415. return LAS_EVAL_INVALID;
  416. }
  417. ACL_CritEnter();
  418. if (*LAS_cookie == NULL) { /* must check again */
  419. *LAS_cookie = context =
  420. (LASIpContext_t *)PERM_MALLOC(sizeof(LASIpContext_t));
  421. if (context == NULL) {
  422. nserrGenerate(errp, ACLERRNOMEM, ACLERR5230, ACL_Program, 1, XP_GetAdminStr(DBT_lasipevalUnableToAllocateContext_));
  423. ACL_CritExit();
  424. return LAS_EVAL_FAIL;
  425. }
  426. context->treetop = NULL;
  427. retcode = LASIpBuild(errp, attr_name, comparator, attr_pattern,
  428. &context->treetop);
  429. if (retcode) {
  430. ACL_CritExit();
  431. return (retcode);
  432. }
  433. }
  434. ACL_CritExit();
  435. } else
  436. context = (LASIpContext *) *LAS_cookie;
  437. node = context->treetop;
  438. for (bit=31; bit >=0; bit--) {
  439. value = (ip & (IPAddr_t) (1<<bit)) ? 1 : 0;
  440. if (LAS_IP_IS_CONSTANT(node->action[value]))
  441. /* Reached a result, so return it */
  442. if (comparator == CMP_OP_EQ)
  443. return((int)(PRSize)node->action[value]);
  444. else
  445. return(((int)(PRSize)node->action[value] ==
  446. LAS_EVAL_TRUE) ?
  447. LAS_EVAL_FALSE : LAS_EVAL_TRUE);
  448. else
  449. /* Move on to the next bit */
  450. node = node->action[value];
  451. }
  452. /* Cannot reach here. Even a 32 bit mismatch has a conclusion in
  453. * the pattern tree.
  454. */
  455. sprintf(ip_str, "%x", ip);
  456. nserrGenerate(errp, ACLERRINTERNAL, ACLERR5240, ACL_Program, 2, XP_GetAdminStr(DBT_lasipevalReach32BitsWithoutConcl_), ip_str);
  457. return LAS_EVAL_INVALID;
  458. }