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 Authen::Passphrase;
25 use FS::UID qw( datasrc );
27 use FS::Record qw( qsearch qsearchs fields dbh dbdef );
28 use FS::Msgcat qw(gettext);
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';
108 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
109 @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '!', '.', ',' );
113 my ( $hashref, $cache ) = @_;
114 if ( $hashref->{'svc_acct_svcnum'} ) {
115 $self->{'_domsvc'} = FS::svc_domain->new( {
116 'svcnum' => $hashref->{'domsvc'},
117 'domain' => $hashref->{'svc_acct_domain'},
118 'catchall' => $hashref->{'svc_acct_catchall'},
125 FS::svc_acct - Object methods for svc_acct records
131 $record = new FS::svc_acct \%hash;
132 $record = new FS::svc_acct { 'column' => 'value' };
134 $error = $record->insert;
136 $error = $new_record->replace($old_record);
138 $error = $record->delete;
140 $error = $record->check;
142 $error = $record->suspend;
144 $error = $record->unsuspend;
146 $error = $record->cancel;
148 %hash = $record->radius;
150 %hash = $record->radius_reply;
152 %hash = $record->radius_check;
154 $domain = $record->domain;
156 $svc_domain = $record->svc_domain;
158 $email = $record->email;
160 $seconds_since = $record->seconds_since($timestamp);
164 An FS::svc_acct object represents an account. FS::svc_acct inherits from
165 FS::svc_Common. The following fields are currently supported:
169 =item svcnum - primary key (assigned automatcially for new accounts)
173 =item _password - generated if blank
175 =item _password_encoding - plain, crypt, ldap (or empty for autodetection)
177 =item sec_phrase - security phrase
179 =item popnum - Point of presence (see L<FS::svc_acct_pop>)
187 =item dir - set automatically if blank (and uid is not)
191 =item quota - (unimplementd)
193 =item slipip - IP address
203 =item domsvc - svcnum from svc_domain
205 =item radius_I<Radius_Attribute> - I<Radius-Attribute> (reply)
207 =item rc_I<Radius_Attribute> - I<Radius-Attribute> (check)
217 Creates a new account. To add the account to the database, see L<"insert">.
224 'longname_plural' => 'Access accounts and mailboxes',
225 'sorts' => [ 'username', 'uid', ],
226 'display_weight' => 10,
227 'cancel_weight' => 50,
229 'dir' => 'Home directory',
232 def_label => 'UID (set to fixed and blank for no UIDs)',
235 'slipip' => 'IP address',
236 # 'popnum' => qq!<A HREF="$p/browse/svc_acct_pop.cgi/">POP number</A>!,
238 label => 'Access number',
240 select_table => 'svc_acct_pop',
241 select_key => 'popnum',
242 select_label => 'city',
248 disable_default => 1,
255 disable_inventory => 1,
258 '_password' => 'Password',
261 def_label => 'GID (when blank, defaults to UID)',
265 #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)',
267 def_label=> 'Shell (set to blank for no shell tracking)',
269 select_list => [ $conf->config('shells') ],
270 disable_inventory => 1,
273 'finger' => 'Real name (GECOS)',
276 #def_label => 'svcnum from svc_domain',
278 select_table => 'svc_domain',
279 select_key => 'svcnum',
280 select_label => 'domain',
281 disable_inventory => 1,
285 label => 'RADIUS groups',
286 type => 'radius_usergroup_selector',
287 disable_inventory => 1,
290 'seconds' => { label => 'Seconds',
292 disable_inventory => 1,
299 sub table { 'svc_acct'; }
303 #false laziness with edit/svc_acct.cgi
305 my( $self, $groups ) = @_;
306 if ( ref($groups) eq 'ARRAY' ) {
308 } elsif ( length($groups) ) {
309 [ split(/\s*,\s*/, $groups) ];
317 =item search_sql STRING
319 Class method which returns an SQL fragment to search for the given string.
324 my( $class, $string ) = @_;
325 if ( $string =~ /^([^@]+)@([^@]+)$/ ) {
326 my( $username, $domain ) = ( $1, $2 );
327 my $q_username = dbh->quote($username);
328 my @svc_domain = qsearch('svc_domain', { 'domain' => $domain } );
330 "svc_acct.username = $q_username AND ( ".
331 join( ' OR ', map { "svc_acct.domsvc = ". $_->svcnum; } @svc_domain ).
336 } elsif ( $string =~ /^(\d{1,3}\.){3}\d{1,3}$/ ) {
338 $class->search_sql_field('slipip', $string ).
340 $class->search_sql_field('username', $string ).
343 $class->search_sql_field('username', $string);
347 =item label [ END_TIMESTAMP [ START_TIMESTAMP ] ]
349 Returns the "username@domain" string for this account.
351 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
363 =item insert [ , OPTION => VALUE ... ]
365 Adds this account to the database. If there is an error, returns the error,
366 otherwise returns false.
368 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
369 defined. An FS::cust_svc record will be created and inserted.
371 The additional field I<usergroup> can optionally be defined; if so it should
372 contain an arrayref of group names. See L<FS::radius_usergroup>.
374 The additional field I<child_objects> can optionally be defined; if so it
375 should contain an arrayref of FS::tablename objects. They will have their
376 svcnum fields set and will be inserted after this record, but before any
377 exports are run. Each element of the array can also optionally be a
378 two-element array reference containing the child object and the name of an
379 alternate field to be filled in with the newly-inserted svcnum, for example
380 C<[ $svc_forward, 'srcsvc' ]>
382 Currently available options are: I<depend_jobnum>
384 If I<depend_jobnum> is set (to a scalar jobnum or an array reference of
385 jobnums), all provisioning jobs will have a dependancy on the supplied
386 jobnum(s) (they will not run until the specific job(s) complete(s)).
388 (TODOC: L<FS::queue> and L<freeside-queued>)
390 (TODOC: new exports!)
399 warn "[$me] insert called on $self: ". Dumper($self).
400 "\nwith options: ". Dumper(%options);
403 local $SIG{HUP} = 'IGNORE';
404 local $SIG{INT} = 'IGNORE';
405 local $SIG{QUIT} = 'IGNORE';
406 local $SIG{TERM} = 'IGNORE';
407 local $SIG{TSTP} = 'IGNORE';
408 local $SIG{PIPE} = 'IGNORE';
410 my $oldAutoCommit = $FS::UID::AutoCommit;
411 local $FS::UID::AutoCommit = 0;
414 my $error = $self->check;
415 return $error if $error;
417 if ( $self->svcnum && qsearchs('cust_svc',{'svcnum'=>$self->svcnum}) ) {
418 my $cust_svc = qsearchs('cust_svc',{'svcnum'=>$self->svcnum});
419 unless ( $cust_svc ) {
420 $dbh->rollback if $oldAutoCommit;
421 return "no cust_svc record found for svcnum ". $self->svcnum;
423 $self->pkgnum($cust_svc->pkgnum);
424 $self->svcpart($cust_svc->svcpart);
427 $error = $self->_check_duplicate;
429 $dbh->rollback if $oldAutoCommit;
434 $error = $self->SUPER::insert(
435 'jobnums' => \@jobnums,
436 'child_objects' => $self->child_objects,
440 $dbh->rollback if $oldAutoCommit;
444 if ( $self->usergroup ) {
445 foreach my $groupname ( @{$self->usergroup} ) {
446 my $radius_usergroup = new FS::radius_usergroup ( {
447 svcnum => $self->svcnum,
448 groupname => $groupname,
450 my $error = $radius_usergroup->insert;
452 $dbh->rollback if $oldAutoCommit;
458 unless ( $skip_fuzzyfiles ) {
459 $error = $self->queue_fuzzyfiles_update;
461 $dbh->rollback if $oldAutoCommit;
462 return "updating fuzzy search cache: $error";
466 my $cust_pkg = $self->cust_svc->cust_pkg;
469 my $cust_main = $cust_pkg->cust_main;
471 if ( $conf->exists('emailinvoiceautoalways')
472 || $conf->exists('emailinvoiceauto')
473 && ! $cust_main->invoicing_list_emailonly
475 my @invoicing_list = $cust_main->invoicing_list;
476 push @invoicing_list, $self->email;
477 $cust_main->invoicing_list(\@invoicing_list);
482 if ( $welcome_template && $cust_pkg ) {
483 my $to = join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
487 'custnum' => $self->custnum,
488 'username' => $self->username,
489 'password' => $self->_password,
490 'first' => $cust_main->first,
491 'last' => $cust_main->getfield('last'),
492 'pkg' => $cust_pkg->part_pkg->pkg,
494 my $wqueue = new FS::queue {
495 'svcnum' => $self->svcnum,
496 'job' => 'FS::svc_acct::send_email'
498 my $error = $wqueue->insert(
500 'from' => $welcome_from,
501 'subject' => $welcome_subject_template->fill_in( HASH => \%hash, ),
502 'mimetype' => $welcome_mimetype,
503 'body' => $welcome_template->fill_in( HASH => \%hash, ),
506 $dbh->rollback if $oldAutoCommit;
507 return "error queuing welcome email: $error";
510 if ( $options{'depend_jobnum'} ) {
511 warn "$me depend_jobnum found; adding to welcome email dependancies"
513 if ( ref($options{'depend_jobnum'}) ) {
514 warn "$me adding jobs ". join(', ', @{$options{'depend_jobnum'}} ).
515 "to welcome email dependancies"
517 push @jobnums, @{ $options{'depend_jobnum'} };
519 warn "$me adding job $options{'depend_jobnum'} ".
520 "to welcome email dependancies"
522 push @jobnums, $options{'depend_jobnum'};
526 foreach my $jobnum ( @jobnums ) {
527 my $error = $wqueue->depend_insert($jobnum);
529 $dbh->rollback if $oldAutoCommit;
530 return "error queuing welcome email job dependancy: $error";
540 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
546 Deletes this account from the database. If there is an error, returns the
547 error, otherwise returns false.
549 The corresponding FS::cust_svc record will be deleted as well.
551 (TODOC: new exports!)
558 return "can't delete system account" if $self->_check_system;
560 return "Can't delete an account which is a (svc_forward) source!"
561 if qsearch( 'svc_forward', { 'srcsvc' => $self->svcnum } );
563 return "Can't delete an account which is a (svc_forward) destination!"
564 if qsearch( 'svc_forward', { 'dstsvc' => $self->svcnum } );
566 return "Can't delete an account with (svc_www) web service!"
567 if qsearch( 'svc_www', { 'usersvc' => $self->svcnum } );
569 # what about records in session ? (they should refer to history table)
571 local $SIG{HUP} = 'IGNORE';
572 local $SIG{INT} = 'IGNORE';
573 local $SIG{QUIT} = 'IGNORE';
574 local $SIG{TERM} = 'IGNORE';
575 local $SIG{TSTP} = 'IGNORE';
576 local $SIG{PIPE} = 'IGNORE';
578 my $oldAutoCommit = $FS::UID::AutoCommit;
579 local $FS::UID::AutoCommit = 0;
582 foreach my $cust_main_invoice (
583 qsearch( 'cust_main_invoice', { 'dest' => $self->svcnum } )
585 unless ( defined($cust_main_invoice) ) {
586 warn "WARNING: something's wrong with qsearch";
589 my %hash = $cust_main_invoice->hash;
590 $hash{'dest'} = $self->email;
591 my $new = new FS::cust_main_invoice \%hash;
592 my $error = $new->replace($cust_main_invoice);
594 $dbh->rollback if $oldAutoCommit;
599 foreach my $svc_domain (
600 qsearch( 'svc_domain', { 'catchall' => $self->svcnum } )
602 my %hash = new FS::svc_domain->hash;
603 $hash{'catchall'} = '';
604 my $new = new FS::svc_domain \%hash;
605 my $error = $new->replace($svc_domain);
607 $dbh->rollback if $oldAutoCommit;
612 foreach my $radius_usergroup (
613 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } )
615 my $error = $radius_usergroup->delete;
617 $dbh->rollback if $oldAutoCommit;
622 my $error = $self->SUPER::delete;
624 $dbh->rollback if $oldAutoCommit;
628 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
632 =item replace OLD_RECORD
634 Replaces OLD_RECORD with this one in the database. If there is an error,
635 returns the error, otherwise returns false.
637 The additional field I<usergroup> can optionally be defined; if so it should
638 contain an arrayref of group names. See L<FS::radius_usergroup>.
644 my ( $new, $old ) = ( shift, shift );
646 warn "$me replacing $old with $new\n" if $DEBUG;
648 # We absolutely have to have an old vs. new record to make this work.
649 if (!defined($old)) {
650 $old = qsearchs( 'svc_acct', { 'svcnum' => $new->svcnum } );
653 return "can't modify system account" if $old->_check_system;
656 #no warnings 'numeric'; #alas, a 5.006-ism
659 foreach my $xid (qw( uid gid )) {
661 return "Can't change $xid!"
662 if ! $conf->exists("svc_acct-edit_$xid")
663 && $old->$xid() != $new->$xid()
664 && $new->cust_svc->part_svc->part_svc_column($xid)->columnflag ne 'F'
669 #change homdir when we change username
670 $new->setfield('dir', '') if $old->username ne $new->username;
672 local $SIG{HUP} = 'IGNORE';
673 local $SIG{INT} = 'IGNORE';
674 local $SIG{QUIT} = 'IGNORE';
675 local $SIG{TERM} = 'IGNORE';
676 local $SIG{TSTP} = 'IGNORE';
677 local $SIG{PIPE} = 'IGNORE';
679 my $oldAutoCommit = $FS::UID::AutoCommit;
680 local $FS::UID::AutoCommit = 0;
683 # redundant, but so $new->usergroup gets set
684 $error = $new->check;
685 return $error if $error;
687 $old->usergroup( [ $old->radius_groups ] );
689 warn $old->email. " old groups: ". join(' ',@{$old->usergroup}). "\n";
690 warn $new->email. "new groups: ". join(' ',@{$new->usergroup}). "\n";
692 if ( $new->usergroup ) {
693 #(sorta) false laziness with FS::part_export::sqlradius::_export_replace
694 my @newgroups = @{$new->usergroup};
695 foreach my $oldgroup ( @{$old->usergroup} ) {
696 if ( grep { $oldgroup eq $_ } @newgroups ) {
697 @newgroups = grep { $oldgroup ne $_ } @newgroups;
700 my $radius_usergroup = qsearchs('radius_usergroup', {
701 svcnum => $old->svcnum,
702 groupname => $oldgroup,
704 my $error = $radius_usergroup->delete;
706 $dbh->rollback if $oldAutoCommit;
707 return "error deleting radius_usergroup $oldgroup: $error";
711 foreach my $newgroup ( @newgroups ) {
712 my $radius_usergroup = new FS::radius_usergroup ( {
713 svcnum => $new->svcnum,
714 groupname => $newgroup,
716 my $error = $radius_usergroup->insert;
718 $dbh->rollback if $oldAutoCommit;
719 return "error adding radius_usergroup $newgroup: $error";
725 if ( $old->username ne $new->username || $old->domsvc != $new->domsvc ) {
726 $new->svcpart( $new->cust_svc->svcpart ) unless $new->svcpart;
727 $error = $new->_check_duplicate;
729 $dbh->rollback if $oldAutoCommit;
734 $error = $new->SUPER::replace($old);
736 $dbh->rollback if $oldAutoCommit;
737 return $error if $error;
740 if ( $new->username ne $old->username && ! $skip_fuzzyfiles ) {
741 $error = $new->queue_fuzzyfiles_update;
743 $dbh->rollback if $oldAutoCommit;
744 return "updating fuzzy search cache: $error";
748 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
752 =item queue_fuzzyfiles_update
754 Used by insert & replace to update the fuzzy search cache
758 sub queue_fuzzyfiles_update {
761 local $SIG{HUP} = 'IGNORE';
762 local $SIG{INT} = 'IGNORE';
763 local $SIG{QUIT} = 'IGNORE';
764 local $SIG{TERM} = 'IGNORE';
765 local $SIG{TSTP} = 'IGNORE';
766 local $SIG{PIPE} = 'IGNORE';
768 my $oldAutoCommit = $FS::UID::AutoCommit;
769 local $FS::UID::AutoCommit = 0;
772 my $queue = new FS::queue {
773 'svcnum' => $self->svcnum,
774 'job' => 'FS::svc_acct::append_fuzzyfiles'
776 my $error = $queue->insert($self->username);
778 $dbh->rollback if $oldAutoCommit;
779 return "queueing job (transaction rolled back): $error";
782 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
790 Suspends this account by calling export-specific suspend hooks. If there is
791 an error, returns the error, otherwise returns false.
793 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
799 return "can't suspend system account" if $self->_check_system;
800 $self->SUPER::suspend;
805 Unsuspends this account by by calling export-specific suspend hooks. If there
806 is an error, returns the error, otherwise returns false.
808 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
814 my %hash = $self->hash;
815 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
816 $hash{_password} = $1;
817 my $new = new FS::svc_acct ( \%hash );
818 my $error = $new->replace($self);
819 return $error if $error;
822 $self->SUPER::unsuspend;
827 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
829 If the B<auto_unset_catchall> configuration option is set, this method will
830 automatically remove any references to the canceled service in the catchall
831 field of svc_domain. This allows packages that contain both a svc_domain and
832 its catchall svc_acct to be canceled in one step.
837 # Only one thing to do at this level
839 foreach my $svc_domain (
840 qsearch( 'svc_domain', { catchall => $self->svcnum } ) ) {
841 if($conf->exists('auto_unset_catchall')) {
842 my %hash = $svc_domain->hash;
843 $hash{catchall} = '';
844 my $new = new FS::svc_domain ( \%hash );
845 my $error = $new->replace($svc_domain);
846 return $error if $error;
848 return "cannot unprovision svc_acct #".$self->svcnum.
849 " while assigned as catchall for svc_domain #".$svc_domain->svcnum;
853 $self->SUPER::cancel;
859 Checks all fields to make sure this is a valid service. If there is an error,
860 returns the error, otherwise returns false. Called by the insert and replace
863 Sets any fixed values; see L<FS::part_svc>.
870 my($recref) = $self->hashref;
872 my $x = $self->setfixed( $self->_fieldhandlers );
873 return $x unless ref($x);
876 if ( $part_svc->part_svc_column('usergroup')->columnflag eq "F" ) {
878 [ split(',', $part_svc->part_svc_column('usergroup')->columnvalue) ] );
881 my $error = $self->ut_numbern('svcnum')
882 #|| $self->ut_number('domsvc')
883 || $self->ut_foreign_key('domsvc', 'svc_domain', 'svcnum' )
884 || $self->ut_textn('sec_phrase')
885 || $self->ut_snumbern('seconds')
886 || $self->ut_snumbern('upbytes')
887 || $self->ut_snumbern('downbytes')
888 || $self->ut_snumbern('totalbytes')
889 || $self->ut_enum( '_password_encoding',
890 [ '', qw( plain crypt ldap ) ]
893 return $error if $error;
895 my $ulen = $usernamemax || $self->dbdef_table->column('username')->length;
896 if ( $username_uppercase ) {
897 $recref->{username} =~ /^([a-z0-9_\-\.\&\%]{$usernamemin,$ulen})$/i
898 or return gettext('illegal_username'). " ($usernamemin-$ulen): ". $recref->{username};
899 $recref->{username} = $1;
901 $recref->{username} =~ /^([a-z0-9_\-\.\&\%]{$usernamemin,$ulen})$/
902 or return gettext('illegal_username'). " ($usernamemin-$ulen): ". $recref->{username};
903 $recref->{username} = $1;
906 if ( $username_letterfirst ) {
907 $recref->{username} =~ /^[a-z]/ or return gettext('illegal_username');
908 } elsif ( $username_letter ) {
909 $recref->{username} =~ /[a-z]/ or return gettext('illegal_username');
911 if ( $username_noperiod ) {
912 $recref->{username} =~ /\./ and return gettext('illegal_username');
914 if ( $username_nounderscore ) {
915 $recref->{username} =~ /_/ and return gettext('illegal_username');
917 if ( $username_nodash ) {
918 $recref->{username} =~ /\-/ and return gettext('illegal_username');
920 unless ( $username_ampersand ) {
921 $recref->{username} =~ /\&/ and return gettext('illegal_username');
923 unless ( $username_percent ) {
924 $recref->{username} =~ /\%/ and return gettext('illegal_username');
927 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum: ".$recref->{popnum};
928 $recref->{popnum} = $1;
929 return "Unknown popnum" unless
930 ! $recref->{popnum} ||
931 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
933 unless ( $part_svc->part_svc_column('uid')->columnflag eq 'F' ) {
935 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
936 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
938 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
939 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
940 #not all systems use gid=uid
941 #you can set a fixed gid in part_svc
943 return "Only root can have uid 0"
944 if $recref->{uid} == 0
945 && $recref->{username} !~ /^(root|toor|smtp)$/;
947 unless ( $recref->{username} eq 'sync' ) {
948 if ( grep $_ eq $recref->{shell}, @shells ) {
949 $recref->{shell} = (grep $_ eq $recref->{shell}, @shells)[0];
951 return "Illegal shell \`". $self->shell. "\'; ".
952 $conf->dir. "/shells contains: @shells";
955 $recref->{shell} = '/bin/sync';
959 $recref->{gid} ne '' ?
960 return "Can't have gid without uid" : ( $recref->{gid}='' );
961 #$recref->{dir} ne '' ?
962 # return "Can't have directory without uid" : ( $recref->{dir}='' );
963 $recref->{shell} ne '' ?
964 return "Can't have shell without uid" : ( $recref->{shell}='' );
967 unless ( $part_svc->part_svc_column('dir')->columnflag eq 'F' ) {
969 $recref->{dir} =~ /^([\/\w\-\.\&]*)$/
970 or return "Illegal directory: ". $recref->{dir};
972 return "Illegal directory"
973 if $recref->{dir} =~ /(^|\/)\.+(\/|$)/; #no .. component
974 return "Illegal directory"
975 if $recref->{dir} =~ /\&/ && ! $username_ampersand;
976 unless ( $recref->{dir} ) {
977 $recref->{dir} = $dir_prefix . '/';
978 if ( $dirhash > 0 ) {
979 for my $h ( 1 .. $dirhash ) {
980 $recref->{dir} .= substr($recref->{username}, $h-1, 1). '/';
982 } elsif ( $dirhash < 0 ) {
983 for my $h ( reverse $dirhash .. -1 ) {
984 $recref->{dir} .= substr($recref->{username}, $h, 1). '/';
987 $recref->{dir} .= $recref->{username};
993 # $error = $self->ut_textn('finger');
994 # return $error if $error;
995 if ( $self->getfield('finger') eq '' ) {
996 my $cust_pkg = $self->svcnum
997 ? $self->cust_svc->cust_pkg
998 : qsearchs('cust_pkg', { 'pkgnum' => $self->getfield('pkgnum') } );
1000 my $cust_main = $cust_pkg->cust_main;
1001 $self->setfield('finger', $cust_main->first.' '.$cust_main->get('last') );
1004 $self->getfield('finger') =~
1005 /^([\w \t\!\@\#\$\%\&\(\)\-\+\;\'\"\,\.\?\/\*\<\>]*)$/
1006 or return "Illegal finger: ". $self->getfield('finger');
1007 $self->setfield('finger', $1);
1009 $recref->{quota} =~ /^(\w*)$/ or return "Illegal quota";
1010 $recref->{quota} = $1;
1012 unless ( $part_svc->part_svc_column('slipip')->columnflag eq 'F' ) {
1013 if ( $recref->{slipip} eq '' ) {
1014 $recref->{slipip} = '';
1015 } elsif ( $recref->{slipip} eq '0e0' ) {
1016 $recref->{slipip} = '0e0';
1018 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
1019 or return "Illegal slipip: ". $self->slipip;
1020 $recref->{slipip} = $1;
1025 #arbitrary RADIUS stuff; allow ut_textn for now
1026 foreach ( grep /^radius_/, fields('svc_acct') ) {
1027 $self->ut_textn($_);
1030 if ( $recref->{_password_encoding} eq 'ldap' ) {
1032 if ( $recref->{_password} =~ /^(\{[\w\-]+\})(!?.{0,64})$/ ) {
1033 $recref->{_password} = uc($1).$2;
1035 return 'Illegal (ldap-encoded) password: '. $recref->{_password};
1038 } elsif ( $recref->{_password_encoding} eq 'crypt' ) {
1040 if ( $recref->{_password} =~
1041 #/^(\$\w+\$.*|[\w\+\/]{13}|_[\w\+\/]{19}|\*)$/
1042 /^(!!?)?(\$\w+\$.*|[\w\+\/]{13}|_[\w\+\/]{19}|\*)$/
1045 $recref->{_password} = $1.$2;
1048 return 'Illegal (crypt-encoded) password';
1051 } elsif ( $recref->{_password_encoding} eq 'plain' ) {
1053 #generate a password if it is blank
1054 $recref->{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) )
1055 unless length( $recref->{_password} );
1057 if ( $recref->{_password} =~ /^([^\t\n]{$passwordmin,$passwordmax})$/ ) {
1058 $recref->{_password} = $1;
1060 return gettext('illegal_password'). " $passwordmin-$passwordmax ".
1061 FS::Msgcat::_gettext('illegal_password_characters').
1062 ": ". $recref->{_password};
1065 if ( $password_noampersand ) {
1066 $recref->{_password} =~ /\&/ and return gettext('illegal_password');
1068 if ( $password_noexclamation ) {
1069 $recref->{_password} =~ /\!/ and return gettext('illegal_password');
1074 #carp "warning: _password_encoding unspecified\n";
1076 #generate a password if it is blank
1077 unless ( length( $recref->{_password} ) ) {
1079 $recref->{_password} =
1080 join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) );
1081 $recref->{_password_encoding} = 'plain';
1085 #if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,16})$/ ) {
1086 if ( $recref->{_password} =~ /^((\*SUSPENDED\* |!!?)?)([^\t\n]{$passwordmin,$passwordmax})$/ ) {
1087 $recref->{_password} = $1.$3;
1088 $recref->{_password_encoding} = 'plain';
1089 } elsif ( $recref->{_password} =~
1090 /^((\*SUSPENDED\* |!!?)?)([\w\.\/\$\;\+]{13,64})$/
1092 $recref->{_password} = $1.$3;
1093 $recref->{_password_encoding} = 'crypt';
1094 } elsif ( $recref->{_password} eq '*' ) {
1095 $recref->{_password} = '*';
1096 $recref->{_password_encoding} = 'crypt';
1097 } elsif ( $recref->{_password} eq '!' ) {
1098 $recref->{_password_encoding} = 'crypt';
1099 $recref->{_password} = '!';
1100 } elsif ( $recref->{_password} eq '!!' ) {
1101 $recref->{_password} = '!!';
1102 $recref->{_password_encoding} = 'crypt';
1104 #return "Illegal password";
1105 return gettext('illegal_password'). " $passwordmin-$passwordmax ".
1106 FS::Msgcat::_gettext('illegal_password_characters').
1107 ": ". $recref->{_password};
1114 $self->SUPER::check;
1120 Internal function to check the username against the list of system usernames
1121 from the I<system_usernames> configuration value. Returns true if the username
1122 is listed on the system username list.
1128 scalar( grep { $self->username eq $_ || $self->email eq $_ }
1129 $conf->config('system_usernames')
1133 =item _check_duplicate
1135 Internal function to check for duplicates usernames, username@domain pairs and
1138 If the I<global_unique-username> configuration value is set to B<username> or
1139 B<username@domain>, enforces global username or username@domain uniqueness.
1141 In all cases, check for duplicate uids and usernames or username@domain pairs
1142 per export and with identical I<svcpart> values.
1146 sub _check_duplicate {
1149 my $global_unique = $conf->config('global_unique-username') || 'none';
1150 return '' if $global_unique eq 'disabled';
1152 #this is Pg-specific. what to do for mysql etc?
1153 # ( mysql LOCK TABLES certainly isn't equivalent or useful here :/ )
1154 warn "$me locking svc_acct table for duplicate search" if $DEBUG;
1155 dbh->do("LOCK TABLE svc_acct IN SHARE ROW EXCLUSIVE MODE")
1157 warn "$me acquired svc_acct table lock for duplicate search" if $DEBUG;
1159 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
1160 unless ( $part_svc ) {
1161 return 'unknown svcpart '. $self->svcpart;
1164 my @dup_user = grep { !$self->svcnum || $_->svcnum != $self->svcnum }
1165 qsearch( 'svc_acct', { 'username' => $self->username } );
1166 return gettext('username_in_use')
1167 if $global_unique eq 'username' && @dup_user;
1169 my @dup_userdomain = grep { !$self->svcnum || $_->svcnum != $self->svcnum }
1170 qsearch( 'svc_acct', { 'username' => $self->username,
1171 'domsvc' => $self->domsvc } );
1172 return gettext('username_in_use')
1173 if $global_unique eq 'username@domain' && @dup_userdomain;
1176 if ( $part_svc->part_svc_column('uid')->columnflag ne 'F'
1177 && $self->username !~ /^(toor|(hyla)?fax)$/ ) {
1178 @dup_uid = grep { !$self->svcnum || $_->svcnum != $self->svcnum }
1179 qsearch( 'svc_acct', { 'uid' => $self->uid } );
1184 if ( @dup_user || @dup_userdomain || @dup_uid ) {
1185 my $exports = FS::part_export::export_info('svc_acct');
1186 my %conflict_user_svcpart;
1187 my %conflict_userdomain_svcpart = ( $self->svcpart => 'SELF', );
1189 foreach my $part_export ( $part_svc->part_export ) {
1191 #this will catch to the same exact export
1192 my @svcparts = map { $_->svcpart } $part_export->export_svc;
1194 #this will catch to exports w/same exporthost+type ???
1195 #my @other_part_export = qsearch('part_export', {
1196 # 'machine' => $part_export->machine,
1197 # 'exporttype' => $part_export->exporttype,
1199 #foreach my $other_part_export ( @other_part_export ) {
1200 # push @svcparts, map { $_->svcpart }
1201 # qsearch('export_svc', { 'exportnum' => $part_export->exportnum });
1204 #my $nodomain = $exports->{$part_export->exporttype}{'nodomain'};
1205 #silly kludge to avoid uninitialized value errors
1206 my $nodomain = exists( $exports->{$part_export->exporttype}{'nodomain'} )
1207 ? $exports->{$part_export->exporttype}{'nodomain'}
1209 if ( $nodomain =~ /^Y/i ) {
1210 $conflict_user_svcpart{$_} = $part_export->exportnum
1213 $conflict_userdomain_svcpart{$_} = $part_export->exportnum
1218 foreach my $dup_user ( @dup_user ) {
1219 my $dup_svcpart = $dup_user->cust_svc->svcpart;
1220 if ( exists($conflict_user_svcpart{$dup_svcpart}) ) {
1221 return "duplicate username: conflicts with svcnum ". $dup_user->svcnum.
1222 " via exportnum ". $conflict_user_svcpart{$dup_svcpart};
1226 foreach my $dup_userdomain ( @dup_userdomain ) {
1227 my $dup_svcpart = $dup_userdomain->cust_svc->svcpart;
1228 if ( exists($conflict_userdomain_svcpart{$dup_svcpart}) ) {
1229 return "duplicate username\@domain: conflicts with svcnum ".
1230 $dup_userdomain->svcnum. " via exportnum ".
1231 $conflict_userdomain_svcpart{$dup_svcpart};
1235 foreach my $dup_uid ( @dup_uid ) {
1236 my $dup_svcpart = $dup_uid->cust_svc->svcpart;
1237 if ( exists($conflict_user_svcpart{$dup_svcpart})
1238 || exists($conflict_userdomain_svcpart{$dup_svcpart}) ) {
1239 return "duplicate uid: conflicts with svcnum ". $dup_uid->svcnum.
1240 " via exportnum ". $conflict_user_svcpart{$dup_svcpart}
1241 || $conflict_userdomain_svcpart{$dup_svcpart};
1253 Depriciated, use radius_reply instead.
1258 carp "FS::svc_acct::radius depriciated, use radius_reply";
1259 $_[0]->radius_reply;
1264 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1265 reply attributes of this record.
1267 Note that this is now the preferred method for reading RADIUS attributes -
1268 accessing the columns directly is discouraged, as the column names are
1269 expected to change in the future.
1276 return %{ $self->{'radius_reply'} }
1277 if exists $self->{'radius_reply'};
1282 my($column, $attrib) = ($1, $2);
1283 #$attrib =~ s/_/\-/g;
1284 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1285 } grep { /^radius_/ && $self->getfield($_) } fields( $self->table );
1287 if ( $self->slipip && $self->slipip ne '0e0' ) {
1288 $reply{$radius_ip} = $self->slipip;
1291 if ( $self->seconds !~ /^$/ ) {
1292 $reply{'Session-Timeout'} = $self->seconds;
1300 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1301 check attributes of this record.
1303 Note that this is now the preferred method for reading RADIUS attributes -
1304 accessing the columns directly is discouraged, as the column names are
1305 expected to change in the future.
1312 return %{ $self->{'radius_check'} }
1313 if exists $self->{'radius_check'};
1318 my($column, $attrib) = ($1, $2);
1319 #$attrib =~ s/_/\-/g;
1320 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1321 } grep { /^rc_/ && $self->getfield($_) } fields( $self->table );
1323 my $password = $self->_password;
1324 my $pw_attrib = length($password) <= 12 ? $radius_password : 'Crypt-Password'; $check{$pw_attrib} = $password;
1326 my $cust_svc = $self->cust_svc;
1327 die "FATAL: no cust_svc record for svc_acct.svcnum ". $self->svcnum. "\n"
1329 my $cust_pkg = $cust_svc->cust_pkg;
1330 if ( $cust_pkg && $cust_pkg->part_pkg->is_prepaid && $cust_pkg->bill ) {
1331 $check{'Expiration'} = time2str('%B %e %Y %T', $cust_pkg->bill ); #http://lists.cistron.nl/pipermail/freeradius-users/2005-January/040184.html
1340 This method instructs the object to "snapshot" or freeze RADIUS check and
1341 reply attributes to the current values.
1345 #bah, my english is too broken this morning
1346 #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
1347 #the FS::cust_pkg's replace method to trigger the correct export updates when
1348 #package dates change)
1353 $self->{$_} = { $self->$_() }
1354 foreach qw( radius_reply radius_check );
1358 =item forget_snapshot
1360 This methos instructs the object to forget any previously snapshotted
1361 RADIUS check and reply attributes.
1365 sub forget_snapshot {
1369 foreach qw( radius_reply radius_check );
1373 =item domain [ END_TIMESTAMP [ START_TIMESTAMP ] ]
1375 Returns the domain associated with this account.
1377 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
1384 die "svc_acct.domsvc is null for svcnum ". $self->svcnum unless $self->domsvc;
1385 my $svc_domain = $self->svc_domain(@_)
1386 or die "no svc_domain.svcnum for svc_acct.domsvc ". $self->domsvc;
1387 $svc_domain->domain;
1392 Returns the FS::svc_domain record for this account's domain (see
1397 # FS::h_svc_acct has a history-aware svc_domain override
1402 ? $self->{'_domsvc'}
1403 : qsearchs( 'svc_domain', { 'svcnum' => $self->domsvc } );
1408 Returns the FS::cust_svc record for this account (see L<FS::cust_svc>).
1412 #inherited from svc_Common
1414 =item email [ END_TIMESTAMP [ START_TIMESTAMP ] ]
1416 Returns an email address associated with the account.
1418 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
1425 $self->username. '@'. $self->domain(@_);
1430 Returns an array of FS::acct_snarf records associated with the account.
1431 If the acct_snarf table does not exist or there are no associated records,
1432 an empty list is returned
1438 return () unless dbdef->table('acct_snarf');
1439 eval "use FS::acct_snarf;";
1441 qsearch('acct_snarf', { 'svcnum' => $self->svcnum } );
1444 =item decrement_upbytes OCTETS
1446 Decrements the I<upbytes> field of this record by the given amount. If there
1447 is an error, returns the error, otherwise returns false.
1451 sub decrement_upbytes {
1452 shift->_op_usage('-', 'upbytes', @_);
1455 =item increment_upbytes OCTETS
1457 Increments the I<upbytes> field of this record by the given amount. If there
1458 is an error, returns the error, otherwise returns false.
1462 sub increment_upbytes {
1463 shift->_op_usage('+', 'upbytes', @_);
1466 =item decrement_downbytes OCTETS
1468 Decrements the I<downbytes> field of this record by the given amount. If there
1469 is an error, returns the error, otherwise returns false.
1473 sub decrement_downbytes {
1474 shift->_op_usage('-', 'downbytes', @_);
1477 =item increment_downbytes OCTETS
1479 Increments the I<downbytes> field of this record by the given amount. If there
1480 is an error, returns the error, otherwise returns false.
1484 sub increment_downbytes {
1485 shift->_op_usage('+', 'downbytes', @_);
1488 =item decrement_totalbytes OCTETS
1490 Decrements the I<totalbytes> field of this record by the given amount. If there
1491 is an error, returns the error, otherwise returns false.
1495 sub decrement_totalbytes {
1496 shift->_op_usage('-', 'totalbytes', @_);
1499 =item increment_totalbytes OCTETS
1501 Increments the I<totalbytes> field of this record by the given amount. If there
1502 is an error, returns the error, otherwise returns false.
1506 sub increment_totalbytes {
1507 shift->_op_usage('+', 'totalbytes', @_);
1510 =item decrement_seconds SECONDS
1512 Decrements the I<seconds> field of this record by the given amount. If there
1513 is an error, returns the error, otherwise returns false.
1517 sub decrement_seconds {
1518 shift->_op_usage('-', 'seconds', @_);
1521 =item increment_seconds SECONDS
1523 Increments the I<seconds> field of this record by the given amount. If there
1524 is an error, returns the error, otherwise returns false.
1528 sub increment_seconds {
1529 shift->_op_usage('+', 'seconds', @_);
1537 my %op2condition = (
1538 '-' => sub { my($self, $column, $amount) = @_;
1539 $self->$column - $amount <= 0;
1541 '+' => sub { my($self, $column, $amount) = @_;
1542 $self->$column + $amount > 0;
1545 my %op2warncondition = (
1546 '-' => sub { my($self, $column, $amount) = @_;
1547 my $threshold = $column . '_threshold';
1548 $self->$column - $amount <= $self->$threshold + 0;
1550 '+' => sub { my($self, $column, $amount) = @_;
1551 $self->$column + $amount > 0;
1556 my( $self, $op, $column, $amount ) = @_;
1558 warn "$me _op_usage called for $column on svcnum ". $self->svcnum.
1559 ' ('. $self->email. "): $op $amount\n"
1562 return '' unless $amount;
1564 local $SIG{HUP} = 'IGNORE';
1565 local $SIG{INT} = 'IGNORE';
1566 local $SIG{QUIT} = 'IGNORE';
1567 local $SIG{TERM} = 'IGNORE';
1568 local $SIG{TSTP} = 'IGNORE';
1569 local $SIG{PIPE} = 'IGNORE';
1571 my $oldAutoCommit = $FS::UID::AutoCommit;
1572 local $FS::UID::AutoCommit = 0;
1575 my $sql = "UPDATE svc_acct SET $column = ".
1576 " CASE WHEN $column IS NULL THEN 0 ELSE $column END ". #$column||0
1577 " $op ? WHERE svcnum = ?";
1581 my $sth = $dbh->prepare( $sql )
1582 or die "Error preparing $sql: ". $dbh->errstr;
1583 my $rv = $sth->execute($amount, $self->svcnum);
1584 die "Error executing $sql: ". $sth->errstr
1585 unless defined($rv);
1586 die "Can't update $column for svcnum". $self->svcnum
1589 my $action = $op2action{$op};
1591 if ( &{$op2condition{$op}}($self, $column, $amount) ) {
1592 foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
1593 if ($part_export->option('overlimit_groups')) {
1595 my $other = new FS::svc_acct $self->hashref;
1596 my $groups = &{ $self->_fieldhandlers->{'usergroup'} }
1597 ($self, $part_export->option('overlimit_groups'));
1598 $other->usergroup( $groups );
1599 if ($action eq 'suspend'){
1600 $new = $other; $old = $self;
1602 $new = $self; $old = $other;
1604 my $error = $part_export->export_replace($new, $old);
1606 $dbh->rollback if $oldAutoCommit;
1607 return "Error replacing radius groups in export, ${op}: $error";
1613 if ( $conf->exists("svc_acct-usage_$action")
1614 && &{$op2condition{$op}}($self, $column, $amount) ) {
1615 #my $error = $self->$action();
1616 my $error = $self->cust_svc->cust_pkg->$action();
1618 $dbh->rollback if $oldAutoCommit;
1619 return "Error ${action}ing: $error";
1623 if ($warning_template && &{$op2warncondition{$op}}($self, $column, $amount)) {
1624 my $wqueue = new FS::queue {
1625 'svcnum' => $self->svcnum,
1626 'job' => 'FS::svc_acct::reached_threshold',
1631 $to = $warning_cc if &{$op2condition{$op}}($self, $column, $amount);
1635 my $error = $wqueue->insert(
1636 'svcnum' => $self->svcnum,
1638 'column' => $column,
1642 $dbh->rollback if $oldAutoCommit;
1643 return "Error queuing threshold activity: $error";
1647 warn "$me update successful; committing\n"
1649 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1655 my( $self, $valueref ) = @_;
1657 warn "$me set_usage called for svcnum ". $self->svcnum.
1658 ' ('. $self->email. "): ".
1659 join(', ', map { "$_ => " . $valueref->{$_}} keys %$valueref) . "\n"
1662 local $SIG{HUP} = 'IGNORE';
1663 local $SIG{INT} = 'IGNORE';
1664 local $SIG{QUIT} = 'IGNORE';
1665 local $SIG{TERM} = 'IGNORE';
1666 local $SIG{TSTP} = 'IGNORE';
1667 local $SIG{PIPE} = 'IGNORE';
1669 local $FS::svc_Common::noexport_hack = 1;
1670 my $oldAutoCommit = $FS::UID::AutoCommit;
1671 local $FS::UID::AutoCommit = 0;
1675 foreach my $field (keys %$valueref){
1676 $reset = 1 if $valueref->{$field};
1677 $self->setfield($field, $valueref->{$field});
1678 $self->setfield( $field.'_threshold',
1679 int($self->getfield($field)
1680 * ( $conf->exists('svc_acct-usage_threshold')
1681 ? 1 - $conf->config('svc_acct-usage_threshold')/100
1687 my $error = $self->replace;
1688 die $error if $error;
1690 if ( $conf->exists("svc_acct-usage_unsuspend") && $reset ) {
1691 my $error = $self->cust_svc->cust_pkg->unsuspend;
1693 $dbh->rollback if $oldAutoCommit;
1694 return "Error unsuspending: $error";
1698 warn "$me update successful; committing\n"
1700 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1706 =item recharge HASHREF
1708 Increments usage columns by the amount specified in HASHREF as
1709 column=>amount pairs.
1714 my ($self, $vhash) = @_;
1717 warn "[$me] recharge called on $self: ". Dumper($self).
1718 "\nwith vhash: ". Dumper($vhash);
1721 my $oldAutoCommit = $FS::UID::AutoCommit;
1722 local $FS::UID::AutoCommit = 0;
1726 foreach my $column (keys %$vhash){
1727 $error ||= $self->_op_usage('+', $column, $vhash->{$column});
1731 $dbh->rollback if $oldAutoCommit;
1733 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1738 =item is_rechargeable
1740 Returns true if this svc_account can be "recharged" and false otherwise.
1744 sub is_rechargable {
1746 $self->seconds ne ''
1747 || $self->upbytes ne ''
1748 || $self->downbytes ne ''
1749 || $self->totalbytes ne '';
1752 =item seconds_since TIMESTAMP
1754 Returns the number of seconds this account has been online since TIMESTAMP,
1755 according to the session monitor (see L<FS::Session>).
1757 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
1758 L<Time::Local> and L<Date::Parse> for conversion functions.
1762 #note: POD here, implementation in FS::cust_svc
1765 $self->cust_svc->seconds_since(@_);
1768 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
1770 Returns the numbers of seconds this account has been online between
1771 TIMESTAMP_START (inclusive) and TIMESTAMP_END (exclusive), according to an
1772 external SQL radacct table, specified via sqlradius export. Sessions which
1773 started in the specified range but are still open are counted from session
1774 start to the end of the range (unless they are over 1 day old, in which case
1775 they are presumed missing their stop record and not counted). Also, sessions
1776 which end in the range but started earlier are counted from the start of the
1777 range to session end. Finally, sessions which start before the range but end
1778 after are counted for the entire range.
1780 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
1781 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
1786 #note: POD here, implementation in FS::cust_svc
1787 sub seconds_since_sqlradacct {
1789 $self->cust_svc->seconds_since_sqlradacct(@_);
1792 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
1794 Returns the sum of the given attribute for all accounts (see L<FS::svc_acct>)
1795 in this package for sessions ending between TIMESTAMP_START (inclusive) and
1796 TIMESTAMP_END (exclusive).
1798 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
1799 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
1804 #note: POD here, implementation in FS::cust_svc
1805 sub attribute_since_sqlradacct {
1807 $self->cust_svc->attribute_since_sqlradacct(@_);
1810 =item get_session_history TIMESTAMP_START TIMESTAMP_END
1812 Returns an array of hash references of this customers login history for the
1813 given time range. (document this better)
1817 sub get_session_history {
1819 $self->cust_svc->get_session_history(@_);
1822 =item get_cdrs TIMESTAMP_START TIMESTAMP_END [ 'OPTION' => 'VALUE ... ]
1827 my($self, $start, $end, %opt ) = @_;
1829 my $did = $self->username; #yup
1831 my $prefix = $opt{'default_prefix'}; #convergent.au '+61'
1833 my $for_update = $opt{'for_update'} ? 'FOR UPDATE' : '';
1835 #SELECT $for_update * FROM cdr
1836 # WHERE calldate >= $start #need a conversion
1837 # AND calldate < $end #ditto
1838 # AND ( charged_party = "$did"
1839 # OR charged_party = "$prefix$did" #if length($prefix);
1840 # OR ( ( charged_party IS NULL OR charged_party = '' )
1842 # ( src = "$did" OR src = "$prefix$did" ) # if length($prefix)
1845 # AND ( freesidestatus IS NULL OR freesidestatus = '' )
1848 if ( length($prefix) ) {
1850 " AND ( charged_party = '$did'
1851 OR charged_party = '$prefix$did'
1852 OR ( ( charged_party IS NULL OR charged_party = '' )
1854 ( src = '$did' OR src = '$prefix$did' )
1860 " AND ( charged_party = '$did'
1861 OR ( ( charged_party IS NULL OR charged_party = '' )
1871 'select' => "$for_update *",
1874 #( freesidestatus IS NULL OR freesidestatus = '' )
1875 'freesidestatus' => '',
1877 'extra_sql' => $charged_or_src,
1885 Returns all RADIUS groups for this account (see L<FS::radius_usergroup>).
1891 if ( $self->usergroup ) {
1892 confess "explicitly specified usergroup not an arrayref: ". $self->usergroup
1893 unless ref($self->usergroup) eq 'ARRAY';
1894 #when provisioning records, export callback runs in svc_Common.pm before
1895 #radius_usergroup records can be inserted...
1896 @{$self->usergroup};
1898 map { $_->groupname }
1899 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } );
1903 =item clone_suspended
1905 Constructor used by FS::part_export::_export_suspend fallback. Document
1910 sub clone_suspended {
1912 my %hash = $self->hash;
1913 $hash{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) );
1914 new FS::svc_acct \%hash;
1917 =item clone_kludge_unsuspend
1919 Constructor used by FS::part_export::_export_unsuspend fallback. Document
1924 sub clone_kludge_unsuspend {
1926 my %hash = $self->hash;
1927 $hash{_password} = '';
1928 new FS::svc_acct \%hash;
1931 =item check_password
1933 Checks the supplied password against the (possibly encrypted) password in the
1934 database. Returns true for a successful authentication, false for no match.
1936 Currently supported encryptions are: classic DES crypt() and MD5
1940 sub check_password {
1941 my($self, $check_password) = @_;
1943 #remove old-style SUSPENDED kludge, they should be allowed to login to
1944 #self-service and pay up
1945 ( my $password = $self->_password ) =~ s/^\*SUSPENDED\* //;
1947 if ( $self->_password_encoding eq 'ldap' ) {
1949 my $auth = from_rfc2307 Authen::Passphrase $self->_password;
1950 return $auth->match($check_password);
1952 } elsif ( $self->_password_encoding eq 'crypt' ) {
1954 my $auth = from_crypt Authen::Passphrase $self->_password;
1955 return $auth->match($check_password);
1957 } elsif ( $self->_password_encoding eq 'plain' ) {
1959 return $check_password eq $password;
1963 #XXX this could be replaced with Authen::Passphrase stuff
1965 if ( $password =~ /^(\*|!!?)$/ ) { #no self-service login
1967 } elsif ( length($password) < 13 ) { #plaintext
1968 $check_password eq $password;
1969 } elsif ( length($password) == 13 ) { #traditional DES crypt
1970 crypt($check_password, $password) eq $password;
1971 } elsif ( $password =~ /^\$1\$/ ) { #MD5 crypt
1972 unix_md5_crypt($check_password, $password) eq $password;
1973 } elsif ( $password =~ /^\$2a?\$/ ) { #Blowfish
1974 warn "Can't check password: Blowfish encryption not yet supported, ".
1975 "svcnum ". $self->svcnum. "\n";
1978 warn "Can't check password: Unrecognized encryption for svcnum ".
1979 $self->svcnum. "\n";
1987 =item crypt_password [ DEFAULT_ENCRYPTION_TYPE ]
1989 Returns an encrypted password, either by passing through an encrypted password
1990 in the database or by encrypting a plaintext password from the database.
1992 The optional DEFAULT_ENCRYPTION_TYPE parameter can be set to I<crypt> (classic
1993 UNIX DES crypt), I<md5> (md5 crypt supported by most modern Linux and BSD
1994 distrubtions), or (eventually) I<blowfish> (blowfish hashing supported by
1995 OpenBSD, SuSE, other Linux distibutions with pam_unix2, etc.). The default
1996 encryption type is only used if the password is not already encrypted in the
2001 sub crypt_password {
2004 if ( $self->_password_encoding eq 'ldap' ) {
2006 if ( $self->_password =~ /^\{(PLAIN|CLEARTEXT)\}(.+)$/ ) {
2009 #XXX this could be replaced with Authen::Passphrase stuff
2011 my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2012 if ( $encryption eq 'crypt' ) {
2015 $saltset[int(rand(64))].$saltset[int(rand(64))]
2017 } elsif ( $encryption eq 'md5' ) {
2018 unix_md5_crypt( $self->_password );
2019 } elsif ( $encryption eq 'blowfish' ) {
2020 croak "unknown encryption method $encryption";
2022 croak "unknown encryption method $encryption";
2025 } elsif ( $self->_password =~ /^\{CRYPT\}(.+)$/ ) {
2029 } elsif ( $self->_password_encoding eq 'crypt' ) {
2031 return $self->_password;
2033 } elsif ( $self->_password_encoding eq 'plain' ) {
2035 #XXX this could be replaced with Authen::Passphrase stuff
2037 my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2038 if ( $encryption eq 'crypt' ) {
2041 $saltset[int(rand(64))].$saltset[int(rand(64))]
2043 } elsif ( $encryption eq 'md5' ) {
2044 unix_md5_crypt( $self->_password );
2045 } elsif ( $encryption eq 'blowfish' ) {
2046 croak "unknown encryption method $encryption";
2048 croak "unknown encryption method $encryption";
2053 if ( length($self->_password) == 13
2054 || $self->_password =~ /^\$(1|2a?)\$/
2055 || $self->_password =~ /^(\*|NP|\*LK\*|!!?)$/
2061 #XXX this could be replaced with Authen::Passphrase stuff
2063 my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2064 if ( $encryption eq 'crypt' ) {
2067 $saltset[int(rand(64))].$saltset[int(rand(64))]
2069 } elsif ( $encryption eq 'md5' ) {
2070 unix_md5_crypt( $self->_password );
2071 } elsif ( $encryption eq 'blowfish' ) {
2072 croak "unknown encryption method $encryption";
2074 croak "unknown encryption method $encryption";
2083 =item ldap_password [ DEFAULT_ENCRYPTION_TYPE ]
2085 Returns an encrypted password in "LDAP" format, with a curly-bracked prefix
2086 describing the format, for example, "{PLAIN}himom", "{CRYPT}94pAVyK/4oIBk" or
2087 "{MD5}5426824942db4253f87a1009fd5d2d4".
2089 The optional DEFAULT_ENCRYPTION_TYPE is not yet used, but the idea is for it
2090 to work the same as the B</crypt_password> method.
2096 #eventually should check a "password-encoding" field
2098 if ( $self->_password_encoding eq 'ldap' ) {
2100 return $self->_password;
2102 } elsif ( $self->_password_encoding eq 'crypt' ) {
2104 if ( length($self->_password) == 13 ) { #crypt
2105 return '{CRYPT}'. $self->_password;
2106 } elsif ( $self->_password =~ /^\$1\$(.*)$/ && length($1) == 31 ) { #passwdMD5
2108 #} elsif ( $self->_password =~ /^\$2a?\$(.*)$/ ) { #Blowfish
2109 # die "Blowfish encryption not supported in this context, svcnum ".
2110 # $self->svcnum. "\n";
2112 warn "encryption method not (yet?) supported in LDAP context";
2113 return '{CRYPT}*'; #unsupported, should not auth
2116 } elsif ( $self->_password_encoding eq 'plain' ) {
2118 return '{PLAIN}'. $self->_password;
2120 #return '{CLEARTEXT}'. $self->_password; #?
2124 if ( length($self->_password) == 13 ) { #crypt
2125 return '{CRYPT}'. $self->_password;
2126 } elsif ( $self->_password =~ /^\$1\$(.*)$/ && length($1) == 31 ) { #passwdMD5
2128 } elsif ( $self->_password =~ /^\$2a?\$(.*)$/ ) { #Blowfish
2129 warn "Blowfish encryption not supported in this context, svcnum ".
2130 $self->svcnum. "\n";
2133 #are these two necessary anymore?
2134 } elsif ( $self->_password =~ /^(\w{48})$/ ) { #LDAP SSHA
2135 return '{SSHA}'. $1;
2136 } elsif ( $self->_password =~ /^(\w{64})$/ ) { #LDAP NS-MTA-MD5
2137 return '{NS-MTA-MD5}'. $1;
2140 return '{PLAIN}'. $self->_password;
2142 #return '{CLEARTEXT}'. $self->_password; #?
2144 #XXX this could be replaced with Authen::Passphrase stuff if it gets used
2145 #my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2146 #if ( $encryption eq 'crypt' ) {
2147 # return '{CRYPT}'. crypt(
2149 # $saltset[int(rand(64))].$saltset[int(rand(64))]
2151 #} elsif ( $encryption eq 'md5' ) {
2152 # unix_md5_crypt( $self->_password );
2153 #} elsif ( $encryption eq 'blowfish' ) {
2154 # croak "unknown encryption method $encryption";
2156 # croak "unknown encryption method $encryption";
2164 =item domain_slash_username
2166 Returns $domain/$username/
2170 sub domain_slash_username {
2172 $self->domain. '/'. $self->username. '/';
2175 =item virtual_maildir
2177 Returns $domain/maildirs/$username/
2181 sub virtual_maildir {
2183 $self->domain. '/maildirs/'. $self->username. '/';
2194 This is the FS::svc_acct job-queue-able version. It still uses
2195 FS::Misc::send_email under-the-hood.
2202 eval "use FS::Misc qw(send_email)";
2205 $opt{mimetype} ||= 'text/plain';
2206 $opt{mimetype} .= '; charset="iso-8859-1"' unless $opt{mimetype} =~ /charset/;
2208 my $error = send_email(
2209 'from' => $opt{from},
2211 'subject' => $opt{subject},
2212 'content-type' => $opt{mimetype},
2213 'body' => [ map "$_\n", split("\n", $opt{body}) ],
2215 die $error if $error;
2218 =item check_and_rebuild_fuzzyfiles
2222 sub check_and_rebuild_fuzzyfiles {
2223 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
2224 -e "$dir/svc_acct.username"
2225 or &rebuild_fuzzyfiles;
2228 =item rebuild_fuzzyfiles
2232 sub rebuild_fuzzyfiles {
2234 use Fcntl qw(:flock);
2236 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
2240 open(USERNAMELOCK,">>$dir/svc_acct.username")
2241 or die "can't open $dir/svc_acct.username: $!";
2242 flock(USERNAMELOCK,LOCK_EX)
2243 or die "can't lock $dir/svc_acct.username: $!";
2245 my @all_username = map $_->getfield('username'), qsearch('svc_acct', {});
2247 open (USERNAMECACHE,">$dir/svc_acct.username.tmp")
2248 or die "can't open $dir/svc_acct.username.tmp: $!";
2249 print USERNAMECACHE join("\n", @all_username), "\n";
2250 close USERNAMECACHE or die "can't close $dir/svc_acct.username.tmp: $!";
2252 rename "$dir/svc_acct.username.tmp", "$dir/svc_acct.username";
2262 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
2263 open(USERNAMECACHE,"<$dir/svc_acct.username")
2264 or die "can't open $dir/svc_acct.username: $!";
2265 my @array = map { chomp; $_; } <USERNAMECACHE>;
2266 close USERNAMECACHE;
2270 =item append_fuzzyfiles USERNAME
2274 sub append_fuzzyfiles {
2275 my $username = shift;
2277 &check_and_rebuild_fuzzyfiles;
2279 use Fcntl qw(:flock);
2281 my $dir = $FS::UID::conf_dir. "cache.". $FS::UID::datasrc;
2283 open(USERNAME,">>$dir/svc_acct.username")
2284 or die "can't open $dir/svc_acct.username: $!";
2285 flock(USERNAME,LOCK_EX)
2286 or die "can't lock $dir/svc_acct.username: $!";
2288 print USERNAME "$username\n";
2290 flock(USERNAME,LOCK_UN)
2291 or die "can't unlock $dir/svc_acct.username: $!";
2299 =item radius_usergroup_selector GROUPS_ARRAYREF [ SELECTNAME ]
2303 sub radius_usergroup_selector {
2304 my $sel_groups = shift;
2305 my %sel_groups = map { $_=>1 } @$sel_groups;
2307 my $selectname = shift || 'radius_usergroup';
2310 my $sth = $dbh->prepare(
2311 'SELECT DISTINCT(groupname) FROM radius_usergroup ORDER BY groupname'
2312 ) or die $dbh->errstr;
2313 $sth->execute() or die $sth->errstr;
2314 my @all_groups = map { $_->[0] } @{$sth->fetchall_arrayref};
2318 function ${selectname}_doadd(object) {
2319 var myvalue = object.${selectname}_add.value;
2320 var optionName = new Option(myvalue,myvalue,false,true);
2321 var length = object.$selectname.length;
2322 object.$selectname.options[length] = optionName;
2323 object.${selectname}_add.value = "";
2326 <SELECT MULTIPLE NAME="$selectname">
2329 foreach my $group ( @all_groups ) {
2330 $html .= qq(<OPTION VALUE="$group");
2331 if ( $sel_groups{$group} ) {
2332 $html .= ' SELECTED';
2333 $sel_groups{$group} = 0;
2335 $html .= ">$group</OPTION>\n";
2337 foreach my $group ( grep { $sel_groups{$_} } keys %sel_groups ) {
2338 $html .= qq(<OPTION VALUE="$group" SELECTED>$group</OPTION>\n);
2340 $html .= '</SELECT>';
2342 $html .= qq!<BR><INPUT TYPE="text" NAME="${selectname}_add">!.
2343 qq!<INPUT TYPE="button" VALUE="Add new group" onClick="${selectname}_doadd(this.form)">!;
2348 =item reached_threshold
2350 Performs some activities when svc_acct thresholds (such as number of seconds
2351 remaining) are reached.
2355 sub reached_threshold {
2358 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $opt{'svcnum'} } );
2359 die "Cannot find svc_acct with svcnum " . $opt{'svcnum'} unless $svc_acct;
2361 if ( $opt{'op'} eq '+' ){
2362 $svc_acct->setfield( $opt{'column'}.'_threshold',
2363 int($svc_acct->getfield($opt{'column'})
2364 * ( $conf->exists('svc_acct-usage_threshold')
2365 ? $conf->config('svc_acct-usage_threshold')/100
2370 my $error = $svc_acct->replace;
2371 die $error if $error;
2372 }elsif ( $opt{'op'} eq '-' ){
2374 my $threshold = $svc_acct->getfield( $opt{'column'}.'_threshold' );
2375 return '' if ($threshold eq '' );
2377 $svc_acct->setfield( $opt{'column'}.'_threshold', 0 );
2378 my $error = $svc_acct->replace;
2379 die $error if $error; # email next time, i guess
2381 if ( $warning_template ) {
2382 eval "use FS::Misc qw(send_email)";
2385 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
2386 my $cust_main = $cust_pkg->cust_main;
2388 my $to = join(', ', grep { $_ !~ /^(POST|FAX)$/ }
2389 $cust_main->invoicing_list,
2391 ($opt{'to'} ? $opt{'to'} : ())
2394 my $mimetype = $warning_mimetype;
2395 $mimetype .= '; charset="iso-8859-1"' unless $opt{mimetype} =~ /charset/;
2397 my $body = $warning_template->fill_in( HASH => {
2398 'custnum' => $cust_main->custnum,
2399 'username' => $svc_acct->username,
2400 'password' => $svc_acct->_password,
2401 'first' => $cust_main->first,
2402 'last' => $cust_main->getfield('last'),
2403 'pkg' => $cust_pkg->part_pkg->pkg,
2404 'column' => $opt{'column'},
2405 'amount' => $svc_acct->getfield($opt{'column'}),
2406 'threshold' => $threshold,
2410 my $error = send_email(
2411 'from' => $warning_from,
2413 'subject' => $warning_subject,
2414 'content-type' => $mimetype,
2415 'body' => [ map "$_\n", split("\n", $body) ],
2417 die $error if $error;
2420 die "unknown op: " . $opt{'op'};
2428 The $recref stuff in sub check should be cleaned up.
2430 The suspend, unsuspend and cancel methods update the database, but not the
2431 current object. This is probably a bug as it's unexpected and
2434 radius_usergroup_selector? putting web ui components in here? they should
2435 probably live somewhere else...
2437 insertion of RADIUS group stuff in insert could be done with child_objects now
2438 (would probably clean up export of them too)
2442 L<FS::svc_Common>, edit/part_svc.cgi from an installed web interface,
2443 export.html from the base documentation, L<FS::Record>, L<FS::Conf>,
2444 L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, L<FS::queue>,
2445 L<freeside-queued>), L<FS::svc_acct_pop>,
2446 schema.html from the base documentation.