File: cache.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 (584 lines) | stat: -rw-r--r-- 15,456 bytes parent folder | download | duplicates (2)
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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
/*
 * 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.
 */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif

#include <libsmbclient.h>
#include <pthread.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include <sys/param.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/types.h>
#include <signal.h>
#include <unistd.h>

#include "stringlist.h"
#include "smbctx.h"
#include "hash.h"
#include "configfile.h"
#include "debug.h"

#define MAX_SERVERLEN 255
#define MAX_WGLEN 255


stringlist_t *cache;
pthread_mutex_t cache_mutex = PTHREAD_MUTEX_INITIALIZER;

struct fusesmb_cache_opt {
    stringlist_t *ignore_servers;
    stringlist_t *ignore_workgroups;
};


config_t cfg;
struct fusesmb_cache_opt opts;

static void options_read(config_t *cfg, struct fusesmb_cache_opt *opt)
{
    opt->ignore_servers = NULL;
    if (-1 == config_read_stringlist(cfg, "ignore", "servers", &(opt->ignore_servers), ','))
    {
        opt->ignore_servers = NULL;
    }
    opt->ignore_workgroups = NULL;
    if (-1 == config_read_stringlist(cfg, "ignore", "workgroups", &(opt->ignore_workgroups), ','))
    {
        opt->ignore_workgroups = NULL;
    }
}

static void options_free(struct fusesmb_cache_opt *opt)
{
    if (NULL != opt->ignore_servers)
    {
        sl_free(opt->ignore_servers);
    }
    if (NULL != opt->ignore_workgroups)
    {
        sl_free(opt->ignore_workgroups);
    }
}


/*
 * Some servers refuse to return a server list using libsmbclient, so using
 *  broadcast lookup through nmblookup
 */
static int nmblookup(const char *wg, stringlist_t *sl, hash_t *ipcache)
{
    /* Find all ips for the workgroup by running :
    $ nmblookup 'workgroup_name'
    */
    char wg_cmd[512];
    snprintf(wg_cmd, 512, "nmblookup '%s'", wg);
    //fprintf(stderr, "%s\n", cmd);
    FILE *pipe;
    pipe = popen(wg_cmd, "r");
    if (pipe == NULL)
        return -1;

    int ip_cmd_size = 8192;
    char *ip_cmd = (char *)malloc(ip_cmd_size * sizeof(char));
    if (ip_cmd == NULL)
        return -1;
    strcpy(ip_cmd, "nmblookup -A ");
    int ip_cmd_len = strlen(ip_cmd);
    while (!feof(pipe))
    {
        /* Parse output that looks like this:
        querying boerderie on 172.20.91.255
        172.20.89.134 boerderie<00>
        172.20.89.191 boerderie<00>
        172.20.88.213 boerderie<00>
        */
        char buf[4096];
        if (NULL == fgets(buf, 4096, pipe))
            continue;

        char *pip = buf;
        /* Yes also include the space */
        while (isdigit(*pip) || *pip == '.' || *pip == ' ')
        {
            pip++;
        }
        *pip = '\0';
        int len = strlen(buf);
        if (len == 0) continue;
        ip_cmd_len += len;
        if (ip_cmd_len >= (ip_cmd_size -1))
        {
            ip_cmd_size *= 2;
            char *tmp = realloc(ip_cmd, ip_cmd_size *sizeof(char));
            if (tmp == NULL)
            {
                ip_cmd_size /= 2;
                ip_cmd_len -= len;
                continue;
            }
            ip_cmd = tmp;
        }
        /* Append the ip to the command:
        $ nmblookup -A ip1 ... ipn
        */
        strcat(ip_cmd, buf);
    }
    pclose(pipe);

    if (strlen(ip_cmd) == 13)
    {
        free(ip_cmd);
        return 0;
    }
    debug("%s\n", ip_cmd);
    pipe = popen(ip_cmd, "r");
    if (pipe == NULL)
    {
        free(ip_cmd);
        return -1;
    }

    while (!feof(pipe))
    {
        char buf2[4096];
        char buf[4096];
        char ip[32];

        char *start = buf;
        if (NULL == fgets(buf2, 4096, pipe))
            continue;
        /* Parse following input:
            Looking up status of 123.123.123.123
                    SERVER          <00> -         B <ACTIVE>
                    SERVER          <03> -         B <ACTIVE>
                    SERVER          <20> -         B <ACTIVE>
                    ..__MSBROWSE__. <01> - <GROUP> B <ACTIVE>
                    WORKGROUP       <00> - <GROUP> B <ACTIVE>
                    WORKGROUP       <1d> -         B <ACTIVE>
                    WORKGROUP       <1e> - <GROUP> B <ACTIVE>
        */
        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);
            debug("%s", ip);
        }
        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--;
            }
            sl_add(sl, start, 1);
            if (NULL == hash_lookup(ipcache, start))
                hash_alloc_insert(ipcache, strdup(start), strdup(ip));
            debug("%s : %s", ip, start);
        }

    }
    pclose(pipe);
    free(ip_cmd);
    return 0;
}

static int server_listing(SMBCCTX *ctx, stringlist_t *cache, const char *wg, const char *sv, const char *ip)
{
    //return 0;
    char tmp_path[MAXPATHLEN] = "smb://";
    if (ip != NULL)
    {
        strcat(tmp_path, ip);
    }
    else
    {
        strcat(tmp_path, sv);
    }

    struct smbc_dirent *share_dirent;
    SMBCFILE *dir;
    //SMBCCTX *ctx = fusesmb_new_context();
    dir = ctx->opendir(ctx, tmp_path);
    if (dir == NULL)
    {
        //smbc_free_context(ctx, 1);
        ctx->closedir(ctx, dir);
        return -1;
    }

    while (NULL != (share_dirent = ctx->readdir(ctx, dir)))
    {
        if (//share_dirent->name[strlen(share_dirent->name)-1] == '$' ||
            share_dirent->smbc_type != SMBC_FILE_SHARE ||
            share_dirent->namelen == 0)
            continue;
        if (0 == strcmp("ADMIN$", share_dirent->name) ||
            0 == strcmp("print$", share_dirent->name))
            continue;
        int len = strlen(wg)+ strlen(sv) + strlen(share_dirent->name) + 4;
        char tmp[len];
        snprintf(tmp, len, "/%s/%s/%s", wg, sv, share_dirent->name);
        debug("%s", tmp);
        pthread_mutex_lock(&cache_mutex);
        if (-1 == sl_add(cache, tmp, 1))
        {
            pthread_mutex_unlock(&cache_mutex);
            fprintf(stderr, "sl_add failed\n");
            ctx->closedir(ctx, dir);
            //smbc_free_context(ctx, 1);
            return -1;
        }
        pthread_mutex_unlock(&cache_mutex);

    }
    ctx->closedir(ctx, dir);
    //smbc_free_context(ctx, 1);
    return 0;
}

static void *workgroup_listing_thread(void *args)
{
    char *wg = (char *)args;
    //SMBCCTX *ctx, stringlist_t *cache, hash_t *ip_cache, const char *wg

    hash_t *ip_cache = hash_create(HASHCOUNT_T_MAX, NULL, NULL);
    if (NULL == ip_cache)
        return NULL;

    stringlist_t *servers = sl_init();
    if (NULL == servers)
    {
        fprintf(stderr, "Malloc failed\n");
        return NULL;
    }
    SMBCCTX *ctx = fusesmb_cache_new_context(&cfg);
    SMBCFILE *dir;
    char temp_path[MAXPATHLEN] = "smb://";
    strcat(temp_path, wg);
    debug("Looking up Workgroup: %s", wg);
    struct smbc_dirent *server_dirent;
    dir = ctx->opendir(ctx, temp_path);
    if (dir == NULL)
    {
        ctx->closedir(ctx, dir);

        goto use_popen;
    }
    while (NULL != (server_dirent = ctx->readdir(ctx, dir)))
    {
        if (server_dirent->namelen == 0 ||
            server_dirent->smbc_type != SMBC_SERVER)
        {
            continue;
        }

        if (-1 == sl_add(servers, server_dirent->name, 1))
            continue;


    }
    ctx->closedir(ctx, dir);

use_popen:


    nmblookup(wg, servers, ip_cache);
    sl_casesort(servers);

    size_t i;
    for (i=0; i < sl_count(servers); i++)
    {
        /* Skip duplicates */
        if (i > 0 && strcmp(sl_item(servers, i), sl_item(servers, i-1)) == 0)
            continue;

        /* Check if this server is in the ignore list in fusesmb.conf */
        if (NULL != opts.ignore_servers)
        {
            if (NULL != sl_find(opts.ignore_servers, sl_item(servers, i)))
            {
                debug("Ignoring %s", sl_item(servers, i));
                continue;
            }
        }
        char sv[1024] = "/";
        strcat(sv, sl_item(servers, i));
        int ignore = 0;

        /* Check if server specific option says ignore */
        if (0 == config_read_bool(&cfg, sv, "ignore", &ignore))
        {
            if (ignore == 1)
                continue;
        }

        hnode_t *node = hash_lookup(ip_cache, sl_item(servers, i));
        if (node == NULL)
            server_listing(ctx, cache, wg, sl_item(servers, i), NULL);
        else
            server_listing(ctx, cache, wg, sl_item(servers, i), hnode_get(node));
    }

    hscan_t sc;
    hnode_t *n;
    hash_scan_begin(&sc, ip_cache);
    while (NULL != (n = hash_scan_next(&sc)))
    {
        void *data = hnode_get(n);
        const void *key = hnode_getkey(n);
        hash_scan_delfree(ip_cache, n);
        free((void *)key);
        free(data);

    }
    hash_destroy(ip_cache);
    sl_free(servers);
    smbc_free_context(ctx, 1);
    return 0;
}


int cache_servers(SMBCCTX *ctx)
{
    //SMBCCTX *ctx = fusesmb_new_context();
    SMBCFILE *dir;
    struct smbc_dirent *workgroup_dirent;

    /* Initialize cache */
    cache = sl_init();
    size_t i;


    dir = ctx->opendir(ctx, "smb://");

    if (dir == NULL)
    {
        ctx->closedir(ctx, dir);
        sl_free(cache);
        //smbc_free_context(ctx, 1);
        return -1;
    }

    pthread_t *threads;
    threads = (pthread_t *)malloc(sizeof(pthread_t));
    if (NULL == threads)
        return -1;
    pthread_attr_t thread_attr;
    pthread_attr_init(&thread_attr);
    pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_JOINABLE);

    unsigned int num_threads = 0;

    while (NULL != (workgroup_dirent = ctx->readdir(ctx, dir)) )
    {
        if (workgroup_dirent->namelen == 0 ||
            workgroup_dirent->smbc_type != SMBC_WORKGROUP)
        {
            continue;
        }
        //char wg[1024];
        //strncpy(wg, workgroup_dirent->name, 1024);
        char *thread_arg = strdup(workgroup_dirent->name);

        if (opts.ignore_workgroups != NULL)
        {
            if (NULL != sl_find(opts.ignore_workgroups, workgroup_dirent->name))
            {
                debug("Ignoring Workgroup: %s", workgroup_dirent->name);
                continue;
            }
        }

        if (NULL == thread_arg)
            continue;
        int rc;
        rc = pthread_create(&threads[num_threads],
                             &thread_attr, workgroup_listing_thread,
                             (void*)thread_arg);
        //workgroup_listing(ctx, cache, ip_cache, wg);
        if (rc)
        {
            fprintf(stderr, "Failed to create thread for workgroup: %s\n", workgroup_dirent->name);
            free(thread_arg);
            continue;
        }
        num_threads++;
        threads = (pthread_t *)realloc(threads, (num_threads+1)*sizeof(pthread_t));
    }
    ctx->closedir(ctx, dir);

    //smbc_free_context(ctx, 1);

    pthread_attr_destroy(&thread_attr);

    for (i=0; i<num_threads; i++)
    {
        int rc = pthread_join(threads[i], NULL);
        if (rc)
        {
            fprintf(stderr, "Error while joining thread, errorcode: %d\n", rc);
            exit(-1);
        }
    }
    free(threads);

    sl_casesort(cache);
    char cachefile[1024];
    char tmp_cachefile[1024];
    snprintf(tmp_cachefile, 1024, "%s/.smb/fusesmb.cache.XXXXX", getenv("HOME"));
    mkstemp(tmp_cachefile);
    snprintf(cachefile, 1024, "%s/.smb/fusesmb.cache", getenv("HOME"));
    mode_t oldmask;
    oldmask = umask(022);
    FILE *fp = fopen(tmp_cachefile, "w");
    umask(oldmask);
    if (fp == NULL)
    {
        sl_free(cache);
        return -1;
    }

    for (i=0 ; i < sl_count(cache); i++)
    {
        fprintf(fp, "%s\n", sl_item(cache, i));
    }
    fclose(fp);
    /* Make refreshing cache file atomic */
    rename(tmp_cachefile, cachefile);
    sl_free(cache);
    return 0;
}

int main(int argc, char *argv[])
{
    char pidfile[1024];
    snprintf(pidfile, 1024, "%s/.smb/fusesmb.cache.pid", getenv("HOME"));

    char configfile[1024];
    snprintf(configfile, 1024, "%s/.smb/fusesmb.conf", getenv("HOME"));
    if (-1 == config_init(&cfg, configfile))
    {
        fprintf(stderr, "Could not open config file: %s (%s)", configfile, strerror(errno));
        exit(EXIT_FAILURE);
    }
    options_read(&cfg, &opts);

    struct stat st;
    if (argc == 1)
    {
        pid_t pid, sid;

        if (-1 != stat(pidfile, &st))
        {
                
            FILE *f;
            pid_t pid_from_file;
            int n;
            if ((f=fopen(pidfile, "r"))) 
            {
                n = fscanf(f, "%d", &pid_from_file);
                fclose(f);
                if (n != 1) 
                {
                    fprintf(stderr, "Error: Can't find pid in pid file\n");
                } 
                else 
                {
                    if ((kill (pid_from_file, 0)) == 0) 
                    {
                        fprintf(stderr, "Error: %s is already running with %d\n", argv[0], pid_from_file);
                        exit(EXIT_FAILURE);
                    } 
                    else 
                    {
                        unlink(pidfile);
                    }
                }
            }
            else
            {
                fprintf(stderr, "Error: Can't read pid file\n");
            }
        }

        pid = fork();
        if (pid < 0)
            exit(EXIT_FAILURE);
        if (pid > 0)
            exit(EXIT_SUCCESS);

        sid = setsid();
        if (sid < 0) {
            exit(EXIT_FAILURE);
        }
        if (chdir("/") < 0)
            exit(EXIT_FAILURE);

        mode_t oldmask;
        oldmask = umask(077);
        FILE *fp = fopen(pidfile, "w");
        umask(oldmask);
        if (NULL == fp)
            exit(EXIT_FAILURE);
        fprintf(fp, "%i\n", sid);
        fclose(fp);

        close(STDIN_FILENO);
        close(STDOUT_FILENO);
        close(STDERR_FILENO);
    }
    SMBCCTX *ctx = fusesmb_cache_new_context(&cfg);
    cache_servers(ctx);
    smbc_free_context(ctx, 1);
    options_free(&opts);
    if (argc == 1)
    {
        unlink(pidfile);
    }
    exit(EXIT_SUCCESS);
}