00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
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
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068 int KsmZoneInit(DB_RESULT* result, int policy_id)
00069 {
00070 int where = 0;
00071 char* sql = NULL;
00072 int status = 0;
00073
00074
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
00084
00085 status = DbExecuteSql(DbHandle(), sql, result);
00086
00087 DqsFree(sql);
00088
00089 return status;
00090 }
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108
00109 int KsmZoneCountInit(DB_RESULT* result, int id)
00110 {
00111 int where = 0;
00112 char* sql = NULL;
00113 int status = 0;
00114
00115
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
00124
00125 status = DbExecuteSql(DbHandle(), sql, result);
00126
00127 DqsFree(sql);
00128
00129 return status;
00130 }
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 int KsmZone(DB_RESULT result, KSM_ZONE *data)
00153 {
00154 int status = 0;
00155 DB_ROW row = NULL;
00156
00157
00158 status = DbFetchRow(result, &row);
00159
00160 if (status == 0) {
00161
00162
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
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
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204 int KsmZoneCount(DB_RESULT result, int* count)
00205 {
00206 int status = 0;
00207 DB_ROW row = NULL;
00208
00209
00210 status = DbFetchRow(result, &row);
00211
00212 if (status == 0) {
00213
00214
00215 status = DbInt(row, DB_COUNT, count);
00216
00217 }
00218 else if (status == -1) {}
00219
00220 else {
00221 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
00222 }
00223
00224 DbFreeRow(row);
00225
00226 return status;
00227 }
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245 int KsmZoneIdFromName(const char* zone_name, int* zone_id)
00246 {
00247 int where = 0;
00248 char* sql = NULL;
00249 DB_RESULT result;
00250 DB_ROW row = NULL;
00251 int status = 0;
00252
00253
00254 if (zone_name == NULL) {
00255 return MsgLog(KSM_INVARG, "NULL zone name");
00256 }
00257
00258
00259
00260 sql = DqsSpecifyInit("zones","id, name");
00261 DqsConditionString(&sql, "NAME", DQS_COMPARE_EQ, zone_name, where++);
00262 DqsOrderBy(&sql, "id");
00263
00264
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
00276 status = DbFetchRow(result, &row);
00277 if (status == 0) {
00278 DbInt(row, DB_ZONE_ID, zone_id);
00279 }
00280 else if (status == -1) {}
00281
00282 else {
00283 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
00284 }
00285
00286 DbFreeRow(row);
00287 DbFreeResult(result);
00288 return status;
00289 }
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300
00301
00302
00303
00304
00305
00306
00307
00308 int KsmZoneIdAndPolicyFromName(const char* zone_name, int* policy_id, int* zone_id)
00309 {
00310 int where = 0;
00311 char* sql = NULL;
00312 DB_RESULT result;
00313 DB_ROW row = NULL;
00314 int status = 0;
00315
00316
00317 if (zone_name == NULL) {
00318 return MsgLog(KSM_INVARG, "NULL zone name");
00319 }
00320
00321
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
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
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
00346 else {
00347 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
00348 }
00349
00350 DbFreeRow(row);
00351 DbFreeResult(result);
00352 return status;
00353 }
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370 int KsmDeleteZone(int zone_id)
00371 {
00372 int status = 0;
00373 char* sql = NULL;
00374
00375
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
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
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425 int KsmZoneNameFromId(int zone_id, char** zone_name)
00426 {
00427 int where = 0;
00428 char* sql = NULL;
00429 DB_RESULT result;
00430 DB_ROW row = NULL;
00431 int status = 0;
00432
00433
00434 if (zone_id == -1) {
00435 return MsgLog(KSM_INVARG, "NULL zone id");
00436 }
00437
00438
00439
00440 sql = DqsSpecifyInit("zones","id, name");
00441 DqsConditionInt(&sql, "id", DQS_COMPARE_EQ, zone_id, where++);
00442 DqsOrderBy(&sql, "id");
00443
00444
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
00456 status = DbFetchRow(result, &row);
00457 if (status == 0) {
00458 DbString(row, DB_ZONE_NAME, zone_name);
00459 }
00460 else if (status == -1) {}
00461
00462 else {
00463 status = MsgLog(KSM_SQLFAIL, DbErrmsg(DbHandle()));
00464 }
00465
00466 DbFreeRow(row);
00467 DbFreeResult(result);
00468 return status;
00469 }