src/server/config/keyfile.c

changeset 415
d938228c382e
parent 255
b5d15a4a19f5
child 453
4586d534f9b5
--- a/src/server/config/keyfile.c	Wed Nov 02 19:19:01 2022 +0100
+++ b/src/server/config/keyfile.c	Sun Nov 06 15:53:32 2022 +0100
@@ -31,6 +31,8 @@
 
 #include "keyfile.h"
 
+#define KEYFILE_MAX_TOKENS 4096
+
 KeyfileConfig *load_keyfile_config(const char *file) {
     FILE *in = fopen(file, "r");
     if(in == NULL) {
@@ -40,7 +42,8 @@
     KeyfileConfig *conf = malloc(sizeof(KeyfileConfig));
     conf->parser.parse = keyfile_parse;
     conf->file = strdup(file);
-    conf->users = NULL;
+    conf->users_begin = NULL;
+    conf->users_end = NULL;
 
     int r = cfg_parse_basic_file((ConfigParser*)conf, in);
     if(r != 0) {
@@ -56,33 +59,34 @@
 }
 
 void free_keyfile_config(KeyfileConfig *conf) {
+    /*
     if(conf->users) {
         ucx_list_free_a(conf->parser.mp, conf->users);
     }
     ucx_mempool_destroy(conf->parser.mp->pool);
+    */
     free(conf);
 }
 
-int keyfile_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line) {
+int keyfile_parse(void *p, ConfigLine *begin, ConfigLine *end, cxmutstr line) {
     KeyfileConfig *conf = p;
-    UcxAllocator *mp = conf->parser.mp;
+    CxAllocator *mp = conf->parser.mp;
     
-    ssize_t tkn = 0;
-    sstr_t *tk = sstrsplit(line, sstrn(";", 1), &tkn);
+    cxstring *tk = NULL;
+    ssize_t tkn = cx_strsplit_a(mp, cx_strcast(line), cx_strn(";", 1), KEYFILE_MAX_TOKENS, &tk);
     
     if(tkn < 2) {
         return 1;
     }
     
     KeyfileEntry *entry = OBJ_NEW(mp, KeyfileEntry);
-    entry->groups = NULL;
-    entry->numgroups = 0;
+    ZERO(entry, sizeof(KeyfileEntry));
     
     // get user name
-    entry->name = sstrdup_a(mp, tk[0]);
+    entry->name = cx_strdup_a(mp, tk[0]);
     
     // get hash
-    sstr_t hash = sstrtrim(tk[1]);
+    cxstring hash = cx_strtrim(tk[1]);
     if(hash.length < 4) {
         // to short
         return 1;
@@ -93,56 +97,52 @@
     }
     
     // get hash type and data
-    sstr_t hash_type;
-    sstr_t hash_data;
+    cxstring hash_type;
+    cxstring hash_data;
     for(int i=1;i<hash.length;i++) {
         if(hash.ptr[i] == '}') {
-            hash_type = sstrsubsl(hash, 1, i-1);
-            hash_data = sstrsubs(hash, i+1);
+            hash_type = cx_strsubsl(hash, 1, i-1);
+            hash_data = cx_strsubs(hash, i+1);
         }
     }
     
-    if(!sstrcmp(hash_type, sstr("SSHA"))) {
+    if(!cx_strcmp(hash_type, cx_str("SSHA"))) {
         entry->hashtype = KEYFILE_SSHA;
-    } else if(!sstrcmp(hash_type, sstr("SSHA256"))) {
+    } else if(!cx_strcmp(hash_type, cx_str("SSHA256"))) {
         entry->hashtype = KEYFILE_SSHA256;
-    } else if(!sstrcmp(hash_type, sstr("SSHA512"))) {
+    } else if(!cx_strcmp(hash_type, cx_str("SSHA512"))) {
         entry->hashtype = KEYFILE_SSHA512;
     } else {
         // unkown hash type
         log_ereport(
                 LOG_FAILURE,
                 "keyfile_parse: unknown hash type: %s",
-                sstrdup_a(mp, hash_type).ptr);
+                cx_strdup_a(mp, hash_type).ptr);
         return 1;
     }
     
-    entry->hashdata = sstrdup_a(mp, hash_data);
+    entry->hashdata = cx_strdup_a(mp, hash_data);
     
     // get groups
     if(tkn == 3) {
-        sstr_t groups_str = sstrtrim(tk[2]);
-        ssize_t ngroups = 0;
-        sstr_t *groups = sstrsplit(groups_str, sstrn(",", 1), &ngroups);
+        cxstring groups_str = cx_strtrim(tk[2]);
+        cxstring *groups = NULL;
+        ssize_t ngroups = cx_strsplit_a(mp, groups_str, cx_strn(",", 1), KEYFILE_MAX_TOKENS, &groups);
         if(ngroups > 0) {
-            entry->groups = mp->calloc(mp->pool, ngroups, sizeof(sstr_t));
+            entry->groups = cxCalloc(mp, ngroups, sizeof(cxmutstr));
             entry->numgroups = ngroups;
             for(int i=0;i<ngroups;i++) {
-                entry->groups[i] = sstrdup_a(mp, sstrtrim(groups[i]));
-                free(groups[i].ptr);
+                entry->groups[i] = cx_strdup_a(mp, cx_strtrim(groups[i]));
             }
-            free(groups);
+            cxFree(mp, groups);
         }
     }
     
     // add user
-    conf->users = ucx_list_append_a(mp, conf->users, entry);
+    CFG_KEYFILE_ADD(&conf->users_begin, &conf->users_end, entry);
     
     // free tokens
-    for(int i=0;i<tkn;i++) {
-        free(tk[i].ptr);
-    }
-    free(tk);
+    cxFree(mp, tk);
     
     return 0;
 }

mercurial