aclutil.cpp 7.1 KB

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