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>
195 =item domsvc - service number of svc_domain with which to associate
205 Creates a new account. To add the account to the database, see L<"insert">.
209 sub table { 'svc_acct'; }
213 Adds this account to the database. If there is an error, returns the error,
214 otherwise returns false.
216 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
217 defined. An FS::cust_svc record will be created and inserted.
219 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
220 username, uid, and dir fields are defined, the command(s) specified in
221 the shellmachine-useradd configuration are added to the job queue (see
222 L<FS::queue> and L<freeside-queued>) to be exectued on shellmachine via ssh.
223 This behaviour can be surpressed by setting $FS::svc_acct::nossh_hack true.
224 If the shellmachine-useradd configuration file does not exist,
226 useradd -d $dir -m -s $shell -u $uid $username
228 is the default. If the shellmachine-useradd configuration file exists but
231 cp -pr /etc/skel $dir; chown -R $uid.$gid $dir
233 is the default instead. Otherwise the contents of the file are treated as
234 a double-quoted perl string, with the following variables available:
235 $username, $uid, $gid, $dir, and $shell.
237 (TODOC: cyrus config file, L<FS::queue> and L<freeside-queued>)
239 (TODOC: new exports! $noexport_hack)
247 local $SIG{HUP} = 'IGNORE';
248 local $SIG{INT} = 'IGNORE';
249 local $SIG{QUIT} = 'IGNORE';
250 local $SIG{TERM} = 'IGNORE';
251 local $SIG{TSTP} = 'IGNORE';
252 local $SIG{PIPE} = 'IGNORE';
254 my $oldAutoCommit = $FS::UID::AutoCommit;
255 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;
293 unless ( $noexport_hack ) {
294 foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
295 my $error = $part_export->export_insert($self);
297 $dbh->rollback if $oldAutoCommit;
298 return "exporting to ". $part_export->exporttype.
299 " (transaction rolled back): $error";
306 my( $username, $uid, $gid, $dir, $shell ) = (
313 if ( $username && $uid && $dir && $shellmachine && ! $nossh_hack ) {
314 my $queue = new FS::queue {
315 'svcnum' => $self->svcnum,
316 'job' => 'Net::SSH::ssh_cmd',
318 $error = $queue->insert("root\@$shellmachine", eval qq("$useradd") );
320 $dbh->rollback if $oldAutoCommit;
321 return "queueing job (transaction rolled back): $error";
325 if ( $cyrus_server ) {
326 my $queue = new FS::queue {
327 'svcnum' => $self->svcnum,
328 'job' => 'FS::svc_acct::cyrus_insert',
330 $error = $queue->insert($self->username, $self->quota);
332 $dbh->rollback if $oldAutoCommit;
333 return "queueing job (transaction rolled back): $error";
338 my $queue = new FS::queue {
339 'svcnum' => $self->svcnum,
340 'job' => 'FS::svc_acct::cp_insert'
342 $error = $queue->insert($self->username, $self->_password);
344 $dbh->rollback if $oldAutoCommit;
345 return "queueing job (transaction rolled back): $error";
353 'svcnum' => $self->svcnum,
354 'job' => 'FS::svc_acct::vpopmail_insert'
356 $error = $vpopmail_queue->insert( $self->username,
357 crypt($self->_password,$saltset[int(rand(64))].$saltset[int(rand(64))]),
362 $dbh->rollback if $oldAutoCommit;
363 return "queueing job (transaction rolled back): $error";
368 #end of old-style exports
370 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
375 my( $username, $quota ) = @_;
377 warn "cyrus_insert: starting for user $username, quota $quota\n";
379 warn "cyrus_insert: connecting to $cyrus_server\n";
380 my $client = Cyrus::IMAP::Admin->new($cyrus_server);
382 warn "cyrus_insert: authentication as $cyrus_admin_user\n";
383 $client->authenticate(
384 -user => $cyrus_admin_user,
385 -mechanism => "login",
386 -password => $cyrus_admin_pass
389 warn "cyrus_insert: creating user.$username\n";
390 my $rc = $client->create("user.$username");
391 my $error = $client->error;
392 die "cyrus_insert: error creating user.$username: $error" if $error;
394 warn "cyrus_insert: setacl user.$username, $username => all\n";
395 $rc = $client->setacl("user.$username", $username => 'all' );
396 $error = $client->error;
397 die "cyrus_insert: error setacl user.$username: $error" if $error;
400 warn "cyrus_insert: setquota user.$username, STORAGE => $quota\n";
401 $rc = $client->setquota("user.$username", 'STORAGE' => $quota );
402 $error = $client->error;
403 die "cyrus_insert: error setquota user.$username: $error" if $error;
410 my( $username, $password ) = @_;
412 my $app = new Net::APP ( $cp_server,
414 Password => $cp_pass,
420 $app->create_mailbox(
421 Mailbox => $username,
422 Password => $password,
423 Workgroup => $cp_workgroup,
427 die $app->message."\n" unless $app->ok;
430 sub vpopmail_insert {
431 my( $username, $password, $domain, $vpopdir ) = @_;
433 (open(VPASSWD, ">>$exportdir/domains/$domain/vpasswd")
434 and flock(VPASSWD,LOCK_EX)
435 ) or die "can't open vpasswd file for $username\@$domain: $exportdir/domains/$domain/vpasswd";
436 print VPASSWD join(":",
442 "$vpopdir/domains/$domain/$username",
446 flock(VPASSWD,LOCK_UN);
449 mkdir "$exportdir/domains/$domain/$username", 0700 or die "can't create Maildir";
450 mkdir "$exportdir/domains/$domain/$username/Maildir", 0700 or die "can't create Maildir";
451 mkdir "$exportdir/domains/$domain/$username/Maildir/cur", 0700 or die "can't create Maildir";
452 mkdir "$exportdir/domains/$domain/$username/Maildir/new", 0700 or die "can't create Maildir";
453 mkdir "$exportdir/domains/$domain/$username/Maildir/tmp", 0700 or die "can't create Maildir";
455 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_sync' };
456 my $error = $queue->insert;
457 die $error if $error;
464 my (@vpopmailmachines) = $conf->config('vpopmailmachines');
465 my ($machine, $dir, $uid, $gid) = split (/\s+/, $vpopmailmachines[0]);
468 my @args = ("$rsync", "-rlpt", "-e", "$ssh", "domains/", "vpopmail\@$machine:$vpopdir/domains/");
469 system {$args[0]} @args;
475 Deletes this account from the database. If there is an error, returns the
476 error, otherwise returns false.
478 The corresponding FS::cust_svc record will be deleted as well.
480 If the configuration value (see L<FS::Conf>) shellmachine exists, the
481 command(s) specified in the shellmachine-userdel configuration file are
482 added to the job queue (see L<FS::queue> and L<freeside-queued>) to be executed
483 on shellmachine via ssh. This behavior can be surpressed by setting
484 $FS::svc_acct::nossh_hack true. If the shellmachine-userdel configuration
489 is the default. If the shellmachine-userdel configuration file exists but
494 is the default instead. Otherwise the contents of the file are treated as a
495 double-quoted perl string, with the following variables available:
498 (TODOC: cyrus config file)
500 (TODOC: new exports! $noexport_hack)
507 if ( defined( $FS::Record::dbdef->table('svc_acct_sm') ) ) {
508 return "Can't delete an account which has (svc_acct_sm) mail aliases!"
509 if $self->uid && qsearch( 'svc_acct_sm', { 'domuid' => $self->uid } );
512 return "Can't delete an account which is a (svc_forward) source!"
513 if qsearch( 'svc_forward', { 'srcsvc' => $self->svcnum } );
515 return "Can't delete an account which is a (svc_forward) destination!"
516 if qsearch( 'svc_forward', { 'dstsvc' => $self->svcnum } );
518 return "Can't delete an account with (svc_www) web service!"
519 if qsearch( 'svc_www', { 'usersvc' => $self->usersvc } );
521 # what about records in session ? (they should refer to history table)
523 local $SIG{HUP} = 'IGNORE';
524 local $SIG{INT} = 'IGNORE';
525 local $SIG{QUIT} = 'IGNORE';
526 local $SIG{TERM} = 'IGNORE';
527 local $SIG{TSTP} = 'IGNORE';
528 local $SIG{PIPE} = 'IGNORE';
530 my $oldAutoCommit = $FS::UID::AutoCommit;
531 local $FS::UID::AutoCommit = 0;
534 foreach my $cust_main_invoice (
535 qsearch( 'cust_main_invoice', { 'dest' => $self->svcnum } )
537 unless ( defined($cust_main_invoice) ) {
538 warn "WARNING: something's wrong with qsearch";
541 my %hash = $cust_main_invoice->hash;
542 $hash{'dest'} = $self->email;
543 my $new = new FS::cust_main_invoice \%hash;
544 my $error = $new->replace($cust_main_invoice);
546 $dbh->rollback if $oldAutoCommit;
551 foreach my $svc_domain (
552 qsearch( 'svc_domain', { 'catchall' => $self->svcnum } )
554 my %hash = new FS::svc_domain->hash;
555 $hash{'catchall'} = '';
556 my $new = new FS::svc_domain \%hash;
557 my $error = $new->replace($svc_domain);
559 $dbh->rollback if $oldAutoCommit;
564 my $error = $self->SUPER::delete;
566 $dbh->rollback if $oldAutoCommit;
571 unless ( $noexport_hack ) {
572 foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
573 my $error = $part_export->export_delete($self);
575 $dbh->rollback if $oldAutoCommit;
576 return "exporting to ". $part_export->exporttype.
577 " (transaction rolled back): $error";
584 my( $username, $dir ) = (
588 if ( $username && $shellmachine && ! $nossh_hack ) {
589 my $queue = new FS::queue { 'job' => 'Net::SSH::ssh_cmd' };
590 $error = $queue->insert("root\@$shellmachine", eval qq("$userdel") );
592 $dbh->rollback if $oldAutoCommit;
593 return "queueing job (transaction rolled back): $error";
598 if ( $cyrus_server ) {
599 my $queue = new FS::queue { 'job' => 'FS::svc_acct::cyrus_delete' };
600 $error = $queue->insert($self->username);
602 $dbh->rollback if $oldAutoCommit;
603 return "queueing job (transaction rolled back): $error";
608 my $queue = new FS::queue { 'job' => 'FS::svc_acct::cp_delete' };
609 $error = $queue->insert($self->username);
611 $dbh->rollback if $oldAutoCommit;
612 return "queueing job (transaction rolled back): $error";
617 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_delete' };
618 $error = $queue->insert( $self->username, $self->domain );
620 $dbh->rollback if $oldAutoCommit;
621 return "queueing job (transaction rolled back): $error";
626 #end of old-style exports
628 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
633 my $username = shift;
635 my $client = Cyrus::IMAP::Admin->new($cyrus_server);
636 $client->authenticate(
637 -user => $cyrus_admin_user,
638 -mechanism => "login",
639 -password => $cyrus_admin_pass
642 my $rc = $client->setacl("user.$username", $cyrus_admin_user => 'all' );
643 my $error = $client->error;
644 die $error if $error;
646 $rc = $client->delete("user.$username");
647 $error = $client->error;
648 die $error if $error;
654 my( $username ) = @_;
655 my $app = new Net::APP ( $cp_server,
657 Password => $cp_pass,
663 $app->delete_mailbox(
664 Mailbox => $username,
668 die $app->message."\n" unless $app->ok;
671 sub vpopmail_delete {
672 my( $username, $domain ) = @_;
674 (open(VPASSWD, "$exportdir/domains/$domain/vpasswd")
675 and flock(VPASSWD,LOCK_EX)
676 ) or die "can't open $exportdir/domains/$domain/vpasswd: $!";
678 open(VPASSWDTMP, ">$exportdir/domains/$domain/vpasswd.tmp")
679 or die "Can't open $exportdir/domains/$domain/vpasswd.tmp: $!";
682 my ($mailbox, $rest) = split(':', $_);
683 print VPASSWDTMP $_ unless $username eq $mailbox;
688 rename "$exportdir/domains/$domain/vpasswd.tmp", "$exportdir/domains/$domain/vpasswd"
689 or die "Can't rename $exportdir/domains/$domain/vpasswd.tmp: $!";
691 flock(VPASSWD,LOCK_UN);
694 rmtree "$exportdir/domains/$domain/$username" or die "can't destroy Maildir";+
698 =item replace OLD_RECORD
700 Replaces OLD_RECORD with this one in the database. If there is an error,
701 returns the error, otherwise returns false.
703 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
704 dir field has changed, the command(s) specified in the shellmachine-usermod
705 configuraiton file are added to the job queue (see L<FS::queue> and
706 L<freeside-queued>) to be executed on shellmachine via ssh. This behavior can
707 be surpressed by setting $FS::svc-acct::nossh_hack true. If the
708 shellmachine-userdel configuration file does not exist or is empty,
710 [ -d $old_dir ] && mv $old_dir $new_dir || (
714 find . -depth -print | cpio -pdm $new_dir;
716 chown -R $uid.$gid $new_dir;
720 is the default. This behaviour can be surpressed by setting
721 $FS::svc_acct::nossh_hack true.
726 my ( $new, $old ) = ( shift, shift );
729 return "Username in use"
730 if $old->username ne $new->username &&
731 qsearchs( 'svc_acct', { 'username' => $new->username,
732 'domsvc' => $new->domsvc,
735 #no warnings 'numeric'; #alas, a 5.006-ism
737 return "Can't change uid!" if $old->uid != $new->uid;
740 return "can't change username using Cyrus"
741 if $cyrus_server && $old->username ne $new->username;
743 #change homdir when we change username
744 $new->setfield('dir', '') if $old->username ne $new->username;
746 local $SIG{HUP} = 'IGNORE';
747 local $SIG{INT} = 'IGNORE';
748 local $SIG{QUIT} = 'IGNORE';
749 local $SIG{TERM} = 'IGNORE';
750 local $SIG{TSTP} = 'IGNORE';
751 local $SIG{PIPE} = 'IGNORE';
753 my $oldAutoCommit = $FS::UID::AutoCommit;
754 local $FS::UID::AutoCommit = 0;
757 $error = $new->SUPER::replace($old);
759 $dbh->rollback if $oldAutoCommit;
760 return $error if $error;
764 unless ( $noexport_hack ) {
765 foreach my $part_export ( $new->cust_svc->part_svc->part_export ) {
766 my $error = $part_export->export_replace($new,$old);
768 $dbh->rollback if $oldAutoCommit;
769 return "exporting to ". $part_export->exporttype.
770 " (transaction rolled back): $error";
777 my ( $old_dir, $new_dir, $uid, $gid ) = (
778 $old->getfield('dir'),
779 $new->getfield('dir'),
780 $new->getfield('uid'),
781 $new->getfield('gid'),
783 if ( $old_dir && $new_dir && $old_dir ne $new_dir && ! $nossh_hack ) {
784 my $queue = new FS::queue {
785 'svcnum' => $new->svcnum,
786 'job' => 'Net::SSH::ssh_cmd'
788 $error = $queue->insert("root\@$shellmachine", eval qq("$usermod") );
790 $dbh->rollback if $oldAutoCommit;
791 return "queueing job (transaction rolled back): $error";
795 if ( $cp_server && $old->username ne $new->username ) {
796 my $queue = new FS::queue {
797 'svcnum' => $new->svcnum,
798 'job' => 'FS::svc_acct::cp_rename'
800 $error = $queue->insert( $old->username, $new->username );
802 $dbh->rollback if $oldAutoCommit;
803 return "queueing job (transaction rolled back): $error";
807 if ( $cp_server && $old->_password ne $new->_password ) {
808 my $queue = new FS::queue {
809 'svcnum' => $new->svcnum,
810 'job' => 'FS::svc_acct::cp_change'
812 $error = $queue->insert( $new->username, $new->_password );
814 $dbh->rollback if $oldAutoCommit;
815 return "queueing job (transaction rolled back): $error";
820 my $cpassword = crypt(
821 $new->_password,$saltset[int(rand(64))].$saltset[int(rand(64))]
824 if ($old->username ne $new->username || $old->domain ne $new->domain ) {
825 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_delete' };
826 $error = $queue->insert( $old->username, $old->domain );
827 my $queue2 = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_insert' };
828 $error = $queue2->insert( $new->username,
834 } elsif ($old->_password ne $new->_password) {
835 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_replace_password' };
836 $error = $queue->insert( $new->username, $cpassword, $new->domain );
839 $dbh->rollback if $oldAutoCommit;
840 return "queueing job (transaction rolled back): $error";
844 #end of old-style exports
846 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
851 my ( $old_username, $new_username ) = @_;
853 my $app = new Net::APP ( $cp_server,
855 Password => $cp_pass,
861 $app->rename_mailbox(
863 Old_Mailbox => $old_username,
864 New_Mailbox => $new_username,
867 die $app->message."\n" unless $app->ok;
872 my ( $username, $password ) = @_;
874 my $app = new Net::APP ( $cp_server,
876 Password => $cp_pass,
882 if ( $password =~ /^\*SUSPENDED\* (.*)$/ ) {
884 $app->set_mailbox_status(
886 Mailbox => $username,
891 $app->set_mailbox_status(
893 Mailbox => $username,
898 die $app->message."\n" unless $app->ok;
900 $app->change_mailbox(
902 Mailbox => $username,
903 Password => $password,
905 die $app->message."\n" unless $app->ok;
909 sub vpopmail_replace_password {
910 my( $username, $password, $domain ) = @_;
912 (open(VPASSWD, "$exportdir/domains/$domain/vpasswd")
913 and flock(VPASSWD,LOCK_EX)
914 ) or die "can't open $exportdir/domains/$domain/vpasswd: $!";
916 open(VPASSWDTMP, ">$exportdir/domains/$domain/vpasswd.tmp")
917 or die "Can't open $exportdir/domains/$domain/vpasswd.tmp: $!";
920 my ($mailbox, $pw, @rest) = split(':', $_);
921 print VPASSWDTMP $_ unless $username eq $mailbox;
922 print VPASSWDTMP join (':', ($mailbox, $password, @rest))
923 if $username eq $mailbox;
928 rename "$exportdir/domains/$domain/vpasswd.tmp", "$exportdir/domains/$domain/vpasswd"
929 or die "Can't rename $exportdir/domains/$domain/vpasswd.tmp: $!";
931 flock(VPASSWD,LOCK_UN);
934 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_sync' };
935 my $error = $queue->insert;
936 die $error if $error;
944 Suspends this account by prefixing *SUSPENDED* to the password. If there is an
945 error, returns the error, otherwise returns false.
947 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
953 my %hash = $self->hash;
954 unless ( $hash{_password} =~ /^\*SUSPENDED\* /
955 || $hash{_password} eq '*'
957 $hash{_password} = '*SUSPENDED* '.$hash{_password};
958 my $new = new FS::svc_acct ( \%hash );
959 $new->replace($self);
961 ''; #no error (already suspended)
967 Unsuspends this account by removing *SUSPENDED* from the password. If there is
968 an error, returns the error, otherwise returns false.
970 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
976 my %hash = $self->hash;
977 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
978 $hash{_password} = $1;
979 my $new = new FS::svc_acct ( \%hash );
980 $new->replace($self);
982 ''; #no error (already unsuspended)
988 Just returns false (no error) for now.
990 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
994 Checks all fields to make sure this is a valid service. If there is an error,
995 returns the error, otherwise returns false. Called by the insert and replace
998 Sets any fixed values; see L<FS::part_svc>.
1005 my($recref) = $self->hashref;
1007 my $x = $self->setfixed;
1008 return $x unless ref($x);
1011 my $error = $self->ut_numbern('svcnum')
1012 || $self->ut_number('domsvc')
1014 return $error if $error;
1016 my $ulen = $usernamemax || $self->dbdef_table->column('username')->length;
1017 if ( $username_uppercase ) {
1018 $recref->{username} =~ /^([a-z0-9_\-\.\&]{$usernamemin,$ulen})$/i
1019 or return "Illegal username: ". $recref->{username};
1020 $recref->{username} = $1;
1022 $recref->{username} =~ /^([a-z0-9_\-\.\&]{$usernamemin,$ulen})$/
1023 or return "Illegal username: ". $recref->{username};
1024 $recref->{username} = $1;
1027 if ( $username_letterfirst ) {
1028 $recref->{username} =~ /^[a-z]/ or return "Illegal username";
1029 } elsif ( $username_letter ) {
1030 $recref->{username} =~ /[a-z]/ or return "Illegal username";
1032 if ( $username_noperiod ) {
1033 $recref->{username} =~ /\./ and return "Illegal username";
1035 unless ( $username_ampersand ) {
1036 $recref->{username} =~ /\&/ and return "Illegal username";
1039 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum: ".$recref->{popnum};
1040 $recref->{popnum} = $1;
1041 return "Unknown popnum" unless
1042 ! $recref->{popnum} ||
1043 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
1045 unless ( $part_svc->part_svc_column('uid')->columnflag eq 'F' ) {
1047 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
1048 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
1050 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
1051 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
1052 #not all systems use gid=uid
1053 #you can set a fixed gid in part_svc
1055 return "Only root can have uid 0"
1056 if $recref->{uid} == 0 && $recref->{username} ne 'root';
1058 # $error = $self->ut_textn('finger');
1059 # return $error if $error;
1060 $self->getfield('finger') =~
1061 /^([\w \t\!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\*\<\>]*)$/
1062 or return "Illegal finger: ". $self->getfield('finger');
1063 $self->setfield('finger', $1);
1065 $recref->{dir} =~ /^([\/\w\-\.\&]*)$/
1066 or return "Illegal directory";
1067 $recref->{dir} = $1;
1068 return "Illegal directory"
1069 if $recref->{dir} =~ /(^|\/)\.+(\/|$)/; #no .. component
1070 return "Illegal directory"
1071 if $recref->{dir} =~ /\&/ && ! $username_ampersand;
1072 unless ( $recref->{dir} ) {
1073 $recref->{dir} = $dir_prefix . '/';
1074 if ( $dirhash > 0 ) {
1075 for my $h ( 1 .. $dirhash ) {
1076 $recref->{dir} .= substr($recref->{username}, $h-1, 1). '/';
1078 } elsif ( $dirhash < 0 ) {
1079 for my $h ( reverse $dirhash .. -1 ) {
1080 $recref->{dir} .= substr($recref->{username}, $h, 1). '/';
1083 $recref->{dir} .= $recref->{username};
1087 unless ( $recref->{username} eq 'sync' ) {
1088 if ( grep $_ eq $recref->{shell}, @shells ) {
1089 $recref->{shell} = (grep $_ eq $recref->{shell}, @shells)[0];
1091 return "Illegal shell \`". $self->shell. "\'; ".
1092 $conf->dir. "/shells contains: @shells";
1095 $recref->{shell} = '/bin/sync';
1098 $recref->{quota} =~ /^(\d*)$/ or return "Illegal quota (unimplemented)";
1099 $recref->{quota} = $1;
1102 $recref->{gid} ne '' ?
1103 return "Can't have gid without uid" : ( $recref->{gid}='' );
1104 $recref->{finger} ne '' ?
1105 return "Can't have finger-name without uid" : ( $recref->{finger}='' );
1106 $recref->{dir} ne '' ?
1107 return "Can't have directory without uid" : ( $recref->{dir}='' );
1108 $recref->{shell} ne '' ?
1109 return "Can't have shell without uid" : ( $recref->{shell}='' );
1110 $recref->{quota} ne '' ?
1111 return "Can't have quota without uid" : ( $recref->{quota}='' );
1114 unless ( $part_svc->part_svc_column('slipip')->columnflag eq 'F' ) {
1115 unless ( $recref->{slipip} eq '0e0' ) {
1116 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
1117 or return "Illegal slipip". $self->slipip;
1118 $recref->{slipip} = $1;
1120 $recref->{slipip} = '0e0';
1125 #arbitrary RADIUS stuff; allow ut_textn for now
1126 foreach ( grep /^radius_/, fields('svc_acct') ) {
1127 $self->ut_textn($_);
1130 #generate a password if it is blank
1131 $recref->{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) )
1132 unless ( $recref->{_password} );
1134 #if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,16})$/ ) {
1135 if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{$passwordmin,$passwordmax})$/ ) {
1136 $recref->{_password} = $1.$3;
1137 #uncomment this to encrypt password immediately upon entry, or run
1138 #bin/crypt_pw in cron to give new users a window during which their
1139 #password is available to techs, for faxing, etc. (also be aware of
1141 #$recref->{password} = $1.
1142 # crypt($3,$saltset[int(rand(64))].$saltset[int(rand(64))]
1144 } elsif ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([\w\.\/\$]{13,34})$/ ) {
1145 $recref->{_password} = $1.$3;
1146 } elsif ( $recref->{_password} eq '*' ) {
1147 $recref->{_password} = '*';
1148 } elsif ( $recref->{_password} eq '!!' ) {
1149 $recref->{_password} = '!!';
1151 #return "Illegal password";
1152 return "Illegal password: ". $recref->{_password};
1160 Depriciated, use radius_reply instead.
1165 carp "FS::svc_acct::radius depriciated, use radius_reply";
1166 $_[0]->radius_reply;
1171 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1172 reply attributes of this record.
1174 Note that this is now the preferred method for reading RADIUS attributes -
1175 accessing the columns directly is discouraged, as the column names are
1176 expected to change in the future.
1185 my($column, $attrib) = ($1, $2);
1186 #$attrib =~ s/_/\-/g;
1187 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1188 } grep { /^radius_/ && $self->getfield($_) } fields( $self->table );
1189 if ( $self->ip && $self->ip ne '0e0' ) {
1190 $reply{'Framed-IP-Address'} = $self->ip;
1197 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1198 check attributes of this record.
1200 Note that this is now the preferred method for reading RADIUS attributes -
1201 accessing the columns directly is discouraged, as the column names are
1202 expected to change in the future.
1208 ( 'Password' => $self->_password,
1211 my($column, $attrib) = ($1, $2);
1212 #$attrib =~ s/_/\-/g;
1213 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1214 } grep { /^rc_/ && $self->getfield($_) } fields( $self->table )
1220 Returns the domain associated with this account.
1226 if ( $self->domsvc ) {
1227 #$self->svc_domain->domain;
1228 my $svc_domain = $self->svc_domain
1229 or die "no svc_domain.svcnum for svc_acct.domsvc ". $self->domsvc;
1230 $svc_domain->domain;
1232 $mydomain or die "svc_acct.domsvc is null and no legacy domain config file";
1238 Returns the FS::svc_domain record for this account's domain (see
1246 ? $self->{'_domsvc'}
1247 : qsearchs( 'svc_domain', { 'svcnum' => $self->domsvc } );
1252 Returns the FS::cust_svc record for this account (see L<FS::cust_svc>).
1256 qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
1261 Returns an email address associated with the account.
1267 $self->username. '@'. $self->domain;
1270 =item seconds_since TIMESTAMP
1272 Returns the number of seconds this account has been online since TIMESTAMP.
1275 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
1276 L<Time::Local> and L<Date::Parse> for conversion functions.
1280 #note: POD here, implementation in FS::cust_svc
1283 $self->cust_svc->seconds_since(@_);
1288 Returns all RADIUS groups for this account (see L<FS::radius_usergroup>).
1294 map { $_->groupname }
1295 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } );
1302 =item radius_usergroup_selector GROUPS_ARRAYREF
1306 sub radius_usergroup_selector {
1307 my $sel_groups = shift;
1308 my %sel_groups = map { $_=>1 } @$sel_groups;
1310 my $selectname = shift || 'radius_usergroup';
1313 my $sth = $dbh->prepare(
1314 'SELECT DISTINCT(groupname) FROM radius_usergroup ORDER BY groupname'
1315 ) or die $dbh->errstr;
1316 $sth->execute() or die $sth->errstr;
1317 my @all_groups = map { $_->[0] } @{$sth->fetchall_arrayref};
1321 function ${selectname}_doadd(object) {
1322 var myvalue = object.${selectname}_add.value;
1323 var optionName = new Option(myvalue,myvalue,false,true);
1324 var length = object.$selectname.length;
1325 object.$selectname.options[length] = optionName;
1328 <SELECT MULTIPLE NAME="$selectname">
1331 foreach my $group ( @all_groups ) {
1333 $html .= ' SELECTED' if $sel_groups{$group};
1334 $html .= ">$group</OPTION>\n";
1336 $html .= '</SELECT>';
1338 $html .= qq!<BR><INPUT TYPE="text" NAME="${selectname}_add">!.
1339 qq!<INPUT TYPE="button" VALUE="Add new group" onClick="${selectname}_doadd(this.form)">!;
1346 The $recref stuff in sub check should be cleaned up.
1348 The suspend, unsuspend and cancel methods update the database, but not the
1349 current object. This is probably a bug as it's unexpected and
1352 radius_usergroup_selector? putting web ui components in here? they should
1353 probably live somewhere else...
1357 L<FS::svc_Common>, edit/part_svc.cgi from an installed web interface,
1358 export.html from the base documentation, L<FS::Record>, L<FS::Conf>,
1359 L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, L<FS::queue>,
1360 L<freeside-queued>), L<Net::SSH>, L<ssh>, L<FS::svc_acct_pop>,
1361 schema.html from the base documentation.