dav/pwd.c

changeset 474
017a4f09e6fa
parent 473
6740adb5fccd
child 488
29b979ca8750
equal deleted inserted replaced
473:6740adb5fccd 474:017a4f09e6fa
54 } 54 }
55 55
56 PwdStore *p = malloc(sizeof(PwdStore)); 56 PwdStore *p = malloc(sizeof(PwdStore));
57 p->ids = ucx_map_new(16); 57 p->ids = ucx_map_new(16);
58 p->locations = NULL; 58 p->locations = NULL;
59 p->noloc = NULL;
60 p->index = ucx_map_new(16);
59 p->content = buf; 61 p->content = buf;
60 p->key = NULL; 62 p->key = NULL;
61 p->encoffset = PWDS_HEADER_SIZE; 63 p->encoffset = PWDS_HEADER_SIZE;
62 p->isdecrypted = 0; 64 p->isdecrypted = 0;
63 65
70 } 72 }
71 73
72 PwdStore* pwdstore_new(void) { 74 PwdStore* pwdstore_new(void) {
73 PwdStore *p = calloc(1, sizeof(PwdStore)); 75 PwdStore *p = calloc(1, sizeof(PwdStore));
74 p->ids = ucx_map_new(16); 76 p->ids = ucx_map_new(16);
77 p->locations = NULL;
78 p->noloc = NULL;
79 p->index = ucx_map_new(16);
75 p->content = ucx_buffer_new(NULL, PWDS_HEADER_SIZE, UCX_BUFFER_AUTOEXTEND); 80 p->content = ucx_buffer_new(NULL, PWDS_HEADER_SIZE, UCX_BUFFER_AUTOEXTEND);
76 PWDS_MAGIC(p) = PWDS_MAGIC_CHAR; 81 PWDS_MAGIC(p) = PWDS_MAGIC_CHAR;
77 PWDS_VERSION(p) = 1; 82 PWDS_VERSION(p) = 1;
78 PWDS_ENC(p) = DAV_KEY_AES256; 83 PWDS_ENC(p) = DAV_KEY_AES256;
79 PWDS_PWFUNC(p) = DAV_PWFUNC_PBKDF2_SHA256; 84 PWDS_PWFUNC(p) = DAV_PWFUNC_PBKDF2_SHA256;
116 char *location = NULL; 121 char *location = NULL;
117 char *user = NULL; 122 char *user = NULL;
118 char *password = NULL; 123 char *password = NULL;
119 124
120 int res = 0; 125 int res = 0;
126 int ret = 0;
121 if((res += readval(in, &id, FALSE)) == 1) { 127 if((res += readval(in, &id, FALSE)) == 1) {
128 if(index) {
129 if((res += readval(in, &location, TRUE)) == 2) {
130 pwdstore_put_index(p, id, location);
131 ret = 1;
132 }
133 } else {
134 if((res += readval(in, &user, FALSE)) == 2) {
135 if((res += readval(in, &password, FALSE)) == 3) {
136 pwdstore_put(p, id, user, password);
137 ret = 1;
138 }
139 }
140 }
122 if((res += readval(in, &location, TRUE)) == 2) { 141 if((res += readval(in, &location, TRUE)) == 2) {
123 if(!index) { 142 if(!index) {
124 if((res += readval(in, &user, FALSE)) == 3) { 143 if((res += readval(in, &user, FALSE)) == 3) {
125 res += readval(in, &password, FALSE); 144 res += readval(in, &password, FALSE);
126 } 145 }
127 } 146 }
128 } 147 }
129 }
130
131 int ret = 0;
132 if((!index && res == 4) || (index && res == 2)) {
133 pwdstore_put(p, id, location, user, password);
134 ret = 1;
135 } 148 }
136 149
137 if(id) free(id); 150 if(id) free(id);
138 if(location) free(location); 151 if(location) free(location);
139 if(user) free(user); 152 if(user) free(user);
213 PWDS_PWFUNC(p) = pwfunc; 226 PWDS_PWFUNC(p) = pwfunc;
214 } 227 }
215 228
216 void pwdstore_free_entry(PwdEntry *e) { 229 void pwdstore_free_entry(PwdEntry *e) {
217 if(e->id) free(e->id); 230 if(e->id) free(e->id);
218 if(e->location) free(e->location);
219 if(e->user) free(e->user); 231 if(e->user) free(e->user);
220 if(e->password) free(e->password); 232 if(e->password) free(e->password);
221 free(e); 233 free(e);
222 } 234 }
223 235
233 245
234 free(p); 246 free(p);
235 } 247 }
236 248
237 int pwdstore_has_id(PwdStore *s, const char *id) { 249 int pwdstore_has_id(PwdStore *s, const char *id) {
238 return ucx_map_cstr_get(s->ids, id) ? 1 : 0; 250 return ucx_map_cstr_get(s->index, id) ? 1 : 0;
239 }
240
241 int pwdstore_has_location(PwdStore *s, const char *location) {
242 return 0;
243 } 251 }
244 252
245 PwdEntry* pwdstore_get(PwdStore *p, const char *id) { 253 PwdEntry* pwdstore_get(PwdStore *p, const char *id) {
246 PwdEntry *e = ucx_map_cstr_get(p->ids, id); 254 PwdEntry *e = ucx_map_cstr_get(p->ids, id);
247 if(e && e->user && e->password) { 255 if(e && e->user && e->password) {
249 } else { 257 } else {
250 return NULL; 258 return NULL;
251 } 259 }
252 } 260 }
253 261
254 void pwdstore_put(PwdStore *p, const char *id, const char *location, const char *username, const char *password) { 262 void pwdstore_put(PwdStore *p, const char *id, const char *username, const char *password) {
255 PwdEntry *entry = malloc(sizeof(PwdEntry)); 263 PwdEntry *entry = malloc(sizeof(PwdEntry));
256 entry->id = strdup(id); 264 entry->id = strdup(id);
257 entry->location = location ? strdup(location) : NULL; 265 entry->user = strdup(username);
258 entry->user = username ? strdup(username) : NULL; 266 entry->password = strdup(password);
259 entry->password = password ? strdup(password) : NULL;
260 ucx_map_cstr_put(p->ids, id, entry); 267 ucx_map_cstr_put(p->ids, id, entry);
261 268 }
269
270 void pwdstore_put_index(PwdStore *p, const char *id, const char *location) {
271 PwdIndexEntry *e = ucx_map_cstr_get(p->index, id);
272 if(e) {
273 return;
274 }
275 PwdIndexEntry *newentry = malloc(sizeof(PwdIndexEntry));
276 newentry->id = strdup(id);
262 if(location) { 277 if(location) {
263 p->locations = ucx_list_append(p->locations, entry); 278 newentry->location = strdup(location);
279 p->locations = ucx_list_append(p->locations, newentry);
280 } else {
281 newentry->location = NULL;
282 p->noloc = ucx_list_append(p->noloc, newentry);
283 }
284 ucx_map_cstr_put(p->index, id, newentry);
285 }
286
287 void write_index_entry(UcxBuffer *out, PwdIndexEntry *e) {
288 uint32_t idlen = strlen(e->id);
289 uint32_t locationlen = e->location ? strlen(e->location) : 0;
290 uint32_t netidlen = htonl(idlen);
291 uint32_t netlocationlen = htonl(locationlen);
292
293 ucx_buffer_putc(out, 0); // type
294
295 ucx_buffer_write(&netidlen, 1, sizeof(uint32_t), out);
296 ucx_buffer_write(e->id, 1, idlen, out);
297 ucx_buffer_write(&netlocationlen, 1, sizeof(uint32_t), out);
298 if(e->location) {
299 ucx_buffer_write(e->location, 1, locationlen, out);
264 } 300 }
265 } 301 }
266 302
267 int pwdstore_store(PwdStore *p, const char *file) { 303 int pwdstore_store(PwdStore *p, const char *file) {
268 if(!p->key) { 304 if(!p->key) {
269 return 1; 305 return 1;
270 } 306 }
271 307
272 UcxBuffer *index = ucx_buffer_new(NULL, 2048, UCX_BUFFER_AUTOEXTEND); 308 UcxBuffer *index = ucx_buffer_new(NULL, 2048, UCX_BUFFER_AUTOEXTEND);
273 UcxBuffer *content = ucx_buffer_new(NULL, 2048, UCX_BUFFER_AUTOEXTEND); 309 UcxBuffer *content = ucx_buffer_new(NULL, 2048, UCX_BUFFER_AUTOEXTEND);
310
311 // create index
312 UCX_FOREACH(elm, p->noloc) {
313 PwdIndexEntry *e = elm->data;
314 write_index_entry(index, e);
315 }
316 UCX_FOREACH(elm, p->locations) {
317 PwdIndexEntry *e = elm->data;
318 write_index_entry(index, e);
319 }
274 320
275 UcxMapIterator i = ucx_map_iterator(p->ids); 321 UcxMapIterator i = ucx_map_iterator(p->ids);
276 PwdEntry *value; 322 PwdEntry *value;
277 UCX_MAP_FOREACH(key, value, i) { 323 UCX_MAP_FOREACH(key, value, i) {
278 if(!value->id || !value->user || !value->password) { 324 if(!value->id || !value->user || !value->password) {
279 continue; 325 continue;
280 } 326 }
281 327
282 uint32_t idlen = strlen(value->id); 328 uint32_t idlen = strlen(value->id);
283 uint32_t locationlen = value->location ? strlen(value->location) : 0;
284 uint32_t ulen = strlen(value->user); 329 uint32_t ulen = strlen(value->user);
285 uint32_t plen = strlen(value->password); 330 uint32_t plen = strlen(value->password);
286 uint32_t netidlen = htonl(idlen); 331 uint32_t netidlen = htonl(idlen);
287 uint32_t netlocationlen = htonl(locationlen);
288 uint32_t netulen = htonl(ulen); 332 uint32_t netulen = htonl(ulen);
289 uint32_t netplen = htonl(plen); 333 uint32_t netplen = htonl(plen);
290
291 // index buffer
292 ucx_buffer_putc(index, 0); // type
293
294 ucx_buffer_write(&netidlen, 1, sizeof(uint32_t), index);
295 ucx_buffer_write(value->id, 1, idlen, index);
296 ucx_buffer_write(&netlocationlen, 1, sizeof(uint32_t), index);
297 if(value->location) {
298 ucx_buffer_write(value->location, 1, locationlen, index);
299 }
300 334
301 // content buffer 335 // content buffer
302 ucx_buffer_putc(content, 0); // type 336 ucx_buffer_putc(content, 0); // type
303 337
304 ucx_buffer_write(&netidlen, 1, sizeof(uint32_t), content); 338 ucx_buffer_write(&netidlen, 1, sizeof(uint32_t), content);
305 ucx_buffer_write(value->id, 1, idlen, content); 339 ucx_buffer_write(value->id, 1, idlen, content);
306 ucx_buffer_write(&netlocationlen, 1, sizeof(uint32_t), content);
307 if(value->location) {
308 ucx_buffer_write(value->location, 1, locationlen, content);
309 }
310 ucx_buffer_write(&netulen, 1, sizeof(uint32_t), content); 340 ucx_buffer_write(&netulen, 1, sizeof(uint32_t), content);
311 ucx_buffer_write(value->user, 1, ulen, content); 341 ucx_buffer_write(value->user, 1, ulen, content);
312 ucx_buffer_write(&netplen, 1, sizeof(uint32_t), content); 342 ucx_buffer_write(&netplen, 1, sizeof(uint32_t), content);
313 ucx_buffer_write(value->password, 1, plen, content); 343 ucx_buffer_write(value->password, 1, plen, content);
314 } 344 }

mercurial