OpenDNSSEC-enforcer  1.3.14
ksm_zone.c
Go to the documentation of this file.
1 /*
2  * $Id: ksm_zone.c 6352 2012-05-29 08:45:11Z 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));
173  }
174  else if (status == -1) {}
175  /* No rows to return (but no error) */
176  else {
177  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
178  }
179 
180  if (row != NULL) {
181  DbFreeRow(row);
182  }
183 
184  return status;
185 }
186 /*+
187  * KsmZoneCount
188  *
189  * Arguments:
190  * DB_RESULT result
191  * Handle from KsmParameterInit
192  *
193  *
194  * Returns:
195  * int
196  * Status return:
197  * 0 success
198  * -1 end of record set reached
199  * non-zero some error occurred and a message has been output.
200  *
201  * If the status is non-zero, the returned data is meaningless.
202 -*/
203 
204 int KsmZoneCount(DB_RESULT result, int* count)
205 {
206  int status = 0; /* Return status */
207  DB_ROW row = NULL; /* Row data */
208 
209  /* Get the next row from the data */
210  status = DbFetchRow(result, &row);
211 
212  if (status == 0) {
213 
214  /* Now copy the results into the output data */
215  status = DbInt(row, DB_COUNT, count);
216 
217  }
218  else if (status == -1) {}
219  /* No rows to return (but no error) */
220  else {
221  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
222  }
223 
224  DbFreeRow(row);
225 
226  return status;
227 }
228 
229 /*+
230  * KsmZoneIdFromName
231  *
232  * Arguments:
233  * const char* zone_name name of the zone to get the id for
234  * int* zone_id returned id
235  *
236  * Returns:
237  * int
238  * Status return:
239  * 0 success
240  * -1 no record found
241  * non-zero some error occurred and a message has been output.
242  *
243  * If the status is non-zero, the returned data is meaningless.
244 -*/
245 int KsmZoneIdFromName(const char* zone_name, int* zone_id)
246 {
247  int where = 0; /* WHERE clause value */
248  char* sql = NULL; /* SQL query */
249  DB_RESULT result; /* Handle converted to a result object */
250  DB_ROW row = NULL; /* Row data */
251  int status = 0; /* Status return */
252 
253  /* check the argument */
254  if (zone_name == NULL) {
255  return MsgLog(KSM_INVARG, "NULL zone name");
256  }
257 
258  /* Construct the query */
259 
260  sql = DqsSpecifyInit("zones","id, name");
261  DqsConditionString(&sql, "NAME", DQS_COMPARE_EQ, zone_name, where++);
262  DqsOrderBy(&sql, "id");
263 
264  /* Execute query and free up the query string */
265  status = DbExecuteSql(DbHandle(), sql, &result);
266  DqsFree(sql);
267 
268  if (status != 0)
269  {
270  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
271  DbFreeResult(result);
272  return status;
273  }
274 
275  /* Get the next row from the data */
276  status = DbFetchRow(result, &row);
277  if (status == 0) {
278  DbInt(row, DB_ZONE_ID, zone_id);
279  }
280  else if (status == -1) {}
281  /* No rows to return (but no DB error) */
282  else {
283  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
284  }
285 
286  DbFreeRow(row);
287  DbFreeResult(result);
288  return status;
289 }
290 
291 /*+
292  * KsmZoneIdAndPolicyFromName
293  *
294  * Arguments:
295  * const char* zone_name name of the zone to get the id for
296  * int* policy_id returned id
297  * int* zone_id returned id
298  *
299  * Returns:
300  * int
301  * Status return:
302  * 0 success
303  * -1 no record found
304  * non-zero some error occurred and a message has been output.
305  *
306  * If the status is non-zero, the returned data is meaningless.
307 -*/
308 int KsmZoneIdAndPolicyFromName(const char* zone_name, int* policy_id, int* zone_id)
309 {
310  int where = 0; /* WHERE clause value */
311  char* sql = NULL; /* SQL query */
312  DB_RESULT result; /* Handle converted to a result object */
313  DB_ROW row = NULL; /* Row data */
314  int status = 0; /* Status return */
315 
316  /* check the argument */
317  if (zone_name == NULL) {
318  return MsgLog(KSM_INVARG, "NULL zone name");
319  }
320 
321  /* Construct the query */
322 
323  sql = DqsSpecifyInit("zones","id, name, policy_id");
324  DqsConditionString(&sql, "NAME", DQS_COMPARE_EQ, zone_name, where++);
325  DqsOrderBy(&sql, "id");
326 
327  /* Execute query and free up the query string */
328  status = DbExecuteSql(DbHandle(), sql, &result);
329  DqsFree(sql);
330 
331  if (status != 0)
332  {
333  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
334  DbFreeResult(result);
335  return status;
336  }
337 
338  /* Get the next row from the data */
339  status = DbFetchRow(result, &row);
340  if (status == 0) {
341  DbInt(row, DB_ZONE_ID, zone_id);
342  DbInt(row, DB_ZONE_POLICY_ID, policy_id);
343  }
344  else if (status == -1) {}
345  /* No rows to return (but no DB error) */
346  else {
347  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
348  }
349 
350  DbFreeRow(row);
351  DbFreeResult(result);
352  return status;
353 }
354 
355 /*+
356  * KsmDeleteZone
357  *
358  * Description:
359  * Will remove all dnsseckeys allocated to a zone before removing the entry in
360  * the zones table itself
361  *
362  * Arguments:
363  * int zone_id id of the zone to be deleted (-1 will delete all)
364  *
365  * Returns:
366  * int
367  * Status return. 0=> Success, non-zero => error.
368 -*/
369 
370 int KsmDeleteZone(int zone_id)
371 {
372  int status = 0; /* Status return */
373  char* sql = NULL; /* SQL Statement */
374 
375  /* Delete from zones */
376  sql = DdsInit("zones");
377  if (zone_id != -1) {
378  DdsConditionInt(&sql, "id", DQS_COMPARE_EQ, zone_id, 0);
379  }
380  DdsEnd(&sql);
381 
382  status = DbExecuteSqlNoResult(DbHandle(), sql);
383  DdsFree(sql);
384 
385  if (status != 0)
386  {
387  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
388  return status;
389  }
390 
391  return status;
392 }
393 
394 /*+
395  * KsmZoneNameFromId
396  *
397  * Arguments:
398  * int zone_id id of the zone to get the name for
399  * char** zone_name returned name
400  *
401  * Returns:
402  * int
403  * Status return:
404  * 0 success
405  * -1 no record found
406  * non-zero some error occurred and a message has been output.
407  *
408  * If the status is non-zero, the returned data is meaningless.
409 -*/
410 int KsmZoneNameFromId(int zone_id, char** zone_name)
411 {
412  int where = 0; /* WHERE clause value */
413  char* sql = NULL; /* SQL query */
414  DB_RESULT result; /* Handle converted to a result object */
415  DB_ROW row = NULL; /* Row data */
416  int status = 0; /* Status return */
417 
418  /* check the argument */
419  if (zone_id == -1) {
420  return MsgLog(KSM_INVARG, "NULL zone id");
421  }
422 
423  /* Construct the query */
424 
425  sql = DqsSpecifyInit("zones","id, name");
426  DqsConditionInt(&sql, "id", DQS_COMPARE_EQ, zone_id, where++);
427  DqsOrderBy(&sql, "id");
428 
429  /* Execute query and free up the query string */
430  status = DbExecuteSql(DbHandle(), sql, &result);
431  DqsFree(sql);
432 
433  if (status != 0)
434  {
435  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
436  DbFreeResult(result);
437  return status;
438  }
439 
440  /* Get the next row from the data */
441  status = DbFetchRow(result, &row);
442  if (status == 0) {
443  DbString(row, DB_ZONE_NAME, zone_name);
444  }
445  else if (status == -1) {}
446  /* No rows to return (but no DB error) */
447  else {
448  status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
449  }
450 
451  DbFreeRow(row);
452  DbFreeResult(result);
453  return status;
454 }