5 @ISA @EXPORT_OK $cgi $dbh $freeside_uid $user
6 $conf_dir $secrets $datasrc $db_user $db_pass %callback $driver_name
10 getsecrets cgisetotaker
13 use Carp qw(carp croak cluck);
18 @EXPORT_OK = qw(checkeuid checkruid swapuid cgisuidsetup
19 adminsuidsetup getotaker dbh datasrc getsecrets driver_name );
21 $freeside_uid = scalar(getpwnam('freeside'));
23 $conf_dir = "/usr/local/etc/freeside/";
25 $AutoCommit = 1; #ours, not DBI
29 FS::UID - Subroutines for database login and assorted other stuff
33 use FS::UID qw(adminsuidsetup cgisuidsetup dbh datasrc getotaker
34 checkeuid checkruid swapuid);
39 $dbh = cgisuidsetup($cgi);
45 $driver_name = driver_name;
49 Provides a hodgepodge of subroutines.
55 =item adminsuidsetup USER
57 Sets the user to USER (see config.html from the base documentation).
58 Cleans the environment.
59 Make sure the script is running as freeside, or setuid freeside.
60 Opens a connection to the database.
61 Swaps real and effective UIDs.
62 Runs any defined callbacks (see below).
63 Returns the DBI database handle (usually you don't need this).
70 croak "fatal: adminsuidsetup called without arguements" unless $user;
72 $ENV{'PATH'} ='/usr/local/bin:/usr/bin:/usr/ucb:/bin';
73 $ENV{'SHELL'} = '/bin/sh';
74 $ENV{'IFS'} = " \t\n";
77 $ENV{'BASH_ENV'} = '';
79 croak "Not running uid freeside!" unless checkeuid();
81 $dbh = DBI->connect($datasrc,$db_user,$db_pass, {
82 'AutoCommit' => 'false',
83 'ChopBlanks' => 'true',
84 } ) or die "DBI->connect error: $DBI::errstr\n";
86 swapuid(); #go to non-privledged user if running setuid freeside
88 foreach ( keys %callback ) {
95 =item cgisuidsetup CGI_object
97 Takes a single argument, which is a CGI (see L<CGI>) or Apache (see L<Apache>)
98 object (CGI::Base is depriciated). Runs cgisetotaker and then adminsuidsetup.
104 if ( $cgi->isa('CGI::Base') ) {
105 carp "Use of CGI::Base is depriciated";
106 } elsif ( $cgi->isa('Apache') ) {
108 } elsif ( ! $cgi->isa('CGI') ) {
109 croak "fatal: unrecognized object $cgi";
112 adminsuidsetup($user);
117 Returns the CGI (see L<CGI>) object.
122 carp "warning: \$FS::UID::cgi isa Apache" if $cgi->isa('Apache');
128 Returns the DBI database handle.
138 Returns the DBI data source.
148 Returns just the driver name portion of the DBI data source.
153 return $driver_name if defined $driver_name;
154 $driver_name = ( split(':', $datasrc) )[1];
158 croak "suidsetup depriciated";
163 Returns the current Freeside user.
173 Sets and returns the CGI REMOTE_USER. $cgi should be defined as a CGI.pm
174 object (see L<CGI>) or an Apache object (see L<Apache>). Support for CGI::Base
175 and derived classes is depriciated.
180 if ( $cgi && $cgi->isa('CGI::Base') && defined $cgi->var('REMOTE_USER')) {
181 carp "Use of CGI::Base is depriciated";
182 $user = lc ( $cgi->var('REMOTE_USER') );
183 } elsif ( $cgi && $cgi->isa('CGI') && defined $cgi->remote_user ) {
184 $user = lc ( $cgi->remote_user );
185 } elsif ( $cgi && $cgi->isa('Apache') ) {
186 $user = lc ( $cgi->connection->user );
188 die "fatal: Can't get REMOTE_USER! for cgi $cgi";
195 Returns true if effective UID is that of the freeside user.
200 ( $> == $freeside_uid );
205 Returns true if the real UID is that of the freeside user.
210 ( $< == $freeside_uid );
215 Swaps real and effective UIDs.
220 ($<,$>) = ($>,$<) if $< != $>;
223 =item getsecrets [ USER ]
225 Sets the user to USER, if supplied.
226 Sets and returns the DBI datasource, username and password for this user from
227 the `/usr/local/etc/freeside/mapsecrets' file.
232 my($setuser) = shift;
233 $user = $setuser if $setuser;
234 die "No user!" unless $user;
235 my($conf) = new FS::Conf $conf_dir;
236 my($line) = grep /^\s*$user\s/, $conf->config('mapsecrets');
237 die "User not found in mapsecrets!" unless $line;
238 $line =~ /^\s*$user\s+(.*)$/;
240 die "Illegal mapsecrets line for user?!" unless $secrets;
241 ($datasrc, $db_user, $db_pass) = $conf->config($secrets)
242 or die "Can't get secrets: $!";
243 $FS::Conf::default_dir = $conf_dir. "/conf.$datasrc";
245 ($datasrc, $db_user, $db_pass);
252 Warning: this interface is likely to change in future releases.
254 A package can install a callback to be run in adminsuidsetup by putting a
255 coderef into the hash %FS::UID::callback :
257 $coderef = sub { warn "Hi, I'm returning your call!" };
258 $FS::UID::callback{'Package::Name'};
262 $Id: UID.pm,v 1.4 2001-02-03 14:03:49 ivan Exp $
266 Too many package-global variables.
270 No capabilities yet. When mod_perl and Authen::DBI are implemented,
271 cgisuidsetup will go away as well.
273 Goes through contortions to support non-OO syntax with multiple datasrc's.
275 Callbacks are inelegant.
279 L<FS::Record>, L<CGI>, L<DBI>, config.html from the base documentation.