00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040 #include <assert.h>
00041 #include <stdio.h>
00042 #include <stdlib.h>
00043 #include <string.h>
00044 #include <time.h>
00045
00046 #include "ksm/database.h"
00047 #include "ksm/database_statement.h"
00048 #include "ksm/datetime.h"
00049 #include "ksm/db_fields.h"
00050 #include "ksm/debug.h"
00051 #include "ksm/kmedef.h"
00052 #include "ksm/ksmdef.h"
00053 #include "ksm/ksm.h"
00054 #include "ksm/ksm_internal.h"
00055 #include "ksm/message.h"
00056 #include "ksm/string_util.h"
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084
00085 int KsmParameterInit(DB_RESULT* result, const char* name, const char* category, int policy_id)
00086 {
00087 int where = 0;
00088 char* sql = NULL;
00089 int status = 0;
00090
00091
00092
00093 sql = DqsSpecifyInit("PARAMETER_VIEW", DB_PARAMETER_VIEW_FIELDS);
00094 if (name) {
00095 DqsConditionString(&sql, "NAME", DQS_COMPARE_EQ, name, where++);
00096 DqsConditionString(&sql, "CATEGORY", DQS_COMPARE_EQ, category, where++);
00097 }
00098 DqsConditionInt(&sql, "policy_id", DQS_COMPARE_EQ, policy_id, where++);
00099
00100 DqsOrderBy(&sql, "NAME");
00101
00102
00103
00104 status = DbExecuteSql(DbHandle(), sql, result);
00105
00106 DqsFree(sql);
00107
00108 return status;
00109 }
00110
00111
00112
00113
00114
00115
00116
00117
00118
00119
00120
00121
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135 int KsmParameterExist(DB_RESULT* result, const char* name, const char* category, int* parameter_id)
00136 {
00137 int where = 0;
00138 char* sql = NULL;
00139 DB_ROW row = NULL;
00140 int status = 0;
00141
00142
00143
00144 sql = DqsSpecifyInit("PARAMETER_LIST", DB_PARAMETER_LIST_FIELDS);
00145 DqsConditionString(&sql, "NAME", DQS_COMPARE_EQ, name, where++);
00146 DqsConditionString(&sql, "CATEGORY", DQS_COMPARE_EQ, category, where++);
00147
00148 DqsOrderBy(&sql, "NAME");
00149
00150
00151
00152 status = DbExecuteSql(DbHandle(), sql, result);
00153
00154 if (status == 0) {
00155 status = DbFetchRow(*result, &row);
00156 }
00157 if (status == 0) {
00158 status = DbInt(row, DB_PARAMETER_ID, parameter_id);
00159 }
00160
00161 DqsFree(sql);
00162 DbFreeRow(row);
00163
00164 return status;
00165 }
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190 int KsmParameter(DB_RESULT result, KSM_PARAMETER* data)
00191 {
00192 int status = 0;
00193 DB_ROW row = NULL;
00194
00195 if (data == NULL) {
00196 return MsgLog(KSM_INVARG, "NULL data");
00197 }
00198
00199
00200
00201 memset(data, 0, sizeof(KSM_PARAMETER));
00202
00203
00204
00205 status = DbFetchRow(result, &row);
00206
00207 if (status == 0) {
00208 status = DbStringBuffer(row, DB_PARAMETER_NAME, data->name,
00209 sizeof(data->name));
00210 }
00211 if (status == 0) {
00212 status = DbStringBuffer(row, DB_PARAMETER_CATEGORY, data->category,
00213 sizeof(data->category));
00214 }
00215 if (status == 0) {
00216 status = DbInt(row, DB_PARAMETER_ID, &(data->parameter_id));
00217 }
00218 if (status == 0) {
00219 status = DbInt(row, DB_PARAMETER_VALUE, &(data->value));
00220 }
00221
00222 if (row != NULL) {
00223 DbFreeRow(row);
00224 }
00225
00226 return status;
00227 }
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241 void KsmParameterEnd(DB_RESULT result)
00242 {
00243 DbFreeResult(result);
00244 }
00245
00246
00247
00248
00249
00250
00251
00252
00253
00254
00255
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278 int KsmParameterValue(const char* name, const char* category, int* value, int policy_id, int* parameter_id)
00279 {
00280 DB_RESULT handle;
00281 DB_RESULT handle2;
00282 KSM_PARAMETER data;
00283 int status;
00284
00285
00286 if (value == NULL || parameter_id == NULL) {
00287 return MsgLog(KSM_INVARG, "NULL arg");
00288 }
00289 status = KsmParameterInit(&handle, name, category, policy_id);
00290 if (status == 0) {
00291
00292
00293
00294 status = KsmParameter(handle, &data);
00295 if (status == 0) {
00296 *value = data.value;
00297 *parameter_id = data.parameter_id;
00298 }
00299 else if (status == -1) {
00300 status = KsmParameterExist(&handle2, name, category, parameter_id);
00301 if (status == 0) {
00302
00303 status = -2;
00304 }
00305 else {
00306 status = MsgLog(KME_NOSUCHPAR, name);
00307 }
00308 DbFreeResult(handle2);
00309 }
00310
00311
00312
00313 }
00314 DbFreeResult(handle);
00315
00316 return status;
00317 }
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340 int KsmCollectionInit(KSM_PARCOLL* data)
00341 {
00342 if (data == NULL) {
00343 return MsgLog(KSM_INVARG, "NULL data");
00344 }
00345
00346 data->clockskew = KSM_PAR_CLOCKSKEW;
00347 data->ksklife = KSM_PAR_KSKLIFE;
00348 data->standbyksks = KSM_PAR_STANDBYKSKS;
00349 data->standbyzsks = KSM_PAR_STANDBYZSKS;
00350 data->propdelay = KSM_PAR_PROPDELAY;
00351 data->signint = KSM_PAR_SIGNINT;
00352 data->soamin = KSM_PAR_SOAMIN;
00353 data->soattl = KSM_PAR_SOATTL;
00354 data->zsksiglife = KSM_PAR_ZSKSIGLIFE;
00355 data->zsklife = KSM_PAR_ZSKLIFE;
00356 data->zskttl = KSM_PAR_ZSKTTL;
00357 data->kskttl = KSM_PAR_KSKTTL;
00358 data->kskpropdelay = KSM_PAR_KSKPROPDELAY;
00359 data->regdelay = KSM_PAR_REGDELAY;
00360 data->pub_safety = KSM_PAR_PUBSAFETY;
00361 data->ret_safety = KSM_PAR_RETSAFETY;
00362
00363 return(0);
00364 }
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379
00380
00381
00382
00383
00384
00385 int KsmParameterCollection(KSM_PARCOLL* data, int policy_id)
00386 {
00387 int status = 0;
00388 int param_id;
00389
00390
00391 if (data == NULL) {
00392 return MsgLog(KSM_INVARG, "NULL data");
00393 }
00394
00395 status = KsmParameterValue(KSM_PAR_CLOCKSKEW_STRING, KSM_PAR_CLOCKSKEW_CAT, &(data->clockskew), policy_id, ¶m_id);
00396 if (status > 0) return status;
00397
00398 status = KsmParameterValue(KSM_PAR_KSKLIFE_STRING, KSM_PAR_KSKLIFE_CAT, &(data->ksklife), policy_id, ¶m_id);
00399 if (status > 0) return status;
00400
00401 status = KsmParameterValue(KSM_PAR_STANDBYKSKS_STRING, KSM_PAR_STANDBYKSKS_CAT, &(data->standbyksks), policy_id, ¶m_id);
00402 if (status > 0) return status;
00403
00404 status = KsmParameterValue(KSM_PAR_STANDBYZSKS_STRING, KSM_PAR_STANDBYZSKS_CAT, &(data->standbyzsks), policy_id, ¶m_id);
00405 if (status > 0) return status;
00406
00407 status = KsmParameterValue(KSM_PAR_PROPDELAY_STRING, KSM_PAR_PROPDELAY_CAT, &(data->propdelay), policy_id, ¶m_id);
00408 if (status > 0) return status;
00409
00410 status = KsmParameterValue(KSM_PAR_SIGNINT_STRING, KSM_PAR_SIGNINT_CAT, &(data->signint), policy_id, ¶m_id);
00411 if (status > 0) return status;
00412
00413 status = KsmParameterValue(KSM_PAR_SOAMIN_STRING, KSM_PAR_SOAMIN_CAT, &(data->soamin), policy_id, ¶m_id);
00414 if (status > 0) return status;
00415
00416 status = KsmParameterValue(KSM_PAR_SOATTL_STRING, KSM_PAR_SOATTL_CAT, &(data->soattl), policy_id, ¶m_id);
00417 if (status > 0) return status;
00418
00419 status = KsmParameterValue(KSM_PAR_ZSKSIGLIFE_STRING, KSM_PAR_ZSKSIGLIFE_CAT, &(data->zsksiglife), policy_id, ¶m_id);
00420 if (status > 0) return status;
00421
00422 status = KsmParameterValue(KSM_PAR_ZSKLIFE_STRING, KSM_PAR_ZSKLIFE_CAT, &(data->zsklife), policy_id, ¶m_id);
00423 if (status > 0) return status;
00424
00425 status = KsmParameterValue(KSM_PAR_ZSKTTL_STRING, KSM_PAR_ZSKTTL_CAT, &(data->zskttl), policy_id, ¶m_id);
00426 if (status > 0) return status;
00427
00428 status = KsmParameterValue(KSM_PAR_KSKTTL_STRING, KSM_PAR_KSKTTL_CAT, &(data->kskttl), policy_id, ¶m_id);
00429 if (status > 0) return status;
00430
00431 status = KsmParameterValue(KSM_PAR_KSKPROPDELAY_STRING, KSM_PAR_KSKPROPDELAY_CAT, &(data->kskpropdelay), policy_id, ¶m_id);
00432 if (status > 0) return status;
00433
00434 status = KsmParameterValue(KSM_PAR_REGDELAY_STRING, KSM_PAR_REGDELAY_CAT, &(data->regdelay), policy_id, ¶m_id);
00435 if (status > 0) return status;
00436
00437 status = KsmParameterValue(KSM_PAR_PUBSAFETY_STRING, KSM_PAR_PUBSAFETY_CAT, &(data->pub_safety), policy_id, ¶m_id);
00438 if (status > 0) return status;
00439
00440 status = KsmParameterValue(KSM_PAR_RETSAFETY_STRING, KSM_PAR_RETSAFETY_CAT, &(data->ret_safety), policy_id, ¶m_id);
00441 if (status > 0) return status;
00442
00443 status = KsmParameterValue(KSM_PAR_KSK_MAN_ROLL_STRING, KSM_PAR_KSK_MAN_ROLL_CAT, &(data->kskmanroll), policy_id, ¶m_id);
00444 if (status > 0) return status;
00445
00446 status = KsmParameterValue(KSM_PAR_ZSK_MAN_ROLL_STRING, KSM_PAR_ZSK_MAN_ROLL_CAT, &(data->zskmanroll), policy_id, ¶m_id);
00447 if (status > 0) return status;
00448
00449 status = KsmParameterValue(KSM_PAR_DSTTL_STRING, KSM_PAR_DSTTL_CAT, &(data->dsttl), policy_id, ¶m_id);
00450 if (status > 0) return status;
00451
00452
00453
00454
00455
00456
00457
00458 data->kskroll = KSM_ROLL_DEFAULT;
00459
00460
00461 return 0;
00462 }
00463
00464
00465
00466
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481
00482
00483
00484
00485
00486
00487 int KsmParameterSet(const char* name, const char* category, int value, int policy_id)
00488 {
00489 int curvalue;
00490 int param_id;
00491 int status = 0;
00492 int set = 0;
00493 char* sql = NULL;
00494 int where = 0;
00495
00496
00497
00498 status = KsmParameterValue(name, category, &curvalue, policy_id, ¶m_id);
00499 if (status == 0) {
00500
00501
00502
00503 sql = DusInit("parameters_policies");
00504 DusSetInt(&sql, "value", value, set++);
00505 DusConditionInt(&sql, "parameter_id", DQS_COMPARE_EQ, param_id, where++);
00506 DusConditionInt(&sql, "policy_id", DQS_COMPARE_EQ, policy_id, where++);
00507 DusEnd(&sql);
00508
00509 status = DbExecuteSqlNoResult(DbHandle(), sql);
00510 DusFree(sql);
00511 }
00512 else if (status == -2) {
00513
00514 sql = DisInit("parameters_policies");
00515 DisAppendInt(&sql, param_id);
00516 DisAppendInt(&sql, policy_id);
00517 DisAppendInt(&sql, value);
00518 DisEnd(&sql);
00519
00520 status = DbExecuteSqlNoResult(DbHandle(), sql);
00521 DisFree(sql);
00522 }
00523
00524
00525
00526
00527
00528
00529 return status;
00530 }
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543 int KsmParameterShow(const char* name, const char* category, int policy_id)
00544 {
00545 KSM_PARAMETER data;
00546 DB_RESULT result;
00547 int param_id;
00548 int status = 0;
00549 char text[32];
00550 int value;
00551
00552
00553
00554
00555
00556
00557 if (name) {
00558 status = KsmParameterValue(name, category, &value, policy_id, ¶m_id);
00559 }
00560
00561 if (status == 0) {
00562
00563
00564
00565 status = KsmParameterInit(&result, name, category, policy_id);
00566 if (status == 0) {
00567 status = KsmParameter(result, &data);
00568 while (status == 0) {
00569
00570
00571
00572 DtSecondsInterval(data.value, text, sizeof(text));
00573
00574
00575
00576 StrTrimR(data.name);
00577 printf("%-12s %-12s %9d (%s)\n", data.name, data.category, data.value, text);
00578
00579
00580
00581 status = KsmParameter(result, &data);
00582 }
00583
00584
00585
00586 KsmParameterEnd(result);
00587 }
00588 }
00589
00590 return 0;
00591 }