OpenDNSSEC-enforcer  1.4.3
ksm_zone.c
Go to the documentation of this file.
1 /*
2  * $Id: ksm_zone.c 6394 2012-06-08 09:51:50Z 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_zone.c - Manipulation of Zone 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  * KsmZoneInit - Query for Zone 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  * const char* name
60  * Name of the parameter to retrieve information on. If NULL, information
61  * on all parameters is retrieved.
62  *
63  * Returns:
64  * int
65  * Status return. 0 on success.
66 -*/
67 
68 int KsmZoneInit(DB_RESULT* result, int policy_id)
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 
77  if (policy_id != -1) {
78  DqsConditionInt(&sql, "policy_id", DQS_COMPARE_EQ, policy_id, where++);
79 
80  }
81  DqsOrderBy(&sql, "policy_id");
82 
83  /* Execute query and free up the query string */
84 
85  status = DbExecuteSql(DbHandle(), sql, result);
86 
87  DqsFree(sql);
88 
89  return status;
90 }
91 
92 /*+
93  * KsmZoneCountInit
94  *
95  *
96  * Arguments:
97  * DB_RESULT* result
98  * Pointer to a handle to be used for information retrieval. Will
99  * be NULL on error.
100  *
101  * id
102  * id of the policy
103  *
104  * Returns:
105  * int
106  * Status return. 0 on success.
107 -*/
108 
109 int KsmZoneCountInit(DB_RESULT* result, int id)
110 {
111  int where = 0; /* WHERE clause value */
112  char* sql = NULL; /* SQL query */
113  int status = 0; /* Status return */
114 
115  /* Construct the query */
116 
118  if (id >= 0) {
119  DqsConditionInt(&sql, "policy_id", DQS_COMPARE_EQ, id, where++);
120  }
121 
122 
123  /* Execute query and free up the query string */
124 
125  status = DbExecuteSql(DbHandle(), sql, result);
126 
127  DqsFree(sql);
128 
129  return status;
130 }
131 
132 /*+
133  * KsmZone - Return Zone Information
134  *
135  * Arguments:
136  * DB_RESULT result
137  * Handle from KsmParameterInit
138  *
139  * KSM_PARAMETER* data
140  * Data is returned in here.
141  *
142  * Returns:
143  * int
144  * Status return:
145  * 0 success
146  * -1 end of record set reached
147  * non-zero some error occurred and a message has been output.
148  *
149  * If the status is non-zero, the returned data is meaningless.
150 -*/
151 
152 int KsmZone(DB_RESULT result, KSM_ZONE *data)
153 {
154  int status = 0; /* Return status */
155  DB_ROW row = NULL; /* Row data */
156 
157  /* Get the next row from the data */
158  status = DbFetchRow(result, &row);
159 
160  if (status == 0) {
161 
162  /* Now copy the results into the output data */
163  DbInt(row, DB_ZONE_ID, &(data->id));
164  DbStringBuffer(row, DB_ZONE_NAME, data->name,
165  KSM_ZONE_NAME_LENGTH*sizeof(char));
166  DbInt(row, DB_ZONE_POLICY_ID, &(data->policy_id));
168  KSM_PATH_LENGTH*sizeof(char));
169  DbStringBuffer(row, DB_ZONE_INPUT, data->input,
170  KSM_PATH_LENGTH*sizeof(char));
172  KSM_PATH_LENGTH*sizeof(char));
174  KSM_ADAPTER_NAME_LENGTH*sizeof(char));
176  KSM_ADAPTER_NAME_LENGTH*sizeof(char));
177  }
178  else if (status == -1) {}
179  /* No rows to return (but no error) */
180  else {
181  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
182  }
183 
184  if (row != NULL) {
185  DbFreeRow(row);
186  }
187 
188  return status;
189 }
190 /*+
191  * KsmZoneCount
192  *
193  * Arguments:
194  * DB_RESULT result
195  * Handle from KsmParameterInit
196  *
197  *
198  * Returns:
199  * int
200  * Status return:
201  * 0 success
202  * -1 end of record set reached
203  * non-zero some error occurred and a message has been output.
204  *
205  * If the status is non-zero, the returned data is meaningless.
206 -*/
207 
208 int KsmZoneCount(DB_RESULT result, int* count)
209 {
210  int status = 0; /* Return status */
211  DB_ROW row = NULL; /* Row data */
212 
213  /* Get the next row from the data */
214  status = DbFetchRow(result, &row);
215 
216  if (status == 0) {
217 
218  /* Now copy the results into the output data */
219  status = DbInt(row, DB_COUNT, count);
220 
221  }
222  else if (status == -1) {}
223  /* No rows to return (but no error) */
224  else {
225  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
226  }
227 
228  DbFreeRow(row);
229 
230  return status;
231 }
232 
233 /*+
234  * KsmZoneIdFromName
235  *
236  * Arguments:
237  * const char* zone_name name of the zone to get the id for
238  * int* zone_id returned id
239  *
240  * Returns:
241  * int
242  * Status return:
243  * 0 success
244  * -1 no record found
245  * non-zero some error occurred and a message has been output.
246  *
247  * If the status is non-zero, the returned data is meaningless.
248 -*/
249 int KsmZoneIdFromName(const char* zone_name, int* zone_id)
250 {
251  int where = 0; /* WHERE clause value */
252  char* sql = NULL; /* SQL query */
253  DB_RESULT result; /* Handle converted to a result object */
254  DB_ROW row = NULL; /* Row data */
255  int status = 0; /* Status return */
256 
257  /* check the argument */
258  if (zone_name == NULL) {
259  return MsgLog(KSM_INVARG, "NULL zone name");
260  }
261 
262  /* Construct the query */
263 
264  sql = DqsSpecifyInit("zones","id, name");
265  DqsConditionString(&sql, "NAME", DQS_COMPARE_EQ, zone_name, where++);
266  DqsOrderBy(&sql, "id");
267 
268  /* Execute query and free up the query string */
269  status = DbExecuteSql(DbHandle(), sql, &result);
270  DqsFree(sql);
271 
272  if (status != 0)
273  {
274  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
275  DbFreeResult(result);
276  return status;
277  }
278 
279  /* Get the next row from the data */
280  status = DbFetchRow(result, &row);
281  if (status == 0) {
282  DbInt(row, DB_ZONE_ID, zone_id);
283  }
284  else if (status == -1) {}
285  /* No rows to return (but no DB error) */
286  else {
287  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
288  }
289 
290  DbFreeRow(row);
291  DbFreeResult(result);
292  return status;
293 }
294 
295 /*+
296  * KsmZoneIdAndPolicyFromName
297  *
298  * Arguments:
299  * const char* zone_name name of the zone to get the id for
300  * int* policy_id returned id
301  * int* zone_id returned id
302  *
303  * Returns:
304  * int
305  * Status return:
306  * 0 success
307  * -1 no record found
308  * non-zero some error occurred and a message has been output.
309  *
310  * If the status is non-zero, the returned data is meaningless.
311 -*/
312 int KsmZoneIdAndPolicyFromName(const char* zone_name, int* policy_id, int* zone_id)
313 {
314  int where = 0; /* WHERE clause value */
315  char* sql = NULL; /* SQL query */
316  DB_RESULT result; /* Handle converted to a result object */
317  DB_ROW row = NULL; /* Row data */
318  int status = 0; /* Status return */
319 
320  /* check the argument */
321  if (zone_name == NULL) {
322  return MsgLog(KSM_INVARG, "NULL zone name");
323  }
324 
325  /* Construct the query */
326 
327  sql = DqsSpecifyInit("zones","id, name, policy_id");
328  DqsConditionString(&sql, "NAME", DQS_COMPARE_EQ, zone_name, where++);
329  DqsOrderBy(&sql, "id");
330 
331  /* Execute query and free up the query string */
332  status = DbExecuteSql(DbHandle(), sql, &result);
333  DqsFree(sql);
334 
335  if (status != 0)
336  {
337  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
338  DbFreeResult(result);
339  return status;
340  }
341 
342  /* Get the next row from the data */
343  status = DbFetchRow(result, &row);
344  if (status == 0) {
345  DbInt(row, DB_ZONE_ID, zone_id);
346  DbInt(row, DB_ZONE_POLICY_ID, policy_id);
347  }
348  else if (status == -1) {}
349  /* No rows to return (but no DB error) */
350  else {
351  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
352  }
353 
354  DbFreeRow(row);
355  DbFreeResult(result);
356  return status;
357 }
358 
359 /*+
360  * KsmDeleteZone
361  *
362  * Description:
363  * Will remove all dnsseckeys allocated to a zone before removing the entry in
364  * the zones table itself
365  *
366  * Arguments:
367  * int zone_id id of the zone to be deleted (-1 will delete all)
368  *
369  * Returns:
370  * int
371  * Status return. 0=> Success, non-zero => error.
372 -*/
373 
374 int KsmDeleteZone(int zone_id)
375 {
376  int status = 0; /* Status return */
377  char* sql = NULL; /* SQL Statement */
378 
379  /* Delete from zones */
380  sql = DdsInit("zones");
381  if (zone_id != -1) {
382  DdsConditionInt(&sql, "id", DQS_COMPARE_EQ, zone_id, 0);
383  }
384  DdsEnd(&sql);
385 
386  status = DbExecuteSqlNoResult(DbHandle(), sql);
387  DdsFree(sql);
388 
389  if (status != 0)
390  {
391  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
392  return status;
393  }
394 
395  return status;
396 }
397 
398 /*+
399  * KsmZoneNameFromId
400  *
401  * Arguments:
402  * int zone_id id of the zone to get the name for
403  * char** zone_name returned name
404  *
405  * Returns:
406  * int
407  * Status return:
408  * 0 success
409  * -1 no record found
410  * non-zero some error occurred and a message has been output.
411  *
412  * If the status is non-zero, the returned data is meaningless.
413 -*/
414 int KsmZoneNameFromId(int zone_id, char** zone_name)
415 {
416  int where = 0; /* WHERE clause value */
417  char* sql = NULL; /* SQL query */
418  DB_RESULT result; /* Handle converted to a result object */
419  DB_ROW row = NULL; /* Row data */
420  int status = 0; /* Status return */
421 
422  /* check the argument */
423  if (zone_id == -1) {
424  return MsgLog(KSM_INVARG, "NULL zone id");
425  }
426 
427  /* Construct the query */
428 
429  sql = DqsSpecifyInit("zones","id, name");
430  DqsConditionInt(&sql, "id", DQS_COMPARE_EQ, zone_id, where++);
431  DqsOrderBy(&sql, "id");
432 
433  /* Execute query and free up the query string */
434  status = DbExecuteSql(DbHandle(), sql, &result);
435  DqsFree(sql);
436 
437  if (status != 0)
438  {
439  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
440  DbFreeResult(result);
441  return status;
442  }
443 
444  /* Get the next row from the data */
445  status = DbFetchRow(result, &row);
446  if (status == 0) {
447  DbString(row, DB_ZONE_NAME, zone_name);
448  }
449  else if (status == -1) {}
450  /* No rows to return (but no DB error) */
451  else {
452  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
453  }
454 
455  DbFreeRow(row);
456  DbFreeResult(result);
457  return status;
458 }
void DbFreeResult(DB_RESULT result)
#define DB_COUNT
Definition: db_fields.h:119
char name[KSM_ZONE_NAME_LENGTH]
Definition: ksm.h:286
char signconf[KSM_PATH_LENGTH]
Definition: ksm.h:287
#define KSM_INVARG
Definition: ksmdef.h:68
int DbFetchRow(DB_RESULT result, DB_ROW *row)
#define KSM_SQLFAIL
Definition: ksmdef.h:69
char * DqsSpecifyInit(const char *table, const char *fields)
Definition: dq_string.c:119
int KsmZone(DB_RESULT result, KSM_ZONE *data)
Definition: ksm_zone.c:152
#define DB_ZONE_NAME
Definition: db_fields.h:102
int KsmZoneInit(DB_RESULT *result, int policy_id)
Definition: ksm_zone.c:68
int KsmZoneCountInit(DB_RESULT *result, int id)
Definition: ksm_zone.c:109
#define KSM_ZONE_NAME_LENGTH
Definition: ksm.h:68
void DqsOrderBy(char **query, const char *field)
Definition: dq_string.c:279
int MsgLog(int status,...)
Definition: message.c:337
void DqsFree(char *query)
Definition: dq_string.c:322
#define KSM_ADAPTER_NAME_LENGTH
Definition: ksm.h:69
#define DB_ZONE_INPUT
Definition: db_fields.h:105
void DdsFree(char *query)
Definition: dd_string.c:117
char * DqsCountInit(const char *table)
Definition: dq_string.c:92
int KsmZoneNameFromId(int zone_id, char **zone_name)
Definition: ksm_zone.c:414
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:226
void DdsConditionInt(char **query, const char *field, DQS_COMPARISON compare, int value, int index)
Definition: dd_string.c:90
int policy_id
Definition: ksm.h:285
int KsmZoneIdFromName(const char *zone_name, int *zone_id)
Definition: ksm_zone.c:249
char * DdsInit(const char *table)
Definition: dd_string.c:62
char output[KSM_PATH_LENGTH]
Definition: ksm.h:289
const char * DbErrmsg(DB_HANDLE handle)
#define KSM_PATH_LENGTH
Definition: ksm.h:61
void DbFreeRow(DB_ROW row)
char input[KSM_PATH_LENGTH]
Definition: ksm.h:288
#define DB_ZONE_POLICY_ID
Definition: db_fields.h:103
int KsmZoneCount(DB_RESULT result, int *count)
Definition: ksm_zone.c:208
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:312
int KsmDeleteZone(int zone_id)
Definition: ksm_zone.c:374
#define DB_ZONE_IN_TYPE
Definition: db_fields.h:107
#define DB_ZONE_TABLE
Definition: db_fields.h:99
void DdsEnd(char **query)
Definition: dd_string.c:111
#define DB_ZONE_OUTPUT
Definition: db_fields.h:106
#define DB_ZONE_SIGNCONF
Definition: db_fields.h:104
char in_type[KSM_ADAPTER_NAME_LENGTH]
Definition: ksm.h:291
int DbInt(DB_ROW row, int field_index, int *value)
char out_type[KSM_ADAPTER_NAME_LENGTH]
Definition: ksm.h:292
#define DB_ZONE_FIELDS
Definition: db_fields.h:100
int id
Definition: ksm.h:284
#define DB_ZONE_OUT_TYPE
Definition: db_fields.h:108
Definition: ksm.h:283
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:240
#define DB_ZONE_ID
Definition: db_fields.h:101