From ba2bbc75e63b968d03da1094fcb8f28a1f734b39 Mon Sep 17 00:00:00 2001 From: Jan Lipovsky Date: Fri, 28 May 2010 13:59:28 +0200 Subject: Release --- sfshare-daemon/Makefile.am | 39 +- sfshare-daemon/data/Makefile.am | 25 +- .../data/org.fedoraproject.SimpleFileShare.service | 4 - .../org.fedoraproject.SimpleFileShare.service.in | 4 + sfshare-daemon/dbus_service.c | 459 ++++++++++ sfshare-daemon/dbus_service.h | 47 + sfshare-daemon/dbus_service_glue.h | 212 +++++ sfshare-daemon/samba_share.c | 955 +++++++++++++++++++++ sfshare-daemon/samba_share.h | 84 ++ sfshare-daemon/sfshare_errors.h | 49 ++ sfshare-daemon/sfshared.c | 33 + sfshare-daemon/src/Makefile.am | 6 - sfshare-daemon/src/dbus_service.c | 457 ---------- sfshare-daemon/src/dbus_service.h | 44 - sfshare-daemon/src/dbus_service_glue.h | 212 ----- sfshare-daemon/src/samba_share.c | 952 -------------------- sfshare-daemon/src/samba_share.h | 81 -- sfshare-daemon/src/sfshare_errors.h | 46 - sfshare-daemon/src/sfshared.c | 30 - 19 files changed, 1900 insertions(+), 1839 deletions(-) delete mode 100755 sfshare-daemon/data/org.fedoraproject.SimpleFileShare.service create mode 100755 sfshare-daemon/data/org.fedoraproject.SimpleFileShare.service.in create mode 100755 sfshare-daemon/dbus_service.c create mode 100755 sfshare-daemon/dbus_service.h create mode 100755 sfshare-daemon/dbus_service_glue.h create mode 100755 sfshare-daemon/samba_share.c create mode 100755 sfshare-daemon/samba_share.h create mode 100755 sfshare-daemon/sfshare_errors.h create mode 100755 sfshare-daemon/sfshared.c delete mode 100755 sfshare-daemon/src/Makefile.am delete mode 100644 sfshare-daemon/src/dbus_service.c delete mode 100644 sfshare-daemon/src/dbus_service.h delete mode 100644 sfshare-daemon/src/dbus_service_glue.h delete mode 100644 sfshare-daemon/src/samba_share.c delete mode 100644 sfshare-daemon/src/samba_share.h delete mode 100644 sfshare-daemon/src/sfshare_errors.h delete mode 100644 sfshare-daemon/src/sfshared.c (limited to 'sfshare-daemon') diff --git a/sfshare-daemon/Makefile.am b/sfshare-daemon/Makefile.am index 834fbb9..6998c17 100755 --- a/sfshare-daemon/Makefile.am +++ b/sfshare-daemon/Makefile.am @@ -1,5 +1,36 @@ -AUTOMAKE_OPTIONS = foreign +NULL = -SUBDIRS = \ - src \ - data +SUBDIRS = data + +INCLUDES = \ + -DDATADIR=\"$(datadir)\" \ + -DGNOMELOCALEDIR=\""$(datadir)/locale"\" \ + -I$(top_srcdir) \ + -I$(top_builddir) \ + $(WARN_CFLAGS) \ + $(DISABLE_DEPRECATED_CFLAGS) \ + $(GLIB_CFLAGS) \ + $(GTK_CFLAGS) \ + $(DBUS_GLIB_CFLAGS) \ + $(POLKIT_CFLAGS) \ + $(NULL) + +LDADD = \ + $(GLIB_LIBS) \ + $(GTK_LIBS) \ + $(DBUS_GLIB_LIBS) \ + $(POLKIT_LIBS) \ + $(NULL) + + +libexec_PROGRAMS = sfshared + +sfshared_SOURCES = \ + dbus_service.h \ + dbus_service.c \ + dbus_service_glue.h \ + samba_share.h \ + samba_share.c \ + sfshare_errors.h \ + sfshared.c \ + $(NULL) diff --git a/sfshare-daemon/data/Makefile.am b/sfshare-daemon/data/Makefile.am index 0e32e00..67fbe11 100755 --- a/sfshare-daemon/data/Makefile.am +++ b/sfshare-daemon/data/Makefile.am @@ -1,7 +1,26 @@ -#/etc/dbus-1/system.d -dbusconfdir = $(sysconfdir)/dbus-1/system.d/ + +dbusxmldir = $(datadir)/dbus-1/interfaces +dbusxml_DATA = org.fedoraproject.SimpleFileShare.xml + +dbusconfdir = /etc/dbus-1/system.d/ +#/etc/dbus-1/system.d/ +#$(sysconfdir)/dbus-1/system.d/ dbusconf_DATA = org.fedoraproject.SimpleFileShare.conf +polkitactionsdir = /usr/share/polkit-1/actions/ +# /usr/share/polkit-1/actions/ +#$(datadir)/polkit-1/actions/ +polkitactions_DATA = org.fedoraproject.SimpleFileShare.policy + +dbusservicedir = /usr/share/dbus-1/system-services +#/usr/share/dbus-1/system-services +#$(datadir)/dbus-1/system-services +dbusservice_in_files = org.fedoraproject.SimpleFileShare.service.in +dbusservice_DATA = $(dbusservice_in_files:.service.in=.service) + +$(dbusservice_DATA): $(dbusservice_in_files) Makefile + @sed -e "s|\@libexecdir\@|$(libexecdir)|" $< >$@ +EXTRA_DIST = $(dbusconf_DATA) $(dbusxml_DATA) $(polkitactions_DATA) $(dbusservice_DATA) org.fedoraproject.SimpleFileShare.xml -EXTRA_DIST = $(dbusconf_DATA) +DISTCLEANFILES = $(dbusservice_DATA) diff --git a/sfshare-daemon/data/org.fedoraproject.SimpleFileShare.service b/sfshare-daemon/data/org.fedoraproject.SimpleFileShare.service deleted file mode 100755 index ebf3c37..0000000 --- a/sfshare-daemon/data/org.fedoraproject.SimpleFileShare.service +++ /dev/null @@ -1,4 +0,0 @@ -[D-BUS Service] -Name=org.fedoraproject.SimpleFileShare -Exec=/usr/sbin/sfshared -User=root diff --git a/sfshare-daemon/data/org.fedoraproject.SimpleFileShare.service.in b/sfshare-daemon/data/org.fedoraproject.SimpleFileShare.service.in new file mode 100755 index 0000000..1df92b9 --- /dev/null +++ b/sfshare-daemon/data/org.fedoraproject.SimpleFileShare.service.in @@ -0,0 +1,4 @@ +[D-BUS Service] +Name=org.fedoraproject.SimpleFileShare +Exec=@libexecdir@/sfshared +User=root diff --git a/sfshare-daemon/dbus_service.c b/sfshare-daemon/dbus_service.c new file mode 100755 index 0000000..3e08c6d --- /dev/null +++ b/sfshare-daemon/dbus_service.c @@ -0,0 +1,459 @@ +/* + * dbus_service.c + * + * 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 library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * Author: Jan Lipovsky + */ +#include +#include +#include +#include +#include +#include + +#include +#include + +#include + +#include "samba_share.h" +#include "dbus_service.h" +#include "sfshare_errors.h" + +/* DBUS metods */ +#define ACTION_ID_SETUP_SHARE "org.fedoraproject.SimpleFileShare.setup_share" +#define ASTION_ID_GET_SHARE_STATUS "org.fedoraproject.SimpleFileShare.get_share_status" +#define ASTION_ID_DELETE_SHARE "org.fedoraproject.SimpleFileShare.delete_share" + + +typedef struct DaemonObject DaemonObject; +typedef struct DaemonObjectClass DaemonObjectClass; + +static PolkitSubject *subject_dbus = NULL; +static PolkitAuthority *authority = NULL; + + +gboolean is_authorized = FALSE; +DBusGConnection *bus; +DBusGProxy *bus_proxy; + +GType daemon_object_get_type (void); + +struct DaemonObject +{ + GObject parent; +}; + +struct DaemonObjectClass +{ + GObjectClass parent; +}; + + +#define DAEMON_TYPE_OBJECT (daemon_object_get_type ()) +#define DAEMON_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), DAEMON_TYPE_OBJECT, DaemonObject)) +#define DAEMON_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DAEMON_TYPE_OBJECT, DaemonObjectClass)) +#define DAEMON_IS_OBJECT(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), DAEMON_TYPE_OBJECT)) +#define DAEMON_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DAEMON_TYPE_OBJECT)) +#define DAEMON_OBJECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DAEMON_TYPE_OBJECT, DaemonObjectClass)) + +G_DEFINE_TYPE(DaemonObject, daemon_object, G_TYPE_OBJECT) + +gboolean daemon_get_share_status (DaemonObject *obj, const gchar *path, gchar ***result, GError **error); + +/* Async calls */ +gboolean daemon_setup_share (DaemonObject *obj, const gchar **parameters, DBusGMethodInvocation *context); +gboolean daemon_delete_share (DaemonObject *obj,const gchar *path, DBusGMethodInvocation *context); + +#include "dbus_service_glue.h" + +/** +* Return text of Error +*/ +gchar * +get_error_msg (Error_sfshare err) +{ + gchar *errmsg; + + switch (err) + { + case ERROR_FAILED: + errmsg = g_strdup ("Failed"); + break; + + case ERROR_PERMISSION_DENIED: + errmsg = g_strdup ("Permission denied"); + break; + + case ERROR_FILE_NOT_EXIST: + errmsg = g_strdup ("Config file does not exist"); + break; + + case ERROR_CAN_NOT_OPEN_FILE: + errmsg = g_strdup ("Can not open config file"); + break; + + case ERROR_DIRECTORY_NOT_SHARED: + errmsg = g_strdup ("Directory is not shared"); + break; + + case ERROR_WRONG_NAME: + errmsg = g_strdup ("Wrong name"); + break; + + case ERROR_WRONG_PATH: + errmsg = g_strdup ("Wrong path"); + break; + + case ERROR_READONLY_WRITABLE: + errmsg = g_strdup ("Read only is same as writable"); + break; + + case ERROR_SHARE_NAME_EXIST: + errmsg = g_strdup ("Share name already exist"); + break; + + default: + errmsg = g_strdup ("Unknow error type"); + break; + } + + return errmsg; +} + + +GQuark +get_error_quark (void) +{ + static GQuark ret = 0; + + if (ret == 0) + { + ret = g_quark_from_static_string ("sfshare_daemon_error"); + } + return ret; +} + + +/** Return error to context */ +static void +send_error (DBusGMethodInvocation *context, + gint error_code, + const gchar *format, + ...) +{ + GError *error; + va_list args; + gchar *message; + + va_start (args, format); + message = g_strdup_vprintf (format, args); + va_end (args); + + error = g_error_new (ERROR_QUARK, error_code, "%s", message); + dbus_g_method_return_error (context, error); + g_error_free (error); + + g_free (message); +} + + +/** +* Create pointer to DaemonData filled with values from parameters +*/ +DaemonData * +daemon_data_new ( DBusGMethodInvocation *context, AuthorizedCallback auth_cb, const gchar **in) +{ + DaemonData *ret; + + ret = g_new0 (DaemonData, 1); + + ret->context = context; + ret->authorized_cb = auth_cb; + + ret->in = g_new0 (gchar *, 6); + + gint i = 0; + + while(in[i] != NULL) + { + ret->in[i] = g_strdup (in[i]); + i++; + } + + return ret; +} + +/** +* Free DaemonData pointer +*/ +void +daemon_data_free (DaemonData * data) +{ + gint i; + i = 0; + + while (data->in[i] != NULL) + { + g_free (data->in[i++]); + } + + g_free (data); + +} + + +/** +* PolicyKit callback function +*/ +static void +check_authorization_cb (PolkitAuthority *authority, + GAsyncResult *res, + DaemonData *data) +{ + GError *error; + PolkitAuthorizationResult *result; + + + is_authorized = FALSE; + + error = NULL; + result = polkit_authority_check_authorization_finish (authority, res, &error); + if (error != NULL) + { + send_error (data->context, ERROR_PERMISSION_DENIED, "Not authorized: %s", error->message); + g_error_free (error); + } + else + { + if (polkit_authorization_result_get_is_authorized (result)) + { + is_authorized = TRUE; + } + else if (polkit_authorization_result_get_is_challenge (result)) + { + send_error (data->context, ERROR_PERMISSION_DENIED, "Authentication is required"); + } + else + { + send_error (data->context, ERROR_PERMISSION_DENIED, "Not authorized"); + } + } + + if(is_authorized) + { + /* Run authorized callback */ + (* data->authorized_cb) (data); + } + + daemon_data_free (data); +} + + + + + + +static void +daemon_object_init (DaemonObject *obj) +{ +} + +static void +daemon_object_class_init (DaemonObjectClass *klass) +{ +} + +/** +* Handle incomming request - D-Bus get_share_status +*/ +gboolean +daemon_get_share_status (DaemonObject *obj, const gchar *path, gchar ***result, GError **error) +{ + /* puts("daemon_get_share_status"); */ + + Error_sfshare err; + + err = smb_get_share_status (path, result); + + if (err != OK) + { + *error = g_error_new (ERROR_QUARK, err, "%s", get_error_msg(err)); + return FALSE; + } + else + return TRUE; +} + +/** +* Authorized call of smb_set_share - write or change share section +*/ +void +setup_share_authorized (gpointer data) +{ + Error_sfshare err; + DaemonData *dd = data; + + err = smb_set_share (dd->in); + + if (err != OK) + { + send_error (dd->context, err, "%s", get_error_msg(err)); + } + + smb_reload_service (); + + /* Return */ + dbus_g_method_return (dd->context); +} + + + +/** +* Dbus setup share - polkit authorization check +*/ +gboolean +daemon_setup_share (DaemonObject *obj,const gchar **parameters, DBusGMethodInvocation *context) +{ + /* puts ("daemon_setup_share"); */ + + DaemonData *data; + data = daemon_data_new (context, setup_share_authorized, parameters); + + subject_dbus = polkit_system_bus_name_new (dbus_g_method_get_sender (context)); + + polkit_authority_check_authorization (authority, + subject_dbus, + ACTION_ID_SETUP_SHARE, + NULL, /* PolkitDetails */ + POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE, + NULL, + (GAsyncReadyCallback) check_authorization_cb, + data); + return TRUE; +} + + + +/** +* Authorized call of smb_delete_share - delete share section +*/ +void +delete_share_authorized (gpointer data) +{ + Error_sfshare err; + DaemonData *dd = data; + + err = smb_delete_share(dd->in); + + if (err != OK) + { + send_error (dd->context, err, "%s", get_error_msg(err)); + } + + smb_reload_service (); + + /* Return */ + dbus_g_method_return (dd->context); +} + + +/** +* Dbus delete share - polkit authorization check +*/ +gboolean +daemon_delete_share (DaemonObject *obj, const gchar *path, DBusGMethodInvocation *context) +{ + /* puts("daemon_delete_share"); */ + + DaemonData *data; + + const gchar *in[2] = {path, NULL}; + + data = daemon_data_new (context, delete_share_authorized, in); + + subject_dbus = polkit_system_bus_name_new (dbus_g_method_get_sender (context)); + + polkit_authority_check_authorization (authority, + subject_dbus, + ASTION_ID_DELETE_SHARE, + NULL, /* PolkitDetails */ + POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE, + NULL, + (GAsyncReadyCallback) check_authorization_cb, + data); + + return TRUE; +} + + +/** +* Start of sfshare deamon +*/ +int +dbus_sfshare_start () +{ + GError *error = NULL; + GMainLoop *mainloop; + guint request_name_result; + DaemonObject *obj; + + g_type_init (); + + dbus_g_object_type_install_info (DAEMON_TYPE_OBJECT, &dbus_glib_daemon_object_info); + + mainloop = g_main_loop_new (NULL, FALSE); + + bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error); + if (!bus) + { + g_warning ("Couldn't connect to system bus : %s", error->message); + g_error_free (error); + return -1; + } + + + /* PolKit */ + authority = polkit_authority_get (); + + bus_proxy = dbus_g_proxy_new_for_name (bus, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS); + + if (!dbus_g_proxy_call (bus_proxy, "RequestName", &error, + G_TYPE_STRING, "org.fedoraproject.SimpleFileShare", + G_TYPE_UINT, 0, + G_TYPE_INVALID, + G_TYPE_UINT, &request_name_result, + G_TYPE_INVALID)) + { + g_warning("Failed to acquire org.fedoraproject.SimpleFileShare : %s", error->message); + g_error_free (error); + return -1; + } + + + obj = g_object_new (DAEMON_TYPE_OBJECT, NULL); + + dbus_g_connection_register_g_object (bus, "/org/fedoraproject/SimpleFileShare", G_OBJECT (obj)); + + g_print ("Simple File Share - service running\n"); + + g_main_loop_run (mainloop); + + g_object_unref (obj); + + + return 0; +} diff --git a/sfshare-daemon/dbus_service.h b/sfshare-daemon/dbus_service.h new file mode 100755 index 0000000..3888352 --- /dev/null +++ b/sfshare-daemon/dbus_service.h @@ -0,0 +1,47 @@ +/* + * dbus_service.c + * + * 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 library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * Author: Jan Lipovsky + */ + +#ifndef DBUS_SERVICE_H +#define DBUS_SERVICE_H + +#include +#include + + + +typedef void (*AuthorizedCallback) (void *data); + +typedef struct +{ + DBusGMethodInvocation *context; + gchar **in; + AuthorizedCallback authorized_cb; + +} DaemonData; + +/** Daemon start */ +int dbus_sfshare_start (); + +GQuark get_error_quark (void); +#define ERROR_QUARK get_error_quark () + + + +#endif diff --git a/sfshare-daemon/dbus_service_glue.h b/sfshare-daemon/dbus_service_glue.h new file mode 100755 index 0000000..ec1b428 --- /dev/null +++ b/sfshare-daemon/dbus_service_glue.h @@ -0,0 +1,212 @@ +/* Generated by dbus-binding-tool; do not edit! */ + + +#ifndef __dbus_glib_marshal_daemon_MARSHAL_H__ +#define __dbus_glib_marshal_daemon_MARSHAL_H__ + +#include + +G_BEGIN_DECLS + +#ifdef G_ENABLE_DEBUG +#define g_marshal_value_peek_boolean(v) g_value_get_boolean (v) +#define g_marshal_value_peek_char(v) g_value_get_char (v) +#define g_marshal_value_peek_uchar(v) g_value_get_uchar (v) +#define g_marshal_value_peek_int(v) g_value_get_int (v) +#define g_marshal_value_peek_uint(v) g_value_get_uint (v) +#define g_marshal_value_peek_long(v) g_value_get_long (v) +#define g_marshal_value_peek_ulong(v) g_value_get_ulong (v) +#define g_marshal_value_peek_int64(v) g_value_get_int64 (v) +#define g_marshal_value_peek_uint64(v) g_value_get_uint64 (v) +#define g_marshal_value_peek_enum(v) g_value_get_enum (v) +#define g_marshal_value_peek_flags(v) g_value_get_flags (v) +#define g_marshal_value_peek_float(v) g_value_get_float (v) +#define g_marshal_value_peek_double(v) g_value_get_double (v) +#define g_marshal_value_peek_string(v) (char*) g_value_get_string (v) +#define g_marshal_value_peek_param(v) g_value_get_param (v) +#define g_marshal_value_peek_boxed(v) g_value_get_boxed (v) +#define g_marshal_value_peek_pointer(v) g_value_get_pointer (v) +#define g_marshal_value_peek_object(v) g_value_get_object (v) +#else /* !G_ENABLE_DEBUG */ +/* WARNING: This code accesses GValues directly, which is UNSUPPORTED API. + * Do not access GValues directly in your code. Instead, use the + * g_value_get_*() functions + */ +#define g_marshal_value_peek_boolean(v) (v)->data[0].v_int +#define g_marshal_value_peek_char(v) (v)->data[0].v_int +#define g_marshal_value_peek_uchar(v) (v)->data[0].v_uint +#define g_marshal_value_peek_int(v) (v)->data[0].v_int +#define g_marshal_value_peek_uint(v) (v)->data[0].v_uint +#define g_marshal_value_peek_long(v) (v)->data[0].v_long +#define g_marshal_value_peek_ulong(v) (v)->data[0].v_ulong +#define g_marshal_value_peek_int64(v) (v)->data[0].v_int64 +#define g_marshal_value_peek_uint64(v) (v)->data[0].v_uint64 +#define g_marshal_value_peek_enum(v) (v)->data[0].v_long +#define g_marshal_value_peek_flags(v) (v)->data[0].v_ulong +#define g_marshal_value_peek_float(v) (v)->data[0].v_float +#define g_marshal_value_peek_double(v) (v)->data[0].v_double +#define g_marshal_value_peek_string(v) (v)->data[0].v_pointer +#define g_marshal_value_peek_param(v) (v)->data[0].v_pointer +#define g_marshal_value_peek_boxed(v) (v)->data[0].v_pointer +#define g_marshal_value_peek_pointer(v) (v)->data[0].v_pointer +#define g_marshal_value_peek_object(v) (v)->data[0].v_pointer +#endif /* !G_ENABLE_DEBUG */ + + +/* NONE:STRING,POINTER */ +extern void dbus_glib_marshal_daemon_VOID__STRING_POINTER (GClosure *closure, + GValue *return_value, + guint n_param_values, + const GValue *param_values, + gpointer invocation_hint, + gpointer marshal_data); +void +dbus_glib_marshal_daemon_VOID__STRING_POINTER (GClosure *closure, + GValue *return_value G_GNUC_UNUSED, + guint n_param_values, + const GValue *param_values, + gpointer invocation_hint G_GNUC_UNUSED, + gpointer marshal_data) +{ + typedef void (*GMarshalFunc_VOID__STRING_POINTER) (gpointer data1, + gpointer arg_1, + gpointer arg_2, + gpointer data2); + register GMarshalFunc_VOID__STRING_POINTER callback; + register GCClosure *cc = (GCClosure*) closure; + register gpointer data1, data2; + + g_return_if_fail (n_param_values == 3); + + if (G_CCLOSURE_SWAP_DATA (closure)) + { + data1 = closure->data; + data2 = g_value_peek_pointer (param_values + 0); + } + else + { + data1 = g_value_peek_pointer (param_values + 0); + data2 = closure->data; + } + callback = (GMarshalFunc_VOID__STRING_POINTER) (marshal_data ? marshal_data : cc->callback); + + callback (data1, + g_marshal_value_peek_string (param_values + 1), + g_marshal_value_peek_pointer (param_values + 2), + data2); +} +#define dbus_glib_marshal_daemon_NONE__STRING_POINTER dbus_glib_marshal_daemon_VOID__STRING_POINTER + +/* BOOLEAN:STRING,POINTER,POINTER */ +extern void dbus_glib_marshal_daemon_BOOLEAN__STRING_POINTER_POINTER (GClosure *closure, + GValue *return_value, + guint n_param_values, + const GValue *param_values, + gpointer invocation_hint, + gpointer marshal_data); +void +dbus_glib_marshal_daemon_BOOLEAN__STRING_POINTER_POINTER (GClosure *closure, + GValue *return_value G_GNUC_UNUSED, + guint n_param_values, + const GValue *param_values, + gpointer invocation_hint G_GNUC_UNUSED, + gpointer marshal_data) +{ + typedef gboolean (*GMarshalFunc_BOOLEAN__STRING_POINTER_POINTER) (gpointer data1, + gpointer arg_1, + gpointer arg_2, + gpointer arg_3, + gpointer data2); + register GMarshalFunc_BOOLEAN__STRING_POINTER_POINTER callback; + register GCClosure *cc = (GCClosure*) closure; + register gpointer data1, data2; + gboolean v_return; + + g_return_if_fail (return_value != NULL); + g_return_if_fail (n_param_values == 4); + + if (G_CCLOSURE_SWAP_DATA (closure)) + { + data1 = closure->data; + data2 = g_value_peek_pointer (param_values + 0); + } + else + { + data1 = g_value_peek_pointer (param_values + 0); + data2 = closure->data; + } + callback = (GMarshalFunc_BOOLEAN__STRING_POINTER_POINTER) (marshal_data ? marshal_data : cc->callback); + + v_return = callback (data1, + g_marshal_value_peek_string (param_values + 1), + g_marshal_value_peek_pointer (param_values + 2), + g_marshal_value_peek_pointer (param_values + 3), + data2); + + g_value_set_boolean (return_value, v_return); +} + +/* NONE:BOXED,POINTER */ +extern void dbus_glib_marshal_daemon_VOID__BOXED_POINTER (GClosure *closure, + GValue *return_value, + guint n_param_values, + const GValue *param_values, + gpointer invocation_hint, + gpointer marshal_data); +void +dbus_glib_marshal_daemon_VOID__BOXED_POINTER (GClosure *closure, + GValue *return_value G_GNUC_UNUSED, + guint n_param_values, + const GValue *param_values, + gpointer invocation_hint G_GNUC_UNUSED, + gpointer marshal_data) +{ + typedef void (*GMarshalFunc_VOID__BOXED_POINTER) (gpointer data1, + gpointer arg_1, + gpointer arg_2, + gpointer data2); + register GMarshalFunc_VOID__BOXED_POINTER callback; + register GCClosure *cc = (GCClosure*) closure; + register gpointer data1, data2; + + g_return_if_fail (n_param_values == 3); + + if (G_CCLOSURE_SWAP_DATA (closure)) + { + data1 = closure->data; + data2 = g_value_peek_pointer (param_values + 0); + } + else + { + data1 = g_value_peek_pointer (param_values + 0); + data2 = closure->data; + } + callback = (GMarshalFunc_VOID__BOXED_POINTER) (marshal_data ? marshal_data : cc->callback); + + callback (data1, + g_marshal_value_peek_boxed (param_values + 1), + g_marshal_value_peek_pointer (param_values + 2), + data2); +} +#define dbus_glib_marshal_daemon_NONE__BOXED_POINTER dbus_glib_marshal_daemon_VOID__BOXED_POINTER + +G_END_DECLS + +#endif /* __dbus_glib_marshal_daemon_MARSHAL_H__ */ + +#include +static const DBusGMethodInfo dbus_glib_daemon_methods[] = { + { (GCallback) daemon_get_share_status, dbus_glib_marshal_daemon_BOOLEAN__STRING_POINTER_POINTER, 0 }, + { (GCallback) daemon_setup_share, dbus_glib_marshal_daemon_NONE__BOXED_POINTER, 79 }, + { (GCallback) daemon_delete_share, dbus_glib_marshal_daemon_NONE__STRING_POINTER, 144 }, +}; + +const DBusGObjectInfo dbus_glib_daemon_object_info = { + 0, + dbus_glib_daemon_methods, + 3, +"org.fedoraproject.SimpleFileShare\0get_share_status\0S\0path\0I\0s\0status\0O\0F\0N\0as\0\0org.fedoraproject.SimpleFileShare\0setup_share\0A\0parameters\0I\0as\0\0org.fedoraproject.SimpleFileShare\0delete_share\0A\0path\0I\0s\0\0\0", +"\0", +"\0" +}; + diff --git a/sfshare-daemon/samba_share.c b/sfshare-daemon/samba_share.c new file mode 100755 index 0000000..a58042b --- /dev/null +++ b/sfshare-daemon/samba_share.c @@ -0,0 +1,955 @@ +/* + * samba_share.c + * + * 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 library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * Author: Jan Lipovsky + */ + +#include /* strlen, strstr */ +#include /* system() call */ + +#include +#include + +#include "samba_share.h" +#include "sfshare_errors.h" + +/* Define */ +#define SECTIONS_COUNT 3 /* Count of special sections in smb.conf */ +#define KEYWORDS_COUNT 6 /* Count of keywords used for setup share in smb.conf */ + + +/* [share name], "path =", "commennt =", "read only =", "writable =, writeable =, write ok =", "guest ok =" */ +const gchar *keywords[KEYWORDS_COUNT] = {"[" ,"path", "comment", "read only", "writ", "guest"}; + +typedef enum keywords_id +{ + SHARE_NAME_ID = 0, + PATH_ID, + COMMENT_ID, + READ_ONLY_ID, + WRTITABLE_ID, + GUEST_OK_ID +} TKeywords_id; + +/* Path too smb.conf */ +gchar *smb_conf_path = "/etc/samba/smb.conf"; +const gchar *smb_special_section [SECTIONS_COUNT] = {"global", "homes", "printers"}; + + +/** Send SIGHUP to smb and nmb */ +void +smb_reload_service () +{ + system("killall -HUP smb nmb"); +} + +/** +* Function changes path to smb.conf to path +*/ +void +set_smbconf_path (const gchar *path) +{ + smb_conf_path = g_strdup(path); +} + + +/** +* Save string str as new smb.conf +*/ +gint +write_smbconf (const gchar *content) +{ + FILE *smb_file; + + /* smb.conf - File exist test */ + if(!g_file_test (smb_conf_path, G_FILE_TEST_EXISTS)) + { + g_warning ("Config file \"%s\" does not exist!", smb_conf_path); + return ERROR_FILE_NOT_EXIST; + } + + /* Try open smb.conf - rewrite all */ + smb_file = fopen(smb_conf_path, "w"); + if(smb_file == NULL) + { + g_warning ("Can not open file \"%s\"! Maybe, you dont have rights for change smb.conf", smb_conf_path); + return ERROR_CAN_NOT_OPEN_FILE; + } + + /* Write to smb.conf */ + if(fputs (content,smb_file) == EOF) + { + g_warning ("Can not write to file \"%s\"!", smb_conf_path); + return ERROR_CAN_NOT_WRITE_TO_FILE; + } + + fclose(smb_file); + + return OK; +} + + +/** +* Function free TSmbConfItem from memmory +*/ +void +smbconf_item_free(TSmbConfItem *item) +{ + g_string_free(item->guest_ok, TRUE); + g_string_free(item->writable, TRUE); + g_string_free(item->read_only, TRUE); + g_string_free(item->comment, TRUE); + g_string_free(item->path, TRUE); + g_string_free(item->name, TRUE); + + g_free(item); +} + + +/** +* Function allocs memory for TSmbConfItem +*/ +TSmbConfItem* +smbconf_item_new0 () +{ + TSmbConfItem *ret; + ret = g_malloc(sizeof(struct smb_conf_item)); + + ret->name = g_string_new(""); + ret->path = g_string_new(""); + ret->comment = g_string_new(""); + ret->read_only = g_string_new(""); + ret->writable = g_string_new(""); + ret->guest_ok = g_string_new(""); + + return ret; +} + + +/** +* Function allocs memory for TSmbConfItem +*/ +TSmbConfItem* +smbconf_item_new (const gchar *name, const gchar *path, const gchar *comment, const gchar *read_only, const gchar *guest_ok) +{ + TSmbConfItem *ret; + ret = g_malloc(sizeof(struct smb_conf_item)); + + ret->name = g_string_new(name); + ret->path = g_string_new(path); + ret->comment = g_string_new(comment); + + ret->read_only = g_string_new(read_only); + + if (!g_strcmp0(read_only,"yes")) + ret->writable = g_string_new("no"); + else + ret->writable = g_string_new("yes"); + + ret->guest_ok = g_string_new(guest_ok); + + return ret; +} + +/** +* Function returns new array +*/ +GPtrArray* +shared_items_array_new () +{ + GPtrArray *ret = g_ptr_array_new(); + return ret; +} + + +/** +* Destroy array +*/ +void +shared_items_array_free (GPtrArray *array) +{ + TSmbConfItem *tmp; + gint i; + for(i = 0; i < array->len; i++) + { + tmp = g_ptr_array_index (array,i); + smbconf_item_free (tmp); + } + + g_ptr_array_free(array, TRUE); +} + + +/** +* If ok return 0 else return number of error +*/ +gint check_item(TSmbConfItem *item) +{ + if(item->name->len <= 0) + return ERROR_WRONG_NAME; + + if(item->path->len <= 0) + return ERROR_WRONG_PATH; + + /* read_only = true ... writable muust be = false */ + if(g_string_equal(item->writable, item->read_only) && item->writable->len > 0) + return ERROR_READONLY_WRITABLE; + + return OK; +} + + +/** +* Parse line of smb.conf and fill competent field of item structure +*/ +void +parse_to_share_item (gchar *txt, TSmbConfItem *item) +{ + gchar *lower = g_ascii_strdown(txt, strlen(txt)); + gboolean found = FALSE; + + /* Find keywords on line *txt */ + gint i; + for(i = 0; i < KEYWORDS_COUNT; i++) + { + gchar *point; + if((point = strstr(lower,keywords[i])) != NULL) + { + /* Keywords must be at begining of line */ + if(point == lower) + { + found = TRUE; + break; + } + } + } + + /* Save atributes - Boolean variables case-insensitive - yes, no, true, false, 1, or 0 */ + if(found) + switch(i) + { + /* [share name] */ + case SHARE_NAME_ID: + { + g_string_append_len(item->name, txt + 1, strlen(txt)-2); + } + break; + + /* path = */ + case PATH_ID: + { + gchar *tmp; + if((tmp = strstr(txt,"=")) != NULL) + { + g_string_assign(item->path, g_strstrip(tmp + 1)); + } + } + break; + + /* comment = */ + case COMMENT_ID: + { + gchar *tmp; + if((tmp = strstr(txt,"=")) != NULL) + { + g_string_assign(item->comment, g_strstrip(tmp + 1)); + } + } + break; + + /* read only = */ + case READ_ONLY_ID: + { + gchar *tmp; + if((tmp = strstr(lower,"=")) != NULL) + { + g_strstrip(++tmp); + + if(g_strcmp0(tmp, "yes") && g_strcmp0(tmp, "true") && g_strcmp0(tmp, "1")) + g_string_assign(item->read_only,"no"); + else + g_string_assign(item->read_only,"yes"); + } + } + break; + + /* writ = writable, writeable, write ok = */ + case WRTITABLE_ID: + { + gchar *tmp; + if((tmp = strstr(lower,"=")) != NULL) + { + g_strstrip(++tmp); + + if(g_strcmp0(tmp, "yes") && g_strcmp0(tmp, "true") && g_strcmp0(tmp, "1")) + g_string_assign(item->writable,"no"); + else + g_string_assign(item->writable,"yes"); + } + } + break; + + /* guest ok = */ + case GUEST_OK_ID: + { + gchar *tmp; + if((tmp = strstr(lower,"=")) != NULL) + { + g_strstrip(++tmp); + + if(g_strcmp0(tmp, "yes") && g_strcmp0(tmp, "true") && g_strcmp0(tmp, "1")) + g_string_assign(item->guest_ok,"no"); + else + g_string_assign(item->guest_ok,"yes"); + } + } + break; + + default: + break; + } + + g_free(lower); +} + + + +/** +* Funciton loads all share section to array +*/ +gint +load_smb_conf (GPtrArray *shared_items) +{ + + FILE *smb_file; /* Samba config file */ + gchar *line; /* Line buffer */ + gboolean skip = FALSE; /* Skip lines of special sections */ + + gint arr_index = shared_items->len - 1; /* Count of items in shared_items array - 1; */ + + /* smb.conf - File exist test */ + if (!g_file_test(smb_conf_path, G_FILE_TEST_EXISTS)) + { + g_warning ("Config file \"%s\" not exist!", smb_conf_path); + return ERROR_FILE_NOT_EXIST; + } + + /* Try open smb.conf */ + smb_file = fopen(smb_conf_path, "r"); + if (smb_file == NULL) + { + g_warning ("Can not open file \"%s\"!", smb_conf_path); + return ERROR_CAN_NOT_OPEN_FILE; + } + + /* Inicialize buffers */ + line = g_strnfill(BUFSIZ, '\0'); + + /* Parse smb.conf file and load shared folders setings */ + while (fgets(line, BUFSIZ, smb_file)) + { + /* Remove white space */ + g_strstrip(line); + + /* Skip comments and empty lines */ + if(line[0] == ';' || line[0] == '#' || line[0] == '\0') + continue; + + if(line[0] == '[') + { + skip = FALSE; + + /* Test special sections */ + gint i ; + for (i = 0; i < SECTIONS_COUNT; i++) + { + if(strstr(line,smb_special_section[i]) != NULL) + { + skip = TRUE; + break; + } + } + + if(!skip) + { + /* Start of share section - alloc new item of array */ + TSmbConfItem *tmp_item = smbconf_item_new0(); + + /* Add item to array */ + g_ptr_array_add(shared_items, (gpointer) tmp_item); + arr_index++; + + /* Print error if pointers are different */ + if (g_ptr_array_index (shared_items, arr_index) != (gpointer) tmp_item) + g_warning ("load_smb_conf (GPtrArray **shared_items): got %p instead of %p\n", + g_ptr_array_index(shared_items, arr_index), tmp_item); + } + } + + /* Skip special sections (global, homes, printers) */ + if(skip) + continue; + + TSmbConfItem *tmp = g_ptr_array_index(shared_items, arr_index); + + /* Recognize parameters and fill in item */ + parse_to_share_item (line, tmp); + + } + + /* Free */ + g_free(line); + + fclose(smb_file); + + return 0; +} + + +/** +* Returns share imte if directory [path] is shared, +* if not return NULL; +*/ +TSmbConfItem +*is_shared_item(GPtrArray *shared_items, const gchar *path) +{ + TSmbConfItem *ret = NULL; + + TSmbConfItem *tmp; + gint i; + for(i = 0; i < shared_items->len; i++) + { + tmp = g_ptr_array_index(shared_items,i); + if(strstr(tmp->path->str,path)) + { + ret = tmp; + break; + } + } + + return ret; +} + + +/** +* True if share name exist +*/ +gboolean +share_name_exist (GPtrArray *shared_items, const gchar *sharename) +{ + TSmbConfItem *tmp; + gint i; + for(i = 0; i < shared_items->len; i++) + { + tmp = g_ptr_array_index(shared_items,i); + if(strstr(tmp->name->str,sharename)) + { + return TRUE; + } + } + + return FALSE; +} + + +/** +* Write new share section or change chare section defined by share parameter +*/ +gint +write_share (GPtrArray *shared_items, TSmbConfItem *share) +{ + + TSmbConfItem *item; /* Shared item */ + FILE *smb_file; /* Samba config file */ + GString *smb_conf_new; /* Content of smb.conf */ + gchar *line; /* Line buffer */ + gchar *tmp; /* Temp */ + + gboolean writed_change [KEYWORDS_COUNT] = {FALSE}; + gboolean check_writed_changes = FALSE; + gboolean new_share = FALSE; + gboolean change = FALSE; + gboolean found = FALSE; + + item = is_shared_item(shared_items, share->path->str); + + /* If item is not shared we just append it to end of smb.conf */ + if(!item) + { + if(share_name_exist(shared_items, share->name->str)) + return ERROR_SHARE_NAME_EXIST; + + new_share = TRUE; + } + + /* smb.conf - File exist test */ + if(!g_file_test(smb_conf_path, G_FILE_TEST_EXISTS)) + { + g_warning ("Config file \"%s\" does not exist!", smb_conf_path); + return ERROR_FILE_NOT_EXIST; + } + + /* Try open smb.conf - rewrite all */ + smb_file = fopen(smb_conf_path, "r"); + if(smb_file == NULL) + { + g_warning ("Can not open file \"%s\"!", smb_conf_path); + return ERROR_CAN_NOT_OPEN_FILE; + } + + /* Inicialize buffers */ + smb_conf_new = g_string_new(""); + line = g_strnfill(BUFSIZ, '\0'); + tmp = g_strnfill(BUFSIZ, '\0'); + + + /* Load smb.conf, change or add share section share */ + while(fgets(line, BUFSIZ, smb_file)) + { + gchar *orig = g_strdup(line); + + /* Remove white space */ + g_strstrip(line); + + if(!new_share) /* Find section to change */ + if(line[0] == '[') + { + if (change) /* Section to change ends, check if all atributes are wroted */ + check_writed_changes = TRUE; + + change = FALSE; + + /* Is this section to change? */ + if(strstr(line,item->name->str)) + { + change = TRUE; + } + } + + if(check_writed_changes) + { + /* Section name and path are allways writed */ + + if(!writed_change[COMMENT_ID] && (share->comment->len > 0)) + { + g_string_append_printf(smb_conf_new, "\tcomment = %s\n",share->comment->str); + } + + if(!writed_change[READ_ONLY_ID] && (share->read_only->len > 0)) + { + g_string_append_printf(smb_conf_new, "\tread only = %s\n",share->read_only->str); + } + + if(!writed_change[WRTITABLE_ID] && (share->writable->len > 0)) + { + g_string_append_printf(smb_conf_new, "\twritable = %s\n",share->writable->str); + } + + if(!writed_change[GUEST_OK_ID] && (share->guest_ok->len > 0)) + { + g_string_append_printf(smb_conf_new, "\tguest ok = %s\n",share->guest_ok->str); + } + } + + + /* New share or no change */ + if(!change || new_share) + { + /* Just copy text from smb.conf */ + g_string_append(smb_conf_new,orig); + } + else + { + /* Change this line */ + gchar *lower = g_ascii_strdown(line, strlen(line)); /* lower line */ + + found = FALSE; + + /* Find keywords on line *txt */ + gint i; + for(i = 0; i < KEYWORDS_COUNT; i++) + { + gchar *point; + if((point = strstr(lower,keywords[i])) != NULL) + { + /* Keywords must be at begining of line */ + if(point == lower) + { + found = TRUE; + break; + } + } + } + + /* Change share atributes */ + if(found) + switch(i) + { + /* [share name] */ + case SHARE_NAME_ID: + { + g_string_append_printf(smb_conf_new, "[%s]\n",share->name->str); + writed_change[SHARE_NAME_ID] = TRUE; + } + break; + + + /* path = */ + case PATH_ID: + { + /* no change - path must be same */ + g_string_append(smb_conf_new,orig); + writed_change[PATH_ID] = TRUE; + } + break; + + + /* comment = */ + case COMMENT_ID: + { + if(share->comment->len > 0) + { + + g_string_append_printf(smb_conf_new, "\tcomment = %s\n",share->comment->str); + writed_change[COMMENT_ID] = TRUE; + } + } + break; + + + /* read only = */ + case READ_ONLY_ID: + { + + if(share->read_only->len > 0) + { + g_string_append_printf(smb_conf_new, "\tread only = %s\n",share->read_only->str); + writed_change[READ_ONLY_ID] = TRUE; + } + } + break; + + + /* writ = writable, writeable, write ok = */ + case WRTITABLE_ID: + { + if(share->writable->len > 0) + { + g_string_append_printf(smb_conf_new, "\twritable = %s\n",share->writable->str); + writed_change[WRTITABLE_ID] = TRUE; + } + } + break; + + + /* guest ok = */ + case GUEST_OK_ID: + { + if(share->guest_ok->len > 0) + { + g_string_append_printf(smb_conf_new, "\tguest ok = %s\n",share->guest_ok->str); + writed_change[GUEST_OK_ID] = TRUE; + } + } + break; + + default: + break; + } + + g_free(lower); + } + + g_free(orig); + } + + if(!check_writed_changes && change) + { + /* Section name and path are allways writed */ + + if(!writed_change[COMMENT_ID] && (share->comment->len > 0)) + { + g_string_append_printf(smb_conf_new, "\tcomment = %s\n",share->comment->str); + } + + if(!writed_change[READ_ONLY_ID] && (share->read_only->len > 0)) + { + g_string_append_printf(smb_conf_new, "\tread only = %s\n",share->read_only->str); + } + + if(!writed_change[WRTITABLE_ID] && (share->writable->len > 0)) + { + g_string_append_printf(smb_conf_new, "\twritable = %s\n",share->writable->str); + } + + if(!writed_change[GUEST_OK_ID] && (share->guest_ok->len > 0)) + { + g_string_append_printf(smb_conf_new, "\tguest ok = %s\n",share->guest_ok->str); + } + } + + + /* Create new share */ + if(new_share) + { + + g_string_append_printf(smb_conf_new, "\n[%s]\n",share->name->str); + + g_string_append_printf(smb_conf_new, "\tpath = %s\n",share->path->str); + + + if(share->comment->len > 0) + { + g_string_append_printf(smb_conf_new, "\tcomment = %s\n",share->comment->str); + } + + + if(share->read_only->len > 0) + { + g_string_append_printf(smb_conf_new, "\tread only = %s\n",share->read_only->str); + } + + if(share->writable->len > 0) + { + g_string_append_printf(smb_conf_new, "\twritable = %s\n",share->writable->str); + } + + if(share->guest_ok->len > 0) + { + g_string_append_printf(smb_conf_new, "\tguest ok = %s\n",share->guest_ok->str); + } + } + + /* Write new share to smb.conf */ + write_smbconf(smb_conf_new->str); + + fclose(smb_file); + + g_free(line); + g_free(tmp); + + g_string_free(smb_conf_new,TRUE); + + return 0; +} + + + +/** +* Function erase shared section containing path from smb.conf +* shared_items must by actual! +*/ +gint delete_share (GPtrArray *shared_items, const gchar *path) +{ + FILE *smb_file; /* Samba config file */ + GString *smb_conf_new; /* Content of smb.conf */ + gchar *line; /* Line buffer */ + gchar *orig; /* Original line */ + TSmbConfItem *skip_item; /* Skip this share */ + gboolean skip = FALSE; /* Skip lines (erase from config) */ + + skip_item = is_shared_item(shared_items, path); + + if(!skip_item) + { + g_warning ("Directory \"%s\" is NOT shared",path); + return ERROR_DIRECTORY_NOT_SHARED; + } + + + /* smb.conf - File exist test */ + if(!g_file_test(smb_conf_path, G_FILE_TEST_EXISTS)) + { + g_warning ("Config file \"%s\" does not exist!", smb_conf_path); + return ERROR_FILE_NOT_EXIST; + } + + /* Try open smb.conf - rewrite all */ + smb_file = fopen(smb_conf_path, "r"); + if(smb_file == NULL) + { + g_warning ("Can not open file \"%s\"!", smb_conf_path); + return ERROR_CAN_NOT_OPEN_FILE; + } + + /* Inicialize buffers */ + smb_conf_new = g_string_new(""); + line = g_strnfill(BUFSIZ, '\0'); + orig = g_strnfill(BUFSIZ, '\0'); + + + /* Load smb.conf WITHOUT section skip_name (section containing path) */ + while(fgets(line, BUFSIZ, smb_file)) + { + g_stpcpy(orig, line); + + /* Remove white space */ + g_strstrip(line); + + if(line[0] == '[') + { + skip = FALSE; + + /* Test special sections */ + if(strstr(line,skip_item->name->str)) + { + skip = TRUE; + } + } + + if(!skip) + { + g_string_append(smb_conf_new,orig); + } + } + + /* Write deleted share to smb.conf */ + write_smbconf(smb_conf_new->str); + + fclose(smb_file); + + g_free(line); + g_free(orig); + + g_string_free (smb_conf_new,TRUE); + + return OK; +} + +void item_to_strv (TSmbConfItem *item, gchar ***ret) +{ + /* [share name], "path =", "commennt =", "read only =", "guest ok =" */ + + *ret = g_new (gchar *, 6); + + (*ret)[0] = g_strdup (item->name->str); + (*ret)[1] = g_strdup (item->path->str); + (*ret)[2] = g_strdup (item->comment->str); + (*ret)[3] = g_strdup (item->read_only->str); + (*ret)[4] = g_strdup (item->guest_ok->str); + + (*ret)[5] = NULL; +} + + + +void char_to_strv(const gchar *str, gchar ***ret) +{ + *ret = g_new (char *, 2); + + (*ret)[0] = g_strdup (str); + (*ret)[1] = NULL; +} + + +/** +* If path is shared then returns share parameters in result. +*/ +gint +smb_get_share_status (const gchar *path, gchar ***result) +{ + gboolean found = FALSE; + GPtrArray *shared_items = shared_items_array_new(); + + Error_sfshare err; + + err = load_smb_conf (shared_items); + + if (err != OK) + { + /* Free */ + shared_items_array_free (shared_items); + return err; + } + + + TSmbConfItem *test; + gint i; + for (i = 0; i < shared_items->len; i++) + { + test = g_ptr_array_index(shared_items, i); + if (!g_strcmp0(test->path->str,path)) + { + found = TRUE; /* Path found in smb.conf */ + break; + } + } + + if (found) + item_to_strv(test, result); + else + char_to_strv(NULL,result); + + /* Free */ + shared_items_array_free (shared_items); + + return OK; +} + + +/** +* Write share to smb.conf +*/ +gint +smb_set_share (gchar **parameters) +{ + gint ret = OK; + /* Create new items array */ + GPtrArray *shared_items = shared_items_array_new (); + + /* Load share sections from smb.conf */ + ret = load_smb_conf (shared_items); + + /* Create share item */ + /* [share name], "path =", "commennt =", "read only =", "guest ok =" */ + TSmbConfItem *item = smbconf_item_new (parameters[0], parameters[1], parameters[2], parameters[3], parameters[4]); + + /* Write share to smb.conf */ + if(ret == OK) + ret = write_share (shared_items, item); + + /* free */ + smbconf_item_free (item); + shared_items_array_free (shared_items); + + return ret; +} + + +/** +* Delete share section from smb.conf +*/ +gint +smb_delete_share (gchar **path) +{ + gint ret = OK; + /* Create new items array */ + GPtrArray *shared_items = shared_items_array_new (); + + /* Load share sections from smb.conf */ + ret = load_smb_conf (shared_items); + + /* try to delete shared section */ + if(ret == OK) + ret = delete_share (shared_items, path[0]); + + /* Free */ + shared_items_array_free (shared_items); + + return ret; +} + diff --git a/sfshare-daemon/samba_share.h b/sfshare-daemon/samba_share.h new file mode 100755 index 0000000..2519382 --- /dev/null +++ b/sfshare-daemon/samba_share.h @@ -0,0 +1,84 @@ +/* + * samba_share.h + * + * 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 library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * Author: Jan Lipovsky + */ + +#ifndef SAMBA_SHARE_H +#define SAMBA_SHARE_H + +#include + + +typedef struct smb_conf_item +{ + GString *name; /* [share name] */ + GString *path; /* path = /path/to/folder */ + GString *comment; /* comment = Some text comment */ + GString *read_only; /* read only = yes | no */ + GString *writable; /* writable = yes | no ... writeable, write ok */ + GString *guest_ok; /* guest ok = yes | no */ +} TSmbConfItem; + + +/** Funciton loads all share section to array */ +gint load_smb_conf (GPtrArray *shared_items); + +/** Function erase shared section containing path from smb.conf */ +gint delete_share (GPtrArray *shared_items, const gchar *path); + +/** Write new share section or change chare section defined by share parameter */ +gint write_share (GPtrArray *shared_items, TSmbConfItem *share); + +/** If path is shared then returns share parameters in result. */ +gboolean smb_get_share_status (const gchar *path, gchar ***result); + +/** Write or channge section in smb.conf */ +gint smb_set_share (gchar **parameters); + +/** Delete share section from smb.conf */ +gint smb_delete_share (gchar **path); + +void item_to_strv (TSmbConfItem *item, gchar ***ret); + + +/** Function changes path to smb.conf to path */ +void set_smbconf_path (const gchar *path); + + +/** Function returns new array */ +GPtrArray* shared_items_array_new(); + +/** Function destroy Share Items Array */ +void shared_items_array_free (GPtrArray *array); + +/** Function allocs memory for TSmbConfItem and sets parameters */ +TSmbConfItem* smbconf_item_new (const gchar *name, const gchar *path, const gchar *comment, const gchar *read_only, const gchar *guest_ok); + +/** Function allocs memory for TSmbConfItem */ +TSmbConfItem* smbconf_item_new0 (); + + +/** If ok return 0 else return number of error */ +gint check_item (TSmbConfItem *item); + +/** Function free memory */ +void smbconf_item_free (TSmbConfItem *item); + +/** Send SIGHUP to smb and nmb */ +void smb_reload_service (); +#endif diff --git a/sfshare-daemon/sfshare_errors.h b/sfshare-daemon/sfshare_errors.h new file mode 100755 index 0000000..069c9e2 --- /dev/null +++ b/sfshare-daemon/sfshare_errors.h @@ -0,0 +1,49 @@ +/* + * sfshare_errors.c + * + * 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 library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * Author: Jan Lipovsky + */ + +#ifndef SFSHARE_ERRORS_H +#define SFSHARE_ERRORS_H + + +/** Types of errors */ +typedef enum +{ + OK, + ERROR_FAILED, + ERROR_PERMISSION_DENIED, + + /* Samba share */ + ERROR_FILE_NOT_EXIST, + ERROR_CAN_NOT_OPEN_FILE, + ERROR_CAN_NOT_WRITE_TO_FILE, + ERROR_DIRECTORY_NOT_SHARED, + ERROR_WRONG_NAME, + ERROR_WRONG_PATH, + ERROR_READONLY_WRITABLE, + ERROR_SHARE_NAME_EXIST, + + NUM_ERRORS +} Error_sfshare; + + +/** Definition in file: dbus_service.c */ +gchar *get_error_msg (Error_sfshare err); + +#endif diff --git a/sfshare-daemon/sfshared.c b/sfshare-daemon/sfshared.c new file mode 100755 index 0000000..2784fcf --- /dev/null +++ b/sfshare-daemon/sfshared.c @@ -0,0 +1,33 @@ +/* + * sfshared.c + * + * 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 library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU General Public + * License along with this library; if not, write to the Free + * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + * + * Author: Jan Lipovsky + */ + +#include +#include +#include +#include + +#include "samba_share.h" +#include "dbus_service.h" + +/** Run daemon */ +int main() +{ + return dbus_sfshare_start(); +} diff --git a/sfshare-daemon/src/Makefile.am b/sfshare-daemon/src/Makefile.am deleted file mode 100755 index f4c11a2..0000000 --- a/sfshare-daemon/src/Makefile.am +++ /dev/null @@ -1,6 +0,0 @@ -AM_CFLAGS=-Wall @POLKIT_CFLAGS@ @GTK_CFLAGS@ @DBUS_GLIB_CFLAGS@ -AM_LDFLAGS= @POLKIT_LIBS@ @GTK_LIBS@ @DBUS_GLIB_LIBS@ - - -bin_PROGRAMS = sfshared -sfshared_SOURCES = dbus_service.c dbus_service.h samba_share.c sfshared.c dbus_service_glue.h samba_share.h sfshare_errors.h diff --git a/sfshare-daemon/src/dbus_service.c b/sfshare-daemon/src/dbus_service.c deleted file mode 100644 index c2f0258..0000000 --- a/sfshare-daemon/src/dbus_service.c +++ /dev/null @@ -1,457 +0,0 @@ -/* - * 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 library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this library; if not, write to the Free - * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - */ - -#include -#include -#include -#include -#include -#include - -#include -#include - -#include - -#include "samba_share.h" -#include "dbus_service.h" -#include "sfshare_errors.h" - -/* DBUS metods */ -#define ACTION_ID_SETUP_SHARE "org.fedoraproject.SimpleFileShare.setup_share" -#define ASTION_ID_GET_SHARE_STATUS "org.fedoraproject.SimpleFileShare.get_share_status" -#define ASTION_ID_DELETE_SHARE "org.fedoraproject.SimpleFileShare.delete_share" - - -typedef struct DaemonObject DaemonObject; -typedef struct DaemonObjectClass DaemonObjectClass; - -static PolkitSubject *subject_dbus = NULL; -static PolkitAuthority *authority = NULL; - - -gboolean is_authorized = FALSE; -DBusGConnection *bus; -DBusGProxy *bus_proxy; - -GType daemon_object_get_type (void); - -struct DaemonObject -{ - GObject parent; -}; - -struct DaemonObjectClass -{ - GObjectClass parent; -}; - - -#define DAEMON_TYPE_OBJECT (daemon_object_get_type ()) -#define DAEMON_OBJECT(object) (G_TYPE_CHECK_INSTANCE_CAST ((object), DAEMON_TYPE_OBJECT, DaemonObject)) -#define DAEMON_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), DAEMON_TYPE_OBJECT, DaemonObjectClass)) -#define DAEMON_IS_OBJECT(object) (G_TYPE_CHECK_INSTANCE_TYPE ((object), DAEMON_TYPE_OBJECT)) -#define DAEMON_IS_OBJECT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), DAEMON_TYPE_OBJECT)) -#define DAEMON_OBJECT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), DAEMON_TYPE_OBJECT, DaemonObjectClass)) - -G_DEFINE_TYPE(DaemonObject, daemon_object, G_TYPE_OBJECT) - -gboolean daemon_get_share_status (DaemonObject *obj, const gchar *path, gchar ***result, GError **error); - -/* Async calls */ -gboolean daemon_setup_share (DaemonObject *obj, const gchar **parameters, DBusGMethodInvocation *context); -gboolean daemon_delete_share (DaemonObject *obj,const gchar *path, DBusGMethodInvocation *context); - -#include "dbus_service_glue.h" - -/** -* Return text of Error -*/ -gchar * -get_error_msg (Error_sfshare err) -{ - gchar *errmsg; - - switch (err) - { - case ERROR_FAILED: - errmsg = g_strdup ("Failed"); - break; - - case ERROR_PERMISSION_DENIED: - errmsg = g_strdup ("Permission denied"); - break; - - case ERROR_FILE_NOT_EXIST: - errmsg = g_strdup ("Config file does not exist"); - break; - - case ERROR_CAN_NOT_OPEN_FILE: - errmsg = g_strdup ("Can not open config file"); - break; - - case ERROR_DIRECTORY_NOT_SHARED: - errmsg = g_strdup ("Directory is not shared"); - break; - - case ERROR_WRONG_NAME: - errmsg = g_strdup ("Wrong name"); - break; - - case ERROR_WRONG_PATH: - errmsg = g_strdup ("Wrong path"); - break; - - case ERROR_READONLY_WRITABLE: - errmsg = g_strdup ("Read only is same as writable"); - break; - - case ERROR_SHARE_NAME_EXIST: - errmsg = g_strdup ("Share name already exist"); - break; - - default: - errmsg = g_strdup ("Unknow error type"); - break; - } - - return errmsg; -} - - -GQuark -get_error_quark (void) -{ - static GQuark ret = 0; - - if (ret == 0) - { - ret = g_quark_from_static_string ("sfshare_daemon_error"); - } - return ret; -} - - -/** Return error to context */ -static void -send_error (DBusGMethodInvocation *context, - gint error_code, - const gchar *format, - ...) -{ - GError *error; - va_list args; - gchar *message; - - va_start (args, format); - message = g_strdup_vprintf (format, args); - va_end (args); - - error = g_error_new (ERROR_QUARK, error_code, "%s", message); - dbus_g_method_return_error (context, error); - g_error_free (error); - - g_free (message); -} - - -/** -* Create pointer to DaemonData filled with values from parameters -*/ -DaemonData * -daemon_data_new ( DBusGMethodInvocation *context, AuthorizedCallback auth_cb, const gchar **in) -{ - DaemonData *ret; - - ret = g_new0 (DaemonData, 1); - - ret->context = context; - ret->authorized_cb = auth_cb; - - ret->in = g_new0 (gchar *, 6); - - gint i = 0; - - while(in[i] != NULL) - { - ret->in[i] = g_strdup (in[i]); - i++; - } - - return ret; -} - -/** -* Free DaemonData pointer -*/ -void -daemon_data_free (DaemonData * data) -{ - gint i; - i = 0; - - while (data->in[i] != NULL) - { - g_free (data->in[i++]); - } - - g_free (data); - -} - - -/** -* PolicyKit callback function -*/ -static void -check_authorization_cb (PolkitAuthority *authority, - GAsyncResult *res, - DaemonData *data) -{ - GError *error; - PolkitAuthorizationResult *result; - - - is_authorized = FALSE; - - error = NULL; - result = polkit_authority_check_authorization_finish (authority, res, &error); - if (error != NULL) - { - send_error (data->context, ERROR_PERMISSION_DENIED, "Not authorized: %s", error->message); - g_error_free (error); - } - else - { - if (polkit_authorization_result_get_is_authorized (result)) - { - is_authorized = TRUE; - } - else if (polkit_authorization_result_get_is_challenge (result)) - { - send_error (data->context, ERROR_PERMISSION_DENIED, "Authentication is required"); - } - else - { - send_error (data->context, ERROR_PERMISSION_DENIED, "Not authorized"); - } - } - - if(is_authorized) - { - /* Run authorized callback */ - (* data->authorized_cb) (data); - } - - daemon_data_free (data); -} - - - - - - -static void -daemon_object_init (DaemonObject *obj) -{ -} - -static void -daemon_object_class_init (DaemonObjectClass *klass) -{ -} - -/** -* Handle incomming request - D-Bus get_share_status -*/ -gboolean -daemon_get_share_status (DaemonObject *obj, const gchar *path, gchar ***result, GError **error) -{ - /* puts("daemon_get_share_status"); */ - - Error_sfshare err; - - err = smb_get_share_status (path, result); - - if (err != OK) - { - *error = g_error_new (ERROR_QUARK, err, "%s", get_error_msg(err)); - return FALSE; - } - else - return TRUE; -} - -/** -* Authorized call of smb_set_share - write or change share section -*/ -void -setup_share_authorized (gpointer data) -{ - Error_sfshare err; - DaemonData *dd = data; - - err = smb_set_share (dd->in); - - if (err != OK) - { - send_error (dd->context, err, "%s", get_error_msg(err)); - } - - smb_reload_service (); - - /* Return */ - dbus_g_method_return (dd->context); -} - - - -/** -* Dbus setup share - polkit authorization check -*/ -gboolean -daemon_setup_share (DaemonObject *obj,const gchar **parameters, DBusGMethodInvocation *context) -{ - /* puts ("daemon_setup_share"); */ - - DaemonData *data; - data = daemon_data_new (context, setup_share_authorized, parameters); - - subject_dbus = polkit_system_bus_name_new (dbus_g_method_get_sender (context)); - - polkit_authority_check_authorization (authority, - subject_dbus, - ACTION_ID_SETUP_SHARE, - NULL, /* PolkitDetails */ - POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE, - NULL, - (GAsyncReadyCallback) check_authorization_cb, - data); - return TRUE; -} - - - -/** -* Authorized call of smb_delete_share - delete share section -*/ -void -delete_share_authorized (gpointer data) -{ - Error_sfshare err; - DaemonData *dd = data; - - err = smb_delete_share(dd->in); - - if (err != OK) - { - send_error (dd->context, err, "%s", get_error_msg(err)); - } - - smb_reload_service (); - - /* Return */ - dbus_g_method_return (dd->context); -} - - -/** -* Dbus delete share - polkit authorization check -*/ -gboolean -daemon_delete_share (DaemonObject *obj, const gchar *path, DBusGMethodInvocation *context) -{ - /* puts("daemon_delete_share"); */ - - DaemonData *data; - - const gchar *in[2] = {path, NULL}; - - data = daemon_data_new (context, delete_share_authorized, in); - - subject_dbus = polkit_system_bus_name_new (dbus_g_method_get_sender (context)); - - polkit_authority_check_authorization (authority, - subject_dbus, - ASTION_ID_DELETE_SHARE, - NULL, /* PolkitDetails */ - POLKIT_CHECK_AUTHORIZATION_FLAGS_NONE, - NULL, - (GAsyncReadyCallback) check_authorization_cb, - data); - - return TRUE; -} - - -/** -* Start of sfshare deamon -*/ -int -dbus_sfshare_start () -{ - GError *error = NULL; - GMainLoop *mainloop; - guint request_name_result; - DaemonObject *obj; - - g_type_init (); - - dbus_g_object_type_install_info (DAEMON_TYPE_OBJECT, &dbus_glib_daemon_object_info); - - mainloop = g_main_loop_new (NULL, FALSE); - - bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error); - if (!bus) - { - g_warning ("Couldn't connect to system bus : %s", error->message); - g_error_free (error); - return -1; - } - - - /* PolKit */ - authority = polkit_authority_get (); - - bus_proxy = dbus_g_proxy_new_for_name (bus, DBUS_SERVICE_DBUS, DBUS_PATH_DBUS, DBUS_INTERFACE_DBUS); - - if (!dbus_g_proxy_call (bus_proxy, "RequestName", &error, - G_TYPE_STRING, "org.fedoraproject.SimpleFileShare", - G_TYPE_UINT, 0, - G_TYPE_INVALID, - G_TYPE_UINT, &request_name_result, - G_TYPE_INVALID)) - { - g_warning("Failed to acquire org.fedoraproject.SimpleFileShare : %s", error->message); - g_error_free (error); - return -1; - } - - - obj = g_object_new (DAEMON_TYPE_OBJECT, NULL); - - dbus_g_connection_register_g_object (bus, "/org/fedoraproject/SimpleFileShare", G_OBJECT (obj)); - - g_print ("Simple File Share - service running\n"); - - g_main_loop_run (mainloop); - - g_object_unref (obj); - - - return 0; -} diff --git a/sfshare-daemon/src/dbus_service.h b/sfshare-daemon/src/dbus_service.h deleted file mode 100644 index 79897a1..0000000 --- a/sfshare-daemon/src/dbus_service.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * 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 library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this library; if not, write to the Free - * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - */ - -#ifndef DBUS_SERVICE_H -#define DBUS_SERVICE_H - -#include -#include - - - -typedef void (*AuthorizedCallback) (void *data); - -typedef struct -{ - DBusGMethodInvocation *context; - gchar **in; - AuthorizedCallback authorized_cb; - -} DaemonData; - -/** Daemon start */ -int dbus_sfshare_start (); - -GQuark get_error_quark (void); -#define ERROR_QUARK get_error_quark () - - - -#endif diff --git a/sfshare-daemon/src/dbus_service_glue.h b/sfshare-daemon/src/dbus_service_glue.h deleted file mode 100644 index ec1b428..0000000 --- a/sfshare-daemon/src/dbus_service_glue.h +++ /dev/null @@ -1,212 +0,0 @@ -/* Generated by dbus-binding-tool; do not edit! */ - - -#ifndef __dbus_glib_marshal_daemon_MARSHAL_H__ -#define __dbus_glib_marshal_daemon_MARSHAL_H__ - -#include - -G_BEGIN_DECLS - -#ifdef G_ENABLE_DEBUG -#define g_marshal_value_peek_boolean(v) g_value_get_boolean (v) -#define g_marshal_value_peek_char(v) g_value_get_char (v) -#define g_marshal_value_peek_uchar(v) g_value_get_uchar (v) -#define g_marshal_value_peek_int(v) g_value_get_int (v) -#define g_marshal_value_peek_uint(v) g_value_get_uint (v) -#define g_marshal_value_peek_long(v) g_value_get_long (v) -#define g_marshal_value_peek_ulong(v) g_value_get_ulong (v) -#define g_marshal_value_peek_int64(v) g_value_get_int64 (v) -#define g_marshal_value_peek_uint64(v) g_value_get_uint64 (v) -#define g_marshal_value_peek_enum(v) g_value_get_enum (v) -#define g_marshal_value_peek_flags(v) g_value_get_flags (v) -#define g_marshal_value_peek_float(v) g_value_get_float (v) -#define g_marshal_value_peek_double(v) g_value_get_double (v) -#define g_marshal_value_peek_string(v) (char*) g_value_get_string (v) -#define g_marshal_value_peek_param(v) g_value_get_param (v) -#define g_marshal_value_peek_boxed(v) g_value_get_boxed (v) -#define g_marshal_value_peek_pointer(v) g_value_get_pointer (v) -#define g_marshal_value_peek_object(v) g_value_get_object (v) -#else /* !G_ENABLE_DEBUG */ -/* WARNING: This code accesses GValues directly, which is UNSUPPORTED API. - * Do not access GValues directly in your code. Instead, use the - * g_value_get_*() functions - */ -#define g_marshal_value_peek_boolean(v) (v)->data[0].v_int -#define g_marshal_value_peek_char(v) (v)->data[0].v_int -#define g_marshal_value_peek_uchar(v) (v)->data[0].v_uint -#define g_marshal_value_peek_int(v) (v)->data[0].v_int -#define g_marshal_value_peek_uint(v) (v)->data[0].v_uint -#define g_marshal_value_peek_long(v) (v)->data[0].v_long -#define g_marshal_value_peek_ulong(v) (v)->data[0].v_ulong -#define g_marshal_value_peek_int64(v) (v)->data[0].v_int64 -#define g_marshal_value_peek_uint64(v) (v)->data[0].v_uint64 -#define g_marshal_value_peek_enum(v) (v)->data[0].v_long -#define g_marshal_value_peek_flags(v) (v)->data[0].v_ulong -#define g_marshal_value_peek_float(v) (v)->data[0].v_float -#define g_marshal_value_peek_double(v) (v)->data[0].v_double -#define g_marshal_value_peek_string(v) (v)->data[0].v_pointer -#define g_marshal_value_peek_param(v) (v)->data[0].v_pointer -#define g_marshal_value_peek_boxed(v) (v)->data[0].v_pointer -#define g_marshal_value_peek_pointer(v) (v)->data[0].v_pointer -#define g_marshal_value_peek_object(v) (v)->data[0].v_pointer -#endif /* !G_ENABLE_DEBUG */ - - -/* NONE:STRING,POINTER */ -extern void dbus_glib_marshal_daemon_VOID__STRING_POINTER (GClosure *closure, - GValue *return_value, - guint n_param_values, - const GValue *param_values, - gpointer invocation_hint, - gpointer marshal_data); -void -dbus_glib_marshal_daemon_VOID__STRING_POINTER (GClosure *closure, - GValue *return_value G_GNUC_UNUSED, - guint n_param_values, - const GValue *param_values, - gpointer invocation_hint G_GNUC_UNUSED, - gpointer marshal_data) -{ - typedef void (*GMarshalFunc_VOID__STRING_POINTER) (gpointer data1, - gpointer arg_1, - gpointer arg_2, - gpointer data2); - register GMarshalFunc_VOID__STRING_POINTER callback; - register GCClosure *cc = (GCClosure*) closure; - register gpointer data1, data2; - - g_return_if_fail (n_param_values == 3); - - if (G_CCLOSURE_SWAP_DATA (closure)) - { - data1 = closure->data; - data2 = g_value_peek_pointer (param_values + 0); - } - else - { - data1 = g_value_peek_pointer (param_values + 0); - data2 = closure->data; - } - callback = (GMarshalFunc_VOID__STRING_POINTER) (marshal_data ? marshal_data : cc->callback); - - callback (data1, - g_marshal_value_peek_string (param_values + 1), - g_marshal_value_peek_pointer (param_values + 2), - data2); -} -#define dbus_glib_marshal_daemon_NONE__STRING_POINTER dbus_glib_marshal_daemon_VOID__STRING_POINTER - -/* BOOLEAN:STRING,POINTER,POINTER */ -extern void dbus_glib_marshal_daemon_BOOLEAN__STRING_POINTER_POINTER (GClosure *closure, - GValue *return_value, - guint n_param_values, - const GValue *param_values, - gpointer invocation_hint, - gpointer marshal_data); -void -dbus_glib_marshal_daemon_BOOLEAN__STRING_POINTER_POINTER (GClosure *closure, - GValue *return_value G_GNUC_UNUSED, - guint n_param_values, - const GValue *param_values, - gpointer invocation_hint G_GNUC_UNUSED, - gpointer marshal_data) -{ - typedef gboolean (*GMarshalFunc_BOOLEAN__STRING_POINTER_POINTER) (gpointer data1, - gpointer arg_1, - gpointer arg_2, - gpointer arg_3, - gpointer data2); - register GMarshalFunc_BOOLEAN__STRING_POINTER_POINTER callback; - register GCClosure *cc = (GCClosure*) closure; - register gpointer data1, data2; - gboolean v_return; - - g_return_if_fail (return_value != NULL); - g_return_if_fail (n_param_values == 4); - - if (G_CCLOSURE_SWAP_DATA (closure)) - { - data1 = closure->data; - data2 = g_value_peek_pointer (param_values + 0); - } - else - { - data1 = g_value_peek_pointer (param_values + 0); - data2 = closure->data; - } - callback = (GMarshalFunc_BOOLEAN__STRING_POINTER_POINTER) (marshal_data ? marshal_data : cc->callback); - - v_return = callback (data1, - g_marshal_value_peek_string (param_values + 1), - g_marshal_value_peek_pointer (param_values + 2), - g_marshal_value_peek_pointer (param_values + 3), - data2); - - g_value_set_boolean (return_value, v_return); -} - -/* NONE:BOXED,POINTER */ -extern void dbus_glib_marshal_daemon_VOID__BOXED_POINTER (GClosure *closure, - GValue *return_value, - guint n_param_values, - const GValue *param_values, - gpointer invocation_hint, - gpointer marshal_data); -void -dbus_glib_marshal_daemon_VOID__BOXED_POINTER (GClosure *closure, - GValue *return_value G_GNUC_UNUSED, - guint n_param_values, - const GValue *param_values, - gpointer invocation_hint G_GNUC_UNUSED, - gpointer marshal_data) -{ - typedef void (*GMarshalFunc_VOID__BOXED_POINTER) (gpointer data1, - gpointer arg_1, - gpointer arg_2, - gpointer data2); - register GMarshalFunc_VOID__BOXED_POINTER callback; - register GCClosure *cc = (GCClosure*) closure; - register gpointer data1, data2; - - g_return_if_fail (n_param_values == 3); - - if (G_CCLOSURE_SWAP_DATA (closure)) - { - data1 = closure->data; - data2 = g_value_peek_pointer (param_values + 0); - } - else - { - data1 = g_value_peek_pointer (param_values + 0); - data2 = closure->data; - } - callback = (GMarshalFunc_VOID__BOXED_POINTER) (marshal_data ? marshal_data : cc->callback); - - callback (data1, - g_marshal_value_peek_boxed (param_values + 1), - g_marshal_value_peek_pointer (param_values + 2), - data2); -} -#define dbus_glib_marshal_daemon_NONE__BOXED_POINTER dbus_glib_marshal_daemon_VOID__BOXED_POINTER - -G_END_DECLS - -#endif /* __dbus_glib_marshal_daemon_MARSHAL_H__ */ - -#include -static const DBusGMethodInfo dbus_glib_daemon_methods[] = { - { (GCallback) daemon_get_share_status, dbus_glib_marshal_daemon_BOOLEAN__STRING_POINTER_POINTER, 0 }, - { (GCallback) daemon_setup_share, dbus_glib_marshal_daemon_NONE__BOXED_POINTER, 79 }, - { (GCallback) daemon_delete_share, dbus_glib_marshal_daemon_NONE__STRING_POINTER, 144 }, -}; - -const DBusGObjectInfo dbus_glib_daemon_object_info = { - 0, - dbus_glib_daemon_methods, - 3, -"org.fedoraproject.SimpleFileShare\0get_share_status\0S\0path\0I\0s\0status\0O\0F\0N\0as\0\0org.fedoraproject.SimpleFileShare\0setup_share\0A\0parameters\0I\0as\0\0org.fedoraproject.SimpleFileShare\0delete_share\0A\0path\0I\0s\0\0\0", -"\0", -"\0" -}; - diff --git a/sfshare-daemon/src/samba_share.c b/sfshare-daemon/src/samba_share.c deleted file mode 100644 index 32987bf..0000000 --- a/sfshare-daemon/src/samba_share.c +++ /dev/null @@ -1,952 +0,0 @@ -/* - * 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 library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this library; if not, write to the Free - * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - */ - -#include /* strlen, strstr */ -#include /* system() call */ - -#include -#include - -#include "samba_share.h" -#include "sfshare_errors.h" - -/* Define */ -#define SECTIONS_COUNT 3 /* Count of special sections in smb.conf */ -#define KEYWORDS_COUNT 6 /* Count of keywords used for setup share in smb.conf */ - - -/* [share name], "path =", "commennt =", "read only =", "writable =, writeable =, write ok =", "guest ok =" */ -const gchar *keywords[KEYWORDS_COUNT] = {"[" ,"path", "comment", "read only", "writ", "guest"}; - -typedef enum keywords_id -{ - SHARE_NAME_ID = 0, - PATH_ID, - COMMENT_ID, - READ_ONLY_ID, - WRTITABLE_ID, - GUEST_OK_ID -} TKeywords_id; - -/* Path too smb.conf */ -gchar *smb_conf_path = "/etc/samba/smb.conf"; -const gchar *smb_special_section [SECTIONS_COUNT] = {"global", "homes", "printers"}; - - -/** Send SIGHUP to smb and nmb */ -void -smb_reload_service () -{ - system("killall -HUP smb nmb"); -} - -/** -* Function changes path to smb.conf to path -*/ -void -set_smbconf_path (const gchar *path) -{ - smb_conf_path = g_strdup(path); -} - - -/** -* Save string str as new smb.conf -*/ -gint -write_smbconf (const gchar *content) -{ - FILE *smb_file; - - /* smb.conf - File exist test */ - if(!g_file_test (smb_conf_path, G_FILE_TEST_EXISTS)) - { - g_warning ("Config file \"%s\" does not exist!", smb_conf_path); - return ERROR_FILE_NOT_EXIST; - } - - /* Try open smb.conf - rewrite all */ - smb_file = fopen(smb_conf_path, "w"); - if(smb_file == NULL) - { - g_warning ("Can not open file \"%s\"! Maybe, you dont have rights for change smb.conf", smb_conf_path); - return ERROR_CAN_NOT_OPEN_FILE; - } - - /* Write to smb.conf */ - if(fputs (content,smb_file) == EOF) - { - g_warning ("Can not write to file \"%s\"!", smb_conf_path); - return ERROR_CAN_NOT_WRITE_TO_FILE; - } - - fclose(smb_file); - - return OK; -} - - -/** -* Function free TSmbConfItem from memmory -*/ -void -smbconf_item_free(TSmbConfItem *item) -{ - g_string_free(item->guest_ok, TRUE); - g_string_free(item->writable, TRUE); - g_string_free(item->read_only, TRUE); - g_string_free(item->comment, TRUE); - g_string_free(item->path, TRUE); - g_string_free(item->name, TRUE); - - g_free(item); -} - - -/** -* Function allocs memory for TSmbConfItem -*/ -TSmbConfItem* -smbconf_item_new0 () -{ - TSmbConfItem *ret; - ret = g_malloc(sizeof(struct smb_conf_item)); - - ret->name = g_string_new(""); - ret->path = g_string_new(""); - ret->comment = g_string_new(""); - ret->read_only = g_string_new(""); - ret->writable = g_string_new(""); - ret->guest_ok = g_string_new(""); - - return ret; -} - - -/** -* Function allocs memory for TSmbConfItem -*/ -TSmbConfItem* -smbconf_item_new (const gchar *name, const gchar *path, const gchar *comment, const gchar *read_only, const gchar *guest_ok) -{ - TSmbConfItem *ret; - ret = g_malloc(sizeof(struct smb_conf_item)); - - ret->name = g_string_new(name); - ret->path = g_string_new(path); - ret->comment = g_string_new(comment); - - ret->read_only = g_string_new(read_only); - - if (!g_strcmp0(read_only,"yes")) - ret->writable = g_string_new("no"); - else - ret->writable = g_string_new("yes"); - - ret->guest_ok = g_string_new(guest_ok); - - return ret; -} - -/** -* Function returns new array -*/ -GPtrArray* -shared_items_array_new () -{ - GPtrArray *ret = g_ptr_array_new(); - return ret; -} - - -/** -* Destroy array -*/ -void -shared_items_array_free (GPtrArray *array) -{ - TSmbConfItem *tmp; - gint i; - for(i = 0; i < array->len; i++) - { - tmp = g_ptr_array_index (array,i); - smbconf_item_free (tmp); - } - - g_ptr_array_free(array, TRUE); -} - - -/** -* If ok return 0 else return number of error -*/ -gint check_item(TSmbConfItem *item) -{ - if(item->name->len <= 0) - return ERROR_WRONG_NAME; - - if(item->path->len <= 0) - return ERROR_WRONG_PATH; - - /* read_only = true ... writable muust be = false */ - if(g_string_equal(item->writable, item->read_only) && item->writable->len > 0) - return ERROR_READONLY_WRITABLE; - - return OK; -} - - -/** -* Parse line of smb.conf and fill competent field of item structure -*/ -void -parse_to_share_item (gchar *txt, TSmbConfItem *item) -{ - gchar *lower = g_ascii_strdown(txt, strlen(txt)); - gboolean found = FALSE; - - /* Find keywords on line *txt */ - gint i; - for(i = 0; i < KEYWORDS_COUNT; i++) - { - gchar *point; - if((point = strstr(lower,keywords[i])) != NULL) - { - /* Keywords must be at begining of line */ - if(point == lower) - { - found = TRUE; - break; - } - } - } - - /* Save atributes - Boolean variables case-insensitive - yes, no, true, false, 1, or 0 */ - if(found) - switch(i) - { - /* [share name] */ - case SHARE_NAME_ID: - { - g_string_append_len(item->name, txt + 1, strlen(txt)-2); - } - break; - - /* path = */ - case PATH_ID: - { - gchar *tmp; - if((tmp = strstr(txt,"=")) != NULL) - { - g_string_assign(item->path, g_strstrip(tmp + 1)); - } - } - break; - - /* comment = */ - case COMMENT_ID: - { - gchar *tmp; - if((tmp = strstr(txt,"=")) != NULL) - { - g_string_assign(item->comment, g_strstrip(tmp + 1)); - } - } - break; - - /* read only = */ - case READ_ONLY_ID: - { - gchar *tmp; - if((tmp = strstr(lower,"=")) != NULL) - { - g_strstrip(++tmp); - - if(g_strcmp0(tmp, "yes") && g_strcmp0(tmp, "true") && g_strcmp0(tmp, "1")) - g_string_assign(item->read_only,"no"); - else - g_string_assign(item->read_only,"yes"); - } - } - break; - - /* writ = writable, writeable, write ok = */ - case WRTITABLE_ID: - { - gchar *tmp; - if((tmp = strstr(lower,"=")) != NULL) - { - g_strstrip(++tmp); - - if(g_strcmp0(tmp, "yes") && g_strcmp0(tmp, "true") && g_strcmp0(tmp, "1")) - g_string_assign(item->writable,"no"); - else - g_string_assign(item->writable,"yes"); - } - } - break; - - /* guest ok = */ - case GUEST_OK_ID: - { - gchar *tmp; - if((tmp = strstr(lower,"=")) != NULL) - { - g_strstrip(++tmp); - - if(g_strcmp0(tmp, "yes") && g_strcmp0(tmp, "true") && g_strcmp0(tmp, "1")) - g_string_assign(item->guest_ok,"no"); - else - g_string_assign(item->guest_ok,"yes"); - } - } - break; - - default: - break; - } - - g_free(lower); -} - - - -/** -* Funciton loads all share section to array -*/ -gint -load_smb_conf (GPtrArray *shared_items) -{ - - FILE *smb_file; /* Samba config file */ - gchar *line; /* Line buffer */ - gboolean skip = FALSE; /* Skip lines of special sections */ - - gint arr_index = shared_items->len - 1; /* Count of items in shared_items array - 1; */ - - /* smb.conf - File exist test */ - if (!g_file_test(smb_conf_path, G_FILE_TEST_EXISTS)) - { - g_warning ("Config file \"%s\" not exist!", smb_conf_path); - return ERROR_FILE_NOT_EXIST; - } - - /* Try open smb.conf */ - smb_file = fopen(smb_conf_path, "r"); - if (smb_file == NULL) - { - g_warning ("Can not open file \"%s\"!", smb_conf_path); - return ERROR_CAN_NOT_OPEN_FILE; - } - - /* Inicialize buffers */ - line = g_strnfill(BUFSIZ, '\0'); - - /* Parse smb.conf file and load shared folders setings */ - while (fgets(line, BUFSIZ, smb_file)) - { - /* Remove white space */ - g_strstrip(line); - - /* Skip comments and empty lines */ - if(line[0] == ';' || line[0] == '#' || line[0] == '\0') - continue; - - if(line[0] == '[') - { - skip = FALSE; - - /* Test special sections */ - gint i ; - for (i = 0; i < SECTIONS_COUNT; i++) - { - if(strstr(line,smb_special_section[i]) != NULL) - { - skip = TRUE; - break; - } - } - - if(!skip) - { - /* Start of share section - alloc new item of array */ - TSmbConfItem *tmp_item = smbconf_item_new0(); - - /* Add item to array */ - g_ptr_array_add(shared_items, (gpointer) tmp_item); - arr_index++; - - /* Print error if pointers are different */ - if (g_ptr_array_index (shared_items, arr_index) != (gpointer) tmp_item) - g_warning ("load_smb_conf (GPtrArray **shared_items): got %p instead of %p\n", - g_ptr_array_index(shared_items, arr_index), tmp_item); - } - } - - /* Skip special sections (global, homes, printers) */ - if(skip) - continue; - - TSmbConfItem *tmp = g_ptr_array_index(shared_items, arr_index); - - /* Recognize parameters and fill in item */ - parse_to_share_item (line, tmp); - - } - - /* Free */ - g_free(line); - - fclose(smb_file); - - return 0; -} - - -/** -* Returns share imte if directory [path] is shared, -* if not return NULL; -*/ -TSmbConfItem -*is_shared_item(GPtrArray *shared_items, const gchar *path) -{ - TSmbConfItem *ret = NULL; - - TSmbConfItem *tmp; - gint i; - for(i = 0; i < shared_items->len; i++) - { - tmp = g_ptr_array_index(shared_items,i); - if(strstr(tmp->path->str,path)) - { - ret = tmp; - break; - } - } - - return ret; -} - - -/** -* True if share name exist -*/ -gboolean -share_name_exist (GPtrArray *shared_items, const gchar *sharename) -{ - TSmbConfItem *tmp; - gint i; - for(i = 0; i < shared_items->len; i++) - { - tmp = g_ptr_array_index(shared_items,i); - if(strstr(tmp->name->str,sharename)) - { - return TRUE; - } - } - - return FALSE; -} - - -/** -* Write new share section or change chare section defined by share parameter -*/ -gint -write_share (GPtrArray *shared_items, TSmbConfItem *share) -{ - - TSmbConfItem *item; /* Shared item */ - FILE *smb_file; /* Samba config file */ - GString *smb_conf_new; /* Content of smb.conf */ - gchar *line; /* Line buffer */ - gchar *tmp; /* Temp */ - - gboolean writed_change [KEYWORDS_COUNT] = {FALSE}; - gboolean check_writed_changes = FALSE; - gboolean new_share = FALSE; - gboolean change = FALSE; - gboolean found = FALSE; - - item = is_shared_item(shared_items, share->path->str); - - /* If item is not shared we just append it to end of smb.conf */ - if(!item) - { - if(share_name_exist(shared_items, share->name->str)) - return ERROR_SHARE_NAME_EXIST; - - new_share = TRUE; - } - - /* smb.conf - File exist test */ - if(!g_file_test(smb_conf_path, G_FILE_TEST_EXISTS)) - { - g_warning ("Config file \"%s\" does not exist!", smb_conf_path); - return ERROR_FILE_NOT_EXIST; - } - - /* Try open smb.conf - rewrite all */ - smb_file = fopen(smb_conf_path, "r"); - if(smb_file == NULL) - { - g_warning ("Can not open file \"%s\"!", smb_conf_path); - return ERROR_CAN_NOT_OPEN_FILE; - } - - /* Inicialize buffers */ - smb_conf_new = g_string_new(""); - line = g_strnfill(BUFSIZ, '\0'); - tmp = g_strnfill(BUFSIZ, '\0'); - - - /* Load smb.conf, change or add share section share */ - while(fgets(line, BUFSIZ, smb_file)) - { - gchar *orig = g_strdup(line); - - /* Remove white space */ - g_strstrip(line); - - if(!new_share) /* Find section to change */ - if(line[0] == '[') - { - if (change) /* Section to change ends, check if all atributes are wroted */ - check_writed_changes = TRUE; - - change = FALSE; - - /* Is this section to change? */ - if(strstr(line,item->name->str)) - { - change = TRUE; - } - } - - if(check_writed_changes) - { - /* Section name and path are allways writed */ - - if(!writed_change[COMMENT_ID] && (share->comment->len > 0)) - { - g_string_append_printf(smb_conf_new, "\tcomment = %s\n",share->comment->str); - } - - if(!writed_change[READ_ONLY_ID] && (share->read_only->len > 0)) - { - g_string_append_printf(smb_conf_new, "\tread only = %s\n",share->read_only->str); - } - - if(!writed_change[WRTITABLE_ID] && (share->writable->len > 0)) - { - g_string_append_printf(smb_conf_new, "\twritable = %s\n",share->writable->str); - } - - if(!writed_change[GUEST_OK_ID] && (share->guest_ok->len > 0)) - { - g_string_append_printf(smb_conf_new, "\tguest ok = %s\n",share->guest_ok->str); - } - } - - - /* New share or no change */ - if(!change || new_share) - { - /* Just copy text from smb.conf */ - g_string_append(smb_conf_new,orig); - } - else - { - /* Change this line */ - gchar *lower = g_ascii_strdown(line, strlen(line)); /* lower line */ - - found = FALSE; - - /* Find keywords on line *txt */ - gint i; - for(i = 0; i < KEYWORDS_COUNT; i++) - { - gchar *point; - if((point = strstr(lower,keywords[i])) != NULL) - { - /* Keywords must be at begining of line */ - if(point == lower) - { - found = TRUE; - break; - } - } - } - - /* Change share atributes */ - if(found) - switch(i) - { - /* [share name] */ - case SHARE_NAME_ID: - { - g_string_append_printf(smb_conf_new, "[%s]\n",share->name->str); - writed_change[SHARE_NAME_ID] = TRUE; - } - break; - - - /* path = */ - case PATH_ID: - { - /* no change - path must be same */ - g_string_append(smb_conf_new,orig); - writed_change[PATH_ID] = TRUE; - } - break; - - - /* comment = */ - case COMMENT_ID: - { - if(share->comment->len > 0) - { - - g_string_append_printf(smb_conf_new, "\tcomment = %s\n",share->comment->str); - writed_change[COMMENT_ID] = TRUE; - } - } - break; - - - /* read only = */ - case READ_ONLY_ID: - { - - if(share->read_only->len > 0) - { - g_string_append_printf(smb_conf_new, "\tread only = %s\n",share->read_only->str); - writed_change[READ_ONLY_ID] = TRUE; - } - } - break; - - - /* writ = writable, writeable, write ok = */ - case WRTITABLE_ID: - { - if(share->writable->len > 0) - { - g_string_append_printf(smb_conf_new, "\twritable = %s\n",share->writable->str); - writed_change[WRTITABLE_ID] = TRUE; - } - } - break; - - - /* guest ok = */ - case GUEST_OK_ID: - { - if(share->guest_ok->len > 0) - { - g_string_append_printf(smb_conf_new, "\tguest ok = %s\n",share->guest_ok->str); - writed_change[GUEST_OK_ID] = TRUE; - } - } - break; - - default: - break; - } - - g_free(lower); - } - - g_free(orig); - } - - if(!check_writed_changes && change) - { - /* Section name and path are allways writed */ - - if(!writed_change[COMMENT_ID] && (share->comment->len > 0)) - { - g_string_append_printf(smb_conf_new, "\tcomment = %s\n",share->comment->str); - } - - if(!writed_change[READ_ONLY_ID] && (share->read_only->len > 0)) - { - g_string_append_printf(smb_conf_new, "\tread only = %s\n",share->read_only->str); - } - - if(!writed_change[WRTITABLE_ID] && (share->writable->len > 0)) - { - g_string_append_printf(smb_conf_new, "\twritable = %s\n",share->writable->str); - } - - if(!writed_change[GUEST_OK_ID] && (share->guest_ok->len > 0)) - { - g_string_append_printf(smb_conf_new, "\tguest ok = %s\n",share->guest_ok->str); - } - } - - - /* Create new share */ - if(new_share) - { - - g_string_append_printf(smb_conf_new, "\n[%s]\n",share->name->str); - - g_string_append_printf(smb_conf_new, "\tpath = %s\n",share->path->str); - - - if(share->comment->len > 0) - { - g_string_append_printf(smb_conf_new, "\tcomment = %s\n",share->comment->str); - } - - - if(share->read_only->len > 0) - { - g_string_append_printf(smb_conf_new, "\tread only = %s\n",share->read_only->str); - } - - if(share->writable->len > 0) - { - g_string_append_printf(smb_conf_new, "\twritable = %s\n",share->writable->str); - } - - if(share->guest_ok->len > 0) - { - g_string_append_printf(smb_conf_new, "\tguest ok = %s\n",share->guest_ok->str); - } - } - - /* Write new share to smb.conf */ - write_smbconf(smb_conf_new->str); - - fclose(smb_file); - - g_free(line); - g_free(tmp); - - g_string_free(smb_conf_new,TRUE); - - return 0; -} - - - -/** -* Function erase shared section containing path from smb.conf -* shared_items must by actual! -*/ -gint delete_share (GPtrArray *shared_items, const gchar *path) -{ - FILE *smb_file; /* Samba config file */ - GString *smb_conf_new; /* Content of smb.conf */ - gchar *line; /* Line buffer */ - gchar *orig; /* Original line */ - TSmbConfItem *skip_item; /* Skip this share */ - gboolean skip = FALSE; /* Skip lines (erase from config) */ - - skip_item = is_shared_item(shared_items, path); - - if(!skip_item) - { - g_warning ("Directory \"%s\" is NOT shared",path); - return ERROR_DIRECTORY_NOT_SHARED; - } - - - /* smb.conf - File exist test */ - if(!g_file_test(smb_conf_path, G_FILE_TEST_EXISTS)) - { - g_warning ("Config file \"%s\" does not exist!", smb_conf_path); - return ERROR_FILE_NOT_EXIST; - } - - /* Try open smb.conf - rewrite all */ - smb_file = fopen(smb_conf_path, "r"); - if(smb_file == NULL) - { - g_warning ("Can not open file \"%s\"!", smb_conf_path); - return ERROR_CAN_NOT_OPEN_FILE; - } - - /* Inicialize buffers */ - smb_conf_new = g_string_new(""); - line = g_strnfill(BUFSIZ, '\0'); - orig = g_strnfill(BUFSIZ, '\0'); - - - /* Load smb.conf WITHOUT section skip_name (section containing path) */ - while(fgets(line, BUFSIZ, smb_file)) - { - g_stpcpy(orig, line); - - /* Remove white space */ - g_strstrip(line); - - if(line[0] == '[') - { - skip = FALSE; - - /* Test special sections */ - if(strstr(line,skip_item->name->str)) - { - skip = TRUE; - } - } - - if(!skip) - { - g_string_append(smb_conf_new,orig); - } - } - - /* Write deleted share to smb.conf */ - write_smbconf(smb_conf_new->str); - - fclose(smb_file); - - g_free(line); - g_free(orig); - - g_string_free (smb_conf_new,TRUE); - - return OK; -} - -void item_to_strv (TSmbConfItem *item, gchar ***ret) -{ - /* [share name], "path =", "commennt =", "read only =", "guest ok =" */ - - *ret = g_new (gchar *, 6); - - (*ret)[0] = g_strdup (item->name->str); - (*ret)[1] = g_strdup (item->path->str); - (*ret)[2] = g_strdup (item->comment->str); - (*ret)[3] = g_strdup (item->read_only->str); - (*ret)[4] = g_strdup (item->guest_ok->str); - - (*ret)[5] = NULL; -} - - - -void char_to_strv(const gchar *str, gchar ***ret) -{ - *ret = g_new (char *, 2); - - (*ret)[0] = g_strdup (str); - (*ret)[1] = NULL; -} - - -/** -* If path is shared then returns share parameters in result. -*/ -gint -smb_get_share_status (const gchar *path, gchar ***result) -{ - gboolean found = FALSE; - GPtrArray *shared_items = shared_items_array_new(); - - Error_sfshare err; - - err = load_smb_conf (shared_items); - - if (err != OK) - { - /* Free */ - shared_items_array_free (shared_items); - return err; - } - - - TSmbConfItem *test; - gint i; - for (i = 0; i < shared_items->len; i++) - { - test = g_ptr_array_index(shared_items, i); - if (!g_strcmp0(test->path->str,path)) - { - found = TRUE; /* Path found in smb.conf */ - break; - } - } - - if (found) - item_to_strv(test, result); - else - char_to_strv(NULL,result); - - /* Free */ - shared_items_array_free (shared_items); - - return OK; -} - - -/** -* Write share to smb.conf -*/ -gint -smb_set_share (gchar **parameters) -{ - gint ret = OK; - /* Create new items array */ - GPtrArray *shared_items = shared_items_array_new (); - - /* Load share sections from smb.conf */ - ret = load_smb_conf (shared_items); - - /* Create share item */ - /* [share name], "path =", "commennt =", "read only =", "guest ok =" */ - TSmbConfItem *item = smbconf_item_new (parameters[0], parameters[1], parameters[2], parameters[3], parameters[4]); - - /* Write share to smb.conf */ - if(ret == OK) - ret = write_share (shared_items, item); - - /* free */ - smbconf_item_free (item); - shared_items_array_free (shared_items); - - return ret; -} - - -/** -* Delete share section from smb.conf -*/ -gint -smb_delete_share (gchar **path) -{ - gint ret = OK; - /* Create new items array */ - GPtrArray *shared_items = shared_items_array_new (); - - /* Load share sections from smb.conf */ - ret = load_smb_conf (shared_items); - - /* try to delete shared section */ - if(ret == OK) - ret = delete_share (shared_items, path[0]); - - /* Free */ - shared_items_array_free (shared_items); - - return ret; -} - diff --git a/sfshare-daemon/src/samba_share.h b/sfshare-daemon/src/samba_share.h deleted file mode 100644 index ae9e34b..0000000 --- a/sfshare-daemon/src/samba_share.h +++ /dev/null @@ -1,81 +0,0 @@ -/* - * 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 library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this library; if not, write to the Free - * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - */ - -#ifndef SAMBA_SHARE_H -#define SAMBA_SHARE_H - -#include - - -typedef struct smb_conf_item -{ - GString *name; /* [share name] */ - GString *path; /* path = /path/to/folder */ - GString *comment; /* comment = Some text comment */ - GString *read_only; /* read only = yes | no */ - GString *writable; /* writable = yes | no ... writeable, write ok */ - GString *guest_ok; /* guest ok = yes | no */ -} TSmbConfItem; - - -/** Funciton loads all share section to array */ -gint load_smb_conf (GPtrArray *shared_items); - -/** Function erase shared section containing path from smb.conf */ -gint delete_share (GPtrArray *shared_items, const gchar *path); - -/** Write new share section or change chare section defined by share parameter */ -gint write_share (GPtrArray *shared_items, TSmbConfItem *share); - -/** If path is shared then returns share parameters in result. */ -gboolean smb_get_share_status (const gchar *path, gchar ***result); - -/** Write or channge section in smb.conf */ -gint smb_set_share (gchar **parameters); - -/** Delete share section from smb.conf */ -gint smb_delete_share (gchar **path); - -void item_to_strv (TSmbConfItem *item, gchar ***ret); - - -/** Function changes path to smb.conf to path */ -void set_smbconf_path (const gchar *path); - - -/** Function returns new array */ -GPtrArray* shared_items_array_new(); - -/** Function destroy Share Items Array */ -void shared_items_array_free (GPtrArray *array); - -/** Function allocs memory for TSmbConfItem and sets parameters */ -TSmbConfItem* smbconf_item_new (const gchar *name, const gchar *path, const gchar *comment, const gchar *read_only, const gchar *guest_ok); - -/** Function allocs memory for TSmbConfItem */ -TSmbConfItem* smbconf_item_new0 (); - - -/** If ok return 0 else return number of error */ -gint check_item (TSmbConfItem *item); - -/** Function free memory */ -void smbconf_item_free (TSmbConfItem *item); - -/** Send SIGHUP to smb and nmb */ -void smb_reload_service (); -#endif diff --git a/sfshare-daemon/src/sfshare_errors.h b/sfshare-daemon/src/sfshare_errors.h deleted file mode 100644 index 6c7bc4b..0000000 --- a/sfshare-daemon/src/sfshare_errors.h +++ /dev/null @@ -1,46 +0,0 @@ -/* - * 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 library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this library; if not, write to the Free - * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - */ - -#ifndef SFSHARE_ERRORS_H -#define SFSHARE_ERRORS_H - - -/** Types of errors */ -typedef enum -{ - OK, - ERROR_FAILED, - ERROR_PERMISSION_DENIED, - - /* Samba share */ - ERROR_FILE_NOT_EXIST, - ERROR_CAN_NOT_OPEN_FILE, - ERROR_CAN_NOT_WRITE_TO_FILE, - ERROR_DIRECTORY_NOT_SHARED, - ERROR_WRONG_NAME, - ERROR_WRONG_PATH, - ERROR_READONLY_WRITABLE, - ERROR_SHARE_NAME_EXIST, - - NUM_ERRORS -} Error_sfshare; - - -/** Definition in file: dbus_service.c */ -gchar *get_error_msg (Error_sfshare err); - -#endif diff --git a/sfshare-daemon/src/sfshared.c b/sfshare-daemon/src/sfshared.c deleted file mode 100644 index 0a2ee3b..0000000 --- a/sfshare-daemon/src/sfshared.c +++ /dev/null @@ -1,30 +0,0 @@ -/* - * 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 library 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 - * Library General Public License for more details. - * - * You should have received a copy of the GNU General Public - * License along with this library; if not, write to the Free - * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - * - */ - -#include -#include -#include -#include - -#include "samba_share.h" -#include "dbus_service.h" - -/** Run daemon */ -int main() -{ - return dbus_sfshare_start(); -} -- cgit