Tue, 29 Oct 2024 19:07:11 +0100
implement repo Generate Key button
55
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
1 | /* |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
2 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
3 | * |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
4 | * Copyright 2018 Olaf Wintermann. All rights reserved. |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
5 | * |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
6 | * Redistribution and use in source and binary forms, with or without |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
7 | * modification, are permitted provided that the following conditions are met: |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
8 | * |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
9 | * 1. Redistributions of source code must retain the above copyright |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
10 | * notice, this list of conditions and the following disclaimer. |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
11 | * |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
12 | * 2. Redistributions in binary form must reproduce the above copyright |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
13 | * notice, this list of conditions and the following disclaimer in the |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
14 | * documentation and/or other materials provided with the distribution. |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
15 | * |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
16 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
17 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
18 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
19 | * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
20 | * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
21 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
22 | * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
23 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
24 | * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
25 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
26 | * POSSIBILITY OF SUCH DAMAGE. |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
27 | */ |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
28 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
29 | #include <stdio.h> |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
30 | #include <stdlib.h> |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
31 | #include <string.h> |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
32 | #include <sys/types.h> |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
33 | #include <cx/hash_map.h> |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
34 | #include <cx/utils.h> |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
35 | #include <errno.h> |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
36 | #include <libxml/tree.h> |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
37 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
38 | #include "config.h" |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
39 | #include "pwd.h" |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
40 | #include "system.h" |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
41 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
42 | #include <libidav/utils.h> |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
43 | #include <libidav/config.h> |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
44 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
45 | #define xstreq(a,b) xmlStrEqual(BAD_CAST a, BAD_CAST b) |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
46 | #define xstrEQ(a,b) !xmlStrcasecmp(BAD_CAST a, BAD_CAST b) |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
47 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
48 | #define print_error(lineno, ...) \ |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
49 | do {\ |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
50 | fprintf(stderr, "Error (config.xml line %u): ", lineno); \ |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
51 | fprintf(stderr, __VA_ARGS__); \ |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
52 | fprintf(stderr, "Abort.\n"); \ |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
53 | } while(0); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
54 | #define print_warning(lineno, ...) \ |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
55 | do {\ |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
56 | fprintf(stderr, "Warning (config.xml line %u): ", lineno); \ |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
57 | fprintf(stderr, __VA_ARGS__); \ |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
58 | } while(0); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
59 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
60 | #ifdef _WIN32 |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
61 | #define ENV_HOME getenv("USERPROFILE") |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
62 | #else |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
63 | #define ENV_HOME getenv("HOME") |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
64 | #endif /* _WIN32 */ |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
65 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
66 | static CxMap *repos; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
67 | static CxMap *keys; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
68 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
69 | static DavConfig *davconfig; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
70 | static PwdStore *pstore; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
71 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
72 | static char *secretstore_unlock_cmd; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
73 | static char *secretstore_lock_cmd; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
74 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
75 | int check_config_dir(void) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
76 | char *file = util_concat_path(ENV_HOME, ".dav"); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
77 | int ret = 0; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
78 | if(util_mkdir(file, S_IRWXU)) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
79 | if(errno != EEXIST) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
80 | ret = 1; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
81 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
82 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
83 | free(file); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
84 | return ret; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
85 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
86 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
87 | static DavContext *context; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
88 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
89 | void create_default_config(char *file) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
90 | xmlDoc *doc = xmlNewDoc(BAD_CAST "1.0"); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
91 | xmlNode *root = xmlNewNode(NULL, BAD_CAST "configuration"); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
92 | xmlDocSetRootElement(doc, root); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
93 | xmlSaveFormatFileEnc(file, doc, "UTF-8", 1); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
94 | xmlFreeDoc(doc); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
95 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
96 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
97 | char* config_file_path(char *name) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
98 | char *davd = util_concat_path(ENV_HOME, ".dav"); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
99 | if(!davd) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
100 | return NULL; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
101 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
102 | char *path = util_concat_path(davd, name); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
103 | free(davd); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
104 | return path; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
105 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
106 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
107 | cxmutstr config_load_file(const char *path) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
108 | FILE *file = sys_fopen(path, "r"); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
109 | if(!file) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
110 | return (cxmutstr){NULL,0}; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
111 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
112 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
113 | CxBuffer buf; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
114 | cxBufferInit(&buf, NULL, 1024, cxDefaultAllocator, CX_BUFFER_AUTO_EXTEND); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
115 | cx_stream_copy(file, &buf, (cx_read_func)fread, (cx_write_func)cxBufferWrite); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
116 | fclose(file); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
117 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
118 | return cx_mutstrn(buf.space, buf.size); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
119 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
120 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
121 | int load_config(DavContext *ctx) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
122 | context = ctx; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
123 | // TODO: free the config somewhere |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
124 | repos = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
125 | keys = cxHashMapCreate(cxDefaultAllocator, CX_STORE_POINTERS, 16); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
126 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
127 | char *pwfile = util_concat_path(ENV_HOME, ".dav/secrets.crypt"); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
128 | pstore = pwdstore_open(pwfile); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
129 | free(pwfile); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
130 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
131 | char *file = util_concat_path(ENV_HOME, ".dav/config.xml"); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
132 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
133 | struct stat s; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
134 | if(stat(file, &s)) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
135 | switch(errno) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
136 | case ENOENT: { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
137 | davconfig = dav_config_new(NULL); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
138 | return 0; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
139 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
140 | default: { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
141 | perror("Cannot load config.xml"); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
142 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
143 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
144 | return 1; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
145 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
146 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
147 | cxmutstr config_content = config_load_file(file); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
148 | int config_error; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
149 | davconfig = dav_config_load(config_content, &config_error); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
150 | free(config_content.ptr); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
151 | free(file); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
152 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
153 | if(!davconfig) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
154 | fprintf(stderr, "Cannot load config.xml\n"); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
155 | return 1; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
156 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
157 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
158 | if(dav_config_register_namespaces(davconfig, ctx)) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
159 | return 1; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
160 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
161 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
162 | return dav_config_register_keys(davconfig, ctx, load_key_file); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
163 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
164 | |
60
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
165 | DavConfig* load_config_file(void) { |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
166 | char *file = util_concat_path(ENV_HOME, ".dav/config.xml"); |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
167 | |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
168 | struct stat s; |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
169 | if(stat(file, &s)) { |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
170 | switch(errno) { |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
171 | case ENOENT: { |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
172 | davconfig = dav_config_new(NULL); |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
173 | return NULL; |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
174 | } |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
175 | default: { |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
176 | perror("Cannot load config.xml"); |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
177 | } |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
178 | } |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
179 | return NULL; |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
180 | } |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
181 | |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
182 | cxmutstr config_content = config_load_file(file); |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
183 | int config_error; |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
184 | DavConfig *cfg = dav_config_load(config_content, &config_error); |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
185 | free(config_content.ptr); |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
186 | free(file); |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
187 | return cfg; |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
188 | } |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
189 | |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
190 | |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
191 | void set_config(DavConfig *cfg) { |
72
d5307e9ee384
implement key management UI
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
192 | if(davconfig) { |
d5307e9ee384
implement key management UI
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
193 | dav_config_free(davconfig); |
d5307e9ee384
implement key management UI
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
194 | } |
60
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
195 | davconfig = cfg; |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
196 | } |
ee4e4742391e
add settings window and implement repository editing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
55
diff
changeset
|
197 | |
55
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
198 | DavConfig* get_config(void) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
199 | return davconfig; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
200 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
201 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
202 | int store_config(void) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
203 | if(check_config_dir()) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
204 | return 1; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
205 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
206 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
207 | CxBuffer *buf = dav_config2buf(davconfig); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
208 | if(!buf) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
209 | return 1; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
210 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
211 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
212 | char *file = util_concat_path(ENV_HOME, ".dav/config.xml"); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
213 | FILE *cout = sys_fopen(file, "w"); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
214 | if(!cout) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
215 | cxBufferFree(buf); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
216 | return 1; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
217 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
218 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
219 | // should only fail if we run out of disk space or something like that |
72
d5307e9ee384
implement key management UI
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
65
diff
changeset
|
220 | // in that case, the config file is destroyed |
55
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
221 | // could only be prevented, if we write to a temp file first and than |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
222 | // rename it |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
223 | fwrite(buf->space, buf->size, 1, cout); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
224 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
225 | cxBufferFree(buf); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
226 | fclose(cout); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
227 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
228 | return 0; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
229 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
230 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
231 | void free_config(void) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
232 | if(davconfig) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
233 | dav_config_free(davconfig); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
234 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
235 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
236 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
237 | cxmutstr load_key_file(const char *filename) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
238 | cxmutstr k; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
239 | k.ptr = NULL; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
240 | k.length = 0; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
241 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
242 | FILE *file = NULL; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
243 | if(filename[0] == '/') { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
244 | file = sys_fopen(filename, "r"); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
245 | } else { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
246 | char *path = util_concat_path(ENV_HOME, ".dav/"); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
247 | char *p2 = util_concat_path(path, filename); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
248 | file = sys_fopen(p2, "r"); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
249 | free(path); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
250 | free(p2); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
251 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
252 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
253 | if(!file) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
254 | fprintf(stderr, "Error: cannot load keyfile %s\n", filename); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
255 | return k; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
256 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
257 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
258 | char *data = malloc(256); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
259 | size_t r = fread(data, 1, 256, file); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
260 | k.ptr = data; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
261 | k.length = r; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
262 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
263 | fclose(file); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
264 | return k; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
265 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
266 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
267 | PwdStore* get_pwdstore(void) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
268 | return pstore; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
269 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
270 | |
65
48f43130b4a2
implement UI for credentials settings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
60
diff
changeset
|
271 | void set_pwdstore(PwdStore *newstore) { |
48f43130b4a2
implement UI for credentials settings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
60
diff
changeset
|
272 | if(pstore) { |
48f43130b4a2
implement UI for credentials settings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
60
diff
changeset
|
273 | // TODO: free |
48f43130b4a2
implement UI for credentials settings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
60
diff
changeset
|
274 | } |
48f43130b4a2
implement UI for credentials settings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
60
diff
changeset
|
275 | pstore = newstore; |
48f43130b4a2
implement UI for credentials settings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
60
diff
changeset
|
276 | } |
48f43130b4a2
implement UI for credentials settings
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
60
diff
changeset
|
277 | |
55
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
278 | int pwdstore_save(PwdStore *pwdstore) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
279 | if(check_config_dir()) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
280 | return 1; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
281 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
282 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
283 | char *pwfile = util_concat_path(ENV_HOME, ".dav/secrets.crypt"); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
284 | int ret = pwdstore_store(pwdstore, pwfile); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
285 | free(pwfile); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
286 | return ret; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
287 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
288 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
289 | typedef struct CredLocation { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
290 | char *id; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
291 | char *location; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
292 | } CredLocation; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
293 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
294 | static int cmp_url_cred_entry(CredLocation *e1, CredLocation *e2, void *n) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
295 | return strcmp(e2->location, e1->location); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
296 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
297 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
298 | static void free_cred_location(CredLocation *c) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
299 | // c->id is not a copy, therefore we don't have to free it |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
300 | free(c->location); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
301 | free(c); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
302 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
303 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
304 | int get_stored_credentials(char *credid, char **user, char **password) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
305 | if(!credid) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
306 | return 0; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
307 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
308 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
309 | PwdStore *secrets = get_pwdstore(); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
310 | if(!secrets) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
311 | fprintf(stderr, "Error: no secrets store available\n"); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
312 | return 0; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
313 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
314 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
315 | if(pwdstore_has_id(secrets, credid)) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
316 | if(!secrets->isdecrypted) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
317 | if(pwdstore_decrypt_secrets(secrets)) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
318 | return 0; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
319 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
320 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
321 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
322 | PwdEntry *s_cred = pwdstore_get(secrets, credid); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
323 | if(s_cred) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
324 | *user = s_cred->user; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
325 | *password = s_cred->password; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
326 | return 1; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
327 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
328 | } else { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
329 | fprintf(stderr, "Error: credentials id '%s' not found\n", credid); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
330 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
331 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
332 | return 0; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
333 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
334 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
335 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
336 | int get_location_credentials(DavCfgRepository *repo, const char *path, char **user, char **password) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
337 | PwdStore *secrets = get_pwdstore(); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
338 | if(!secrets) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
339 | return 0; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
340 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
341 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
342 | /* |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
343 | * The list secrets->location contains urls or repo names as |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
344 | * location strings. We need a list, that contains only urls |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
345 | */ |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
346 | CxList *locations = cxLinkedListCreate(cxDefaultAllocator, (cx_compare_func)cmp_url_cred_entry, CX_STORE_POINTERS); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
347 | cxDefineDestructor(locations, free_cred_location); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
348 | CxIterator i = cxListIterator(secrets->locations); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
349 | cx_foreach(PwdIndexEntry*, e, i) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
350 | CxIterator entry_iter = cxListIterator(e->locations); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
351 | cx_foreach(char *, loc, entry_iter) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
352 | cxmutstr rpath; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
353 | DavCfgRepository *r = dav_config_url2repo_s(davconfig, cx_str(loc), &rpath); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
354 | CredLocation *urlentry = calloc(1, sizeof(CredLocation)); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
355 | urlentry->id = e->id; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
356 | urlentry->location = util_concat_path_s(cx_strcast(r->url.value), cx_strcast(rpath)).ptr; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
357 | cxListAdd(locations, urlentry); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
358 | free(rpath.ptr); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
359 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
360 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
361 | // the list must be sorted |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
362 | cxListSort(locations); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
363 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
364 | // create full request url string and remove protocol prefix |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
365 | cxmutstr req_url_proto = util_concat_path_s(cx_strcast(repo->url.value), cx_str(path)); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
366 | cxstring req_url = cx_strcast(req_url_proto); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
367 | if(cx_strprefix(req_url, CX_STR("http://"))) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
368 | req_url = cx_strsubs(req_url, 7); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
369 | } else if(cx_strprefix(req_url, CX_STR("https://"))) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
370 | req_url = cx_strsubs(req_url, 8); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
371 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
372 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
373 | // iterate over sorted locations and check if a location is a prefix |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
374 | // of the requested url |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
375 | char *id = NULL; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
376 | int ret = 0; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
377 | i = cxListIterator(locations); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
378 | cx_foreach(CredLocation*, cred, i) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
379 | cxstring cred_url = cx_str(cred->location); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
380 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
381 | // remove protocol prefix |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
382 | if(cx_strprefix(cred_url, CX_STR("http://"))) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
383 | cred_url = cx_strsubs(cred_url, 7); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
384 | } else if(cx_strprefix(cred_url, CX_STR("https://"))) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
385 | cred_url = cx_strsubs(cred_url, 8); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
386 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
387 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
388 | if(cx_strprefix(req_url, cred_url)) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
389 | id = cred->id; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
390 | break; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
391 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
392 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
393 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
394 | // if an id is found and we can access the decrypted secret store |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
395 | // we can set the user/password |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
396 | if(id && (secrets->isdecrypted || !pwdstore_decrypt_secrets(secrets))) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
397 | PwdEntry *cred = pwdstore_get(secrets, id); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
398 | if(cred) { |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
399 | *user = cred->user; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
400 | *password = cred->password; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
401 | ret = 1; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
402 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
403 | } |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
404 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
405 | free(req_url_proto.ptr); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
406 | cxListDestroy(locations); |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
407 | |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
408 | return ret; |
1ce14068ef31
update libidav, unify config.c with cmdutils
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
50
diff
changeset
|
409 | } |