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;
32 @ISA = qw( FS::svc_Common );
34 #ask FS::UID to run this stuff for us later
35 $FS::UID::callback{'FS::svc_acct'} = sub {
39 $dir_prefix = $conf->config('home');
40 @shells = $conf->config('shells');
41 $shellmachine = $conf->config('shellmachine');
42 $usernamemin = $conf->config('usernamemin') || 2;
43 $usernamemax = $conf->config('usernamemax');
44 $passwordmin = $conf->config('passwordmin') || 6;
45 $passwordmax = $conf->config('passwordmax') || 8;
46 if ( $shellmachine ) {
47 if ( $conf->exists('shellmachine-useradd') ) {
48 $useradd = join("\n", $conf->config('shellmachine-useradd') )
49 || 'cp -pr /etc/skel $dir; chown -R $uid.$gid $dir';
51 $useradd = 'useradd -d $dir -m -s $shell -u $uid $username';
53 if ( $conf->exists('shellmachine-userdel') ) {
54 $userdel = join("\n", $conf->config('shellmachine-userdel') )
57 $userdel = 'userdel $username';
59 $usermod = join("\n", $conf->config('shellmachine-usermod') )
60 || '[ -d $old_dir ] && mv $old_dir $new_dir || ( '.
61 'chmod u+t $old_dir; mkdir $new_dir; cd $old_dir; '.
62 'find . -depth -print | cpio -pdm $new_dir; '.
63 'chmod u-t $new_dir; chown -R $uid.$gid $new_dir; '.
67 $username_letter = $conf->exists('username-letter');
68 $username_letterfirst = $conf->exists('username-letterfirst');
69 $username_noperiod = $conf->exists('username-noperiod');
70 $username_uppercase = $conf->exists('username-uppercase');
71 $username_ampersand = $conf->exists('username-ampersand');
72 $mydomain = $conf->config('domain');
73 if ( $conf->exists('cyrus') ) {
74 ($cyrus_server, $cyrus_admin_user, $cyrus_admin_pass) =
75 $conf->config('cyrus');
76 eval "use Cyrus::IMAP::Admin;"
79 $cyrus_admin_user = '';
80 $cyrus_admin_pass = '';
82 if ( $conf->exists('cp_app') ) {
83 ($cp_server, $cp_user, $cp_pass, $cp_workgroup) =
84 $conf->config('cp_app');
93 $dirhash = $conf->config('dirhash') || 0;
94 $exportdir = "/usr/local/etc/freeside/export." . datasrc;
95 if ( $conf->exists('vpopmailmachines') ) {
96 my (@vpopmailmachines) = $conf->config('vpopmailmachines');
97 my ($machine, $dir, $uid, $gid) = split (/\s+/, $vpopmailmachines[0]);
104 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
105 @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '!', '.', ',' );
107 #not needed in 5.004 #srand($$|time);
111 my ( $hashref, $cache ) = @_;
112 if ( $hashref->{'svc_acct_svcnum'} ) {
113 $self->{'_domsvc'} = FS::svc_domain->new( {
114 'svcnum' => $hashref->{'domsvc'},
115 'domain' => $hashref->{'svc_acct_domain'},
116 'catchall' => $hashref->{'svc_acct_catchall'},
123 FS::svc_acct - Object methods for svc_acct records
129 $record = new FS::svc_acct \%hash;
130 $record = new FS::svc_acct { 'column' => 'value' };
132 $error = $record->insert;
134 $error = $new_record->replace($old_record);
136 $error = $record->delete;
138 $error = $record->check;
140 $error = $record->suspend;
142 $error = $record->unsuspend;
144 $error = $record->cancel;
146 %hash = $record->radius;
148 %hash = $record->radius_reply;
150 %hash = $record->radius_check;
152 $domain = $record->domain;
154 $svc_domain = $record->svc_domain;
156 $email = $record->email;
158 $seconds_since = $record->seconds_since($timestamp);
162 An FS::svc_acct object represents an account. FS::svc_acct inherits from
163 FS::svc_Common. The following fields are currently supported:
167 =item svcnum - primary key (assigned automatcially for new accounts)
171 =item _password - generated if blank
173 =item popnum - Point of presence (see L<FS::svc_acct_pop>)
181 =item dir - set automatically if blank (and uid is not)
185 =item quota - (unimplementd)
187 =item slipip - IP address
191 =item domsvc - svcnum from svc_domain
193 =item radius_I<Radius_Attribute> - I<Radius-Attribute>
203 Creates a new account. To add the account to the database, see L<"insert">.
207 sub table { 'svc_acct'; }
211 Adds this account to the database. If there is an error, returns the error,
212 otherwise returns false.
214 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
215 defined. An FS::cust_svc record will be created and inserted.
217 The additional field I<usergroup> can optionally be defined; if so it should
218 contain an arrayref of group names. See L<FS::radius_usergroup>. (used in
219 sqlradius export only)
221 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
222 username, uid, and dir fields are defined, the command(s) specified in
223 the shellmachine-useradd configuration are added to the job queue (see
224 L<FS::queue> and L<freeside-queued>) to be exectued on shellmachine via ssh.
225 This behaviour can be surpressed by setting $FS::svc_acct::nossh_hack true.
226 If the shellmachine-useradd configuration file does not exist,
228 useradd -d $dir -m -s $shell -u $uid $username
230 is the default. If the shellmachine-useradd configuration file exists but
233 cp -pr /etc/skel $dir; chown -R $uid.$gid $dir
235 is the default instead. Otherwise the contents of the file are treated as
236 a double-quoted perl string, with the following variables available:
237 $username, $uid, $gid, $dir, and $shell.
239 (TODOC: cyrus config file, L<FS::queue> and L<freeside-queued>)
241 (TODOC: new exports! $noexport_hack)
249 local $SIG{HUP} = 'IGNORE';
250 local $SIG{INT} = 'IGNORE';
251 local $SIG{QUIT} = 'IGNORE';
252 local $SIG{TERM} = 'IGNORE';
253 local $SIG{TSTP} = 'IGNORE';
254 local $SIG{PIPE} = 'IGNORE';
256 my $oldAutoCommit = $FS::UID::AutoCommit;
257 local $FS::UID::AutoCommit = 0;
260 $error = $self->check;
261 return $error if $error;
263 return "Username ". $self->username. " in use"
264 if qsearchs( 'svc_acct', { 'username' => $self->username,
265 'domsvc' => $self->domsvc,
268 if ( $self->svcnum ) {
269 my $cust_svc = qsearchs('cust_svc',{'svcnum'=>$self->svcnum});
270 unless ( $cust_svc ) {
271 $dbh->rollback if $oldAutoCommit;
272 return "no cust_svc record found for svcnum ". $self->svcnum;
274 $self->pkgnum($cust_svc->pkgnum);
275 $self->svcpart($cust_svc->svcpart);
278 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
279 return "Unknown svcpart" unless $part_svc;
281 if $part_svc->part_svc_column('uid')->columnflag ne 'F'
282 && qsearchs( 'svc_acct', { 'uid' => $self->uid } )
283 && $self->username !~ /^(hyla)?fax$/
286 $error = $self->SUPER::insert;
288 $dbh->rollback if $oldAutoCommit;
292 if ( $self->usergroup ) {
293 foreach my $groupname ( @{$self->usergroup} ) {
294 my $radius_usergroup = new FS::radius_usergroup ( {
295 svcnum => $self->svcnum,
296 groupname => $groupname,
298 my $error = $radius_usergroup->insert;
300 $dbh->rollback if $oldAutoCommit;
307 unless ( $noexport_hack ) {
308 foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
309 my $error = $part_export->export_insert($self);
311 $dbh->rollback if $oldAutoCommit;
312 return "exporting to ". $part_export->exporttype.
313 " (transaction rolled back): $error";
320 my( $username, $uid, $gid, $dir, $shell ) = (
327 if ( $username && $uid && $dir && $shellmachine && ! $nossh_hack ) {
328 my $queue = new FS::queue {
329 'svcnum' => $self->svcnum,
330 'job' => 'Net::SSH::ssh_cmd',
332 $error = $queue->insert("root\@$shellmachine", eval qq("$useradd") );
334 $dbh->rollback if $oldAutoCommit;
335 return "queueing job (transaction rolled back): $error";
339 if ( $cyrus_server ) {
340 my $queue = new FS::queue {
341 'svcnum' => $self->svcnum,
342 'job' => 'FS::svc_acct::cyrus_insert',
344 $error = $queue->insert($self->username, $self->quota);
346 $dbh->rollback if $oldAutoCommit;
347 return "queueing job (transaction rolled back): $error";
352 my $queue = new FS::queue {
353 'svcnum' => $self->svcnum,
354 'job' => 'FS::svc_acct::cp_insert'
356 $error = $queue->insert($self->username, $self->_password);
358 $dbh->rollback if $oldAutoCommit;
359 return "queueing job (transaction rolled back): $error";
367 'svcnum' => $self->svcnum,
368 'job' => 'FS::svc_acct::vpopmail_insert'
370 $error = $vpopmail_queue->insert( $self->username,
371 crypt($self->_password,$saltset[int(rand(64))].$saltset[int(rand(64))]),
376 $dbh->rollback if $oldAutoCommit;
377 return "queueing job (transaction rolled back): $error";
382 #end of old-style exports
384 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
389 my( $username, $quota ) = @_;
391 warn "cyrus_insert: starting for user $username, quota $quota\n";
393 warn "cyrus_insert: connecting to $cyrus_server\n";
394 my $client = Cyrus::IMAP::Admin->new($cyrus_server);
396 warn "cyrus_insert: authentication as $cyrus_admin_user\n";
397 $client->authenticate(
398 -user => $cyrus_admin_user,
399 -mechanism => "login",
400 -password => $cyrus_admin_pass
403 warn "cyrus_insert: creating user.$username\n";
404 my $rc = $client->create("user.$username");
405 my $error = $client->error;
406 die "cyrus_insert: error creating user.$username: $error" if $error;
408 warn "cyrus_insert: setacl user.$username, $username => all\n";
409 $rc = $client->setacl("user.$username", $username => 'all' );
410 $error = $client->error;
411 die "cyrus_insert: error setacl user.$username: $error" if $error;
414 warn "cyrus_insert: setquota user.$username, STORAGE => $quota\n";
415 $rc = $client->setquota("user.$username", 'STORAGE' => $quota );
416 $error = $client->error;
417 die "cyrus_insert: error setquota user.$username: $error" if $error;
424 my( $username, $password ) = @_;
426 my $app = new Net::APP ( $cp_server,
428 Password => $cp_pass,
434 $app->create_mailbox(
435 Mailbox => $username,
436 Password => $password,
437 Workgroup => $cp_workgroup,
441 die $app->message."\n" unless $app->ok;
444 sub vpopmail_insert {
445 my( $username, $password, $domain, $vpopdir ) = @_;
447 (open(VPASSWD, ">>$exportdir/domains/$domain/vpasswd")
448 and flock(VPASSWD,LOCK_EX)
449 ) or die "can't open vpasswd file for $username\@$domain: $exportdir/domains/$domain/vpasswd";
450 print VPASSWD join(":",
456 "$vpopdir/domains/$domain/$username",
460 flock(VPASSWD,LOCK_UN);
463 mkdir "$exportdir/domains/$domain/$username", 0700 or die "can't create Maildir";
464 mkdir "$exportdir/domains/$domain/$username/Maildir", 0700 or die "can't create Maildir";
465 mkdir "$exportdir/domains/$domain/$username/Maildir/cur", 0700 or die "can't create Maildir";
466 mkdir "$exportdir/domains/$domain/$username/Maildir/new", 0700 or die "can't create Maildir";
467 mkdir "$exportdir/domains/$domain/$username/Maildir/tmp", 0700 or die "can't create Maildir";
469 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_sync' };
470 my $error = $queue->insert;
471 die $error if $error;
478 my (@vpopmailmachines) = $conf->config('vpopmailmachines');
479 my ($machine, $dir, $uid, $gid) = split (/\s+/, $vpopmailmachines[0]);
482 my @args = ("$rsync", "-rlpt", "-e", "$ssh", "domains/", "vpopmail\@$machine:$vpopdir/domains/");
483 system {$args[0]} @args;
489 Deletes this account from the database. If there is an error, returns the
490 error, otherwise returns false.
492 The corresponding FS::cust_svc record will be deleted as well.
494 If the configuration value (see L<FS::Conf>) shellmachine exists, the
495 command(s) specified in the shellmachine-userdel configuration file are
496 added to the job queue (see L<FS::queue> and L<freeside-queued>) to be executed
497 on shellmachine via ssh. This behavior can be surpressed by setting
498 $FS::svc_acct::nossh_hack true. If the shellmachine-userdel configuration
503 is the default. If the shellmachine-userdel configuration file exists but
508 is the default instead. Otherwise the contents of the file are treated as a
509 double-quoted perl string, with the following variables available:
512 (TODOC: cyrus config file)
514 (TODOC: new exports! $noexport_hack)
521 if ( defined( $FS::Record::dbdef->table('svc_acct_sm') ) ) {
522 return "Can't delete an account which has (svc_acct_sm) mail aliases!"
523 if $self->uid && qsearch( 'svc_acct_sm', { 'domuid' => $self->uid } );
526 return "Can't delete an account which is a (svc_forward) source!"
527 if qsearch( 'svc_forward', { 'srcsvc' => $self->svcnum } );
529 return "Can't delete an account which is a (svc_forward) destination!"
530 if qsearch( 'svc_forward', { 'dstsvc' => $self->svcnum } );
532 return "Can't delete an account with (svc_www) web service!"
533 if qsearch( 'svc_www', { 'usersvc' => $self->usersvc } );
535 # what about records in session ? (they should refer to history table)
537 local $SIG{HUP} = 'IGNORE';
538 local $SIG{INT} = 'IGNORE';
539 local $SIG{QUIT} = 'IGNORE';
540 local $SIG{TERM} = 'IGNORE';
541 local $SIG{TSTP} = 'IGNORE';
542 local $SIG{PIPE} = 'IGNORE';
544 my $oldAutoCommit = $FS::UID::AutoCommit;
545 local $FS::UID::AutoCommit = 0;
548 foreach my $cust_main_invoice (
549 qsearch( 'cust_main_invoice', { 'dest' => $self->svcnum } )
551 unless ( defined($cust_main_invoice) ) {
552 warn "WARNING: something's wrong with qsearch";
555 my %hash = $cust_main_invoice->hash;
556 $hash{'dest'} = $self->email;
557 my $new = new FS::cust_main_invoice \%hash;
558 my $error = $new->replace($cust_main_invoice);
560 $dbh->rollback if $oldAutoCommit;
565 foreach my $svc_domain (
566 qsearch( 'svc_domain', { 'catchall' => $self->svcnum } )
568 my %hash = new FS::svc_domain->hash;
569 $hash{'catchall'} = '';
570 my $new = new FS::svc_domain \%hash;
571 my $error = $new->replace($svc_domain);
573 $dbh->rollback if $oldAutoCommit;
578 foreach my $radius_usergroup (
579 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } )
581 my $error = $radius_usergroup->delete;
583 $dbh->rollback if $oldAutoCommit;
588 my $part_svc = $self->cust_svc->part_svc;
590 my $error = $self->SUPER::delete;
592 $dbh->rollback if $oldAutoCommit;
597 unless ( $noexport_hack ) {
598 foreach my $part_export ( $part_svc->part_export ) {
599 my $error = $part_export->export_delete($self);
601 $dbh->rollback if $oldAutoCommit;
602 return "exporting to ". $part_export->exporttype.
603 " (transaction rolled back): $error";
610 my( $username, $dir ) = (
614 if ( $username && $shellmachine && ! $nossh_hack ) {
615 my $queue = new FS::queue { 'job' => 'Net::SSH::ssh_cmd' };
616 $error = $queue->insert("root\@$shellmachine", eval qq("$userdel") );
618 $dbh->rollback if $oldAutoCommit;
619 return "queueing job (transaction rolled back): $error";
624 if ( $cyrus_server ) {
625 my $queue = new FS::queue { 'job' => 'FS::svc_acct::cyrus_delete' };
626 $error = $queue->insert($self->username);
628 $dbh->rollback if $oldAutoCommit;
629 return "queueing job (transaction rolled back): $error";
634 my $queue = new FS::queue { 'job' => 'FS::svc_acct::cp_delete' };
635 $error = $queue->insert($self->username);
637 $dbh->rollback if $oldAutoCommit;
638 return "queueing job (transaction rolled back): $error";
643 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_delete' };
644 $error = $queue->insert( $self->username, $self->domain );
646 $dbh->rollback if $oldAutoCommit;
647 return "queueing job (transaction rolled back): $error";
652 #end of old-style exports
654 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
659 my $username = shift;
661 my $client = Cyrus::IMAP::Admin->new($cyrus_server);
662 $client->authenticate(
663 -user => $cyrus_admin_user,
664 -mechanism => "login",
665 -password => $cyrus_admin_pass
668 my $rc = $client->setacl("user.$username", $cyrus_admin_user => 'all' );
669 my $error = $client->error;
670 die $error if $error;
672 $rc = $client->delete("user.$username");
673 $error = $client->error;
674 die $error if $error;
680 my( $username ) = @_;
681 my $app = new Net::APP ( $cp_server,
683 Password => $cp_pass,
689 $app->delete_mailbox(
690 Mailbox => $username,
694 die $app->message."\n" unless $app->ok;
697 sub vpopmail_delete {
698 my( $username, $domain ) = @_;
700 (open(VPASSWD, "$exportdir/domains/$domain/vpasswd")
701 and flock(VPASSWD,LOCK_EX)
702 ) or die "can't open $exportdir/domains/$domain/vpasswd: $!";
704 open(VPASSWDTMP, ">$exportdir/domains/$domain/vpasswd.tmp")
705 or die "Can't open $exportdir/domains/$domain/vpasswd.tmp: $!";
708 my ($mailbox, $rest) = split(':', $_);
709 print VPASSWDTMP $_ unless $username eq $mailbox;
714 rename "$exportdir/domains/$domain/vpasswd.tmp", "$exportdir/domains/$domain/vpasswd"
715 or die "Can't rename $exportdir/domains/$domain/vpasswd.tmp: $!";
717 flock(VPASSWD,LOCK_UN);
720 rmtree "$exportdir/domains/$domain/$username" or die "can't destroy Maildir";+
724 =item replace OLD_RECORD
726 Replaces OLD_RECORD with this one in the database. If there is an error,
727 returns the error, otherwise returns false.
729 The additional field I<usergroup> can optionally be defined; if so it should
730 contain an arrayref of group names. See L<FS::radius_usergroup>. (used in
731 sqlradius export only)
733 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
734 dir field has changed, the command(s) specified in the shellmachine-usermod
735 configuraiton file are added to the job queue (see L<FS::queue> and
736 L<freeside-queued>) to be executed on shellmachine via ssh. This behavior can
737 be surpressed by setting $FS::svc-acct::nossh_hack true. If the
738 shellmachine-userdel configuration file does not exist or is empty,
740 [ -d $old_dir ] && mv $old_dir $new_dir || (
744 find . -depth -print | cpio -pdm $new_dir;
746 chown -R $uid.$gid $new_dir;
750 is the default. This behaviour can be surpressed by setting
751 $FS::svc_acct::nossh_hack true.
756 my ( $new, $old ) = ( shift, shift );
759 return "Username in use"
760 if $old->username ne $new->username &&
761 qsearchs( 'svc_acct', { 'username' => $new->username,
762 'domsvc' => $new->domsvc,
765 #no warnings 'numeric'; #alas, a 5.006-ism
767 return "Can't change uid!" if $old->uid != $new->uid;
770 return "can't change username using Cyrus"
771 if $cyrus_server && $old->username ne $new->username;
773 #change homdir when we change username
774 $new->setfield('dir', '') if $old->username ne $new->username;
776 local $SIG{HUP} = 'IGNORE';
777 local $SIG{INT} = 'IGNORE';
778 local $SIG{QUIT} = 'IGNORE';
779 local $SIG{TERM} = 'IGNORE';
780 local $SIG{TSTP} = 'IGNORE';
781 local $SIG{PIPE} = 'IGNORE';
783 my $oldAutoCommit = $FS::UID::AutoCommit;
784 local $FS::UID::AutoCommit = 0;
787 $error = $new->SUPER::replace($old);
789 $dbh->rollback if $oldAutoCommit;
790 return $error if $error;
793 $old->usergroup( [ $old->radius_groups ] );
794 if ( $new->usergroup ) {
795 #(sorta) false laziness with FS::part_export::sqlradius::_export_replace
796 my @newgroups = @{$new->usergroup};
797 foreach my $oldgroup ( @{$old->usergroup} ) {
798 if ( grep { $oldgroup eq $_ } @newgroups ) {
799 @newgroups = grep { $oldgroup ne $_ } @newgroups;
802 my $radius_usergroup = qsearchs('radius_usergroup', {
803 svcnum => $old->svcnum,
804 groupname => $oldgroup,
806 my $error = $radius_usergroup->delete;
808 $dbh->rollback if $oldAutoCommit;
809 return "error deleting radius_usergroup $oldgroup: $error";
813 foreach my $newgroup ( @newgroups ) {
814 my $radius_usergroup = new FS::radius_usergroup ( {
815 svcnum => $new->svcnum,
816 groupname => $newgroup,
818 my $error = $radius_usergroup->insert;
820 $dbh->rollback if $oldAutoCommit;
821 return "error adding radius_usergroup $newgroup: $error";
828 unless ( $noexport_hack ) {
829 foreach my $part_export ( $new->cust_svc->part_svc->part_export ) {
830 my $error = $part_export->export_replace($new,$old);
832 $dbh->rollback if $oldAutoCommit;
833 return "exporting to ". $part_export->exporttype.
834 " (transaction rolled back): $error";
841 my ( $old_dir, $new_dir, $uid, $gid ) = (
842 $old->getfield('dir'),
843 $new->getfield('dir'),
844 $new->getfield('uid'),
845 $new->getfield('gid'),
847 if ( $old_dir && $new_dir && $old_dir ne $new_dir && ! $nossh_hack ) {
848 my $queue = new FS::queue {
849 'svcnum' => $new->svcnum,
850 'job' => 'Net::SSH::ssh_cmd'
852 $error = $queue->insert("root\@$shellmachine", eval qq("$usermod") );
854 $dbh->rollback if $oldAutoCommit;
855 return "queueing job (transaction rolled back): $error";
859 if ( $cp_server && $old->username ne $new->username ) {
860 my $queue = new FS::queue {
861 'svcnum' => $new->svcnum,
862 'job' => 'FS::svc_acct::cp_rename'
864 $error = $queue->insert( $old->username, $new->username );
866 $dbh->rollback if $oldAutoCommit;
867 return "queueing job (transaction rolled back): $error";
871 if ( $cp_server && $old->_password ne $new->_password ) {
872 my $queue = new FS::queue {
873 'svcnum' => $new->svcnum,
874 'job' => 'FS::svc_acct::cp_change'
876 $error = $queue->insert( $new->username, $new->_password );
878 $dbh->rollback if $oldAutoCommit;
879 return "queueing job (transaction rolled back): $error";
884 my $cpassword = crypt(
885 $new->_password,$saltset[int(rand(64))].$saltset[int(rand(64))]
888 if ($old->username ne $new->username || $old->domain ne $new->domain ) {
889 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_delete' };
890 $error = $queue->insert( $old->username, $old->domain );
891 my $queue2 = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_insert' };
892 $error = $queue2->insert( $new->username,
898 } elsif ($old->_password ne $new->_password) {
899 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_replace_password' };
900 $error = $queue->insert( $new->username, $cpassword, $new->domain );
903 $dbh->rollback if $oldAutoCommit;
904 return "queueing job (transaction rolled back): $error";
908 #end of old-style exports
910 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
915 my ( $old_username, $new_username ) = @_;
917 my $app = new Net::APP ( $cp_server,
919 Password => $cp_pass,
925 $app->rename_mailbox(
927 Old_Mailbox => $old_username,
928 New_Mailbox => $new_username,
931 die $app->message."\n" unless $app->ok;
936 my ( $username, $password ) = @_;
938 my $app = new Net::APP ( $cp_server,
940 Password => $cp_pass,
946 if ( $password =~ /^\*SUSPENDED\* (.*)$/ ) {
948 $app->set_mailbox_status(
950 Mailbox => $username,
955 $app->set_mailbox_status(
957 Mailbox => $username,
962 die $app->message."\n" unless $app->ok;
964 $app->change_mailbox(
966 Mailbox => $username,
967 Password => $password,
969 die $app->message."\n" unless $app->ok;
973 sub vpopmail_replace_password {
974 my( $username, $password, $domain ) = @_;
976 (open(VPASSWD, "$exportdir/domains/$domain/vpasswd")
977 and flock(VPASSWD,LOCK_EX)
978 ) or die "can't open $exportdir/domains/$domain/vpasswd: $!";
980 open(VPASSWDTMP, ">$exportdir/domains/$domain/vpasswd.tmp")
981 or die "Can't open $exportdir/domains/$domain/vpasswd.tmp: $!";
984 my ($mailbox, $pw, @rest) = split(':', $_);
985 print VPASSWDTMP $_ unless $username eq $mailbox;
986 print VPASSWDTMP join (':', ($mailbox, $password, @rest))
987 if $username eq $mailbox;
992 rename "$exportdir/domains/$domain/vpasswd.tmp", "$exportdir/domains/$domain/vpasswd"
993 or die "Can't rename $exportdir/domains/$domain/vpasswd.tmp: $!";
995 flock(VPASSWD,LOCK_UN);
998 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_sync' };
999 my $error = $queue->insert;
1000 die $error if $error;
1008 Suspends this account by prefixing *SUSPENDED* to the password. If there is an
1009 error, returns the error, otherwise returns false.
1011 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
1017 my %hash = $self->hash;
1018 unless ( $hash{_password} =~ /^\*SUSPENDED\* /
1019 || $hash{_password} eq '*'
1021 $hash{_password} = '*SUSPENDED* '.$hash{_password};
1022 my $new = new FS::svc_acct ( \%hash );
1023 $new->replace($self);
1025 ''; #no error (already suspended)
1031 Unsuspends this account by removing *SUSPENDED* from the password. If there is
1032 an error, returns the error, otherwise returns false.
1034 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
1040 my %hash = $self->hash;
1041 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
1042 $hash{_password} = $1;
1043 my $new = new FS::svc_acct ( \%hash );
1044 $new->replace($self);
1046 ''; #no error (already unsuspended)
1052 Just returns false (no error) for now.
1054 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
1058 Checks all fields to make sure this is a valid service. If there is an error,
1059 returns the error, otherwise returns false. Called by the insert and replace
1062 Sets any fixed values; see L<FS::part_svc>.
1069 my($recref) = $self->hashref;
1071 my $x = $self->setfixed;
1072 return $x unless ref($x);
1075 if ( $part_svc->part_svc_column('usergroup')->columnflag eq "F" ) {
1077 [ split(',', $part_svc->part_svc_column('usergroup')->columnvalue) ] );
1080 my $error = $self->ut_numbern('svcnum')
1081 || $self->ut_number('domsvc')
1082 || $self->ut_textn('sec_phrase')
1084 return $error if $error;
1086 my $ulen = $usernamemax || $self->dbdef_table->column('username')->length;
1087 if ( $username_uppercase ) {
1088 $recref->{username} =~ /^([a-z0-9_\-\.\&]{$usernamemin,$ulen})$/i
1089 or return "Illegal username: ". $recref->{username};
1090 $recref->{username} = $1;
1092 $recref->{username} =~ /^([a-z0-9_\-\.\&]{$usernamemin,$ulen})$/
1093 or return "Illegal username: ". $recref->{username};
1094 $recref->{username} = $1;
1097 if ( $username_letterfirst ) {
1098 $recref->{username} =~ /^[a-z]/ or return "Illegal username";
1099 } elsif ( $username_letter ) {
1100 $recref->{username} =~ /[a-z]/ or return "Illegal username";
1102 if ( $username_noperiod ) {
1103 $recref->{username} =~ /\./ and return "Illegal username";
1105 unless ( $username_ampersand ) {
1106 $recref->{username} =~ /\&/ and return "Illegal username";
1109 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum: ".$recref->{popnum};
1110 $recref->{popnum} = $1;
1111 return "Unknown popnum" unless
1112 ! $recref->{popnum} ||
1113 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
1115 unless ( $part_svc->part_svc_column('uid')->columnflag eq 'F' ) {
1117 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
1118 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
1120 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
1121 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
1122 #not all systems use gid=uid
1123 #you can set a fixed gid in part_svc
1125 return "Only root can have uid 0"
1126 if $recref->{uid} == 0 && $recref->{username} ne 'root';
1128 # $error = $self->ut_textn('finger');
1129 # return $error if $error;
1130 $self->getfield('finger') =~
1131 /^([\w \t\!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\*\<\>]*)$/
1132 or return "Illegal finger: ". $self->getfield('finger');
1133 $self->setfield('finger', $1);
1135 $recref->{dir} =~ /^([\/\w\-\.\&]*)$/
1136 or return "Illegal directory";
1137 $recref->{dir} = $1;
1138 return "Illegal directory"
1139 if $recref->{dir} =~ /(^|\/)\.+(\/|$)/; #no .. component
1140 return "Illegal directory"
1141 if $recref->{dir} =~ /\&/ && ! $username_ampersand;
1142 unless ( $recref->{dir} ) {
1143 $recref->{dir} = $dir_prefix . '/';
1144 if ( $dirhash > 0 ) {
1145 for my $h ( 1 .. $dirhash ) {
1146 $recref->{dir} .= substr($recref->{username}, $h-1, 1). '/';
1148 } elsif ( $dirhash < 0 ) {
1149 for my $h ( reverse $dirhash .. -1 ) {
1150 $recref->{dir} .= substr($recref->{username}, $h, 1). '/';
1153 $recref->{dir} .= $recref->{username};
1157 unless ( $recref->{username} eq 'sync' ) {
1158 if ( grep $_ eq $recref->{shell}, @shells ) {
1159 $recref->{shell} = (grep $_ eq $recref->{shell}, @shells)[0];
1161 return "Illegal shell \`". $self->shell. "\'; ".
1162 $conf->dir. "/shells contains: @shells";
1165 $recref->{shell} = '/bin/sync';
1168 $recref->{quota} =~ /^(\d*)$/ or return "Illegal quota (unimplemented)";
1169 $recref->{quota} = $1;
1172 $recref->{gid} ne '' ?
1173 return "Can't have gid without uid" : ( $recref->{gid}='' );
1174 $recref->{finger} ne '' ?
1175 return "Can't have finger-name without uid" : ( $recref->{finger}='' );
1176 $recref->{dir} ne '' ?
1177 return "Can't have directory without uid" : ( $recref->{dir}='' );
1178 $recref->{shell} ne '' ?
1179 return "Can't have shell without uid" : ( $recref->{shell}='' );
1180 $recref->{quota} ne '' ?
1181 return "Can't have quota without uid" : ( $recref->{quota}='' );
1184 unless ( $part_svc->part_svc_column('slipip')->columnflag eq 'F' ) {
1185 unless ( $recref->{slipip} eq '0e0' ) {
1186 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
1187 or return "Illegal slipip". $self->slipip;
1188 $recref->{slipip} = $1;
1190 $recref->{slipip} = '0e0';
1195 #arbitrary RADIUS stuff; allow ut_textn for now
1196 foreach ( grep /^radius_/, fields('svc_acct') ) {
1197 $self->ut_textn($_);
1200 #generate a password if it is blank
1201 $recref->{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) )
1202 unless ( $recref->{_password} );
1204 #if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,16})$/ ) {
1205 if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{$passwordmin,$passwordmax})$/ ) {
1206 $recref->{_password} = $1.$3;
1207 #uncomment this to encrypt password immediately upon entry, or run
1208 #bin/crypt_pw in cron to give new users a window during which their
1209 #password is available to techs, for faxing, etc. (also be aware of
1211 #$recref->{password} = $1.
1212 # crypt($3,$saltset[int(rand(64))].$saltset[int(rand(64))]
1214 } elsif ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([\w\.\/\$]{13,34})$/ ) {
1215 $recref->{_password} = $1.$3;
1216 } elsif ( $recref->{_password} eq '*' ) {
1217 $recref->{_password} = '*';
1218 } elsif ( $recref->{_password} eq '!!' ) {
1219 $recref->{_password} = '!!';
1221 #return "Illegal password";
1222 return "Illegal password: ". $recref->{_password};
1230 Depriciated, use radius_reply instead.
1235 carp "FS::svc_acct::radius depriciated, use radius_reply";
1236 $_[0]->radius_reply;
1241 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1242 reply attributes of this record.
1244 Note that this is now the preferred method for reading RADIUS attributes -
1245 accessing the columns directly is discouraged, as the column names are
1246 expected to change in the future.
1255 my($column, $attrib) = ($1, $2);
1256 #$attrib =~ s/_/\-/g;
1257 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1258 } grep { /^radius_/ && $self->getfield($_) } fields( $self->table );
1259 if ( $self->ip && $self->ip ne '0e0' ) {
1260 $reply{'Framed-IP-Address'} = $self->ip;
1267 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1268 check attributes of this record.
1270 Note that this is now the preferred method for reading RADIUS attributes -
1271 accessing the columns directly is discouraged, as the column names are
1272 expected to change in the future.
1278 ( 'Password' => $self->_password,
1281 my($column, $attrib) = ($1, $2);
1282 #$attrib =~ s/_/\-/g;
1283 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1284 } grep { /^rc_/ && $self->getfield($_) } fields( $self->table )
1290 Returns the domain associated with this account.
1296 if ( $self->domsvc ) {
1297 #$self->svc_domain->domain;
1298 my $svc_domain = $self->svc_domain
1299 or die "no svc_domain.svcnum for svc_acct.domsvc ". $self->domsvc;
1300 $svc_domain->domain;
1302 $mydomain or die "svc_acct.domsvc is null and no legacy domain config file";
1308 Returns the FS::svc_domain record for this account's domain (see
1316 ? $self->{'_domsvc'}
1317 : qsearchs( 'svc_domain', { 'svcnum' => $self->domsvc } );
1322 Returns the FS::cust_svc record for this account (see L<FS::cust_svc>).
1326 qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
1331 Returns an email address associated with the account.
1337 $self->username. '@'. $self->domain;
1340 =item seconds_since TIMESTAMP
1342 Returns the number of seconds this account has been online since TIMESTAMP.
1345 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
1346 L<Time::Local> and L<Date::Parse> for conversion functions.
1350 #note: POD here, implementation in FS::cust_svc
1353 $self->cust_svc->seconds_since(@_);
1358 Returns all RADIUS groups for this account (see L<FS::radius_usergroup>).
1364 map { $_->groupname }
1365 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } );
1372 =item radius_usergroup_selector GROUPS_ARRAYREF [ SELECTNAME ]
1376 sub radius_usergroup_selector {
1377 my $sel_groups = shift;
1378 my %sel_groups = map { $_=>1 } @$sel_groups;
1380 my $selectname = shift || 'radius_usergroup';
1383 my $sth = $dbh->prepare(
1384 'SELECT DISTINCT(groupname) FROM radius_usergroup ORDER BY groupname'
1385 ) or die $dbh->errstr;
1386 $sth->execute() or die $sth->errstr;
1387 my @all_groups = map { $_->[0] } @{$sth->fetchall_arrayref};
1391 function ${selectname}_doadd(object) {
1392 var myvalue = object.${selectname}_add.value;
1393 var optionName = new Option(myvalue,myvalue,false,true);
1394 var length = object.$selectname.length;
1395 object.$selectname.options[length] = optionName;
1396 object.${selectname}_add.value = "";
1399 <SELECT MULTIPLE NAME="$selectname">
1402 foreach my $group ( @all_groups ) {
1404 if ( $sel_groups{$group} ) {
1405 $html .= ' SELECTED';
1406 $sel_groups{$group} = 0;
1408 $html .= ">$group</OPTION>\n";
1410 foreach my $group ( grep { $sel_groups{$_} } keys %sel_groups ) {
1411 $html .= "<OPTION SELECTED>$group</OPTION>\n";
1413 $html .= '</SELECT>';
1415 $html .= qq!<BR><INPUT TYPE="text" NAME="${selectname}_add">!.
1416 qq!<INPUT TYPE="button" VALUE="Add new group" onClick="${selectname}_doadd(this.form)">!;
1423 The $recref stuff in sub check should be cleaned up.
1425 The suspend, unsuspend and cancel methods update the database, but not the
1426 current object. This is probably a bug as it's unexpected and
1429 radius_usergroup_selector? putting web ui components in here? they should
1430 probably live somewhere else...
1434 L<FS::svc_Common>, edit/part_svc.cgi from an installed web interface,
1435 export.html from the base documentation, L<FS::Record>, L<FS::Conf>,
1436 L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, L<FS::queue>,
1437 L<freeside-queued>), L<Net::SSH>, L<ssh>, L<FS::svc_acct_pop>,
1438 schema.html from the base documentation.