Imported Upstream version 2.6.0p2
[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 any
96 configuration.
97
98 C<$name> is the name of the application, used in log messages, etc.  C<$type>
99 is usualy one of "server" or "client".  It specifies the subdirectory in which
100 debug logfiles will be created.  C<$context> indicates the usual manner in
101 which this application is invoked; one of C<"cmdline"> for a user-invoked
102 command-line utility (e.g., C<amadmin>) which should send human-readable error
103 messages to stderr; C<"daemon"> for a program started by C<amandad>, e.g.,
104 C<sendbackup>; or C<"scriptutil"> for a small program used from shell scripts,
105 e.g., C<amgetconf>
106
107 Based on C<$type> and C<$context>, this function does the following:
108
109 =over
110
111 =item *
112
113 sets up debug logging;
114
115 =item *
116
117 configures internationalization
118
119 =item *
120
121 sets the umask;
122
123 =item *
124
125 sets the current working directory to the debug or temporary directory;
126
127 =item *
128
129 closes any unnecessary file descriptors as a security meaasure;
130
131 =item *
132
133 ignores C<SIGPIPE>; and
134
135 =item *
136
137 sets the appropriate target for error messages.
138
139 =back
140
141 =cut
142
143 # private package variables
144 my $_pname;
145 my $_ptype;
146 my $_pcontext;
147
148 sub setup_application {
149     my ($name, $type, $context) = @_;
150
151     # sanity check
152     croak("no name given") unless ($name);
153     croak("no type given") unless ($type);
154     croak("no context given") unless ($context);
155
156     # store these as perl values
157     $_pname = $name;
158     $_ptype = $type;
159     $_pcontext = $context;
160
161     # and let the C side know about the pname
162     set_pname($name);
163
164     safe_cd(); # (also sets umask)
165     check_std_fds();
166
167     # set up debugging for this application type
168     dbopen($type);
169
170     # ignore SIGPIPE
171     $SIG{'PIPE'} = 'IGNORE';
172
173     set_erroutput_type($type, $context);
174 }
175
176 =item C<finish_setup($running_as_flags)>
177
178 Perform final initialization tasks that require a loaded configuration.
179 Specifically, move the debug log into a configuration-specific
180 subdirectory, and check that the current userid is appropriate for
181 this applciation.
182
183 The user is specified by one of the following flags, which are
184 available in export tag C<:check_running_as_flags>:
185
186   $RUNNING_AS_ROOT                # root
187   $RUNNING_AS_DUMPUSER            # dumpuser, from configuration
188   $RUNNING_AS_DUMPUSER_PREFERRED  # dumpuser, but client_login is OK too
189   $RUNNING_AS_CLIENT_LOGIN        # client_login (--with-user at build time)
190
191 If the flag C<$RUNNING_AS_UID_ONLY> is bit-or'd into C<$running_as_flags>, then
192 the euid is ignored; this is used for programs that expect to be setuid-root.
193
194 =cut
195
196 sub finish_setup {
197     my ($running_as) = @_;
198
199     my $config_name = Amanda::Config::get_config_name();
200
201     if ($config_name) {
202         dbrename($config_name, $_ptype);
203     }
204
205     check_running_as($running_as);
206 }
207
208 =item C<safe_env()>
209
210 Return a "safe" environment hash.  For non-setuid programs, this means filtering out any
211 localization variables.
212
213 =cut
214
215 sub safe_env {
216     my %rv = %ENV;
217
218     delete @rv{qw(IFS CDPATH ENV BASH_ENV LANG)};
219
220     # delete all LC_* variables
221     for my $var (grep /^LC_/, keys %rv) {
222         delete $rv{$var};
223     }
224
225     return %rv;
226 }
227
228
229 push @EXPORT_OK, qw(running_as_flags_to_strings);
230 push @{$EXPORT_TAGS{"running_as_flags"}}, qw(running_as_flags_to_strings);
231
232 my %_running_as_flags_VALUES;
233 #Convert a flag value to a list of names for flags that are set.
234 sub running_as_flags_to_strings {
235     my ($flags) = @_;
236     my @result = ();
237
238     for my $k (keys %_running_as_flags_VALUES) {
239         my $v = $_running_as_flags_VALUES{$k};
240
241         #is this a matching flag?
242         if (($v == 0 && $flags == 0) || ($v != 0 && ($flags & $v) == $v)) {
243             push @result, $k;
244         }
245     }
246
247 #by default, just return the number as a 1-element list
248     if (!@result) {
249         return ($flags);
250     }
251
252     return @result;
253 }
254
255 push @EXPORT_OK, qw($RUNNING_AS_ROOT);
256 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_ROOT);
257
258 $_running_as_flags_VALUES{"RUNNING_AS_ROOT"} = $RUNNING_AS_ROOT;
259
260 push @EXPORT_OK, qw($RUNNING_AS_DUMPUSER);
261 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_DUMPUSER);
262
263 $_running_as_flags_VALUES{"RUNNING_AS_DUMPUSER"} = $RUNNING_AS_DUMPUSER;
264
265 push @EXPORT_OK, qw($RUNNING_AS_DUMPUSER_PREFERRED);
266 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_DUMPUSER_PREFERRED);
267
268 $_running_as_flags_VALUES{"RUNNING_AS_DUMPUSER_PREFERRED"} = $RUNNING_AS_DUMPUSER_PREFERRED;
269
270 push @EXPORT_OK, qw($RUNNING_AS_CLIENT_LOGIN);
271 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_CLIENT_LOGIN);
272
273 $_running_as_flags_VALUES{"RUNNING_AS_CLIENT_LOGIN"} = $RUNNING_AS_CLIENT_LOGIN;
274
275 push @EXPORT_OK, qw($RUNNING_AS_UID_ONLY);
276 push @{$EXPORT_TAGS{"running_as_flags"}}, qw($RUNNING_AS_UID_ONLY);
277
278 $_running_as_flags_VALUES{"RUNNING_AS_UID_ONLY"} = $RUNNING_AS_UID_ONLY;
279
280 sub check_std_fds {
281     fcntl(STDIN, F_GETFD, 0) or critical("Standard input is not open");
282     fcntl(STDOUT, F_GETFD, 0) or critical("Standard output is not open");
283     fcntl(STDERR, F_GETFD, 0) or critical("Standard error is not open");
284 }
285
286 =back
287
288 =cut
289 1;