src/server/config/keyfile.c

changeset 62
c47e081b6c0f
child 79
f48cea237ec3
equal deleted inserted replaced
61:c858850f3d3a 62:c47e081b6c0f
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2013 Olaf Wintermann. 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 are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include <stdio.h>
30 #include <stdlib.h>
31
32 #include "keyfile.h"
33
34 KeyfileConfig *load_keyfile_config(char *file) {
35 FILE *in = fopen(file, "r");
36 if(in == NULL) {
37 return NULL;
38 }
39
40 KeyfileConfig *conf = malloc(sizeof(KeyfileConfig));
41 conf->parser.parse = keyfile_parse;
42 conf->file = file;
43 conf->users = NULL;
44
45 int r = cfg_parse_basic_file((ConfigParser*)conf, in);
46 if(r != 0) {
47 // TODO: free
48 return NULL;
49 }
50
51 fclose(in);
52
53 return conf;
54 }
55
56 void free_keyfile_config(KeyfileConfig *conf) {
57 if(conf->users) {
58 ucx_list_free(conf->users);
59 }
60 ucx_mempool_free(conf->parser.mp);
61 free(conf);
62 }
63
64 int keyfile_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line) {
65 KeyfileConfig *conf = p;
66 UcxMempool *mp = conf->parser.mp;
67
68 size_t tkn = 0;
69 sstr_t *tk = sstrsplit(line, sstrn(";", 1), &tkn);
70
71 if(tkn < 2) {
72 return 1;
73 }
74
75 KeyfileEntry *entry = OBJ_NEW(mp, KeyfileEntry);
76 entry->groups = NULL;
77 entry->numgroups = 0;
78
79 // get user name
80 entry->name = sstrdup_mp(mp, tk[0]);
81
82 // get hash
83 sstr_t hash = sstrtrim(tk[1]);
84 if(hash.length < 4) {
85 // to short
86 return 1;
87 }
88 if(hash.ptr[0] != '{') {
89 // missing hash type specification
90 return 1;
91 }
92
93 // get hash type and data
94 sstr_t hash_type;
95 sstr_t hash_data;
96 for(int i=1;i<hash.length;i++) {
97 if(hash.ptr[i] == '}') {
98 hash_type = sstrsubsl(hash, 1, i-1);
99 hash_data = sstrsubs(hash, i+1);
100 }
101 }
102
103 if(!sstrcmp(hash_type, sstr("SSHA"))) {
104 entry->hashtype = KEYFILE_SSHA;
105 } else {
106 // unkown hash type
107 fprintf(stderr, "unknown hash type: %s\n", sstrdup(hash_type).ptr);
108 return 1;
109 }
110
111 entry->hashdata = sstrdup_mp(mp, hash_data);
112
113 // get groups
114 if(tkn == 3) {
115 sstr_t groups_str = sstrtrim(tk[2]);
116 size_t ngroups = 0;
117 sstr_t *groups = sstrsplit(groups_str, sstrn(",", 1), &ngroups);
118 entry->groups = calloc(ngroups, sizeof(sstr_t));
119 entry->numgroups = ngroups;
120 for(int i=0;i<ngroups;i++) {
121 entry->groups[i] = sstrdup_mp(mp, sstrtrim(groups[i]));
122 free(groups[i].ptr);
123 }
124 free(groups);
125 }
126
127 // add user
128 conf->users = ucx_list_append(conf->users, entry);
129
130 // free tokens
131 for(int i=0;i<tkn;i++) {
132 free(tk[i].ptr);
133 }
134 free(tk);
135
136 return 0;
137 }

mercurial