86870e1e3e1633c3178ab0b7926ab15968459c62
[debian/amanda] / common-src / amgetconf.pl
1 #! @PERL@
2 # Copyright (c) 2005-2008 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 Mathlida 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     }
63     exit(1);
64 }
65
66 sub fail {
67     print STDERR @_, "\n";
68     exit(1);
69 }
70
71 sub no_such_param {
72     my ($parameter) = @_;
73     fail("amgetconf: no such parameter \"$parameter\"");
74 }
75
76 ## build parameters
77
78 my %build_info = (
79     # NOTE TO MAINTAINERS:
80     #   If you add to this list, be sure to also add the new parameter 
81     #   amgetconf(8) manual page.  Note that all keys are lower-case.
82
83     ## directories from Amanda::Paths
84
85     'bindir' => $bindir,
86     'sbindir' => $sbindir,
87     'libexecdir' => $libexecdir,
88     'amlibexecdir' => $amlibexecdir,
89     'mandir' => $mandir,
90     'amanda_tmpdir' => $AMANDA_TMPDIR,
91     'config_dir' => $CONFIG_DIR,
92     'amanda_dbgdir' => $AMANDA_DBGDIR,
93     'application_dir' => $APPLICATION_DIR,
94     'gnutar_listed_incremental_dir' => $GNUTAR_LISTED_INCREMENTAL_DIR,
95     'listed_inc_dir' => $GNUTAR_LISTED_INCREMENTAL_DIR, # (historical alias)
96
97     ## constants from Amanda::Constants
98
99     # build environment info
100
101     'cc' => $Amanda::Constants::CC,
102     'version' => $Amanda::Constants::VERSION,
103     'assertions' => $Amanda::Constants::ASSERTIONS,
104     'use_version_suffixes' => $Amanda::Constants::USE_VERSION_SUFFIXES,
105     'locking' => $Amanda::Constants::LOCKING,
106
107     # executable paths
108
109     'dump' => $Amanda::Constants::DUMP,
110     'restore' => $Amanda::Constants::RESTORE,
111     'vdump' => $Amanda::Constants::VDUMP,
112     'vrestore' => $Amanda::Constants::VRESTORE,
113     'xfsdump' => $Amanda::Constants::XFSDUMP,
114     'xfsrestore' => $Amanda::Constants::XFSRESTORE,
115     'vxdump' => $Amanda::Constants::VXDUMP,
116     'vxrestore' => $Amanda::Constants::VXRESTORE,
117     'samba_client' => $Amanda::Constants::SAMBA_CLIENT,
118     'gnutar' => $Amanda::Constants::GNUTAR,
119     'star' => $Amanda::Constants::STAR,
120     'compress_path' => $Amanda::Constants::COMPRESS_PATH,
121     'uncompress_path' => $Amanda::Constants::UNCOMPRESS_PATH,
122     'aix_backup' => $Amanda::Constants::AIX_BACKUP,
123     'dump_returns_1' => $Amanda::Constants::DUMP_RETURNS_1,
124
125     # amanda modules
126
127     'bsd_security' => $Amanda::Constants::BSD_SECURITY,
128     'bsdudp_security' => $Amanda::Constants::BSDUDP_SECURITY,
129     'bsdtcp_security' => $Amanda::Constants::BSDTCP_SECURITY,
130     'krb4_security' => $Amanda::Constants::KRB4_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     if (($appname) = $parameter =~ /^dbopen\.(.*)/) {
197         $appname =~ s/[^[:alnum:]]/_/g;
198         Amanda::Util::setup_application($appname, "server", $CONTEXT_CMDLINE);
199         print Amanda::Debug::dbfn(), "\n";
200     } elsif (($appname, $filename) = $parameter =~ /^dbclose\.([^:]*):(.*)/) {
201         fail("debug file $filename does not exist") unless (-f $filename);
202         Amanda::Debug::dbreopen($filename, '');
203         Amanda::Debug::dbclose();
204         print "$filename\n";
205     } else {
206         fail("cannot parse $parameter");
207     }
208 }
209
210 ## regular configuration parameters
211
212 sub conf_param {
213     my ($parameter, $opt_list) = @_;
214
215     if ($opt_list) {
216         # getconf_list will return an empty list for any unrecognized name,
217         # so first check that the user has supplied a real subsection
218         no_such_param($parameter)
219             unless defined($Amanda::Config::subsection_names{$parameter});
220         my @list = getconf_list($parameter);
221
222         for my $subsec (@list) {
223             print "$subsec\n";
224         }
225     } else {
226         no_such_param($parameter)
227             unless defined(getconf_byname($parameter));
228         my @strs = getconf_byname_strs($parameter, 0);
229         
230         for my $str (@strs) {
231             print "$str\n";
232         }
233     }
234 }
235
236 ## Command-line parsing
237
238 my $opt_list = '';
239 my $config_overwrites = new_config_overwrites($#ARGV+1);
240 my $execute_where = undef;
241
242 Getopt::Long::Configure(qw{bundling});
243 GetOptions(
244     'list|l' => \$opt_list,
245     'o=s' => sub { add_config_overwrite_opt($config_overwrites, $_[1]); },
246     'execute-where=s' => sub {
247         my $where = lc($_[1]);
248         fail("Invalid value ($_[1]) for --execute-where. Must be client or server.") 
249             unless $where eq 'client' or $where eq 'server';
250         fail("--execute-where=server conflicts with --execute-where=client or --client.")
251             unless !defined($execute_where) || (
252                 ($where eq 'client' && $execute_where) ||
253                 ($where eq 'server' && !$execute_where));
254         $execute_where = ($where eq 'client')? $CONFIG_INIT_CLIENT : 0;
255     },
256     'client' => sub {
257         fail("--execute-where=server conflicts with --execute-where=client or --client.")
258             unless !defined($execute_where) || $execute_where;
259         $execute_where = $CONFIG_INIT_CLIENT;
260     }
261 ) or usage();
262
263 my $config_name;
264 my $parameter;
265
266 if (@ARGV == 1) {
267     $parameter = $ARGV[0];
268 } elsif (@ARGV >= 2) {
269     # note that we ignore any arguments past these two.  Amdump lazily passes 
270     # such arguments on to us, so we have no choice.
271     $config_name = $ARGV[0];
272     $parameter = $ARGV[1];
273 } else {
274     usage();
275 }
276
277 ## Now start looking at the parameter.
278
279 if ($parameter =~ /^build(?:\..*)?/) {
280     build_param($parameter, $opt_list);
281     exit(0);
282
283
284 if ($parameter =~ /^db(open|close)\./) {
285     db_param($parameter, $opt_list);
286     exit(0);
287 }
288
289 # finally, finish up the application startup procedure
290 Amanda::Util::setup_application("amgetconf", "server", $CONTEXT_SCRIPTUTIL);
291 config_init($CONFIG_INIT_EXPLICIT_NAME | $CONFIG_INIT_USE_CWD | $execute_where, $config_name);
292 apply_config_overwrites($config_overwrites);
293 my ($cfgerr_level, @cfgerr_errors) = config_errors();
294 if ($cfgerr_level >= $CFGERR_WARNINGS) {
295     config_print_errors();
296     if ($cfgerr_level >= $CFGERR_ERRORS) {
297         die("errors processing config file");
298     }
299 }
300
301 Amanda::Util::finish_setup($RUNNING_AS_ANY);
302
303 conf_param($parameter, $opt_list);