OpenDNSSEC-enforcer
1.3.8
|
00001 /* 00002 * $Id: ksm_import.c 5838 2011-11-08 14:28:05Z sion $ 00003 * 00004 * Copyright (c) 2008-2009 Nominet UK. All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 1. Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 2. Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * 00015 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00016 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00017 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00018 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 00019 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00020 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 00021 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 00023 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 00024 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 00025 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 * 00027 */ 00028 00029 /* 00030 * ksm_import.c - Import/update configuration data in kasp database 00031 */ 00032 00033 #include <assert.h> 00034 #include <stdio.h> 00035 #include <stdlib.h> 00036 #include <string.h> 00037 #include <time.h> 00038 00039 #include "ksm/database.h" 00040 #include "ksm/database_statement.h" 00041 #include "ksm/datetime.h" 00042 #include "ksm/db_fields.h" 00043 #include "ksm/debug.h" 00044 #include "ksm/ksmdef.h" 00045 #include "ksm/ksm.h" 00046 #include "ksm/ksm_internal.h" 00047 #include "ksm/message.h" 00048 #include "ksm/string_util.h" 00049 #include "ksm/string_util2.h" 00050 00051 /*+ 00052 * KsmImportRepository - Insert or update a repository 00053 * 00054 * 00055 * Arguments: 00056 * 00057 * const char* repo_name 00058 * Name of the repository 00059 * 00060 * const char* repo_capacity 00061 * Capacity for that repository 00062 * 00063 * int require_backup 00064 * flag to indicate if keys in this repo need to be backed up before they can be used 00065 * 00066 * Returns: 00067 * int 00068 * Status return. 0 on success. 00069 * -1 if an unexpected count value was returned 00070 -*/ 00071 00072 int KsmImportRepository(const char* repo_name, const char* repo_capacity, int require_backup) 00073 { 00074 char* sql = NULL; /* SQL query */ 00075 int status = 0; /* Status return */ 00076 int count = 0; /* Do we already have a repository with this name? */ 00077 00078 /* check the main argument (capacity may be NULL) */ 00079 if (repo_name == NULL) { 00080 return MsgLog(KSM_INVARG, "NULL repository name"); 00081 } 00082 00083 /* 00084 * First see if this repository exists 00085 */ 00086 sql = DqsCountInit(DB_SECURITY_MODULE_TABLE); 00087 DqsConditionString(&sql, "NAME", DQS_COMPARE_EQ, repo_name, 0); 00088 DqsEnd(&sql); 00089 00090 /* Execute query and free up the query string */ 00091 status = DbIntQuery(DbHandle(), &count, sql); 00092 DqsFree(sql); 00093 00094 if (status != 0) 00095 { 00096 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle())); 00097 return status; 00098 } 00099 00100 /* If the count was 0 then we do an insert, otherwise we do an update */ 00101 if (count == 0) 00102 { 00103 sql = DisSpecifyInit(DB_SECURITY_MODULE_TABLE, "name, capacity, requirebackup"); 00104 DisAppendString(&sql, repo_name); 00105 DisAppendString(&sql, repo_capacity); 00106 DisAppendInt(&sql, require_backup); 00107 DisEnd(&sql); 00108 00109 status = DbExecuteSqlNoResult(DbHandle(), sql); 00110 DisFree(sql); 00111 } 00112 else if (count == 1) 00113 { 00114 sql = DusInit(DB_SECURITY_MODULE_TABLE); 00115 DusSetString(&sql, "capacity", repo_capacity, 0); 00116 DusSetInt(&sql, "requirebackup", require_backup, 1); 00117 DusConditionString(&sql, "name", DQS_COMPARE_EQ, repo_name, 0); 00118 DusEnd(&sql); 00119 00120 status = DbExecuteSqlNoResult(DbHandle(), sql); 00121 DusFree(sql); 00122 } 00123 else 00124 { 00125 return -1; 00126 } 00127 00128 return status; 00129 } 00130 00131 /*+ 00132 * KsmImportPolicy - Insert a policy (will not be called if policy exists, unlike above 00133 * 00134 * 00135 * Arguments: 00136 * 00137 * const char* policy_name 00138 * Name of the policy 00139 * 00140 * const char* policy_description 00141 * Description for that policy 00142 * 00143 * Returns: 00144 * int 00145 * Status return. 0 on success. 00146 * -1 if an unexpected count value was returned 00147 -*/ 00148 00149 int KsmImportPolicy(const char* policy_name, const char* policy_description) 00150 { 00151 char* sql = NULL; /* SQL query */ 00152 int status = 0; /* Status return */ 00153 00154 /* check the main argument (description may be NULL) */ 00155 if (policy_name == NULL) { 00156 return MsgLog(KSM_INVARG, "NULL policy name"); 00157 } 00158 00159 /* Insert policy */ 00160 sql = DisSpecifyInit("policies", "name, description"); 00161 DisAppendString(&sql, policy_name); 00162 DisAppendString(&sql, policy_description); 00163 DisEnd(&sql); 00164 00165 status = DbExecuteSqlNoResult(DbHandle(), sql); 00166 DisFree(sql); 00167 00168 return status; 00169 } 00170 00171 /*+ 00172 * KsmImportZone - Insert or update a zone 00173 * 00174 * 00175 * Arguments: 00176 * 00177 * const char* zone_name 00178 * Name of the repository 00179 * 00180 * int policy_id 00181 * Policy for the zone 00182 * 00183 * int fail_if_exists 00184 * Set to 1 if you don't want to update existing zones 00185 * 00186 * int *new_zone 00187 * (returned) indicate if the zone was new to the database 00188 * 00189 * const char* signconf 00190 * Where is the signconf saved 00191 * 00192 * const char* input 00193 * Where is the input file 00194 * 00195 * const char* output 00196 * Where is the output file 00197 * 00198 * Returns: 00199 * int 00200 * Status return. 0 on success. 00201 * -1 if an unexpected count value was returned 00202 * -2 if the zone exists and fail_if_exists == 1 00203 * -3 if the zone exists with and without a trailing dot 00204 -*/ 00205 int KsmImportZone(const char* zone_name, int policy_id, int fail_if_exists, int *new_zone, const char* signconf, const char* input, const char* output) 00206 { 00207 char* sql = NULL; /* SQL query */ 00208 int status = 0; /* Status return */ 00209 int count = 0; /* Do we already have a zone with this name? */ 00210 char* zone_name_td = NULL; /* zone name with td swapped */ 00211 char in_clause[KSM_SQL_SIZE]; /* in part of where clause */ 00212 00213 /* check the arguments */ 00214 if (zone_name == NULL || policy_id == 0) { 00215 return MsgLog(KSM_INVARG, "NULL zone name or policy"); 00216 } 00217 00218 /* make copy of zone_name with opposite td to original (unless original is 00219 "."; in which case the copy is identical */ 00220 zone_name_td = StrStrdup(zone_name); 00221 if (strlen(zone_name_td) > 1 && zone_name_td[strlen(zone_name_td)-1] == '.') { 00222 zone_name_td[strlen(zone_name_td)-1] = '\0'; 00223 } 00224 else if (strlen(zone_name_td) > 1) { 00225 StrAppend(&zone_name_td, "."); 00226 } 00227 00228 snprintf(in_clause, KSM_SQL_SIZE, "(\"%s\",\"%s\")", zone_name, zone_name_td); 00229 00230 /* 00231 * First see if this zone exists 00232 */ 00233 sql = DqsCountInit(DB_ZONE_TABLE); 00234 DqsConditionKeyword(&sql, "NAME", DQS_COMPARE_IN, in_clause, 0); 00235 DqsEnd(&sql); 00236 00237 /* Execute query and free up the query string */ 00238 status = DbIntQuery(DbHandle(), &count, sql); 00239 DqsFree(sql); 00240 00241 if (status != 0) 00242 { 00243 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle())); 00244 return status; 00245 } 00246 00247 /* If the count was 0 then we do an insert, otherwise we do an update */ 00248 if (count == 0) 00249 { 00250 sql = DisSpecifyInit(DB_ZONE_TABLE, "name, policy_id, signconf, input, output"); 00251 DisAppendString(&sql, zone_name); 00252 DisAppendInt(&sql, policy_id); 00253 DisAppendString(&sql, signconf); 00254 DisAppendString(&sql, input); 00255 DisAppendString(&sql, output); 00256 DisEnd(&sql); 00257 00258 status = DbExecuteSqlNoResult(DbHandle(), sql); 00259 DisFree(sql); 00260 00261 *new_zone = 1; 00262 } 00263 else if (count == 1) 00264 { 00265 if (fail_if_exists == 1) { 00266 return -2; 00267 } 00268 sql = DusInit(DB_ZONE_TABLE); 00269 DusSetInt(&sql, "policy_id", policy_id, 0); 00270 DusSetString(&sql, "signconf", signconf, 1); 00271 DusSetString(&sql, "input", input, 2); 00272 DusSetString(&sql, "output", output, 3); 00273 DusConditionString(&sql, "name", DQS_COMPARE_EQ, zone_name, 0); 00274 DusEnd(&sql); 00275 00276 status = DbExecuteSqlNoResult(DbHandle(), sql); 00277 DusFree(sql); 00278 00279 *new_zone = 0; 00280 } 00281 else if (count == 2) 00282 { 00283 return -3; 00284 } 00285 else 00286 { 00287 return -1; 00288 } 00289 00290 return status; 00291 } 00292 00293 /*+ 00294 * KsmImportAudit - Import contents of the Audit tag for a policy, which will already exist 00295 * 00296 * 00297 * Arguments: 00298 * 00299 * int policy_id 00300 * ID of the policy 00301 * 00302 * const char* audit_contents 00303 * Audit information for that policy 00304 * 00305 * Returns: 00306 * int 00307 * Status return. 0 on success. 00308 * -1 if an unexpected count value was returned 00309 -*/ 00310 00311 int KsmImportAudit(int policy_id, const char* audit_contents) 00312 { 00313 char* sql = NULL; /* SQL query */ 00314 int status = 0; /* Status return */ 00315 00316 /* Insert policy */ 00317 sql = DusInit("policies"); 00318 DusSetString(&sql, "audit", audit_contents, 0); 00319 DusConditionInt(&sql, "id", DQS_COMPARE_EQ, policy_id, 0); 00320 DusEnd(&sql); 00321 00322 status = DbExecuteSqlNoResult(DbHandle(), sql); 00323 DusFree(sql); 00324 00325 return status; 00326 } 00327 00328 /*+ 00329 * KsmImportKeyPair - Create Entry in the KeyPairs table for an existing key 00330 * 00331 * Description: 00332 * Creates a key in the database. If the retire time is set then it is marked as 00333 * fixed (I.e. it will not be changed to fit the policy timings.) 00334 * 00335 * Arguments: 00336 * policy_id 00337 * policy that the key is created for 00338 * HSMKeyID 00339 * ID the key is refered to in the HSM 00340 * smID 00341 * security module ID 00342 * size 00343 * size of key 00344 * alg 00345 * algorithm used 00346 * state 00347 * state to set key to 00348 * time 00349 * timestamp of entry into state given 00350 * fixDate 00351 * set to 1 if the retire date should be fixed 00352 * 00353 * DB_ID* id (returned) 00354 * ID of the created entry. This will be undefined on error. 00355 * 00356 * Returns: 00357 * int 00358 * Status return. 0=> Success, non-zero => error. 00359 -*/ 00360 int KsmImportKeyPair(int policy_id, const char* HSMKeyID, int smID, int size, int alg, int state, const char* time, int fixDate, DB_ID* id) 00361 { 00362 unsigned long rowid; /* ID of last inserted row */ 00363 int status = 0; /* Status return */ 00364 char* sql = NULL; /* SQL Statement */ 00365 char* columns = NULL; /* what columns are we setting */ 00366 00367 /* Check arguments */ 00368 if (id == NULL) { 00369 return MsgLog(KSM_INVARG, "NULL id"); 00370 } 00371 00372 StrAppend(&columns, "policy_id, HSMkey_id, securitymodule_id, size, algorithm"); 00373 if (state == KSM_STATE_GENERATE) { 00374 StrAppend(&columns, ", "); 00375 StrAppend(&columns, KsmKeywordStateValueToName(state)); 00376 } 00377 if (state == KSM_STATE_ACTIVE && fixDate == 1) { 00378 StrAppend(&columns, ", fixedDate"); 00379 } 00380 00381 sql = DisSpecifyInit("keypairs", columns); 00382 DisAppendInt(&sql, policy_id); 00383 DisAppendString(&sql, HSMKeyID); 00384 DisAppendInt(&sql, smID); 00385 DisAppendInt(&sql, size); 00386 DisAppendInt(&sql, alg); 00387 if (state == KSM_STATE_GENERATE) { 00388 DisAppendString(&sql, time); 00389 } 00390 if (state == KSM_STATE_ACTIVE && fixDate == 1) { 00391 DisAppendInt(&sql, fixDate); 00392 } 00393 DisEnd(&sql); 00394 00395 /* Execute the statement */ 00396 00397 status = DbExecuteSqlNoResult(DbHandle(), sql); 00398 DisFree(sql); 00399 StrFree(columns); 00400 00401 if (status == 0) { 00402 00403 /* Succcess, get the ID of the inserted record */ 00404 00405 status = DbLastRowId(DbHandle(), &rowid); 00406 if (status == 0) { 00407 *id = (DB_ID) rowid; 00408 } 00409 } 00410 00411 return status; 00412 } 00413 00414 int KsmSmIdFromName(const char* name, int *id) 00415 { 00416 char* sql = NULL; /* SQL query */ 00417 int status = 0; /* Status return */ 00418 00419 /* check the argument */ 00420 if (name == NULL) { 00421 return MsgLog(KSM_INVARG, "NULL name"); 00422 } 00423 00424 /* Construct the query */ 00425 00426 sql = DqsSpecifyInit(DB_SECURITY_MODULE_TABLE,"id"); 00427 DqsConditionString(&sql, "name", DQS_COMPARE_EQ, name, 0); 00428 DqsEnd(&sql); 00429 00430 /* Execute query and free up the query string */ 00431 status = DbIntQuery(DbHandle(), id, sql); 00432 DqsFree(sql); 00433 00434 if (status != 0) 00435 { 00436 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle())); 00437 return status; 00438 } 00439 00440 return status; 00441 } 00442 00443 int KsmSerialIdFromName(const char* name, int *id) 00444 { 00445 char* sql = NULL; /* SQL query */ 00446 int status = 0; /* Status return */ 00447 00448 /* check the argument */ 00449 if (name == NULL) { 00450 return MsgLog(KSM_INVARG, "NULL name"); 00451 } 00452 00453 /* Construct the query */ 00454 00455 sql = DqsSpecifyInit("serialmodes","id"); 00456 DqsConditionString(&sql, "name", DQS_COMPARE_EQ, name, 0); 00457 DqsEnd(&sql); 00458 00459 /* Execute query and free up the query string */ 00460 status = DbIntQuery(DbHandle(), id, sql); 00461 DqsFree(sql); 00462 00463 if (status != 0) 00464 { 00465 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle())); 00466 return status; 00467 } 00468 00469 return status; 00470 } 00471 00472 /*+ 00473 * KsmPolicyIdFromName - Given a policy name return the id 00474 * 00475 * 00476 * Arguments: 00477 * 00478 * Name of the policy. 00479 * 00480 * 00481 * Returns: 00482 * int 00483 * 0 Success, value found 00484 * Other Error 00485 -*/ 00486 int KsmPolicyIdFromName(const char* name, int *id) 00487 { 00488 char* sql = NULL; /* SQL query */ 00489 int status = 0; /* Status return */ 00490 00491 /* check the argument */ 00492 if (name == NULL) { 00493 return MsgLog(KSM_INVARG, "NULL name"); 00494 } 00495 00496 /* Construct the query */ 00497 00498 sql = DqsSpecifyInit("policies","id"); 00499 DqsConditionString(&sql, "name", DQS_COMPARE_EQ, name, 0); 00500 DqsEnd(&sql); 00501 00502 /* Execute query and free up the query string */ 00503 status = DbIntQuery(DbHandle(), id, sql); 00504 DqsFree(sql); 00505 00506 if (status != 0) 00507 { 00508 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle())); 00509 return status; 00510 } 00511 00512 return status; 00513 } 00514 00515 /*+ 00516 * KsmMarkPreBackup - Mark a backup as having been prepared 00517 * 00518 * 00519 * Arguments: 00520 * 00521 * int repo_id 00522 * ID of the repository (-1 for all) 00523 * 00524 * const char* datetime 00525 * When the pre backup was done 00526 * 00527 * Returns: 00528 * int 00529 * Status return. 0 on success. 00530 * other on fail 00531 */ 00532 00533 int KsmMarkPreBackup(int repo_id, const char* datetime) 00534 { 00535 char* sql = NULL; /* SQL query */ 00536 int status = 0; /* Status return */ 00537 int count = -1; /* How many keys get marked */ 00538 00539 /* Count how many we will mark */ 00540 sql = DqsCountInit("keypairs"); 00541 if (repo_id != -1) { 00542 DqsConditionInt(&sql, "securitymodule_id", DQS_COMPARE_EQ, repo_id, 0); 00543 StrAppend(&sql, " and pre_backup is null"); 00544 } else { 00545 StrAppend(&sql, " where pre_backup is null"); 00546 } 00547 DqsEnd(&sql); 00548 00549 /* Execute query and free up the query string */ 00550 status = DbIntQuery(DbHandle(), &count, sql); 00551 DqsFree(sql); 00552 00553 if (status != 0) 00554 { 00555 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle())); 00556 return status; 00557 } 00558 00559 if (count == 0) { 00560 /* No work to do */ 00561 return -1; 00562 } 00563 00564 /* Update rows */ 00565 sql = DusInit("keypairs"); 00566 DusSetString(&sql, "PRE_BACKUP", datetime, 0); 00567 if (repo_id != -1) { 00568 DusConditionInt(&sql, "securitymodule_id", DQS_COMPARE_EQ, repo_id, 0); 00569 StrAppend(&sql, " and pre_backup is null"); 00570 } else { 00571 StrAppend(&sql, " where pre_backup is null"); 00572 } 00573 DusEnd(&sql); 00574 00575 status = DbExecuteSqlNoResult(DbHandle(), sql); 00576 DusFree(sql); 00577 00578 return status; 00579 } 00580 00581 /*+ 00582 * KsmRollbackPreBackup - Rollback a backup prepare step 00583 * 00584 * 00585 * Arguments: 00586 * 00587 * int repo_id 00588 * ID of the repository (-1 for all) 00589 * 00590 * Returns: 00591 * int 00592 * Status return. 0 on success. 00593 * other on fail 00594 */ 00595 00596 int KsmRollbackMarkPreBackup(int repo_id) 00597 { 00598 char* sql = NULL; /* SQL query */ 00599 int status = 0; /* Status return */ 00600 int count = -1; /* How many keys get marked */ 00601 00602 /* Count how many we will mark */ 00603 sql = DqsCountInit("keypairs"); 00604 if (repo_id != -1) { 00605 DqsConditionInt(&sql, "securitymodule_id", DQS_COMPARE_EQ, repo_id, 0); 00606 StrAppend(&sql, " and pre_backup is not null"); 00607 StrAppend(&sql, " and backup is null"); 00608 } else { 00609 StrAppend(&sql, " where pre_backup is not null"); 00610 StrAppend(&sql, " and backup is null"); 00611 } 00612 DqsEnd(&sql); 00613 00614 /* Execute query and free up the query string */ 00615 status = DbIntQuery(DbHandle(), &count, sql); 00616 DqsFree(sql); 00617 00618 if (status != 0) 00619 { 00620 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle())); 00621 return status; 00622 } 00623 00624 if (count == 0) { 00625 /* No work to do */ 00626 return -1; 00627 } 00628 00629 /* Update rows */ 00630 sql = DusInit("keypairs"); 00631 DusSetString(&sql, "PRE_BACKUP", NULL, 0); 00632 if (repo_id != -1) { 00633 DusConditionInt(&sql, "securitymodule_id", DQS_COMPARE_EQ, repo_id, 0); 00634 StrAppend(&sql, " and pre_backup is not null"); 00635 StrAppend(&sql, " and backup is null"); 00636 } else { 00637 StrAppend(&sql, " where pre_backup is null"); 00638 StrAppend(&sql, " and backup is null"); 00639 } 00640 DusEnd(&sql); 00641 00642 status = DbExecuteSqlNoResult(DbHandle(), sql); 00643 DusFree(sql); 00644 00645 return status; 00646 } 00647 00648 /*+ 00649 * KsmMarkBackup - Mark a backup as having been done 00650 * 00651 * 00652 * Arguments: 00653 * 00654 * int repo_id 00655 * ID of the repository (-1 for all) 00656 * 00657 * const char* datetime 00658 * When the backup was done 00659 * 00660 * Returns: 00661 * int 00662 * Status return. 0 on success. 00663 * other on fail 00664 */ 00665 00666 int KsmMarkBackup(int repo_id, const char* datetime) 00667 { 00668 char* sql = NULL; /* SQL query */ 00669 int status = 0; /* Status return */ 00670 int count = -1; /* How many keys get marked */ 00671 00672 /* Count how many we will mark */ 00673 sql = DqsCountInit("keypairs"); 00674 if (repo_id != -1) { 00675 DqsConditionInt(&sql, "securitymodule_id", DQS_COMPARE_EQ, repo_id, 0); 00676 StrAppend(&sql, " and pre_backup is not null"); 00677 StrAppend(&sql, " and backup is null"); 00678 } else { 00679 StrAppend(&sql, " where pre_backup is not null"); 00680 StrAppend(&sql, " and backup is null"); 00681 } 00682 DqsEnd(&sql); 00683 00684 /* Execute query and free up the query string */ 00685 status = DbIntQuery(DbHandle(), &count, sql); 00686 DqsFree(sql); 00687 00688 if (status != 0) 00689 { 00690 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle())); 00691 return status; 00692 } 00693 00694 if (count == 0) { 00695 /* No work to do */ 00696 return -1; 00697 } 00698 00699 /* Update rows */ 00700 sql = DusInit("keypairs"); 00701 DusSetString(&sql, "BACKUP", datetime, 0); 00702 if (repo_id != -1) { 00703 DusConditionInt(&sql, "securitymodule_id", DQS_COMPARE_EQ, repo_id, 0); 00704 StrAppend(&sql, " and backup is null"); 00705 StrAppend(&sql, " and pre_backup is not null"); 00706 } else { 00707 StrAppend(&sql, " where backup is null"); 00708 StrAppend(&sql, " and pre_backup is not null"); 00709 } 00710 DusEnd(&sql); 00711 00712 status = DbExecuteSqlNoResult(DbHandle(), sql); 00713 DusFree(sql); 00714 00715 return status; 00716 } 00717 00718 /*+ 00719 * KsmCheckHSMkeyID - Checks if the cka_id exists in the hsm specified 00720 * 00721 * 00722 * Arguments: 00723 * 00724 * int repo_id 00725 * ID of the repository (-1 for all) 00726 * 00727 * const char* cka_id 00728 * ID to look for 00729 * 00730 * int *exists 00731 * Flag to say if the ID exists 00732 * 00733 * Returns: 00734 * int 00735 * Status return. 0 on success. 00736 * -1 if an unexpected count value was returned 00737 -*/ 00738 00739 int KsmCheckHSMkeyID(int repo_id, const char* cka_id, int *exists) 00740 { 00741 char* sql = NULL; /* SQL query */ 00742 int status = 0; /* Status return */ 00743 int count = 0; /* Do we already have a key with this ID? */ 00744 00745 /* check the arguments */ 00746 if (cka_id == NULL) { 00747 return MsgLog(KSM_INVARG, "NULL cka_id"); 00748 } 00749 00750 /* 00751 * Set up the count 00752 */ 00753 sql = DqsCountInit("keypairs"); 00754 DqsConditionString(&sql, "HSMkey_id", DQS_COMPARE_EQ, cka_id, 0); 00755 if (repo_id != -1) { 00756 DqsConditionInt(&sql, "securitymodule_id", DQS_COMPARE_EQ, repo_id, 1); 00757 } 00758 DqsEnd(&sql); 00759 00760 /* Execute query and free up the query string */ 00761 status = DbIntQuery(DbHandle(), &count, sql); 00762 DqsFree(sql); 00763 00764 if (status != 0) 00765 { 00766 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle())); 00767 return status; 00768 } 00769 00770 if (count > 0) { 00771 *exists = 1; 00772 } 00773 else { 00774 *exists = 0; 00775 } 00776 00777 return 0; 00778 } 00779