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