1 /* $Id$ */
3 /***
4 This file is part of fusedav.
6 fusedav is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 fusedav is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
14 License for more details.
16 You should have received a copy of the GNU General Public License
17 along with fusedav; if not, write to the Free Software Foundation,
18 Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 ***/
21 #ifdef HAVE_CONFIG_H
22 #include <config.h>
23 #endif
25 #include <stdio.h>
26 #include <assert.h>
27 #include <pthread.h>
28 #include <string.h>
29 #include <stdlib.h>
30 #include <termios.h>
31 #include <unistd.h>
33 #include <ne_uri.h>
34 #include <ne_request.h>
35 #include <ne_basic.h>
36 #include <ne_props.h>
37 #include <ne_utils.h>
38 #include <ne_socket.h>
39 #include <ne_auth.h>
40 #include <ne_dates.h>
41 #include <ne_redirect.h>
43 #include "session.h"
44 #include "fusedav.h"
46 static pthread_once_t session_once = PTHREAD_ONCE_INIT;
47 static pthread_key_t session_tsd_key;
49 ne_uri uri;
50 static int b_uri = 0;
52 static char *username = NULL, *password = NULL;
53 char *base_directory = NULL;
55 static pthread_mutex_t credential_mutex = PTHREAD_MUTEX_INITIALIZER;
57 static char* ask_user(const char *p, int hidden) {
58 char q[256], *r;
59 struct termios t;
60 int c = 0, l;
62 if (hidden) {
63 if (!isatty(fileno(stdin)))
64 hidden = 0;
65 else {
66 if (tcgetattr(fileno(stdin), &t) < 0)
67 hidden = 0;
68 else {
69 c = t.c_lflag;
70 t.c_lflag &= ~ECHO;
71 if (tcsetattr(fileno(stdin), TCSANOW, &t) < 0)
72 hidden = 0;
73 }
74 }
75 }
77 fprintf(stderr, "%s: ", p);
78 r = fgets(q, sizeof(q), stdin);
79 l = strlen(q);
80 if (l && q[l-1] == '\n')
81 q[l-1] = 0;
83 if (hidden) {
84 t.c_lflag = c;
85 tcsetattr(fileno(stdin), TCSANOW, &t);
86 fprintf(stderr, "\n");
87 }
89 return r ? strdup(r) : NULL;
90 }
92 static int ssl_verify_cb(__unused void *userdata, __unused int failures, __unused const ne_ssl_certificate *cert) {
93 return 0;
94 }
96 static int ne_auth_creds_cb(__unused void *userdata, const char *realm, int attempt, char *u, char *p) {
97 int r = -1;
99 pthread_mutex_lock(&credential_mutex);
101 if (attempt) {
102 fprintf(stderr, "Authentication failure!\n");
103 free((void*) username);
104 free((void*) password);
105 username = password = NULL;
106 }
108 fprintf(stderr, "Realm '%s' requires authentication.\n", realm);
110 if (!username)
111 username = ask_user("Username", 0);
113 if (username && !password)
114 password = ask_user("Password", 1);
116 if (username && password) {
117 snprintf(u, NE_ABUFSIZ, "%s", username);
118 snprintf(p, NE_ABUFSIZ, "%s", password);
119 r = 0;
120 }
122 pthread_mutex_unlock(&credential_mutex);
123 return r;
124 }
126 static ne_session *session_open(int with_lock) {
127 const char *scheme = NULL;
128 ne_session *session;
130 extern ne_lock_store *lock_store;
132 if (!b_uri)
133 return NULL;
135 scheme = uri.scheme ? uri.scheme : "http";
137 if (!(session = ne_session_create(scheme, uri.host, uri.port ? uri.port : ne_uri_defaultport(scheme)))) {
138 fprintf(stderr, "Failed to create session\n");
139 return NULL;
140 }
142 ne_ssl_set_verify(session, ssl_verify_cb, NULL);
143 ne_set_server_auth(session, ne_auth_creds_cb, NULL);
144 ne_redirect_register(session);
146 if (with_lock && lock_store)
147 ne_lockstore_register(lock_store, session);
149 return session;
150 }
152 static void session_destroy(void *s) {
153 ne_session *session = s;
154 assert(s);
155 ne_session_destroy(session);
156 }
158 static void session_tsd_key_init(void) {
159 pthread_key_create(&session_tsd_key, session_destroy);
160 }
162 ne_session *session_get(int with_lock) {
163 ne_session *session;
165 pthread_once(&session_once, session_tsd_key_init);
167 if ((session = pthread_getspecific(session_tsd_key)))
168 return session;
170 session = session_open(with_lock);
171 pthread_setspecific(session_tsd_key, session);
173 return session;
174 }
176 int session_set_uri(const char *s, const char *u, const char *p) {
177 int l;
179 assert(!b_uri);
180 assert(!username);
181 assert(!password);
183 if (ne_uri_parse(s, &uri)) {
184 fprintf(stderr, "Invalid URI <%s>\n", s);
185 goto finish;
186 }
188 b_uri = 1;
190 if (!uri.host) {
191 fprintf(stderr, "Missing host part in URI <%s>\n", s);
192 goto finish;
193 }
195 base_directory = strdup(uri.path);
196 l = strlen(base_directory);
197 if (base_directory[l-1] == '/')
198 ((char*) base_directory)[l-1] = 0;
200 if (u)
201 username = strdup(u);
203 if (p)
204 password = strdup(p);
206 return 0;
208 finish:
210 if (b_uri) {
211 ne_uri_free(&uri);
212 b_uri = 0;
213 }
215 return -1;
216 }
219 void session_free(void) {
220 if (b_uri) {
221 ne_uri_free(&uri);
222 b_uri = 0;
223 }
225 free((char*) username);
226 free((char*) password);
227 free((char*) base_directory);
229 username = password = base_directory = NULL;
230 }
232 int session_is_local(const ne_uri *u) {
233 assert(u);
234 assert(b_uri);
236 return
237 strcmp(u->scheme, uri.scheme) == 0 &&
238 strcmp(u->host, uri.host) == 0 &&
239 u->port == uri.port;
240 }