lastod.cpp 5.3 KB

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