aclutil.cpp 7.3 KB

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