]>
Commit | Line | Data |
---|---|---|
5dee515e | 1 | /* |
4a7a3d56 | 2 | * $Id: ACLTimeData.cc,v 1.15 2007/04/30 16:56:09 wessels Exp $ |
5dee515e | 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. | |
26ac0430 | 23 | * |
5dee515e | 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. | |
26ac0430 | 28 | * |
5dee515e | 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 "ACLTimeData.h" | |
39 | #include "authenticate.h" | |
40 | #include "ACLChecklist.h" | |
d295d770 | 41 | #include "wordlist.h" |
5dee515e | 42 | |
5dee515e | 43 | ACLTimeData::ACLTimeData () : weekbits (0), start (0), stop (0), next (NULL) {} |
62e76326 | 44 | |
45 | ACLTimeData::ACLTimeData(ACLTimeData const &old) : weekbits(old.weekbits), start (old.start), stop (old.stop), next (NULL) | |
5dee515e | 46 | { |
47 | if (old.next) | |
62e76326 | 48 | next = (ACLTimeData *)old.next->clone(); |
5dee515e | 49 | } |
50 | ||
51 | ACLTimeData& | |
52 | ACLTimeData::operator=(ACLTimeData const &old) | |
53 | { | |
54 | weekbits = old.weekbits; | |
55 | start = old.start; | |
56 | stop = old.stop; | |
57 | next = NULL; | |
62e76326 | 58 | |
5dee515e | 59 | if (old.next) |
62e76326 | 60 | next = (ACLTimeData *)old.next->clone(); |
61 | ||
5dee515e | 62 | return *this; |
63 | } | |
64 | ||
65 | ACLTimeData::~ACLTimeData() | |
66 | { | |
67 | if (next) | |
00d77d6b | 68 | delete next; |
62e76326 | 69 | } |
5dee515e | 70 | |
71 | bool | |
72 | ACLTimeData::match(time_t when) | |
73 | { | |
74 | static time_t last_when = 0; | |
62e76326 | 75 | |
5dee515e | 76 | static struct tm tm; |
77 | time_t t; | |
62e76326 | 78 | |
5dee515e | 79 | if (when != last_when) { |
62e76326 | 80 | last_when = when; |
81 | ||
82 | xmemcpy(&tm, localtime(&when), sizeof(struct tm)); | |
5dee515e | 83 | } |
62e76326 | 84 | |
5dee515e | 85 | t = (time_t) (tm.tm_hour * 60 + tm.tm_min); |
86 | ACLTimeData *data = this; | |
62e76326 | 87 | |
5dee515e | 88 | while (data) { |
4a7a3d56 | 89 | debugs(28, 3, "aclMatchTime: checking " << t << " in " << |
90 | data->start << "-" << data->stop << ", weekbits=" << | |
bf8fe701 | 91 | std::hex << data->weekbits); |
5dee515e | 92 | |
62e76326 | 93 | if (t >= data->start && t <= data->stop && (data->weekbits & (1 << tm.tm_wday))) |
94 | return 1; | |
95 | ||
96 | data = data->next; | |
5dee515e | 97 | } |
62e76326 | 98 | |
5dee515e | 99 | return 0; |
100 | } | |
101 | ||
102 | wordlist * | |
103 | ACLTimeData::dump() | |
104 | { | |
105 | wordlist *W = NULL; | |
106 | char buf[128]; | |
107 | ACLTimeData *t = this; | |
62e76326 | 108 | |
5dee515e | 109 | while (t != NULL) { |
62e76326 | 110 | snprintf(buf, sizeof(buf), "%c%c%c%c%c%c%c %02d:%02d-%02d:%02d", |
111 | t->weekbits & ACL_SUNDAY ? 'S' : '-', | |
112 | t->weekbits & ACL_MONDAY ? 'M' : '-', | |
113 | t->weekbits & ACL_TUESDAY ? 'T' : '-', | |
114 | t->weekbits & ACL_WEDNESDAY ? 'W' : '-', | |
115 | t->weekbits & ACL_THURSDAY ? 'H' : '-', | |
116 | t->weekbits & ACL_FRIDAY ? 'F' : '-', | |
117 | t->weekbits & ACL_SATURDAY ? 'A' : '-', | |
118 | t->start / 60, t->start % 60, t->stop / 60, t->stop % 60); | |
119 | wordlistAdd(&W, buf); | |
120 | t = t->next; | |
5dee515e | 121 | } |
62e76326 | 122 | |
5dee515e | 123 | return W; |
124 | } | |
125 | ||
126 | void | |
127 | ACLTimeData::parse() | |
128 | { | |
129 | ACLTimeData **Tail; | |
3f7580f9 | 130 | long weekbits = 0; |
62e76326 | 131 | |
3d0ac046 | 132 | for (Tail = &next; *Tail; Tail = &((*Tail)->next)); |
5dee515e | 133 | ACLTimeData *q = NULL; |
62e76326 | 134 | |
5dee515e | 135 | int h1, m1, h2, m2; |
62e76326 | 136 | |
5dee515e | 137 | char *t = NULL; |
62e76326 | 138 | |
5dee515e | 139 | while ((t = strtokFile())) { |
62e76326 | 140 | if (*t < '0' || *t > '9') { |
141 | /* assume its day-of-week spec */ | |
142 | ||
143 | while (*t) { | |
144 | switch (*t++) { | |
145 | ||
146 | case 'S': | |
3f7580f9 | 147 | weekbits |= ACL_SUNDAY; |
62e76326 | 148 | break; |
149 | ||
150 | case 'M': | |
3f7580f9 | 151 | weekbits |= ACL_MONDAY; |
62e76326 | 152 | break; |
153 | ||
154 | case 'T': | |
3f7580f9 | 155 | weekbits |= ACL_TUESDAY; |
62e76326 | 156 | break; |
157 | ||
158 | case 'W': | |
3f7580f9 | 159 | weekbits |= ACL_WEDNESDAY; |
62e76326 | 160 | break; |
161 | ||
162 | case 'H': | |
3f7580f9 | 163 | weekbits |= ACL_THURSDAY; |
62e76326 | 164 | break; |
165 | ||
166 | case 'F': | |
3f7580f9 | 167 | weekbits |= ACL_FRIDAY; |
62e76326 | 168 | break; |
169 | ||
170 | case 'A': | |
3f7580f9 | 171 | weekbits |= ACL_SATURDAY; |
62e76326 | 172 | break; |
173 | ||
174 | case 'D': | |
3f7580f9 | 175 | weekbits |= ACL_WEEKDAYS; |
62e76326 | 176 | break; |
177 | ||
178 | case '-': | |
179 | /* ignore placeholder */ | |
180 | break; | |
181 | ||
182 | default: | |
bf8fe701 | 183 | debugs(28, 0, "" << cfg_filename << " line " << config_lineno << |
184 | ": " << config_input_line); | |
185 | debugs(28, 0, "aclParseTimeSpec: Bad Day '" << *t << "'" ); | |
62e76326 | 186 | break; |
187 | } | |
188 | } | |
189 | } else { | |
190 | /* assume its time-of-day spec */ | |
191 | ||
6494b68a | 192 | 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)))) { |
bf8fe701 | 193 | debugs(28, 0, "aclParseTimeSpec: Bad time range '" << t << "'"); |
4b0f5de8 | 194 | self_destruct(); |
62e76326 | 195 | |
196 | if (q != this) | |
00d77d6b | 197 | delete q; |
62e76326 | 198 | |
199 | return; | |
200 | } | |
201 | ||
3f7580f9 | 202 | if ((weekbits == 0) && (start == 0) && (stop == 0)) |
203 | q = this; | |
204 | else | |
205 | q = new ACLTimeData; | |
206 | ||
62e76326 | 207 | q->start = h1 * 60 + m1; |
3f7580f9 | 208 | |
62e76326 | 209 | q->stop = h2 * 60 + m2; |
210 | ||
3f7580f9 | 211 | q->weekbits = weekbits; |
212 | ||
213 | weekbits = 0; | |
214 | ||
62e76326 | 215 | if (q->start > q->stop) { |
bf8fe701 | 216 | debugs(28, 0, "aclParseTimeSpec: Reversed time range"); |
4b0f5de8 | 217 | self_destruct(); |
62e76326 | 218 | |
219 | if (q != this) | |
00d77d6b | 220 | delete q; |
62e76326 | 221 | |
222 | return; | |
223 | } | |
3f7580f9 | 224 | |
225 | if (q->weekbits == 0) | |
226 | q->weekbits = ACL_ALLWEEK; | |
227 | ||
228 | if (q != this) { | |
229 | *(Tail) = q; | |
230 | Tail = &q->next; | |
231 | } | |
62e76326 | 232 | } |
5dee515e | 233 | } |
62e76326 | 234 | |
3f7580f9 | 235 | if (weekbits) { |
236 | ||
237 | if ((weekbits == 0) && (start == 0) && (stop == 0)) | |
238 | q = this; | |
239 | else | |
240 | q = new ACLTimeData; | |
241 | ||
242 | q->start = 0 * 60 + 0; | |
62e76326 | 243 | |
3f7580f9 | 244 | q->stop = 24 * 60 + 0; |
62e76326 | 245 | |
3f7580f9 | 246 | q->weekbits = weekbits; |
247 | ||
248 | if (q != this) { | |
249 | *(Tail) = q; | |
250 | Tail = &q->next; | |
251 | } | |
252 | } | |
5dee515e | 253 | } |
254 | ||
65092baf | 255 | bool |
256 | ACLTimeData::empty() const | |
257 | { | |
258 | return false; | |
259 | } | |
5dee515e | 260 | |
261 | ACLData<time_t> * | |
262 | ACLTimeData::clone() const | |
263 | { | |
264 | return new ACLTimeData(*this); | |
265 | } |