libidav/pwdstore.c

Thu, 14 Nov 2024 17:25:23 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 14 Nov 2024 17:25:23 +0100
changeset 83
a612adaee43d
parent 66
eee1f3092844
permissions
-rw-r--r--

fix pathbar elmfunc creating an element for an empty string

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

mercurial