File: ksm_parameter.c

package info (click to toggle)
opendnssec 1%3A1.3.9-5
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 6,960 kB
  • sloc: ansic: 41,128; sh: 11,825; ruby: 3,817; xml: 1,045; makefile: 551; perl: 380
file content (617 lines) | stat: -rw-r--r-- 18,741 bytes parent folder | download
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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
/*
 * $Id: ksm_parameter.c 6379 2012-06-05 08:52:37Z jerry $
 *
 * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

/*+
 * ksm_parameter.c - Manipulation of Parameter Information
 *
 * Description:
 *      Holds the functions needed to manipulate the PARAMETER table.
 *
 *      N.B.  The table is the KEYDATA table - rather than the KEY table - as
 *      KEY is a reserved word in SQL.

-*/

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

#include "ksm/database.h"
#include "ksm/database_statement.h"
#include "ksm/datetime.h"
#include "ksm/db_fields.h"
#include "ksm/debug.h"
#include "ksm/kmedef.h"
#include "ksm/ksmdef.h"
#include "ksm/ksm.h"
#include "ksm/ksm_internal.h"
#include "ksm/message.h"
#include "ksm/string_util.h"




/*+
 * KsmParameterInit - Query for Key Information
 *
 * Description:
 *      Performs a query for parameters in the parameter table that match the
 *      given conditions.
 *
 * Arguments:
 *      DB_RESULT* result
 *          Pointer to a result set to be used for information retrieval.  Will
 *          be undefined on error.
 *
 *      const char* name
 *          Name of the parameter to retrieve information on.  If NULL,
 *          information on all parameters is retrieved.
 *
 * Returns:
 *      int
 *          Status return.
 *
 *          	0		Success
 *          	Other	Error.  A message will have been output.
-*/

int KsmParameterInit(DB_RESULT* result, const char* name, const char* category, int policy_id)
{
    int     where = 0;          /* WHERE clause value */
    char*   sql = NULL;         /* SQL query */
    int     status = 0;         /* Status return */

    /* Construct the query */

    sql = DqsSpecifyInit("PARAMETER_VIEW", DB_PARAMETER_VIEW_FIELDS);
    if (name) {
        DqsConditionString(&sql, "NAME", DQS_COMPARE_EQ, name, where++);
        DqsConditionString(&sql, "CATEGORY", DQS_COMPARE_EQ, category, where++);
    }
    DqsConditionInt(&sql, "policy_id", DQS_COMPARE_EQ, policy_id, where++);

    DqsOrderBy(&sql, "NAME");

    /* Execute query and free up the query string */

    status = DbExecuteSql(DbHandle(), sql, result);

    DqsFree(sql);

    return status;
}

/*+
 * KsmParameterExist - Does the parameter exist at all?
 *
 * Description:
 *      Performs a query for parameters in the parameter table that match the
 *      given conditions.
 *
 * Arguments:
 *      DB_RESULT* result
 *          Pointer to a result set to be used for information retrieval.  Will
 *          be undefined on error.
 *
 *      const char* name
 *          Name of the parameter to retrieve information on.  If NULL,
 *          information on all parameters is retrieved.
 *
 * Returns:
 *      int
 *          Status return.
 *
 *          	0		Success
 *          	Other	Error.  A message will have been output.
-*/

int KsmParameterExist(DB_RESULT* result, const char* name, const char* category, int* parameter_id)
{
    int     where = 0;          /* WHERE clause value */
    char*   sql = NULL;         /* SQL query */
    DB_ROW		row = NULL;            /* Row data */
    int     status = 0;         /* Status return */

    /* Construct the query */

    sql = DqsSpecifyInit("PARAMETER_LIST", DB_PARAMETER_LIST_FIELDS);
    DqsConditionString(&sql, "NAME", DQS_COMPARE_EQ, name, where++);
    DqsConditionString(&sql, "CATEGORY", DQS_COMPARE_EQ, category, where++);

    DqsOrderBy(&sql, "NAME");

    /* Execute query and free up the query string */

    status = DbExecuteSql(DbHandle(), sql, result);

    if (status == 0) {
        status = DbFetchRow(*result, &row);
    }
    if (status == 0) {
        status = DbInt(row, DB_PARAMETER_ID, parameter_id);
    }

    DqsFree(sql);
    DbFreeRow(row);

    return status;
}

/*+
 * KsmParameter - Return Parameter Information
 *
 * Description:
 *      Returns information about the next key in the result set.
 *
 * Arguments:
 *      DB_RESULT result
 *          Result set from KsmParameterInit.
 *
 *      KSM_PARAMETER* data
 *          Data is returned in here.
 *
 * Returns:
 *      int
 *          Status return:
 *              0           success
 *              -1          end of record set reached
 *              non-zero    some error occurred and a message has been output.
 *
 *          If the status is non-zero, the returned data is meaningless.
-*/

int KsmParameter(DB_RESULT result, KSM_PARAMETER* data)
{
    int         status = 0;     /* Return status */
    DB_ROW		row = NULL;     /* Row data */

    if (data == NULL) {
        return MsgLog(KSM_INVARG, "NULL data");
    }

	/* Initialize */

	memset(data, 0, sizeof(KSM_PARAMETER));

    /* Get the next row from the data */

	status = DbFetchRow(result, &row);

	if (status == 0) {
        status = DbStringBuffer(row, DB_PARAMETER_NAME, data->name,
            sizeof(data->name));
    }
	if (status == 0) {
        status = DbStringBuffer(row, DB_PARAMETER_CATEGORY, data->category,
            sizeof(data->category));
    }
    if (status == 0) {
        status = DbInt(row, DB_PARAMETER_ID, &(data->parameter_id));
    }
	if (status == 0) {
        status = DbInt(row, DB_PARAMETER_VALUE, &(data->value));
    }

    if (row != NULL) {
        DbFreeRow(row);
    }

    return status;
}


/*+
 * KsmParameterEnd - End Parameter Information
 *
 * Description:
 *      Called at the end of a KsmParameter cycle, frees up a result set.
 *
 * Arguments:
 *      DB_RESULT result
 *          Handle from KsmParameterInit
-*/

void KsmParameterEnd(DB_RESULT result)
{
    DbFreeResult(result);
}



/*+
 * KsmParameterValue - Get Parameter Value
 *
 * Description:
 *      Gets the data for the named parameter.  If the parameter does not
 *      exist, a warning is output and an error returned.
 *
 * Arguments:
 *      const char* name
 *          Name of the parameter.
 *
 *      const char* category
 *          Category of the parameter.
 *
 *      int* value
 *          Location into which the value of the parameter is put.
 *
 *      int policy_id
 *          ID of the policy we are interested in
 *
 *      int* parameter_id
 *          Location into which the ID of the parameter is put.
 *
 * Returns:
 *      int
 *          0       Success, value found
 *          -2      Success, value not set
 *          Other   Error, message has been output
-*/

int KsmParameterValue(const char* name, const char* category, int* value, int policy_id, int* parameter_id)
{
    DB_RESULT       handle;     /* Handle to the parameter information */
    DB_RESULT       handle2;     /* Handle to the parameter information */
    KSM_PARAMETER   data;       /* Parameter data */
    int             status;     /* Status return */

    /* check the arguments */
    if (value == NULL || parameter_id == NULL) {
        return MsgLog(KSM_INVARG, "NULL arg");
    }
    status = KsmParameterInit(&handle, name, category, policy_id);
    if (status == 0) {

        /* Initialized OK, get the value */

        status = KsmParameter(handle, &data);
        if (status == 0) {
            *value = data.value;
            *parameter_id = data.parameter_id;
        }
        else if (status == -1) {
            status = KsmParameterExist(&handle2, name, category, parameter_id);
            if (status == 0) {
                /* parameter by that name exists, but is not set */
                status = -2;
            } 
            else {
                status = MsgLog(KME_NOSUCHPAR, name);
            }
            DbFreeResult(handle2);
        }

        /* ... and tidy up */

    }
    DbFreeResult(handle);

    return status;
}



/*+
 * KsmCollectionInit - Fill In Parameter Collection with defaults
 *
 * Description:
 *      Fills in the parameter collection object with the values of the
 *      parameters given in ksm.h.
 *
 * Arguments:
 *      KSM_PARCOLL* data
 *          Pointer to the parameter collection object.  This will be filled in
 *          by this function.
 *
 * Returns:
 *      int
 *          0       Success
 *          Other   One or more errors,  in which case a message will have been
 *                  output.
-*/

int KsmCollectionInit(KSM_PARCOLL* data)
{
    if (data == NULL) {
        return MsgLog(KSM_INVARG, "NULL data");
    }

    data->clockskew = KSM_PAR_CLOCKSKEW;
    data->ksklife = KSM_PAR_KSKLIFE;
    data->standbyksks = KSM_PAR_STANDBYKSKS;
    data->standbyzsks = KSM_PAR_STANDBYZSKS;
    data->propdelay = KSM_PAR_PROPDELAY;
    data->signint = KSM_PAR_SIGNINT;
    data->soamin = KSM_PAR_SOAMIN;
    data->soattl = KSM_PAR_SOATTL;
    data->zsksiglife = KSM_PAR_ZSKSIGLIFE;
    data->zsklife = KSM_PAR_ZSKLIFE;
    data->zskttl = KSM_PAR_ZSKTTL;
    data->kskttl = KSM_PAR_KSKTTL;
    data->kskpropdelay = KSM_PAR_KSKPROPDELAY;
    data->regdelay = KSM_PAR_REGDELAY;
    data->pub_safety = KSM_PAR_PUBSAFETY;
    data->ret_safety = KSM_PAR_RETSAFETY;

    return(0);
}

/*+
 * KsmParameterCollection - Fill In Parameter Collection Given Name
 *
 * Description:
 *      Fills in the parameter collection object with the values of the
 *      parameters.
 *
 * Arguments:
 *      KSM_PARCOLL* data
 *          Pointer to the parameter collection object.  This will be filled in
 *          by this function.
 *
 * Returns:
 *      int
 *          0       Success
 *          Other   One or more errors,  in which case a message will have been
 *                  output.
-*/

static KSM_PARCOLL __parcoll_cache;
static int __parcoll_cache_policy_id;
static int __parcoll_cached = 0;
static int __parcoll_cache_enabled = 0;

void KsmParameterCollectionCache(int enable) {
    if (enable && !__parcoll_cache_enabled) {
        __parcoll_cache_enabled = 1;
        __parcoll_cached = 0;
    }
    else if (!enable && __parcoll_cache_enabled) {
        __parcoll_cache_enabled = 0;
    }
}

int KsmParameterCollection(KSM_PARCOLL* data, int policy_id)
{
    int status = 0;
    int param_id;

    /* check the arguments */
    if (data == NULL) {
        return MsgLog(KSM_INVARG, "NULL data");
    }

    if (__parcoll_cache_enabled && __parcoll_cached && __parcoll_cache_policy_id == policy_id) {
        memcpy(data, &__parcoll_cache, sizeof(KSM_PARCOLL));
        return 0;
    }

    status = KsmParameterValue(KSM_PAR_CLOCKSKEW_STRING, KSM_PAR_CLOCKSKEW_CAT, &(data->clockskew), policy_id, &param_id);
    if (status > 0) return status;

    status = KsmParameterValue(KSM_PAR_KSKLIFE_STRING, KSM_PAR_KSKLIFE_CAT, &(data->ksklife), policy_id, &param_id);
    if (status > 0) return status;

    status = KsmParameterValue(KSM_PAR_STANDBYKSKS_STRING, KSM_PAR_STANDBYKSKS_CAT, &(data->standbyksks), policy_id, &param_id);
    if (status > 0) return status;

    status = KsmParameterValue(KSM_PAR_STANDBYZSKS_STRING, KSM_PAR_STANDBYZSKS_CAT, &(data->standbyzsks), policy_id, &param_id);
    if (status > 0) return status;

    status = KsmParameterValue(KSM_PAR_PROPDELAY_STRING, KSM_PAR_PROPDELAY_CAT, &(data->propdelay), policy_id, &param_id);
    if (status > 0) return status;

    status = KsmParameterValue(KSM_PAR_SIGNINT_STRING, KSM_PAR_SIGNINT_CAT, &(data->signint), policy_id, &param_id);
    if (status > 0) return status;

    status = KsmParameterValue(KSM_PAR_SOAMIN_STRING, KSM_PAR_SOAMIN_CAT, &(data->soamin), policy_id, &param_id);
    if (status > 0) return status;

    status = KsmParameterValue(KSM_PAR_SOATTL_STRING, KSM_PAR_SOATTL_CAT, &(data->soattl), policy_id, &param_id);
    if (status > 0) return status;

    status = KsmParameterValue(KSM_PAR_ZSKSIGLIFE_STRING, KSM_PAR_ZSKSIGLIFE_CAT, &(data->zsksiglife), policy_id, &param_id);
    if (status > 0) return status;

    status = KsmParameterValue(KSM_PAR_ZSKLIFE_STRING, KSM_PAR_ZSKLIFE_CAT, &(data->zsklife), policy_id, &param_id);
    if (status > 0) return status;

    status = KsmParameterValue(KSM_PAR_ZSKTTL_STRING, KSM_PAR_ZSKTTL_CAT, &(data->zskttl), policy_id, &param_id);
    if (status > 0) return status;

    status = KsmParameterValue(KSM_PAR_KSKTTL_STRING, KSM_PAR_KSKTTL_CAT, &(data->kskttl), policy_id, &param_id);
    if (status > 0) return status;

    status = KsmParameterValue(KSM_PAR_KSKPROPDELAY_STRING, KSM_PAR_KSKPROPDELAY_CAT, &(data->kskpropdelay), policy_id, &param_id);
    if (status > 0) return status;

    status = KsmParameterValue(KSM_PAR_REGDELAY_STRING, KSM_PAR_REGDELAY_CAT, &(data->regdelay), policy_id, &param_id);
    if (status > 0) return status;

    status = KsmParameterValue(KSM_PAR_PUBSAFETY_STRING, KSM_PAR_PUBSAFETY_CAT, &(data->pub_safety), policy_id, &param_id);
    if (status > 0) return status;

    status = KsmParameterValue(KSM_PAR_RETSAFETY_STRING, KSM_PAR_RETSAFETY_CAT, &(data->ret_safety), policy_id, &param_id);
    if (status > 0) return status;

    status = KsmParameterValue(KSM_PAR_KSK_MAN_ROLL_STRING, KSM_PAR_KSK_MAN_ROLL_CAT, &(data->kskmanroll), policy_id, &param_id);
    if (status > 0) return status;

    status = KsmParameterValue(KSM_PAR_ZSK_MAN_ROLL_STRING, KSM_PAR_ZSK_MAN_ROLL_CAT, &(data->zskmanroll), policy_id, &param_id);
    if (status > 0) return status;

    status = KsmParameterValue(KSM_PAR_DSTTL_STRING, KSM_PAR_DSTTL_CAT, &(data->dsttl), policy_id, &param_id);
    if (status > 0) return status;

/* For now we only set our default KSK rollover scheme */
/*    status = KsmParameterValue(KSM_PAR_KSK_ROLL_STRING, KSM_PAR_KSK_ROLL_CAT, &(data->kskroll), policy_id, &param_id);
    if (status > 0) return status;
    else if (status == -2) 
    { */
        /* Not set, use our default */
        data->kskroll = KSM_ROLL_DEFAULT;
    /*}*/

    if (__parcoll_cache_enabled) {
        memcpy(&__parcoll_cache, data, sizeof(KSM_PARCOLL));
        __parcoll_cache_policy_id = policy_id;
        __parcoll_cached = 1;
    }

    return 0;
}




/*+
 * KsmParameterSet - Set Parameter Entry
 *
 * Description:
 *      Sets the value of a parameter in the database.
 *
 * Arguments:
 *      const char* name
 *          Name of parameter to set.  This must exist, else the setting
 *          will fail.
 *
 *      int value
 *          Value of the parameter.  For intervals, this is the value in
 *          seconds.
 *
 * Returns:
 *      int
 *          Status return.  0 => Success, non-zero => error.DisInt
-*/

int KsmParameterSet(const char* name, const char* category, int value, int policy_id)
{
    int             curvalue;               /* Current value */
    int             param_id;               /* Unique ID of this param */
    int             status = 0;             /* Status return */
    int             set = 0;                /* SET clause value */
    char*           sql = NULL;             /* SQL for the insert */
    int             where = 0;              /* WHERE clause value */

    /* Check to see if the parameter exists */

    status = KsmParameterValue(name, category, &curvalue, policy_id, &param_id);
    if (status == 0) {

        /* It does.  Update the value */

        sql = DusInit("parameters_policies");
        DusSetInt(&sql, "value", value, set++);
        DusConditionInt(&sql, "parameter_id", DQS_COMPARE_EQ, param_id, where++);
        DusConditionInt(&sql, "policy_id", DQS_COMPARE_EQ, policy_id, where++);
        DusEnd(&sql);

        status = DbExecuteSqlNoResult(DbHandle(), sql);
        DusFree(sql);
    }
    else if (status == -2) {
        /* param name is legal, but is not set for this policy */
        sql = DisInit("parameters_policies");
        DisAppendInt(&sql, param_id);
        DisAppendInt(&sql, policy_id);
        DisAppendInt(&sql, value);
        DisEnd(&sql);

        status = DbExecuteSqlNoResult(DbHandle(), sql);
        DisFree(sql);
    }
    /*
     * else {
     *      Error.  A message will have been output.
     * }
     */

    return status;
}

/*+
 * KsmParameterShow - Show Parameter
 *
 * Description:
 *      Prints to stdout the values of the parameter (or parameters).
 *
 * Arguments:
 *      const char* name
 *          Name of parameter to output, or NULL for all parameters.
-*/

int KsmParameterShow(const char* name, const char* category, int policy_id)
{
    KSM_PARAMETER data;         /* Parameter information */
    DB_RESULT	result;         /* Result of parameter query */
    int         param_id;       /* Unique ID of param */
    int         status = 0;     /* Status return */
    char        text[32];       /* For translated string */
    int         value;          /* Value of the parameter */

    /*
     * If a parameter was given, does it exist?  An error will be output if not
     * and the status return will be non-zero.
     */

    if (name) {
        status = KsmParameterValue(name, category, &value, policy_id, &param_id);
    }

    if (status == 0) {

        /* No problem to perform ther listing */

        status = KsmParameterInit(&result, name, category, policy_id);
        if (status == 0) {
            status = KsmParameter(result, &data);
            while (status == 0) {

                /* Get a text form of the value */

                DtSecondsInterval(data.value, text, sizeof(text));

                /* ... and print */

                StrTrimR(data.name);
                printf("%-12s %-12s %9d (%s)\n", data.name, data.category, data.value, text);
                
                /* Get the next parameter */

                status = KsmParameter(result, &data);
            }

            /* All done, so tidy up */

            KsmParameterEnd(result);
        }
    }

    return 0;
}