summaryrefslogtreecommitdiff
path: root/agent/mibgroup/disman/schedule/schedConf.c
blob: 2e67b73e77cb03a8c3dc78a1909a3b6e5319d95b (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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/*
 * DisMan Schedule MIB:
 *     Implementation of the schedule MIB config handling
 */

#include <net-snmp/net-snmp-config.h>
#include <net-snmp/net-snmp-includes.h>
#include <net-snmp/agent/net-snmp-agent-includes.h>
#include <ctype.h>
#include "disman/schedule/schedCore.h"
#include "disman/schedule/schedConf.h"

static int schedEntries;

/** Initializes the schedConf module */
void
init_schedConf(void)
{
    DEBUGMSGTL(("disman:schedule:init", "Initializing config module\n"));
    init_schedule_container();

    /*
     * Register public configuration directives
     */
    snmpd_register_config_handler("repeat", parse_sched_periodic, NULL,
                                  "repeat period  OID = value");
    snmpd_register_config_handler("cron",   parse_sched_timed,    NULL,
                                  "cron * * * * * OID = value");
    snmpd_register_config_handler("at",     parse_sched_timed,    NULL,
                                  "at   * * * * * OID = value");

    /*
     * Register internal configuration directive,
     *   and arrange for dynamically configured entries to be saved
     */
    snmpd_register_config_handler("_schedTable", parse_schedTable, NULL, NULL);
    snmp_register_callback(SNMP_CALLBACK_LIBRARY, SNMP_CALLBACK_STORE_DATA,
                                     store_schedTable, NULL);
    schedEntries = 0;
}


/* =======================================================
 *
 * Handlers for user-configured (static) scheduled actions
 *
 * ======================================================= */
 
void
parse_sched_periodic( const char *token, char *line )
{
    netsnmp_tdata_row       *row;
    struct schedTable_entry *entry;
    char   buf[24];
    long   frequency;
    long   value;
    size_t tmpint;
    oid    variable[ MAX_OID_LEN], *var_ptr = variable;
    size_t var_len = MAX_OID_LEN;
    
    schedEntries++;
    sprintf(buf, "_conf%03d", schedEntries);

    DEBUGMSGTL(( "disman:schedule:conf", "periodic: %s %s\n", token, line));
    /*
     *  Parse the configure directive line
     */
    line = read_config_read_data(ASN_INTEGER,   line, &frequency, &tmpint);
    line = read_config_read_data(ASN_OBJECT_ID, line, &var_ptr,   &var_len);
    if (var_len == 0) {
        config_perror("invalid specification for schedVariable");
        return;
    }
    /*
     * Skip over optional assignment in "var = value"
     */
    while (line && isspace(*line))
        line++;
    if (line && *line == '=' ) {
        line++;
        while (line && isspace(*line)) {
            line++;
        }
    }
    line = read_config_read_data(ASN_INTEGER,   line, &value, &tmpint);
    
    /*
     * Create an entry in the schedTable
     */
    row   = schedTable_createEntry( "snmpd.conf", buf );
    if (!row || !row->data) {
        config_perror("create schedule entry failure");
        return;
    }
    entry = (struct schedTable_entry *)row->data;

    entry->schedInterval     = frequency;
    entry->schedValue        = value;
    entry->schedVariable_len = var_len;
    memcpy(entry->schedVariable, variable, var_len*sizeof(oid));

    entry->schedType         = SCHED_TYPE_PERIODIC;
    entry->schedStorageType  = ST_READONLY;  /* or PERMANENT */
    entry->flags             = SCHEDULE_FLAG_ENABLED |
                               SCHEDULE_FLAG_ACTIVE  |
                               SCHEDULE_FLAG_VALID;
    entry->session           = netsnmp_query_get_default_session();
    sched_nextTime( entry );
}


/*
 * Timed-schedule utility:
 *     Convert from a cron-style specification to the equivalent set
 *     of bits. Note that minute, hour and weekday crontab fields are
 *     0-based, while day and month more naturally start from 1.
 */
void
_sched_convert_bits( char *cron_spec, char *bit_buf,
                     int  bit_buf_len, int max_val, int startAt1 ) {
    char *cp = cron_spec;
    char b[] = {0x80, 0x40, 0x20, 0x10, 0x08, 0x04, 0x02, 0x01};
    int val, major, minor;
    int overshoot;

    if (!cron_spec || !bit_buf)
        return;
 
    /*
     * Wildcard field - set all bits
     */
    if ( *cp == '*' ) {
        memset( bit_buf, 0xff, bit_buf_len );

        /*
         * An "all-bits" specification may not be an exact multiple of 8.
         * Work out how far we've overshot things, and tidy up the excess.
         */
        overshoot = 8*bit_buf_len-max_val;
        while ( overshoot > 0 ) {
            bit_buf[ bit_buf_len-1 ] ^= b[8-overshoot];
            overshoot--;
        }
        return;
    }

    /*
     * Otherwise, clear the bit string buffer,
     * and start calculating which bits to set
     */
    memset( bit_buf, 0, bit_buf_len );

    while (1) {
        sscanf( cp, "%d", &val);
        /* Handle negative day specification */
        if ( val < 0 ) {
            val = max_val - val; 
        }
        if ( startAt1 )
            val--;
        major = val/8;
        minor = val%8;
        bit_buf[ major ] |= b[minor];

        /* XXX - ideally we should handle "X-Y" syntax as well */
        while (*cp && *cp!=',')
            cp++;
        if (!*cp)
            break;
        cp++;
    }
}

void
parse_sched_timed( const char *token, char *line )
{
    netsnmp_tdata_row       *row;
    struct schedTable_entry *entry;
    char   buf[24], *cp;

    char  minConf[512];  size_t  min_len = sizeof(minConf);  char  minVal[8];
    char hourConf[512];  size_t hour_len = sizeof(hourConf); char hourVal[3];
    char dateConf[512];  size_t date_len = sizeof(dateConf); char dateVal[8];
    char  monConf[512];  size_t  mon_len = sizeof(monConf);  char  monVal[2];
    char  dayConf[512];  size_t  day_len = sizeof(dayConf);  char  dayVal;

    long   value;
    size_t tmpint;
    oid    variable[ MAX_OID_LEN], *var_ptr = variable;
    size_t var_len = MAX_OID_LEN;
    
    schedEntries++;
    sprintf(buf, "_conf%03d", schedEntries);

    DEBUGMSGTL(( "sched", "config: %s %s\n", token, line));
    /*
     *  Parse the configure directive line
     */
    cp       = minConf;
    line = read_config_read_data(ASN_OCTET_STR, line, &cp,   &min_len);
    cp       = hourConf;
    line = read_config_read_data(ASN_OCTET_STR, line, &cp,  &hour_len);
    cp       = dateConf;
    line = read_config_read_data(ASN_OCTET_STR, line, &cp,  &date_len);
    cp       = monConf;
    line = read_config_read_data(ASN_OCTET_STR, line, &cp,   &mon_len);
    cp       = dayConf;
    line = read_config_read_data(ASN_OCTET_STR, line, &cp,   &day_len);
    if (!line) {
        config_perror("invalid schedule time specification");
        return;
    }

    line = read_config_read_data(ASN_OBJECT_ID, line, &var_ptr,   &var_len);
    if (var_len == 0) {
        config_perror("invalid specification for schedVariable");
        return;
    }
    /*
     * Skip over optional assignment in "var = value"
     */
    while (line && isspace(*line))
        line++;
    if ( *line == '=' ) {
        line++;
        while (line && isspace(*line)) {
            line++;
        }
    }
    line = read_config_read_data(ASN_INTEGER,   line, &value, &tmpint);

    /*
     * Convert from cron-style specifications into bits
     */
    _sched_convert_bits( minConf,  minVal,  8, 60, 0 );
    _sched_convert_bits( hourConf, hourVal, 3, 24, 0 );
    memset(dateVal+4, 0, 4); /* Clear the reverse day bits */
    _sched_convert_bits( dateConf, dateVal, 4, 31, 1 );
    _sched_convert_bits( monConf,  monVal,  2, 12, 1 );
    _sched_convert_bits( dayConf, &dayVal,  1,  8, 0 );
    if ( dayVal & 0x01 ) {  /* sunday(7) = sunday(0) */
         dayVal |= 0x80;
         dayVal &= 0xfe;
    }
    
    /*
     * Create an entry in the schedTable
     */
    row   = schedTable_createEntry("snmpd.conf", buf);
    if (!row || !row->data) {
        config_perror("create schedule entry failure");
        return;
    }
    entry = (struct schedTable_entry *)row->data;

    entry->schedWeekDay = dayVal;
    memcpy(entry->schedMonth,  monVal,  2);
    memcpy(entry->schedDay,    dateVal, 4+4);
    memcpy(entry->schedHour,   hourVal, 3);
    memcpy(entry->schedMinute, minVal,  8);
    
    memcpy(entry->schedVariable, variable, var_len*sizeof(oid));
    entry->schedVariable_len = var_len;
    entry->schedValue        = value;

    if ( !strcmp( token, "at" ))
        entry->schedType     = SCHED_TYPE_ONESHOT;
    else
        entry->schedType     = SCHED_TYPE_CALENDAR;
    entry->schedStorageType  = ST_READONLY;  /* or PERMANENT */
    entry->flags             = SCHEDULE_FLAG_ENABLED |
                               SCHEDULE_FLAG_ACTIVE  |
                               SCHEDULE_FLAG_VALID;
    entry->session           = netsnmp_query_get_default_session();
    sched_nextTime( entry );
}


/* ========================================
 *
 * Handlers for persistent schedule entries
 *
 * ======================================== */
 
void
parse_schedTable( const char *token, char *line )
{
    char   owner[SCHED_STR1_LEN+1];
    char   name[ SCHED_STR1_LEN+1];
    char   time_bits[22];  /* schedWeekDay..schedMinute */
    void  *vp;
    size_t len;
    netsnmp_tdata_row       *row;
    struct schedTable_entry *entry;

    DEBUGMSGTL(("disman:schedule:conf", "Parsing schedTable config...  "));

    /*
     * Read in the index information for this entry
     *  and create a (non-fixed) data structure for it.
     */
    memset( owner, 0, sizeof(owner));
    memset( name,  0, sizeof(name));
    len  = SCHED_STR1_LEN; vp = owner;
    line = read_config_read_data(ASN_OCTET_STR, line, &vp,  &len);
    len  = SCHED_STR1_LEN; vp = name;
    line = read_config_read_data(ASN_OCTET_STR, line, &vp,  &len);
    row  = schedTable_createEntry(owner, name);
    if (!row || !row->data) {
        config_perror("create schedule entry failure");
        return;
    }
    entry = (struct schedTable_entry *)row->data;
    DEBUGMSG(("disman:schedule:conf", "(%s, %s) ", owner, name));
    
    /*
     * Read in the column values.
     */
    len  = SCHED_STR2_LEN; vp = entry->schedDescr;
    line = read_config_read_data(ASN_OCTET_STR, line, &vp,  &len);
    line = read_config_read_data(ASN_UNSIGNED,  line,
                                     &entry->schedInterval, NULL);
        /* Unpick the various timed bits */
    len  = 22; vp = time_bits;
    line = read_config_read_data(ASN_OCTET_STR, line, &vp,  &len);
    entry->schedWeekDay  = time_bits[0];
    entry->schedMonth[0] = time_bits[1];
    entry->schedMonth[1] = time_bits[2];
    entry->schedHour[0]  = time_bits[11];
    entry->schedHour[1]  = time_bits[12];
    entry->schedHour[2]  = time_bits[13];
    memcpy(entry->schedDay,    time_bits+3,  8);
    memcpy(entry->schedMinute, time_bits+14, 8);

    len  = SCHED_STR1_LEN; vp = entry->schedContextName;
    line = read_config_read_data(ASN_OCTET_STR, line, &vp,  &len);
    len  = MAX_OID_LEN;    vp = entry->schedVariable;
    line = read_config_read_data(ASN_OBJECT_ID, line, &vp,  &len);
    entry->schedVariable_len = len;
    line = read_config_read_data(ASN_INTEGER,  line,
                                     &entry->schedValue, NULL);
    line = read_config_read_data(ASN_UNSIGNED, line,
                                     &entry->schedType,  NULL);
    line = read_config_read_data(ASN_UNSIGNED, line, &len, NULL);
    entry->flags |= (len /* & WHAT ?? */);
    /* XXX - Will need to read in the 'iquery' access information */
    entry->flags |= SCHEDULE_FLAG_VALID;

    DEBUGMSG(("disman:schedule:conf", "\n"));
}

/*
 * Save dynamically-configured schedTable entries into persistent storage
 */
int
store_schedTable(int majorID, int minorID, void *serverarg, void *clientarg)
{
    char              line[SNMP_MAXBUF];
    char              time_bits[22];  /* schedWeekDay..schedMinute */
    char             *cptr;
    void             *vp;
    size_t            tint;
    netsnmp_tdata_row *row;
    struct schedTable_entry *entry;


    DEBUGMSGTL(( "disman:schedule:conf", "Storing schedTable:\n"));

    for (row = netsnmp_tdata_row_first( schedule_table );
         row;
         row = netsnmp_tdata_row_next(  schedule_table, row )) {

        if (!row->data)
            continue;
        entry = (struct schedTable_entry *)row->data;

        /*
         * Only save (dynamically-created) 'nonVolatile' entries
         *    (XXX - what about dynamic 'permanent' entries ??)
         */
        if (entry->schedStorageType != ST_NONVOLATILE )
            continue;
        DEBUGMSGTL(( "disman:schedule:conf", "  Storing (%s, %s)\n",
                             entry->schedOwner, entry->schedName));

        memset(line, 0, sizeof(line));
        strcpy(line, "_schedTable ");
        cptr = line + strlen(line);

        vp   = entry->schedOwner;     tint = strlen( vp );
        cptr = read_config_store_data(ASN_OCTET_STR, cptr, &vp,  &tint );
        vp   = entry->schedName;      tint = strlen( vp );
        cptr = read_config_store_data(ASN_OCTET_STR, cptr, &vp,  &tint );
        vp   = entry->schedDescr;     tint = strlen( vp );
        cptr = read_config_store_data(ASN_OCTET_STR, cptr, &vp,  &tint );
        tint = entry->schedInterval;
        cptr = read_config_store_data(ASN_UNSIGNED,  cptr, &tint, NULL );

        /* Combine all the timed bits into a single field */
        time_bits[0]  = entry->schedWeekDay;
        time_bits[1]  = entry->schedMonth[0];
        time_bits[2]  = entry->schedMonth[1];
        time_bits[11] = entry->schedHour[0];
        time_bits[12] = entry->schedHour[1];
        time_bits[13] = entry->schedHour[2];
        memcpy(time_bits+3,  entry->schedDay,    8);
        memcpy(time_bits+14, entry->schedMinute, 8);
        vp   = time_bits;    tint = 22;
        cptr = read_config_store_data(ASN_OCTET_STR, cptr, &vp, &tint );

        vp   = entry->schedContextName; tint = strlen( vp );
        cptr = read_config_store_data(ASN_OCTET_STR, cptr, &vp,  &tint );
        vp   = entry->schedVariable;
        tint = entry->schedVariable_len;
        cptr = read_config_store_data(ASN_OBJECT_ID, cptr, &vp,  &tint );
        tint = entry->schedValue;
        cptr = read_config_store_data(ASN_INTEGER,   cptr, &tint, NULL );
        tint = entry->schedType;
        cptr = read_config_store_data(ASN_UNSIGNED,  cptr, &tint, NULL );
        tint = entry->flags /* & WHAT ?? */;
        cptr = read_config_store_data(ASN_UNSIGNED,  cptr, &tint, NULL );
        /* XXX - Need to store the 'iquery' access information */
        snmpd_store_config(line);
    }
    DEBUGMSGTL(( "disman:schedule:conf", "  done.\n"));
    return SNMPERR_SUCCESS;
}