diff options
author | Jan Lipovsky <janlipovsky@gmail.com> | 2010-03-02 15:36:53 +0100 |
---|---|---|
committer | Jan Lipovsky <janlipovsky@gmail.com> | 2010-03-02 15:36:53 +0100 |
commit | c4b35e4ac79272b3b5716284d8d6565be4da0eb4 (patch) | |
tree | aaa27dfd73aed2c1e26351fe721460b9422cbde4 | |
parent | c6c9c8a731c7b2d521685e0f4f36e2cbb81b7876 (diff) | |
download | sfshare-c4b35e4ac79272b3b5716284d8d6565be4da0eb4.tar.gz sfshare-c4b35e4ac79272b3b5716284d8d6565be4da0eb4.tar.xz sfshare-c4b35e4ac79272b3b5716284d8d6565be4da0eb4.zip |
Simple File Share Daemon - smb.conf functions (Write, Change, Delete share) and reload samba ...
-rw-r--r-- | sfshare-daemon/src/samba_share.c | 225 | ||||
-rw-r--r-- | sfshare-daemon/src/samba_share.h | 36 | ||||
-rw-r--r-- | sfshare-daemon/src/sfshared.c | 12 |
3 files changed, 173 insertions, 100 deletions
diff --git a/sfshare-daemon/src/samba_share.c b/sfshare-daemon/src/samba_share.c index 873460a..076cdf7 100644 --- a/sfshare-daemon/src/samba_share.c +++ b/sfshare-daemon/src/samba_share.c @@ -1,14 +1,16 @@ -#include <string.h> //strlen +#include <string.h> //strlen, strstr +
+#include <stdlib.h> //system() call #include <glib.h> #include <glib/gstdio.h> #include "samba_share.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 + // [share name], "path =", "commennt =", "read only =", "writable =, writeable =, write ok =", "guest ok =" const gchar *keywords[KEYWORDS_COUNT] = {"[" ,"path", "comment", "read only", "writ", "guest"}; @@ -22,16 +24,23 @@ typedef enum keywords_id GUEST_OK_ID } TKeywords_id; + 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 ReloadService() +{ + system("killall -HUP smb nmb"); +} + /* * Function changes path to smb.conf to path */ -void ChangeSmbConfPath(const gchar *path) +void SetSmbConfPath(const gchar *path) { - g_free(smb_conf_path); + //g_free(smb_conf_path); smb_conf_path = g_strdup(path); } @@ -72,7 +81,6 @@ gint WriteSmbConf(const gchar *content) } - /* * Function free TSmbConfItem from memmory */ @@ -107,20 +115,37 @@ TSmbConfItem* SmbConfItem_new0() return ret; } + /* * Function allocs memory for TSmbConfItem */ -TSmbConfItem* SmbConfItem_new(gchar *name, gchar *path, gchar *comment, gchar *read_only, gchar *writable, gchar *guest_ok) +TSmbConfItem* SmbConfItem_new(gchar *name, gchar *path, gchar *comment, gboolean read_only, gboolean guest_ok) { TSmbConfItem *ret; ret = g_malloc(sizeof(struct smb_conf_item)); + gchar *yes = "yes"; + gchar *no = "no"; + 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); - ret->writable = g_string_new(writable); - ret->guest_ok = g_string_new(guest_ok); + if(read_only) + { + ret->read_only = g_string_new(yes); + ret->writable = g_string_new(no); + } + else + { + ret->read_only = g_string_new(no); + ret->writable = g_string_new(yes); + } + + if(guest_ok) + ret->guest_ok = g_string_new(yes); + else + ret->guest_ok = g_string_new(no); + return ret; } @@ -152,27 +177,51 @@ void SharedItemsArray_free(GPtrArray *array) /* +* If ok return 0 else return number of error +*/ +gint CheckItem(TSmbConfItem *item) +{ + if(item->name->len <= 0) + return -1; + + if(item->path->len <= 0) + return -2; + + // read_only = true ... writable muust be = fals ... + if(g_string_equal(item->writable, item->read_only) && item->writable->len > 0) + return -3; + + return 0; +} + + +/* * Parse line of smb.conf and fill competent field of item structure * return id of keyword found on line (txt) */ void ParseShareItem(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 = g_strrstr(lower,keywords[i])) != NULL) + 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] @@ -186,7 +235,7 @@ void ParseShareItem(gchar *txt, TSmbConfItem *item) case PATH_ID: { gchar *tmp; - if((tmp = g_strrstr(txt,"=")) != NULL) + if((tmp = strstr(txt,"=")) != NULL) { g_string_assign(item->path, g_strstrip(tmp + 1)); } @@ -197,7 +246,7 @@ void ParseShareItem(gchar *txt, TSmbConfItem *item) case COMMENT_ID: { gchar *tmp; - if((tmp = g_strrstr(txt,"=")) != NULL) + if((tmp = strstr(txt,"=")) != NULL) { g_string_assign(item->comment, g_strstrip(tmp + 1)); } @@ -208,7 +257,7 @@ void ParseShareItem(gchar *txt, TSmbConfItem *item) case READ_ONLY_ID: { gchar *tmp; - if((tmp = g_strrstr(lower,"=")) != NULL) + if((tmp = strstr(lower,"=")) != NULL) { g_strstrip(++tmp); @@ -218,12 +267,13 @@ void ParseShareItem(gchar *txt, TSmbConfItem *item) g_string_assign(item->read_only,"yes"); } } + break; // writ = writable, writeable, write ok = case WRTITABLE_ID: { gchar *tmp; - if((tmp = g_strrstr(lower,"=")) != NULL) + if((tmp = strstr(lower,"=")) != NULL) { g_strstrip(++tmp); @@ -239,7 +289,7 @@ void ParseShareItem(gchar *txt, TSmbConfItem *item) case GUEST_OK_ID: { gchar *tmp; - if((tmp = g_strrstr(lower,"=")) != NULL) + if((tmp = strstr(lower,"=")) != NULL) { g_strstrip(++tmp); @@ -307,7 +357,7 @@ gint LoadSmbConf(GPtrArray *shared_items) //Test special sections for(int i = 0; i < SECTIONS_COUNT; i++) { - if(g_strrstr(line,smb_special_section[i]) != NULL) + if(strstr(line,smb_special_section[i]) != NULL) { skip = TRUE; break; @@ -360,11 +410,12 @@ gint LoadSmbConf(GPtrArray *shared_items) TSmbConfItem *IsShared(GPtrArray *shared_items, const gchar *path) { TSmbConfItem *ret = NULL; + TSmbConfItem *tmp; for(int i = 0; i < shared_items->len; i++) { tmp = g_ptr_array_index(shared_items,i); - if(g_strrstr(tmp->path->str,path)) + if(strstr(tmp->path->str,path)) { ret = tmp; break; @@ -389,6 +440,7 @@ gint WriteShare(GPtrArray *shared_items, TSmbConfItem *share) gboolean new_share = FALSE; gboolean change = FALSE; + gboolean found = FALSE; item = IsShared(shared_items, share->path->str); @@ -433,7 +485,7 @@ gint WriteShare(GPtrArray *shared_items, TSmbConfItem *share) change = FALSE; //Is this section to change? - if(g_strrstr(line,item->name->str)) + if(strstr(line,item->name->str)) { change = TRUE; } @@ -447,78 +499,99 @@ gint WriteShare(GPtrArray *shared_items, TSmbConfItem *share) 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 = g_strrstr(lower,keywords[i])) != NULL) + if((point = strstr(lower,keywords[i])) != NULL) { //Keywords must be at begining of line if(point == lower) + { + found = TRUE; break; + + } } } //Change atributes - Boolean variables case-insensitive - yes, no, true, false, 1, or 0 + if(found) switch(i) { - // [share name] - case SHARE_NAME_ID: - { - g_sprintf(tmp,"[%s]\n",share->name->str); - g_string_append(smb_conf_new,tmp); - } - break; + // [share name] + case SHARE_NAME_ID: + { + g_sprintf(tmp,"[%s]\n",share->name->str); + g_string_append(smb_conf_new,tmp); + } + break; - // path = - case PATH_ID: - { - //no change - } - break; + // path = + case PATH_ID: + { + //no change + g_string_append(smb_conf_new,orig); + } + break; - // comment = - case COMMENT_ID: - { - g_sprintf(tmp,"\tcomment = %s\n",share->comment->str); - g_string_append(smb_conf_new,tmp); - } - break; + // 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); + } + } + break; - // read only = - case READ_ONLY_ID: - { - g_sprintf(tmp,"\tread only = %s\n",share->read_only->str); - g_string_append(smb_conf_new,tmp); - } + // read only = + case READ_ONLY_ID: + { + if(share->read_only->len > 0) + { + g_sprintf(tmp,"\tread only = %s\n",share->read_only->str); + g_string_append(smb_conf_new,tmp); + } + } + break; - // writ = writable, writeable, write ok = - case WRTITABLE_ID: - { - g_sprintf(tmp,"\twritable = %s\n",share->writable->str); - g_string_append(smb_conf_new,tmp); - } - break; + // 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); + } + } + break; - // guest ok = - case GUEST_OK_ID: - { - g_sprintf(tmp,"\tguest ok = %s\n",share->guest_ok->str); - g_string_append(smb_conf_new,tmp); - } - break; - default: + // 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); + } + } break; + + default: + break; } g_free(lower); @@ -536,18 +609,30 @@ gint WriteShare(GPtrArray *shared_items, TSmbConfItem *share) g_sprintf(tmp,"\tpath = %s\n",share->path->str); g_string_append(smb_conf_new,tmp); - g_sprintf(tmp,"\tcomment = %s\n",share->comment->str); - g_string_append(smb_conf_new,tmp); + if(share->comment->len > 0) + { + g_sprintf(tmp,"\tcomment = %s\n",share->comment->str); + g_string_append(smb_conf_new,tmp); + } - g_sprintf(tmp,"\tread only = %s\n",share->read_only->str); - g_string_append(smb_conf_new,tmp); - g_sprintf(tmp,"\twritable = %s\n",share->writable->str); - g_string_append(smb_conf_new,tmp); + if(share->read_only->len > 0) + { + g_sprintf(tmp,"\tread only = %s\n",share->read_only->str); + g_string_append(smb_conf_new,tmp); + } - g_sprintf(tmp,"\tguest ok = %s\n",share->guest_ok->str); - g_string_append(smb_conf_new,tmp); + if(share->writable->len > 0) + { + g_sprintf(tmp,"\twritable = %s\n",share->writable->str); + g_string_append(smb_conf_new,tmp); + } + if(share->guest_ok->len > 0) + { + g_sprintf(tmp,"\tguest ok = %s\n",share->guest_ok->str); + g_string_append(smb_conf_new,tmp); + } } //Write deleted share to smb.conf @@ -621,7 +706,7 @@ gint DeleteShare(GPtrArray *shared_items, const gchar *path) skip = FALSE; //Test special sections - if(g_strrstr(line,skip_item->name->str)) + if(strstr(line,skip_item->name->str)) { skip = TRUE; } diff --git a/sfshare-daemon/src/samba_share.h b/sfshare-daemon/src/samba_share.h index 2100565..95e7024 100644 --- a/sfshare-daemon/src/samba_share.h +++ b/sfshare-daemon/src/samba_share.h @@ -3,29 +3,6 @@ #include <glib.h> -/* -config path -load config -parse config -write share -delete share -reload service -*/ - - -// -// int WriteShare(); -// -// int DeleteShare(); -// -// void ReloadService(); -// -// -// std::string LoadConfig(); -// std::string ParseConfig(); -// -// std::string smb_conf_path; -// std::string smb_conf; typedef struct smb_conf_item { @@ -49,7 +26,7 @@ gint WriteShare(GPtrArray *shared_items, TSmbConfItem *share); //Function changes path to smb.conf to path -void ChangeSmbConfPath(const gchar *path); +void SetSmbConfPath(const gchar *path); //Function returns new array @@ -58,10 +35,19 @@ GPtrArray* SharedItemsArray_new(); //Function destroy Share Items Array void SharedItemsArray_free(GPtrArray *array); +//Function allocs memory for TSmbConfItem and sets parameters +TSmbConfItem* SmbConfItem_new(gchar *name, gchar *path, gchar *comment, gboolean read_only, gboolean guest_ok); -TSmbConfItem* SmbConfItem_new(gchar *name, gchar *path, gchar *comment, gchar *read_only, gchar *writable, gchar *guest_ok); +//Function allocs memory for TSmbConfItem TSmbConfItem* SmbConfItem_new0(); +//If ok return 0 else return number of error +gint CheckItem(TSmbConfItem *item); + +//Function free memory void SmbConfItem_free(TSmbConfItem *item); + +// Send SIGHUP to smb and nmb +void ReloadService(); #endif diff --git a/sfshare-daemon/src/sfshared.c b/sfshare-daemon/src/sfshared.c index 9a0cdc1..707822b 100644 --- a/sfshare-daemon/src/sfshared.c +++ b/sfshare-daemon/src/sfshared.c @@ -7,31 +7,33 @@ int main()
{ + //SetSmbConfPath("smb.conf"); + GPtrArray *shared_items = SharedItemsArray_new();
LoadSmbConf(shared_items); - DeleteShare(shared_items,"/test"); + //DeleteShare(shared_items,"/test/cesta"); 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); - + 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("testik", "/test", "Commentar", "no", "yes", "yes"); + test = SmbConfItem_new("TEST", "/absolutni/cesta", "Nefunguje - test sfshare comment", TRUE, TRUE); WriteShare(shared_items, test); SmbConfItem_free(test); SharedItemsArray_free(shared_items); + + ReloadService(); return 0;
}
|