1 /* $Id$ */
3 /***
4 Copyright (c) 2004-2006 Lennart Poettering
6 Permission is hereby granted, free of charge, to any person
7 obtaining a copy of this software and associated documentation files
8 (the "Software"), to deal in the Software without restriction,
9 including without limitation the rights to use, copy, modify, merge,
10 publish, distribute, sublicense, and/or sell copies of the Software,
11 and to permit persons to whom the Software is furnished to do so,
12 subject to the following conditions:
14 The above copyright notice and this permission notice shall be
15 included in all copies or substantial portions of the Software.
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
21 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
22 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
23 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
24 SOFTWARE.
25 ***/
27 #ifdef HAVE_CONFIG_H
28 #include <config.h>
29 #endif
31 #include <stdio.h>
32 #include <assert.h>
33 #include <pthread.h>
34 #include <string.h>
35 #include <stdlib.h>
36 #include <termios.h>
37 #include <unistd.h>
39 #include <ne_uri.h>
40 #include <ne_request.h>
41 #include <ne_basic.h>
42 #include <ne_props.h>
43 #include <ne_utils.h>
44 #include <ne_socket.h>
45 #include <ne_auth.h>
46 #include <ne_dates.h>
47 #include <ne_redirect.h>
49 #include "session.h"
50 #include "fusedav.h"
52 static pthread_once_t session_once = PTHREAD_ONCE_INIT;
53 static pthread_key_t session_tsd_key;
55 ne_uri uri;
56 static int b_uri = 0;
58 char *username = NULL;
59 static char *password = NULL;
60 char *base_directory = NULL;
62 static pthread_mutex_t credential_mutex = PTHREAD_MUTEX_INITIALIZER;
64 static char* ask_user(const char *p, int hidden) {
65 char q[256], *r;
66 struct termios t;
67 int c = 0, l;
69 if (hidden) {
70 if (!isatty(fileno(stdin)))
71 hidden = 0;
72 else {
73 if (tcgetattr(fileno(stdin), &t) < 0)
74 hidden = 0;
75 else {
76 c = t.c_lflag;
77 t.c_lflag &= ~ECHO;
78 if (tcsetattr(fileno(stdin), TCSANOW, &t) < 0)
79 hidden = 0;
80 }
81 }
82 }
84 fprintf(stderr, "%s: ", p);
85 r = fgets(q, sizeof(q), stdin);
86 l = strlen(q);
87 if (l && q[l-1] == '\n')
88 q[l-1] = 0;
90 if (hidden) {
91 t.c_lflag = c;
92 tcsetattr(fileno(stdin), TCSANOW, &t);
93 fprintf(stderr, "\n");
94 }
96 return r ? strdup(r) : NULL;
97 }
99 static int ssl_verify_cb(__unused void *userdata, __unused int failures, __unused const ne_ssl_certificate *cert) {
100 return 0;
101 }
103 static int ne_auth_creds_cb(__unused void *userdata, const char *realm, int attempt, char *u, char *p) {
104 int r = -1;
106 pthread_mutex_lock(&credential_mutex);
108 if (attempt) {
109 fprintf(stderr, "Authentication failure!\n");
110 free((void*) username);
111 free((void*) password);
112 username = password = NULL;
113 }
115 if (!username || !password)
116 fprintf(stderr, "Realm '%s' requires authentication.\n", realm);
118 if (!username)
119 username = ask_user("Username", 0);
121 if (username && !password)
122 password = ask_user("Password", 1);
124 if (username && password) {
125 snprintf(u, NE_ABUFSIZ, "%s", username);
126 snprintf(p, NE_ABUFSIZ, "%s", password);
127 r = 0;
128 }
130 pthread_mutex_unlock(&credential_mutex);
131 return r;
132 }
134 static ne_session *session_open(int with_lock) {
135 const char *scheme = NULL;
136 ne_session *session;
138 extern ne_lock_store *lock_store;
140 if (!b_uri)
141 return NULL;
143 scheme = uri.scheme ? uri.scheme : "http";
145 if (!(session = ne_session_create(scheme, uri.host, uri.port ? uri.port : ne_uri_defaultport(scheme)))) {
146 fprintf(stderr, "Failed to create session\n");
147 return NULL;
148 }
150 ne_ssl_set_verify(session, ssl_verify_cb, NULL);
151 ne_set_server_auth(session, ne_auth_creds_cb, NULL);
152 ne_redirect_register(session);
154 if (with_lock && lock_store)
155 ne_lockstore_register(lock_store, session);
157 return session;
158 }
160 static void session_destroy(void *s) {
161 ne_session *session = s;
162 assert(s);
163 ne_session_destroy(session);
164 }
166 static void session_tsd_key_init(void) {
167 pthread_key_create(&session_tsd_key, session_destroy);
168 }
170 ne_session *session_get(int with_lock) {
171 ne_session *session;
173 pthread_once(&session_once, session_tsd_key_init);
175 if ((session = pthread_getspecific(session_tsd_key)))
176 return session;
178 session = session_open(with_lock);
179 pthread_setspecific(session_tsd_key, session);
181 return session;
182 }
184 int session_set_uri(const char *s, const char *u, const char *p) {
185 int l;
187 assert(!b_uri);
188 assert(!username);
189 assert(!password);
191 if (ne_uri_parse(s, &uri)) {
192 fprintf(stderr, "Invalid URI <%s>\n", s);
193 goto finish;
194 }
196 b_uri = 1;
198 if (!uri.host) {
199 fprintf(stderr, "Missing host part in URI <%s>\n", s);
200 goto finish;
201 }
203 base_directory = strdup(uri.path);
204 l = strlen(base_directory);
205 if (base_directory[l-1] == '/')
206 ((char*) base_directory)[l-1] = 0;
208 if (u)
209 username = strdup(u);
211 if (p)
212 password = strdup(p);
214 return 0;
216 finish:
218 if (b_uri) {
219 ne_uri_free(&uri);
220 b_uri = 0;
221 }
223 return -1;
224 }
227 void session_free(void) {
228 if (b_uri) {
229 ne_uri_free(&uri);
230 b_uri = 0;
231 }
233 free((char*) username);
234 free((char*) password);
235 free((char*) base_directory);
237 username = password = base_directory = NULL;
238 }
240 int session_is_local(const ne_uri *u) {
241 assert(u);
242 assert(b_uri);
244 return
245 strcmp(u->scheme, uri.scheme) == 0 &&
246 strcmp(u->host, uri.host) == 0 &&
247 u->port == uri.port;
248 }