summaryrefslogtreecommitdiffstats
path: root/sfshare-daemon/src/samba_share.c
blob: 076cdf70d36d7315b7489484318245d790b06473 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
#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"};

typedef enum keywords_id
{
    SHARE_NAME_ID = 0,
    PATH_ID,
    COMMENT_ID,
    READ_ONLY_ID,
    WRTITABLE_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 SetSmbConfPath(const gchar *path)
{
    //g_free(smb_conf_path);

    smb_conf_path = g_strdup(path);
}


/*
* Save string str as new smb.conf
*/
gint WriteSmbConf(const gchar *content)
{
    FILE *smb_file;

    //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;
    }

    //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;
    }

    //Write to smb.conf
    if(fputs(content,smb_file) == EOF)
    {
        g_error("Can not write to file \"%s\"!", smb_conf_path);
        return 1;
    }

    fclose(smb_file);

    return 0;
}


/*
* Function free TSmbConfItem from memmory
*/
void SmbConfItem_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* SmbConfItem_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* 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);
    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;
}

/*
* Function returns new array
*/
GPtrArray* SharedItemsArray_new()
{
    GPtrArray *ret = g_ptr_array_new();
    return ret;
}


/*
* Destroy array
*/
void SharedItemsArray_free(GPtrArray *array)
{
    TSmbConfItem *tmp;
    for(int i = 0; i < array->len; i++)
    {
        tmp = g_ptr_array_index(array,i);
        SmbConfItem_free(tmp);
    }

    g_ptr_array_free(array, TRUE);
}


/*
* 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 = 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->read_only,"no");
                else
                    g_string_assign(item->read_only,"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 LoadSmbConf(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_error("Config file \"%s\" not exist!", smb_conf_path);
        return 1;
    }

    //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;
    }

    //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
            for(int 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 = SmbConfItem_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_error("LoadSmbConf(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
        ParseShareItem(line, tmp);

        //g_print("%s\n", line);

    }

    //Free
    g_free(line);

    //SmbConfItem_free(&tmp_item);
    fclose(smb_file);

    return 0;
}


/*
*   Returns share imte if directory [path] is shared,
*   if not return NULL;
*/
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(strstr(tmp->path->str,path))
        {
            ret = tmp;
            break;
        }
    }

    return ret;
}


/*
* Write ne share section or change chare section defined by share parameter
*/
gint WriteShare(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 new_share = FALSE;
    gboolean change = FALSE;
    gboolean found = FALSE;


    item = IsShared(shared_items, share->path->str);

    //If item is not shared we just append it to end of smb.conf
    if(!item)
        new_share = TRUE;


    //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;
    }

    //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;
    }

    //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] == '[')
            {
                change = FALSE;

                //Is this section to change?
                if(strstr(line,item->name->str))
                {
                    change = TRUE;
                }
            }

        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 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;


                // path =
                case PATH_ID:
                {
                    //no change
                    g_string_append(smb_conf_new,orig);
                }
                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:
                {

                    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:
                {
                    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:
                {
                    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);
        }

        g_free(orig);
    }


    if(new_share)
    {
        g_sprintf(tmp,"\n[%s]\n",share->name->str);
        g_string_append(smb_conf_new,tmp);

        g_sprintf(tmp,"\tpath = %s\n",share->path->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);
        }


        if(share->read_only->len > 0)
        {
            g_sprintf(tmp,"\tread only = %s\n",share->read_only->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
    WriteSmbConf(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 DeleteShare(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 = IsShared(shared_items, path);

    if(!skip_item)
    {
        g_warning("Directory \"%s\" is NOT shared",path);
        return -1;
    }


    //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;
    }

    //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;
    }

    //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
    WriteSmbConf(smb_conf_new->str);

    fclose(smb_file);

    g_free(line);
    g_free(orig);

    g_string_free(smb_conf_new,TRUE);

    return 0;
}