4 use vars qw( @ISA $nossh_hack $conf $dir_prefix @shells $usernamemin
5 $usernamemax $passwordmin
6 $shellmachine $useradd $usermod $userdel
10 use FS::Record qw( qsearchs fields );
17 @ISA = qw( FS::svc_Common );
19 #ask FS::UID to run this stuff for us later
20 $FS::UID::callback{'FS::svc_acct'} = sub {
22 $dir_prefix = $conf->config('home');
23 @shells = $conf->config('shells');
24 $shellmachine = $conf->config('shellmachine');
25 $usernamemin = $conf->config('usernamemin') || 2;
26 $usernamemax = $conf->config('usernamemax');
27 $passwordmin = $conf->config('passwordmin') || 6;
28 if ( $shellmachine ) {
29 if ( $conf->exists('shellmachine-useradd') ) {
30 $useradd = join("\n", $conf->config('shellmachine-useradd') )
31 || 'cp -pr /etc/skel $dir; chown -R $uid.$gid $dir';
33 $useradd = 'useradd -d $dir -m -s $shell -u $uid $username';
35 if ( $conf->exists('shellmachine-userdel') ) {
36 $userdel = join("\n", $conf->config('shellmachine-userdel') )
39 $userdel = 'userdel $username';
41 $usermod = join("\n", $conf->config('shellmachine-usermod') )
42 || '[ -d $old_dir ] && mv $old_dir $new_dir || ( '.
43 'chmod u+t $old_dir; mkdir $new_dir; cd $old_dir; '.
44 'find . -depth -print | cpio -pdm $new_dir; '.
45 'chmod u-t $new_dir; chown -R $uid.$gid $new_dir; '.
51 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
52 @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '!', '.', ',' );
54 #not needed in 5.004 #srand($$|time);
58 FS::svc_acct - Object methods for svc_acct records
64 $record = new FS::svc_acct \%hash;
65 $record = new FS::svc_acct { 'column' => 'value' };
67 $error = $record->insert;
69 $error = $new_record->replace($old_record);
71 $error = $record->delete;
73 $error = $record->check;
75 $error = $record->suspend;
77 $error = $record->unsuspend;
79 $error = $record->cancel;
81 %hash = $record->radius;
85 An FS::svc_acct object represents an account. FS::svc_acct inherits from
86 FS::svc_Common. The following fields are currently supported:
90 =item svcnum - primary key (assigned automatcially for new accounts)
94 =item _password - generated if blank
96 =item popnum - Point of presence (see L<FS::svc_acct_pop>)
104 =item dir - set automatically if blank (and uid is not)
108 =item quota - (unimplementd)
110 =item slipip - IP address
112 =item radius_I<Radius_Attribute> - I<Radius-Attribute>
122 Creates a new account. To add the account to the database, see L<"insert">.
126 sub table { 'svc_acct'; }
130 Adds this account to the database. If there is an error, returns the error,
131 otherwise returns false.
133 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
134 defined. An FS::cust_svc record will be created and inserted.
136 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
137 username, uid, and dir fields are defined, the command(s) specified in
138 the shellmachine-useradd configuration are exectued on shellmachine via ssh.
139 This behaviour can be surpressed by setting $FS::svc_acct::nossh_hack true.
140 If the shellmachine-useradd configuration file does not exist,
142 useradd -d $dir -m -s $shell -u $uid $username
144 is the default. If the shellmachine-useradd configuration file exists but
147 cp -pr /etc/skel $dir; chown -R $uid.$gid $dir
149 is the default instead. Otherwise the contents of the file are treated as
150 a double-quoted perl string, with the following variables available:
151 $username, $uid, $gid, $dir, and $shell.
159 local $SIG{HUP} = 'IGNORE';
160 local $SIG{INT} = 'IGNORE';
161 local $SIG{QUIT} = 'IGNORE';
162 local $SIG{TERM} = 'IGNORE';
163 local $SIG{TSTP} = 'IGNORE';
164 local $SIG{PIPE} = 'IGNORE';
166 $error = $self->check;
167 return $error if $error;
169 return "Username ". $self->username. " in use"
170 if qsearchs( 'svc_acct', { 'username' => $self->username } );
172 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
173 return "Unkonwn svcpart" unless $part_svc;
175 if $part_svc->svc_acct__uid_flag ne 'F'
176 && qsearchs( 'svc_acct', { 'uid' => $self->uid } )
177 && $self->username !~ /^(hyla)?fax$/
180 $error = $self->SUPER::insert;
181 return $error if $error;
183 my( $username, $uid, $gid, $dir, $shell ) = (
190 if ( $username && $uid && $dir && $shellmachine && ! $nossh_hack ) {
191 ssh("root\@$shellmachine", eval qq("$useradd") );
199 Deletes this account from the database. If there is an error, returns the
200 error, otherwise returns false.
202 The corresponding FS::cust_svc record will be deleted as well.
204 If the configuration value (see L<FS::Conf>) shellmachine exists, the
205 command(s) specified in the shellmachine-userdel configuration file are
206 executed on shellmachine via ssh. This behavior can be surpressed by setting
207 $FS::svc_acct::nossh_hack true. If the shellmachine-userdel configuration
212 is the default. If the shellmachine-userdel configuration file exists but
217 is the default instead. Otherwise the contents of the file are treated as a
218 double-quoted perl string, with the following variables available:
227 return "Can't delete an account which has mail aliases pointed to it!"
228 if $self->uid && qsearch( 'svc_acct_sm', { 'domuid' => $self->uid } );
230 local $SIG{HUP} = 'IGNORE';
231 local $SIG{INT} = 'IGNORE';
232 local $SIG{QUIT} = 'IGNORE';
233 local $SIG{TERM} = 'IGNORE';
234 local $SIG{TSTP} = 'IGNORE';
235 local $SIG{PIPE} = 'IGNORE';
237 $error = $self->SUPER::delete;
238 return $error if $error;
240 my( $username, $dir ) = (
244 if ( $username && $shellmachine && ! $nossh_hack ) {
245 ssh("root\@$shellmachine", eval qq("$userdel") );
251 =item replace OLD_RECORD
253 Replaces OLD_RECORD with this one in the database. If there is an error,
254 returns the error, otherwise returns false.
256 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
257 dir field has changed, the command(s) specified in the shellmachine-usermod
258 configuraiton file are executed on shellmachine via ssh. This behavior can
259 be surpressed by setting $FS::svc-acct::nossh_hack true. If the
260 shellmachine-userdel configuration file does not exist or is empty, :
262 [ -d $old_dir ] && mv $old_dir $new_dir || (
266 find . -depth -print | cpio -pdm $new_dir;
268 chown -R $uid.$gid $new_dir;
272 is executed on shellmachine via ssh. This behaviour can be surpressed by
273 setting $FS::svc_acct::nossh_hack true.
278 my ( $new, $old ) = ( shift, shift );
281 return "Username in use"
282 if $old->username ne $new->username &&
283 qsearchs( 'svc_acct', { 'username' => $new->username } );
285 return "Can't change uid!" if $old->uid != $new->uid;
287 #change homdir when we change username
288 $new->setfield('dir', '') if $old->username ne $new->username;
290 local $SIG{HUP} = 'IGNORE';
291 local $SIG{INT} = 'IGNORE';
292 local $SIG{QUIT} = 'IGNORE';
293 local $SIG{TERM} = 'IGNORE';
294 local $SIG{TSTP} = 'IGNORE';
295 local $SIG{PIPE} = 'IGNORE';
297 $error = $new->SUPER::replace($old);
298 return $error if $error;
300 my ( $old_dir, $new_dir, $uid, $gid ) = (
301 $old->getfield('dir'),
302 $new->getfield('dir'),
303 $new->getfield('uid'),
304 $new->getfield('gid'),
306 if ( $old_dir && $new_dir && $old_dir ne $new_dir && ! $nossh_hack ) {
307 ssh("root\@$shellmachine", eval qq("$usermod") );
315 Suspends this account by prefixing *SUSPENDED* to the password. If there is an
316 error, returns the error, otherwise returns false.
318 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
324 my %hash = $self->hash;
325 unless ( $hash{_password} =~ /^\*SUSPENDED\* / ) {
326 $hash{_password} = '*SUSPENDED* '.$hash{_password};
327 my $new = new FS::svc_acct ( \%hash );
328 $new->replace($self);
330 ''; #no error (already suspended)
336 Unsuspends this account by removing *SUSPENDED* from the password. If there is
337 an error, returns the error, otherwise returns false.
339 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
345 my %hash = $self->hash;
346 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
347 $hash{_password} = $1;
348 my $new = new FS::svc_acct ( \%hash );
349 $new->replace($self);
351 ''; #no error (already unsuspended)
357 Just returns false (no error) for now.
359 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
363 Checks all fields to make sure this is a valid service. If there is an error,
364 returns the error, otherwise returns false. Called by the insert and replace
367 Sets any fixed values; see L<FS::part_svc>.
374 my($recref) = $self->hashref;
376 my $x = $self->setfixed;
377 return $x unless ref($x);
380 my $ulen = $usernamemax || $self->dbdef_table->column('username')->length;
381 $recref->{username} =~ /^([a-z0-9_\-\.]{$usernamemin,$ulen})$/
382 or return "Illegal username";
383 $recref->{username} = $1;
384 $recref->{username} =~ /[a-z]/ or return "Illegal username";
386 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum: ".$recref->{popnum};
387 $recref->{popnum} = $1;
388 return "Unkonwn popnum" unless
389 ! $recref->{popnum} ||
390 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
392 unless ( $part_svc->getfield('svc_acct__uid_flag') eq 'F' ) {
394 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
395 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
397 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
398 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
399 #not all systems use gid=uid
400 #you can set a fixed gid in part_svc
402 return "Only root can have uid 0"
403 if $recref->{uid} == 0 && $recref->{username} ne 'root';
406 return $error if $error=$self->ut_textn('finger');
408 $recref->{dir} =~ /^([\/\w\-]*)$/
409 or return "Illegal directory";
410 $recref->{dir} = $1 ||
411 $dir_prefix . '/' . $recref->{username}
412 #$dir_prefix . '/' . substr($recref->{username},0,1). '/' . $recref->{username}
415 unless ( $recref->{username} eq 'sync' ) {
416 if ( grep $_ eq $recref->{shell}, @shells ) {
417 $recref->{shell} = (grep $_ eq $recref->{shell}, @shells)[0];
419 return "Illegal shell \`". $self->shell. "\'; ".
420 $conf->dir. "/shells contains: @shells";
423 $recref->{shell} = '/bin/sync';
426 $recref->{quota} =~ /^(\d*)$/ or return "Illegal quota (unimplemented)";
427 $recref->{quota} = $1;
430 $recref->{gid} ne '' ?
431 return "Can't have gid without uid" : ( $recref->{gid}='' );
432 $recref->{finger} ne '' ?
433 return "Can't have finger-name without uid" : ( $recref->{finger}='' );
434 $recref->{dir} ne '' ?
435 return "Can't have directory without uid" : ( $recref->{dir}='' );
436 $recref->{shell} ne '' ?
437 return "Can't have shell without uid" : ( $recref->{shell}='' );
438 $recref->{quota} ne '' ?
439 return "Can't have quota without uid" : ( $recref->{quota}='' );
442 unless ( $part_svc->getfield('svc_acct__slipip_flag') eq 'F' ) {
443 unless ( $recref->{slipip} eq '0e0' ) {
444 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
445 or return "Illegal slipip". $self->slipip;
446 $recref->{slipip} = $1;
448 $recref->{slipip} = '0e0';
453 #arbitrary RADIUS stuff; allow ut_textn for now
454 foreach ( grep /^radius_/, fields('svc_acct') ) {
458 #generate a password if it is blank
459 $recref->{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) )
460 unless ( $recref->{_password} );
462 #if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,16})$/ ) {
463 if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{$passwordmin,8})$/ ) {
464 $recref->{_password} = $1.$3;
465 #uncomment this to encrypt password immediately upon entry, or run
466 #bin/crypt_pw in cron to give new users a window during which their
467 #password is available to techs, for faxing, etc. (also be aware of
469 #$recref->{password} = $1.
470 # crypt($3,$saltset[int(rand(64))].$saltset[int(rand(64))]
472 } elsif ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([\w\.\/\$]{13,34})$/ ) {
473 $recref->{_password} = $1.$3;
474 } elsif ( $recref->{_password} eq '*' ) {
475 $recref->{_password} = '*';
477 return "Illegal password";
485 Depriciated, use radius_reply instead.
490 carp "FS::svc_acct::radius depriciated, use radius_reply";
496 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
497 reply attributes of this record.
499 Note that this is now the preferred method for reading RADIUS attributes -
500 accessing the columns directly is discouraged, as the column names are
501 expected to change in the future.
509 my($column, $attrib) = ($1, $2);
511 ( $attrib, $self->getfield($column) );
512 } grep { /^radius_/ && $self->getfield($_) } fields( $self->table );
517 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
518 check attributes of this record.
520 Accessing RADIUS attributes directly is not supported and will break in the
529 my($column, $attrib) = ($1, $2);
531 ( $attrib, $self->getfield($column) );
532 } grep { /^rc_/ && $self->getfield($_) } fields( $self->table );
539 $Id: svc_acct.pm,v 1.13 2000-07-17 13:51:07 ivan Exp $
543 The bits which ssh should fork before doing so (or maybe queue jobs for a
546 The $recref stuff in sub check should be cleaned up.
548 The suspend, unsuspend and cancel methods update the database, but not the
549 current object. This is probably a bug as it's unexpected and
554 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
555 L<FS::part_svc>, L<FS::cust_pkg>, L<FS::SSH>, L<ssh>, L<FS::svc_acct_pop>,
556 schema.html from the base documentation.