45 #define ENV_HOME getenv("HOME") |
45 #define ENV_HOME getenv("HOME") |
46 #endif /* _WIN32 */ |
46 #endif /* _WIN32 */ |
47 |
47 |
48 static UcxMap *repos; |
48 static UcxMap *repos; |
49 static UcxMap *keys; |
49 static UcxMap *keys; |
|
50 static Proxy *http_proxy; |
|
51 static Proxy *https_proxy; |
50 |
52 |
51 int check_config_dir() { |
53 int check_config_dir() { |
52 char *file = util_concat_path(ENV_HOME, ".dav"); |
54 char *file = util_concat_path(ENV_HOME, ".dav"); |
53 int ret = 0; |
55 int ret = 0; |
54 if(util_mkdir(file, S_IRWXU)) { |
56 if(util_mkdir(file, S_IRWXU)) { |
59 free(file); |
61 free(file); |
60 return ret; |
62 return ret; |
61 } |
63 } |
62 |
64 |
63 void load_config() { |
65 void load_config() { |
|
66 // TODO: free the config somewhere |
64 repos = ucx_map_new(16); |
67 repos = ucx_map_new(16); |
65 keys = ucx_map_new(16); |
68 keys = ucx_map_new(16); |
|
69 http_proxy = calloc(1, sizeof(Proxy)); |
|
70 https_proxy = calloc(1, sizeof(Proxy)); |
66 if(check_config_dir()) { |
71 if(check_config_dir()) { |
67 return; |
72 return; |
68 } |
73 } |
69 |
74 |
70 char *file = util_concat_path(ENV_HOME, ".dav/config.xml"); |
75 char *file = util_concat_path(ENV_HOME, ".dav/config.xml"); |
86 if(node->type == XML_ELEMENT_NODE) { |
91 if(node->type == XML_ELEMENT_NODE) { |
87 if(xstreq(node->name, "repository")) { |
92 if(xstreq(node->name, "repository")) { |
88 load_repository(node); |
93 load_repository(node); |
89 } else if(xstreq(node->name, "key")) { |
94 } else if(xstreq(node->name, "key")) { |
90 load_key(node); |
95 load_key(node); |
|
96 } else if (xstreq(node->name, "http-proxy")) { |
|
97 load_proxy(node, HTTP_PROXY); |
|
98 } else if (xstreq(node->name, "https-proxy")) { |
|
99 load_proxy(node, HTTPS_PROXY); |
91 } |
100 } |
92 } |
101 } |
93 node = node->next; |
102 node = node->next; |
94 } |
103 } |
95 |
104 |
144 } |
153 } |
145 |
154 |
146 ucx_map_cstr_put(repos, repo->name, repo); |
155 ucx_map_cstr_put(repos, repo->name, repo); |
147 } |
156 } |
148 |
157 |
|
158 void load_proxy(xmlNode *proxynode, int type) { |
|
159 Proxy *proxy; |
|
160 const char *stype; |
|
161 if (type == HTTPS_PROXY) { |
|
162 proxy = https_proxy; |
|
163 stype = "https"; |
|
164 } else if (type == HTTP_PROXY) { |
|
165 proxy = http_proxy; |
|
166 stype = "http"; |
|
167 } |
|
168 |
|
169 xmlNode *node = proxynode->children; |
|
170 while(node) { |
|
171 if(node->type == XML_ELEMENT_NODE) { |
|
172 char *value = util_xml_get_text(node); |
|
173 if(!value) { |
|
174 // next |
|
175 } else if(xstreq(node->name, "url")) { |
|
176 proxy->url = strdup(value); |
|
177 } else if(xstreq(node->name, "user")) { |
|
178 proxy->user = strdup(value); |
|
179 } else if(xstreq(node->name, "password")) { |
|
180 proxy->password = util_base64decode(value); |
|
181 } else if(xstreq(node->name, "no")) { |
|
182 proxy->no = strdup(value); |
|
183 } |
|
184 } |
|
185 node = node->next; |
|
186 } |
|
187 |
|
188 if(!proxy->url) { |
|
189 fprintf(stderr, |
|
190 "Cannot load config.xml: missing url for %s proxy.\n", stype); |
|
191 fprintf(stderr, "Abort.\n"); |
|
192 exit(-1); |
|
193 } |
|
194 } |
|
195 |
149 void load_key(xmlNode *keynode) { |
196 void load_key(xmlNode *keynode) { |
150 xmlNode *node = keynode->children; |
197 xmlNode *node = keynode->children; |
151 Key *key = calloc(1, sizeof(Key)); |
198 Key *key = calloc(1, sizeof(Key)); |
152 key->type = KEY_AES256; |
199 key->type = KEY_AES256; |
153 |
200 |