diff options
author | Simon Quigley <tsimonq2@ubuntu.com> | 2017-06-19 15:19:47 -0500 |
---|---|---|
committer | Simon Quigley <tsimonq2@ubuntu.com> | 2017-06-19 15:19:47 -0500 |
commit | 7aacc9f2510901c9e97b30fa9bcb550bb7f99c03 (patch) | |
tree | 16908948750c11da8332d80d8bb9b339399ee4d7 /modules/network/samba.c | |
parent | 7c47b5b9584f5011aeba18d7e1b26b3d3124825f (diff) |
New upstream version 0.5.1+git20170605
Diffstat (limited to 'modules/network/samba.c')
-rw-r--r-- | modules/network/samba.c | 124 |
1 files changed, 124 insertions, 0 deletions
diff --git a/modules/network/samba.c b/modules/network/samba.c new file mode 100644 index 00000000..71ba6ab6 --- /dev/null +++ b/modules/network/samba.c @@ -0,0 +1,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); +} + |