home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / system-tools-backends-2.0 / scripts / Shares / SMB.pm < prev   
Encoding:
Perl POD Document  |  2006-08-14  |  7.3 KB  |  279 lines

  1. #-*- Mode: perl; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2. #
  3. # Copyright (C) 2000-2001 Ximian, Inc.
  4. #
  5. # Authors: Hans Petter Jansson <hpj@ximian.com>
  6. #
  7. # This program is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU Library General Public License as published
  9. # by the Free Software Foundation; either version 2 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # This program is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15. # GNU Library General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU Library General Public License
  18. # along with this program; if not, write to the Free Software
  19. # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
  20.  
  21. package Shares::SMB;
  22.  
  23. use Utils::File;
  24.  
  25. # --- share_export_smb_info; information on a particular SMB export --- #
  26.  
  27. sub gst_share_smb_info_set
  28. {
  29.   my ($info, $key, $value) = @_;
  30.   
  31.   if ($value eq "")
  32.   {
  33.     delete $info->{$key};
  34.   }
  35.   else
  36.   {
  37.     $info->{$key} = $value;
  38.   }
  39. }
  40.  
  41. sub gst_share_smb_info_get_name
  42. {
  43.   return $_[0]->{'name'};
  44. }
  45.  
  46. sub gst_share_smb_info_set_name
  47. {
  48.   &gst_share_smb_info_set ($_[0], 'name', $_[1]);
  49. }
  50.  
  51. sub gst_share_smb_info_get_point
  52. {
  53.   return $_[0]->{'point'};
  54. }
  55.  
  56. sub gst_share_smb_info_set_point
  57. {
  58.   &gst_share_smb_info_set ($_[0], 'point', $_[1]);
  59. }
  60.  
  61. sub gst_share_smb_info_get_comment
  62. {
  63.   return $_[0]->{'comment'};
  64. }
  65.  
  66. sub gst_share_smb_info_set_comment
  67. {
  68.   &gst_share_smb_info_set ($_[0], 'comment', $_[1]);
  69. }
  70.  
  71. sub gst_share_smb_info_get_enabled
  72. {
  73.   return $_[0]->{'enabled'};
  74. }
  75.  
  76. sub gst_share_smb_info_set_enabled
  77. {
  78.   &gst_share_smb_info_set ($_[0], 'enabled', $_[1]);
  79. }
  80.  
  81. sub gst_share_smb_info_get_browse
  82. {
  83.   return $_[0]->{'browse'};
  84. }
  85.  
  86. sub gst_share_smb_info_set_browse
  87. {
  88.   &gst_share_smb_info_set ($_[0], 'browse', $_[1]);
  89. }
  90.  
  91. sub gst_share_smb_info_get_public
  92. {
  93.   return $_[0]->{'public'};
  94. }
  95.  
  96. sub gst_share_smb_info_set_public
  97. {
  98.   &gst_share_smb_info_set ($_[0], 'public', $_[1]);
  99. }
  100.  
  101. sub gst_share_smb_info_get_write
  102. {
  103.   return $_[0]->{'write'};
  104. }
  105.  
  106. sub gst_share_smb_info_set_write
  107. {
  108.   &gst_share_smb_info_set ($_[0], 'write', $_[1]);
  109. }
  110.  
  111.  
  112. # --- share_smb_table; multiple instances of share_smb_info --- #
  113.  
  114. sub smb_table_find
  115. {
  116.   my ($name, $shares) = @_;
  117.  
  118.   foreach $i (@$shares)
  119.   {
  120.     return $i if ($$i[0] eq $name)
  121.   }
  122.  
  123.   return undef;
  124. }
  125.  
  126. sub get_distro_smb_file
  127. {
  128.   my ($smb_comb);
  129.  
  130.   my %dist_map =
  131.   (
  132.    "redhat-6.2"      => "redhat-6.2",
  133.    "redhat-7.0"      => "debian-3.0",
  134.    "redhat-7.1"      => "debian-3.0",
  135.    "redhat-7.2"      => "debian-3.0",
  136.    "redhat-7.3"      => "debian-3.0",
  137.    "redhat-8.0"      => "debian-3.0",
  138.    "mandrake-9.0"    => "debian-3.0",
  139.    "debian-3.0"      => "debian-3.0",
  140.    "suse-9.0"        => "debian-3.0",
  141.    "slackware-9.1.0" => "debian-3.0",
  142.    "gentoo"          => "debian-3.0",
  143.    "archlinux"       => "debian-3.0",
  144.    "pld-1.0"         => "pld-1.0",
  145.    "vine-3.0"        => "debian-3.0",
  146.    "freebsd-5"       => "freebsd-5",
  147.   );
  148.  
  149.   my %dist_tables =
  150.   (
  151.    "redhat-6.2" => "/etc/smb.conf",
  152.    "debian-3.0" => "/etc/samba/smb.conf",
  153.    "pld-1.0"    => "/etc/smb/smb.conf",
  154.    "freebsd-5"  => "/usr/local/etc/smb.conf",
  155.   );
  156.  
  157.   my $dist = $dist_map {$Utils::Backend::tool{"platform"}};
  158.   return $dist_tables{$dist} if $dist;
  159.   return undef;
  160. }
  161.  
  162. sub get_share_info
  163. {
  164.   my ($smb_conf_name, $section) = @_;
  165.   my @share;
  166.  
  167.   push @share, $section;
  168.   push @share, &Utils::Parse::get_from_ini      ($smb_conf_name, $section, "path");
  169.   push @share, &Utils::Parse::get_from_ini      ($smb_conf_name, $section, "comment");
  170.   push @share, &Utils::Parse::get_from_ini_bool ($smb_conf_name, $section, "available");
  171.   push @share, &Utils::Parse::get_from_ini_bool ($smb_conf_name, $section, "browsable") ||
  172.                &Utils::Parse::get_from_ini_bool ($smb_conf_name, $section, "browseable");
  173.   push @share, &Utils::Parse::get_from_ini_bool ($smb_conf_name, $section, "public")      ||
  174.                &Utils::Parse::get_from_ini_bool ($smb_conf_name, $section, "guest");
  175.   push @share, &Utils::Parse::get_from_ini_bool ($smb_conf_name, $section, "writable")    ||
  176.                &Utils::Parse::get_from_ini_bool ($smb_conf_name, $section, "writeable");
  177.  
  178.   return \@share;
  179. }
  180.  
  181. sub set_share_info
  182. {
  183.   my ($smb_conf_file, $share) = @_;
  184.   my ($section);
  185.  
  186.   $section = shift (@$share);
  187.  
  188.   &Utils::Replace::set_ini        ($smb_conf_file, $section, "path",      shift (@$share));
  189.   &Utils::Replace::set_ini        ($smb_conf_file, $section, "comment",   shift (@$share));
  190.   &Utils::Replace::set_ini_bool   ($smb_conf_file, $section, "available", shift (@$share));
  191.   &Utils::Replace::set_ini_bool   ($smb_conf_file, $section, "browsable", shift (@$share));
  192.   &Utils::Replace::set_ini_bool   ($smb_conf_file, $section, "public",    shift (@$share));
  193.   &Utils::Replace::set_ini_bool   ($smb_conf_file, $section, "writable",  shift (@$share));
  194.  
  195.   &Utils::Replace::remove_ini_var ($smb_conf_file, $section, "browseable");
  196.   &Utils::Replace::remove_ini_var ($smb_conf_file, $section, "guest");
  197.   &Utils::Replace::remove_ini_var ($smb_conf_file, $section, "writeable");
  198. }
  199.  
  200. sub get_shares
  201. {
  202.   my ($smb_conf_file) = @_;
  203.   my (@sections, @table, $share);
  204.  
  205.   # Get the sections.
  206.   @sections = &Utils::Parse::get_ini_sections ($smb_conf_file);
  207.  
  208.   for $section (@sections)
  209.   {
  210.     next if ($section =~ /^(global)|(homes)|(printers)|(print\$)$/);
  211.     next if (&Utils::Parse::get_from_ini_bool ($smb_conf_file, $section, "printable"));
  212.  
  213.     $share = &get_share_info ($smb_conf_file, $section);
  214.     push @table, $share;
  215.   }
  216.  
  217.   return \@table;
  218. }
  219.  
  220. sub get
  221. {
  222.   my ($smb_conf_file);
  223.   my ($shares, $workgroup, $desc, $wins, $winsserver);
  224.  
  225.   $smb_conf_file = &get_distro_smb_file;
  226.   $shares = &get_shares ($smb_conf_file);
  227.   
  228.   $workgroup = &Utils::Parse::get_from_ini ($smb_conf_file, "global", "workgroup");
  229.   $smbdesc = &Utils::Parse::get_from_ini ($smb_conf_file, "global", "server string");
  230.   $wins = &Utils::Parse::get_from_ini_bool ($smb_conf_file, "global", "wins support");
  231.   $winsserver = &Utils::Parse::get_from_ini ($smb_conf_file, "global", "wins server");
  232.  
  233.   return ($shares, $workgroup, $smbdesc, $wins, $winsserver);
  234. }
  235.  
  236. sub set_shares
  237. {
  238.   my ($smb_conf_file, $shares) = @_;
  239.   my (@sections, $section, $share);
  240.  
  241.   # Get the sections.
  242.   @sections = &Utils::Parse::get_ini_sections ($smb_conf_file);
  243.  
  244.   # remove deleted sections
  245.   foreach $section (@sections)
  246.   {
  247.     next if ($section =~ /^(global)|(homes)|(printers)|(print\$)$/);
  248.     next if (&Utils::Parse::get_from_ini_bool ($smb_conf_file, $section, "printable"));
  249.  
  250.     if (!&smb_table_find ($section, $shares))
  251.     {
  252.       Utils::Replace::remove_ini_section ($smb_conf_file, $section);
  253.     }
  254.   }
  255.  
  256.   for $share (@$shares)
  257.   {
  258.     &set_share_info ($smb_conf_file, $share);
  259.   }
  260. }
  261.  
  262. sub set
  263. {
  264.   my ($shares, $workgroup, $desc, $wins, $winsserver) = @_;
  265.   my ($smb_conf_file);
  266.   my (@sections, $export);
  267.  
  268.   $smb_conf_file = &get_distro_smb_file;
  269.  
  270.   &set_shares ($smb_conf_file, $shares);
  271.  
  272.   &Utils::Replace::set_ini ($smb_conf_file, "global", "workgroup", $workgroup);
  273.   &Utils::Replace::set_ini ($smb_conf_file, "global", "server string", $desc);
  274.   &Utils::Replace::set_ini_bool ($smb_conf_file, "global", "wins support", $wins);
  275.   &Utils::Replace::set_ini ($smb_conf_file, "global", "wins server", ($wins) ? "" : $winsserver);
  276. }
  277.  
  278. 1;
  279.