4 use vars qw( @ISA $nossh_hack $conf $dir_prefix @shells $usernamemin
5 $usernamemax $passwordmin
6 $shellmachine @saltset @pw_set);
9 use FS::Record qw( qsearchs fields );
15 @ISA = qw( FS::svc_Common );
17 #ask FS::UID to run this stuff for us later
18 $FS::UID::callback{'FS::svc_acct'} = sub {
20 $dir_prefix = $conf->config('home');
21 @shells = $conf->config('shells');
22 $shellmachine = $conf->config('shellmachine');
23 $usernamemin = $conf->config('usernamemin') || 2;
24 $usernamemax = $conf->config('usernamemax');
25 $passwordmin = $conf->config('passwordmin') || 6;
28 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
29 @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '!', '.', ',' );
31 #not needed in 5.004 #srand($$|time);
35 FS::svc_acct - Object methods for svc_acct records
41 $record = new FS::svc_acct \%hash;
42 $record = new FS::svc_acct { 'column' => 'value' };
44 $error = $record->insert;
46 $error = $new_record->replace($old_record);
48 $error = $record->delete;
50 $error = $record->check;
52 $error = $record->suspend;
54 $error = $record->unsuspend;
56 $error = $record->cancel;
58 %hash = $record->radius;
62 An FS::svc_acct object represents an account. FS::svc_acct inherits from
63 FS::svc_Common. The following fields are currently supported:
67 =item svcnum - primary key (assigned automatcially for new accounts)
71 =item _password - generated if blank
73 =item popnum - Point of presence (see L<FS::svc_acct_pop>)
81 =item dir - set automatically if blank (and uid is not)
85 =item quota - (unimplementd)
87 =item slipip - IP address
89 =item radius_I<Radius_Attribute> - I<Radius-Attribute>
99 Creates a new account. To add the account to the database, see L<"insert">.
103 sub table { 'svc_acct'; }
107 Adds this account to the database. If there is an error, returns the error,
108 otherwise returns false.
110 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
111 defined. An FS::cust_svc record will be created and inserted.
113 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
114 username, uid, and dir fields are defined, the command
116 useradd -d $dir -m -s $shell -u $uid $username
118 is executed on shellmachine via ssh. This behaviour can be surpressed by
119 setting $FS::svc_acct::nossh_hack true.
127 local $SIG{HUP} = 'IGNORE';
128 local $SIG{INT} = 'IGNORE';
129 local $SIG{QUIT} = 'IGNORE';
130 local $SIG{TERM} = 'IGNORE';
131 local $SIG{TSTP} = 'IGNORE';
132 local $SIG{PIPE} = 'IGNORE';
134 $error = $self->check;
135 return $error if $error;
137 return "Username ". $self->username. " in use"
138 if qsearchs( 'svc_acct', { 'username' => $self->username } );
140 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
141 return "Unkonwn svcpart" unless $part_svc;
143 if $part_svc->svc_acct__uid_flag ne 'F'
144 && qsearchs( 'svc_acct', { 'uid' => $self->uid } )
145 && $self->username !~ /^(hyla)?fax$/
148 $error = $self->SUPER::insert;
149 return $error if $error;
151 my ( $username, $uid, $dir, $shell ) = (
163 ssh("root\@$shellmachine",
164 "useradd -d $dir -m -s $shell -u $uid $username"
167 #ssh("root\@$shellmachine","/bin/mkdir $dir; /bin/chmod 711 $dir; ".
168 # "/bin/cp -p /etc/skel/.* $dir 2>/dev/null; ".
169 # "/bin/cp -pR /etc/skel/Maildir $dir 2>/dev/null; ".
170 # "/bin/chown -R $uid $dir") unless $nossh_hack;
178 Deletes this account from the database. If there is an error, returns the
179 error, otherwise returns false.
181 The corresponding FS::cust_svc record will be deleted as well.
183 If the configuration value (see L<FS::Conf>) shellmachine exists, the command:
187 is executed on shellmachine via ssh. This behaviour can be surpressed by
188 setting $FS::svc_acct::nossh_hack true.
196 local $SIG{HUP} = 'IGNORE';
197 local $SIG{INT} = 'IGNORE';
198 local $SIG{QUIT} = 'IGNORE';
199 local $SIG{TERM} = 'IGNORE';
200 local $SIG{TSTP} = 'IGNORE';
201 local $SIG{PIPE} = 'IGNORE';
203 $error = $self->SUPER::delete;
204 return $error if $error;
206 my $username = $self->username;
207 if ( $username && $shellmachine && ! $nossh_hack ) {
208 ssh("root\@$shellmachine","userdel $username");
214 =item replace OLD_RECORD
216 Replaces OLD_RECORD with this one in the database. If there is an error,
217 returns the error, otherwise returns false.
219 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
220 dir field has changed, the command:
227 find . -depth -print | cpio -pdm $new_dir;
229 chown -R $uid.$gid $new_dir;
233 is executed on shellmachine via ssh. This behaviour can be surpressed by
234 setting $FS::svc_acct::nossh_hack true.
239 my ( $new, $old ) = ( shift, shift );
242 return "Username in use"
243 if $old->username ne $new->username &&
244 qsearchs( 'svc_acct', { 'username' => $new->username } );
246 return "Can't change uid!" if $old->uid != $new->uid;
248 #change homdir when we change username
249 $new->setfield('dir', '') if $old->username ne $new->username;
251 local $SIG{HUP} = 'IGNORE';
252 local $SIG{INT} = 'IGNORE';
253 local $SIG{QUIT} = 'IGNORE';
254 local $SIG{TERM} = 'IGNORE';
255 local $SIG{TSTP} = 'IGNORE';
256 local $SIG{PIPE} = 'IGNORE';
258 $error = $new->SUPER::replace($old);
259 return $error if $error;
261 my ( $old_dir, $new_dir ) = ( $old->getfield('dir'), $new->getfield('dir') );
262 my ( $uid, $gid) = ( $new->getfield('uid'), $new->getfield('gid') );
265 && $old_dir ne $new_dir
268 ssh("root\@$shellmachine","[ -d $old_dir ] && ".
269 "( chmod u+t $old_dir; ". #turn off qmail delivery
270 "umask 022; mkdir $new_dir; cd $old_dir; ".
271 "find . -depth -print | cpio -pdm $new_dir; ".
272 "chmod u-t $new_dir; chown -R $uid.$gid $new_dir; ".
283 Suspends this account by prefixing *SUSPENDED* to the password. If there is an
284 error, returns the error, otherwise returns false.
286 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
292 my %hash = $self->hash;
293 unless ( $hash{_password} =~ /^\*SUSPENDED\* / ) {
294 $hash{_password} = '*SUSPENDED* '.$hash{_password};
295 my $new = new FS::svc_acct ( \%hash );
296 $new->replace($self);
298 ''; #no error (already suspended)
304 Unsuspends this account by removing *SUSPENDED* from the password. If there is
305 an error, returns the error, otherwise returns false.
307 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
313 my %hash = $self->hash;
314 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
315 $hash{_password} = $1;
316 my $new = new FS::svc_acct ( \%hash );
317 $new->replace($self);
319 ''; #no error (already unsuspended)
325 Just returns false (no error) for now.
327 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
331 Checks all fields to make sure this is a valid service. If there is an error,
332 returns the error, otherwise returns false. Called by the insert and replace
335 Sets any fixed values; see L<FS::part_svc>.
342 my($recref) = $self->hashref;
344 my $x = $self->setfixed;
345 return $x unless ref($x);
348 my $ulen = $usernamemax || $self->dbdef_table->column('username')->length;
349 $recref->{username} =~ /^([a-z0-9_\-\.]{$usernamemin,$ulen})$/
350 or return "Illegal username";
351 $recref->{username} = $1;
352 $recref->{username} =~ /[a-z]/ or return "Illegal username";
354 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum: ".$recref->{popnum};
355 $recref->{popnum} = $1;
356 return "Unkonwn popnum" unless
357 ! $recref->{popnum} ||
358 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
360 unless ( $part_svc->getfield('svc_acct__uid_flag') eq 'F' ) {
362 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
363 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
365 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
366 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
367 #not all systems use gid=uid
368 #you can set a fixed gid in part_svc
370 return "Only root can have uid 0"
371 if $recref->{uid} == 0 && $recref->{username} ne 'root';
374 return $error if $error=$self->ut_textn('finger');
376 $recref->{dir} =~ /^([\/\w\-]*)$/
377 or return "Illegal directory";
378 $recref->{dir} = $1 ||
379 $dir_prefix . '/' . $recref->{username}
380 #$dir_prefix . '/' . substr($recref->{username},0,1). '/' . $recref->{username}
383 unless ( $recref->{username} eq 'sync' ) {
384 if ( grep $_ eq $recref->{shell}, @shells ) {
385 $recref->{shell} = (grep $_ eq $recref->{shell}, @shells)[0];
387 return "Illegal shell \`". $self->shell. "\'; ".
388 $conf->dir. "/shells contains: @shells";
391 $recref->{shell} = '/bin/sync';
394 $recref->{quota} =~ /^(\d*)$/ or return "Illegal quota (unimplemented)";
395 $recref->{quota} = $1;
398 $recref->{gid} ne '' ?
399 return "Can't have gid without uid" : ( $recref->{gid}='' );
400 $recref->{finger} ne '' ?
401 return "Can't have finger-name without uid" : ( $recref->{finger}='' );
402 $recref->{dir} ne '' ?
403 return "Can't have directory without uid" : ( $recref->{dir}='' );
404 $recref->{shell} ne '' ?
405 return "Can't have shell without uid" : ( $recref->{shell}='' );
406 $recref->{quota} ne '' ?
407 return "Can't have quota without uid" : ( $recref->{quota}='' );
410 unless ( $part_svc->getfield('svc_acct__slipip_flag') eq 'F' ) {
411 unless ( $recref->{slipip} eq '0e0' ) {
412 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
413 or return "Illegal slipip". $self->slipip;
414 $recref->{slipip} = $1;
416 $recref->{slipip} = '0e0';
421 #arbitrary RADIUS stuff; allow ut_textn for now
422 foreach ( grep /^radius_/, fields('svc_acct') ) {
426 #generate a password if it is blank
427 $recref->{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) )
428 unless ( $recref->{_password} );
430 #if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,16})$/ ) {
431 if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{$passwordmin,8})$/ ) {
432 $recref->{_password} = $1.$3;
433 #uncomment this to encrypt password immediately upon entry, or run
434 #bin/crypt_pw in cron to give new users a window during which their
435 #password is available to techs, for faxing, etc. (also be aware of
437 #$recref->{password} = $1.
438 # crypt($3,$saltset[int(rand(64))].$saltset[int(rand(64))]
440 } elsif ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([\w\.\/\$]{13,34})$/ ) {
441 $recref->{_password} = $1.$3;
442 } elsif ( $recref->{_password} eq '*' ) {
443 $recref->{_password} = '*';
445 return "Illegal password";
453 Depriciated, use radius_reply instead.
458 carp "FS::svc_acct::radius depriciated, use radius_reply";
464 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
465 reply attributes of this record.
467 Note that this is now the preferred method for reading RADIUS attributes -
468 accessing the columns directly is discouraged, as the column names are
469 expected to change in the future.
477 my($column, $attrib) = ($1, $2);
479 ( $attrib, $self->getfield($column) );
480 } grep { /^radius_/ && $self->getfield($_) } fields( $self->table );
485 Returns key/value pairs, suitable for assigning to a hash, for any RADIUS
486 check attributes of this record.
488 Accessing RADIUS attributes directly is not supported and will break in the
497 my($column, $attrib) = ($1, $2);
499 ( $attrib, $self->getfield($column) );
500 } grep { /^rc_/ && $self->getfield($_) } fields( $self->table );
507 $Id: svc_acct.pm,v 1.10 2000-07-06 13:56:42 ivan Exp $
511 The remote commands should be configurable.
513 The bits which ssh should fork before doing so.
515 The $recref stuff in sub check should be cleaned up.
517 The suspend, unsuspend and cancel methods update the database, but not the
518 current object. This is probably a bug as it's unexpected and
523 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
524 L<FS::part_svc>, L<FS::cust_pkg>, L<FS::SSH>, L<ssh>, L<FS::svc_acct_pop>,
525 schema.html from the base documentation.