3 # $Id: fs-setup,v 1.30 2000-12-01 18:33:32 ivan Exp $
5 # ivan@sisd.com 97-nov-8,9
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
11 # pulled modified version back out of register.cgi ivan@sisd.com 98-feb-21
13 # removed extraneous sample data ivan@sisd.com 98-mar-23
15 # gained the big hash from dbdef.pm, dbdef.pm usage rewrite ivan@sisd.com
16 # 98-apr-19 - 98-may-11 plus
18 # finished up ivan@sisd.com 98-jun-1
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
25 # ss is 11 chars ivan@sisd.com 98-jul-20
27 # setup of arbitrary radius fields ivan@sisd.com 98-aug-9
29 # ouch, removed index on company name that wasn't supposed to be there
30 # ivan@sisd.com 98-sep-4
32 # fix radius attributes ivan@sisd.com 98-sep-27
35 # Revision 1.30 2000-12-01 18:33:32 ivan
38 # Revision 1.29 2000/11/07 15:00:37 ivan
41 # Revision 1.28 2000/10/30 10:47:26 ivan
42 # nas.last can't be defined NULL if indexed
44 # Revision 1.26 2000/07/06 08:57:27 ivan
45 # support for radius check attributes (except importing). poorly documented.
47 # Revision 1.25 2000/06/29 12:00:49 ivan
48 # support for pre-encrypted md5 passwords.
50 # Revision 1.24 2000/03/02 07:44:07 ivan
51 # typo forgot closing '
53 # Revision 1.23 2000/02/03 05:16:52 ivan
54 # beginning of DNS and Apache support
56 # Revision 1.22 2000/01/31 05:22:23 ivan
57 # prepaid "internet cards"
59 # Revision 1.21 2000/01/30 06:03:26 ivan
60 # postgres 6.5 finally supports decimal(10,2)
62 # Revision 1.20 2000/01/28 22:53:33 ivan
63 # track full phone number
65 # Revision 1.19 1999/07/29 08:50:35 ivan
66 # wrong type for cust_pay_batch.exp
68 # Revision 1.18 1999/04/15 22:46:30 ivan
71 # Revision 1.17 1999/04/14 07:58:39 ivan
72 # export getsecrets from FS::UID instead of calling it explicitly
74 # Revision 1.16 1999/02/28 19:44:16 ivan
75 # constructors s/create/new/ pointed out by "Bao C. Ha" <bao@hacom.net>
77 # Revision 1.15 1999/02/27 21:06:21 ivan
78 # cust_main.paydate should be varchar(10), not @date_type ; problem reported
79 # by Ben Leibig <leibig@colorado.edu>
81 # Revision 1.14 1999/02/07 09:59:14 ivan
82 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
84 # Revision 1.13 1999/02/04 06:09:23 ivan
87 # Revision 1.12 1999/02/03 10:42:27 ivan
88 # *** empty log message ***
90 # Revision 1.11 1999/01/17 03:11:52 ivan
91 # remove preliminary completehost changes
93 # Revision 1.10 1998/12/16 06:05:38 ivan
94 # add table cust_main_invoice
96 # Revision 1.9 1998/12/15 04:36:29 ivan
99 # Revision 1.8 1998/12/15 04:33:27 ivan
100 # dies if it isn't running as the freeside user
102 # Revision 1.7 1998/11/18 09:01:31 ivan
105 # Revision 1.6 1998/11/15 13:18:02 ivan
108 # Revision 1.5 1998/11/15 09:43:03 ivan
109 # update for new config file syntax, new adminsuidsetup
111 # Revision 1.4 1998/10/22 15:51:23 ivan
112 # also varchar with no length specified - postgresql fix broke mysql.
114 # Revision 1.3 1998/10/22 15:46:28 ivan
115 # now smallint is illegal, so remove that too.
118 #to delay loading dbdef until we're ready
119 BEGIN { $FS::Record::setup_hack = 1; }
124 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
126 use FS::cust_main_county;
128 die "Not running uid freeside!" unless checkeuid();
130 my $user = shift or die &usage;
133 #needs to match FS::Record
134 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
138 print "\nEnter the maximum username length: ";
139 my($username_len)=&getvalue;
141 print "\n\n", <<END, ":";
142 Freeside tracks the RADIUS attributes User-Name, check attribute Password and
143 reply attribute Framed-IP-Address for each user. You can specify additional
144 check and reply attributes. First enter any additional RADIUS check attributes
145 you need to track for each user, separated by whitespace.
147 my @check_attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
149 print "\n\n", <<END, ":";
150 Now enter any additional reply attributes you need to track for each user,
151 separated by whitespace.
153 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
156 my($x)=scalar(<STDIN>);
163 my($char_d) = 80; #default maxlength for text fields
165 #my(@date_type) = ( 'timestamp', '', '' );
166 my(@date_type) = ( 'int', 'NULL', '' );
167 my(@perl_type) = ( 'varchar', 'NULL', 255 );
168 my @money_type = ( 'decimal', '', '10,2' );
171 # create a dbdef object from the old data structure
174 my(%tables)=&tables_hash_hack;
176 #turn it into objects
177 my($dbdef) = new FS::dbdef ( map {
179 while (@{$tables{$_}{'columns'}}) {
180 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
181 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
183 FS::dbdef_table->new(
185 $tables{$_}{'primary_key'},
186 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
187 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
188 FS::dbdef_unique->new($tables{$_}{'unique'}),
189 FS::dbdef_index->new($tables{$_}{'index'}),
194 #add radius attributes to svc_acct
196 my($svc_acct)=$dbdef->table('svc_acct');
199 foreach $attribute (@attributes) {
200 $svc_acct->addcolumn ( new FS::dbdef_column (
201 'radius_'. $attribute,
208 foreach $attribute (@check_attributes) {
209 $svc_acct->addcolumn( new FS::dbdef_column (
217 #make part_svc table (but now as object)
219 my($part_svc)=$dbdef->table('part_svc');
221 #because of svc_acct_pop
222 #foreach (grep /^svc_/, $dbdef->tables) {
223 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
224 foreach (qw(svc_acct svc_acct_sm svc_domain svc_www)) {
225 my($table)=$dbdef->table($_);
227 foreach $col ( $table->columns ) {
228 next if $col =~ /^svcnum$/;
229 $part_svc->addcolumn( new FS::dbdef_column (
230 $table->name. '__' . $table->column($col)->name,
231 'varchar', #$table->column($col)->type,
233 $char_d, #$table->column($col)->length,
235 $part_svc->addcolumn ( new FS::dbdef_column (
236 $table->name. '__'. $table->column($col)->name . "_flag",
245 $dbdef->save($dbdef_file);
246 &FS::Record::reload_dbdef($dbdef_file);
252 my($dbh)=adminsuidsetup $user;
258 foreach ($dbdef->tables) {
259 my($table)=$dbdef->table($_);
260 print "Creating $_...";
265 foreach $statement ($table->sql_create_table(datasrc)) {
266 #print $statement, "\n";
267 $dbh->do( $statement )
268 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
274 #not really sample data (and shouldn't default to US)
280 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
281 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
282 SC SD TN TX UT VT VI VA WA WV WI WY AE AA AP
284 my($cust_main_county)=new FS::cust_main_county({
290 $error=$cust_main_county->insert;
291 die $error if $error;
294 #AU "offical" state codes ala mark.williamson@ebbs.com.au (Mark Williamson)
296 VIC NSW NT QLD TAS ACT WA SA
298 my($cust_main_county)=new FS::cust_main_county({
304 $error=$cust_main_county->insert;
305 die $error if $error;
308 #ISO 2-letter country codes (same as country TLDs) except US and AU
310 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
311 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
312 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
313 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
314 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
315 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
316 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
317 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
318 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
321 my($cust_main_county)=new FS::cust_main_county({
326 $error=$cust_main_county->insert;
327 die $error if $error;
330 $dbh->disconnect or die $dbh->errstr;
333 die "Usage:\n fs-setup user\n";
337 # Now it becomes an object. much better.
339 sub tables_hash_hack {
341 #note that s/(date|change)/_$1/; to avoid keyword conflict.
342 #put a kludge in FS::Record to catch this or? (pry need some date-handling
345 my(%tables)=( #yech.}
349 'agentnum', 'int', '', '',
350 'agent', 'varchar', '', $char_d,
351 'typenum', 'int', '', '',
352 'freq', 'int', 'NULL', '',
355 'primary_key' => 'agentnum',
357 'index' => [ ['typenum'] ],
362 'typenum', 'int', '', '',
363 'atype', 'varchar', '', $char_d,
365 'primary_key' => 'typenum',
372 'typenum', 'int', '', '',
373 'pkgpart', 'int', '', '',
376 'unique' => [ ['typenum', 'pkgpart'] ],
377 'index' => [ ['typenum'] ],
382 'invnum', 'int', '', '',
383 'custnum', 'int', '', '',
385 'charged', @money_type,
387 'printed', 'int', '', '',
389 'primary_key' => 'invnum',
391 'index' => [ ['custnum'] ],
396 'pkgnum', 'int', '', '',
397 'invnum', 'int', '', '',
398 'setup', @money_type,
399 'recur', @money_type,
404 'unique' => [ ['pkgnum', 'invnum'] ],
405 'index' => [ ['invnum'] ],
410 'crednum', 'int', '', '',
411 'custnum', 'int', '', '',
413 'amount', @money_type,
414 'credited', @money_type,
415 'otaker', 'varchar', '', 8,
416 'reason', 'varchar', '', 255,
418 'primary_key' => 'crednum',
420 'index' => [ ['custnum'] ],
425 'custnum', 'int', '', '',
426 'agentnum', 'int', '', '',
427 # 'titlenum', 'int', 'NULL', '',
428 'last', 'varchar', '', $char_d,
429 # 'middle', 'varchar', 'NULL', $char_d,
430 'first', 'varchar', '', $char_d,
431 'ss', 'char', 'NULL', 11,
432 'company', 'varchar', 'NULL', $char_d,
433 'address1', 'varchar', '', $char_d,
434 'address2', 'varchar', 'NULL', $char_d,
435 'city', 'varchar', '', $char_d,
436 'county', 'varchar', 'NULL', $char_d,
437 'state', 'varchar', 'NULL', $char_d,
438 'zip', 'varchar', '', 10,
439 'country', 'char', '', 2,
440 'daytime', 'varchar', 'NULL', 20,
441 'night', 'varchar', 'NULL', 20,
442 'fax', 'varchar', 'NULL', 12,
443 'payby', 'char', '', 4,
444 'payinfo', 'varchar', 'NULL', 16,
445 #'paydate', @date_type,
446 'paydate', 'varchar', 'NULL', 10,
447 'payname', 'varchar', 'NULL', $char_d,
448 'tax', 'char', 'NULL', 1,
449 'otaker', 'varchar', '', 8,
450 'refnum', 'int', '', '',
452 'primary_key' => 'custnum',
454 #'index' => [ ['last'], ['company'] ],
455 'index' => [ ['last'], ],
458 'cust_main_invoice' => {
460 'destnum', 'int', '', '',
461 'custnum', 'int', '', '',
462 'dest', 'varchar', '', $char_d,
464 'primary_key' => 'destnum',
466 'index' => [ ['custnum'], ],
469 'cust_main_county' => { #county+state+country are checked off the
470 #cust_main_county for validation and to provide
473 'taxnum', 'int', '', '',
474 'state', 'varchar', 'NULL', $char_d,
475 'county', 'varchar', 'NULL', $char_d,
476 'country', 'char', '', 2,
477 'tax', 'real', '', '', #tax %
479 'primary_key' => 'taxnum',
481 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
487 'paynum', 'int', '', '',
488 'invnum', 'int', '', '',
491 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
492 # payment type table.
493 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
494 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
496 'primary_key' => 'paynum',
498 'index' => [ ['invnum'] ],
501 'cust_pay_batch' => { #what's this used for again? list of customers
502 #in current CARD batch? (necessarily CARD?)
504 'invnum', 'int', '', '',
505 'custnum', 'int', '', '',
506 'last', 'varchar', '', $char_d,
507 'first', 'varchar', '', $char_d,
508 'address1', 'varchar', '', $char_d,
509 'address2', 'varchar', 'NULL', $char_d,
510 'city', 'varchar', '', $char_d,
511 'state', 'varchar', '', $char_d,
512 'zip', 'varchar', '', 10,
513 'country', 'char', '', 2,
514 'trancode', 'int', '', '',
515 'cardnum', 'varchar', '', 16,
517 'exp', 'varchar', '', 11,
518 'payname', 'varchar', 'NULL', $char_d,
519 'amount', @money_type,
523 'index' => [ ['invnum'], ['custnum'] ],
528 'pkgnum', 'int', '', '',
529 'custnum', 'int', '', '',
530 'pkgpart', 'int', '', '',
531 'otaker', 'varchar', '', 8,
535 'cancel', @date_type,
536 'expire', @date_type,
538 'primary_key' => 'pkgnum',
540 'index' => [ ['custnum'] ],
545 'refundnum', 'int', '', '',
546 'crednum', 'int', '', '',
548 'refund', @money_type,
549 'otaker', 'varchar', '', 8,
550 'reason', 'varchar', '', $char_d,
551 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
552 # into payment type table.
553 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
555 'primary_key' => 'refundnum',
557 'index' => [ ['crednum'] ],
562 'svcnum', 'int', '', '',
563 'pkgnum', 'int', '', '',
564 'svcpart', 'int', '', '',
566 'primary_key' => 'svcnum',
568 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
573 'pkgpart', 'int', '', '',
574 'pkg', 'varchar', '', $char_d,
575 'comment', 'varchar', '', $char_d,
577 'freq', 'int', '', '', #billing frequency (months)
580 'primary_key' => 'pkgpart',
587 # 'titlenum', 'int', '', '',
588 # 'title', 'varchar', '', $char_d,
590 # 'primary_key' => 'titlenum',
591 # 'unique' => [ [] ],
597 'pkgpart', 'int', '', '',
598 'svcpart', 'int', '', '',
599 'quantity', 'int', '', '',
602 'unique' => [ ['pkgpart', 'svcpart'] ],
603 'index' => [ ['pkgpart'] ],
608 'refnum', 'int', '', '',
609 'referral', 'varchar', '', $char_d,
611 'primary_key' => 'refnum',
618 'svcpart', 'int', '', '',
619 'svc', 'varchar', '', $char_d,
620 'svcdb', 'varchar', '', $char_d,
622 'primary_key' => 'svcpart',
627 #(this should be renamed to part_pop)
630 'popnum', 'int', '', '',
631 'city', 'varchar', '', $char_d,
632 'state', 'varchar', '', $char_d,
634 'exch', 'char', '', 3,
635 'loc', 'char', 'NULL', 4, #NULL for legacy purposes
637 'primary_key' => 'popnum',
644 'svcnum', 'int', '', '',
645 'username', 'varchar', '', $username_len, #unique (& remove dup code)
646 '_password', 'varchar', '', 50, #13 for encryped pw's plus ' *SUSPENDED* (mp5 passwords can be 34)
647 'popnum', 'int', 'NULL', '',
648 'uid', 'int', 'NULL', '',
649 'gid', 'int', 'NULL', '',
650 'finger', 'varchar', 'NULL', $char_d,
651 'dir', 'varchar', 'NULL', $char_d,
652 'shell', 'varchar', 'NULL', $char_d,
653 'quota', 'varchar', 'NULL', $char_d,
654 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
656 'primary_key' => 'svcnum',
658 'index' => [ ['username'] ],
663 'svcnum', 'int', '', '',
664 'domsvc', 'int', '', '',
665 'domuid', 'int', '', '',
666 'domuser', 'varchar', '', $char_d,
668 'primary_key' => 'svcnum',
670 'index' => [ ['domsvc'], ['domuid'] ],
675 # 'svcnum', 'int', '', '',
676 # 'amount', @money_type,
678 # 'primary_key' => 'svcnum',
679 # 'unique' => [ [] ],
685 'svcnum', 'int', '', '',
686 'domain', 'varchar', '', $char_d,
688 'primary_key' => 'svcnum',
689 'unique' => [ ['domain'] ],
695 'recnum', 'int', '', '',
696 'svcnum', 'int', '', '',
697 'reczone', 'varchar', '', $char_d,
698 'recaf', 'char', '', 2,
699 'rectype', 'char', '', 5,
700 'recdata', 'varchar', '', $char_d,
702 'primary_key' => 'recnum',
704 'index' => [ ['svcnum'] ],
709 'svcnum', 'int', '', '',
710 'recnum', 'int', '', '',
711 'usersvc', 'int', '', '',
713 'primary_key' => 'svcnum',
720 # 'svcnum', 'int', '', '',
721 # 'svcnum', 'int', '', '',
722 # 'svcnum', 'int', '', '',
723 # 'worker', 'varchar', '', $char_d,
724 # '_date', @date_type,
726 # 'primary_key' => 'svcnum',
727 # 'unique' => [ [] ],
733 'prepaynum', 'int', '', '',
734 'identifier', 'varchar', '', $char_d,
735 'amount', @money_type,
737 'primary_key' => 'prepaynum',
738 'unique' => [ ['identifier'] ],
744 'portnum', 'int', '', '',
745 'ip', 'varchar', NULL, 15,
746 'nasport', 'int', NULL, '',
747 'nasnum', 'int', '', '',
749 'primary_key' => 'portnum',
756 'nasnum', 'int', '', '',
757 'nas', 'varchar', '', $char_d,
758 'nasip', 'varchar', '', 15,
759 'nasfqdn', 'varchar', '', $char_d,
762 'primary_key' => 'nasnum',
763 'unique' => [ [ 'nas' ], [ 'nasip' ] ],
764 'index' => [ [ 'last' ] ],
769 'sessionnum', 'int', '', '',
770 'portnum', 'int', '', '',
771 'svcnum', 'int', '', '',
773 'logout', @date_type,
775 'primary_key' => 'sessionnum',
777 'index' => [ [ 'portnum' ] ],