src/server/config/keyfile.c

changeset 62
c47e081b6c0f
child 79
f48cea237ec3
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/server/config/keyfile.c	Thu May 09 19:41:11 2013 +0200
@@ -0,0 +1,137 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2013 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "keyfile.h"
+
+KeyfileConfig *load_keyfile_config(char *file) {
+    FILE *in = fopen(file, "r");
+    if(in == NULL) {
+        return NULL;
+    }
+    
+    KeyfileConfig *conf = malloc(sizeof(KeyfileConfig));
+    conf->parser.parse = keyfile_parse;
+    conf->file = file;
+    conf->users = NULL;
+
+    int r = cfg_parse_basic_file((ConfigParser*)conf, in);
+    if(r != 0) {
+        // TODO: free
+        return NULL;
+    }
+    
+    fclose(in);
+
+    return conf;
+}
+
+void free_keyfile_config(KeyfileConfig *conf) {
+    if(conf->users) {
+        ucx_list_free(conf->users);
+    }
+    ucx_mempool_free(conf->parser.mp);
+    free(conf);
+}
+
+int keyfile_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line) {
+    KeyfileConfig *conf = p;
+    UcxMempool *mp = conf->parser.mp;
+    
+    size_t tkn = 0;
+    sstr_t *tk = sstrsplit(line, sstrn(";", 1), &tkn);
+    
+    if(tkn < 2) {
+        return 1;
+    }
+    
+    KeyfileEntry *entry = OBJ_NEW(mp, KeyfileEntry);
+    entry->groups = NULL;
+    entry->numgroups = 0;
+    
+    // get user name
+    entry->name = sstrdup_mp(mp, tk[0]);
+    
+    // get hash
+    sstr_t hash = sstrtrim(tk[1]);
+    if(hash.length < 4) {
+        // to short
+        return 1;
+    }
+    if(hash.ptr[0] != '{') {
+        // missing hash type specification
+        return 1;
+    }
+    
+    // get hash type and data
+    sstr_t hash_type;
+    sstr_t 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);
+        }
+    }
+    
+    if(!sstrcmp(hash_type, sstr("SSHA"))) {
+        entry->hashtype = KEYFILE_SSHA;
+    } else {
+        // unkown hash type
+        fprintf(stderr, "unknown hash type: %s\n", sstrdup(hash_type).ptr);
+        return 1;
+    }
+    
+    entry->hashdata = sstrdup_mp(mp, hash_data);
+    
+    // get groups
+    if(tkn == 3) {
+        sstr_t groups_str = sstrtrim(tk[2]);
+        size_t ngroups = 0;
+        sstr_t *groups = sstrsplit(groups_str, sstrn(",", 1), &ngroups);
+        entry->groups = calloc(ngroups, sizeof(sstr_t));
+        entry->numgroups = ngroups;
+        for(int i=0;i<ngroups;i++) {
+            entry->groups[i] = sstrdup_mp(mp, sstrtrim(groups[i]));
+            free(groups[i].ptr);
+        }
+        free(groups);
+    }
+    
+    // add user
+    conf->users = ucx_list_append(conf->users, entry);
+    
+    // free tokens
+    for(int i=0;i<tkn;i++) {
+        free(tk[i].ptr);
+    }
+    free(tk);
+    
+    return 0;
+}

mercurial