application/settings.c

changeset 60
ee4e4742391e
child 61
eb63af2f2bdd
equal deleted inserted replaced
59:6bd37fe6d905 60:ee4e4742391e
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright 2024 Olaf Wintermann. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
26 * POSSIBILITY OF SUCH DAMAGE.
27 */
28
29 #include "settings.h"
30
31 #include <cx/list.h>
32 #include <libidav/utils.h>
33
34
35 #define SETTINGS_STATE_REPOLIST_SELECTED 1
36
37 #define SETTINGS_STATE_REPO_ENCRYPTION 20
38
39
40 static void repolist_activate(UiEvent *event, void *userdata) {
41 UiListSelection *selection = event->eventdata;
42 SettingsWindow *settings = event->window;
43 settings_edit_repository(settings, selection->rows[0]);
44 }
45
46 static void repolist_selection(UiEvent *event, void *userdata) {
47 UiListSelection *selection = event->eventdata;
48 SettingsWindow *settings = event->window;
49 if(selection->count > 0) {
50 ui_set_group(event->obj->ctx, SETTINGS_STATE_REPOLIST_SELECTED);
51 settings->selected_repo = selection->rows[0];
52 } else {
53 ui_unset_group(event->obj->ctx, SETTINGS_STATE_REPOLIST_SELECTED);
54 }
55 }
56
57 static void repolist_edit(UiEvent *event, void *userdata) {
58 SettingsWindow *settings = event->window;
59 if(settings->selected_repo >= 0) {
60 settings_edit_repository(settings, settings->selected_repo);
61 }
62 }
63
64 static void repolist_remove(UiEvent *event, void *userdata) {
65 // TODO
66 }
67
68 static void editrepo_go_back(UiEvent *event, void *userdata) {
69 SettingsWindow *settings = event->window;
70 settings_store_repository(settings);
71 ui_set(settings->repo_tabview, 0);
72 }
73
74
75 void settings_ok(UiEvent *event, void *userdata) {
76 SettingsWindow *settings = event->window;
77 settings->config_saved = TRUE;
78 // save any changed settings
79 settings_store_repository(settings);
80
81 set_config(settings->config); // TODO: free old config
82 if(store_config()) {
83 ui_dialog(event->obj, .title = "Error", .content = "Cannot store settings", .closebutton_label = "OK");
84 }
85 application_update_repolist(get_application());
86 ui_close(event->obj);
87 }
88
89 void settings_cancel(UiEvent *event, void *userdata) {
90 SettingsWindow *settings = event->window;
91 if(settings->config_saved) {
92 // function was called as context closefunc by ui_close
93 // in settings_ok
94 // don't free anything
95 return;
96 }
97 dav_config_free(settings->config);
98 if(userdata) {
99 ui_close(event->obj);
100 }
101 }
102
103 void settings_window_open() {
104 DavConfig *config = load_config_file();
105 if(!config) {
106 return;
107 }
108
109 UiObject *obj = ui_simple_window("Settings", NULL);
110 ui_context_closefunc(obj->ctx, settings_cancel, NULL);
111 SettingsWindow *wdata = ui_malloc(obj->ctx, sizeof(SettingsWindow));
112 wdata->config = config;
113 obj->window = wdata;
114 settings_init(obj, wdata);
115
116 ui_tabview(obj) {
117 ui_tab(obj, "General") {
118 ui_grid(obj, .margin = 10) {
119 ui_label(obj, .label = "TODO");
120 }
121 }
122
123 ui_tab(obj, "Repositories") {
124
125 ui_tabview(obj, .value = wdata->repo_tabview, .margin = 10, .tabview = UI_TABVIEW_INVISIBLE) {
126 ui_tab(obj, "list") {
127 ui_grid(obj, .margin = 10, .columnspacing = 10, .rowspacing = 10) {
128 ui_hbox(obj, .spacing = 10) {
129 ui_button(obj, .label = "Add");
130 ui_button(obj, .label = "Edit", .onclick = repolist_edit, .groups = UI_GROUPS(SETTINGS_STATE_REPOLIST_SELECTED));
131 ui_button(obj, .label = "Remove", .groups = UI_GROUPS(SETTINGS_STATE_REPOLIST_SELECTED));
132 }
133 ui_newline(obj);
134
135 UiModel* model = ui_model(obj->ctx, UI_STRING, "Name", UI_STRING, "URL", UI_STRING, "User", UI_STRING, "Encrypted", -1);
136 model->getvalue = (ui_getvaluefunc) settings_repolist_getvalue;
137 ui_table(obj,
138 .model = model,
139 .list = wdata->repos,
140 .multiselection = FALSE,
141 .onactivate = repolist_activate,
142 .onselection = repolist_selection,
143 .vexpand = TRUE, .hexpand = TRUE, .colspan = 3);
144 }
145 }
146
147 ui_tab(obj, "repo") {
148 ui_vbox(obj, .margin = 10, .spacing = 10) {
149 ui_hbox(obj, .fill = UI_OFF, .spacing = 4) {
150 ui_button(obj, .icon = UI_ICON_GO_BACK, .onclick = editrepo_go_back);
151 ui_label(obj, .label = "Repository List");
152 }
153
154 ui_scrolledwindow(obj, .hexpand = TRUE, .vexpand = TRUE, .subcontainer = UI_CONTAINER_NO_SUB) {
155 ui_grid(obj, .margin = 10, .columnspacing = 10, .rowspacing = 10) {
156 ui_llabel(obj, .label = "Name");
157 ui_hbox0(obj) {
158 ui_textfield(obj, .value = wdata->repo_name, .width = 15);
159 }
160 ui_newline(obj);
161 ui_llabel(obj, .label = "URL");
162 ui_textfield(obj, .value = wdata->repo_url, .hexpand = TRUE);
163 ui_newline(obj);
164
165 ui_llabel(obj, .label = "Credentials", .style = UI_LABEL_STYLE_TITLE, .colspan = 2);
166 ui_newline(obj);
167 ui_hbox(obj, .spacing = 4, .colspan = 2) {
168 ui_combobox(obj, .list = wdata->repo_credentials);
169 ui_hbox0(obj) {
170 ui_button(obj, .label = "New Credentials");
171 }
172 }
173 ui_newline(obj);
174 ui_expander(obj, .spacing = 10, .colspan = 2, .label = "Unencrypted User/Password", .margin = 10) {
175 ui_llabel(obj, .label = "Store the credentials unencrypted in the repository and not in the secret store", .style = UI_LABEL_STYLE_DIM);
176 ui_grid(obj, .rowspacing = 10, .columnspacing = 10, .fill = UI_OFF) {
177 ui_llabel(obj, .label = "User");
178 ui_textfield(obj, .value = wdata->repo_user, .width = 15);
179 ui_newline(obj);
180
181 ui_llabel(obj, .label = "Password");
182 ui_passwordfield(obj, .value = wdata->repo_password, .width = 15);
183 }
184 }
185 ui_newline(obj);
186
187 ui_llabel(obj, .label = "Encryption", .style = UI_LABEL_STYLE_TITLE, .colspan = 2);
188 ui_newline(obj);
189
190 ui_checkbox(obj, .label = "Enable client-side encryption", .value = wdata->repo_encryption, .colspan = 2, .enable_group = SETTINGS_STATE_REPO_ENCRYPTION);
191 ui_newline(obj);
192 ui_llabel(obj, .label = "Default key");
193 ui_hbox(obj, .spacing = 4) {
194 ui_combobox(obj, .list = wdata->repo_keys, .groups = UI_GROUPS(SETTINGS_STATE_REPO_ENCRYPTION));
195 ui_button(obj, .label = "Generate Key", .groups = UI_GROUPS(SETTINGS_STATE_REPO_ENCRYPTION));
196 }
197 ui_newline(obj);
198
199 ui_llabel(obj, .label = "TLS", .style = UI_LABEL_STYLE_TITLE, .colspan = 2);
200 ui_newline(obj);
201
202 ui_llabel(obj, .label = "Cert Path");
203 ui_hbox0(obj) {
204 ui_textfield(obj, .value = wdata->repo_cacert, .width = 15);
205 }
206 ui_newline(obj);
207
208 ui_llabel(obj, .label = "TLS Version");
209 ui_hbox0(obj) {
210 ui_combobox(obj, .list = wdata->repo_tls_versions);
211 }
212 ui_newline(obj);
213 ui_checkbox(obj, .label = "Disable TLS verification", .value = wdata->repo_disable_verification, .colspan = 2);
214 }
215 }
216 }
217 }
218 }
219
220
221 }
222
223 ui_tab(obj, "Sync Directories") {
224 ui_grid(obj, .margin = 10) {
225 ui_label(obj, .label = "TODO");
226 }
227 }
228
229 ui_tab(obj, "Credentials") {
230 ui_grid(obj, .margin = 10) {
231 ui_label(obj, .label = "TODO");
232 }
233 }
234
235 ui_tab(obj, "Keys") {
236 ui_grid(obj, .margin = 10) {
237 ui_label(obj, .label = "TODO");
238 }
239 }
240
241 ui_tab(obj, "Properties") {
242 ui_grid(obj, .margin = 10) {
243 ui_label(obj, .label = "TODO");
244 }
245 }
246 }
247
248 ui_hbox(obj, .fill = UI_OFF, .margin = 10) {
249 ui_button(obj, .label = "Cancel", .onclick = settings_cancel, .onclickdata = "close");
250 ui_label(obj, .fill = UI_ON);
251 ui_button(obj, .label = "Save", .onclick = settings_ok);
252 }
253
254
255 ui_show(obj);
256 }
257
258 void settings_init(UiObject *obj, SettingsWindow *settings) {
259 settings->repos = ui_list_new(obj->ctx, NULL);
260 settings->repo_tabview = ui_int_new(obj->ctx, NULL);
261
262 settings->repo_name = ui_string_new(obj->ctx, NULL);
263 settings->repo_url = ui_string_new(obj->ctx, NULL);
264 settings->repo_user = ui_string_new(obj->ctx, NULL);
265 settings->repo_password = ui_string_new(obj->ctx, NULL);
266 settings->repo_cacert = ui_string_new(obj->ctx, NULL);
267 settings->repo_credentials = ui_list_new(obj->ctx, NULL);
268 settings->repo_keys = ui_list_new(obj->ctx, NULL);
269 settings->repo_tls_versions = ui_list_new(obj->ctx, NULL);
270 settings->repo_encryption = ui_int_new(obj->ctx, NULL);
271 settings->repo_disable_verification = ui_int_new(obj->ctx, NULL);
272
273 ui_list_append(settings->repo_tls_versions, "Default");
274 ui_list_append(settings->repo_tls_versions, "TLSv1.3");
275 ui_list_append(settings->repo_tls_versions, "TLSv1.2");
276 ui_list_append(settings->repo_tls_versions, "TLSv1.1");
277 ui_list_append(settings->repo_tls_versions, "TLSv1.0");
278
279 // load some list values, that can be reused
280 settings_update_repolist(settings);
281 settings_reload_keys(settings);
282 settings_reload_credentials(settings);
283
284 settings->selected_repo = -1;
285 }
286
287 #define SETTINGS_SET_STRING(str, setting) if(setting.value.ptr) ui_set(str, setting.value.ptr);
288
289 void settings_edit_repository(SettingsWindow *settings, int repo_index) {
290 DavCfgRepository *repo = ui_list_get(settings->repos, repo_index);
291 if(!repo) {
292 fprintf(stderr, "Error: cannot get repository at index %d\n", repo_index);
293 return;
294 }
295 settings->selected_repo = repo_index;
296
297 // load plain string values
298 SETTINGS_SET_STRING(settings->repo_name, repo->name);
299 SETTINGS_SET_STRING(settings->repo_url, repo->url);
300 SETTINGS_SET_STRING(settings->repo_cacert, repo->cert);
301 SETTINGS_SET_STRING(settings->repo_user, repo->user);
302 // load decrypted password
303 if(repo->password.value.ptr) {
304 char *decoded_pw = util_base64decode(repo->password.value.ptr);
305 ui_set(settings->repo_password, decoded_pw);
306 size_t decoded_pw_len = strlen(decoded_pw);
307 memset(decoded_pw, 0, decoded_pw_len);
308 free(decoded_pw);
309 }
310
311 // select credentials dropdown value
312 CxList *cred = settings->repo_credentials->data;
313 cred->collection.cmpfunc = (cx_compare_func)strcmp;
314 ssize_t cred_index = repo->stored_user.value.ptr ? cxListFind(cred, repo->stored_user.value.ptr) : 0;
315 if(cred_index > 0) {
316 ui_list_setselection(settings->repo_credentials, cred_index);
317 } else {
318 ui_list_setselection(settings->repo_credentials, 0);
319 }
320
321 // load encryption value and default key value
322 ui_set(settings->repo_encryption, repo->full_encryption.value);
323 CxList *keys = settings->repo_keys->data;
324 keys->collection.cmpfunc = (cx_compare_func)strcmp;
325 ssize_t key_index = repo->default_key.value.ptr ? cxListFind(keys, repo->default_key.value.ptr) : 0;
326 if(key_index > 0) {
327 ui_list_setselection(settings->repo_keys, key_index);
328 } else {
329 ui_list_setselection(settings->repo_keys, 0);
330 }
331
332 // select tls version from dropdown menu
333 CxList *tlsVersions = settings->repo_tls_versions->data;
334 tlsVersions->collection.cmpfunc = (cx_compare_func)strcmp;
335 const char *tls_str = dav_tlsversion2str(repo->ssl_version.value);
336 if(!tls_str) tls_str = "";
337 ssize_t tlsv_index = cxListFind(tlsVersions, tls_str);
338 if(tlsv_index > 0) {
339 ui_list_setselection(settings->repo_tls_versions, tlsv_index);
340 } else {
341 ui_list_setselection(settings->repo_tls_versions, 0);
342 }
343
344 if(!repo->verification.value) {
345 ui_set(settings->repo_disable_verification, TRUE);
346 }
347
348
349 // switch to editing tab
350 ui_set(settings->repo_tabview, 1);
351 }
352
353 /*
354 * sets a config value
355 * if new_value is null, the xml node is removed
356 */
357 static void cfg_string_set_value_or_remove(DavConfig *config, CfgString *str, xmlNode *parent, cxstring new_value, const char *nodename) {
358 if(new_value.length == 0) {
359 new_value.ptr = NULL;
360 }
361 dav_cfg_string_set_value(config, str, parent, new_value, nodename);
362 if(!new_value.ptr) {
363 dav_cfg_string_remove(str);
364 }
365 }
366
367 /*
368 * return the selected list value, if it is not the default value at index 0
369 */
370 static cxstring default_list_get_value(UiList *list) {
371 cxstring ret = { NULL, 0 };
372 UiListSelection sel = ui_list_getselection(list);
373 if(sel.count > 0) {
374 int index = sel.rows[0];
375 if(index > 0) {
376 ret = cx_str(ui_list_get(list, index));
377 }
378 free(sel.rows);
379 }
380 return ret;
381 }
382
383 void settings_store_repository(SettingsWindow *settings) {
384 DavConfig *config = settings->config;
385 DavCfgRepository *repo = ui_list_get(settings->repos, settings->selected_repo);
386 if(!repo) {
387 fprintf(stderr, "Error: cannot get repository at index %d\n", settings->selected_repo);
388 return;
389 }
390
391 // always store name/url nodes
392 dav_cfg_string_set_value(config, &repo->name, repo->node, cx_str(ui_get(settings->repo_name)), "name");
393 dav_cfg_string_set_value(config, &repo->url, repo->node, cx_str(ui_get(settings->repo_url)), "url");
394 // store user of remove node, if no user is configured
395 cfg_string_set_value_or_remove(config, &repo->user, repo->node, cx_str(ui_get(settings->repo_user)), "user");
396 // store cert or remove node
397 cfg_string_set_value_or_remove(config, &repo->cert, repo->node, cx_str(ui_get(settings->repo_cacert)), "cert");
398
399
400 // store password or remove node, if no password is configured
401 char *pw = ui_get(settings->repo_password);
402 size_t pwlen = strlen(pw);
403 if(pwlen > 0) {
404 char *pwenc = util_base64encode(pw, pwlen);
405 memset(pw, 0, pwlen);
406 dav_cfg_string_set_value(config, &repo->password, repo->node, cx_str(pwenc), "password");
407 free(pwenc);
408 } else {
409 // set password 0 NULL and remove password config node
410 cfg_string_set_value_or_remove(config, &repo->password, repo->node, cx_strn(NULL, 0), "password");
411 }
412
413 // get the selected credentials and set "stored-user"
414 // remove node if not needed
415 cxstring stored_user = default_list_get_value(settings->repo_credentials);
416 cfg_string_set_value_or_remove(config, &repo->stored_user, repo->node, stored_user, "stored-user");
417
418 // adjust full-encryption node if necessary
419 int encryption = ui_get(settings->repo_encryption);
420 if(encryption || repo->full_encryption.node) {
421 dav_cfg_bool_set_value(config, &repo->full_encryption, repo->node, encryption, "full-encryption");
422 }
423
424 // set default-key
425 cxstring key = default_list_get_value(settings->repo_keys);
426 cfg_string_set_value_or_remove(config, &repo->default_key, repo->node, key, "default-key");
427
428 // if disable_verification is enabled, set repo verification to false
429 // otherwise remove the node, because verification is enabled by default
430 int disable_verification = ui_get(settings->repo_disable_verification);
431 if(disable_verification) {
432 dav_cfg_bool_set_value(config, &repo->verification, repo->node, !disable_verification, "verification");
433 } else {
434 dav_cfg_bool_remove(&repo->verification);
435 }
436
437 // set tls version if configured
438 cxstring tlsversion_str = default_list_get_value(settings->repo_tls_versions);
439 int tlsversion = dav_str2ssl_version(tlsversion_str.ptr);
440 if(tlsversion >= 0) {
441 dav_cfg_int_set_value(config, &repo->ssl_version, repo->node, tlsversion, "ssl-version");
442 } else {
443 dav_cfg_int_remove(&repo->ssl_version);
444 }
445
446 settings->selected_repo = -1;
447 }
448
449 void settings_update_repolist(SettingsWindow *settings) {
450 DavConfig *config = settings->config;
451
452 ui_list_clear(settings->repos);
453
454 for (DavCfgRepository *repo = config->repositories; repo; repo = repo->next) {
455 ui_list_append(settings->repos, repo);
456 }
457 }
458
459 void* settings_repolist_getvalue(DavCfgRepository *repo, int col) {
460 switch(col) {
461 case 0: {
462 return repo->name.value.ptr;
463 }
464 case 1: {
465 return repo->url.value.ptr;
466 }
467 case 2: {
468 return repo->user.value.ptr;
469 }
470 case 3: {
471 return repo->full_encryption.value ? "yes" : "no";
472 }
473 }
474 return NULL;
475 }
476
477 void settings_reload_keys(SettingsWindow *settings) {
478 DavConfig *config = settings->config;
479 DavCfgKey *key = config->keys;
480 ui_list_clear(settings->repo_keys);
481 ui_list_append(settings->repo_keys, "-");
482 while(key) {
483 if(key->name.value.ptr) {
484 ui_list_append(settings->repo_keys, key->name.value.ptr);
485 }
486 key = key->next;
487 }
488 if(settings->repo_keys->update) {
489 ui_list_update(settings->repo_keys);
490 }
491 }
492
493 const char* dav_tlsversion2str(int value) {
494 if(value == CURL_SSLVERSION_TLSv1) {
495 return "TLSv1";
496 } else if(value == CURL_SSLVERSION_SSLv2) {
497 return "SSLv2";
498 } else if(value == CURL_SSLVERSION_SSLv3) {
499 return "SSLv3";
500 }
501 #if LIBCURL_VERSION_MAJOR * 1000 + LIBCURL_VERSION_MINOR >= 7034
502 else if(value == CURL_SSLVERSION_TLSv1_0) {
503 return "TLSv1.0";
504 } else if(value == CURL_SSLVERSION_TLSv1_1) {
505 return "TLSv1.1";
506 } else if(value == CURL_SSLVERSION_TLSv1_2) {
507 return "TLSv1.2";
508 }
509 #endif
510 #if LIBCURL_VERSION_MAJOR * 1000 + LIBCURL_VERSION_MINOR >= 7052
511 else if(value == CURL_SSLVERSION_TLSv1_3) {
512 return "TLSv1.3";
513 }
514 #endif
515 return NULL;
516 }
517
518 void settings_reload_credentials(SettingsWindow *settings) {
519 PwdStore *pwd = get_pwdstore();
520 if(!pwd) {
521 return;
522 }
523
524 ui_list_clear(settings->repo_credentials);
525 ui_list_append(settings->repo_credentials, "-");
526
527 CxIterator i = cxListIterator(pwd->noloc);
528 cx_foreach(PwdIndexEntry*, entry, i) {
529 ui_list_append(settings->repo_credentials, entry->id);
530 }
531
532 if(settings->repo_credentials->update) {
533 ui_list_update(settings->repo_credentials);
534 }
535 }
536
537

mercurial