summaryrefslogtreecommitdiffstats
path: root/samba_wizard/Sambaprint.pm
blob: 75059097cc1fe764226b3f59cad5e8da2156e284 (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
#!/usr/bin/perl

# Drakwizard
# Copyright (C) 2005 Mandriva
#
# Authors: antoine Ginies <aginies -@-  mandriva>
#
# 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 $wiz_samba_etc = "/etc/sysconfig/wizard_samba";
my @listshare;
my @listprinters;

my $o = {
	 name => 'Samba 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/samba.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', show_commented_info => 1 });

#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('Samba printers Wizard') . "\n\n" . N(''),
			   no_back => 1,
                           pre => sub {
			     if (! -f $wiz_samba_etc) {
                               $::in->ask_warn(N("Information"), N("It seems that you don\'t setup a Samba server. Please setup a Samba server with Samba wizard before manage your share."));
			       exit(1);
			     }
			   },
			   data => [
                                    { label => N("? :"), val => \$o->{var}{wiz_todo}, list => [ keys %cprint ], format => sub { $cprint{$_[0]} } },
                                   ],
			   next => 'ask_printers',
                          },
	       ask_printers => {
				name => 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};
					  $o->{var}{wiz_printers_guestok} ||= $samba->{printers}{'guest ok'};
					  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 priniters'),
			   pre => sub {
			     $o->{var}{wiz_sambatype} = $type{$o->{var}{wiz_type}};
			   },
			   data => [
				    { label => N('Samba type:'), fixed_val => \$o->{var}{wiz_sambatype} },
				    { label => N('Workgroup:'), fixed_val => \$o->{var}{wiz_workgroup} },
				    { label => N('Server banner:'), fixed_val => \$o->{var}{wiz_banner} },
				    { label => N('Log file:'), fixed_val => \$o->{var}{wiz_log_file} },
				   ],
			   post => \&do_it,
			   next => 'end'
			  },
	       end => {
		       name => N('Congratulations') . "\n\n" . N('The wizard successfully configured your Samba server.'),
		       post => sub {
			 store_samba_type($o->{var}{wiz_type});
		       },
		       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   => $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 sedction 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";
  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 priner_section() {
  $samba->{'pdf-gen'};
  $samba->{path} = "/var/tmp";
  $samba->{'guest ok'} = "no";
  $samba->{printable} = "yes";
  $samba->{comment} = "PDF Generator (only valid users)";
  $samba->{printing} = "bsd";
  $samba->{'print command'} = "/usr/share/samba/scripts/print-pdf \"%s\" \"%H\" \"//%L/%u\" \"%m\" \"%I\" \"%J\" &";
  $samba->{'lpq command'} = "/bin/true";
}

sub printdollar_section() {
  $samba->{'print$'};
  $samba->{path} = "/var/lib/samba/printers";
  $samba->{browseable} = "yes";
  $samba->{'write list'} = "\@adm root";
  $samba->{'guest ok'} = "yes";
  $samba->{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}) {
      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]);
	}
      }
      delete $samba->{global}{'printer adm'};
      delete $samba->{printers};
      delete $samba->{'print$'};
      delete $samba->{'pdf-gen'};
    }

    write_conf_restart_smb();
    # remove wait message
    undef $w;
    check_started('smbd');
  }
1;