4 use base qw( FS::svc_Domain_Mixin FS::svc_CGP_Mixin FS::svc_CGPRule_Mixin
6 use vars qw( $DEBUG $me $conf $skip_fuzzyfiles
7 $dir_prefix @shells $usernamemin
8 $usernamemax $passwordmin $passwordmax
9 $username_ampersand $username_letter $username_letterfirst
10 $username_noperiod $username_nounderscore $username_nodash
11 $username_uppercase $username_percent $username_colon
12 $username_slash $username_equals
13 $password_noampersand $password_noexclamation
14 $warning_template $warning_from $warning_subject $warning_mimetype
17 $radius_password $radius_ip
20 use Scalar::Util qw( blessed );
25 use Crypt::PasswdMD5 1.2;
26 use Digest::SHA1 'sha1_base64';
27 use Digest::MD5 'md5_base64';
30 use Authen::Passphrase;
31 use FS::UID qw( datasrc driver_name );
33 use FS::Record qw( qsearch qsearchs fields dbh dbdef );
34 use FS::Msgcat qw(gettext);
35 use FS::UI::bytecount;
40 use FS::cust_main_invoice;
45 use FS::radius_usergroup;
54 $me = '[FS::svc_acct]';
56 #ask FS::UID to run this stuff for us later
57 FS::UID->install_callback( sub {
59 $dir_prefix = $conf->config('home');
60 @shells = $conf->config('shells');
61 $usernamemin = $conf->config('usernamemin') || 2;
62 $usernamemax = $conf->config('usernamemax');
63 $passwordmin = $conf->config('passwordmin'); # || 6;
65 $passwordmin = ( defined($passwordmin) && $passwordmin =~ /\d+/ )
68 $passwordmax = $conf->config('passwordmax') || 8;
69 $username_letter = $conf->exists('username-letter');
70 $username_letterfirst = $conf->exists('username-letterfirst');
71 $username_noperiod = $conf->exists('username-noperiod');
72 $username_nounderscore = $conf->exists('username-nounderscore');
73 $username_nodash = $conf->exists('username-nodash');
74 $username_uppercase = $conf->exists('username-uppercase');
75 $username_ampersand = $conf->exists('username-ampersand');
76 $username_percent = $conf->exists('username-percent');
77 $username_colon = $conf->exists('username-colon');
78 $username_slash = $conf->exists('username-slash');
79 $username_equals = $conf->exists('username-equals');
80 $password_noampersand = $conf->exists('password-noexclamation');
81 $password_noexclamation = $conf->exists('password-noexclamation');
82 $dirhash = $conf->config('dirhash') || 0;
83 if ( $conf->exists('warning_email') ) {
84 $warning_template = new Text::Template (
86 SOURCE => [ map "$_\n", $conf->config('warning_email') ]
87 ) or warn "can't create warning email template: $Text::Template::ERROR";
88 $warning_from = $conf->config('warning_email-from'); # || 'your-isp-is-dum'
89 $warning_subject = $conf->config('warning_email-subject') || 'Warning';
90 $warning_mimetype = $conf->config('warning_email-mimetype') || 'text/plain';
91 $warning_cc = $conf->config('warning_email-cc');
93 $warning_template = '';
95 $warning_subject = '';
96 $warning_mimetype = '';
99 $smtpmachine = $conf->config('smtpmachine');
100 $radius_password = $conf->config('radius-password') || 'Password';
101 $radius_ip = $conf->config('radius-ip') || 'Framed-IP-Address';
102 @pw_set = ( 'A'..'Z' ) if $conf->exists('password-generated-allcaps');
106 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
107 @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '.', ',' );
111 my ( $hashref, $cache ) = @_;
112 if ( $hashref->{'svc_acct_svcnum'} ) {
113 $self->{'_domsvc'} = FS::svc_domain->new( {
114 'svcnum' => $hashref->{'domsvc'},
115 'domain' => $hashref->{'svc_acct_domain'},
116 'catchall' => $hashref->{'svc_acct_catchall'},
123 FS::svc_acct - Object methods for svc_acct records
129 $record = new FS::svc_acct \%hash;
130 $record = new FS::svc_acct { 'column' => 'value' };
132 $error = $record->insert;
134 $error = $new_record->replace($old_record);
136 $error = $record->delete;
138 $error = $record->check;
140 $error = $record->suspend;
142 $error = $record->unsuspend;
144 $error = $record->cancel;
146 %hash = $record->radius;
148 %hash = $record->radius_reply;
150 %hash = $record->radius_check;
152 $domain = $record->domain;
154 $svc_domain = $record->svc_domain;
156 $email = $record->email;
158 $seconds_since = $record->seconds_since($timestamp);
162 An FS::svc_acct object represents an account. FS::svc_acct inherits from
163 FS::svc_Common. The following fields are currently supported:
169 Primary key (assigned automatcially for new accounts)
177 =item _password_encoding
179 plain, crypt, ldap (or empty for autodetection)
187 Point of presence (see L<FS::svc_acct_pop>)
199 set automatically if blank (and uid is not)
219 svcnum from svc_domain
223 Optional svcnum from svc_pbx
225 =item radius_I<Radius_Attribute>
227 I<Radius-Attribute> (reply)
229 =item rc_I<Radius_Attribute>
231 I<Radius-Attribute> (check)
241 Creates a new account. To add the account to the database, see L<"insert">.
248 'longname_plural' => 'Access accounts and mailboxes',
249 'sorts' => [ 'username', 'uid', 'seconds', 'last_login' ],
250 'display_weight' => 10,
251 'cancel_weight' => 50,
253 'dir' => 'Home directory',
256 def_info => 'set to fixed and blank for no UIDs',
259 'slipip' => 'IP address',
260 # 'popnum' => qq!<A HREF="$p/browse/svc_acct_pop.cgi/">POP number</A>!,
262 label => 'Access number',
264 select_table => 'svc_acct_pop',
265 select_key => 'popnum',
266 select_label => 'city',
272 disable_default => 1,
276 'password_selfchange' => { label => 'Password modification',
279 'password_recover' => { label => 'Password recovery',
283 label => 'Quota', #Mail storage limit
285 disable_inventory => 1,
289 label => 'File storage limit',
291 disable_inventory => 1,
295 label => 'Number of files limit',
297 disable_inventory => 1,
301 label => 'File size limit',
303 disable_inventory => 1,
306 '_password' => 'Password',
309 def_info => 'when blank, defaults to UID',
314 def_info => 'set to blank for no shell tracking',
316 #select_list => [ $conf->config('shells') ],
317 select_list => [ $conf ? $conf->config('shells') : () ],
318 disable_inventory => 1,
321 'finger' => 'Real name', # (GECOS)',
325 select_table => 'svc_domain',
326 select_key => 'svcnum',
327 select_label => 'domain',
328 disable_inventory => 1,
330 'pbxsvc' => { label => 'PBX',
331 type => 'select-svc_pbx.html',
332 disable_inventory => 1,
333 disable_select => 1, #UI wonky, pry works otherwise
336 label => 'RADIUS groups',
337 type => 'radius_usergroup_selector',
338 disable_inventory => 1,
341 'seconds' => { label => 'Seconds',
342 label_sort => 'with Time Remaining',
344 disable_inventory => 1,
346 disable_part_svc_column => 1,
348 'upbytes' => { label => 'Upload',
350 disable_inventory => 1,
352 'format' => \&FS::UI::bytecount::display_bytecount,
353 'parse' => \&FS::UI::bytecount::parse_bytecount,
354 disable_part_svc_column => 1,
356 'downbytes' => { label => 'Download',
358 disable_inventory => 1,
360 'format' => \&FS::UI::bytecount::display_bytecount,
361 'parse' => \&FS::UI::bytecount::parse_bytecount,
362 disable_part_svc_column => 1,
364 'totalbytes'=> { label => 'Total up and download',
366 disable_inventory => 1,
368 'format' => \&FS::UI::bytecount::display_bytecount,
369 'parse' => \&FS::UI::bytecount::parse_bytecount,
370 disable_part_svc_column => 1,
372 'seconds_threshold' => { label => 'Seconds threshold',
374 disable_inventory => 1,
376 disable_part_svc_column => 1,
378 'upbytes_threshold' => { label => 'Upload threshold',
380 disable_inventory => 1,
382 'format' => \&FS::UI::bytecount::display_bytecount,
383 'parse' => \&FS::UI::bytecount::parse_bytecount,
384 disable_part_svc_column => 1,
386 'downbytes_threshold' => { label => 'Download threshold',
388 disable_inventory => 1,
390 'format' => \&FS::UI::bytecount::display_bytecount,
391 'parse' => \&FS::UI::bytecount::parse_bytecount,
392 disable_part_svc_column => 1,
394 'totalbytes_threshold'=> { label => 'Total up and download threshold',
396 disable_inventory => 1,
398 'format' => \&FS::UI::bytecount::display_bytecount,
399 'parse' => \&FS::UI::bytecount::parse_bytecount,
400 disable_part_svc_column => 1,
403 label => 'Last login',
407 label => 'Last logout',
412 label => 'Communigate aliases',
414 disable_inventory => 1,
419 label => 'Communigate account type',
421 select_list => [qw( MultiMailbox TextMailbox MailDirMailbox AGrade BGrade CGrade )],
422 disable_inventory => 1,
425 'cgp_accessmodes' => {
426 label => 'Communigate enabled services',
427 type => 'communigate_pro-accessmodes',
428 disable_inventory => 1,
431 'cgp_rulesallowed' => {
432 label => 'Allowed mail rules',
434 select_list => [ '', 'No', 'Filter Only', 'All But Exec', 'Any' ],
435 disable_inventory => 1,
438 'cgp_rpopallowed' => { label => 'RPOP modifications',
441 'cgp_mailtoall' => { label => 'Accepts mail to "all"',
444 'cgp_addmailtrailer' => { label => 'Add trailer to sent mail',
447 'cgp_archiveafter' => {
448 label => 'Archive messages after',
451 -2 => 'default(730 days)',
458 1209600 => '2 weeks',
459 2592000 => '30 days',
460 7776000 => '90 days',
461 15552000 => '180 days',
462 31536000 => '365 days',
463 63072000 => '730 days',
465 disable_inventory => 1,
471 'cgp_deletemode' => {
472 label => 'Communigate message delete method',
474 select_list => [ 'Move To Trash', 'Immediately', 'Mark' ],
475 disable_inventory => 1,
478 'cgp_emptytrash' => {
479 label => 'Communigate on logout remove trash',
481 select_list => __PACKAGE__->cgp_emptytrash_values,
482 disable_inventory => 1,
486 label => 'Communigate language',
488 select_list => [ '', qw( English Arabic Chinese Dutch French German Hebrew Italian Japanese Portuguese Russian Slovak Spanish Thai ) ],
489 disable_inventory => 1,
493 label => 'Communigate time zone',
495 select_list => __PACKAGE__->cgp_timezone_values,
496 disable_inventory => 1,
500 label => 'Communigate layout',
502 select_list => [ '', '***', 'GoldFleece', 'Skin2' ],
503 disable_inventory => 1,
506 'cgp_prontoskinname' => {
507 label => 'Communigate Pronto style',
509 select_list => [ '', 'Pronto', 'Pronto-darkflame', 'Pronto-steel', 'Pronto-twilight', ],
510 disable_inventory => 1,
513 'cgp_sendmdnmode' => {
514 label => 'Communigate send read receipts',
516 select_list => [ '', 'Never', 'Manually', 'Automatically' ],
517 disable_inventory => 1,
528 sub table { 'svc_acct'; }
530 sub table_dupcheck_fields { ( 'username', 'domsvc' ); }
534 #false laziness with edit/svc_acct.cgi
536 my( $self, $groups ) = @_;
537 if ( ref($groups) eq 'ARRAY' ) {
539 } elsif ( length($groups) ) {
540 [ split(/\s*,\s*/, $groups) ];
549 shift->_lastlog('in', @_);
553 shift->_lastlog('out', @_);
557 my( $self, $op, $time ) = @_;
559 if ( defined($time) ) {
560 warn "$me last_log$op called on svcnum ". $self->svcnum.
561 ' ('. $self->email. "): $time\n"
566 my $sql = "UPDATE svc_acct SET last_log$op = ? WHERE svcnum = ?";
570 my $sth = $dbh->prepare( $sql )
571 or die "Error preparing $sql: ". $dbh->errstr;
572 my $rv = $sth->execute($time, $self->svcnum);
573 die "Error executing $sql: ". $sth->errstr
575 die "Can't update last_log$op for svcnum". $self->svcnum
578 $self->{'Hash'}->{"last_log$op"} = $time;
580 $self->getfield("last_log$op");
584 =item search_sql STRING
586 Class method which returns an SQL fragment to search for the given string.
591 my( $class, $string ) = @_;
592 if ( $string =~ /^([^@]+)@([^@]+)$/ ) {
593 my( $username, $domain ) = ( $1, $2 );
594 my $q_username = dbh->quote($username);
595 my @svc_domain = qsearch('svc_domain', { 'domain' => $domain } );
597 "svc_acct.username = $q_username AND ( ".
598 join( ' OR ', map { "svc_acct.domsvc = ". $_->svcnum; } @svc_domain ).
603 } elsif ( $string =~ /^(\d{1,3}\.){3}\d{1,3}$/ ) {
605 $class->search_sql_field('slipip', $string ).
607 $class->search_sql_field('username', $string ).
610 $class->search_sql_field('username', $string);
614 =item label [ END_TIMESTAMP [ START_TIMESTAMP ] ]
616 Returns the "username@domain" string for this account.
618 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
628 =item label_long [ END_TIMESTAMP [ START_TIMESTAMP ] ]
630 Returns a longer string label for this acccount ("Real Name <username@domain>"
631 if available, or "username@domain").
633 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
640 my $label = $self->label(@_);
641 my $finger = $self->finger;
642 return $label unless $finger =~ /\S/;
643 my $maxlen = 40 - length($label) - length($self->cust_svc->part_svc->svc);
644 $finger = substr($finger, 0, $maxlen-3).'...' if length($finger) > $maxlen;
648 =item insert [ , OPTION => VALUE ... ]
650 Adds this account to the database. If there is an error, returns the error,
651 otherwise returns false.
653 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
654 defined. An FS::cust_svc record will be created and inserted.
656 The additional field I<usergroup> can optionally be defined; if so it should
657 contain an arrayref of group names. See L<FS::radius_usergroup>.
659 The additional field I<child_objects> can optionally be defined; if so it
660 should contain an arrayref of FS::tablename objects. They will have their
661 svcnum fields set and will be inserted after this record, but before any
662 exports are run. Each element of the array can also optionally be a
663 two-element array reference containing the child object and the name of an
664 alternate field to be filled in with the newly-inserted svcnum, for example
665 C<[ $svc_forward, 'srcsvc' ]>
667 Currently available options are: I<depend_jobnum>
669 If I<depend_jobnum> is set (to a scalar jobnum or an array reference of
670 jobnums), all provisioning jobs will have a dependancy on the supplied
671 jobnum(s) (they will not run until the specific job(s) complete(s)).
673 (TODOC: L<FS::queue> and L<freeside-queued>)
675 (TODOC: new exports!)
684 warn "[$me] insert called on $self: ". Dumper($self).
685 "\nwith options: ". Dumper(%options);
688 local $SIG{HUP} = 'IGNORE';
689 local $SIG{INT} = 'IGNORE';
690 local $SIG{QUIT} = 'IGNORE';
691 local $SIG{TERM} = 'IGNORE';
692 local $SIG{TSTP} = 'IGNORE';
693 local $SIG{PIPE} = 'IGNORE';
695 my $oldAutoCommit = $FS::UID::AutoCommit;
696 local $FS::UID::AutoCommit = 0;
700 my $error = $self->SUPER::insert(
701 'jobnums' => \@jobnums,
702 'child_objects' => $self->child_objects,
706 $dbh->rollback if $oldAutoCommit;
710 if ( $self->usergroup ) {
711 foreach my $groupname ( @{$self->usergroup} ) {
712 my $radius_usergroup = new FS::radius_usergroup ( {
713 svcnum => $self->svcnum,
714 groupname => $groupname,
716 my $error = $radius_usergroup->insert;
718 $dbh->rollback if $oldAutoCommit;
724 unless ( $skip_fuzzyfiles ) {
725 $error = $self->queue_fuzzyfiles_update;
727 $dbh->rollback if $oldAutoCommit;
728 return "updating fuzzy search cache: $error";
732 my $cust_pkg = $self->cust_svc->cust_pkg;
735 my $cust_main = $cust_pkg->cust_main;
736 my $agentnum = $cust_main->agentnum;
738 if ( $conf->exists('emailinvoiceautoalways')
739 || $conf->exists('emailinvoiceauto')
740 && ! $cust_main->invoicing_list_emailonly
742 my @invoicing_list = $cust_main->invoicing_list;
743 push @invoicing_list, $self->email;
744 $cust_main->invoicing_list(\@invoicing_list);
749 my $msgnum = $conf->config('welcome_msgnum', $agentnum);
751 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
752 $error = $msg_template->send('cust_main' => $cust_main,
756 my ($to,$welcome_template,$welcome_from,$welcome_subject,$welcome_subject_template,$welcome_mimetype)
757 = ('','','','','','');
759 if ( $conf->exists('welcome_email', $agentnum) ) {
760 $welcome_template = new Text::Template (
762 SOURCE => [ map "$_\n", $conf->config('welcome_email', $agentnum) ]
763 ) or warn "can't create welcome email template: $Text::Template::ERROR";
764 $welcome_from = $conf->config('welcome_email-from', $agentnum);
765 # || 'your-isp-is-dum'
766 $welcome_subject = $conf->config('welcome_email-subject', $agentnum)
768 $welcome_subject_template = new Text::Template (
770 SOURCE => $welcome_subject,
771 ) or warn "can't create welcome email subject template: $Text::Template::ERROR";
772 $welcome_mimetype = $conf->config('welcome_email-mimetype', $agentnum)
775 if ( $welcome_template ) {
776 my $to = join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
780 'custnum' => $self->custnum,
781 'username' => $self->username,
782 'password' => $self->_password,
783 'first' => $cust_main->first,
784 'last' => $cust_main->getfield('last'),
785 'pkg' => $cust_pkg->part_pkg->pkg,
787 my $wqueue = new FS::queue {
788 'svcnum' => $self->svcnum,
789 'job' => 'FS::svc_acct::send_email'
791 my $error = $wqueue->insert(
793 'from' => $welcome_from,
794 'subject' => $welcome_subject_template->fill_in( HASH => \%hash, ),
795 'mimetype' => $welcome_mimetype,
796 'body' => $welcome_template->fill_in( HASH => \%hash, ),
799 $dbh->rollback if $oldAutoCommit;
800 return "error queuing welcome email: $error";
803 if ( $options{'depend_jobnum'} ) {
804 warn "$me depend_jobnum found; adding to welcome email dependancies"
806 if ( ref($options{'depend_jobnum'}) ) {
807 warn "$me adding jobs ". join(', ', @{$options{'depend_jobnum'}} ).
808 "to welcome email dependancies"
810 push @jobnums, @{ $options{'depend_jobnum'} };
812 warn "$me adding job $options{'depend_jobnum'} ".
813 "to welcome email dependancies"
815 push @jobnums, $options{'depend_jobnum'};
819 foreach my $jobnum ( @jobnums ) {
820 my $error = $wqueue->depend_insert($jobnum);
822 $dbh->rollback if $oldAutoCommit;
823 return "error queuing welcome email job dependancy: $error";
829 } # if $welcome_template
833 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
837 # set usage fields and thresholds if unset but set in a package def
838 # AND the package already has a last bill date (otherwise they get double added)
839 sub preinsert_hook_first {
842 return '' unless $self->pkgnum;
844 my $cust_pkg = qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
845 return '' unless $cust_pkg && $cust_pkg->last_bill;
847 my $part_pkg = $cust_pkg->part_pkg;
848 return '' unless $part_pkg && $part_pkg->can('usage_valuehash');
850 my %values = $part_pkg->usage_valuehash;
851 my $multiplier = $conf->exists('svc_acct-usage_threshold')
852 ? 1 - $conf->config('svc_acct-usage_threshold')/100
853 : 0.20; #doesn't matter
855 foreach ( keys %values ) {
856 next if $self->getfield($_);
857 $self->setfield( $_, $values{$_} );
858 $self->setfield( $_. '_threshold', int( $values{$_} * $multiplier ) )
859 if $conf->exists('svc_acct-usage_threshold');
867 Deletes this account from the database. If there is an error, returns the
868 error, otherwise returns false.
870 The corresponding FS::cust_svc record will be deleted as well.
872 (TODOC: new exports!)
879 return "can't delete system account" if $self->_check_system;
881 return "Can't delete an account which is a (svc_forward) source!"
882 if qsearch( 'svc_forward', { 'srcsvc' => $self->svcnum } );
884 return "Can't delete an account which is a (svc_forward) destination!"
885 if qsearch( 'svc_forward', { 'dstsvc' => $self->svcnum } );
887 return "Can't delete an account with (svc_www) web service!"
888 if qsearch( 'svc_www', { 'usersvc' => $self->svcnum } );
890 # what about records in session ? (they should refer to history table)
892 local $SIG{HUP} = 'IGNORE';
893 local $SIG{INT} = 'IGNORE';
894 local $SIG{QUIT} = 'IGNORE';
895 local $SIG{TERM} = 'IGNORE';
896 local $SIG{TSTP} = 'IGNORE';
897 local $SIG{PIPE} = 'IGNORE';
899 my $oldAutoCommit = $FS::UID::AutoCommit;
900 local $FS::UID::AutoCommit = 0;
903 foreach my $cust_main_invoice (
904 qsearch( 'cust_main_invoice', { 'dest' => $self->svcnum } )
906 unless ( defined($cust_main_invoice) ) {
907 warn "WARNING: something's wrong with qsearch";
910 my %hash = $cust_main_invoice->hash;
911 $hash{'dest'} = $self->email;
912 my $new = new FS::cust_main_invoice \%hash;
913 my $error = $new->replace($cust_main_invoice);
915 $dbh->rollback if $oldAutoCommit;
920 foreach my $svc_domain (
921 qsearch( 'svc_domain', { 'catchall' => $self->svcnum } )
923 my %hash = new FS::svc_domain->hash;
924 $hash{'catchall'} = '';
925 my $new = new FS::svc_domain \%hash;
926 my $error = $new->replace($svc_domain);
928 $dbh->rollback if $oldAutoCommit;
933 my $error = $self->SUPER::delete;
935 $dbh->rollback if $oldAutoCommit;
939 foreach my $radius_usergroup (
940 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } )
942 my $error = $radius_usergroup->delete;
944 $dbh->rollback if $oldAutoCommit;
949 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
953 =item replace OLD_RECORD
955 Replaces OLD_RECORD with this one in the database. If there is an error,
956 returns the error, otherwise returns false.
958 The additional field I<usergroup> can optionally be defined; if so it should
959 contain an arrayref of group names. See L<FS::radius_usergroup>.
967 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
971 warn "$me replacing $old with $new\n" if $DEBUG;
975 return "can't modify system account" if $old->_check_system;
978 #no warnings 'numeric'; #alas, a 5.006-ism
981 foreach my $xid (qw( uid gid )) {
983 return "Can't change $xid!"
984 if ! $conf->exists("svc_acct-edit_$xid")
985 && $old->$xid() != $new->$xid()
986 && $new->cust_svc->part_svc->part_svc_column($xid)->columnflag ne 'F'
991 #change homdir when we change username
992 $new->setfield('dir', '') if $old->username ne $new->username;
994 local $SIG{HUP} = 'IGNORE';
995 local $SIG{INT} = 'IGNORE';
996 local $SIG{QUIT} = 'IGNORE';
997 local $SIG{TERM} = 'IGNORE';
998 local $SIG{TSTP} = 'IGNORE';
999 local $SIG{PIPE} = 'IGNORE';
1001 my $oldAutoCommit = $FS::UID::AutoCommit;
1002 local $FS::UID::AutoCommit = 0;
1005 # redundant, but so $new->usergroup gets set
1006 $error = $new->check;
1007 return $error if $error;
1009 $old->usergroup( [ $old->radius_groups ] );
1011 warn $old->email. " old groups: ". join(' ',@{$old->usergroup}). "\n";
1012 warn $new->email. "new groups: ". join(' ',@{$new->usergroup}). "\n";
1014 if ( $new->usergroup ) {
1015 #(sorta) false laziness with FS::part_export::sqlradius::_export_replace
1016 my @newgroups = @{$new->usergroup};
1017 foreach my $oldgroup ( @{$old->usergroup} ) {
1018 if ( grep { $oldgroup eq $_ } @newgroups ) {
1019 @newgroups = grep { $oldgroup ne $_ } @newgroups;
1022 my $radius_usergroup = qsearchs('radius_usergroup', {
1023 svcnum => $old->svcnum,
1024 groupname => $oldgroup,
1026 my $error = $radius_usergroup->delete;
1028 $dbh->rollback if $oldAutoCommit;
1029 return "error deleting radius_usergroup $oldgroup: $error";
1033 foreach my $newgroup ( @newgroups ) {
1034 my $radius_usergroup = new FS::radius_usergroup ( {
1035 svcnum => $new->svcnum,
1036 groupname => $newgroup,
1038 my $error = $radius_usergroup->insert;
1040 $dbh->rollback if $oldAutoCommit;
1041 return "error adding radius_usergroup $newgroup: $error";
1047 $error = $new->SUPER::replace($old, @_);
1049 $dbh->rollback if $oldAutoCommit;
1050 return $error if $error;
1053 if ( $new->username ne $old->username && ! $skip_fuzzyfiles ) {
1054 $error = $new->queue_fuzzyfiles_update;
1056 $dbh->rollback if $oldAutoCommit;
1057 return "updating fuzzy search cache: $error";
1061 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1065 =item queue_fuzzyfiles_update
1067 Used by insert & replace to update the fuzzy search cache
1071 sub queue_fuzzyfiles_update {
1074 local $SIG{HUP} = 'IGNORE';
1075 local $SIG{INT} = 'IGNORE';
1076 local $SIG{QUIT} = 'IGNORE';
1077 local $SIG{TERM} = 'IGNORE';
1078 local $SIG{TSTP} = 'IGNORE';
1079 local $SIG{PIPE} = 'IGNORE';
1081 my $oldAutoCommit = $FS::UID::AutoCommit;
1082 local $FS::UID::AutoCommit = 0;
1085 my $queue = new FS::queue {
1086 'svcnum' => $self->svcnum,
1087 'job' => 'FS::svc_acct::append_fuzzyfiles'
1089 my $error = $queue->insert($self->username);
1091 $dbh->rollback if $oldAutoCommit;
1092 return "queueing job (transaction rolled back): $error";
1095 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1103 Suspends this account by calling export-specific suspend hooks. If there is
1104 an error, returns the error, otherwise returns false.
1106 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
1112 return "can't suspend system account" if $self->_check_system;
1113 $self->SUPER::suspend(@_);
1118 Unsuspends this account by by calling export-specific suspend hooks. If there
1119 is an error, returns the error, otherwise returns false.
1121 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
1127 my %hash = $self->hash;
1128 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
1129 $hash{_password} = $1;
1130 my $new = new FS::svc_acct ( \%hash );
1131 my $error = $new->replace($self);
1132 return $error if $error;
1135 $self->SUPER::unsuspend(@_);
1140 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
1142 If the B<auto_unset_catchall> configuration option is set, this method will
1143 automatically remove any references to the canceled service in the catchall
1144 field of svc_domain. This allows packages that contain both a svc_domain and
1145 its catchall svc_acct to be canceled in one step.
1150 # Only one thing to do at this level
1152 foreach my $svc_domain (
1153 qsearch( 'svc_domain', { catchall => $self->svcnum } ) ) {
1154 if($conf->exists('auto_unset_catchall')) {
1155 my %hash = $svc_domain->hash;
1156 $hash{catchall} = '';
1157 my $new = new FS::svc_domain ( \%hash );
1158 my $error = $new->replace($svc_domain);
1159 return $error if $error;
1161 return "cannot unprovision svc_acct #".$self->svcnum.
1162 " while assigned as catchall for svc_domain #".$svc_domain->svcnum;
1166 $self->SUPER::cancel(@_);
1172 Checks all fields to make sure this is a valid service. If there is an error,
1173 returns the error, otherwise returns false. Called by the insert and replace
1176 Sets any fixed values; see L<FS::part_svc>.
1183 my($recref) = $self->hashref;
1185 my $x = $self->setfixed( $self->_fieldhandlers );
1186 return $x unless ref($x);
1189 if ( $part_svc->part_svc_column('usergroup')->columnflag eq "F" ) {
1191 [ split(',', $part_svc->part_svc_column('usergroup')->columnvalue) ] );
1194 my $error = $self->ut_numbern('svcnum')
1195 #|| $self->ut_number('domsvc')
1196 || $self->ut_foreign_key( 'domsvc', 'svc_domain', 'svcnum' )
1197 || $self->ut_foreign_keyn('pbxsvc', 'svc_pbx', 'svcnum' )
1198 || $self->ut_textn('sec_phrase')
1199 || $self->ut_snumbern('seconds')
1200 || $self->ut_snumbern('upbytes')
1201 || $self->ut_snumbern('downbytes')
1202 || $self->ut_snumbern('totalbytes')
1203 || $self->ut_snumbern('seconds_threshold')
1204 || $self->ut_snumbern('upbytes_threshold')
1205 || $self->ut_snumbern('downbytes_threshold')
1206 || $self->ut_snumbern('totalbytes_threshold')
1207 || $self->ut_enum('_password_encoding', ['',qw(plain crypt ldap)])
1208 || $self->ut_enum('password_selfchange', [ '', 'Y' ])
1209 || $self->ut_enum('password_recover', [ '', 'Y' ])
1211 || $self->ut_anything('cf_privatekey')
1213 || $self->ut_textn('cgp_accessmodes')
1214 || $self->ut_alphan('cgp_type')
1215 || $self->ut_textn('cgp_aliases' ) #well
1217 || $self->ut_alphasn('cgp_rulesallowed')
1218 || $self->ut_enum('cgp_rpopallowed', [ '', 'Y' ])
1219 || $self->ut_enum('cgp_mailtoall', [ '', 'Y' ])
1220 || $self->ut_enum('cgp_addmailtrailer', [ '', 'Y' ])
1221 || $self->ut_snumbern('cgp_archiveafter')
1223 || $self->ut_alphasn('cgp_deletemode')
1224 || $self->ut_enum('cgp_emptytrash', $self->cgp_emptytrash_values)
1225 || $self->ut_alphan('cgp_language')
1226 || $self->ut_textn('cgp_timezone')
1227 || $self->ut_textn('cgp_skinname')
1228 || $self->ut_textn('cgp_prontoskinname')
1229 || $self->ut_alphan('cgp_sendmdnmode')
1231 return $error if $error;
1234 local $username_letter = $username_letter;
1235 if ($self->svcnum) {
1236 my $cust_svc = $self->cust_svc
1237 or return "no cust_svc record found for svcnum ". $self->svcnum;
1238 my $cust_pkg = $cust_svc->cust_pkg;
1240 if ($self->pkgnum) {
1241 $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $self->pkgnum } );#complain?
1245 $conf->exists('username-letter', $cust_pkg->cust_main->agentnum);
1248 my $ulen = $usernamemax || $self->dbdef_table->column('username')->length;
1250 $recref->{username} =~ /^([a-z0-9_\-\.\&\%\:\/\=]{$usernamemin,$ulen})$/i
1251 or return gettext('illegal_username'). " ($usernamemin-$ulen): ". $recref->{username};
1252 $recref->{username} = $1;
1254 unless ( $username_uppercase ) {
1255 $recref->{username} =~ /[A-Z]/ and return gettext('illegal_username');
1257 if ( $username_letterfirst ) {
1258 $recref->{username} =~ /^[a-z]/ or return gettext('illegal_username');
1259 } elsif ( $username_letter ) {
1260 $recref->{username} =~ /[a-z]/ or return gettext('illegal_username');
1262 if ( $username_noperiod ) {
1263 $recref->{username} =~ /\./ and return gettext('illegal_username');
1265 if ( $username_nounderscore ) {
1266 $recref->{username} =~ /_/ and return gettext('illegal_username');
1268 if ( $username_nodash ) {
1269 $recref->{username} =~ /\-/ and return gettext('illegal_username');
1271 unless ( $username_ampersand ) {
1272 $recref->{username} =~ /\&/ and return gettext('illegal_username');
1274 unless ( $username_percent ) {
1275 $recref->{username} =~ /\%/ and return gettext('illegal_username');
1277 unless ( $username_colon ) {
1278 $recref->{username} =~ /\:/ and return gettext('illegal_username');
1280 unless ( $username_slash ) {
1281 $recref->{username} =~ /\// and return gettext('illegal_username');
1283 unless ( $username_equals ) {
1284 $recref->{username} =~ /\=/ and return gettext('illegal_username');
1287 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum: ".$recref->{popnum};
1288 $recref->{popnum} = $1;
1289 return "Unknown popnum" unless
1290 ! $recref->{popnum} ||
1291 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
1293 unless ( $part_svc->part_svc_column('uid')->columnflag eq 'F' ) {
1295 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
1296 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
1298 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
1299 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
1300 #not all systems use gid=uid
1301 #you can set a fixed gid in part_svc
1303 return "Only root can have uid 0"
1304 if $recref->{uid} == 0
1305 && $recref->{username} !~ /^(root|toor|smtp)$/;
1307 unless ( $recref->{username} eq 'sync' ) {
1308 if ( grep $_ eq $recref->{shell}, @shells ) {
1309 $recref->{shell} = (grep $_ eq $recref->{shell}, @shells)[0];
1311 return "Illegal shell \`". $self->shell. "\'; ".
1312 "shells configuration value contains: @shells";
1315 $recref->{shell} = '/bin/sync';
1319 $recref->{gid} ne '' ?
1320 return "Can't have gid without uid" : ( $recref->{gid}='' );
1321 #$recref->{dir} ne '' ?
1322 # return "Can't have directory without uid" : ( $recref->{dir}='' );
1323 $recref->{shell} ne '' ?
1324 return "Can't have shell without uid" : ( $recref->{shell}='' );
1327 unless ( $part_svc->part_svc_column('dir')->columnflag eq 'F' ) {
1329 $recref->{dir} =~ /^([\/\w\-\.\&]*)$/
1330 or return "Illegal directory: ". $recref->{dir};
1331 $recref->{dir} = $1;
1332 return "Illegal directory"
1333 if $recref->{dir} =~ /(^|\/)\.+(\/|$)/; #no .. component
1334 return "Illegal directory"
1335 if $recref->{dir} =~ /\&/ && ! $username_ampersand;
1336 unless ( $recref->{dir} ) {
1337 $recref->{dir} = $dir_prefix . '/';
1338 if ( $dirhash > 0 ) {
1339 for my $h ( 1 .. $dirhash ) {
1340 $recref->{dir} .= substr($recref->{username}, $h-1, 1). '/';
1342 } elsif ( $dirhash < 0 ) {
1343 for my $h ( reverse $dirhash .. -1 ) {
1344 $recref->{dir} .= substr($recref->{username}, $h, 1). '/';
1347 $recref->{dir} .= $recref->{username};
1353 # $error = $self->ut_textn('finger');
1354 # return $error if $error;
1355 if ( $self->getfield('finger') eq '' ) {
1356 my $cust_pkg = $self->svcnum
1357 ? $self->cust_svc->cust_pkg
1358 : qsearchs('cust_pkg', { 'pkgnum' => $self->getfield('pkgnum') } );
1360 my $cust_main = $cust_pkg->cust_main;
1361 $self->setfield('finger', $cust_main->first.' '.$cust_main->get('last') );
1364 $self->getfield('finger') =~
1365 /^([µ_0123456789aAáÁàÀâÂåÅäÄãêæÆbBcCçÇdDðÐeEéÉèÈêÊëËfFgGhHiIíÍìÌîÎïÏjJkKlLmMnNñÑoOóÓòÒôÔöÖõÕøغpPqQrRsSßtTuUúÚùÙûÛüÜvVwWxXyYýÝÿzZþÞ \t\!\@\#\$\%\&\(\)\-\+\;\'\"\,\.\?\/\*\<\>]*)$/
1366 or return "Illegal finger: ". $self->getfield('finger');
1367 $self->setfield('finger', $1);
1369 for (qw( quota file_quota file_maxsize )) {
1370 $recref->{$_} =~ /^(\w*)$/ or return "Illegal $_";
1373 $recref->{file_maxnum} =~ /^\s*(\d*)\s*$/ or return "Illegal file_maxnum";
1374 $recref->{file_maxnum} = $1;
1376 unless ( $part_svc->part_svc_column('slipip')->columnflag eq 'F' ) {
1377 if ( $recref->{slipip} eq '' ) {
1378 $recref->{slipip} = '';
1379 } elsif ( $recref->{slipip} eq '0e0' ) {
1380 $recref->{slipip} = '0e0';
1382 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
1383 or return "Illegal slipip: ". $self->slipip;
1384 $recref->{slipip} = $1;
1389 #arbitrary RADIUS stuff; allow ut_textn for now
1390 foreach ( grep /^radius_/, fields('svc_acct') ) {
1391 $self->ut_textn($_);
1394 # First, if _password is blank, generate one and set default encoding.
1395 if ( ! $recref->{_password} ) {
1396 $error = $self->set_password('');
1398 # But if there's a _password but no encoding, assume it's plaintext and
1399 # set it to default encoding.
1400 elsif ( ! $recref->{_password_encoding} ) {
1401 $error = $self->set_password($recref->{_password});
1403 return $error if $error;
1405 # Next, check _password to ensure compliance with the encoding.
1406 if ( $recref->{_password_encoding} eq 'ldap' ) {
1408 if ( $recref->{_password} =~ /^(\{[\w\-]+\})(!?.{0,64})$/ ) {
1409 $recref->{_password} = uc($1).$2;
1411 return 'Illegal (ldap-encoded) password: '. $recref->{_password};
1414 } elsif ( $recref->{_password_encoding} eq 'crypt' ) {
1416 if ( $recref->{_password} =~
1417 #/^(\$\w+\$.*|[\w\+\/]{13}|_[\w\+\/]{19}|\*)$/
1418 /^(!!?)?(\$\w+\$.*|[\w\+\/\.]{13}|_[\w\+\/\.]{19}|\*)$/
1421 $recref->{_password} = ( defined($1) ? $1 : '' ). $2;
1424 return 'Illegal (crypt-encoded) password: '. $recref->{_password};
1427 } elsif ( $recref->{_password_encoding} eq 'plain' ) {
1428 # Password randomization is now in set_password.
1429 # Strip whitespace characters, check length requirements, etc.
1430 if ( $recref->{_password} =~ /^([^\t\n]{$passwordmin,$passwordmax})$/ ) {
1431 $recref->{_password} = $1;
1433 return gettext('illegal_password'). " $passwordmin-$passwordmax ".
1434 FS::Msgcat::_gettext('illegal_password_characters').
1435 ": ". $recref->{_password};
1438 if ( $password_noampersand ) {
1439 $recref->{_password} =~ /\&/ and return gettext('illegal_password');
1441 if ( $password_noexclamation ) {
1442 $recref->{_password} =~ /\!/ and return gettext('illegal_password');
1446 return "invalid password encoding ('".$recref->{_password_encoding}."'";
1448 $self->SUPER::check;
1453 sub _password_encryption {
1455 my $encoding = lc($self->_password_encoding);
1456 return if !$encoding;
1457 return 'plain' if $encoding eq 'plain';
1458 if($encoding eq 'crypt') {
1459 my $pass = $self->_password;
1460 $pass =~ s/^\*SUSPENDED\* //;
1462 return 'md5' if $pass =~ /^\$1\$/;
1463 #return 'blowfish' if $self->_password =~ /^\$2\$/;
1464 return 'des' if length($pass) == 13;
1467 if($encoding eq 'ldap') {
1468 uc($self->_password) =~ /^\{([\w-]+)\}/;
1469 return 'crypt' if $1 eq 'CRYPT' or $1 eq 'DES';
1470 return 'plain' if $1 eq 'PLAIN' or $1 eq 'CLEARTEXT';
1471 return 'md5' if $1 eq 'MD5';
1472 return 'sha1' if $1 eq 'SHA' or $1 eq 'SHA-1';
1479 sub get_cleartext_password {
1481 if($self->_password_encryption eq 'plain') {
1482 if($self->_password_encoding eq 'ldap') {
1483 $self->_password =~ /\{\w+\}(.*)$/;
1487 return $self->_password;
1496 Set the cleartext password for the account. If _password_encoding is set, the
1497 new password will be encoded according to the existing method (including
1498 encryption mode, if it can be determined). Otherwise,
1499 config('default-password-encoding') is used.
1501 If no password is supplied (or a zero-length password when minimum password length
1502 is >0), one will be generated randomly.
1507 my( $self, $pass ) = ( shift, shift );
1509 warn "[$me] set_password (to $pass) called on $self: ". Dumper($self)
1512 my $failure = gettext('illegal_password'). " $passwordmin-$passwordmax ".
1513 FS::Msgcat::_gettext('illegal_password_characters').
1516 my( $encoding, $encryption ) = ('', '');
1518 if ( $self->_password_encoding ) {
1519 $encoding = $self->_password_encoding;
1520 # identify existing encryption method, try to use it.
1521 $encryption = $self->_password_encryption;
1523 # use the system default
1529 # set encoding to system default
1530 ($encoding, $encryption) =
1531 split(/-/, lc($conf->config('default-password-encoding')));
1532 $encoding ||= 'legacy';
1533 $self->_password_encoding($encoding);
1536 if ( $encoding eq 'legacy' ) {
1538 # The legacy behavior from check():
1539 # If the password is blank, randomize it and set encoding to 'plain'.
1540 if(!defined($pass) or (length($pass) == 0 and $passwordmin)) {
1541 $pass = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) );
1542 $self->_password_encoding('plain');
1544 # Prefix + valid-length password
1545 if ( $pass =~ /^((\*SUSPENDED\* |!!?)?)([^\t\n]{$passwordmin,$passwordmax})$/ ) {
1547 $self->_password_encoding('plain');
1548 # Prefix + crypt string
1549 } elsif ( $pass =~ /^((\*SUSPENDED\* |!!?)?)([\w\.\/\$\;\+]{13,64})$/ ) {
1551 $self->_password_encoding('crypt');
1552 # Various disabled crypt passwords
1553 } elsif ( $pass eq '*' || $pass eq '!' || $pass eq '!!' ) {
1554 $self->_password_encoding('crypt');
1560 $self->_password($pass);
1566 if $passwordmin && length($pass) < $passwordmin
1567 or $passwordmax && length($pass) > $passwordmax;
1569 if ( $encoding eq 'crypt' ) {
1570 if ($encryption eq 'md5') {
1571 $pass = unix_md5_crypt($pass);
1572 } elsif ($encryption eq 'des') {
1573 $pass = crypt($pass, $saltset[int(rand(64))].$saltset[int(rand(64))]);
1576 } elsif ( $encoding eq 'ldap' ) {
1577 if ($encryption eq 'md5') {
1578 $pass = md5_base64($pass);
1579 } elsif ($encryption eq 'sha1') {
1580 $pass = sha1_base64($pass);
1581 } elsif ($encryption eq 'crypt') {
1582 $pass = crypt($pass, $saltset[int(rand(64))].$saltset[int(rand(64))]);
1584 # else $encryption eq 'plain', do nothing
1585 $pass = '{'.uc($encryption).'}'.$pass;
1587 # else encoding eq 'plain'
1589 $self->_password($pass);
1595 Internal function to check the username against the list of system usernames
1596 from the I<system_usernames> configuration value. Returns true if the username
1597 is listed on the system username list.
1603 scalar( grep { $self->username eq $_ || $self->email eq $_ }
1604 $conf->config('system_usernames')
1608 =item _check_duplicate
1610 Internal method to check for duplicates usernames, username@domain pairs and
1613 If the I<global_unique-username> configuration value is set to B<username> or
1614 B<username@domain>, enforces global username or username@domain uniqueness.
1616 In all cases, check for duplicate uids and usernames or username@domain pairs
1617 per export and with identical I<svcpart> values.
1621 sub _check_duplicate {
1624 my $global_unique = $conf->config('global_unique-username') || 'none';
1625 return '' if $global_unique eq 'disabled';
1629 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
1630 unless ( $part_svc ) {
1631 return 'unknown svcpart '. $self->svcpart;
1634 my @dup_user = grep { !$self->svcnum || $_->svcnum != $self->svcnum }
1635 qsearch( 'svc_acct', { 'username' => $self->username } );
1636 return gettext('username_in_use')
1637 if $global_unique eq 'username' && @dup_user;
1639 my @dup_userdomain = grep { !$self->svcnum || $_->svcnum != $self->svcnum }
1640 qsearch( 'svc_acct', { 'username' => $self->username,
1641 'domsvc' => $self->domsvc } );
1642 return gettext('username_in_use')
1643 if $global_unique eq 'username@domain' && @dup_userdomain;
1646 if ( $part_svc->part_svc_column('uid')->columnflag ne 'F'
1647 && $self->username !~ /^(toor|(hyla)?fax)$/ ) {
1648 @dup_uid = grep { !$self->svcnum || $_->svcnum != $self->svcnum }
1649 qsearch( 'svc_acct', { 'uid' => $self->uid } );
1654 if ( @dup_user || @dup_userdomain || @dup_uid ) {
1655 my $exports = FS::part_export::export_info('svc_acct');
1656 my %conflict_user_svcpart;
1657 my %conflict_userdomain_svcpart = ( $self->svcpart => 'SELF', );
1659 foreach my $part_export ( $part_svc->part_export ) {
1661 #this will catch to the same exact export
1662 my @svcparts = map { $_->svcpart } $part_export->export_svc;
1664 #this will catch to exports w/same exporthost+type ???
1665 #my @other_part_export = qsearch('part_export', {
1666 # 'machine' => $part_export->machine,
1667 # 'exporttype' => $part_export->exporttype,
1669 #foreach my $other_part_export ( @other_part_export ) {
1670 # push @svcparts, map { $_->svcpart }
1671 # qsearch('export_svc', { 'exportnum' => $part_export->exportnum });
1674 #my $nodomain = $exports->{$part_export->exporttype}{'nodomain'};
1675 #silly kludge to avoid uninitialized value errors
1676 my $nodomain = exists( $exports->{$part_export->exporttype}{'nodomain'} )
1677 ? $exports->{$part_export->exporttype}{'nodomain'}
1679 if ( $nodomain =~ /^Y/i ) {
1680 $conflict_user_svcpart{$_} = $part_export->exportnum
1683 $conflict_userdomain_svcpart{$_} = $part_export->exportnum
1688 foreach my $dup_user ( @dup_user ) {
1689 my $dup_svcpart = $dup_user->cust_svc->svcpart;
1690 if ( exists($conflict_user_svcpart{$dup_svcpart}) ) {
1691 return "duplicate username ". $self->username.
1692 ": conflicts with svcnum ". $dup_user->svcnum.
1693 " via exportnum ". $conflict_user_svcpart{$dup_svcpart};
1697 foreach my $dup_userdomain ( @dup_userdomain ) {
1698 my $dup_svcpart = $dup_userdomain->cust_svc->svcpart;
1699 if ( exists($conflict_userdomain_svcpart{$dup_svcpart}) ) {
1700 return "duplicate username\@domain ". $self->email.
1701 ": conflicts with svcnum ". $dup_userdomain->svcnum.
1702 " via exportnum ". $conflict_userdomain_svcpart{$dup_svcpart};
1706 foreach my $dup_uid ( @dup_uid ) {
1707 my $dup_svcpart = $dup_uid->cust_svc->svcpart;
1708 if ( exists($conflict_user_svcpart{$dup_svcpart})
1709 || exists($conflict_userdomain_svcpart{$dup_svcpart}) ) {
1710 return "duplicate uid ". $self->uid.
1711 ": conflicts with svcnum ". $dup_uid->svcnum.
1713 ( $conflict_user_svcpart{$dup_svcpart}
1714 || $conflict_userdomain_svcpart{$dup_svcpart} );
1726 Depriciated, use radius_reply instead.
1731 carp "FS::svc_acct::radius depriciated, use radius_reply";
1732 $_[0]->radius_reply;
1737 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1738 reply attributes of this record.
1740 Note that this is now the preferred method for reading RADIUS attributes -
1741 accessing the columns directly is discouraged, as the column names are
1742 expected to change in the future.
1749 return %{ $self->{'radius_reply'} }
1750 if exists $self->{'radius_reply'};
1755 my($column, $attrib) = ($1, $2);
1756 #$attrib =~ s/_/\-/g;
1757 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1758 } grep { /^radius_/ && $self->getfield($_) } fields( $self->table );
1760 if ( $self->slipip && $self->slipip ne '0e0' ) {
1761 $reply{$radius_ip} = $self->slipip;
1764 if ( $self->seconds !~ /^$/ ) {
1765 $reply{'Session-Timeout'} = $self->seconds;
1768 if ( $conf->exists('radius-chillispot-max') ) {
1769 #http://dev.coova.org/svn/coova-chilli/doc/dictionary.chillispot
1771 #hmm. just because sqlradius.pm says so?
1778 foreach my $what (qw( input output total )) {
1779 my $is = $whatis{$what}.'bytes';
1780 if ( $self->$is() =~ /\d/ ) {
1781 my $big = new Math::BigInt $self->$is();
1782 $big = new Math::BigInt '0' if $big->is_neg();
1783 my $att = "Chillispot-Max-\u$what";
1784 $reply{"$att-Octets"} = $big->copy->band(0xffffffff)->bstr;
1785 $reply{"$att-Gigawords"} = $big->copy->brsft(32)->bstr;
1796 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1797 check attributes of this record.
1799 Note that this is now the preferred method for reading RADIUS attributes -
1800 accessing the columns directly is discouraged, as the column names are
1801 expected to change in the future.
1808 return %{ $self->{'radius_check'} }
1809 if exists $self->{'radius_check'};
1814 my($column, $attrib) = ($1, $2);
1815 #$attrib =~ s/_/\-/g;
1816 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1817 } grep { /^rc_/ && $self->getfield($_) } fields( $self->table );
1820 my($pw_attrib, $password) = $self->radius_password;
1821 $check{$pw_attrib} = $password;
1823 my $cust_svc = $self->cust_svc;
1825 my $cust_pkg = $cust_svc->cust_pkg;
1826 if ( $cust_pkg && $cust_pkg->part_pkg->is_prepaid && $cust_pkg->bill ) {
1827 $check{'Expiration'} = time2str('%B %e %Y %T', $cust_pkg->bill ); #http://lists.cistron.nl/pipermail/freeradius-users/2005-January/040184.html
1830 warn "WARNING: no cust_svc record for svc_acct.svcnum ". $self->svcnum.
1831 "; can't set Expiration\n"
1839 =item radius_password
1841 Returns a key/value pair containing the RADIUS attribute name and value
1846 sub radius_password {
1850 if ( $self->_password_encoding eq 'ldap' ) {
1851 $pw_attrib = 'Password-With-Header';
1852 } elsif ( $self->_password_encoding eq 'crypt' ) {
1853 $pw_attrib = 'Crypt-Password';
1854 } elsif ( $self->_password_encoding eq 'plain' ) {
1855 $pw_attrib = $radius_password;
1857 $pw_attrib = length($self->_password) <= 12
1862 ($pw_attrib, $self->_password);
1868 This method instructs the object to "snapshot" or freeze RADIUS check and
1869 reply attributes to the current values.
1873 #bah, my english is too broken this morning
1874 #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
1875 #the FS::cust_pkg's replace method to trigger the correct export updates when
1876 #package dates change)
1881 $self->{$_} = { $self->$_() }
1882 foreach qw( radius_reply radius_check );
1886 =item forget_snapshot
1888 This methos instructs the object to forget any previously snapshotted
1889 RADIUS check and reply attributes.
1893 sub forget_snapshot {
1897 foreach qw( radius_reply radius_check );
1901 =item domain [ END_TIMESTAMP [ START_TIMESTAMP ] ]
1903 Returns the domain associated with this account.
1905 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
1912 die "svc_acct.domsvc is null for svcnum ". $self->svcnum unless $self->domsvc;
1913 my $svc_domain = $self->svc_domain(@_)
1914 or die "no svc_domain.svcnum for svc_acct.domsvc ". $self->domsvc;
1915 $svc_domain->domain;
1920 Returns the FS::cust_svc record for this account (see L<FS::cust_svc>).
1924 #inherited from svc_Common
1926 =item email [ END_TIMESTAMP [ START_TIMESTAMP ] ]
1928 Returns an email address associated with the account.
1930 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
1937 $self->username. '@'. $self->domain(@_);
1942 Returns an array of FS::acct_snarf records associated with the account.
1949 'table' => 'acct_snarf',
1950 'hashref' => { 'svcnum' => $self->svcnum },
1951 #'order_by' => 'ORDER BY priority ASC',
1955 =item cgp_rpop_hashref
1957 Returns an arrayref of RPOP data suitable for Communigate Pro API commands.
1961 sub cgp_rpop_hashref {
1963 { map { $_->snarfname => $_->cgp_hashref } $self->acct_snarf };
1966 =item decrement_upbytes OCTETS
1968 Decrements the I<upbytes> field of this record by the given amount. If there
1969 is an error, returns the error, otherwise returns false.
1973 sub decrement_upbytes {
1974 shift->_op_usage('-', 'upbytes', @_);
1977 =item increment_upbytes OCTETS
1979 Increments the I<upbytes> field of this record by the given amount. If there
1980 is an error, returns the error, otherwise returns false.
1984 sub increment_upbytes {
1985 shift->_op_usage('+', 'upbytes', @_);
1988 =item decrement_downbytes OCTETS
1990 Decrements the I<downbytes> field of this record by the given amount. If there
1991 is an error, returns the error, otherwise returns false.
1995 sub decrement_downbytes {
1996 shift->_op_usage('-', 'downbytes', @_);
1999 =item increment_downbytes OCTETS
2001 Increments the I<downbytes> field of this record by the given amount. If there
2002 is an error, returns the error, otherwise returns false.
2006 sub increment_downbytes {
2007 shift->_op_usage('+', 'downbytes', @_);
2010 =item decrement_totalbytes OCTETS
2012 Decrements the I<totalbytes> field of this record by the given amount. If there
2013 is an error, returns the error, otherwise returns false.
2017 sub decrement_totalbytes {
2018 shift->_op_usage('-', 'totalbytes', @_);
2021 =item increment_totalbytes OCTETS
2023 Increments the I<totalbytes> field of this record by the given amount. If there
2024 is an error, returns the error, otherwise returns false.
2028 sub increment_totalbytes {
2029 shift->_op_usage('+', 'totalbytes', @_);
2032 =item decrement_seconds SECONDS
2034 Decrements the I<seconds> field of this record by the given amount. If there
2035 is an error, returns the error, otherwise returns false.
2039 sub decrement_seconds {
2040 shift->_op_usage('-', 'seconds', @_);
2043 =item increment_seconds SECONDS
2045 Increments the I<seconds> field of this record by the given amount. If there
2046 is an error, returns the error, otherwise returns false.
2050 sub increment_seconds {
2051 shift->_op_usage('+', 'seconds', @_);
2059 my %op2condition = (
2060 '-' => sub { my($self, $column, $amount) = @_;
2061 $self->$column - $amount <= 0;
2063 '+' => sub { my($self, $column, $amount) = @_;
2064 ($self->$column || 0) + $amount > 0;
2067 my %op2warncondition = (
2068 '-' => sub { my($self, $column, $amount) = @_;
2069 my $threshold = $column . '_threshold';
2070 $self->$column - $amount <= $self->$threshold + 0;
2072 '+' => sub { my($self, $column, $amount) = @_;
2073 ($self->$column || 0) + $amount > 0;
2078 my( $self, $op, $column, $amount ) = @_;
2080 warn "$me _op_usage called for $column on svcnum ". $self->svcnum.
2081 ' ('. $self->email. "): $op $amount\n"
2084 return '' unless $amount;
2086 local $SIG{HUP} = 'IGNORE';
2087 local $SIG{INT} = 'IGNORE';
2088 local $SIG{QUIT} = 'IGNORE';
2089 local $SIG{TERM} = 'IGNORE';
2090 local $SIG{TSTP} = 'IGNORE';
2091 local $SIG{PIPE} = 'IGNORE';
2093 my $oldAutoCommit = $FS::UID::AutoCommit;
2094 local $FS::UID::AutoCommit = 0;
2097 my $sql = "UPDATE svc_acct SET $column = ".
2098 " CASE WHEN $column IS NULL THEN 0 ELSE $column END ". #$column||0
2099 " $op ? WHERE svcnum = ?";
2103 my $sth = $dbh->prepare( $sql )
2104 or die "Error preparing $sql: ". $dbh->errstr;
2105 my $rv = $sth->execute($amount, $self->svcnum);
2106 die "Error executing $sql: ". $sth->errstr
2107 unless defined($rv);
2108 die "Can't update $column for svcnum". $self->svcnum
2111 #$self->snapshot; #not necessary, we retain the old values
2112 #create an object with the updated usage values
2113 my $new = qsearchs('svc_acct', { 'svcnum' => $self->svcnum });
2115 my $error = $new->replace($self);
2117 $dbh->rollback if $oldAutoCommit;
2118 return "Error replacing: $error";
2121 #overlimit_action eq 'cancel' handling
2122 my $cust_pkg = $self->cust_svc->cust_pkg;
2124 && $cust_pkg->part_pkg->option('overlimit_action', 1) eq 'cancel'
2125 && $op eq '-' && &{$op2condition{$op}}($self, $column, $amount)
2129 my $error = $cust_pkg->cancel; #XXX should have a reason
2131 $dbh->rollback if $oldAutoCommit;
2132 return "Error cancelling: $error";
2135 #nothing else is relevant if we're cancelling, so commit & return success
2136 warn "$me update successful; committing\n"
2138 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2143 my $action = $op2action{$op};
2145 if ( &{$op2condition{$op}}($self, $column, $amount) &&
2146 ( $action eq 'suspend' && !$self->overlimit
2147 || $action eq 'unsuspend' && $self->overlimit )
2150 my $error = $self->_op_overlimit($action);
2152 $dbh->rollback if $oldAutoCommit;
2158 if ( $conf->exists("svc_acct-usage_$action")
2159 && &{$op2condition{$op}}($self, $column, $amount) ) {
2160 #my $error = $self->$action();
2161 my $error = $self->cust_svc->cust_pkg->$action();
2162 # $error ||= $self->overlimit($action);
2164 $dbh->rollback if $oldAutoCommit;
2165 return "Error ${action}ing: $error";
2169 if ($warning_template && &{$op2warncondition{$op}}($self, $column, $amount)) {
2170 my $wqueue = new FS::queue {
2171 'svcnum' => $self->svcnum,
2172 'job' => 'FS::svc_acct::reached_threshold',
2177 $to = $warning_cc if &{$op2condition{$op}}($self, $column, $amount);
2181 my $error = $wqueue->insert(
2182 'svcnum' => $self->svcnum,
2184 'column' => $column,
2188 $dbh->rollback if $oldAutoCommit;
2189 return "Error queuing threshold activity: $error";
2193 warn "$me update successful; committing\n"
2195 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2201 my( $self, $action ) = @_;
2203 local $SIG{HUP} = 'IGNORE';
2204 local $SIG{INT} = 'IGNORE';
2205 local $SIG{QUIT} = 'IGNORE';
2206 local $SIG{TERM} = 'IGNORE';
2207 local $SIG{TSTP} = 'IGNORE';
2208 local $SIG{PIPE} = 'IGNORE';
2210 my $oldAutoCommit = $FS::UID::AutoCommit;
2211 local $FS::UID::AutoCommit = 0;
2214 my $cust_pkg = $self->cust_svc->cust_pkg;
2216 my $conf_overlimit =
2218 ? $conf->config('overlimit_groups', $cust_pkg->cust_main->agentnum )
2219 : $conf->config('overlimit_groups');
2221 foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
2223 my $groups = $conf_overlimit || $part_export->option('overlimit_groups');
2224 next unless $groups;
2226 my $gref = &{ $self->_fieldhandlers->{'usergroup'} }( $self, $groups );
2228 my $other = new FS::svc_acct $self->hashref;
2229 $other->usergroup( $gref );
2232 if ($action eq 'suspend') {
2235 } else { # $action eq 'unsuspend'
2240 my $error = $part_export->export_replace($new, $old)
2241 || $self->overlimit($action);
2244 $dbh->rollback if $oldAutoCommit;
2245 return "Error replacing radius groups: $error";
2250 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2256 my( $self, $valueref, %options ) = @_;
2258 warn "$me set_usage called for svcnum ". $self->svcnum.
2259 ' ('. $self->email. "): ".
2260 join(', ', map { "$_ => " . $valueref->{$_}} keys %$valueref) . "\n"
2263 local $SIG{HUP} = 'IGNORE';
2264 local $SIG{INT} = 'IGNORE';
2265 local $SIG{QUIT} = 'IGNORE';
2266 local $SIG{TERM} = 'IGNORE';
2267 local $SIG{TSTP} = 'IGNORE';
2268 local $SIG{PIPE} = 'IGNORE';
2270 local $FS::svc_Common::noexport_hack = 1;
2271 my $oldAutoCommit = $FS::UID::AutoCommit;
2272 local $FS::UID::AutoCommit = 0;
2277 if ( $options{null} ) {
2278 %handyhash = ( map { ( $_ => undef, $_."_threshold" => undef ) }
2279 qw( seconds upbytes downbytes totalbytes )
2282 foreach my $field (keys %$valueref){
2283 $reset = 1 if $valueref->{$field};
2284 $self->setfield($field, $valueref->{$field});
2285 $self->setfield( $field.'_threshold',
2286 int($self->getfield($field)
2287 * ( $conf->exists('svc_acct-usage_threshold')
2288 ? 1 - $conf->config('svc_acct-usage_threshold')/100
2293 $handyhash{$field} = $self->getfield($field);
2294 $handyhash{$field.'_threshold'} = $self->getfield($field.'_threshold');
2296 #my $error = $self->replace; #NO! we avoid the call to ->check for
2297 #die $error if $error; #services not explicity changed via the UI
2299 my $sql = "UPDATE svc_acct SET " .
2300 join (',', map { "$_ = ?" } (keys %handyhash) ).
2301 " WHERE svcnum = ". $self->svcnum;
2306 if (scalar(keys %handyhash)) {
2307 my $sth = $dbh->prepare( $sql )
2308 or die "Error preparing $sql: ". $dbh->errstr;
2309 my $rv = $sth->execute(values %handyhash);
2310 die "Error executing $sql: ". $sth->errstr
2311 unless defined($rv);
2312 die "Can't update usage for svcnum ". $self->svcnum
2316 #$self->snapshot; #not necessary, we retain the old values
2317 #create an object with the updated usage values
2318 my $new = qsearchs('svc_acct', { 'svcnum' => $self->svcnum });
2319 local($FS::Record::nowarn_identical) = 1;
2320 my $error = $new->replace($self); #call exports
2322 $dbh->rollback if $oldAutoCommit;
2323 return "Error replacing: $error";
2330 $error = $self->_op_overlimit('unsuspend')
2331 if $self->overlimit;;
2333 $error ||= $self->cust_svc->cust_pkg->unsuspend
2334 if $conf->exists("svc_acct-usage_unsuspend");
2337 $dbh->rollback if $oldAutoCommit;
2338 return "Error unsuspending: $error";
2343 warn "$me update successful; committing\n"
2345 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2351 =item recharge HASHREF
2353 Increments usage columns by the amount specified in HASHREF as
2354 column=>amount pairs.
2359 my ($self, $vhash) = @_;
2362 warn "[$me] recharge called on $self: ". Dumper($self).
2363 "\nwith vhash: ". Dumper($vhash);
2366 my $oldAutoCommit = $FS::UID::AutoCommit;
2367 local $FS::UID::AutoCommit = 0;
2371 foreach my $column (keys %$vhash){
2372 $error ||= $self->_op_usage('+', $column, $vhash->{$column});
2376 $dbh->rollback if $oldAutoCommit;
2378 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2383 =item is_rechargeable
2385 Returns true if this svc_account can be "recharged" and false otherwise.
2389 sub is_rechargable {
2391 $self->seconds ne ''
2392 || $self->upbytes ne ''
2393 || $self->downbytes ne ''
2394 || $self->totalbytes ne '';
2397 =item seconds_since TIMESTAMP
2399 Returns the number of seconds this account has been online since TIMESTAMP,
2400 according to the session monitor (see L<FS::Session>).
2402 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
2403 L<Time::Local> and L<Date::Parse> for conversion functions.
2407 #note: POD here, implementation in FS::cust_svc
2410 $self->cust_svc->seconds_since(@_);
2413 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
2415 Returns the numbers of seconds this account has been online between
2416 TIMESTAMP_START (inclusive) and TIMESTAMP_END (exclusive), according to an
2417 external SQL radacct table, specified via sqlradius export. Sessions which
2418 started in the specified range but are still open are counted from session
2419 start to the end of the range (unless they are over 1 day old, in which case
2420 they are presumed missing their stop record and not counted). Also, sessions
2421 which end in the range but started earlier are counted from the start of the
2422 range to session end. Finally, sessions which start before the range but end
2423 after are counted for the entire range.
2425 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
2426 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
2431 #note: POD here, implementation in FS::cust_svc
2432 sub seconds_since_sqlradacct {
2434 $self->cust_svc->seconds_since_sqlradacct(@_);
2437 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
2439 Returns the sum of the given attribute for all accounts (see L<FS::svc_acct>)
2440 in this package for sessions ending between TIMESTAMP_START (inclusive) and
2441 TIMESTAMP_END (exclusive).
2443 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
2444 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
2449 #note: POD here, implementation in FS::cust_svc
2450 sub attribute_since_sqlradacct {
2452 $self->cust_svc->attribute_since_sqlradacct(@_);
2455 =item get_session_history TIMESTAMP_START TIMESTAMP_END
2457 Returns an array of hash references of this customers login history for the
2458 given time range. (document this better)
2462 sub get_session_history {
2464 $self->cust_svc->get_session_history(@_);
2467 =item last_login_text
2469 Returns text describing the time of last login.
2473 sub last_login_text {
2475 $self->last_login ? ctime($self->last_login) : 'unknown';
2478 =item get_cdrs TIMESTAMP_START TIMESTAMP_END [ 'OPTION' => 'VALUE ... ]
2483 my($self, $start, $end, %opt ) = @_;
2485 my $did = $self->username; #yup
2487 my $prefix = $opt{'default_prefix'}; #convergent.au '+61'
2489 my $for_update = $opt{'for_update'} ? 'FOR UPDATE' : '';
2491 #SELECT $for_update * FROM cdr
2492 # WHERE calldate >= $start #need a conversion
2493 # AND calldate < $end #ditto
2494 # AND ( charged_party = "$did"
2495 # OR charged_party = "$prefix$did" #if length($prefix);
2496 # OR ( ( charged_party IS NULL OR charged_party = '' )
2498 # ( src = "$did" OR src = "$prefix$did" ) # if length($prefix)
2501 # AND ( freesidestatus IS NULL OR freesidestatus = '' )
2504 if ( length($prefix) ) {
2506 " AND ( charged_party = '$did'
2507 OR charged_party = '$prefix$did'
2508 OR ( ( charged_party IS NULL OR charged_party = '' )
2510 ( src = '$did' OR src = '$prefix$did' )
2516 " AND ( charged_party = '$did'
2517 OR ( ( charged_party IS NULL OR charged_party = '' )
2527 'select' => "$for_update *",
2530 #( freesidestatus IS NULL OR freesidestatus = '' )
2531 'freesidestatus' => '',
2533 'extra_sql' => $charged_or_src,
2541 Returns all RADIUS groups for this account (see L<FS::radius_usergroup>).
2547 if ( $self->usergroup ) {
2548 confess "explicitly specified usergroup not an arrayref: ". $self->usergroup
2549 unless ref($self->usergroup) eq 'ARRAY';
2550 #when provisioning records, export callback runs in svc_Common.pm before
2551 #radius_usergroup records can be inserted...
2552 @{$self->usergroup};
2554 map { $_->groupname }
2555 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } );
2559 =item clone_suspended
2561 Constructor used by FS::part_export::_export_suspend fallback. Document
2566 sub clone_suspended {
2568 my %hash = $self->hash;
2569 $hash{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) );
2570 new FS::svc_acct \%hash;
2573 =item clone_kludge_unsuspend
2575 Constructor used by FS::part_export::_export_unsuspend fallback. Document
2580 sub clone_kludge_unsuspend {
2582 my %hash = $self->hash;
2583 $hash{_password} = '';
2584 new FS::svc_acct \%hash;
2587 =item check_password
2589 Checks the supplied password against the (possibly encrypted) password in the
2590 database. Returns true for a successful authentication, false for no match.
2592 Currently supported encryptions are: classic DES crypt() and MD5
2596 sub check_password {
2597 my($self, $check_password) = @_;
2599 #remove old-style SUSPENDED kludge, they should be allowed to login to
2600 #self-service and pay up
2601 ( my $password = $self->_password ) =~ s/^\*SUSPENDED\* //;
2603 if ( $self->_password_encoding eq 'ldap' ) {
2605 my $auth = from_rfc2307 Authen::Passphrase $self->_password;
2606 return $auth->match($check_password);
2608 } elsif ( $self->_password_encoding eq 'crypt' ) {
2610 my $auth = from_crypt Authen::Passphrase $self->_password;
2611 return $auth->match($check_password);
2613 } elsif ( $self->_password_encoding eq 'plain' ) {
2615 return $check_password eq $password;
2619 #XXX this could be replaced with Authen::Passphrase stuff
2621 if ( $password =~ /^(\*|!!?)$/ ) { #no self-service login
2623 } elsif ( length($password) < 13 ) { #plaintext
2624 $check_password eq $password;
2625 } elsif ( length($password) == 13 ) { #traditional DES crypt
2626 crypt($check_password, $password) eq $password;
2627 } elsif ( $password =~ /^\$1\$/ ) { #MD5 crypt
2628 unix_md5_crypt($check_password, $password) eq $password;
2629 } elsif ( $password =~ /^\$2a?\$/ ) { #Blowfish
2630 warn "Can't check password: Blowfish encryption not yet supported, ".
2631 "svcnum ". $self->svcnum. "\n";
2634 warn "Can't check password: Unrecognized encryption for svcnum ".
2635 $self->svcnum. "\n";
2643 =item crypt_password [ DEFAULT_ENCRYPTION_TYPE ]
2645 Returns an encrypted password, either by passing through an encrypted password
2646 in the database or by encrypting a plaintext password from the database.
2648 The optional DEFAULT_ENCRYPTION_TYPE parameter can be set to I<crypt> (classic
2649 UNIX DES crypt), I<md5> (md5 crypt supported by most modern Linux and BSD
2650 distrubtions), or (eventually) I<blowfish> (blowfish hashing supported by
2651 OpenBSD, SuSE, other Linux distibutions with pam_unix2, etc.). The default
2652 encryption type is only used if the password is not already encrypted in the
2657 sub crypt_password {
2660 if ( $self->_password_encoding eq 'ldap' ) {
2662 if ( $self->_password =~ /^\{(PLAIN|CLEARTEXT)\}(.+)$/ ) {
2665 #XXX this could be replaced with Authen::Passphrase stuff
2667 my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2668 if ( $encryption eq 'crypt' ) {
2671 $saltset[int(rand(64))].$saltset[int(rand(64))]
2673 } elsif ( $encryption eq 'md5' ) {
2674 unix_md5_crypt( $self->_password );
2675 } elsif ( $encryption eq 'blowfish' ) {
2676 croak "unknown encryption method $encryption";
2678 croak "unknown encryption method $encryption";
2681 } elsif ( $self->_password =~ /^\{CRYPT\}(.+)$/ ) {
2685 } elsif ( $self->_password_encoding eq 'crypt' ) {
2687 return $self->_password;
2689 } elsif ( $self->_password_encoding eq 'plain' ) {
2691 #XXX this could be replaced with Authen::Passphrase stuff
2693 my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2694 if ( $encryption eq 'crypt' ) {
2697 $saltset[int(rand(64))].$saltset[int(rand(64))]
2699 } elsif ( $encryption eq 'md5' ) {
2700 unix_md5_crypt( $self->_password );
2701 } elsif ( $encryption eq 'blowfish' ) {
2702 croak "unknown encryption method $encryption";
2704 croak "unknown encryption method $encryption";
2709 if ( length($self->_password) == 13
2710 || $self->_password =~ /^\$(1|2a?)\$/
2711 || $self->_password =~ /^(\*|NP|\*LK\*|!!?)$/
2717 #XXX this could be replaced with Authen::Passphrase stuff
2719 my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2720 if ( $encryption eq 'crypt' ) {
2723 $saltset[int(rand(64))].$saltset[int(rand(64))]
2725 } elsif ( $encryption eq 'md5' ) {
2726 unix_md5_crypt( $self->_password );
2727 } elsif ( $encryption eq 'blowfish' ) {
2728 croak "unknown encryption method $encryption";
2730 croak "unknown encryption method $encryption";
2739 =item ldap_password [ DEFAULT_ENCRYPTION_TYPE ]
2741 Returns an encrypted password in "LDAP" format, with a curly-bracked prefix
2742 describing the format, for example, "{PLAIN}himom", "{CRYPT}94pAVyK/4oIBk" or
2743 "{MD5}5426824942db4253f87a1009fd5d2d4".
2745 The optional DEFAULT_ENCRYPTION_TYPE is not yet used, but the idea is for it
2746 to work the same as the B</crypt_password> method.
2752 #eventually should check a "password-encoding" field
2754 if ( $self->_password_encoding eq 'ldap' ) {
2756 return $self->_password;
2758 } elsif ( $self->_password_encoding eq 'crypt' ) {
2760 if ( length($self->_password) == 13 ) { #crypt
2761 return '{CRYPT}'. $self->_password;
2762 } elsif ( $self->_password =~ /^\$1\$(.*)$/ && length($1) == 31 ) { #passwdMD5
2764 #} elsif ( $self->_password =~ /^\$2a?\$(.*)$/ ) { #Blowfish
2765 # die "Blowfish encryption not supported in this context, svcnum ".
2766 # $self->svcnum. "\n";
2768 warn "encryption method not (yet?) supported in LDAP context";
2769 return '{CRYPT}*'; #unsupported, should not auth
2772 } elsif ( $self->_password_encoding eq 'plain' ) {
2774 return '{PLAIN}'. $self->_password;
2776 #return '{CLEARTEXT}'. $self->_password; #?
2780 if ( length($self->_password) == 13 ) { #crypt
2781 return '{CRYPT}'. $self->_password;
2782 } elsif ( $self->_password =~ /^\$1\$(.*)$/ && length($1) == 31 ) { #passwdMD5
2784 } elsif ( $self->_password =~ /^\$2a?\$(.*)$/ ) { #Blowfish
2785 warn "Blowfish encryption not supported in this context, svcnum ".
2786 $self->svcnum. "\n";
2789 #are these two necessary anymore?
2790 } elsif ( $self->_password =~ /^(\w{48})$/ ) { #LDAP SSHA
2791 return '{SSHA}'. $1;
2792 } elsif ( $self->_password =~ /^(\w{64})$/ ) { #LDAP NS-MTA-MD5
2793 return '{NS-MTA-MD5}'. $1;
2796 return '{PLAIN}'. $self->_password;
2798 #return '{CLEARTEXT}'. $self->_password; #?
2800 #XXX this could be replaced with Authen::Passphrase stuff if it gets used
2801 #my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2802 #if ( $encryption eq 'crypt' ) {
2803 # return '{CRYPT}'. crypt(
2805 # $saltset[int(rand(64))].$saltset[int(rand(64))]
2807 #} elsif ( $encryption eq 'md5' ) {
2808 # unix_md5_crypt( $self->_password );
2809 #} elsif ( $encryption eq 'blowfish' ) {
2810 # croak "unknown encryption method $encryption";
2812 # croak "unknown encryption method $encryption";
2820 =item domain_slash_username
2822 Returns $domain/$username/
2826 sub domain_slash_username {
2828 $self->domain. '/'. $self->username. '/';
2831 =item virtual_maildir
2833 Returns $domain/maildirs/$username/
2837 sub virtual_maildir {
2839 $self->domain. '/maildirs/'. $self->username. '/';
2844 =head1 CLASS METHODS
2848 =item search HASHREF
2850 Class method which returns a qsearch hash expression to search for parameters
2851 specified in HASHREF. Valid parameters are
2865 Arrayref of pkgparts
2871 Arrayref of additional WHERE clauses, will be ANDed together.
2882 my ($class, $params) = @_;
2887 if ( $params->{'domain'} ) {
2888 my $svc_domain = qsearchs('svc_domain', { 'domain'=>$params->{'domain'} } );
2889 #preserve previous behavior & bubble up an error if $svc_domain not found?
2890 push @where, 'domsvc = '. $svc_domain->svcnum if $svc_domain;
2894 if ( $params->{'domsvc'} =~ /^(\d+)$/ ) {
2895 push @where, "domsvc = $1";
2899 push @where, 'pkgnum IS NULL' if $params->{'unlinked'};
2902 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
2903 push @where, "agentnum = $1";
2907 if ( $params->{'custnum'} =~ /^(\d+)$/ and $1 ) {
2908 push @where, "custnum = $1";
2912 if ( $params->{'pkgpart'} && scalar(@{ $params->{'pkgpart'} }) ) {
2913 #XXX untaint or sql quote
2915 'cust_pkg.pkgpart IN ('. join(',', @{ $params->{'pkgpart'} } ). ')';
2919 if ( $params->{'popnum'} =~ /^(\d+)$/ ) {
2920 push @where, "popnum = $1";
2924 if ( $params->{'svcpart'} =~ /^(\d+)$/ ) {
2925 push @where, "svcpart = $1";
2929 # here is the agent virtualization
2930 #if ($params->{CurrentUser}) {
2932 # qsearchs('access_user', { username => $params->{CurrentUser} });
2934 # if ($access_user) {
2935 # push @where, $access_user->agentnums_sql('table'=>'cust_main');
2937 # push @where, "1=0";
2940 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql(
2941 'table' => 'cust_main',
2942 'null_right' => 'View/link unlinked services',
2946 push @where, @{ $params->{'where'} } if $params->{'where'};
2948 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
2950 my $addl_from = ' LEFT JOIN cust_svc USING ( svcnum ) '.
2951 ' LEFT JOIN part_svc USING ( svcpart ) '.
2952 ' LEFT JOIN cust_pkg USING ( pkgnum ) '.
2953 ' LEFT JOIN cust_main USING ( custnum ) ';
2955 my $count_query = "SELECT COUNT(*) FROM svc_acct $addl_from $extra_sql";
2956 #if ( keys %svc_acct ) {
2957 # $count_query .= ' WHERE '.
2958 # join(' AND ', map "$_ = ". dbh->quote($svc_acct{$_}),
2964 'table' => 'svc_acct',
2965 'hashref' => {}, # \%svc_acct,
2966 'select' => join(', ',
2969 'cust_main.custnum',
2970 FS::UI::Web::cust_sql_fields($params->{'cust_fields'}),
2972 'addl_from' => $addl_from,
2973 'extra_sql' => $extra_sql,
2974 'order_by' => $params->{'order_by'},
2975 'count_query' => $count_query,
2988 This is the FS::svc_acct job-queue-able version. It still uses
2989 FS::Misc::send_email under-the-hood.
2996 eval "use FS::Misc qw(send_email)";
2999 $opt{mimetype} ||= 'text/plain';
3000 $opt{mimetype} .= '; charset="iso-8859-1"' unless $opt{mimetype} =~ /charset/;
3002 my $error = send_email(
3003 'from' => $opt{from},
3005 'subject' => $opt{subject},
3006 'content-type' => $opt{mimetype},
3007 'body' => [ map "$_\n", split("\n", $opt{body}) ],
3009 die $error if $error;
3012 =item check_and_rebuild_fuzzyfiles
3016 sub check_and_rebuild_fuzzyfiles {
3017 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
3018 -e "$dir/svc_acct.username"
3019 or &rebuild_fuzzyfiles;
3022 =item rebuild_fuzzyfiles
3026 sub rebuild_fuzzyfiles {
3028 use Fcntl qw(:flock);
3030 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
3034 open(USERNAMELOCK,">>$dir/svc_acct.username")
3035 or die "can't open $dir/svc_acct.username: $!";
3036 flock(USERNAMELOCK,LOCK_EX)
3037 or die "can't lock $dir/svc_acct.username: $!";
3039 my @all_username = map $_->getfield('username'), qsearch('svc_acct', {});
3041 open (USERNAMECACHE,">$dir/svc_acct.username.tmp")
3042 or die "can't open $dir/svc_acct.username.tmp: $!";
3043 print USERNAMECACHE join("\n", @all_username), "\n";
3044 close USERNAMECACHE or die "can't close $dir/svc_acct.username.tmp: $!";
3046 rename "$dir/svc_acct.username.tmp", "$dir/svc_acct.username";
3056 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
3057 open(USERNAMECACHE,"<$dir/svc_acct.username")
3058 or die "can't open $dir/svc_acct.username: $!";
3059 my @array = map { chomp; $_; } <USERNAMECACHE>;
3060 close USERNAMECACHE;
3064 =item append_fuzzyfiles USERNAME
3068 sub append_fuzzyfiles {
3069 my $username = shift;
3071 &check_and_rebuild_fuzzyfiles;
3073 use Fcntl qw(:flock);
3075 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
3077 open(USERNAME,">>$dir/svc_acct.username")
3078 or die "can't open $dir/svc_acct.username: $!";
3079 flock(USERNAME,LOCK_EX)
3080 or die "can't lock $dir/svc_acct.username: $!";
3082 print USERNAME "$username\n";
3084 flock(USERNAME,LOCK_UN)
3085 or die "can't unlock $dir/svc_acct.username: $!";
3093 =item radius_usergroup_selector GROUPS_ARRAYREF [ SELECTNAME ]
3097 sub radius_usergroup_selector {
3098 my $sel_groups = shift;
3099 my %sel_groups = map { $_=>1 } @$sel_groups;
3101 my $selectname = shift || 'radius_usergroup';
3104 my $sth = $dbh->prepare(
3105 'SELECT DISTINCT(groupname) FROM radius_usergroup ORDER BY groupname'
3106 ) or die $dbh->errstr;
3107 $sth->execute() or die $sth->errstr;
3108 my @all_groups = map { $_->[0] } @{$sth->fetchall_arrayref};
3112 function ${selectname}_doadd(object) {
3113 var myvalue = object.${selectname}_add.value;
3114 var optionName = new Option(myvalue,myvalue,false,true);
3115 var length = object.$selectname.length;
3116 object.$selectname.options[length] = optionName;
3117 object.${selectname}_add.value = "";
3120 <SELECT MULTIPLE NAME="$selectname">
3123 foreach my $group ( @all_groups ) {
3124 $html .= qq(<OPTION VALUE="$group");
3125 if ( $sel_groups{$group} ) {
3126 $html .= ' SELECTED';
3127 $sel_groups{$group} = 0;
3129 $html .= ">$group</OPTION>\n";
3131 foreach my $group ( grep { $sel_groups{$_} } keys %sel_groups ) {
3132 $html .= qq(<OPTION VALUE="$group" SELECTED>$group</OPTION>\n);
3134 $html .= '</SELECT>';
3136 $html .= qq!<BR><INPUT TYPE="text" NAME="${selectname}_add">!.
3137 qq!<INPUT TYPE="button" VALUE="Add new group" onClick="${selectname}_doadd(this.form)">!;
3142 =item reached_threshold
3144 Performs some activities when svc_acct thresholds (such as number of seconds
3145 remaining) are reached.
3149 sub reached_threshold {
3152 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $opt{'svcnum'} } );
3153 die "Cannot find svc_acct with svcnum " . $opt{'svcnum'} unless $svc_acct;
3155 if ( $opt{'op'} eq '+' ){
3156 $svc_acct->setfield( $opt{'column'}.'_threshold',
3157 int($svc_acct->getfield($opt{'column'})
3158 * ( $conf->exists('svc_acct-usage_threshold')
3159 ? $conf->config('svc_acct-usage_threshold')/100
3164 my $error = $svc_acct->replace;
3165 die $error if $error;
3166 }elsif ( $opt{'op'} eq '-' ){
3168 my $threshold = $svc_acct->getfield( $opt{'column'}.'_threshold' );
3169 return '' if ($threshold eq '' );
3171 $svc_acct->setfield( $opt{'column'}.'_threshold', 0 );
3172 my $error = $svc_acct->replace;
3173 die $error if $error; # email next time, i guess
3175 if ( $warning_template ) {
3176 eval "use FS::Misc qw(send_email)";
3179 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
3180 my $cust_main = $cust_pkg->cust_main;
3182 my $to = join(', ', grep { $_ !~ /^(POST|FAX)$/ }
3183 $cust_main->invoicing_list,
3184 ($opt{'to'} ? $opt{'to'} : ())
3187 my $mimetype = $warning_mimetype;
3188 $mimetype .= '; charset="iso-8859-1"' unless $opt{mimetype} =~ /charset/;
3190 my $body = $warning_template->fill_in( HASH => {
3191 'custnum' => $cust_main->custnum,
3192 'username' => $svc_acct->username,
3193 'password' => $svc_acct->_password,
3194 'first' => $cust_main->first,
3195 'last' => $cust_main->getfield('last'),
3196 'pkg' => $cust_pkg->part_pkg->pkg,
3197 'column' => $opt{'column'},
3198 'amount' => $opt{'column'} =~/bytes/
3199 ? FS::UI::bytecount::display_bytecount($svc_acct->getfield($opt{'column'}))
3200 : $svc_acct->getfield($opt{'column'}),
3201 'threshold' => $opt{'column'} =~/bytes/
3202 ? FS::UI::bytecount::display_bytecount($threshold)
3207 my $error = send_email(
3208 'from' => $warning_from,
3210 'subject' => $warning_subject,
3211 'content-type' => $mimetype,
3212 'body' => [ map "$_\n", split("\n", $body) ],
3214 die $error if $error;
3217 die "unknown op: " . $opt{'op'};
3225 The $recref stuff in sub check should be cleaned up.
3227 The suspend, unsuspend and cancel methods update the database, but not the
3228 current object. This is probably a bug as it's unexpected and
3231 radius_usergroup_selector? putting web ui components in here? they should
3232 probably live somewhere else...
3234 insertion of RADIUS group stuff in insert could be done with child_objects now
3235 (would probably clean up export of them too)
3237 _op_usage and set_usage bypass the history... maybe they shouldn't
3241 L<FS::svc_Common>, edit/part_svc.cgi from an installed web interface,
3242 export.html from the base documentation, L<FS::Record>, L<FS::Conf>,
3243 L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, L<FS::queue>,
3244 L<freeside-queued>), L<FS::svc_acct_pop>,
3245 schema.html from the base documentation.