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
13 $rsync $ssh $exportdir $vpopdir);
17 use FS::UID qw( datasrc );
19 use FS::Record qw( qsearch qsearchs fields dbh );
25 use FS::cust_main_invoice;
29 use FS::radius_usergroup;
30 use FS::Msgcat qw(gettext);
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 = '';
83 $dirhash = $conf->config('dirhash') || 0;
84 $exportdir = "/usr/local/etc/freeside/export." . datasrc;
85 if ( $conf->exists('vpopmailmachines') ) {
86 my (@vpopmailmachines) = $conf->config('vpopmailmachines');
87 my ($machine, $dir, $uid, $gid) = split (/\s+/, $vpopmailmachines[0]);
94 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
95 @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '!', '.', ',' );
97 #not needed in 5.004 #srand($$|time);
101 my ( $hashref, $cache ) = @_;
102 if ( $hashref->{'svc_acct_svcnum'} ) {
103 $self->{'_domsvc'} = FS::svc_domain->new( {
104 'svcnum' => $hashref->{'domsvc'},
105 'domain' => $hashref->{'svc_acct_domain'},
106 'catchall' => $hashref->{'svc_acct_catchall'},
113 FS::svc_acct - Object methods for svc_acct records
119 $record = new FS::svc_acct \%hash;
120 $record = new FS::svc_acct { 'column' => 'value' };
122 $error = $record->insert;
124 $error = $new_record->replace($old_record);
126 $error = $record->delete;
128 $error = $record->check;
130 $error = $record->suspend;
132 $error = $record->unsuspend;
134 $error = $record->cancel;
136 %hash = $record->radius;
138 %hash = $record->radius_reply;
140 %hash = $record->radius_check;
142 $domain = $record->domain;
144 $svc_domain = $record->svc_domain;
146 $email = $record->email;
148 $seconds_since = $record->seconds_since($timestamp);
152 An FS::svc_acct object represents an account. FS::svc_acct inherits from
153 FS::svc_Common. The following fields are currently supported:
157 =item svcnum - primary key (assigned automatcially for new accounts)
161 =item _password - generated if blank
163 =item sec_phrase - security phrase
165 =item popnum - Point of presence (see L<FS::svc_acct_pop>)
173 =item dir - set automatically if blank (and uid is not)
177 =item quota - (unimplementd)
179 =item slipip - IP address
183 =item domsvc - svcnum from svc_domain
185 =item radius_I<Radius_Attribute> - I<Radius-Attribute>
195 Creates a new account. To add the account to the database, see L<"insert">.
199 sub table { 'svc_acct'; }
203 Adds this account to the database. If there is an error, returns the error,
204 otherwise returns false.
206 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
207 defined. An FS::cust_svc record will be created and inserted.
209 The additional field I<usergroup> can optionally be defined; if so it should
210 contain an arrayref of group names. See L<FS::radius_usergroup>. (used in
211 sqlradius export only)
213 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
214 username, uid, and dir fields are defined, the command(s) specified in
215 the shellmachine-useradd configuration are added to the job queue (see
216 L<FS::queue> and L<freeside-queued>) to be exectued on shellmachine via ssh.
217 This behaviour can be surpressed by setting $FS::svc_acct::nossh_hack true.
218 If the shellmachine-useradd configuration file does not exist,
220 useradd -d $dir -m -s $shell -u $uid $username
222 is the default. If the shellmachine-useradd configuration file exists but
225 cp -pr /etc/skel $dir; chown -R $uid.$gid $dir
227 is the default instead. Otherwise the contents of the file are treated as
228 a double-quoted perl string, with the following variables available:
229 $username, $uid, $gid, $dir, and $shell.
231 (TODOC: cyrus config file, L<FS::queue> and L<freeside-queued>)
233 (TODOC: new exports! $noexport_hack)
241 local $SIG{HUP} = 'IGNORE';
242 local $SIG{INT} = 'IGNORE';
243 local $SIG{QUIT} = 'IGNORE';
244 local $SIG{TERM} = 'IGNORE';
245 local $SIG{TSTP} = 'IGNORE';
246 local $SIG{PIPE} = 'IGNORE';
248 my $oldAutoCommit = $FS::UID::AutoCommit;
249 local $FS::UID::AutoCommit = 0;
252 $error = $self->check;
253 return $error if $error;
255 return gettext('username_in_use'). ": ". $self->username
256 if qsearchs( 'svc_acct', { 'username' => $self->username,
257 'domsvc' => $self->domsvc,
260 if ( $self->svcnum ) {
261 my $cust_svc = qsearchs('cust_svc',{'svcnum'=>$self->svcnum});
262 unless ( $cust_svc ) {
263 $dbh->rollback if $oldAutoCommit;
264 return "no cust_svc record found for svcnum ". $self->svcnum;
266 $self->pkgnum($cust_svc->pkgnum);
267 $self->svcpart($cust_svc->svcpart);
270 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
271 return "Unknown svcpart" unless $part_svc;
273 if $part_svc->part_svc_column('uid')->columnflag ne 'F'
274 && qsearchs( 'svc_acct', { 'uid' => $self->uid } )
275 && $self->username !~ /^(hyla)?fax$/
278 $error = $self->SUPER::insert;
280 $dbh->rollback if $oldAutoCommit;
284 if ( $self->usergroup ) {
285 foreach my $groupname ( @{$self->usergroup} ) {
286 my $radius_usergroup = new FS::radius_usergroup ( {
287 svcnum => $self->svcnum,
288 groupname => $groupname,
290 my $error = $radius_usergroup->insert;
292 $dbh->rollback if $oldAutoCommit;
299 unless ( $noexport_hack ) {
300 foreach my $part_export ( $self->cust_svc->part_svc->part_export ) {
301 my $error = $part_export->export_insert($self);
303 $dbh->rollback if $oldAutoCommit;
304 return "exporting to ". $part_export->exporttype.
305 " (transaction rolled back): $error";
312 my( $username, $uid, $gid, $dir, $shell ) = (
319 if ( $username && $uid && $dir && $shellmachine && ! $nossh_hack ) {
320 my $queue = new FS::queue {
321 'svcnum' => $self->svcnum,
322 'job' => 'Net::SSH::ssh_cmd',
324 $error = $queue->insert("root\@$shellmachine", eval qq("$useradd") );
326 $dbh->rollback if $oldAutoCommit;
327 return "queueing job (transaction rolled back): $error";
331 if ( $cyrus_server ) {
332 my $queue = new FS::queue {
333 'svcnum' => $self->svcnum,
334 'job' => 'FS::svc_acct::cyrus_insert',
336 $error = $queue->insert($self->username, $self->quota);
338 $dbh->rollback if $oldAutoCommit;
339 return "queueing job (transaction rolled back): $error";
347 'svcnum' => $self->svcnum,
348 'job' => 'FS::svc_acct::vpopmail_insert'
350 $error = $vpopmail_queue->insert( $self->username,
351 crypt($self->_password,$saltset[int(rand(64))].$saltset[int(rand(64))]),
356 $dbh->rollback if $oldAutoCommit;
357 return "queueing job (transaction rolled back): $error";
362 #end of old-style exports
364 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
369 my( $username, $quota ) = @_;
371 warn "cyrus_insert: starting for user $username, quota $quota\n";
373 warn "cyrus_insert: connecting to $cyrus_server\n";
374 my $client = Cyrus::IMAP::Admin->new($cyrus_server);
376 warn "cyrus_insert: authentication as $cyrus_admin_user\n";
377 $client->authenticate(
378 -user => $cyrus_admin_user,
379 -mechanism => "login",
380 -password => $cyrus_admin_pass
383 warn "cyrus_insert: creating user.$username\n";
384 my $rc = $client->create("user.$username");
385 my $error = $client->error;
386 die "cyrus_insert: error creating user.$username: $error" if $error;
388 warn "cyrus_insert: setacl user.$username, $username => all\n";
389 $rc = $client->setacl("user.$username", $username => 'all' );
390 $error = $client->error;
391 die "cyrus_insert: error setacl user.$username: $error" if $error;
394 warn "cyrus_insert: setquota user.$username, STORAGE => $quota\n";
395 $rc = $client->setquota("user.$username", 'STORAGE' => $quota );
396 $error = $client->error;
397 die "cyrus_insert: error setquota user.$username: $error" if $error;
403 sub vpopmail_insert {
404 my( $username, $password, $domain, $vpopdir ) = @_;
406 (open(VPASSWD, ">>$exportdir/domains/$domain/vpasswd")
407 and flock(VPASSWD,LOCK_EX)
408 ) or die "can't open vpasswd file for $username\@$domain: $exportdir/domains/$domain/vpasswd";
409 print VPASSWD join(":",
415 "$vpopdir/domains/$domain/$username",
419 flock(VPASSWD,LOCK_UN);
422 mkdir "$exportdir/domains/$domain/$username", 0700 or die "can't create Maildir";
423 mkdir "$exportdir/domains/$domain/$username/Maildir", 0700 or die "can't create Maildir";
424 mkdir "$exportdir/domains/$domain/$username/Maildir/cur", 0700 or die "can't create Maildir";
425 mkdir "$exportdir/domains/$domain/$username/Maildir/new", 0700 or die "can't create Maildir";
426 mkdir "$exportdir/domains/$domain/$username/Maildir/tmp", 0700 or die "can't create Maildir";
428 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_sync' };
429 my $error = $queue->insert;
430 die $error if $error;
437 my (@vpopmailmachines) = $conf->config('vpopmailmachines');
438 my ($machine, $dir, $uid, $gid) = split (/\s+/, $vpopmailmachines[0]);
441 my @args = ("$rsync", "-rlpt", "-e", "$ssh", "domains/", "vpopmail\@$machine:$vpopdir/domains/");
442 system {$args[0]} @args;
448 Deletes this account from the database. If there is an error, returns the
449 error, otherwise returns false.
451 The corresponding FS::cust_svc record will be deleted as well.
453 If the configuration value (see L<FS::Conf>) shellmachine exists, the
454 command(s) specified in the shellmachine-userdel configuration file are
455 added to the job queue (see L<FS::queue> and L<freeside-queued>) to be executed
456 on shellmachine via ssh. This behavior can be surpressed by setting
457 $FS::svc_acct::nossh_hack true. If the shellmachine-userdel configuration
462 is the default. If the shellmachine-userdel configuration file exists but
467 is the default instead. Otherwise the contents of the file are treated as a
468 double-quoted perl string, with the following variables available:
471 (TODOC: cyrus config file)
473 (TODOC: new exports! $noexport_hack)
480 if ( defined( $FS::Record::dbdef->table('svc_acct_sm') ) ) {
481 return "Can't delete an account which has (svc_acct_sm) mail aliases!"
482 if $self->uid && qsearch( 'svc_acct_sm', { 'domuid' => $self->uid } );
485 return "Can't delete an account which is a (svc_forward) source!"
486 if qsearch( 'svc_forward', { 'srcsvc' => $self->svcnum } );
488 return "Can't delete an account which is a (svc_forward) destination!"
489 if qsearch( 'svc_forward', { 'dstsvc' => $self->svcnum } );
491 return "Can't delete an account with (svc_www) web service!"
492 if qsearch( 'svc_www', { 'usersvc' => $self->usersvc } );
494 # what about records in session ? (they should refer to history table)
496 local $SIG{HUP} = 'IGNORE';
497 local $SIG{INT} = 'IGNORE';
498 local $SIG{QUIT} = 'IGNORE';
499 local $SIG{TERM} = 'IGNORE';
500 local $SIG{TSTP} = 'IGNORE';
501 local $SIG{PIPE} = 'IGNORE';
503 my $oldAutoCommit = $FS::UID::AutoCommit;
504 local $FS::UID::AutoCommit = 0;
507 foreach my $cust_main_invoice (
508 qsearch( 'cust_main_invoice', { 'dest' => $self->svcnum } )
510 unless ( defined($cust_main_invoice) ) {
511 warn "WARNING: something's wrong with qsearch";
514 my %hash = $cust_main_invoice->hash;
515 $hash{'dest'} = $self->email;
516 my $new = new FS::cust_main_invoice \%hash;
517 my $error = $new->replace($cust_main_invoice);
519 $dbh->rollback if $oldAutoCommit;
524 foreach my $svc_domain (
525 qsearch( 'svc_domain', { 'catchall' => $self->svcnum } )
527 my %hash = new FS::svc_domain->hash;
528 $hash{'catchall'} = '';
529 my $new = new FS::svc_domain \%hash;
530 my $error = $new->replace($svc_domain);
532 $dbh->rollback if $oldAutoCommit;
537 foreach my $radius_usergroup (
538 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } )
540 my $error = $radius_usergroup->delete;
542 $dbh->rollback if $oldAutoCommit;
547 my $part_svc = $self->cust_svc->part_svc;
549 my $error = $self->SUPER::delete;
551 $dbh->rollback if $oldAutoCommit;
556 unless ( $noexport_hack ) {
557 foreach my $part_export ( $part_svc->part_export ) {
558 my $error = $part_export->export_delete($self);
560 $dbh->rollback if $oldAutoCommit;
561 return "exporting to ". $part_export->exporttype.
562 " (transaction rolled back): $error";
569 my( $username, $dir ) = (
573 if ( $username && $shellmachine && ! $nossh_hack ) {
574 my $queue = new FS::queue { 'job' => 'Net::SSH::ssh_cmd' };
575 $error = $queue->insert("root\@$shellmachine", eval qq("$userdel") );
577 $dbh->rollback if $oldAutoCommit;
578 return "queueing job (transaction rolled back): $error";
583 if ( $cyrus_server ) {
584 my $queue = new FS::queue { 'job' => 'FS::svc_acct::cyrus_delete' };
585 $error = $queue->insert($self->username);
587 $dbh->rollback if $oldAutoCommit;
588 return "queueing job (transaction rolled back): $error";
593 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_delete' };
594 $error = $queue->insert( $self->username, $self->domain );
596 $dbh->rollback if $oldAutoCommit;
597 return "queueing job (transaction rolled back): $error";
602 #end of old-style exports
604 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
609 my $username = shift;
611 my $client = Cyrus::IMAP::Admin->new($cyrus_server);
612 $client->authenticate(
613 -user => $cyrus_admin_user,
614 -mechanism => "login",
615 -password => $cyrus_admin_pass
618 my $rc = $client->setacl("user.$username", $cyrus_admin_user => 'all' );
619 my $error = $client->error;
620 die $error if $error;
622 $rc = $client->delete("user.$username");
623 $error = $client->error;
624 die $error if $error;
629 sub vpopmail_delete {
630 my( $username, $domain ) = @_;
632 (open(VPASSWD, "$exportdir/domains/$domain/vpasswd")
633 and flock(VPASSWD,LOCK_EX)
634 ) or die "can't open $exportdir/domains/$domain/vpasswd: $!";
636 open(VPASSWDTMP, ">$exportdir/domains/$domain/vpasswd.tmp")
637 or die "Can't open $exportdir/domains/$domain/vpasswd.tmp: $!";
640 my ($mailbox, $rest) = split(':', $_);
641 print VPASSWDTMP $_ unless $username eq $mailbox;
646 rename "$exportdir/domains/$domain/vpasswd.tmp", "$exportdir/domains/$domain/vpasswd"
647 or die "Can't rename $exportdir/domains/$domain/vpasswd.tmp: $!";
649 flock(VPASSWD,LOCK_UN);
652 rmtree "$exportdir/domains/$domain/$username" or die "can't destroy Maildir";+
656 =item replace OLD_RECORD
658 Replaces OLD_RECORD with this one in the database. If there is an error,
659 returns the error, otherwise returns false.
661 The additional field I<usergroup> can optionally be defined; if so it should
662 contain an arrayref of group names. See L<FS::radius_usergroup>. (used in
663 sqlradius export only)
665 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
666 dir field has changed, the command(s) specified in the shellmachine-usermod
667 configuraiton file are added to the job queue (see L<FS::queue> and
668 L<freeside-queued>) to be executed on shellmachine via ssh. This behavior can
669 be surpressed by setting $FS::svc-acct::nossh_hack true. If the
670 shellmachine-userdel configuration file does not exist or is empty,
672 [ -d $old_dir ] && mv $old_dir $new_dir || (
676 find . -depth -print | cpio -pdm $new_dir;
678 chown -R $uid.$gid $new_dir;
682 is the default. This behaviour can be surpressed by setting
683 $FS::svc_acct::nossh_hack true.
688 my ( $new, $old ) = ( shift, shift );
691 return "Username in use"
692 if $old->username ne $new->username &&
693 qsearchs( 'svc_acct', { 'username' => $new->username,
694 'domsvc' => $new->domsvc,
697 #no warnings 'numeric'; #alas, a 5.006-ism
699 return "Can't change uid!" if $old->uid != $new->uid;
702 return "can't change username using Cyrus"
703 if $cyrus_server && $old->username ne $new->username;
705 #change homdir when we change username
706 $new->setfield('dir', '') if $old->username ne $new->username;
708 local $SIG{HUP} = 'IGNORE';
709 local $SIG{INT} = 'IGNORE';
710 local $SIG{QUIT} = 'IGNORE';
711 local $SIG{TERM} = 'IGNORE';
712 local $SIG{TSTP} = 'IGNORE';
713 local $SIG{PIPE} = 'IGNORE';
715 my $oldAutoCommit = $FS::UID::AutoCommit;
716 local $FS::UID::AutoCommit = 0;
719 $error = $new->SUPER::replace($old);
721 $dbh->rollback if $oldAutoCommit;
722 return $error if $error;
725 $old->usergroup( [ $old->radius_groups ] );
726 if ( $new->usergroup ) {
727 #(sorta) false laziness with FS::part_export::sqlradius::_export_replace
728 my @newgroups = @{$new->usergroup};
729 foreach my $oldgroup ( @{$old->usergroup} ) {
730 if ( grep { $oldgroup eq $_ } @newgroups ) {
731 @newgroups = grep { $oldgroup ne $_ } @newgroups;
734 my $radius_usergroup = qsearchs('radius_usergroup', {
735 svcnum => $old->svcnum,
736 groupname => $oldgroup,
738 my $error = $radius_usergroup->delete;
740 $dbh->rollback if $oldAutoCommit;
741 return "error deleting radius_usergroup $oldgroup: $error";
745 foreach my $newgroup ( @newgroups ) {
746 my $radius_usergroup = new FS::radius_usergroup ( {
747 svcnum => $new->svcnum,
748 groupname => $newgroup,
750 my $error = $radius_usergroup->insert;
752 $dbh->rollback if $oldAutoCommit;
753 return "error adding radius_usergroup $newgroup: $error";
760 unless ( $noexport_hack ) {
761 foreach my $part_export ( $new->cust_svc->part_svc->part_export ) {
762 my $error = $part_export->export_replace($new,$old);
764 $dbh->rollback if $oldAutoCommit;
765 return "exporting to ". $part_export->exporttype.
766 " (transaction rolled back): $error";
773 my ( $old_dir, $new_dir, $uid, $gid ) = (
774 $old->getfield('dir'),
775 $new->getfield('dir'),
776 $new->getfield('uid'),
777 $new->getfield('gid'),
779 if ( $old_dir && $new_dir && $old_dir ne $new_dir && ! $nossh_hack ) {
780 my $queue = new FS::queue {
781 'svcnum' => $new->svcnum,
782 'job' => 'Net::SSH::ssh_cmd'
784 $error = $queue->insert("root\@$shellmachine", eval qq("$usermod") );
786 $dbh->rollback if $oldAutoCommit;
787 return "queueing job (transaction rolled back): $error";
791 if ( $cp_server && $old->username ne $new->username ) {
792 my $queue = new FS::queue {
793 'svcnum' => $new->svcnum,
794 'job' => 'FS::svc_acct::cp_rename'
796 $error = $queue->insert( $old->username, $new->username );
798 $dbh->rollback if $oldAutoCommit;
799 return "queueing job (transaction rolled back): $error";
804 my $cpassword = crypt(
805 $new->_password,$saltset[int(rand(64))].$saltset[int(rand(64))]
808 if ($old->username ne $new->username || $old->domain ne $new->domain ) {
809 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_delete' };
810 $error = $queue->insert( $old->username, $old->domain );
811 my $queue2 = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_insert' };
812 $error = $queue2->insert( $new->username,
818 } elsif ($old->_password ne $new->_password) {
819 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_replace_password' };
820 $error = $queue->insert( $new->username, $cpassword, $new->domain );
823 $dbh->rollback if $oldAutoCommit;
824 return "queueing job (transaction rolled back): $error";
828 #end of old-style exports
830 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
834 sub vpopmail_replace_password {
835 my( $username, $password, $domain ) = @_;
837 (open(VPASSWD, "$exportdir/domains/$domain/vpasswd")
838 and flock(VPASSWD,LOCK_EX)
839 ) or die "can't open $exportdir/domains/$domain/vpasswd: $!";
841 open(VPASSWDTMP, ">$exportdir/domains/$domain/vpasswd.tmp")
842 or die "Can't open $exportdir/domains/$domain/vpasswd.tmp: $!";
845 my ($mailbox, $pw, @rest) = split(':', $_);
846 print VPASSWDTMP $_ unless $username eq $mailbox;
847 print VPASSWDTMP join (':', ($mailbox, $password, @rest))
848 if $username eq $mailbox;
853 rename "$exportdir/domains/$domain/vpasswd.tmp", "$exportdir/domains/$domain/vpasswd"
854 or die "Can't rename $exportdir/domains/$domain/vpasswd.tmp: $!";
856 flock(VPASSWD,LOCK_UN);
859 my $queue = new FS::queue { 'job' => 'FS::svc_acct::vpopmail_sync' };
860 my $error = $queue->insert;
861 die $error if $error;
869 Suspends this account by prefixing *SUSPENDED* to the password. If there is an
870 error, returns the error, otherwise returns false.
872 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
878 my %hash = $self->hash;
879 unless ( $hash{_password} =~ /^\*SUSPENDED\* /
880 || $hash{_password} eq '*'
882 $hash{_password} = '*SUSPENDED* '.$hash{_password};
883 my $new = new FS::svc_acct ( \%hash );
884 $new->replace($self);
886 ''; #no error (already suspended)
892 Unsuspends this account by removing *SUSPENDED* from the password. If there is
893 an error, returns the error, otherwise returns false.
895 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
901 my %hash = $self->hash;
902 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
903 $hash{_password} = $1;
904 my $new = new FS::svc_acct ( \%hash );
905 $new->replace($self);
907 ''; #no error (already unsuspended)
913 Just returns false (no error) for now.
915 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
919 Checks all fields to make sure this is a valid service. If there is an error,
920 returns the error, otherwise returns false. Called by the insert and replace
923 Sets any fixed values; see L<FS::part_svc>.
930 my($recref) = $self->hashref;
932 my $x = $self->setfixed;
933 return $x unless ref($x);
936 if ( $part_svc->part_svc_column('usergroup')->columnflag eq "F" ) {
938 [ split(',', $part_svc->part_svc_column('usergroup')->columnvalue) ] );
941 my $error = $self->ut_numbern('svcnum')
942 || $self->ut_number('domsvc')
943 || $self->ut_textn('sec_phrase')
945 return $error if $error;
947 my $ulen = $usernamemax || $self->dbdef_table->column('username')->length;
948 if ( $username_uppercase ) {
949 $recref->{username} =~ /^([a-z0-9_\-\.\&]{$usernamemin,$ulen})$/i
950 or return gettext('illegal_username'). ": ". $recref->{username};
951 $recref->{username} = $1;
953 $recref->{username} =~ /^([a-z0-9_\-\.\&]{$usernamemin,$ulen})$/
954 or return gettext('illegal_username'). ": ". $recref->{username};
955 $recref->{username} = $1;
958 if ( $username_letterfirst ) {
959 $recref->{username} =~ /^[a-z]/ or return gettext('illegal_username');
960 } elsif ( $username_letter ) {
961 $recref->{username} =~ /[a-z]/ or return gettext('illegal_username');
963 if ( $username_noperiod ) {
964 $recref->{username} =~ /\./ and return gettext('illegal_username');
966 unless ( $username_ampersand ) {
967 $recref->{username} =~ /\&/ and return gettext('illegal_username');
970 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum: ".$recref->{popnum};
971 $recref->{popnum} = $1;
972 return "Unknown popnum" unless
973 ! $recref->{popnum} ||
974 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
976 unless ( $part_svc->part_svc_column('uid')->columnflag eq 'F' ) {
978 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
979 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
981 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
982 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
983 #not all systems use gid=uid
984 #you can set a fixed gid in part_svc
986 return "Only root can have uid 0"
987 if $recref->{uid} == 0 && $recref->{username} ne 'root';
989 # $error = $self->ut_textn('finger');
990 # return $error if $error;
991 $self->getfield('finger') =~
992 /^([\w \t\!\@\#\$\%\&\(\)\-\+\;\:\'\"\,\.\?\/\*\<\>]*)$/
993 or return "Illegal finger: ". $self->getfield('finger');
994 $self->setfield('finger', $1);
996 $recref->{dir} =~ /^([\/\w\-\.\&]*)$/
997 or return "Illegal directory";
999 return "Illegal directory"
1000 if $recref->{dir} =~ /(^|\/)\.+(\/|$)/; #no .. component
1001 return "Illegal directory"
1002 if $recref->{dir} =~ /\&/ && ! $username_ampersand;
1003 unless ( $recref->{dir} ) {
1004 $recref->{dir} = $dir_prefix . '/';
1005 if ( $dirhash > 0 ) {
1006 for my $h ( 1 .. $dirhash ) {
1007 $recref->{dir} .= substr($recref->{username}, $h-1, 1). '/';
1009 } elsif ( $dirhash < 0 ) {
1010 for my $h ( reverse $dirhash .. -1 ) {
1011 $recref->{dir} .= substr($recref->{username}, $h, 1). '/';
1014 $recref->{dir} .= $recref->{username};
1018 unless ( $recref->{username} eq 'sync' ) {
1019 if ( grep $_ eq $recref->{shell}, @shells ) {
1020 $recref->{shell} = (grep $_ eq $recref->{shell}, @shells)[0];
1022 return "Illegal shell \`". $self->shell. "\'; ".
1023 $conf->dir. "/shells contains: @shells";
1026 $recref->{shell} = '/bin/sync';
1029 $recref->{quota} =~ /^(\d*)$/ or return "Illegal quota (unimplemented)";
1030 $recref->{quota} = $1;
1033 $recref->{gid} ne '' ?
1034 return "Can't have gid without uid" : ( $recref->{gid}='' );
1035 $recref->{finger} ne '' ?
1036 return "Can't have finger-name without uid" : ( $recref->{finger}='' );
1037 $recref->{dir} ne '' ?
1038 return "Can't have directory without uid" : ( $recref->{dir}='' );
1039 $recref->{shell} ne '' ?
1040 return "Can't have shell without uid" : ( $recref->{shell}='' );
1041 $recref->{quota} ne '' ?
1042 return "Can't have quota without uid" : ( $recref->{quota}='' );
1045 unless ( $part_svc->part_svc_column('slipip')->columnflag eq 'F' ) {
1046 unless ( $recref->{slipip} eq '0e0' ) {
1047 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
1048 or return "Illegal slipip". $self->slipip;
1049 $recref->{slipip} = $1;
1051 $recref->{slipip} = '0e0';
1056 #arbitrary RADIUS stuff; allow ut_textn for now
1057 foreach ( grep /^radius_/, fields('svc_acct') ) {
1058 $self->ut_textn($_);
1061 #generate a password if it is blank
1062 $recref->{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) )
1063 unless ( $recref->{_password} );
1065 #if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,16})$/ ) {
1066 if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{$passwordmin,$passwordmax})$/ ) {
1067 $recref->{_password} = $1.$3;
1068 #uncomment this to encrypt password immediately upon entry, or run
1069 #bin/crypt_pw in cron to give new users a window during which their
1070 #password is available to techs, for faxing, etc. (also be aware of
1072 #$recref->{password} = $1.
1073 # crypt($3,$saltset[int(rand(64))].$saltset[int(rand(64))]
1075 } elsif ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([\w\.\/\$]{13,34})$/ ) {
1076 $recref->{_password} = $1.$3;
1077 } elsif ( $recref->{_password} eq '*' ) {
1078 $recref->{_password} = '*';
1079 } elsif ( $recref->{_password} eq '!!' ) {
1080 $recref->{_password} = '!!';
1082 #return "Illegal password";
1083 return gettext('illegal_password'). ": ". $recref->{_password};
1091 Depriciated, use radius_reply instead.
1096 carp "FS::svc_acct::radius depriciated, use radius_reply";
1097 $_[0]->radius_reply;
1102 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1103 reply attributes of this record.
1105 Note that this is now the preferred method for reading RADIUS attributes -
1106 accessing the columns directly is discouraged, as the column names are
1107 expected to change in the future.
1116 my($column, $attrib) = ($1, $2);
1117 #$attrib =~ s/_/\-/g;
1118 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1119 } grep { /^radius_/ && $self->getfield($_) } fields( $self->table );
1120 if ( $self->ip && $self->ip ne '0e0' ) {
1121 $reply{'Framed-IP-Address'} = $self->ip;
1128 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
1129 check attributes of this record.
1131 Note that this is now the preferred method for reading RADIUS attributes -
1132 accessing the columns directly is discouraged, as the column names are
1133 expected to change in the future.
1139 ( 'Password' => $self->_password,
1142 my($column, $attrib) = ($1, $2);
1143 #$attrib =~ s/_/\-/g;
1144 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
1145 } grep { /^rc_/ && $self->getfield($_) } fields( $self->table )
1151 Returns the domain associated with this account.
1157 if ( $self->domsvc ) {
1158 #$self->svc_domain->domain;
1159 my $svc_domain = $self->svc_domain
1160 or die "no svc_domain.svcnum for svc_acct.domsvc ". $self->domsvc;
1161 $svc_domain->domain;
1163 $mydomain or die "svc_acct.domsvc is null and no legacy domain config file";
1169 Returns the FS::svc_domain record for this account's domain (see
1177 ? $self->{'_domsvc'}
1178 : qsearchs( 'svc_domain', { 'svcnum' => $self->domsvc } );
1183 Returns the FS::cust_svc record for this account (see L<FS::cust_svc>).
1187 qsearchs( 'cust_svc', { 'svcnum' => $self->svcnum } );
1192 Returns an email address associated with the account.
1198 $self->username. '@'. $self->domain;
1201 =item seconds_since TIMESTAMP
1203 Returns the number of seconds this account has been online since TIMESTAMP.
1206 TIMESTAMP is specified as a UNIX timestamp; see L<perlfunc/"time">. Also see
1207 L<Time::Local> and L<Date::Parse> for conversion functions.
1211 #note: POD here, implementation in FS::cust_svc
1214 $self->cust_svc->seconds_since(@_);
1219 Returns all RADIUS groups for this account (see L<FS::radius_usergroup>).
1225 map { $_->groupname }
1226 qsearch('radius_usergroup', { 'svcnum' => $self->svcnum } );
1233 =item radius_usergroup_selector GROUPS_ARRAYREF [ SELECTNAME ]
1237 sub radius_usergroup_selector {
1238 my $sel_groups = shift;
1239 my %sel_groups = map { $_=>1 } @$sel_groups;
1241 my $selectname = shift || 'radius_usergroup';
1244 my $sth = $dbh->prepare(
1245 'SELECT DISTINCT(groupname) FROM radius_usergroup ORDER BY groupname'
1246 ) or die $dbh->errstr;
1247 $sth->execute() or die $sth->errstr;
1248 my @all_groups = map { $_->[0] } @{$sth->fetchall_arrayref};
1252 function ${selectname}_doadd(object) {
1253 var myvalue = object.${selectname}_add.value;
1254 var optionName = new Option(myvalue,myvalue,false,true);
1255 var length = object.$selectname.length;
1256 object.$selectname.options[length] = optionName;
1257 object.${selectname}_add.value = "";
1260 <SELECT MULTIPLE NAME="$selectname">
1263 foreach my $group ( @all_groups ) {
1265 if ( $sel_groups{$group} ) {
1266 $html .= ' SELECTED';
1267 $sel_groups{$group} = 0;
1269 $html .= ">$group</OPTION>\n";
1271 foreach my $group ( grep { $sel_groups{$_} } keys %sel_groups ) {
1272 $html .= "<OPTION SELECTED>$group</OPTION>\n";
1274 $html .= '</SELECT>';
1276 $html .= qq!<BR><INPUT TYPE="text" NAME="${selectname}_add">!.
1277 qq!<INPUT TYPE="button" VALUE="Add new group" onClick="${selectname}_doadd(this.form)">!;
1284 The $recref stuff in sub check should be cleaned up.
1286 The suspend, unsuspend and cancel methods update the database, but not the
1287 current object. This is probably a bug as it's unexpected and
1290 radius_usergroup_selector? putting web ui components in here? they should
1291 probably live somewhere else...
1295 L<FS::svc_Common>, edit/part_svc.cgi from an installed web interface,
1296 export.html from the base documentation, L<FS::Record>, L<FS::Conf>,
1297 L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>, L<FS::queue>,
1298 L<freeside-queued>), L<Net::SSH>, L<ssh>, L<FS::svc_acct_pop>,
1299 schema.html from the base documentation.