summaryrefslogtreecommitdiff
path: root/src/libcharon/plugins/certexpire/certexpire_cron.c
blob: 5f2fd7ca4183858f168c28c6302c547bc243741e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
/*
 * Copyright (C) 2011 Martin Willi
 * Copyright (C) 2011 revosec AG
 *
 * This program is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation; either version 2 of the License, or (at your
 * option) any later version.  See <http://www.fsf.org/copyleft/gpl.txt>.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * for more details.
 */

#include "certexpire_cron.h"

#include <time.h>

#include <utils/debug.h>
#include <processing/jobs/callback_job.h>

typedef struct private_certexpire_cron_t private_certexpire_cron_t;

/**
 * Private data of an certexpire_cron_t object.
 */
struct private_certexpire_cron_t {

	/**
	 * Public certexpire_cron_t interface.
	 */
	certexpire_cron_t public;

	/**
	 * time when to run export job
	 */
	struct {
		bool m[60];
		bool h[24];
		bool d[32];
		bool my[13];
		bool dw[8];
	} cron;

	/**
	 * Callback function to execute
	 */
	certexpire_cron_job_t job;

	/**
	 * Data to pass to callback
	 */
	void *data;
};

/**
 * Check if we should execute the export job
 */
static job_requeue_t check_cron(private_certexpire_cron_t *this)
{
	struct tm tm;
	time_t t;

	t = time(NULL);
	localtime_r(&t, &tm);

	/* recheck every minute at second 0 */
	lib->scheduler->schedule_job(lib->scheduler,
			(job_t*)callback_job_create_with_prio((callback_job_cb_t)check_cron,
			this, NULL, NULL, JOB_PRIO_CRITICAL), 60 - tm.tm_sec);

	/* skip this minute if we had a large negative time shift */
	if (tm.tm_sec <= 30)
	{
		if (this->cron.m[tm.tm_min] &&
			this->cron.h[tm.tm_hour] &&
			this->cron.d[tm.tm_mday] &&
			this->cron.my[tm.tm_mon + 1] &&
			(this->cron.dw[tm.tm_wday] ||
			 (this->cron.dw[7] && tm.tm_wday == 0)))
		{
			this->job(this->data);
		}
	}
	return JOB_REQUEUE_NONE;
}

/**
 * Parse a cron range component into boolean fields
 */
static void parse_ranges(bool *fields, char *label, int mi, int ma, char *range)
{
	enumerator_t *enumerator;
	int from, to, i;

	if (streq(range, "*"))
	{
		for (i = mi; i <= ma; i++)
		{
			fields[i] = TRUE;
		}
	}
	else
	{
		enumerator = enumerator_create_token(range, ",", "");
		while (enumerator->enumerate(enumerator, &range))
		{
			switch (sscanf(range, "%d-%d", &from, &to))
			{
				case 1: /* single value */
					if (from >= mi && from <= ma)
					{
						fields[from] = TRUE;
					}
					else
					{
						DBG1(DBG_CFG, "ignoring cron %s %d, out of range",
							 label, from);
					}
					break;
				case 2: /* range */
					if (from < mi)
					{
						DBG1(DBG_CFG, "cron %s out of range, shortening start "
							 "from %d to %d", label, from, mi);
						from = mi;
					}
					if (to > ma)
					{
						DBG1(DBG_CFG, "cron %s out of range, shortening end "
							 "from %d to %d", label, to, ma);
						to = ma;
					}
					for (i = from; i <= to; i++)
					{
						fields[i] = TRUE;
					}
					break;
				default:
					break;
			}
		}
		enumerator->destroy(enumerator);
	}
	DBG3(DBG_CFG, "cron job with enabled %ss:", label);
	for (i = mi; i <= ma; i++)
	{
		if (fields[i])
		{
			DBG3(DBG_CFG, "  %d", i);
		}
	}
}

/**
 * Start cron processing, if configured
 */
static void start_cron(private_certexpire_cron_t *this, char *cron)
{
	enumerator_t *enumerator;
	int i = 0;

	enumerator = enumerator_create_token(cron, " ", " ");
	for (i = 0; i < 5; i++)
	{
		if (!enumerator->enumerate(enumerator, &cron))
		{
			DBG1(DBG_CFG, "cron misses a field, using '*'");
			cron = "*";
		}
		switch (i)
		{
			case 0:
				parse_ranges(this->cron.m, "minute", 0, 59, cron);
				break;
			case 1:
				parse_ranges(this->cron.h, "hour", 0, 23, cron);
				break;
			case 2:
				parse_ranges(this->cron.d, "day", 1, 31, cron);
				break;
			case 3:
				parse_ranges(this->cron.my, "month", 1, 12, cron);
				break;
			case 4:
				parse_ranges(this->cron.dw, "weekday", 0, 7, cron);
				break;
			default:
				break;
		}
	}
	if (enumerator->enumerate(enumerator, &cron))
	{
		DBG1(DBG_CFG, "ignoring extra fields in cron");
	}
	enumerator->destroy(enumerator);

	check_cron(this);
}

METHOD(certexpire_cron_t, destroy, void,
	private_certexpire_cron_t *this)
{
	free(this);
}

/**
 * See header
 */
certexpire_cron_t *certexpire_cron_create(char *cron, certexpire_cron_job_t job,
										  void *data)
{
	private_certexpire_cron_t *this;

	INIT(this,
		.public = {
			.destroy = _destroy,
		},
		.job = job,
		.data = data,
	);

	start_cron(this, cron);

	return &this->public;
}