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 warn "cyrus_insert: starting for user $username, quota $quota\n";
260 warn "cyrus_insert: connecting to $cyrus_server\n";
261 my $client = Cyrus::IMAP::Admin->new($cyrus_server);
263 warn "cyrus_insert: authentication as $cyrus_admin_user\n";
264 $client->authenticate(
265 -user => $cyrus_admin_user,
266 -mechanism => "login",
267 -password => $cyrus_admin_pass
270 warn "cyrus_insert: creating user.$username\n";
271 my $rc = $client->create("user.$username");
272 my $error = $client->error;
273 die "cyrus_insert: error creating user.$username: $error" if $error;
275 warn "cyrus_insert: setacl user.$username, $username => all\n";
276 $rc = $client->setacl("user.$username", $username => 'all' );
277 $error = $client->error;
278 die $error if $error;
281 warn "cyrus_insert: setquota user.$username, STORAGE => $quota\n";
282 $rc = $client->setquota("user.$username", 'STORAGE' => $quota );
283 $error = $client->error;
284 die $error if $error;
292 Deletes this account from the database. If there is an error, returns the
293 error, otherwise returns false.
295 The corresponding FS::cust_svc record will be deleted as well.
297 If the configuration value (see L<FS::Conf>) shellmachine exists, the
298 command(s) specified in the shellmachine-userdel configuration file are
299 added to the job queue (see L<FS::queue> and L<freeside-queued>) to be executed
300 on shellmachine via ssh. This behavior can be surpressed by setting
301 $FS::svc_acct::nossh_hack true. If the shellmachine-userdel configuration
306 is the default. If the shellmachine-userdel configuration file exists but
311 is the default instead. Otherwise the contents of the file are treated as a
312 double-quoted perl string, with the following variables available:
315 (TODOC: cyrus config file)
322 return "Can't delete an account which has (svc_acct_sm) mail aliases!"
323 if $self->uid && qsearch( 'svc_acct_sm', { 'domuid' => $self->uid } );
325 return "Can't delete an account which is a (svc_forward) source!"
326 if qsearch( 'svc_forward', { 'srcsvc' => $self->svcnum } );
328 return "Can't delete an account which is a (svc_forward) destination!"
329 if qsearch( 'svc_forward', { 'dstsvc' => $self->svcnum } );
331 return "Can't delete an account with (svc_www) web service!"
332 if qsearch( 'svc_www', { 'usersvc' => $self->usersvc } );
334 # what about records in session ?
336 local $SIG{HUP} = 'IGNORE';
337 local $SIG{INT} = 'IGNORE';
338 local $SIG{QUIT} = 'IGNORE';
339 local $SIG{TERM} = 'IGNORE';
340 local $SIG{TSTP} = 'IGNORE';
341 local $SIG{PIPE} = 'IGNORE';
343 my $oldAutoCommit = $FS::UID::AutoCommit;
344 local $FS::UID::AutoCommit = 0;
347 foreach my $cust_main_invoice (
348 qsearch( 'cust_main_invoice', { 'dest' => $self->svcnum } )
350 my %hash = $cust_main_invoice->hash;
351 $hash{'dest'} = $self->email;
352 my $new = new FS::cust_main_invoice \%hash;
353 my $error = $new->replace($cust_main_invoice);
355 $dbh->rollback if $oldAutoCommit;
360 foreach my $svc_domain (
361 qsearch( 'svc_domain', { 'catchall' => $self->svcnum } )
363 my %hash = new FS::svc_domain->hash;
364 $hash{'catchall'} = '';
365 my $new = new FS::svc_domain \%hash;
366 my $error = $new->replace($svc_domain);
368 $dbh->rollback if $oldAutoCommit;
373 my $error = $self->SUPER::delete;
375 $dbh->rollback if $oldAutoCommit;
379 my( $username, $dir ) = (
383 if ( $username && $shellmachine && ! $nossh_hack ) {
384 my $queue = new FS::queue { 'job' => 'Net::SSH::ssh' };
385 $error = $queue->insert("root\@$shellmachine", eval qq("$userdel") );
387 $dbh->rollback if $oldAutoCommit;
388 return "queueing job (transaction rolled back): $error";
393 if ( $cyrus_server ) {
394 my $queue = new FS::queue { 'job' => 'FS::svc_acct::cyrus_delete' };
395 $error = $queue->insert($self->username);
397 $dbh->rollback if $oldAutoCommit;
398 return "queueing job (transaction rolled back): $error";
402 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
407 my $username = shift;
409 my $client = Cyrus::IMAP::Admin->new($cyrus_server);
410 $client->authenticate(
411 -user => $cyrus_admin_user,
412 -mechanism => "login",
413 -password => $cyrus_admin_pass
416 my $rc = $client->setacl("user.$username", $cyrus_admin_user => 'all' );
417 my $error = $client->error;
418 die $error if $error;
420 $rc = $client->delete("user.$username");
421 $error = $client->error;
422 die $error if $error;
427 =item replace OLD_RECORD
429 Replaces OLD_RECORD with this one in the database. If there is an error,
430 returns the error, otherwise returns false.
432 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
433 dir field has changed, the command(s) specified in the shellmachine-usermod
434 configuraiton file are added to the job queue (see L<FS::queue> and
435 L<freeside-queued>) to be executed on shellmachine via ssh. This behavior can
436 be surpressed by setting $FS::svc-acct::nossh_hack true. If the
437 shellmachine-userdel configuration file does not exist or is empty,
439 [ -d $old_dir ] && mv $old_dir $new_dir || (
443 find . -depth -print | cpio -pdm $new_dir;
445 chown -R $uid.$gid $new_dir;
449 is the default. This behaviour can be surpressed by setting
450 $FS::svc_acct::nossh_hack true.
455 my ( $new, $old ) = ( shift, shift );
458 return "Username in use"
459 if $old->username ne $new->username &&
460 qsearchs( 'svc_acct', { 'username' => $new->username } );
462 return "Can't change uid!" if $old->uid != $new->uid;
464 return "can't change username using Cyrus"
465 if $cyrus_server && $old->username ne $new->username;
467 #change homdir when we change username
468 $new->setfield('dir', '') if $old->username ne $new->username;
470 local $SIG{HUP} = 'IGNORE';
471 local $SIG{INT} = 'IGNORE';
472 local $SIG{QUIT} = 'IGNORE';
473 local $SIG{TERM} = 'IGNORE';
474 local $SIG{TSTP} = 'IGNORE';
475 local $SIG{PIPE} = 'IGNORE';
477 my $oldAutoCommit = $FS::UID::AutoCommit;
478 local $FS::UID::AutoCommit = 0;
481 $error = $new->SUPER::replace($old);
483 $dbh->rollback if $oldAutoCommit;
484 return $error if $error;
487 my ( $old_dir, $new_dir, $uid, $gid ) = (
488 $old->getfield('dir'),
489 $new->getfield('dir'),
490 $new->getfield('uid'),
491 $new->getfield('gid'),
493 if ( $old_dir && $new_dir && $old_dir ne $new_dir && ! $nossh_hack ) {
494 my $queue = new FS::queue { 'job' => 'Net::SSH::ssh' };
495 $error = $queue->insert("root\@$shellmachine", eval qq("$usermod") );
497 $dbh->rollback if $oldAutoCommit;
498 return "queueing job (transaction rolled back): $error";
502 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
508 Suspends this account by prefixing *SUSPENDED* to the password. If there is an
509 error, returns the error, otherwise returns false.
511 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
517 my %hash = $self->hash;
518 unless ( $hash{_password} =~ /^\*SUSPENDED\* / ) {
519 $hash{_password} = '*SUSPENDED* '.$hash{_password};
520 my $new = new FS::svc_acct ( \%hash );
521 $new->replace($self);
523 ''; #no error (already suspended)
529 Unsuspends this account by removing *SUSPENDED* from the password. If there is
530 an error, returns the error, otherwise returns false.
532 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
538 my %hash = $self->hash;
539 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
540 $hash{_password} = $1;
541 my $new = new FS::svc_acct ( \%hash );
542 $new->replace($self);
544 ''; #no error (already unsuspended)
550 Just returns false (no error) for now.
552 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
556 Checks all fields to make sure this is a valid service. If there is an error,
557 returns the error, otherwise returns false. Called by the insert and replace
560 Sets any fixed values; see L<FS::part_svc>.
567 my($recref) = $self->hashref;
569 my $x = $self->setfixed;
570 return $x unless ref($x);
573 my $error = $self->ut_numbern('svcnum')
574 || $self->ut_number('domsvc')
576 return $error if $error;
578 my $ulen = $usernamemax || $self->dbdef_table->column('username')->length;
579 $recref->{username} =~ /^([a-z0-9_\-\.]{$usernamemin,$ulen})$/
580 or return "Illegal username";
581 $recref->{username} = $1;
582 if ( $username_letterfirst ) {
583 $recref->{username} =~ /^[a-z]/ or return "Illegal username";
584 } elsif ( $username_letter ) {
585 $recref->{username} =~ /[a-z]/ or return "Illegal username";
587 if ( $username_noperiod ) {
588 $recref->{username} =~ /\./ and return "Illegal username";
591 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum: ".$recref->{popnum};
592 $recref->{popnum} = $1;
593 return "Unknown popnum" unless
594 ! $recref->{popnum} ||
595 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
597 unless ( $part_svc->part_svc_column('uid')->columnflag eq 'F' ) {
599 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
600 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
602 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
603 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
604 #not all systems use gid=uid
605 #you can set a fixed gid in part_svc
607 return "Only root can have uid 0"
608 if $recref->{uid} == 0 && $recref->{username} ne 'root';
610 $error = $self->ut_textn('finger');
611 return $error if $error;
613 $recref->{dir} =~ /^([\/\w\-]*)$/
614 or return "Illegal directory";
615 $recref->{dir} = $1 ||
616 $dir_prefix . '/' . $recref->{username}
617 #$dir_prefix . '/' . substr($recref->{username},0,1). '/' . $recref->{username}
620 unless ( $recref->{username} eq 'sync' ) {
621 if ( grep $_ eq $recref->{shell}, @shells ) {
622 $recref->{shell} = (grep $_ eq $recref->{shell}, @shells)[0];
624 return "Illegal shell \`". $self->shell. "\'; ".
625 $conf->dir. "/shells contains: @shells";
628 $recref->{shell} = '/bin/sync';
631 $recref->{quota} =~ /^(\d*)$/ or return "Illegal quota (unimplemented)";
632 $recref->{quota} = $1;
635 $recref->{gid} ne '' ?
636 return "Can't have gid without uid" : ( $recref->{gid}='' );
637 $recref->{finger} ne '' ?
638 return "Can't have finger-name without uid" : ( $recref->{finger}='' );
639 $recref->{dir} ne '' ?
640 return "Can't have directory without uid" : ( $recref->{dir}='' );
641 $recref->{shell} ne '' ?
642 return "Can't have shell without uid" : ( $recref->{shell}='' );
643 $recref->{quota} ne '' ?
644 return "Can't have quota without uid" : ( $recref->{quota}='' );
647 unless ( $part_svc->part_svc_column('slipip')->columnflag eq 'F' ) {
648 unless ( $recref->{slipip} eq '0e0' ) {
649 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
650 or return "Illegal slipip". $self->slipip;
651 $recref->{slipip} = $1;
653 $recref->{slipip} = '0e0';
658 #arbitrary RADIUS stuff; allow ut_textn for now
659 foreach ( grep /^radius_/, fields('svc_acct') ) {
663 #generate a password if it is blank
664 $recref->{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) )
665 unless ( $recref->{_password} );
667 #if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,16})$/ ) {
668 if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{$passwordmin,8})$/ ) {
669 $recref->{_password} = $1.$3;
670 #uncomment this to encrypt password immediately upon entry, or run
671 #bin/crypt_pw in cron to give new users a window during which their
672 #password is available to techs, for faxing, etc. (also be aware of
674 #$recref->{password} = $1.
675 # crypt($3,$saltset[int(rand(64))].$saltset[int(rand(64))]
677 } elsif ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([\w\.\/\$]{13,34})$/ ) {
678 $recref->{_password} = $1.$3;
679 } elsif ( $recref->{_password} eq '*' ) {
680 $recref->{_password} = '*';
681 } elsif ( $recref->{_password} eq '!!' ) {
682 $recref->{_password} = '!!';
684 return "Illegal password";
692 Depriciated, use radius_reply instead.
697 carp "FS::svc_acct::radius depriciated, use radius_reply";
703 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
704 reply attributes of this record.
706 Note that this is now the preferred method for reading RADIUS attributes -
707 accessing the columns directly is discouraged, as the column names are
708 expected to change in the future.
716 my($column, $attrib) = ($1, $2);
717 #$attrib =~ s/_/\-/g;
718 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
719 } grep { /^radius_/ && $self->getfield($_) } fields( $self->table );
724 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
725 check attributes of this record.
727 Accessing RADIUS attributes directly is not supported and will break in the
736 my($column, $attrib) = ($1, $2);
737 #$attrib =~ s/_/\-/g;
738 ( $FS::raddb::attrib{lc($attrib)}, $self->getfield($column) );
739 } grep { /^rc_/ && $self->getfield($_) } fields( $self->table );
744 Returns the domain associated with this account.
750 if ( $self->domsvc ) {
751 my $svc_domain = qsearchs( 'svc_domain', { 'svcnum' => $self->domsvc } )
752 or die "no svc_domain.svcnum for svc_acct.domsvc ". $self->domsvc;
755 $mydomain or die "svc_acct.domsvc is null and no legacy domain config file";
761 Returns an email address associated with the account.
767 $self->username. '@'. $self->domain;
774 $Id: svc_acct.pm,v 1.37 2001-09-11 12:06:57 ivan Exp $
778 The bits which ssh should fork before doing so (or maybe queue jobs for a
781 The $recref stuff in sub check should be cleaned up.
783 The suspend, unsuspend and cancel methods update the database, but not the
784 current object. This is probably a bug as it's unexpected and
789 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
790 L<FS::part_svc>, L<FS::cust_pkg>, L<FS::queue>, L<freeside-queued>),
791 L<Net::SSH>, L<ssh>, L<FS::svc_acct_pop>,
792 schema.html from the base documentation.