lastod.cpp 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. /* Source file for the TimeOfDay and DayOfWeek LAS drivers
  42. */
  43. #include <time.h>
  44. #include <netsite.h>
  45. #include <base/util.h>
  46. #include <base/plist.h>
  47. #include <libaccess/nserror.h>
  48. #include <libaccess/acl.h>
  49. #include "aclpriv.h"
  50. #include <libaccess/aclproto.h>
  51. #include <libaccess/las.h>
  52. #include "aclutil.h"
  53. #include <libaccess/dbtlibaccess.h>
  54. #include <libaccess/aclerror.h>
  55. #include "plstr.h"
  56. /* Day of the week LAS driver
  57. * Note that everything is case-insensitive.
  58. * INPUT
  59. * attr must be the string "dayofweek".
  60. * comparator can only be "=" or "!=".
  61. * pattern any sequence of 3-letter day names. I.e. sun, mon,
  62. * tue, wed, thu, fri, sat. Comma delimiters can be used
  63. * but are not necessary. E.g. mon,TueweDThuFRISat
  64. * OUTPUT
  65. * cachable Will be set to ACL_NOT_CACHABLE.
  66. * return code set to LAS_EVAL_*
  67. */
  68. int
  69. LASDayOfWeekEval(NSErr_t *errp, char *attr, CmpOp_t comparator, char *pattern,
  70. ACLCachable_t *cachable, void **las_cookie, PList_t subject,
  71. PList_t resource, PList_t auth_info, PList_t global_auth)
  72. {
  73. #ifndef UTEST
  74. struct tm *tm_p, tm;
  75. #endif
  76. time_t t;
  77. char daystr[5]; /* Current local day in ddd */
  78. char lcl_pattern[512];
  79. char *compare;
  80. /* Sanity checking */
  81. if (strcmp(attr, "dayofweek") != 0) {
  82. nserrGenerate(errp, ACLERRINVAL, ACLERR5400, ACL_Program, 2, XP_GetAdminStr(DBT_unexpectedAttributeInDayofweekSN_), attr);
  83. return LAS_EVAL_INVALID;
  84. }
  85. if ((comparator != CMP_OP_EQ) && (comparator != CMP_OP_NE)) {
  86. nserrGenerate(errp, ACLERRINVAL, ACLERR5410, ACL_Program, 2, XP_GetAdminStr(DBT_illegalComparatorForDayofweekDN_), comparator_string(comparator));
  87. return LAS_EVAL_INVALID;
  88. }
  89. *cachable = ACL_NOT_CACHABLE;
  90. /* Obtain and format the local time */
  91. #ifndef UTEST
  92. t = time(NULL);
  93. tm_p = system_localtime(&t, &tm);
  94. util_strftime(daystr, "%a", tm_p);
  95. #else
  96. t = (0x1000000); /* Mon 2120 hr */
  97. strftime(daystr, 4, "%a", localtime(&t));
  98. #endif
  99. makelower(daystr);
  100. PL_strncpyz(lcl_pattern, pattern, sizeof(lcl_pattern));
  101. makelower(lcl_pattern);
  102. /* Compare the value to the pattern */
  103. compare = strstr(lcl_pattern, daystr);
  104. if ((compare != NULL) && (comparator == CMP_OP_EQ))
  105. return LAS_EVAL_TRUE;
  106. if ((compare == NULL) && (comparator == CMP_OP_NE))
  107. return LAS_EVAL_TRUE;
  108. return LAS_EVAL_FALSE;
  109. }
  110. /* Time of day LAS
  111. * INPUT
  112. * attr must be "timeofday".
  113. * comparator one of =, !=, >, <, >=, <=
  114. * pattern HHMM military 24-hour clock. E.g. 0700, 2315.
  115. * OUTPUT
  116. * cachable will be set to ACL_NOT_CACHABLE.
  117. * return code set to LAS_EVAL_*
  118. */
  119. int
  120. LASTimeOfDayEval(NSErr_t *errp, char *attr, CmpOp_t comparator, char *pattern,
  121. ACLCachable_t *cachable, void **LAS_cookie, PList_t subject,
  122. PList_t resource, PList_t auth_info, PList_t global_auth)
  123. {
  124. #ifndef UTEST
  125. struct tm *tm_p, tm;
  126. #endif
  127. time_t t;
  128. char timestr[6]; /* Current local time in HHMM */
  129. char start[6], end[6];
  130. int compare; /* >0, 0, <0 means that current time is greater, equal to, or less than the pattern */
  131. char *dash;
  132. int intpattern, inttime, intstart, intend;
  133. if (strcmp(attr, "timeofday") != 0) {
  134. nserrGenerate(errp, ACLERRINVAL, ACLERR5600, ACL_Program, 2, XP_GetAdminStr(DBT_unexpectedAttributeInTimeofdaySN_), attr);
  135. return LAS_EVAL_INVALID;
  136. }
  137. *cachable = ACL_NOT_CACHABLE;
  138. /* Obtain and format the local time */
  139. #ifndef UTEST
  140. t = time(NULL);
  141. tm_p = system_localtime(&t, &tm);
  142. util_strftime(timestr, "%H%M", tm_p);
  143. #else
  144. t = (0x1000000); /* Mon 2120 hr */
  145. strftime(timestr, 5, "%H%M", localtime(&t));
  146. #endif
  147. #ifdef DEBUG
  148. printf ("timestr = %s\n", timestr);
  149. #endif
  150. inttime = atoi(timestr);
  151. dash = strchr(pattern, '-');
  152. if (dash) {
  153. if (comparator != CMP_OP_EQ && comparator != CMP_OP_NE) {
  154. nserrGenerate(errp, ACLERRINVAL, ACLERR5610, ACL_Program, 2, XP_GetAdminStr(DBT_illegalComparatorForTimeOfDayDN_), comparator_string(comparator));
  155. return LAS_EVAL_INVALID;
  156. }
  157. if ((size_t)(dash-pattern) >= sizeof(start)) {
  158. nserrGenerate(errp, ACLERRINVAL, ACLERR5610, ACL_Program, 2, XP_GetAdminStr(DBT_illegalComparatorForTimeOfDayDN_), comparator_string(comparator));
  159. return LAS_EVAL_INVALID;
  160. }
  161. strncpy(start, pattern, dash-pattern);
  162. start[dash-pattern]='\0';
  163. intstart = atoi(start);
  164. if (strlen(dash+1) >= sizeof(end)) {
  165. nserrGenerate(errp, ACLERRINVAL, ACLERR5610, ACL_Program, 2, XP_GetAdminStr(DBT_illegalComparatorForTimeOfDayDN_), comparator_string(comparator));
  166. return LAS_EVAL_INVALID;
  167. }
  168. strcpy(end, dash+1);
  169. intend = atoi(end);
  170. if (intend >= intstart) {
  171. return(evalComparator(comparator, !(inttime >= intstart && inttime <= intend)));
  172. } else { /* range wraps around midnight */
  173. return(evalComparator(comparator, !(inttime >= intstart || inttime <= intend)));
  174. }
  175. }
  176. /* ELSE - Just a single time value given. */
  177. /* Compare the value to the pattern */
  178. intpattern = atoi(pattern);
  179. compare = inttime - intpattern;
  180. /* Test against what the user wanted done */
  181. return(evalComparator(comparator, compare));
  182. }
  183. void
  184. LASDayOfWeekFlush(void **cookie)
  185. {
  186. return;
  187. }
  188. void
  189. LASTimeOfDayFlush(void **cookie)
  190. {
  191. return;
  192. }