#!/usr/bin/perl # Drakwizard # Copyright (C) 2005 Mandriva # # Authors: antoine Ginies # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2, 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 General Public License for more details. # # You should have received a copy of the GNU 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. package MDK::Wizard::Sambaprint; use strict; use common; use services; use MDK::Wizard::Wizcommon; use Libconf::Templates; use Libconf::Glueconf::Samba::Smb_conf; my $wiz = new MDK::Wizard::Wizcommon; my $in = interactive->vnew; my $wiz_samba_etc = "/etc/sysconfig/wizard_samba"; my @listshare; my @listprinters; my $o = { name => 'Samba printers wizard', var => { wiz_all_printers => '', wiz_printers => '', wiz_do_printer_sharing => '', wiz_printers_comment => '', wiz_printers_browseable => '', wiz_printers_guestok => '', wiz_printers_createmode => '', list_printers => '', }, defaultimage => "/usr/share/wizards/samba_wizard/images/IC-sambaprt-48.png" }; $::Wizard_pix_up = "/usr/share/wizards/samba_wizard/images/IC-sambaprt-48.png"; # we ask glueconf to give us the structure representing /etc/samba/smb.conf my $samba = new Libconf::Glueconf::Samba::Smb_conf({ filename => '/etc/samba/smb.conf'}); #debug use Data::Dumper; print Dumper($samba); my %cprint = ( 1 => N("Printers - configure your printers"), # 2 => N('Print - printers drivers'), ); my @yesorno = qw(yes no); push @yesorno, ""; $o->{pages} = { welcome => { name => N("You can enable or disable printers in your Samba server configuration."), no_back => 1, pre => sub { if (! -f $wiz_samba_etc) { $in->ask_warn(N("Information"), N("It seems that you haven't setup a Samba server. Please setup a Samba server with Samba wizard before manage your shares.")) or $in->exit; } $o->{var}{wiz_do_printer_sharing} = "0"; }, data => [ # { label => "", val => \$o->{var}{wiz_todo}, list => [ keys %cprint ], format => sub { $cprint{$_[0]} } }, { text => N("Enable printers in Samba?"), type => 'bool', val => \$o->{var}{wiz_do_printer_sharing} }, ], post => sub { if ($o->{var}{wiz_do_printer_sharing} == 0) { return 'summary_disable'; } else { return 'ask_printers' } }, next => 'ask_printers', }, ask_printers => { name => N("Printers are available.") . "\n\n" . N("Select which printers you want to be accessible from known users"), pre => sub { $o->{var}{list_printers} ||= [ list_printers() ]; $o->{var}{wiz_box_list} ||= []; }, data => [ { text => N("Enable all printers"), type => 'bool', val => \$o->{var}{wiz_all_printers} }, { val => \$o->{var}{wiz_box_list}, boolean_list => \$o->{var}{list_printers}, disabled => sub { $o->{var}{wiz_all_printers} and return 1; 0 } }, ], next => 'ask_printers_options', }, ask_printers_options => { name => N("Now you can configure your printers service. Change value only if you know what your are doing."), pre => sub { if ($samba->{printers}{comment}) { $o->{var}{wiz_printers_comment} ||= $samba->{printers}{comment}; } else { $o->{var}{wiz_printers_comment} = "All Printers"; } $o->{var}{wiz_printers_browseable} ||= $samba->{printers}{browseable} or $o->{var}{wiz_printers_browseable} = "no"; $o->{var}{wiz_printers_guestok} ||= $samba->{printers}{'guest ok'} or $o->{var}{wiz_printers_guestok} = "yes"; if ($samba->{printers}{'create mode'}) { $o->{var}{wiz_printers_createmode} ||= $samba->{printers}{'create mode'}; } else { $o->{var}{wiz_printers_createmode} = "0700"; } }, post => sub { if ($o->{var}{wiz_do_homes} == 1) { return 'ask_homes'; } }, data => [ { label => N("Comment:"), val => \$o->{var}{wiz_printers_comment} }, # { label => N("Create mode:"), val => \$o->{var}{wiz_printers_createmode} }, { label => N("Browseable:"), val => \$o->{var}{wiz_printers_browseable}, fixed_list => \@yesorno }, { label => N("Guest ok:"), val => \$o->{var}{wiz_printers_guestok}, fixed_list => \@yesorno }, ], next => 'summary', }, summary => { name => N("Configuring Samba printers"), pre => sub { $o->{var}{wiz_sambatype} = $cprint{$o->{var}{wiz_type}}; }, data => [ { label => N("Comment:"), val_ref => \$o->{var}{wiz_printers_comment} }, { label => N("Create mode:"), val_ref => \$o->{var}{wiz_printers_createmode} }, { label => N("Browseable:"), val_ref => \$o->{var}{wiz_printers_browseable} }, { label => N("Guest ok:"), val_ref => \$o->{var}{wiz_printers_guestok} }, ], post => \&do_it, next => 'end', }, summary_disable => { name => N("Disable Samba printers"), post => \&do_it, next => 'end', no_back => 1, }, end => { name => N("Congratulations") . "\n\n" . N("The wizard successfully configured your Samba printer."), no_back => 1, end => 1, next => 0 }, error_end => { name => N("Failed"), data => [ { label => N("Please relaunch drakwizard, and try to change some parameters.") } ], no_back => 1, end => 1, next => 0, }, }; sub new { my ($class, $conf) = @_; bless $o, $class; } sub check() { $> and return 1; $wiz->{net}->is_dhcp and return 2; 0; } #section has the name of the printer sub add_printer { my ($printer) = @_; $samba->{$printer}{printer} = $printer; $samba->{$printer}{printable} = 'yes'; } sub remove_printer { my ($printer) = @_; delete $samba->{$printer}; } sub list_printers_smbconf() { undef @listprinters; foreach my $clef (keys %$samba) { if ($samba->{$clef}{printable} =~ /yes/i) { print "$clef is a printer\n"; push @listprinters, $clef; } } return @listprinters; } sub list_printers() { my @list if 0; return @list if @list; @list = sort grep /^(?!\#).+/, map { my ($printer) = split(':', $_); } cat_("/etc/printcap"); if (@list) { @list; } else { () } } sub printer_sharing() { # create default section for printers with default value $samba->{global}{'printer adm'} = "\@adm"; $samba->{printers}; $samba->{printers}{comment} = $o->{var}{wiz_printers_comment}; $samba->{printers}{browseable} = $o->{var}{wiz_printers_browseable}; $samba->{printers}{'guest ok'} = $o->{var}{wiz_printers_guestok}; $samba->{printers}{'create mode'} = $o->{var}{wiz_printers_createmode}; # dont want user to change those value... $samba->{printers}{path} = "/var/spool/samba"; $samba->{printers}{writable} = "no"; $samba->{printers}{printable} = "yes"; $samba->{printers}{'print command'} = "lpr-cups -P \%p -o raw \%s -r"; $samba->{printers}{'use client driver'} = "yes"; if ($o->{var}{wiz_box_list}) { for (my $i; $i < @{$o->{var}{wiz_box_list}}; $i++) { my $printer = $o->{var}{list_printers}[$i]; if ($o->{var}{wiz_box_list}[$i]) { add_printer($printer); } else { remove_printer($printer); } } } } sub printer_section() { $samba->{'pdf-gen'}; $samba->{'pdf-gen'}{path} = "/var/tmp"; $samba->{'pdf-gen'}{'guest ok'} = "no"; $samba->{'pdf-gen'}{printable} = "yes"; $samba->{'pdf-gen'}{comment} = "PDF Generator (only valid users)"; $samba->{'pdf-gen'}{printing} = "bsd"; $samba->{'pdf-gen'}{'print command'} = "/usr/share/samba/scripts/print-pdf \"%s\" \"%H\" \"//%L/%u\" \"%m\" \"%I\" \"%J\" &"; $samba->{'pdf-gen'}{'lpq command'} = "/bin/true"; } sub printdollar_section() { $samba->{'print$'}; $samba->{'print$'}{browseable} = "yes"; $samba->{'print$'}{path} = "/var/lib/samba/printers"; $samba->{'print$'}{browseable} = "yes"; $samba->{'print$'}{'write list'} = "\@adm root"; $samba->{'print$'}{'guest ok'} = "yes"; $samba->{'print$'}{'inherit permissions'} = "yes"; } sub get_printers() { if ($o->{var}{wiz_do_printer_sharing}) { my $string; $o->{var}{wiz_all_printers} and return "all printers"; for (my $i; $i < @{$o->{var}{wiz_box_list}}; $i++) { $string .= "$o->{var}{list_printers}[$i]\n" if $o->{var}{wiz_box_list}[$i]; } $string; } else { 'disabled'; } } sub write_conf_restart_smb { $samba->write_conf("/etc/samba/smb.conf"); if (services::is_service_running('smb')) { services::restart('smb'); } else { services::start('smb') } } # remember one variable cannot be commented and not in the same file. sub do_it { $::testing and return; # display a wait dialog box my $in = 'interactive'->vnew('su', 'Samba'); my $w = $in->wait_message(N("Samba server"), N("Configuring your Samba server...")); # share printers if ($o->{var}{wiz_do_printer_sharing} == 1) { printer_sharing(); printer_section(); printdollar_section(); } else { if ($o->{var}{wiz_box_list}) { for (my $i; $i < @{$o->{var}{wiz_box_list}}; $i++) { $o->{var}{list_printers}[$i] and remove_printer($o->{var}{list_printers}[$i]); } } $samba->{global}{'printer adm'} and delete $samba->{global}{'printer adm'}; $samba->{printers} and delete $samba->{printers}; $samba->{'print$'} and delete $samba->{'print$'}; $samba->{'pdf-gen'} and delete $samba->{'pdf-gen'}; } write_conf_restart_smb(); # remove wait message undef $w; check_started('smbd'); } 1;