application/pwd.c

Fri, 14 Jun 2024 18:05:41 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 14 Jun 2024 18:05:41 +0200
changeset 40
af5c2bfe0f21
parent 6
09ac07345656
child 49
2f71f4ee247a
permissions
-rw-r--r--

add space between nav buttons and path textfield

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

mercurial