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