lastod.cpp 4.7 KB

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