libidav/pwdstore.c

changeset 65
48f43130b4a2
parent 60
ee4e4742391e
child 66
eee1f3092844
equal deleted inserted replaced
64:98d0e2516f4e 65:48f43130b4a2
107 p->isdecrypted = 1; 107 p->isdecrypted = 1;
108 p->encoffset = PWDS_HEADER_SIZE; 108 p->encoffset = PWDS_HEADER_SIZE;
109 return p; 109 return p;
110 } 110 }
111 111
112 PwdStore* pwdstore_clone(PwdStore *p) {
113 CxBuffer *newbuffer = calloc(1, sizeof(CxBuffer));
114 *newbuffer = *p->content;
115 newbuffer->space = malloc(p->content->capacity);
116 memcpy(newbuffer->space, p->content->space, p->content->capacity);
117
118 DavKey *key = NULL;
119 if(p->key) {
120 key = malloc(sizeof(DavKey));
121 key->data = malloc(p->key->length);
122 memcpy(key->data, p->key->data, p->key->length);
123 key->length = p->key->length;
124 key->type = p->key->type;
125 key->name = NULL;
126 }
127
128 PwdStore *newp = calloc(1, sizeof(PwdStore));
129 newp->ids = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
130 newp->locations = cxLinkedListCreateSimple(CX_STORE_POINTERS);
131 newp->noloc = cxLinkedListCreateSimple(CX_STORE_POINTERS);
132 newp->index = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
133 newp->content = newbuffer;
134 newp->key = key;
135 newp->unlock_cmd = p->unlock_cmd ? strdup(p->unlock_cmd) : NULL;
136 newp->lock_cmd = p->lock_cmd ? strdup(p->lock_cmd) : NULL;
137 newp->encoffset = p->encoffset;
138 newp->isdecrypted = p->isdecrypted;
139
140 CxIterator i = cxMapIterator(p->ids);
141 cx_foreach(CxMapEntry *, e, i) {
142 PwdEntry *entry = e->value;
143 PwdEntry *new_entry = malloc(sizeof(PwdEntry));
144 new_entry->id = strdup(entry->id);
145 new_entry->user = entry->user ? strdup(entry->user) : NULL;
146 new_entry->password = entry->password ? strdup(entry->password) : NULL;
147 cxMapPut(newp->ids, *e->key, new_entry);
148 }
149
150 i = cxMapIterator(p->index);
151 cx_foreach(CxMapEntry *, e, i) {
152 PwdIndexEntry *entry = e->value;
153 CxList *locations = NULL;
154 if(entry->locations) {
155 locations = cxLinkedListCreateSimple(CX_STORE_POINTERS);
156 CxIterator li = cxListIterator(entry->locations);
157 cx_foreach(char *, location, li) {
158 cxListAdd(locations, strdup(location));
159 }
160 }
161 pwdstore_put_index(newp, entry->id, locations);
162 }
163
164 return newp;
165 }
166
112 static int readval(CxBuffer *in, char **val, int allowzero) { 167 static int readval(CxBuffer *in, char **val, int allowzero) {
113 // value = length string 168 // value = length string
114 // length = uint32 169 // length = uint32
115 // string = bytes 170 // string = bytes
116 171
170 } 225 }
171 } 226 }
172 227
173 if(ret) { 228 if(ret) {
174 pwdstore_put_index(p, id, locations); 229 pwdstore_put_index(p, id, locations);
230 if(cxListSize(locations) == 0) {
231 cxListDestroy(locations);
232 }
175 } else { 233 } else {
176 if(id) free(id); 234 if(id) free(id);
177 cxListDestroy(locations); 235 cxListDestroy(locations);
178 } 236 }
179 237
233 CxHashKey key = cx_hash_key_str(id); 291 CxHashKey key = cx_hash_key_str(id);
234 PwdIndexEntry *i = cxMapRemoveAndGet(s->index, key); 292 PwdIndexEntry *i = cxMapRemoveAndGet(s->index, key);
235 PwdEntry *e = cxMapRemoveAndGet(s->ids, key); 293 PwdEntry *e = cxMapRemoveAndGet(s->ids, key);
236 294
237 if(i) { 295 if(i) {
238 cxListDestroy(i->locations); 296 if(i->locations) {
297 cxListDestroy(i->locations);
298 }
239 free(i->id); 299 free(i->id);
240 free(i); 300 free(i);
241 } 301 }
242 if(e) { 302 if(e) {
243 free(e->id); 303 free(e->id);
304 364
305 while(read_pwdentry(p, content)) {} 365 while(read_pwdentry(p, content)) {}
306 366
307 cxBufferFree(content); 367 cxBufferFree(content);
308 368
369 p->isdecrypted = 1;
370
309 return 0; 371 return 0;
310 } 372 }
311 373
312 int pwdstore_setpassword(PwdStore *p, const char *password) { 374 int pwdstore_setpassword(PwdStore *p, const char *password) {
313 DavKey *key = dav_pw2key( 375 DavKey *key = dav_pw2key(
375 if(e) { 437 if(e) {
376 return; 438 return;
377 } 439 }
378 PwdIndexEntry *newentry = malloc(sizeof(PwdIndexEntry)); 440 PwdIndexEntry *newentry = malloc(sizeof(PwdIndexEntry));
379 newentry->id = id; 441 newentry->id = id;
380 if(cxListSize(locations) > 0) { 442 if(locations && cxListSize(locations) > 0) {
381 newentry->locations = locations; 443 newentry->locations = locations;
382 cxListAdd(p->locations, newentry); 444 cxListAdd(p->locations, newentry);
383 } else { 445 } else {
384 newentry->locations = NULL; 446 newentry->locations = NULL;
385 cxListAdd(p->noloc, newentry); 447 cxListAdd(p->noloc, newentry);
386 cxListDestroy(locations);
387 } 448 }
388 cxMapPut(p->index, cx_hash_key_str(id), newentry); 449 cxMapPut(p->index, cx_hash_key_str(id), newentry);
389 } 450 }
390 451
391 void write_index_entry(CxBuffer *out, PwdIndexEntry *e) { 452 void write_index_entry(CxBuffer *out, PwdIndexEntry *e) {
395 cxBufferPut(out, 0); // type 456 cxBufferPut(out, 0); // type
396 457
397 cxBufferWrite(&netidlen, 1, sizeof(uint32_t), out); 458 cxBufferWrite(&netidlen, 1, sizeof(uint32_t), out);
398 cxBufferWrite(e->id, 1, idlen, out); 459 cxBufferWrite(e->id, 1, idlen, out);
399 460
400 CxIterator i = cxListIterator(e->locations); 461 if(e->locations) {
401 cx_foreach(char *, location, i) { 462 CxIterator i = cxListIterator(e->locations);
402 uint32_t locationlen = strlen(location); 463 cx_foreach(char *, location, i) {
403 uint32_t netlocationlen = htonl(locationlen); 464 uint32_t locationlen = strlen(location);
404 465 uint32_t netlocationlen = htonl(locationlen);
405 cxBufferWrite(&netlocationlen, 1, sizeof(uint32_t), out); 466
406 cxBufferWrite(location, 1, locationlen, out); 467 cxBufferWrite(&netlocationlen, 1, sizeof(uint32_t), out);
468 cxBufferWrite(location, 1, locationlen, out);
469 }
407 } 470 }
408 471
409 uint32_t terminate = 0; 472 uint32_t terminate = 0;
410 cxBufferWrite(&terminate, 1, sizeof(uint32_t), out); 473 cxBufferWrite(&terminate, 1, sizeof(uint32_t), out);
411 } 474 }

mercurial