4 use vars qw( @ISA $nossh_hack $noexport_hack $conf
5 $dir_prefix @shells $usernamemin
6 $usernamemax $passwordmin $passwordmax
7 $username_ampersand $username_letter $username_letterfirst
8 $username_noperiod $username_uppercase
9 $shellmachine $useradd $usermod $userdel $mydomain
10 $cyrus_server $cyrus_admin_user $cyrus_admin_pass
11 $cp_server $cp_user $cp_pass $cp_workgroup
14 $rsync $ssh $exportdir $vpopdir);
18 use FS::UID qw( datasrc );
20 use FS::Record qw( qsearch qsearchs fields dbh );
26 use FS::cust_main_invoice;
30 use FS::radius_usergroup;
31 use FS::Msgcat qw(gettext);
33 @ISA = qw( FS::svc_Common );
35 #ask FS::UID to run this stuff for us later
36 $FS::UID::callback{'FS::svc_acct'} = sub {
40 $dir_prefix = $conf->config('home');
41 @shells = $conf->config('shells');
42 $shellmachine = $conf->config('shellmachine');
43 $usernamemin = $conf->config('usernamemin') || 2;
44 $usernamemax = $conf->config('usernamemax');
45 $passwordmin = $conf->config('passwordmin') || 6;
46 $passwordmax = $conf->config('passwordmax') || 8;
47 if ( $shellmachine ) {
48 if ( $conf->exists('shellmachine-useradd') ) {
49 $useradd = join("\n", $conf->config('shellmachine-useradd') )
50 || 'cp -pr /etc/skel $dir; chown -R $uid.$gid $dir';
52 $useradd = 'useradd -d $dir -m -s $shell -u $uid $username';
54 if ( $conf->exists('shellmachine-userdel') ) {
55 $userdel = join("\n", $conf->config('shellmachine-userdel') )
58 $userdel = 'userdel $username';
60 $usermod = join("\n", $conf->config('shellmachine-usermod') )
61 || '[ -d $old_dir ] && mv $old_dir $new_dir || ( '.
62 'chmod u+t $old_dir; mkdir $new_dir; cd $old_dir; '.
63 'find . -depth -print | cpio -pdm $new_dir; '.
64 'chmod u-t $new_dir; chown -R $uid.$gid $new_dir; '.
68 $username_letter = $conf->exists('username-letter');
69 $username_letterfirst = $conf->exists('username-letterfirst');
70 $username_noperiod = $conf->exists('username-noperiod');
71 $username_uppercase = $conf->exists('username-uppercase');
72 $username_ampersand = $conf->exists('username-ampersand');
73 $mydomain = $conf->config('domain');
74 if ( $conf->exists('cyrus') ) {
75 ($cyrus_server, $cyrus_admin_user, $cyrus_admin_pass) =
76 $conf->config('cyrus');
77 eval "use Cyrus::IMAP::Admin;"
80 $cyrus_admin_user = '';
81 $cyrus_admin_pass = '';
83 if ( $conf->exists('cp_app') ) {
84 ($cp_server, $cp_user, $cp_pass, $cp_workgroup) =
85 $conf->config('cp_app');
94 $dirhash = $conf->config('dirhash') || 0;
95 $exportdir = "/usr/local/etc/freeside/export." . datasrc;
96 if ( $conf->exists('vpopmailmachines') ) {
97 my (@vpopmailmachines) = $conf->config('vpopmailmachines');
98 my ($machine, $dir, $uid, $gid) = split (/\s+/, $vpopmailmachines[0]);
105 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
106 @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '!', '.', ',' );
108 #not needed in 5.004 #srand($$|time);
112 my ( $hashref, $cache ) = @_;
113 if ( $hashref->{'svc_acct_svcnum'} ) {
114 $self->{'_domsvc'} = FS::svc_domain->new( {
115 'svcnum' => $hashref->{'domsvc'},
116 'domain' => $hashref->{'svc_acct_domain'},
117 'catchall' => $hashref->{'svc_acct_catchall'},
124 FS::svc_acct - Object methods for svc_acct records
130 $record = new FS::svc_acct \%hash;
131 $record = new FS::svc_acct { 'column' => 'value' };
133 $error = $record->insert;
135 $error = $new_record->replace($old_record);
137 $error = $record->delete;
139 $error = $record->check;
141 $error = $record->suspend;
143 $error = $record->unsuspend;
145 $error = $record->cancel;
147 %hash = $record->radius;
149 %hash = $record->radius_reply;
151 %hash = $record->radius_check;
153 $domain = $record->domain;
155 $svc_domain = $record->svc_domain;
157 $email = $record->email;
159 $seconds_since = $record->seconds_since($timestamp);
163 An FS::svc_acct object represents an account. FS::svc_acct inherits from
164 FS::svc_Common. The following fields are currently supported:
168 =item svcnum - primary key (assigned automatcially for new accounts)
172 =item _password - generated if blank
174 =item sec_phrase - security phrase
176 =item popnum - Point of presence (see L<FS::svc_acct_pop>)
184 =item dir - set automatically if blank (and uid is not)
188 =item quota - (unimplementd)
190 =item slipip - IP address
194 =item domsvc - svcnum from svc_domain
196 =item radius_I<Radius_Attribute> - I<Radius-Attribute>
206 Creates a new account. To add the account to the database, see L<"insert">.
210 sub table { 'svc_acct'; }
214 Adds this account to the database. If there is an error, returns the error,
215 otherwise returns false.
217 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
218 defined. An FS::cust_svc record will be created and inserted.
220 The additional field I<usergroup> can optionally be defined; if so it should
221 contain an arrayref of group names. See L<FS::radius_usergroup>. (used in
222 sqlradius export only)
224 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
225 username, uid, and dir fields are defined, the command(s) specified in
226 the shellmachine-useradd configuration are added to the job queue (see
227 L<FS::queue> and L<freeside-queued>) to be exectued on shellmachine via ssh.
228 This behaviour can be surpressed by setting $FS::svc_acct::nossh_hack true.
229 If the shellmachine-useradd configuration file does not exist,
231 useradd -d $dir -m -s $shell -u $uid $username
233 is the default. If the shellmachine-useradd configuration file exists but
236 cp -pr /etc/skel $dir; chown -R $uid.$gid $dir
238 is the default instead. Otherwise the contents of the file are treated as
239 a double-quoted perl string, with the following variables available:
240 $username, $uid, $gid, $dir, and $shell.
242 (TODOC: cyrus config file, L<FS::queue> and L<freeside-queued>)
244 (TODOC: new exports! $noexport_hack)
252 local $SIG{HUP} = 'IGNORE';
253 local $SIG{INT} = 'IGNORE';
254 local $SIG{QUIT} = 'IGNORE';
255 local $SIG{TERM} = 'IGNORE';
256 local $SIG{TSTP} = 'IGNORE';
257 local $SIG{PIPE} = 'IGNORE';
259 my $oldAutoCommit = $FS::UID::AutoCommit;
260 local $FS::UID::AutoCommit = 0;
263 $error = $self->check;
264 return $error if $error;
266 return gettext('username_in_use'). ": ". $self->username
267 if qsearchs( 'svc_acct', { 'username' => $self->username,
268 'domsvc' => $self->domsvc,
271 if ( $self->svcnum ) {
272 my $cust_svc = qsearchs('cust_svc',{'svcnum'=>$self->svcnum});
273 unless ( $cust_svc ) {
274 $dbh->rollback if $oldAutoCommit;
275 return "no cust_svc record found for svcnum ". $self->svcnum;
277 $self->pkgnum($cust_svc->pkgnum);
278 $self->svcpart($cust_svc->svcpart);
281 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
282 return "Unknown svcpart" unless $part_svc;
284 if $part_svc->part_svc_column('uid')->columnflag ne 'F'
285 && qsearchs( 'svc_acct', { 'uid' => $self->uid } )
286 && $self->username !~ /^(hyla)?fax$/
289 $error = $self->SUPER::insert;
291 $dbh->rollback if $oldAutoCommit;
295 if ( $self->usergroup ) {
296 foreach my $groupname ( @{$self->usergroup} ) {
297 my $radius_usergroup = new FS::radius_usergroup ( {
298 svcnum => $self->svcnum,
299 groupname => $groupname,
301 my $error = $radius_usergroup->insert;
303 $dbh->rollback if $oldAutoCommit;
310 unless ( $noexport_hack ) {
311 foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
312 my $error = $part_export->export_insert($self);
314 $dbh->rollback if $oldAutoCommit;
315 return "exporting to ". $part_export->exporttype.
316 " (transaction rolled back): $error";
323 my( $username, $uid, $gid, $dir, $shell ) = (
330 if ( $username && $uid && $dir && $shellmachine && ! $nossh_hack ) {
331 my $queue = new FS::queue {
332 'svcnum' => $self->svcnum,
333 'job' => 'Net::SSH::ssh_cmd',
335 $error = $queue->insert("root\@$shellmachine", eval qq("$useradd") );
337 $dbh->rollback if $oldAutoCommit;
338 return "queueing job (transaction rolled back): $error";
342 if ( $cyrus_server ) {
343 my $queue = new FS::queue {
344 'svcnum' => $self->svcnum,
345 'job' => 'FS::svc_acct::cyrus_insert',
347 $error = $queue->insert($self->username, $self->quota);
349 $dbh->rollback if $oldAutoCommit;
350 return "queueing job (transaction rolled back): $error";
355 my $queue = new FS::queue {
356 'svcnum' => $self->svcnum,
357 'job' => 'FS::svc_acct::cp_insert'
359 $error = $queue->insert($self->username, $self->_password);
361 $dbh->rollback if $oldAutoCommit;
362 return "queueing job (transaction rolled back): $error";
370 'svcnum' => $self->svcnum,
371 'job' => 'FS::svc_acct::vpopmail_insert'
373 $error = $vpopmail_queue->insert( $self->username,
374 crypt($self->_password,$saltset[int(rand(64))].$saltset[int(rand(64))]),
379 $dbh->rollback if $oldAutoCommit;
380 return "queueing job (transaction rolled back): $error";
385 #end of old-style exports
387 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
392 my( $username, $quota ) = @_;
394 warn "cyrus_insert: starting for user $username, quota $quota\n";
396 warn "cyrus_insert: connecting to $cyrus_server\n";
397 my $client = Cyrus::IMAP::Admin->new($cyrus_server);
399 warn "cyrus_insert: authentication as $cyrus_admin_user\n";
400 $client->authenticate(
401 -user => $cyrus_admin_user,
402 -mechanism => "login",
403 -password => $cyrus_admin_pass
406 warn "cyrus_insert: creating user.$username\n";
407 my $rc = $client->create("user.$username");
408 my $error = $client->error;
409 die "cyrus_insert: error creating user.$username: $error" if $error;
411 warn "cyrus_insert: setacl user.$username, $username => all\n";
412 $rc = $client->setacl("user.$username", $username => 'all' );
413 $error = $client->error;
414 die "cyrus_insert: error setacl user.$username: $error" if $error;
417 warn "cyrus_insert: setquota user.$username, STORAGE => $quota\n";
418 $rc = $client->setquota("user.$username", 'STORAGE' => $quota );
419 $error = $client->error;
420 die "cyrus_insert: error setquota user.$username: $error" if $error;
427 my( $username, $password ) = @_;
429 my $app = new Net::APP ( $cp_server,
431 Password => $cp_pass,
437 $app->create_mailbox(
438 Mailbox => $username,
439 Password => $password,
440 Workgroup => $cp_workgroup,
444 die $app->message."\n" unless $app->ok;
447 sub vpopmail_insert {
448 my( $username, $password, $domain, $vpopdir ) = @_;
450 (open(VPASSWD, ">>$exportdir/domains/$domain/vpasswd")
451 and flock(VPASSWD,LOCK_EX)
452 ) or die "can't open vpasswd file for $username\@$domain: $exportdir/domains/$domain/vpasswd";
453 print VPASSWD join(":",
459 "$vpopdir/domains/$domain/$username",
463 flock(VPASSWD,LOCK_UN);
466 mkdir "$exportdir/domains/$domain/$username", 0700 or die "can't create Maildir";
467 mkdir "$exportdir/domains/$domain/$username/Maildir", 0700 or die "can't create Maildir";
468 mkdir "$exportdir/domains/$domain/$username/Maildir/cur", 0700 or die "can't create Maildir";
469 mkdir "$exportdir/domains/$domain/$username/Maildir/new", 0700 or die "can't create Maildir";
470 mkdir "$exportdir/domains/$domain/$username/Maildir/tmp", 0700 or die "can't create Maildir";
472 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_sync' };
473 my $error = $queue->insert;
474 die $error if $error;
481 my (@vpopmailmachines) = $conf->config('vpopmailmachines');
482 my ($machine, $dir, $uid, $gid) = split (/\s+/, $vpopmailmachines[0]);
485 my @args = ("$rsync", "-rlpt", "-e", "$ssh", "domains/", "vpopmail\@$machine:$vpopdir/domains/");
486 system {$args[0]} @args;
492 Deletes this account from the database. If there is an error, returns the
493 error, otherwise returns false.
495 The corresponding FS::cust_svc record will be deleted as well.
497 If the configuration value (see L<FS::Conf>) shellmachine exists, the
498 command(s) specified in the shellmachine-userdel configuration file are
499 added to the job queue (see L<FS::queue> and L<freeside-queued>) to be executed
500 on shellmachine via ssh. This behavior can be surpressed by setting
501 $FS::svc_acct::nossh_hack true. If the shellmachine-userdel configuration
506 is the default. If the shellmachine-userdel configuration file exists but
511 is the default instead. Otherwise the contents of the file are treated as a
512 double-quoted perl string, with the following variables available:
515 (TODOC: cyrus config file)
517 (TODOC: new exports! $noexport_hack)
524 if ( defined( $FS::Record::dbdef->table('svc_acct_sm') ) ) {
525 return "Can't delete an account which has (svc_acct_sm) mail aliases!"
526 if $self->uid && qsearch( 'svc_acct_sm', { 'domuid' => $self->uid } );
529 return "Can't delete an account which is a (svc_forward) source!"
530 if qsearch( 'svc_forward', { 'srcsvc' => $self->svcnum } );
532 return "Can't delete an account which is a (svc_forward) destination!"
533 if qsearch( 'svc_forward', { 'dstsvc' => $self->svcnum } );
535 return "Can't delete an account with (svc_www) web service!"
536 if qsearch( 'svc_www', { 'usersvc' => $self->usersvc } );
538 # what about records in session ? (they should refer to history table)
540 local $SIG{HUP} = 'IGNORE';
541 local $SIG{INT} = 'IGNORE';
542 local $SIG{QUIT} = 'IGNORE';
543 local $SIG{TERM} = 'IGNORE';
544 local $SIG{TSTP} = 'IGNORE';
545 local $SIG{PIPE} = 'IGNORE';
547 my $oldAutoCommit = $FS::UID::AutoCommit;
548 local $FS::UID::AutoCommit = 0;
551 foreach my $cust_main_invoice (
552 qsearch( 'cust_main_invoice', { 'dest' => $self->svcnum } )
554 unless ( defined($cust_main_invoice) ) {
555 warn "WARNING: something's wrong with qsearch";
558 my %hash = $cust_main_invoice->hash;
559 $hash{'dest'} = $self->email;
560 my $new = new FS::cust_main_invoice \%hash;
561 my $error = $new->replace($cust_main_invoice);
563 $dbh->rollback if $oldAutoCommit;
568 foreach my $svc_domain (
569 qsearch( 'svc_domain', { 'catchall' => $self->svcnum } )
571 my %hash = new FS::svc_domain->hash;
572 $hash{'catchall'} = '';
573 my $new = new FS::svc_domain \%hash;
574 my $error = $new->replace($svc_domain);
576 $dbh->rollback if $oldAutoCommit;
581 foreach my $radius_usergroup (
582 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } )
584 my $error = $radius_usergroup->delete;
586 $dbh->rollback if $oldAutoCommit;
591 my $part_svc = $self->cust_svc->part_svc;
593 my $error = $self->SUPER::delete;
595 $dbh->rollback if $oldAutoCommit;
600 unless ( $noexport_hack ) {
601 foreach my $part_export ( $part_svc->part_export ) {
602 my $error = $part_export->export_delete($self);
604 $dbh->rollback if $oldAutoCommit;
605 return "exporting to ". $part_export->exporttype.
606 " (transaction rolled back): $error";
613 my( $username, $dir ) = (
617 if ( $username && $shellmachine && ! $nossh_hack ) {
618 my $queue = new FS::queue { 'job' => 'Net::SSH::ssh_cmd' };
619 $error = $queue->insert("root\@$shellmachine", eval qq("$userdel") );
621 $dbh->rollback if $oldAutoCommit;
622 return "queueing job (transaction rolled back): $error";
627 if ( $cyrus_server ) {
628 my $queue = new FS::queue { 'job' => 'FS::svc_acct::cyrus_delete' };
629 $error = $queue->insert($self->username);
631 $dbh->rollback if $oldAutoCommit;
632 return "queueing job (transaction rolled back): $error";
637 my $queue = new FS::queue { 'job' => 'FS::svc_acct::cp_delete' };
638 $error = $queue->insert($self->username);
640 $dbh->rollback if $oldAutoCommit;
641 return "queueing job (transaction rolled back): $error";
646 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_delete' };
647 $error = $queue->insert( $self->username, $self->domain );
649 $dbh->rollback if $oldAutoCommit;
650 return "queueing job (transaction rolled back): $error";
655 #end of old-style exports
657 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
662 my $username = shift;
664 my $client = Cyrus::IMAP::Admin->new($cyrus_server);
665 $client->authenticate(
666 -user => $cyrus_admin_user,
667 -mechanism => "login",
668 -password => $cyrus_admin_pass
671 my $rc = $client->setacl("user.$username", $cyrus_admin_user => 'all' );
672 my $error = $client->error;
673 die $error if $error;
675 $rc = $client->delete("user.$username");
676 $error = $client->error;
677 die $error if $error;
683 my( $username ) = @_;
684 my $app = new Net::APP ( $cp_server,
686 Password => $cp_pass,
692 $app->delete_mailbox(
693 Mailbox => $username,
697 die $app->message."\n" unless $app->ok;
700 sub vpopmail_delete {
701 my( $username, $domain ) = @_;
703 (open(VPASSWD, "$exportdir/domains/$domain/vpasswd")
704 and flock(VPASSWD,LOCK_EX)
705 ) or die "can't open $exportdir/domains/$domain/vpasswd: $!";
707 open(VPASSWDTMP, ">$exportdir/domains/$domain/vpasswd.tmp")
708 or die "Can't open $exportdir/domains/$domain/vpasswd.tmp: $!";
711 my ($mailbox, $rest) = split(':', $_);
712 print VPASSWDTMP $_ unless $username eq $mailbox;
717 rename "$exportdir/domains/$domain/vpasswd.tmp", "$exportdir/domains/$domain/vpasswd"
718 or die "Can't rename $exportdir/domains/$domain/vpasswd.tmp: $!";
720 flock(VPASSWD,LOCK_UN);
723 rmtree "$exportdir/domains/$domain/$username" or die "can't destroy Maildir";+
727 =item replace OLD_RECORD
729 Replaces OLD_RECORD with this one in the database. If there is an error,
730 returns the error, otherwise returns false.
732 The additional field I<usergroup> can optionally be defined; if so it should
733 contain an arrayref of group names. See L<FS::radius_usergroup>. (used in
734 sqlradius export only)
736 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
737 dir field has changed, the command(s) specified in the shellmachine-usermod
738 configuraiton file are added to the job queue (see L<FS::queue> and
739 L<freeside-queued>) to be executed on shellmachine via ssh. This behavior can
740 be surpressed by setting $FS::svc-acct::nossh_hack true. If the
741 shellmachine-userdel configuration file does not exist or is empty,
743 [ -d $old_dir ] && mv $old_dir $new_dir || (
747 find . -depth -print | cpio -pdm $new_dir;
749 chown -R $uid.$gid $new_dir;
753 is the default. This behaviour can be surpressed by setting
754 $FS::svc_acct::nossh_hack true.
759 my ( $new, $old ) = ( shift, shift );
762 return "Username in use"
763 if $old->username ne $new->username &&
764 qsearchs( 'svc_acct', { 'username' => $new->username,
765 'domsvc' => $new->domsvc,
768 #no warnings 'numeric'; #alas, a 5.006-ism
770 return "Can't change uid!" if $old->uid != $new->uid;
773 return "can't change username using Cyrus"
774 if $cyrus_server && $old->username ne $new->username;
776 #change homdir when we change username
777 $new->setfield('dir', '') if $old->username ne $new->username;
779 local $SIG{HUP} = 'IGNORE';
780 local $SIG{INT} = 'IGNORE';
781 local $SIG{QUIT} = 'IGNORE';
782 local $SIG{TERM} = 'IGNORE';
783 local $SIG{TSTP} = 'IGNORE';
784 local $SIG{PIPE} = 'IGNORE';
786 my $oldAutoCommit = $FS::UID::AutoCommit;
787 local $FS::UID::AutoCommit = 0;
790 $error = $new->SUPER::replace($old);
792 $dbh->rollback if $oldAutoCommit;
793 return $error if $error;
796 $old->usergroup( [ $old->radius_groups ] );
797 if ( $new->usergroup ) {
798 #(sorta) false laziness with FS::part_export::sqlradius::_export_replace
799 my @newgroups = @{$new->usergroup};
800 foreach my $oldgroup ( @{$old->usergroup} ) {
801 if ( grep { $oldgroup eq $_ } @newgroups ) {
802 @newgroups = grep { $oldgroup ne $_ } @newgroups;
805 my $radius_usergroup = qsearchs('radius_usergroup', {
806 svcnum => $old->svcnum,
807 groupname => $oldgroup,
809 my $error = $radius_usergroup->delete;
811 $dbh->rollback if $oldAutoCommit;
812 return "error deleting radius_usergroup $oldgroup: $error";
816 foreach my $newgroup ( @newgroups ) {
817 my $radius_usergroup = new FS::radius_usergroup ( {
818 svcnum => $new->svcnum,
819 groupname => $newgroup,
821 my $error = $radius_usergroup->insert;
823 $dbh->rollback if $oldAutoCommit;
824 return "error adding radius_usergroup $newgroup: $error";
831 unless ( $noexport_hack ) {
832 foreach my $part_export ( $new->cust_svc->part_svc->part_export ) {
833 my $error = $part_export->export_replace($new,$old);
835 $dbh->rollback if $oldAutoCommit;
836 return "exporting to ". $part_export->exporttype.
837 " (transaction rolled back): $error";
844 my ( $old_dir, $new_dir, $uid, $gid ) = (
845 $old->getfield('dir'),
846 $new->getfield('dir'),
847 $new->getfield('uid'),
848 $new->getfield('gid'),
850 if ( $old_dir && $new_dir && $old_dir ne $new_dir && ! $nossh_hack ) {
851 my $queue = new FS::queue {
852 'svcnum' => $new->svcnum,
853 'job' => 'Net::SSH::ssh_cmd'
855 $error = $queue->insert("root\@$shellmachine", eval qq("$usermod") );
857 $dbh->rollback if $oldAutoCommit;
858 return "queueing job (transaction rolled back): $error";
862 if ( $cp_server && $old->username ne $new->username ) {
863 my $queue = new FS::queue {
864 'svcnum' => $new->svcnum,
865 'job' => 'FS::svc_acct::cp_rename'
867 $error = $queue->insert( $old->username, $new->username );
869 $dbh->rollback if $oldAutoCommit;
870 return "queueing job (transaction rolled back): $error";
874 if ( $cp_server && $old->_password ne $new->_password ) {
875 my $queue = new FS::queue {
876 'svcnum' => $new->svcnum,
877 'job' => 'FS::svc_acct::cp_change'
879 $error = $queue->insert( $new->username, $new->_password );
881 $dbh->rollback if $oldAutoCommit;
882 return "queueing job (transaction rolled back): $error";
887 my $cpassword = crypt(
888 $new->_password,$saltset[int(rand(64))].$saltset[int(rand(64))]
891 if ($old->username ne $new->username || $old->domain ne $new->domain ) {
892 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_delete' };
893 $error = $queue->insert( $old->username, $old->domain );
894 my $queue2 = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_insert' };
895 $error = $queue2->insert( $new->username,
901 } elsif ($old->_password ne $new->_password) {
902 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_replace_password' };
903 $error = $queue->insert( $new->username, $cpassword, $new->domain );
906 $dbh->rollback if $oldAutoCommit;
907 return "queueing job (transaction rolled back): $error";
911 #end of old-style exports
913 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
918 my ( $old_username, $new_username ) = @_;
920 my $app = new Net::APP ( $cp_server,
922 Password => $cp_pass,
928 $app->rename_mailbox(
930 Old_Mailbox => $old_username,
931 New_Mailbox => $new_username,
934 die $app->message."\n" unless $app->ok;
939 my ( $username, $password ) = @_;
941 my $app = new Net::APP ( $cp_server,
943 Password => $cp_pass,
949 if ( $password =~ /^\*SUSPENDED\* (.*)$/ ) {
951 $app->set_mailbox_status(
953 Mailbox => $username,
958 $app->set_mailbox_status(
960 Mailbox => $username,
965 die $app->message."\n" unless $app->ok;
967 $app->change_mailbox(
969 Mailbox => $username,
970 Password => $password,
972 die $app->message."\n" unless $app->ok;
976 sub vpopmail_replace_password {
977 my( $username, $password, $domain ) = @_;
979 (open(VPASSWD, "$exportdir/domains/$domain/vpasswd")
980 and flock(VPASSWD,LOCK_EX)
981 ) or die "can't open $exportdir/domains/$domain/vpasswd: $!";
983 open(VPASSWDTMP, ">$exportdir/domains/$domain/vpasswd.tmp")
984 or die "Can't open $exportdir/domains/$domain/vpasswd.tmp: $!";
987 my ($mailbox, $pw, @rest) = split(':', $_);
988 print VPASSWDTMP $_ unless $username eq $mailbox;
989 print VPASSWDTMP join (':', ($mailbox, $password, @rest))
990 if $username eq $mailbox;
995 rename "$exportdir/domains/$domain/vpasswd.tmp", "$exportdir/domains/$domain/vpasswd"
996 or die "Can't rename $exportdir/domains/$domain/vpasswd.tmp: $!";
998 flock(VPASSWD,LOCK_UN);
1001 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_sync' };
1002 my $error = $queue->insert;
1003 die $error if $error;
1011 Suspends this account by prefixing *SUSPENDED* to the password. If there is an
1012 error, returns the error, otherwise returns false.
1014 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
1020 my %hash = $self->hash;
1021 unless ( $hash{_password} =~ /^\*SUSPENDED\* /
1022 || $hash{_password} eq '*'
1024 $hash{_password} = '*SUSPENDED* '.$hash{_password};
1025 my $new = new FS::svc_acct ( \%hash );
1026 $new->replace($self);
1028 ''; #no error (already suspended)
1034 Unsuspends this account by removing *SUSPENDED* from the password. If there is
1035 an error, returns the error, otherwise returns false.
1037 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
1043 my %hash = $self->hash;
1044 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
1045 $hash{_password} = $1;
1046 my $new = new FS::svc_acct ( \%hash );
1047 $new->replace($self);
1049 ''; #no error (already unsuspended)
1055 Just returns false (no error) for now.
1057 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
1061 Checks all fields to make sure this is a valid service. If there is an error,
1062 returns the error, otherwise returns false. Called by the insert and replace
1065 Sets any fixed values; see L<FS::part_svc>.
1072 my($recref) = $self->hashref;
1074 my $x = $self->setfixed;
1075 return $x unless ref($x);
1078 if ( $part_svc->part_svc_column('usergroup')->columnflag eq "F" ) {
1080 [ split(',', $part_svc->part_svc_column('usergroup')->columnvalue) ] );
1083 my $error = $self->ut_numbern('svcnum')
1084 || $self->ut_number('domsvc')
1085 || $self->ut_textn('sec_phrase')
1087 return $error if $error;
1089 my $ulen = $usernamemax || $self->dbdef_table->column('username')->length;
1090 if ( $username_uppercase ) {
1091 $recref->{username} =~ /^([a-z0-9_\-\.\&]{$usernamemin,$ulen})$/i
1092 or return gettext('illegal_username'). ": ". $recref->{username};
1093 $recref->{username} = $1;
1095 $recref->{username} =~ /^([a-z0-9_\-\.\&]{$usernamemin,$ulen})$/
1096 or return gettext('illegal_username'). ": ". $recref->{username};
1097 $recref->{username} = $1;
1100 if ( $username_letterfirst ) {
1101 $recref->{username} =~ /^[a-z]/ or return gettext('illegal_username');
1102 } elsif ( $username_letter ) {
1103 $recref->{username} =~ /[a-z]/ or return gettext('illegal_username');
1105 if ( $username_noperiod ) {
1106 $recref->{username} =~ /\./ and return gettext('illegal_username');
1108 unless ( $username_ampersand ) {
1109 $recref->{username} =~ /\&/ and return gettext('illegal_username');
1112 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum: ".$recref->{popnum};
1113 $recref->{popnum} = $1;
1114 return "Unknown popnum" unless
1115 ! $recref->{popnum} ||
1116 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
1118 unless ( $part_svc->part_svc_column('uid')->columnflag eq 'F' ) {
1120 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
1121 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
1123 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
1124 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
1125 #not all systems use gid=uid
1126 #you can set a fixed gid in part_svc
1128 return "Only root can have uid 0"
1129 if $recref->{uid} == 0 && $recref->{username} ne 'root';
1131 # $error = $self->ut_textn('finger');
1132 # return $error if $error;
1133 $self->getfield('finger') =~
1134 /^([\w \t\!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\*\<\>]*)$/
1135 or return "Illegal finger: ". $self->getfield('finger');
1136 $self->setfield('finger', $1);
1138 $recref->{dir} =~ /^([\/\w\-\.\&]*)$/
1139 or return "Illegal directory";
1140 $recref->{dir} = $1;
1141 return "Illegal directory"
1142 if $recref->{dir} =~ /(^|\/)\.+(\/|$)/; #no .. component
1143 return "Illegal directory"
1144 if $recref->{dir} =~ /\&/ && ! $username_ampersand;
1145 unless ( $recref->{dir} ) {
1146 $recref->{dir} = $dir_prefix . '/';
1147 if ( $dirhash > 0 ) {
1148 for my $h ( 1 .. $dirhash ) {
1149 $recref->{dir} .= substr($recref->{username}, $h-1, 1). '/';
1151 } elsif ( $dirhash < 0 ) {
1152 for my $h ( reverse $dirhash .. -1 ) {
1153 $recref->{dir} .= substr($recref->{username}, $h, 1). '/';
1156 $recref->{dir} .= $recref->{username};
1160 unless ( $recref->{username} eq 'sync' ) {
1161 if ( grep $_ eq $recref->{shell}, @shells ) {
1162 $recref->{shell} = (grep $_ eq $recref->{shell}, @shells)[0];
1164 return "Illegal shell \`". $self->shell. "\'; ".
1165 $conf->dir. "/shells contains: @shells";
1168 $recref->{shell} = '/bin/sync';
1171 $recref->{quota} =~ /^(\d*)$/ or return "Illegal quota (unimplemented)";
1172 $recref->{quota} = $1;
1175 $recref->{gid} ne '' ?
1176 return "Can't have gid without uid" : ( $recref->{gid}='' );
1177 $recref->{finger} ne '' ?
1178 return "Can't have finger-name without uid" : ( $recref->{finger}='' );
1179 $recref->{dir} ne '' ?
1180 return "Can't have directory without uid" : ( $recref->{dir}='' );
1181 $recref->{shell} ne '' ?
1182 return "Can't have shell without uid" : ( $recref->{shell}='' );
1183 $recref->{quota} ne '' ?
1184 return "Can't have quota without uid" : ( $recref->{quota}='' );
1187 unless ( $part_svc->part_svc_column('slipip')->columnflag eq 'F' ) {
1188 unless ( $recref->{slipip} eq '0e0' ) {
1189 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
1190 or return "Illegal slipip". $self->slipip;
1191 $recref->{slipip} = $1;
1193 $recref->{slipip} = '0e0';
1198 #arbitrary RADIUS stuff; allow ut_textn for now
1199 foreach ( grep /^radius_/, fields('svc_acct') ) {
1200 $self->ut_textn($_);
1203 #generate a password if it is blank
1204 $recref->{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) )
1205 unless ( $recref->{_password} );
1207 #if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,16})$/ ) {
1208 if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{$passwordmin,$passwordmax})$/ ) {
1209 $recref->{_password} = $1.$3;
1210 #uncomment this to encrypt password immediately upon entry, or run
1211 #bin/crypt_pw in cron to give new users a window during which their
1212 #password is available to techs, for faxing, etc. (also be aware of
1214 #$recref->{password} = $1.
1215 # crypt($3,$saltset[int(rand(64))].$saltset[int(rand(64))]
1217 } elsif ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([\w\.\/\$]{13,34})$/ ) {
1218 $recref->{_password} = $1.$3;
1219 } elsif ( $recref->{_password} eq '*' ) {
1220 $recref->{_password} = '*';
1221 } elsif ( $recref->{_password} eq '!!' ) {
1222 $recref->{_password} = '!!';
1224 #return "Illegal password";
1225 return gettext('illegal_password'). ": ". $recref->{_password};
1233 Depriciated, use radius_reply instead.
1238 carp "FS::svc_acct::radius depriciated, use radius_reply";
1239 $_[0]->radius_reply;
1244 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1245 reply attributes of this record.
1247 Note that this is now the preferred method for reading RADIUS attributes -
1248 accessing the columns directly is discouraged, as the column names are
1249 expected to change in the future.
1258 my($column, $attrib) = ($1, $2);
1259 #$attrib =~ s/_/\-/g;
1260 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1261 } grep { /^radius_/ && $self->getfield($_) } fields( $self->table );
1262 if ( $self->ip && $self->ip ne '0e0' ) {
1263 $reply{'Framed-IP-Address'} = $self->ip;
1270 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1271 check attributes of this record.
1273 Note that this is now the preferred method for reading RADIUS attributes -
1274 accessing the columns directly is discouraged, as the column names are
1275 expected to change in the future.
1281 ( 'Password' => $self->_password,
1284 my($column, $attrib) = ($1, $2);
1285 #$attrib =~ s/_/\-/g;
1286 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1287 } grep { /^rc_/ && $self->getfield($_) } fields( $self->table )
1293 Returns the domain associated with this account.
1299 if ( $self->domsvc ) {
1300 #$self->svc_domain->domain;
1301 my $svc_domain = $self->svc_domain
1302 or die "no svc_domain.svcnum for svc_acct.domsvc ". $self->domsvc;
1303 $svc_domain->domain;
1305 $mydomain or die "svc_acct.domsvc is null and no legacy domain config file";
1311 Returns the FS::svc_domain record for this account's domain (see
1319 ? $self->{'_domsvc'}
1320 : qsearchs( 'svc_domain', { 'svcnum' => $self->domsvc } );
1325 Returns the FS::cust_svc record for this account (see L<FS::cust_svc>).
1329 qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
1334 Returns an email address associated with the account.
1340 $self->username. '@'. $self->domain;
1343 =item seconds_since TIMESTAMP
1345 Returns the number of seconds this account has been online since TIMESTAMP.
1348 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
1349 L<Time::Local> and L<Date::Parse> for conversion functions.
1353 #note: POD here, implementation in FS::cust_svc
1356 $self->cust_svc->seconds_since(@_);
1361 Returns all RADIUS groups for this account (see L<FS::radius_usergroup>).
1367 map { $_->groupname }
1368 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } );
1375 =item radius_usergroup_selector GROUPS_ARRAYREF [ SELECTNAME ]
1379 sub radius_usergroup_selector {
1380 my $sel_groups = shift;
1381 my %sel_groups = map { $_=>1 } @$sel_groups;
1383 my $selectname = shift || 'radius_usergroup';
1386 my $sth = $dbh->prepare(
1387 'SELECT DISTINCT(groupname) FROM radius_usergroup ORDER BY groupname'
1388 ) or die $dbh->errstr;
1389 $sth->execute() or die $sth->errstr;
1390 my @all_groups = map { $_->[0] } @{$sth->fetchall_arrayref};
1394 function ${selectname}_doadd(object) {
1395 var myvalue = object.${selectname}_add.value;
1396 var optionName = new Option(myvalue,myvalue,false,true);
1397 var length = object.$selectname.length;
1398 object.$selectname.options[length] = optionName;
1399 object.${selectname}_add.value = "";
1402 <SELECT MULTIPLE NAME="$selectname">
1405 foreach my $group ( @all_groups ) {
1407 if ( $sel_groups{$group} ) {
1408 $html .= ' SELECTED';
1409 $sel_groups{$group} = 0;
1411 $html .= ">$group</OPTION>\n";
1413 foreach my $group ( grep { $sel_groups{$_} } keys %sel_groups ) {
1414 $html .= "<OPTION SELECTED>$group</OPTION>\n";
1416 $html .= '</SELECT>';
1418 $html .= qq!<BR><INPUT TYPE="text" NAME="${selectname}_add">!.
1419 qq!<INPUT TYPE="button" VALUE="Add new group" onClick="${selectname}_doadd(this.form)">!;
1426 The $recref stuff in sub check should be cleaned up.
1428 The suspend, unsuspend and cancel methods update the database, but not the
1429 current object. This is probably a bug as it's unexpected and
1432 radius_usergroup_selector? putting web ui components in here? they should
1433 probably live somewhere else...
1437 L<FS::svc_Common>, edit/part_svc.cgi from an installed web interface,
1438 export.html from the base documentation, L<FS::Record>, L<FS::Conf>,
1439 L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, L<FS::queue>,
1440 L<freeside-queued>), L<Net::SSH>, L<ssh>, L<FS::svc_acct_pop>,
1441 schema.html from the base documentation.