summaryrefslogtreecommitdiff
path: root/share.pl.in
blob: 55a83a63d35e79af821c1ac5d601f9c364a70f03 (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
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
#!/usr/bin/env perl
#-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-

# Common functions for exporting network shares (NFS or SMB).
#
# Copyright (C) 2000-2001 Ximian, Inc.
#
# Authors: Hans Petter Jansson <hpj@ximian.com>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as published
# by the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.


$SCRIPTSDIR = "___scriptsdir___";
if ($SCRIPTSDIR =~ /^___scriptsdir__[_]/)
{
    $SCRIPTSDIR = ".";
    $DOTIN = ".in";
}

require "$SCRIPTSDIR/general.pl$DOTIN";
require "$SCRIPTSDIR/file.pl$DOTIN";
require "$SCRIPTSDIR/parse.pl$DOTIN";
require "$SCRIPTSDIR/replace.pl$DOTIN";
require "$SCRIPTSDIR/network.pl$DOTIN";


# --- share_export_smb_info; information on a particular SMB export --- #

sub xst_share_smb_info_new
{
  my $info = {};
  return $info;
}

sub xst_share_smb_info_set
{
  my ($info, $key, $value) = @_;
  
  if ($value eq "")
  {
    delete $info->{$key};
  }
  else
  {
    $info->{$key} = $value;
  }
}

sub xst_share_smb_info_get_name
{
  return $_[0]->{'name'};
}

sub xst_share_smb_info_set_name
{
  &xst_share_smb_info_set ($_[0], 'name', $_[1]);
}

sub xst_share_smb_info_get_point
{
  return $_[0]->{'point'};
}

sub xst_share_smb_info_set_point
{
  &xst_share_smb_info_set ($_[0], 'point', $_[1]);
}

sub xst_share_smb_info_get_comment
{
  return $_[0]->{'comment'};
}

sub xst_share_smb_info_set_comment
{
  &xst_share_smb_info_set ($_[0], 'comment', $_[1]);
}

sub xst_share_smb_info_get_enabled
{
  return $_[0]->{'enabled'};
}

sub xst_share_smb_info_set_enabled
{
  &xst_share_smb_info_set ($_[0], 'enabled', $_[1]);
}

sub xst_share_smb_info_get_browse
{
  return $_[0]->{'browse'};
}

sub xst_share_smb_info_set_browse
{
  &xst_share_smb_info_set ($_[0], 'browse', $_[1]);
}

sub xst_share_smb_info_get_public
{
  return $_[0]->{'public'};
}

sub xst_share_smb_info_set_public
{
  &xst_share_smb_info_set ($_[0], 'public', $_[1]);
}

sub xst_share_smb_info_get_write
{
  return $_[0]->{'write'};
}

sub xst_share_smb_info_set_write
{
  &xst_share_smb_info_set ($_[0], 'write', $_[1]);
}

# --- share_smb_table; multiple instances of share_smb_info --- #

sub xst_share_smb_table_new
{
  my @array;
  return \@array;
}

sub xst_share_smb_table_add
{
  my ($table, $info) = @_;
  push @$table, $info;
}

sub xst_share_smb_table_find
{
  my ($table, $name) = @_;

  for $i (@$table)
  {
    if (&xst_share_smb_info_get_name ($i) eq $name)
    {
      return $i;
    }
  }

  return undef;
}

# --- share_export_nfs_info; information on a particular NFS export --- #

sub xst_share_nfs_info_new
{
  my $info = {};
  return $info;
}

sub xst_share_nfs_info_dup
{
  my ($orig) = @_;
  my $dup;

  $dup = { %$orig };
  &xst_share_nfs_info_set_client_table ($dup, &xst_share_nfs_client_table_dup (&xst_share_nfs_info_get_client_table ($orig)));
  return $dup;
}

sub xst_share_nfs_info_match_data
{
  my ($info_a, $info_b) = @_;

  if (&xst_share_nfs_info_get_point ($info_a) eq &xst_share_nfs_info_get_point ($info_b) &&
      &xst_share_nfs_info_print_clients ($info_a) eq &xst_share_nfs_info_print_clients ($info_b))
  {
    return 1;
  }

  return 0;
}

sub xst_share_nfs_info_set
{
  my ($info, $key, $value) = @_;
  
  if ($value eq "")
  {
    delete $info->{$key};
  }
  else
  {
    $info->{$key} = $value;
  }
}

sub xst_share_nfs_info_get_point
{
  return $_[0]->{'point'};
}

sub xst_share_nfs_info_set_point
{
  &xst_share_nfs_info_set ($_[0], 'point', $_[1]);
}

sub xst_share_nfs_info_get_client_table
{
  return $_[0]->{'clients'};
}

sub xst_share_nfs_info_set_client_table
{
  &xst_share_nfs_info_set ($_[0], 'clients', $_[1]);
}

# --- share_nfs_table; multiple instances of share_smb_info --- #

sub xst_share_nfs_table_new
{
  my @array;
  return \@array;
}

sub xst_share_nfs_table_add
{
  my ($table, $info) = @_;
  push @$table, $info;
}

sub xst_share_nfs_table_dup
{
  my ($orig) = @_;
  my $dup = &xst_share_nfs_table_new ();
  my $i;

  foreach $i (@$orig)
  {
    &xst_share_nfs_table_add ($dup, &xst_share_nfs_info_dup ($i));
  }

  return $dup;
}

sub xst_share_nfs_table_find
{
  my ($table, $point) = @_;
  my $i;

  for $i (@$table)
  {
    if (&xst_share_nfs_info_get_point ($i) eq $point)
    {
      return $i;
    }
  }

  return undef;
}

sub xst_share_nfs_table_find_info_equivalent
{
  my ($table, $info) = @_;

  return &xst_share_nfs_table_find ($table, &xst_share_nfs_info_get_point ($info));
}

# --- share_export_nfs_client_info; information on a particular NFS export's client --- #

sub xst_share_nfs_client_info_new
{
  my $info = {};
  return $info;
}

sub xst_share_nfs_client_info_dup
{
  my ($orig) = @_;
  my $dup;

  $dup = { %$orig };
  return $dup;
}

sub xst_share_nfs_client_info_set
{
  my ($info, $key, $value) = @_;
  
  if ($value eq "")
  {
    delete $info->{$key};
  }
  else
  {
    $info->{$key} = $value;
  }
}

sub xst_share_nfs_client_info_get_pattern
{
  return $_[0]->{'pattern'};
}

sub xst_share_nfs_client_info_set_pattern
{
  &xst_share_nfs_client_info_set ($_[0], 'pattern', $_[1]);
}

sub xst_share_nfs_client_info_get_write
{
  return $_[0]->{'write'};
}

sub xst_share_nfs_client_info_set_write
{
  &xst_share_nfs_client_info_set ($_[0], 'write', $_[1]);
}

# --- share_nfs_client_table; multiple instances of share_smb_client_info --- #

sub xst_share_nfs_client_table_new
{
  my @array;
  return \@array;
}

sub xst_share_nfs_client_table_dup
{
  my ($orig) = @_;
  my $dup = &xst_share_nfs_client_table_new ();
  my $i;

  foreach $i (@$orig)
  {
    &xst_share_nfs_client_table_add ($dup, &xst_share_nfs_client_info_dup ($i));
  }

  return $dup;
}

sub xst_share_nfs_client_table_add
{
  my ($table, $info) = @_;
  push @$table, $info;
}

# --- Parsing --- #

sub xst_share_parse_smb_conf
{
  my ($smb_conf_name) = @_;
  my (@sections, $table);

  $table = xst_share_smb_table_new ();

  # Get the sections.

  @sections = &xst_parse_ini_sections ($smb_conf_name);

  for $section (@sections)
  {
    next if ($section =~ /^(global)|(homes)|(printers)$/);
    next if (&xst_parse_ini_bool ($smb_conf_name, $section, "printable"));

    my $sesi = &xst_share_smb_info_new ();
    my $point, $comment, $enabled, $browseable, $public, $writable, $printable;

    $point      = &xst_parse_ini      ($smb_conf_name, $section, "path");
    $comment    = &xst_parse_ini      ($smb_conf_name, $section, "comment");
    $enabled    = &xst_parse_ini_bool ($smb_conf_name, $section, "available");
    $browseable = &xst_parse_ini_bool ($smb_conf_name, $section, "browsable")   ||
                  &xst_parse_ini_bool ($smb_conf_name, $section, "browseable");
    $public     = &xst_parse_ini_bool ($smb_conf_name, $section, "public")      ||
                  &xst_parse_ini_bool ($smb_conf_name, $section, "guest");
    $writable   = &xst_parse_ini_bool ($smb_conf_name, $section, "writable")    ||
                  &xst_parse_ini_bool ($smb_conf_name, $section, "writeable");

    &xst_share_smb_info_set_name    ($sesi, $section);
    &xst_share_smb_info_set_point   ($sesi, $point);
    &xst_share_smb_info_set_comment ($sesi, $comment);
    &xst_share_smb_info_set_enabled ($sesi, $enabled);
    &xst_share_smb_info_set_browse  ($sesi, $browseable);
    &xst_share_smb_info_set_public  ($sesi, $public);
    &xst_share_smb_info_set_write   ($sesi, $writable);

    &xst_share_smb_table_add ($table, $sesi);
  }

  return $table;
}

sub xst_share_parse_nfs_exports
{
  my ($nfs_exports_name) = @_;
  my (@sections, $table, $entries);
  my $point;

  $table = xst_share_nfs_table_new ();

  $entries = &xst_parse_split_hash_with_continuation ($nfs_exports_name, "[ \t]+", "[ \t]+");

  foreach $point (keys %$entries)
  {
    my $clients = $$entries{$point};
    my $info = &xst_share_nfs_info_new ();
    my $client_table = &xst_share_nfs_client_table_new ();

    &xst_share_nfs_info_set_point ($info, $point);

    foreach $client (@$clients)
    {
      my $cinfo = &xst_share_nfs_client_info_new ();
      my $pattern;

      $client =~ /^([a-zA-Z0-9.-_*?@\/]+)/;
      $pattern = $1;
      $pattern = "0.0.0.0/0" if $pattern eq "";
      &xst_share_nfs_client_info_set_pattern ($cinfo, $pattern);

      my $option_str = "";
      my @options;

      if ($client =~ /\((.+)\)/) { $option_str = $1; }
      @options = ($option_str =~ /([a-zA-Z0-9_=-]+),?/mg);

      for $option (@options)
      {
        if ($option eq "rw") { &xst_share_nfs_client_info_set_write ($cinfo, 1); }
        # Add supported NFS export options here. Some might have to be split on '='.
      }

      &xst_share_nfs_client_table_add ($client_table, $cinfo);
    }

    &xst_share_nfs_info_set_client_table ($info, $client_table);
    &xst_share_nfs_table_add ($table, $info);
  }

  return $table;
}

# --- Replacing --- #

sub xst_share_replace_smb_conf
{
  my ($file, $table) = @_;
  my (@sections);

  # Get the sections.

  @sections = &xst_parse_ini_sections ($file);

  for $section (@sections)
  {
    next if ($section =~ /^(global)|(homes)|(printers)$/);
    next if (&xst_parse_ini_bool ($file, $section, "printable"));

    if (!&xst_share_smb_table_find ($table, $section))
    {
      &xst_replace_remove_ini_section ($file, $section);
    }
  }

  for $export (@$table)
  {
    my $point, $comment, $enabled, $browseable, $public, $writable, $printable;
    my $name = &xst_share_smb_info_get_name ($export);

    &xst_replace_ini      ($file, $name, "path",       &xst_share_smb_info_get_point      ($export));
    &xst_replace_ini      ($file, $name, "comment",    &xst_share_smb_info_get_comment    ($export));
    &xst_replace_ini_bool ($file, $name, "available",  &xst_share_smb_info_get_enabled    ($export));
    &xst_replace_ini_bool ($file, $name, "browseable", &xst_share_smb_info_get_browse     ($export));
    &xst_replace_ini_bool ($file, $name, "public",     &xst_share_smb_info_get_public     ($export));
    &xst_replace_ini_bool ($file, $name, "writable",   &xst_share_smb_info_get_write      ($export));

    &xst_replace_remove_ini_var ($file, $name, "browsable");
    &xst_replace_remove_ini_var ($file, $name, "guest");
    &xst_replace_remove_ini_var ($file, $name, "writeable");
  }
}

sub xst_share_nfs_exports_get_next_entry_line  # $infd, $outfd
{
  my ($infd, $outfd) = @_;

  while (<$infd>)
  {
    # Each line is in the following format:
    # <point> <clients>
    my @line = split /[ \t]+/, $_;
    if ($line[0] eq "") { shift @line; }
    if (@line < 1 || &xst_ignore_line (@line)) { print $outfd $_; next; }

    return $_;
  }

  return undef;
}

sub xst_share_nfs_exports_get_entry_line_fields  # line
{
  my ($line) = @_;

  # Remove leading spaces.
  $line =~ s/^[ \t]*//;

  # Remove trailing spaces and comments.
  $line =~ s/[ \t]*\#.*//;

  return split /[ \t]+/, $line;
}

sub xst_share_nfs_info_print_clients
{
  my ($info) = @_;
  my $line = "";
  my $clients, $client;

  $clients = &xst_share_nfs_info_get_client_table ($info);

  for $client (@$clients)
  {
    $line .= &xst_share_nfs_client_info_get_pattern ($client);
    $line .= "(rw)" if (&xst_share_nfs_client_info_get_write ($client));
    $line .= " ";
  }

  return $line;
}

sub xst_share_nfs_info_print_entry
{
  my ($info) = @_;
  my $line;

  # <point>

  $line = sprintf ("%-15s ", &xst_share_nfs_info_get_point ($info));

  # <clients>

  $line .= &xst_share_nfs_info_print_clients ($info);

  return $line;
}

sub xst_share_nfs_client_print_option_hash
{
  my ($opthash) = @_;
  my $string = "";

  for $key (keys %$opthash)
  {
    my $value = $$opthash{$key};

    $string .= "," if $string ne "";
    $string .= $key;

    if ($value eq " ")
    {
      $string .= "=";
    }
    elsif ($value ne "")
    {
      $string .= "=" . $value;
    }
  }

  return $string;
}

sub xst_share_nfs_info_print_synthesized_entry
{
  my ($info, $line) = @_;
  my $outline;
  my $ctable = &xst_share_nfs_info_get_client_table ($info);

  # <point>

  $outline = sprintf ("%-15s", &xst_share_nfs_info_get_point ($info));

  # <clients>

  chomp $line;
  my @clients = split /[ \t]+/, $line;
  shift @clients;

  # Make client hash based on line.

  my $chash = { };

  for $client (@clients)
  {
    my $opthash = { };

    $client =~ /^([a-zA-Z0-9.-_*?@\/]+)/;
    my $pattern = $1;
    $$chash{$pattern} = $opthash;

    my $option_str = "";
    if ($client =~ /\((.+)\)/) { $option_str = $1; }
    @options = ($option_str =~ /([a-zA-Z0-9_=-]+),?/mg);

    for $option (@options)
    {
      my ($key, $value) = split /[=]/, $option;
      next if ($key eq "");

      if ($value eq "" && $option =~ /=/) { $value = " "; }
      $$opthash{$key} = $value;
    }
  }

  # @clients contains client(options) entries.

  for $cinfo (@$ctable)
  {
    my $pattern = &xst_share_nfs_client_info_get_pattern ($cinfo);
    my $opthash = $$chash{$pattern};

    if (&xst_share_nfs_client_info_get_write ($cinfo))
    {
      $$opthash{'rw'} = "";
    }
    else
    {
      delete $$opthash{'rw'};
    }

    $outline .= " " . &xst_share_nfs_client_info_get_pattern ($cinfo);
    my $client_string = &xst_share_nfs_client_print_option_hash ($opthash);
    if ($client_string ne "")
    {
      $outline .= "(" . $client_string . ")";
    }
  }

  return $outline;
}

sub xst_share_nfs_exports_add_entry
{
  my ($file, $info) = @_;
  my ($infd, $outfd);
  my ($line);

  ($infd, $outfd) = &xst_file_open_filter_write_from_names ($file);
  return undef if !$outfd;

  while (<$infd>) { print $outfd $_; }
  &xst_file_close ($infd);

  print $outfd &xst_share_nfs_info_print_entry ($info) . "\n";
  &xst_file_close ($outfd);
}

sub xst_share_nfs_exports_update_entry  # filename, filesys_info
{
  my ($file, $info) = @_;
  my ($infd, $outfd);
  my ($line);
  my $replaced = 0;

  ($infd, $outfd) = &xst_file_open_filter_write_from_names ($file);
  return undef if !$outfd;

  while ($line = &xst_share_nfs_exports_get_next_entry_line ($infd, $outfd))
  {
    my $point = &xst_share_nfs_exports_get_entry_line_point ($line);

    if (!$replaced && $point eq &xst_share_nfs_info_get_point ($info))
    {
      print $outfd &xst_share_nfs_info_print_synthesized_entry ($info, $line) . "\n";
      $replaced = 1;
    }
    else
    {
      print $outfd $line;
    }
  }

  &xst_file_close ($infd);
  &xst_file_close ($outfd);
}

sub xst_share_nfs_exports_remove_entry
{
  my ($file, $info) = @_;
  my ($infd, $outfd);
  my ($line);

  ($infd, $outfd) = &xst_file_open_filter_write_from_names ($file);
  return undef if !$outfd;

  while ($line = &xst_share_nfs_exports_get_next_entry_line ($infd, $outfd))
  {
    my $point = &xst_share_nfs_exports_get_entry_line_point ($line);

    if ($point ne &xst_share_nfs_info_get_point ($info))
    {
      print $outfd $line;
    }
  }

  &xst_file_close ($infd);
  &xst_file_close ($outfd);
}

sub xst_share_nfs_exports_get_entry_line_point
{
  my ($line) = @_;
  my $point;

  ($point) = split /[ \t]+/, $line;
  return $point;
}

sub xst_share_replace_nfs_exports  # filename, table
{
  my ($file, $table) = @_;
  my ($new_table, $old_table);

  $old_table = &xst_share_parse_nfs_exports ($file);
  $new_table = &xst_share_nfs_table_dup ($table);

  for $info (@$new_table)
  {
    my $old_info = &xst_share_nfs_table_find_info_equivalent ($old_table, $info);

#    print "Looking for entry - ";
    if (!$old_info)
    {
#      print "adding.\n";
      &xst_share_nfs_exports_add_entry ($file, $info);
    }
    elsif ($old_info && !&xst_share_nfs_info_match_data ($old_info, $info))
    {
#      print "updating.\n";
      &xst_share_nfs_exports_update_entry ($file, $info);
    }
    else
    {
#      print "found.\n";
    }
  }

  for $old_info (@$old_table)
  {
#    print "Looking for entry - ";
    if (!&xst_share_nfs_table_find_info_equivalent ($new_table, $old_info))
    {
#      print "removing.\n";
      &xst_share_nfs_exports_remove_entry ($file, $old_info);
    }
    else
    {
#      print "found.\n";
    }
  } 
}


sub xst_share_get_smbclient_list_cmd
{
  my ($host) = @_;
  my ($tool_smbclient);

  $tool_smbclient = &xst_file_locate_tool ("smbclient");

  return "$tool_smbclient -N -U % -L -I $host";
}


sub xst_share_scan_smb_host_call
{
  my ($host) = @_;
  my ($cmd, $dirs);
  local *SMBCLIENT;

  $cmd = &xst_share_get_smbclient_list_cmd ($host);
  open (SMBCLIENT, "/bin/sh -c \"$cmd\" 2> /dev/null |");

  while (<SMBCLIENT>)
  {
    last if /^[ \t]+Sharename[ \t]+Type[ \t]+Comment/;
  }
  
  while (<SMBCLIENT>)
  {
    chomp;
    last if /^$/;
    # Looking for sth like " 	fredsdir       Disk      Fred's Service"
    if (/^[ \t]*([^ \t]+)[ \t]+Disk/)
    {
      $dirs .= ":" . $1;
    }
  }
  close SMBCLIENT;

  return "$host:$dirs";
}


sub xst_share_scan_smb_start
{
  my (@hosts) = @_;
  my ($host, @procs);

  foreach $host (@hosts)
  {
    push @procs, &xst_process_fork (\&xst_share_scan_smb_host_call, $host);
  }

  return \@procs;
}


sub xst_share_scan_nfs_host_call
{
  my ($host) = @_;
  my ($cmd, $dirs);
  local *SHOWMOUNT;

  $cmd = &xst_share_get_showmount_cmd ($host);
  open (SHOWMOUNT, "/bin/sh -c \"$cmd\" 2> /dev/null |");
  while (<SHOWMOUNT>)
  {
    chomp;
    $dirs .= ":$_";
  }
  close SHOWMOUNT;

  return "$host$dirs";
}


sub xst_share_scan_nfs_start
{
  my (@hosts) = @_;
  my ($host, @procs);

  foreach $host (@hosts)
  {
    push @procs, &xst_process_fork (\&xst_share_scan_nfs_host_call, $host);
  }

  return \@procs;
}


sub xst_share_scan_share_collect
{
  my ($procs, $host2name) = @_;
  my ($proc, $str, %hash, $host, @dirs);

  foreach $proc (@$procs)
  {
    if ($$proc{"ready"})
    {
      sysread ($$proc{"fd"}, $str, 4096);
      @dirs = split (':', $str);
      $host = shift @dirs;
      $hash{$$host2name{$host}} = [ @dirs ];
    }
    &xst_process_kill ($proc);
  }

  return \%hash;
}


sub xst_share_scan_network
{
  my ($host, @hosts);
  my ($proc, @procs, $lookup_procs, $nfs_procs, $smb_procs);
  my ($i, @names, %host2name, $nfs_table, $smb_table);

  @hosts = &xst_network_find_hosts ();
  
  $lookup_procs = &xst_network_lookup_address_start (@hosts);
  $nfs_procs = &xst_share_scan_nfs_start (@hosts);
  $smb_procs = &xst_share_scan_smb_start (@hosts);

  &xst_process_list_check_ready (10, [@$lookup_procs, @$nfs_procs, @$smb_procs]);

  @names = &xst_network_lookup_address_collect ($lookup_procs);
  # http://tlc.perlarchive.com/articles/perl/ug0001.shtml
  @host2name{@hosts} = @names;

  return &xst_share_scan_collect ([@$nfs_procs, @$smb_procs], \%host2name);
}

1;