2 Copyright (c) 2004-2006 Lennart Poettering
4 Permission is hereby granted, free of charge, to any person
5 obtaining a copy of this software and associated documentation files
6 (the "Software"), to deal in the Software without restriction,
7 including without limitation the rights to use, copy, modify, merge,
8 publish, distribute, sublicense, and/or sell copies of the Software,
9 and to permit persons to whom the Software is furnished to do so,
10 subject to the following conditions:
12 The above copyright notice and this permission notice shall be
13 included in all copies or substantial portions of the Software.
15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
18 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
19 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
20 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
38 #include <ne_request.h>
42 #include <ne_socket.h>
45 #include <ne_redirect.h>
50 static pthread_once_t session_once
= PTHREAD_ONCE_INIT
;
51 static pthread_key_t session_tsd_key
;
56 char *username
= NULL
;
57 static char *password
= NULL
;
58 char *base_directory
= NULL
;
60 static pthread_mutex_t credential_mutex
= PTHREAD_MUTEX_INITIALIZER
;
62 static char* ask_user(const char *p
, int hidden
) {
68 if (!isatty(fileno(stdin
)))
71 if (tcgetattr(fileno(stdin
), &t
) < 0)
76 if (tcsetattr(fileno(stdin
), TCSANOW
, &t
) < 0)
82 fprintf(stderr
, "%s: ", p
);
83 r
= fgets(q
, sizeof(q
), stdin
);
85 if (l
&& q
[l
-1] == '\n')
90 tcsetattr(fileno(stdin
), TCSANOW
, &t
);
91 fprintf(stderr
, "\n");
94 return r
? strdup(r
) : NULL
;
97 static int ssl_verify_cb(__unused
void *userdata
, __unused
int failures
, __unused
const ne_ssl_certificate
*cert
) {
101 static int ne_auth_creds_cb(__unused
void *userdata
, const char *realm
, int attempt
, char *u
, char *p
) {
104 pthread_mutex_lock(&credential_mutex
);
107 fprintf(stderr
, "Authentication failure!\n");
108 free((void*) username
);
109 free((void*) password
);
110 username
= password
= NULL
;
113 if (!username
|| !password
)
114 fprintf(stderr
, "Realm '%s' requires authentication.\n", realm
);
117 username
= ask_user("Username", 0);
119 if (username
&& !password
)
120 password
= ask_user("Password", 1);
122 if (username
&& password
) {
123 snprintf(u
, NE_ABUFSIZ
, "%s", username
);
124 snprintf(p
, NE_ABUFSIZ
, "%s", password
);
128 pthread_mutex_unlock(&credential_mutex
);
132 static ne_session
*session_open(int with_lock
) {
133 const char *scheme
= NULL
;
136 extern ne_lock_store
*lock_store
;
141 scheme
= uri
.scheme
? uri
.scheme
: "http";
143 if (!(session
= ne_session_create(scheme
, uri
.host
, uri
.port
? uri
.port
: ne_uri_defaultport(scheme
)))) {
144 fprintf(stderr
, "Failed to create session\n");
148 ne_ssl_set_verify(session
, ssl_verify_cb
, NULL
);
149 ne_set_server_auth(session
, ne_auth_creds_cb
, NULL
);
150 ne_redirect_register(session
);
152 if (with_lock
&& lock_store
)
153 ne_lockstore_register(lock_store
, session
);
158 static void session_destroy(void *s
) {
159 ne_session
*session
= s
;
161 ne_session_destroy(session
);
164 static void session_tsd_key_init(void) {
165 pthread_key_create(&session_tsd_key
, session_destroy
);
168 ne_session
*session_get(int with_lock
) {
171 pthread_once(&session_once
, session_tsd_key_init
);
173 if ((session
= pthread_getspecific(session_tsd_key
)))
176 session
= session_open(with_lock
);
177 pthread_setspecific(session_tsd_key
, session
);
182 int session_set_uri(const char *s
, const char *u
, const char *p
) {
189 if (ne_uri_parse(s
, &uri
)) {
190 fprintf(stderr
, "Invalid URI <%s>\n", s
);
197 fprintf(stderr
, "Missing host part in URI <%s>\n", s
);
201 base_directory
= strdup(uri
.path
);
202 l
= strlen(base_directory
);
203 if (base_directory
[l
-1] == '/')
204 ((char*) base_directory
)[l
-1] = 0;
207 username
= strdup(u
);
210 password
= strdup(p
);
225 void session_free(void) {
231 free((char*) username
);
232 free((char*) password
);
233 free((char*) base_directory
);
235 username
= password
= base_directory
= NULL
;
238 int session_is_local(const ne_uri
*u
) {
243 strcmp(u
->scheme
, uri
.scheme
) == 0 &&
244 strcmp(u
->host
, uri
.host
) == 0 &&