OpenDNSSEC-enforcer 1.3.0
/build/buildd/opendnssec-1.3.0/enforcer/ksm/ksm_key_delete.c
Go to the documentation of this file.
00001 /*
00002  * $Id: ksm_key_delete.c 5320 2011-07-12 10:42:26Z jakob $
00003  *
00004  * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
00005  *
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  * 1. Redistributions of source code must retain the above copyright
00010  *    notice, this list of conditions and the following disclaimer.
00011  * 2. Redistributions in binary form must reproduce the above copyright
00012  *    notice, this list of conditions and the following disclaimer in the
00013  *    documentation and/or other materials provided with the distribution.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
00016  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
00017  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00018  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
00019  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00020  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
00021  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
00023  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
00024  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
00025  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026  *
00027  */
00028 
00029 /*+
00030  * ksm_key_delete - Deletion of keys
00031  *
00032  * Description:
00033  *      Holds the functions needed to delete information from the KEYDATA
00034  *      table.
00035 -*/
00036 
00037 #include <assert.h>
00038 #include <stdio.h>
00039 #include <stdlib.h>
00040 #include <string.h>
00041 #include <time.h>
00042 
00043 #include "ksm/database.h"
00044 #include "ksm/database_statement.h"
00045 #include "ksm/kmedef.h"
00046 #include "ksm/ksm.h"
00047 
00048 
00049 /*+
00050  * KsmDeleteKeyRange - Delete Range of Keys
00051  *
00052  * Description:
00053  *      Deletes keys whose ID (the primary key of the table) lies between the
00054  *      given arguments.
00055  *
00056  * Arguments:
00057  *      int minid
00058  *          Minimum ID of the set of keys to be deleted.
00059  *
00060  *      int maxid
00061  *          Maximum ID of the keys to be deleted.  This can be equal to the
00062  *          minid.
00063  *
00064  *      Note, if minid > maxid, the values are silently swapped.
00065  *
00066  * Returns:
00067  *      int
00068  *          0       Success
00069  *          <>0     Error.  A message will have been output.
00070 -*/
00071 
00072 int KsmDeleteKeyRange(int minid, int maxid)
00073 {
00074     char*   sql = NULL;     /* Constructed SQL deletion string */
00075     int     status;         /* Status return */
00076     int     temp;           /* For swapping inetegers */
00077     int     where = 0;      /* For constructing the delete statement */
00078 
00079     /* Ensure minimum and maximum are in the correct order */
00080 
00081     if (minid > maxid) {
00082         temp = minid;
00083         minid = maxid;
00084         maxid = temp;
00085     }
00086 
00087     /*
00088      * Create the deletion string.  Although we could have one code path, the
00089      * check for the minimum and maximum IDs the same lease to the (possible
00090      * more efficient) single condition check.
00091      *
00092      * Don't rely on cascading delete, so we need to go through this twice
00093      */
00094 
00095     /* First delete from dnsseckeys */
00096     sql = DdsInit("dnsseckeys");
00097     if (minid == maxid) {
00098         DdsConditionInt(&sql, "keypair_id", DQS_COMPARE_EQ, minid, where++);
00099     }
00100     else {
00101         DdsConditionInt(&sql, "keypair_id", DQS_COMPARE_GE, minid, where++);
00102         DdsConditionInt(&sql, "keypair_id", DQS_COMPARE_LE, maxid, where++);
00103     }
00104     DdsEnd(&sql);
00105 
00106     status = DbExecuteSqlNoResult(DbHandle(), sql);
00107     DdsFree(sql);
00108 
00109     /* Then delete from keypairs */
00110     where = 0;
00111     sql = DdsInit("keypairs");
00112     if (minid == maxid) {
00113         DdsConditionInt(&sql, "id", DQS_COMPARE_EQ, minid, where++);
00114     }
00115     else {
00116         DdsConditionInt(&sql, "id", DQS_COMPARE_GE, minid, where++);
00117         DdsConditionInt(&sql, "id", DQS_COMPARE_LE, maxid, where++);
00118     }
00119     DdsEnd(&sql);
00120 
00121     status = DbExecuteSqlNoResult(DbHandle(), sql);
00122     DdsFree(sql);
00123 
00124     return status;
00125 }
00126 
00127 
00128 /*+
00129  * KsmDeleteKeyRanges - Delete Ranges of Keys
00130  *
00131  * Description:
00132  *      Deletes a number of ranges of keys.
00133  *
00134  *      A range of keys is set by two numbers, the ID of the lowest key in the
00135  *      range, and the ID of the highest key.  This function allows the
00136  *      specification of multiple ranges.
00137  *
00138  * Arguments:
00139  *      int limit[]
00140  *          Array of ranges.  Each range is set by two consecurity elements in
00141  *          the array, i.e. elements 0 and 1 are one range, 2 and 3 another.
00142  *
00143  *      int size
00144  *          Size of the array.  This must be even.
00145  *
00146  * Returns:
00147  *      int
00148  *          0       Success
00149  *          <>0     Error.  A message will have been output.  In this case,
00150  *                  not all of the ranges may have been deleted.
00151 -*/
00152 
00153 int KsmDeleteKeyRanges(int limit[], int size)
00154 {
00155     int     i;              /* Loop counter */
00156     int     status = 0;     /* Status return */
00157 
00158     assert((size % 2) == 0);
00159 
00160     for (i = 0; ((i < size) && (status == 0)); i+= 2) {
00161         status = KsmDeleteKeyRange(limit[i], limit[i + 1]);
00162     }
00163 
00164     return status;
00165 }