application/config.c

Thu, 08 Feb 2024 10:31:19 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 08 Feb 2024 10:31:19 +0100
changeset 24
12ad3393c151
parent 7
905ac52c910f
child 49
2f71f4ee247a
permissions
-rw-r--r--

don't show number of uploading files when there is only 1 file

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 2024 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 #include <sys/types.h>
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33 #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
34 #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
35 #include <errno.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 <libxml/tree.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 #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
39 #include "config.h"
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40 #include "system.h"
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 #include <libidav/utils.h>
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 #include <libidav/config.h>
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 #define xstreq(a,b) xmlStrEqual(BAD_CAST a, BAD_CAST b)
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
46 #define xstrEQ(a,b) !xmlStrcasecmp(BAD_CAST a, BAD_CAST b)
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 #define print_error(lineno, ...) \
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 do {\
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 fprintf(stderr, "Error (config.xml line %u): ", lineno); \
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51 fprintf(stderr, __VA_ARGS__); \
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52 fprintf(stderr, "Abort.\n"); \
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 } while(0);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 #define print_warning(lineno, ...) \
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 do {\
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56 fprintf(stderr, "Warning (config.xml line %u): ", lineno); \
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 fprintf(stderr, __VA_ARGS__); \
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 } while(0);
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 #ifdef _WIN32
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61 #define ENV_HOME getenv("USERPROFILE")
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 #else
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 #define ENV_HOME getenv("HOME")
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 #endif /* _WIN32 */
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 static CxMap* repos;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 static CxMap* keys;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 static DavConfig* davconfig;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 static PwdStore* pstore;
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 static char* secretstore_unlock_cmd;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 static char* secretstore_lock_cmd;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 int check_config_dir(void) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 char* file = util_concat_path(ENV_HOME, ".dav");
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77 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
78 if (util_mkdir(file, S_IRWXU)) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 if (errno != EEXIST) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 ret = 1;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 free(file);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 return ret;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 static DavContext* context;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 void create_default_config(char* file) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 xmlDoc* doc = xmlNewDoc(BAD_CAST "1.0");
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 xmlNode* root = xmlNewNode(NULL, BAD_CAST "configuration");
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 xmlDocSetRootElement(doc, root);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 xmlSaveFormatFileEnc(file, doc, "UTF-8", 1);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 xmlFreeDoc(doc);
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 char* config_file_path(char* name) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 char* davd = util_concat_path(ENV_HOME, ".dav");
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 if (!davd) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 return NULL;
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 char* path = util_concat_path(davd, name);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 free(davd);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 return path;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 cxmutstr config_load_file(const char* path) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 FILE* file = sys_fopen(path, "r");
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 if (!file) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 return (cxmutstr) { NULL, 0 };
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 CxBuffer buf;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 cxBufferInit(&buf, NULL, 1024, cxDefaultAllocator, 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
115 cx_stream_copy(file, &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
116 fclose(file);
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 return cx_mutstrn(buf.space, buf.size);
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 int load_config(DavContext* ctx) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 context = ctx;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 // TODO: free the config somewhere
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 repos = 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
125 keys = 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
126
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 char* pwfile = util_concat_path(ENV_HOME, ".dav/secrets.crypt");
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 pstore = pwdstore_open(pwfile);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129 free(pwfile);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 char* file = util_concat_path(ENV_HOME, ".dav/config.xml");
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 struct stat s;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 if (stat(file, &s)) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 switch (errno) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 case ENOENT: {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 return 0;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 default: {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 perror("Cannot load config.xml");
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 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 return 1;
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
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 cxmutstr config_content = config_load_file(file);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 int config_error;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 davconfig = dav_config_load(config_content, &config_error);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 free(config_content.ptr);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 free(file);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 if (!davconfig) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153 fprintf(stderr, "Cannot load config.xml\n");
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 return 1;
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 return dav_config_register_keys(davconfig, ctx, load_key_file);
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 DavConfig* get_config(void) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 return davconfig;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 }
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 int store_config(void) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165 if (check_config_dir()) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
166 return 1;
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
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
169 CxBuffer* buf = dav_config2buf(davconfig);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
170 if (!buf) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
171 return 1;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
172 }
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 char* file = util_concat_path(ENV_HOME, ".dav/config.xml");
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 FILE* cout = sys_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
176 if (!cout) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 cxBufferFree(buf);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178 return 1;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 // should only fail if we run out of disk space or something like that
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 // in that case, the config file is only destroyed
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183 // could only be prevented, if we write to a temp file first and than
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 // rename it
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 fwrite(buf->space, buf->size, 1, cout);
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 cxBufferFree(buf);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 fclose(cout);
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 return 0;
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
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 void free_config(void) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 if (davconfig) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 dav_config_free(davconfig);
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 }
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 cxmutstr load_key_file(const char* filename) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 cxmutstr k;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201 k.ptr = NULL;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 k.length = 0;
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 FILE* file = NULL;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 if (filename[0] == '/') {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 file = sys_fopen(filename, "r");
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 else {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 char* path = util_concat_path(ENV_HOME, ".dav/");
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 char* p2 = util_concat_path(path, filename);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 file = sys_fopen(p2, "r");
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 free(path);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 free(p2);
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
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 if (!file) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 fprintf(stderr, "Error: cannot load keyfile %s\n", filename);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 return k;
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 char* data = malloc(256);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 size_t r = fread(data, 1, 256, file);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 k.ptr = data;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 k.length = r;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 fclose(file);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227 return k;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230 static char* get_attr_content(xmlNode* node) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 // TODO: remove code duplication (util_xml_get_text)
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232 while (node) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 if (node->type == XML_TEXT_NODE) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 return (char*)node->content;
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 node = node->next;
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 return NULL;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241 int load_namespace(const xmlNode* node) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 const char* prefix = NULL;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243 const char* uri = NULL;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 xmlAttr* attr = node->properties;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246 while (attr) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247 if (attr->type == XML_ATTRIBUTE_NODE) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248 char* value = get_attr_content(attr->children);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 if (!value) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250 print_error(
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 node->line,
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252 "missing value for attribute %s\n", (char*)attr->name);
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 if (xstreq(attr->name, "prefix")) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 prefix = value;
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 else if (xstreq(attr->name, "uri")) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
259 uri = value;
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 else {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 print_error(
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263 node->line,
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
264 "unexpected attribute %s\n", (char*)attr->name);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
265 return 1;
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 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
268 attr = attr->next;
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 if (!prefix) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272 print_error(node->line, "missing prefix attribute\n");
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 (!uri) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
276 print_error(node->line, "missing uri attribute\n");
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
277 return 1;
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 if (dav_get_namespace(context, prefix)) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
281 print_error(node->line, "namespace prefix '%s' already used\n", prefix);
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
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
285 return dav_add_namespace(context, prefix, uri);
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 int load_secretstore(const xmlNode* node) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289 // currently only one secretstore is supported
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
290
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
291 if (!pstore) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
292 return 0;
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
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
295 node = node->children;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
296 int error = 0;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 while (node) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298 if (node->type == XML_ELEMENT_NODE) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
299 char* value = util_xml_get_text(node);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300 if (value) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
301 if (xstreq(node->name, "unlock-command")) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302 pstore->unlock_cmd = strdup(value);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
303 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
304 else if (xstreq(node->name, "lock-command")) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
305 pstore->lock_cmd = strdup(value);
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 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
309 node = node->next;
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 return error;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
313 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
314
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315 PwdStore* get_pwdstore(void) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
316 return pstore;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
317 }
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
318
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
319 int pwdstore_save(PwdStore* pwdstore) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
320 if (check_config_dir()) {
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
321 return 1;
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 char* pwfile = util_concat_path(ENV_HOME, ".dav/secrets.crypt");
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
325 int ret = pwdstore_store(pwdstore, pwfile);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
326 free(pwfile);
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
327 return ret;
09ac07345656 add config related code from dav / load config and fill repo list
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
328 }
7
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
329
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
330
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
331
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
332 static int decrypt_secrets(PwdStore *secrets) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
333 char *ps_password = NULL;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
334 if(secrets->unlock_cmd && strlen(secrets->unlock_cmd) > 0) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
335 CxBuffer *cmd_out = cxBufferCreate(NULL, 128, cxDefaultAllocator, CX_BUFFER_FREE_CONTENTS|CX_BUFFER_AUTO_EXTEND);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
336 if(!util_exec_command(secrets->unlock_cmd, cmd_out)) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
337 // command successful, get first line from output without newline
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
338 // and use that as password for the secretstore
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
339 size_t len = 0;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
340 for(size_t i=0;i<=cmd_out->size;i++) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
341 if(i == cmd_out->size || cmd_out->space[i] == '\n') {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
342 len = i;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
343 break;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
344 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
345 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
346 if(len > 0) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
347 ps_password = malloc(len + 1);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
348 memcpy(ps_password, cmd_out->space, len);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
349 ps_password[len] = 0;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
350 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
351 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
352 cxBufferFree(cmd_out);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
353 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
354
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
355 return 1;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
356 // TODO
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
357 /*
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
358 if(!ps_password) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
359 ps_password = util_password_input("Master password: ");
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
360 if(!ps_password) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
361 return 1;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
362 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
363 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
364
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
365 if(pwdstore_setpassword(secrets, ps_password)) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
366 fprintf(stderr, "Error: cannot create key from password\n");
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
367 return 1;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
368 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
369 if(pwdstore_decrypt(secrets)) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
370 fprintf(stderr, "Error: cannot decrypt secrets store\n");
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
371 return 1;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
372 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
373 return 0;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
374 */
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
375 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
376
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
377
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
378 typedef struct CredLocation {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
379 char *id;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
380 char *location;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
381 } CredLocation;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
382
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
383 static int cmp_url_cred_entry(CredLocation *e1, CredLocation *e2, void *n) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
384 return strcmp(e2->location, e1->location);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
385 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
386
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
387 static void free_cred_location(CredLocation *c) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
388 // c->id is not a copy, therefore we don't have to free it
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
389 free(c->location);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
390 free(c);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
391 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
392
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
393 static int get_stored_credentials(char *credid, char **user, char **password) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
394 return 0;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
395 // TODO
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
396 /*
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
397 if(!credid) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
398 return 0;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
399 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
400 PwdStore *secrets = get_pwdstore();
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
401 if(!secrets) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
402 fprintf(stderr, "Error: no secrets store available\n");
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
403 return 0;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
404 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
405
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
406 if(pwdstore_has_id(secrets, credid)) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
407 if(!secrets->isdecrypted) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
408 if(decrypt_secrets(a, secrets)) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
409 return 0;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
410 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
411 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
412
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
413 PwdEntry *s_cred = pwdstore_get(secrets, credid);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
414 if(s_cred) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
415 *user = s_cred->user;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
416 *password = s_cred->password;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
417 return 1;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
418 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
419 } else {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
420 fprintf(stderr, "Error: credentials id '%s' not found\n", credid);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
421 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
422
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
423 return 0;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
424 */
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
425 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
426
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
427
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
428 static int get_location_credentials(DavCfgRepository *repo, const char *path, char **user, char **password) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
429 PwdStore *secrets = get_pwdstore();
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
430 if(!secrets) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
431 return 0;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
432 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
433
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
434 /*
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
435 * The list secrets->location contains urls or repo names as
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
436 * location strings. We need a list, that contains only urls
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
437 */
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
438 CxList *locations = cxLinkedListCreate(cxDefaultAllocator, (cx_compare_func)cmp_url_cred_entry, CX_STORE_POINTERS);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
439 locations->simple_destructor = (cx_destructor_func)free_cred_location;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
440 CxIterator i = cxListIterator(secrets->locations);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
441 cx_foreach(PwdIndexEntry*, e, i) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
442 CxIterator entry_iter = cxListIterator(e->locations);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
443 cx_foreach(char *, loc, entry_iter) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
444 cxmutstr rpath;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
445 DavCfgRepository *r = dav_config_url2repo_s(davconfig, cx_str(loc), &rpath);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
446 CredLocation *urlentry = calloc(1, sizeof(CredLocation));
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
447 urlentry->id = e->id;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
448 urlentry->location = util_concat_path_s(cx_strcast(r->url.value), cx_strcast(rpath)).ptr;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
449 cxListAdd(locations, urlentry);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
450 free(rpath.ptr);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
451 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
452 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
453 // the list must be sorted
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
454 cxListSort(locations);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
455
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
456 // create full request url string and remove protocol prefix
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
457 cxmutstr req_url_proto = util_concat_path_s(cx_strcast(repo->url.value), cx_str(path));
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
458 cxstring req_url = cx_strcast(req_url_proto);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
459 if(cx_strprefix(req_url, CX_STR("http://"))) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
460 req_url = cx_strsubs(req_url, 7);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
461 } else if(cx_strprefix(req_url, CX_STR("https://"))) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
462 req_url = cx_strsubs(req_url, 8);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
463 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
464
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
465 // iterate over sorted locations and check if a location is a prefix
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
466 // of the requested url
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
467 char *id = NULL;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
468 int ret = 0;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
469 i = cxListIterator(locations);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
470 cx_foreach(CredLocation*, cred, i) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
471 cxstring cred_url = cx_str(cred->location);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
472
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
473 // remove protocol prefix
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
474 if(cx_strprefix(cred_url, CX_STR("http://"))) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
475 cred_url = cx_strsubs(cred_url, 7);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
476 } else if(cx_strprefix(cred_url, CX_STR("https://"))) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
477 cred_url = cx_strsubs(cred_url, 8);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
478 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
479
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
480 if(cx_strprefix(req_url, cred_url)) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
481 id = cred->id;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
482 break;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
483 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
484 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
485
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
486 // if an id is found and we can access the decrypted secret store
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
487 // we can set the user/password
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
488 if(id && (secrets->isdecrypted || !decrypt_secrets(secrets))) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
489 PwdEntry *cred = pwdstore_get(secrets, id);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
490 if(cred) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
491 *user = cred->user;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
492 *password = cred->password;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
493 ret = 1;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
494 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
495 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
496
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
497 free(req_url_proto.ptr);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
498 cxListDestroy(locations);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
499
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
500 return ret;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
501 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
502
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
503
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
504 DavSession* connect_to_repo(DavContext *ctx, DavCfgRepository *repo, const char *path, dav_auth_func authfunc) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
505 cxmutstr decodedpw = dav_repository_get_decodedpassword(repo);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
506
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
507 char *user = repo->user.value.ptr;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
508 char *password = decodedpw.ptr;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
509
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
510 if(!user && !password) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
511 if(!get_stored_credentials(repo->stored_user.value.ptr, &user, &password)) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
512 get_location_credentials(repo, path, &user, &password);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
513 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
514 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
515
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
516 DavSession *sn = dav_session_new_auth(ctx, repo->url.value.ptr, user, password);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
517 if(password) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
518 free(password);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
519 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
520
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
521 sn->flags = dav_repository_get_flags(repo);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
522 sn->key = dav_context_get_key(ctx, repo->default_key.value.ptr);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
523 curl_easy_setopt(sn->handle, CURLOPT_HTTPAUTH, repo->authmethods);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
524 curl_easy_setopt(sn->handle, CURLOPT_SSLVERSION, repo->ssl_version);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
525 if(repo->cert.value.ptr) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
526 curl_easy_setopt(sn->handle, CURLOPT_CAINFO, repo->cert.value.ptr);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
527 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
528 if(!repo->verification.value) {
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
529 curl_easy_setopt(sn->handle, CURLOPT_SSL_VERIFYPEER, 0);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
530 curl_easy_setopt(sn->handle, CURLOPT_SSL_VERIFYHOST, 0);
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
531 }
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
532
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
533 return sn;
905ac52c910f port more dav config code
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 6
diff changeset
534 }

mercurial