summaryrefslogtreecommitdiffstats
path: root/src/session.c
blob: ca5b89be3c34961eb90d76fb8dbd4b663b13dd93 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/***
  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 <config.h>
#endif

#include <stdio.h>
#include <assert.h>
#include <pthread.h>
#include <string.h>
#include <stdlib.h>
#include <termios.h>
#include <unistd.h>

#include <ne_uri.h>
#include <ne_request.h>
#include <ne_basic.h>
#include <ne_props.h>
#include <ne_utils.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;

ne_uri uri;
static int b_uri = 0;

char *username = NULL;
static char *password = NULL;
char *base_directory = NULL;

static pthread_mutex_t credential_mutex = PTHREAD_MUTEX_INITIALIZER;

static char* ask_user(const char *p, int hidden) {
    char q[256], *r;
    struct termios t;
    int c = 0, l;

    if (hidden) {
        if (!isatty(fileno(stdin)))
            hidden = 0;
        else {
            if (tcgetattr(fileno(stdin),  &t) < 0)
                hidden = 0;
            else {
                c = t.c_lflag;
                t.c_lflag &= ~ECHO;
                if (tcsetattr(fileno(stdin), TCSANOW, &t) < 0)
                    hidden = 0;
            }
        }
    }

    fprintf(stderr, "%s: ", p);
    r = fgets(q, sizeof(q), stdin);
    l = strlen(q);
    if (l && q[l-1] == '\n')
        q[l-1] = 0;

    if (hidden) {
        t.c_lflag = c;
        tcsetattr(fileno(stdin), TCSANOW, &t);
        fprintf(stderr, "\n");
    }

    return r ? strdup(r) : NULL;
}

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(__unused void *userdata, const char *realm, int attempt, char *u, char *p) {
    int r = -1;

    pthread_mutex_lock(&credential_mutex);

    if (attempt) {
        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);

    if (username && !password)
        password = ask_user("Password", 1);

    if (username && password) {
        snprintf(u, NE_ABUFSIZ, "%s", username);
        snprintf(p, NE_ABUFSIZ, "%s", password);
        r  = 0;
    }

    pthread_mutex_unlock(&credential_mutex);
    return r;
}

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;

    scheme = uri.scheme ? uri.scheme : "http";

    if (!(session = ne_session_create(scheme, uri.host, uri.port ? uri.port : ne_uri_defaultport(scheme)))) {
        fprintf(stderr, "Failed to create session\n");
        return NULL;
    }

    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;
}

static void session_destroy(void *s) {
    ne_session *session = s;
    assert(s);
    ne_session_destroy(session);
}

static void session_tsd_key_init(void) {
    pthread_key_create(&session_tsd_key, session_destroy);
}

ne_session *session_get(int with_lock) {
    ne_session *session;

    pthread_once(&session_once, session_tsd_key_init);

    if ((session = pthread_getspecific(session_tsd_key)))
        return session;

    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) {
    int l;

    assert(!b_uri);
    assert(!username);
    assert(!password);

    if (ne_uri_parse(s, &uri)) {
        fprintf(stderr, "Invalid URI <%s>\n", s);
        goto finish;
    }

    b_uri = 1;

    if (!uri.host) {
        fprintf(stderr, "Missing host part in URI <%s>\n", s);
        goto finish;
    }

    base_directory = strdup(uri.path);
    l = strlen(base_directory);
    if (base_directory[l-1] == '/')
        ((char*) base_directory)[l-1] = 0;

    if (u)
        username = strdup(u);

    if (p)
        password = strdup(p);

    return 0;

finish:

    if (b_uri) {
        ne_uri_free(&uri);
        b_uri = 0;
    }

    return -1;
}


void session_free(void) {
    if (b_uri) {
        ne_uri_free(&uri);
        b_uri = 0;
    }

    free((char*) username);
    free((char*) password);
    free((char*) base_directory);

    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;
}