dies if it isn't running as the freeside user
[freeside.git] / bin / fs-setup
1 #!/usr/bin/perl -Tw
2 #
3 # $Id: fs-setup,v 1.8 1998-12-15 04:33:27 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.8  1998-12-15 04:33:27  ivan
36 # dies if it isn't running as the freeside user
37 #
38 # Revision 1.7  1998/11/18 09:01:31  ivan
39 # i18n! i18n!
40 #
41 # Revision 1.6  1998/11/15 13:18:02  ivan
42 # remove debugging
43 #
44 # Revision 1.5  1998/11/15 09:43:03  ivan
45 # update for new config file syntax, new adminsuidsetup
46 #
47 # Revision 1.4  1998/10/22 15:51:23  ivan
48 # also varchar with no length specified - postgresql fix broke mysql.
49 #
50 # Revision 1.3  1998/10/22 15:46:28  ivan
51 # now smallint is illegal, so remove that too.
52 #
53
54 #to delay loading dbdef until we're ready
55 BEGIN { $FS::Record::setup_hack = 1; }
56
57 use strict;
58 use DBI;
59 use FS::dbdef;
60 use FS::UID qw(adminsuidsetup datasrc checkeuid);
61 use FS::Record;
62 use FS::cust_main_county;
63
64 croak "Not running uid freeside!" unless checkeuid();
65
66 my $user = shift or die &usage;
67 FS::UID::getsecrets $user;
68
69 #needs to match FS::Record
70 my($dbdef_file) = "/usr/local/etc/freeside/dbdef.". datasrc;
71
72 ###
73
74 print "\nEnter the maximum username length: ";
75 my($username_len)=&getvalue;
76
77 print "\n\n", <<END, ":";
78 Freeside tracks the RADIUS attributes User-Name, Password and Framed-IP-Address
79 for each user.  Enter any additional RADIUS attributes you need to track for
80 each user, separated by whitespace.
81 END
82 my @attributes = map { s/\-/_/g; $_; } split(" ",&getvalue);
83
84 sub getvalue {
85   my($x)=scalar(<STDIN>);
86   chop $x;
87   $x;
88 }
89
90 ###
91
92 my($char_d) = 80; #default maxlength for text fields
93
94 #my(@date_type)  = ( 'timestamp', '', ''     );
95 my(@date_type)  = ( 'int', 'NULL', ''     );
96 my(@perl_type) = ( 'varchar', 'NULL', 255  ); 
97 my(@money_type);
98 if (datasrc =~ m/Pg/) { #Pg can't do decimal(10,2)
99   @money_type = ( 'money',   '', '' );
100 } else {
101   @money_type = ( 'decimal',   '', '10,2' );
102 }
103
104 ###
105 # create a dbdef object from the old data structure
106 ###
107
108 my(%tables)=&tables_hash_hack;
109
110 #turn it into objects
111 my($dbdef) = new FS::dbdef ( map {  
112   my(@columns);
113   while (@{$tables{$_}{'columns'}}) {
114     my($name,$type,$null,$length)=splice @{$tables{$_}{'columns'}}, 0, 4;
115     push @columns, new FS::dbdef_column ( $name,$type,$null,$length );
116   }
117   FS::dbdef_table->new(
118     $_,
119     $tables{$_}{'primary_key'},
120     #FS::dbdef_unique->new(@{$tables{$_}{'unique'}}),
121     #FS::dbdef_index->new(@{$tables{$_}{'index'}}),
122     FS::dbdef_unique->new($tables{$_}{'unique'}),
123     FS::dbdef_index->new($tables{$_}{'index'}),
124     @columns,
125   );
126 } (keys %tables) );
127
128 #add radius attributes to svc_acct
129
130 my($svc_acct)=$dbdef->table('svc_acct');
131
132 my($attribute);
133 foreach $attribute (@attributes) {
134   $svc_acct->addcolumn ( new FS::dbdef_column (
135     'radius_'. $attribute,
136     'varchar',
137     'NULL',
138     $char_d,
139   ));
140 }
141
142 #make part_svc table (but now as object)
143
144 my($part_svc)=$dbdef->table('part_svc');
145
146 #because of svc_acct_pop
147 #foreach (grep /^svc_/, $dbdef->tables) { 
148 #foreach (qw(svc_acct svc_acct_sm svc_charge svc_domain svc_wo)) {
149 foreach (qw(svc_acct svc_acct_sm svc_domain)) {
150   my($table)=$dbdef->table($_);
151   my($col);
152   foreach $col ( $table->columns ) {
153     next if $col =~ /^svcnum$/;
154     $part_svc->addcolumn( new FS::dbdef_column (
155       $table->name. '__' . $table->column($col)->name,
156       'varchar', #$table->column($col)->type, 
157       'NULL',
158       $char_d, #$table->column($col)->length,
159     ));
160     $part_svc->addcolumn ( new FS::dbdef_column (
161       $table->name. '__'. $table->column($col)->name . "_flag",
162       'char',
163       'NULL',
164       1,
165     ));
166   }
167 }
168
169 #important
170 $dbdef->save($dbdef_file);
171 FS::Record::reload_dbdef($dbdef_file);
172
173 ###
174 # create 'em
175 ###
176
177 my($dbh)=adminsuidsetup $user;
178
179 #create tables
180 $|=1;
181
182 my($table);
183 foreach  ($dbdef->tables) {
184   my($table)=$dbdef->table($_);
185   print "Creating $_...";
186
187   my($statement);
188
189   #create table
190   foreach $statement ($table->sql_create_table(datasrc)) {
191     #print $statement, "\n"; 
192     $dbh->do( $statement )
193       or die "CREATE error: ",$dbh->errstr, "\ndoing statement: $statement";
194   }
195
196   print "\n";
197 }
198
199 #not really sample data (and shouldn't default to US)
200
201 #cust_main_county
202
203 #USPS state codes
204 foreach ( qw(
205 AL AK AS AZ AR CA CO CT DC DE FM FL GA GU HI ID IL IN IA KS KY LA
206 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 
207 SC SD TN TX TT UT VT VI VA WA WV WI WY AE AA AP
208 ) ) {
209   my($cust_main_county)=create FS::cust_main_county({
210     'state' => $_,
211     'tax'   => 0,
212     'country' => 'US',
213   });  
214   my($error);
215   $error=$cust_main_county->insert;
216   die $error if $error;
217 }
218
219 #ISO 2-letter country codes (same as country TLDs) except US
220 foreach ( qw(
221 AF AL DZ AS AD AO AI AQ AG AR AM AW AU AT AZ BS BH BD BB BY BE BZ BJ BM BT BO
222 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
223 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
224 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
225 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
226 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
227 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
228 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
229 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
230 YE YU ZR ZM ZW
231 ) ) {
232   my($cust_main_county)=create FS::cust_main_county({
233     'tax'   => 0,
234     'country' => $_,
235   });  
236   my($error);
237   $error=$cust_main_county->insert;
238   die $error if $error;
239 }
240
241 $dbh->disconnect or die $dbh->errstr;
242
243 sub usage {
244   die "Usage:\n  fs-setup user\n"; 
245 }
246
247 ###
248 # Now it becomes an object.  much better.
249 ###
250 sub tables_hash_hack {
251
252   #note that s/(date|change)/_$1/; to avoid keyword conflict.
253   #put a kludge in FS::Record to catch this or? (pry need some date-handling
254   #stuff anyway also)
255
256   my(%tables)=( #yech.}
257
258     'agent' => {
259       'columns' => [
260         'agentnum', 'int',            '',     '',
261         'agent',    'varchar',           '',     $char_d,
262         'typenum',  'int',            '',     '',
263         'freq',     'int',       'NULL', '',
264         'prog',     @perl_type,
265       ],
266       'primary_key' => 'agentnum',
267       'unique' => [ [] ],
268       'index' => [ ['typenum'] ],
269     },
270
271     'agent_type' => {
272       'columns' => [
273         'typenum',   'int',  '', '',
274         'atype',     'varchar', '', $char_d,
275       ],
276       'primary_key' => 'typenum',
277       'unique' => [ [] ],
278       'index' => [ [] ],
279     },
280
281     'type_pkgs' => {
282       'columns' => [
283         'typenum',   'int',  '', '',
284         'pkgpart',   'int',  '', '',
285       ],
286       'primary_key' => '',
287       'unique' => [ ['typenum', 'pkgpart'] ],
288       'index' => [ ['typenum'] ],
289     },
290
291     'cust_bill' => {
292       'columns' => [
293         'invnum',    'int',  '', '',
294         'custnum',   'int',  '', '',
295         '_date',     @date_type,
296         'charged',   @money_type,
297         'owed',      @money_type,
298         'printed',   'int',  '', '',
299       ],
300       'primary_key' => 'invnum',
301       'unique' => [ [] ],
302       'index' => [ ['custnum'] ],
303     },
304
305     'cust_bill_pkg' => {
306       'columns' => [
307         'pkgnum',  'int', '', '',
308         'invnum',  'int', '', '',
309         'setup',   @money_type,
310         'recur',   @money_type,
311         'sdate',   @date_type,
312         'edate',   @date_type,
313       ],
314       'primary_key' => '',
315       'unique' => [ ['pkgnum', 'invnum'] ],
316       'index' => [ ['invnum'] ],
317     },
318
319     'cust_credit' => {
320       'columns' => [
321         'crednum',  'int', '', '',
322         'custnum',  'int', '', '',
323         '_date',    @date_type,
324         'amount',   @money_type,
325         'credited', @money_type,
326         'otaker',   'varchar', '', 8,
327         'reason',   'varchar', '', 255,
328       ],
329       'primary_key' => 'crednum',
330       'unique' => [ [] ],
331       'index' => [ ['custnum'] ],
332     },
333
334     'cust_main' => {
335       'columns' => [
336         'custnum',  'int',  '',     '',
337         'agentnum', 'int',  '',     '',
338         'titlenum', 'int',  'NULL',   '',
339         'last',     'varchar', '',     $char_d,
340         'middle',   'varchar', 'NULL', $char_d,
341         'first',    'varchar', '',     $char_d,
342         'ss',       'char', 'NULL', 11,
343         'company',  'varchar', 'NULL', $char_d,
344         'address1', 'varchar', '',     $char_d,
345         'address2', 'varchar', 'NULL', $char_d,
346         'city',     'varchar', '',     $char_d,
347         'county',   'varchar', 'NULL', $char_d,
348         'state',    'varchar', '',     $char_d,
349         'zip',      'varchar', '',     10,
350         'country',  'char', '',     2,
351         'daytime',  'varchar', 'NULL', 20,
352         'night',    'varchar', 'NULL', 20,
353         'fax',      'varchar', 'NULL', 12,
354         'payby',    'char', '',     4,
355         'payinfo',  'varchar', 'NULL', 16,
356         'paydate',  @date_type,
357         'payname',  'varchar', 'NULL', $char_d,
358         'tax',      'char', 'NULL', 1,
359         'otaker',   'varchar', '',     8,
360         'refnum',   'int',  '',     '',
361       ],
362       'primary_key' => 'custnum',
363       'unique' => [ [] ],
364       #'index' => [ ['last'], ['company'] ],
365       'index' => [ ['last'], ],
366     },
367
368     'cust_main_county' => { #county+state+country are checked off the
369                             #cust_main_county for validation and to provide
370                             # a tax rate.
371       'columns' => [
372         'taxnum',   'int',   '',    '',
373         'state',    'char',  'NULL',    $char_d,
374         'county',   'varchar',  'NULL',    $char_d,
375         'country',  'char',  '', 2, 
376         'tax',      'real',  '',    '', #tax %
377       ],
378       'primary_key' => 'taxnum',
379       'unique' => [ [] ],
380   #    'unique' => [ ['taxnum'], ['state', 'county'] ],
381       'index' => [ [] ],
382     },
383
384     'cust_pay' => {
385       'columns' => [
386         'paynum',   'int',    '',   '',
387         'invnum',   'int',    '',   '',
388         'paid',     @money_type,
389         '_date',    @date_type,
390         'payby',    'char',   '',     4, # CARD/BILL/COMP, should be index into
391                                          # payment type table.
392         'payinfo',  'varchar',   'NULL', 16,  #see cust_main above
393         'paybatch', 'varchar',   'NULL', $char_d, #for auditing purposes.
394       ],
395       'primary_key' => 'paynum',
396       'unique' => [ [] ],
397       'index' => [ ['invnum'] ],
398     },
399
400     'cust_pay_batch' => { #what's this used for again?  list of customers
401                           #in current CARD batch? (necessarily CARD?)
402       'columns' => [
403         'invnum',   'int',    '',   '',
404         'custnum',   'int',    '',   '',
405         'last',     'varchar', '',     $char_d,
406         'first',    'varchar', '',     $char_d,
407         'address1', 'varchar', '',     $char_d,
408         'address2', 'varchar', 'NULL', $char_d,
409         'city',     'varchar', '',     $char_d,
410         'state',    'char', '',     2,
411         'zip',      'varchar', '',     10,
412         'country',  'char', '',     2,
413         'trancode', 'int', '', '',
414         'cardnum',  'varchar', '',     16,
415         'exp',      @date_type,
416         'payname',  'varchar', 'NULL', $char_d,
417         'amount',   @money_type,
418       ],
419       'primary_key' => '',
420       'unique' => [ [] ],
421       'index' => [ ['invnum'], ['custnum'] ],
422     },
423
424     'cust_pkg' => {
425       'columns' => [
426         'pkgnum',    'int',    '',   '',
427         'custnum',   'int',    '',   '',
428         'pkgpart',   'int',    '',   '',
429         'otaker',    'varchar', '', 8,
430         'setup',     @date_type,
431         'bill',      @date_type,
432         'susp',      @date_type,
433         'cancel',    @date_type,
434         'expire',    @date_type,
435       ],
436       'primary_key' => 'pkgnum',
437       'unique' => [ [] ],
438       'index' => [ ['custnum'] ],
439     },
440
441     'cust_refund' => {
442       'columns' => [
443         'refundnum',    'int',    '',   '',
444         'crednum',      'int',    '',   '',
445         '_date',        @date_type,
446         'refund',       @money_type,
447         'otaker',       'varchar',   '',   8,
448         'reason',       'varchar',   '',   $char_d,
449         'payby',        'char',   '',     4, # CARD/BILL/COMP, should be index
450                                              # into payment type table.
451         'payinfo',      'varchar',   'NULL', 16,  #see cust_main above
452       ],
453       'primary_key' => 'refundnum',
454       'unique' => [ [] ],
455       'index' => [ ['crednum'] ],
456     },
457
458     'cust_svc' => {
459       'columns' => [
460         'svcnum',    'int',    '',   '',
461         'pkgnum',    'int',    '',   '',
462         'svcpart',   'int',    '',   '',
463       ],
464       'primary_key' => 'svcnum',
465       'unique' => [ [] ],
466       'index' => [ ['svcnum'], ['pkgnum'], ['svcpart'] ],
467     },
468
469     'part_pkg' => {
470       'columns' => [
471         'pkgpart',    'int',    '',   '',
472         'pkg',        'varchar',   '',   $char_d,
473         'comment',    'varchar',   '',   $char_d,
474         'setup',      @perl_type,
475         'freq',       'int', '', '',  #billing frequency (months)
476         'recur',      @perl_type,
477       ],
478       'primary_key' => 'pkgpart',
479       'unique' => [ [] ],
480       'index' => [ [] ],
481     },
482
483     'part_title' => {
484       'columns' => [
485         'titlenum',   'int',    '',   '',
486         'title',      'varchar',   '',   $char_d,
487       ],
488       'primary_key' => 'titlenum',
489       'unique' => [ [] ],
490       'index' => [ [] ],
491     },
492
493     'pkg_svc' => {
494       'columns' => [
495         'pkgpart',    'int',    '',   '',
496         'svcpart',    'int',    '',   '',
497         'quantity',   'int',    '',   '',
498       ],
499       'primary_key' => '',
500       'unique' => [ ['pkgpart', 'svcpart'] ],
501       'index' => [ ['pkgpart'] ],
502     },
503
504     'part_referral' => {
505       'columns' => [
506         'refnum',   'int',    '',   '',
507         'referral', 'varchar',   '',   $char_d,
508       ],
509       'primary_key' => 'refnum',
510       'unique' => [ [] ],
511       'index' => [ [] ],
512     },
513
514     'part_svc' => {
515       'columns' => [
516         'svcpart',    'int',    '',   '',
517         'svc',        'varchar',   '',   $char_d,
518         'svcdb',      'varchar',   '',   $char_d,
519       ],
520       'primary_key' => 'svcpart',
521       'unique' => [ [] ],
522       'index' => [ [] ],
523     },
524
525     #(this should be renamed to part_pop)
526     'svc_acct_pop' => {
527       'columns' => [
528         'popnum',    'int',    '',   '',
529         'city',      'varchar',   '',   $char_d,
530         'state',     'char',   '',   2,
531         'ac',        'char',   '',   3,
532         'exch',      'char',   '',   3,
533         #rest o' number?
534       ],
535       'primary_key' => 'popnum',
536       'unique' => [ [] ],
537       'index' => [ [] ],
538     },
539
540     'svc_acct' => {
541       'columns' => [
542         'svcnum',    'int',    '',   '',
543         'username',  'varchar',   '',   $username_len, #unique (& remove dup code)
544         '_password', 'varchar',   '',   25, #13 for encryped pw's plus ' *SUSPENDED*
545         'popnum',    'int',    'NULL',   '',
546         'uid',       'int', 'NULL',   '',
547         'gid',       'int', 'NULL',   '',
548         'finger',    'varchar',   'NULL',   $char_d,
549         'dir',       'varchar',   'NULL',   $char_d,
550         'shell',     'varchar',   'NULL',   $char_d,
551         'quota',     'varchar',   'NULL',   $char_d,
552         'slipip',    'varchar',   'NULL',   15, #four TINYINTs, bah.
553       ],
554       'primary_key' => 'svcnum',
555       'unique' => [ [] ],
556       'index' => [ ['username'] ],
557     },
558
559     'svc_acct_sm' => {
560       'columns' => [
561         'svcnum',    'int',    '',   '',
562         'domsvc',    'int',    '',   '',
563         'domuid',    'int', '',   '',
564         'domuser',   'varchar',   '',   $char_d,
565       ],
566       'primary_key' => 'svcnum',
567       'unique' => [ [] ],
568       'index' => [ ['domsvc'], ['domuid'] ], 
569     },
570
571     #'svc_charge' => {
572     #  'columns' => [
573     #    'svcnum',    'int',    '',   '',
574     #    'amount',    @money_type,
575     #  ],
576     #  'primary_key' => 'svcnum',
577     #  'unique' => [ [] ],
578     #  'index' => [ [] ],
579     #},
580
581     'svc_domain' => {
582       'columns' => [
583         'svcnum',    'int',    '',   '',
584         'domain',    'varchar',    '',   $char_d,
585       ],
586       'primary_key' => 'svcnum',
587       'unique' => [ ['domain'] ],
588       'index' => [ [] ],
589     },
590
591     #'svc_wo' => {
592     #  'columns' => [
593     #    'svcnum',    'int',    '',   '',
594     #    'svcnum',    'int',    '',   '',
595     #    'svcnum',    'int',    '',   '',
596     #    'worker',    'varchar',   '',   $char_d,
597     #    '_date',     @date_type,
598     #  ],
599     #  'primary_key' => 'svcnum',
600     #  'unique' => [ [] ],
601     #  'index' => [ [] ],
602     #},
603
604   );
605
606   %tables;
607
608 }
609