libidav/pwdstore.c

Thu, 28 Nov 2024 17:53:13 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 28 Nov 2024 17:53:13 +0100
changeset 850
bbe2925eb590
parent 842
10ee615ca557
permissions
-rw-r--r--

revert property set/remove list changes

832
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
1 /*
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
3 *
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
4 * Copyright 2024 Olaf Wintermann. All rights reserved.
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
5 *
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
6 * Redistribution and use in source and binary forms, with or without
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
7 * modification, are permitted provided that the following conditions are met:
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
8 *
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
10 * notice, this list of conditions and the following disclaimer.
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
11 *
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
14 * documentation and/or other materials provided with the distribution.
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
15 *
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
27 */
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
28
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #include "pwdstore.h"
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include "utils.h"
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #include <stdio.h>
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include <stdlib.h>
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35 #include <string.h>
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 #include <cx/utils.h>
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #include <cx/hash_map.h>
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 #ifdef _WIN32
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 #include <winsock.h>
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 #pragma comment(lib, "Ws2_32.lib")
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 #else
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44 #include <netinet/in.h>
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 #endif
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 static pwdstore_pwinput_func pw_input = (pwdstore_pwinput_func)pwdstore_default_pwinput;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 static void *pw_input_data = "Master password: ";
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 char * pwdstore_default_pwinput(char *prompt) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 return util_password_input(prompt);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54
833
8aa2dc02d9b7 fix build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 832
diff changeset
55 void pwdstore_set_pwinput_func(pwdstore_pwinput_func func, void *userdata) {
8aa2dc02d9b7 fix build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 832
diff changeset
56 pw_input = func;
8aa2dc02d9b7 fix build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 832
diff changeset
57 pw_input_data = userdata;
8aa2dc02d9b7 fix build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 832
diff changeset
58 }
8aa2dc02d9b7 fix build
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 832
diff changeset
59
832
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 PwdStore* pwdstore_open(const char *file) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 FILE *in = fopen(file, "r");
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 if(!in) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 return NULL;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 CxBuffer *buf = cxBufferCreate(NULL, 2048, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 cx_stream_copy(in, buf, (cx_read_func)fread, (cx_write_func)cxBufferWrite);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 fclose(in);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 if(buf->size < PWDS_HEADER_SIZE || buf->space[0] != PWDS_MAGIC_CHAR) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 cxBufferFree(buf);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 return NULL;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 PwdStore *p = malloc(sizeof(PwdStore));
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 p->ids = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 p->locations = cxLinkedListCreateSimple(CX_STORE_POINTERS);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 p->noloc = cxLinkedListCreateSimple(CX_STORE_POINTERS);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 p->index = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 p->content = buf;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 p->key = NULL;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 p->unlock_cmd = NULL;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 p->lock_cmd = NULL;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 p->encoffset = PWDS_HEADER_SIZE;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 p->isdecrypted = 0;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 if(pwdstore_getindex(p)) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 pwdstore_free(p);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 return NULL;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 return p;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 PwdStore* pwdstore_new(void) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 PwdStore *p = calloc(1, sizeof(PwdStore));
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 p->ids = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 p->locations = cxLinkedListCreateSimple(CX_STORE_POINTERS);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 p->noloc = cxLinkedListCreateSimple(CX_STORE_POINTERS);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 p->index = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 p->content = cxBufferCreate(NULL, PWDS_HEADER_SIZE, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 PWDS_MAGIC(p) = PWDS_MAGIC_CHAR;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 PWDS_VERSION(p) = 1;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 PWDS_ENC(p) = DAV_KEY_AES256;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 PWDS_PWFUNC(p) = DAV_PWFUNC_PBKDF2_SHA256;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 dav_rand_bytes((unsigned char*)p->content->space+4, 16);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 p->isdecrypted = 1;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 p->encoffset = PWDS_HEADER_SIZE;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 return p;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111
841
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
112 PwdStore* pwdstore_clone(PwdStore *p) {
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
113 CxBuffer *newbuffer = calloc(1, sizeof(CxBuffer));
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
114 *newbuffer = *p->content;
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
115 newbuffer->space = malloc(p->content->capacity);
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
116 memcpy(newbuffer->space, p->content->space, p->content->capacity);
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
117
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
118 DavKey *key = NULL;
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
119 if(p->key) {
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
120 key = malloc(sizeof(DavKey));
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
121 key->data = malloc(p->key->length);
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
122 memcpy(key->data, p->key->data, p->key->length);
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
123 key->length = p->key->length;
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
124 key->type = p->key->type;
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
125 key->name = NULL;
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
126 }
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
127
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
128 PwdStore *newp = calloc(1, sizeof(PwdStore));
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
129 newp->ids = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
130 newp->locations = cxLinkedListCreateSimple(CX_STORE_POINTERS);
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
131 newp->noloc = cxLinkedListCreateSimple(CX_STORE_POINTERS);
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
132 newp->index = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16);
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
133 newp->content = newbuffer;
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
134 newp->key = key;
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
135 newp->unlock_cmd = p->unlock_cmd ? strdup(p->unlock_cmd) : NULL;
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
136 newp->lock_cmd = p->lock_cmd ? strdup(p->lock_cmd) : NULL;
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
137 newp->encoffset = p->encoffset;
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
138 newp->isdecrypted = p->isdecrypted;
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
139
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
140 CxIterator i = cxMapIterator(p->ids);
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
141 cx_foreach(CxMapEntry *, e, i) {
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
142 PwdEntry *entry = e->value;
842
10ee615ca557 fix missing strdup in pwdstore_clone
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 841
diff changeset
143 pwdstore_put(newp, entry->id, entry->user, entry->password);
841
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
144 }
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
145
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
146 i = cxMapIterator(p->index);
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
147 cx_foreach(CxMapEntry *, e, i) {
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
148 PwdIndexEntry *entry = e->value;
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
149 CxList *locations = NULL;
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
150 if(entry->locations) {
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
151 locations = cxLinkedListCreateSimple(CX_STORE_POINTERS);
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
152 CxIterator li = cxListIterator(entry->locations);
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
153 cx_foreach(char *, location, li) {
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
154 cxListAdd(locations, strdup(location));
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
155 }
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
156 }
842
10ee615ca557 fix missing strdup in pwdstore_clone
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 841
diff changeset
157 pwdstore_put_index(newp, strdup(entry->id), locations);
841
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
158 }
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
159
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
160 return newp;
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
161 }
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
162
832
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 static int readval(CxBuffer *in, char **val, int allowzero) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 // value = length string
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 // length = uint32
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 // string = bytes
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
167
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 *val = NULL;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 // get length
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 uint32_t length = 0;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 if(cxBufferRead(&length, 1, sizeof(uint32_t), in) != sizeof(uint32_t)) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 return 0;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 length = ntohl(length); // convert from BE to host byte order
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 if(length == 0) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 if(allowzero) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 return 1;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 } else {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 return 0;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 if(length > PWDSTORE_MAX_LEN) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 return 0;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 // get value
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 char *value = malloc(length + 1);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 value[length] = 0;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 if(cxBufferRead(value, 1, length, in) != length) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 free(value);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192 return 0;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 *val = value;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 return 1;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 static int read_indexentry(PwdStore *p, CxBuffer *in) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 // read type of index element
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 int type = cxBufferGet(in);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 if(type == EOF || type != 0) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 // only type 0 supported yet
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 return 0;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 char *id = NULL;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 CxList *locations = cxLinkedListCreateSimple(CX_STORE_POINTERS);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 cxDefineDestructor(locations, free);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 // get id (required)
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 int ret = 0;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 if(readval(in, &id, FALSE)) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 // get locations
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 char *location = NULL;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 while((ret = readval(in, &location, TRUE)) == 1) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 if(!location) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 break;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 cxListAdd(locations, location);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 if(ret) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 pwdstore_put_index(p, id, locations);
841
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
226 if(cxListSize(locations) == 0) {
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
227 cxListDestroy(locations);
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
228 }
832
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 } else {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230 if(id) free(id);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 cxListDestroy(locations);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 return ret;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 static int read_pwdentry(PwdStore *p, CxBuffer *in) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 int type = cxBufferGet(in);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 if(type == EOF || type != 0) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240 // only type 0 supported yet
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241 return 0;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244 char *id = NULL;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 char *user = NULL;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246 char *password = NULL;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 int ret = 0;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 if(readval(in, &id, FALSE)) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250 if(readval(in, &user, FALSE)) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 if(readval(in, &password, FALSE)) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 pwdstore_put(p, id, user, password);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
253 ret = 1;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
254 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
257
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258 if(id) free(id);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
259 if(user) free(user);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
260 if(password) free(password);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
261
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 return ret;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
264
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
265 static void remove_list_entries(PwdStore *s, const char *id) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
266 CxIterator i = cxListMutIterator(s->locations);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
267 cx_foreach(PwdIndexEntry*, ie, i) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
268 if(!strcmp(ie->id, id)) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
269 cxIteratorFlagRemoval(i);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
270 cxIteratorNext(i);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
271 break;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
273 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
274 i = cxListMutIterator(s->noloc);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
275 cx_foreach(PwdIndexEntry*, ie, i) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
276 if(!strcmp(ie->id, id)) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
277 cxIteratorFlagRemoval(i);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
278 cxIteratorNext(i);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
279 break;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
280 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
281 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
282 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
283
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
284 void pwdstore_remove_entry(PwdStore *s, const char *id) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
285 remove_list_entries(s, id);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
286
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
287 CxHashKey key = cx_hash_key_str(id);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
288 PwdIndexEntry *i = cxMapRemoveAndGet(s->index, key);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289 PwdEntry *e = cxMapRemoveAndGet(s->ids, key);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
290
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
291 if(i) {
840
9904eee3ca9a fix crash when removing pwdentries without locations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 839
diff changeset
292 if(i->locations) {
9904eee3ca9a fix crash when removing pwdentries without locations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 839
diff changeset
293 cxListDestroy(i->locations);
9904eee3ca9a fix crash when removing pwdentries without locations
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 839
diff changeset
294 }
832
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
295 free(i->id);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
296 free(i);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298 if(e) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
299 free(e->id);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300 free(e->user);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
301 free(e->password);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302 free(e);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
303 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
304 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
305
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
306 int pwdstore_getindex(PwdStore *s) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
307 uint32_t netindexlen;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
308
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
309 // set the position to the last 4 bytes of the header
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
310 // for reading index length
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
311 s->content->pos = PWDS_HEADER_SIZE - sizeof(uint32_t);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
313 // read indexlen and convert to host byte order
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
314 if(cxBufferRead(&netindexlen, 1, sizeof(uint32_t), s->content) != sizeof(uint32_t)) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315 return 1;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
316 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
317 uint32_t indexlen = ntohl(netindexlen);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
318
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
319 // integer overflow check
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
320 if(UINT32_MAX - PWDS_HEADER_SIZE < indexlen) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
321 return 1;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
322 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
323 if(s->content->size < PWDS_HEADER_SIZE + indexlen) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
324 return 1;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
325 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
326 // encrypted content starts after the index content
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
327 s->encoffset = PWDS_HEADER_SIZE + indexlen;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
328
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
329 // the index starts after the header
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
330 CxBuffer *index = cxBufferCreate(s->content->space+PWDS_HEADER_SIZE, indexlen, cxDefaultAllocator, 0);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
331 index->size = indexlen;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
332
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
333 // read index
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
334 while(read_indexentry(s, index)) {}
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
335
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
336 // free index buffer structure (not the content)
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
337 cxBufferFree(index);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
339 return 0;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 int pwdstore_decrypt(PwdStore *p) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343 if(!p->key) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 return 1;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
345 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
346 if(p->isdecrypted) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347 return 0;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
348 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
349
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
350 // decrypt contet
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
351 size_t encsz = p->content->size - p->encoffset;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
352 CxBuffer *enc = cxBufferCreate(p->content->space + p->encoffset, encsz, cxDefaultAllocator, 0);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
353 enc->size = encsz;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
354 enc->size = p->content->size - p->encoffset;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
355 CxBuffer *content = aes_decrypt_buffer(enc, p->key);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
356 cxBufferFree(enc);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
357 if(!content) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
358 return 1;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
359 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
360
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
361 while(read_pwdentry(p, content)) {}
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
362
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
363 cxBufferFree(content);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
364
839
23f3b02c6725 safe pwdstore decryption status
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 835
diff changeset
365 p->isdecrypted = 1;
23f3b02c6725 safe pwdstore decryption status
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 835
diff changeset
366
832
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
367 return 0;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
368 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
369
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
370 int pwdstore_setpassword(PwdStore *p, const char *password) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
371 DavKey *key = dav_pw2key(
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
372 password,
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
373 (unsigned char*)(p->content->space + 4),
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
374 16,
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
375 PWDS_PWFUNC(p),
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
376 PWDS_ENC(p));
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
377 if(!key) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
378 return 1;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
379 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
380
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
381 p->key = key;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
382 return 0;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
383 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
384
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
385 void pwdstore_encsettings(PwdStore *p, uint8_t enc, uint8_t pwfunc) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
386 PWDS_ENC(p) = enc;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
387 PWDS_PWFUNC(p) = pwfunc;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
388 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
389
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
390 void pwdstore_free_entry(PwdEntry *e) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
391 if(e->id) free(e->id);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
392 if(e->user) free(e->user);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
393 if(e->password) free(e->password);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
394 free(e);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
395 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
396
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
397 void pwdstore_free(PwdStore* p) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
398 cxDefineDestructor(p->ids, pwdstore_free_entry);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
399 cxMapDestroy(p->ids);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
400
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
401 cxListDestroy(p->locations);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
402
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
403 if(p->content) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
404 cxBufferFree(p->content);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
405 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
406
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
407 free(p);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
408 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
409
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
410 int pwdstore_has_id(PwdStore *s, const char *id) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
411 return cxMapGet(s->index, cx_hash_key_str(id)) ? 1 : 0;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
412 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
413
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
414 PwdEntry* pwdstore_get(PwdStore *p, const char *id) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
415 PwdEntry *e = cxMapGet(p->ids, cx_hash_key_str(id));
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
416 if(e && e->user && e->password) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
417 return e;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
418 } else {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
419 return NULL;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
420 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
421 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
422
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
423 void pwdstore_put(PwdStore *p, const char *id, const char *username, const char *password) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
424 PwdEntry *entry = malloc(sizeof(PwdEntry));
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
425 entry->id = strdup(id);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
426 entry->user = strdup(username);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
427 entry->password = strdup(password);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
428 cxMapPut(p->ids, cx_hash_key_str(id), entry);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
429 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
430
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
431 void pwdstore_put_index(PwdStore *p, char *id, CxList *locations) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
432 PwdIndexEntry *e = cxMapGet(p->index, cx_hash_key_str(id));
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
433 if(e) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
434 return;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
435 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
436 PwdIndexEntry *newentry = malloc(sizeof(PwdIndexEntry));
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
437 newentry->id = id;
841
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
438 if(locations && cxListSize(locations) > 0) {
832
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
439 newentry->locations = locations;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
440 cxListAdd(p->locations, newentry);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
441 } else {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
442 newentry->locations = NULL;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
443 cxListAdd(p->noloc, newentry);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
444 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
445 cxMapPut(p->index, cx_hash_key_str(id), newentry);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
446 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
447
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
448 void write_index_entry(CxBuffer *out, PwdIndexEntry *e) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
449 uint32_t idlen = strlen(e->id);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
450 uint32_t netidlen = htonl(idlen);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
451
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
452 cxBufferPut(out, 0); // type
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
453
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
454 cxBufferWrite(&netidlen, 1, sizeof(uint32_t), out);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
455 cxBufferWrite(e->id, 1, idlen, out);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
456
841
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
457 if(e->locations) {
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
458 CxIterator i = cxListIterator(e->locations);
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
459 cx_foreach(char *, location, i) {
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
460 uint32_t locationlen = strlen(location);
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
461 uint32_t netlocationlen = htonl(locationlen);
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
462
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
463 cxBufferWrite(&netlocationlen, 1, sizeof(uint32_t), out);
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
464 cxBufferWrite(location, 1, locationlen, out);
21403bdaf54c add pwdstore_clone function and fix some pwdstore bugs
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 840
diff changeset
465 }
832
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
466 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
467
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
468 uint32_t terminate = 0;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
469 cxBufferWrite(&terminate, 1, sizeof(uint32_t), out);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
470 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
471
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
472 int pwdstore_store(PwdStore *p, const char *file) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
473 if(!p->key) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
474 return 1;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
475 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
476
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
477 CxBuffer *index = cxBufferCreate(NULL, 2048, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
478 CxBuffer *content = cxBufferCreate(NULL, 2048, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
479
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
480 // create index
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
481 CxIterator i = cxListIterator(p->noloc);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
482 cx_foreach(PwdIndexEntry*, e, i) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
483 write_index_entry(index, e);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
484 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
485 i = cxListIterator(p->locations);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
486 cx_foreach(PwdIndexEntry*, e, i) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
487 write_index_entry(index, e);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
488 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
489
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
490 i = cxMapIteratorValues(p->ids);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
491 cx_foreach(PwdEntry*, value, i) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
492 if(!value->id || !value->user || !value->password) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
493 continue;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
494 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
495
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
496 uint32_t idlen = strlen(value->id);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
497 uint32_t ulen = strlen(value->user);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
498 uint32_t plen = strlen(value->password);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
499 uint32_t netidlen = htonl(idlen);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
500 uint32_t netulen = htonl(ulen);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
501 uint32_t netplen = htonl(plen);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
502
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
503 // content buffer
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
504 cxBufferPut(content, 0); // type
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
505
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
506 cxBufferWrite(&netidlen, 1, sizeof(uint32_t), content);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
507 cxBufferWrite(value->id, 1, idlen, content);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
508 cxBufferWrite(&netulen, 1, sizeof(uint32_t), content);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
509 cxBufferWrite(value->user, 1, ulen, content);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
510 cxBufferWrite(&netplen, 1, sizeof(uint32_t), content);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
511 cxBufferWrite(value->password, 1, plen, content);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
512 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
513
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
514 content->pos = 0;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
515 CxBuffer *enc = aes_encrypt_buffer(content, p->key);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
516
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
517 p->content->pos = PWDS_HEADER_SIZE - sizeof(uint32_t);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
518 p->content->size = PWDS_HEADER_SIZE;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
519
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
520 // add index after header
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
521 uint32_t netindexlen = htonl((uint32_t)index->size);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
522 cxBufferWrite(&netindexlen, 1, sizeof(uint32_t), p->content);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
523 cxBufferWrite(index->space, 1, index->size, p->content);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
524
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
525 // add encrypted buffer
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
526 cxBufferWrite(enc->space, 1, enc->size, p->content);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
527
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
528 cxBufferFree(enc);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
529
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
530 FILE *out = fopen(file, "w");
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
531 if(!out) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
532 return 1;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
533 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
534 fwrite(p->content->space, 1, p->content->size, out);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
535 fclose(out);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
536
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
537 return 0;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
538 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
539
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
540 int pwdstore_decrypt_secrets(PwdStore *secrets) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
541 if(!pw_input) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
542 return 1;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
543 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
544
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
545 char *ps_password = NULL;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
546 if(secrets->unlock_cmd && strlen(secrets->unlock_cmd) > 0) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
547 CxBuffer *cmd_out = cxBufferCreate(NULL, 128, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
548 if(!util_exec_command(secrets->unlock_cmd, cmd_out)) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
549 // command successful, get first line from output without newline
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
550 // and use that as password for the secretstore
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
551 size_t len = 0;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
552 for(size_t i=0;i<=cmd_out->size;i++) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
553 if(i == cmd_out->size || cmd_out->space[i] == '\n') {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
554 len = i;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
555 break;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
556 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
557 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
558 if(len > 0) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
559 ps_password = malloc(len + 1);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
560 memcpy(ps_password, cmd_out->space, len);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
561 ps_password[len] = 0;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
562 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
563 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
564 cxBufferFree(cmd_out);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
565 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
566
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
567 if(!ps_password) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
568 ps_password = pw_input(pw_input_data);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
569 if(!ps_password) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
570 return 1;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
571 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
572 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
573
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
574 int err = pwdstore_setpassword(secrets, ps_password);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
575 free(ps_password);
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
576 if(err) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
577 fprintf(stderr, "Error: cannot create key from password\n");
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
578 return 1;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
579 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
580 if(pwdstore_decrypt(secrets)) {
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
581 fprintf(stderr, "Error: cannot decrypt secrets store\n");
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
582 return 1;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
583 }
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
584 return 0;
dff5f4c23aa7 move pwdstore to libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
585 }

mercurial