application/pwd.c

Mon, 21 Oct 2024 13:22:45 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 21 Oct 2024 13:22:45 +0200
changeset 53
da05df77652e
parent 49
2f71f4ee247a
permissions
-rw-r--r--

add menu items to open resource properties

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/utils.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/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
37
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38 #ifdef _WIN32
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 #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
40 #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
41 #else
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 #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
43 #endif
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 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
46 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
47 if(!in) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 return NULL;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 }
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 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
52 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
53 fclose(in);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 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
56 cxBufferFree(buf);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 return NULL;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 }
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 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
61 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
62 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
63 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
64 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
65 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
66 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
67 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
68 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
69 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
70 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
71
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 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
73 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
74 return NULL;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 }
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 return p;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 }
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 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
81 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
82 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
83 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
84 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
85 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
86 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
87 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
88 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
89 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
90 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
91 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
92 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
93 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
94 return p;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 }
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 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
98 // 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
99 // length = uint32
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 // string = bytes
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 *val = NULL;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 // get length
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 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
106 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
107 return 0;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 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
110 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
111 if(allowzero) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112 return 1;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 } else {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 return 0;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 }
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 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
118 return 0;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 }
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 // get value
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 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
123 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
124 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
125 free(value);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126 return 0;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 }
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 *val = value;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 return 1;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 }
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 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
134 // 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
135 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
136 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
137 // 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
138 return 0;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 }
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 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
142 CxList *locations = cxLinkedListCreateSimple(CX_STORE_POINTERS);
49
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
143 cxDefineDestructor(locations, free);
6
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 // 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
146 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
147 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
148 // get locations
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 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
150 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
151 if(!location) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 break;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 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
155 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 }
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 if(ret) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 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
160 } else {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 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
162 cxListDestroy(locations);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 return ret;
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
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
168 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
169 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
170 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
171 // 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
172 return 0;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 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
176 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
177 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
178
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 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
180 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
181 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
182 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
183 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
184 ret = 1;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 }
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 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
190 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
191 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
192
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 return ret;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195
49
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
196 static void remove_list_entries(PwdStore *s, const char *id) {
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
197 CxIterator i = cxListMutIterator(s->locations);
6
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 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
199 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
200 cxIteratorFlagRemoval(i);
49
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
201 cxIteratorNext(i);
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
202 break;
6
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 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
206 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
207 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
208 cxIteratorFlagRemoval(i);
49
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
209 cxIteratorNext(i);
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
210 break;
6
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 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 void pwdstore_remove_entry(PwdStore *s, const char *id) {
49
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
216 remove_list_entries(s, id);
6
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 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
219 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
220 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
221
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 if(i) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 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
224 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
225 free(i);
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 if(e) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 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
229 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
230 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
231 free(e);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 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
236 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
237
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 // 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
239 // 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
240 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
241
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 // 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
243 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
244 return 1;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246 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
247
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 // 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
249 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
250 return 1;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 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
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 // 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
256 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
257
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
258 // 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
259 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
260 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
261
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 // read index
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263 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
264
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
265 // 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
266 cxBufferFree(index);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
267
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
268 return 0;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
269 }
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 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
272 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
273 return 1;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
274 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
275 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
276 return 0;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
277 }
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 // decrypt contet
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
280 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
281 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
282 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
283 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
284 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
285 cxBufferFree(enc);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
286 if(!content) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
287 return 1;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
288 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
290 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
291
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
292 cxBufferFree(content);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
294 return 0;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
295 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
296
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 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
298 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
299 password,
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300 (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
301 16,
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302 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
303 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
304 if(!key) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
305 return 1;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
306 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
307
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
308 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
309 return 0;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
310 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
311
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312 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
313 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
314 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
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 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
318 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
319 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
320 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
321 free(e);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
322 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
323
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
324 void pwdstore_free(PwdStore* p) {
49
2f71f4ee247a update toolkit, ucx, libidav
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
325 cxDefineDestructor(p->ids, pwdstore_free_entry);
6
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
326 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
327
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
328 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
329
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
330 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
331 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
332 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
333
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
334 free(p);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
335 }
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 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
338 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
339 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341 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
342 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
343 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
344 return e;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
345 } else {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
346 return NULL;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347 }
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 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
351 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
352 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
353 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
354 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
355 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
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 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
359 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
360 if(e) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
361 return;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
362 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
363 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
364 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
365 if(locations) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
366 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
367 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
368 } else {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
369 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
370 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
371 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
372 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
373 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
374
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
375 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
376 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
377 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
378
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
379 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
380
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
381 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
382 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
383
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
384 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
385 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
386 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
387 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
388
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
389 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
390 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
391 }
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 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
394 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
395 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
396
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
397 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
398 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
399 return 1;
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 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
403 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
404
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
405 // create index
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
406 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
407 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
408 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
409 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
410 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
411 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
412 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
413 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
414
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
415 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
416 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
417 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
418 continue;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
419 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
420
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
421 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
422 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
423 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
424 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
425 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
426 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
427
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
428 // content buffer
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
429 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
430
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
431 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
432 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
433 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
434 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
435 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
436 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
437 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
438
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
439 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
440 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
441
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
442 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
443 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
444
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
445 // 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
446 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
447 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
448 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
449
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
450 // 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
451 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
452
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
453 cxBufferFree(enc);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
454
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
455 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
456 if(!out) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
457 return 1;
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 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
460 fclose(out);
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 return 0;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
463 }

mercurial