4713841371872dec7a7eb7fcb81ec4bb303448df
[freeside.git] / fs_webdemo / registerd.Pg
1 #!/usr/bin/perl -w
2 #
3 # $Id: registerd.Pg,v 1.2 2001-04-23 16:25:05 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     system("/usr/bin/createdb --username freeside demo_$username >/dev/null");
89
90     system "psql -U freeside demo_$username </usr/local/etc/freeside/demo_template.Pg";
91
92     mkdir "${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username", 0755;    
93     system "cp -pr ${freeside_conf}conf.DBI:Pg:host=localhost\\;dbname=demo_template/* ".
94            "${freeside_conf}conf.DBI:Pg:host=localhost\\;dbname=demo_$username";
95
96     mkdir "${freeside_conf}counters.DBI:Pg:host=localhost;dbname=demo_$username", 0755;    
97     system "cp -p ${freeside_conf}counters.DBI:Pg:host=localhost\\;dbname=demo_template/* ".
98            "${freeside_conf}counters.DBI:Pg:host=localhost\\;dbname=demo_$username";
99     chown scalar(getpwnam('freeside')), scalar(getgrnam('freeside')),
100            "${freeside_conf}counters.DBI:Pg:host=localhost;dbname=demo_$username";
101
102     system "cp -p ${freeside_conf}dbdef.DBI:Pg:host=localhost\\;dbname=demo_template ".
103            "${freeside_conf}dbdef.DBI:Pg:host=localhost\\;dbname=demo_$username";
104
105     open(INVOICE_FROM, ">${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/invoice_from")
106       or die "Can\'t open ${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/invoice_from: $!";
107     print INVOICE_FROM "$email\n";
108     close INVOICE_FROM;
109
110     open(LPR, ">${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/lpr")
111       or die "Can\'t open ${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/lpr: $!";
112     print LPR "mail $email";
113     close LPR;
114
115     open(FROM, ">${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/from")
116       or die "Can\'t open ${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/from: $!";
117     print FROM "$email\n";
118     close FROM;
119
120     open(TO, ">${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/to")
121       or die "Can\'t open ${freeside_conf}conf.DBI:Pg:host=localhost;dbname=demo_$username/registries/internic/to: $!";
122     print TO "$email\n";
123     close TO;
124
125     open(SECRETS, ">${freeside_conf}secrets.demo_$username")
126       or die "Can\'t open ${freeside_conf}secrets.demo_$username: $!";
127     chown scalar(getpwnam('freeside')), scalar(getgrnam('freeside')),
128           "${freeside_conf}secrets.demo_$username";
129     chmod 0600, "${freeside_conf}secrets.demo_$username";
130     print SECRETS "DBI:Pg:host=localhost;dbname=demo_$username\nfreeside\nmaelcolm\n";
131     close SECRETS;
132
133     open(MAPSECRETS, ">>${freeside_conf}mapsecrets")
134       or die "Can\'t open ${freeside_conf}mapsecrets: $!";
135     print MAPSECRETS "$username secrets.demo_$username\n";
136     close MAPSECRETS;
137
138     my $user_pw = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) );
139     my $crypt_pw =
140       crypt($user_pw,$saltset[int(rand(64))].$saltset[int(rand(64))]);
141
142     $sth = $dbh->prepare(
143       qq(UPDATE mysql_auth SET passwd = "$crypt_pw", status = "done" WHERE username = "$username")
144     );
145     $sth->execute or die $sth->errstr;
146
147     #$ENV{SMTPHOSTS} = "localhost";
148     $ENV{SMTPHOSTS} = "192.168.1.1";
149     $ENV{MAILADDRESS} = 'ivan-fsreg@sisd.com';
150     $ENV{TZ} = "PST8PDT";
151     $header = Mail::Header->new( [
152       'From: ivan-fsreg@sisd.com',
153       "To: $email",
154       'Bcc: ivan-fsreg_bcc@sisd.com',
155       'Sender: ivan-fsreg@sisd.com',
156       'Reply-To: ivan-fsreg@sisd.com',
157       #'Date: '. time2str("%a, %d %b %Y %X %z", time ),
158       'Date: '. time2str("%a, %d %b %Y %X ", time ). "-0800",
159       'Subject: Freeside demo information',
160     ] );
161     $msg = Mail::Internet->new(
162       'Header' => $header,
163       'Body' => [
164     "Hello,\n",
165     "\n",
166     "Your sample Freeside database has been setup.\n",
167     "\n",
168     "Point your web browswer at http://freeside.sisd.com/ and use the following\n",
169     "authentication information:\n",
170     "\n",
171     "Username: $username\n",
172     "Password: $user_pw\n",
173     "\n",
174     "-- \n",
175     "ivan\n",
176                 ]
177     );
178     $msg->smtpsend or die "Can\'t send registration email!";
179
180   }
181
182   $SIG{HUP} = 'DEFAULT';
183   $SIG{INT} = 'DEFAULT';
184   $SIG{QUIT} = 'DEFAULT';
185   $SIG{TERM} = 'DEFAULT';
186   $SIG{TSTP} = 'DEFAULT';
187   $SIG{PIPE} = 'DEFAULT';
188
189   sleep 5;
190
191 }
192