backport some unused new-style auth stuff from master for development purposes, shoul...
[freeside.git] / FS / FS / UID.pm
index 2307c9e..9592447 100644 (file)
@@ -2,9 +2,9 @@ package FS::UID;
 
 use strict;
 use vars qw(
-  @ISA @EXPORT_OK $DEBUG $me $cgi $dbh $freeside_uid $user 
-  $conf_dir $secrets $datasrc $db_user $db_pass %callback @callback
-  $driver_name $AutoCommit $callback_hack $use_confcompat
+  @ISA @EXPORT_OK $DEBUG $me $cgi $freeside_uid $user $conf_dir $cache_dir
+  $secrets $datasrc $db_user $db_pass $schema $dbh $driver_name
+  $AutoCommit %callback @callback $callback_hack $use_confcompat
 );
 use subs qw(
   getsecrets cgisetotaker
@@ -16,16 +16,19 @@ use IO::File;
 use FS::CurrentUser;
 
 @ISA = qw(Exporter);
-@EXPORT_OK = qw(checkeuid checkruid cgisuidsetup adminsuidsetup forksuidsetup
-                getotaker dbh datasrc getsecrets driver_name myconnect
-                use_confcompat);
+@EXPORT_OK = qw( checkeuid checkruid cgisuidsetup adminsuidsetup forksuidsetup
+                 preuser_setup
+                 getotaker dbh datasrc getsecrets driver_name myconnect
+                 use_confcompat
+               );
 
 $DEBUG = 0;
 $me = '[FS::UID]';
 
 $freeside_uid = scalar(getpwnam('freeside'));
 
-$conf_dir = "%%%FREESIDE_CONF%%%";
+$conf_dir  = "%%%FREESIDE_CONF%%%";
+$cache_dir = "%%%FREESIDE_CACHE%%%";
 
 $AutoCommit = 1; #ours, not DBI
 $use_confcompat = 1;
@@ -97,7 +100,7 @@ sub forksuidsetup {
   $ENV{'ENV'} = '';
   $ENV{'BASH_ENV'} = '';
 
-  croak "Not running uid freeside!" unless checkeuid();
+  croak "Not running uid freeside (\$>=$>, \$<=$<)\n" unless checkeuid();
 
   warn "$me forksuidsetup connecting to database\n" if $DEBUG;
   if ( $FS::CurrentUser::upgrade_hack && $olduser ) {
@@ -108,22 +111,26 @@ sub forksuidsetup {
   warn "$me forksuidsetup connected to database with handle $dbh\n" if $DEBUG;
 
   warn "$me forksuidsetup loading schema\n" if $DEBUG;
-  use FS::Schema qw(reload_dbdef);
+  use FS::Schema qw(reload_dbdef dbdef);
   reload_dbdef("$conf_dir/dbdef.$datasrc")
     unless $FS::Schema::setup_hack;
 
   warn "$me forksuidsetup deciding upon config system to use\n" if $DEBUG;
 
-  my $confcount = 0;
+  if ( ! $FS::Schema::setup_hack && dbdef->table('conf') ) {
 
-  my $sth = $dbh->prepare("SELECT COUNT(*) FROM conf") or warn $dbh->errstr;
-  $sth and $sth->execute or warn $sth->errstr;
-  $sth and $confcount = $sth->fetchrow_arrayref->[0];
+    my $sth = $dbh->prepare("SELECT COUNT(*) FROM conf") or die $dbh->errstr;
+    $sth->execute or die $sth->errstr;
+    my $confcount = $sth->fetchrow_arrayref->[0];
+  
+    if ($confcount) {
+      $use_confcompat = 0;
+    }else{
+      die "NO CONFIGURATION RECORDS FOUND";
+    }
 
-  if ($confcount) {
-    $use_confcompat = 0;
-  }else{
-    warn "NO CONFIGURATION RECORDS FOUND";
+  } else {
+    die "NO CONFIGURATION TABLE FOUND" unless $FS::Schema::setup_hack;
   }
 
   unless ( $callback_hack ) {
@@ -144,13 +151,105 @@ sub forksuidsetup {
   $dbh;
 }
 
+# start of backported functions from HEAD/4.x only used in development w/
+#  a new style AuthCookie setup
+sub preuser_setup {
+  $dbh->disconnect if $dbh;
+  env_setup();
+  db_setup();
+  callback_setup();
+  $dbh;
+}
+
+sub env_setup {
+
+  $ENV{'PATH'} ='/usr/local/bin:/usr/bin:/bin';
+  $ENV{'SHELL'} = '/bin/sh';
+  $ENV{'IFS'} = " \t\n";
+  $ENV{'CDPATH'} = '';
+  $ENV{'ENV'} = '';
+  $ENV{'BASH_ENV'} = '';
+
+}
+
+sub db_setup {
+  my $olduser = shift;
+
+  croak "Not running uid freeside (\$>=$>, \$<=$<)\n" unless checkeuid();
+
+  warn "$me forksuidsetup connecting to database\n" if $DEBUG;
+  if ( $FS::CurrentUser::upgrade_hack && $olduser ) {
+    $dbh = &myconnect($olduser);
+  } else {
+    $dbh = &myconnect();
+  }
+  warn "$me forksuidsetup connected to database with handle $dbh\n" if $DEBUG;
+
+  warn "$me forksuidsetup loading schema\n" if $DEBUG;
+  use FS::Schema qw(reload_dbdef dbdef);
+  reload_dbdef("$conf_dir/dbdef.$datasrc")
+    unless $FS::Schema::setup_hack;
+
+  warn "$me forksuidsetup deciding upon config system to use\n" if $DEBUG;
+
+  if ( ! $FS::Schema::setup_hack && dbdef->table('conf') ) {
+
+    my $sth = $dbh->prepare("SELECT COUNT(*) FROM conf") or die $dbh->errstr;
+    $sth->execute or die $sth->errstr;
+    my $confcount = $sth->fetchrow_arrayref->[0];
+  
+    if ($confcount) {
+      $use_confcompat = 0;
+    }else{
+      die "NO CONFIGURATION RECORDS FOUND";
+    }
+
+  } else {
+    die "NO CONFIGURATION TABLE FOUND" unless $FS::Schema::setup_hack;
+  }
+
+
+}
+# end of backported functions from HEAD/4.x only used in development
+
+sub callback_setup {
+
+  unless ( $callback_hack ) {
+    warn "$me calling callbacks\n" if $DEBUG;
+    foreach ( keys %callback ) {
+      &{$callback{$_}};
+      # breaks multi-database installs # delete $callback{$_}; #run once
+    }
+
+    &{$_} foreach @callback;
+  } else {
+    warn "$me skipping callbacks (callback_hack set)\n" if $DEBUG;
+  }
+
+}
+
+
 sub myconnect {
-  DBI->connect( getsecrets(@_), { 'AutoCommit'         => 0,
-                                  'ChopBlanks'         => 1,
-                                  'ShowErrorStatement' => 1,
-                                }
-              )
+  my $handle = DBI->connect( getsecrets(@_), { 'AutoCommit'         => 0,
+                                               'ChopBlanks'         => 1,
+                                               'ShowErrorStatement' => 1,
+                                               'pg_enable_utf8'     => 1,
+                                               #'mysql_enable_utf8'  => 1,
+                                             }
+                           )
     or die "DBI->connect error: $DBI::errstr\n";
+
+  if ( $schema ) {
+    use DBIx::DBSchema::_util qw(_load_driver ); #quelle hack
+    my $driver = _load_driver($handle);
+    if ( $driver =~ /^Pg/ ) {
+      no warnings 'redefine';
+      eval "sub DBIx::DBSchema::DBD::${driver}::default_db_schema {'$schema'}";
+      die $@ if $@;
+    }
+  }
+
+  $handle;
 }
 
 =item install_callback
@@ -269,7 +368,7 @@ sub cgisetotaker {
     $user = lc ( $cgi->connection->user );
   } else {
     die "fatal: Can't get REMOTE_USER! for cgi $cgi - you need to setup ".
-        "Apache user authentication as documented in httemplate/docs/install.html";
+        "Apache user authentication as documented in the installation instructions";
   }
   $user;
 }
@@ -281,6 +380,7 @@ Returns true if effective UID is that of the freeside user.
 =cut
 
 sub checkeuid {
+  #$> = $freeside_uid unless $>; #huh.  mpm-itk hack
   ( $> == $freeside_uid );
 }
 
@@ -319,10 +419,11 @@ sub getsecrets {
     $secrets = 'secrets';
   }
 
-  ($datasrc, $db_user, $db_pass) = 
+  ($datasrc, $db_user, $db_pass, $schema) = 
     map { /^(.*)$/; $1 } readline(new IO::File "$conf_dir/$secrets")
       or die "Can't get secrets: $conf_dir/$secrets: $!\n";
   undef $driver_name;
+
   ($datasrc, $db_user, $db_pass);
 }