1#!/usr/bin/perl -w
2
3use strict;
4
5use Getopt::Long;
6use Cwd qw(abs_path);
7
8my $opt_help = 0;
9my $opt_smb_conf = undef;
10my $opt_add = 0;
11my $opt_delete = 0;
12
13my $result = GetOptions(
14	'help|h|?'	=> \$opt_help,
15	'smb_conf|s=s'	=> \$opt_smb_conf,
16	'add|a'		=> \$opt_add,
17	'delete|d'	=> \$opt_delete
18);
19
20sub usage($;$)
21{
22	my ($ret, $msg) = @_;
23
24	print $msg."\n\n" if defined($msg);
25
26	print "usage:
27
28	--help|-h|-?		Show this help.
29
30	--smb_conf|-s <path>	Path of the 'smb.conf' file.
31
32	--add|-a		'add' a printer.
33	--delete|-d		'delete' a printer.
34
35	printer_name share_name port_name driver_name location XX remote_machine
36";
37	exit($ret);
38}
39
40usage(1) if (not $result);
41
42usage(0) if ($opt_help);
43
44if (!$opt_add && !$opt_delete) {
45	usage(1, "invalid: neither --add|-a nor --delete|-d set");
46}
47
48if (!$opt_smb_conf) {
49	usage(1, "invalid: no smb.conf file set");
50}
51
52my @argv = @ARGV;
53
54my $printer_name = shift(@argv);
55my $share_name = shift(@argv);
56my $port_name = shift(@argv);
57my $driver_name = shift(@argv);
58my $location = shift(@argv);
59my $win9x_driver_location = shift(@argv);
60my $remote_machine = shift(@argv);
61
62if (!defined($share_name) || length($share_name) == 0) {
63	$share_name = $printer_name;
64}
65
66if (!defined($share_name)) {
67	die "share name not defined";
68}
69
70my $smb_conf_file = $opt_smb_conf;
71if ($smb_conf_file =~ /^(.*)$/) {
72	$smb_conf_file = $1; # untaint file name
73} else {
74	die "Invalid file name $smb_conf_file";
75}
76
77my $tmp = $smb_conf_file.$$;
78
79my $section = undef;
80my $within_section = 0;
81my $found_section = 0;
82
83open(CONFIGFILE_NEW, "+>$tmp") || die "Unable top open conf file $tmp";
84
85open (CONFIGFILE, "+<$smb_conf_file") || die "Unable to open config file $smb_conf_file";
86while (<CONFIGFILE>) {
87	my $line = $_;
88	chomp($_);
89	$_ =~ s/^\s*//;
90	$_ =~ s/\s*$//;
91	if (($_ =~ /^#/) || ($_ =~ /^;/)) {
92		print CONFIGFILE_NEW $line;
93		next;
94	}
95	if ($_ =~ /^\[.*\]$/) {
96		$_ = substr($_, 1, length($_)-2);
97		if (length($_)) {
98			$section = $_;
99		} else {
100			die "invalid section found";
101		}
102		if ($section eq $share_name) {
103			$found_section = 1;
104			if ($opt_add) {
105				exit 0;
106#				die("share $share_name already exists\n");
107			}
108			if ($opt_delete) {
109				$within_section = 1;
110				next;
111			}
112		} else {
113			print CONFIGFILE_NEW $line;
114			$within_section = 0;
115		}
116		next;
117	} else {
118		if ($within_section == 1) {
119			next;
120		}
121		print CONFIGFILE_NEW $line;
122	}
123}
124if ($opt_add) {
125	print CONFIGFILE_NEW "[$share_name]\n\tprintable = yes\n\tpath = /tmp\n";
126}
127close (CONFIGFILE);
128close (CONFIGFILE_NEW);
129
130if ($opt_delete && ($found_section == 0)) {
131	die "share $share_name not found";
132}
133
134delete @ENV{"BASH_ENV"};
135
136$ENV{'PATH'} = '/bin:/usr/bin'; # untaint PATH
137system("cp", "$tmp", "$smb_conf_file");
138unlink $tmp;
139
140exit 0;
141