application/connect.c

changeset 55
1ce14068ef31
equal deleted inserted replaced
54:3ca3acefc66a 55:1ce14068ef31
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 <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32
33 #include "connect.h"
34
35 #include <libidav/utils.h>
36
37 #include <cx/string.h>
38 #include <cx/utils.h>
39
40 DavSession* connect_to_repo(DavContext *ctx, DavCfgRepository *repo, const char *path, dav_auth_func authfunc) {
41 cxmutstr decodedpw = dav_repository_get_decodedpassword(repo);
42
43 char *user = repo->user.value.ptr;
44 char *password = decodedpw.ptr;
45
46 if(!user && !password) {
47 if(!get_stored_credentials(repo->stored_user.value.ptr, &user, &password)) {
48 get_location_credentials(repo, path, &user, &password);
49 }
50 }
51
52 DavSession *sn = dav_session_new_auth(ctx, repo->url.value.ptr, user, password);
53 if(password) {
54 free(password);
55 }
56
57 sn->flags = dav_repository_get_flags(repo);
58 sn->key = dav_context_get_key(ctx, repo->default_key.value.ptr);
59 // TODO: reactivate
60 //curl_easy_setopt(sn->handle, CURLOPT_HTTPAUTH, repo->authmethods);
61 curl_easy_setopt(sn->handle, CURLOPT_SSLVERSION, repo->ssl_version);
62 if(repo->cert.value.ptr) {
63 curl_easy_setopt(sn->handle, CURLOPT_CAINFO, repo->cert.value.ptr);
64 }
65 /*
66 if(!repo->verification.value || cmd_getoption(a, "insecure")) {
67 curl_easy_setopt(sn->handle, CURLOPT_SSL_VERIFYPEER, 0);
68 curl_easy_setopt(sn->handle, CURLOPT_SSL_VERIFYHOST, 0);
69 }
70 */
71 /*
72 if(!cmd_getoption(a, "noinput")) {
73 dav_session_set_authcallback(sn, authfunc, repo);
74 }
75 */
76 return sn;
77 }
78
79 int request_auth(DavSession *sn, void *userdata) {
80 DavCfgRepository *repo = userdata;
81
82 cxstring user = {NULL, 0};
83 char ubuf[256];
84 if(repo->user.value.ptr) {
85 user = cx_strcast(repo->user.value);
86 } else {
87 fprintf(stderr, "User: ");
88 fflush(stderr);
89 user = cx_str(fgets(ubuf, 256, stdin));
90 }
91 if(!user.ptr) {
92 return 0;
93 }
94 if(user.length > 0 && user.ptr[user.length-1] == '\n') {
95 user.length--;
96 }
97 if(user.length > 0 && user.ptr[user.length-1] == '\r') {
98 user.length--;
99 }
100
101 char *password = util_password_input("Password: ");
102 if(!password || strlen(password) == 0) {
103 return 0;
104 }
105
106 dav_session_set_auth_s(sn, user, cx_str(password));
107 free(password);
108
109 return 0;
110 }

mercurial