OpenDNSSEC-enforcer  1.3.9
ksm_dnsseckeys.c
Go to the documentation of this file.
1 /*
2  * $Id: ksm_dnsseckeys.c 1290 2009-07-15 15:28:23Z sion $
3  *
4  * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
19  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
21  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
23  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
24  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
25  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  *
27  */
28 
29 /*
30  * ksm_dnsseckeys.c - Manipulation of dnssec key Information
31  */
32 
33 #include <assert.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <time.h>
38 
39 #include "ksm/database.h"
40 #include "ksm/database_statement.h"
41 #include "ksm/datetime.h"
42 #include "ksm/db_fields.h"
43 #include "ksm/debug.h"
44 #include "ksm/ksmdef.h"
45 #include "ksm/ksm.h"
46 #include "ksm/ksm_internal.h"
47 #include "ksm/message.h"
48 #include "ksm/string_util.h"
49 
50 /*+
51  * KsmDNSSECKeysInSMCountInit - Query for Key Information
52  *
53  *
54  * Arguments:
55  * DB_RESULT* result
56  * Pointer to a handle to be used for information retrieval. Will
57  * be NULL on error.
58  *
59  * int id
60  * optional id of the security module that the keys must be in
61  *
62  *
63  * Returns:
64  * int
65  * Status return. 0 on success.
66 -*/
67 
69 {
70  int where = 0; /* WHERE clause value */
71  char* sql = NULL; /* SQL query */
72  int status = 0; /* Status return */
73 
74  /* Construct the query */
75 
76  sql = DqsCountInit("dnsseckeys");
77  if (id >= 0) {
78  DqsConditionInt(&sql, "securitymodule_id", DQS_COMPARE_EQ, id, where++);
79  }
80 
81 
82  /* Execute query and free up the query string */
83 
84  status = DbExecuteSql(DbHandle(), sql, result);
85 
86  DqsFree(sql);
87 
88  return status;
89 }
90 
91 /*+
92  * KsmDNSSECKeysInSMCountInit - Query for Policy Information
93  *
94  *
95  * Arguments:
96  * DB_RESULT* result
97  * Pointer to a handle to be used for information retrieval. Will
98  * be NULL on error.
99  *
100  * policy_id
101  * id of the policy that keys must belong to
102  *
103  * key_policy
104  * key policy that the keys must be consitent with.
105  *
106  * int state
107  * state that the key must be in
108  *
109  * Returns:
110  * int
111  * Status return. 0 on success.
112 -*/
113 
114 
115 int KsmDNSSECKeysStateCountInit(DB_RESULT* result, int policy_id, KSM_KEY_POLICY *key_policy, int state)
116 {
117  int where = 0; /* WHERE clause value */
118  char* sql = NULL; /* SQL query */
119  int status = 0; /* Status return */
120 
121  /* Check arguments */
122  if (key_policy == NULL) {
123  return MsgLog(KSM_INVARG, "NULL key_policy");
124  }
125 
126  /* Construct the query */
127 
128  sql = DqsCountInit("dnsseckeys");
129 
130  DqsConditionInt(&sql, "securitymodule_id", DQS_COMPARE_EQ, key_policy->sm, where++);
131  DqsConditionInt(&sql, "policy_id", DQS_COMPARE_EQ, policy_id, where++);
132  DqsConditionInt(&sql, "size", DQS_COMPARE_EQ, key_policy->bits, where++);
133  DqsConditionInt(&sql, "algorithm", DQS_COMPARE_EQ, key_policy->algorithm, where++);
134  DqsConditionInt(&sql, "keytype", DQS_COMPARE_EQ, key_policy->type, where++);
135  DqsConditionInt(&sql, "state", DQS_COMPARE_EQ, state, where++);
136 
137 
138  /* Execute query and free up the query string */
139 
140  status = DbExecuteSql(DbHandle(), sql, result);
141 
142  DqsFree(sql);
143 
144  return status;
145 }
146 
147 /*+
148  * KsmDNSSECKeysInSMCount
149  *
150  * Arguments:
151  * DB_RESULT result
152  * Handle from KsmParameterInit
153  *
154  * count (returns)
155  * count of keys found
156  *
157  * Returns:
158  * int
159  * Status return:
160  * 0 success
161  * -1 end of record set reached
162  * non-zero some error occurred and a message has been output.
163  *
164  * If the status is non-zero, the returned data is meaningless.
165 -*/
166 
167 int KsmDNSSECKeysInSMCount(DB_RESULT result, int* count)
168 {
169  int status = 0; /* Return status */
170  DB_ROW row = NULL; /* Row data */
171 
172  /* Get the next row from the data */
173 
174  status = DbFetchRow(result, &row);
175  if (status == 0) {
176 
177  /* Now copy the results into the output data */
178 
179  status = DbInt(row, DB_COUNT, count);
180  }
181  else if (status == -1) {}
182  /* No rows to return (but no error) */
183  else {
184  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
185  }
186 
187  DbFreeRow(row);
188 
189  return status;
190 }