aclutil.cpp 5.4 KB

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