libidav/pwdstore.c

Mon, 06 Jan 2025 22:22:55 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 06 Jan 2025 22:22:55 +0100
changeset 101
7b3a3130be44
parent 66
eee1f3092844
permissions
-rw-r--r--

update ucx, toolkit

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) {
101
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
227 cxListFree(locations);
65
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);
101
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
231 cxListFree(locations);
55
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);
101
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
288 PwdIndexEntry *i = NULL;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
289 cxMapRemoveAndGet(s->index, key, &i);
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
290 PwdEntry *e = NULL;
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
291 cxMapRemoveAndGet(s->ids, key, &e);
55
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
292
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293 if(i) {
65
48f43130b4a2 implement UI for credentials settings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
294 if(i->locations) {
101
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
295 cxListFree(i->locations);
65
48f43130b4a2 implement UI for credentials settings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
296 }
55
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 free(i->id);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298 free(i);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
299 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300 if(e) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
301 free(e->id);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302 free(e->user);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
303 free(e->password);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
304 free(e);
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 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
307
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
308 int pwdstore_getindex(PwdStore *s) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
309 uint32_t netindexlen;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
310
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
311 // 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
312 // for reading index length
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
313 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
314
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315 // 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
316 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
317 return 1;
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 uint32_t indexlen = ntohl(netindexlen);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
320
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
321 // integer overflow check
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
322 if(UINT32_MAX - PWDS_HEADER_SIZE < indexlen) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
323 return 1;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
324 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
325 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
326 return 1;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
327 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
328 // encrypted content starts after the index content
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
329 s->encoffset = PWDS_HEADER_SIZE + indexlen;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
330
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
331 // the index starts after the header
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
332 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
333 index->size = indexlen;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
334
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
335 // read index
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
336 while(read_indexentry(s, index)) {}
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
337
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338 // free index buffer structure (not the content)
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
339 cxBufferFree(index);
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 return 0;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 int pwdstore_decrypt(PwdStore *p) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
345 if(!p->key) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
346 return 1;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
348 if(p->isdecrypted) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
349 return 0;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
350 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
351
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
352 // decrypt contet
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
353 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
354 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
355 enc->size = encsz;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
356 enc->size = p->content->size - p->encoffset;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
357 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
358 cxBufferFree(enc);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
359 if(!content) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
360 return 1;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
361 }
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 while(read_pwdentry(p, content)) {}
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
364
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
365 cxBufferFree(content);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
366
65
48f43130b4a2 implement UI for credentials settings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
367 p->isdecrypted = 1;
48f43130b4a2 implement UI for credentials settings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
368
55
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
369 return 0;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
370 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
371
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
372 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
373 DavKey *key = dav_pw2key(
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
374 password,
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
375 (unsigned char*)(p->content->space + 4),
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
376 16,
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
377 PWDS_PWFUNC(p),
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
378 PWDS_ENC(p));
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
379 if(!key) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
380 return 1;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
381 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
382
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
383 p->key = key;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
384 return 0;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
385 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
386
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
387 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
388 PWDS_ENC(p) = enc;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
389 PWDS_PWFUNC(p) = pwfunc;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
390 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
391
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
392 void pwdstore_free_entry(PwdEntry *e) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
393 if(e->id) free(e->id);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
394 if(e->user) free(e->user);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
395 if(e->password) free(e->password);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
396 free(e);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
397 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
398
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
399 void pwdstore_free(PwdStore* p) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
400 cxDefineDestructor(p->ids, pwdstore_free_entry);
101
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
401 cxMapFree(p->ids);
55
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
402
101
7b3a3130be44 update ucx, toolkit
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 66
diff changeset
403 cxListFree(p->locations);
55
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
404
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
405 if(p->content) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
406 cxBufferFree(p->content);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
407 }
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 free(p);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
410 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
411
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
412 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
413 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
414 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
415
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
416 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
417 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
418 if(e && e->user && e->password) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
419 return e;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
420 } else {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
421 return NULL;
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 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
424
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
425 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
426 PwdEntry *entry = malloc(sizeof(PwdEntry));
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
427 entry->id = strdup(id);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
428 entry->user = strdup(username);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
429 entry->password = strdup(password);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
430 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
431 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
432
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
433 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
434 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
435 if(e) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
436 return;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
437 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
438 PwdIndexEntry *newentry = malloc(sizeof(PwdIndexEntry));
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
439 newentry->id = id;
65
48f43130b4a2 implement UI for credentials settings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
440 if(locations && cxListSize(locations) > 0) {
55
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
441 newentry->locations = locations;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
442 cxListAdd(p->locations, newentry);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
443 } else {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
444 newentry->locations = NULL;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
445 cxListAdd(p->noloc, 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 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
448 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
449
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
450 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
451 uint32_t idlen = strlen(e->id);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
452 uint32_t netidlen = htonl(idlen);
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 cxBufferPut(out, 0); // type
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
455
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
456 cxBufferWrite(&netidlen, 1, sizeof(uint32_t), out);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
457 cxBufferWrite(e->id, 1, idlen, out);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
458
65
48f43130b4a2 implement UI for credentials settings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
459 if(e->locations) {
48f43130b4a2 implement UI for credentials settings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
460 CxIterator i = cxListIterator(e->locations);
48f43130b4a2 implement UI for credentials settings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
461 cx_foreach(char *, location, i) {
48f43130b4a2 implement UI for credentials settings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
462 uint32_t locationlen = strlen(location);
48f43130b4a2 implement UI for credentials settings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
463 uint32_t netlocationlen = htonl(locationlen);
48f43130b4a2 implement UI for credentials settings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
464
48f43130b4a2 implement UI for credentials settings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
465 cxBufferWrite(&netlocationlen, 1, sizeof(uint32_t), out);
48f43130b4a2 implement UI for credentials settings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
466 cxBufferWrite(location, 1, locationlen, out);
48f43130b4a2 implement UI for credentials settings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 60
diff changeset
467 }
55
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
468 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
469
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
470 uint32_t terminate = 0;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
471 cxBufferWrite(&terminate, 1, sizeof(uint32_t), out);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
472 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
473
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
474 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
475 if(!p->key) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
476 return 1;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
477 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
478
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
479 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
480 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
481
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
482 // create index
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
483 CxIterator i = cxListIterator(p->noloc);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
484 cx_foreach(PwdIndexEntry*, e, i) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
485 write_index_entry(index, e);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
486 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
487 i = cxListIterator(p->locations);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
488 cx_foreach(PwdIndexEntry*, e, i) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
489 write_index_entry(index, e);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
490 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
491
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
492 i = cxMapIteratorValues(p->ids);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
493 cx_foreach(PwdEntry*, value, i) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
494 if(!value->id || !value->user || !value->password) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
495 continue;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
496 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
497
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
498 uint32_t idlen = strlen(value->id);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
499 uint32_t ulen = strlen(value->user);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
500 uint32_t plen = strlen(value->password);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
501 uint32_t netidlen = htonl(idlen);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
502 uint32_t netulen = htonl(ulen);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
503 uint32_t netplen = htonl(plen);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
504
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
505 // content buffer
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
506 cxBufferPut(content, 0); // type
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
507
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
508 cxBufferWrite(&netidlen, 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->id, 1, idlen, content);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
510 cxBufferWrite(&netulen, 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->user, 1, ulen, content);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
512 cxBufferWrite(&netplen, 1, sizeof(uint32_t), content);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
513 cxBufferWrite(value->password, 1, plen, content);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
514 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
515
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
516 content->pos = 0;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
517 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
518
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
519 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
520 p->content->size = PWDS_HEADER_SIZE;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
521
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
522 // add index after header
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
523 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
524 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
525 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
526
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
527 // add encrypted buffer
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
528 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
529
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
530 cxBufferFree(enc);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
531
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
532 FILE *out = fopen(file, "w");
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
533 if(!out) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
534 return 1;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
535 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
536 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
537 fclose(out);
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 return 0;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
540 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
541
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
542 int pwdstore_decrypt_secrets(PwdStore *secrets) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
543 if(!pw_input) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
544 return 1;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
545 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
546
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
547 char *ps_password = NULL;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
548 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
549 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
550 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
551 // 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
552 // 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
553 size_t len = 0;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
554 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
555 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
556 len = i;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
557 break;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
558 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
559 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
560 if(len > 0) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
561 ps_password = malloc(len + 1);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
562 memcpy(ps_password, cmd_out->space, len);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
563 ps_password[len] = 0;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
564 }
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 cxBufferFree(cmd_out);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
567 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
568
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 ps_password = pw_input(pw_input_data);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
571 if(!ps_password) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
572 return 1;
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 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
575
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
576 int err = pwdstore_setpassword(secrets, ps_password);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
577 free(ps_password);
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
578 if(err) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
579 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
580 return 1;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
581 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
582 if(pwdstore_decrypt(secrets)) {
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
583 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
584 return 1;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
585 }
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
586 return 0;
1ce14068ef31 update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
587 }

mercurial