23a9f1e93e484808548ea5f3b25d85346c71083e
[debian/amanda] / common-src / amgetconf.pl
1 #! @PERL@
2 # Copyright (c) 2008, 2009, 2010 Zmanda, Inc.  All Rights Reserved.
3 #
4 # This program is free software; you can redistribute it and/or modify it
5 # under the terms of the GNU General Public License version 2 as published
6 # by the Free Software Foundation.
7 #
8 # This program is distributed in the hope that it will be useful, but
9 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
10 # or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
11 # for more details.
12 #
13 # You should have received a copy of the GNU General Public License along
14 # with this program; if not, write to the Free Software Foundation, Inc.,
15 # 59 Temple Place, Suite 330, Boston, MA  02111-1307 USA
16 #
17 # Contact information: Zmanda Inc., 465 S. Mathilda Ave., Suite 300
18 # Sunnyvale, CA 94086, USA, or: http://www.zmanda.com
19
20 use lib '@amperldir@';
21 use strict;
22 use warnings;
23 use Amanda::Config qw( :getconf :init );
24 use Amanda::Debug qw( :logging );
25 use Amanda::Util qw( :constants );
26 use Amanda::Paths;
27 use Amanda::Constants;
28 use Getopt::Long;
29
30 # Implementation note: this application is a bit funny, because it does not
31 # set up Amanda fully until some time into processing.  This lets it respond
32 # with build configuration information without a config file, and lets it set
33 # up debugging for the caller.  
34 #
35 # The most obvious consequence is that, rather than calling die (which interfaces
36 # with Amanda::Debug), this file uses a locally defined 'fail' to print error 
37 # messages.
38
39 sub usage {
40     print <<EOF;
41 Usage: amgetconf [--client] [--execute-where client|server] [-l|--list] [-o configoption]* <config> <paramname>
42   (any ordering of options and arguments is acceptable)
43
44 --client is equivalent to --execute-where client
45
46 --execute-where tells amgetconf whether to operate on the client or the 
47 server; the server is the default.
48
49 paramname can be one of
50   dbopen.APPNAME -- open a debug file
51   dbclose.APPNAME:FILENAME -- close debug file FILENAME
52   build.PARAM -- get a build parameter
53   PARAM -- get an Amanda configuration parameter
54
55 For all but Amanda configuration parameters, the <config> option is
56 ignored, but must be present.  For Amanda configuration parameters,
57 values in subsections are specified in the form TYPE:NAME:PARAMETER.
58
59 With --list, PARAM can be one of
60 EOF
61     for my $name (keys %Amanda::Config::subsection_names) {
62         print "    $name\n"
63             if $Amanda::Config::subsection_names{$name};
64     }
65     exit(1);
66 }
67
68 sub fail {
69     print STDERR @_, "\n";
70     exit(1);
71 }
72
73 sub no_such_param {
74     my ($parameter) = @_;
75     fail("amgetconf: no such parameter \"$parameter\"");
76 }
77
78 ## build parameters
79
80 my %build_info = (
81     # NOTE TO MAINTAINERS:
82     #   If you add to this list, be sure to also add the new parameter 
83     #   amgetconf(8) manual page.  Note that all keys are lower-case.
84
85     ## directories from Amanda::Paths
86
87     'bindir' => $bindir,
88     'sbindir' => $sbindir,
89     'libexecdir' => $libexecdir,
90     'amlibexecdir' => $amlibexecdir,
91     'mandir' => $mandir,
92     'amanda_tmpdir' => $AMANDA_TMPDIR,
93     'config_dir' => $CONFIG_DIR,
94     'amanda_dbgdir' => $AMANDA_DBGDIR,
95     'application_dir' => $APPLICATION_DIR,
96     'gnutar_listed_incremental_dir' => $GNUTAR_LISTED_INCREMENTAL_DIR,
97     'listed_inc_dir' => $GNUTAR_LISTED_INCREMENTAL_DIR, # (historical alias)
98
99     ## constants from Amanda::Constants
100
101     # build environment info
102
103     'cc' => $Amanda::Constants::CC,
104     'version' => $Amanda::Constants::VERSION,
105     'assertions' => $Amanda::Constants::ASSERTIONS,
106     'use_version_suffixes' => 'no', # for backward compatibility
107     'locking' => $Amanda::Constants::LOCKING,
108
109     # executable paths
110
111     'dump' => $Amanda::Constants::DUMP,
112     'restore' => $Amanda::Constants::RESTORE,
113     'vdump' => $Amanda::Constants::VDUMP,
114     'vrestore' => $Amanda::Constants::VRESTORE,
115     'xfsdump' => $Amanda::Constants::XFSDUMP,
116     'xfsrestore' => $Amanda::Constants::XFSRESTORE,
117     'vxdump' => $Amanda::Constants::VXDUMP,
118     'vxrestore' => $Amanda::Constants::VXRESTORE,
119     'samba_client' => $Amanda::Constants::SAMBA_CLIENT,
120     'gnutar' => $Amanda::Constants::GNUTAR,
121     'star' => $Amanda::Constants::STAR,
122     'compress_path' => $Amanda::Constants::COMPRESS_PATH,
123     'uncompress_path' => $Amanda::Constants::UNCOMPRESS_PATH,
124     'aix_backup' => $Amanda::Constants::AIX_BACKUP,
125     'dump_returns_1' => $Amanda::Constants::DUMP_RETURNS_1,
126
127     # amanda modules
128
129     'bsd_security' => $Amanda::Constants::BSD_SECURITY,
130     'bsdudp_security' => $Amanda::Constants::BSDUDP_SECURITY,
131     'bsdtcp_security' => $Amanda::Constants::BSDTCP_SECURITY,
132     'krb5_security' => $Amanda::Constants::KRB5_SECURITY,
133     'ssh_security' => $Amanda::Constants::SSH_SECURITY,
134     'rsh_security' => $Amanda::Constants::RSH_SECURITY,
135     'use_amandahosts' => $Amanda::Constants::USE_AMANDAHOSTS,
136
137     # build-time constants
138     
139     'amanda_debug_days' => $Amanda::Constants::AMANDA_DEBUG_DAYS,
140     'default_server' => $Amanda::Constants::DEFAULT_SERVER,
141     'default_amandates_file' => $Amanda::Constants::DEFAULT_AMANDATES_FILE,
142     'default_config' => $Amanda::Constants::DEFAULT_CONFIG,
143     'default_tape_server' => $Amanda::Constants::DEFAULT_TAPE_SERVER,
144     'default_tape_device' => $Amanda::Constants::DEFAULT_TAPE_DEVICE,
145     'client_login' => $Amanda::Constants::CLIENT_LOGIN,
146     'use_rundump' => $Amanda::Constants::USE_RUNDUMP,
147     'check_userid' => $Amanda::Constants::CHECK_USERID,
148
149     # compression information
150
151     'compress_suffix' => $Amanda::Constants::COMPRESS_SUFFIX,
152     'compress_fast_opt' => $Amanda::Constants::COMPRESS_FAST_OPT,
153     'compress_best_opt' => $Amanda::Constants::COMPRESS_BEST_OPT,
154     'uncompress_opt' => $Amanda::Constants::UNCOMPRESS_OPT,
155
156     # kerberos information
157
158     'ticket_lifetime' => $Amanda::Constants::TICKET_LIFETIME,
159     'server_host_principal' => $Amanda::Constants::SERVER_HOST_PRINCIPAL,
160     'server_host_instance' => $Amanda::Constants::SERVER_HOST_INSTANCE,
161     'server_host_key_file' => $Amanda::Constants::SERVER_HOST_KEY_FILE,
162     'client_host_principal' => $Amanda::Constants::CLIENT_HOST_PRINCIPAL,
163     'client_host_instance' => $Amanda::Constants::CLIENT_HOST_INSTANCE,
164     'client_host_key_file' => $Amanda::Constants::CLIENT_HOST_KEY_FILE,
165     # (historical typos:)
166     'server_host_principle' => $Amanda::Constants::SERVER_HOST_PRINCIPAL,
167     'client_host_principle' => $Amanda::Constants::CLIENT_HOST_PRINCIPAL,
168     # (for testing purposes)
169     '__empty' => '',
170
171 );
172
173 sub build_param {
174     my ($parameter, $opt_list) = @_;
175
176     if ($opt_list) {
177         usage() unless ($parameter eq "build");
178
179         for my $pname (sort keys %build_info) {
180             print "$pname\n";
181         }
182     } else {
183         my ($pname) = $parameter =~ /^build\.(.*)/;
184
185         my $val = $build_info{lc $pname};
186         no_such_param($parameter) unless (defined($val));
187
188         print "$val\n";
189     }
190 }
191
192 ## dbopen or dbclose
193
194 sub db_param {
195     my ($parameter, $opt_list) = @_;
196     my ($appname, $filename);
197
198     # copy amgetconf pname and pcontext
199     my $pname = Amanda::Util::get_pname();
200     my $pcontext = Amanda::Util::get_pcontext();
201
202     if (($appname) = $parameter =~ /^dbopen\.(.*)/) {
203         $appname =~ s/[^[:alnum:]]/_/g;
204         # set pname and pcontext for the application
205         Amanda::Util::set_pname($appname);
206         Amanda::Util::set_pcontext($CONTEXT_CMDLINE);
207         Amanda::Debug::dbopen("server");
208         print Amanda::Debug::dbfn(), "\n";
209     } elsif (($appname, $filename) = $parameter =~ /^dbclose\.([^:]*):(.*)/) {
210         fail("debug file $filename does not exist") unless (-f $filename);
211         # set pname and pcontext for the application
212         Amanda::Util::set_pname($appname);
213         Amanda::Util::set_pcontext($CONTEXT_CMDLINE);
214         Amanda::Debug::dbreopen($filename, '');
215         Amanda::Debug::dbclose();
216         print "$filename\n";
217     } else {
218         fail("cannot parse $parameter");
219     }
220     # reset pname and pcontext for amgetconf
221     Amanda::Util::set_pcontext($pcontext);
222     Amanda::Util::set_pname($pname);
223 }
224
225 ## regular configuration parameters
226
227 sub conf_param {
228     my ($parameter, $opt_list) = @_;
229
230     if ($opt_list) {
231         # getconf_list will return an empty list for any unrecognized name,
232         # so first check that the user has supplied a real subsection
233         no_such_param($parameter)
234             unless defined($Amanda::Config::subsection_names{$parameter});
235         my @list = getconf_list($parameter);
236
237         for my $subsec (@list) {
238             print "$subsec\n";
239         }
240     } elsif ($parameter =~ /^property:/i) {
241         my %properties = %{ getconf($CNF_PROPERTY)};
242         my $propname = $parameter;
243         $propname =~ s/^property://i;
244         $propname = lc($propname);
245         if (exists $properties{$propname}) {
246             print $properties{$propname}->{'values'}[0], "\n";
247         }
248     } elsif ($parameter =~ /^device-property:/i ||
249              $parameter =~ /^device_property:/i) {
250         my %properties = %{ getconf($CNF_DEVICE_PROPERTY) };
251         my $propname = $parameter;
252         $propname =~ s/^device-property://i;
253         $propname =~ s/^device_property://i;
254         $propname = lc($propname);
255         if (exists $properties{$propname}) {
256             print $properties{$propname}->{'values'}[0], "\n";
257         }
258     } else {
259         no_such_param($parameter)
260             unless defined(getconf_byname($parameter));
261         my @strs = getconf_byname_strs($parameter, 0);
262         
263         for my $str (@strs) {
264             print "$str\n";
265         }
266     }
267 }
268
269 Amanda::Util::setup_application("amgetconf", "server", $CONTEXT_SCRIPTUTIL);
270
271 ## Command-line parsing
272
273 my $opt_list = '';
274 my $config_overrides = new_config_overrides($#ARGV+1);
275 my $execute_where = undef;
276
277 debug("Arguments: " . join(' ', @ARGV));
278 Getopt::Long::Configure(qw{bundling});
279 GetOptions(
280     'version' => \&Amanda::Util::version_opt,
281     'list|l' => \$opt_list,
282     'o=s' => sub { add_config_override_opt($config_overrides, $_[1]); },
283     'execute-where=s' => sub {
284         my $where = lc($_[1]);
285         fail("Invalid value ($_[1]) for --execute-where. Must be client or server.") 
286             unless $where eq 'client' or $where eq 'server';
287         fail("--execute-where=server conflicts with --execute-where=client or --client.")
288             unless !defined($execute_where) || (
289                 ($where eq 'client' && $execute_where) ||
290                 ($where eq 'server' && !$execute_where));
291         $execute_where = ($where eq 'client')? $CONFIG_INIT_CLIENT : 0;
292     },
293     'client' => sub {
294         fail("--execute-where=server conflicts with --execute-where=client or --client.")
295             unless !defined($execute_where) || $execute_where;
296         $execute_where = $CONFIG_INIT_CLIENT;
297     }
298 ) or usage();
299
300 my $config_name;
301 my $parameter;
302
303 if (@ARGV == 1) {
304     $parameter = $ARGV[0];
305 } elsif (@ARGV >= 2) {
306     # note that we ignore any arguments past these two.  Amdump lazily passes 
307     # such arguments on to us, so we have no choice.
308     $config_name = $ARGV[0];
309     $parameter = $ARGV[1];
310 } else {
311     usage();
312 }
313
314 ## Now start looking at the parameter.
315
316 if ($parameter =~ /^build(?:\..*)?/) {
317     config_init(0|$execute_where, undef);
318     build_param($parameter, $opt_list);
319     Amanda::Util::finish_application();
320     exit(0);
321
322
323 if ($parameter =~ /^db(open|close)\./) {
324     config_init(0|$execute_where, undef);
325     db_param($parameter, $opt_list);
326     Amanda::Util::finish_application();
327     exit(0);
328 }
329
330 # finally, finish up the application startup procedure
331 set_config_overrides($config_overrides);
332 if ($execute_where == $CONFIG_INIT_CLIENT &&
333     defined($config_name) && $config_name eq '.') {
334     config_init($CONFIG_INIT_USE_CWD | $execute_where, undef);
335 } elsif ($execute_where == $CONFIG_INIT_CLIENT &&
336     defined($config_name) && $config_name ne '.') {
337     config_init($CONFIG_INIT_EXPLICIT_NAME | $execute_where, $config_name);
338 } elsif ($execute_where == $CONFIG_INIT_CLIENT) {
339     config_init($execute_where, undef);
340 } else {
341     config_init($CONFIG_INIT_EXPLICIT_NAME | $CONFIG_INIT_USE_CWD | $execute_where, $config_name);
342 }
343 my ($cfgerr_level, @cfgerr_errors) = config_errors();
344 if ($cfgerr_level >= $CFGERR_WARNINGS) {
345     config_print_errors();
346     if ($cfgerr_level >= $CFGERR_ERRORS) {
347         die("errors processing config file");
348     }
349 }
350
351 Amanda::Util::finish_setup($RUNNING_AS_ANY);
352
353 conf_param($parameter, $opt_list);
354
355 Amanda::Util::finish_application();