File: smbctx.c

package info (click to toggle)
fusesmb 0.8.7-1.3
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd
  • size: 592 kB
  • ctags: 254
  • sloc: sh: 3,185; ansic: 2,912; makefile: 67
file content (282 lines) | stat: -rw-r--r-- 8,267 bytes parent folder | download | duplicates (5)
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
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
/*
 * Copyright (C) 2006 Vincent Wagelaar
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "smbctx.h"
#include "debug.h"

config_t *fusesmb_auth_fn_cfg = NULL;
pthread_mutex_t *fusesmb_auth_fn_cfg_mutex = NULL;

static int nmblookup(const char *ip_server, char *output, size_t outputsize)
{
    char ipcmd[1024] = "nmblookup -A ";
    strcat(ipcmd, ip_server);
    FILE *pipe = popen(ipcmd, "r");
    if (NULL == pipe)
        return -1;
    while (!feof(pipe))
    {
        char buf2[4096];
        char buf[4096];
        char ip[32];

        char *start = buf;
        if (NULL == fgets(buf2, 4096, pipe))
            continue;
        if (strncmp(buf2, "Looking up status of ", strlen("Looking up status of ")) == 0)
        {
            char *tmp = rindex(buf2, ' ');
            tmp++;
            char *end = index(tmp, '\n');
            *end = '\0';
            strcpy(ip, tmp);
        }
        else
        {
            continue;
        }

        while (!feof(pipe))
        {

            if (NULL == fgets(buf, 4096, pipe))
                break;
            char *sep = buf;

            if (*buf != '\t')
                break;
            if (NULL != strstr(buf, "<GROUP>"))
                break;
            if (NULL == (sep = strstr(buf, "<00>")))
                break;
            *sep = '\0';

            start++;

            while (*sep == '\t' || *sep == ' ' || *sep == '\0')
            {
                *sep = '\0';
                sep--;
            }
            strncpy(output, start, outputsize);
        }

    }
    pclose(pipe);
    return 0;
}


static void fusesmb_auth_fn(const char *server, const char *share,
                            char *workgroup, int wgmaxlen,
                            char *username, int unmaxlen,
                            char *password, int pwmaxlen)
{
    (void)workgroup;
    (void)wgmaxlen;

    /* Don't authenticate for workgroup listing */
    if (NULL == server || server[0] == '\0')
    {
        debug("empty server name");
        return;
    }
    /* Look for username, password for /SERVER/SHARE in the config file */
    char sv_section[1024] = "/";
    strcat(sv_section, server);
    strcat(sv_section, "/");
    strcat(sv_section, share);

    char *un, *pw;
    pthread_mutex_lock(fusesmb_auth_fn_cfg_mutex);
    debug("before share password");
    if (0 == config_read_string(fusesmb_auth_fn_cfg, sv_section, "username", &un))
    {
        if (0 == config_read_string(fusesmb_auth_fn_cfg, sv_section, "password", &pw))
        {
            debug("found share username, password");
            pthread_mutex_unlock(fusesmb_auth_fn_cfg_mutex);
            strncpy(username, un, unmaxlen);
            strncpy(password, pw, pwmaxlen);
            free(un);
            free(pw);
            return;
        }
        free(un);
    }

    /* Look for username, password for /SERVER in the config file */
    strcpy(sv_section, "/");
    strcat(sv_section, server);
    debug("before server password");
    if (0 == config_read_string(fusesmb_auth_fn_cfg, sv_section, "username", &un))
    {
        if (0 == config_read_string(fusesmb_auth_fn_cfg, sv_section, "password", &pw))
        {
            debug("found server username, password");
            pthread_mutex_unlock(fusesmb_auth_fn_cfg_mutex);
            strncpy(username, un, unmaxlen);
            strncpy(password, pw, pwmaxlen);
            free(un);
            free(pw);
            return;
        }
        free(un);
    }

    /* Look for global username, password in the config file */
    debug("before global password");
    if (0 == config_read_string(fusesmb_auth_fn_cfg, "global", "username", &un))
    {
        if (0 == config_read_string(fusesmb_auth_fn_cfg, "global", "password", &pw))
        {
            debug("found global username, password");
            pthread_mutex_unlock(fusesmb_auth_fn_cfg_mutex);
            strncpy(username, un, unmaxlen);
            strncpy(password, pw, pwmaxlen);
            free(un);
            free(pw);
            return;
        }
        free(un);
    }
    
    pthread_mutex_unlock(fusesmb_auth_fn_cfg_mutex);
    debug("before guest password");
    username = NULL;
    password = NULL;
    return;
}

static void fusesmb_cache_auth_fn(const char *server, const char *share,
                                  char *workgroup, int wgmaxlen,
                                  char *username, int unmaxlen,
                                  char *password, int pwmaxlen)
{
    (void)workgroup;
    (void)wgmaxlen;
    char sv[1024];

    /* Don't authenticate for workgroup listing */
    if (NULL == server || server[0] == '\0')
    {
        fprintf(stderr, "empty server name");
        return;
    }
    debug("server: %s : share: %s : workgroup: %s", server, share, workgroup);

    /* Convert ip to server name */
    nmblookup(server, sv, 1024);

    /* Look for username, password for /SERVER/SHARE in the config file */
    char sv_section[1024] = "/";
    strcat(sv_section, sv);
    strcat(sv_section, "/");
    strcat(sv_section, share);

    char *un, *pw;
    if (0 == config_read_string(fusesmb_auth_fn_cfg, sv_section, "username", &un))
    {
        if (0 == config_read_string(fusesmb_auth_fn_cfg, sv_section, "password", &pw))
        {
            strncpy(username, un, unmaxlen);
            strncpy(password, pw, pwmaxlen);
            free(un);
            free(pw);
            return;
        }
        free(un);
    }

    /* Look for username, password for /SERVER in the config file */
    strcpy(sv_section, "/");
    strcat(sv_section, sv);

    if (0 == config_read_string(fusesmb_auth_fn_cfg, sv_section, "username", &un))
    {
        if (0 == config_read_string(fusesmb_auth_fn_cfg, sv_section, "password", &pw))
        {
            strncpy(username, un, unmaxlen);
            strncpy(password, pw, pwmaxlen);
            free(un);
            free(pw);
            return;
        }
        free(un);
    }

    /* Look for global username, password in the config file */
    if (0 == config_read_string(fusesmb_auth_fn_cfg, "global", "username", &un))
    {
        if (0 == config_read_string(fusesmb_auth_fn_cfg, "global", "password", &pw))
        {
            strncpy(username, un, unmaxlen);
            strncpy(password, pw, pwmaxlen);
            free(un);
            free(pw);
            return;
        }
        free(un);
    }
    username = NULL;
    password = NULL;
    return;
}

/*
 * Create a new libsmbclient context with all necessary options
 */
static SMBCCTX *fusesmb_context(smbc_get_auth_data_fn fn)
{
    /* Initializing libsbmclient */
    SMBCCTX *ctx;
    ctx = smbc_new_context();
    if (ctx == NULL)
        return NULL;

    ctx->callbacks.auth_fn = fn;
    //ctx->debug = 4;
    /* Timeout a bit bigger, by Jim Ramsay */
    ctx->timeout = 10000;       //10 seconds
    /* Kerberos authentication by Esben Nielsen */
#if defined(SMB_CTX_FLAG_USE_KERBEROS) && defined(SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS)
    ctx->flags |=
        SMB_CTX_FLAG_USE_KERBEROS | SMB_CTX_FLAG_FALLBACK_AFTER_KERBEROS;
#endif
    //ctx->options.one_share_per_server = 1;
    ctx = smbc_init_context(ctx);
    return ctx;
}

SMBCCTX *fusesmb_cache_new_context(config_t *cf)
{
    fusesmb_auth_fn_cfg = cf;
    return fusesmb_context(fusesmb_cache_auth_fn);
}

SMBCCTX *fusesmb_new_context(config_t *cf, pthread_mutex_t *mutex)
{
    fusesmb_auth_fn_cfg = cf;
    fusesmb_auth_fn_cfg_mutex = mutex;
    return fusesmb_context(fusesmb_auth_fn);
}