lasip.cpp 18 KB

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