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 $password_noampersand $password_noexclamation
13 $warning_template $warning_from $warning_subject $warning_mimetype
16 $radius_password $radius_ip
19 use Scalar::Util qw( blessed );
24 use Crypt::PasswdMD5 1.2;
25 use Digest::SHA1 'sha1_base64';
26 use Digest::MD5 'md5_base64';
29 use Authen::Passphrase;
30 use FS::UID qw( datasrc driver_name );
32 use FS::Record qw( qsearch qsearchs fields dbh dbdef );
33 use FS::Msgcat qw(gettext);
34 use FS::UI::bytecount;
39 use FS::cust_main_invoice;
44 use FS::radius_usergroup;
53 $me = '[FS::svc_acct]';
55 #ask FS::UID to run this stuff for us later
56 FS::UID->install_callback( sub {
58 $dir_prefix = $conf->config('home');
59 @shells = $conf->config('shells');
60 $usernamemin = $conf->config('usernamemin') || 2;
61 $usernamemax = $conf->config('usernamemax');
62 $passwordmin = $conf->config('passwordmin'); # || 6;
64 $passwordmin = ( defined($passwordmin) && $passwordmin =~ /\d+/ )
67 $passwordmax = $conf->config('passwordmax') || 8;
68 $username_letter = $conf->exists('username-letter');
69 $username_letterfirst = $conf->exists('username-letterfirst');
70 $username_noperiod = $conf->exists('username-noperiod');
71 $username_nounderscore = $conf->exists('username-nounderscore');
72 $username_nodash = $conf->exists('username-nodash');
73 $username_uppercase = $conf->exists('username-uppercase');
74 $username_ampersand = $conf->exists('username-ampersand');
75 $username_percent = $conf->exists('username-percent');
76 $username_colon = $conf->exists('username-colon');
77 $password_noampersand = $conf->exists('password-noexclamation');
78 $password_noexclamation = $conf->exists('password-noexclamation');
79 $dirhash = $conf->config('dirhash') || 0;
80 if ( $conf->exists('warning_email') ) {
81 $warning_template = new Text::Template (
83 SOURCE => [ map "$_\n", $conf->config('warning_email') ]
84 ) or warn "can't create warning email template: $Text::Template::ERROR";
85 $warning_from = $conf->config('warning_email-from'); # || 'your-isp-is-dum'
86 $warning_subject = $conf->config('warning_email-subject') || 'Warning';
87 $warning_mimetype = $conf->config('warning_email-mimetype') || 'text/plain';
88 $warning_cc = $conf->config('warning_email-cc');
90 $warning_template = '';
92 $warning_subject = '';
93 $warning_mimetype = '';
96 $smtpmachine = $conf->config('smtpmachine');
97 $radius_password = $conf->config('radius-password') || 'Password';
98 $radius_ip = $conf->config('radius-ip') || 'Framed-IP-Address';
99 @pw_set = ( 'A'..'Z' ) if $conf->exists('password-generated-allcaps');
103 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
104 @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '!', '.', ',' );
108 my ( $hashref, $cache ) = @_;
109 if ( $hashref->{'svc_acct_svcnum'} ) {
110 $self->{'_domsvc'} = FS::svc_domain->new( {
111 'svcnum' => $hashref->{'domsvc'},
112 'domain' => $hashref->{'svc_acct_domain'},
113 'catchall' => $hashref->{'svc_acct_catchall'},
120 FS::svc_acct - Object methods for svc_acct records
126 $record = new FS::svc_acct \%hash;
127 $record = new FS::svc_acct { 'column' => 'value' };
129 $error = $record->insert;
131 $error = $new_record->replace($old_record);
133 $error = $record->delete;
135 $error = $record->check;
137 $error = $record->suspend;
139 $error = $record->unsuspend;
141 $error = $record->cancel;
143 %hash = $record->radius;
145 %hash = $record->radius_reply;
147 %hash = $record->radius_check;
149 $domain = $record->domain;
151 $svc_domain = $record->svc_domain;
153 $email = $record->email;
155 $seconds_since = $record->seconds_since($timestamp);
159 An FS::svc_acct object represents an account. FS::svc_acct inherits from
160 FS::svc_Common. The following fields are currently supported:
166 Primary key (assigned automatcially for new accounts)
174 =item _password_encoding
176 plain, crypt, ldap (or empty for autodetection)
184 Point of presence (see L<FS::svc_acct_pop>)
196 set automatically if blank (and uid is not)
216 svcnum from svc_domain
220 Optional svcnum from svc_pbx
222 =item radius_I<Radius_Attribute>
224 I<Radius-Attribute> (reply)
226 =item rc_I<Radius_Attribute>
228 I<Radius-Attribute> (check)
238 Creates a new account. To add the account to the database, see L<"insert">.
245 'longname_plural' => 'Access accounts and mailboxes',
246 'sorts' => [ 'username', 'uid', 'seconds', 'last_login' ],
247 'display_weight' => 10,
248 'cancel_weight' => 50,
250 'dir' => 'Home directory',
253 def_info => 'set to fixed and blank for no UIDs',
256 'slipip' => 'IP address',
257 # 'popnum' => qq!<A HREF="$p/browse/svc_acct_pop.cgi/">POP number</A>!,
259 label => 'Access number',
261 select_table => 'svc_acct_pop',
262 select_key => 'popnum',
263 select_label => 'city',
269 disable_default => 1,
273 'password_selfchange' => { label => 'Password modification',
276 'password_recover' => { label => 'Password recovery',
280 label => 'Quota', #Mail storage limit
282 disable_inventory => 1,
286 label => 'File storage limit',
288 disable_inventory => 1,
292 label => 'Number of files limit',
294 disable_inventory => 1,
298 label => 'File size limit',
300 disable_inventory => 1,
303 '_password' => 'Password',
306 def_info => 'when blank, defaults to UID',
311 def_info => 'set to blank for no shell tracking',
313 #select_list => [ $conf->config('shells') ],
314 select_list => [ $conf ? $conf->config('shells') : () ],
315 disable_inventory => 1,
318 'finger' => 'Real name', # (GECOS)',
322 select_table => 'svc_domain',
323 select_key => 'svcnum',
324 select_label => 'domain',
325 disable_inventory => 1,
327 'pbxsvc' => { label => 'PBX',
328 type => 'select-svc_pbx.html',
329 disable_inventory => 1,
330 disable_select => 1, #UI wonky, pry works otherwise
333 label => 'RADIUS groups',
334 type => 'radius_usergroup_selector',
335 disable_inventory => 1,
338 'seconds' => { label => 'Seconds',
339 label_sort => 'with Time Remaining',
341 disable_inventory => 1,
343 disable_part_svc_column => 1,
345 'upbytes' => { label => 'Upload',
347 disable_inventory => 1,
349 'format' => \&FS::UI::bytecount::display_bytecount,
350 'parse' => \&FS::UI::bytecount::parse_bytecount,
351 disable_part_svc_column => 1,
353 'downbytes' => { label => 'Download',
355 disable_inventory => 1,
357 'format' => \&FS::UI::bytecount::display_bytecount,
358 'parse' => \&FS::UI::bytecount::parse_bytecount,
359 disable_part_svc_column => 1,
361 'totalbytes'=> { label => 'Total up and download',
363 disable_inventory => 1,
365 'format' => \&FS::UI::bytecount::display_bytecount,
366 'parse' => \&FS::UI::bytecount::parse_bytecount,
367 disable_part_svc_column => 1,
369 'seconds_threshold' => { label => 'Seconds threshold',
371 disable_inventory => 1,
373 disable_part_svc_column => 1,
375 'upbytes_threshold' => { label => 'Upload threshold',
377 disable_inventory => 1,
379 'format' => \&FS::UI::bytecount::display_bytecount,
380 'parse' => \&FS::UI::bytecount::parse_bytecount,
381 disable_part_svc_column => 1,
383 'downbytes_threshold' => { label => 'Download threshold',
385 disable_inventory => 1,
387 'format' => \&FS::UI::bytecount::display_bytecount,
388 'parse' => \&FS::UI::bytecount::parse_bytecount,
389 disable_part_svc_column => 1,
391 'totalbytes_threshold'=> { label => 'Total up and download threshold',
393 disable_inventory => 1,
395 'format' => \&FS::UI::bytecount::display_bytecount,
396 'parse' => \&FS::UI::bytecount::parse_bytecount,
397 disable_part_svc_column => 1,
400 label => 'Last login',
404 label => 'Last logout',
409 label => 'Communigate aliases',
411 disable_inventory => 1,
416 label => 'Communigate account type',
418 select_list => [qw( MultiMailbox TextMailbox MailDirMailbox AGrade BGrade CGrade )],
419 disable_inventory => 1,
422 'cgp_accessmodes' => {
423 label => 'Communigate enabled services',
424 type => 'communigate_pro-accessmodes',
425 disable_inventory => 1,
428 'cgp_rulesallowed' => {
429 label => 'Allowed mail rules',
431 select_list => [ '', 'No', 'Filter Only', 'All But Exec', 'Any' ],
432 disable_inventory => 1,
435 'cgp_rpopallowed' => { label => 'RPOP modifications',
438 'cgp_mailtoall' => { label => 'Accepts mail to "all"',
441 'cgp_addmailtrailer' => { label => 'Add trailer to sent mail',
444 'cgp_archiveafter' => {
445 label => 'Archive messages after',
448 -2 => 'default(730 days)',
455 1209600 => '2 weeks',
456 2592000 => '30 days',
457 7776000 => '90 days',
458 15552000 => '180 days',
459 31536000 => '365 days',
460 63072000 => '730 days',
462 disable_inventory => 1,
468 'cgp_deletemode' => {
469 label => 'Communigate message delete method',
471 select_list => [ 'Move To Trash', 'Immediately', 'Mark' ],
472 disable_inventory => 1,
475 'cgp_emptytrash' => {
476 label => 'Communigate on logout remove trash',
478 select_list => __PACKAGE__->cgp_emptytrash_values,
479 disable_inventory => 1,
483 label => 'Communigate language',
485 select_list => [ '', qw( English Arabic Chinese Dutch French German Hebrew Italian Japanese Portuguese Russian Slovak Spanish Thai ) ],
486 disable_inventory => 1,
490 label => 'Communigate time zone',
492 select_list => __PACKAGE__->cgp_timezone_values,
493 disable_inventory => 1,
497 label => 'Communigate layout',
499 select_list => [ '', '***', 'GoldFleece', 'Skin2' ],
500 disable_inventory => 1,
503 'cgp_prontoskinname' => {
504 label => 'Communigate Pronto style',
506 select_list => [ '', 'Pronto', 'Pronto-darkflame', 'Pronto-steel', 'Pronto-twilight', ],
507 disable_inventory => 1,
510 'cgp_sendmdnmode' => {
511 label => 'Communigate send read receipts',
513 select_list => [ '', 'Never', 'Manually', 'Automatically' ],
514 disable_inventory => 1,
525 sub table { 'svc_acct'; }
527 sub table_dupcheck_fields { ( 'username', 'domsvc' ); }
531 #false laziness with edit/svc_acct.cgi
533 my( $self, $groups ) = @_;
534 if ( ref($groups) eq 'ARRAY' ) {
536 } elsif ( length($groups) ) {
537 [ split(/\s*,\s*/, $groups) ];
546 shift->_lastlog('in', @_);
550 shift->_lastlog('out', @_);
554 my( $self, $op, $time ) = @_;
556 if ( defined($time) ) {
557 warn "$me last_log$op called on svcnum ". $self->svcnum.
558 ' ('. $self->email. "): $time\n"
563 my $sql = "UPDATE svc_acct SET last_log$op = ? WHERE svcnum = ?";
567 my $sth = $dbh->prepare( $sql )
568 or die "Error preparing $sql: ". $dbh->errstr;
569 my $rv = $sth->execute($time, $self->svcnum);
570 die "Error executing $sql: ". $sth->errstr
572 die "Can't update last_log$op for svcnum". $self->svcnum
575 $self->{'Hash'}->{"last_log$op"} = $time;
577 $self->getfield("last_log$op");
581 =item search_sql STRING
583 Class method which returns an SQL fragment to search for the given string.
588 my( $class, $string ) = @_;
589 if ( $string =~ /^([^@]+)@([^@]+)$/ ) {
590 my( $username, $domain ) = ( $1, $2 );
591 my $q_username = dbh->quote($username);
592 my @svc_domain = qsearch('svc_domain', { 'domain' => $domain } );
594 "svc_acct.username = $q_username AND ( ".
595 join( ' OR ', map { "svc_acct.domsvc = ". $_->svcnum; } @svc_domain ).
600 } elsif ( $string =~ /^(\d{1,3}\.){3}\d{1,3}$/ ) {
602 $class->search_sql_field('slipip', $string ).
604 $class->search_sql_field('username', $string ).
607 $class->search_sql_field('username', $string);
611 =item label [ END_TIMESTAMP [ START_TIMESTAMP ] ]
613 Returns the "username@domain" string for this account.
615 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
625 =item label_long [ END_TIMESTAMP [ START_TIMESTAMP ] ]
627 Returns a longer string label for this acccount ("Real Name <username@domain>"
628 if available, or "username@domain").
630 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
637 my $label = $self->label(@_);
638 my $finger = $self->finger;
639 return $label unless $finger =~ /\S/;
640 my $maxlen = 40 - length($label) - length($self->cust_svc->part_svc->svc);
641 $finger = substr($finger, 0, $maxlen-3).'...' if length($finger) > $maxlen;
645 =item insert [ , OPTION => VALUE ... ]
647 Adds this account to the database. If there is an error, returns the error,
648 otherwise returns false.
650 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
651 defined. An FS::cust_svc record will be created and inserted.
653 The additional field I<usergroup> can optionally be defined; if so it should
654 contain an arrayref of group names. See L<FS::radius_usergroup>.
656 The additional field I<child_objects> can optionally be defined; if so it
657 should contain an arrayref of FS::tablename objects. They will have their
658 svcnum fields set and will be inserted after this record, but before any
659 exports are run. Each element of the array can also optionally be a
660 two-element array reference containing the child object and the name of an
661 alternate field to be filled in with the newly-inserted svcnum, for example
662 C<[ $svc_forward, 'srcsvc' ]>
664 Currently available options are: I<depend_jobnum>
666 If I<depend_jobnum> is set (to a scalar jobnum or an array reference of
667 jobnums), all provisioning jobs will have a dependancy on the supplied
668 jobnum(s) (they will not run until the specific job(s) complete(s)).
670 (TODOC: L<FS::queue> and L<freeside-queued>)
672 (TODOC: new exports!)
681 warn "[$me] insert called on $self: ". Dumper($self).
682 "\nwith options: ". Dumper(%options);
685 local $SIG{HUP} = 'IGNORE';
686 local $SIG{INT} = 'IGNORE';
687 local $SIG{QUIT} = 'IGNORE';
688 local $SIG{TERM} = 'IGNORE';
689 local $SIG{TSTP} = 'IGNORE';
690 local $SIG{PIPE} = 'IGNORE';
692 my $oldAutoCommit = $FS::UID::AutoCommit;
693 local $FS::UID::AutoCommit = 0;
697 my $error = $self->SUPER::insert(
698 'jobnums' => \@jobnums,
699 'child_objects' => $self->child_objects,
703 $dbh->rollback if $oldAutoCommit;
707 if ( $self->usergroup ) {
708 foreach my $groupname ( @{$self->usergroup} ) {
709 my $radius_usergroup = new FS::radius_usergroup ( {
710 svcnum => $self->svcnum,
711 groupname => $groupname,
713 my $error = $radius_usergroup->insert;
715 $dbh->rollback if $oldAutoCommit;
721 unless ( $skip_fuzzyfiles ) {
722 $error = $self->queue_fuzzyfiles_update;
724 $dbh->rollback if $oldAutoCommit;
725 return "updating fuzzy search cache: $error";
729 my $cust_pkg = $self->cust_svc->cust_pkg;
732 my $cust_main = $cust_pkg->cust_main;
733 my $agentnum = $cust_main->agentnum;
735 if ( $conf->exists('emailinvoiceautoalways')
736 || $conf->exists('emailinvoiceauto')
737 && ! $cust_main->invoicing_list_emailonly
739 my @invoicing_list = $cust_main->invoicing_list;
740 push @invoicing_list, $self->email;
741 $cust_main->invoicing_list(\@invoicing_list);
746 my $msgnum = $conf->config('welcome_msgnum', $agentnum);
748 my $msg_template = qsearchs('msg_template', { msgnum => $msgnum });
749 $error = $msg_template->send('cust_main' => $cust_main);
752 my ($to,$welcome_template,$welcome_from,$welcome_subject,$welcome_subject_template,$welcome_mimetype)
753 = ('','','','','','');
755 if ( $conf->exists('welcome_email', $agentnum) ) {
756 $welcome_template = new Text::Template (
758 SOURCE => [ map "$_\n", $conf->config('welcome_email', $agentnum) ]
759 ) or warn "can't create welcome email template: $Text::Template::ERROR";
760 $welcome_from = $conf->config('welcome_email-from', $agentnum);
761 # || 'your-isp-is-dum'
762 $welcome_subject = $conf->config('welcome_email-subject', $agentnum)
764 $welcome_subject_template = new Text::Template (
766 SOURCE => $welcome_subject,
767 ) or warn "can't create welcome email subject template: $Text::Template::ERROR";
768 $welcome_mimetype = $conf->config('welcome_email-mimetype', $agentnum)
771 if ( $welcome_template ) {
772 my $to = join(', ', grep { $_ !~ /^(POST|FAX)$/ } $cust_main->invoicing_list );
776 'custnum' => $self->custnum,
777 'username' => $self->username,
778 'password' => $self->_password,
779 'first' => $cust_main->first,
780 'last' => $cust_main->getfield('last'),
781 'pkg' => $cust_pkg->part_pkg->pkg,
783 my $wqueue = new FS::queue {
784 'svcnum' => $self->svcnum,
785 'job' => 'FS::svc_acct::send_email'
787 my $error = $wqueue->insert(
789 'from' => $welcome_from,
790 'subject' => $welcome_subject_template->fill_in( HASH => \%hash, ),
791 'mimetype' => $welcome_mimetype,
792 'body' => $welcome_template->fill_in( HASH => \%hash, ),
795 $dbh->rollback if $oldAutoCommit;
796 return "error queuing welcome email: $error";
799 if ( $options{'depend_jobnum'} ) {
800 warn "$me depend_jobnum found; adding to welcome email dependancies"
802 if ( ref($options{'depend_jobnum'}) ) {
803 warn "$me adding jobs ". join(', ', @{$options{'depend_jobnum'}} ).
804 "to welcome email dependancies"
806 push @jobnums, @{ $options{'depend_jobnum'} };
808 warn "$me adding job $options{'depend_jobnum'} ".
809 "to welcome email dependancies"
811 push @jobnums, $options{'depend_jobnum'};
815 foreach my $jobnum ( @jobnums ) {
816 my $error = $wqueue->depend_insert($jobnum);
818 $dbh->rollback if $oldAutoCommit;
819 return "error queuing welcome email job dependancy: $error";
825 } # if $welcome_template
829 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
833 # set usage fields and thresholds if unset but set in a package def
834 # AND the package already has a last bill date (otherwise they get double added)
835 sub preinsert_hook_first {
838 return '' unless $self->pkgnum;
840 my $cust_pkg = qsearchs( 'cust_pkg', { 'pkgnum' => $self->pkgnum } );
841 return '' unless $cust_pkg && $cust_pkg->last_bill;
843 my $part_pkg = $cust_pkg->part_pkg;
844 return '' unless $part_pkg && $part_pkg->can('usage_valuehash');
846 my %values = $part_pkg->usage_valuehash;
847 my $multiplier = $conf->exists('svc_acct-usage_threshold')
848 ? 1 - $conf->config('svc_acct-usage_threshold')/100
849 : 0.20; #doesn't matter
851 foreach ( keys %values ) {
852 next if $self->getfield($_);
853 $self->setfield( $_, $values{$_} );
854 $self->setfield( $_. '_threshold', int( $values{$_} * $multiplier ) )
855 if $conf->exists('svc_acct-usage_threshold');
863 Deletes this account from the database. If there is an error, returns the
864 error, otherwise returns false.
866 The corresponding FS::cust_svc record will be deleted as well.
868 (TODOC: new exports!)
875 return "can't delete system account" if $self->_check_system;
877 return "Can't delete an account which is a (svc_forward) source!"
878 if qsearch( 'svc_forward', { 'srcsvc' => $self->svcnum } );
880 return "Can't delete an account which is a (svc_forward) destination!"
881 if qsearch( 'svc_forward', { 'dstsvc' => $self->svcnum } );
883 return "Can't delete an account with (svc_www) web service!"
884 if qsearch( 'svc_www', { 'usersvc' => $self->svcnum } );
886 # what about records in session ? (they should refer to history table)
888 local $SIG{HUP} = 'IGNORE';
889 local $SIG{INT} = 'IGNORE';
890 local $SIG{QUIT} = 'IGNORE';
891 local $SIG{TERM} = 'IGNORE';
892 local $SIG{TSTP} = 'IGNORE';
893 local $SIG{PIPE} = 'IGNORE';
895 my $oldAutoCommit = $FS::UID::AutoCommit;
896 local $FS::UID::AutoCommit = 0;
899 foreach my $cust_main_invoice (
900 qsearch( 'cust_main_invoice', { 'dest' => $self->svcnum } )
902 unless ( defined($cust_main_invoice) ) {
903 warn "WARNING: something's wrong with qsearch";
906 my %hash = $cust_main_invoice->hash;
907 $hash{'dest'} = $self->email;
908 my $new = new FS::cust_main_invoice \%hash;
909 my $error = $new->replace($cust_main_invoice);
911 $dbh->rollback if $oldAutoCommit;
916 foreach my $svc_domain (
917 qsearch( 'svc_domain', { 'catchall' => $self->svcnum } )
919 my %hash = new FS::svc_domain->hash;
920 $hash{'catchall'} = '';
921 my $new = new FS::svc_domain \%hash;
922 my $error = $new->replace($svc_domain);
924 $dbh->rollback if $oldAutoCommit;
929 my $error = $self->SUPER::delete;
931 $dbh->rollback if $oldAutoCommit;
935 foreach my $radius_usergroup (
936 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } )
938 my $error = $radius_usergroup->delete;
940 $dbh->rollback if $oldAutoCommit;
945 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
949 =item replace OLD_RECORD
951 Replaces OLD_RECORD with this one in the database. If there is an error,
952 returns the error, otherwise returns false.
954 The additional field I<usergroup> can optionally be defined; if so it should
955 contain an arrayref of group names. See L<FS::radius_usergroup>.
963 my $old = ( blessed($_[0]) && $_[0]->isa('FS::Record') )
967 warn "$me replacing $old with $new\n" if $DEBUG;
971 return "can't modify system account" if $old->_check_system;
974 #no warnings 'numeric'; #alas, a 5.006-ism
977 foreach my $xid (qw( uid gid )) {
979 return "Can't change $xid!"
980 if ! $conf->exists("svc_acct-edit_$xid")
981 && $old->$xid() != $new->$xid()
982 && $new->cust_svc->part_svc->part_svc_column($xid)->columnflag ne 'F'
987 #change homdir when we change username
988 $new->setfield('dir', '') if $old->username ne $new->username;
990 local $SIG{HUP} = 'IGNORE';
991 local $SIG{INT} = 'IGNORE';
992 local $SIG{QUIT} = 'IGNORE';
993 local $SIG{TERM} = 'IGNORE';
994 local $SIG{TSTP} = 'IGNORE';
995 local $SIG{PIPE} = 'IGNORE';
997 my $oldAutoCommit = $FS::UID::AutoCommit;
998 local $FS::UID::AutoCommit = 0;
1001 # redundant, but so $new->usergroup gets set
1002 $error = $new->check;
1003 return $error if $error;
1005 $old->usergroup( [ $old->radius_groups ] );
1007 warn $old->email. " old groups: ". join(' ',@{$old->usergroup}). "\n";
1008 warn $new->email. "new groups: ". join(' ',@{$new->usergroup}). "\n";
1010 if ( $new->usergroup ) {
1011 #(sorta) false laziness with FS::part_export::sqlradius::_export_replace
1012 my @newgroups = @{$new->usergroup};
1013 foreach my $oldgroup ( @{$old->usergroup} ) {
1014 if ( grep { $oldgroup eq $_ } @newgroups ) {
1015 @newgroups = grep { $oldgroup ne $_ } @newgroups;
1018 my $radius_usergroup = qsearchs('radius_usergroup', {
1019 svcnum => $old->svcnum,
1020 groupname => $oldgroup,
1022 my $error = $radius_usergroup->delete;
1024 $dbh->rollback if $oldAutoCommit;
1025 return "error deleting radius_usergroup $oldgroup: $error";
1029 foreach my $newgroup ( @newgroups ) {
1030 my $radius_usergroup = new FS::radius_usergroup ( {
1031 svcnum => $new->svcnum,
1032 groupname => $newgroup,
1034 my $error = $radius_usergroup->insert;
1036 $dbh->rollback if $oldAutoCommit;
1037 return "error adding radius_usergroup $newgroup: $error";
1043 $error = $new->SUPER::replace($old, @_);
1045 $dbh->rollback if $oldAutoCommit;
1046 return $error if $error;
1049 if ( $new->username ne $old->username && ! $skip_fuzzyfiles ) {
1050 $error = $new->queue_fuzzyfiles_update;
1052 $dbh->rollback if $oldAutoCommit;
1053 return "updating fuzzy search cache: $error";
1057 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1061 =item queue_fuzzyfiles_update
1063 Used by insert & replace to update the fuzzy search cache
1067 sub queue_fuzzyfiles_update {
1070 local $SIG{HUP} = 'IGNORE';
1071 local $SIG{INT} = 'IGNORE';
1072 local $SIG{QUIT} = 'IGNORE';
1073 local $SIG{TERM} = 'IGNORE';
1074 local $SIG{TSTP} = 'IGNORE';
1075 local $SIG{PIPE} = 'IGNORE';
1077 my $oldAutoCommit = $FS::UID::AutoCommit;
1078 local $FS::UID::AutoCommit = 0;
1081 my $queue = new FS::queue {
1082 'svcnum' => $self->svcnum,
1083 'job' => 'FS::svc_acct::append_fuzzyfiles'
1085 my $error = $queue->insert($self->username);
1087 $dbh->rollback if $oldAutoCommit;
1088 return "queueing job (transaction rolled back): $error";
1091 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
1099 Suspends this account by calling export-specific suspend hooks. If there is
1100 an error, returns the error, otherwise returns false.
1102 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
1108 return "can't suspend system account" if $self->_check_system;
1109 $self->SUPER::suspend(@_);
1114 Unsuspends this account by by calling export-specific suspend hooks. If there
1115 is an error, returns the error, otherwise returns false.
1117 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
1123 my %hash = $self->hash;
1124 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
1125 $hash{_password} = $1;
1126 my $new = new FS::svc_acct ( \%hash );
1127 my $error = $new->replace($self);
1128 return $error if $error;
1131 $self->SUPER::unsuspend(@_);
1136 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
1138 If the B<auto_unset_catchall> configuration option is set, this method will
1139 automatically remove any references to the canceled service in the catchall
1140 field of svc_domain. This allows packages that contain both a svc_domain and
1141 its catchall svc_acct to be canceled in one step.
1146 # Only one thing to do at this level
1148 foreach my $svc_domain (
1149 qsearch( 'svc_domain', { catchall => $self->svcnum } ) ) {
1150 if($conf->exists('auto_unset_catchall')) {
1151 my %hash = $svc_domain->hash;
1152 $hash{catchall} = '';
1153 my $new = new FS::svc_domain ( \%hash );
1154 my $error = $new->replace($svc_domain);
1155 return $error if $error;
1157 return "cannot unprovision svc_acct #".$self->svcnum.
1158 " while assigned as catchall for svc_domain #".$svc_domain->svcnum;
1162 $self->SUPER::cancel(@_);
1168 Checks all fields to make sure this is a valid service. If there is an error,
1169 returns the error, otherwise returns false. Called by the insert and replace
1172 Sets any fixed values; see L<FS::part_svc>.
1179 my($recref) = $self->hashref;
1181 my $x = $self->setfixed( $self->_fieldhandlers );
1182 return $x unless ref($x);
1185 if ( $part_svc->part_svc_column('usergroup')->columnflag eq "F" ) {
1187 [ split(',', $part_svc->part_svc_column('usergroup')->columnvalue) ] );
1190 my $error = $self->ut_numbern('svcnum')
1191 #|| $self->ut_number('domsvc')
1192 || $self->ut_foreign_key( 'domsvc', 'svc_domain', 'svcnum' )
1193 || $self->ut_foreign_keyn('pbxsvc', 'svc_pbx', 'svcnum' )
1194 || $self->ut_textn('sec_phrase')
1195 || $self->ut_snumbern('seconds')
1196 || $self->ut_snumbern('upbytes')
1197 || $self->ut_snumbern('downbytes')
1198 || $self->ut_snumbern('totalbytes')
1199 || $self->ut_snumbern('seconds_threshold')
1200 || $self->ut_snumbern('upbytes_threshold')
1201 || $self->ut_snumbern('downbytes_threshold')
1202 || $self->ut_snumbern('totalbytes_threshold')
1203 || $self->ut_enum('_password_encoding', ['',qw(plain crypt ldap)])
1204 || $self->ut_enum('password_selfchange', [ '', 'Y' ])
1205 || $self->ut_enum('password_recover', [ '', 'Y' ])
1206 || $self->ut_textn('cgp_accessmodes')
1207 || $self->ut_alphan('cgp_type')
1208 || $self->ut_textn('cgp_aliases' ) #well
1210 || $self->ut_alphasn('cgp_rulesallowed')
1211 || $self->ut_enum('cgp_rpopallowed', [ '', 'Y' ])
1212 || $self->ut_enum('cgp_mailtoall', [ '', 'Y' ])
1213 || $self->ut_enum('cgp_addmailtrailer', [ '', 'Y' ])
1214 || $self->ut_snumbern('cgp_archiveafter')
1216 || $self->ut_alphasn('cgp_deletemode')
1217 || $self->ut_enum('cgp_emptytrash', $self->cgp_emptytrash_values)
1218 || $self->ut_alphan('cgp_language')
1219 || $self->ut_textn('cgp_timezone')
1220 || $self->ut_textn('cgp_skinname')
1221 || $self->ut_textn('cgp_prontoskinname')
1222 || $self->ut_alphan('cgp_sendmdnmode')
1225 return $error if $error;
1228 local $username_letter = $username_letter;
1229 if ($self->svcnum) {
1230 my $cust_svc = $self->cust_svc
1231 or return "no cust_svc record found for svcnum ". $self->svcnum;
1232 my $cust_pkg = $cust_svc->cust_pkg;
1234 if ($self->pkgnum) {
1235 $cust_pkg = qsearchs('cust_pkg', { 'pkgnum' => $self->pkgnum } );#complain?
1239 $conf->exists('username-letter', $cust_pkg->cust_main->agentnum);
1242 my $ulen = $usernamemax || $self->dbdef_table->column('username')->length;
1243 if ( $username_uppercase ) {
1244 $recref->{username} =~ /^([a-z0-9_\-\.\&\%\:]{$usernamemin,$ulen})$/i
1245 or return gettext('illegal_username'). " ($usernamemin-$ulen): ". $recref->{username};
1246 $recref->{username} = $1;
1248 $recref->{username} =~ /^([a-z0-9_\-\.\&\%\:]{$usernamemin,$ulen})$/
1249 or return gettext('illegal_username'). " ($usernamemin-$ulen): ". $recref->{username};
1250 $recref->{username} = $1;
1253 if ( $username_letterfirst ) {
1254 $recref->{username} =~ /^[a-z]/ or return gettext('illegal_username');
1255 } elsif ( $username_letter ) {
1256 $recref->{username} =~ /[a-z]/ or return gettext('illegal_username');
1258 if ( $username_noperiod ) {
1259 $recref->{username} =~ /\./ and return gettext('illegal_username');
1261 if ( $username_nounderscore ) {
1262 $recref->{username} =~ /_/ and return gettext('illegal_username');
1264 if ( $username_nodash ) {
1265 $recref->{username} =~ /\-/ and return gettext('illegal_username');
1267 unless ( $username_ampersand ) {
1268 $recref->{username} =~ /\&/ and return gettext('illegal_username');
1270 unless ( $username_percent ) {
1271 $recref->{username} =~ /\%/ and return gettext('illegal_username');
1273 unless ( $username_colon ) {
1274 $recref->{username} =~ /\:/ and return gettext('illegal_username');
1277 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum: ".$recref->{popnum};
1278 $recref->{popnum} = $1;
1279 return "Unknown popnum" unless
1280 ! $recref->{popnum} ||
1281 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
1283 unless ( $part_svc->part_svc_column('uid')->columnflag eq 'F' ) {
1285 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
1286 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
1288 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
1289 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
1290 #not all systems use gid=uid
1291 #you can set a fixed gid in part_svc
1293 return "Only root can have uid 0"
1294 if $recref->{uid} == 0
1295 && $recref->{username} !~ /^(root|toor|smtp)$/;
1297 unless ( $recref->{username} eq 'sync' ) {
1298 if ( grep $_ eq $recref->{shell}, @shells ) {
1299 $recref->{shell} = (grep $_ eq $recref->{shell}, @shells)[0];
1301 return "Illegal shell \`". $self->shell. "\'; ".
1302 "shells configuration value contains: @shells";
1305 $recref->{shell} = '/bin/sync';
1309 $recref->{gid} ne '' ?
1310 return "Can't have gid without uid" : ( $recref->{gid}='' );
1311 #$recref->{dir} ne '' ?
1312 # return "Can't have directory without uid" : ( $recref->{dir}='' );
1313 $recref->{shell} ne '' ?
1314 return "Can't have shell without uid" : ( $recref->{shell}='' );
1317 unless ( $part_svc->part_svc_column('dir')->columnflag eq 'F' ) {
1319 $recref->{dir} =~ /^([\/\w\-\.\&]*)$/
1320 or return "Illegal directory: ". $recref->{dir};
1321 $recref->{dir} = $1;
1322 return "Illegal directory"
1323 if $recref->{dir} =~ /(^|\/)\.+(\/|$)/; #no .. component
1324 return "Illegal directory"
1325 if $recref->{dir} =~ /\&/ && ! $username_ampersand;
1326 unless ( $recref->{dir} ) {
1327 $recref->{dir} = $dir_prefix . '/';
1328 if ( $dirhash > 0 ) {
1329 for my $h ( 1 .. $dirhash ) {
1330 $recref->{dir} .= substr($recref->{username}, $h-1, 1). '/';
1332 } elsif ( $dirhash < 0 ) {
1333 for my $h ( reverse $dirhash .. -1 ) {
1334 $recref->{dir} .= substr($recref->{username}, $h, 1). '/';
1337 $recref->{dir} .= $recref->{username};
1343 # $error = $self->ut_textn('finger');
1344 # return $error if $error;
1345 if ( $self->getfield('finger') eq '' ) {
1346 my $cust_pkg = $self->svcnum
1347 ? $self->cust_svc->cust_pkg
1348 : qsearchs('cust_pkg', { 'pkgnum' => $self->getfield('pkgnum') } );
1350 my $cust_main = $cust_pkg->cust_main;
1351 $self->setfield('finger', $cust_main->first.' '.$cust_main->get('last') );
1354 $self->getfield('finger') =~
1355 /^([\w \t\!\@\#\$\%\&\(\)\-\+\;\'\"\,\.\?\/\*\<\>]*)$/
1356 or return "Illegal finger: ". $self->getfield('finger');
1357 $self->setfield('finger', $1);
1359 for (qw( quota file_quota file_maxsize )) {
1360 $recref->{$_} =~ /^(\w*)$/ or return "Illegal $_";
1363 $recref->{file_maxnum} =~ /^\s*(\d*)\s*$/ or return "Illegal file_maxnum";
1364 $recref->{file_maxnum} = $1;
1366 unless ( $part_svc->part_svc_column('slipip')->columnflag eq 'F' ) {
1367 if ( $recref->{slipip} eq '' ) {
1368 $recref->{slipip} = '';
1369 } elsif ( $recref->{slipip} eq '0e0' ) {
1370 $recref->{slipip} = '0e0';
1372 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
1373 or return "Illegal slipip: ". $self->slipip;
1374 $recref->{slipip} = $1;
1379 #arbitrary RADIUS stuff; allow ut_textn for now
1380 foreach ( grep /^radius_/, fields('svc_acct') ) {
1381 $self->ut_textn($_);
1384 # First, if _password is blank, generate one and set default encoding.
1385 if ( ! $recref->{_password} ) {
1386 $error = $self->set_password('');
1388 # But if there's a _password but no encoding, assume it's plaintext and
1389 # set it to default encoding.
1390 elsif ( ! $recref->{_password_encoding} ) {
1391 $error = $self->set_password($recref->{_password});
1393 return $error if $error;
1395 # Next, check _password to ensure compliance with the encoding.
1396 if ( $recref->{_password_encoding} eq 'ldap' ) {
1398 if ( $recref->{_password} =~ /^(\{[\w\-]+\})(!?.{0,64})$/ ) {
1399 $recref->{_password} = uc($1).$2;
1401 return 'Illegal (ldap-encoded) password: '. $recref->{_password};
1404 } elsif ( $recref->{_password_encoding} eq 'crypt' ) {
1406 if ( $recref->{_password} =~
1407 #/^(\$\w+\$.*|[\w\+\/]{13}|_[\w\+\/]{19}|\*)$/
1408 /^(!!?)?(\$\w+\$.*|[\w\+\/\.]{13}|_[\w\+\/\.]{19}|\*)$/
1411 $recref->{_password} = ( defined($1) ? $1 : '' ). $2;
1414 return 'Illegal (crypt-encoded) password: '. $recref->{_password};
1417 } elsif ( $recref->{_password_encoding} eq 'plain' ) {
1418 # Password randomization is now in set_password.
1419 # Strip whitespace characters, check length requirements, etc.
1420 if ( $recref->{_password} =~ /^([^\t\n]{$passwordmin,$passwordmax})$/ ) {
1421 $recref->{_password} = $1;
1423 return gettext('illegal_password'). " $passwordmin-$passwordmax ".
1424 FS::Msgcat::_gettext('illegal_password_characters').
1425 ": ". $recref->{_password};
1428 if ( $password_noampersand ) {
1429 $recref->{_password} =~ /\&/ and return gettext('illegal_password');
1431 if ( $password_noexclamation ) {
1432 $recref->{_password} =~ /\!/ and return gettext('illegal_password');
1436 return "invalid password encoding ('".$recref->{_password_encoding}."'";
1438 $self->SUPER::check;
1443 sub _password_encryption {
1445 my $encoding = lc($self->_password_encoding);
1446 return if !$encoding;
1447 return 'plain' if $encoding eq 'plain';
1448 if($encoding eq 'crypt') {
1449 my $pass = $self->_password;
1450 $pass =~ s/^\*SUSPENDED\* //;
1452 return 'md5' if $pass =~ /^\$1\$/;
1453 #return 'blowfish' if $self->_password =~ /^\$2\$/;
1454 return 'des' if length($pass) == 13;
1457 if($encoding eq 'ldap') {
1458 uc($self->_password) =~ /^\{([\w-]+)\}/;
1459 return 'crypt' if $1 eq 'CRYPT' or $1 eq 'DES';
1460 return 'plain' if $1 eq 'PLAIN' or $1 eq 'CLEARTEXT';
1461 return 'md5' if $1 eq 'MD5';
1462 return 'sha1' if $1 eq 'SHA' or $1 eq 'SHA-1';
1469 sub get_cleartext_password {
1471 if($self->_password_encryption eq 'plain') {
1472 if($self->_password_encoding eq 'ldap') {
1473 $self->_password =~ /\{\w+\}(.*)$/;
1477 return $self->_password;
1486 Set the cleartext password for the account. If _password_encoding is set, the
1487 new password will be encoded according to the existing method (including
1488 encryption mode, if it can be determined). Otherwise,
1489 config('default-password-encoding') is used.
1491 If no password is supplied (or a zero-length password when minimum password length
1492 is >0), one will be generated randomly.
1497 my( $self, $pass ) = ( shift, shift );
1499 warn "[$me] set_password (to $pass) called on $self: ". Dumper($self)
1502 my $failure = gettext('illegal_password'). " $passwordmin-$passwordmax ".
1503 FS::Msgcat::_gettext('illegal_password_characters').
1506 my( $encoding, $encryption ) = ('', '');
1508 if ( $self->_password_encoding ) {
1509 $encoding = $self->_password_encoding;
1510 # identify existing encryption method, try to use it.
1511 $encryption = $self->_password_encryption;
1513 # use the system default
1519 # set encoding to system default
1520 ($encoding, $encryption) =
1521 split(/-/, lc($conf->config('default-password-encoding')));
1522 $encoding ||= 'legacy';
1523 $self->_password_encoding($encoding);
1526 if ( $encoding eq 'legacy' ) {
1528 # The legacy behavior from check():
1529 # If the password is blank, randomize it and set encoding to 'plain'.
1530 if(!defined($pass) or (length($pass) == 0 and $passwordmin)) {
1531 $pass = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) );
1532 $self->_password_encoding('plain');
1534 # Prefix + valid-length password
1535 if ( $pass =~ /^((\*SUSPENDED\* |!!?)?)([^\t\n]{$passwordmin,$passwordmax})$/ ) {
1537 $self->_password_encoding('plain');
1538 # Prefix + crypt string
1539 } elsif ( $pass =~ /^((\*SUSPENDED\* |!!?)?)([\w\.\/\$\;\+]{13,64})$/ ) {
1541 $self->_password_encoding('crypt');
1542 # Various disabled crypt passwords
1543 } elsif ( $pass eq '*' || $pass eq '!' || $pass eq '!!' ) {
1544 $self->_password_encoding('crypt');
1550 $self->_password($pass);
1556 if $passwordmin && length($pass) < $passwordmin
1557 or $passwordmax && length($pass) > $passwordmax;
1559 if ( $encoding eq 'crypt' ) {
1560 if ($encryption eq 'md5') {
1561 $pass = unix_md5_crypt($pass);
1562 } elsif ($encryption eq 'des') {
1563 $pass = crypt($pass, $saltset[int(rand(64))].$saltset[int(rand(64))]);
1566 } elsif ( $encoding eq 'ldap' ) {
1567 if ($encryption eq 'md5') {
1568 $pass = md5_base64($pass);
1569 } elsif ($encryption eq 'sha1') {
1570 $pass = sha1_base64($pass);
1571 } elsif ($encryption eq 'crypt') {
1572 $pass = crypt($pass, $saltset[int(rand(64))].$saltset[int(rand(64))]);
1574 # else $encryption eq 'plain', do nothing
1575 $pass = '{'.uc($encryption).'}'.$pass;
1577 # else encoding eq 'plain'
1579 $self->_password($pass);
1585 Internal function to check the username against the list of system usernames
1586 from the I<system_usernames> configuration value. Returns true if the username
1587 is listed on the system username list.
1593 scalar( grep { $self->username eq $_ || $self->email eq $_ }
1594 $conf->config('system_usernames')
1598 =item _check_duplicate
1600 Internal method to check for duplicates usernames, username@domain pairs and
1603 If the I<global_unique-username> configuration value is set to B<username> or
1604 B<username@domain>, enforces global username or username@domain uniqueness.
1606 In all cases, check for duplicate uids and usernames or username@domain pairs
1607 per export and with identical I<svcpart> values.
1611 sub _check_duplicate {
1614 my $global_unique = $conf->config('global_unique-username') || 'none';
1615 return '' if $global_unique eq 'disabled';
1619 my $part_svc = qsearchs('part_svc', { 'svcpart' => $self->svcpart } );
1620 unless ( $part_svc ) {
1621 return 'unknown svcpart '. $self->svcpart;
1624 my @dup_user = grep { !$self->svcnum || $_->svcnum != $self->svcnum }
1625 qsearch( 'svc_acct', { 'username' => $self->username } );
1626 return gettext('username_in_use')
1627 if $global_unique eq 'username' && @dup_user;
1629 my @dup_userdomain = grep { !$self->svcnum || $_->svcnum != $self->svcnum }
1630 qsearch( 'svc_acct', { 'username' => $self->username,
1631 'domsvc' => $self->domsvc } );
1632 return gettext('username_in_use')
1633 if $global_unique eq 'username@domain' && @dup_userdomain;
1636 if ( $part_svc->part_svc_column('uid')->columnflag ne 'F'
1637 && $self->username !~ /^(toor|(hyla)?fax)$/ ) {
1638 @dup_uid = grep { !$self->svcnum || $_->svcnum != $self->svcnum }
1639 qsearch( 'svc_acct', { 'uid' => $self->uid } );
1644 if ( @dup_user || @dup_userdomain || @dup_uid ) {
1645 my $exports = FS::part_export::export_info('svc_acct');
1646 my %conflict_user_svcpart;
1647 my %conflict_userdomain_svcpart = ( $self->svcpart => 'SELF', );
1649 foreach my $part_export ( $part_svc->part_export ) {
1651 #this will catch to the same exact export
1652 my @svcparts = map { $_->svcpart } $part_export->export_svc;
1654 #this will catch to exports w/same exporthost+type ???
1655 #my @other_part_export = qsearch('part_export', {
1656 # 'machine' => $part_export->machine,
1657 # 'exporttype' => $part_export->exporttype,
1659 #foreach my $other_part_export ( @other_part_export ) {
1660 # push @svcparts, map { $_->svcpart }
1661 # qsearch('export_svc', { 'exportnum' => $part_export->exportnum });
1664 #my $nodomain = $exports->{$part_export->exporttype}{'nodomain'};
1665 #silly kludge to avoid uninitialized value errors
1666 my $nodomain = exists( $exports->{$part_export->exporttype}{'nodomain'} )
1667 ? $exports->{$part_export->exporttype}{'nodomain'}
1669 if ( $nodomain =~ /^Y/i ) {
1670 $conflict_user_svcpart{$_} = $part_export->exportnum
1673 $conflict_userdomain_svcpart{$_} = $part_export->exportnum
1678 foreach my $dup_user ( @dup_user ) {
1679 my $dup_svcpart = $dup_user->cust_svc->svcpart;
1680 if ( exists($conflict_user_svcpart{$dup_svcpart}) ) {
1681 return "duplicate username ". $self->username.
1682 ": conflicts with svcnum ". $dup_user->svcnum.
1683 " via exportnum ". $conflict_user_svcpart{$dup_svcpart};
1687 foreach my $dup_userdomain ( @dup_userdomain ) {
1688 my $dup_svcpart = $dup_userdomain->cust_svc->svcpart;
1689 if ( exists($conflict_userdomain_svcpart{$dup_svcpart}) ) {
1690 return "duplicate username\@domain ". $self->email.
1691 ": conflicts with svcnum ". $dup_userdomain->svcnum.
1692 " via exportnum ". $conflict_userdomain_svcpart{$dup_svcpart};
1696 foreach my $dup_uid ( @dup_uid ) {
1697 my $dup_svcpart = $dup_uid->cust_svc->svcpart;
1698 if ( exists($conflict_user_svcpart{$dup_svcpart})
1699 || exists($conflict_userdomain_svcpart{$dup_svcpart}) ) {
1700 return "duplicate uid ". $self->uid.
1701 ": conflicts with svcnum ". $dup_uid->svcnum.
1703 ( $conflict_user_svcpart{$dup_svcpart}
1704 || $conflict_userdomain_svcpart{$dup_svcpart} );
1716 Depriciated, use radius_reply instead.
1721 carp "FS::svc_acct::radius depriciated, use radius_reply";
1722 $_[0]->radius_reply;
1727 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1728 reply attributes of this record.
1730 Note that this is now the preferred method for reading RADIUS attributes -
1731 accessing the columns directly is discouraged, as the column names are
1732 expected to change in the future.
1739 return %{ $self->{'radius_reply'} }
1740 if exists $self->{'radius_reply'};
1745 my($column, $attrib) = ($1, $2);
1746 #$attrib =~ s/_/\-/g;
1747 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1748 } grep { /^radius_/ && $self->getfield($_) } fields( $self->table );
1750 if ( $self->slipip && $self->slipip ne '0e0' ) {
1751 $reply{$radius_ip} = $self->slipip;
1754 if ( $self->seconds !~ /^$/ ) {
1755 $reply{'Session-Timeout'} = $self->seconds;
1758 if ( $conf->exists('radius-chillispot-max') ) {
1759 #http://dev.coova.org/svn/coova-chilli/doc/dictionary.chillispot
1761 #hmm. just because sqlradius.pm says so?
1768 foreach my $what (qw( input output total )) {
1769 my $is = $whatis{$what}.'bytes';
1770 if ( $self->$is() =~ /\d/ ) {
1771 my $big = new Math::BigInt $self->$is();
1772 $big = new Math::BigInt '0' if $big->is_neg();
1773 my $att = "Chillispot-Max-\u$what";
1774 $reply{"$att-Octets"} = $big->copy->band(0xffffffff)->bstr;
1775 $reply{"$att-Gigawords"} = $big->copy->brsft(32)->bstr;
1786 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1787 check attributes of this record.
1789 Note that this is now the preferred method for reading RADIUS attributes -
1790 accessing the columns directly is discouraged, as the column names are
1791 expected to change in the future.
1798 return %{ $self->{'radius_check'} }
1799 if exists $self->{'radius_check'};
1804 my($column, $attrib) = ($1, $2);
1805 #$attrib =~ s/_/\-/g;
1806 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1807 } grep { /^rc_/ && $self->getfield($_) } fields( $self->table );
1810 my($pw_attrib, $password) = $self->radius_password;
1811 $check{$pw_attrib} = $password;
1813 my $cust_svc = $self->cust_svc;
1815 my $cust_pkg = $cust_svc->cust_pkg;
1816 if ( $cust_pkg && $cust_pkg->part_pkg->is_prepaid && $cust_pkg->bill ) {
1817 $check{'Expiration'} = time2str('%B %e %Y %T', $cust_pkg->bill ); #http://lists.cistron.nl/pipermail/freeradius-users/2005-January/040184.html
1820 warn "WARNING: no cust_svc record for svc_acct.svcnum ". $self->svcnum.
1821 "; can't set Expiration\n"
1829 =item radius_password
1831 Returns a key/value pair containing the RADIUS attribute name and value
1836 sub radius_password {
1840 if ( $self->_password_encoding eq 'ldap' ) {
1841 $pw_attrib = 'Password-With-Header';
1842 } elsif ( $self->_password_encoding eq 'crypt' ) {
1843 $pw_attrib = 'Crypt-Password';
1844 } elsif ( $self->_password_encoding eq 'plain' ) {
1845 $pw_attrib = $radius_password;
1847 $pw_attrib = length($self->_password) <= 12
1852 ($pw_attrib, $self->_password);
1858 This method instructs the object to "snapshot" or freeze RADIUS check and
1859 reply attributes to the current values.
1863 #bah, my english is too broken this morning
1864 #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
1865 #the FS::cust_pkg's replace method to trigger the correct export updates when
1866 #package dates change)
1871 $self->{$_} = { $self->$_() }
1872 foreach qw( radius_reply radius_check );
1876 =item forget_snapshot
1878 This methos instructs the object to forget any previously snapshotted
1879 RADIUS check and reply attributes.
1883 sub forget_snapshot {
1887 foreach qw( radius_reply radius_check );
1891 =item domain [ END_TIMESTAMP [ START_TIMESTAMP ] ]
1893 Returns the domain associated with this account.
1895 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
1902 die "svc_acct.domsvc is null for svcnum ". $self->svcnum unless $self->domsvc;
1903 my $svc_domain = $self->svc_domain(@_)
1904 or die "no svc_domain.svcnum for svc_acct.domsvc ". $self->domsvc;
1905 $svc_domain->domain;
1910 Returns the FS::cust_svc record for this account (see L<FS::cust_svc>).
1914 #inherited from svc_Common
1916 =item email [ END_TIMESTAMP [ START_TIMESTAMP ] ]
1918 Returns an email address associated with the account.
1920 END_TIMESTAMP and START_TIMESTAMP can optionally be passed when dealing with
1927 $self->username. '@'. $self->domain(@_);
1932 Returns an array of FS::acct_snarf records associated with the account.
1939 'table' => 'acct_snarf',
1940 'hashref' => { 'svcnum' => $self->svcnum },
1941 #'order_by' => 'ORDER BY priority ASC',
1945 =item cgp_rpop_hashref
1947 Returns an arrayref of RPOP data suitable for Communigate Pro API commands.
1951 sub cgp_rpop_hashref {
1953 { map { $_->snarfname => $_->cgp_hashref } $self->acct_snarf };
1956 =item decrement_upbytes OCTETS
1958 Decrements the I<upbytes> field of this record by the given amount. If there
1959 is an error, returns the error, otherwise returns false.
1963 sub decrement_upbytes {
1964 shift->_op_usage('-', 'upbytes', @_);
1967 =item increment_upbytes OCTETS
1969 Increments the I<upbytes> field of this record by the given amount. If there
1970 is an error, returns the error, otherwise returns false.
1974 sub increment_upbytes {
1975 shift->_op_usage('+', 'upbytes', @_);
1978 =item decrement_downbytes OCTETS
1980 Decrements the I<downbytes> field of this record by the given amount. If there
1981 is an error, returns the error, otherwise returns false.
1985 sub decrement_downbytes {
1986 shift->_op_usage('-', 'downbytes', @_);
1989 =item increment_downbytes OCTETS
1991 Increments the I<downbytes> field of this record by the given amount. If there
1992 is an error, returns the error, otherwise returns false.
1996 sub increment_downbytes {
1997 shift->_op_usage('+', 'downbytes', @_);
2000 =item decrement_totalbytes OCTETS
2002 Decrements the I<totalbytes> field of this record by the given amount. If there
2003 is an error, returns the error, otherwise returns false.
2007 sub decrement_totalbytes {
2008 shift->_op_usage('-', 'totalbytes', @_);
2011 =item increment_totalbytes OCTETS
2013 Increments the I<totalbytes> field of this record by the given amount. If there
2014 is an error, returns the error, otherwise returns false.
2018 sub increment_totalbytes {
2019 shift->_op_usage('+', 'totalbytes', @_);
2022 =item decrement_seconds SECONDS
2024 Decrements the I<seconds> field of this record by the given amount. If there
2025 is an error, returns the error, otherwise returns false.
2029 sub decrement_seconds {
2030 shift->_op_usage('-', 'seconds', @_);
2033 =item increment_seconds SECONDS
2035 Increments the I<seconds> field of this record by the given amount. If there
2036 is an error, returns the error, otherwise returns false.
2040 sub increment_seconds {
2041 shift->_op_usage('+', 'seconds', @_);
2049 my %op2condition = (
2050 '-' => sub { my($self, $column, $amount) = @_;
2051 $self->$column - $amount <= 0;
2053 '+' => sub { my($self, $column, $amount) = @_;
2054 ($self->$column || 0) + $amount > 0;
2057 my %op2warncondition = (
2058 '-' => sub { my($self, $column, $amount) = @_;
2059 my $threshold = $column . '_threshold';
2060 $self->$column - $amount <= $self->$threshold + 0;
2062 '+' => sub { my($self, $column, $amount) = @_;
2063 ($self->$column || 0) + $amount > 0;
2068 my( $self, $op, $column, $amount ) = @_;
2070 warn "$me _op_usage called for $column on svcnum ". $self->svcnum.
2071 ' ('. $self->email. "): $op $amount\n"
2074 return '' unless $amount;
2076 local $SIG{HUP} = 'IGNORE';
2077 local $SIG{INT} = 'IGNORE';
2078 local $SIG{QUIT} = 'IGNORE';
2079 local $SIG{TERM} = 'IGNORE';
2080 local $SIG{TSTP} = 'IGNORE';
2081 local $SIG{PIPE} = 'IGNORE';
2083 my $oldAutoCommit = $FS::UID::AutoCommit;
2084 local $FS::UID::AutoCommit = 0;
2087 my $sql = "UPDATE svc_acct SET $column = ".
2088 " CASE WHEN $column IS NULL THEN 0 ELSE $column END ". #$column||0
2089 " $op ? WHERE svcnum = ?";
2093 my $sth = $dbh->prepare( $sql )
2094 or die "Error preparing $sql: ". $dbh->errstr;
2095 my $rv = $sth->execute($amount, $self->svcnum);
2096 die "Error executing $sql: ". $sth->errstr
2097 unless defined($rv);
2098 die "Can't update $column for svcnum". $self->svcnum
2101 #$self->snapshot; #not necessary, we retain the old values
2102 #create an object with the updated usage values
2103 my $new = qsearchs('svc_acct', { 'svcnum' => $self->svcnum });
2105 my $error = $new->replace($self);
2107 $dbh->rollback if $oldAutoCommit;
2108 return "Error replacing: $error";
2111 #overlimit_action eq 'cancel' handling
2112 my $cust_pkg = $self->cust_svc->cust_pkg;
2114 && $cust_pkg->part_pkg->option('overlimit_action', 1) eq 'cancel'
2115 && $op eq '-' && &{$op2condition{$op}}($self, $column, $amount)
2119 my $error = $cust_pkg->cancel; #XXX should have a reason
2121 $dbh->rollback if $oldAutoCommit;
2122 return "Error cancelling: $error";
2125 #nothing else is relevant if we're cancelling, so commit & return success
2126 warn "$me update successful; committing\n"
2128 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2133 my $action = $op2action{$op};
2135 if ( &{$op2condition{$op}}($self, $column, $amount) &&
2136 ( $action eq 'suspend' && !$self->overlimit
2137 || $action eq 'unsuspend' && $self->overlimit )
2140 my $error = $self->_op_overlimit($action);
2142 $dbh->rollback if $oldAutoCommit;
2148 if ( $conf->exists("svc_acct-usage_$action")
2149 && &{$op2condition{$op}}($self, $column, $amount) ) {
2150 #my $error = $self->$action();
2151 my $error = $self->cust_svc->cust_pkg->$action();
2152 # $error ||= $self->overlimit($action);
2154 $dbh->rollback if $oldAutoCommit;
2155 return "Error ${action}ing: $error";
2159 if ($warning_template && &{$op2warncondition{$op}}($self, $column, $amount)) {
2160 my $wqueue = new FS::queue {
2161 'svcnum' => $self->svcnum,
2162 'job' => 'FS::svc_acct::reached_threshold',
2167 $to = $warning_cc if &{$op2condition{$op}}($self, $column, $amount);
2171 my $error = $wqueue->insert(
2172 'svcnum' => $self->svcnum,
2174 'column' => $column,
2178 $dbh->rollback if $oldAutoCommit;
2179 return "Error queuing threshold activity: $error";
2183 warn "$me update successful; committing\n"
2185 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2191 my( $self, $action ) = @_;
2193 local $SIG{HUP} = 'IGNORE';
2194 local $SIG{INT} = 'IGNORE';
2195 local $SIG{QUIT} = 'IGNORE';
2196 local $SIG{TERM} = 'IGNORE';
2197 local $SIG{TSTP} = 'IGNORE';
2198 local $SIG{PIPE} = 'IGNORE';
2200 my $oldAutoCommit = $FS::UID::AutoCommit;
2201 local $FS::UID::AutoCommit = 0;
2204 my $cust_pkg = $self->cust_svc->cust_pkg;
2206 my $conf_overlimit =
2208 ? $conf->config('overlimit_groups', $cust_pkg->cust_main->agentnum )
2209 : $conf->config('overlimit_groups');
2211 foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
2213 my $groups = $conf_overlimit || $part_export->option('overlimit_groups');
2214 next unless $groups;
2216 my $gref = &{ $self->_fieldhandlers->{'usergroup'} }( $self, $groups );
2218 my $other = new FS::svc_acct $self->hashref;
2219 $other->usergroup( $gref );
2222 if ($action eq 'suspend') {
2225 } else { # $action eq 'unsuspend'
2230 my $error = $part_export->export_replace($new, $old)
2231 || $self->overlimit($action);
2234 $dbh->rollback if $oldAutoCommit;
2235 return "Error replacing radius groups: $error";
2240 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2246 my( $self, $valueref, %options ) = @_;
2248 warn "$me set_usage called for svcnum ". $self->svcnum.
2249 ' ('. $self->email. "): ".
2250 join(', ', map { "$_ => " . $valueref->{$_}} keys %$valueref) . "\n"
2253 local $SIG{HUP} = 'IGNORE';
2254 local $SIG{INT} = 'IGNORE';
2255 local $SIG{QUIT} = 'IGNORE';
2256 local $SIG{TERM} = 'IGNORE';
2257 local $SIG{TSTP} = 'IGNORE';
2258 local $SIG{PIPE} = 'IGNORE';
2260 local $FS::svc_Common::noexport_hack = 1;
2261 my $oldAutoCommit = $FS::UID::AutoCommit;
2262 local $FS::UID::AutoCommit = 0;
2267 if ( $options{null} ) {
2268 %handyhash = ( map { ( $_ => undef, $_."_threshold" => undef ) }
2269 qw( seconds upbytes downbytes totalbytes )
2272 foreach my $field (keys %$valueref){
2273 $reset = 1 if $valueref->{$field};
2274 $self->setfield($field, $valueref->{$field});
2275 $self->setfield( $field.'_threshold',
2276 int($self->getfield($field)
2277 * ( $conf->exists('svc_acct-usage_threshold')
2278 ? 1 - $conf->config('svc_acct-usage_threshold')/100
2283 $handyhash{$field} = $self->getfield($field);
2284 $handyhash{$field.'_threshold'} = $self->getfield($field.'_threshold');
2286 #my $error = $self->replace; #NO! we avoid the call to ->check for
2287 #die $error if $error; #services not explicity changed via the UI
2289 my $sql = "UPDATE svc_acct SET " .
2290 join (',', map { "$_ = ?" } (keys %handyhash) ).
2291 " WHERE svcnum = ". $self->svcnum;
2296 if (scalar(keys %handyhash)) {
2297 my $sth = $dbh->prepare( $sql )
2298 or die "Error preparing $sql: ". $dbh->errstr;
2299 my $rv = $sth->execute(values %handyhash);
2300 die "Error executing $sql: ". $sth->errstr
2301 unless defined($rv);
2302 die "Can't update usage for svcnum ". $self->svcnum
2306 #$self->snapshot; #not necessary, we retain the old values
2307 #create an object with the updated usage values
2308 my $new = qsearchs('svc_acct', { 'svcnum' => $self->svcnum });
2309 local($FS::Record::nowarn_identical) = 1;
2310 my $error = $new->replace($self); #call exports
2312 $dbh->rollback if $oldAutoCommit;
2313 return "Error replacing: $error";
2320 $error = $self->_op_overlimit('unsuspend')
2321 if $self->overlimit;;
2323 $error ||= $self->cust_svc->cust_pkg->unsuspend
2324 if $conf->exists("svc_acct-usage_unsuspend");
2327 $dbh->rollback if $oldAutoCommit;
2328 return "Error unsuspending: $error";
2333 warn "$me update successful; committing\n"
2335 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2341 =item recharge HASHREF
2343 Increments usage columns by the amount specified in HASHREF as
2344 column=>amount pairs.
2349 my ($self, $vhash) = @_;
2352 warn "[$me] recharge called on $self: ". Dumper($self).
2353 "\nwith vhash: ". Dumper($vhash);
2356 my $oldAutoCommit = $FS::UID::AutoCommit;
2357 local $FS::UID::AutoCommit = 0;
2361 foreach my $column (keys %$vhash){
2362 $error ||= $self->_op_usage('+', $column, $vhash->{$column});
2366 $dbh->rollback if $oldAutoCommit;
2368 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
2373 =item is_rechargeable
2375 Returns true if this svc_account can be "recharged" and false otherwise.
2379 sub is_rechargable {
2381 $self->seconds ne ''
2382 || $self->upbytes ne ''
2383 || $self->downbytes ne ''
2384 || $self->totalbytes ne '';
2387 =item seconds_since TIMESTAMP
2389 Returns the number of seconds this account has been online since TIMESTAMP,
2390 according to the session monitor (see L<FS::Session>).
2392 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
2393 L<Time::Local> and L<Date::Parse> for conversion functions.
2397 #note: POD here, implementation in FS::cust_svc
2400 $self->cust_svc->seconds_since(@_);
2403 =item seconds_since_sqlradacct TIMESTAMP_START TIMESTAMP_END
2405 Returns the numbers of seconds this account has been online between
2406 TIMESTAMP_START (inclusive) and TIMESTAMP_END (exclusive), according to an
2407 external SQL radacct table, specified via sqlradius export. Sessions which
2408 started in the specified range but are still open are counted from session
2409 start to the end of the range (unless they are over 1 day old, in which case
2410 they are presumed missing their stop record and not counted). Also, sessions
2411 which end in the range but started earlier are counted from the start of the
2412 range to session end. Finally, sessions which start before the range but end
2413 after are counted for the entire range.
2415 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
2416 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
2421 #note: POD here, implementation in FS::cust_svc
2422 sub seconds_since_sqlradacct {
2424 $self->cust_svc->seconds_since_sqlradacct(@_);
2427 =item attribute_since_sqlradacct TIMESTAMP_START TIMESTAMP_END ATTRIBUTE
2429 Returns the sum of the given attribute for all accounts (see L<FS::svc_acct>)
2430 in this package for sessions ending between TIMESTAMP_START (inclusive) and
2431 TIMESTAMP_END (exclusive).
2433 TIMESTAMP_START and TIMESTAMP_END are specified as UNIX timestamps; see
2434 L<perlfunc/"time">. Also see L<Time::Local> and L<Date::Parse> for conversion
2439 #note: POD here, implementation in FS::cust_svc
2440 sub attribute_since_sqlradacct {
2442 $self->cust_svc->attribute_since_sqlradacct(@_);
2445 =item get_session_history TIMESTAMP_START TIMESTAMP_END
2447 Returns an array of hash references of this customers login history for the
2448 given time range. (document this better)
2452 sub get_session_history {
2454 $self->cust_svc->get_session_history(@_);
2457 =item last_login_text
2459 Returns text describing the time of last login.
2463 sub last_login_text {
2465 $self->last_login ? ctime($self->last_login) : 'unknown';
2468 =item get_cdrs TIMESTAMP_START TIMESTAMP_END [ 'OPTION' => 'VALUE ... ]
2473 my($self, $start, $end, %opt ) = @_;
2475 my $did = $self->username; #yup
2477 my $prefix = $opt{'default_prefix'}; #convergent.au '+61'
2479 my $for_update = $opt{'for_update'} ? 'FOR UPDATE' : '';
2481 #SELECT $for_update * FROM cdr
2482 # WHERE calldate >= $start #need a conversion
2483 # AND calldate < $end #ditto
2484 # AND ( charged_party = "$did"
2485 # OR charged_party = "$prefix$did" #if length($prefix);
2486 # OR ( ( charged_party IS NULL OR charged_party = '' )
2488 # ( src = "$did" OR src = "$prefix$did" ) # if length($prefix)
2491 # AND ( freesidestatus IS NULL OR freesidestatus = '' )
2494 if ( length($prefix) ) {
2496 " AND ( charged_party = '$did'
2497 OR charged_party = '$prefix$did'
2498 OR ( ( charged_party IS NULL OR charged_party = '' )
2500 ( src = '$did' OR src = '$prefix$did' )
2506 " AND ( charged_party = '$did'
2507 OR ( ( charged_party IS NULL OR charged_party = '' )
2517 'select' => "$for_update *",
2520 #( freesidestatus IS NULL OR freesidestatus = '' )
2521 'freesidestatus' => '',
2523 'extra_sql' => $charged_or_src,
2531 Returns all RADIUS groups for this account (see L<FS::radius_usergroup>).
2537 if ( $self->usergroup ) {
2538 confess "explicitly specified usergroup not an arrayref: ". $self->usergroup
2539 unless ref($self->usergroup) eq 'ARRAY';
2540 #when provisioning records, export callback runs in svc_Common.pm before
2541 #radius_usergroup records can be inserted...
2542 @{$self->usergroup};
2544 map { $_->groupname }
2545 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } );
2549 =item clone_suspended
2551 Constructor used by FS::part_export::_export_suspend fallback. Document
2556 sub clone_suspended {
2558 my %hash = $self->hash;
2559 $hash{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) );
2560 new FS::svc_acct \%hash;
2563 =item clone_kludge_unsuspend
2565 Constructor used by FS::part_export::_export_unsuspend fallback. Document
2570 sub clone_kludge_unsuspend {
2572 my %hash = $self->hash;
2573 $hash{_password} = '';
2574 new FS::svc_acct \%hash;
2577 =item check_password
2579 Checks the supplied password against the (possibly encrypted) password in the
2580 database. Returns true for a successful authentication, false for no match.
2582 Currently supported encryptions are: classic DES crypt() and MD5
2586 sub check_password {
2587 my($self, $check_password) = @_;
2589 #remove old-style SUSPENDED kludge, they should be allowed to login to
2590 #self-service and pay up
2591 ( my $password = $self->_password ) =~ s/^\*SUSPENDED\* //;
2593 if ( $self->_password_encoding eq 'ldap' ) {
2595 my $auth = from_rfc2307 Authen::Passphrase $self->_password;
2596 return $auth->match($check_password);
2598 } elsif ( $self->_password_encoding eq 'crypt' ) {
2600 my $auth = from_crypt Authen::Passphrase $self->_password;
2601 return $auth->match($check_password);
2603 } elsif ( $self->_password_encoding eq 'plain' ) {
2605 return $check_password eq $password;
2609 #XXX this could be replaced with Authen::Passphrase stuff
2611 if ( $password =~ /^(\*|!!?)$/ ) { #no self-service login
2613 } elsif ( length($password) < 13 ) { #plaintext
2614 $check_password eq $password;
2615 } elsif ( length($password) == 13 ) { #traditional DES crypt
2616 crypt($check_password, $password) eq $password;
2617 } elsif ( $password =~ /^\$1\$/ ) { #MD5 crypt
2618 unix_md5_crypt($check_password, $password) eq $password;
2619 } elsif ( $password =~ /^\$2a?\$/ ) { #Blowfish
2620 warn "Can't check password: Blowfish encryption not yet supported, ".
2621 "svcnum ". $self->svcnum. "\n";
2624 warn "Can't check password: Unrecognized encryption for svcnum ".
2625 $self->svcnum. "\n";
2633 =item crypt_password [ DEFAULT_ENCRYPTION_TYPE ]
2635 Returns an encrypted password, either by passing through an encrypted password
2636 in the database or by encrypting a plaintext password from the database.
2638 The optional DEFAULT_ENCRYPTION_TYPE parameter can be set to I<crypt> (classic
2639 UNIX DES crypt), I<md5> (md5 crypt supported by most modern Linux and BSD
2640 distrubtions), or (eventually) I<blowfish> (blowfish hashing supported by
2641 OpenBSD, SuSE, other Linux distibutions with pam_unix2, etc.). The default
2642 encryption type is only used if the password is not already encrypted in the
2647 sub crypt_password {
2650 if ( $self->_password_encoding eq 'ldap' ) {
2652 if ( $self->_password =~ /^\{(PLAIN|CLEARTEXT)\}(.+)$/ ) {
2655 #XXX this could be replaced with Authen::Passphrase stuff
2657 my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2658 if ( $encryption eq 'crypt' ) {
2661 $saltset[int(rand(64))].$saltset[int(rand(64))]
2663 } elsif ( $encryption eq 'md5' ) {
2664 unix_md5_crypt( $self->_password );
2665 } elsif ( $encryption eq 'blowfish' ) {
2666 croak "unknown encryption method $encryption";
2668 croak "unknown encryption method $encryption";
2671 } elsif ( $self->_password =~ /^\{CRYPT\}(.+)$/ ) {
2675 } elsif ( $self->_password_encoding eq 'crypt' ) {
2677 return $self->_password;
2679 } elsif ( $self->_password_encoding eq 'plain' ) {
2681 #XXX this could be replaced with Authen::Passphrase stuff
2683 my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2684 if ( $encryption eq 'crypt' ) {
2687 $saltset[int(rand(64))].$saltset[int(rand(64))]
2689 } elsif ( $encryption eq 'md5' ) {
2690 unix_md5_crypt( $self->_password );
2691 } elsif ( $encryption eq 'blowfish' ) {
2692 croak "unknown encryption method $encryption";
2694 croak "unknown encryption method $encryption";
2699 if ( length($self->_password) == 13
2700 || $self->_password =~ /^\$(1|2a?)\$/
2701 || $self->_password =~ /^(\*|NP|\*LK\*|!!?)$/
2707 #XXX this could be replaced with Authen::Passphrase stuff
2709 my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2710 if ( $encryption eq 'crypt' ) {
2713 $saltset[int(rand(64))].$saltset[int(rand(64))]
2715 } elsif ( $encryption eq 'md5' ) {
2716 unix_md5_crypt( $self->_password );
2717 } elsif ( $encryption eq 'blowfish' ) {
2718 croak "unknown encryption method $encryption";
2720 croak "unknown encryption method $encryption";
2729 =item ldap_password [ DEFAULT_ENCRYPTION_TYPE ]
2731 Returns an encrypted password in "LDAP" format, with a curly-bracked prefix
2732 describing the format, for example, "{PLAIN}himom", "{CRYPT}94pAVyK/4oIBk" or
2733 "{MD5}5426824942db4253f87a1009fd5d2d4".
2735 The optional DEFAULT_ENCRYPTION_TYPE is not yet used, but the idea is for it
2736 to work the same as the B</crypt_password> method.
2742 #eventually should check a "password-encoding" field
2744 if ( $self->_password_encoding eq 'ldap' ) {
2746 return $self->_password;
2748 } elsif ( $self->_password_encoding eq 'crypt' ) {
2750 if ( length($self->_password) == 13 ) { #crypt
2751 return '{CRYPT}'. $self->_password;
2752 } elsif ( $self->_password =~ /^\$1\$(.*)$/ && length($1) == 31 ) { #passwdMD5
2754 #} elsif ( $self->_password =~ /^\$2a?\$(.*)$/ ) { #Blowfish
2755 # die "Blowfish encryption not supported in this context, svcnum ".
2756 # $self->svcnum. "\n";
2758 warn "encryption method not (yet?) supported in LDAP context";
2759 return '{CRYPT}*'; #unsupported, should not auth
2762 } elsif ( $self->_password_encoding eq 'plain' ) {
2764 return '{PLAIN}'. $self->_password;
2766 #return '{CLEARTEXT}'. $self->_password; #?
2770 if ( length($self->_password) == 13 ) { #crypt
2771 return '{CRYPT}'. $self->_password;
2772 } elsif ( $self->_password =~ /^\$1\$(.*)$/ && length($1) == 31 ) { #passwdMD5
2774 } elsif ( $self->_password =~ /^\$2a?\$(.*)$/ ) { #Blowfish
2775 warn "Blowfish encryption not supported in this context, svcnum ".
2776 $self->svcnum. "\n";
2779 #are these two necessary anymore?
2780 } elsif ( $self->_password =~ /^(\w{48})$/ ) { #LDAP SSHA
2781 return '{SSHA}'. $1;
2782 } elsif ( $self->_password =~ /^(\w{64})$/ ) { #LDAP NS-MTA-MD5
2783 return '{NS-MTA-MD5}'. $1;
2786 return '{PLAIN}'. $self->_password;
2788 #return '{CLEARTEXT}'. $self->_password; #?
2790 #XXX this could be replaced with Authen::Passphrase stuff if it gets used
2791 #my $encryption = ( scalar(@_) && $_[0] ) ? shift : 'crypt';
2792 #if ( $encryption eq 'crypt' ) {
2793 # return '{CRYPT}'. crypt(
2795 # $saltset[int(rand(64))].$saltset[int(rand(64))]
2797 #} elsif ( $encryption eq 'md5' ) {
2798 # unix_md5_crypt( $self->_password );
2799 #} elsif ( $encryption eq 'blowfish' ) {
2800 # croak "unknown encryption method $encryption";
2802 # croak "unknown encryption method $encryption";
2810 =item domain_slash_username
2812 Returns $domain/$username/
2816 sub domain_slash_username {
2818 $self->domain. '/'. $self->username. '/';
2821 =item virtual_maildir
2823 Returns $domain/maildirs/$username/
2827 sub virtual_maildir {
2829 $self->domain. '/maildirs/'. $self->username. '/';
2834 =head1 CLASS METHODS
2838 =item search HASHREF
2840 Class method which returns a qsearch hash expression to search for parameters
2841 specified in HASHREF. Valid parameters are
2855 Arrayref of pkgparts
2861 Arrayref of additional WHERE clauses, will be ANDed together.
2872 my ($class, $params) = @_;
2877 if ( $params->{'domain'} ) {
2878 my $svc_domain = qsearchs('svc_domain', { 'domain'=>$params->{'domain'} } );
2879 #preserve previous behavior & bubble up an error if $svc_domain not found?
2880 push @where, 'domsvc = '. $svc_domain->svcnum if $svc_domain;
2884 if ( $params->{'domsvc'} =~ /^(\d+)$/ ) {
2885 push @where, "domsvc = $1";
2889 push @where, 'pkgnum IS NULL' if $params->{'unlinked'};
2892 if ( $params->{'agentnum'} =~ /^(\d+)$/ and $1 ) {
2893 push @where, "agentnum = $1";
2897 if ( $params->{'custnum'} =~ /^(\d+)$/ and $1 ) {
2898 push @where, "custnum = $1";
2902 if ( $params->{'pkgpart'} && scalar(@{ $params->{'pkgpart'} }) ) {
2903 #XXX untaint or sql quote
2905 'cust_pkg.pkgpart IN ('. join(',', @{ $params->{'pkgpart'} } ). ')';
2909 if ( $params->{'popnum'} =~ /^(\d+)$/ ) {
2910 push @where, "popnum = $1";
2914 if ( $params->{'svcpart'} =~ /^(\d+)$/ ) {
2915 push @where, "svcpart = $1";
2919 # here is the agent virtualization
2920 #if ($params->{CurrentUser}) {
2922 # qsearchs('access_user', { username => $params->{CurrentUser} });
2924 # if ($access_user) {
2925 # push @where, $access_user->agentnums_sql('table'=>'cust_main');
2927 # push @where, "1=0";
2930 push @where, $FS::CurrentUser::CurrentUser->agentnums_sql(
2931 'table' => 'cust_main',
2932 'null_right' => 'View/link unlinked services',
2936 push @where, @{ $params->{'where'} } if $params->{'where'};
2938 my $extra_sql = scalar(@where) ? ' WHERE '. join(' AND ', @where) : '';
2940 my $addl_from = ' LEFT JOIN cust_svc USING ( svcnum ) '.
2941 ' LEFT JOIN part_svc USING ( svcpart ) '.
2942 ' LEFT JOIN cust_pkg USING ( pkgnum ) '.
2943 ' LEFT JOIN cust_main USING ( custnum ) ';
2945 my $count_query = "SELECT COUNT(*) FROM svc_acct $addl_from $extra_sql";
2946 #if ( keys %svc_acct ) {
2947 # $count_query .= ' WHERE '.
2948 # join(' AND ', map "$_ = ". dbh->quote($svc_acct{$_}),
2954 'table' => 'svc_acct',
2955 'hashref' => {}, # \%svc_acct,
2956 'select' => join(', ',
2959 'cust_main.custnum',
2960 FS::UI::Web::cust_sql_fields($params->{'cust_fields'}),
2962 'addl_from' => $addl_from,
2963 'extra_sql' => $extra_sql,
2964 'order_by' => $params->{'order_by'},
2965 'count_query' => $count_query,
2978 This is the FS::svc_acct job-queue-able version. It still uses
2979 FS::Misc::send_email under-the-hood.
2986 eval "use FS::Misc qw(send_email)";
2989 $opt{mimetype} ||= 'text/plain';
2990 $opt{mimetype} .= '; charset="iso-8859-1"' unless $opt{mimetype} =~ /charset/;
2992 my $error = send_email(
2993 'from' => $opt{from},
2995 'subject' => $opt{subject},
2996 'content-type' => $opt{mimetype},
2997 'body' => [ map "$_\n", split("\n", $opt{body}) ],
2999 die $error if $error;
3002 =item check_and_rebuild_fuzzyfiles
3006 sub check_and_rebuild_fuzzyfiles {
3007 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
3008 -e "$dir/svc_acct.username"
3009 or &rebuild_fuzzyfiles;
3012 =item rebuild_fuzzyfiles
3016 sub rebuild_fuzzyfiles {
3018 use Fcntl qw(:flock);
3020 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
3024 open(USERNAMELOCK,">>$dir/svc_acct.username")
3025 or die "can't open $dir/svc_acct.username: $!";
3026 flock(USERNAMELOCK,LOCK_EX)
3027 or die "can't lock $dir/svc_acct.username: $!";
3029 my @all_username = map $_->getfield('username'), qsearch('svc_acct', {});
3031 open (USERNAMECACHE,">$dir/svc_acct.username.tmp")
3032 or die "can't open $dir/svc_acct.username.tmp: $!";
3033 print USERNAMECACHE join("\n", @all_username), "\n";
3034 close USERNAMECACHE or die "can't close $dir/svc_acct.username.tmp: $!";
3036 rename "$dir/svc_acct.username.tmp", "$dir/svc_acct.username";
3046 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
3047 open(USERNAMECACHE,"<$dir/svc_acct.username")
3048 or die "can't open $dir/svc_acct.username: $!";
3049 my @array = map { chomp; $_; } <USERNAMECACHE>;
3050 close USERNAMECACHE;
3054 =item append_fuzzyfiles USERNAME
3058 sub append_fuzzyfiles {
3059 my $username = shift;
3061 &check_and_rebuild_fuzzyfiles;
3063 use Fcntl qw(:flock);
3065 my $dir = $FS::UID::conf_dir. "/cache.". $FS::UID::datasrc;
3067 open(USERNAME,">>$dir/svc_acct.username")
3068 or die "can't open $dir/svc_acct.username: $!";
3069 flock(USERNAME,LOCK_EX)
3070 or die "can't lock $dir/svc_acct.username: $!";
3072 print USERNAME "$username\n";
3074 flock(USERNAME,LOCK_UN)
3075 or die "can't unlock $dir/svc_acct.username: $!";
3083 =item radius_usergroup_selector GROUPS_ARRAYREF [ SELECTNAME ]
3087 sub radius_usergroup_selector {
3088 my $sel_groups = shift;
3089 my %sel_groups = map { $_=>1 } @$sel_groups;
3091 my $selectname = shift || 'radius_usergroup';
3094 my $sth = $dbh->prepare(
3095 'SELECT DISTINCT(groupname) FROM radius_usergroup ORDER BY groupname'
3096 ) or die $dbh->errstr;
3097 $sth->execute() or die $sth->errstr;
3098 my @all_groups = map { $_->[0] } @{$sth->fetchall_arrayref};
3102 function ${selectname}_doadd(object) {
3103 var myvalue = object.${selectname}_add.value;
3104 var optionName = new Option(myvalue,myvalue,false,true);
3105 var length = object.$selectname.length;
3106 object.$selectname.options[length] = optionName;
3107 object.${selectname}_add.value = "";
3110 <SELECT MULTIPLE NAME="$selectname">
3113 foreach my $group ( @all_groups ) {
3114 $html .= qq(<OPTION VALUE="$group");
3115 if ( $sel_groups{$group} ) {
3116 $html .= ' SELECTED';
3117 $sel_groups{$group} = 0;
3119 $html .= ">$group</OPTION>\n";
3121 foreach my $group ( grep { $sel_groups{$_} } keys %sel_groups ) {
3122 $html .= qq(<OPTION VALUE="$group" SELECTED>$group</OPTION>\n);
3124 $html .= '</SELECT>';
3126 $html .= qq!<BR><INPUT TYPE="text" NAME="${selectname}_add">!.
3127 qq!<INPUT TYPE="button" VALUE="Add new group" onClick="${selectname}_doadd(this.form)">!;
3132 =item reached_threshold
3134 Performs some activities when svc_acct thresholds (such as number of seconds
3135 remaining) are reached.
3139 sub reached_threshold {
3142 my $svc_acct = qsearchs('svc_acct', { 'svcnum' => $opt{'svcnum'} } );
3143 die "Cannot find svc_acct with svcnum " . $opt{'svcnum'} unless $svc_acct;
3145 if ( $opt{'op'} eq '+' ){
3146 $svc_acct->setfield( $opt{'column'}.'_threshold',
3147 int($svc_acct->getfield($opt{'column'})
3148 * ( $conf->exists('svc_acct-usage_threshold')
3149 ? $conf->config('svc_acct-usage_threshold')/100
3154 my $error = $svc_acct->replace;
3155 die $error if $error;
3156 }elsif ( $opt{'op'} eq '-' ){
3158 my $threshold = $svc_acct->getfield( $opt{'column'}.'_threshold' );
3159 return '' if ($threshold eq '' );
3161 $svc_acct->setfield( $opt{'column'}.'_threshold', 0 );
3162 my $error = $svc_acct->replace;
3163 die $error if $error; # email next time, i guess
3165 if ( $warning_template ) {
3166 eval "use FS::Misc qw(send_email)";
3169 my $cust_pkg = $svc_acct->cust_svc->cust_pkg;
3170 my $cust_main = $cust_pkg->cust_main;
3172 my $to = join(', ', grep { $_ !~ /^(POST|FAX)$/ }
3173 $cust_main->invoicing_list,
3174 ($opt{'to'} ? $opt{'to'} : ())
3177 my $mimetype = $warning_mimetype;
3178 $mimetype .= '; charset="iso-8859-1"' unless $opt{mimetype} =~ /charset/;
3180 my $body = $warning_template->fill_in( HASH => {
3181 'custnum' => $cust_main->custnum,
3182 'username' => $svc_acct->username,
3183 'password' => $svc_acct->_password,
3184 'first' => $cust_main->first,
3185 'last' => $cust_main->getfield('last'),
3186 'pkg' => $cust_pkg->part_pkg->pkg,
3187 'column' => $opt{'column'},
3188 'amount' => $opt{'column'} =~/bytes/
3189 ? FS::UI::bytecount::display_bytecount($svc_acct->getfield($opt{'column'}))
3190 : $svc_acct->getfield($opt{'column'}),
3191 'threshold' => $opt{'column'} =~/bytes/
3192 ? FS::UI::bytecount::display_bytecount($threshold)
3197 my $error = send_email(
3198 'from' => $warning_from,
3200 'subject' => $warning_subject,
3201 'content-type' => $mimetype,
3202 'body' => [ map "$_\n", split("\n", $body) ],
3204 die $error if $error;
3207 die "unknown op: " . $opt{'op'};
3215 The $recref stuff in sub check should be cleaned up.
3217 The suspend, unsuspend and cancel methods update the database, but not the
3218 current object. This is probably a bug as it's unexpected and
3221 radius_usergroup_selector? putting web ui components in here? they should
3222 probably live somewhere else...
3224 insertion of RADIUS group stuff in insert could be done with child_objects now
3225 (would probably clean up export of them too)
3227 _op_usage and set_usage bypass the history... maybe they shouldn't
3231 L<FS::svc_Common>, edit/part_svc.cgi from an installed web interface,
3232 export.html from the base documentation, L<FS::Record>, L<FS::Conf>,
3233 L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, L<FS::queue>,
3234 L<freeside-queued>), L<FS::svc_acct_pop>,
3235 schema.html from the base documentation.