]> git.ipfire.org Git - thirdparty/squid.git/blob - src/acl/TimeData.cc
protos.h refactoring, part one.
[thirdparty/squid.git] / src / acl / TimeData.cc
1 /*
2 * $Id$
3 *
4 * DEBUG: section 28 Access Control
5 * AUTHOR: Duane Wessels
6 *
7 * SQUID Web Proxy Cache http://www.squid-cache.org/
8 * ----------------------------------------------------------
9 *
10 * Squid is the result of efforts by numerous individuals from
11 * the Internet community; see the CONTRIBUTORS file for full
12 * details. Many organizations have provided support for Squid's
13 * development; see the SPONSORS file for full details. Squid is
14 * Copyrighted (C) 2001 by the Regents of the University of
15 * California; see the COPYRIGHT file for full details. Squid
16 * incorporates software developed and/or copyrighted by other
17 * sources; see the CREDITS file for full details.
18 *
19 * This program is free software; you can redistribute it and/or modify
20 * it under the terms of the GNU General Public License as published by
21 * the Free Software Foundation; either version 2 of the License, or
22 * (at your option) any later version.
23 *
24 * This program is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * GNU General Public License for more details.
28 *
29 * You should have received a copy of the GNU General Public License
30 * along with this program; if not, write to the Free Software
31 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
32 *
33 *
34 * Copyright (c) 2003, Robert Collins <robertc@squid-cache.org>
35 */
36
37 #include "squid.h"
38 #include "acl/TimeData.h"
39 #include "acl/Checklist.h"
40 #include "cache_cf.h"
41 #include "Debug.h"
42 #include "protos.h"
43 #include "wordlist.h"
44
45 ACLTimeData::ACLTimeData () : weekbits (0), start (0), stop (0), next (NULL) {}
46
47 ACLTimeData::ACLTimeData(ACLTimeData const &old) : weekbits(old.weekbits), start (old.start), stop (old.stop), next (NULL)
48 {
49 if (old.next)
50 next = (ACLTimeData *)old.next->clone();
51 }
52
53 ACLTimeData&
54 ACLTimeData::operator=(ACLTimeData const &old)
55 {
56 weekbits = old.weekbits;
57 start = old.start;
58 stop = old.stop;
59 next = NULL;
60
61 if (old.next)
62 next = (ACLTimeData *)old.next->clone();
63
64 return *this;
65 }
66
67 ACLTimeData::~ACLTimeData()
68 {
69 if (next)
70 delete next;
71 }
72
73 bool
74 ACLTimeData::match(time_t when)
75 {
76 static time_t last_when = 0;
77
78 static struct tm tm;
79 time_t t;
80
81 if (when != last_when) {
82 last_when = when;
83 memcpy(&tm, localtime(&when), sizeof(struct tm));
84 }
85
86 t = (time_t) (tm.tm_hour * 60 + tm.tm_min);
87 ACLTimeData *data = this;
88
89 while (data) {
90 debugs(28, 3, "aclMatchTime: checking " << t << " in " <<
91 data->start << "-" << data->stop << ", weekbits=" <<
92 std::hex << data->weekbits);
93
94 if (t >= data->start && t <= data->stop && (data->weekbits & (1 << tm.tm_wday)))
95 return 1;
96
97 data = data->next;
98 }
99
100 return 0;
101 }
102
103 wordlist *
104 ACLTimeData::dump()
105 {
106 wordlist *W = NULL;
107 char buf[128];
108 ACLTimeData *t = this;
109
110 while (t != NULL) {
111 snprintf(buf, sizeof(buf), "%c%c%c%c%c%c%c %02d:%02d-%02d:%02d",
112 t->weekbits & ACL_SUNDAY ? 'S' : '-',
113 t->weekbits & ACL_MONDAY ? 'M' : '-',
114 t->weekbits & ACL_TUESDAY ? 'T' : '-',
115 t->weekbits & ACL_WEDNESDAY ? 'W' : '-',
116 t->weekbits & ACL_THURSDAY ? 'H' : '-',
117 t->weekbits & ACL_FRIDAY ? 'F' : '-',
118 t->weekbits & ACL_SATURDAY ? 'A' : '-',
119 t->start / 60, t->start % 60, t->stop / 60, t->stop % 60);
120 wordlistAdd(&W, buf);
121 t = t->next;
122 }
123
124 return W;
125 }
126
127 void
128 ACLTimeData::parse()
129 {
130 ACLTimeData **Tail;
131 long parsed_weekbits = 0;
132
133 for (Tail = &next; *Tail; Tail = &((*Tail)->next));
134 ACLTimeData *q = NULL;
135
136 int h1, m1, h2, m2;
137
138 char *t = NULL;
139
140 while ((t = strtokFile())) {
141 if (*t < '0' || *t > '9') {
142 /* assume its day-of-week spec */
143
144 while (*t) {
145 switch (*t++) {
146
147 case 'S':
148 parsed_weekbits |= ACL_SUNDAY;
149 break;
150
151 case 'M':
152 parsed_weekbits |= ACL_MONDAY;
153 break;
154
155 case 'T':
156 parsed_weekbits |= ACL_TUESDAY;
157 break;
158
159 case 'W':
160 parsed_weekbits |= ACL_WEDNESDAY;
161 break;
162
163 case 'H':
164 parsed_weekbits |= ACL_THURSDAY;
165 break;
166
167 case 'F':
168 parsed_weekbits |= ACL_FRIDAY;
169 break;
170
171 case 'A':
172 parsed_weekbits |= ACL_SATURDAY;
173 break;
174
175 case 'D':
176 parsed_weekbits |= ACL_WEEKDAYS;
177 break;
178
179 case '-':
180 /* ignore placeholder */
181 break;
182
183 default:
184 debugs(28, DBG_CRITICAL, "" << cfg_filename << " line " << config_lineno <<
185 ": " << config_input_line);
186 debugs(28, DBG_CRITICAL, "aclParseTimeSpec: Bad Day '" << *t << "'" );
187 break;
188 }
189 }
190 } else {
191 /* assume its time-of-day spec */
192
193 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)))) {
194 debugs(28, DBG_CRITICAL, "aclParseTimeSpec: Bad time range '" << t << "'");
195 self_destruct();
196
197 if (q != this)
198 delete q;
199
200 return;
201 }
202
203 if ((parsed_weekbits == 0) && (start == 0) && (stop == 0))
204 q = this;
205 else
206 q = new ACLTimeData;
207
208 q->start = h1 * 60 + m1;
209
210 q->stop = h2 * 60 + m2;
211
212 q->weekbits = parsed_weekbits;
213
214 parsed_weekbits = 0;
215
216 if (q->start > q->stop) {
217 debugs(28, DBG_CRITICAL, "aclParseTimeSpec: Reversed time range");
218 self_destruct();
219
220 if (q != this)
221 delete q;
222
223 return;
224 }
225
226 if (q->weekbits == 0)
227 q->weekbits = ACL_ALLWEEK;
228
229 if (q != this) {
230 *(Tail) = q;
231 Tail = &q->next;
232 }
233 }
234 }
235
236 if (parsed_weekbits) {
237
238 q = new ACLTimeData;
239
240 q->start = 0 * 60 + 0;
241
242 q->stop = 24 * 60 + 0;
243
244 q->weekbits = parsed_weekbits;
245
246 *(Tail) = q;
247 Tail = &q->next;
248 }
249 }
250
251 bool
252 ACLTimeData::empty() const
253 {
254 return false;
255 }
256
257 ACLData<time_t> *
258 ACLTimeData::clone() const
259 {
260 return new ACLTimeData(*this);
261 }