TimeData.cc
Go to the documentation of this file.
1/*
2 * Copyright (C) 1996-2023 The Squid Software Foundation and contributors
3 *
4 * Squid software is distributed under GPLv2+ license and includes
5 * contributions from numerous individuals and organizations.
6 * Please see the COPYING and CONTRIBUTORS files for details.
7 */
8
9/* DEBUG: section 28 Access Control */
10
11#include "squid.h"
12#include "acl/Checklist.h"
13#include "acl/TimeData.h"
14#include "cache_cf.h"
15#include "ConfigParser.h"
16#include "debug/Stream.h"
17#include "wordlist.h"
18
19#define ACL_SUNDAY 0x01
20#define ACL_MONDAY 0x02
21#define ACL_TUESDAY 0x04
22#define ACL_WEDNESDAY 0x08
23#define ACL_THURSDAY 0x10
24#define ACL_FRIDAY 0x20
25#define ACL_SATURDAY 0x40
26#define ACL_ALLWEEK 0x7F
27#define ACL_WEEKDAYS 0x3E
28
29ACLTimeData::ACLTimeData () : weekbits (0), start (0), stop (0), next (nullptr) {}
30
32{
33 if (next)
34 delete next;
35}
36
37bool
39{
40 static time_t last_when = 0;
41
42 static struct tm tm;
43 time_t t;
44
45 if (when != last_when) {
46 last_when = when;
47 memcpy(&tm, localtime(&when), sizeof(struct tm));
48 }
49
50 t = (time_t) (tm.tm_hour * 60 + tm.tm_min);
51 ACLTimeData *data = this;
52
53 while (data) {
54 debugs(28, 3, "aclMatchTime: checking " << t << " in " <<
55 data->start << "-" << data->stop << ", weekbits=" <<
56 std::hex << data->weekbits);
57
58 if (t >= data->start && t <= data->stop && (data->weekbits & (1 << tm.tm_wday)))
59 return 1;
60
61 data = data->next;
62 }
63
64 return 0;
65}
66
69{
70 SBufList sl;
71 const ACLTimeData *t = this;
72
73 while (t != nullptr) {
74 SBuf s;
75 s.Printf("%c%c%c%c%c%c%c %02d:%02d-%02d:%02d",
76 t->weekbits & ACL_SUNDAY ? 'S' : '-',
77 t->weekbits & ACL_MONDAY ? 'M' : '-',
78 t->weekbits & ACL_TUESDAY ? 'T' : '-',
79 t->weekbits & ACL_WEDNESDAY ? 'W' : '-',
80 t->weekbits & ACL_THURSDAY ? 'H' : '-',
81 t->weekbits & ACL_FRIDAY ? 'F' : '-',
82 t->weekbits & ACL_SATURDAY ? 'A' : '-',
83 t->start / 60, t->start % 60, t->stop / 60, t->stop % 60);
84 sl.push_back(s);
85 t = t->next;
86 }
87
88 return sl;
89}
90
91void
93{
94 ACLTimeData **Tail;
95 long parsed_weekbits = 0;
96
97 for (Tail = &next; *Tail; Tail = &((*Tail)->next));
98 ACLTimeData *q = nullptr;
99
100 int h1, m1, h2, m2;
101
102 while (char *t = ConfigParser::strtokFile()) {
103 if (*t < '0' || *t > '9') {
104 /* assume its day-of-week spec */
105
106 while (*t) {
107 switch (*t++) {
108
109 case 'S':
110 parsed_weekbits |= ACL_SUNDAY;
111 break;
112
113 case 'M':
114 parsed_weekbits |= ACL_MONDAY;
115 break;
116
117 case 'T':
118 parsed_weekbits |= ACL_TUESDAY;
119 break;
120
121 case 'W':
122 parsed_weekbits |= ACL_WEDNESDAY;
123 break;
124
125 case 'H':
126 parsed_weekbits |= ACL_THURSDAY;
127 break;
128
129 case 'F':
130 parsed_weekbits |= ACL_FRIDAY;
131 break;
132
133 case 'A':
134 parsed_weekbits |= ACL_SATURDAY;
135 break;
136
137 case 'D':
138 parsed_weekbits |= ACL_WEEKDAYS;
139 break;
140
141 case '-':
142 /* ignore placeholder */
143 break;
144
145 default:
146 debugs(28, DBG_CRITICAL, "" << cfg_filename << " line " << config_lineno <<
147 ": " << config_input_line);
148 debugs(28, DBG_CRITICAL, "ERROR: aclParseTimeSpec: Bad Day '" << *t << "'" );
149 break;
150 }
151 }
152 } else {
153 /* assume its time-of-day spec */
154
155 if ((sscanf(t, "%d:%d-%d:%d", &h1, &m1, &h2, &m2) < 4) || (!((h1 >= 0 && h1 < 24) && ((h2 >= 0 && h2 < 24) || (h2 == 24 && m2 == 0)) && (m1 >= 0 && m1 < 60) && (m2 >= 0 && m2 < 60)))) {
156 debugs(28, DBG_CRITICAL, "ERROR: aclParseTimeSpec: Bad time range '" << t << "'");
158
159 if (q != this)
160 delete q;
161
162 return;
163 }
164
165 if ((parsed_weekbits == 0) && (start == 0) && (stop == 0))
166 q = this;
167 else
168 q = new ACLTimeData;
169
170 q->start = h1 * 60 + m1;
171
172 q->stop = h2 * 60 + m2;
173
174 q->weekbits = parsed_weekbits;
175
176 parsed_weekbits = 0;
177
178 if (q->start > q->stop) {
179 debugs(28, DBG_CRITICAL, "aclParseTimeSpec: Reversed time range");
181
182 if (q != this)
183 delete q;
184
185 return;
186 }
187
188 if (q->weekbits == 0)
190
191 if (q != this) {
192 *(Tail) = q;
193 Tail = &q->next;
194 }
195 }
196 }
197
198 if (parsed_weekbits) {
199
200 q = new ACLTimeData;
201
202 q->start = 0 * 60 + 0;
203
204 q->stop = 24 * 60 + 0;
205
206 q->weekbits = parsed_weekbits;
207
208 *(Tail) = q;
209 Tail = &q->next;
210 }
211}
212
213bool
215{
216 return false;
217}
218
#define ACL_THURSDAY
Definition: TimeData.cc:23
#define ACL_WEDNESDAY
Definition: TimeData.cc:22
#define ACL_ALLWEEK
Definition: TimeData.cc:26
#define ACL_TUESDAY
Definition: TimeData.cc:21
#define ACL_MONDAY
Definition: TimeData.cc:20
#define ACL_FRIDAY
Definition: TimeData.cc:24
#define ACL_WEEKDAYS
Definition: TimeData.cc:27
#define ACL_SUNDAY
Definition: TimeData.cc:19
#define ACL_SATURDAY
Definition: TimeData.cc:25
char config_input_line[BUFSIZ]
Definition: cache_cf.cc:274
const char * cfg_filename
Definition: cache_cf.cc:272
int config_lineno
Definition: cache_cf.cc:273
void self_destruct(void)
Definition: cache_cf.cc:277
int weekbits
Definition: TimeData.h:28
SBufList dump() const override
Definition: TimeData.cc:68
~ACLTimeData() override
Definition: TimeData.cc:31
bool match(time_t) override
Definition: TimeData.cc:38
int stop
Definition: TimeData.h:30
ACLTimeData * next
Definition: TimeData.h:31
bool empty() const override
Definition: TimeData.cc:214
int start
Definition: TimeData.h:29
void parse() override
Definition: TimeData.cc:92
static char * strtokFile()
Definition: ConfigParser.cc:65
Definition: SBuf.h:94
SBuf & Printf(const char *fmt,...) PRINTF_FORMAT_ARG2
Definition: SBuf.cc:214
#define debugs(SECTION, LEVEL, CONTENT)
Definition: Stream.h:194
#define DBG_CRITICAL
Definition: Stream.h:37
std::list< SBuf > SBufList
Definition: forward.h:23

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors