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 *set_pname = *Amanda::Utilc::set_pname;
53 *safe_cd = *Amanda::Utilc::safe_cd;
54 *check_running_as = *Amanda::Utilc::check_running_as;
55 *set_erroutput_type = *Amanda::Utilc::set_erroutput_type;
57 # ------- VARIABLE STUBS --------
61 *RUNNING_AS_ROOT = *Amanda::Utilc::RUNNING_AS_ROOT;
62 *RUNNING_AS_DUMPUSER = *Amanda::Utilc::RUNNING_AS_DUMPUSER;
63 *RUNNING_AS_DUMPUSER_PREFERRED = *Amanda::Utilc::RUNNING_AS_DUMPUSER_PREFERRED;
64 *RUNNING_AS_CLIENT_LOGIN = *Amanda::Utilc::RUNNING_AS_CLIENT_LOGIN;
65 *RUNNING_AS_UID_ONLY = *Amanda::Utilc::RUNNING_AS_UID_ONLY;
70 use Amanda::Debug qw(:init);
72 use POSIX qw(:fcntl_h);
76 Amanda::Util - Runtime support for Amanda applications
78 =head1 Application Initialization
80 Application initialization generally looks like this:
82 use Amanda::Config qw( :init );
83 use Amanda::Util qw( :check_running_as_flags );
86 Amanda::Util::setup_application("myapp", "server", "cmdline");
87 # .. command-line processing ..
88 Amanda::Config::config_init(...);
89 Amanda::Util::finish_setup($RUNNING_AS_DUMPUSER);
93 =item C<setup_application($name, $type, $context)>
95 Set up the operating environment for an application, without requiring
100 =item C<$name> is the name of the application, used in log messages, etc.
102 =item C<$type> is one of "server" or "client".
104 =item C<$context> is one of "cmdline" for a user-invoked command-line
105 utility (e.g., C<amadmin>) or "daemon" for a program started by
106 C<amandad>. (TODO: daemon is not supported yet)
110 Based on C<$type> and C<$context>, this function does the following:
114 =item sets up debug logging;
116 =item configures internationalization
118 =item sets the umask;
120 =item sets the current working directory to the debug or temporary directory;
122 =item closes any unnecessary file descriptors as a security meaasure;
124 =item ignores C<SIGPIPE>; and
126 =item sets the appropriate target for error messages.
132 # private package variables
137 sub setup_application {
138 my ($name, $type, $context) = @_;
141 croak("no name given") unless ($name);
142 croak("no type given") unless ($type);
143 croak("no context given") unless ($context);
145 # store these as perl values
148 $_pcontext = $context;
150 # and let the C side know about the pname
153 safe_cd(); # (also sets umask)
156 # set up debugging for this application type
160 $SIG{'PIPE'} = 'IGNORE';
162 set_erroutput_type($type, $context);
165 =item C<finish_setup($running_as_flags)>
167 Perform final initialization tasks that require a loaded configuration.
168 Specifically, move the debug log into a configuration-specific
169 subdirectory, and check that the current userid is appropriate for
172 The user is specified by one of the following flags, which are
173 available in export tag C<:check_running_as_flags>:
175 $RUNNING_AS_ROOT # root
176 $RUNNING_AS_DUMPUSER # dumpuser, from configuration
177 $RUNNING_AS_DUMPUSER_PREFERRED # dumpuser, but client_login is OK too
178 $RUNNING_AS_CLIENT_LOGIN # client_login (--with-user at build time)
180 If the flag C<$RUNNING_AS_UID_ONLY> is bit-or'd into C<$running_as_flags>, then
181 the euid is ignored; this is used for programs that expect to be setuid-root.
186 my ($running_as) = @_;
188 my $config_name = Amanda::Config::get_config_name();
191 dbrename($config_name, $_ptype);
194 check_running_as($running_as);
199 Return a "safe" environment hash. For non-setuid programs, this means filtering out any
200 localization variables.
207 delete @rv{qw(IFS CDPATH ENV BASH_ENV LANG)};
209 # delete all LC_* variables
210 for my $var (grep /^LC_/, keys %rv) {
218 push @EXPORT_OK, qw(running_as_flags_to_strings);
219 push @{$EXPORT_TAGS{"running_as_flags"}}, qw(running_as_flags_to_strings);
221 my %_running_as_flags_VALUES;
222 #Convert a flag value to a list of names for flags that are set.
223 sub running_as_flags_to_strings {
227 for my $k (keys %_running_as_flags_VALUES) {
228 my $v = $_running_as_flags_VALUES{$k};
230 #is this a matching flag?
231 if (($v == 0 && $flags == 0) || ($v != 0 && ($flags & $v) == $v)) {
236 #by default, just return the number as a 1-element list
244 push @EXPORT_OK, qw($RUNNING_AS_ROOT);
245 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_ROOT);
247 $_running_as_flags_VALUES{"RUNNING_AS_ROOT"} = $RUNNING_AS_ROOT;
249 push @EXPORT_OK, qw($RUNNING_AS_DUMPUSER);
250 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_DUMPUSER);
252 $_running_as_flags_VALUES{"RUNNING_AS_DUMPUSER"} = $RUNNING_AS_DUMPUSER;
254 push @EXPORT_OK, qw($RUNNING_AS_DUMPUSER_PREFERRED);
255 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_DUMPUSER_PREFERRED);
257 $_running_as_flags_VALUES{"RUNNING_AS_DUMPUSER_PREFERRED"} = $RUNNING_AS_DUMPUSER_PREFERRED;
259 push @EXPORT_OK, qw($RUNNING_AS_CLIENT_LOGIN);
260 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_CLIENT_LOGIN);
262 $_running_as_flags_VALUES{"RUNNING_AS_CLIENT_LOGIN"} = $RUNNING_AS_CLIENT_LOGIN;
264 push @EXPORT_OK, qw($RUNNING_AS_UID_ONLY);
265 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_UID_ONLY);
267 $_running_as_flags_VALUES{"RUNNING_AS_UID_ONLY"} = $RUNNING_AS_UID_ONLY;
270 fcntl(STDIN, F_GETFD, 0) or critical("Standard input is not open");
271 fcntl(STDOUT, F_GETFD, 0) or critical("Standard output is not open");
272 fcntl(STDERR, F_GETFD, 0) or critical("Standard error is not open");