OpenDNSSEC-enforcer  1.4.5
ksm_key_delete.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_key_delete - Deletion of keys
29  *
30  * Description:
31  * Holds the functions needed to delete information from the KEYDATA
32  * table.
33 -*/
34 
35 #include <assert.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <time.h>
40 
41 #include "ksm/database.h"
42 #include "ksm/database_statement.h"
43 #include "ksm/kmedef.h"
44 #include "ksm/ksm.h"
45 
46 
47 /*+
48  * KsmDeleteKeyRange - Delete Range of Keys
49  *
50  * Description:
51  * Deletes keys whose ID (the primary key of the table) lies between the
52  * given arguments.
53  *
54  * Arguments:
55  * int minid
56  * Minimum ID of the set of keys to be deleted.
57  *
58  * int maxid
59  * Maximum ID of the keys to be deleted. This can be equal to the
60  * minid.
61  *
62  * Note, if minid > maxid, the values are silently swapped.
63  *
64  * Returns:
65  * int
66  * 0 Success
67  * <>0 Error. A message will have been output.
68 -*/
69 
70 int KsmDeleteKeyRange(int minid, int maxid)
71 {
72  char* sql = NULL; /* Constructed SQL deletion string */
73  int status; /* Status return */
74  int temp; /* For swapping inetegers */
75  int where = 0; /* For constructing the delete statement */
76 
77  /* Ensure minimum and maximum are in the correct order */
78 
79  if (minid > maxid) {
80  temp = minid;
81  minid = maxid;
82  maxid = temp;
83  }
84 
85  /*
86  * Create the deletion string. Although we could have one code path, the
87  * check for the minimum and maximum IDs the same lease to the (possible
88  * more efficient) single condition check.
89  *
90  * Don't rely on cascading delete, so we need to go through this twice
91  */
92 
93  /* First delete from dnsseckeys */
94  sql = DdsInit("dnsseckeys");
95  if (minid == maxid) {
96  DdsConditionInt(&sql, "keypair_id", DQS_COMPARE_EQ, minid, where++);
97  }
98  else {
99  DdsConditionInt(&sql, "keypair_id", DQS_COMPARE_GE, minid, where++);
100  DdsConditionInt(&sql, "keypair_id", DQS_COMPARE_LE, maxid, where++);
101  }
102  DdsEnd(&sql);
103 
104  status = DbExecuteSqlNoResult(DbHandle(), sql);
105  DdsFree(sql);
106 
107  /* Then delete from keypairs */
108  where = 0;
109  sql = DdsInit("keypairs");
110  if (minid == maxid) {
111  DdsConditionInt(&sql, "id", DQS_COMPARE_EQ, minid, where++);
112  }
113  else {
114  DdsConditionInt(&sql, "id", DQS_COMPARE_GE, minid, where++);
115  DdsConditionInt(&sql, "id", DQS_COMPARE_LE, maxid, where++);
116  }
117  DdsEnd(&sql);
118 
119  status = DbExecuteSqlNoResult(DbHandle(), sql);
120  DdsFree(sql);
121 
122  return status;
123 }
124 
125 
126 /*+
127  * KsmDeleteKeyRanges - Delete Ranges of Keys
128  *
129  * Description:
130  * Deletes a number of ranges of keys.
131  *
132  * A range of keys is set by two numbers, the ID of the lowest key in the
133  * range, and the ID of the highest key. This function allows the
134  * specification of multiple ranges.
135  *
136  * Arguments:
137  * int limit[]
138  * Array of ranges. Each range is set by two consecurity elements in
139  * the array, i.e. elements 0 and 1 are one range, 2 and 3 another.
140  *
141  * int size
142  * Size of the array. This must be even.
143  *
144  * Returns:
145  * int
146  * 0 Success
147  * <>0 Error. A message will have been output. In this case,
148  * not all of the ranges may have been deleted.
149 -*/
150 
151 int KsmDeleteKeyRanges(int limit[], int size)
152 {
153  int i; /* Loop counter */
154  int status = 0; /* Status return */
155 
156  assert((size % 2) == 0);
157 
158  for (i = 0; ((i < size) && (status == 0)); i+= 2) {
159  status = KsmDeleteKeyRange(limit[i], limit[i + 1]);
160  }
161 
162  return status;
163 }