Code

liecense: Relicense to MIT
[fusedav.git] / src / session.c
index 75c37c86e654c449b3db5e146c82e74ea5a2e46d..acf4a581c1c6bb6d58af4b4f650e2c9ce7ad2566 100644 (file)
@@ -1,21 +1,27 @@
 /* $Id$ */
 
 /***
-  This file is part of fusedav.
-
-  fusedav is free software; you can redistribute it and/or modify it
-  under the terms of the GNU General Public License as published by
-  the Free Software Foundation; either version 2 of the License, or
-  (at your option) any later version.
-  
-  fusedav is distributed in the hope that it will be useful, but WITHOUT
-  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
-  or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
-  License for more details.
-  
-  You should have received a copy of the GNU General Public License
-  along with fusedav; if not, write to the Free Software Foundation,
-  Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+  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 <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;
@@ -89,22 +96,24 @@ static char* ask_user(char *p, int hidden) {
     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);
@@ -122,10 +131,12 @@ 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;
 
@@ -138,6 +149,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;
 }
 
@@ -151,7 +167,7 @@ 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);
@@ -159,16 +175,19 @@ ne_session *session_get(void) {
     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;
@@ -218,3 +237,13 @@ 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;
+}
+