add cfg functions for modifying keys

Tue, 29 Oct 2024 17:53:47 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 29 Oct 2024 17:53:47 +0100
changeset 845
8ac8bf29e2ee
parent 842
10ee615ca557
child 846
be77f90ec48e

add cfg functions for modifying keys

libidav/config.c file | annotate | diff | comparison | revisions
libidav/config.h file | annotate | diff | comparison | revisions
--- a/libidav/config.c	Mon Oct 28 16:45:18 2024 +0100
+++ b/libidav/config.c	Tue Oct 29 17:53:47 2024 +0100
@@ -611,6 +611,74 @@
 }
 
 
+void dav_config_add_key(DavConfig *config, DavCfgKey *key) {
+    cx_linked_list_add(
+            (void**)&config->keys,
+            NULL,
+            offsetof(DavCfgKey, prev),
+            offsetof(DavCfgKey, next),
+            key);
+    
+    if(key->node) {
+        fprintf(stderr, "Error: dav_config_add_key: node already exists\n");
+        return;
+    }
+    
+    xmlNode *keyNode = xmlNewNode(NULL, BAD_CAST "key");
+    xmlNode *rtext1 = xmlNewDocText(config->doc, BAD_CAST "\n");
+    xmlAddChild(keyNode, rtext1);
+    key->node = keyNode;
+    
+    if(key->name.value.ptr) {
+        key->name.node = addXmlNode(keyNode, "name", key->name.value);
+    }
+    const char *type = dav_config_keytype_str(key->type);
+    if(type) {
+        key->type_node = addXmlNode(keyNode, "type", cx_mutstr((char*)type));
+    }
+    if(key->file.value.ptr) {
+        key->file.node = addXmlNode(keyNode, "file", key->file.value);
+    }
+    
+     // indent closing tag
+    xmlNode *rtext2 = xmlNewDocText(config->doc, BAD_CAST "\t");
+    xmlAddChild(keyNode, rtext2);
+    
+    // add key element to the xml document
+    xmlNode *xml_root = xmlDocGetRootElement(config->doc);
+    
+    xmlNode *text1 = xmlNewDocText(config->doc, BAD_CAST "\n\t");
+    xmlAddChild(xml_root, text1);
+    
+    xmlAddChild(xml_root, keyNode);
+    
+    xmlNode *text2 = xmlNewDocText(config->doc, BAD_CAST "\n");
+    xmlAddChild(xml_root, text2);
+}
+
+DavCfgKey* dav_key_new(DavConfig *config) {
+    DavCfgKey *key = cxMalloc(config->mp->allocator, sizeof(DavCfgKey));
+    memset(key, 0, sizeof(DavCfgKey));
+    key->type = DAV_KEY_TYPE_AES256;
+    return key;
+}
+
+void dav_key_remove_and_free(DavConfig *config, DavCfgKey *key) {
+    cx_linked_list_remove(
+            (void**)&config->keys,
+            NULL,
+            offsetof(DavCfgKey, prev),
+            offsetof(DavCfgKey, next),
+            key);
+    if(key->node) {
+        // TODO: remove newline after key node
+        
+        xmlUnlinkNode(key->node);
+        xmlFreeNode(key->node);
+    }
+}
+
+
 static int load_key(
         DavConfig *config,
         DavCfgKey **list_begin,
@@ -621,6 +689,7 @@
     DavCfgKey *key = cxMalloc(config->mp->allocator, sizeof(DavCfgKey));
     memset(key, 0, sizeof(DavCfgKey));
     key->type = DAV_KEY_TYPE_AES256;
+    key->node = keynode;
     
     int error = 0;
     while(node) {
@@ -922,6 +991,15 @@
     return 0;
 }
 
+const char* dav_config_keytype_str(DavCfgKeyType type) {
+    switch(type) {
+        default: break;
+        case DAV_KEY_TYPE_AES256: return "aes256";
+        case DAV_KEY_TYPE_AES128: return "aes128";
+    }
+    return NULL;
+}
+
 int dav_config_register_keys(DavConfig *config, DavContext *ctx, dav_loadkeyfile_func loadkey) {
     for(DavCfgKey *key=config->keys;key;key=key->next) {
         char *file = cx_strdup_m(key->file.value).ptr;
--- a/libidav/config.h	Mon Oct 28 16:45:18 2024 +0100
+++ b/libidav/config.h	Tue Oct 29 17:53:47 2024 +0100
@@ -131,6 +131,8 @@
 };
 
 struct DavCfgKey {
+    xmlNode *node;
+    
     CfgString  name;
     CfgString  file;
     DavCfgKeyType type;
@@ -178,6 +180,11 @@
 void dav_repository_set_auth(DavConfig *config, DavCfgRepository *repo, cxstring user, cxstring password);
 cxmutstr dav_repository_get_decodedpassword(DavCfgRepository *repo);
 
+void dav_config_add_key(DavConfig *config, DavCfgKey *key);
+
+DavCfgKey* dav_key_new(DavConfig *config);
+void dav_key_remove_and_free(DavConfig *config, DavCfgKey *key);
+
 int dav_str2ssl_version(const char *str);
 
 int dav_cfg_string_set_node_value(DavConfig *config, CfgString *str, xmlNode *node);
@@ -198,6 +205,7 @@
 DavCfgRepository* dav_config_url2repo_s(DavConfig *config, cxstring url, cxmutstr *path);
 
 int dav_config_keytype(DavCfgKeyType type);
+const char* dav_config_keytype_str(DavCfgKeyType type);
 int dav_config_register_keys(DavConfig *config, DavContext *ctx, dav_loadkeyfile_func loadkey);
 
 int dav_config_register_namespaces(DavConfig *config, DavContext *ctx);

mercurial