summaryrefslogtreecommitdiffstats
path: root/samba_wizard/Sambaprint.pm
blob: eb69770395a66d0c5c9e4db88bc7d9d2ce14e092 (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
#!/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 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"
	};

# 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("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 hadn't setup a Samba server. Please setup a Samba server with Samba wizard before manage your share."));
			       exit(1);
			     }
			     $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:"), fixed_val => \$o->{var}{wiz_printers_comment} },
				    { label => N("Create mode:"), fixed_val => \$o->{var}{wiz_printers_createmode} },
				    { label => N("Browseable:"), fixed_val => \$o->{var}{wiz_printers_browseable} },
				    { label => N("Guest ok:"), fixed_val => \$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   => $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;