5 @ISA @EXPORT_OK $cgi $dbh $freeside_uid $user
6 $conf_dir $secrets $datasrc $db_user $db_pass %callback @callback
7 $driver_name $AutoCommit
10 getsecrets cgisetotaker
13 use Carp qw(carp croak cluck);
19 @EXPORT_OK = qw(checkeuid checkruid cgisuidsetup adminsuidsetup forksuidsetup
20 getotaker dbh datasrc getsecrets driver_name myconnect );
22 $freeside_uid = scalar(getpwnam('freeside'));
24 $conf_dir = "/usr/local/etc/freeside/";
26 $AutoCommit = 1; #ours, not DBI
30 FS::UID - Subroutines for database login and assorted other stuff
34 use FS::UID qw(adminsuidsetup cgisuidsetup dbh datasrc getotaker
40 $dbh = cgisuidsetup($cgi);
46 $driver_name = driver_name;
50 Provides a hodgepodge of subroutines.
56 =item adminsuidsetup USER
58 Sets the user to USER (see config.html from the base documentation).
59 Cleans the environment.
60 Make sure the script is running as freeside, or setuid freeside.
61 Opens a connection to the database.
62 Swaps real and effective UIDs.
63 Runs any defined callbacks (see below).
64 Returns the DBI database handle (usually you don't need this).
69 $dbh->disconnect if $dbh;
76 if ( $FS::CurrentUser::upgrade_hack ) {
77 $user = 'fs_bootstrap';
79 croak "fatal: adminsuidsetup called without arguements" unless $user;
81 $user =~ /^([\w\-\.]+)$/ or croak "fatal: illegal user $user";
85 $ENV{'PATH'} ='/usr/local/bin:/usr/bin:/usr/ucb:/bin';
86 $ENV{'SHELL'} = '/bin/sh';
87 $ENV{'IFS'} = " \t\n";
90 $ENV{'BASH_ENV'} = '';
92 croak "Not running uid freeside!" unless checkeuid();
96 use FS::Schema qw(reload_dbdef);
97 reload_dbdef("/usr/local/etc/freeside/dbdef.$datasrc")
98 unless $FS::Schema::setup_hack;
100 FS::CurrentUser->load_user($user);
102 foreach ( keys %callback ) {
104 # breaks multi-database installs # delete $callback{$_}; #run once
107 &{$_} foreach @callback;
113 DBI->connect( getsecrets, { 'AutoCommit' => 0,
115 'ShowErrorStatement' => 1,
118 or die "DBI->connect error: $DBI::errstr\n";
121 =item install_callback
123 A package can install a callback to be run in adminsuidsetup by passing
124 a coderef to the FS::UID->install_callback class method. If adminsuidsetup has
125 run already, the callback will also be run immediately.
127 $coderef = sub { warn "Hi, I'm returning your call!" };
128 FS::UID->install_callback($coderef);
130 install_callback FS::UID sub {
131 warn "Hi, I'm returning your call!"
136 sub install_callback {
138 my $callback = shift;
139 push @callback, $callback;
140 &{$callback} if $dbh;
143 =item cgisuidsetup CGI_object
145 Takes a single argument, which is a CGI (see L<CGI>) or Apache (see L<Apache>)
146 object (CGI::Base is depriciated). Runs cgisetotaker and then adminsuidsetup.
152 if ( $cgi->isa('CGI::Base') ) {
153 carp "Use of CGI::Base is depriciated";
154 } elsif ( $cgi->isa('Apache') ) {
156 } elsif ( ! $cgi->isa('CGI') ) {
157 croak "fatal: unrecognized object $cgi";
160 adminsuidsetup($user);
165 Returns the CGI (see L<CGI>) object.
170 carp "warning: \$FS::UID::cgi isa Apache" if $cgi->isa('Apache');
176 Returns the DBI database handle.
186 Returns the DBI data source.
196 Returns just the driver name portion of the DBI data source.
201 return $driver_name if defined $driver_name;
202 $driver_name = ( split(':', $datasrc) )[1];
206 croak "suidsetup depriciated";
211 Returns the current Freeside user.
221 Sets and returns the CGI REMOTE_USER. $cgi should be defined as a CGI.pm
222 object (see L<CGI>) or an Apache object (see L<Apache>). Support for CGI::Base
223 and derived classes is depriciated.
228 if ( $cgi && $cgi->isa('CGI::Base') && defined $cgi->var('REMOTE_USER')) {
229 carp "Use of CGI::Base is depriciated";
230 $user = lc ( $cgi->var('REMOTE_USER') );
231 } elsif ( $cgi && $cgi->isa('CGI') && defined $cgi->remote_user ) {
232 $user = lc ( $cgi->remote_user );
233 } elsif ( $cgi && $cgi->isa('Apache') ) {
234 $user = lc ( $cgi->connection->user );
236 die "fatal: Can't get REMOTE_USER! for cgi $cgi - you need to setup ".
237 "Apache user authentication as documented in httemplate/docs/install.html";
244 Returns true if effective UID is that of the freeside user.
249 ( $> == $freeside_uid );
254 Returns true if the real UID is that of the freeside user.
259 ( $< == $freeside_uid );
262 =item getsecrets [ USER ]
264 Sets the user to USER, if supplied.
265 Sets and returns the DBI datasource, username and password for this user from
266 the `/usr/local/etc/freeside/mapsecrets' file.
271 my($setuser) = shift;
272 $user = $setuser if $setuser;
273 my($conf) = new FS::Conf $conf_dir;
275 if ( $conf->exists('mapsecrets') ) {
276 die "No user!" unless $user;
277 my($line) = grep /^\s*($user|\*)\s/, $conf->config('mapsecrets');
278 die "User $user not found in mapsecrets!" unless $line;
279 $line =~ /^\s*($user|\*)\s+(.*)$/;
281 die "Illegal mapsecrets line for user?!" unless $secrets;
283 # no mapsecrets file at all, so do the default thing
284 $secrets = 'secrets';
287 ($datasrc, $db_user, $db_pass) = $conf->config($secrets)
288 or die "Can't get secrets: $secrets: $!\n";
289 $FS::Conf::default_dir = $conf_dir. "/conf.$datasrc";
291 ($datasrc, $db_user, $db_pass);
298 Warning: this interface is (still) likely to change in future releases.
300 New (experimental) callback interface:
302 A package can install a callback to be run in adminsuidsetup by passing
303 a coderef to the FS::UID->install_callback class method. If adminsuidsetup has
304 run already, the callback will also be run immediately.
306 $coderef = sub { warn "Hi, I'm returning your call!" };
307 FS::UID->install_callback($coderef);
309 install_callback FS::UID sub {
310 warn "Hi, I'm returning your call!"
313 Old (deprecated) callback interface:
315 A package can install a callback to be run in adminsuidsetup by putting a
316 coderef into the hash %FS::UID::callback :
318 $coderef = sub { warn "Hi, I'm returning your call!" };
319 $FS::UID::callback{'Package::Name'} = $coderef;
323 Too many package-global variables.
327 No capabilities yet. When mod_perl and Authen::DBI are implemented,
328 cgisuidsetup will go away as well.
330 Goes through contortions to support non-OO syntax with multiple datasrc's.
332 Callbacks are (still) inelegant.
336 L<FS::Record>, L<CGI>, L<DBI>, config.html from the base documentation.