application/connect.c

changeset 55
1ce14068ef31
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/application/connect.c	Wed Oct 23 10:37:43 2024 +0200
@@ -0,0 +1,110 @@
+/*
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
+ *
+ * Copyright 2024 Olaf Wintermann. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *   1. Redistributions of source code must retain the above copyright
+ *      notice, this list of conditions and the following disclaimer.
+ *
+ *   2. Redistributions in binary form must reproduce the above copyright
+ *      notice, this list of conditions and the following disclaimer in the
+ *      documentation and/or other materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
+ * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
+ * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
+ * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
+ * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
+ * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
+ * POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+#include "connect.h"
+
+#include <libidav/utils.h>
+
+#include <cx/string.h>
+#include <cx/utils.h>
+
+DavSession* connect_to_repo(DavContext *ctx, DavCfgRepository *repo, const char *path, dav_auth_func authfunc) {
+    cxmutstr decodedpw = dav_repository_get_decodedpassword(repo);
+    
+    char *user = repo->user.value.ptr;
+    char *password = decodedpw.ptr;
+    
+    if(!user && !password) {
+        if(!get_stored_credentials(repo->stored_user.value.ptr, &user, &password)) {
+            get_location_credentials(repo, path, &user, &password);
+        }
+    }
+    
+    DavSession *sn = dav_session_new_auth(ctx, repo->url.value.ptr, user, password);
+    if(password) {
+        free(password);
+    }
+    
+    sn->flags = dav_repository_get_flags(repo);
+    sn->key = dav_context_get_key(ctx, repo->default_key.value.ptr);
+    // TODO: reactivate
+    //curl_easy_setopt(sn->handle, CURLOPT_HTTPAUTH, repo->authmethods);
+    curl_easy_setopt(sn->handle, CURLOPT_SSLVERSION, repo->ssl_version);
+    if(repo->cert.value.ptr) {
+        curl_easy_setopt(sn->handle, CURLOPT_CAINFO, repo->cert.value.ptr);
+    }
+    /*
+    if(!repo->verification.value || cmd_getoption(a, "insecure")) {
+        curl_easy_setopt(sn->handle, CURLOPT_SSL_VERIFYPEER, 0);
+        curl_easy_setopt(sn->handle, CURLOPT_SSL_VERIFYHOST, 0);
+    }
+    */
+    /*
+    if(!cmd_getoption(a, "noinput")) {
+        dav_session_set_authcallback(sn, authfunc, repo);
+    }
+    */
+    return sn;
+}
+
+int request_auth(DavSession *sn, void *userdata) {
+    DavCfgRepository *repo = userdata;
+    
+    cxstring user = {NULL, 0};
+    char ubuf[256];
+    if(repo->user.value.ptr) {
+       user = cx_strcast(repo->user.value);
+    } else {
+        fprintf(stderr, "User: ");
+        fflush(stderr);
+        user = cx_str(fgets(ubuf, 256, stdin));
+    }
+    if(!user.ptr) {
+        return 0;
+    }
+    if(user.length > 0 && user.ptr[user.length-1] == '\n') {
+        user.length--;
+    }
+    if(user.length > 0 && user.ptr[user.length-1] == '\r') {
+        user.length--;
+    }
+    
+    char *password = util_password_input("Password: ");
+    if(!password || strlen(password) == 0) {
+        return 0;
+    }
+    
+    dav_session_set_auth_s(sn, user, cx_str(password));
+    free(password);
+    
+    return 0;
+}

mercurial