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