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;
31 @ISA = qw( FS::svc_Common );
33 #ask FS::UID to run this stuff for us later
34 $FS::UID::callback{'FS::svc_acct'} = sub {
38 $dir_prefix = $conf->config('home');
39 @shells = $conf->config('shells');
40 $shellmachine = $conf->config('shellmachine');
41 $usernamemin = $conf->config('usernamemin') || 2;
42 $usernamemax = $conf->config('usernamemax');
43 $passwordmin = $conf->config('passwordmin') || 6;
44 $passwordmax = $conf->config('passwordmax') || 8;
45 if ( $shellmachine ) {
46 if ( $conf->exists('shellmachine-useradd') ) {
47 $useradd = join("\n", $conf->config('shellmachine-useradd') )
48 || 'cp -pr /etc/skel $dir; chown -R $uid.$gid $dir';
50 $useradd = 'useradd -d $dir -m -s $shell -u $uid $username';
52 if ( $conf->exists('shellmachine-userdel') ) {
53 $userdel = join("\n", $conf->config('shellmachine-userdel') )
56 $userdel = 'userdel $username';
58 $usermod = join("\n", $conf->config('shellmachine-usermod') )
59 || '[ -d $old_dir ] && mv $old_dir $new_dir || ( '.
60 'chmod u+t $old_dir; mkdir $new_dir; cd $old_dir; '.
61 'find . -depth -print | cpio -pdm $new_dir; '.
62 'chmod u-t $new_dir; chown -R $uid.$gid $new_dir; '.
66 $username_letter = $conf->exists('username-letter');
67 $username_letterfirst = $conf->exists('username-letterfirst');
68 $username_noperiod = $conf->exists('username-noperiod');
69 $username_uppercase = $conf->exists('username-uppercase');
70 $username_ampersand = $conf->exists('username-ampersand');
71 $mydomain = $conf->config('domain');
72 if ( $conf->exists('cyrus') ) {
73 ($cyrus_server, $cyrus_admin_user, $cyrus_admin_pass) =
74 $conf->config('cyrus');
75 eval "use Cyrus::IMAP::Admin;"
78 $cyrus_admin_user = '';
79 $cyrus_admin_pass = '';
81 if ( $conf->exists('cp_app') ) {
82 ($cp_server, $cp_user, $cp_pass, $cp_workgroup) =
83 $conf->config('cp_app');
92 $dirhash = $conf->config('dirhash') || 0;
93 $exportdir = "/usr/local/etc/freeside/export." . datasrc;
94 if ( $conf->exists('vpopmailmachines') ) {
95 my (@vpopmailmachines) = $conf->config('vpopmailmachines');
96 my ($machine, $dir, $uid, $gid) = split (/\s+/, $vpopmailmachines[0]);
103 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
104 @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '!', '.', ',' );
106 #not needed in 5.004 #srand($$|time);
110 my ( $hashref, $cache ) = @_;
111 if ( $hashref->{'svc_acct_svcnum'} ) {
112 $self->{'_domsvc'} = FS::svc_domain->new( {
113 'svcnum' => $hashref->{'domsvc'},
114 'domain' => $hashref->{'svc_acct_domain'},
115 'catchall' => $hashref->{'svc_acct_catchall'},
122 FS::svc_acct - Object methods for svc_acct records
128 $record = new FS::svc_acct \%hash;
129 $record = new FS::svc_acct { 'column' => 'value' };
131 $error = $record->insert;
133 $error = $new_record->replace($old_record);
135 $error = $record->delete;
137 $error = $record->check;
139 $error = $record->suspend;
141 $error = $record->unsuspend;
143 $error = $record->cancel;
145 %hash = $record->radius;
147 %hash = $record->radius_reply;
149 %hash = $record->radius_check;
151 $domain = $record->domain;
153 $svc_domain = $record->svc_domain;
155 $email = $record->email;
157 $seconds_since = $record->seconds_since($timestamp);
161 An FS::svc_acct object represents an account. FS::svc_acct inherits from
162 FS::svc_Common. The following fields are currently supported:
166 =item svcnum - primary key (assigned automatcially for new accounts)
170 =item _password - generated if blank
172 =item popnum - Point of presence (see L<FS::svc_acct_pop>)
180 =item dir - set automatically if blank (and uid is not)
184 =item quota - (unimplementd)
186 =item slipip - IP address
190 =item domsvc - svcnum from svc_domain
192 =item radius_I<Radius_Attribute> - I<Radius-Attribute>
194 =item domsvc - service number of svc_domain with which to associate
204 Creates a new account. To add the account to the database, see L<"insert">.
208 sub table { 'svc_acct'; }
212 Adds this account to the database. If there is an error, returns the error,
213 otherwise returns false.
215 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
216 defined. An FS::cust_svc record will be created and inserted.
218 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
219 username, uid, and dir fields are defined, the command(s) specified in
220 the shellmachine-useradd configuration are added to the job queue (see
221 L<FS::queue> and L<freeside-queued>) to be exectued on shellmachine via ssh.
222 This behaviour can be surpressed by setting $FS::svc_acct::nossh_hack true.
223 If the shellmachine-useradd configuration file does not exist,
225 useradd -d $dir -m -s $shell -u $uid $username
227 is the default. If the shellmachine-useradd configuration file exists but
230 cp -pr /etc/skel $dir; chown -R $uid.$gid $dir
232 is the default instead. Otherwise the contents of the file are treated as
233 a double-quoted perl string, with the following variables available:
234 $username, $uid, $gid, $dir, and $shell.
236 (TODOC: cyrus config file, L<FS::queue> and L<freeside-queued>)
238 (TODOC: new exports! $noexport_hack)
246 local $SIG{HUP} = 'IGNORE';
247 local $SIG{INT} = 'IGNORE';
248 local $SIG{QUIT} = 'IGNORE';
249 local $SIG{TERM} = 'IGNORE';
250 local $SIG{TSTP} = 'IGNORE';
251 local $SIG{PIPE} = 'IGNORE';
253 my $oldAutoCommit = $FS::UID::AutoCommit;
254 local $FS::UID::AutoCommit = 0;
259 $error = $self->check;
260 return $error if $error;
262 return "Username ". $self->username. " in use"
263 if qsearchs( 'svc_acct', { 'username' => $self->username,
264 'domsvc' => $self->domsvc,
267 if ( $self->svcnum ) {
268 my $cust_svc = qsearchs('cust_svc',{'svcnum'=>$self->svcnum});
269 unless ( $cust_svc ) {
270 $dbh->rollback if $oldAutoCommit;
271 return "no cust_svc record found for svcnum ". $self->svcnum;
273 $self->pkgnum($cust_svc->pkgnum);
274 $self->svcpart($cust_svc->svcpart);
277 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
278 return "Unknown svcpart" unless $part_svc;
280 if $part_svc->part_svc_column('uid')->columnflag ne 'F'
281 && qsearchs( 'svc_acct', { 'uid' => $self->uid } )
282 && $self->username !~ /^(hyla)?fax$/
285 $error = $self->SUPER::insert;
287 $dbh->rollback if $oldAutoCommit;
292 unless ( $noexport_hack ) {
293 foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
294 my $error = $part_export->export_insert($self);
296 $dbh->rollback if $oldAutoCommit;
297 return "exporting to ". $part_export->exporttype.
298 " (transaction rolled back): $error";
305 my( $username, $uid, $gid, $dir, $shell ) = (
312 if ( $username && $uid && $dir && $shellmachine && ! $nossh_hack ) {
313 my $queue = new FS::queue {
314 'svcnum' => $self->svcnum,
315 'job' => 'Net::SSH::ssh_cmd',
317 $error = $queue->insert("root\@$shellmachine", eval qq("$useradd") );
319 $dbh->rollback if $oldAutoCommit;
320 return "queueing job (transaction rolled back): $error";
324 if ( $cyrus_server ) {
325 my $queue = new FS::queue {
326 'svcnum' => $self->svcnum,
327 'job' => 'FS::svc_acct::cyrus_insert',
329 $error = $queue->insert($self->username, $self->quota);
331 $dbh->rollback if $oldAutoCommit;
332 return "queueing job (transaction rolled back): $error";
337 my $queue = new FS::queue {
338 'svcnum' => $self->svcnum,
339 'job' => 'FS::svc_acct::cp_insert'
341 $error = $queue->insert($self->username, $self->_password);
343 $dbh->rollback if $oldAutoCommit;
344 return "queueing job (transaction rolled back): $error";
352 'svcnum' => $self->svcnum,
353 'job' => 'FS::svc_acct::vpopmail_insert'
355 $error = $vpopmail_queue->insert( $self->username,
356 crypt($self->_password,$saltset[int(rand(64))].$saltset[int(rand(64))]),
361 $dbh->rollback if $oldAutoCommit;
362 return "queueing job (transaction rolled back): $error";
367 #end of old-style exports
369 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
374 my( $username, $quota ) = @_;
376 warn "cyrus_insert: starting for user $username, quota $quota\n";
378 warn "cyrus_insert: connecting to $cyrus_server\n";
379 my $client = Cyrus::IMAP::Admin->new($cyrus_server);
381 warn "cyrus_insert: authentication as $cyrus_admin_user\n";
382 $client->authenticate(
383 -user => $cyrus_admin_user,
384 -mechanism => "login",
385 -password => $cyrus_admin_pass
388 warn "cyrus_insert: creating user.$username\n";
389 my $rc = $client->create("user.$username");
390 my $error = $client->error;
391 die "cyrus_insert: error creating user.$username: $error" if $error;
393 warn "cyrus_insert: setacl user.$username, $username => all\n";
394 $rc = $client->setacl("user.$username", $username => 'all' );
395 $error = $client->error;
396 die "cyrus_insert: error setacl user.$username: $error" if $error;
399 warn "cyrus_insert: setquota user.$username, STORAGE => $quota\n";
400 $rc = $client->setquota("user.$username", 'STORAGE' => $quota );
401 $error = $client->error;
402 die "cyrus_insert: error setquota user.$username: $error" if $error;
409 my( $username, $password ) = @_;
411 my $app = new Net::APP ( $cp_server,
413 Password => $cp_pass,
419 $app->create_mailbox(
420 Mailbox => $username,
421 Password => $password,
422 Workgroup => $cp_workgroup,
426 die $app->message."\n" unless $app->ok;
429 sub vpopmail_insert {
430 my( $username, $password, $domain, $vpopdir ) = @_;
432 (open(VPASSWD, ">>$exportdir/domains/$domain/vpasswd")
433 and flock(VPASSWD,LOCK_EX)
434 ) or die "can't open vpasswd file for $username\@$domain: $exportdir/domains/$domain/vpasswd";
435 print VPASSWD join(":",
441 "$vpopdir/domains/$domain/$username",
445 flock(VPASSWD,LOCK_UN);
448 mkdir "$exportdir/domains/$domain/$username", 0700 or die "can't create Maildir";
449 mkdir "$exportdir/domains/$domain/$username/Maildir", 0700 or die "can't create Maildir";
450 mkdir "$exportdir/domains/$domain/$username/Maildir/cur", 0700 or die "can't create Maildir";
451 mkdir "$exportdir/domains/$domain/$username/Maildir/new", 0700 or die "can't create Maildir";
452 mkdir "$exportdir/domains/$domain/$username/Maildir/tmp", 0700 or die "can't create Maildir";
454 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_sync' };
455 my $error = $queue->insert;
456 die $error if $error;
463 my (@vpopmailmachines) = $conf->config('vpopmailmachines');
464 my ($machine, $dir, $uid, $gid) = split (/\s+/, $vpopmailmachines[0]);
467 my @args = ("$rsync", "-rlpt", "-e", "$ssh", "domains/", "vpopmail\@$machine:$vpopdir/domains/");
468 system {$args[0]} @args;
474 Deletes this account from the database. If there is an error, returns the
475 error, otherwise returns false.
477 The corresponding FS::cust_svc record will be deleted as well.
479 If the configuration value (see L<FS::Conf>) shellmachine exists, the
480 command(s) specified in the shellmachine-userdel configuration file are
481 added to the job queue (see L<FS::queue> and L<freeside-queued>) to be executed
482 on shellmachine via ssh. This behavior can be surpressed by setting
483 $FS::svc_acct::nossh_hack true. If the shellmachine-userdel configuration
488 is the default. If the shellmachine-userdel configuration file exists but
493 is the default instead. Otherwise the contents of the file are treated as a
494 double-quoted perl string, with the following variables available:
497 (TODOC: cyrus config file)
499 (TODOC: new exports! $noexport_hack)
506 if ( defined( $FS::Record::dbdef->table('svc_acct_sm') ) ) {
507 return "Can't delete an account which has (svc_acct_sm) mail aliases!"
508 if $self->uid && qsearch( 'svc_acct_sm', { 'domuid' => $self->uid } );
511 return "Can't delete an account which is a (svc_forward) source!"
512 if qsearch( 'svc_forward', { 'srcsvc' => $self->svcnum } );
514 return "Can't delete an account which is a (svc_forward) destination!"
515 if qsearch( 'svc_forward', { 'dstsvc' => $self->svcnum } );
517 return "Can't delete an account with (svc_www) web service!"
518 if qsearch( 'svc_www', { 'usersvc' => $self->usersvc } );
520 # what about records in session ? (they should refer to history table)
522 local $SIG{HUP} = 'IGNORE';
523 local $SIG{INT} = 'IGNORE';
524 local $SIG{QUIT} = 'IGNORE';
525 local $SIG{TERM} = 'IGNORE';
526 local $SIG{TSTP} = 'IGNORE';
527 local $SIG{PIPE} = 'IGNORE';
529 my $oldAutoCommit = $FS::UID::AutoCommit;
530 local $FS::UID::AutoCommit = 0;
533 foreach my $cust_main_invoice (
534 qsearch( 'cust_main_invoice', { 'dest' => $self->svcnum } )
536 unless ( defined($cust_main_invoice) ) {
537 warn "WARNING: something's wrong with qsearch";
540 my %hash = $cust_main_invoice->hash;
541 $hash{'dest'} = $self->email;
542 my $new = new FS::cust_main_invoice \%hash;
543 my $error = $new->replace($cust_main_invoice);
545 $dbh->rollback if $oldAutoCommit;
550 foreach my $svc_domain (
551 qsearch( 'svc_domain', { 'catchall' => $self->svcnum } )
553 my %hash = new FS::svc_domain->hash;
554 $hash{'catchall'} = '';
555 my $new = new FS::svc_domain \%hash;
556 my $error = $new->replace($svc_domain);
558 $dbh->rollback if $oldAutoCommit;
563 my $error = $self->SUPER::delete;
565 $dbh->rollback if $oldAutoCommit;
570 unless ( $noexport_hack ) {
571 foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
572 my $error = $part_export->export_delete($self);
574 $dbh->rollback if $oldAutoCommit;
575 return "exporting to ". $part_export->exporttype.
576 " (transaction rolled back): $error";
583 my( $username, $dir ) = (
587 if ( $username && $shellmachine && ! $nossh_hack ) {
588 my $queue = new FS::queue { 'job' => 'Net::SSH::ssh_cmd' };
589 $error = $queue->insert("root\@$shellmachine", eval qq("$userdel") );
591 $dbh->rollback if $oldAutoCommit;
592 return "queueing job (transaction rolled back): $error";
597 if ( $cyrus_server ) {
598 my $queue = new FS::queue { 'job' => 'FS::svc_acct::cyrus_delete' };
599 $error = $queue->insert($self->username);
601 $dbh->rollback if $oldAutoCommit;
602 return "queueing job (transaction rolled back): $error";
607 my $queue = new FS::queue { 'job' => 'FS::svc_acct::cp_delete' };
608 $error = $queue->insert($self->username);
610 $dbh->rollback if $oldAutoCommit;
611 return "queueing job (transaction rolled back): $error";
616 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_delete' };
617 $error = $queue->insert( $self->username, $self->domain );
619 $dbh->rollback if $oldAutoCommit;
620 return "queueing job (transaction rolled back): $error";
625 #end of old-style exports
627 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
632 my $username = shift;
634 my $client = Cyrus::IMAP::Admin->new($cyrus_server);
635 $client->authenticate(
636 -user => $cyrus_admin_user,
637 -mechanism => "login",
638 -password => $cyrus_admin_pass
641 my $rc = $client->setacl("user.$username", $cyrus_admin_user => 'all' );
642 my $error = $client->error;
643 die $error if $error;
645 $rc = $client->delete("user.$username");
646 $error = $client->error;
647 die $error if $error;
653 my( $username ) = @_;
654 my $app = new Net::APP ( $cp_server,
656 Password => $cp_pass,
662 $app->delete_mailbox(
663 Mailbox => $username,
667 die $app->message."\n" unless $app->ok;
670 sub vpopmail_delete {
671 my( $username, $domain ) = @_;
673 (open(VPASSWD, "$exportdir/domains/$domain/vpasswd")
674 and flock(VPASSWD,LOCK_EX)
675 ) or die "can't open $exportdir/domains/$domain/vpasswd: $!";
677 open(VPASSWDTMP, ">$exportdir/domains/$domain/vpasswd.tmp")
678 or die "Can't open $exportdir/domains/$domain/vpasswd.tmp: $!";
681 my ($mailbox, $rest) = split(':', $_);
682 print VPASSWDTMP $_ unless $username eq $mailbox;
687 rename "$exportdir/domains/$domain/vpasswd.tmp", "$exportdir/domains/$domain/vpasswd"
688 or die "Can't rename $exportdir/domains/$domain/vpasswd.tmp: $!";
690 flock(VPASSWD,LOCK_UN);
693 rmtree "$exportdir/domains/$domain/$username" or die "can't destroy Maildir";+
697 =item replace OLD_RECORD
699 Replaces OLD_RECORD with this one in the database. If there is an error,
700 returns the error, otherwise returns false.
702 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
703 dir field has changed, the command(s) specified in the shellmachine-usermod
704 configuraiton file are added to the job queue (see L<FS::queue> and
705 L<freeside-queued>) to be executed on shellmachine via ssh. This behavior can
706 be surpressed by setting $FS::svc-acct::nossh_hack true. If the
707 shellmachine-userdel configuration file does not exist or is empty,
709 [ -d $old_dir ] && mv $old_dir $new_dir || (
713 find . -depth -print | cpio -pdm $new_dir;
715 chown -R $uid.$gid $new_dir;
719 is the default. This behaviour can be surpressed by setting
720 $FS::svc_acct::nossh_hack true.
725 my ( $new, $old ) = ( shift, shift );
728 return "Username in use"
729 if $old->username ne $new->username &&
730 qsearchs( 'svc_acct', { 'username' => $new->username,
731 'domsvc' => $new->domsvc,
734 #no warnings 'numeric'; #alas, a 5.006-ism
736 return "Can't change uid!" if $old->uid != $new->uid;
739 return "can't change username using Cyrus"
740 if $cyrus_server && $old->username ne $new->username;
742 #change homdir when we change username
743 $new->setfield('dir', '') if $old->username ne $new->username;
745 local $SIG{HUP} = 'IGNORE';
746 local $SIG{INT} = 'IGNORE';
747 local $SIG{QUIT} = 'IGNORE';
748 local $SIG{TERM} = 'IGNORE';
749 local $SIG{TSTP} = 'IGNORE';
750 local $SIG{PIPE} = 'IGNORE';
752 my $oldAutoCommit = $FS::UID::AutoCommit;
753 local $FS::UID::AutoCommit = 0;
756 $error = $new->SUPER::replace($old);
758 $dbh->rollback if $oldAutoCommit;
759 return $error if $error;
763 unless ( $noexport_hack ) {
764 foreach my $part_export ( $new->cust_svc->part_svc->part_export ) {
765 my $error = $part_export->export_replace($new,$old);
767 $dbh->rollback if $oldAutoCommit;
768 return "exporting to ". $part_export->exporttype.
769 " (transaction rolled back): $error";
776 my ( $old_dir, $new_dir, $uid, $gid ) = (
777 $old->getfield('dir'),
778 $new->getfield('dir'),
779 $new->getfield('uid'),
780 $new->getfield('gid'),
782 if ( $old_dir && $new_dir && $old_dir ne $new_dir && ! $nossh_hack ) {
783 my $queue = new FS::queue {
784 'svcnum' => $new->svcnum,
785 'job' => 'Net::SSH::ssh_cmd'
787 $error = $queue->insert("root\@$shellmachine", eval qq("$usermod") );
789 $dbh->rollback if $oldAutoCommit;
790 return "queueing job (transaction rolled back): $error";
794 if ( $cp_server && $old->username ne $new->username ) {
795 my $queue = new FS::queue {
796 'svcnum' => $new->svcnum,
797 'job' => 'FS::svc_acct::cp_rename'
799 $error = $queue->insert( $old->username, $new->username );
801 $dbh->rollback if $oldAutoCommit;
802 return "queueing job (transaction rolled back): $error";
806 if ( $cp_server && $old->_password ne $new->_password ) {
807 my $queue = new FS::queue {
808 'svcnum' => $new->svcnum,
809 'job' => 'FS::svc_acct::cp_change'
811 $error = $queue->insert( $new->username, $new->_password );
813 $dbh->rollback if $oldAutoCommit;
814 return "queueing job (transaction rolled back): $error";
819 my $cpassword = crypt(
820 $new->_password,$saltset[int(rand(64))].$saltset[int(rand(64))]
823 if ($old->username ne $new->username || $old->domain ne $new->domain ) {
824 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_delete' };
825 $error = $queue->insert( $old->username, $old->domain );
826 my $queue2 = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_insert' };
827 $error = $queue2->insert( $new->username,
833 } elsif ($old->_password ne $new->_password) {
834 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_replace_password' };
835 $error = $queue->insert( $new->username, $cpassword, $new->domain );
838 $dbh->rollback if $oldAutoCommit;
839 return "queueing job (transaction rolled back): $error";
843 #end of old-style exports
845 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
850 my ( $old_username, $new_username ) = @_;
852 my $app = new Net::APP ( $cp_server,
854 Password => $cp_pass,
860 $app->rename_mailbox(
862 Old_Mailbox => $old_username,
863 New_Mailbox => $new_username,
866 die $app->message."\n" unless $app->ok;
871 my ( $username, $password ) = @_;
873 my $app = new Net::APP ( $cp_server,
875 Password => $cp_pass,
881 if ( $password =~ /^\*SUSPENDED\* (.*)$/ ) {
883 $app->set_mailbox_status(
885 Mailbox => $username,
890 $app->set_mailbox_status(
892 Mailbox => $username,
897 die $app->message."\n" unless $app->ok;
899 $app->change_mailbox(
901 Mailbox => $username,
902 Password => $password,
904 die $app->message."\n" unless $app->ok;
908 sub vpopmail_replace_password {
909 my( $username, $password, $domain ) = @_;
911 (open(VPASSWD, "$exportdir/domains/$domain/vpasswd")
912 and flock(VPASSWD,LOCK_EX)
913 ) or die "can't open $exportdir/domains/$domain/vpasswd: $!";
915 open(VPASSWDTMP, ">$exportdir/domains/$domain/vpasswd.tmp")
916 or die "Can't open $exportdir/domains/$domain/vpasswd.tmp: $!";
919 my ($mailbox, $pw, @rest) = split(':', $_);
920 print VPASSWDTMP $_ unless $username eq $mailbox;
921 print VPASSWDTMP join (':', ($mailbox, $password, @rest))
922 if $username eq $mailbox;
927 rename "$exportdir/domains/$domain/vpasswd.tmp", "$exportdir/domains/$domain/vpasswd"
928 or die "Can't rename $exportdir/domains/$domain/vpasswd.tmp: $!";
930 flock(VPASSWD,LOCK_UN);
933 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_sync' };
934 my $error = $queue->insert;
935 die $error if $error;
943 Suspends this account by prefixing *SUSPENDED* to the password. If there is an
944 error, returns the error, otherwise returns false.
946 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
952 my %hash = $self->hash;
953 unless ( $hash{_password} =~ /^\*SUSPENDED\* /
954 || $hash{_password} eq '*'
956 $hash{_password} = '*SUSPENDED* '.$hash{_password};
957 my $new = new FS::svc_acct ( \%hash );
958 $new->replace($self);
960 ''; #no error (already suspended)
966 Unsuspends this account by removing *SUSPENDED* from the password. If there is
967 an error, returns the error, otherwise returns false.
969 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
975 my %hash = $self->hash;
976 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
977 $hash{_password} = $1;
978 my $new = new FS::svc_acct ( \%hash );
979 $new->replace($self);
981 ''; #no error (already unsuspended)
987 Just returns false (no error) for now.
989 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
993 Checks all fields to make sure this is a valid service. If there is an error,
994 returns the error, otherwise returns false. Called by the insert and replace
997 Sets any fixed values; see L<FS::part_svc>.
1004 my($recref) = $self->hashref;
1006 my $x = $self->setfixed;
1007 return $x unless ref($x);
1010 my $error = $self->ut_numbern('svcnum')
1011 || $self->ut_number('domsvc')
1013 return $error if $error;
1015 my $ulen = $usernamemax || $self->dbdef_table->column('username')->length;
1016 if ( $username_uppercase ) {
1017 $recref->{username} =~ /^([a-z0-9_\-\.\&]{$usernamemin,$ulen})$/i
1018 or return "Illegal username: ". $recref->{username};
1019 $recref->{username} = $1;
1021 $recref->{username} =~ /^([a-z0-9_\-\.\&]{$usernamemin,$ulen})$/
1022 or return "Illegal username: ". $recref->{username};
1023 $recref->{username} = $1;
1026 if ( $username_letterfirst ) {
1027 $recref->{username} =~ /^[a-z]/ or return "Illegal username";
1028 } elsif ( $username_letter ) {
1029 $recref->{username} =~ /[a-z]/ or return "Illegal username";
1031 if ( $username_noperiod ) {
1032 $recref->{username} =~ /\./ and return "Illegal username";
1034 unless ( $username_ampersand ) {
1035 $recref->{username} =~ /\&/ and return "Illegal username";
1038 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum: ".$recref->{popnum};
1039 $recref->{popnum} = $1;
1040 return "Unknown popnum" unless
1041 ! $recref->{popnum} ||
1042 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
1044 unless ( $part_svc->part_svc_column('uid')->columnflag eq 'F' ) {
1046 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
1047 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
1049 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
1050 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
1051 #not all systems use gid=uid
1052 #you can set a fixed gid in part_svc
1054 return "Only root can have uid 0"
1055 if $recref->{uid} == 0 && $recref->{username} ne 'root';
1057 # $error = $self->ut_textn('finger');
1058 # return $error if $error;
1059 $self->getfield('finger') =~
1060 /^([\w \t\!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\*\<\>]*)$/
1061 or return "Illegal finger: ". $self->getfield('finger');
1062 $self->setfield('finger', $1);
1064 $recref->{dir} =~ /^([\/\w\-\.\&]*)$/
1065 or return "Illegal directory";
1066 $recref->{dir} = $1;
1067 return "Illegal directory"
1068 if $recref->{dir} =~ /(^|\/)\.+(\/|$)/; #no .. component
1069 return "Illegal directory"
1070 if $recref->{dir} =~ /\&/ && ! $username_ampersand;
1071 unless ( $recref->{dir} ) {
1072 $recref->{dir} = $dir_prefix . '/';
1073 if ( $dirhash > 0 ) {
1074 for my $h ( 1 .. $dirhash ) {
1075 $recref->{dir} .= substr($recref->{username}, $h-1, 1). '/';
1077 } elsif ( $dirhash < 0 ) {
1078 for my $h ( reverse $dirhash .. -1 ) {
1079 $recref->{dir} .= substr($recref->{username}, $h, 1). '/';
1082 $recref->{dir} .= $recref->{username};
1086 unless ( $recref->{username} eq 'sync' ) {
1087 if ( grep $_ eq $recref->{shell}, @shells ) {
1088 $recref->{shell} = (grep $_ eq $recref->{shell}, @shells)[0];
1090 return "Illegal shell \`". $self->shell. "\'; ".
1091 $conf->dir. "/shells contains: @shells";
1094 $recref->{shell} = '/bin/sync';
1097 $recref->{quota} =~ /^(\d*)$/ or return "Illegal quota (unimplemented)";
1098 $recref->{quota} = $1;
1101 $recref->{gid} ne '' ?
1102 return "Can't have gid without uid" : ( $recref->{gid}='' );
1103 $recref->{finger} ne '' ?
1104 return "Can't have finger-name without uid" : ( $recref->{finger}='' );
1105 $recref->{dir} ne '' ?
1106 return "Can't have directory without uid" : ( $recref->{dir}='' );
1107 $recref->{shell} ne '' ?
1108 return "Can't have shell without uid" : ( $recref->{shell}='' );
1109 $recref->{quota} ne '' ?
1110 return "Can't have quota without uid" : ( $recref->{quota}='' );
1113 unless ( $part_svc->part_svc_column('slipip')->columnflag eq 'F' ) {
1114 unless ( $recref->{slipip} eq '0e0' ) {
1115 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
1116 or return "Illegal slipip". $self->slipip;
1117 $recref->{slipip} = $1;
1119 $recref->{slipip} = '0e0';
1124 #arbitrary RADIUS stuff; allow ut_textn for now
1125 foreach ( grep /^radius_/, fields('svc_acct') ) {
1126 $self->ut_textn($_);
1129 #generate a password if it is blank
1130 $recref->{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) )
1131 unless ( $recref->{_password} );
1133 #if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,16})$/ ) {
1134 if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{$passwordmin,$passwordmax})$/ ) {
1135 $recref->{_password} = $1.$3;
1136 #uncomment this to encrypt password immediately upon entry, or run
1137 #bin/crypt_pw in cron to give new users a window during which their
1138 #password is available to techs, for faxing, etc. (also be aware of
1140 #$recref->{password} = $1.
1141 # crypt($3,$saltset[int(rand(64))].$saltset[int(rand(64))]
1143 } elsif ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([\w\.\/\$]{13,34})$/ ) {
1144 $recref->{_password} = $1.$3;
1145 } elsif ( $recref->{_password} eq '*' ) {
1146 $recref->{_password} = '*';
1147 } elsif ( $recref->{_password} eq '!!' ) {
1148 $recref->{_password} = '!!';
1150 #return "Illegal password";
1151 return "Illegal password: ". $recref->{_password};
1159 Depriciated, use radius_reply instead.
1164 carp "FS::svc_acct::radius depriciated, use radius_reply";
1165 $_[0]->radius_reply;
1170 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1171 reply attributes of this record.
1173 Note that this is now the preferred method for reading RADIUS attributes -
1174 accessing the columns directly is discouraged, as the column names are
1175 expected to change in the future.
1184 my($column, $attrib) = ($1, $2);
1185 #$attrib =~ s/_/\-/g;
1186 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1187 } grep { /^radius_/ && $self->getfield($_) } fields( $self->table );
1188 if ( $self->ip && $self->ip ne '0e0' ) {
1189 $reply{'Framed-IP-Address'} = $self->ip;
1196 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1197 check attributes of this record.
1199 Note that this is now the preferred method for reading RADIUS attributes -
1200 accessing the columns directly is discouraged, as the column names are
1201 expected to change in the future.
1207 ( 'Password' => $self->_password,
1210 my($column, $attrib) = ($1, $2);
1211 #$attrib =~ s/_/\-/g;
1212 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1213 } grep { /^rc_/ && $self->getfield($_) } fields( $self->table )
1219 Returns the domain associated with this account.
1225 if ( $self->domsvc ) {
1226 #$self->svc_domain->domain;
1227 my $svc_domain = $self->svc_domain
1228 or die "no svc_domain.svcnum for svc_acct.domsvc ". $self->domsvc;
1229 $svc_domain->domain;
1231 $mydomain or die "svc_acct.domsvc is null and no legacy domain config file";
1237 Returns the FS::svc_domain record for this account's domain (see
1245 ? $self->{'_domsvc'}
1246 : qsearchs( 'svc_domain', { 'svcnum' => $self->domsvc } );
1251 Returns the FS::cust_svc record for this account (see L<FS::cust_svc>).
1255 qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
1260 Returns an email address associated with the account.
1266 $self->username. '@'. $self->domain;
1269 =item seconds_since TIMESTAMP
1271 Returns the number of seconds this account has been online since TIMESTAMP.
1274 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
1275 L<Time::Local> and L<Date::Parse> for conversion functions.
1279 #note: POD here, implementation in FS::cust_svc
1282 $self->cust_svc->seconds_since(@_);
1289 The $recref stuff in sub check should be cleaned up.
1291 The suspend, unsuspend and cancel methods update the database, but not the
1292 current object. This is probably a bug as it's unexpected and
1297 L<FS::svc_Common>, edit/part_svc.cgi from an installed web interface,
1298 export.html from the base documentation, L<FS::Record>, L<FS::Conf>,
1299 L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, L<FS::queue>,
1300 L<freeside-queued>), L<Net::SSH>, L<ssh>, L<FS::svc_acct_pop>,
1301 schema.html from the base documentation.