OpenDNSSEC-enforcer  1.4.5
ksm_zone.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
27 /*
28  * ksm_zone.c - Manipulation of Zone Information
29  */
30 
31 #include <assert.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <time.h>
36 
37 #include "ksm/database.h"
38 #include "ksm/database_statement.h"
39 #include "ksm/datetime.h"
40 #include "ksm/db_fields.h"
41 #include "ksm/debug.h"
42 #include "ksm/ksmdef.h"
43 #include "ksm/ksm.h"
44 #include "ksm/ksm_internal.h"
45 #include "ksm/message.h"
46 #include "ksm/string_util.h"
47 
48 /*+
49  * KsmZoneInit - Query for Zone Information
50  *
51  *
52  * Arguments:
53  * DB_RESULT* result
54  * Pointer to a handle to be used for information retrieval. Will
55  * be NULL on error.
56  *
57  * const char* name
58  * Name of the parameter to retrieve information on. If NULL, information
59  * on all parameters is retrieved.
60  *
61  * Returns:
62  * int
63  * Status return. 0 on success.
64 -*/
65 
66 int KsmZoneInit(DB_RESULT* result, int policy_id)
67 {
68  int where = 0; /* WHERE clause value */
69  char* sql = NULL; /* SQL query */
70  int status = 0; /* Status return */
71 
72  /* Construct the query */
73 
75  if (policy_id != -1) {
76  DqsConditionInt(&sql, "policy_id", DQS_COMPARE_EQ, policy_id, where++);
77 
78  }
79  DqsOrderBy(&sql, "policy_id");
80 
81  /* Execute query and free up the query string */
82 
83  status = DbExecuteSql(DbHandle(), sql, result);
84 
85  DqsFree(sql);
86 
87  return status;
88 }
89 
90 /*+
91  * KsmZoneCountInit
92  *
93  *
94  * Arguments:
95  * DB_RESULT* result
96  * Pointer to a handle to be used for information retrieval. Will
97  * be NULL on error.
98  *
99  * id
100  * id of the policy
101  *
102  * Returns:
103  * int
104  * Status return. 0 on success.
105 -*/
106 
107 int KsmZoneCountInit(DB_RESULT* result, int id)
108 {
109  int where = 0; /* WHERE clause value */
110  char* sql = NULL; /* SQL query */
111  int status = 0; /* Status return */
112 
113  /* Construct the query */
114 
116  if (id >= 0) {
117  DqsConditionInt(&sql, "policy_id", DQS_COMPARE_EQ, id, where++);
118  }
119 
120 
121  /* Execute query and free up the query string */
122 
123  status = DbExecuteSql(DbHandle(), sql, result);
124 
125  DqsFree(sql);
126 
127  return status;
128 }
129 
130 /*+
131  * KsmZone - Return Zone Information
132  *
133  * Arguments:
134  * DB_RESULT result
135  * Handle from KsmParameterInit
136  *
137  * KSM_PARAMETER* data
138  * Data is returned in here.
139  *
140  * Returns:
141  * int
142  * Status return:
143  * 0 success
144  * -1 end of record set reached
145  * non-zero some error occurred and a message has been output.
146  *
147  * If the status is non-zero, the returned data is meaningless.
148 -*/
149 
150 int KsmZone(DB_RESULT result, KSM_ZONE *data)
151 {
152  int status = 0; /* Return status */
153  DB_ROW row = NULL; /* Row data */
154 
155  /* Get the next row from the data */
156  status = DbFetchRow(result, &row);
157 
158  if (status == 0) {
159 
160  /* Now copy the results into the output data */
161  DbInt(row, DB_ZONE_ID, &(data->id));
162  DbStringBuffer(row, DB_ZONE_NAME, data->name,
163  KSM_ZONE_NAME_LENGTH*sizeof(char));
164  DbInt(row, DB_ZONE_POLICY_ID, &(data->policy_id));
166  KSM_PATH_LENGTH*sizeof(char));
167  DbStringBuffer(row, DB_ZONE_INPUT, data->input,
168  KSM_PATH_LENGTH*sizeof(char));
170  KSM_PATH_LENGTH*sizeof(char));
172  KSM_ADAPTER_NAME_LENGTH*sizeof(char));
174  KSM_ADAPTER_NAME_LENGTH*sizeof(char));
175  }
176  else if (status == -1) {}
177  /* No rows to return (but no error) */
178  else {
179  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
180  }
181 
182  if (row != NULL) {
183  DbFreeRow(row);
184  }
185 
186  return status;
187 }
188 /*+
189  * KsmZoneCount
190  *
191  * Arguments:
192  * DB_RESULT result
193  * Handle from KsmParameterInit
194  *
195  *
196  * Returns:
197  * int
198  * Status return:
199  * 0 success
200  * -1 end of record set reached
201  * non-zero some error occurred and a message has been output.
202  *
203  * If the status is non-zero, the returned data is meaningless.
204 -*/
205 
206 int KsmZoneCount(DB_RESULT result, int* count)
207 {
208  int status = 0; /* Return status */
209  DB_ROW row = NULL; /* Row data */
210 
211  /* Get the next row from the data */
212  status = DbFetchRow(result, &row);
213 
214  if (status == 0) {
215 
216  /* Now copy the results into the output data */
217  status = DbInt(row, DB_COUNT, count);
218 
219  }
220  else if (status == -1) {}
221  /* No rows to return (but no error) */
222  else {
223  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
224  }
225 
226  DbFreeRow(row);
227 
228  return status;
229 }
230 
231 /*+
232  * KsmZoneIdFromName
233  *
234  * Arguments:
235  * const char* zone_name name of the zone to get the id for
236  * int* zone_id returned id
237  *
238  * Returns:
239  * int
240  * Status return:
241  * 0 success
242  * -1 no record found
243  * non-zero some error occurred and a message has been output.
244  *
245  * If the status is non-zero, the returned data is meaningless.
246 -*/
247 int KsmZoneIdFromName(const char* zone_name, int* zone_id)
248 {
249  int where = 0; /* WHERE clause value */
250  char* sql = NULL; /* SQL query */
251  DB_RESULT result; /* Handle converted to a result object */
252  DB_ROW row = NULL; /* Row data */
253  int status = 0; /* Status return */
254 
255  /* check the argument */
256  if (zone_name == NULL) {
257  return MsgLog(KSM_INVARG, "NULL zone name");
258  }
259 
260  /* Construct the query */
261 
262  sql = DqsSpecifyInit("zones","id, name");
263  DqsConditionString(&sql, "NAME", DQS_COMPARE_EQ, zone_name, where++);
264  DqsOrderBy(&sql, "id");
265 
266  /* Execute query and free up the query string */
267  status = DbExecuteSql(DbHandle(), sql, &result);
268  DqsFree(sql);
269 
270  if (status != 0)
271  {
272  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
273  DbFreeResult(result);
274  return status;
275  }
276 
277  /* Get the next row from the data */
278  status = DbFetchRow(result, &row);
279  if (status == 0) {
280  DbInt(row, DB_ZONE_ID, zone_id);
281  }
282  else if (status == -1) {}
283  /* No rows to return (but no DB error) */
284  else {
285  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
286  }
287 
288  DbFreeRow(row);
289  DbFreeResult(result);
290  return status;
291 }
292 
293 /*+
294  * KsmZoneIdAndPolicyFromName
295  *
296  * Arguments:
297  * const char* zone_name name of the zone to get the id for
298  * int* policy_id returned id
299  * int* zone_id returned id
300  *
301  * Returns:
302  * int
303  * Status return:
304  * 0 success
305  * -1 no record found
306  * non-zero some error occurred and a message has been output.
307  *
308  * If the status is non-zero, the returned data is meaningless.
309 -*/
310 int KsmZoneIdAndPolicyFromName(const char* zone_name, int* policy_id, int* zone_id)
311 {
312  int where = 0; /* WHERE clause value */
313  char* sql = NULL; /* SQL query */
314  DB_RESULT result; /* Handle converted to a result object */
315  DB_ROW row = NULL; /* Row data */
316  int status = 0; /* Status return */
317 
318  /* check the argument */
319  if (zone_name == NULL) {
320  return MsgLog(KSM_INVARG, "NULL zone name");
321  }
322 
323  /* Construct the query */
324 
325  sql = DqsSpecifyInit("zones","id, name, policy_id");
326  DqsConditionString(&sql, "NAME", DQS_COMPARE_EQ, zone_name, where++);
327  DqsOrderBy(&sql, "id");
328 
329  /* Execute query and free up the query string */
330  status = DbExecuteSql(DbHandle(), sql, &result);
331  DqsFree(sql);
332 
333  if (status != 0)
334  {
335  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
336  DbFreeResult(result);
337  return status;
338  }
339 
340  /* Get the next row from the data */
341  status = DbFetchRow(result, &row);
342  if (status == 0) {
343  DbInt(row, DB_ZONE_ID, zone_id);
344  DbInt(row, DB_ZONE_POLICY_ID, policy_id);
345  }
346  else if (status == -1) {}
347  /* No rows to return (but no DB error) */
348  else {
349  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
350  }
351 
352  DbFreeRow(row);
353  DbFreeResult(result);
354  return status;
355 }
356 
357 /*+
358  * KsmDeleteZone
359  *
360  * Description:
361  * Will remove all dnsseckeys allocated to a zone before removing the entry in
362  * the zones table itself
363  *
364  * Arguments:
365  * int zone_id id of the zone to be deleted (-1 will delete all)
366  *
367  * Returns:
368  * int
369  * Status return. 0=> Success, non-zero => error.
370 -*/
371 
372 int KsmDeleteZone(int zone_id)
373 {
374  int status = 0; /* Status return */
375  char* sql = NULL; /* SQL Statement */
376 
377  /* Delete from zones */
378  sql = DdsInit("zones");
379  if (zone_id != -1) {
380  DdsConditionInt(&sql, "id", DQS_COMPARE_EQ, zone_id, 0);
381  }
382  DdsEnd(&sql);
383 
384  status = DbExecuteSqlNoResult(DbHandle(), sql);
385  DdsFree(sql);
386 
387  if (status != 0)
388  {
389  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
390  return status;
391  }
392 
393  return status;
394 }
395 
396 /*+
397  * KsmZoneNameFromId
398  *
399  * Arguments:
400  * int zone_id id of the zone to get the name for
401  * char** zone_name returned name
402  *
403  * Returns:
404  * int
405  * Status return:
406  * 0 success
407  * -1 no record found
408  * non-zero some error occurred and a message has been output.
409  *
410  * If the status is non-zero, the returned data is meaningless.
411 -*/
412 int KsmZoneNameFromId(int zone_id, char** zone_name)
413 {
414  int where = 0; /* WHERE clause value */
415  char* sql = NULL; /* SQL query */
416  DB_RESULT result; /* Handle converted to a result object */
417  DB_ROW row = NULL; /* Row data */
418  int status = 0; /* Status return */
419 
420  /* check the argument */
421  if (zone_id == -1) {
422  return MsgLog(KSM_INVARG, "NULL zone id");
423  }
424 
425  /* Construct the query */
426 
427  sql = DqsSpecifyInit("zones","id, name");
428  DqsConditionInt(&sql, "id", DQS_COMPARE_EQ, zone_id, where++);
429  DqsOrderBy(&sql, "id");
430 
431  /* Execute query and free up the query string */
432  status = DbExecuteSql(DbHandle(), sql, &result);
433  DqsFree(sql);
434 
435  if (status != 0)
436  {
437  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
438  DbFreeResult(result);
439  return status;
440  }
441 
442  /* Get the next row from the data */
443  status = DbFetchRow(result, &row);
444  if (status == 0) {
445  DbString(row, DB_ZONE_NAME, zone_name);
446  }
447  else if (status == -1) {}
448  /* No rows to return (but no DB error) */
449  else {
450  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
451  }
452 
453  DbFreeRow(row);
454  DbFreeResult(result);
455  return status;
456 }
void DbFreeResult(DB_RESULT result)
#define DB_COUNT
Definition: db_fields.h:117
char name[KSM_ZONE_NAME_LENGTH]
Definition: ksm.h:284
char signconf[KSM_PATH_LENGTH]
Definition: ksm.h:285
#define KSM_INVARG
Definition: ksmdef.h:66
int DbFetchRow(DB_RESULT result, DB_ROW *row)
#define KSM_SQLFAIL
Definition: ksmdef.h:67
char * DqsSpecifyInit(const char *table, const char *fields)
Definition: dq_string.c:117
int KsmZone(DB_RESULT result, KSM_ZONE *data)
Definition: ksm_zone.c:150
#define DB_ZONE_NAME
Definition: db_fields.h:100
int KsmZoneInit(DB_RESULT *result, int policy_id)
Definition: ksm_zone.c:66
int KsmZoneCountInit(DB_RESULT *result, int id)
Definition: ksm_zone.c:107
#define KSM_ZONE_NAME_LENGTH
Definition: ksm.h:66
void DqsOrderBy(char **query, const char *field)
Definition: dq_string.c:277
int MsgLog(int status,...)
Definition: message.c:335
void DqsFree(char *query)
Definition: dq_string.c:320
#define KSM_ADAPTER_NAME_LENGTH
Definition: ksm.h:67
#define DB_ZONE_INPUT
Definition: db_fields.h:103
void DdsFree(char *query)
Definition: dd_string.c:115
char * DqsCountInit(const char *table)
Definition: dq_string.c:90
int KsmZoneNameFromId(int zone_id, char **zone_name)
Definition: ksm_zone.c:412
DB_HANDLE DbHandle(void)
int DbString(DB_ROW row, int field_index, char **result)
void DqsConditionInt(char **query, const char *field, DQS_COMPARISON compare, int value, int index)
Definition: dq_string.c:224
void DdsConditionInt(char **query, const char *field, DQS_COMPARISON compare, int value, int index)
Definition: dd_string.c:88
int policy_id
Definition: ksm.h:283
int KsmZoneIdFromName(const char *zone_name, int *zone_id)
Definition: ksm_zone.c:247
char * DdsInit(const char *table)
Definition: dd_string.c:60
char output[KSM_PATH_LENGTH]
Definition: ksm.h:287
const char * DbErrmsg(DB_HANDLE handle)
#define KSM_PATH_LENGTH
Definition: ksm.h:59
void DbFreeRow(DB_ROW row)
char input[KSM_PATH_LENGTH]
Definition: ksm.h:286
#define DB_ZONE_POLICY_ID
Definition: db_fields.h:101
int KsmZoneCount(DB_RESULT result, int *count)
Definition: ksm_zone.c:206
int DbExecuteSql(DB_HANDLE handle, const char *stmt_str, DB_RESULT *result)
int DbStringBuffer(DB_ROW row, int field_index, char *buffer, size_t buflen)
int KsmZoneIdAndPolicyFromName(const char *zone_name, int *policy_id, int *zone_id)
Definition: ksm_zone.c:310
int KsmDeleteZone(int zone_id)
Definition: ksm_zone.c:372
#define DB_ZONE_IN_TYPE
Definition: db_fields.h:105
#define DB_ZONE_TABLE
Definition: db_fields.h:97
void DdsEnd(char **query)
Definition: dd_string.c:109
#define DB_ZONE_OUTPUT
Definition: db_fields.h:104
#define DB_ZONE_SIGNCONF
Definition: db_fields.h:102
char in_type[KSM_ADAPTER_NAME_LENGTH]
Definition: ksm.h:289
int DbInt(DB_ROW row, int field_index, int *value)
char out_type[KSM_ADAPTER_NAME_LENGTH]
Definition: ksm.h:290
#define DB_ZONE_FIELDS
Definition: db_fields.h:98
int id
Definition: ksm.h:282
#define DB_ZONE_OUT_TYPE
Definition: db_fields.h:106
Definition: ksm.h:281
int DbExecuteSqlNoResult(DB_HANDLE handle, const char *stmt_str)
void DqsConditionString(char **query, const char *field, DQS_COMPARISON compare, const char *value, int index)
Definition: dq_string.c:238
#define DB_ZONE_ID
Definition: db_fields.h:99