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_nounderscore $username_nodash
15 use FS::UID qw( datasrc );
17 use FS::Record qw( qsearch qsearchs fields dbh );
23 use FS::cust_main_invoice;
27 use FS::radius_usergroup;
28 use FS::Msgcat qw(gettext);
30 @ISA = qw( FS::svc_Common );
32 #ask FS::UID to run this stuff for us later
33 $FS::UID::callback{'FS::svc_acct'} = sub {
35 $dir_prefix = $conf->config('home');
36 @shells = $conf->config('shells');
37 $usernamemin = $conf->config('usernamemin') || 2;
38 $usernamemax = $conf->config('usernamemax');
39 $passwordmin = $conf->config('passwordmin') || 6;
40 $passwordmax = $conf->config('passwordmax') || 8;
41 $username_letter = $conf->exists('username-letter');
42 $username_letterfirst = $conf->exists('username-letterfirst');
43 $username_noperiod = $conf->exists('username-noperiod');
44 $username_nounderscore = $conf->exists('username-nounderscore');
45 $username_nodash = $conf->exists('username-nodash');
46 $username_uppercase = $conf->exists('username-uppercase');
47 $username_ampersand = $conf->exists('username-ampersand');
48 $mydomain = $conf->config('domain');
50 $dirhash = $conf->config('dirhash') || 0;
53 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
54 @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '!', '.', ',' );
58 my ( $hashref, $cache ) = @_;
59 if ( $hashref->{'svc_acct_svcnum'} ) {
60 $self->{'_domsvc'} = FS::svc_domain->new( {
61 'svcnum' => $hashref->{'domsvc'},
62 'domain' => $hashref->{'svc_acct_domain'},
63 'catchall' => $hashref->{'svc_acct_catchall'},
70 FS::svc_acct - Object methods for svc_acct records
76 $record = new FS::svc_acct \%hash;
77 $record = new FS::svc_acct { 'column' => 'value' };
79 $error = $record->insert;
81 $error = $new_record->replace($old_record);
83 $error = $record->delete;
85 $error = $record->check;
87 $error = $record->suspend;
89 $error = $record->unsuspend;
91 $error = $record->cancel;
93 %hash = $record->radius;
95 %hash = $record->radius_reply;
97 %hash = $record->radius_check;
99 $domain = $record->domain;
101 $svc_domain = $record->svc_domain;
103 $email = $record->email;
105 $seconds_since = $record->seconds_since($timestamp);
109 An FS::svc_acct object represents an account. FS::svc_acct inherits from
110 FS::svc_Common. The following fields are currently supported:
114 =item svcnum - primary key (assigned automatcially for new accounts)
118 =item _password - generated if blank
120 =item sec_phrase - security phrase
122 =item popnum - Point of presence (see L<FS::svc_acct_pop>)
130 =item dir - set automatically if blank (and uid is not)
134 =item quota - (unimplementd)
136 =item slipip - IP address
140 =item domsvc - svcnum from svc_domain
142 =item radius_I<Radius_Attribute> - I<Radius-Attribute>
152 Creates a new account. To add the account to the database, see L<"insert">.
156 sub table { 'svc_acct'; }
160 Adds this account to the database. If there is an error, returns the error,
161 otherwise returns false.
163 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
164 defined. An FS::cust_svc record will be created and inserted.
166 The additional field I<usergroup> can optionally be defined; if so it should
167 contain an arrayref of group names. See L<FS::radius_usergroup>. (used in
168 sqlradius export only)
170 (TODOC: L<FS::queue> and L<freeside-queued>)
172 (TODOC: new exports! $noexport_hack)
180 local $SIG{HUP} = 'IGNORE';
181 local $SIG{INT} = 'IGNORE';
182 local $SIG{QUIT} = 'IGNORE';
183 local $SIG{TERM} = 'IGNORE';
184 local $SIG{TSTP} = 'IGNORE';
185 local $SIG{PIPE} = 'IGNORE';
187 my $oldAutoCommit = $FS::UID::AutoCommit;
188 local $FS::UID::AutoCommit = 0;
191 $error = $self->check;
192 return $error if $error;
194 return gettext('username_in_use'). ": ". $self->username
195 if qsearchs( 'svc_acct', { 'username' => $self->username,
196 'domsvc' => $self->domsvc,
199 if ( $self->svcnum ) {
200 my $cust_svc = qsearchs('cust_svc',{'svcnum'=>$self->svcnum});
201 unless ( $cust_svc ) {
202 $dbh->rollback if $oldAutoCommit;
203 return "no cust_svc record found for svcnum ". $self->svcnum;
205 $self->pkgnum($cust_svc->pkgnum);
206 $self->svcpart($cust_svc->svcpart);
209 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
210 return "Unknown svcpart" unless $part_svc;
212 if $part_svc->part_svc_column('uid')->columnflag ne 'F'
213 && qsearchs( 'svc_acct', { 'uid' => $self->uid } )
214 && $self->username !~ /^(hyla)?fax$/
215 && $self->username !~ /^toor$/ #FreeBSD
218 $error = $self->SUPER::insert;
220 $dbh->rollback if $oldAutoCommit;
224 if ( $self->usergroup ) {
225 foreach my $groupname ( @{$self->usergroup} ) {
226 my $radius_usergroup = new FS::radius_usergroup ( {
227 svcnum => $self->svcnum,
228 groupname => $groupname,
230 my $error = $radius_usergroup->insert;
232 $dbh->rollback if $oldAutoCommit;
238 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
244 Deletes this account from the database. If there is an error, returns the
245 error, otherwise returns false.
247 The corresponding FS::cust_svc record will be deleted as well.
249 (TODOC: new exports! $noexport_hack)
256 if ( defined( $FS::Record::dbdef->table('svc_acct_sm') ) ) {
257 return "Can't delete an account which has (svc_acct_sm) mail aliases!"
258 if $self->uid && qsearch( 'svc_acct_sm', { 'domuid' => $self->uid } );
261 return "Can't delete an account which is a (svc_forward) source!"
262 if qsearch( 'svc_forward', { 'srcsvc' => $self->svcnum } );
264 return "Can't delete an account which is a (svc_forward) destination!"
265 if qsearch( 'svc_forward', { 'dstsvc' => $self->svcnum } );
267 return "Can't delete an account with (svc_www) web service!"
268 if qsearch( 'svc_www', { 'usersvc' => $self->usersvc } );
270 # what about records in session ? (they should refer to history table)
272 local $SIG{HUP} = 'IGNORE';
273 local $SIG{INT} = 'IGNORE';
274 local $SIG{QUIT} = 'IGNORE';
275 local $SIG{TERM} = 'IGNORE';
276 local $SIG{TSTP} = 'IGNORE';
277 local $SIG{PIPE} = 'IGNORE';
279 my $oldAutoCommit = $FS::UID::AutoCommit;
280 local $FS::UID::AutoCommit = 0;
283 foreach my $cust_main_invoice (
284 qsearch( 'cust_main_invoice', { 'dest' => $self->svcnum } )
286 unless ( defined($cust_main_invoice) ) {
287 warn "WARNING: something's wrong with qsearch";
290 my %hash = $cust_main_invoice->hash;
291 $hash{'dest'} = $self->email;
292 my $new = new FS::cust_main_invoice \%hash;
293 my $error = $new->replace($cust_main_invoice);
295 $dbh->rollback if $oldAutoCommit;
300 foreach my $svc_domain (
301 qsearch( 'svc_domain', { 'catchall' => $self->svcnum } )
303 my %hash = new FS::svc_domain->hash;
304 $hash{'catchall'} = '';
305 my $new = new FS::svc_domain \%hash;
306 my $error = $new->replace($svc_domain);
308 $dbh->rollback if $oldAutoCommit;
313 foreach my $radius_usergroup (
314 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } )
316 my $error = $radius_usergroup->delete;
318 $dbh->rollback if $oldAutoCommit;
323 my $part_svc = $self->cust_svc->part_svc;
325 my $error = $self->SUPER::delete;
327 $dbh->rollback if $oldAutoCommit;
331 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
335 =item replace OLD_RECORD
337 Replaces OLD_RECORD with this one in the database. If there is an error,
338 returns the error, otherwise returns false.
340 The additional field I<usergroup> can optionally be defined; if so it should
341 contain an arrayref of group names. See L<FS::radius_usergroup>. (used in
342 sqlradius export only)
347 my ( $new, $old ) = ( shift, shift );
350 return "Username in use"
351 if $old->username ne $new->username &&
352 qsearchs( 'svc_acct', { 'username' => $new->username,
353 'domsvc' => $new->domsvc,
356 #no warnings 'numeric'; #alas, a 5.006-ism
358 return "Can't change uid!" if $old->uid != $new->uid;
361 #change homdir when we change username
362 $new->setfield('dir', '') if $old->username ne $new->username;
364 local $SIG{HUP} = 'IGNORE';
365 local $SIG{INT} = 'IGNORE';
366 local $SIG{QUIT} = 'IGNORE';
367 local $SIG{TERM} = 'IGNORE';
368 local $SIG{TSTP} = 'IGNORE';
369 local $SIG{PIPE} = 'IGNORE';
371 my $oldAutoCommit = $FS::UID::AutoCommit;
372 local $FS::UID::AutoCommit = 0;
375 $error = $new->SUPER::replace($old);
377 $dbh->rollback if $oldAutoCommit;
378 return $error if $error;
381 $old->usergroup( [ $old->radius_groups ] );
382 if ( $new->usergroup ) {
383 #(sorta) false laziness with FS::part_export::sqlradius::_export_replace
384 my @newgroups = @{$new->usergroup};
385 foreach my $oldgroup ( @{$old->usergroup} ) {
386 if ( grep { $oldgroup eq $_ } @newgroups ) {
387 @newgroups = grep { $oldgroup ne $_ } @newgroups;
390 my $radius_usergroup = qsearchs('radius_usergroup', {
391 svcnum => $old->svcnum,
392 groupname => $oldgroup,
394 my $error = $radius_usergroup->delete;
396 $dbh->rollback if $oldAutoCommit;
397 return "error deleting radius_usergroup $oldgroup: $error";
401 foreach my $newgroup ( @newgroups ) {
402 my $radius_usergroup = new FS::radius_usergroup ( {
403 svcnum => $new->svcnum,
404 groupname => $newgroup,
406 my $error = $radius_usergroup->insert;
408 $dbh->rollback if $oldAutoCommit;
409 return "error adding radius_usergroup $newgroup: $error";
415 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
421 Suspends this account by prefixing *SUSPENDED* to the password. If there is an
422 error, returns the error, otherwise returns false.
424 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
430 my %hash = $self->hash;
431 unless ( $hash{_password} =~ /^\*SUSPENDED\* /
432 || $hash{_password} eq '*'
434 $hash{_password} = '*SUSPENDED* '.$hash{_password};
435 my $new = new FS::svc_acct ( \%hash );
436 $new->replace($self);
438 ''; #no error (already suspended)
444 Unsuspends this account by removing *SUSPENDED* from the password. If there is
445 an error, returns the error, otherwise returns false.
447 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
453 my %hash = $self->hash;
454 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
455 $hash{_password} = $1;
456 my $new = new FS::svc_acct ( \%hash );
457 $new->replace($self);
459 ''; #no error (already unsuspended)
465 Just returns false (no error) for now.
467 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
471 Checks all fields to make sure this is a valid service. If there is an error,
472 returns the error, otherwise returns false. Called by the insert and replace
475 Sets any fixed values; see L<FS::part_svc>.
482 my($recref) = $self->hashref;
484 my $x = $self->setfixed;
485 return $x unless ref($x);
488 if ( $part_svc->part_svc_column('usergroup')->columnflag eq "F" ) {
490 [ split(',', $part_svc->part_svc_column('usergroup')->columnvalue) ] );
493 my $error = $self->ut_numbern('svcnum')
494 || $self->ut_number('domsvc')
495 || $self->ut_textn('sec_phrase')
497 return $error if $error;
499 my $ulen = $usernamemax || $self->dbdef_table->column('username')->length;
500 if ( $username_uppercase ) {
501 $recref->{username} =~ /^([a-z0-9_\-\.\&]{$usernamemin,$ulen})$/i
502 or return gettext('illegal_username'). " ($usernamemin-$ulen): ". $recref->{username};
503 $recref->{username} = $1;
505 $recref->{username} =~ /^([a-z0-9_\-\.\&]{$usernamemin,$ulen})$/
506 or return gettext('illegal_username'). " ($usernamemin-$ulen): ". $recref->{username};
507 $recref->{username} = $1;
510 if ( $username_letterfirst ) {
511 $recref->{username} =~ /^[a-z]/ or return gettext('illegal_username');
512 } elsif ( $username_letter ) {
513 $recref->{username} =~ /[a-z]/ or return gettext('illegal_username');
515 if ( $username_noperiod ) {
516 $recref->{username} =~ /\./ and return gettext('illegal_username');
518 if ( $username_nounderscore ) {
519 $recref->{username} =~ /_/ and return gettext('illegal_username');
521 if ( $username_nodash ) {
522 $recref->{username} =~ /\-/ and return gettext('illegal_username');
524 unless ( $username_ampersand ) {
525 $recref->{username} =~ /\&/ and return gettext('illegal_username');
528 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum: ".$recref->{popnum};
529 $recref->{popnum} = $1;
530 return "Unknown popnum" unless
531 ! $recref->{popnum} ||
532 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
534 unless ( $part_svc->part_svc_column('uid')->columnflag eq 'F' ) {
536 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
537 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
539 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
540 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
541 #not all systems use gid=uid
542 #you can set a fixed gid in part_svc
544 return "Only root can have uid 0"
545 if $recref->{uid} == 0
546 && $recref->{username} ne 'root'
547 && $recref->{username} ne 'toor';
549 # $error = $self->ut_textn('finger');
550 # return $error if $error;
551 $self->getfield('finger') =~
552 /^([\w \t\!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\*\<\>]*)$/
553 or return "Illegal finger: ". $self->getfield('finger');
554 $self->setfield('finger', $1);
556 $recref->{dir} =~ /^([\/\w\-\.\&]*)$/
557 or return "Illegal directory";
559 return "Illegal directory"
560 if $recref->{dir} =~ /(^|\/)\.+(\/|$)/; #no .. component
561 return "Illegal directory"
562 if $recref->{dir} =~ /\&/ && ! $username_ampersand;
563 unless ( $recref->{dir} ) {
564 $recref->{dir} = $dir_prefix . '/';
565 if ( $dirhash > 0 ) {
566 for my $h ( 1 .. $dirhash ) {
567 $recref->{dir} .= substr($recref->{username}, $h-1, 1). '/';
569 } elsif ( $dirhash < 0 ) {
570 for my $h ( reverse $dirhash .. -1 ) {
571 $recref->{dir} .= substr($recref->{username}, $h, 1). '/';
574 $recref->{dir} .= $recref->{username};
578 unless ( $recref->{username} eq 'sync' ) {
579 if ( grep $_ eq $recref->{shell}, @shells ) {
580 $recref->{shell} = (grep $_ eq $recref->{shell}, @shells)[0];
582 return "Illegal shell \`". $self->shell. "\'; ".
583 $conf->dir. "/shells contains: @shells";
586 $recref->{shell} = '/bin/sync';
589 $recref->{quota} =~ /^(\d*)$/ or return "Illegal quota (unimplemented)";
590 $recref->{quota} = $1;
593 $recref->{gid} ne '' ?
594 return "Can't have gid without uid" : ( $recref->{gid}='' );
595 $recref->{finger} ne '' ?
596 return "Can't have finger-name without uid" : ( $recref->{finger}='' );
597 $recref->{dir} ne '' ?
598 return "Can't have directory without uid" : ( $recref->{dir}='' );
599 $recref->{shell} ne '' ?
600 return "Can't have shell without uid" : ( $recref->{shell}='' );
601 $recref->{quota} ne '' ?
602 return "Can't have quota without uid" : ( $recref->{quota}='' );
605 unless ( $part_svc->part_svc_column('slipip')->columnflag eq 'F' ) {
606 unless ( $recref->{slipip} eq '0e0' ) {
607 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
608 or return "Illegal slipip". $self->slipip;
609 $recref->{slipip} = $1;
611 $recref->{slipip} = '0e0';
616 #arbitrary RADIUS stuff; allow ut_textn for now
617 foreach ( grep /^radius_/, fields('svc_acct') ) {
621 #generate a password if it is blank
622 $recref->{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) )
623 unless ( $recref->{_password} );
625 #if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,16})$/ ) {
626 if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{$passwordmin,$passwordmax})$/ ) {
627 $recref->{_password} = $1.$3;
628 #uncomment this to encrypt password immediately upon entry, or run
629 #bin/crypt_pw in cron to give new users a window during which their
630 #password is available to techs, for faxing, etc. (also be aware of
632 #$recref->{password} = $1.
633 # crypt($3,$saltset[int(rand(64))].$saltset[int(rand(64))]
635 } elsif ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([\w\.\/\$]{13,34})$/ ) {
636 $recref->{_password} = $1.$3;
637 } elsif ( $recref->{_password} eq '*' ) {
638 $recref->{_password} = '*';
639 } elsif ( $recref->{_password} eq '!!' ) {
640 $recref->{_password} = '!!';
642 #return "Illegal password";
643 return gettext('illegal_password'). "$passwordmin-$passwordmax".
644 FS::Msgcat::_gettext('illegal_password_characters').
645 ": ". $recref->{_password};
653 Depriciated, use radius_reply instead.
658 carp "FS::svc_acct::radius depriciated, use radius_reply";
664 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
665 reply attributes of this record.
667 Note that this is now the preferred method for reading RADIUS attributes -
668 accessing the columns directly is discouraged, as the column names are
669 expected to change in the future.
678 my($column, $attrib) = ($1, $2);
679 #$attrib =~ s/_/\-/g;
680 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
681 } grep { /^radius_/ && $self->getfield($_) } fields( $self->table );
682 if ( $self->ip && $self->ip ne '0e0' ) {
683 $reply{'Framed-IP-Address'} = $self->ip;
690 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
691 check attributes of this record.
693 Note that this is now the preferred method for reading RADIUS attributes -
694 accessing the columns directly is discouraged, as the column names are
695 expected to change in the future.
701 ( 'Password' => $self->_password,
704 my($column, $attrib) = ($1, $2);
705 #$attrib =~ s/_/\-/g;
706 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
707 } grep { /^rc_/ && $self->getfield($_) } fields( $self->table )
713 Returns the domain associated with this account.
719 if ( $self->domsvc ) {
720 #$self->svc_domain->domain;
721 my $svc_domain = $self->svc_domain
722 or die "no svc_domain.svcnum for svc_acct.domsvc ". $self->domsvc;
725 $mydomain or die "svc_acct.domsvc is null and no legacy domain config file";
731 Returns the FS::svc_domain record for this account's domain (see
740 : qsearchs( 'svc_domain', { 'svcnum' => $self->domsvc } );
745 Returns the FS::cust_svc record for this account (see L<FS::cust_svc>).
749 qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
754 Returns an email address associated with the account.
760 $self->username. '@'. $self->domain;
763 =item seconds_since TIMESTAMP
765 Returns the number of seconds this account has been online since TIMESTAMP.
768 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
769 L<Time::Local> and L<Date::Parse> for conversion functions.
773 #note: POD here, implementation in FS::cust_svc
776 $self->cust_svc->seconds_since(@_);
781 Returns all RADIUS groups for this account (see L<FS::radius_usergroup>).
787 map { $_->groupname }
788 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } );
795 =item radius_usergroup_selector GROUPS_ARRAYREF [ SELECTNAME ]
799 sub radius_usergroup_selector {
800 my $sel_groups = shift;
801 my %sel_groups = map { $_=>1 } @$sel_groups;
803 my $selectname = shift || 'radius_usergroup';
806 my $sth = $dbh->prepare(
807 'SELECT DISTINCT(groupname) FROM radius_usergroup ORDER BY groupname'
808 ) or die $dbh->errstr;
809 $sth->execute() or die $sth->errstr;
810 my @all_groups = map { $_->[0] } @{$sth->fetchall_arrayref};
814 function ${selectname}_doadd(object) {
815 var myvalue = object.${selectname}_add.value;
816 var optionName = new Option(myvalue,myvalue,false,true);
817 var length = object.$selectname.length;
818 object.$selectname.options[length] = optionName;
819 object.${selectname}_add.value = "";
822 <SELECT MULTIPLE NAME="$selectname">
825 foreach my $group ( @all_groups ) {
827 if ( $sel_groups{$group} ) {
828 $html .= ' SELECTED';
829 $sel_groups{$group} = 0;
831 $html .= ">$group</OPTION>\n";
833 foreach my $group ( grep { $sel_groups{$_} } keys %sel_groups ) {
834 $html .= "<OPTION SELECTED>$group</OPTION>\n";
836 $html .= '</SELECT>';
838 $html .= qq!<BR><INPUT TYPE="text" NAME="${selectname}_add">!.
839 qq!<INPUT TYPE="button" VALUE="Add new group" onClick="${selectname}_doadd(this.form)">!;
846 The $recref stuff in sub check should be cleaned up.
848 The suspend, unsuspend and cancel methods update the database, but not the
849 current object. This is probably a bug as it's unexpected and
852 radius_usergroup_selector? putting web ui components in here? they should
853 probably live somewhere else...
857 L<FS::svc_Common>, edit/part_svc.cgi from an installed web interface,
858 export.html from the base documentation, L<FS::Record>, L<FS::Conf>,
859 L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, L<FS::queue>,
860 L<freeside-queued>), L<Net::SSH>, L<ssh>, L<FS::svc_acct_pop>,
861 schema.html from the base documentation.