OpenDNSSEC-enforcer  1.3.15
ksm_list.c
Go to the documentation of this file.
1 /*
2  * $Id: ksm_list.c 4169 2010-11-04 14:24:23Z 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)
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 
383  /* Select rows */
384  StrAppend(&sql, "select z.name, k.keytype, k.retire from zones z, KEYDATA_VIEW k where z.id = k.zone_id and k.state = 4 ");
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 
405  printf("%-31s %-13s %s\n", temp_zone, (temp_type == KSM_TYPE_KSK) ? "KSK" : "ZSK", (temp_date == NULL) ? "(not scheduled)" : temp_date);
406 
407  status = DbFetchRow(result, &row);
408  }
409 
410  /* Convert EOF status to success */
411 
412  if (status == -1) {
413  status = 0;
414  }
415 
416  DbFreeResult(result);
417  }
418 
419  DusFree(sql);
420  DbFreeRow(row);
421  DbStringFree(temp_zone);
422  DbStringFree(temp_date);
423 
424  return status;
425 }
426 
427 /*+
428  * KsmCheckNextRollover - Find next expected rollover
429  *
430  *
431  * Arguments:
432  *
433  * int keytype
434  * KSK or ZSK
435  *
436  * int zone_id
437  * ID of the zone
438  *
439  * char** datetime
440  * (returned) date that a rollover is expected
441  *
442  * Returns:
443  * int
444  * Status return. 0 on success.
445  * other on fail
446  */
447 
448 int KsmCheckNextRollover(int keytype, int zone_id, char** datetime)
449 {
450  char* sql = NULL; /* SQL query */
451  int status = 0; /* Status return */
452  DB_RESULT result; /* Result of the query */
453  DB_ROW row = NULL; /* Row data */
454 
455  /* Select rows */
456  sql = DqsSpecifyInit("KEYDATA_VIEW", "retire");
457  DqsConditionInt(&sql, "KEYTYPE", DQS_COMPARE_EQ, keytype, 0);
458  DqsConditionInt(&sql, "STATE", DQS_COMPARE_EQ, KSM_STATE_ACTIVE, 1);
459  DqsConditionInt(&sql, "ZONE_ID", DQS_COMPARE_EQ, zone_id, 2);
460  StrAppend(&sql, " order by retire asc");
461 
462  DqsEnd(&sql);
463 
464  status = DbExecuteSql(DbHandle(), sql, &result);
465 
466  if (status == 0) {
467  status = DbFetchRow(result, &row);
468 
469  /* First row should be the closest rollover if there are multiple active keys */
470  if (status == 0) {
471  DbString(row, 0, datetime);
472  }
473 
474  DbFreeResult(result);
475  DbFreeRow(row);
476  }
477 
478  DusFree(sql);
479 
480  return status;
481 }