argh
[freeside.git] / fs_webdemo / registerd.Pg
1 #!/usr/bin/perl -w
2 #
3 # $Id: registerd.Pg,v 1.5 2001-04-26 02:18:15 ivan Exp $
4
5 use strict;
6 use vars qw(
7              $freeside_conf
8              $mysql_data
9              $datasrc $user $pass $x
10              $dbh $sth
11              @pw_set @saltset
12              $header $msg
13            );
14             # $freeside_bin $freeside_test 
15             # $cgi $username $name $email $user_pw $crypt_pw 
16 #use CGI;
17 #use CGI::Carp qw(fatalsToBrowser);
18 use DBI;
19 use Mail::Internet;
20 use Mail::Header;
21 use Date::Format;
22
23 #$ENV{'PATH'} ='/usr/local/bin:/usr/bin:/usr/ucb:/bin';
24 #$ENV{'SHELL'} = '/bin/sh';
25 #$ENV{'IFS'} = " \t\n";
26 #$ENV{'CDPATH'} = '';
27 #$ENV{'ENV'} = '';
28 #$ENV{'BASH_ENV'} = '';
29
30 #$freeside_bin = '/home/freeside/bin/';
31 #$freeside_test = '/home/freeside/test/';
32 $freeside_conf = '/usr/local/etc/freeside/';
33
34 #$mysql_data = "/var/lib/mysql";
35
36 $datasrc = 'DBI:mysql:http_auth';
37 $user = "freeside";
38 $pass = "maelcolm";
39
40 #my(@pw_set)= ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '!', '.', ',' );
41 #my(@pw_set)= ( 'a'..'z', 'A'..'Z', '0'..'9' );
42 @pw_set = ( 'a'..'z', '0'..'9' );
43 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
44
45 #die "not running as system user freeside"
46 #  unless $> == scalar(getpwnam('freeside'));
47 die "not running as root user"
48   unless $> == 0;
49
50 $dbh = DBI->connect( $datasrc, $user, $pass, {
51         'AutoCommit' => 'true',
52 } ) or die "DBI->connect error: $DBI::errstr\n";
53 #$x = $DBI::errstr; #silly; to avoid "used only once" warning
54
55 while ( 1 ) {
56
57   $SIG{HUP} = 'IGNORE';
58   $SIG{INT} = 'IGNORE';
59   $SIG{QUIT} = 'IGNORE';
60   $SIG{TERM} = 'IGNORE';
61   $SIG{TSTP} = 'IGNORE';
62   $SIG{PIPE} = 'IGNORE';
63
64   $sth = $dbh->prepare("LOCK TABLES mysql_auth WRITE");
65   $sth->execute or die $sth->errstr;
66
67   $sth = $dbh->prepare(
68     'SELECT * FROM mysql_auth WHERE status = "unconfigured"'
69   );
70   $sth->execute or die $sth->errstr;
71   my $pending = $sth->fetchall_arrayref( {} );
72
73   $sth = $dbh->prepare(
74     'UPDATE mysql_auth SET status = "locked" WHERE status = "unconfigured"'
75   );
76   $sth->execute or die $sth->errstr;
77
78   $sth = $dbh->prepare("UNLOCK TABLES");
79   $sth->execute or die $sth->errstr;
80
81   #
82
83   foreach my $row ( @{$pending} ) {
84
85     my $username = $row->{'username'};
86     my $email = $row->{'passwd'};
87
88     $pdbh = DBI->connect( 'DBI:Pg:host=localhost;database=demo_template', 'freeside', 'maelcolm' )
89       or do { &myerr("$username: ". $DBI::errstr); next; };
90
91     $psth = $pdbh->prepare("CREATE DATABASE demo_$username")
92       or do { &myerr("$username: ". $pdbh->errstr;); next; };
93     $psth->execute()
94       or do { &myerr("$username: ". $psth->errstr;); next; };
95
96     open(PSQL,"|psql -U freeside demo_$username")
97       or do { &myerr("|psql -U freeside demo_$username: $!"); next; };
98     open(PSQLDATA, "</usr/local/etc/freeside/demo_template.Pg")
99       or do { &myerr("/usr/local/etc/freeside/demo_template.Pg: $!"); next; };
100     while(<PSQLDATA>) {
101       print PSQL $_;
102     }
103     close PSQLDATA
104       or do { &myerr("/usr/local/etc/freeside/demo_template.Pg: $!"); next; };
105     close PSQL
106       or do { &myerr("|psql -U freeside demo_$username: $!"); next; };
107
108     mkdir "${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username", 0755;    
109     system "cp -pr ${freeside_conf}conf.DBI:Pg:host=localhost\\;dbname=demo_template/* ".
110            "${freeside_conf}conf.DBI:Pg:host=localhost\\;dbname=demo_$username";
111
112     mkdir "${freeside_conf}counters.DBI:Pg:host=localhost;dbname=demo_$username", 0755;    
113     system "cp -p ${freeside_conf}counters.DBI:Pg:host=localhost\\;dbname=demo_template/* ".
114            "${freeside_conf}counters.DBI:Pg:host=localhost\\;dbname=demo_$username";
115     chown scalar(getpwnam('freeside')), scalar(getgrnam('freeside')),
116            "${freeside_conf}counters.DBI:Pg:host=localhost;dbname=demo_$username";
117
118     system "cp -p ${freeside_conf}dbdef.DBI:Pg:host=localhost\\;dbname=demo_template ".
119            "${freeside_conf}dbdef.DBI:Pg:host=localhost\\;dbname=demo_$username";
120
121     open(INVOICE_FROM, ">${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/invoice_from")
122       or die "Can\'t open ${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/invoice_from: $!";
123     print INVOICE_FROM "$email\n";
124     close INVOICE_FROM;
125
126     open(LPR, ">${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/lpr")
127       or die "Can\'t open ${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/lpr: $!";
128     print LPR "mail $email";
129     close LPR;
130
131     open(FROM, ">${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/from")
132       or die "Can\'t open ${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/from: $!";
133     print FROM "$email\n";
134     close FROM;
135
136     open(TO, ">${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/to")
137       or die "Can\'t open ${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/to: $!";
138     print TO "$email\n";
139     close TO;
140
141     open(SECRETS, ">${freeside_conf}secrets.demo_$username")
142       or die "Can\'t open ${freeside_conf}secrets.demo_$username: $!";
143     chown scalar(getpwnam('freeside')), scalar(getgrnam('freeside')),
144           "${freeside_conf}secrets.demo_$username";
145     chmod 0600, "${freeside_conf}secrets.demo_$username";
146     print SECRETS "DBI:Pg:host=localhost;dbname=demo_$username\nfreeside\nmaelcolm\n";
147     close SECRETS;
148
149     open(MAPSECRETS, ">>${freeside_conf}mapsecrets")
150       or die "Can\'t open ${freeside_conf}mapsecrets: $!";
151     print MAPSECRETS "$username secrets.demo_$username\n";
152     close MAPSECRETS;
153
154     my $user_pw = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) );
155     my $crypt_pw =
156       crypt($user_pw,$saltset[int(rand(64))].$saltset[int(rand(64))]);
157
158     $sth = $dbh->prepare(
159       qq(UPDATE mysql_auth SET passwd = "$crypt_pw", status = "done" WHERE username = "$username")
160     );
161     $sth->execute or die $sth->errstr;
162
163     #$ENV{SMTPHOSTS} = "localhost";
164     $ENV{SMTPHOSTS} = "192.168.1.1";
165     $ENV{MAILADDRESS} = 'ivan-fsreg@sisd.com';
166     $ENV{TZ} = "PST8PDT";
167     $header = Mail::Header->new( [
168       'From: ivan-fsreg@sisd.com',
169       "To: $email",
170       'Bcc: ivan-fsreg_bcc@sisd.com',
171       'Sender: ivan-fsreg@sisd.com',
172       'Reply-To: ivan-fsreg@sisd.com',
173       #'Date: '. time2str("%a, %d %b %Y %X %z", time ),
174       'Date: '. time2str("%a, %d %b %Y %X ", time ). "-0800",
175       'Subject: Freeside demo information',
176     ] );
177     $msg = Mail::Internet->new(
178       'Header' => $header,
179       'Body' => [
180     "Hello,\n",
181     "\n",
182     "Your sample Freeside database has been setup.\n",
183     "\n",
184     "Point your web browswer at http://freeside.sisd.com/ and use the following\n",
185     "authentication information:\n",
186     "\n",
187     "Username: $username\n",
188     "Password: $user_pw\n",
189     "\n",
190     "-- \n",
191     "ivan\n",
192                 ]
193     );
194     $msg->smtpsend or die "Can\'t send registration email!";
195
196   }
197
198   $SIG{HUP} = 'DEFAULT';
199   $SIG{INT} = 'DEFAULT';
200   $SIG{QUIT} = 'DEFAULT';
201   $SIG{TERM} = 'DEFAULT';
202   $SIG{TSTP} = 'DEFAULT';
203   $SIG{PIPE} = 'DEFAULT';
204
205   sleep 5;
206
207 }
208
209 sub myerr {
210   my $msg = shift;
211   open(MAIL,"|mail ivan-fsdemoerr@420.am");
212   print MAIL $msg, "\n\n";
213   print MAIL $msg, "\n\n";
214   close MAIL;
215 };
216