1 # This file was automatically generated by SWIG (http://www.swig.org).
4 # Don't modify this file, modify the SWIG interface instead.
9 @ISA = qw(Exporter DynaLoader);
10 package Amanda::Utilc;
11 bootstrap Amanda::Util;
15 # ---------- BASE METHODS -------------
20 my ($classname,$obj) = @_;
21 return bless $obj, $classname;
31 my ($self,$field) = @_;
32 my $member_func = "swig_${field}_get";
33 $self->$member_func();
37 my ($self,$field,$newval) = @_;
38 my $member_func = "swig_${field}_set";
39 $self->$member_func($newval);
48 # ------- FUNCTION WRAPPERS --------
52 *get_original_cwd = *Amanda::Utilc::get_original_cwd;
53 *sanitise_filename = *Amanda::Utilc::sanitise_filename;
54 *quote_string = *Amanda::Utilc::quote_string;
55 *unquote_string = *Amanda::Utilc::unquote_string;
56 *set_pname = *Amanda::Utilc::set_pname;
57 *set_ptype = *Amanda::Utilc::set_ptype;
58 *set_pcontext = *Amanda::Utilc::set_pcontext;
59 *safe_cd = *Amanda::Utilc::safe_cd;
60 *check_running_as = *Amanda::Utilc::check_running_as;
62 # ------- VARIABLE STUBS --------
66 *RUNNING_AS_ANY = *Amanda::Utilc::RUNNING_AS_ANY;
67 *RUNNING_AS_ROOT = *Amanda::Utilc::RUNNING_AS_ROOT;
68 *RUNNING_AS_DUMPUSER = *Amanda::Utilc::RUNNING_AS_DUMPUSER;
69 *RUNNING_AS_DUMPUSER_PREFERRED = *Amanda::Utilc::RUNNING_AS_DUMPUSER_PREFERRED;
70 *RUNNING_AS_CLIENT_LOGIN = *Amanda::Utilc::RUNNING_AS_CLIENT_LOGIN;
71 *RUNNING_AS_UID_ONLY = *Amanda::Utilc::RUNNING_AS_UID_ONLY;
72 *CONTEXT_DEFAULT = *Amanda::Utilc::CONTEXT_DEFAULT;
73 *CONTEXT_CMDLINE = *Amanda::Utilc::CONTEXT_CMDLINE;
74 *CONTEXT_DAEMON = *Amanda::Utilc::CONTEXT_DAEMON;
75 *CONTEXT_SCRIPTUTIL = *Amanda::Utilc::CONTEXT_SCRIPTUTIL;
80 use Amanda::Debug qw(:init);
81 use Amanda::Config qw(:getconf);
83 use POSIX qw( :fcntl_h strftime );
87 Amanda::Util - Runtime support for Amanda applications
89 =head1 Application Initialization
91 Application initialization generally looks like this:
93 use Amanda::Config qw( :init );
94 use Amanda::Util qw( :constants );
97 Amanda::Util::setup_application("myapp", "server", $CONTEXT_CMDLINE);
98 # .. command-line processing ..
99 Amanda::Config::config_init(...);
100 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
104 =item C<setup_application($name, $type, $context)>
106 Set up the operating environment for an application, without requiring any
109 C<$name> is the name of the application, used in log messages, etc. C<$type>
110 is usualy one of "server" or "client". It specifies the subdirectory in which
111 debug logfiles will be created. C<$context> indicates the usual manner in
112 which this application is invoked; one of C<$CONTEXT_CMDLINE> for a
113 user-invoked command-line utility (e.g., C<amadmin>) which should send
114 human-readable error messages to stderr; C<$CONTEXT_DAEMON> for a program
115 started by C<amandad>, e.g., C<sendbackup>; or C<$CONTEXT_SCRIPTUTIL> for a
116 small program used from shell scripts, e.g., C<amgetconf>
118 Based on C<$type> and C<$context>, this function does the following:
124 sets up debug logging;
128 configures internationalization
136 sets the current working directory to the debug or temporary directory;
140 closes any unnecessary file descriptors as a security meaasure;
144 ignores C<SIGPIPE>; and
148 sets the appropriate target for error messages.
154 # private package variables
159 sub setup_application {
160 my ($name, $type, $context) = @_;
163 croak("no name given") unless ($name);
164 croak("no type given") unless ($type);
165 croak("no context given") unless ($context);
167 # store these as perl values
170 $_pcontext = $context;
172 # and let the C side know about them too
175 set_pcontext($context);
177 safe_cd(); # (also sets umask)
180 # set up debugging, now that we have a name, type, and context
184 $SIG{'PIPE'} = 'IGNORE';
187 =item C<finish_setup($running_as_flags)>
189 Perform final initialization tasks that require a loaded configuration.
190 Specifically, move the debug log into a configuration-specific
191 subdirectory, and check that the current userid is appropriate for
194 The user is specified by one of the following flags, which are
195 available in export tag C<:check_running_as_flags>:
197 $RUNNING_AS_ANY # any user is OK
198 $RUNNING_AS_ROOT # root
199 $RUNNING_AS_DUMPUSER # dumpuser, from configuration
200 $RUNNING_AS_DUMPUSER_PREFERRED # dumpuser, but client_login is OK too
201 $RUNNING_AS_CLIENT_LOGIN # client_login (--with-user at build time)
203 If the flag C<$RUNNING_AS_UID_ONLY> is bit-or'd into C<$running_as_flags>, then
204 the euid is ignored; this is used for programs that expect to be setuid-root.
209 my ($running_as) = @_;
211 my $config_name = Amanda::Config::get_config_name();
214 dbrename($config_name, $_ptype);
217 check_running_as($running_as);
220 =item C<get_original_cwd()>
222 Return the original current directory with C<get_original_cwd>.
226 push @EXPORT_OK, qw(get_original_cwd);
227 push @{$EXPORT_TAGS{"util"}}, qw(get_original_cwd);
229 =head1 Miscellaneous Utilities
233 Return a "safe" environment hash. For non-setuid programs, this means filtering out any
234 localization variables.
241 delete @rv{qw(IFS CDPATH ENV BASH_ENV LANG)};
243 # delete all LC_* variables
244 for my $var (grep /^LC_/, keys %rv) {
252 push @EXPORT_OK, qw(running_as_flags_to_strings);
253 push @{$EXPORT_TAGS{"running_as_flags"}}, qw(running_as_flags_to_strings);
255 my %_running_as_flags_VALUES;
256 #Convert a flag value to a list of names for flags that are set.
257 sub running_as_flags_to_strings {
261 for my $k (keys %_running_as_flags_VALUES) {
262 my $v = $_running_as_flags_VALUES{$k};
264 #is this a matching flag?
265 if (($v == 0 && $flags == 0) || ($v != 0 && ($flags & $v) == $v)) {
270 #by default, just return the number as a 1-element list
278 push @EXPORT_OK, qw($RUNNING_AS_ANY);
279 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_ANY);
281 $_running_as_flags_VALUES{"RUNNING_AS_ANY"} = $RUNNING_AS_ANY;
283 push @EXPORT_OK, qw($RUNNING_AS_ROOT);
284 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_ROOT);
286 $_running_as_flags_VALUES{"RUNNING_AS_ROOT"} = $RUNNING_AS_ROOT;
288 push @EXPORT_OK, qw($RUNNING_AS_DUMPUSER);
289 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_DUMPUSER);
291 $_running_as_flags_VALUES{"RUNNING_AS_DUMPUSER"} = $RUNNING_AS_DUMPUSER;
293 push @EXPORT_OK, qw($RUNNING_AS_DUMPUSER_PREFERRED);
294 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_DUMPUSER_PREFERRED);
296 $_running_as_flags_VALUES{"RUNNING_AS_DUMPUSER_PREFERRED"} = $RUNNING_AS_DUMPUSER_PREFERRED;
298 push @EXPORT_OK, qw($RUNNING_AS_CLIENT_LOGIN);
299 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_CLIENT_LOGIN);
301 $_running_as_flags_VALUES{"RUNNING_AS_CLIENT_LOGIN"} = $RUNNING_AS_CLIENT_LOGIN;
303 push @EXPORT_OK, qw($RUNNING_AS_UID_ONLY);
304 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_UID_ONLY);
306 $_running_as_flags_VALUES{"RUNNING_AS_UID_ONLY"} = $RUNNING_AS_UID_ONLY;
308 #copy symbols in running_as_flags to constants
309 push @{$EXPORT_TAGS{"constants"}}, @{$EXPORT_TAGS{"running_as_flags"}};
311 push @EXPORT_OK, qw(pcontext_t_to_string);
312 push @{$EXPORT_TAGS{"pcontext_t"}}, qw(pcontext_t_to_string);
314 my %_pcontext_t_VALUES;
315 #Convert an enum value to a single string
316 sub pcontext_t_to_string {
319 for my $k (keys %_pcontext_t_VALUES) {
320 my $v = $_pcontext_t_VALUES{$k};
322 #is this a matching flag?
323 if ($enumval == $v) {
328 #default, just return the number
332 push @EXPORT_OK, qw($CONTEXT_DEFAULT);
333 push @{$EXPORT_TAGS{"pcontext_t"}}, qw($CONTEXT_DEFAULT);
335 $_pcontext_t_VALUES{"CONTEXT_DEFAULT"} = $CONTEXT_DEFAULT;
337 push @EXPORT_OK, qw($CONTEXT_CMDLINE);
338 push @{$EXPORT_TAGS{"pcontext_t"}}, qw($CONTEXT_CMDLINE);
340 $_pcontext_t_VALUES{"CONTEXT_CMDLINE"} = $CONTEXT_CMDLINE;
342 push @EXPORT_OK, qw($CONTEXT_DAEMON);
343 push @{$EXPORT_TAGS{"pcontext_t"}}, qw($CONTEXT_DAEMON);
345 $_pcontext_t_VALUES{"CONTEXT_DAEMON"} = $CONTEXT_DAEMON;
347 push @EXPORT_OK, qw($CONTEXT_SCRIPTUTIL);
348 push @{$EXPORT_TAGS{"pcontext_t"}}, qw($CONTEXT_SCRIPTUTIL);
350 $_pcontext_t_VALUES{"CONTEXT_SCRIPTUTIL"} = $CONTEXT_SCRIPTUTIL;
352 #copy symbols in pcontext_t to constants
353 push @{$EXPORT_TAGS{"constants"}}, @{$EXPORT_TAGS{"pcontext_t"}};
355 =item C<quote_string($str)>
357 Quote a string using Amanda's quoting algorithm. Strings with no whitespace,
358 control, or quote characters are returned unchanged. An empty string is
359 represented as the two-character string C<"">. Otherwise, tab, newline,
360 carriage return, form-feed, backslash, and double-quote (C<">) characters are
361 escaped with a backslash and the string is surrounded by double quotes.
363 =item C<unquote_string($str)>
365 Unquote a string as quoted with C<quote_string>.
367 =item C<skip_quoted_string($str)>
369 my($q, $remaider) = skip_quoted_string($str)
371 Return the first quoted string and the remainder of the string.
373 Both C<quote_string>, C<unquote_string> and C<skip_quoted_string> are
374 available under the export tag C<:quoting>.
378 sub skip_quoted_string {
384 my $c = substr $str, $i, 1;
385 while ($c ne "" && !($iq == 0 && $c =~ /\s/)) {
388 } elsif ($c eq '\\') {
392 $c = substr $str, $i, 1;
394 my $quoted_string = substr $str, 0, $i;
395 my $remainder = substr $str, $i+1;
397 return ($quoted_string, $remainder);
401 push @EXPORT_OK, qw(quote_string unquote_string skip_quoted_string sanitise_filename);
402 push @{$EXPORT_TAGS{"quoting"}}, qw(quote_string unquote_string skip_quoted_string sanitise_filename);
404 =item C<generate_timestamp()>
406 Generate a timestamp from the current time, obeying the 'USETIMESTAMPS'
407 config parameter. The Amanda configuration must already be loaded.
411 sub generate_timestamp {
412 # this corresponds to common-src/timestamp.c's get_proper_stamp_from_time
413 if (getconf($CNF_USETIMESTAMPS)) {
414 return strftime "%Y%m%d%H%M%S", localtime;
416 return strftime "%Y%m%d", localtime;
421 fcntl(STDIN, F_GETFD, 0) or critical("Standard input is not open");
422 fcntl(STDOUT, F_GETFD, 0) or critical("Standard output is not open");
423 fcntl(STDERR, F_GETFD, 0) or critical("Standard error is not open");