OpenDNSSEC-enforcer  1.3.4
/build/buildd/opendnssec-1.3.4/enforcer/ksm/ksm_dnsseckeys.c
Go to the documentation of this file.
00001 /*
00002  * $Id: ksm_dnsseckeys.c 1290 2009-07-15 15:28:23Z 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_dnsseckeys.c - Manipulation of dnssec key Information
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 
00050 /*+
00051  * KsmDNSSECKeysInSMCountInit - Query for Key Information
00052  *
00053  *
00054  * Arguments:
00055  *      DB_RESULT* result
00056  *          Pointer to a handle to be used for information retrieval.  Will
00057  *          be NULL on error.
00058  *
00059  *      int id
00060  *          optional id of the security module that the keys must be in
00061  *
00062  *
00063  * Returns:
00064  *      int
00065  *          Status return.  0 on success.
00066 -*/
00067 
00068 int KsmDNSSECKeysInSMCountInit(DB_RESULT* result, int id)
00069 {
00070         int     where = 0;          /* WHERE clause value */
00071         char*   sql = NULL;         /* SQL query */
00072         int     status = 0;         /* Status return */
00073 
00074         /* Construct the query */
00075 
00076         sql = DqsCountInit("dnsseckeys");
00077         if (id >= 0) {
00078                 DqsConditionInt(&sql, "securitymodule_id", DQS_COMPARE_EQ, id, where++);
00079         }
00080 
00081 
00082         /* Execute query and free up the query string */
00083 
00084         status = DbExecuteSql(DbHandle(), sql, result);
00085 
00086         DqsFree(sql);
00087 
00088         return status;
00089 }
00090 
00091 /*+
00092  * KsmDNSSECKeysInSMCountInit - Query for Policy Information
00093  *
00094  *
00095  * Arguments:
00096  *      DB_RESULT* result
00097  *          Pointer to a handle to be used for information retrieval.  Will
00098  *          be NULL on error.
00099  *
00100  *      policy_id
00101  *          id of the policy that keys must belong to
00102  *
00103  *      key_policy
00104  *          key policy that the keys must be consitent with.
00105  *
00106  *      int state
00107  *              state that the key must be in
00108  *
00109  * Returns:
00110  *      int
00111  *          Status return.  0 on success.
00112 -*/
00113 
00114 
00115 int KsmDNSSECKeysStateCountInit(DB_RESULT* result, int policy_id, KSM_KEY_POLICY *key_policy, int state)
00116 {
00117         int     where = 0;          /* WHERE clause value */
00118         char*   sql = NULL;         /* SQL query */
00119         int     status = 0;         /* Status return */
00120 
00121     /* Check arguments */
00122     if (key_policy == NULL) {
00123         return MsgLog(KSM_INVARG, "NULL key_policy");
00124     }
00125 
00126         /* Construct the query */
00127 
00128         sql = DqsCountInit("dnsseckeys");
00129 
00130         DqsConditionInt(&sql, "securitymodule_id", DQS_COMPARE_EQ, key_policy->sm, where++);
00131         DqsConditionInt(&sql, "policy_id", DQS_COMPARE_EQ, policy_id, where++);
00132         DqsConditionInt(&sql, "size", DQS_COMPARE_EQ, key_policy->bits, where++);
00133         DqsConditionInt(&sql, "algorithm", DQS_COMPARE_EQ, key_policy->algorithm, where++);
00134         DqsConditionInt(&sql, "keytype", DQS_COMPARE_EQ, key_policy->type, where++);
00135         DqsConditionInt(&sql, "state", DQS_COMPARE_EQ, state, where++);
00136 
00137 
00138         /* Execute query and free up the query string */
00139 
00140         status = DbExecuteSql(DbHandle(), sql, result);
00141 
00142         DqsFree(sql);
00143 
00144         return status;
00145 }
00146 
00147 /*+
00148  * KsmDNSSECKeysInSMCount
00149  *
00150  * Arguments:
00151  *      DB_RESULT result
00152  *          Handle from KsmParameterInit
00153  *
00154  *              count (returns)
00155  *          count of keys found
00156  *
00157  * Returns:
00158  *      int
00159  *          Status return:
00160  *              0           success
00161  *              -1          end of record set reached
00162  *              non-zero    some error occurred and a message has been output.
00163  *
00164  *          If the status is non-zero, the returned data is meaningless.
00165 -*/
00166 
00167 int KsmDNSSECKeysInSMCount(DB_RESULT result, int* count)
00168 {
00169         int         status = 0;     /* Return status */
00170         DB_ROW      row = NULL;     /* Row data */
00171 
00172         /* Get the next row from the data */
00173 
00174         status = DbFetchRow(result, &row);
00175         if (status == 0) {
00176 
00177                 /* Now copy the results into the output data */
00178 
00179                 status = DbInt(row, DB_COUNT, count);
00180         }
00181     else if (status == -1) {}
00182         /* No rows to return (but no error) */
00183         else {
00184         status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
00185         }
00186 
00187     DbFreeRow(row);
00188 
00189         return status;
00190 }