4 use vars qw( @ISA $nossh_hack $conf $dir_prefix @shells $usernamemin
5 $usernamemax $passwordmin $username_letter $username_letterfirst
7 $shellmachine $useradd $usermod $userdel $mydomain
8 $cyrus_server $cyrus_admin_user $cyrus_admin_pass
12 use FS::Record qw( qsearch qsearchs fields dbh );
18 use FS::cust_main_invoice;
23 @ISA = qw( FS::svc_Common );
25 #ask FS::UID to run this stuff for us later
26 $FS::UID::callback{'FS::svc_acct'} = sub {
28 $dir_prefix = $conf->config('home');
29 @shells = $conf->config('shells');
30 $shellmachine = $conf->config('shellmachine');
31 $usernamemin = $conf->config('usernamemin') || 2;
32 $usernamemax = $conf->config('usernamemax');
33 $passwordmin = $conf->config('passwordmin') || 6;
34 if ( $shellmachine ) {
35 if ( $conf->exists('shellmachine-useradd') ) {
36 $useradd = join("\n", $conf->config('shellmachine-useradd') )
37 || 'cp -pr /etc/skel $dir; chown -R $uid.$gid $dir';
39 $useradd = 'useradd -d $dir -m -s $shell -u $uid $username';
41 if ( $conf->exists('shellmachine-userdel') ) {
42 $userdel = join("\n", $conf->config('shellmachine-userdel') )
45 $userdel = 'userdel $username';
47 $usermod = join("\n", $conf->config('shellmachine-usermod') )
48 || '[ -d $old_dir ] && mv $old_dir $new_dir || ( '.
49 'chmod u+t $old_dir; mkdir $new_dir; cd $old_dir; '.
50 'find . -depth -print | cpio -pdm $new_dir; '.
51 'chmod u-t $new_dir; chown -R $uid.$gid $new_dir; '.
55 $username_letter = $conf->exists('username-letter');
56 $username_letterfirst = $conf->exists('username-letterfirst');
57 $username_noperiod = $conf->exists('username-noperiod');
58 $mydomain = $conf->config('domain');
59 if ( $conf->exists('cyrus') ) {
60 ($cyrus_server, $cyrus_admin_user, $cyrus_admin_pass) =
61 $conf->config('cyrus');
62 eval "use Cyrus::IMAP::Admin;"
65 $cyrus_admin_user = '';
66 $cyrus_admin_pass = '';
70 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
71 @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '!', '.', ',' );
73 #not needed in 5.004 #srand($$|time);
77 FS::svc_acct - Object methods for svc_acct records
83 $record = new FS::svc_acct \%hash;
84 $record = new FS::svc_acct { 'column' => 'value' };
86 $error = $record->insert;
88 $error = $new_record->replace($old_record);
90 $error = $record->delete;
92 $error = $record->check;
94 $error = $record->suspend;
96 $error = $record->unsuspend;
98 $error = $record->cancel;
100 %hash = $record->radius;
102 %hash = $record->radius_reply;
104 %hash = $record->radius_check;
108 An FS::svc_acct object represents an account. FS::svc_acct inherits from
109 FS::svc_Common. The following fields are currently supported:
113 =item svcnum - primary key (assigned automatcially for new accounts)
117 =item _password - generated if blank
119 =item popnum - Point of presence (see L<FS::svc_acct_pop>)
127 =item dir - set automatically if blank (and uid is not)
131 =item quota - (unimplementd)
133 =item slipip - IP address
137 =item domsvc - svcnum from svc_domain
139 =item radius_I<Radius_Attribute> - I<Radius-Attribute>
141 =item domsvc - service number of svc_domain with which to associate
151 Creates a new account. To add the account to the database, see L<"insert">.
155 sub table { 'svc_acct'; }
159 Adds this account to the database. If there is an error, returns the error,
160 otherwise returns false.
162 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
163 defined. An FS::cust_svc record will be created and inserted.
165 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
166 username, uid, and dir fields are defined, the command(s) specified in
167 the shellmachine-useradd configuration are added to the job queue (see
168 L<FS::queue> and L<freeside-queued>) to be exectued on shellmachine via ssh.
169 This behaviour can be surpressed by setting $FS::svc_acct::nossh_hack true.
170 If the shellmachine-useradd configuration file does not exist,
172 useradd -d $dir -m -s $shell -u $uid $username
174 is the default. If the shellmachine-useradd configuration file exists but
177 cp -pr /etc/skel $dir; chown -R $uid.$gid $dir
179 is the default instead. Otherwise the contents of the file are treated as
180 a double-quoted perl string, with the following variables available:
181 $username, $uid, $gid, $dir, and $shell.
183 (TODOC: cyrus config file, L<FS::queue> and L<freeside-queued>)
191 local $SIG{HUP} = 'IGNORE';
192 local $SIG{INT} = 'IGNORE';
193 local $SIG{QUIT} = 'IGNORE';
194 local $SIG{TERM} = 'IGNORE';
195 local $SIG{TSTP} = 'IGNORE';
196 local $SIG{PIPE} = 'IGNORE';
198 my $oldAutoCommit = $FS::UID::AutoCommit;
199 local $FS::UID::AutoCommit = 0;
204 $error = $self->check;
205 return $error if $error;
207 return "Username ". $self->username. " in use"
208 if qsearchs( 'svc_acct', { 'username' => $self->username,
209 'domsvc' => $self->domsvc,
212 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
213 return "Unknown svcpart" unless $part_svc;
215 if $part_svc->part_svc_column('uid')->columnflag ne 'F'
216 && qsearchs( 'svc_acct', { 'uid' => $self->uid } )
217 && $self->username !~ /^(hyla)?fax$/
220 $error = $self->SUPER::insert;
222 $dbh->rollback if $oldAutoCommit;
226 my( $username, $uid, $gid, $dir, $shell ) = (
233 if ( $username && $uid && $dir && $shellmachine && ! $nossh_hack ) {
234 my $queue = new FS::queue { 'job' => 'Net::SSH::ssh' };
235 $error = $queue->insert("root\@$shellmachine", eval qq("$useradd") );
237 $dbh->rollback if $oldAutoCommit;
238 return "queueing job (transaction rolled back): $error";
242 if ( $cyrus_server ) {
243 my $queue = new FS::queue { 'job' => 'FS::svc_acct::cyrus_insert' };
244 $error = $queue->insert($self->username, $self->quota);
246 $dbh->rollback if $oldAutoCommit;
247 return "queueing job (transaction rolled back): $error";
251 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
256 my( $username, $quota ) =
258 my $client = Cyrus::IMAP::Admin->new($cyrus_server);
259 $client->authenticate(
260 -user => $cyrus_admin_user,
261 -mechanism => "login",
262 -password => $cyrus_admin_pass
265 my $rc = $client->create("user.$username");
266 my $error = $client->error;
267 die $error if $error;
269 $rc = $client->setacl("user.$username", $username => 'all' );
270 $error = $client->error;
271 die $error if $error;
274 $rc = $client->setquota("user.$username", 'STORAGE' => $quota );
275 $error = $client->error;
276 die $error if $error;
284 Deletes this account from the database. If there is an error, returns the
285 error, otherwise returns false.
287 The corresponding FS::cust_svc record will be deleted as well.
289 If the configuration value (see L<FS::Conf>) shellmachine exists, the
290 command(s) specified in the shellmachine-userdel configuration file are
291 added to the job queue (see L<FS::queue> and L<freeside-queued>) to be executed
292 on shellmachine via ssh. This behavior can be surpressed by setting
293 $FS::svc_acct::nossh_hack true. If the shellmachine-userdel configuration
298 is the default. If the shellmachine-userdel configuration file exists but
303 is the default instead. Otherwise the contents of the file are treated as a
304 double-quoted perl string, with the following variables available:
307 (TODOC: cyrus config file)
314 return "Can't delete an account which has (svc_acct_sm) mail aliases!"
315 if $self->uid && qsearch( 'svc_acct_sm', { 'domuid' => $self->uid } );
317 return "Can't delete an account which is a (svc_forward) source!"
318 if qsearch( 'svc_forward', { 'srcsvc' => $self->svcnum } );
320 return "Can't delete an account which is a (svc_forward) destination!"
321 if qsearch( 'svc_forward', { 'dstsvc' => $self->svcnum } );
323 return "Can't delete an account with (svc_www) web service!"
324 if qsearch( 'svc_www', { 'usersvc' => $self->usersvc } );
326 # what about records in session ?
328 local $SIG{HUP} = 'IGNORE';
329 local $SIG{INT} = 'IGNORE';
330 local $SIG{QUIT} = 'IGNORE';
331 local $SIG{TERM} = 'IGNORE';
332 local $SIG{TSTP} = 'IGNORE';
333 local $SIG{PIPE} = 'IGNORE';
335 my $oldAutoCommit = $FS::UID::AutoCommit;
336 local $FS::UID::AutoCommit = 0;
339 foreach my $cust_main_invoice (
340 qsearch( 'cust_main_invoice', { 'dest' => $self->svcnum } )
342 my %hash = $cust_main_invoice->hash;
343 $hash{'dest'} = $self->email;
344 my $new = new FS::cust_main_invoice \%hash;
345 my $error = $new->replace($cust_main_invoice);
347 $dbh->rollback if $oldAutoCommit;
352 foreach my $svc_domain (
353 qsearch( 'svc_domain', { 'catchall' => $self->svcnum } )
355 my %hash = new FS::svc_domain->hash;
356 $hash{'catchall'} = '';
357 my $new = new FS::svc_domain \%hash;
358 my $error = $new->replace($svc_domain);
360 $dbh->rollback if $oldAutoCommit;
365 my $error = $self->SUPER::delete;
367 $dbh->rollback if $oldAutoCommit;
371 my( $username, $dir ) = (
375 if ( $username && $shellmachine && ! $nossh_hack ) {
376 my $queue = new FS::queue { 'job' => 'Net::SSH::ssh' };
377 $error = $queue->insert("root\@$shellmachine", eval qq("$userdel") );
379 $dbh->rollback if $oldAutoCommit;
380 return "queueing job (transaction rolled back): $error";
385 if ( $cyrus_server ) {
386 my $queue = new FS::queue { 'job' => 'FS::svc_acct::cyrus_delete' };
387 $error = $queue->insert($self->username);
389 $dbh->rollback if $oldAutoCommit;
390 return "queueing job (transaction rolled back): $error";
394 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
399 my( $username ) = shift;
401 my $client = Cyrus::IMAP::Admin->new($cyrus_server);
402 $client->authenticate(
403 -user => $cyrus_admin_user,
404 -mechanism => "login",
405 -password => $cyrus_admin_pass
408 my $rc = $client->setacl("user.$username", $cyrus_admin_user => 'all' );
409 my $error = $client->error;
410 die $error if $error;
412 $rc = $client->delete("user.$username");
413 $error = $client->error;
414 die $error if $error;
419 =item replace OLD_RECORD
421 Replaces OLD_RECORD with this one in the database. If there is an error,
422 returns the error, otherwise returns false.
424 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
425 dir field has changed, the command(s) specified in the shellmachine-usermod
426 configuraiton file are added to the job queue (see L<FS::queue> and
427 L<freeside-queued>) to be executed on shellmachine via ssh. This behavior can
428 be surpressed by setting $FS::svc-acct::nossh_hack true. If the
429 shellmachine-userdel configuration file does not exist or is empty,
431 [ -d $old_dir ] && mv $old_dir $new_dir || (
435 find . -depth -print | cpio -pdm $new_dir;
437 chown -R $uid.$gid $new_dir;
441 is the default. This behaviour can be surpressed by setting
442 $FS::svc_acct::nossh_hack true.
447 my ( $new, $old ) = ( shift, shift );
450 return "Username in use"
451 if $old->username ne $new->username &&
452 qsearchs( 'svc_acct', { 'username' => $new->username } );
454 return "Can't change uid!" if $old->uid != $new->uid;
456 return "can't change username using Cyrus"
457 if $cyrus_server && $old->username ne $new->username;
459 #change homdir when we change username
460 $new->setfield('dir', '') if $old->username ne $new->username;
462 local $SIG{HUP} = 'IGNORE';
463 local $SIG{INT} = 'IGNORE';
464 local $SIG{QUIT} = 'IGNORE';
465 local $SIG{TERM} = 'IGNORE';
466 local $SIG{TSTP} = 'IGNORE';
467 local $SIG{PIPE} = 'IGNORE';
469 my $oldAutoCommit = $FS::UID::AutoCommit;
470 local $FS::UID::AutoCommit = 0;
473 $error = $new->SUPER::replace($old);
475 $dbh->rollback if $oldAutoCommit;
476 return $error if $error;
479 my ( $old_dir, $new_dir, $uid, $gid ) = (
480 $old->getfield('dir'),
481 $new->getfield('dir'),
482 $new->getfield('uid'),
483 $new->getfield('gid'),
485 if ( $old_dir && $new_dir && $old_dir ne $new_dir && ! $nossh_hack ) {
486 my $queue = new FS::queue { 'job' => 'Net::SSH::ssh' };
487 $error = $queue->insert("root\@$shellmachine", eval qq("$usermod") );
489 $dbh->rollback if $oldAutoCommit;
490 return "queueing job (transaction rolled back): $error";
494 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
500 Suspends this account by prefixing *SUSPENDED* to the password. If there is an
501 error, returns the error, otherwise returns false.
503 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
509 my %hash = $self->hash;
510 unless ( $hash{_password} =~ /^\*SUSPENDED\* / ) {
511 $hash{_password} = '*SUSPENDED* '.$hash{_password};
512 my $new = new FS::svc_acct ( \%hash );
513 $new->replace($self);
515 ''; #no error (already suspended)
521 Unsuspends this account by removing *SUSPENDED* from the password. If there is
522 an error, returns the error, otherwise returns false.
524 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
530 my %hash = $self->hash;
531 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
532 $hash{_password} = $1;
533 my $new = new FS::svc_acct ( \%hash );
534 $new->replace($self);
536 ''; #no error (already unsuspended)
542 Just returns false (no error) for now.
544 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
548 Checks all fields to make sure this is a valid service. If there is an error,
549 returns the error, otherwise returns false. Called by the insert and replace
552 Sets any fixed values; see L<FS::part_svc>.
559 my($recref) = $self->hashref;
561 my $x = $self->setfixed;
562 return $x unless ref($x);
565 my $error = $self->ut_numbern('svcnum')
566 || $self->ut_number('domsvc')
568 return $error if $error;
570 my $ulen = $usernamemax || $self->dbdef_table->column('username')->length;
571 $recref->{username} =~ /^([a-z0-9_\-\.]{$usernamemin,$ulen})$/
572 or return "Illegal username";
573 $recref->{username} = $1;
574 if ( $username_letterfirst ) {
575 $recref->{username} =~ /^[a-z]/ or return "Illegal username";
576 } elsif ( $username_letter ) {
577 $recref->{username} =~ /[a-z]/ or return "Illegal username";
579 if ( $username_noperiod ) {
580 $recref->{username} =~ /\./ and return "Illegal username";
583 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum: ".$recref->{popnum};
584 $recref->{popnum} = $1;
585 return "Unknown popnum" unless
586 ! $recref->{popnum} ||
587 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
589 unless ( $part_svc->part_svc_column('uid')->columnflag eq 'F' ) {
591 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
592 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
594 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
595 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
596 #not all systems use gid=uid
597 #you can set a fixed gid in part_svc
599 return "Only root can have uid 0"
600 if $recref->{uid} == 0 && $recref->{username} ne 'root';
602 $error = $self->ut_textn('finger');
603 return $error if $error;
605 $recref->{dir} =~ /^([\/\w\-]*)$/
606 or return "Illegal directory";
607 $recref->{dir} = $1 ||
608 $dir_prefix . '/' . $recref->{username}
609 #$dir_prefix . '/' . substr($recref->{username},0,1). '/' . $recref->{username}
612 unless ( $recref->{username} eq 'sync' ) {
613 if ( grep $_ eq $recref->{shell}, @shells ) {
614 $recref->{shell} = (grep $_ eq $recref->{shell}, @shells)[0];
616 return "Illegal shell \`". $self->shell. "\'; ".
617 $conf->dir. "/shells contains: @shells";
620 $recref->{shell} = '/bin/sync';
623 $recref->{quota} =~ /^(\d*)$/ or return "Illegal quota (unimplemented)";
624 $recref->{quota} = $1;
627 $recref->{gid} ne '' ?
628 return "Can't have gid without uid" : ( $recref->{gid}='' );
629 $recref->{finger} ne '' ?
630 return "Can't have finger-name without uid" : ( $recref->{finger}='' );
631 $recref->{dir} ne '' ?
632 return "Can't have directory without uid" : ( $recref->{dir}='' );
633 $recref->{shell} ne '' ?
634 return "Can't have shell without uid" : ( $recref->{shell}='' );
635 $recref->{quota} ne '' ?
636 return "Can't have quota without uid" : ( $recref->{quota}='' );
639 unless ( $part_svc->part_svc_column('slipip')->columnflag eq 'F' ) {
640 unless ( $recref->{slipip} eq '0e0' ) {
641 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
642 or return "Illegal slipip". $self->slipip;
643 $recref->{slipip} = $1;
645 $recref->{slipip} = '0e0';
650 #arbitrary RADIUS stuff; allow ut_textn for now
651 foreach ( grep /^radius_/, fields('svc_acct') ) {
655 #generate a password if it is blank
656 $recref->{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) )
657 unless ( $recref->{_password} );
659 #if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,16})$/ ) {
660 if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{$passwordmin,8})$/ ) {
661 $recref->{_password} = $1.$3;
662 #uncomment this to encrypt password immediately upon entry, or run
663 #bin/crypt_pw in cron to give new users a window during which their
664 #password is available to techs, for faxing, etc. (also be aware of
666 #$recref->{password} = $1.
667 # crypt($3,$saltset[int(rand(64))].$saltset[int(rand(64))]
669 } elsif ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([\w\.\/\$]{13,34})$/ ) {
670 $recref->{_password} = $1.$3;
671 } elsif ( $recref->{_password} eq '*' ) {
672 $recref->{_password} = '*';
673 } elsif ( $recref->{_password} eq '!!' ) {
674 $recref->{_password} = '!!';
676 return "Illegal password";
684 Depriciated, use radius_reply instead.
689 carp "FS::svc_acct::radius depriciated, use radius_reply";
695 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
696 reply attributes of this record.
698 Note that this is now the preferred method for reading RADIUS attributes -
699 accessing the columns directly is discouraged, as the column names are
700 expected to change in the future.
708 my($column, $attrib) = ($1, $2);
709 #$attrib =~ s/_/\-/g;
710 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
711 } grep { /^radius_/ && $self->getfield($_) } fields( $self->table );
716 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
717 check attributes of this record.
719 Accessing RADIUS attributes directly is not supported and will break in the
728 my($column, $attrib) = ($1, $2);
729 #$attrib =~ s/_/\-/g;
730 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
731 } grep { /^rc_/ && $self->getfield($_) } fields( $self->table );
736 Returns the domain associated with this account.
742 if ( $self->domsvc ) {
743 my $svc_domain = qsearchs( 'svc_domain', { 'svcnum' => $self->domsvc } )
744 or die "no svc_domain.svcnum for svc_acct.domsvc ". $self->domsvc;
747 $mydomain or die "svc_acct.domsvc is null and no legacy domain config file";
753 Returns an email address associated with the account.
759 $self->username. '@'. $self->domain;
766 $Id: svc_acct.pm,v 1.35 2001-09-11 04:17:47 ivan Exp $
770 The bits which ssh should fork before doing so (or maybe queue jobs for a
773 The $recref stuff in sub check should be cleaned up.
775 The suspend, unsuspend and cancel methods update the database, but not the
776 current object. This is probably a bug as it's unexpected and
781 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
782 L<FS::part_svc>, L<FS::cust_pkg>, L<FS::queue>, L<freeside-queued>),
783 L<Net::SSH>, L<ssh>, L<FS::svc_acct_pop>,
784 schema.html from the base documentation.