diff options
author | Jan Lipovsky <janlipovsky@gmail.com> | 2010-04-20 15:53:28 +0200 |
---|---|---|
committer | Jan Lipovsky <janlipovsky@gmail.com> | 2010-04-20 15:53:28 +0200 |
commit | 807a346ac1bc467106b7956757fd019833d19b25 (patch) | |
tree | 7828d1ed091367210918ebdb795ad1ed6541fff1 /sfshare-daemon | |
parent | d324f3f6a85cd1f43deb2ac281eb172abc66f431 (diff) | |
download | sfshare-807a346ac1bc467106b7956757fd019833d19b25.tar.gz sfshare-807a346ac1bc467106b7956757fd019833d19b25.tar.xz sfshare-807a346ac1bc467106b7956757fd019833d19b25.zip |
Polkit authorization, nautilus plugin, dbus methods
Diffstat (limited to 'sfshare-daemon')
-rwxr-xr-x | sfshare-daemon/data/org.fedoraproject.SimpleFileShare.conf | 19 | ||||
-rwxr-xr-x | sfshare-daemon/data/org.fedoraproject.SimpleFileShare.policy | 31 | ||||
-rwxr-xr-x | sfshare-daemon/data/org.fedoraproject.SimpleFileShare.service | 4 | ||||
-rwxr-xr-x | sfshare-daemon/data/org.fedoraproject.SimpleFileShare.xml | 25 | ||||
-rw-r--r--[-rwxr-xr-x] | sfshare-daemon/src/Makefile | 25 | ||||
-rw-r--r-- | sfshare-daemon/src/dbus_service.c | 360 | ||||
-rw-r--r-- | sfshare-daemon/src/dbus_service.h | 20 | ||||
-rw-r--r-- | sfshare-daemon/src/dbus_service_glue.h | 105 | ||||
-rw-r--r-- | sfshare-daemon/src/samba_share.c | 366 | ||||
-rw-r--r-- | sfshare-daemon/src/samba_share.h | 42 | ||||
-rw-r--r-- | sfshare-daemon/src/sfshare_errors.h | 28 | ||||
-rw-r--r-- | sfshare-daemon/src/sfshared.c | 42 |
12 files changed, 787 insertions, 280 deletions
diff --git a/sfshare-daemon/data/org.fedoraproject.SimpleFileShare.conf b/sfshare-daemon/data/org.fedoraproject.SimpleFileShare.conf new file mode 100755 index 0000000..83a2540 --- /dev/null +++ b/sfshare-daemon/data/org.fedoraproject.SimpleFileShare.conf @@ -0,0 +1,19 @@ +<?xml version="1.0" encoding="UTF-8"?> <!-- -*- XML -*- --> + +<!DOCTYPE busconfig PUBLIC + "-//freedesktop//DTD D-BUS Bus Configuration 1.0//EN" + "http://www.freedesktop.org/standards/dbus/1.0/busconfig.dtd"> +<busconfig> + + <!-- Only root can own the service --> + <policy user="root"> + <allow own="org.fedoraproject.SimpleFileShare"/> + </policy> + + <!-- Allow anyone to invoke methods on the interfaces, + authorization is performed by PolicyKit --> + <policy context="default"> + <allow send_destination="org.fedoraproject.SimpleFileShare"/> + </policy> + +</busconfig> diff --git a/sfshare-daemon/data/org.fedoraproject.SimpleFileShare.policy b/sfshare-daemon/data/org.fedoraproject.SimpleFileShare.policy new file mode 100755 index 0000000..f0c6894 --- /dev/null +++ b/sfshare-daemon/data/org.fedoraproject.SimpleFileShare.policy @@ -0,0 +1,31 @@ +<?xml version="1.0" encoding="UTF-8"?> +<!DOCTYPE policyconfig PUBLIC + "-//freedesktop//DTD PolicyKit Policy Configuration 1.0//EN" + "http://www.freedesktop.org/standards/PolicyKit/1.0/policyconfig.dtd"> +<policyconfig> + + <vendor>SimpleFileShare</vendor> + <vendor_url>git://fedorapeople.org/home/fedora/lipoja/public_git/sfshare.git</vendor_url> + + <action id="org.fedoraproject.SimpleFileShare.setup_share"> + <description>Setup share</description> + <message>Writing to smb.conf requires authentication</message> + <defaults> + <allow_any>auth_admin_keep</allow_any> + <allow_inactive>auth_admin_keep</allow_inactive> + <allow_active>auth_admin_keep</allow_active> + </defaults> + </action> + + + <action id="org.fedoraproject.SimpleFileShare.delete_share"> + <description>Delete share from smb.conf</description> + <message>Writing to smb.conf requires authentication</message> + <defaults> + <allow_any>auth_admin_keep</allow_any> + <allow_inactive>auth_admin_keep</allow_inactive> + <allow_active>auth_admin_keep</allow_active> + </defaults> + </action> + +</policyconfig> diff --git a/sfshare-daemon/data/org.fedoraproject.SimpleFileShare.service b/sfshare-daemon/data/org.fedoraproject.SimpleFileShare.service new file mode 100755 index 0000000..ebf3c37 --- /dev/null +++ b/sfshare-daemon/data/org.fedoraproject.SimpleFileShare.service @@ -0,0 +1,4 @@ +[D-BUS Service] +Name=org.fedoraproject.SimpleFileShare +Exec=/usr/sbin/sfshared +User=root diff --git a/sfshare-daemon/data/org.fedoraproject.SimpleFileShare.xml b/sfshare-daemon/data/org.fedoraproject.SimpleFileShare.xml new file mode 100755 index 0000000..419fa30 --- /dev/null +++ b/sfshare-daemon/data/org.fedoraproject.SimpleFileShare.xml @@ -0,0 +1,25 @@ +<!DOCTYPE node PUBLIC +"-//freedesktop//DTD D-BUS Object Introspection 1.0//EN" +"http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd" > + +<node name="/org/fedoraproject/SimpleFileShare"> + <interface name="org.fedoraproject.SimpleFileShare"> + <method name="get_share_status"> + <arg type="s" name="path" direction="in" /> + <arg type="as" name="status" direction="out" /> + </method> + + <method name="setup_share"> + <annotation name="org.freedesktop.DBus.GLib.Async" value=""/> + <arg type="as" name="parameters" direction="in" /> + </method> + + <method name="delete_share"> + <annotation name="org.freedesktop.DBus.GLib.Async" value=""/> + <arg type="s" name="path" direction="in" /> + </method> + </interface> +</node> + + + diff --git a/sfshare-daemon/src/Makefile b/sfshare-daemon/src/Makefile index c7ea93c..3499b8c 100755..100644 --- a/sfshare-daemon/src/Makefile +++ b/sfshare-daemon/src/Makefile @@ -1,7 +1,7 @@ # TEMPORARY MAKEFILE CXX=gcc -CXXFLAGS=-std=c99 -Wall `pkg-config --cflags --libs gtk+-2.0 dbus-glib-1` +CXXFLAGS=-std=c99 -Wall `pkg-config --cflags --libs gtk+-2.0 dbus-glib-1 polkit-gtk-1` # objektove soubory @@ -11,10 +11,10 @@ all: sfshared ############################################################################### -samba_share.o: samba_share.c samba_share.h +samba_share.o: samba_share.c samba_share.h sfshare_errors.h $(CXX) $(CXXFLAGS) -c samba_share.c -dbus_service.o: dbus_service.c dbus_service.h samba_share.h +dbus_service.o: dbus_service.c dbus_service.h samba_share.h sfshare_errors.h $(CXX) $(CXXFLAGS) -c dbus_service.c sfshared: $(OBJS) @@ -22,11 +22,28 @@ sfshared: $(OBJS) ################################################################################ -.PHONY: clean edit +.PHONY: clean edit install uninstall clean: rm -f *.o rm -f sfshared + +install: all + cp sfshared /usr/sbin +#### only root can run it + cp ../data/org.fedoraproject.SimpleFileShare.conf /etc/dbus-1/system.d/ +#### only root can run it + cp ../data/org.fedoraproject.SimpleFileShare.policy /usr/share/polkit-1/actions/ +#### only root can run it + cp ../data/org.fedoraproject.SimpleFileShare.service /usr/share/dbus-1/system-services/ + +uninstall: + rm -f /usr/sbin/sfshared + rm -f /etc/dbus-1/system.d/org.fedoraproject.SimpleFileShare.conf + rm -f /usr/share/polkit-1/actions/org.fedoraproject.SimpleFileShare.policy + rm -f /usr/share/dbus-1/system-services/org.fedoraproject.SimpleFileShare.service + + ################################################################################ edit: diff --git a/sfshare-daemon/src/dbus_service.c b/sfshare-daemon/src/dbus_service.c index 09ffc28..c81006d 100644 --- a/sfshare-daemon/src/dbus_service.c +++ b/sfshare-daemon/src/dbus_service.c @@ -1,49 +1,42 @@ #include <dbus/dbus-glib.h> +#include <dbus/dbus-glib-lowlevel.h> #include <glib.h> #include <dbus/dbus.h> #include <stdio.h> #include <stdlib.h> - #include <sys/types.h> #include <glib-object.h> +#include <polkit/polkit.h> #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" -//static void lose (const char *fmt, ...) G_GNUC_NORETURN G_GNUC_PRINTF (1, 2); -//static void lose_gerror (const char *prefix, GError *error) G_GNUC_NORETURN; -// -//static void -//lose (const char *str, ...) -//{ -// va_list args; -// -// va_start (args, str); -// -// vfprintf (stderr, str, args); -// fputc ('\n', stderr); -// -// va_end (args); -// -// exit (1); -//} -// -//static void -//lose_gerror (const char *prefix, GError *error) -//{ -// lose ("%s: %s", prefix, error->message); -//} 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 @@ -51,6 +44,8 @@ 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)) @@ -60,12 +55,189 @@ struct DaemonObjectClass G_DEFINE_TYPE(DaemonObject, daemon_object, G_TYPE_OBJECT) -gboolean daemon_get_share_status (DaemonObject *obj, const char *path, char ***result, GError **error); -gboolean daemon_setup_share (DaemonObject *obj, const char **parameters, char **result, GError **error); -gboolean daemon_delete_share (DaemonObject *obj,const char *path, char **result, GError **error); +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" +gchar * +get_error_msg (Error 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; + + 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); +} + + + +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); + + int i = 0; + + while(in[i] != NULL) + { + ret->in[i] = g_strdup (in[i]); + i++; + } + + return ret; +} + + +void +daemon_data_free (DaemonData * data) +{ + int i; + i = 0; + + while (data->in[i] != NULL) + { + g_free (data->in[i++]); + } + + g_free (data); + +} + + + + + +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) @@ -78,77 +250,157 @@ daemon_object_class_init (DaemonObjectClass *klass) } gboolean -daemon_get_share_status (DaemonObject *obj, const char *path,char ***result, GError **error) +daemon_get_share_status (DaemonObject *obj, const gchar *path, gchar ***result, GError **error) { puts("daemon_get_share_status"); - return smb_get_share_status(path, result); + Error 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; +} + + +void +setup_share_authorized (gpointer data) +{ + Error 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); } + + + gboolean -daemon_setup_share (DaemonObject *obj,const char **parameters, char **result, GError **error) +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; +} + + +void +delete_share_authorized (gpointer data) { - puts("daemon_setup_share"); + Error err; + DaemonData *dd = data; - return smb_set_share(parameters, result); + 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); } + + gboolean -daemon_delete_share (DaemonObject *obj, const char *path,char **result, GError **error) +daemon_delete_share (DaemonObject *obj, const gchar *path, DBusGMethodInvocation *context) { puts("daemon_delete_share"); - return smb_delete_share(path, result);; + 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; } + int dbus_sfshare_start () { - DBusGConnection *bus; - DBusGProxy *bus_proxy; GError *error = NULL; - DaemonObject *obj; GMainLoop *mainloop; guint request_name_result; + DaemonObject *obj; g_type_init (); - { - GLogLevelFlags fatal_mask; - - fatal_mask = g_log_set_always_fatal (G_LOG_FATAL_MASK); - fatal_mask |= G_LOG_LEVEL_WARNING | G_LOG_LEVEL_CRITICAL; - g_log_set_always_fatal (fatal_mask); - } - 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_SESSION, &error); + bus = dbus_g_bus_get (DBUS_BUS_SYSTEM, &error); if (!bus) - // lose_gerror ("Couldn't connect to session bus", error); - g_warning ("Couldn't connect to session bus : %s", error->message); + g_warning ("Couldn't connect to system bus : %s", error->message); + + + /* 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, "net.lipoja.SimpleFileShare", + G_TYPE_STRING, "org.fedoraproject.SimpleFileShare", G_TYPE_UINT, 0, G_TYPE_INVALID, G_TYPE_UINT, &request_name_result, G_TYPE_INVALID)) - //lose_gerror ("Failed to acquire org.designfu.SampleService", error); - g_warning("Failed to acquire org.designfu.SampleService : %s", error->message); + { + g_warning("Failed to acquire org.fedoraproject.SimpleFileShare : %s", error->message); + return -1; + } obj = g_object_new (DAEMON_TYPE_OBJECT, NULL); - dbus_g_connection_register_g_object (bus, "/SFShare", G_OBJECT (obj)); - + dbus_g_connection_register_g_object (bus, "/org/fedoraproject/SimpleFileShare", G_OBJECT (obj)); - printf ("Simple File Share - service runnning\n"); + g_print ("Simple File Share - service running\n"); g_main_loop_run (mainloop); diff --git a/sfshare-daemon/src/dbus_service.h b/sfshare-daemon/src/dbus_service.h index b6163f8..9f4bd09 100644 --- a/sfshare-daemon/src/dbus_service.h +++ b/sfshare-daemon/src/dbus_service.h @@ -1,7 +1,27 @@ #ifndef DBUS_SERVICE_H #define DBUS_SERVICE_H +#include <glib.h> +#include <dbus/dbus-glib-lowlevel.h> + + + +typedef void (*AuthorizedCallback) (void *data); + +typedef struct +{ + DBusGMethodInvocation *context; + gchar **in; + AuthorizedCallback authorized_cb; + +} DaemonData; + + 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 index a3f1259..ec1b428 100644 --- a/sfshare-daemon/src/dbus_service_glue.h +++ b/sfshare-daemon/src/dbus_service_glue.h @@ -53,6 +53,50 @@ G_BEGIN_DECLS #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, @@ -102,33 +146,30 @@ dbus_glib_marshal_daemon_BOOLEAN__STRING_POINTER_POINTER (GClosure *closure, g_value_set_boolean (return_value, v_return); } -/* BOOLEAN:BOXED,POINTER,POINTER */ -extern void dbus_glib_marshal_daemon_BOOLEAN__BOXED_POINTER_POINTER (GClosure *closure, - GValue *return_value, - guint n_param_values, - const GValue *param_values, - gpointer invocation_hint, - gpointer marshal_data); +/* 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_BOOLEAN__BOXED_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) +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 gboolean (*GMarshalFunc_BOOLEAN__BOXED_POINTER_POINTER) (gpointer data1, - gpointer arg_1, - gpointer arg_2, - gpointer arg_3, - gpointer data2); - register GMarshalFunc_BOOLEAN__BOXED_POINTER_POINTER callback; + 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; - gboolean v_return; - g_return_if_fail (return_value != NULL); - g_return_if_fail (n_param_values == 4); + g_return_if_fail (n_param_values == 3); if (G_CCLOSURE_SWAP_DATA (closure)) { @@ -140,16 +181,14 @@ dbus_glib_marshal_daemon_BOOLEAN__BOXED_POINTER_POINTER (GClosure *closure, data1 = g_value_peek_pointer (param_values + 0); data2 = closure->data; } - callback = (GMarshalFunc_BOOLEAN__BOXED_POINTER_POINTER) (marshal_data ? marshal_data : cc->callback); + callback = (GMarshalFunc_VOID__BOXED_POINTER) (marshal_data ? marshal_data : cc->callback); - v_return = callback (data1, - g_marshal_value_peek_boxed (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); + 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 @@ -158,15 +197,15 @@ G_END_DECLS #include <dbus/dbus-glib.h> 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_BOOLEAN__BOXED_POINTER_POINTER, 62 }, - { (GCallback) daemon_delete_share, dbus_glib_marshal_daemon_BOOLEAN__STRING_POINTER_POINTER, 126 }, + { (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, -"net.lipoja.sfshare\0GetShareStatus\0S\0path\0I\0s\0status\0O\0F\0N\0as\0\0net.lipoja.sfshare\0SetupShare\0S\0parameters\0I\0as\0result\0O\0F\0N\0s\0\0net.lipoja.sfshare\0DeleteShare\0S\0path\0I\0s\0result\0O\0F\0N\0s\0\0\0", +"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 index 1cdd285..5bfd97d 100644 --- a/sfshare-daemon/src/samba_share.c +++ b/sfshare-daemon/src/samba_share.c @@ -1,17 +1,18 @@ -#include <string.h> //strlen, strstr -
-#include <stdlib.h> //system() call +#include <string.h> /* strlen, strstr */ + +#include <stdlib.h> /* system() call */ #include <glib.h> #include <glib/gstdio.h> #include "samba_share.h" +#include "sfshare_errors.h" -#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 +#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 =" +/* [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 @@ -24,13 +25,13 @@ typedef enum keywords_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"}; +const gchar *smb_special_section [SECTIONS_COUNT] = {"global", "homes", "printers"}; -// Send SIGHUP to smb and nmb -void ReloadService() +/* Send SIGHUP to smb and nmb */ +void smb_reload_service () { system("killall -HUP smb nmb"); } @@ -40,7 +41,6 @@ void ReloadService() */ void set_smbconf_path (const gchar *path) { - //g_free(smb_conf_path); smb_conf_path = g_strdup(path); } @@ -48,35 +48,35 @@ void set_smbconf_path (const gchar *path) /* * Save string str as new smb.conf */ -gint WriteSmbConf(const gchar *content) +gint write_smbconf (const gchar *content) { FILE *smb_file; - //smb.conf - File exist test + /* smb.conf - File exist test */ if(!g_file_test(smb_conf_path, G_FILE_TEST_EXISTS)) { - g_error("Config file \"%s\" does not exist!", smb_conf_path); - return 1; + g_warning ("Config file \"%s\" does not exist!", smb_conf_path); + return ERROR_FILE_NOT_EXIST; } - //Try open smb.conf - rewrite all + /* Try open smb.conf - rewrite all */ smb_file = fopen(smb_conf_path, "w"); if(smb_file == NULL) { - g_error("Can not open file \"%s\"! Maybe, you dont have rights for change smb.conf", smb_conf_path); - return 1; + 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 + /* Write to smb.conf */ if(fputs(content,smb_file) == EOF) { - g_error("Can not write to file \"%s\"!", smb_conf_path); - return 1; + g_warning ("Can not write to file \"%s\"!", smb_conf_path); + return ERROR_CAN_NOT_WRITE_TO_FILE; } fclose(smb_file); - return 0; + return OK; } @@ -99,7 +99,7 @@ void smbconf_item_free(TSmbConfItem *item) /* * Function allocs memory for TSmbConfItem */ -TSmbConfItem* SmbConfItem_new0() +TSmbConfItem* smbconf_item_new0 () { TSmbConfItem *ret; ret = g_malloc(sizeof(struct smb_conf_item)); @@ -155,7 +155,8 @@ GPtrArray* shared_items_array_new() void shared_items_array_free(GPtrArray *array) { TSmbConfItem *tmp; - for(int i = 0; i < array->len; i++) + int i; + for(i = 0; i < array->len; i++) { tmp = g_ptr_array_index(array,i); smbconf_item_free(tmp); @@ -168,19 +169,19 @@ void shared_items_array_free(GPtrArray *array) /* * If ok return 0 else return number of error */ -gint CheckItem(TSmbConfItem *item) +gint check_item(TSmbConfItem *item) { if(item->name->len <= 0) - return -1; + return ERROR_WRONG_NAME; if(item->path->len <= 0) - return -2; + return ERROR_WRONG_PATH; - // read_only = true ... writable muust be = fals ... + /* read_only = true ... writable muust be = false */ if(g_string_equal(item->writable, item->read_only) && item->writable->len > 0) - return -3; + return ERROR_READONLY_WRITABLE; - return 0; + return OK; } @@ -193,14 +194,14 @@ void ParseShareItem(gchar *txt, TSmbConfItem *item) gchar *lower = g_ascii_strdown(txt, strlen(txt)); gboolean found = FALSE; - //Find keywords on line *txt + /* 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 + /* Keywords must be at begining of line */ if(point == lower) { found = TRUE; @@ -209,18 +210,18 @@ void ParseShareItem(gchar *txt, TSmbConfItem *item) } } - //Save atributes - Boolean variables case-insensitive - yes, no, true, false, 1, or 0 + /* Save atributes - Boolean variables case-insensitive - yes, no, true, false, 1, or 0 */ if(found) - switch(i) - { - // [share name] + switch(i) + { + /* [share name] */ case SHARE_NAME_ID: { g_string_append_len(item->name, txt + 1, strlen(txt)-2); } break; - // path = + /* path = */ case PATH_ID: { gchar *tmp; @@ -231,7 +232,7 @@ void ParseShareItem(gchar *txt, TSmbConfItem *item) } break; - // comment = + /* comment = */ case COMMENT_ID: { gchar *tmp; @@ -242,7 +243,7 @@ void ParseShareItem(gchar *txt, TSmbConfItem *item) } break; - // read only = + /* read only = */ case READ_ONLY_ID: { gchar *tmp; @@ -258,7 +259,7 @@ void ParseShareItem(gchar *txt, TSmbConfItem *item) } break; - // writ = writable, writeable, write ok = + /* writ = writable, writeable, write ok = */ case WRTITABLE_ID: { gchar *tmp; @@ -274,7 +275,7 @@ void ParseShareItem(gchar *txt, TSmbConfItem *item) } break; - // guest ok = + /* guest ok = */ case GUEST_OK_ID: { gchar *tmp; @@ -292,7 +293,7 @@ void ParseShareItem(gchar *txt, TSmbConfItem *item) default: break; - } + } g_free(lower); } @@ -314,16 +315,16 @@ gint load_smb_conf (GPtrArray *shared_items) /* smb.conf - File exist test */ if(!g_file_test(smb_conf_path, G_FILE_TEST_EXISTS)) { - g_error("Config file \"%s\" not exist!", smb_conf_path); - return 1; + 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_error("Can not open file \"%s\"!", smb_conf_path); - return 1; + g_warning ("Can not open file \"%s\"!", smb_conf_path); + return ERROR_CAN_NOT_OPEN_FILE; } /* Inicialize buffers */ @@ -344,7 +345,8 @@ gint load_smb_conf (GPtrArray *shared_items) skip = FALSE; /* Test special sections */ - for(int i = 0; i < SECTIONS_COUNT; i++) + int i ; + for(i = 0; i < SECTIONS_COUNT; i++) { if(strstr(line,smb_special_section[i]) != NULL) { @@ -356,7 +358,7 @@ gint load_smb_conf (GPtrArray *shared_items) if(!skip) { /* Start of share section - alloc new item of array */ - TSmbConfItem *tmp_item = SmbConfItem_new0(); + TSmbConfItem *tmp_item = smbconf_item_new0(); /* Add item to array */ g_ptr_array_add(shared_items, (gpointer) tmp_item); @@ -364,8 +366,8 @@ gint load_smb_conf (GPtrArray *shared_items) /* Print error if pointers are different */ if (g_ptr_array_index (shared_items, arr_index) != (gpointer) tmp_item) - g_error ("load_smb_conf (GPtrArray **shared_items): got %p instead of %p\n", - g_ptr_array_index(shared_items, arr_index), 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); } } @@ -393,12 +395,13 @@ gint load_smb_conf (GPtrArray *shared_items) * Returns share imte if directory [path] is shared, * if not return NULL; */ -TSmbConfItem *IsShared(GPtrArray *shared_items, const gchar *path) +TSmbConfItem *is_shared_item(GPtrArray *shared_items, const gchar *path) { TSmbConfItem *ret = NULL; TSmbConfItem *tmp; - for(int i = 0; i < shared_items->len; i++) + int i; + for(i = 0; i < shared_items->len; i++) { tmp = g_ptr_array_index(shared_items,i); if(strstr(tmp->path->str,path)) @@ -413,134 +416,174 @@ TSmbConfItem *IsShared(GPtrArray *shared_items, const gchar *path) /* -* Write ne share section or change chare section defined by share parameter +* Write new share section or change chare section defined by share parameter */ -gint write_share(GPtrArray *shared_items, TSmbConfItem *share) +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 + 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 = IsShared(shared_items, share->path->str); + 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 is not shared we just append it to end of smb.conf */ if(!item) new_share = TRUE; - //smb.conf - File exist test + /* smb.conf - File exist test */ if(!g_file_test(smb_conf_path, G_FILE_TEST_EXISTS)) { - g_error("Config file \"%s\" does not exist!", smb_conf_path); - return 1; + g_warning ("Config file \"%s\" does not exist!", smb_conf_path); + return ERROR_FILE_NOT_EXIST; } - //Try open smb.conf - rewrite all + /* Try open smb.conf - rewrite all */ smb_file = fopen(smb_conf_path, "r"); if(smb_file == NULL) { - g_error("Can not open file \"%s\"!", smb_conf_path); - return 1; + g_warning ("Can not open file \"%s\"!", smb_conf_path); + return ERROR_CAN_NOT_OPEN_FILE; } - //Inicialize buffers + /* 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 + /* Load smb.conf, change or add share section share */ while(fgets(line, BUFSIZ, smb_file)) { gchar *orig = g_strdup(line); - //Remove white space + /* Remove white space */ g_strstrip(line); - if(!new_share) //Find section to change + 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? + /* 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_sprintf(tmp,"\tcomment = %s\n",share->comment->str); + g_string_append(smb_conf_new,tmp); + } + + if(!writed_change[READ_ONLY_ID] && (share->read_only->len > 0)) + { + g_sprintf(tmp,"\tcomment = %s\n",share->read_only->str); + g_string_append(smb_conf_new,tmp); + } + + if(!writed_change[WRTITABLE_ID] && (share->writable->len > 0)) + { + g_sprintf(tmp,"\tcomment = %s\n",share->writable->str); + g_string_append(smb_conf_new,tmp); + } + + if(!writed_change[GUEST_OK_ID] && (share->guest_ok->len > 0)) + { + g_sprintf(tmp,"\tcomment = %s\n",share->guest_ok->str); + g_string_append(smb_conf_new,tmp); + } + } + + + /* New share or no change */ if(!change || new_share) { - //Just copy text from smb.conf + /* 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 + /* Change this line */ + gchar *lower = g_ascii_strdown(line, strlen(line)); /* lower line */ found = FALSE; - //Find keywords on line *txt + /* 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 + /* Keywords must be at begining of line */ if(point == lower) { found = TRUE; break; - } } } - //Change share atributes + /* Change share atributes */ if(found) - switch(i) - { - // [share name] + switch(i) + { + /* [share name] */ case SHARE_NAME_ID: { g_sprintf(tmp,"[%s]\n",share->name->str); g_string_append(smb_conf_new,tmp); + + writed_change[SHARE_NAME_ID] = TRUE; } break; - // path = + /* path = */ case PATH_ID: { - //no change - path must be same + /* no change - path must be same */ g_string_append(smb_conf_new,orig); + writed_change[PATH_ID] = TRUE; } break; - // comment = + /* comment = */ case COMMENT_ID: { if(share->comment->len > 0) { g_sprintf(tmp,"\tcomment = %s\n",share->comment->str); g_string_append(smb_conf_new,tmp); + writed_change[COMMENT_ID] = TRUE; } } break; - // read only = + /* read only = */ case READ_ONLY_ID: { @@ -548,37 +591,40 @@ gint write_share(GPtrArray *shared_items, TSmbConfItem *share) { g_sprintf(tmp,"\tread only = %s\n",share->read_only->str); g_string_append(smb_conf_new,tmp); + writed_change[READ_ONLY_ID] = TRUE; } } break; - // writ = writable, writeable, write ok = + /* writ = writable, writeable, write ok = */ case WRTITABLE_ID: { if(share->writable->len > 0) { g_sprintf(tmp,"\twritable = %s\n",share->writable->str); g_string_append(smb_conf_new,tmp); + writed_change[WRTITABLE_ID] = TRUE; } } break; - // guest ok = + /* guest ok = */ case GUEST_OK_ID: { if(share->guest_ok->len > 0) { g_sprintf(tmp,"\tguest ok = %s\n",share->guest_ok->str); g_string_append(smb_conf_new,tmp); + writed_change[GUEST_OK_ID] = TRUE; } } break; default: break; - } + } g_free(lower); } @@ -586,7 +632,37 @@ gint write_share(GPtrArray *shared_items, TSmbConfItem *share) g_free(orig); } - //Create new share + if(!check_writed_changes && change) + { + /* Section name and path are allways writed */ + + if(!writed_change[COMMENT_ID] && (share->comment->len > 0)) + { + g_sprintf(tmp,"\tcomment = %s\n",share->comment->str); + g_string_append(smb_conf_new,tmp); + } + + if(!writed_change[READ_ONLY_ID] && (share->read_only->len > 0)) + { + g_sprintf(tmp,"\tcomment = %s\n",share->read_only->str); + g_string_append(smb_conf_new,tmp); + } + + if(!writed_change[WRTITABLE_ID] && (share->writable->len > 0)) + { + g_sprintf(tmp,"\tcomment = %s\n",share->writable->str); + g_string_append(smb_conf_new,tmp); + } + + if(!writed_change[GUEST_OK_ID] && (share->guest_ok->len > 0)) + { + g_sprintf(tmp,"\tcomment = %s\n",share->guest_ok->str); + g_string_append(smb_conf_new,tmp); + } + } + + + /* Create new share */ if(new_share) { g_sprintf(tmp,"\n[%s]\n",share->name->str); @@ -621,8 +697,8 @@ gint write_share(GPtrArray *shared_items, TSmbConfItem *share) } } - //Write new share to smb.conf - WriteSmbConf(smb_conf_new->str); + /* Write new share to smb.conf */ + write_smbconf(smb_conf_new->str); fclose(smb_file); @@ -642,56 +718,56 @@ gint write_share(GPtrArray *shared_items, TSmbConfItem *share) */ 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) + 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 = IsShared(shared_items, path); + skip_item = is_shared_item(shared_items, path); if(!skip_item) { - g_warning("Directory \"%s\" is NOT shared",path); - return -1; + g_warning ("Directory \"%s\" is NOT shared",path); + return ERROR_DIRECTORY_NOT_SHARED; } - //smb.conf - File exist test + /* smb.conf - File exist test */ if(!g_file_test(smb_conf_path, G_FILE_TEST_EXISTS)) { - g_error("Config file \"%s\" does not exist!", smb_conf_path); - return -1; + g_warning ("Config file \"%s\" does not exist!", smb_conf_path); + return ERROR_FILE_NOT_EXIST; } - //Try open smb.conf - rewrite all + /* Try open smb.conf - rewrite all */ smb_file = fopen(smb_conf_path, "r"); if(smb_file == NULL) { - g_error("Can not open file \"%s\"!", smb_conf_path); - return -1; + g_warning ("Can not open file \"%s\"!", smb_conf_path); + return ERROR_CAN_NOT_OPEN_FILE; } - //Inicialize buffers + /* 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) + /* Load smb.conf WITHOUT section skip_name (section containing path) */ while(fgets(line, BUFSIZ, smb_file)) { g_stpcpy(orig, line); - //Remove white space + /* Remove white space */ g_strstrip(line); if(line[0] == '[') { skip = FALSE; - //Test special sections + /* Test special sections */ if(strstr(line,skip_item->name->str)) { skip = TRUE; @@ -704,20 +780,20 @@ gint delete_share (GPtrArray *shared_items, const gchar *path) } } - //Write deleted share to smb.conf - WriteSmbConf(smb_conf_new->str); + /* 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); + g_string_free (smb_conf_new,TRUE); - return 0; + return OK; } -void item_to_strv(TSmbConfItem *item, gchar ***ret) +void item_to_strv (TSmbConfItem *item, gchar ***ret) { /* [share name], "path =", "commennt =", "read only =", "guest ok =" */ @@ -730,8 +806,6 @@ void item_to_strv(TSmbConfItem *item, gchar ***ret) (*ret)[4] = g_strdup (item->guest_ok->str); (*ret)[5] = NULL; - -// return ret; } @@ -748,11 +822,22 @@ void char_to_strv(const gchar *str, gchar ***ret) /* * If path is shared then returns share parameters in result. */ -gboolean smb_get_share_status (const gchar *path, gchar ***result) +gint +smb_get_share_status (const gchar *path, gchar ***result) { gboolean found = FALSE; - GPtrArray *shared_items = shared_items_array_new();
- load_smb_conf (shared_items); + GPtrArray *shared_items = shared_items_array_new(); + + Error err; + + err = load_smb_conf (shared_items); + + if (err != OK) + { + /* Free */ + shared_items_array_free (shared_items); + return err; + } TSmbConfItem *test; @@ -762,7 +847,7 @@ gboolean smb_get_share_status (const gchar *path, gchar ***result) test = g_ptr_array_index(shared_items, i); if (!g_strcmp0(test->path->str,path)) { - found = TRUE; + found = TRUE; /* Path found in smb.conf */ break; } } @@ -772,45 +857,62 @@ gboolean smb_get_share_status (const gchar *path, gchar ***result) else char_to_strv(NULL,result); + /* Free */ shared_items_array_free (shared_items); - return TRUE; + return OK; } /* * Write share to smb.conf */ -gboolean smb_set_share (const gchar **parameters, gchar **result) +gint +smb_set_share (const gchar **parameters) { + gint ret = OK; + /* Create new items array */ GPtrArray *shared_items = shared_items_array_new (); - load_smb_conf (shared_items); + /* Load share sections from smb.conf */ + ret = load_smb_conf (shared_items); - /* [share name], "path =", "commennt =", "read only =", "guest ok =" */ + /* 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 (shared_items, item); + /* 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 TRUE; + return ret; } /* * Delete share to smb.conf */ -gboolean smb_delete_share (const gchar *path, gchar **result) +gint +smb_delete_share (const gchar **path) { + gint ret = OK; + /* Create new items array */ GPtrArray *shared_items = shared_items_array_new (); - load_smb_conf (shared_items); - delete_share (shared_items, path); + /* 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 TRUE; + return ret; } diff --git a/sfshare-daemon/src/samba_share.h b/sfshare-daemon/src/samba_share.h index 3fac458..61a3d5e 100644 --- a/sfshare-daemon/src/samba_share.h +++ b/sfshare-daemon/src/samba_share.h @@ -6,19 +6,19 @@ 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 + 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 +/* Funciton loads all share section to array */ gint load_smb_conf (GPtrArray *shared_items); -//Function erase shared section containing path from smb.conf +/*Function erase shared section containing path from smb.conf */ gint delete_share (GPtrArray *shared_items, const gchar *path); @@ -26,35 +26,35 @@ gint write_share (GPtrArray *shared_items, TSmbConfItem *share); gboolean smb_get_share_status (const gchar *path, gchar ***result); -gboolean smb_set_share (const gchar **parameters, gchar **result); -gboolean smb_delete_share (const gchar *path, gchar **result); +gint smb_set_share (const gchar **parameters); +gint smb_delete_share (const gchar **path); void item_to_strv (TSmbConfItem *item, gchar ***ret); -//Function changes path to smb.conf to path +/*Function changes path to smb.conf to path */ void set_smbconf_path (const gchar *path); -//Function returns new array +/*Function returns new array */ GPtrArray* shared_items_array_new(); -//Function destroy Share Items Array +/*Function destroy Share Items Array */ void shared_items_array_free (GPtrArray *array); -//Function allocs memory for TSmbConfItem and sets parameters +/*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* SmbConfItem_new0 (); +/*Function allocs memory for TSmbConfItem */ +TSmbConfItem* smbconf_item_new0 (); -//If ok return 0 else return number of error -gint CheckItem (TSmbConfItem *item); +/*If ok return 0 else return number of error */ +gint check_item (TSmbConfItem *item); -//Function free memory +/*Function free memory */ void smbconf_item_free (TSmbConfItem *item); -// Send SIGHUP to smb and nmb -void reload_service (); +/* 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 new file mode 100644 index 0000000..c1a5b3c --- /dev/null +++ b/sfshare-daemon/src/sfshare_errors.h @@ -0,0 +1,28 @@ +#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, + + NUM_ERRORS +} Error; + + +/* Definition inn file: dbus_service.c */ +gchar *get_error_msg (Error err); + +#endif diff --git a/sfshare-daemon/src/sfshared.c b/sfshare-daemon/src/sfshared.c index 5cad11b..4696008 100644 --- a/sfshare-daemon/src/sfshared.c +++ b/sfshare-daemon/src/sfshared.c @@ -1,44 +1,14 @@ -#include <stdio.h>
-#include <stdlib.h>
+#include <stdio.h> +#include <stdlib.h> #include <glib.h> #include <glib/gstdio.h> #include "samba_share.h" #include "dbus_service.h" -
-int main()
-{ - //set_smbconf_path ("smb.conf"); // Uncomment to set smb.conf path -/* - GPtrArray *shared_items = SharedItemsArray_new();
- LoadSmbConf(shared_items); - - //DeleteShare(shared_items,"/test/cesta"); // Uncomment to delete share specify by path - - TSmbConfItem *test; - for(int i = 0; i < shared_items->len; i++) - { - test = g_ptr_array_index(shared_items, i); - g_print("%s\n", test->path->str);//Vypise vsechny sdilene adresare - } - SharedItemsArray_free(shared_items); - - shared_items = SharedItemsArray_new();
- LoadSmbConf(shared_items); - - test = SmbConfItem_new("TEST", "/absolutni/cesta", "My Own New Comment", TRUE, TRUE); - - //WriteShare(shared_items, test); // Uncomment to write new share item (test) - - SmbConfItem_free(test); - - SharedItemsArray_free(shared_items); - - //ReloadService(); // Uncomment to reload samba daemon -*/ +int main() +{ dbus_sfshare_start(); -
- return 0;
-}
+ return 0; +} |