15 day open invoice reports for qis
[freeside.git] / fs_webdemo / registerd.Pg
1 #!/usr/bin/perl -w
2 #
3 # $Id: registerd.Pg,v 1.11 2001-10-24 15:29:30 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     my $pdbh = DBI->connect( 'DBI:Pg:host=localhost;dbname=demo_template', 'freeside', 'maelcolm' )
89       or do { &myerr("$username: ". $DBI::errstr); next; };
90
91     my $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     $pdbh->disconnect
97       or do { &myerr("fatal: $DBI::errstr"); die; };
98
99     open(PSQL,"|psql -U freeside demo_$username")
100       or do { &myerr("|psql -U freeside demo_$username: $!"); next; };
101     open(PSQLDATA, "</usr/local/etc/freeside/demo_template.Pg")
102       or do { &myerr("/usr/local/etc/freeside/demo_template.Pg: $!"); next; };
103     while(<PSQLDATA>) {
104       print PSQL $_;
105     }
106     close PSQLDATA
107       or do { &myerr("/usr/local/etc/freeside/demo_template.Pg: $!"); next; };
108     close PSQL
109       or do { &myerr("|psql -U freeside demo_$username: $!"); next; };
110
111     mkdir "${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username", 0755;    
112     system "cp -pr ${freeside_conf}conf.DBI:Pg:host=localhost\\;dbname=demo_template/* ".
113            "${freeside_conf}conf.DBI:Pg:host=localhost\\;dbname=demo_$username";
114
115     mkdir "${freeside_conf}counters.DBI:Pg:host=localhost;dbname=demo_$username", 0755;    
116     system "cp -p ${freeside_conf}counters.DBI:Pg:host=localhost\\;dbname=demo_template/* ".
117            "${freeside_conf}counters.DBI:Pg:host=localhost\\;dbname=demo_$username";
118     chown scalar(getpwnam('freeside')), scalar(getgrnam('freeside')),
119            "${freeside_conf}counters.DBI:Pg:host=localhost;dbname=demo_$username";
120
121     system "cp -p ${freeside_conf}dbdef.DBI:Pg:host=localhost\\;dbname=demo_template ".
122            "${freeside_conf}dbdef.DBI:Pg:host=localhost\\;dbname=demo_$username";
123
124     open(INVOICE_FROM, ">${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/invoice_from")
125       or die "Can\'t open ${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/invoice_from: $!";
126     print INVOICE_FROM "$email\n";
127     close INVOICE_FROM;
128
129     open(LPR, ">${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/lpr")
130       or die "Can\'t open ${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/lpr: $!";
131     print LPR "mail $email";
132     close LPR;
133
134 #    open(FROM, ">${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/from")
135 #      or die "Can\'t open ${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/from: $!";
136 #    print FROM "$email\n";
137 #    close FROM;
138 #
139 #    open(TO, ">${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/to")
140 #      or die "Can\'t open ${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/to: $!";
141 #    print TO "$email\n";
142 #    close TO;
143
144     open(SECRETS, ">${freeside_conf}secrets.demo_$username")
145       or die "Can\'t open ${freeside_conf}secrets.demo_$username: $!";
146     chown scalar(getpwnam('freeside')), scalar(getgrnam('freeside')),
147           "${freeside_conf}secrets.demo_$username";
148     chmod 0600, "${freeside_conf}secrets.demo_$username";
149     print SECRETS "DBI:Pg:host=localhost;dbname=demo_$username\nfreeside\nmaelcolm\n";
150     close SECRETS;
151
152     open(MAPSECRETS, ">>${freeside_conf}mapsecrets")
153       or die "Can\'t open ${freeside_conf}mapsecrets: $!";
154     print MAPSECRETS "$username secrets.demo_$username\n";
155     close MAPSECRETS;
156
157     my $user_pw = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) );
158     my $crypt_pw =
159       crypt($user_pw,$saltset[int(rand(64))].$saltset[int(rand(64))]);
160
161     $sth = $dbh->prepare(
162       qq(UPDATE mysql_auth SET passwd = "$crypt_pw", status = "done" WHERE username = "$username")
163     );
164     $sth->execute or die $sth->errstr;
165
166     #$ENV{SMTPHOSTS} = "localhost";
167     $ENV{SMTPHOSTS} = "192.168.1.1";
168     $ENV{MAILADDRESS} = 'ivan-fsreg@sisd.com';
169     $ENV{TZ} = "PST8PDT";
170     $header = Mail::Header->new( [
171       'From: ivan-fsreg@sisd.com',
172       "To: $email",
173       'Bcc: ivan-fsreg_bcc@sisd.com',
174       'Sender: ivan-fsreg@sisd.com',
175       'Reply-To: ivan-fsreg@sisd.com',
176       #'Date: '. time2str("%a, %d %b %Y %X %z", time ),
177       'Date: '. time2str("%a, %d %b %Y %X ", time ). "-0800",
178       'Subject: Freeside demo information',
179     ] );
180     $msg = Mail::Internet->new(
181       'Header' => $header,
182       'Body' => [
183     "Hello,\n",
184     "\n",
185     "Your sample Freeside database has been setup.\n",
186     "\n",
187     "Your login and database will be automatically deleted in 1-2 months.\n",
188     "\n",        
189     "Point your web browswer at http://freeside.sisd.com/ and use the following\n",
190     "authentication information:\n",
191     "\n",
192     "Username: $username\n",
193     "Password: $user_pw\n",
194     "\n",
195     "-- \n",
196     "ivan\n",
197                 ]
198     );
199     $msg->smtpsend or die "Can\'t send registration email!";
200
201   }
202
203   $SIG{HUP} = 'DEFAULT';
204   $SIG{INT} = 'DEFAULT';
205   $SIG{QUIT} = 'DEFAULT';
206   $SIG{TERM} = 'DEFAULT';
207   $SIG{TSTP} = 'DEFAULT';
208   $SIG{PIPE} = 'DEFAULT';
209
210   sleep 5;
211
212 }
213
214 sub myerr {
215   my $msg = shift;
216   open(MAIL,"|mail ivan-fsdemoerr\@420.am");
217   print MAIL $msg, "\n\n";
218   print MAIL $msg, "\n\n";
219   close MAIL;
220 };
221