Debian Codegraph @

agent/mibgroup/disman/schedule/schedConf.c

raw
1 /*
2 * DisMan Schedule MIB:
3 * Implementation of the schedule MIB config handling
4 */
5
6 #include <net-snmp/net-snmp-config.h>
7 #include <net-snmp/net-snmp-features.h>
8 #include <net-snmp/net-snmp-includes.h>
9 #include <net-snmp/agent/net-snmp-agent-includes.h>
10 #include <ctype.h>
11 #include "disman/schedule/schedCore.h"
12 #include "disman/schedule/schedConf.h"
13
14 netsnmp_feature_require(iquery);
15 netsnmp_feature_require(string_time_to_secs);
16
17 static int schedEntries;
18
19 /** Initializes the schedConf module */
20 void
21 init_schedConf(void)
22 {
23 DEBUGMSGTL(("disman:schedule:init", "Initializing config module\n"));
24 init_schedule_container();
25
26 /*
27 * Register public configuration directives
28 */
29 snmpd_register_config_handler("repeat", parse_sched_periodic, NULL,
30 "repeat period OID = value");
31 snmpd_register_config_handler("cron", parse_sched_timed, NULL,
32 "cron * * * * * OID = value");
33 snmpd_register_config_handler("at", parse_sched_timed, NULL,
34 "at * * * * * OID = value");
35
36 /*
37 * Register internal configuration directive,
38 * and arrange for dynamically configured entries to be saved
39 */
40 snmpd_register_config_handler("_schedTable", parse_schedTable, NULL, NULL);
41 snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA,
42 store_schedTable, NULL);
43 schedEntries = 0;
44 }
45
46
47 /* =======================================================
48 *
49 * Handlers for user-configured (static) scheduled actions
50 *
51 * ======================================================= */
52
53 void
54 parse_sched_periodic( const char *token, char *line )
55 {
56 netsnmp_tdata_row *row;
57 struct schedTable_entry *entry;
58 char buf[24], tmpbuf[SPRINT_MAX_LEN];
59 long frequency;
60 long value;
61 size_t tmpint;
62 oid variable[ MAX_OID_LEN], *var_ptr = variable;
63 size_t var_len = MAX_OID_LEN;
64
65 schedEntries++;
66 sprintf(buf, "_conf%03d", schedEntries);
67
68 DEBUGMSGTL(( "disman:schedule:conf", "periodic: %s %s\n", token, line));
69 /*
70 * Parse the configure directive line
71 */
72 line = copy_nword(line, tmpbuf, sizeof(tmpbuf));
73 frequency = netsnmp_string_time_to_secs(tmpbuf);
74 if (frequency == -1) {
75 config_perror("Illegal frequency specified");
76 return;
77 }
78
79 line = read_config_read_data(ASN_OBJECT_ID, line, &var_ptr, &var_len);
80 if (var_len == 0) {
81 config_perror("invalid specification for schedVariable");
82 return;
83 }
84 /*
85 * Skip over optional assignment in "var = value"
86 */
87 while (line && isspace((unsigned char)(*line)))
88 line++;
89 if (line && *line == '=' ) {
90 line++;
91 while (line && isspace((unsigned char)(*line))) {
92 line++;
93 }
94 }
95 line = read_config_read_data(ASN_INTEGER, line, &value, &tmpint);
96
97 /*
98 * Create an entry in the schedTable
99 */
100 row = schedTable_createEntry( "snmpd.conf", buf );
101 if (!row || !row->data) {
102 config_perror("create schedule entry failure");
103 return;
104 }
105 entry = (struct schedTable_entry *)row->data;
106
107 entry->schedInterval = frequency;
108 entry->schedValue = value;
109 entry->schedVariable_len = var_len;
110 memcpy(entry->schedVariable, variable, var_len*sizeof(oid));
111
112 entry->schedType = SCHED_TYPE_PERIODIC;
113 entry->schedStorageType = ST_READONLY; /* or PERMANENT */
114 entry->flags = SCHEDULE_FLAG_ENABLED |
115 SCHEDULE_FLAG_ACTIVE |
116 SCHEDULE_FLAG_VALID;
117 entry->session = netsnmp_query_get_default_session();
118 sched_nextTime( entry );
119 }
120
121
122 /*
123 * Timed-schedule utility:
124 * Convert from a cron-style specification to the equivalent set
125 * of bits. Note that minute, hour and weekday crontab fields are
126 * 0-based, while day and month more naturally start from 1.
127 */
128 void
129 _sched_convert_bits( char *cron_spec, char *bit_buf,
130 int bit_buf_len, int max_val, int startAt1 ) {
131 char *cp = cron_spec;
132 u_char b[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
133 int val, major, minor;
134 int overshoot;
135
136 if (!cron_spec || !bit_buf)
137 return;
138
139 /*
140 * Wildcard field - set all bits
141 */
142 if ( *cp == '*' ) {
143 memset( bit_buf, 0xff, bit_buf_len );
144
145 /*
146 * An "all-bits" specification may not be an exact multiple of 8.
147 * Work out how far we've overshot things, and tidy up the excess.
148 */
149 overshoot = 8*bit_buf_len-max_val;
150 while ( overshoot > 0 ) {
151 bit_buf[ bit_buf_len-1 ] ^= b[8-overshoot];
152 overshoot--;
153 }
154 return;
155 }
156
157 /*
158 * Otherwise, clear the bit string buffer,
159 * and start calculating which bits to set
160 */
161 memset( bit_buf, 0, bit_buf_len );
162
163 while (1) {
164 if ( sscanf( cp, "%d", &val) != 1 ) {
165 config_perror("Bad number format");
166 return;
167 }
168 /* Handle negative day specification */
169 if ( val < 0 ) {
170 val = max_val - val;
171 }
172 if ( startAt1 )
173 val--;
174 if ( val < 0 || val >= max_val ) {
175 config_perror("Bad time spec");
176 return;
177 }
178 major = val/8;
179 minor = val%8;
180 bit_buf[ major ] |= b[minor];
181
182 /* XXX - ideally we should handle "X-Y" syntax as well */
183 while (*cp && *cp!=',')
184 cp++;
185 if (!*cp)
186 break;
187 cp++;
188 }
189 }
190
191 void
192 parse_sched_timed( const char *token, char *line )
193 {
194 netsnmp_tdata_row *row;
195 struct schedTable_entry *entry;
196 char buf[24], *cp;
197
198 char minConf[512]; size_t min_len = sizeof(minConf); char minVal[8];
199 char hourConf[512]; size_t hour_len = sizeof(hourConf); char hourVal[3];
200 char dateConf[512]; size_t date_len = sizeof(dateConf); char dateVal[8];
201 char monConf[512]; size_t mon_len = sizeof(monConf); char monVal[2];
202 char dayConf[512]; size_t day_len = sizeof(dayConf); char dayVal;
203
204 long value;
205 size_t tmpint;
206 oid variable[ MAX_OID_LEN], *var_ptr = variable;
207 size_t var_len = MAX_OID_LEN;
208
209 schedEntries++;
210 sprintf(buf, "_conf%03d", schedEntries);
211
212 DEBUGMSGTL(( "sched", "config: %s %s\n", token, line));
213 /*
214 * Parse the configure directive line
215 */
216 cp = minConf;
217 line = read_config_read_data(ASN_OCTET_STR, line, &cp, &min_len);
218 cp = hourConf;
219 line = read_config_read_data(ASN_OCTET_STR, line, &cp, &hour_len);
220 cp = dateConf;
221 line = read_config_read_data(ASN_OCTET_STR, line, &cp, &date_len);
222 cp = monConf;
223 line = read_config_read_data(ASN_OCTET_STR, line, &cp, &mon_len);
224 cp = dayConf;
225 line = read_config_read_data(ASN_OCTET_STR, line, &cp, &day_len);
226 if (!line) {
227 config_perror("invalid schedule time specification");
228 return;
229 }
230
231 line = read_config_read_data(ASN_OBJECT_ID, line, &var_ptr, &var_len);
232 if (var_len == 0) {
233 config_perror("invalid specification for schedVariable");
234 return;
235 }
236 /*
237 * Skip over optional assignment in "var = value"
238 */
239 while (line && isspace((unsigned char)(*line)))
240 line++;
241 if (line && *line == '=') {
242 line++;
243 while (line && isspace((unsigned char)(*line))) {
244 line++;
245 }
246 }
247 line = read_config_read_data(ASN_INTEGER, line, &value, &tmpint);
248
249 /*
250 * Convert from cron-style specifications into bits
251 */
252 _sched_convert_bits( minConf, minVal, 8, 60, 0 );
253 _sched_convert_bits( hourConf, hourVal, 3, 24, 0 );
254 memset(dateVal+4, 0, 4); /* Clear the reverse day bits */
255 _sched_convert_bits( dateConf, dateVal, 4, 31, 1 );
256 _sched_convert_bits( monConf, monVal, 2, 12, 1 );
257 _sched_convert_bits( dayConf, &dayVal, 1, 8, 0 );
258 if ( dayVal & 0x01 ) { /* sunday(7) = sunday(0) */
259 dayVal |= 0x80;
260 dayVal &= 0xfe;
261 }
262
263 /*
264 * Create an entry in the schedTable
265 */
266 row = schedTable_createEntry("snmpd.conf", buf);
267 if (!row || !row->data) {
268 config_perror("create schedule entry failure");
269 return;
270 }
271 entry = (struct schedTable_entry *)row->data;
272
273 entry->schedWeekDay[0] = dayVal;
274 memcpy(entry->schedMonth, monVal, 2);
275 memcpy(entry->schedDay, dateVal, 4+4);
276 memcpy(entry->schedHour, hourVal, 3);
277 memcpy(entry->schedMinute, minVal, 8);
278
279 memcpy(entry->schedVariable, variable, var_len*sizeof(oid));
280 entry->schedVariable_len = var_len;
281 entry->schedValue = value;
282
283 if ( !strcmp( token, "at" ))
284 entry->schedType = SCHED_TYPE_ONESHOT;
285 else
286 entry->schedType = SCHED_TYPE_CALENDAR;
287 entry->schedStorageType = ST_READONLY; /* or PERMANENT */
288 entry->flags = SCHEDULE_FLAG_ENABLED |
289 SCHEDULE_FLAG_ACTIVE |
290 SCHEDULE_FLAG_VALID;
291 entry->session = netsnmp_query_get_default_session();
292 sched_nextTime( entry );
293 }
294
295
296 /* ========================================
297 *
298 * Handlers for persistent schedule entries
299 *
300 * ======================================== */
301
302 void
303 parse_schedTable( const char *token, char *line )
304 {
305 char owner[SCHED_STR1_LEN+1];
306 char name[ SCHED_STR1_LEN+1];
307 char time_bits[22]; /* schedWeekDay..schedMinute */
308 void *vp;
309 size_t len;
310 netsnmp_tdata_row *row;
311 struct schedTable_entry *entry;
312
313 DEBUGMSGTL(("disman:schedule:conf", "Parsing schedTable config... "));
314
315 /*
316 * Read in the index information for this entry
317 * and create a (non-fixed) data structure for it.
318 */
319 memset( owner, 0, sizeof(owner));
320 memset( name, 0, sizeof(name));
321 len = SCHED_STR1_LEN; vp = owner;
322 line = read_config_read_data(ASN_OCTET_STR, line, &vp, &len);
323 len = SCHED_STR1_LEN; vp = name;
324 line = read_config_read_data(ASN_OCTET_STR, line, &vp, &len);
325 row = schedTable_createEntry(owner, name);
326 if (!row || !row->data) {
327 config_perror("create schedule entry failure");
328 return;
329 }
330 entry = (struct schedTable_entry *)row->data;
331 DEBUGMSG(("disman:schedule:conf", "(%s, %s) ", owner, name));
332
333 /*
334 * Read in the column values.
335 */
336 len = SCHED_STR2_LEN; vp = entry->schedDescr;
337 line = read_config_read_data(ASN_OCTET_STR, line, &vp, &len);
338 line = read_config_read_data(ASN_UNSIGNED, line,
339 &entry->schedInterval, NULL);
340 /* Unpick the various timed bits */
341 len = 22; vp = time_bits;
342 line = read_config_read_data(ASN_OCTET_STR, line, &vp, &len);
343 entry->schedWeekDay[0] = time_bits[0];
344 entry->schedMonth[0] = time_bits[1];
345 entry->schedMonth[1] = time_bits[2];
346 entry->schedHour[0] = time_bits[11];
347 entry->schedHour[1] = time_bits[12];
348 entry->schedHour[2] = time_bits[13];
349 memcpy(entry->schedDay, time_bits+3, 8);
350 memcpy(entry->schedMinute, time_bits+14, 8);
351
352 len = SCHED_STR1_LEN; vp = entry->schedContextName;
353 line = read_config_read_data(ASN_OCTET_STR, line, &vp, &len);
354 len = MAX_OID_LEN; vp = entry->schedVariable;
355 line = read_config_read_data(ASN_OBJECT_ID, line, &vp, &len);
356 entry->schedVariable_len = len;
357 line = read_config_read_data(ASN_INTEGER, line,
358 &entry->schedValue, NULL);
359 line = read_config_read_data(ASN_UNSIGNED, line,
360 &entry->schedType, NULL);
361 line = read_config_read_data(ASN_UNSIGNED, line, &len, NULL);
362 entry->flags |= (len /* & WHAT ?? */);
363 /* XXX - Will need to read in the 'iquery' access information */
364 entry->flags |= SCHEDULE_FLAG_VALID;
365
366 DEBUGMSG(("disman:schedule:conf", "\n"));
367 }
368
369 /*
370 * Save dynamically-configured schedTable entries into persistent storage
371 */
372 int
373 store_schedTable(int majorID, int minorID, void *serverarg, void *clientarg)
374 {
375 char line[SNMP_MAXBUF];
376 char time_bits[22]; /* schedWeekDay..schedMinute */
377 char *cptr, *cp;
378 void *vp;
379 size_t tint;
380 netsnmp_tdata_row *row;
381 struct schedTable_entry *entry;
382
383
384 DEBUGMSGTL(( "disman:schedule:conf", "Storing schedTable:\n"));
385
386 for (row = netsnmp_tdata_row_first( schedule_table );
387 row;
388 row = netsnmp_tdata_row_next( schedule_table, row )) {
389
390 if (!row->data)
391 continue;
392 entry = (struct schedTable_entry *)row->data;
393
394 /*
395 * Only save (dynamically-created) 'nonVolatile' entries
396 * (XXX - what about dynamic 'permanent' entries ??)
397 */
398 if (entry->schedStorageType != ST_NONVOLATILE )
399 continue;
400 DEBUGMSGTL(( "disman:schedule:conf", " Storing (%s, %s)\n",
401 entry->schedOwner, entry->schedName));
402
403 memset(line, 0, sizeof(line));
404 strcpy(line, "_schedTable ");
405 cptr = line + strlen(line);
406
407 cp = entry->schedOwner; tint = strlen( cp );
408 cptr = read_config_store_data(ASN_OCTET_STR, cptr, &cp, &tint );
409 cp = entry->schedName; tint = strlen( cp );
410 cptr = read_config_store_data(ASN_OCTET_STR, cptr, &cp, &tint );
411 cp = entry->schedDescr; tint = strlen( cp );
412 cptr = read_config_store_data(ASN_OCTET_STR, cptr, &cp, &tint );
413 tint = entry->schedInterval;
414 cptr = read_config_store_data(ASN_UNSIGNED, cptr, &tint, NULL );
415
416 /* Combine all the timed bits into a single field */
417 time_bits[0] = entry->schedWeekDay[0];
418 time_bits[1] = entry->schedMonth[0];
419 time_bits[2] = entry->schedMonth[1];
420 time_bits[11] = entry->schedHour[0];
421 time_bits[12] = entry->schedHour[1];
422 time_bits[13] = entry->schedHour[2];
423 memcpy(time_bits+3, entry->schedDay, 8);
424 memcpy(time_bits+14, entry->schedMinute, 8);
425 vp = time_bits; tint = 22;
426 cptr = read_config_store_data(ASN_OCTET_STR, cptr, &vp, &tint );
427
428 cp = entry->schedContextName; tint = strlen( cp );
429 cptr = read_config_store_data(ASN_OCTET_STR, cptr, &cp, &tint );
430 vp = entry->schedVariable;
431 tint = entry->schedVariable_len;
432 cptr = read_config_store_data(ASN_OBJECT_ID, cptr, &vp, &tint );
433 tint = entry->schedValue;
434 cptr = read_config_store_data(ASN_INTEGER, cptr, &tint, NULL );
435 tint = entry->schedType;
436 cptr = read_config_store_data(ASN_UNSIGNED, cptr, &tint, NULL );
437 tint = entry->flags /* & WHAT ?? */;
438 cptr = read_config_store_data(ASN_UNSIGNED, cptr, &tint, NULL );
439 /* XXX - Need to store the 'iquery' access information */
440 snmpd_store_config(line);
441 }
442 DEBUGMSGTL(( "disman:schedule:conf", " done.\n"));
443 return SNMPERR_SUCCESS;
444 }