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
00041
00042
00043 #include <stdio.h>
00044 #include <limits.h>
00045
00046 #include "ksm/database.h"
00047 #include "ksm/db_fields.h"
00048 #include "ksm/debug.h"
00049 #include "ksm/ksm.h"
00050 #include "ksm/kmedef.h"
00051 #include "ksm/ksmdef.h"
00052 #include "ksm/message.h"
00053
00054 #define MAX(a, b) ((a) > (b) ? (a) : (b))
00055 #define MIN(a, b) ((a) < (b) ? (a) : (b))
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072 int KsmUpdate(int policy_id, int zone_id)
00073 {
00074 KSM_PARCOLL collection;
00075 KSM_KEYDATA data;
00076 DB_RESULT result;
00077 int status = 0;
00078 DQS_QUERY_CONDITION condition[2];
00079
00080
00081 KsmCollectionInit(&collection);
00082
00083
00084 status = KsmParameterCollection(&collection, policy_id);
00085 if (status == 0) {
00086
00087
00088
00089
00090
00091
00092
00093
00094 if (zone_id == -1) {
00095 status = KsmKeyInit(&result, NULL);
00096 }
00097 else {
00098 condition[0].code = DB_KEYDATA_ZONE_ID;
00099 condition[0].data.number = zone_id;
00100 condition[0].compare = DQS_COMPARE_EQ;
00101
00102 condition[1].compare = DQS_END_OF_LIST;
00103
00104 status = KsmKeyInit(&result, condition);
00105 }
00106
00107 if (status == 0) {
00108
00109 status = KsmKey(result, &data);
00110 while (status == 0) {
00111 (void) KsmUpdateKey(&data, &collection, zone_id);
00112 status = KsmKey(result, &data);
00113 }
00114 (void) KsmKeyEnd(result);
00115
00116
00117
00118 if (status == -1) {
00119 status = 0;
00120 }
00121 }
00122 }
00123
00124
00125
00126
00127
00128
00129
00130 return status;
00131 }
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 void KsmUpdateKey(KSM_KEYDATA* data, KSM_PARCOLL* collection, int zone_id)
00153 {
00154
00155 if (data == NULL) {
00156 MsgLog(KSM_INVARG, "NULL data");
00157 return;
00158 }
00159
00160 switch (data->state) {
00161 case KSM_STATE_GENERATE:
00162 KsmUpdateGenerateKeyTime(data);
00163 break;
00164
00165 case KSM_STATE_PUBLISH:
00166 KsmUpdatePublishKeyTime(data, collection, zone_id);
00167 break;
00168
00169 case KSM_STATE_READY:
00170 KsmUpdateReadyKeyTime(data);
00171 break;
00172
00173 case KSM_STATE_ACTIVE:
00174 KsmUpdateActiveKeyTime(data, collection, zone_id);
00175 break;
00176
00177 case KSM_STATE_RETIRE:
00178 KsmUpdateRetireKeyTime(data, collection, zone_id);
00179 break;
00180
00181 case KSM_STATE_DEAD:
00182 KsmUpdateDeadKeyTime(data);
00183 break;
00184
00185 case KSM_STATE_DSSUB:
00186
00187 break;
00188
00189 case KSM_STATE_DSPUBLISH:
00190 KsmUpdateDSPublishKeyTime(data, collection, zone_id);
00191 break;
00192
00193 case KSM_STATE_DSREADY:
00194
00195 break;
00196
00197 case KSM_STATE_KEYPUBLISH:
00198 KsmUpdateKEYPublishKeyTime(data, collection, zone_id);
00199 break;
00200 default:
00201
00202
00203
00204 MsgLog(KME_UNRKEYSTA, (int) data->keypair_id, data->state);
00205 }
00206
00207 return;
00208 }
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218
00219
00220
00221
00222
00223 void KsmUpdateGenerateKeyTime(KSM_KEYDATA* data)
00224 {
00225
00226
00227
00228
00229
00230
00231 if (data == NULL) {
00232 MsgLog(KSM_INVARG, "NULL data");
00233 return;
00234 }
00235 DbgOutput(DBG_M_UPDATE, "Key ID %d in state 'generate' - not updated\n",
00236 (int) data->keypair_id);
00237
00238 return;
00239 }
00240
00241 void KsmUpdatePublishKeyTime(KSM_KEYDATA* data, KSM_PARCOLL* collection, int zone_id)
00242 {
00243 int deltat = 0;
00244 int Ipc;
00245
00246
00247 if (data == NULL || collection == NULL) {
00248 MsgLog(KSM_INVARG, "NULL argument");
00249 return;
00250 }
00251 DbgOutput(DBG_M_UPDATE, "Key ID %d in state 'publish' - updating\n",
00252 (int) data->keypair_id);
00253
00254 Ipc = collection->zskttl +
00255 collection->propdelay + collection->pub_safety;
00256 if (data->keytype == KSM_TYPE_ZSK) {
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270
00271 deltat = Ipc;
00272 }
00273 else if (data->keytype == KSM_TYPE_KSK) {
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286
00287
00288
00289
00290 if (collection->kskroll == KSM_ROLL_DNSKEY) {
00291 deltat = Ipc;
00292 }
00293 else if (collection->kskroll == KSM_ROLL_DS) {
00294 deltat = collection->kskttl + collection->kskpropdelay +
00295 collection->pub_safety;
00296 }
00297 }
00298 else {
00299 return;
00300 }
00301
00302 (void) KsmUpdateKeyTime(data, "PUBLISH", "READY", deltat, zone_id);
00303
00304 return;
00305 }
00306
00307 void KsmUpdateReadyKeyTime(KSM_KEYDATA* data)
00308 {
00309
00310
00311
00312
00313
00314
00315 if (data == NULL) {
00316 MsgLog(KSM_INVARG, "NULL data");
00317 return;
00318 }
00319 DbgOutput(DBG_M_UPDATE, "Key ID %d in state 'ready' - not updated\n",
00320 (int) data->keypair_id);
00321
00322 return;
00323 }
00324
00325 void KsmUpdateActiveKeyTime(KSM_KEYDATA* data, KSM_PARCOLL* collection, int zone_id)
00326 {
00327 int deltat;
00328
00329
00330 if (data == NULL || collection == NULL) {
00331 MsgLog(KSM_INVARG, "NULL argument");
00332 return;
00333 }
00334 DbgOutput(DBG_M_UPDATE, "Key ID %d in state 'active' - updating\n",
00335 (int) data->keypair_id);
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348 if (data->keytype == KSM_TYPE_ZSK) {
00349 deltat = collection->zsklife;
00350 }
00351 else if (data->keytype == KSM_TYPE_KSK) {
00352 deltat = collection->ksklife;
00353 }
00354 else {
00355 return;
00356 }
00357
00358
00359 if (deltat == 0) {
00360 deltat = INT_MAX -1;
00361 }
00362
00363
00364
00365
00366
00367
00368 if (!data->fixedDate) {
00369 (void) KsmUpdateKeyTime(data, "ACTIVE", "RETIRE", deltat, zone_id);
00370 }
00371
00372 return;
00373 }
00374
00375 void KsmUpdateRetireKeyTime(KSM_KEYDATA* data, KSM_PARCOLL* collection, int zone_id)
00376 {
00377 int deltat = 0;
00378
00379
00380 if (data == NULL || collection == NULL) {
00381 MsgLog(KSM_INVARG, "NULL argument");
00382 return;
00383 }
00384 DbgOutput(DBG_M_UPDATE, "Key ID %d in state 'retire' - updating\n",
00385 (int) data->keypair_id);
00386
00387
00388
00389
00390
00391
00392
00393
00394
00395
00396
00397
00398
00399
00400 if (data->keytype == KSM_TYPE_ZSK) {
00401 deltat = collection->zsksiglife + collection->propdelay + collection->ret_safety;
00402 }
00403 else if (data->keytype == KSM_TYPE_KSK) {
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416 deltat = collection->dsttl + collection->kskpropdelay +
00417 collection->ret_safety;
00418 }
00419 else {
00420 return;
00421 }
00422
00423 (void) KsmUpdateKeyTime(data, "RETIRE", "DEAD", deltat, zone_id);
00424
00425 return;
00426 }
00427
00428 void KsmUpdateDeadKeyTime(KSM_KEYDATA* data)
00429 {
00430
00431
00432
00433
00434
00435
00436
00437 if (data == NULL) {
00438 MsgLog(KSM_INVARG, "NULL data");
00439 return;
00440 }
00441 DbgOutput(DBG_M_UPDATE, "Key ID %d in state 'dead' - not updated\n",
00442 (int) data->keypair_id);
00443
00444 return;
00445 }
00446
00447 void KsmUpdateDSPublishKeyTime(KSM_KEYDATA* data, KSM_PARCOLL* collection, int zone_id)
00448 {
00449 int deltat = 0;
00450
00451
00452 if (data == NULL || collection == NULL) {
00453 MsgLog(KSM_INVARG, "NULL argument");
00454 return;
00455 }
00456 DbgOutput(DBG_M_UPDATE, "Key ID %d in state 'publish' - updating\n",
00457 (int) data->keypair_id);
00458
00459 if (data->keytype == KSM_TYPE_ZSK) {
00460
00461
00462
00463
00464 return;
00465 }
00466 else if (data->keytype == KSM_TYPE_KSK) {
00467
00468
00469
00470
00471
00472
00473
00474
00475
00476
00477
00478
00479
00480
00481 deltat = collection->kskttl + collection->kskpropdelay +
00482 collection->pub_safety;
00483 }
00484 else {
00485 return;
00486 }
00487
00488 (void) KsmUpdateKeyTime(data, "PUBLISH", "READY", deltat, zone_id);
00489
00490 return;
00491 }
00492
00493 void KsmUpdateKEYPublishKeyTime(KSM_KEYDATA* data, KSM_PARCOLL* collection, int zone_id)
00494 {
00495 int deltat = 0;
00496
00497
00498 if (data == NULL || collection == NULL) {
00499 MsgLog(KSM_INVARG, "NULL argument");
00500 return;
00501 }
00502 DbgOutput(DBG_M_UPDATE, "Key ID %d in state 'KEYpublish' - updating\n",
00503 (int) data->keypair_id);
00504
00505
00506
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518 deltat = collection->zskttl +
00519 collection->propdelay + collection->pub_safety;
00520
00521 (void) KsmUpdateKeyTime(data, "PUBLISH", "ACTIVE", deltat, zone_id);
00522
00523 return;
00524 }
00525
00526
00527
00528
00529
00530
00531
00532
00533
00534
00535
00536
00537
00538
00539
00540
00541
00542
00543
00544
00545
00546
00547
00548
00549
00550
00551
00552
00553
00554
00555
00556
00557 int KsmUpdateKeyTime(const KSM_KEYDATA* data, const char* source,
00558 const char* destination, int interval, int zone_id)
00559 {
00560 char buffer[KSM_SQL_SIZE];
00561 unsigned int nchar;
00562 int status;
00563
00564
00565 if (data == NULL || source == NULL || destination == NULL) {
00566 return MsgLog(KSM_INVARG, "NULL argument");
00567 }
00568
00569 #ifdef USE_MYSQL
00570 nchar = snprintf(buffer, sizeof(buffer),
00571 "UPDATE dnsseckeys SET %s = DATE_ADD(%s, INTERVAL %d SECOND) WHERE KEYPAIR_ID = %lu and zone_id = %d",
00572 destination, source, interval, (unsigned long) data->keypair_id, zone_id);
00573 #else
00574 nchar = snprintf(buffer, sizeof(buffer),
00575 "UPDATE dnsseckeys SET %s = DATETIME(%s, '+%d SECONDS') WHERE KEYPAIR_ID = %lu and zone_id = %d",
00576 destination, source, interval, (unsigned long) data->keypair_id, zone_id);
00577 #endif
00578
00579 if (nchar < sizeof(buffer)) {
00580
00581
00582
00583 status = DbExecuteSqlNoResult(DbHandle(), buffer);
00584 }
00585 else {
00586
00587
00588
00589 status = MsgLog(KME_BUFFEROVF, "KsmUpdateKeyTime");
00590 }
00591
00592 return status;
00593 }