1
0

lastod.cpp 6.7 KB

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