Code

only ask for password once
[fusedav.git] / src / session.c
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.
10   
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.
15   
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     }
76     
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     }
88     
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;
98     
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     if (!username || !password)
109         fprintf(stderr, "Realm '%s' requires authentication.\n", realm);
110     
111     if (!username)
112         username = ask_user("Username", 0);
113     
114     if (username && !password)
115         password = ask_user("Password", 1);
117     if (username && password) {
118         snprintf(u, NE_ABUFSIZ, "%s", username);
119         snprintf(p, NE_ABUFSIZ, "%s", password);
120         r  = 0;
121     }
123     pthread_mutex_unlock(&credential_mutex);
124     return r;
127 static ne_session *session_open(int with_lock) {
128     const char *scheme = NULL;
129     ne_session *session;
131     extern ne_lock_store *lock_store;
133     if (!b_uri)
134         return NULL;
136     scheme = uri.scheme ? uri.scheme : "http";
137     
138     if (!(session = ne_session_create(scheme, uri.host, uri.port ? uri.port : ne_uri_defaultport(scheme)))) {
139         fprintf(stderr, "Failed to create session\n");
140         return NULL;
141     }
143     ne_ssl_set_verify(session, ssl_verify_cb, NULL);
144     ne_set_server_auth(session, ne_auth_creds_cb, NULL);
145     ne_redirect_register(session);
147     if (with_lock && lock_store)
148         ne_lockstore_register(lock_store, session);
149     
150     return session;
153 static void session_destroy(void *s) {
154     ne_session *session = s;
155     assert(s);
156     ne_session_destroy(session);
159 static void session_tsd_key_init(void) {
160     pthread_key_create(&session_tsd_key, session_destroy);
163 ne_session *session_get(int with_lock) {
164     ne_session *session;
165     
166     pthread_once(&session_once, session_tsd_key_init);
168     if ((session = pthread_getspecific(session_tsd_key)))
169         return session;
171     session = session_open(with_lock);
172     pthread_setspecific(session_tsd_key, session);
174     return session;
177 int session_set_uri(const char *s, const char *u, const char *p) {
178     int l;
179         
180     assert(!b_uri);
181     assert(!username);
182     assert(!password);
184     if (ne_uri_parse(s, &uri)) {
185         fprintf(stderr, "Invalid URI <%s>\n", s);
186         goto finish;
187     }
189     b_uri = 1;
191     if (!uri.host) {
192         fprintf(stderr, "Missing host part in URI <%s>\n", s);
193         goto finish;
194     }
196     base_directory = strdup(uri.path);
197     l = strlen(base_directory);
198     if (base_directory[l-1] == '/')
199         ((char*) base_directory)[l-1] = 0;
201     if (u)
202         username = strdup(u);
204     if (p)
205         password = strdup(p);
207     return 0;
208     
209 finish:
210     
211     if (b_uri) {
212         ne_uri_free(&uri);
213         b_uri = 0;
214     }
216     return -1;
220 void session_free(void) {
221     if (b_uri) {
222         ne_uri_free(&uri);
223         b_uri = 0;
224     }
226     free((char*) username);
227     free((char*) password);
228     free((char*) base_directory);
230     username = password = base_directory = NULL;
233 int session_is_local(const ne_uri *u) {
234     assert(u);
235     assert(b_uri);
237     return
238         strcmp(u->scheme, uri.scheme) == 0 &&
239         strcmp(u->host, uri.host) == 0 &&
240         u->port == uri.port;