4 use vars qw( @ISA $noexport_hack $conf
5 $dir_prefix @shells $usernamemin
6 $usernamemax $passwordmin $passwordmax
7 $username_ampersand $username_letter $username_letterfirst
8 $username_noperiod $username_uppercase
14 use FS::UID qw( datasrc );
16 use FS::Record qw( qsearch qsearchs fields dbh );
22 use FS::cust_main_invoice;
26 use FS::radius_usergroup;
27 use FS::Msgcat qw(gettext);
29 @ISA = qw( FS::svc_Common );
31 #ask FS::UID to run this stuff for us later
32 $FS::UID::callback{'FS::svc_acct'} = sub {
34 $dir_prefix = $conf->config('home');
35 @shells = $conf->config('shells');
36 $usernamemin = $conf->config('usernamemin') || 2;
37 $usernamemax = $conf->config('usernamemax');
38 $passwordmin = $conf->config('passwordmin') || 6;
39 $passwordmax = $conf->config('passwordmax') || 8;
40 $username_letter = $conf->exists('username-letter');
41 $username_letterfirst = $conf->exists('username-letterfirst');
42 $username_noperiod = $conf->exists('username-noperiod');
43 $username_uppercase = $conf->exists('username-uppercase');
44 $username_ampersand = $conf->exists('username-ampersand');
45 $mydomain = $conf->config('domain');
47 $dirhash = $conf->config('dirhash') || 0;
50 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
51 @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '!', '.', ',' );
55 my ( $hashref, $cache ) = @_;
56 if ( $hashref->{'svc_acct_svcnum'} ) {
57 $self->{'_domsvc'} = FS::svc_domain->new( {
58 'svcnum' => $hashref->{'domsvc'},
59 'domain' => $hashref->{'svc_acct_domain'},
60 'catchall' => $hashref->{'svc_acct_catchall'},
67 FS::svc_acct - Object methods for svc_acct records
73 $record = new FS::svc_acct \%hash;
74 $record = new FS::svc_acct { 'column' => 'value' };
76 $error = $record->insert;
78 $error = $new_record->replace($old_record);
80 $error = $record->delete;
82 $error = $record->check;
84 $error = $record->suspend;
86 $error = $record->unsuspend;
88 $error = $record->cancel;
90 %hash = $record->radius;
92 %hash = $record->radius_reply;
94 %hash = $record->radius_check;
96 $domain = $record->domain;
98 $svc_domain = $record->svc_domain;
100 $email = $record->email;
102 $seconds_since = $record->seconds_since($timestamp);
106 An FS::svc_acct object represents an account. FS::svc_acct inherits from
107 FS::svc_Common. The following fields are currently supported:
111 =item svcnum - primary key (assigned automatcially for new accounts)
115 =item _password - generated if blank
117 =item sec_phrase - security phrase
119 =item popnum - Point of presence (see L<FS::svc_acct_pop>)
127 =item dir - set automatically if blank (and uid is not)
131 =item quota - (unimplementd)
133 =item slipip - IP address
137 =item domsvc - svcnum from svc_domain
139 =item radius_I<Radius_Attribute> - I<Radius-Attribute>
149 Creates a new account. To add the account to the database, see L<"insert">.
153 sub table { 'svc_acct'; }
157 Adds this account to the database. If there is an error, returns the error,
158 otherwise returns false.
160 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
161 defined. An FS::cust_svc record will be created and inserted.
163 The additional field I<usergroup> can optionally be defined; if so it should
164 contain an arrayref of group names. See L<FS::radius_usergroup>. (used in
165 sqlradius export only)
167 (TODOC: L<FS::queue> and L<freeside-queued>)
169 (TODOC: new exports! $noexport_hack)
177 local $SIG{HUP} = 'IGNORE';
178 local $SIG{INT} = 'IGNORE';
179 local $SIG{QUIT} = 'IGNORE';
180 local $SIG{TERM} = 'IGNORE';
181 local $SIG{TSTP} = 'IGNORE';
182 local $SIG{PIPE} = 'IGNORE';
184 my $oldAutoCommit = $FS::UID::AutoCommit;
185 local $FS::UID::AutoCommit = 0;
188 $error = $self->check;
189 return $error if $error;
191 return gettext('username_in_use'). ": ". $self->username
192 if qsearchs( 'svc_acct', { 'username' => $self->username,
193 'domsvc' => $self->domsvc,
196 if ( $self->svcnum ) {
197 my $cust_svc = qsearchs('cust_svc',{'svcnum'=>$self->svcnum});
198 unless ( $cust_svc ) {
199 $dbh->rollback if $oldAutoCommit;
200 return "no cust_svc record found for svcnum ". $self->svcnum;
202 $self->pkgnum($cust_svc->pkgnum);
203 $self->svcpart($cust_svc->svcpart);
206 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
207 return "Unknown svcpart" unless $part_svc;
209 if $part_svc->part_svc_column('uid')->columnflag ne 'F'
210 && qsearchs( 'svc_acct', { 'uid' => $self->uid } )
211 && $self->username !~ /^(hyla)?fax$/
214 $error = $self->SUPER::insert;
216 $dbh->rollback if $oldAutoCommit;
220 if ( $self->usergroup ) {
221 foreach my $groupname ( @{$self->usergroup} ) {
222 my $radius_usergroup = new FS::radius_usergroup ( {
223 svcnum => $self->svcnum,
224 groupname => $groupname,
226 my $error = $radius_usergroup->insert;
228 $dbh->rollback if $oldAutoCommit;
235 unless ( $noexport_hack ) {
236 foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
237 my $error = $part_export->export_insert($self);
239 $dbh->rollback if $oldAutoCommit;
240 return "exporting to ". $part_export->exporttype.
241 " (transaction rolled back): $error";
246 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
252 Deletes this account from the database. If there is an error, returns the
253 error, otherwise returns false.
255 The corresponding FS::cust_svc record will be deleted as well.
257 (TODOC: new exports! $noexport_hack)
264 if ( defined( $FS::Record::dbdef->table('svc_acct_sm') ) ) {
265 return "Can't delete an account which has (svc_acct_sm) mail aliases!"
266 if $self->uid && qsearch( 'svc_acct_sm', { 'domuid' => $self->uid } );
269 return "Can't delete an account which is a (svc_forward) source!"
270 if qsearch( 'svc_forward', { 'srcsvc' => $self->svcnum } );
272 return "Can't delete an account which is a (svc_forward) destination!"
273 if qsearch( 'svc_forward', { 'dstsvc' => $self->svcnum } );
275 return "Can't delete an account with (svc_www) web service!"
276 if qsearch( 'svc_www', { 'usersvc' => $self->usersvc } );
278 # what about records in session ? (they should refer to history table)
280 local $SIG{HUP} = 'IGNORE';
281 local $SIG{INT} = 'IGNORE';
282 local $SIG{QUIT} = 'IGNORE';
283 local $SIG{TERM} = 'IGNORE';
284 local $SIG{TSTP} = 'IGNORE';
285 local $SIG{PIPE} = 'IGNORE';
287 my $oldAutoCommit = $FS::UID::AutoCommit;
288 local $FS::UID::AutoCommit = 0;
291 foreach my $cust_main_invoice (
292 qsearch( 'cust_main_invoice', { 'dest' => $self->svcnum } )
294 unless ( defined($cust_main_invoice) ) {
295 warn "WARNING: something's wrong with qsearch";
298 my %hash = $cust_main_invoice->hash;
299 $hash{'dest'} = $self->email;
300 my $new = new FS::cust_main_invoice \%hash;
301 my $error = $new->replace($cust_main_invoice);
303 $dbh->rollback if $oldAutoCommit;
308 foreach my $svc_domain (
309 qsearch( 'svc_domain', { 'catchall' => $self->svcnum } )
311 my %hash = new FS::svc_domain->hash;
312 $hash{'catchall'} = '';
313 my $new = new FS::svc_domain \%hash;
314 my $error = $new->replace($svc_domain);
316 $dbh->rollback if $oldAutoCommit;
321 foreach my $radius_usergroup (
322 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } )
324 my $error = $radius_usergroup->delete;
326 $dbh->rollback if $oldAutoCommit;
331 my $part_svc = $self->cust_svc->part_svc;
333 my $error = $self->SUPER::delete;
335 $dbh->rollback if $oldAutoCommit;
340 unless ( $noexport_hack ) {
341 foreach my $part_export ( $part_svc->part_export ) {
342 my $error = $part_export->export_delete($self);
344 $dbh->rollback if $oldAutoCommit;
345 return "exporting to ". $part_export->exporttype.
346 " (transaction rolled back): $error";
351 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
355 =item replace OLD_RECORD
357 Replaces OLD_RECORD with this one in the database. If there is an error,
358 returns the error, otherwise returns false.
360 The additional field I<usergroup> can optionally be defined; if so it should
361 contain an arrayref of group names. See L<FS::radius_usergroup>. (used in
362 sqlradius export only)
367 my ( $new, $old ) = ( shift, shift );
370 return "Username in use"
371 if $old->username ne $new->username &&
372 qsearchs( 'svc_acct', { 'username' => $new->username,
373 'domsvc' => $new->domsvc,
376 #no warnings 'numeric'; #alas, a 5.006-ism
378 return "Can't change uid!" if $old->uid != $new->uid;
381 #change homdir when we change username
382 $new->setfield('dir', '') if $old->username ne $new->username;
384 local $SIG{HUP} = 'IGNORE';
385 local $SIG{INT} = 'IGNORE';
386 local $SIG{QUIT} = 'IGNORE';
387 local $SIG{TERM} = 'IGNORE';
388 local $SIG{TSTP} = 'IGNORE';
389 local $SIG{PIPE} = 'IGNORE';
391 my $oldAutoCommit = $FS::UID::AutoCommit;
392 local $FS::UID::AutoCommit = 0;
395 $error = $new->SUPER::replace($old);
397 $dbh->rollback if $oldAutoCommit;
398 return $error if $error;
401 $old->usergroup( [ $old->radius_groups ] );
402 if ( $new->usergroup ) {
403 #(sorta) false laziness with FS::part_export::sqlradius::_export_replace
404 my @newgroups = @{$new->usergroup};
405 foreach my $oldgroup ( @{$old->usergroup} ) {
406 if ( grep { $oldgroup eq $_ } @newgroups ) {
407 @newgroups = grep { $oldgroup ne $_ } @newgroups;
410 my $radius_usergroup = qsearchs('radius_usergroup', {
411 svcnum => $old->svcnum,
412 groupname => $oldgroup,
414 my $error = $radius_usergroup->delete;
416 $dbh->rollback if $oldAutoCommit;
417 return "error deleting radius_usergroup $oldgroup: $error";
421 foreach my $newgroup ( @newgroups ) {
422 my $radius_usergroup = new FS::radius_usergroup ( {
423 svcnum => $new->svcnum,
424 groupname => $newgroup,
426 my $error = $radius_usergroup->insert;
428 $dbh->rollback if $oldAutoCommit;
429 return "error adding radius_usergroup $newgroup: $error";
436 unless ( $noexport_hack ) {
437 foreach my $part_export ( $new->cust_svc->part_svc->part_export ) {
438 my $error = $part_export->export_replace($new,$old);
440 $dbh->rollback if $oldAutoCommit;
441 return "exporting to ". $part_export->exporttype.
442 " (transaction rolled back): $error";
447 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
453 Suspends this account by prefixing *SUSPENDED* to the password. If there is an
454 error, returns the error, otherwise returns false.
456 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
462 my %hash = $self->hash;
463 unless ( $hash{_password} =~ /^\*SUSPENDED\* /
464 || $hash{_password} eq '*'
466 $hash{_password} = '*SUSPENDED* '.$hash{_password};
467 my $new = new FS::svc_acct ( \%hash );
468 $new->replace($self);
470 ''; #no error (already suspended)
476 Unsuspends this account by removing *SUSPENDED* from the password. If there is
477 an error, returns the error, otherwise returns false.
479 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
485 my %hash = $self->hash;
486 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
487 $hash{_password} = $1;
488 my $new = new FS::svc_acct ( \%hash );
489 $new->replace($self);
491 ''; #no error (already unsuspended)
497 Just returns false (no error) for now.
499 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
503 Checks all fields to make sure this is a valid service. If there is an error,
504 returns the error, otherwise returns false. Called by the insert and replace
507 Sets any fixed values; see L<FS::part_svc>.
514 my($recref) = $self->hashref;
516 my $x = $self->setfixed;
517 return $x unless ref($x);
520 if ( $part_svc->part_svc_column('usergroup')->columnflag eq "F" ) {
522 [ split(',', $part_svc->part_svc_column('usergroup')->columnvalue) ] );
525 my $error = $self->ut_numbern('svcnum')
526 || $self->ut_number('domsvc')
527 || $self->ut_textn('sec_phrase')
529 return $error if $error;
531 my $ulen = $usernamemax || $self->dbdef_table->column('username')->length;
532 if ( $username_uppercase ) {
533 $recref->{username} =~ /^([a-z0-9_\-\.\&]{$usernamemin,$ulen})$/i
534 or return gettext('illegal_username'). ": ". $recref->{username};
535 $recref->{username} = $1;
537 $recref->{username} =~ /^([a-z0-9_\-\.\&]{$usernamemin,$ulen})$/
538 or return gettext('illegal_username'). ": ". $recref->{username};
539 $recref->{username} = $1;
542 if ( $username_letterfirst ) {
543 $recref->{username} =~ /^[a-z]/ or return gettext('illegal_username');
544 } elsif ( $username_letter ) {
545 $recref->{username} =~ /[a-z]/ or return gettext('illegal_username');
547 if ( $username_noperiod ) {
548 $recref->{username} =~ /\./ and return gettext('illegal_username');
550 unless ( $username_ampersand ) {
551 $recref->{username} =~ /\&/ and return gettext('illegal_username');
554 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum: ".$recref->{popnum};
555 $recref->{popnum} = $1;
556 return "Unknown popnum" unless
557 ! $recref->{popnum} ||
558 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
560 unless ( $part_svc->part_svc_column('uid')->columnflag eq 'F' ) {
562 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
563 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
565 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
566 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
567 #not all systems use gid=uid
568 #you can set a fixed gid in part_svc
570 return "Only root can have uid 0"
571 if $recref->{uid} == 0 && $recref->{username} ne 'root';
573 # $error = $self->ut_textn('finger');
574 # return $error if $error;
575 $self->getfield('finger') =~
576 /^([\w \t\!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\*\<\>]*)$/
577 or return "Illegal finger: ". $self->getfield('finger');
578 $self->setfield('finger', $1);
580 $recref->{dir} =~ /^([\/\w\-\.\&]*)$/
581 or return "Illegal directory";
583 return "Illegal directory"
584 if $recref->{dir} =~ /(^|\/)\.+(\/|$)/; #no .. component
585 return "Illegal directory"
586 if $recref->{dir} =~ /\&/ && ! $username_ampersand;
587 unless ( $recref->{dir} ) {
588 $recref->{dir} = $dir_prefix . '/';
589 if ( $dirhash > 0 ) {
590 for my $h ( 1 .. $dirhash ) {
591 $recref->{dir} .= substr($recref->{username}, $h-1, 1). '/';
593 } elsif ( $dirhash < 0 ) {
594 for my $h ( reverse $dirhash .. -1 ) {
595 $recref->{dir} .= substr($recref->{username}, $h, 1). '/';
598 $recref->{dir} .= $recref->{username};
602 unless ( $recref->{username} eq 'sync' ) {
603 if ( grep $_ eq $recref->{shell}, @shells ) {
604 $recref->{shell} = (grep $_ eq $recref->{shell}, @shells)[0];
606 return "Illegal shell \`". $self->shell. "\'; ".
607 $conf->dir. "/shells contains: @shells";
610 $recref->{shell} = '/bin/sync';
613 $recref->{quota} =~ /^(\d*)$/ or return "Illegal quota (unimplemented)";
614 $recref->{quota} = $1;
617 $recref->{gid} ne '' ?
618 return "Can't have gid without uid" : ( $recref->{gid}='' );
619 $recref->{finger} ne '' ?
620 return "Can't have finger-name without uid" : ( $recref->{finger}='' );
621 $recref->{dir} ne '' ?
622 return "Can't have directory without uid" : ( $recref->{dir}='' );
623 $recref->{shell} ne '' ?
624 return "Can't have shell without uid" : ( $recref->{shell}='' );
625 $recref->{quota} ne '' ?
626 return "Can't have quota without uid" : ( $recref->{quota}='' );
629 unless ( $part_svc->part_svc_column('slipip')->columnflag eq 'F' ) {
630 unless ( $recref->{slipip} eq '0e0' ) {
631 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
632 or return "Illegal slipip". $self->slipip;
633 $recref->{slipip} = $1;
635 $recref->{slipip} = '0e0';
640 #arbitrary RADIUS stuff; allow ut_textn for now
641 foreach ( grep /^radius_/, fields('svc_acct') ) {
645 #generate a password if it is blank
646 $recref->{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) )
647 unless ( $recref->{_password} );
649 #if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,16})$/ ) {
650 if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{$passwordmin,$passwordmax})$/ ) {
651 $recref->{_password} = $1.$3;
652 #uncomment this to encrypt password immediately upon entry, or run
653 #bin/crypt_pw in cron to give new users a window during which their
654 #password is available to techs, for faxing, etc. (also be aware of
656 #$recref->{password} = $1.
657 # crypt($3,$saltset[int(rand(64))].$saltset[int(rand(64))]
659 } elsif ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([\w\.\/\$]{13,34})$/ ) {
660 $recref->{_password} = $1.$3;
661 } elsif ( $recref->{_password} eq '*' ) {
662 $recref->{_password} = '*';
663 } elsif ( $recref->{_password} eq '!!' ) {
664 $recref->{_password} = '!!';
666 #return "Illegal password";
667 return gettext('illegal_password'). ": ". $recref->{_password};
675 Depriciated, use radius_reply instead.
680 carp "FS::svc_acct::radius depriciated, use radius_reply";
686 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
687 reply attributes of this record.
689 Note that this is now the preferred method for reading RADIUS attributes -
690 accessing the columns directly is discouraged, as the column names are
691 expected to change in the future.
700 my($column, $attrib) = ($1, $2);
701 #$attrib =~ s/_/\-/g;
702 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
703 } grep { /^radius_/ && $self->getfield($_) } fields( $self->table );
704 if ( $self->ip && $self->ip ne '0e0' ) {
705 $reply{'Framed-IP-Address'} = $self->ip;
712 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
713 check attributes of this record.
715 Note that this is now the preferred method for reading RADIUS attributes -
716 accessing the columns directly is discouraged, as the column names are
717 expected to change in the future.
723 ( 'Password' => $self->_password,
726 my($column, $attrib) = ($1, $2);
727 #$attrib =~ s/_/\-/g;
728 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
729 } grep { /^rc_/ && $self->getfield($_) } fields( $self->table )
735 Returns the domain associated with this account.
741 if ( $self->domsvc ) {
742 #$self->svc_domain->domain;
743 my $svc_domain = $self->svc_domain
744 or die "no svc_domain.svcnum for svc_acct.domsvc ". $self->domsvc;
747 $mydomain or die "svc_acct.domsvc is null and no legacy domain config file";
753 Returns the FS::svc_domain record for this account's domain (see
762 : qsearchs( 'svc_domain', { 'svcnum' => $self->domsvc } );
767 Returns the FS::cust_svc record for this account (see L<FS::cust_svc>).
771 qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
776 Returns an email address associated with the account.
782 $self->username. '@'. $self->domain;
785 =item seconds_since TIMESTAMP
787 Returns the number of seconds this account has been online since TIMESTAMP.
790 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
791 L<Time::Local> and L<Date::Parse> for conversion functions.
795 #note: POD here, implementation in FS::cust_svc
798 $self->cust_svc->seconds_since(@_);
803 Returns all RADIUS groups for this account (see L<FS::radius_usergroup>).
809 map { $_->groupname }
810 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } );
817 =item radius_usergroup_selector GROUPS_ARRAYREF [ SELECTNAME ]
821 sub radius_usergroup_selector {
822 my $sel_groups = shift;
823 my %sel_groups = map { $_=>1 } @$sel_groups;
825 my $selectname = shift || 'radius_usergroup';
828 my $sth = $dbh->prepare(
829 'SELECT DISTINCT(groupname) FROM radius_usergroup ORDER BY groupname'
830 ) or die $dbh->errstr;
831 $sth->execute() or die $sth->errstr;
832 my @all_groups = map { $_->[0] } @{$sth->fetchall_arrayref};
836 function ${selectname}_doadd(object) {
837 var myvalue = object.${selectname}_add.value;
838 var optionName = new Option(myvalue,myvalue,false,true);
839 var length = object.$selectname.length;
840 object.$selectname.options[length] = optionName;
841 object.${selectname}_add.value = "";
844 <SELECT MULTIPLE NAME="$selectname">
847 foreach my $group ( @all_groups ) {
849 if ( $sel_groups{$group} ) {
850 $html .= ' SELECTED';
851 $sel_groups{$group} = 0;
853 $html .= ">$group</OPTION>\n";
855 foreach my $group ( grep { $sel_groups{$_} } keys %sel_groups ) {
856 $html .= "<OPTION SELECTED>$group</OPTION>\n";
858 $html .= '</SELECT>';
860 $html .= qq!<BR><INPUT TYPE="text" NAME="${selectname}_add">!.
861 qq!<INPUT TYPE="button" VALUE="Add new group" onClick="${selectname}_doadd(this.form)">!;
868 The $recref stuff in sub check should be cleaned up.
870 The suspend, unsuspend and cancel methods update the database, but not the
871 current object. This is probably a bug as it's unexpected and
874 radius_usergroup_selector? putting web ui components in here? they should
875 probably live somewhere else...
879 L<FS::svc_Common>, edit/part_svc.cgi from an installed web interface,
880 export.html from the base documentation, L<FS::Record>, L<FS::Conf>,
881 L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, L<FS::queue>,
882 L<freeside-queued>), L<Net::SSH>, L<ssh>, L<FS::svc_acct_pop>,
883 schema.html from the base documentation.