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

 

Introduction

Documentation

Support

Miscellaneous

Web Site Translations

Mirrors