Imported Upstream version 2.6.0
[debian/amanda] / perl / Amanda / Util.pm
1 # This file was automatically generated by SWIG (http://www.swig.org).
2 # Version 1.3.33
3 #
4 # Don't modify this file, modify the SWIG interface instead.
5
6 package Amanda::Util;
7 require Exporter;
8 require DynaLoader;
9 @ISA = qw(Exporter DynaLoader);
10 package Amanda::Utilc;
11 bootstrap Amanda::Util;
12 package Amanda::Util;
13 @EXPORT = qw( );
14
15 # ---------- BASE METHODS -------------
16
17 package Amanda::Util;
18
19 sub TIEHASH {
20     my ($classname,$obj) = @_;
21     return bless $obj, $classname;
22 }
23
24 sub CLEAR { }
25
26 sub FIRSTKEY { }
27
28 sub NEXTKEY { }
29
30 sub FETCH {
31     my ($self,$field) = @_;
32     my $member_func = "swig_${field}_get";
33     $self->$member_func();
34 }
35
36 sub STORE {
37     my ($self,$field,$newval) = @_;
38     my $member_func = "swig_${field}_set";
39     $self->$member_func($newval);
40 }
41
42 sub this {
43     my $ptr = shift;
44     return tied(%$ptr);
45 }
46
47
48 # ------- FUNCTION WRAPPERS --------
49
50 package Amanda::Util;
51
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;
56
57 # ------- VARIABLE STUBS --------
58
59 package Amanda::Util;
60
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;
66
67 @EXPORT_OK = ();
68 %EXPORT_TAGS = ();
69
70 use Amanda::Debug qw(:init);
71 use Carp;
72 use POSIX qw(:fcntl_h);
73
74 =head1 NAME
75
76 Amanda::Util - Runtime support for Amanda applications
77
78 =head1 Application Initialization
79
80 Application initialization generally looks like this:
81
82   use Amanda::Config qw( :init );
83   use Amanda::Util qw( :check_running_as_flags );
84   use Amanda::Debug;
85
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);
90
91 =over
92
93 =item C<setup_application($name, $type, $context)>
94
95 Set up the operating environment for an application, without requiring
96 any configuration.
97
98 =over
99
100 =item C<$name> is the name of the application, used in log messages, etc.
101
102 =item C<$type> is one of "server" or "client".
103
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)
107
108 =back
109
110 Based on C<$type> and C<$context>, this function does the following:
111
112 =over
113
114 =item sets up debug logging;
115
116 =item configures internationalization
117
118 =item sets the umask;
119
120 =item sets the current working directory to the debug or temporary directory;
121
122 =item closes any unnecessary file descriptors as a security meaasure;
123
124 =item ignores C<SIGPIPE>; and
125
126 =item sets the appropriate target for error messages.
127
128 =back
129
130 =cut
131
132 # private package variables
133 my $_pname;
134 my $_ptype;
135 my $_pcontext;
136
137 sub setup_application {
138     my ($name, $type, $context) = @_;
139
140     # sanity check
141     croak("no name given") unless ($name);
142     croak("no type given") unless ($type);
143     croak("no context given") unless ($context);
144
145     # store these as perl values
146     $_pname = $name;
147     $_ptype = $type;
148     $_pcontext = $context;
149
150     # and let the C side know about the pname
151     set_pname($name);
152
153     safe_cd(); # (also sets umask)
154     check_std_fds();
155
156     # set up debugging for this application type
157     dbopen($type);
158
159     # ignore SIGPIPE
160     $SIG{'PIPE'} = 'IGNORE';
161
162     set_erroutput_type($type, $context);
163 }
164
165 =item C<finish_setup($running_as_flags)>
166
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
170 this applciation.
171
172 The user is specified by one of the following flags, which are
173 available in export tag C<:check_running_as_flags>:
174
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)
179
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.
182
183 =cut
184
185 sub finish_setup {
186     my ($running_as) = @_;
187
188     my $config_name = Amanda::Config::get_config_name();
189
190     if ($config_name) {
191         dbrename($config_name, $_ptype);
192     }
193
194     check_running_as($running_as);
195 }
196
197 =item safe_env
198
199 Return a "safe" environment hash.  For non-setuid programs, this means filtering out any
200 localization variables.
201
202 =cut
203
204 sub safe_env {
205     my %rv = %ENV;
206
207     delete @rv{qw(IFS CDPATH ENV BASH_ENV LANG)};
208
209     # delete all LC_* variables
210     for my $var (grep /^LC_/, keys %rv) {
211         delete $rv{$var};
212     }
213
214     return %rv;
215 }
216
217
218 push @EXPORT_OK, qw(running_as_flags_to_strings);
219 push @{$EXPORT_TAGS{"running_as_flags"}}, qw(running_as_flags_to_strings);
220
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 {
224     my ($flags) = @_;
225     my @result = ();
226
227     for my $k (keys %_running_as_flags_VALUES) {
228         my $v = $_running_as_flags_VALUES{$k};
229
230         #is this a matching flag?
231         if (($v == 0 && $flags == 0) || ($v != 0 && ($flags & $v) == $v)) {
232             push @result, $k;
233         }
234     }
235
236 #by default, just return the number as a 1-element list
237     if (!@result) {
238         return ($flags);
239     }
240
241     return @result;
242 }
243
244 push @EXPORT_OK, qw($RUNNING_AS_ROOT);
245 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_ROOT);
246
247 $_running_as_flags_VALUES{"RUNNING_AS_ROOT"} = $RUNNING_AS_ROOT;
248
249 push @EXPORT_OK, qw($RUNNING_AS_DUMPUSER);
250 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_DUMPUSER);
251
252 $_running_as_flags_VALUES{"RUNNING_AS_DUMPUSER"} = $RUNNING_AS_DUMPUSER;
253
254 push @EXPORT_OK, qw($RUNNING_AS_DUMPUSER_PREFERRED);
255 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_DUMPUSER_PREFERRED);
256
257 $_running_as_flags_VALUES{"RUNNING_AS_DUMPUSER_PREFERRED"} = $RUNNING_AS_DUMPUSER_PREFERRED;
258
259 push @EXPORT_OK, qw($RUNNING_AS_CLIENT_LOGIN);
260 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_CLIENT_LOGIN);
261
262 $_running_as_flags_VALUES{"RUNNING_AS_CLIENT_LOGIN"} = $RUNNING_AS_CLIENT_LOGIN;
263
264 push @EXPORT_OK, qw($RUNNING_AS_UID_ONLY);
265 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_UID_ONLY);
266
267 $_running_as_flags_VALUES{"RUNNING_AS_UID_ONLY"} = $RUNNING_AS_UID_ONLY;
268
269 sub check_std_fds {
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");
273 }
274 1;