4 use vars qw( @ISA $DEBUG $me $conf $skip_fuzzyfiles
5 $dir_prefix @shells $usernamemin
6 $usernamemax $passwordmin $passwordmax
7 $username_ampersand $username_letter $username_letterfirst
8 $username_noperiod $username_nounderscore $username_nodash
9 $username_uppercase $username_percent
10 $password_noampersand $password_noexclamation
11 $welcome_template $welcome_from
12 $welcome_subject $welcome_subject_template $welcome_mimetype
13 $warning_template $warning_from $warning_subject $warning_mimetype
16 $radius_password $radius_ip
22 use Crypt::PasswdMD5 1.2;
24 use FS::UID qw( datasrc );
26 use FS::Record qw( qsearch qsearchs fields dbh dbdef );
27 use FS::Msgcat qw(gettext);
28 use FS::UI::bytecount;
33 use FS::cust_main_invoice;
37 use FS::radius_usergroup;
44 @ISA = qw( FS::svc_Common );
47 $me = '[FS::svc_acct]';
49 #ask FS::UID to run this stuff for us later
50 $FS::UID::callback{'FS::svc_acct'} = sub {
52 $dir_prefix = $conf->config('home');
53 @shells = $conf->config('shells');
54 $usernamemin = $conf->config('usernamemin') || 2;
55 $usernamemax = $conf->config('usernamemax');
56 $passwordmin = $conf->config('passwordmin') || 6;
57 $passwordmax = $conf->config('passwordmax') || 8;
58 $username_letter = $conf->exists('username-letter');
59 $username_letterfirst = $conf->exists('username-letterfirst');
60 $username_noperiod = $conf->exists('username-noperiod');
61 $username_nounderscore = $conf->exists('username-nounderscore');
62 $username_nodash = $conf->exists('username-nodash');
63 $username_uppercase = $conf->exists('username-uppercase');
64 $username_ampersand = $conf->exists('username-ampersand');
65 $username_percent = $conf->exists('username-percent');
66 $password_noampersand = $conf->exists('password-noexclamation');
67 $password_noexclamation = $conf->exists('password-noexclamation');
68 $dirhash = $conf->config('dirhash') || 0;
69 if ( $conf->exists('welcome_email') ) {
70 $welcome_template = new Text::Template (
72 SOURCE => [ map "$_\n", $conf->config('welcome_email') ]
73 ) or warn "can't create welcome email template: $Text::Template::ERROR";
74 $welcome_from = $conf->config('welcome_email-from'); # || 'your-isp-is-dum'
75 $welcome_subject = $conf->config('welcome_email-subject') || 'Welcome';
76 $welcome_subject_template = new Text::Template (
78 SOURCE => $welcome_subject,
79 ) or warn "can't create welcome email subject template: $Text::Template::ERROR";
80 $welcome_mimetype = $conf->config('welcome_email-mimetype') || 'text/plain';
82 $welcome_template = '';
84 $welcome_subject = '';
85 $welcome_mimetype = '';
87 if ( $conf->exists('warning_email') ) {
88 $warning_template = new Text::Template (
90 SOURCE => [ map "$_\n", $conf->config('warning_email') ]
91 ) or warn "can't create warning email template: $Text::Template::ERROR";
92 $warning_from = $conf->config('warning_email-from'); # || 'your-isp-is-dum'
93 $warning_subject = $conf->config('warning_email-subject') || 'Warning';
94 $warning_mimetype = $conf->config('warning_email-mimetype') || 'text/plain';
95 $warning_cc = $conf->config('warning_email-cc');
97 $warning_template = '';
99 $warning_subject = '';
100 $warning_mimetype = '';
103 $smtpmachine = $conf->config('smtpmachine');
104 $radius_password = $conf->config('radius-password') || 'Password';
105 $radius_ip = $conf->config('radius-ip') || 'Framed-IP-Address';
106 @pw_set = ( 'A'..'Z' ) if $conf->exists('password-generated-allcaps');
109 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
110 @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '!', '.', ',' );
114 my ( $hashref, $cache ) = @_;
115 if ( $hashref->{'svc_acct_svcnum'} ) {
116 $self->{'_domsvc'} = FS::svc_domain->new( {
117 'svcnum' => $hashref->{'domsvc'},
118 'domain' => $hashref->{'svc_acct_domain'},
119 'catchall' => $hashref->{'svc_acct_catchall'},
126 FS::svc_acct - Object methods for svc_acct records
132 $record = new FS::svc_acct \%hash;
133 $record = new FS::svc_acct { 'column' => 'value' };
135 $error = $record->insert;
137 $error = $new_record->replace($old_record);
139 $error = $record->delete;
141 $error = $record->check;
143 $error = $record->suspend;
145 $error = $record->unsuspend;
147 $error = $record->cancel;
149 %hash = $record->radius;
151 %hash = $record->radius_reply;
153 %hash = $record->radius_check;
155 $domain = $record->domain;
157 $svc_domain = $record->svc_domain;
159 $email = $record->email;
161 $seconds_since = $record->seconds_since($timestamp);
165 An FS::svc_acct object represents an account. FS::svc_acct inherits from
166 FS::svc_Common. The following fields are currently supported:
170 =item svcnum - primary key (assigned automatcially for new accounts)
174 =item _password - generated if blank
176 =item sec_phrase - security phrase
178 =item popnum - Point of presence (see L<FS::svc_acct_pop>)
186 =item dir - set automatically if blank (and uid is not)
190 =item quota - (unimplementd)
192 =item slipip - IP address
202 =item domsvc - svcnum from svc_domain
204 =item radius_I<Radius_Attribute> - I<Radius-Attribute> (reply)
206 =item rc_I<Radius_Attribute> - I<Radius-Attribute> (check)
216 Creates a new account. To add the account to the database, see L<"insert">.
223 'longname_plural' => 'Access accounts and mailboxes',
224 'sorts' => [ 'username', 'uid', ],
225 'display_weight' => 10,
226 'cancel_weight' => 50,
228 'dir' => 'Home directory',
231 def_label => 'UID (set to fixed and blank for no UIDs)',
234 'slipip' => 'IP address',
235 # 'popnum' => qq!<A HREF="$p/browse/svc_acct_pop.cgi/">POP number</A>!,
237 label => 'Access number',
239 select_table => 'svc_acct_pop',
240 select_key => 'popnum',
241 select_label => 'city',
247 disable_default => 1,
254 disable_inventory => 1,
257 '_password' => 'Password',
260 def_label => 'GID (when blank, defaults to UID)',
264 #desc =>'Shell (all service definitions should have a default or fixed shell that is present in the <b>shells</b> configuration file, set to blank for no shell tracking)',
266 def_label=> 'Shell (set to blank for no shell tracking)',
268 select_list => [ $conf->config('shells') ],
269 disable_inventory => 1,
272 'finger' => 'Real name (GECOS)',
275 #def_label => 'svcnum from svc_domain',
277 select_table => 'svc_domain',
278 select_key => 'svcnum',
279 select_label => 'domain',
280 disable_inventory => 1,
284 label => 'RADIUS groups',
285 type => 'radius_usergroup_selector',
286 disable_inventory => 1,
289 'seconds' => { label => 'Seconds',
291 disable_inventory => 1,
294 'upbytes' => { label => 'Upload',
296 disable_inventory => 1,
298 'format' => \&FS::UI::bytecount::display_bytecount,
299 'parse' => \&FS::UI::bytecount::parse_bytecount,
301 'downbytes' => { label => 'Download',
303 disable_inventory => 1,
305 'format' => \&FS::UI::bytecount::display_bytecount,
306 'parse' => \&FS::UI::bytecount::parse_bytecount,
308 'totalbytes'=> { label => 'Total up and download',
310 disable_inventory => 1,
312 'format' => \&FS::UI::bytecount::display_bytecount,
313 'parse' => \&FS::UI::bytecount::parse_bytecount,
315 'seconds_threshold' => { label => 'Seconds',
317 disable_inventory => 1,
320 'upbytes_threshold' => { label => 'Upload',
322 disable_inventory => 1,
324 'format' => \&FS::UI::bytecount::display_bytecount,
325 'parse' => \&FS::UI::bytecount::parse_bytecount,
327 'downbytes_threshold' => { label => 'Download',
329 disable_inventory => 1,
331 'format' => \&FS::UI::bytecount::display_bytecount,
332 'parse' => \&FS::UI::bytecount::parse_bytecount,
334 'totalbytes_threshold'=> { label => 'Total up and download',
336 disable_inventory => 1,
338 'format' => \&FS::UI::bytecount::display_bytecount,
339 'parse' => \&FS::UI::bytecount::parse_bytecount,
345 sub table { 'svc_acct'; }
349 #false laziness with edit/svc_acct.cgi
351 my( $self, $groups ) = @_;
352 if ( ref($groups) eq 'ARRAY' ) {
354 } elsif ( length($groups) ) {
355 [ split(/\s*,\s*/, $groups) ];
363 =item search_sql STRING
365 Class method which returns an SQL fragment to search for the given string.
370 my( $class, $string ) = @_;
371 if ( $string =~ /^([^@]+)@([^@]+)$/ ) {
372 my( $username, $domain ) = ( $1, $2 );
373 my $q_username = dbh->quote($username);
374 my @svc_domain = qsearch('svc_domain', { 'domain' => $domain } );
376 "svc_acct.username = $q_username AND ( ".
377 join( ' OR ', map { "svc_acct.domsvc = ". $_->svcnum; } @svc_domain ).
382 } elsif ( $string =~ /^(\d{1,3}\.){3}\d{1,3}$/ ) {
384 $class->search_sql_field('slipip', $string ).
386 $class->search_sql_field('username', $string ).
389 $class->search_sql_field('username', $string);
393 =item label [ END_TIMESTAMP [ START_TIMESTAMP ] ]
395 Returns the "username@domain" string for this account.
397 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
409 =item insert [ , OPTION => VALUE ... ]
411 Adds this account to the database. If there is an error, returns the error,
412 otherwise returns false.
414 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
415 defined. An FS::cust_svc record will be created and inserted.
417 The additional field I<usergroup> can optionally be defined; if so it should
418 contain an arrayref of group names. See L<FS::radius_usergroup>.
420 The additional field I<child_objects> can optionally be defined; if so it
421 should contain an arrayref of FS::tablename objects. They will have their
422 svcnum fields set and will be inserted after this record, but before any
423 exports are run. Each element of the array can also optionally be a
424 two-element array reference containing the child object and the name of an
425 alternate field to be filled in with the newly-inserted svcnum, for example
426 C<[ $svc_forward, 'srcsvc' ]>
428 Currently available options are: I<depend_jobnum>
430 If I<depend_jobnum> is set (to a scalar jobnum or an array reference of
431 jobnums), all provisioning jobs will have a dependancy on the supplied
432 jobnum(s) (they will not run until the specific job(s) complete(s)).
434 (TODOC: L<FS::queue> and L<freeside-queued>)
436 (TODOC: new exports!)
445 warn "[$me] insert called on $self: ". Dumper($self).
446 "\nwith options: ". Dumper(%options);
449 local $SIG{HUP} = 'IGNORE';
450 local $SIG{INT} = 'IGNORE';
451 local $SIG{QUIT} = 'IGNORE';
452 local $SIG{TERM} = 'IGNORE';
453 local $SIG{TSTP} = 'IGNORE';
454 local $SIG{PIPE} = 'IGNORE';
456 my $oldAutoCommit = $FS::UID::AutoCommit;
457 local $FS::UID::AutoCommit = 0;
460 my $error = $self->check;
461 return $error if $error;
463 if ( $self->svcnum && qsearchs('cust_svc',{'svcnum'=>$self->svcnum}) ) {
464 my $cust_svc = qsearchs('cust_svc',{'svcnum'=>$self->svcnum});
465 unless ( $cust_svc ) {
466 $dbh->rollback if $oldAutoCommit;
467 return "no cust_svc record found for svcnum ". $self->svcnum;
469 $self->pkgnum($cust_svc->pkgnum);
470 $self->svcpart($cust_svc->svcpart);
473 $error = $self->_check_duplicate;
475 $dbh->rollback if $oldAutoCommit;
480 $error = $self->SUPER::insert(
481 'jobnums' => \@jobnums,
482 'child_objects' => $self->child_objects,
486 $dbh->rollback if $oldAutoCommit;
490 if ( $self->usergroup ) {
491 foreach my $groupname ( @{$self->usergroup} ) {
492 my $radius_usergroup = new FS::radius_usergroup ( {
493 svcnum => $self->svcnum,
494 groupname => $groupname,
496 my $error = $radius_usergroup->insert;
498 $dbh->rollback if $oldAutoCommit;
504 unless ( $skip_fuzzyfiles ) {
505 $error = $self->queue_fuzzyfiles_update;
507 $dbh->rollback if $oldAutoCommit;
508 return "updating fuzzy search cache: $error";
512 my $cust_pkg = $self->cust_svc->cust_pkg;
515 my $cust_main = $cust_pkg->cust_main;
517 if ( $conf->exists('emailinvoiceautoalways')
518 || $conf->exists('emailinvoiceauto')
519 && ! $cust_main->invoicing_list_emailonly
521 my @invoicing_list = $cust_main->invoicing_list;
522 push @invoicing_list, $self->email;
523 $cust_main->invoicing_list(\@invoicing_list);
528 if ( $welcome_template && $cust_pkg ) {
529 my $to = join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
533 'custnum' => $self->custnum,
534 'username' => $self->username,
535 'password' => $self->_password,
536 'first' => $cust_main->first,
537 'last' => $cust_main->getfield('last'),
538 'pkg' => $cust_pkg->part_pkg->pkg,
540 my $wqueue = new FS::queue {
541 'svcnum' => $self->svcnum,
542 'job' => 'FS::svc_acct::send_email'
544 my $error = $wqueue->insert(
546 'from' => $welcome_from,
547 'subject' => $welcome_subject_template->fill_in( HASH => \%hash, ),
548 'mimetype' => $welcome_mimetype,
549 'body' => $welcome_template->fill_in( HASH => \%hash, ),
552 $dbh->rollback if $oldAutoCommit;
553 return "error queuing welcome email: $error";
556 if ( $options{'depend_jobnum'} ) {
557 warn "$me depend_jobnum found; adding to welcome email dependancies"
559 if ( ref($options{'depend_jobnum'}) ) {
560 warn "$me adding jobs ". join(', ', @{$options{'depend_jobnum'}} ).
561 "to welcome email dependancies"
563 push @jobnums, @{ $options{'depend_jobnum'} };
565 warn "$me adding job $options{'depend_jobnum'} ".
566 "to welcome email dependancies"
568 push @jobnums, $options{'depend_jobnum'};
572 foreach my $jobnum ( @jobnums ) {
573 my $error = $wqueue->depend_insert($jobnum);
575 $dbh->rollback if $oldAutoCommit;
576 return "error queuing welcome email job dependancy: $error";
586 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
592 Deletes this account from the database. If there is an error, returns the
593 error, otherwise returns false.
595 The corresponding FS::cust_svc record will be deleted as well.
597 (TODOC: new exports!)
604 return "can't delete system account" if $self->_check_system;
606 return "Can't delete an account which is a (svc_forward) source!"
607 if qsearch( 'svc_forward', { 'srcsvc' => $self->svcnum } );
609 return "Can't delete an account which is a (svc_forward) destination!"
610 if qsearch( 'svc_forward', { 'dstsvc' => $self->svcnum } );
612 return "Can't delete an account with (svc_www) web service!"
613 if qsearch( 'svc_www', { 'usersvc' => $self->svcnum } );
615 # what about records in session ? (they should refer to history table)
617 local $SIG{HUP} = 'IGNORE';
618 local $SIG{INT} = 'IGNORE';
619 local $SIG{QUIT} = 'IGNORE';
620 local $SIG{TERM} = 'IGNORE';
621 local $SIG{TSTP} = 'IGNORE';
622 local $SIG{PIPE} = 'IGNORE';
624 my $oldAutoCommit = $FS::UID::AutoCommit;
625 local $FS::UID::AutoCommit = 0;
628 foreach my $cust_main_invoice (
629 qsearch( 'cust_main_invoice', { 'dest' => $self->svcnum } )
631 unless ( defined($cust_main_invoice) ) {
632 warn "WARNING: something's wrong with qsearch";
635 my %hash = $cust_main_invoice->hash;
636 $hash{'dest'} = $self->email;
637 my $new = new FS::cust_main_invoice \%hash;
638 my $error = $new->replace($cust_main_invoice);
640 $dbh->rollback if $oldAutoCommit;
645 foreach my $svc_domain (
646 qsearch( 'svc_domain', { 'catchall' => $self->svcnum } )
648 my %hash = new FS::svc_domain->hash;
649 $hash{'catchall'} = '';
650 my $new = new FS::svc_domain \%hash;
651 my $error = $new->replace($svc_domain);
653 $dbh->rollback if $oldAutoCommit;
658 my $error = $self->SUPER::delete;
660 $dbh->rollback if $oldAutoCommit;
664 foreach my $radius_usergroup (
665 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } )
667 my $error = $radius_usergroup->delete;
669 $dbh->rollback if $oldAutoCommit;
674 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
678 =item replace OLD_RECORD
680 Replaces OLD_RECORD with this one in the database. If there is an error,
681 returns the error, otherwise returns false.
683 The additional field I<usergroup> can optionally be defined; if so it should
684 contain an arrayref of group names. See L<FS::radius_usergroup>.
690 my ( $new, $old ) = ( shift, shift );
692 warn "$me replacing $old with $new\n" if $DEBUG;
694 # We absolutely have to have an old vs. new record to make this work.
695 if (!defined($old)) {
696 $old = qsearchs( 'svc_acct', { 'svcnum' => $new->svcnum } );
699 return "can't modify system account" if $old->_check_system;
702 #no warnings 'numeric'; #alas, a 5.006-ism
705 foreach my $xid (qw( uid gid )) {
707 return "Can't change $xid!"
708 if ! $conf->exists("svc_acct-edit_$xid")
709 && $old->$xid() != $new->$xid()
710 && $new->cust_svc->part_svc->part_svc_column($xid)->columnflag ne 'F'
715 #change homdir when we change username
716 $new->setfield('dir', '') if $old->username ne $new->username;
718 local $SIG{HUP} = 'IGNORE';
719 local $SIG{INT} = 'IGNORE';
720 local $SIG{QUIT} = 'IGNORE';
721 local $SIG{TERM} = 'IGNORE';
722 local $SIG{TSTP} = 'IGNORE';
723 local $SIG{PIPE} = 'IGNORE';
725 my $oldAutoCommit = $FS::UID::AutoCommit;
726 local $FS::UID::AutoCommit = 0;
729 # redundant, but so $new->usergroup gets set
730 $error = $new->check;
731 return $error if $error;
733 $old->usergroup( [ $old->radius_groups ] );
735 warn $old->email. " old groups: ". join(' ',@{$old->usergroup}). "\n";
736 warn $new->email. "new groups: ". join(' ',@{$new->usergroup}). "\n";
738 if ( $new->usergroup ) {
739 #(sorta) false laziness with FS::part_export::sqlradius::_export_replace
740 my @newgroups = @{$new->usergroup};
741 foreach my $oldgroup ( @{$old->usergroup} ) {
742 if ( grep { $oldgroup eq $_ } @newgroups ) {
743 @newgroups = grep { $oldgroup ne $_ } @newgroups;
746 my $radius_usergroup = qsearchs('radius_usergroup', {
747 svcnum => $old->svcnum,
748 groupname => $oldgroup,
750 my $error = $radius_usergroup->delete;
752 $dbh->rollback if $oldAutoCommit;
753 return "error deleting radius_usergroup $oldgroup: $error";
757 foreach my $newgroup ( @newgroups ) {
758 my $radius_usergroup = new FS::radius_usergroup ( {
759 svcnum => $new->svcnum,
760 groupname => $newgroup,
762 my $error = $radius_usergroup->insert;
764 $dbh->rollback if $oldAutoCommit;
765 return "error adding radius_usergroup $newgroup: $error";
771 if ( $old->username ne $new->username || $old->domsvc != $new->domsvc ) {
772 $new->svcpart( $new->cust_svc->svcpart ) unless $new->svcpart;
773 $error = $new->_check_duplicate;
775 $dbh->rollback if $oldAutoCommit;
780 $error = $new->SUPER::replace($old);
782 $dbh->rollback if $oldAutoCommit;
783 return $error if $error;
786 if ( $new->username ne $old->username && ! $skip_fuzzyfiles ) {
787 $error = $new->queue_fuzzyfiles_update;
789 $dbh->rollback if $oldAutoCommit;
790 return "updating fuzzy search cache: $error";
794 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
798 =item queue_fuzzyfiles_update
800 Used by insert & replace to update the fuzzy search cache
804 sub queue_fuzzyfiles_update {
807 local $SIG{HUP} = 'IGNORE';
808 local $SIG{INT} = 'IGNORE';
809 local $SIG{QUIT} = 'IGNORE';
810 local $SIG{TERM} = 'IGNORE';
811 local $SIG{TSTP} = 'IGNORE';
812 local $SIG{PIPE} = 'IGNORE';
814 my $oldAutoCommit = $FS::UID::AutoCommit;
815 local $FS::UID::AutoCommit = 0;
818 my $queue = new FS::queue {
819 'svcnum' => $self->svcnum,
820 'job' => 'FS::svc_acct::append_fuzzyfiles'
822 my $error = $queue->insert($self->username);
824 $dbh->rollback if $oldAutoCommit;
825 return "queueing job (transaction rolled back): $error";
828 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
836 Suspends this account by calling export-specific suspend hooks. If there is
837 an error, returns the error, otherwise returns false.
839 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
845 return "can't suspend system account" if $self->_check_system;
846 $self->SUPER::suspend;
851 Unsuspends this account by by calling export-specific suspend hooks. If there
852 is an error, returns the error, otherwise returns false.
854 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
860 my %hash = $self->hash;
861 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
862 $hash{_password} = $1;
863 my $new = new FS::svc_acct ( \%hash );
864 my $error = $new->replace($self);
865 return $error if $error;
868 $self->SUPER::unsuspend;
873 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
875 If the B<auto_unset_catchall> configuration option is set, this method will
876 automatically remove any references to the canceled service in the catchall
877 field of svc_domain. This allows packages that contain both a svc_domain and
878 its catchall svc_acct to be canceled in one step.
883 # Only one thing to do at this level
885 foreach my $svc_domain (
886 qsearch( 'svc_domain', { catchall => $self->svcnum } ) ) {
887 if($conf->exists('auto_unset_catchall')) {
888 my %hash = $svc_domain->hash;
889 $hash{catchall} = '';
890 my $new = new FS::svc_domain ( \%hash );
891 my $error = $new->replace($svc_domain);
892 return $error if $error;
894 return "cannot unprovision svc_acct #".$self->svcnum.
895 " while assigned as catchall for svc_domain #".$svc_domain->svcnum;
899 $self->SUPER::cancel;
905 Checks all fields to make sure this is a valid service. If there is an error,
906 returns the error, otherwise returns false. Called by the insert and replace
909 Sets any fixed values; see L<FS::part_svc>.
916 my($recref) = $self->hashref;
918 my $x = $self->setfixed( $self->_fieldhandlers );
919 return $x unless ref($x);
922 if ( $part_svc->part_svc_column('usergroup')->columnflag eq "F" ) {
924 [ split(',', $part_svc->part_svc_column('usergroup')->columnvalue) ] );
927 my $error = $self->ut_numbern('svcnum')
928 #|| $self->ut_number('domsvc')
929 || $self->ut_foreign_key('domsvc', 'svc_domain', 'svcnum' )
930 || $self->ut_textn('sec_phrase')
931 || $self->ut_snumbern('seconds')
932 || $self->ut_snumbern('upbytes')
933 || $self->ut_snumbern('downbytes')
934 || $self->ut_snumbern('totalbytes')
936 return $error if $error;
938 my $ulen = $usernamemax || $self->dbdef_table->column('username')->length;
939 if ( $username_uppercase ) {
940 $recref->{username} =~ /^([a-z0-9_\-\.\&\%]{$usernamemin,$ulen})$/i
941 or return gettext('illegal_username'). " ($usernamemin-$ulen): ". $recref->{username};
942 $recref->{username} = $1;
944 $recref->{username} =~ /^([a-z0-9_\-\.\&\%]{$usernamemin,$ulen})$/
945 or return gettext('illegal_username'). " ($usernamemin-$ulen): ". $recref->{username};
946 $recref->{username} = $1;
949 if ( $username_letterfirst ) {
950 $recref->{username} =~ /^[a-z]/ or return gettext('illegal_username');
951 } elsif ( $username_letter ) {
952 $recref->{username} =~ /[a-z]/ or return gettext('illegal_username');
954 if ( $username_noperiod ) {
955 $recref->{username} =~ /\./ and return gettext('illegal_username');
957 if ( $username_nounderscore ) {
958 $recref->{username} =~ /_/ and return gettext('illegal_username');
960 if ( $username_nodash ) {
961 $recref->{username} =~ /\-/ and return gettext('illegal_username');
963 unless ( $username_ampersand ) {
964 $recref->{username} =~ /\&/ and return gettext('illegal_username');
966 if ( $password_noampersand ) {
967 $recref->{_password} =~ /\&/ and return gettext('illegal_password');
969 if ( $password_noexclamation ) {
970 $recref->{_password} =~ /\!/ and return gettext('illegal_password');
972 unless ( $username_percent ) {
973 $recref->{username} =~ /\%/ and return gettext('illegal_username');
976 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum: ".$recref->{popnum};
977 $recref->{popnum} = $1;
978 return "Unknown popnum" unless
979 ! $recref->{popnum} ||
980 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
982 unless ( $part_svc->part_svc_column('uid')->columnflag eq 'F' ) {
984 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
985 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
987 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
988 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
989 #not all systems use gid=uid
990 #you can set a fixed gid in part_svc
992 return "Only root can have uid 0"
993 if $recref->{uid} == 0
994 && $recref->{username} !~ /^(root|toor|smtp)$/;
996 unless ( $recref->{username} eq 'sync' ) {
997 if ( grep $_ eq $recref->{shell}, @shells ) {
998 $recref->{shell} = (grep $_ eq $recref->{shell}, @shells)[0];
1000 return "Illegal shell \`". $self->shell. "\'; ".
1001 $conf->dir. "/shells contains: @shells";
1004 $recref->{shell} = '/bin/sync';
1008 $recref->{gid} ne '' ?
1009 return "Can't have gid without uid" : ( $recref->{gid}='' );
1010 #$recref->{dir} ne '' ?
1011 # return "Can't have directory without uid" : ( $recref->{dir}='' );
1012 $recref->{shell} ne '' ?
1013 return "Can't have shell without uid" : ( $recref->{shell}='' );
1016 unless ( $part_svc->part_svc_column('dir')->columnflag eq 'F' ) {
1018 $recref->{dir} =~ /^([\/\w\-\.\&]*)$/
1019 or return "Illegal directory: ". $recref->{dir};
1020 $recref->{dir} = $1;
1021 return "Illegal directory"
1022 if $recref->{dir} =~ /(^|\/)\.+(\/|$)/; #no .. component
1023 return "Illegal directory"
1024 if $recref->{dir} =~ /\&/ && ! $username_ampersand;
1025 unless ( $recref->{dir} ) {
1026 $recref->{dir} = $dir_prefix . '/';
1027 if ( $dirhash > 0 ) {
1028 for my $h ( 1 .. $dirhash ) {
1029 $recref->{dir} .= substr($recref->{username}, $h-1, 1). '/';
1031 } elsif ( $dirhash < 0 ) {
1032 for my $h ( reverse $dirhash .. -1 ) {
1033 $recref->{dir} .= substr($recref->{username}, $h, 1). '/';
1036 $recref->{dir} .= $recref->{username};
1042 # $error = $self->ut_textn('finger');
1043 # return $error if $error;
1044 if ( $self->getfield('finger') eq '' ) {
1045 my $cust_pkg = $self->svcnum
1046 ? $self->cust_svc->cust_pkg
1047 : qsearchs('cust_pkg', { 'pkgnum' => $self->getfield('pkgnum') } );
1049 my $cust_main = $cust_pkg->cust_main;
1050 $self->setfield('finger', $cust_main->first.' '.$cust_main->get('last') );
1053 $self->getfield('finger') =~
1054 /^([\w \t\!\@\#\$\%\&\(\)\-\+\;\'\"\,\.\?\/\*\<\>]*)$/
1055 or return "Illegal finger: ". $self->getfield('finger');
1056 $self->setfield('finger', $1);
1058 $recref->{quota} =~ /^(\w*)$/ or return "Illegal quota";
1059 $recref->{quota} = $1;
1061 unless ( $part_svc->part_svc_column('slipip')->columnflag eq 'F' ) {
1062 if ( $recref->{slipip} eq '' ) {
1063 $recref->{slipip} = '';
1064 } elsif ( $recref->{slipip} eq '0e0' ) {
1065 $recref->{slipip} = '0e0';
1067 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
1068 or return "Illegal slipip: ". $self->slipip;
1069 $recref->{slipip} = $1;
1074 #arbitrary RADIUS stuff; allow ut_textn for now
1075 foreach ( grep /^radius_/, fields('svc_acct') ) {
1076 $self->ut_textn($_);
1079 #generate a password if it is blank
1080 $recref->{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) )
1081 unless ( $recref->{_password} );
1083 #if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,16})$/ ) {
1084 if ( $recref->{_password} =~ /^((\*SUSPENDED\* |!!?)?)([^\t\n]{$passwordmin,$passwordmax})$/ ) {
1085 $recref->{_password} = $1.$3;
1086 #uncomment this to encrypt password immediately upon entry, or run
1087 #bin/crypt_pw in cron to give new users a window during which their
1088 #password is available to techs, for faxing, etc. (also be aware of
1090 #$recref->{password} = $1.
1091 # crypt($3,$saltset[int(rand(64))].$saltset[int(rand(64))]
1093 } elsif ( $recref->{_password} =~ /^((\*SUSPENDED\* |!!?)?)([\w\.\/\$\;\+]{13,64})$/ ) {
1094 $recref->{_password} = $1.$3;
1095 } elsif ( $recref->{_password} eq '*' ) {
1096 $recref->{_password} = '*';
1097 } elsif ( $recref->{_password} eq '!' ) {
1098 $recref->{_password} = '!';
1099 } elsif ( $recref->{_password} eq '!!' ) {
1100 $recref->{_password} = '!!';
1102 #return "Illegal password";
1103 return gettext('illegal_password'). " $passwordmin-$passwordmax ".
1104 FS::Msgcat::_gettext('illegal_password_characters').
1105 ": ". $recref->{_password};
1108 $self->SUPER::check;
1113 Internal function to check the username against the list of system usernames
1114 from the I<system_usernames> configuration value. Returns true if the username
1115 is listed on the system username list.
1121 scalar( grep { $self->username eq $_ || $self->email eq $_ }
1122 $conf->config('system_usernames')
1126 =item _check_duplicate
1128 Internal function to check for duplicates usernames, username@domain pairs and
1131 If the I<global_unique-username> configuration value is set to B<username> or
1132 B<username@domain>, enforces global username or username@domain uniqueness.
1134 In all cases, check for duplicate uids and usernames or username@domain pairs
1135 per export and with identical I<svcpart> values.
1139 sub _check_duplicate {
1142 my $global_unique = $conf->config('global_unique-username') || 'none';
1143 return '' if $global_unique eq 'disabled';
1145 #this is Pg-specific. what to do for mysql etc?
1146 # ( mysql LOCK TABLES certainly isn't equivalent or useful here :/ )
1147 warn "$me locking svc_acct table for duplicate search" if $DEBUG;
1148 dbh->do("LOCK TABLE svc_acct IN SHARE ROW EXCLUSIVE MODE")
1150 warn "$me acquired svc_acct table lock for duplicate search" if $DEBUG;
1152 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
1153 unless ( $part_svc ) {
1154 return 'unknown svcpart '. $self->svcpart;
1157 my @dup_user = grep { !$self->svcnum || $_->svcnum != $self->svcnum }
1158 qsearch( 'svc_acct', { 'username' => $self->username } );
1159 return gettext('username_in_use')
1160 if $global_unique eq 'username' && @dup_user;
1162 my @dup_userdomain = grep { !$self->svcnum || $_->svcnum != $self->svcnum }
1163 qsearch( 'svc_acct', { 'username' => $self->username,
1164 'domsvc' => $self->domsvc } );
1165 return gettext('username_in_use')
1166 if $global_unique eq 'username@domain' && @dup_userdomain;
1169 if ( $part_svc->part_svc_column('uid')->columnflag ne 'F'
1170 && $self->username !~ /^(toor|(hyla)?fax)$/ ) {
1171 @dup_uid = grep { !$self->svcnum || $_->svcnum != $self->svcnum }
1172 qsearch( 'svc_acct', { 'uid' => $self->uid } );
1177 if ( @dup_user || @dup_userdomain || @dup_uid ) {
1178 my $exports = FS::part_export::export_info('svc_acct');
1179 my %conflict_user_svcpart;
1180 my %conflict_userdomain_svcpart = ( $self->svcpart => 'SELF', );
1182 foreach my $part_export ( $part_svc->part_export ) {
1184 #this will catch to the same exact export
1185 my @svcparts = map { $_->svcpart } $part_export->export_svc;
1187 #this will catch to exports w/same exporthost+type ???
1188 #my @other_part_export = qsearch('part_export', {
1189 # 'machine' => $part_export->machine,
1190 # 'exporttype' => $part_export->exporttype,
1192 #foreach my $other_part_export ( @other_part_export ) {
1193 # push @svcparts, map { $_->svcpart }
1194 # qsearch('export_svc', { 'exportnum' => $part_export->exportnum });
1197 #my $nodomain = $exports->{$part_export->exporttype}{'nodomain'};
1198 #silly kludge to avoid uninitialized value errors
1199 my $nodomain = exists( $exports->{$part_export->exporttype}{'nodomain'} )
1200 ? $exports->{$part_export->exporttype}{'nodomain'}
1202 if ( $nodomain =~ /^Y/i ) {
1203 $conflict_user_svcpart{$_} = $part_export->exportnum
1206 $conflict_userdomain_svcpart{$_} = $part_export->exportnum
1211 foreach my $dup_user ( @dup_user ) {
1212 my $dup_svcpart = $dup_user->cust_svc->svcpart;
1213 if ( exists($conflict_user_svcpart{$dup_svcpart}) ) {
1214 return "duplicate username: conflicts with svcnum ". $dup_user->svcnum.
1215 " via exportnum ". $conflict_user_svcpart{$dup_svcpart};
1219 foreach my $dup_userdomain ( @dup_userdomain ) {
1220 my $dup_svcpart = $dup_userdomain->cust_svc->svcpart;
1221 if ( exists($conflict_userdomain_svcpart{$dup_svcpart}) ) {
1222 return "duplicate username\@domain: conflicts with svcnum ".
1223 $dup_userdomain->svcnum. " via exportnum ".
1224 $conflict_userdomain_svcpart{$dup_svcpart};
1228 foreach my $dup_uid ( @dup_uid ) {
1229 my $dup_svcpart = $dup_uid->cust_svc->svcpart;
1230 if ( exists($conflict_user_svcpart{$dup_svcpart})
1231 || exists($conflict_userdomain_svcpart{$dup_svcpart}) ) {
1232 return "duplicate uid: conflicts with svcnum ". $dup_uid->svcnum.
1233 " via exportnum ". $conflict_user_svcpart{$dup_svcpart}
1234 || $conflict_userdomain_svcpart{$dup_svcpart};
1246 Depriciated, use radius_reply instead.
1251 carp "FS::svc_acct::radius depriciated, use radius_reply";
1252 $_[0]->radius_reply;
1257 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1258 reply attributes of this record.
1260 Note that this is now the preferred method for reading RADIUS attributes -
1261 accessing the columns directly is discouraged, as the column names are
1262 expected to change in the future.
1269 return %{ $self->{'radius_reply'} }
1270 if exists $self->{'radius_reply'};
1275 my($column, $attrib) = ($1, $2);
1276 #$attrib =~ s/_/\-/g;
1277 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1278 } grep { /^radius_/ && $self->getfield($_) } fields( $self->table );
1280 if ( $self->slipip && $self->slipip ne '0e0' ) {
1281 $reply{$radius_ip} = $self->slipip;
1284 if ( $self->seconds !~ /^$/ ) {
1285 $reply{'Session-Timeout'} = $self->seconds;
1293 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1294 check attributes of this record.
1296 Note that this is now the preferred method for reading RADIUS attributes -
1297 accessing the columns directly is discouraged, as the column names are
1298 expected to change in the future.
1305 return %{ $self->{'radius_check'} }
1306 if exists $self->{'radius_check'};
1311 my($column, $attrib) = ($1, $2);
1312 #$attrib =~ s/_/\-/g;
1313 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1314 } grep { /^rc_/ && $self->getfield($_) } fields( $self->table );
1316 my $password = $self->_password;
1317 my $pw_attrib = length($password) <= 12 ? $radius_password : 'Crypt-Password'; $check{$pw_attrib} = $password;
1319 my $cust_svc = $self->cust_svc;
1320 die "FATAL: no cust_svc record for svc_acct.svcnum ". $self->svcnum. "\n"
1322 my $cust_pkg = $cust_svc->cust_pkg;
1323 if ( $cust_pkg && $cust_pkg->part_pkg->is_prepaid && $cust_pkg->bill ) {
1324 $check{'Expiration'} = time2str('%B %e %Y %T', $cust_pkg->bill ); #http://lists.cistron.nl/pipermail/freeradius-users/2005-January/040184.html
1333 This method instructs the object to "snapshot" or freeze RADIUS check and
1334 reply attributes to the current values.
1338 #bah, my english is too broken this morning
1339 #Of note is the "Expiration" attribute, which, for accounts in prepaid packages, is typically defined on-the-fly as the associated packages cust_pkg.bill. (This is used by
1340 #the FS::cust_pkg's replace method to trigger the correct export updates when
1341 #package dates change)
1346 $self->{$_} = { $self->$_() }
1347 foreach qw( radius_reply radius_check );
1351 =item forget_snapshot
1353 This methos instructs the object to forget any previously snapshotted
1354 RADIUS check and reply attributes.
1358 sub forget_snapshot {
1362 foreach qw( radius_reply radius_check );
1366 =item domain [ END_TIMESTAMP [ START_TIMESTAMP ] ]
1368 Returns the domain associated with this account.
1370 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
1377 die "svc_acct.domsvc is null for svcnum ". $self->svcnum unless $self->domsvc;
1378 my $svc_domain = $self->svc_domain(@_)
1379 or die "no svc_domain.svcnum for svc_acct.domsvc ". $self->domsvc;
1380 $svc_domain->domain;
1385 Returns the FS::svc_domain record for this account's domain (see
1390 # FS::h_svc_acct has a history-aware svc_domain override
1395 ? $self->{'_domsvc'}
1396 : qsearchs( 'svc_domain', { 'svcnum' => $self->domsvc } );
1401 Returns the FS::cust_svc record for this account (see L<FS::cust_svc>).
1405 #inherited from svc_Common
1407 =item email [ END_TIMESTAMP [ START_TIMESTAMP ] ]
1409 Returns an email address associated with the account.
1411 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
1418 $self->username. '@'. $self->domain(@_);
1423 Returns an array of FS::acct_snarf records associated with the account.
1424 If the acct_snarf table does not exist or there are no associated records,
1425 an empty list is returned
1431 return () unless dbdef->table('acct_snarf');
1432 eval "use FS::acct_snarf;";
1434 qsearch('acct_snarf', { 'svcnum' => $self->svcnum } );
1437 =item decrement_upbytes OCTETS
1439 Decrements the I<upbytes> field of this record by the given amount. If there
1440 is an error, returns the error, otherwise returns false.
1444 sub decrement_upbytes {
1445 shift->_op_usage('-', 'upbytes', @_);
1448 =item increment_upbytes OCTETS
1450 Increments the I<upbytes> field of this record by the given amount. If there
1451 is an error, returns the error, otherwise returns false.
1455 sub increment_upbytes {
1456 shift->_op_usage('+', 'upbytes', @_);
1459 =item decrement_downbytes OCTETS
1461 Decrements the I<downbytes> field of this record by the given amount. If there
1462 is an error, returns the error, otherwise returns false.
1466 sub decrement_downbytes {
1467 shift->_op_usage('-', 'downbytes', @_);
1470 =item increment_downbytes OCTETS
1472 Increments the I<downbytes> field of this record by the given amount. If there
1473 is an error, returns the error, otherwise returns false.
1477 sub increment_downbytes {
1478 shift->_op_usage('+', 'downbytes', @_);
1481 =item decrement_totalbytes OCTETS
1483 Decrements the I<totalbytes> field of this record by the given amount. If there
1484 is an error, returns the error, otherwise returns false.
1488 sub decrement_totalbytes {
1489 shift->_op_usage('-', 'totalbytes', @_);
1492 =item increment_totalbytes OCTETS
1494 Increments the I<totalbytes> field of this record by the given amount. If there
1495 is an error, returns the error, otherwise returns false.
1499 sub increment_totalbytes {
1500 shift->_op_usage('+', 'totalbytes', @_);
1503 =item decrement_seconds SECONDS
1505 Decrements the I<seconds> field of this record by the given amount. If there
1506 is an error, returns the error, otherwise returns false.
1510 sub decrement_seconds {
1511 shift->_op_usage('-', 'seconds', @_);
1514 =item increment_seconds SECONDS
1516 Increments the I<seconds> field of this record by the given amount. If there
1517 is an error, returns the error, otherwise returns false.
1521 sub increment_seconds {
1522 shift->_op_usage('+', 'seconds', @_);
1530 my %op2condition = (
1531 '-' => sub { my($self, $column, $amount) = @_;
1532 $self->$column - $amount <= 0;
1534 '+' => sub { my($self, $column, $amount) = @_;
1535 $self->$column + $amount > 0;
1538 my %op2warncondition = (
1539 '-' => sub { my($self, $column, $amount) = @_;
1540 my $threshold = $column . '_threshold';
1541 $self->$column - $amount <= $self->$threshold + 0;
1543 '+' => sub { my($self, $column, $amount) = @_;
1544 $self->$column + $amount > 0;
1549 my( $self, $op, $column, $amount ) = @_;
1551 warn "$me _op_usage called for $column on svcnum ". $self->svcnum.
1552 ' ('. $self->email. "): $op $amount\n"
1555 return '' unless $amount;
1557 local $SIG{HUP} = 'IGNORE';
1558 local $SIG{INT} = 'IGNORE';
1559 local $SIG{QUIT} = 'IGNORE';
1560 local $SIG{TERM} = 'IGNORE';
1561 local $SIG{TSTP} = 'IGNORE';
1562 local $SIG{PIPE} = 'IGNORE';
1564 my $oldAutoCommit = $FS::UID::AutoCommit;
1565 local $FS::UID::AutoCommit = 0;
1568 my $sql = "UPDATE svc_acct SET $column = ".
1569 " CASE WHEN $column IS NULL THEN 0 ELSE $column END ". #$column||0
1570 " $op ? WHERE svcnum = ?";
1574 my $sth = $dbh->prepare( $sql )
1575 or die "Error preparing $sql: ". $dbh->errstr;
1576 my $rv = $sth->execute($amount, $self->svcnum);
1577 die "Error executing $sql: ". $sth->errstr
1578 unless defined($rv);
1579 die "Can't update $column for svcnum". $self->svcnum
1582 my $action = $op2action{$op};
1584 if ( &{$op2condition{$op}}($self, $column, $amount) &&
1585 ( $action eq 'suspend' && !$self->overlimit
1586 || $action eq 'unsuspend' && $self->overlimit )
1588 foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
1589 if ($part_export->option('overlimit_groups')) {
1591 my $other = new FS::svc_acct $self->hashref;
1592 my $groups = &{ $self->_fieldhandlers->{'usergroup'} }
1593 ($self, $part_export->option('overlimit_groups'));
1594 $other->usergroup( $groups );
1595 if ($action eq 'suspend'){
1596 $new = $other; $old = $self;
1598 $new = $self; $old = $other;
1600 my $error = $part_export->export_replace($new, $old);
1601 $error ||= $self->overlimit($action);
1603 $dbh->rollback if $oldAutoCommit;
1604 return "Error replacing radius groups in export, ${op}: $error";
1610 if ( $conf->exists("svc_acct-usage_$action")
1611 && &{$op2condition{$op}}($self, $column, $amount) ) {
1612 #my $error = $self->$action();
1613 my $error = $self->cust_svc->cust_pkg->$action();
1614 # $error ||= $self->overlimit($action);
1616 $dbh->rollback if $oldAutoCommit;
1617 return "Error ${action}ing: $error";
1621 if ($warning_template && &{$op2warncondition{$op}}($self, $column, $amount)) {
1622 my $wqueue = new FS::queue {
1623 'svcnum' => $self->svcnum,
1624 'job' => 'FS::svc_acct::reached_threshold',
1629 $to = $warning_cc if &{$op2condition{$op}}($self, $column, $amount);
1633 my $error = $wqueue->insert(
1634 'svcnum' => $self->svcnum,
1636 'column' => $column,
1640 $dbh->rollback if $oldAutoCommit;
1641 return "Error queuing threshold activity: $error";
1645 warn "$me update successful; committing\n"
1647 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1653 my( $self, $valueref ) = @_;
1655 warn "$me set_usage called for svcnum ". $self->svcnum.
1656 ' ('. $self->email. "): ".
1657 join(', ', map { "$_ => " . $valueref->{$_}} keys %$valueref) . "\n"
1660 local $SIG{HUP} = 'IGNORE';
1661 local $SIG{INT} = 'IGNORE';
1662 local $SIG{QUIT} = 'IGNORE';
1663 local $SIG{TERM} = 'IGNORE';
1664 local $SIG{TSTP} = 'IGNORE';
1665 local $SIG{PIPE} = 'IGNORE';
1667 local $FS::svc_Common::noexport_hack = 1;
1668 my $oldAutoCommit = $FS::UID::AutoCommit;
1669 local $FS::UID::AutoCommit = 0;
1674 foreach my $field (keys %$valueref){
1675 $reset = 1 if $valueref->{$field};
1676 $self->setfield($field, $valueref->{$field});
1677 $self->setfield( $field.'_threshold',
1678 int($self->getfield($field)
1679 * ( $conf->exists('svc_acct-usage_threshold')
1680 ? 1 - $conf->config('svc_acct-usage_threshold')/100
1685 $handyhash{$field} = $self->getfield($field);
1686 $handyhash{$field.'_threshold'} = $self->getfield($field.'_threshold');
1688 #my $error = $self->replace; #NO! we avoid the call to ->check for
1689 #die $error if $error; #services not explicity changed via the UI
1691 my $sql = "UPDATE svc_acct SET " .
1692 join (',', map { "$_ = ?" } (keys %handyhash) ).
1693 " WHERE svcnum = ?";
1698 if (scalar(keys %handyhash)) {
1699 my $sth = $dbh->prepare( $sql )
1700 or die "Error preparing $sql: ". $dbh->errstr;
1701 my $rv = $sth->execute((values %handyhash), $self->svcnum);
1702 die "Error executing $sql: ". $sth->errstr
1703 unless defined($rv);
1704 die "Can't update usage for svcnum ". $self->svcnum
1711 if ($self->overlimit) {
1712 $error = $self->overlimit('unsuspend');
1713 foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
1714 if ($part_export->option('overlimit_groups')) {
1715 my $old = new FS::svc_acct $self->hashref;
1716 my $groups = &{ $self->_fieldhandlers->{'usergroup'} }
1717 ($self, $part_export->option('overlimit_groups'));
1718 $old->usergroup( $groups );
1719 $error ||= $part_export->export_replace($self, $old);
1724 if ( $conf->exists("svc_acct-usage_unsuspend")) {
1725 $error ||= $self->cust_svc->cust_pkg->unsuspend;
1728 $dbh->rollback if $oldAutoCommit;
1729 return "Error unsuspending: $error";
1733 warn "$me update successful; committing\n"
1735 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1741 =item recharge HASHREF
1743 Increments usage columns by the amount specified in HASHREF as
1744 column=>amount pairs.
1749 my ($self, $vhash) = @_;
1752 warn "[$me] recharge called on $self: ". Dumper($self).
1753 "\nwith vhash: ". Dumper($vhash);
1756 my $oldAutoCommit = $FS::UID::AutoCommit;
1757 local $FS::UID::AutoCommit = 0;
1761 foreach my $column (keys %$vhash){
1762 $error ||= $self->_op_usage('+', $column, $vhash->{$column});
1766 $dbh->rollback if $oldAutoCommit;
1768 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1773 =item is_rechargeable
1775 Returns true if this svc_account can be "recharged" and false otherwise.
1779 sub is_rechargable {
1781 $self->seconds ne ''
1782 || $self->upbytes ne ''
1783 || $self->downbytes ne ''
1784 || $self->totalbytes ne '';
1787 =item seconds_since TIMESTAMP
1789 Returns the number of seconds this account has been online since TIMESTAMP,
1790 according to the session monitor (see L<FS::Session>).
1792 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
1793 L<Time::Local> and L<Date::Parse> for conversion functions.
1797 #note: POD here, implementation in FS::cust_svc
1800 $self->cust_svc->seconds_since(@_);
1803 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
1805 Returns the numbers of seconds this account has been online between
1806 TIMESTAMP_START (inclusive) and TIMESTAMP_END (exclusive), according to an
1807 external SQL radacct table, specified via sqlradius export. Sessions which
1808 started in the specified range but are still open are counted from session
1809 start to the end of the range (unless they are over 1 day old, in which case
1810 they are presumed missing their stop record and not counted). Also, sessions
1811 which end in the range but started earlier are counted from the start of the
1812 range to session end. Finally, sessions which start before the range but end
1813 after are counted for the entire range.
1815 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
1816 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
1821 #note: POD here, implementation in FS::cust_svc
1822 sub seconds_since_sqlradacct {
1824 $self->cust_svc->seconds_since_sqlradacct(@_);
1827 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
1829 Returns the sum of the given attribute for all accounts (see L<FS::svc_acct>)
1830 in this package for sessions ending between TIMESTAMP_START (inclusive) and
1831 TIMESTAMP_END (exclusive).
1833 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
1834 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
1839 #note: POD here, implementation in FS::cust_svc
1840 sub attribute_since_sqlradacct {
1842 $self->cust_svc->attribute_since_sqlradacct(@_);
1845 =item get_session_history TIMESTAMP_START TIMESTAMP_END
1847 Returns an array of hash references of this customers login history for the
1848 given time range. (document this better)
1852 sub get_session_history {
1854 $self->cust_svc->get_session_history(@_);
1857 =item get_cdrs TIMESTAMP_START TIMESTAMP_END [ 'OPTION' => 'VALUE ... ]
1862 my($self, $start, $end, %opt ) = @_;
1864 my $did = $self->username; #yup
1866 my $prefix = $opt{'default_prefix'}; #convergent.au '+61'
1868 my $for_update = $opt{'for_update'} ? 'FOR UPDATE' : '';
1870 #SELECT $for_update * FROM cdr
1871 # WHERE calldate >= $start #need a conversion
1872 # AND calldate < $end #ditto
1873 # AND ( charged_party = "$did"
1874 # OR charged_party = "$prefix$did" #if length($prefix);
1875 # OR ( ( charged_party IS NULL OR charged_party = '' )
1877 # ( src = "$did" OR src = "$prefix$did" ) # if length($prefix)
1880 # AND ( freesidestatus IS NULL OR freesidestatus = '' )
1883 if ( length($prefix) ) {
1885 " AND ( charged_party = '$did'
1886 OR charged_party = '$prefix$did'
1887 OR ( ( charged_party IS NULL OR charged_party = '' )
1889 ( src = '$did' OR src = '$prefix$did' )
1895 " AND ( charged_party = '$did'
1896 OR ( ( charged_party IS NULL OR charged_party = '' )
1906 'select' => "$for_update *",
1909 #( freesidestatus IS NULL OR freesidestatus = '' )
1910 'freesidestatus' => '',
1912 'extra_sql' => $charged_or_src,
1920 Returns all RADIUS groups for this account (see L<FS::radius_usergroup>).
1926 if ( $self->usergroup ) {
1927 confess "explicitly specified usergroup not an arrayref: ". $self->usergroup
1928 unless ref($self->usergroup) eq 'ARRAY';
1929 #when provisioning records, export callback runs in svc_Common.pm before
1930 #radius_usergroup records can be inserted...
1931 @{$self->usergroup};
1933 map { $_->groupname }
1934 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } );
1938 =item clone_suspended
1940 Constructor used by FS::part_export::_export_suspend fallback. Document
1945 sub clone_suspended {
1947 my %hash = $self->hash;
1948 $hash{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) );
1949 new FS::svc_acct \%hash;
1952 =item clone_kludge_unsuspend
1954 Constructor used by FS::part_export::_export_unsuspend fallback. Document
1959 sub clone_kludge_unsuspend {
1961 my %hash = $self->hash;
1962 $hash{_password} = '';
1963 new FS::svc_acct \%hash;
1966 =item check_password
1968 Checks the supplied password against the (possibly encrypted) password in the
1969 database. Returns true for a successful authentication, false for no match.
1971 Currently supported encryptions are: classic DES crypt() and MD5
1975 sub check_password {
1976 my($self, $check_password) = @_;
1978 #remove old-style SUSPENDED kludge, they should be allowed to login to
1979 #self-service and pay up
1980 ( my $password = $self->_password ) =~ s/^\*SUSPENDED\* //;
1982 #eventually should check a "password-encoding" field
1983 if ( $password =~ /^(\*|!!?)$/ ) { #no self-service login
1985 } elsif ( length($password) < 13 ) { #plaintext
1986 $check_password eq $password;
1987 } elsif ( length($password) == 13 ) { #traditional DES crypt
1988 crypt($check_password, $password) eq $password;
1989 } elsif ( $password =~ /^\$1\$/ ) { #MD5 crypt
1990 unix_md5_crypt($check_password, $password) eq $password;
1991 } elsif ( $password =~ /^\$2a?\$/ ) { #Blowfish
1992 warn "Can't check password: Blowfish encryption not yet supported, svcnum".
1993 $self->svcnum. "\n";
1996 warn "Can't check password: Unrecognized encryption for svcnum ".
1997 $self->svcnum. "\n";
2003 =item crypt_password [ DEFAULT_ENCRYPTION_TYPE ]
2005 Returns an encrypted password, either by passing through an encrypted password
2006 in the database or by encrypting a plaintext password from the database.
2008 The optional DEFAULT_ENCRYPTION_TYPE parameter can be set to I<crypt> (classic
2009 UNIX DES crypt), I<md5> (md5 crypt supported by most modern Linux and BSD
2010 distrubtions), or (eventually) I<blowfish> (blowfish hashing supported by
2011 OpenBSD, SuSE, other Linux distibutions with pam_unix2, etc.). The default
2012 encryption type is only used if the password is not already encrypted in the
2017 sub crypt_password {
2019 #eventually should check a "password-encoding" field
2020 if ( length($self->_password) == 13
2021 || $self->_password =~ /^\$(1|2a?)\$/
2022 || $self->_password =~ /^(\*|NP|\*LK\*|!!?)$/
2027 my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2028 if ( $encryption eq 'crypt' ) {
2031 $saltset[int(rand(64))].$saltset[int(rand(64))]
2033 } elsif ( $encryption eq 'md5' ) {
2034 unix_md5_crypt( $self->_password );
2035 } elsif ( $encryption eq 'blowfish' ) {
2036 croak "unknown encryption method $encryption";
2038 croak "unknown encryption method $encryption";
2043 =item ldap_password [ DEFAULT_ENCRYPTION_TYPE ]
2045 Returns an encrypted password in "LDAP" format, with a curly-bracked prefix
2046 describing the format, for example, "{CRYPT}94pAVyK/4oIBk" or
2047 "{PLAIN-MD5}5426824942db4253f87a1009fd5d2d4f".
2049 The optional DEFAULT_ENCRYPTION_TYPE is not yet used, but the idea is for it
2050 to work the same as the B</crypt_password> method.
2056 #eventually should check a "password-encoding" field
2057 if ( length($self->_password) == 13 ) { #crypt
2058 return '{CRYPT}'. $self->_password;
2059 } elsif ( $self->_password =~ /^\$1\$(.*)$/ && length($1) == 31 ) { #passwdMD5
2061 } elsif ( $self->_password =~ /^\$2a?\$(.*)$/ ) { #Blowfish
2062 die "Blowfish encryption not supported in this context, svcnum ".
2063 $self->svcnum. "\n";
2064 } elsif ( $self->_password =~ /^(\w{48})$/ ) { #LDAP SSHA
2065 return '{SSHA}'. $1;
2066 } elsif ( $self->_password =~ /^(\w{64})$/ ) { #LDAP NS-MTA-MD5
2067 return '{NS-MTA-MD5}'. $1;
2069 return '{PLAIN}'. $self->_password;
2070 #my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2071 #if ( $encryption eq 'crypt' ) {
2072 # return '{CRYPT}'. crypt(
2074 # $saltset[int(rand(64))].$saltset[int(rand(64))]
2076 #} elsif ( $encryption eq 'md5' ) {
2077 # unix_md5_crypt( $self->_password );
2078 #} elsif ( $encryption eq 'blowfish' ) {
2079 # croak "unknown encryption method $encryption";
2081 # croak "unknown encryption method $encryption";
2086 =item domain_slash_username
2088 Returns $domain/$username/
2092 sub domain_slash_username {
2094 $self->domain. '/'. $self->username. '/';
2097 =item virtual_maildir
2099 Returns $domain/maildirs/$username/
2103 sub virtual_maildir {
2105 $self->domain. '/maildirs/'. $self->username. '/';
2116 This is the FS::svc_acct job-queue-able version. It still uses
2117 FS::Misc::send_email under-the-hood.
2124 eval "use FS::Misc qw(send_email)";
2127 $opt{mimetype} ||= 'text/plain';
2128 $opt{mimetype} .= '; charset="iso-8859-1"' unless $opt{mimetype} =~ /charset/;
2130 my $error = send_email(
2131 'from' => $opt{from},
2133 'subject' => $opt{subject},
2134 'content-type' => $opt{mimetype},
2135 'body' => [ map "$_\n", split("\n", $opt{body}) ],
2137 die $error if $error;
2140 =item check_and_rebuild_fuzzyfiles
2144 sub check_and_rebuild_fuzzyfiles {
2145 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
2146 -e "$dir/svc_acct.username"
2147 or &rebuild_fuzzyfiles;
2150 =item rebuild_fuzzyfiles
2154 sub rebuild_fuzzyfiles {
2156 use Fcntl qw(:flock);
2158 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
2162 open(USERNAMELOCK,">>$dir/svc_acct.username")
2163 or die "can't open $dir/svc_acct.username: $!";
2164 flock(USERNAMELOCK,LOCK_EX)
2165 or die "can't lock $dir/svc_acct.username: $!";
2167 my @all_username = map $_->getfield('username'), qsearch('svc_acct', {});
2169 open (USERNAMECACHE,">$dir/svc_acct.username.tmp")
2170 or die "can't open $dir/svc_acct.username.tmp: $!";
2171 print USERNAMECACHE join("\n", @all_username), "\n";
2172 close USERNAMECACHE or die "can't close $dir/svc_acct.username.tmp: $!";
2174 rename "$dir/svc_acct.username.tmp", "$dir/svc_acct.username";
2184 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
2185 open(USERNAMECACHE,"<$dir/svc_acct.username")
2186 or die "can't open $dir/svc_acct.username: $!";
2187 my @array = map { chomp; $_; } <USERNAMECACHE>;
2188 close USERNAMECACHE;
2192 =item append_fuzzyfiles USERNAME
2196 sub append_fuzzyfiles {
2197 my $username = shift;
2199 &check_and_rebuild_fuzzyfiles;
2201 use Fcntl qw(:flock);
2203 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
2205 open(USERNAME,">>$dir/svc_acct.username")
2206 or die "can't open $dir/svc_acct.username: $!";
2207 flock(USERNAME,LOCK_EX)
2208 or die "can't lock $dir/svc_acct.username: $!";
2210 print USERNAME "$username\n";
2212 flock(USERNAME,LOCK_UN)
2213 or die "can't unlock $dir/svc_acct.username: $!";
2221 =item radius_usergroup_selector GROUPS_ARRAYREF [ SELECTNAME ]
2225 sub radius_usergroup_selector {
2226 my $sel_groups = shift;
2227 my %sel_groups = map { $_=>1 } @$sel_groups;
2229 my $selectname = shift || 'radius_usergroup';
2232 my $sth = $dbh->prepare(
2233 'SELECT DISTINCT(groupname) FROM radius_usergroup ORDER BY groupname'
2234 ) or die $dbh->errstr;
2235 $sth->execute() or die $sth->errstr;
2236 my @all_groups = map { $_->[0] } @{$sth->fetchall_arrayref};
2240 function ${selectname}_doadd(object) {
2241 var myvalue = object.${selectname}_add.value;
2242 var optionName = new Option(myvalue,myvalue,false,true);
2243 var length = object.$selectname.length;
2244 object.$selectname.options[length] = optionName;
2245 object.${selectname}_add.value = "";
2248 <SELECT MULTIPLE NAME="$selectname">
2251 foreach my $group ( @all_groups ) {
2252 $html .= qq(<OPTION VALUE="$group");
2253 if ( $sel_groups{$group} ) {
2254 $html .= ' SELECTED';
2255 $sel_groups{$group} = 0;
2257 $html .= ">$group</OPTION>\n";
2259 foreach my $group ( grep { $sel_groups{$_} } keys %sel_groups ) {
2260 $html .= qq(<OPTION VALUE="$group" SELECTED>$group</OPTION>\n);
2262 $html .= '</SELECT>';
2264 $html .= qq!<BR><INPUT TYPE="text" NAME="${selectname}_add">!.
2265 qq!<INPUT TYPE="button" VALUE="Add new group" onClick="${selectname}_doadd(this.form)">!;
2270 =item reached_threshold
2272 Performs some activities when svc_acct thresholds (such as number of seconds
2273 remaining) are reached.
2277 sub reached_threshold {
2280 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $opt{'svcnum'} } );
2281 die "Cannot find svc_acct with svcnum " . $opt{'svcnum'} unless $svc_acct;
2283 if ( $opt{'op'} eq '+' ){
2284 $svc_acct->setfield( $opt{'column'}.'_threshold',
2285 int($svc_acct->getfield($opt{'column'})
2286 * ( $conf->exists('svc_acct-usage_threshold')
2287 ? $conf->config('svc_acct-usage_threshold')/100
2292 my $error = $svc_acct->replace;
2293 die $error if $error;
2294 }elsif ( $opt{'op'} eq '-' ){
2296 my $threshold = $svc_acct->getfield( $opt{'column'}.'_threshold' );
2297 return '' if ($threshold eq '' );
2299 $svc_acct->setfield( $opt{'column'}.'_threshold', 0 );
2300 my $error = $svc_acct->replace;
2301 die $error if $error; # email next time, i guess
2303 if ( $warning_template ) {
2304 eval "use FS::Misc qw(send_email)";
2307 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
2308 my $cust_main = $cust_pkg->cust_main;
2310 my $to = join(', ', grep { $_ !~ /^(POST|FAX)$/ }
2311 $cust_main->invoicing_list,
2312 ($opt{'to'} ? $opt{'to'} : ())
2315 my $mimetype = $warning_mimetype;
2316 $mimetype .= '; charset="iso-8859-1"' unless $opt{mimetype} =~ /charset/;
2318 my $body = $warning_template->fill_in( HASH => {
2319 'custnum' => $cust_main->custnum,
2320 'username' => $svc_acct->username,
2321 'password' => $svc_acct->_password,
2322 'first' => $cust_main->first,
2323 'last' => $cust_main->getfield('last'),
2324 'pkg' => $cust_pkg->part_pkg->pkg,
2325 'column' => $opt{'column'},
2326 'amount' => $opt{'column'} =~/bytes/
2327 ? FS::UI::bytecount::display_bytecount($svc_acct->getfield($opt{'column'}))
2328 : $svc_acct->getfield($opt{'column'}),
2329 'threshold' => $opt{'column'} =~/bytes/
2330 ? FS::UI::bytecount::display_bytecount($threshold)
2335 my $error = send_email(
2336 'from' => $warning_from,
2338 'subject' => $warning_subject,
2339 'content-type' => $mimetype,
2340 'body' => [ map "$_\n", split("\n", $body) ],
2342 die $error if $error;
2345 die "unknown op: " . $opt{'op'};
2353 The $recref stuff in sub check should be cleaned up.
2355 The suspend, unsuspend and cancel methods update the database, but not the
2356 current object. This is probably a bug as it's unexpected and
2359 radius_usergroup_selector? putting web ui components in here? they should
2360 probably live somewhere else...
2362 insertion of RADIUS group stuff in insert could be done with child_objects now
2363 (would probably clean up export of them too)
2367 L<FS::svc_Common>, edit/part_svc.cgi from an installed web interface,
2368 export.html from the base documentation, L<FS::Record>, L<FS::Conf>,
2369 L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, L<FS::queue>,
2370 L<freeside-queued>), L<FS::svc_acct_pop>,
2371 schema.html from the base documentation.