OpenDNSSEC-enforcer  1.4.7
ksm_list.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_list.c - List various aspects of the current configuration
29  */
30 
31 #include <assert.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <time.h>
36 
37 #include "ksm/database.h"
38 #include "ksm/database_statement.h"
39 #include "ksm/datetime.h"
40 #include "ksm/db_fields.h"
41 #include "ksm/debug.h"
42 #include "ksm/ksmdef.h"
43 #include "ksm/ksm.h"
44 #include "ksm/ksm_internal.h"
45 #include "ksm/message.h"
46 #include "ksm/string_util.h"
47 #include "ksm/string_util2.h"
48 
49 /*+
50  * KsmListBackups - Output a list of all backups perfomed
51  *
52  *
53  * Arguments:
54  *
55  * int repo_id
56  * ID of the repository (-1 for all)
57  *
58  * Returns:
59  * int
60  * Status return. 0 on success.
61  * other on fail
62  */
63 
64 int KsmListBackups(int repo_id, int verbose_flag)
65 {
66  char* sql = NULL; /* SQL query */
67  char* sql2 = NULL; /* SQL query */
68  char* sql3 = NULL; /* SQL query */
69  int status = 0; /* Status return */
70  char stringval[KSM_INT_STR_SIZE]; /* For Integer to String conversion */
71  DB_RESULT result; /* Result of the query */
72  DB_ROW row = NULL; /* Row data */
73  DB_RESULT result2; /* Result of the query */
74  DB_ROW row2 = NULL; /* Row data */
75  DB_RESULT result3; /* Result of the query */
76  DB_ROW row3 = NULL; /* Row data */
77 
78  char* temp_date = NULL; /* place to store date returned */
79  char* temp_pre_date = NULL; /* place to store pre-backup date returned */
80  char* temp_repo = NULL; /* place to store repository returned */
81  int temp_backup_req = 0; /* place to store backuprequired returned */
82 
83  /* Select rows */
84  StrAppend(&sql, "select distinct k.backup, s.name, k.pre_backup from keypairs k, securitymodules s ");
85  StrAppend(&sql, "where s.id = k.securitymodule_id ");
86  if (repo_id != -1) {
87  StrAppend(&sql, "and s.id = ");
88  snprintf(stringval, KSM_INT_STR_SIZE, "%d", repo_id);
89  StrAppend(&sql, stringval);
90  }
91  StrAppend(&sql, " order by backup");
92 
93  DusEnd(&sql);
94 
95  status = DbExecuteSql(DbHandle(), sql, &result);
96 
97  if (status == 0) {
98  status = DbFetchRow(result, &row);
99  if (verbose_flag == 1) {
100  printf("Pre Backup Date: Backup Date: Repository:\n");
101  } else {
102  printf("Date: Repository:\n");
103  }
104  while (status == 0) {
105  /* Got a row, print it */
106  DbString(row, 0, &temp_date);
107  DbString(row, 1, &temp_repo);
108  DbString(row, 2, &temp_pre_date);
109 
110  if (verbose_flag == 1) {
111  if (temp_date != NULL || temp_pre_date != NULL) { /* Ignore non-backup */
112  printf("%-24s %-24s %s\n", temp_pre_date, temp_date, temp_repo);
113  }
114  } else {
115  if (temp_date != NULL) { /* Ignore non-backup */
116  printf("%-24s %s\n", temp_date, temp_repo);
117  }
118  }
119 
120  status = DbFetchRow(result, &row);
121  }
122 
123  /* Convert EOF status to success */
124 
125  if (status == -1) {
126  status = 0;
127  }
128 
129  DbFreeResult(result);
130  }
131 
132  DusFree(sql);
133  DbFreeRow(row);
134  DbStringFree(temp_date);
135  DbStringFree(temp_pre_date);
136  sql = NULL;
137  row = NULL;
138  temp_date = NULL;
139 
140  /* List repos which need a backup */
141  StrAppend(&sql2, "select s.name, s.requirebackup from keypairs k, securitymodules s ");
142  StrAppend(&sql2, "where s.id = k.securitymodule_id ");
143  if (repo_id != -1) {
144  StrAppend(&sql2, "and s.id = ");
145  snprintf(stringval, KSM_INT_STR_SIZE, "%d", repo_id);
146  StrAppend(&sql2, stringval);
147  }
148  StrAppend(&sql2, " and k.backup is null");
149  StrAppend(&sql2, " group by s.name order by s.name");
150 
151  DusEnd(&sql2);
152 
153  status = DbExecuteSql(DbHandle(), sql2, &result2);
154 
155  if (status == 0) {
156  status = DbFetchRow(result2, &row2);
157  while (status == 0) {
158  /* Got a row, print it */
159  DbString(row2, 0, &temp_repo);
160  DbInt(row2, 1, &temp_backup_req);
161 
162  if (temp_backup_req == 0) {
163  printf("Repository %s has unbacked up keys (that can be used)\n", temp_repo);
164  } else {
165  printf("Repository %s has unbacked up keys (that will not be used)\n", temp_repo);
166  }
167 
168  status = DbFetchRow(result2, &row2);
169  }
170 
171  /* Convert EOF status to success */
172 
173  if (status == -1) {
174  status = 0;
175  }
176 
177  DbFreeResult(result2);
178  }
179 
180  DusFree(sql2);
181  DbFreeRow(row2);
182  DbStringFree(temp_repo);
183 
184  /* List repos which need a backup commit */
185  temp_repo = NULL;
186  StrAppend(&sql3, "select s.name from keypairs k, securitymodules s ");
187  StrAppend(&sql3, "where s.id = k.securitymodule_id ");
188  if (repo_id != -1) {
189  StrAppend(&sql3, "and s.id = ");
190  snprintf(stringval, KSM_INT_STR_SIZE, "%d", repo_id);
191  StrAppend(&sql3, stringval);
192  }
193  StrAppend(&sql3, " and k.backup is null");
194  StrAppend(&sql3, " and k.pre_backup is not null");
195  StrAppend(&sql3, " group by s.name order by s.name");
196 
197  DusEnd(&sql3);
198 
199  status = DbExecuteSql(DbHandle(), sql3, &result3);
200 
201  if (status == 0) {
202  status = DbFetchRow(result3, &row3);
203  while (status == 0) {
204  /* Got a row, print it */
205  DbString(row3, 0, &temp_repo);
206 
207  printf("Repository %s has keys prepared for back up which have not been committed\n", temp_repo);
208 
209  status = DbFetchRow(result3, &row3);
210  }
211 
212  /* Convert EOF status to success */
213 
214  if (status == -1) {
215  status = 0;
216  }
217 
218  DbFreeResult(result3);
219  }
220 
221  DusFree(sql3);
222  DbFreeRow(row3);
223  DbStringFree(temp_repo);
224 
225  return status;
226 }
227 
228 /*+
229  * KsmListRepos - Output a list of all repositories available
230  *
231  *
232  * Arguments:
233  *
234  * none
235  *
236  * Returns:
237  * int
238  * Status return. 0 on success.
239  * other on fail
240  */
241 
243 {
244  char* sql = NULL; /* SQL query */
245  int status = 0; /* Status return */
246  DB_RESULT result; /* Result of the query */
247  DB_ROW row = NULL; /* Row data */
248 
249  char* temp_name = NULL; /* place to store name returned */
250  char* temp_cap = NULL; /* place to store capacity returned */
251  int temp_back = 0; /* place to store backup flag returned */
252 
253  /* Select rows */
254  StrAppend(&sql, "select name, capacity, requirebackup from securitymodules ");
255  StrAppend(&sql, "order by name");
256 
257  DusEnd(&sql);
258 
259  status = DbExecuteSql(DbHandle(), sql, &result);
260 
261  if (status == 0) {
262  status = DbFetchRow(result, &row);
263  printf("Name: Capacity: RequireBackup:\n");
264  while (status == 0) {
265  /* Got a row, print it */
266  DbString(row, 0, &temp_name);
267  DbString(row, 1, &temp_cap);
268  DbInt(row, 2, &temp_back);
269 
270  printf("%-32s %-12s %s\n", temp_name, (strlen(temp_cap) == 0) ? "unset" : temp_cap, (temp_back == 0) ? "No" : "Yes");
271 
272  status = DbFetchRow(result, &row);
273  }
274 
275  /* Convert EOF status to success */
276 
277  if (status == -1) {
278  status = 0;
279  }
280 
281  DbFreeResult(result);
282  }
283 
284  DusFree(sql);
285  DbFreeRow(row);
286  DbStringFree(temp_name);
287  DbStringFree(temp_cap);
288 
289  return status;
290 }
291 
292 /*+
293  * KsmListPolicies - Output a list of all policies available
294  *
295  *
296  * Arguments:
297  *
298  * none
299  *
300  * Returns:
301  * int
302  * Status return. 0 on success.
303  * other on fail
304  */
305 
307 {
308  char* sql = NULL; /* SQL query */
309  int status = 0; /* Status return */
310  DB_RESULT result; /* Result of the query */
311  DB_ROW row = NULL; /* Row data */
312 
313  char* temp_name = NULL; /* place to store name returned */
314  char* temp_desc = NULL; /* place to store description returned */
315 
316  /* Select rows */
317  StrAppend(&sql, "select name, description from policies ");
318  StrAppend(&sql, "order by name");
319 
320  DusEnd(&sql);
321 
322  status = DbExecuteSql(DbHandle(), sql, &result);
323 
324  if (status == 0) {
325  status = DbFetchRow(result, &row);
326  printf("Name: Description:\n");
327  while (status == 0) {
328  /* Got a row, print it */
329  DbString(row, 0, &temp_name);
330  DbString(row, 1, &temp_desc);
331 
332  printf("%-32s %s\n", temp_name, (strlen(temp_desc) == 0) ? "unset" : temp_desc);
333 
334  status = DbFetchRow(result, &row);
335  }
336 
337  /* Convert EOF status to success */
338 
339  if (status == -1) {
340  status = 0;
341  }
342 
343  DbFreeResult(result);
344  }
345 
346  DusFree(sql);
347  DbFreeRow(row);
348  DbStringFree(temp_name);
349  DbStringFree(temp_desc);
350 
351  return status;
352 }
353 
354 /*+
355  * KsmListRollovers - Output a list of expected rollovers
356  *
357  *
358  * Arguments:
359  *
360  * int zone_id
361  * ID of the zone (-1 for all)
362  *
363  * Returns:
364  * int
365  * Status return. 0 on success.
366  * other on fail
367  */
368 
369 int KsmListRollovers(int zone_id, int* ds_count)
370 {
371  char* sql = NULL; /* SQL query */
372  int status = 0; /* Status return */
373  char stringval[KSM_INT_STR_SIZE]; /* For Integer to String conversion */
374  DB_RESULT result; /* Result of the query */
375  DB_ROW row = NULL; /* Row data */
376 
377  char* temp_zone = NULL; /* place to store zone name returned */
378  int temp_type = 0; /* place to store key type returned */
379  char* temp_date = NULL; /* place to store date returned */
380  int temp_state = 0; /* place to store key state returned */
381  int local_count = 0; /* how many ds-seen required */
382 
383  /* Select rows */
384  StrAppend(&sql, "select z.name, k.keytype, k.retire, k.state from zones z, KEYDATA_VIEW k where z.id = k.zone_id and k.state in (3,4,7) ");
385  if (zone_id != -1) {
386  StrAppend(&sql, "and zone_id = ");
387  snprintf(stringval, KSM_INT_STR_SIZE, "%d", zone_id);
388  StrAppend(&sql, stringval);
389  }
390  StrAppend(&sql, " order by zone_id");
391 
392  DusEnd(&sql);
393 
394  status = DbExecuteSql(DbHandle(), sql, &result);
395 
396  if (status == 0) {
397  status = DbFetchRow(result, &row);
398  printf("Zone: Keytype: Rollover expected:\n");
399  while (status == 0) {
400  /* Got a row, print it */
401  DbString(row, 0, &temp_zone);
402  DbInt(row, 1, &temp_type);
403  DbString(row, 2, &temp_date);
404  DbInt(row, 3, &temp_state);
405 
406  if (temp_state == KSM_STATE_ACTIVE) {
407  printf("%-31s %-13s %s\n", temp_zone, (temp_type == KSM_TYPE_KSK) ? "KSK" : "ZSK", (temp_date == NULL) ? "(not scheduled)" : temp_date);
408  }
409  else if (temp_type == KSM_TYPE_KSK) {
410  printf("%-31s %-13s %s\n", temp_zone, "KSK", "waiting for ds-seen");
411  local_count++;
412  }
413 
414  status = DbFetchRow(result, &row);
415  }
416 
417  /* Convert EOF status to success */
418 
419  if (status == -1) {
420  status = 0;
421  }
422 
423  DbFreeResult(result);
424  }
425 
426  DusFree(sql);
427  DbFreeRow(row);
428  DbStringFree(temp_zone);
429  DbStringFree(temp_date);
430 
431  *ds_count = local_count;
432 
433  return status;
434 }
435 
436 /*+
437  * KsmCheckNextRollover - Find next expected rollover
438  *
439  *
440  * Arguments:
441  *
442  * int keytype
443  * KSK or ZSK
444  *
445  * int zone_id
446  * ID of the zone
447  *
448  * char** datetime
449  * (returned) date that a rollover is expected
450  *
451  * Returns:
452  * int
453  * Status return. 0 on success.
454  * other on fail
455  */
456 
457 int KsmCheckNextRollover(int keytype, int zone_id, char** datetime)
458 {
459  char* sql = NULL; /* SQL query */
460  int status = 0; /* Status return */
461  DB_RESULT result; /* Result of the query */
462  DB_ROW row = NULL; /* Row data */
463 
464  /* Select rows */
465  sql = DqsSpecifyInit("KEYDATA_VIEW", "retire");
466  DqsConditionInt(&sql, "KEYTYPE", DQS_COMPARE_EQ, keytype, 0);
467  DqsConditionInt(&sql, "STATE", DQS_COMPARE_EQ, KSM_STATE_ACTIVE, 1);
468  DqsConditionInt(&sql, "ZONE_ID", DQS_COMPARE_EQ, zone_id, 2);
469  StrAppend(&sql, " order by retire asc");
470 
471  DqsEnd(&sql);
472 
473  status = DbExecuteSql(DbHandle(), sql, &result);
474 
475  if (status == 0) {
476  status = DbFetchRow(result, &row);
477 
478  /* First row should be the closest rollover if there are multiple active keys */
479  if (status == 0) {
480  DbString(row, 0, datetime);
481  }
482 
483  DbFreeResult(result);
484  DbFreeRow(row);
485  }
486 
487  DusFree(sql);
488 
489  return status;
490 }
491 
void DbFreeResult(DB_RESULT result)
int DbFetchRow(DB_RESULT result, DB_ROW *row)
char * DqsSpecifyInit(const char *table, const char *fields)
Definition: dq_string.c:117
#define KSM_STATE_ACTIVE
Definition: ksm.h:366
int KsmCheckNextRollover(int keytype, int zone_id, char **datetime)
Definition: ksm_list.c:457
void DusFree(char *sql)
Definition: du_string.c:223
int KsmListRepos()
Definition: ksm_list.c:242
DB_HANDLE DbHandle(void)
int DbString(DB_ROW row, int field_index, char **result)
void DqsConditionInt(char **query, const char *field, DQS_COMPARISON compare, int value, int index)
Definition: dq_string.c:224
void DbFreeRow(DB_ROW row)
int DbExecuteSql(DB_HANDLE handle, const char *stmt_str, DB_RESULT *result)
void StrAppend(char **str1, const char *str2)
Definition: string_util2.c:76
void DusEnd(char **sql)
Definition: du_string.c:202
int KsmListBackups(int repo_id, int verbose_flag)
Definition: ksm_list.c:64
int KsmListRollovers(int zone_id, int *ds_count)
Definition: ksm_list.c:369
#define KSM_INT_STR_SIZE
Definition: ksm.h:64
int KsmListPolicies()
Definition: ksm_list.c:306
#define KSM_TYPE_KSK
Definition: ksm.h:355
int DbInt(DB_ROW row, int field_index, int *value)
void DqsEnd(char **query)
Definition: dq_string.c:299
void DbStringFree(char *string)