3 # $Id: fs-setup,v 1.29 2000-11-07 15:00:37 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.29 2000-11-07 15:00:37 ivan
38 # Revision 1.28 2000/10/30 10:47:26 ivan
39 # nas.last can't be defined NULL if indexed
41 # Revision 1.26 2000/07/06 08:57:27 ivan
42 # support for radius check attributes (except importing). poorly documented.
44 # Revision 1.25 2000/06/29 12:00:49 ivan
45 # support for pre-encrypted md5 passwords.
47 # Revision 1.24 2000/03/02 07:44:07 ivan
48 # typo forgot closing '
50 # Revision 1.23 2000/02/03 05:16:52 ivan
51 # beginning of DNS and Apache support
53 # Revision 1.22 2000/01/31 05:22:23 ivan
54 # prepaid "internet cards"
56 # Revision 1.21 2000/01/30 06:03:26 ivan
57 # postgres 6.5 finally supports decimal(10,2)
59 # Revision 1.20 2000/01/28 22:53:33 ivan
60 # track full phone number
62 # Revision 1.19 1999/07/29 08:50:35 ivan
63 # wrong type for cust_pay_batch.exp
65 # Revision 1.18 1999/04/15 22:46:30 ivan
68 # Revision 1.17 1999/04/14 07:58:39 ivan
69 # export getsecrets from FS::UID instead of calling it explicitly
71 # Revision 1.16 1999/02/28 19:44:16 ivan
72 # constructors s/create/new/ pointed out by "Bao C. Ha" <bao@hacom.net>
74 # Revision 1.15 1999/02/27 21:06:21 ivan
75 # cust_main.paydate should be varchar(10), not @date_type ; problem reported
76 # by Ben Leibig <leibig@colorado.edu>
78 # Revision 1.14 1999/02/07 09:59:14 ivan
79 # more mod_perl fixes, and bugfixes Peter Wemm sent via email
81 # Revision 1.13 1999/02/04 06:09:23 ivan
84 # Revision 1.12 1999/02/03 10:42:27 ivan
85 # *** empty log message ***
87 # Revision 1.11 1999/01/17 03:11:52 ivan
88 # remove preliminary completehost changes
90 # Revision 1.10 1998/12/16 06:05:38 ivan
91 # add table cust_main_invoice
93 # Revision 1.9 1998/12/15 04:36:29 ivan
96 # Revision 1.8 1998/12/15 04:33:27 ivan
97 # dies if it isn't running as the freeside user
99 # Revision 1.7 1998/11/18 09:01:31 ivan
102 # Revision 1.6 1998/11/15 13:18:02 ivan
105 # Revision 1.5 1998/11/15 09:43:03 ivan
106 # update for new config file syntax, new adminsuidsetup
108 # Revision 1.4 1998/10/22 15:51:23 ivan
109 # also varchar with no length specified - postgresql fix broke mysql.
111 # Revision 1.3 1998/10/22 15:46:28 ivan
112 # now smallint is illegal, so remove that too.
115 #to delay loading dbdef until we're ready
116 BEGIN { $FS::Record::setup_hack = 1; }
121 use FS::UID qw(adminsuidsetup datasrc checkeuid getsecrets);
123 use FS::cust_main_county;
125 die "Not running uid freeside!" unless checkeuid();
127 my $user = shift or die &usage;
130 #needs to match FS::Record
131 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
135 print "\nEnter the maximum username length: ";
136 my($username_len)=&getvalue;
138 print "\n\n", <<END, ":";
139 Freeside tracks the RADIUS attributes User-Name, check attribute Password and
140 reply attribute Framed-IP-Address for each user. You can specify additional
141 check and reply attributes. First enter any additional RADIUS check attributes
142 you need to track for each user, separated by whitespace.
144 my @check_attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
146 print "\n\n", <<END, ":";
147 Now enter any additional reply attributes you need to track for each user,
148 separated by whitespace.
150 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
153 my($x)=scalar(<STDIN>);
160 my($char_d) = 80; #default maxlength for text fields
162 #my(@date_type) = ( 'timestamp', '', '' );
163 my(@date_type) = ( 'int', 'NULL', '' );
164 my(@perl_type) = ( 'varchar', 'NULL', 255 );
165 my @money_type = ( 'decimal', '', '10,2' );
168 # create a dbdef object from the old data structure
171 my(%tables)=&tables_hash_hack;
173 #turn it into objects
174 my($dbdef) = new FS::dbdef ( map {
176 while (@{$tables{$_}{'columns'}}) {
177 my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
178 push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
180 FS::dbdef_table->new(
182 $tables{$_}{'primary_key'},
183 #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
184 #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
185 FS::dbdef_unique->new($tables{$_}{'unique'}),
186 FS::dbdef_index->new($tables{$_}{'index'}),
191 #add radius attributes to svc_acct
193 my($svc_acct)=$dbdef->table('svc_acct');
196 foreach $attribute (@attributes) {
197 $svc_acct->addcolumn ( new FS::dbdef_column (
198 'radius_'. $attribute,
205 foreach $attribute (@check_attributes) {
206 $svc_acct->addcolumn( new FS::dbdef_column (
214 #make part_svc table (but now as object)
216 my($part_svc)=$dbdef->table('part_svc');
218 #because of svc_acct_pop
219 #foreach (grep /^svc_/, $dbdef->tables) {
220 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
221 foreach (qw(svc_acct svc_acct_sm svc_domain svc_www)) {
222 my($table)=$dbdef->table($_);
224 foreach $col ( $table->columns ) {
225 next if $col =~ /^svcnum$/;
226 $part_svc->addcolumn( new FS::dbdef_column (
227 $table->name. '__' . $table->column($col)->name,
228 'varchar', #$table->column($col)->type,
230 $char_d, #$table->column($col)->length,
232 $part_svc->addcolumn ( new FS::dbdef_column (
233 $table->name. '__'. $table->column($col)->name . "_flag",
242 $dbdef->save($dbdef_file);
243 &FS::Record::reload_dbdef($dbdef_file);
249 my($dbh)=adminsuidsetup $user;
255 foreach ($dbdef->tables) {
256 my($table)=$dbdef->table($_);
257 print "Creating $_...";
262 foreach $statement ($table->sql_create_table(datasrc)) {
263 #print $statement, "\n";
264 $dbh->do( $statement )
265 or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
271 #not really sample data (and shouldn't default to US)
277 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
278 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
279 SC SD TN TX UT VT VI VA WA WV WI WY AE AA AP
281 my($cust_main_county)=new FS::cust_main_county({
287 $error=$cust_main_county->insert;
288 die $error if $error;
291 #AU "offical" state codes ala mark.williamson@ebbs.com.au (Mark Williamson)
293 VIC NSW NT QLD TAS ACT WA SA
295 my($cust_main_county)=new FS::cust_main_county({
301 $error=$cust_main_county->insert;
302 die $error if $error;
305 #ISO 2-letter country codes (same as country TLDs) except US and AU
307 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
308 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
309 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
310 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
311 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
312 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
313 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
314 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
315 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
318 my($cust_main_county)=new FS::cust_main_county({
323 $error=$cust_main_county->insert;
324 die $error if $error;
327 $dbh->disconnect or die $dbh->errstr;
330 die "Usage:\n fs-setup user\n";
334 # Now it becomes an object. much better.
336 sub tables_hash_hack {
338 #note that s/(date|change)/_$1/; to avoid keyword conflict.
339 #put a kludge in FS::Record to catch this or? (pry need some date-handling
342 my(%tables)=( #yech.}
346 'agentnum', 'int', '', '',
347 'agent', 'varchar', '', $char_d,
348 'typenum', 'int', '', '',
349 'freq', 'int', 'NULL', '',
352 'primary_key' => 'agentnum',
354 'index' => [ ['typenum'] ],
359 'typenum', 'int', '', '',
360 'atype', 'varchar', '', $char_d,
362 'primary_key' => 'typenum',
369 'typenum', 'int', '', '',
370 'pkgpart', 'int', '', '',
373 'unique' => [ ['typenum', 'pkgpart'] ],
374 'index' => [ ['typenum'] ],
379 'invnum', 'int', '', '',
380 'custnum', 'int', '', '',
382 'charged', @money_type,
384 'printed', 'int', '', '',
386 'primary_key' => 'invnum',
388 'index' => [ ['custnum'] ],
393 'pkgnum', 'int', '', '',
394 'invnum', 'int', '', '',
395 'setup', @money_type,
396 'recur', @money_type,
401 'unique' => [ ['pkgnum', 'invnum'] ],
402 'index' => [ ['invnum'] ],
407 'crednum', 'int', '', '',
408 'custnum', 'int', '', '',
410 'amount', @money_type,
411 'credited', @money_type,
412 'otaker', 'varchar', '', 8,
413 'reason', 'varchar', '', 255,
415 'primary_key' => 'crednum',
417 'index' => [ ['custnum'] ],
422 'custnum', 'int', '', '',
423 'agentnum', 'int', '', '',
424 # 'titlenum', 'int', 'NULL', '',
425 'last', 'varchar', '', $char_d,
426 # 'middle', 'varchar', 'NULL', $char_d,
427 'first', 'varchar', '', $char_d,
428 'ss', 'char', 'NULL', 11,
429 'company', 'varchar', 'NULL', $char_d,
430 'address1', 'varchar', '', $char_d,
431 'address2', 'varchar', 'NULL', $char_d,
432 'city', 'varchar', '', $char_d,
433 'county', 'varchar', 'NULL', $char_d,
434 'state', 'varchar', 'NULL', $char_d,
435 'zip', 'varchar', '', 10,
436 'country', 'char', '', 2,
437 'daytime', 'varchar', 'NULL', 20,
438 'night', 'varchar', 'NULL', 20,
439 'fax', 'varchar', 'NULL', 12,
440 'payby', 'char', '', 4,
441 'payinfo', 'varchar', 'NULL', 16,
442 #'paydate', @date_type,
443 'paydate', 'varchar', 'NULL', 10,
444 'payname', 'varchar', 'NULL', $char_d,
445 'tax', 'char', 'NULL', 1,
446 'otaker', 'varchar', '', 8,
447 'refnum', 'int', '', '',
449 'primary_key' => 'custnum',
451 #'index' => [ ['last'], ['company'] ],
452 'index' => [ ['last'], ],
455 'cust_main_invoice' => {
457 'destnum', 'int', '', '',
458 'custnum', 'int', '', '',
459 'dest', 'varchar', '', $char_d,
461 'primary_key' => 'destnum',
463 'index' => [ ['custnum'], ],
466 'cust_main_county' => { #county+state+country are checked off the
467 #cust_main_county for validation and to provide
470 'taxnum', 'int', '', '',
471 'state', 'varchar', 'NULL', $char_d,
472 'county', 'varchar', 'NULL', $char_d,
473 'country', 'char', '', 2,
474 'tax', 'real', '', '', #tax %
476 'primary_key' => 'taxnum',
478 # 'unique' => [ ['taxnum'], ['state', 'county'] ],
484 'paynum', 'int', '', '',
485 'invnum', 'int', '', '',
488 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index into
489 # payment type table.
490 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
491 'paybatch', 'varchar', 'NULL', $char_d, #for auditing purposes.
493 'primary_key' => 'paynum',
495 'index' => [ ['invnum'] ],
498 'cust_pay_batch' => { #what's this used for again? list of customers
499 #in current CARD batch? (necessarily CARD?)
501 'invnum', 'int', '', '',
502 'custnum', 'int', '', '',
503 'last', 'varchar', '', $char_d,
504 'first', 'varchar', '', $char_d,
505 'address1', 'varchar', '', $char_d,
506 'address2', 'varchar', 'NULL', $char_d,
507 'city', 'varchar', '', $char_d,
508 'state', 'varchar', '', $char_d,
509 'zip', 'varchar', '', 10,
510 'country', 'char', '', 2,
511 'trancode', 'int', '', '',
512 'cardnum', 'varchar', '', 16,
514 'exp', 'varchar', '', 11,
515 'payname', 'varchar', 'NULL', $char_d,
516 'amount', @money_type,
520 'index' => [ ['invnum'], ['custnum'] ],
525 'pkgnum', 'int', '', '',
526 'custnum', 'int', '', '',
527 'pkgpart', 'int', '', '',
528 'otaker', 'varchar', '', 8,
532 'cancel', @date_type,
533 'expire', @date_type,
535 'primary_key' => 'pkgnum',
537 'index' => [ ['custnum'] ],
542 'refundnum', 'int', '', '',
543 'crednum', 'int', '', '',
545 'refund', @money_type,
546 'otaker', 'varchar', '', 8,
547 'reason', 'varchar', '', $char_d,
548 'payby', 'char', '', 4, # CARD/BILL/COMP, should be index
549 # into payment type table.
550 'payinfo', 'varchar', 'NULL', 16, #see cust_main above
552 'primary_key' => 'refundnum',
554 'index' => [ ['crednum'] ],
559 'svcnum', 'int', '', '',
560 'pkgnum', 'int', '', '',
561 'svcpart', 'int', '', '',
563 'primary_key' => 'svcnum',
565 'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
570 'pkgpart', 'int', '', '',
571 'pkg', 'varchar', '', $char_d,
572 'comment', 'varchar', '', $char_d,
574 'freq', 'int', '', '', #billing frequency (months)
577 'primary_key' => 'pkgpart',
584 # 'titlenum', 'int', '', '',
585 # 'title', 'varchar', '', $char_d,
587 # 'primary_key' => 'titlenum',
588 # 'unique' => [ [] ],
594 'pkgpart', 'int', '', '',
595 'svcpart', 'int', '', '',
596 'quantity', 'int', '', '',
599 'unique' => [ ['pkgpart', 'svcpart'] ],
600 'index' => [ ['pkgpart'] ],
605 'refnum', 'int', '', '',
606 'referral', 'varchar', '', $char_d,
608 'primary_key' => 'refnum',
615 'svcpart', 'int', '', '',
616 'svc', 'varchar', '', $char_d,
617 'svcdb', 'varchar', '', $char_d,
619 'primary_key' => 'svcpart',
624 #(this should be renamed to part_pop)
627 'popnum', 'int', '', '',
628 'city', 'varchar', '', $char_d,
629 'state', 'varchar', '', $char_d,
631 'exch', 'char', '', 3,
632 'loc', 'char', 'NULL', 4, #NULL for legacy purposes
634 'primary_key' => 'popnum',
641 'svcnum', 'int', '', '',
642 'username', 'varchar', '', $username_len, #unique (& remove dup code)
643 '_password', 'varchar', '', 50, #13 for encryped pw's plus ' *SUSPENDED* (mp5 passwords can be 34)
644 'popnum', 'int', 'NULL', '',
645 'uid', 'int', 'NULL', '',
646 'gid', 'int', 'NULL', '',
647 'finger', 'varchar', 'NULL', $char_d,
648 'dir', 'varchar', 'NULL', $char_d,
649 'shell', 'varchar', 'NULL', $char_d,
650 'quota', 'varchar', 'NULL', $char_d,
651 'slipip', 'varchar', 'NULL', 15, #four TINYINTs, bah.
653 'primary_key' => 'svcnum',
655 'index' => [ ['username'] ],
660 'svcnum', 'int', '', '',
661 'domsvc', 'int', '', '',
662 'domuid', 'int', '', '',
663 'domuser', 'varchar', '', $char_d,
665 'primary_key' => 'svcnum',
667 'index' => [ ['domsvc'], ['domuid'] ],
672 # 'svcnum', 'int', '', '',
673 # 'amount', @money_type,
675 # 'primary_key' => 'svcnum',
676 # 'unique' => [ [] ],
682 'svcnum', 'int', '', '',
683 'domain', 'varchar', '', $char_d,
685 'primary_key' => 'svcnum',
686 'unique' => [ ['domain'] ],
692 'recnum', 'int', '', '',
693 'svcnum', 'int', '', '',
694 'reczone', 'varchar', '', $char_d,
695 'recaf', 'char', '', 2,
696 'rectype', 'char', '', 5,
697 'recdata', 'varchar', '', $char_d,
699 'primary_key' => 'recnum',
701 'index' => [ ['svcnum'] ],
706 'svcnum', 'int', '', '',
707 'recnum', 'int', '', '',
708 'usersvc', 'int', '', '',
710 'primary_key' => 'svcnum',
717 # 'svcnum', 'int', '', '',
718 # 'svcnum', 'int', '', '',
719 # 'svcnum', 'int', '', '',
720 # 'worker', 'varchar', '', $char_d,
721 # '_date', @date_type,
723 # 'primary_key' => 'svcnum',
724 # 'unique' => [ [] ],
730 'prepaynum', 'int', '', '',
731 'identifier', 'varchar', '', $char_d,
732 'amount', @money_type,
734 'primary_key' => 'prepaynum',
735 'unique' => [ ['identifier'] ],
741 'portnum', 'int', '', '',
742 'ip', 'varchar', NULL, 15,
743 'nasport' 'int', NULL, '',
744 'nasnum', 'int', '', '',
746 'primary_key' => 'portnum',
753 'nasnum', 'int', '', '',
754 'nas', 'varchar', '', $char_d,
755 'nasip', 'varchar', '', 15,
756 'nasfqdn', 'varchar', '', $char_d,
759 'primary_key' => 'nasnum',
760 'unique' => [ [ 'nas' ], [ 'nasip' ] ],
761 'index' => [ [ 'last' ] ],
766 'sessionnum', 'int', '', '',
767 'portnum', 'int', '', '',
768 'svcnum', 'int', '', '',
770 'logout', @date_type,
772 'primary_key' => 'sessionnum',
774 'index' => [ [ 'portnum' ] ],