[Opendnssec-commits] [keihatsu.kirei.se/svn/dnssec] r3838 - in trunk/OpenDNSSEC/enforcer: ksm ksm/include/ksm utils

Sion Lloyd sion at nominet.org.uk
Tue Aug 31 13:35:52 CEST 2010


Author: sion
Date: 2010-08-31 13:35:52 +0200 (Tue, 31 Aug 2010)
New Revision: 3838

Modified:
   trunk/OpenDNSSEC/enforcer/ksm/include/ksm/db_fields.h
   trunk/OpenDNSSEC/enforcer/ksm/include/ksm/ksm.h
   trunk/OpenDNSSEC/enforcer/ksm/include/ksm/ksmutil.h
   trunk/OpenDNSSEC/enforcer/ksm/ksm_zone.c
   trunk/OpenDNSSEC/enforcer/utils/ksmutil.c
   trunk/OpenDNSSEC/enforcer/utils/ods-ksmutil.1.in
Log:
Import and export of zonelist, pivotal 3998434.


Modified: trunk/OpenDNSSEC/enforcer/ksm/include/ksm/db_fields.h
===================================================================
--- trunk/OpenDNSSEC/enforcer/ksm/include/ksm/db_fields.h	2010-08-31 10:53:52 UTC (rev 3837)
+++ trunk/OpenDNSSEC/enforcer/ksm/include/ksm/db_fields.h	2010-08-31 11:35:52 UTC (rev 3838)
@@ -97,10 +97,13 @@
 #define DB_SECURITY_MODULE_REQUIREBACKUP 3
 
 #define DB_ZONE_TABLE			"zones"
-#define DB_ZONE_FIELDS 			"id, name"
+#define DB_ZONE_FIELDS 			"id, name, policy_id, signconf, input, output"
 #define DB_ZONE_ID				0
 #define DB_ZONE_NAME			1
 #define DB_ZONE_POLICY_ID   	2
+#define DB_ZONE_SIGNCONF       	3
+#define DB_ZONE_INPUT       	4
+#define DB_ZONE_OUTPUT       	5
 
 /* policy select variables, including salt */
 #define DB_POLICY_ID	0

Modified: trunk/OpenDNSSEC/enforcer/ksm/include/ksm/ksm.h
===================================================================
--- trunk/OpenDNSSEC/enforcer/ksm/include/ksm/ksm.h	2010-08-31 10:53:52 UTC (rev 3837)
+++ trunk/OpenDNSSEC/enforcer/ksm/include/ksm/ksm.h	2010-08-31 11:35:52 UTC (rev 3838)
@@ -57,6 +57,7 @@
 int KsmRundown(void);
 
 #define KSM_NAME_LENGTH     256         /* Includes trailing NULL */
+#define KSM_PATH_LENGTH     4096        /* Includes trailing NULL */
 #define KSM_POLICY_DESC_LENGTH     256  /* Includes trailing NULL */
 #define KSM_POLICY_AUDIT_LENGTH    4096 /* Includes trailing NULL */
 #define KSM_TIME_LENGTH     32          /* Includes trailing NULL */
@@ -285,8 +286,13 @@
 
 /* ksmZone */
 typedef struct {
-    int id;
-    char* name;
+    int   id;
+    int   policy_id;
+    char  name[KSM_ZONE_NAME_LENGTH];
+    char  signconf[KSM_PATH_LENGTH];
+    char  input[KSM_PATH_LENGTH];
+    char  output[KSM_PATH_LENGTH];
+    char  policy_name[KSM_NAME_LENGTH];
 } KSM_ZONE;
 
 int KsmZoneInit(DB_RESULT* handle, int policy_id);

Modified: trunk/OpenDNSSEC/enforcer/ksm/include/ksm/ksmutil.h
===================================================================
--- trunk/OpenDNSSEC/enforcer/ksm/include/ksm/ksmutil.h	2010-08-31 10:53:52 UTC (rev 3837)
+++ trunk/OpenDNSSEC/enforcer/ksm/include/ksm/ksmutil.h	2010-08-31 11:35:52 UTC (rev 3838)
@@ -94,6 +94,8 @@
 int LinkKeys(const char* zone_name, int policy_id);
 int allocateKeysToZone(KSM_POLICY *policy, int key_type, int zone_id, uint16_t interval, const char* zone_name, int man_key_gen, int rollover_scheme);
 int keyRoll(int zone_id, int policy_id, int key_type);
+int get_policy_name_from_id(KSM_ZONE *zone);
+int append_zone(xmlDocPtr doc, KSM_ZONE *zone);
 
 #ifdef __cplusplus
 }

Modified: trunk/OpenDNSSEC/enforcer/ksm/ksm_zone.c
===================================================================
--- trunk/OpenDNSSEC/enforcer/ksm/ksm_zone.c	2010-08-31 10:53:52 UTC (rev 3837)
+++ trunk/OpenDNSSEC/enforcer/ksm/ksm_zone.c	2010-08-31 11:35:52 UTC (rev 3838)
@@ -74,11 +74,11 @@
     /* Construct the query */
 
     sql = DqsSpecifyInit(DB_ZONE_TABLE, DB_ZONE_FIELDS);
-    if (policy_id) {
+    if (policy_id != -1) {
         DqsConditionInt(&sql, "policy_id", DQS_COMPARE_EQ, policy_id, where++);
 
     }
-    DqsOrderBy(&sql, "NAME");
+    DqsOrderBy(&sql, "policy_id");
 
     /* Execute query and free up the query string */
 
@@ -162,7 +162,14 @@
         /* Now copy the results into the output data */
         DbInt(row, DB_ZONE_ID, &(data->id));
         DbStringBuffer(row, DB_ZONE_NAME, data->name,
-            KSM_NAME_LENGTH*sizeof(char));
+            KSM_ZONE_NAME_LENGTH*sizeof(char));
+        DbInt(row, DB_ZONE_POLICY_ID, &(data->policy_id));
+        DbStringBuffer(row, DB_ZONE_SIGNCONF, data->signconf,
+            KSM_PATH_LENGTH*sizeof(char));
+        DbStringBuffer(row, DB_ZONE_INPUT, data->input,
+            KSM_PATH_LENGTH*sizeof(char));
+        DbStringBuffer(row, DB_ZONE_OUTPUT, data->output,
+            KSM_PATH_LENGTH*sizeof(char));
     }
     else if (status == -1) {}
         /* No rows to return (but no error) */

Modified: trunk/OpenDNSSEC/enforcer/utils/ksmutil.c
===================================================================
--- trunk/OpenDNSSEC/enforcer/utils/ksmutil.c	2010-08-31 10:53:52 UTC (rev 3837)
+++ trunk/OpenDNSSEC/enforcer/utils/ksmutil.c	2010-08-31 11:35:52 UTC (rev 3838)
@@ -354,6 +354,14 @@
 }
 
     void
+usage_zonelist2 ()
+{
+        fprintf(stderr,
+            "  zonelist export\n"
+            "  zonelist import\n");
+}
+
+    void
 usage ()
 {
     fprintf(stderr,
@@ -380,6 +388,7 @@
     usage_backup ();
     usage_rollover ();
     usage_database ();
+    usage_zonelist2 ();
 
 }
 
@@ -1472,6 +1481,77 @@
 }
 
 /*
+ * To export: 
+ *          zonelist to xml
+ */
+    int
+cmd_exportzonelist ()
+{
+    int status = 0;
+    /* Database connection details */
+    DB_HANDLE	dbhandle;
+
+    xmlDocPtr doc = xmlNewDoc((const xmlChar *)"1.0");
+    xmlNodePtr root;
+    KSM_ZONE *zone;
+    int prev_policy_id = -1;
+
+    DB_RESULT	result;     /* Result set from query */
+
+    /* try to connect to the database */
+    status = db_connect(&dbhandle, NULL, 0);
+    if (status != 0) {
+        printf("Failed to connect to database\n");
+        return(1);
+    }
+
+    /* Make some space for the zone */ 
+    zone = (KSM_ZONE *)malloc(sizeof(KSM_ZONE));
+    if (zone == NULL) {
+        fprintf(stderr, "Malloc for zone struct failed\n");
+        exit(1);
+    }
+
+    /* Setup doc with a root node of <ZoneList> */
+    xmlKeepBlanksDefault(0);
+    xmlTreeIndentString = "    ";
+    root = xmlNewDocNode(doc, NULL, (const xmlChar *)"ZoneList", NULL);
+    (void) xmlDocSetRootElement(doc, root);
+
+    /* Read zones */
+    status = KsmZoneInit(&result, -1);
+    if (status == 0) {
+        /* get the first zone */
+        status = KsmZone(result, zone);
+
+        while (status == 0) {
+            if (zone->policy_id != prev_policy_id) {
+                prev_policy_id = zone->policy_id;
+                status = get_policy_name_from_id(zone);
+                if (status != 0) {
+                    fprintf(stderr, "Couldn't get name for policy with ID: %d, exiting...\n", zone->policy_id);
+                    return(1);
+                }
+            }
+            append_zone(doc, zone);
+
+            /* get next zone */
+            status = KsmZone(result, zone);
+
+        }
+    }
+
+    xmlSaveFormatFile("-", doc, 1);
+
+    xmlFreeDoc(doc);
+    /*KsmZoneFree(zone);*/
+
+    DbDisconnect(dbhandle);
+
+    return 0;
+}
+
+/*
  * To rollover a zone (or all zones on a policy if keys are shared)
  */
     int
@@ -2973,7 +3053,7 @@
         argc --;
         argv ++;
         result = cmd_update(case_verb);
-    } else if (!strncmp(case_command, "ZONE", 4)) {
+    } else if (!strncmp(case_command, "ZONE", 4) && strlen(case_command) == 4) {
         argc --; argc --;
         argv ++; argv ++;
 
@@ -3104,6 +3184,20 @@
             usage_database();
             result = -1;
         }
+    } else if (!strncmp(case_command, "ZONELIST", 8)) {
+        argc --; argc --;
+        argv ++; argv ++;
+        /* verb should be import or export */
+        if (!strncmp(case_verb, "EXPORT", 6)) {
+            result = cmd_exportzonelist();
+        }
+        else if (!strncmp(case_verb, "IMPORT", 6)) {
+            result = cmd_update("ZONELIST");
+        } else {
+            printf("Unknown command: zonelist %s\n", case_verb);
+            usage_zonelist2();
+            result = -1;
+        }
     } else {
         printf("Unknown command: %s\n", argv[0]);
         usage();
@@ -7336,3 +7430,85 @@
     
     return status;
 }
+
+int get_policy_name_from_id(KSM_ZONE *zone)
+{
+    int     where = 0;          /* WHERE clause value */
+    char*   sql = NULL;         /* SQL query */
+    DB_RESULT       result;     /* Handle converted to a result object */
+    DB_ROW      row = NULL;            /* Row data */
+    int     status = 0;         /* Status return */
+
+    /* Construct the query */
+
+    sql = DqsSpecifyInit("policies","id, name");
+    DqsConditionInt(&sql, "ID", DQS_COMPARE_EQ, zone->policy_id, where++);
+    DqsOrderBy(&sql, "id");
+
+    /* Execute query and free up the query string */
+    status = DbExecuteSql(DbHandle(), sql, &result);
+    DqsFree(sql);
+    
+    if (status != 0)
+    {
+        printf("SQL failed: %s\n", DbErrmsg(DbHandle()));
+        DbFreeResult(result);
+        return status;
+	}
+
+    /* Get the next row from the data */
+    status = DbFetchRow(result, &row);
+    if (status == 0) {
+        DbStringBuffer(row, DB_POLICY_NAME, zone->policy_name, KSM_NAME_LENGTH*sizeof(char));
+    }
+    else if (status == -1) {}
+        /* No rows to return (but no error) */
+	else {
+        printf("SQL failed: %s\n", DbErrmsg(DbHandle()));
+        return status;
+	}
+
+    DbFreeRow(row);
+    DbFreeResult(result);
+    return status;
+}
+
+int append_zone(xmlDocPtr doc, KSM_ZONE *zone)
+{
+    xmlNodePtr root;
+    xmlNodePtr zone_node;
+    xmlNodePtr adapters_node;
+    xmlNodePtr input_node;
+    xmlNodePtr output_node;
+
+    root = xmlDocGetRootElement(doc);
+    if (root == NULL) {
+        fprintf(stderr,"empty document\n");
+        return(1);
+    }
+    if (xmlStrcmp(root->name, (const xmlChar *) "ZoneList")) {
+        fprintf(stderr,"document of the wrong type, root node != %s", "ZoneList");
+        return(1);
+    }
+
+    zone_node = xmlNewTextChild(root, NULL, (const xmlChar *)"Zone", NULL);
+    (void) xmlNewProp(zone_node, (const xmlChar *)"name", (const xmlChar *)zone->name);
+
+    /* Policy */
+    (void) xmlNewTextChild(zone_node, NULL, (const xmlChar *)"Policy", (const xmlChar *)zone->policy_name);
+
+    /* SignConf */
+    (void) xmlNewTextChild(zone_node, NULL, (const xmlChar *)"SignerConfiguration", (const xmlChar *)zone->signconf);
+
+    /* Adapters */
+    adapters_node = xmlNewTextChild(zone_node, NULL, (const xmlChar *)"Adapters", NULL);
+    /* Input */
+    input_node = xmlNewTextChild(adapters_node, NULL, (const xmlChar *)"Input", NULL);
+    (void) xmlNewTextChild(input_node, NULL, (const xmlChar *)"File", (const xmlChar *)zone->input);
+    /* Output */
+    output_node = xmlNewTextChild(adapters_node, NULL, (const xmlChar *)"Output", NULL);
+    (void) xmlNewTextChild(output_node, NULL, (const xmlChar *)"File", (const xmlChar *)zone->output);
+
+
+    return(0);
+}

Modified: trunk/OpenDNSSEC/enforcer/utils/ods-ksmutil.1.in
===================================================================
--- trunk/OpenDNSSEC/enforcer/utils/ods-ksmutil.1.in	2010-08-31 10:53:52 UTC (rev 3837)
+++ trunk/OpenDNSSEC/enforcer/utils/ods-ksmutil.1.in	2010-08-31 11:35:52 UTC (rev 3838)
@@ -13,6 +13,8 @@
 .B ods-ksmutil zone add|delete|list
 .RB ...
 .br
+.B ods-ksmutil zonelist import|export
+.br
 .B ods-ksmutil key generate|import|export|list|purge|rollover|ksk-retire|ds-seen
 .RB ...
 .br
@@ -123,6 +125,12 @@
 .B zone list
 List zones from the zonelist.xml.
 TODO:Not from the database?
+.TP
+.B zonelist export
+Export list of zones from the database in the same format as zonelist.xml
+.TP
+.B zonelist import
+Synchronise the database with the contents of zonelist.xml; identical to "update zonelist"
 .SH "KEY MANAGEMENT SUBCOMMANDS"
 .LP
 .TP




More information about the Opendnssec-commits mailing list