#!/usr/bin/perl -w # # $Id: registerd.Pg,v 1.11 2001-10-24 15:29:30 ivan Exp $ use strict; use vars qw( $freeside_conf $mysql_data $datasrc $user $pass $x $dbh $sth @pw_set @saltset $header $msg ); # $freeside_bin $freeside_test # $cgi $username $name $email $user_pw $crypt_pw #use CGI; #use CGI::Carp qw(fatalsToBrowser); use DBI; use Mail::Internet; use Mail::Header; use Date::Format; #$ENV{'PATH'} ='/usr/local/bin:/usr/bin:/usr/ucb:/bin'; #$ENV{'SHELL'} = '/bin/sh'; #$ENV{'IFS'} = " \t\n"; #$ENV{'CDPATH'} = ''; #$ENV{'ENV'} = ''; #$ENV{'BASH_ENV'} = ''; #$freeside_bin = '/home/freeside/bin/'; #$freeside_test = '/home/freeside/test/'; $freeside_conf = '/usr/local/etc/freeside/'; #$mysql_data = "/var/lib/mysql"; $datasrc = 'DBI:mysql:http_auth'; $user = "freeside"; $pass = "maelcolm"; #my(@pw_set)= ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '!', '.', ',' ); #my(@pw_set)= ( 'a'..'z', 'A'..'Z', '0'..'9' ); @pw_set = ( 'a'..'z', '0'..'9' ); @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' ); #die "not running as system user freeside" # unless $> == scalar(getpwnam('freeside')); die "not running as root user" unless $> == 0; $dbh = DBI->connect( $datasrc, $user, $pass, { 'AutoCommit' => 'true', } ) or die "DBI->connect error: $DBI::errstr\n"; #$x = $DBI::errstr; #silly; to avoid "used only once" warning while ( 1 ) { $SIG{HUP} = 'IGNORE'; $SIG{INT} = 'IGNORE'; $SIG{QUIT} = 'IGNORE'; $SIG{TERM} = 'IGNORE'; $SIG{TSTP} = 'IGNORE'; $SIG{PIPE} = 'IGNORE'; $sth = $dbh->prepare("LOCK TABLES mysql_auth WRITE"); $sth->execute or die $sth->errstr; $sth = $dbh->prepare( 'SELECT * FROM mysql_auth WHERE status = "unconfigured"' ); $sth->execute or die $sth->errstr; my $pending = $sth->fetchall_arrayref( {} ); $sth = $dbh->prepare( 'UPDATE mysql_auth SET status = "locked" WHERE status = "unconfigured"' ); $sth->execute or die $sth->errstr; $sth = $dbh->prepare("UNLOCK TABLES"); $sth->execute or die $sth->errstr; # foreach my $row ( @{$pending} ) { my $username = $row->{'username'}; my $email = $row->{'passwd'}; my $pdbh = DBI->connect( 'DBI:Pg:host=localhost;dbname=demo_template', 'freeside', 'maelcolm' ) or do { &myerr("$username: ". $DBI::errstr); next; }; my $psth = $pdbh->prepare("CREATE DATABASE demo_$username") or do { &myerr("$username: ". $pdbh->errstr); next; }; $psth->execute() or do { &myerr("$username: ". $psth->errstr); next; }; $pdbh->disconnect or do { &myerr("fatal: $DBI::errstr"); die; }; open(PSQL,"|psql -U freeside demo_$username") or do { &myerr("|psql -U freeside demo_$username: $!"); next; }; open(PSQLDATA, ") { print PSQL $_; } close PSQLDATA or do { &myerr("/usr/local/etc/freeside/demo_template.Pg: $!"); next; }; close PSQL or do { &myerr("|psql -U freeside demo_$username: $!"); next; }; mkdir "${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username", 0755; system "cp -pr ${freeside_conf}conf.DBI:Pg:host=localhost\\;dbname=demo_template/* ". "${freeside_conf}conf.DBI:Pg:host=localhost\\;dbname=demo_$username"; mkdir "${freeside_conf}counters.DBI:Pg:host=localhost;dbname=demo_$username", 0755; system "cp -p ${freeside_conf}counters.DBI:Pg:host=localhost\\;dbname=demo_template/* ". "${freeside_conf}counters.DBI:Pg:host=localhost\\;dbname=demo_$username"; chown scalar(getpwnam('freeside')), scalar(getgrnam('freeside')), "${freeside_conf}counters.DBI:Pg:host=localhost;dbname=demo_$username"; system "cp -p ${freeside_conf}dbdef.DBI:Pg:host=localhost\\;dbname=demo_template ". "${freeside_conf}dbdef.DBI:Pg:host=localhost\\;dbname=demo_$username"; open(INVOICE_FROM, ">${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/invoice_from") or die "Can\'t open ${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/invoice_from: $!"; print INVOICE_FROM "$email\n"; close INVOICE_FROM; open(LPR, ">${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/lpr") or die "Can\'t open ${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/lpr: $!"; print LPR "mail $email"; close LPR; # open(FROM, ">${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/from") # or die "Can\'t open ${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/from: $!"; # print FROM "$email\n"; # close FROM; # # open(TO, ">${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/to") # or die "Can\'t open ${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/to: $!"; # print TO "$email\n"; # close TO; open(SECRETS, ">${freeside_conf}secrets.demo_$username") or die "Can\'t open ${freeside_conf}secrets.demo_$username: $!"; chown scalar(getpwnam('freeside')), scalar(getgrnam('freeside')), "${freeside_conf}secrets.demo_$username"; chmod 0600, "${freeside_conf}secrets.demo_$username"; print SECRETS "DBI:Pg:host=localhost;dbname=demo_$username\nfreeside\nmaelcolm\n"; close SECRETS; open(MAPSECRETS, ">>${freeside_conf}mapsecrets") or die "Can\'t open ${freeside_conf}mapsecrets: $!"; print MAPSECRETS "$username secrets.demo_$username\n"; close MAPSECRETS; my $user_pw = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) ); my $crypt_pw = crypt($user_pw,$saltset[int(rand(64))].$saltset[int(rand(64))]); $sth = $dbh->prepare( qq(UPDATE mysql_auth SET passwd = "$crypt_pw", status = "done" WHERE username = "$username") ); $sth->execute or die $sth->errstr; #$ENV{SMTPHOSTS} = "localhost"; $ENV{SMTPHOSTS} = "192.168.1.1"; $ENV{MAILADDRESS} = 'ivan-fsreg@sisd.com'; $ENV{TZ} = "PST8PDT"; $header = Mail::Header->new( [ 'From: ivan-fsreg@sisd.com', "To: $email", 'Bcc: ivan-fsreg_bcc@sisd.com', 'Sender: ivan-fsreg@sisd.com', 'Reply-To: ivan-fsreg@sisd.com', #'Date: '. time2str("%a, %d %b %Y %X %z", time ), 'Date: '. time2str("%a, %d %b %Y %X ", time ). "-0800", 'Subject: Freeside demo information', ] ); $msg = Mail::Internet->new( 'Header' => $header, 'Body' => [ "Hello,\n", "\n", "Your sample Freeside database has been setup.\n", "\n", "Your login and database will be automatically deleted in 1-2 months.\n", "\n", "Point your web browswer at http://freeside.sisd.com/ and use the following\n", "authentication information:\n", "\n", "Username: $username\n", "Password: $user_pw\n", "\n", "-- \n", "ivan\n", ] ); $msg->smtpsend or die "Can\'t send registration email!"; } $SIG{HUP} = 'DEFAULT'; $SIG{INT} = 'DEFAULT'; $SIG{QUIT} = 'DEFAULT'; $SIG{TERM} = 'DEFAULT'; $SIG{TSTP} = 'DEFAULT'; $SIG{PIPE} = 'DEFAULT'; sleep 5; } sub myerr { my $msg = shift; open(MAIL,"|mail ivan-fsdemoerr\@420.am"); print MAIL $msg, "\n\n"; print MAIL $msg, "\n\n"; close MAIL; };