4 use vars qw( @ISA $nossh_hack $conf $dir_prefix @shells $usernamemin
5 $usernamemax $passwordmin $username_letter $username_letterfirst
6 $shellmachine $useradd $usermod $userdel $mydomain
10 use FS::Record qw( qsearch qsearchs fields dbh );
16 use FS::cust_main_invoice;
19 @ISA = qw( FS::svc_Common );
21 #ask FS::UID to run this stuff for us later
22 $FS::UID::callback{'FS::svc_acct'} = sub {
24 $dir_prefix = $conf->config('home');
25 @shells = $conf->config('shells');
26 $shellmachine = $conf->config('shellmachine');
27 $usernamemin = $conf->config('usernamemin') || 2;
28 $usernamemax = $conf->config('usernamemax');
29 $passwordmin = $conf->config('passwordmin') || 6;
30 if ( $shellmachine ) {
31 if ( $conf->exists('shellmachine-useradd') ) {
32 $useradd = join("\n", $conf->config('shellmachine-useradd') )
33 || 'cp -pr /etc/skel $dir; chown -R $uid.$gid $dir';
35 $useradd = 'useradd -d $dir -m -s $shell -u $uid $username';
37 if ( $conf->exists('shellmachine-userdel') ) {
38 $userdel = join("\n", $conf->config('shellmachine-userdel') )
41 $userdel = 'userdel $username';
43 $usermod = join("\n", $conf->config('shellmachine-usermod') )
44 || '[ -d $old_dir ] && mv $old_dir $new_dir || ( '.
45 'chmod u+t $old_dir; mkdir $new_dir; cd $old_dir; '.
46 'find . -depth -print | cpio -pdm $new_dir; '.
47 'chmod u-t $new_dir; chown -R $uid.$gid $new_dir; '.
51 $username_letter = $conf->exists('username-letter');
52 $username_letterfirst = $conf->exists('username-letterfirst');
53 $mydomain = $conf->config('domain');
56 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
57 @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '!', '.', ',' );
59 #not needed in 5.004 #srand($$|time);
63 FS::svc_acct - Object methods for svc_acct records
69 $record = new FS::svc_acct \%hash;
70 $record = new FS::svc_acct { 'column' => 'value' };
72 $error = $record->insert;
74 $error = $new_record->replace($old_record);
76 $error = $record->delete;
78 $error = $record->check;
80 $error = $record->suspend;
82 $error = $record->unsuspend;
84 $error = $record->cancel;
86 %hash = $record->radius;
88 %hash = $record->radius_reply;
90 %hash = $record->radius_check;
94 An FS::svc_acct object represents an account. FS::svc_acct inherits from
95 FS::svc_Common. The following fields are currently supported:
99 =item svcnum - primary key (assigned automatcially for new accounts)
103 =item _password - generated if blank
105 =item popnum - Point of presence (see L<FS::svc_acct_pop>)
113 =item dir - set automatically if blank (and uid is not)
117 =item quota - (unimplementd)
119 =item slipip - IP address
121 =item radius_I<Radius_Attribute> - I<Radius-Attribute>
123 =item domsvc - service number of svc_domain with which to associate
133 Creates a new account. To add the account to the database, see L<"insert">.
137 sub table { 'svc_acct'; }
141 Adds this account to the database. If there is an error, returns the error,
142 otherwise returns false.
144 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
145 defined. An FS::cust_svc record will be created and inserted.
147 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
148 username, uid, and dir fields are defined, the command(s) specified in
149 the shellmachine-useradd configuration are exectued on shellmachine via ssh.
150 This behaviour can be surpressed by setting $FS::svc_acct::nossh_hack true.
151 If the shellmachine-useradd configuration file does not exist,
153 useradd -d $dir -m -s $shell -u $uid $username
155 is the default. If the shellmachine-useradd configuration file exists but
158 cp -pr /etc/skel $dir; chown -R $uid.$gid $dir
160 is the default instead. Otherwise the contents of the file are treated as
161 a double-quoted perl string, with the following variables available:
162 $username, $uid, $gid, $dir, and $shell.
170 local $SIG{HUP} = 'IGNORE';
171 local $SIG{INT} = 'IGNORE';
172 local $SIG{QUIT} = 'IGNORE';
173 local $SIG{TERM} = 'IGNORE';
174 local $SIG{TSTP} = 'IGNORE';
175 local $SIG{PIPE} = 'IGNORE';
177 $error = $self->check;
178 return $error if $error;
180 return "Username ". $self->username. " in use"
181 if qsearchs( 'svc_acct', { 'username' => $self->username,
182 'domsvc' => $self->domsvc,
185 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
186 return "Unknown svcpart" unless $part_svc;
188 if $part_svc->svc_acct__uid_flag ne 'F'
189 && qsearchs( 'svc_acct', { 'uid' => $self->uid } )
190 && $self->username !~ /^(hyla)?fax$/
193 $error = $self->SUPER::insert;
194 return $error if $error;
196 my( $username, $uid, $gid, $dir, $shell ) = (
203 if ( $username && $uid && $dir && $shellmachine && ! $nossh_hack ) {
204 ssh("root\@$shellmachine", eval qq("$useradd") );
212 Deletes this account from the database. If there is an error, returns the
213 error, otherwise returns false.
215 The corresponding FS::cust_svc record will be deleted as well.
217 If the configuration value (see L<FS::Conf>) shellmachine exists, the
218 command(s) specified in the shellmachine-userdel configuration file are
219 executed on shellmachine via ssh. This behavior can be surpressed by setting
220 $FS::svc_acct::nossh_hack true. If the shellmachine-userdel configuration
225 is the default. If the shellmachine-userdel configuration file exists but
230 is the default instead. Otherwise the contents of the file are treated as a
231 double-quoted perl string, with the following variables available:
239 return "Can't delete an account which has (svc_acct_sm) mail aliases!"
240 if $self->uid && qsearch( 'svc_acct_sm', { 'domuid' => $self->uid } );
242 return "Can't delete an account which is a (svc_forward) source!"
243 if qsearch( 'svc_forward', { 'srcsvc' => $self->svcnum } );
245 return "Can't delete an account which is a (svc_forward) destination!"
246 if qsearch( 'svc_forward', { 'dstsvc' => $self->svcnum } );
248 return "Can't delete an account with (svc_www) web service!"
249 if qsearch( 'svc_www', { 'usersvc' => $self->usersvc } );
251 # what about records in session ?
253 local $SIG{HUP} = 'IGNORE';
254 local $SIG{INT} = 'IGNORE';
255 local $SIG{QUIT} = 'IGNORE';
256 local $SIG{TERM} = 'IGNORE';
257 local $SIG{TSTP} = 'IGNORE';
258 local $SIG{PIPE} = 'IGNORE';
260 my $oldAutoCommit = $FS::UID::AutoCommit;
261 local $FS::UID::AutoCommit = 0;
264 foreach my $cust_main_invoice (
265 qsearch( 'cust_main_invoice', { 'dest' => $self->svcnum } )
267 my %hash = $cust_main_invoice->hash;
268 $hash{'dest'} = $self->email;
269 my $new = new FS::cust_main_invoice \%hash;
270 my $error = $new->replace($cust_main_invoice);
272 $dbh->rollback if $oldAutoCommit;
277 foreach my $svc_domain (
278 qsearch( 'svc_domain', { 'catchall' => $self->svcnum } )
280 my %hash = new FS::svc_domain->hash;
281 $hash{'catchall'} = '';
282 my $new = new FS::svc_domain \%hash;
283 my $error = $new->replace($svc_domain);
285 $dbh->rollback if $oldAutoCommit;
290 my $error = $self->SUPER::delete;
292 $dbh->rollback if $oldAutoCommit;
296 $dbh->commit or die $dbh->errstr if $oldAutoCommit;
298 my( $username, $dir ) = (
302 if ( $username && $shellmachine && ! $nossh_hack ) {
303 ssh("root\@$shellmachine", eval qq("$userdel") );
309 =item replace OLD_RECORD
311 Replaces OLD_RECORD with this one in the database. If there is an error,
312 returns the error, otherwise returns false.
314 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
315 dir field has changed, the command(s) specified in the shellmachine-usermod
316 configuraiton file are executed on shellmachine via ssh. This behavior can
317 be surpressed by setting $FS::svc-acct::nossh_hack true. If the
318 shellmachine-userdel configuration file does not exist or is empty, :
320 [ -d $old_dir ] && mv $old_dir $new_dir || (
324 find . -depth -print | cpio -pdm $new_dir;
326 chown -R $uid.$gid $new_dir;
330 is executed on shellmachine via ssh. This behaviour can be surpressed by
331 setting $FS::svc_acct::nossh_hack true.
336 my ( $new, $old ) = ( shift, shift );
339 return "Username in use"
340 if $old->username ne $new->username &&
341 qsearchs( 'svc_acct', { 'username' => $new->username } );
343 return "Can't change uid!" if $old->uid != $new->uid;
345 #change homdir when we change username
346 $new->setfield('dir', '') if $old->username ne $new->username;
348 local $SIG{HUP} = 'IGNORE';
349 local $SIG{INT} = 'IGNORE';
350 local $SIG{QUIT} = 'IGNORE';
351 local $SIG{TERM} = 'IGNORE';
352 local $SIG{TSTP} = 'IGNORE';
353 local $SIG{PIPE} = 'IGNORE';
355 $error = $new->SUPER::replace($old);
356 return $error if $error;
358 my ( $old_dir, $new_dir, $uid, $gid ) = (
359 $old->getfield('dir'),
360 $new->getfield('dir'),
361 $new->getfield('uid'),
362 $new->getfield('gid'),
364 if ( $old_dir && $new_dir && $old_dir ne $new_dir && ! $nossh_hack ) {
365 ssh("root\@$shellmachine", eval qq("$usermod") );
373 Suspends this account by prefixing *SUSPENDED* to the password. If there is an
374 error, returns the error, otherwise returns false.
376 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
382 my %hash = $self->hash;
383 unless ( $hash{_password} =~ /^\*SUSPENDED\* / ) {
384 $hash{_password} = '*SUSPENDED* '.$hash{_password};
385 my $new = new FS::svc_acct ( \%hash );
386 $new->replace($self);
388 ''; #no error (already suspended)
394 Unsuspends this account by removing *SUSPENDED* from the password. If there is
395 an error, returns the error, otherwise returns false.
397 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
403 my %hash = $self->hash;
404 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
405 $hash{_password} = $1;
406 my $new = new FS::svc_acct ( \%hash );
407 $new->replace($self);
409 ''; #no error (already unsuspended)
415 Just returns false (no error) for now.
417 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
421 Checks all fields to make sure this is a valid service. If there is an error,
422 returns the error, otherwise returns false. Called by the insert and replace
425 Sets any fixed values; see L<FS::part_svc>.
432 my($recref) = $self->hashref;
434 my $x = $self->setfixed;
435 return $x unless ref($x);
438 my $error = $self->ut_numbern('svcnum')
439 || $self->ut_number('domsvc')
441 return $error if $error;
443 my $ulen = $usernamemax || $self->dbdef_table->column('username')->length;
444 $recref->{username} =~ /^([a-z0-9_\-\.]{$usernamemin,$ulen})$/
445 or return "Illegal username";
446 $recref->{username} = $1;
447 if ( $username_letterfirst ) {
448 $recref->{username} =~ /^[a-z]/ or return "Illegal username";
449 } elsif ( $username_letter ) {
450 $recref->{username} =~ /[a-z]/ or return "Illegal username";
453 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum: ".$recref->{popnum};
454 $recref->{popnum} = $1;
455 return "Unknown popnum" unless
456 ! $recref->{popnum} ||
457 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
459 unless ( $part_svc->getfield('svc_acct__uid_flag') eq 'F' ) {
461 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
462 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
464 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
465 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
466 #not all systems use gid=uid
467 #you can set a fixed gid in part_svc
469 return "Only root can have uid 0"
470 if $recref->{uid} == 0 && $recref->{username} ne 'root';
472 $error = $self->ut_textn('finger');
473 return $error if $error;
475 $recref->{dir} =~ /^([\/\w\-]*)$/
476 or return "Illegal directory";
477 $recref->{dir} = $1 ||
478 $dir_prefix . '/' . $recref->{username}
479 #$dir_prefix . '/' . substr($recref->{username},0,1). '/' . $recref->{username}
482 unless ( $recref->{username} eq 'sync' ) {
483 if ( grep $_ eq $recref->{shell}, @shells ) {
484 $recref->{shell} = (grep $_ eq $recref->{shell}, @shells)[0];
486 return "Illegal shell \`". $self->shell. "\'; ".
487 $conf->dir. "/shells contains: @shells";
490 $recref->{shell} = '/bin/sync';
493 $recref->{quota} =~ /^(\d*)$/ or return "Illegal quota (unimplemented)";
494 $recref->{quota} = $1;
497 $recref->{gid} ne '' ?
498 return "Can't have gid without uid" : ( $recref->{gid}='' );
499 $recref->{finger} ne '' ?
500 return "Can't have finger-name without uid" : ( $recref->{finger}='' );
501 $recref->{dir} ne '' ?
502 return "Can't have directory without uid" : ( $recref->{dir}='' );
503 $recref->{shell} ne '' ?
504 return "Can't have shell without uid" : ( $recref->{shell}='' );
505 $recref->{quota} ne '' ?
506 return "Can't have quota without uid" : ( $recref->{quota}='' );
509 unless ( $part_svc->getfield('svc_acct__slipip_flag') eq 'F' ) {
510 unless ( $recref->{slipip} eq '0e0' ) {
511 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
512 or return "Illegal slipip". $self->slipip;
513 $recref->{slipip} = $1;
515 $recref->{slipip} = '0e0';
520 #arbitrary RADIUS stuff; allow ut_textn for now
521 foreach ( grep /^radius_/, fields('svc_acct') ) {
525 #generate a password if it is blank
526 $recref->{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) )
527 unless ( $recref->{_password} );
529 #if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,16})$/ ) {
530 if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{$passwordmin,8})$/ ) {
531 $recref->{_password} = $1.$3;
532 #uncomment this to encrypt password immediately upon entry, or run
533 #bin/crypt_pw in cron to give new users a window during which their
534 #password is available to techs, for faxing, etc. (also be aware of
536 #$recref->{password} = $1.
537 # crypt($3,$saltset[int(rand(64))].$saltset[int(rand(64))]
539 } elsif ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([\w\.\/\$]{13,34})$/ ) {
540 $recref->{_password} = $1.$3;
541 } elsif ( $recref->{_password} eq '*' ) {
542 $recref->{_password} = '*';
543 } elsif ( $recref->{_password} eq '!!' ) {
544 $recref->{_password} = '!!';
546 return "Illegal password";
554 Depriciated, use radius_reply instead.
559 carp "FS::svc_acct::radius depriciated, use radius_reply";
565 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
566 reply attributes of this record.
568 Note that this is now the preferred method for reading RADIUS attributes -
569 accessing the columns directly is discouraged, as the column names are
570 expected to change in the future.
578 my($column, $attrib) = ($1, $2);
580 ( $attrib, $self->getfield($column) );
581 } grep { /^radius_/ && $self->getfield($_) } fields( $self->table );
586 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
587 check attributes of this record.
589 Accessing RADIUS attributes directly is not supported and will break in the
598 my($column, $attrib) = ($1, $2);
600 ( $attrib, $self->getfield($column) );
601 } grep { /^rc_/ && $self->getfield($_) } fields( $self->table );
606 Returns the domain associated with this account.
612 if ( $self->domsvc ) {
613 my $svc_domain = qsearchs( 'svc_domain', { 'svcnum' => $self->domsvc } );
616 $mydomain or die "svc_acct.domsvc is null and no legacy domain config file";
622 Returns an email address associated with the account.
628 $self->username. '@'. $self->domain;
635 $Id: svc_acct.pm,v 1.28 2001-08-21 03:03:36 ivan Exp $
639 The bits which ssh should fork before doing so (or maybe queue jobs for a
642 The $recref stuff in sub check should be cleaned up.
644 The suspend, unsuspend and cancel methods update the database, but not the
645 current object. This is probably a bug as it's unexpected and
650 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
651 L<FS::part_svc>, L<FS::cust_pkg>, L<Net::SSH>, L<ssh>, L<FS::svc_acct_pop>,
652 schema.html from the base documentation.