export getsecrets from FS::UID instead of calling it explicitly
[freeside.git] / bin / fs-setup
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: fs-setup,v 1.17 1999-04-14 07:58:39 ivan Exp $
4 #
5 # ivan@sisd.com 97-nov-8,9
6 #
7 # agent_type and type_pkgs added.
8 # (index need to be declared, & primary keys shoudln't have mysql syntax)
9 # ivan@sisd.com 97-nov-13
10 #
11 # pulled modified version back out of register.cgi ivan@sisd.com 98-feb-21
12 #
13 # removed extraneous sample data ivan@sisd.com 98-mar-23
14 #
15 # gained the big hash from dbdef.pm, dbdef.pm usage rewrite ivan@sisd.com
16 # 98-apr-19 - 98-may-11 plus
17 #
18 # finished up ivan@sisd.com 98-jun-1
19 #
20 # part_svc fields are all forced NULL, not the opposite
21 # hmm: also are forced varchar($char_d) as fixed '0' for things like
22 # uid is Not Good.  will this break anything else?
23 # ivan@sisd.com 98-jun-29
24 #
25 # ss is 11 chars ivan@sisd.com 98-jul-20
26 #
27 # setup of arbitrary radius fields ivan@sisd.com 98-aug-9
28 #
29 # ouch, removed index on company name that wasn't supposed to be there
30 # ivan@sisd.com 98-sep-4
31 #
32 # fix radius attributes ivan@sisd.com 98-sep-27
33 #
34 # $Log: fs-setup,v $
35 # Revision 1.17  1999-04-14 07:58:39  ivan
36 # export getsecrets from FS::UID instead of calling it explicitly
37 #
38 # Revision 1.16  1999/02/28 19:44:16  ivan
39 # constructors s/create/new/ pointed out by "Bao C. Ha" <bao@hacom.net>
40 #
41 # Revision 1.15  1999/02/27 21:06:21  ivan
42 # cust_main.paydate should be varchar(10), not @date_type ; problem reported
43 # by Ben Leibig <leibig@colorado.edu>
44 #
45 # Revision 1.14  1999/02/07 09:59:14  ivan
46 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
47 #
48 # Revision 1.13  1999/02/04 06:09:23  ivan
49 # add AU provences
50 #
51 # Revision 1.12  1999/02/03 10:42:27  ivan
52 # *** empty log message ***
53 #
54 # Revision 1.11  1999/01/17 03:11:52  ivan
55 # remove preliminary completehost changes
56 #
57 # Revision 1.10  1998/12/16 06:05:38  ivan
58 # add table cust_main_invoice
59 #
60 # Revision 1.9  1998/12/15 04:36:29  ivan
61 # s/croak/die/; #oops
62 #
63 # Revision 1.8  1998/12/15 04:33:27  ivan
64 # dies if it isn't running as the freeside user
65 #
66 # Revision 1.7  1998/11/18 09:01:31  ivan
67 # i18n! i18n!
68 #
69 # Revision 1.6  1998/11/15 13:18:02  ivan
70 # remove debugging
71 #
72 # Revision 1.5  1998/11/15 09:43:03  ivan
73 # update for new config file syntax, new adminsuidsetup
74 #
75 # Revision 1.4  1998/10/22 15:51:23  ivan
76 # also varchar with no length specified - postgresql fix broke mysql.
77 #
78 # Revision 1.3  1998/10/22 15:46:28  ivan
79 # now smallint is illegal, so remove that too.
80 #
81
82 #to delay loading dbdef until we're ready
83 BEGIN { $FS::Record::setup_hack = 1; }
84
85 use strict;
86 use DBI;
87 use FS::dbdef;
88 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
89 use FS::Record;
90 use FS::cust_main_county;
91
92 die "Not running uid freeside!" unless checkeuid();
93
94 my $user = shift or die &usage;
95 getsecrets($user);
96
97 #needs to match FS::Record
98 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
99
100 ###
101
102 print "\nEnter the maximum username length: ";
103 my($username_len)=&getvalue;
104
105 print "\n\n", <<END, ":";
106 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
107 for each user.  Enter any additional RADIUS attributes you need to track for
108 each user, separated by whitespace.
109 END
110 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
111
112 sub getvalue {
113   my($x)=scalar(<STDIN>);
114   chop $x;
115   $x;
116 }
117
118 ###
119
120 my($char_d) = 80; #default maxlength for text fields
121
122 #my(@date_type)  = ( 'timestamp', '', ''     );
123 my(@date_type)  = ( 'int', 'NULL', ''     );
124 my(@perl_type) = ( 'varchar', 'NULL', 255  ); 
125 my(@money_type);
126 if (datasrc =~ m/Pg/) { #Pg can't do decimal(10,2)
127   @money_type = ( 'money',   '', '' );
128 } else {
129   @money_type = ( 'decimal',   '', '10,2' );
130 }
131
132 ###
133 # create a dbdef object from the old data structure
134 ###
135
136 my(%tables)=&tables_hash_hack;
137
138 #turn it into objects
139 my($dbdef) = new FS::dbdef ( map {  
140   my(@columns);
141   while (@{$tables{$_}{'columns'}}) {
142     my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
143     push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
144   }
145   FS::dbdef_table->new(
146     $_,
147     $tables{$_}{'primary_key'},
148     #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
149     #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
150     FS::dbdef_unique->new($tables{$_}{'unique'}),
151     FS::dbdef_index->new($tables{$_}{'index'}),
152     @columns,
153   );
154 } (keys %tables) );
155
156 #add radius attributes to svc_acct
157
158 my($svc_acct)=$dbdef->table('svc_acct');
159
160 my($attribute);
161 foreach $attribute (@attributes) {
162   $svc_acct->addcolumn ( new FS::dbdef_column (
163     'radius_'. $attribute,
164     'varchar',
165     'NULL',
166     $char_d,
167   ));
168 }
169
170 #make part_svc table (but now as object)
171
172 my($part_svc)=$dbdef->table('part_svc');
173
174 #because of svc_acct_pop
175 #foreach (grep /^svc_/, $dbdef->tables) { 
176 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
177 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
178   my($table)=$dbdef->table($_);
179   my($col);
180   foreach $col ( $table->columns ) {
181     next if $col =~ /^svcnum$/;
182     $part_svc->addcolumn( new FS::dbdef_column (
183       $table->name. '__' . $table->column($col)->name,
184       'varchar', #$table->column($col)->type, 
185       'NULL',
186       $char_d, #$table->column($col)->length,
187     ));
188     $part_svc->addcolumn ( new FS::dbdef_column (
189       $table->name. '__'. $table->column($col)->name . "_flag",
190       'char',
191       'NULL',
192       1,
193     ));
194   }
195 }
196
197 #important
198 $dbdef->save($dbdef_file);
199 &FS::Record::reload_dbdef($dbdef_file);
200
201 ###
202 # create 'em
203 ###
204
205 my($dbh)=adminsuidsetup $user;
206
207 #create tables
208 $|=1;
209
210 my($table);
211 foreach  ($dbdef->tables) {
212   my($table)=$dbdef->table($_);
213   print "Creating $_...";
214
215   my($statement);
216
217   #create table
218   foreach $statement ($table->sql_create_table(datasrc)) {
219     #print $statement, "\n"; 
220     $dbh->do( $statement )
221       or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
222   }
223
224   print "\n";
225 }
226
227 #not really sample data (and shouldn't default to US)
228
229 #cust_main_county
230
231 #USPS state codes
232 foreach ( qw(
233 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
234 ME MH MD MA MI MN MS MO MT NC ND NE NH NJ NM NV NY MP OH OK OR PA PW PR RI 
235 SC SD TN TX TT UT VT VI VA WA WV WI WY AE AA AP
236 ) ) {
237   my($cust_main_county)=new FS::cust_main_county({
238     'state' => $_,
239     'tax'   => 0,
240     'country' => 'US',
241   });  
242   my($error);
243   $error=$cust_main_county->insert;
244   die $error if $error;
245 }
246
247 #AU "offical" state codes ala mark.williamson@ebbs.com.au (Mark Williamson)
248 foreach ( qw(
249 VIC NSW NT QLD TAS ACT WA SA
250 ) ) {
251   my($cust_main_county)=new FS::cust_main_county({
252     'state' => $_,
253     'tax'   => 0,
254     'country' => 'AU',
255   });  
256   my($error);
257   $error=$cust_main_county->insert;
258   die $error if $error;
259 }
260
261 #ISO 2-letter country codes (same as country TLDs) except US and AU
262 foreach ( qw(
263 AF AL DZ AS AD AO AI AQ AG AR AM AW AT AZ BS BH BD BB BY BE BZ BJ BM BT BO
264 BA BW BV BR IO BN BG BF BI KH CM CA CV KY CF TD CL CN CX CC CO KM CG CK CR CI
265 HR CU CY CZ DK DJ DM DO TP EC EG SV GQ ER EE ET FK FO FJ FI FR FX GF PF TF GA
266 GM GE DE GH GI GR GL GD GP GU GT GN GW GY HT HM HN HK HU IS IN ID IR IQ IE IL
267 IT JM JP JO KZ KE KI KP KR KW KG LA LV LB LS LR LY LI LT LU MO MK MG MW MY MV
268 ML MT MH MQ MR MU YT MX FM MD MC MN MS MA MZ MM NA NR NP NL AN NC NZ NI NE NG
269 NU NF MP NO OM PK PW PA PG PY PE PH PN PL PT PR QA RE RO RU RW KN LC VC WS SM
270 ST SA SN SC SL SG SK SI SB SO ZA GS ES LK SH PM SD SR SJ SZ SE CH SY TW TJ TZ
271 TH TG TK TO TT TN TR TM TC TV UG UA AE GB UM UY UZ VU VA VE VN VG VI WF EH
272 YE YU ZR ZM ZW
273 ) ) {
274   my($cust_main_county)=new FS::cust_main_county({
275     'tax'   => 0,
276     'country' => $_,
277   });  
278   my($error);
279   $error=$cust_main_county->insert;
280   die $error if $error;
281 }
282
283 $dbh->disconnect or die $dbh->errstr;
284
285 sub usage {
286   die "Usage:\n  fs-setup user\n"; 
287 }
288
289 ###
290 # Now it becomes an object.  much better.
291 ###
292 sub tables_hash_hack {
293
294   #note that s/(date|change)/_$1/; to avoid keyword conflict.
295   #put a kludge in FS::Record to catch this or? (pry need some date-handling
296   #stuff anyway also)
297
298   my(%tables)=( #yech.}
299
300     'agent' => {
301       'columns' => [
302         'agentnum', 'int',            '',     '',
303         'agent',    'varchar',           '',     $char_d,
304         'typenum',  'int',            '',     '',
305         'freq',     'int',       'NULL', '',
306         'prog',     @perl_type,
307       ],
308       'primary_key' => 'agentnum',
309       'unique' => [ [] ],
310       'index' => [ ['typenum'] ],
311     },
312
313     'agent_type' => {
314       'columns' => [
315         'typenum',   'int',  '', '',
316         'atype',     'varchar', '', $char_d,
317       ],
318       'primary_key' => 'typenum',
319       'unique' => [ [] ],
320       'index' => [ [] ],
321     },
322
323     'type_pkgs' => {
324       'columns' => [
325         'typenum',   'int',  '', '',
326         'pkgpart',   'int',  '', '',
327       ],
328       'primary_key' => '',
329       'unique' => [ ['typenum', 'pkgpart'] ],
330       'index' => [ ['typenum'] ],
331     },
332
333     'cust_bill' => {
334       'columns' => [
335         'invnum',    'int',  '', '',
336         'custnum',   'int',  '', '',
337         '_date',     @date_type,
338         'charged',   @money_type,
339         'owed',      @money_type,
340         'printed',   'int',  '', '',
341       ],
342       'primary_key' => 'invnum',
343       'unique' => [ [] ],
344       'index' => [ ['custnum'] ],
345     },
346
347     'cust_bill_pkg' => {
348       'columns' => [
349         'pkgnum',  'int', '', '',
350         'invnum',  'int', '', '',
351         'setup',   @money_type,
352         'recur',   @money_type,
353         'sdate',   @date_type,
354         'edate',   @date_type,
355       ],
356       'primary_key' => '',
357       'unique' => [ ['pkgnum', 'invnum'] ],
358       'index' => [ ['invnum'] ],
359     },
360
361     'cust_credit' => {
362       'columns' => [
363         'crednum',  'int', '', '',
364         'custnum',  'int', '', '',
365         '_date',    @date_type,
366         'amount',   @money_type,
367         'credited', @money_type,
368         'otaker',   'varchar', '', 8,
369         'reason',   'varchar', '', 255,
370       ],
371       'primary_key' => 'crednum',
372       'unique' => [ [] ],
373       'index' => [ ['custnum'] ],
374     },
375
376     'cust_main' => {
377       'columns' => [
378         'custnum',  'int',  '',     '',
379         'agentnum', 'int',  '',     '',
380 #        'titlenum', 'int',  'NULL',   '',
381         'last',     'varchar', '',     $char_d,
382 #        'middle',   'varchar', 'NULL', $char_d,
383         'first',    'varchar', '',     $char_d,
384         'ss',       'char', 'NULL', 11,
385         'company',  'varchar', 'NULL', $char_d,
386         'address1', 'varchar', '',     $char_d,
387         'address2', 'varchar', 'NULL', $char_d,
388         'city',     'varchar', '',     $char_d,
389         'county',   'varchar', 'NULL', $char_d,
390         'state',    'varchar', 'NULL', $char_d,
391         'zip',      'varchar', '',     10,
392         'country',  'char', '',     2,
393         'daytime',  'varchar', 'NULL', 20,
394         'night',    'varchar', 'NULL', 20,
395         'fax',      'varchar', 'NULL', 12,
396         'payby',    'char', '',     4,
397         'payinfo',  'varchar', 'NULL', 16,
398         #'paydate',  @date_type,
399         'paydate',  'varchar', 'NULL', 10,
400         'payname',  'varchar', 'NULL', $char_d,
401         'tax',      'char', 'NULL', 1,
402         'otaker',   'varchar', '',     8,
403         'refnum',   'int',  '',     '',
404       ],
405       'primary_key' => 'custnum',
406       'unique' => [ [] ],
407       #'index' => [ ['last'], ['company'] ],
408       'index' => [ ['last'], ],
409     },
410
411     'cust_main_invoice' => {
412       'columns' => [
413         'destnum',  'int',  '',     '',
414         'custnum',  'int',  '',     '',
415         'dest',     'varchar', '',  $char_d,
416       ],
417       'primary_key' => 'destnum',
418       'unique' => [ [] ],
419       'index' => [ ['custnum'], ],
420     },
421
422     'cust_main_county' => { #county+state+country are checked off the
423                             #cust_main_county for validation and to provide
424                             # a tax rate.
425       'columns' => [
426         'taxnum',   'int',   '',    '',
427         'state',    'varchar',  'NULL',    $char_d,
428         'county',   'varchar',  'NULL',    $char_d,
429         'country',  'char',  '', 2, 
430         'tax',      'real',  '',    '', #tax %
431       ],
432       'primary_key' => 'taxnum',
433       'unique' => [ [] ],
434   #    'unique' => [ ['taxnum'], ['state', 'county'] ],
435       'index' => [ [] ],
436     },
437
438     'cust_pay' => {
439       'columns' => [
440         'paynum',   'int',    '',   '',
441         'invnum',   'int',    '',   '',
442         'paid',     @money_type,
443         '_date',    @date_type,
444         'payby',    'char',   '',     4, # CARD/BILL/COMP, should be index into
445                                          # payment type table.
446         'payinfo',  'varchar',   'NULL', 16,  #see cust_main above
447         'paybatch', 'varchar',   'NULL', $char_d, #for auditing purposes.
448       ],
449       'primary_key' => 'paynum',
450       'unique' => [ [] ],
451       'index' => [ ['invnum'] ],
452     },
453
454     'cust_pay_batch' => { #what's this used for again?  list of customers
455                           #in current CARD batch? (necessarily CARD?)
456       'columns' => [
457         'invnum',   'int',    '',   '',
458         'custnum',   'int',    '',   '',
459         'last',     'varchar', '',     $char_d,
460         'first',    'varchar', '',     $char_d,
461         'address1', 'varchar', '',     $char_d,
462         'address2', 'varchar', 'NULL', $char_d,
463         'city',     'varchar', '',     $char_d,
464         'state',    'varchar', '',     $char_d,
465         'zip',      'varchar', '',     10,
466         'country',  'char', '',     2,
467         'trancode', 'int', '', '',
468         'cardnum',  'varchar', '',     16,
469         'exp',      @date_type,
470         'payname',  'varchar', 'NULL', $char_d,
471         'amount',   @money_type,
472       ],
473       'primary_key' => '',
474       'unique' => [ [] ],
475       'index' => [ ['invnum'], ['custnum'] ],
476     },
477
478     'cust_pkg' => {
479       'columns' => [
480         'pkgnum',    'int',    '',   '',
481         'custnum',   'int',    '',   '',
482         'pkgpart',   'int',    '',   '',
483         'otaker',    'varchar', '', 8,
484         'setup',     @date_type,
485         'bill',      @date_type,
486         'susp',      @date_type,
487         'cancel',    @date_type,
488         'expire',    @date_type,
489       ],
490       'primary_key' => 'pkgnum',
491       'unique' => [ [] ],
492       'index' => [ ['custnum'] ],
493     },
494
495     'cust_refund' => {
496       'columns' => [
497         'refundnum',    'int',    '',   '',
498         'crednum',      'int',    '',   '',
499         '_date',        @date_type,
500         'refund',       @money_type,
501         'otaker',       'varchar',   '',   8,
502         'reason',       'varchar',   '',   $char_d,
503         'payby',        'char',   '',     4, # CARD/BILL/COMP, should be index
504                                              # into payment type table.
505         'payinfo',      'varchar',   'NULL', 16,  #see cust_main above
506       ],
507       'primary_key' => 'refundnum',
508       'unique' => [ [] ],
509       'index' => [ ['crednum'] ],
510     },
511
512     'cust_svc' => {
513       'columns' => [
514         'svcnum',    'int',    '',   '',
515         'pkgnum',    'int',    '',   '',
516         'svcpart',   'int',    '',   '',
517       ],
518       'primary_key' => 'svcnum',
519       'unique' => [ [] ],
520       'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
521     },
522
523     'part_pkg' => {
524       'columns' => [
525         'pkgpart',    'int',    '',   '',
526         'pkg',        'varchar',   '',   $char_d,
527         'comment',    'varchar',   '',   $char_d,
528         'setup',      @perl_type,
529         'freq',       'int', '', '',  #billing frequency (months)
530         'recur',      @perl_type,
531       ],
532       'primary_key' => 'pkgpart',
533       'unique' => [ [] ],
534       'index' => [ [] ],
535     },
536
537 #    'part_title' => {
538 #      'columns' => [
539 #        'titlenum',   'int',    '',   '',
540 #        'title',      'varchar',   '',   $char_d,
541 #      ],
542 #      'primary_key' => 'titlenum',
543 #      'unique' => [ [] ],
544 #      'index' => [ [] ],
545 #    },
546
547     'pkg_svc' => {
548       'columns' => [
549         'pkgpart',    'int',    '',   '',
550         'svcpart',    'int',    '',   '',
551         'quantity',   'int',    '',   '',
552       ],
553       'primary_key' => '',
554       'unique' => [ ['pkgpart', 'svcpart'] ],
555       'index' => [ ['pkgpart'] ],
556     },
557
558     'part_referral' => {
559       'columns' => [
560         'refnum',   'int',    '',   '',
561         'referral', 'varchar',   '',   $char_d,
562       ],
563       'primary_key' => 'refnum',
564       'unique' => [ [] ],
565       'index' => [ [] ],
566     },
567
568     'part_svc' => {
569       'columns' => [
570         'svcpart',    'int',    '',   '',
571         'svc',        'varchar',   '',   $char_d,
572         'svcdb',      'varchar',   '',   $char_d,
573       ],
574       'primary_key' => 'svcpart',
575       'unique' => [ [] ],
576       'index' => [ [] ],
577     },
578
579     #(this should be renamed to part_pop)
580     'svc_acct_pop' => {
581       'columns' => [
582         'popnum',    'int',    '',   '',
583         'city',      'varchar',   '',   $char_d,
584         'state',     'varchar',   '',   $char_d,
585         'ac',        'char',   '',   3,
586         'exch',      'char',   '',   3,
587         #rest o' number?
588       ],
589       'primary_key' => 'popnum',
590       'unique' => [ [] ],
591       'index' => [ [] ],
592     },
593
594     'svc_acct' => {
595       'columns' => [
596         'svcnum',    'int',    '',   '',
597         'username',  'varchar',   '',   $username_len, #unique (& remove dup code)
598         '_password', 'varchar',   '',   25, #13 for encryped pw's plus ' *SUSPENDED*
599         'popnum',    'int',    'NULL',   '',
600         'uid',       'int', 'NULL',   '',
601         'gid',       'int', 'NULL',   '',
602         'finger',    'varchar',   'NULL',   $char_d,
603         'dir',       'varchar',   'NULL',   $char_d,
604         'shell',     'varchar',   'NULL',   $char_d,
605         'quota',     'varchar',   'NULL',   $char_d,
606         'slipip',    'varchar',   'NULL',   15, #four TINYINTs, bah.
607       ],
608       'primary_key' => 'svcnum',
609       'unique' => [ [] ],
610       'index' => [ ['username'] ],
611     },
612
613     'svc_acct_sm' => {
614       'columns' => [
615         'svcnum',    'int',    '',   '',
616         'domsvc',    'int',    '',   '',
617         'domuid',    'int', '',   '',
618         'domuser',   'varchar',   '',   $char_d,
619       ],
620       'primary_key' => 'svcnum',
621       'unique' => [ [] ],
622       'index' => [ ['domsvc'], ['domuid'] ], 
623     },
624
625     #'svc_charge' => {
626     #  'columns' => [
627     #    'svcnum',    'int',    '',   '',
628     #    'amount',    @money_type,
629     #  ],
630     #  'primary_key' => 'svcnum',
631     #  'unique' => [ [] ],
632     #  'index' => [ [] ],
633     #},
634
635     'svc_domain' => {
636       'columns' => [
637         'svcnum',    'int',    '',   '',
638         'domain',    'varchar',    '',   $char_d,
639       ],
640       'primary_key' => 'svcnum',
641       'unique' => [ ['domain'] ],
642       'index' => [ [] ],
643     },
644
645     #'svc_wo' => {
646     #  'columns' => [
647     #    'svcnum',    'int',    '',   '',
648     #    'svcnum',    'int',    '',   '',
649     #    'svcnum',    'int',    '',   '',
650     #    'worker',    'varchar',   '',   $char_d,
651     #    '_date',     @date_type,
652     #  ],
653     #  'primary_key' => 'svcnum',
654     #  'unique' => [ [] ],
655     #  'index' => [ [] ],
656     #},
657
658   );
659
660   %tables;
661
662 }
663