aclutil.cpp 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /** BEGIN COPYRIGHT BLOCK
  2. * Copyright 2001 Sun Microsystems, Inc.
  3. * Portions copyright 1999, 2001-2003 Netscape Communications Corporation.
  4. * All rights reserved.
  5. * END COPYRIGHT BLOCK **/
  6. /*
  7. * Source file for the TimeOfDay and DayOfWeek LAS drivers
  8. */
  9. #include <netsite.h>
  10. #include <base/crit.h>
  11. /* #include <plhash.h> */
  12. #include <libaccess/acl.h>
  13. #include "aclpriv.h"
  14. #include <libaccess/las.h>
  15. #include <libaccess/nserror.h>
  16. #include "aclutil.h"
  17. /* Generic evaluator of comparison operators in attribute evaluation
  18. * statements.
  19. * INPUT
  20. * CmpOp_t ACL_TOKEN_EQ, ACL_TOKEN_NE etc.
  21. * result 0 if equal, >0 if real > pattern, <0 if
  22. * real < pattern.
  23. * RETURNS
  24. * LAS_EVAL_TRUE or LAS_EVAL_FALSE or LAS_EVAL_INVALID
  25. * DEBUG
  26. * Can add asserts that the strcmp failure cases are one of the
  27. * remaining legal comparators.
  28. */
  29. int
  30. evalComparator(CmpOp_t ctok, int result)
  31. {
  32. if (result == 0) {
  33. switch(ctok) {
  34. case CMP_OP_EQ:
  35. case CMP_OP_GE:
  36. case CMP_OP_LE:
  37. return LAS_EVAL_TRUE;
  38. case CMP_OP_NE:
  39. case CMP_OP_GT:
  40. case CMP_OP_LT:
  41. return LAS_EVAL_FALSE;
  42. default:
  43. return LAS_EVAL_INVALID;
  44. }
  45. } else if (result > 0) {
  46. switch(ctok) {
  47. case CMP_OP_GT:
  48. case CMP_OP_GE:
  49. case CMP_OP_NE:
  50. return LAS_EVAL_TRUE;
  51. case CMP_OP_LT:
  52. case CMP_OP_LE:
  53. case CMP_OP_EQ:
  54. return LAS_EVAL_FALSE;
  55. default:
  56. return LAS_EVAL_INVALID;
  57. }
  58. } else { /* real < pattern */
  59. switch(ctok) {
  60. case CMP_OP_LT:
  61. case CMP_OP_LE:
  62. case CMP_OP_NE:
  63. return LAS_EVAL_TRUE;
  64. case CMP_OP_GT:
  65. case CMP_OP_GE:
  66. case CMP_OP_EQ:
  67. return LAS_EVAL_FALSE;
  68. default:
  69. return LAS_EVAL_INVALID;
  70. }
  71. }
  72. }
  73. /* Takes a string and returns the same string with all uppercase
  74. * letters converted to lowercase.
  75. */
  76. void
  77. makelower(char *string)
  78. {
  79. while (*string) {
  80. *string = tolower(*string);
  81. string++;
  82. }
  83. }
  84. /* Given an LAS_EVAL_* value, translates to ACL_RES_* */
  85. int
  86. EvalToRes(int value)
  87. {
  88. switch (value) {
  89. case LAS_EVAL_TRUE:
  90. return ACL_RES_ALLOW;
  91. case LAS_EVAL_FALSE:
  92. return ACL_RES_DENY;
  93. case LAS_EVAL_DECLINE:
  94. return ACL_RES_FAIL;
  95. case LAS_EVAL_FAIL:
  96. return ACL_RES_FAIL;
  97. case LAS_EVAL_INVALID:
  98. return ACL_RES_INVALID;
  99. case LAS_EVAL_NEED_MORE_INFO:
  100. return ACL_RES_DENY;
  101. default:
  102. PR_ASSERT(1);
  103. return ACL_RES_ERROR;
  104. }
  105. }
  106. const char *comparator_string (int comparator)
  107. {
  108. static char invalid_cmp[32];
  109. switch(comparator) {
  110. case CMP_OP_EQ: return "CMP_OP_EQ";
  111. case CMP_OP_NE: return "CMP_OP_NE";
  112. case CMP_OP_GT: return "CMP_OP_GT";
  113. case CMP_OP_LT: return "CMP_OP_LT";
  114. case CMP_OP_GE: return "CMP_OP_GE";
  115. case CMP_OP_LE: return "CMP_OP_LE";
  116. default:
  117. sprintf(invalid_cmp, "unknown comparator %d", comparator);
  118. return invalid_cmp;
  119. }
  120. }
  121. /* Return the pointer to the next token after replacing the following 'delim'
  122. * char with NULL.
  123. * WARNING - Modifies the first parameter */
  124. char *acl_next_token (char **ptr, char delim)
  125. {
  126. char *str = *ptr;
  127. char *token = str;
  128. char *comma;
  129. if (!token) { *ptr = 0; return 0; }
  130. /* ignore leading whitespace */
  131. while(*token && isspace(*token)) token++;
  132. if (!*token) { *ptr = 0; return 0; }
  133. if ((comma = strchr(token, delim)) != NULL) {
  134. *comma++ = 0;
  135. }
  136. {
  137. /* ignore trailing whitespace */
  138. int len = strlen(token);
  139. char *sptr = token+len-1;
  140. while(*sptr == ' ' || *sptr == '\t') *sptr-- = 0;
  141. }
  142. *ptr = comma;
  143. return token;
  144. }
  145. /* Returns a pointer to the next token and it's length */
  146. /* tokens are separated by 'delim' characters */
  147. /* ignores whitespace surrounding the tokens */
  148. const char *acl_next_token_len (const char *ptr, char delim, int *len)
  149. {
  150. const char *str = ptr;
  151. const char *token = str;
  152. const char *comma;
  153. *len = 0;
  154. if (!token) { return 0; }
  155. /* ignore leading whitespace */
  156. while(*token && isspace(*token)) token++;
  157. if (!*token) { return 0; }
  158. if (*token == delim) { return token; } /* str starts with delim! */
  159. if ((comma = strchr(token, delim)) != NULL) {
  160. *len = comma - token;
  161. }
  162. else {
  163. *len = strlen(token);
  164. }
  165. {
  166. /* ignore trailing whitespace */
  167. const char *sptr = token + *len - 1;
  168. while(*sptr == ' ' || *sptr == '\t') {
  169. sptr--;
  170. (*len)--;
  171. }
  172. }
  173. return token;
  174. }
  175. /* acl_get_req_time --
  176. * If the REQ_TIME is available on the 'resource' plist, return it.
  177. * Otherwise, make a system call to get the time and insert the time on the
  178. * 'resource' PList. Allocate the time_t structure using the 'resource'
  179. * PList's pool.
  180. */
  181. time_t *acl_get_req_time (PList_t resource)
  182. {
  183. time_t *req_time = 0;
  184. int rv = PListGetValue(resource, ACL_ATTR_TIME_INDEX, (void **)&req_time,
  185. NULL);
  186. if (rv < 0) {
  187. req_time = (time_t *)pool_malloc(PListGetPool(resource), sizeof(time_t));
  188. time(req_time);
  189. PListInitProp(resource, ACL_ATTR_TIME_INDEX, ACL_ATTR_TIME,
  190. (void *)req_time, NULL);
  191. }
  192. return req_time;
  193. }