3 # $Id: fs-setup,v 1.31 2000-12-01 18:34:53 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.31 2000-12-01 18:34:53 ivan
38 # Revision 1.30 2000/12/01 18:33:32 ivan
41 # Revision 1.29 2000/11/07 15:00:37 ivan
44 # Revision 1.28 2000/10/30 10:47:26 ivan
45 # nas.last can't be defined NULL if indexed
47 # Revision 1.26 2000/07/06 08:57:27 ivan
48 # support for radius check attributes (except importing). poorly documented.
50 # Revision 1.25 2000/06/29 12:00:49 ivan
51 # support for pre-encrypted md5 passwords.
53 # Revision 1.24 2000/03/02 07:44:07 ivan
54 # typo forgot closing '
56 # Revision 1.23 2000/02/03 05:16:52 ivan
57 # beginning of DNS and Apache support
59 # Revision 1.22 2000/01/31 05:22:23 ivan
60 # prepaid "internet cards"
62 # Revision 1.21 2000/01/30 06:03:26 ivan
63 # postgres 6.5 finally supports decimal(10,2)
65 # Revision 1.20 2000/01/28 22:53:33 ivan
66 # track full phone number
68 # Revision 1.19 1999/07/29 08:50:35 ivan
69 # wrong type for cust_pay_batch.exp
71 # Revision 1.18 1999/04/15 22:46:30 ivan
74 # Revision 1.17 1999/04/14 07:58:39 ivan
75 # export getsecrets from FS::UID instead of calling it explicitly
77 # Revision 1.16 1999/02/28 19:44:16 ivan
78 # constructors s/create/new/ pointed out by "Bao C. Ha" <bao@hacom.net>
80 # Revision 1.15 1999/02/27 21:06:21 ivan
81 # cust_main.paydate should be varchar(10), not @date_type ; problem reported
82 # by Ben Leibig <leibig@colorado.edu>
84 # Revision 1.14 1999/02/07 09:59:14 ivan
85 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
87 # Revision 1.13 1999/02/04 06:09:23 ivan
90 # Revision 1.12 1999/02/03 10:42:27 ivan
91 # *** empty log message ***
93 # Revision 1.11 1999/01/17 03:11:52 ivan
94 # remove preliminary completehost changes
96 # Revision 1.10 1998/12/16 06:05:38 ivan
97 # add table cust_main_invoice
99 # Revision 1.9 1998/12/15 04:36:29 ivan
100 # s/croak/die/; #oops
102 # Revision 1.8 1998/12/15 04:33:27 ivan
103 # dies if it isn't running as the freeside user
105 # Revision 1.7 1998/11/18 09:01:31 ivan
108 # Revision 1.6 1998/11/15 13:18:02 ivan
111 # Revision 1.5 1998/11/15 09:43:03 ivan
112 # update for new config file syntax, new adminsuidsetup
114 # Revision 1.4 1998/10/22 15:51:23 ivan
115 # also varchar with no length specified - postgresql fix broke mysql.
117 # Revision 1.3 1998/10/22 15:46:28 ivan
118 # now smallint is illegal, so remove that too.
121 #to delay loading dbdef until we're ready
122 BEGIN { $FS::Record::setup_hack = 1; }
127 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
129 use FS::cust_main_county;
131 die "Not running uid freeside!" unless checkeuid();
133 my $user = shift or die &usage;
136 #needs to match FS::Record
137 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
141 print "\nEnter the maximum username length: ";
142 my($username_len)=&getvalue;
144 print "\n\n", <<END, ":";
145 Freeside tracks the RADIUS attributes User-Name, check attribute Password and
146 reply attribute Framed-IP-Address for each user. You can specify additional
147 check and reply attributes. First enter any additional RADIUS check attributes
148 you need to track for each user, separated by whitespace.
150 my @check_attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
152 print "\n\n", <<END, ":";
153 Now enter any additional reply attributes you need to track for each user,
154 separated by whitespace.
156 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
159 my($x)=scalar(<STDIN>);
166 my($char_d) = 80; #default maxlength for text fields
168 #my(@date_type) = ( 'timestamp', '', '' );
169 my(@date_type) = ( 'int', 'NULL', '' );
170 my(@perl_type) = ( 'varchar', 'NULL', 255 );
171 my @money_type = ( 'decimal', '', '10,2' );
174 # create a dbdef object from the old data structure
177 my(%tables)=&tables_hash_hack;
179 #turn it into objects
180 my($dbdef) = new FS::dbdef ( map {
182 while (@{$tables{$_}{'columns'}}) {
183 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
184 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
186 FS::dbdef_table->new(
188 $tables{$_}{'primary_key'},
189 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
190 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
191 FS::dbdef_unique->new($tables{$_}{'unique'}),
192 FS::dbdef_index->new($tables{$_}{'index'}),
197 #add radius attributes to svc_acct
199 my($svc_acct)=$dbdef->table('svc_acct');
202 foreach $attribute (@attributes) {
203 $svc_acct->addcolumn ( new FS::dbdef_column (
204 'radius_'. $attribute,
211 foreach $attribute (@check_attributes) {
212 $svc_acct->addcolumn( new FS::dbdef_column (
220 #make part_svc table (but now as object)
222 my($part_svc)=$dbdef->table('part_svc');
224 #because of svc_acct_pop
225 #foreach (grep /^svc_/, $dbdef->tables) {
226 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
227 foreach (qw(svc_acct svc_acct_sm svc_domain svc_www)) {
228 my($table)=$dbdef->table($_);
230 foreach $col ( $table->columns ) {
231 next if $col =~ /^svcnum$/;
232 $part_svc->addcolumn( new FS::dbdef_column (
233 $table->name. '__' . $table->column($col)->name,
234 'varchar', #$table->column($col)->type,
236 $char_d, #$table->column($col)->length,
238 $part_svc->addcolumn ( new FS::dbdef_column (
239 $table->name. '__'. $table->column($col)->name . "_flag",
248 $dbdef->save($dbdef_file);
249 &FS::Record::reload_dbdef($dbdef_file);
255 my($dbh)=adminsuidsetup $user;
261 foreach ($dbdef->tables) {
262 my($table)=$dbdef->table($_);
263 print "Creating $_...";
268 foreach $statement ($table->sql_create_table(datasrc)) {
269 #print $statement, "\n";
270 $dbh->do( $statement )
271 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
277 #not really sample data (and shouldn't default to US)
283 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
284 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
285 SC SD TN TX UT VT VI VA WA WV WI WY AE AA AP
287 my($cust_main_county)=new FS::cust_main_county({
293 $error=$cust_main_county->insert;
294 die $error if $error;
297 #AU "offical" state codes ala mark.williamson@ebbs.com.au (Mark Williamson)
299 VIC NSW NT QLD TAS ACT WA SA
301 my($cust_main_county)=new FS::cust_main_county({
307 $error=$cust_main_county->insert;
308 die $error if $error;
311 #ISO 2-letter country codes (same as country TLDs) except US and AU
313 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
314 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
315 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
316 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
317 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
318 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
319 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
320 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
321 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
324 my($cust_main_county)=new FS::cust_main_county({
329 $error=$cust_main_county->insert;
330 die $error if $error;
333 $dbh->disconnect or die $dbh->errstr;
336 die "Usage:\n fs-setup user\n";
340 # Now it becomes an object. much better.
342 sub tables_hash_hack {
344 #note that s/(date|change)/_$1/; to avoid keyword conflict.
345 #put a kludge in FS::Record to catch this or? (pry need some date-handling
348 my(%tables)=( #yech.}
352 'agentnum', 'int', '', '',
353 'agent', 'varchar', '', $char_d,
354 'typenum', 'int', '', '',
355 'freq', 'int', 'NULL', '',
358 'primary_key' => 'agentnum',
360 'index' => [ ['typenum'] ],
365 'typenum', 'int', '', '',
366 'atype', 'varchar', '', $char_d,
368 'primary_key' => 'typenum',
375 'typenum', 'int', '', '',
376 'pkgpart', 'int', '', '',
379 'unique' => [ ['typenum', 'pkgpart'] ],
380 'index' => [ ['typenum'] ],
385 'invnum', 'int', '', '',
386 'custnum', 'int', '', '',
388 'charged', @money_type,
390 'printed', 'int', '', '',
392 'primary_key' => 'invnum',
394 'index' => [ ['custnum'] ],
399 'pkgnum', 'int', '', '',
400 'invnum', 'int', '', '',
401 'setup', @money_type,
402 'recur', @money_type,
407 'unique' => [ ['pkgnum', 'invnum'] ],
408 'index' => [ ['invnum'] ],
413 'crednum', 'int', '', '',
414 'custnum', 'int', '', '',
416 'amount', @money_type,
417 'credited', @money_type,
418 'otaker', 'varchar', '', 8,
419 'reason', 'varchar', '', 255,
421 'primary_key' => 'crednum',
423 'index' => [ ['custnum'] ],
428 'custnum', 'int', '', '',
429 'agentnum', 'int', '', '',
430 # 'titlenum', 'int', 'NULL', '',
431 'last', 'varchar', '', $char_d,
432 # 'middle', 'varchar', 'NULL', $char_d,
433 'first', 'varchar', '', $char_d,
434 'ss', 'char', 'NULL', 11,
435 'company', 'varchar', 'NULL', $char_d,
436 'address1', 'varchar', '', $char_d,
437 'address2', 'varchar', 'NULL', $char_d,
438 'city', 'varchar', '', $char_d,
439 'county', 'varchar', 'NULL', $char_d,
440 'state', 'varchar', 'NULL', $char_d,
441 'zip', 'varchar', '', 10,
442 'country', 'char', '', 2,
443 'daytime', 'varchar', 'NULL', 20,
444 'night', 'varchar', 'NULL', 20,
445 'fax', 'varchar', 'NULL', 12,
446 'payby', 'char', '', 4,
447 'payinfo', 'varchar', 'NULL', 16,
448 #'paydate', @date_type,
449 'paydate', 'varchar', 'NULL', 10,
450 'payname', 'varchar', 'NULL', $char_d,
451 'tax', 'char', 'NULL', 1,
452 'otaker', 'varchar', '', 8,
453 'refnum', 'int', '', '',
455 'primary_key' => 'custnum',
457 #'index' => [ ['last'], ['company'] ],
458 'index' => [ ['last'], ],
461 'cust_main_invoice' => {
463 'destnum', 'int', '', '',
464 'custnum', 'int', '', '',
465 'dest', 'varchar', '', $char_d,
467 'primary_key' => 'destnum',
469 'index' => [ ['custnum'], ],
472 'cust_main_county' => { #county+state+country are checked off the
473 #cust_main_county for validation and to provide
476 'taxnum', 'int', '', '',
477 'state', 'varchar', 'NULL', $char_d,
478 'county', 'varchar', 'NULL', $char_d,
479 'country', 'char', '', 2,
480 'tax', 'real', '', '', #tax %
482 'primary_key' => 'taxnum',
484 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
490 'paynum', 'int', '', '',
491 'invnum', 'int', '', '',
494 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
495 # payment type table.
496 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
497 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
499 'primary_key' => 'paynum',
501 'index' => [ ['invnum'] ],
504 'cust_pay_batch' => { #what's this used for again? list of customers
505 #in current CARD batch? (necessarily CARD?)
507 'invnum', 'int', '', '',
508 'custnum', 'int', '', '',
509 'last', 'varchar', '', $char_d,
510 'first', 'varchar', '', $char_d,
511 'address1', 'varchar', '', $char_d,
512 'address2', 'varchar', 'NULL', $char_d,
513 'city', 'varchar', '', $char_d,
514 'state', 'varchar', '', $char_d,
515 'zip', 'varchar', '', 10,
516 'country', 'char', '', 2,
517 'trancode', 'int', '', '',
518 'cardnum', 'varchar', '', 16,
520 'exp', 'varchar', '', 11,
521 'payname', 'varchar', 'NULL', $char_d,
522 'amount', @money_type,
526 'index' => [ ['invnum'], ['custnum'] ],
531 'pkgnum', 'int', '', '',
532 'custnum', 'int', '', '',
533 'pkgpart', 'int', '', '',
534 'otaker', 'varchar', '', 8,
538 'cancel', @date_type,
539 'expire', @date_type,
541 'primary_key' => 'pkgnum',
543 'index' => [ ['custnum'] ],
548 'refundnum', 'int', '', '',
549 'crednum', 'int', '', '',
551 'refund', @money_type,
552 'otaker', 'varchar', '', 8,
553 'reason', 'varchar', '', $char_d,
554 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
555 # into payment type table.
556 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
558 'primary_key' => 'refundnum',
560 'index' => [ ['crednum'] ],
565 'svcnum', 'int', '', '',
566 'pkgnum', 'int', '', '',
567 'svcpart', 'int', '', '',
569 'primary_key' => 'svcnum',
571 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
576 'pkgpart', 'int', '', '',
577 'pkg', 'varchar', '', $char_d,
578 'comment', 'varchar', '', $char_d,
580 'freq', 'int', '', '', #billing frequency (months)
583 'primary_key' => 'pkgpart',
590 # 'titlenum', 'int', '', '',
591 # 'title', 'varchar', '', $char_d,
593 # 'primary_key' => 'titlenum',
594 # 'unique' => [ [] ],
600 'pkgpart', 'int', '', '',
601 'svcpart', 'int', '', '',
602 'quantity', 'int', '', '',
605 'unique' => [ ['pkgpart', 'svcpart'] ],
606 'index' => [ ['pkgpart'] ],
611 'refnum', 'int', '', '',
612 'referral', 'varchar', '', $char_d,
614 'primary_key' => 'refnum',
621 'svcpart', 'int', '', '',
622 'svc', 'varchar', '', $char_d,
623 'svcdb', 'varchar', '', $char_d,
625 'primary_key' => 'svcpart',
630 #(this should be renamed to part_pop)
633 'popnum', 'int', '', '',
634 'city', 'varchar', '', $char_d,
635 'state', 'varchar', '', $char_d,
637 'exch', 'char', '', 3,
638 'loc', 'char', 'NULL', 4, #NULL for legacy purposes
640 'primary_key' => 'popnum',
647 'svcnum', 'int', '', '',
648 'username', 'varchar', '', $username_len, #unique (& remove dup code)
649 '_password', 'varchar', '', 50, #13 for encryped pw's plus ' *SUSPENDED* (mp5 passwords can be 34)
650 'popnum', 'int', 'NULL', '',
651 'uid', 'int', 'NULL', '',
652 'gid', 'int', 'NULL', '',
653 'finger', 'varchar', 'NULL', $char_d,
654 'dir', 'varchar', 'NULL', $char_d,
655 'shell', 'varchar', 'NULL', $char_d,
656 'quota', 'varchar', 'NULL', $char_d,
657 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
659 'primary_key' => 'svcnum',
661 'index' => [ ['username'] ],
666 'svcnum', 'int', '', '',
667 'domsvc', 'int', '', '',
668 'domuid', 'int', '', '',
669 'domuser', 'varchar', '', $char_d,
671 'primary_key' => 'svcnum',
673 'index' => [ ['domsvc'], ['domuid'] ],
678 # 'svcnum', 'int', '', '',
679 # 'amount', @money_type,
681 # 'primary_key' => 'svcnum',
682 # 'unique' => [ [] ],
688 'svcnum', 'int', '', '',
689 'domain', 'varchar', '', $char_d,
691 'primary_key' => 'svcnum',
692 'unique' => [ ['domain'] ],
698 'recnum', 'int', '', '',
699 'svcnum', 'int', '', '',
700 'reczone', 'varchar', '', $char_d,
701 'recaf', 'char', '', 2,
702 'rectype', 'char', '', 5,
703 'recdata', 'varchar', '', $char_d,
705 'primary_key' => 'recnum',
707 'index' => [ ['svcnum'] ],
712 'svcnum', 'int', '', '',
713 'recnum', 'int', '', '',
714 'usersvc', 'int', '', '',
716 'primary_key' => 'svcnum',
723 # 'svcnum', 'int', '', '',
724 # 'svcnum', 'int', '', '',
725 # 'svcnum', 'int', '', '',
726 # 'worker', 'varchar', '', $char_d,
727 # '_date', @date_type,
729 # 'primary_key' => 'svcnum',
730 # 'unique' => [ [] ],
736 'prepaynum', 'int', '', '',
737 'identifier', 'varchar', '', $char_d,
738 'amount', @money_type,
740 'primary_key' => 'prepaynum',
741 'unique' => [ ['identifier'] ],
747 'portnum', 'int', '', '',
748 'ip', 'varchar', 'NULL', 15,
749 'nasport', 'int', 'NULL', '',
750 'nasnum', 'int', '', '',
752 'primary_key' => 'portnum',
759 'nasnum', 'int', '', '',
760 'nas', 'varchar', '', $char_d,
761 'nasip', 'varchar', '', 15,
762 'nasfqdn', 'varchar', '', $char_d,
765 'primary_key' => 'nasnum',
766 'unique' => [ [ 'nas' ], [ 'nasip' ] ],
767 'index' => [ [ 'last' ] ],
772 'sessionnum', 'int', '', '',
773 'portnum', 'int', '', '',
774 'svcnum', 'int', '', '',
776 'logout', @date_type,
778 'primary_key' => 'sessionnum',
780 'index' => [ [ 'portnum' ] ],