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