summaryrefslogtreecommitdiff
path: root/fs_webdemo/registerd.Pg
blob: f166846b7d343eeb7f95b9e5db8bdce801968a42 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
#!/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, "</usr/local/etc/freeside/demo_template.Pg")
      or do { &myerr("/usr/local/etc/freeside/demo_template.Pg: $!"); next; };
    while(<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;
};