src/server/config/keyfile.c

changeset 415
d938228c382e
parent 255
b5d15a4a19f5
child 453
4586d534f9b5
equal deleted inserted replaced
414:99a34860c105 415:d938228c382e
29 #include <stdio.h> 29 #include <stdio.h>
30 #include <stdlib.h> 30 #include <stdlib.h>
31 31
32 #include "keyfile.h" 32 #include "keyfile.h"
33 33
34 #define KEYFILE_MAX_TOKENS 4096
35
34 KeyfileConfig *load_keyfile_config(const char *file) { 36 KeyfileConfig *load_keyfile_config(const char *file) {
35 FILE *in = fopen(file, "r"); 37 FILE *in = fopen(file, "r");
36 if(in == NULL) { 38 if(in == NULL) {
37 return NULL; 39 return NULL;
38 } 40 }
39 41
40 KeyfileConfig *conf = malloc(sizeof(KeyfileConfig)); 42 KeyfileConfig *conf = malloc(sizeof(KeyfileConfig));
41 conf->parser.parse = keyfile_parse; 43 conf->parser.parse = keyfile_parse;
42 conf->file = strdup(file); 44 conf->file = strdup(file);
43 conf->users = NULL; 45 conf->users_begin = NULL;
46 conf->users_end = NULL;
44 47
45 int r = cfg_parse_basic_file((ConfigParser*)conf, in); 48 int r = cfg_parse_basic_file((ConfigParser*)conf, in);
46 if(r != 0) { 49 if(r != 0) {
47 fclose(in); 50 fclose(in);
48 free(conf); 51 free(conf);
54 57
55 return conf; 58 return conf;
56 } 59 }
57 60
58 void free_keyfile_config(KeyfileConfig *conf) { 61 void free_keyfile_config(KeyfileConfig *conf) {
62 /*
59 if(conf->users) { 63 if(conf->users) {
60 ucx_list_free_a(conf->parser.mp, conf->users); 64 ucx_list_free_a(conf->parser.mp, conf->users);
61 } 65 }
62 ucx_mempool_destroy(conf->parser.mp->pool); 66 ucx_mempool_destroy(conf->parser.mp->pool);
67 */
63 free(conf); 68 free(conf);
64 } 69 }
65 70
66 int keyfile_parse(void *p, ConfigLine *begin, ConfigLine *end, sstr_t line) { 71 int keyfile_parse(void *p, ConfigLine *begin, ConfigLine *end, cxmutstr line) {
67 KeyfileConfig *conf = p; 72 KeyfileConfig *conf = p;
68 UcxAllocator *mp = conf->parser.mp; 73 CxAllocator *mp = conf->parser.mp;
69 74
70 ssize_t tkn = 0; 75 cxstring *tk = NULL;
71 sstr_t *tk = sstrsplit(line, sstrn(";", 1), &tkn); 76 ssize_t tkn = cx_strsplit_a(mp, cx_strcast(line), cx_strn(";", 1), KEYFILE_MAX_TOKENS, &tk);
72 77
73 if(tkn < 2) { 78 if(tkn < 2) {
74 return 1; 79 return 1;
75 } 80 }
76 81
77 KeyfileEntry *entry = OBJ_NEW(mp, KeyfileEntry); 82 KeyfileEntry *entry = OBJ_NEW(mp, KeyfileEntry);
78 entry->groups = NULL; 83 ZERO(entry, sizeof(KeyfileEntry));
79 entry->numgroups = 0;
80 84
81 // get user name 85 // get user name
82 entry->name = sstrdup_a(mp, tk[0]); 86 entry->name = cx_strdup_a(mp, tk[0]);
83 87
84 // get hash 88 // get hash
85 sstr_t hash = sstrtrim(tk[1]); 89 cxstring hash = cx_strtrim(tk[1]);
86 if(hash.length < 4) { 90 if(hash.length < 4) {
87 // to short 91 // to short
88 return 1; 92 return 1;
89 } 93 }
90 if(hash.ptr[0] != '{') { 94 if(hash.ptr[0] != '{') {
91 // missing hash type specification 95 // missing hash type specification
92 return 1; 96 return 1;
93 } 97 }
94 98
95 // get hash type and data 99 // get hash type and data
96 sstr_t hash_type; 100 cxstring hash_type;
97 sstr_t hash_data; 101 cxstring hash_data;
98 for(int i=1;i<hash.length;i++) { 102 for(int i=1;i<hash.length;i++) {
99 if(hash.ptr[i] == '}') { 103 if(hash.ptr[i] == '}') {
100 hash_type = sstrsubsl(hash, 1, i-1); 104 hash_type = cx_strsubsl(hash, 1, i-1);
101 hash_data = sstrsubs(hash, i+1); 105 hash_data = cx_strsubs(hash, i+1);
102 } 106 }
103 } 107 }
104 108
105 if(!sstrcmp(hash_type, sstr("SSHA"))) { 109 if(!cx_strcmp(hash_type, cx_str("SSHA"))) {
106 entry->hashtype = KEYFILE_SSHA; 110 entry->hashtype = KEYFILE_SSHA;
107 } else if(!sstrcmp(hash_type, sstr("SSHA256"))) { 111 } else if(!cx_strcmp(hash_type, cx_str("SSHA256"))) {
108 entry->hashtype = KEYFILE_SSHA256; 112 entry->hashtype = KEYFILE_SSHA256;
109 } else if(!sstrcmp(hash_type, sstr("SSHA512"))) { 113 } else if(!cx_strcmp(hash_type, cx_str("SSHA512"))) {
110 entry->hashtype = KEYFILE_SSHA512; 114 entry->hashtype = KEYFILE_SSHA512;
111 } else { 115 } else {
112 // unkown hash type 116 // unkown hash type
113 log_ereport( 117 log_ereport(
114 LOG_FAILURE, 118 LOG_FAILURE,
115 "keyfile_parse: unknown hash type: %s", 119 "keyfile_parse: unknown hash type: %s",
116 sstrdup_a(mp, hash_type).ptr); 120 cx_strdup_a(mp, hash_type).ptr);
117 return 1; 121 return 1;
118 } 122 }
119 123
120 entry->hashdata = sstrdup_a(mp, hash_data); 124 entry->hashdata = cx_strdup_a(mp, hash_data);
121 125
122 // get groups 126 // get groups
123 if(tkn == 3) { 127 if(tkn == 3) {
124 sstr_t groups_str = sstrtrim(tk[2]); 128 cxstring groups_str = cx_strtrim(tk[2]);
125 ssize_t ngroups = 0; 129 cxstring *groups = NULL;
126 sstr_t *groups = sstrsplit(groups_str, sstrn(",", 1), &ngroups); 130 ssize_t ngroups = cx_strsplit_a(mp, groups_str, cx_strn(",", 1), KEYFILE_MAX_TOKENS, &groups);
127 if(ngroups > 0) { 131 if(ngroups > 0) {
128 entry->groups = mp->calloc(mp->pool, ngroups, sizeof(sstr_t)); 132 entry->groups = cxCalloc(mp, ngroups, sizeof(cxmutstr));
129 entry->numgroups = ngroups; 133 entry->numgroups = ngroups;
130 for(int i=0;i<ngroups;i++) { 134 for(int i=0;i<ngroups;i++) {
131 entry->groups[i] = sstrdup_a(mp, sstrtrim(groups[i])); 135 entry->groups[i] = cx_strdup_a(mp, cx_strtrim(groups[i]));
132 free(groups[i].ptr);
133 } 136 }
134 free(groups); 137 cxFree(mp, groups);
135 } 138 }
136 } 139 }
137 140
138 // add user 141 // add user
139 conf->users = ucx_list_append_a(mp, conf->users, entry); 142 CFG_KEYFILE_ADD(&conf->users_begin, &conf->users_end, entry);
140 143
141 // free tokens 144 // free tokens
142 for(int i=0;i<tkn;i++) { 145 cxFree(mp, tk);
143 free(tk[i].ptr);
144 }
145 free(tk);
146 146
147 return 0; 147 return 0;
148 } 148 }

mercurial