OpenDNSSEC-enforcer 1.3.0
|
00001 /* 00002 * $Id: ksm_zone.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_zone.c - Manipulation of Zone Information 00031 */ 00032 00033 #include <assert.h> 00034 #include <stdio.h> 00035 #include <stdlib.h> 00036 #include <string.h> 00037 #include <time.h> 00038 00039 #include "ksm/database.h" 00040 #include "ksm/database_statement.h" 00041 #include "ksm/datetime.h" 00042 #include "ksm/db_fields.h" 00043 #include "ksm/debug.h" 00044 #include "ksm/ksmdef.h" 00045 #include "ksm/ksm.h" 00046 #include "ksm/ksm_internal.h" 00047 #include "ksm/message.h" 00048 #include "ksm/string_util.h" 00049 00050 /*+ 00051 * KsmZoneInit - Query for Zone Information 00052 * 00053 * 00054 * Arguments: 00055 * DB_RESULT* result 00056 * Pointer to a handle to be used for information retrieval. Will 00057 * be NULL on error. 00058 * 00059 * const char* name 00060 * Name of the parameter to retrieve information on. If NULL, information 00061 * on all parameters is retrieved. 00062 * 00063 * Returns: 00064 * int 00065 * Status return. 0 on success. 00066 -*/ 00067 00068 int KsmZoneInit(DB_RESULT* result, int policy_id) 00069 { 00070 int where = 0; /* WHERE clause value */ 00071 char* sql = NULL; /* SQL query */ 00072 int status = 0; /* Status return */ 00073 00074 /* Construct the query */ 00075 00076 sql = DqsSpecifyInit(DB_ZONE_TABLE, DB_ZONE_FIELDS); 00077 if (policy_id != -1) { 00078 DqsConditionInt(&sql, "policy_id", DQS_COMPARE_EQ, policy_id, where++); 00079 00080 } 00081 DqsOrderBy(&sql, "policy_id"); 00082 00083 /* Execute query and free up the query string */ 00084 00085 status = DbExecuteSql(DbHandle(), sql, result); 00086 00087 DqsFree(sql); 00088 00089 return status; 00090 } 00091 00092 /*+ 00093 * KsmZoneCountInit 00094 * 00095 * 00096 * Arguments: 00097 * DB_RESULT* result 00098 * Pointer to a handle to be used for information retrieval. Will 00099 * be NULL on error. 00100 * 00101 * id 00102 * id of the policy 00103 * 00104 * Returns: 00105 * int 00106 * Status return. 0 on success. 00107 -*/ 00108 00109 int KsmZoneCountInit(DB_RESULT* result, int id) 00110 { 00111 int where = 0; /* WHERE clause value */ 00112 char* sql = NULL; /* SQL query */ 00113 int status = 0; /* Status return */ 00114 00115 /* Construct the query */ 00116 00117 sql = DqsCountInit(DB_ZONE_TABLE); 00118 if (id >= 0) { 00119 DqsConditionInt(&sql, "policy_id", DQS_COMPARE_EQ, id, where++); 00120 } 00121 00122 00123 /* Execute query and free up the query string */ 00124 00125 status = DbExecuteSql(DbHandle(), sql, result); 00126 00127 DqsFree(sql); 00128 00129 return status; 00130 } 00131 00132 /*+ 00133 * KsmZone - Return Zone Information 00134 * 00135 * Arguments: 00136 * DB_RESULT result 00137 * Handle from KsmParameterInit 00138 * 00139 * KSM_PARAMETER* data 00140 * Data is returned in here. 00141 * 00142 * Returns: 00143 * int 00144 * Status return: 00145 * 0 success 00146 * -1 end of record set reached 00147 * non-zero some error occurred and a message has been output. 00148 * 00149 * If the status is non-zero, the returned data is meaningless. 00150 -*/ 00151 00152 int KsmZone(DB_RESULT result, KSM_ZONE *data) 00153 { 00154 int status = 0; /* Return status */ 00155 DB_ROW row = NULL; /* Row data */ 00156 00157 /* Get the next row from the data */ 00158 status = DbFetchRow(result, &row); 00159 00160 if (status == 0) { 00161 00162 /* Now copy the results into the output data */ 00163 DbInt(row, DB_ZONE_ID, &(data->id)); 00164 DbStringBuffer(row, DB_ZONE_NAME, data->name, 00165 KSM_ZONE_NAME_LENGTH*sizeof(char)); 00166 DbInt(row, DB_ZONE_POLICY_ID, &(data->policy_id)); 00167 DbStringBuffer(row, DB_ZONE_SIGNCONF, data->signconf, 00168 KSM_PATH_LENGTH*sizeof(char)); 00169 DbStringBuffer(row, DB_ZONE_INPUT, data->input, 00170 KSM_PATH_LENGTH*sizeof(char)); 00171 DbStringBuffer(row, DB_ZONE_OUTPUT, data->output, 00172 KSM_PATH_LENGTH*sizeof(char)); 00173 } 00174 else if (status == -1) {} 00175 /* No rows to return (but no error) */ 00176 else { 00177 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle())); 00178 } 00179 00180 if (row != NULL) { 00181 DbFreeRow(row); 00182 } 00183 00184 return status; 00185 } 00186 /*+ 00187 * KsmZoneCount 00188 * 00189 * Arguments: 00190 * DB_RESULT result 00191 * Handle from KsmParameterInit 00192 * 00193 * 00194 * Returns: 00195 * int 00196 * Status return: 00197 * 0 success 00198 * -1 end of record set reached 00199 * non-zero some error occurred and a message has been output. 00200 * 00201 * If the status is non-zero, the returned data is meaningless. 00202 -*/ 00203 00204 int KsmZoneCount(DB_RESULT result, int* count) 00205 { 00206 int status = 0; /* Return status */ 00207 DB_ROW row = NULL; /* Row data */ 00208 00209 /* Get the next row from the data */ 00210 status = DbFetchRow(result, &row); 00211 00212 if (status == 0) { 00213 00214 /* Now copy the results into the output data */ 00215 status = DbInt(row, DB_COUNT, count); 00216 00217 } 00218 else if (status == -1) {} 00219 /* No rows to return (but no error) */ 00220 else { 00221 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle())); 00222 } 00223 00224 DbFreeRow(row); 00225 00226 return status; 00227 } 00228 00229 /*+ 00230 * KsmZoneIdFromName 00231 * 00232 * Arguments: 00233 * const char* zone_name name of the zone to get the id for 00234 * int* zone_id returned id 00235 * 00236 * Returns: 00237 * int 00238 * Status return: 00239 * 0 success 00240 * -1 no record found 00241 * non-zero some error occurred and a message has been output. 00242 * 00243 * If the status is non-zero, the returned data is meaningless. 00244 -*/ 00245 int KsmZoneIdFromName(const char* zone_name, int* zone_id) 00246 { 00247 int where = 0; /* WHERE clause value */ 00248 char* sql = NULL; /* SQL query */ 00249 DB_RESULT result; /* Handle converted to a result object */ 00250 DB_ROW row = NULL; /* Row data */ 00251 int status = 0; /* Status return */ 00252 00253 /* check the argument */ 00254 if (zone_name == NULL) { 00255 return MsgLog(KSM_INVARG, "NULL zone name"); 00256 } 00257 00258 /* Construct the query */ 00259 00260 sql = DqsSpecifyInit("zones","id, name"); 00261 DqsConditionString(&sql, "NAME", DQS_COMPARE_EQ, zone_name, where++); 00262 DqsOrderBy(&sql, "id"); 00263 00264 /* Execute query and free up the query string */ 00265 status = DbExecuteSql(DbHandle(), sql, &result); 00266 DqsFree(sql); 00267 00268 if (status != 0) 00269 { 00270 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle())); 00271 DbFreeResult(result); 00272 return status; 00273 } 00274 00275 /* Get the next row from the data */ 00276 status = DbFetchRow(result, &row); 00277 if (status == 0) { 00278 DbInt(row, DB_ZONE_ID, zone_id); 00279 } 00280 else if (status == -1) {} 00281 /* No rows to return (but no DB error) */ 00282 else { 00283 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle())); 00284 } 00285 00286 DbFreeRow(row); 00287 DbFreeResult(result); 00288 return status; 00289 } 00290 00291 /*+ 00292 * KsmZoneIdAndPolicyFromName 00293 * 00294 * Arguments: 00295 * const char* zone_name name of the zone to get the id for 00296 * int* policy_id returned id 00297 * int* zone_id returned id 00298 * 00299 * Returns: 00300 * int 00301 * Status return: 00302 * 0 success 00303 * -1 no record found 00304 * non-zero some error occurred and a message has been output. 00305 * 00306 * If the status is non-zero, the returned data is meaningless. 00307 -*/ 00308 int KsmZoneIdAndPolicyFromName(const char* zone_name, int* policy_id, int* zone_id) 00309 { 00310 int where = 0; /* WHERE clause value */ 00311 char* sql = NULL; /* SQL query */ 00312 DB_RESULT result; /* Handle converted to a result object */ 00313 DB_ROW row = NULL; /* Row data */ 00314 int status = 0; /* Status return */ 00315 00316 /* check the argument */ 00317 if (zone_name == NULL) { 00318 return MsgLog(KSM_INVARG, "NULL zone name"); 00319 } 00320 00321 /* Construct the query */ 00322 00323 sql = DqsSpecifyInit("zones","id, name, policy_id"); 00324 DqsConditionString(&sql, "NAME", DQS_COMPARE_EQ, zone_name, where++); 00325 DqsOrderBy(&sql, "id"); 00326 00327 /* Execute query and free up the query string */ 00328 status = DbExecuteSql(DbHandle(), sql, &result); 00329 DqsFree(sql); 00330 00331 if (status != 0) 00332 { 00333 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle())); 00334 DbFreeResult(result); 00335 return status; 00336 } 00337 00338 /* Get the next row from the data */ 00339 status = DbFetchRow(result, &row); 00340 if (status == 0) { 00341 DbInt(row, DB_ZONE_ID, zone_id); 00342 DbInt(row, DB_ZONE_POLICY_ID, policy_id); 00343 } 00344 else if (status == -1) {} 00345 /* No rows to return (but no DB error) */ 00346 else { 00347 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle())); 00348 } 00349 00350 DbFreeRow(row); 00351 DbFreeResult(result); 00352 return status; 00353 } 00354 00355 /*+ 00356 * KsmDeleteZone 00357 * 00358 * Description: 00359 * Will remove all dnsseckeys allocated to a zone before removing the entry in 00360 * the zones table itself 00361 * 00362 * Arguments: 00363 * int zone_id id of the zone to be deleted (-1 will delete all) 00364 * 00365 * Returns: 00366 * int 00367 * Status return. 0=> Success, non-zero => error. 00368 -*/ 00369 00370 int KsmDeleteZone(int zone_id) 00371 { 00372 int status = 0; /* Status return */ 00373 char* sql = NULL; /* SQL Statement */ 00374 00375 /* Delete from dnsseckeys */ 00376 sql = DdsInit("dnsseckeys"); 00377 if (zone_id != -1) { 00378 DdsConditionInt(&sql, "zone_id", DQS_COMPARE_EQ, zone_id, 0); 00379 } 00380 DdsEnd(&sql); 00381 00382 status = DbExecuteSqlNoResult(DbHandle(), sql); 00383 DdsFree(sql); 00384 if (status != 0) 00385 { 00386 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle())); 00387 return status; 00388 } 00389 00390 /* Delete from zones */ 00391 sql = DdsInit("zones"); 00392 if (zone_id != -1) { 00393 DdsConditionInt(&sql, "id", DQS_COMPARE_EQ, zone_id, 0); 00394 } 00395 DdsEnd(&sql); 00396 00397 status = DbExecuteSqlNoResult(DbHandle(), sql); 00398 DdsFree(sql); 00399 00400 if (status != 0) 00401 { 00402 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle())); 00403 return status; 00404 } 00405 00406 return status; 00407 } 00408 00409 /*+ 00410 * KsmZoneNameFromId 00411 * 00412 * Arguments: 00413 * int zone_id id of the zone to get the name for 00414 * char** zone_name returned name 00415 * 00416 * Returns: 00417 * int 00418 * Status return: 00419 * 0 success 00420 * -1 no record found 00421 * non-zero some error occurred and a message has been output. 00422 * 00423 * If the status is non-zero, the returned data is meaningless. 00424 -*/ 00425 int KsmZoneNameFromId(int zone_id, char** zone_name) 00426 { 00427 int where = 0; /* WHERE clause value */ 00428 char* sql = NULL; /* SQL query */ 00429 DB_RESULT result; /* Handle converted to a result object */ 00430 DB_ROW row = NULL; /* Row data */ 00431 int status = 0; /* Status return */ 00432 00433 /* check the argument */ 00434 if (zone_id == -1) { 00435 return MsgLog(KSM_INVARG, "NULL zone id"); 00436 } 00437 00438 /* Construct the query */ 00439 00440 sql = DqsSpecifyInit("zones","id, name"); 00441 DqsConditionInt(&sql, "id", DQS_COMPARE_EQ, zone_id, where++); 00442 DqsOrderBy(&sql, "id"); 00443 00444 /* Execute query and free up the query string */ 00445 status = DbExecuteSql(DbHandle(), sql, &result); 00446 DqsFree(sql); 00447 00448 if (status != 0) 00449 { 00450 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle())); 00451 DbFreeResult(result); 00452 return status; 00453 } 00454 00455 /* Get the next row from the data */ 00456 status = DbFetchRow(result, &row); 00457 if (status == 0) { 00458 DbString(row, DB_ZONE_NAME, zone_name); 00459 } 00460 else if (status == -1) {} 00461 /* No rows to return (but no DB error) */ 00462 else { 00463 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle())); 00464 } 00465 00466 DbFreeRow(row); 00467 DbFreeResult(result); 00468 return status; 00469 }