aboutsummaryrefslogtreecommitdiff
path: root/modules/network/samba.c
blob: 71ba6ab679e0b247cfe9a777ecb99ac529a1028f (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
/*
 *    HardInfo - Displays System Information
 *    Copyright (C) 2003-2009 Leandro A. F. Pereira <leandro@hardinfo.org>
 *
 *    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, version 2.
 *
 *    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 St, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include <string.h>

#include "hardinfo.h"
#include "network.h"

gchar *smb_shares_list = NULL;

void scan_samba_from_string(gchar *str, gsize length);
void scan_samba_usershares(void);

void
scan_samba(void)
{
    gchar *str;
    gsize length;
    
    if (smb_shares_list) {
        g_free(smb_shares_list);
        smb_shares_list = g_strdup("");
    }

    if (g_file_get_contents("/etc/samba/smb.conf",
                            &str, &length, NULL)) {
        shell_status_update("Scanning SAMBA shares...");
        scan_samba_from_string(str, length);
        g_free(str);                        
    }

    scan_samba_usershares();
}

void
scan_samba_usershares(void)
{
    FILE *usershare_list;
    
    if ((usershare_list = popen("net usershare list", "r"))) {
        char buffer[512];
        
        shell_status_update("Scanning SAMBA user shares...");
        
        while (fgets(buffer, 512, usershare_list)) {
            gchar *usershare, *cmdline;
            gsize length;
            
            cmdline = g_strdup_printf("net usershare info '%s'",
                                      strend(buffer, '\n'));
            if (g_spawn_command_line_sync(cmdline,
                                          &usershare, NULL,
                                          NULL, NULL)) {
                length = strlen(usershare);
                scan_samba_from_string(usershare, length);
                g_free(usershare);
            }
            
            g_free(cmdline);
            
            shell_status_pulse();
        }
        
        pclose(usershare_list);
    }
}

void
scan_samba_from_string(gchar *str, gsize length)
{
    GKeyFile *keyfile;
    GError *error = NULL;
    gchar **groups;
    gint i = 0;
    
    keyfile = g_key_file_new();
    
    gchar *_smbconf = str;
    for (; *_smbconf; _smbconf++)
        if (*_smbconf == ';') *_smbconf = '\0';
    
    if (!g_key_file_load_from_data(keyfile, str, length, 0, &error)) {
        smb_shares_list = g_strdup("Cannot parse smb.conf=\n");
        if (error)
            g_error_free(error);
        goto cleanup;
    }

    groups = g_key_file_get_groups(keyfile, NULL);
    while (groups[i]) {
        shell_status_pulse();

        if (g_key_file_has_key(keyfile, groups[i], "path", NULL)) {
            gchar *path = g_key_file_get_string(keyfile, groups[i], "path", NULL);
            smb_shares_list = h_strdup_cprintf("%s=%s\n",
                                               smb_shares_list,
                                               groups[i], path);
            g_free(path);
        }
        
        i++;
    }
    
    g_strfreev(groups);
  
  cleanup:
    g_key_file_free(keyfile);
}