Code

git: remove $Id$ svn cruft
[fusedav.git] / src / session.c
index 4f1009b2e24c883fbc7cba4d67c09e0e2f550980..ca5b89be3c34961eb90d76fb8dbd4b663b13dd93 100644 (file)
@@ -1,3 +1,31 @@
+/***
+  Copyright (c) 2004-2006 Lennart Poettering
+
+  Permission is hereby granted, free of charge, to any person
+  obtaining a copy of this software and associated documentation files
+  (the "Software"), to deal in the Software without restriction,
+  including without limitation the rights to use, copy, modify, merge,
+  publish, distribute, sublicense, and/or sell copies of the Software,
+  and to permit persons to whom the Software is furnished to do so,
+  subject to the following conditions:
+
+  The above copyright notice and this permission notice shall be
+  included in all copies or substantial portions of the Software.
+
+  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
+  BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
+  ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
+  CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
+  SOFTWARE.
+***/
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
 #include <stdio.h>
 #include <assert.h>
 #include <pthread.h>
 #include <ne_socket.h>
 #include <ne_auth.h>
 #include <ne_dates.h>
+#include <ne_redirect.h>
 
 #include "session.h"
-
+#include "fusedav.h"
 
 static pthread_once_t session_once = PTHREAD_ONCE_INIT;
 static pthread_key_t session_tsd_key;
 
-static ne_uri uri;
+ne_uri uri;
 static int b_uri = 0;
 
-static const char *username = NULL, *password = NULL;
-const char *base_directory = NULL;
+char *username = NULL;
+static char *password = NULL;
+char *base_directory = NULL;
 
 static pthread_mutex_t credential_mutex = PTHREAD_MUTEX_INITIALIZER;
 
-
-static char* ask_user(char *p, int hidden) {
+static char* ask_user(const char *p, int hidden) {
     char q[256], *r;
     struct termios t;
     int c = 0, l;
@@ -49,7 +78,7 @@ static char* ask_user(char *p, int hidden) {
             }
         }
     }
-    
+
     fprintf(stderr, "%s: ", p);
     r = fgets(q, sizeof(q), stdin);
     l = strlen(q);
@@ -61,30 +90,32 @@ static char* ask_user(char *p, int hidden) {
         tcsetattr(fileno(stdin), TCSANOW, &t);
         fprintf(stderr, "\n");
     }
-    
+
     return r ? strdup(r) : NULL;
 }
 
-static int ssl_verify_cb(void *userdata, int failures, const ne_ssl_certificate *cert) {
+static int ssl_verify_cb(__unused void *userdata, __unused int failures, __unused const ne_ssl_certificate *cert) {
     return 0;
 }
 
-static int ne_auth_creds_cb(void *userdata, const char *realm, int attempt, char *u, char *p) {
+static int ne_auth_creds_cb(__unused void *userdata, const char *realm, int attempt, char *u, char *p) {
     int r = -1;
-    
-    
+
     pthread_mutex_lock(&credential_mutex);
 
     if (attempt) {
-        fprintf(stderr, "Authenication failure!\n");
+        fprintf(stderr, "Authentication failure!\n");
         free((void*) username);
         free((void*) password);
         username = password = NULL;
     }
-    
+
+    if (!username || !password)
+        fprintf(stderr, "Realm '%s' requires authentication.\n", realm);
+
     if (!username)
         username = ask_user("Username", 0);
-    
+
     if (username && !password)
         password = ask_user("Password", 1);
 
@@ -98,15 +129,17 @@ static int ne_auth_creds_cb(void *userdata, const char *realm, int attempt, char
     return r;
 }
 
-static ne_session *session_open(void) {
-    char *scheme = NULL;
+static ne_session *session_open(int with_lock) {
+    const char *scheme = NULL;
     ne_session *session;
 
+    extern ne_lock_store *lock_store;
+
     if (!b_uri)
         return NULL;
 
     scheme = uri.scheme ? uri.scheme : "http";
-    
+
     if (!(session = ne_session_create(scheme, uri.host, uri.port ? uri.port : ne_uri_defaultport(scheme)))) {
         fprintf(stderr, "Failed to create session\n");
         return NULL;
@@ -114,6 +147,11 @@ static ne_session *session_open(void) {
 
     ne_ssl_set_verify(session, ssl_verify_cb, NULL);
     ne_set_server_auth(session, ne_auth_creds_cb, NULL);
+    ne_redirect_register(session);
+
+    if (with_lock && lock_store)
+        ne_lockstore_register(lock_store, session);
+
     return session;
 }
 
@@ -127,24 +165,27 @@ static void session_tsd_key_init(void) {
     pthread_key_create(&session_tsd_key, session_destroy);
 }
 
-ne_session *session_get(void) {
+ne_session *session_get(int with_lock) {
     ne_session *session;
-    
+
     pthread_once(&session_once, session_tsd_key_init);
 
     if ((session = pthread_getspecific(session_tsd_key)))
         return session;
 
-    session = session_open();
+    session = session_open(with_lock);
     pthread_setspecific(session_tsd_key, session);
 
     return session;
 }
 
 int session_set_uri(const char *s, const char *u, const char *p) {
-    assert(!b_uri && !username && !password);
     int l;
-        
+
+    assert(!b_uri);
+    assert(!username);
+    assert(!password);
+
     if (ne_uri_parse(s, &uri)) {
         fprintf(stderr, "Invalid URI <%s>\n", s);
         goto finish;
@@ -169,9 +210,9 @@ int session_set_uri(const char *s, const char *u, const char *p) {
         password = strdup(p);
 
     return 0;
-    
+
 finish:
-    
+
     if (b_uri) {
         ne_uri_free(&uri);
         b_uri = 0;
@@ -194,3 +235,12 @@ void session_free(void) {
     username = password = base_directory = NULL;
 }
 
+int session_is_local(const ne_uri *u) {
+    assert(u);
+    assert(b_uri);
+
+    return
+        strcmp(u->scheme, uri.scheme) == 0 &&
+        strcmp(u->host, uri.host) == 0 &&
+        u->port == uri.port;
+}