4 use vars qw(@ISA $nossh_hack $conf $dir_prefix @shells
5 $shellmachine @saltset @pw_set);
7 use FS::Record qw( qsearchs fields );
11 @ISA = qw( FS::Record );
13 #ask FS::UID to run this stuff for us later
14 $FS::UID::callback{'FS::svc_acct'} = sub {
16 $dir_prefix = $conf->config('home');
17 @shells = $conf->config('shells');
18 $shellmachine = $conf->config('shellmachine');
21 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
22 @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '!', '.', ',' );
24 #not needed in 5.004 #srand($$|time);
28 FS::svc_acct - Object methods for svc_acct records
34 $record = new FS::svc_acct \%hash;
35 $record = new FS::svc_acct { 'column' => 'value' };
37 $error = $record->insert;
39 $error = $new_record->replace($old_record);
41 $error = $record->delete;
43 $error = $record->check;
45 $error = $record->suspend;
47 $error = $record->unsuspend;
49 $error = $record->cancel;
53 An FS::svc_acct object represents an account. FS::svc_acct inherits from
54 FS::Record. The following fields are currently supported:
58 =item svcnum - primary key (assigned automatcially for new accounts)
62 =item _password - generated if blank
64 =item popnum - Point of presence (see L<FS::svc_acct_pop>)
72 =item dir - set automatically if blank (and uid is not)
76 =item quota - (unimplementd)
78 =item slipip - IP address
80 =item radius_I<Radius_Attribute> - I<Radius-Attribute>
90 Creates a new account. To add the account to the database, see L<"insert">.
94 sub table { 'svc_acct'; }
98 Adds this account to the database. If there is an error, returns the error,
99 otherwise returns false.
101 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
102 defined. An FS::cust_svc record will be created and inserted.
104 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
105 username, uid, and dir fields are defined, the command
107 useradd -d $dir -m -s $shell -u $uid $username
109 is executed on shellmachine via ssh. This behaviour can be surpressed by
110 setting $FS::svc_acct::nossh_hack true.
118 local $SIG{HUP} = 'IGNORE';
119 local $SIG{INT} = 'IGNORE';
120 local $SIG{QUIT} = 'IGNORE';
121 local $SIG{TERM} = 'IGNORE';
122 local $SIG{TSTP} = 'IGNORE';
124 $error = $self->check;
125 return $error if $error;
127 return "Username ". $self->username. " in use"
128 if qsearchs( 'svc_acct', { 'username' => $self->username } );
130 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
131 return "Unkonwn svcpart" unless $part_svc;
133 if $part_svc->svc_acct__uid_flag ne 'F'
134 && qsearchs( 'svc_acct', { 'uid' => $self->uid } )
135 && $self->username !~ /^(hyla)?fax$/
138 my $svcnum = $self->svcnum;
141 $cust_svc = new FS::cust_svc ( {
143 'pkgnum' => $self->pkgnum,
144 'svcpart' => $self->svcpart,
146 my $error = $cust_svc->insert;
147 return $error if $error;
148 $svcnum = $self->svcnum($cust_svc->svcnum);
151 $error = $self->SUPER::insert;
153 $cust_svc->delete if $cust_svc;
157 my ( $username, $uid, $dir, $shell ) = (
169 ssh("root\@$shellmachine",
170 "useradd -d $dir -m -s $shell -u $uid $username"
173 #ssh("root\@$shellmachine","/bin/mkdir $dir; /bin/chmod 711 $dir; ".
174 # "/bin/cp -p /etc/skel/.* $dir 2>/dev/null; ".
175 # "/bin/cp -pR /etc/skel/Maildir $dir 2>/dev/null; ".
176 # "/bin/chown -R $uid $dir") unless $nossh_hack;
184 Deletes this account from the database. If there is an error, returns the
185 error, otherwise returns false.
187 The corresponding FS::cust_svc record will be deleted as well.
189 If the configuration value (see L<FS::Conf>) shellmachine exists, the command:
193 is executed on shellmachine via ssh. This behaviour can be surpressed by
194 setting $FS::svc_acct::nossh_hack true.
202 local $SIG{HUP} = 'IGNORE';
203 local $SIG{INT} = 'IGNORE';
204 local $SIG{QUIT} = 'IGNORE';
205 local $SIG{TERM} = 'IGNORE';
206 local $SIG{TSTP} = 'IGNORE';
208 my $svcnum = $self->getfield('svcnum');
210 $error = $self->SUPER::delete;
211 return $error if $error;
213 my $cust_svc = qsearchs( 'cust_svc' , { 'svcnum' => $svcnum } );
214 $error = $cust_svc->delete;
215 return $error if $error;
217 my $username = $self->username;
218 if ( $username && $shellmachine && ! $nossh_hack ) {
219 ssh("root\@$shellmachine","userdel $username");
225 =item replace OLD_RECORD
227 Replaces OLD_RECORD with this one in the database. If there is an error,
228 returns the error, otherwise returns false.
230 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
231 dir field has changed, the command:
238 find . -depth -print | cpio -pdm $new_dir;
240 chown -R $uid.$gid $new_dir;
244 is executed on shellmachine via ssh. This behaviour can be surpressed by
245 setting $FS::svc_acct::nossh_hack true.
250 my ( $new, $old ) = @_;
253 return "Username in use"
254 if $old->username ne $new->username &&
255 qsearchs( 'svc_acct', { 'username' => $new->username } );
257 return "Can't change uid!" if $old->uid ne $new->uid;
259 #change homdir when we change username
260 $new->setfield('dir', '') if $old->username ne $new->username;
262 local $SIG{HUP} = 'IGNORE';
263 local $SIG{INT} = 'IGNORE';
264 local $SIG{QUIT} = 'IGNORE';
265 local $SIG{TERM} = 'IGNORE';
266 local $SIG{TSTP} = 'IGNORE';
268 $error = $new->SUPER::replace($old);
269 return $error if $error;
271 my ( $old_dir, $new_dir ) = ( $old->getfield('dir'), $new->getfield('dir') );
272 my ( $uid, $gid) = ( $new->getfield('uid'), $new->getfield('gid') );
275 && $old_dir ne $new_dir
278 ssh("root\@$shellmachine","[ -d $old_dir ] && ".
279 "( chmod u+t $old_dir; ". #turn off qmail delivery
280 "umask 022; mkdir $new_dir; cd $old_dir; ".
281 "find . -depth -print | cpio -pdm $new_dir; ".
282 "chmod u-t $new_dir; chown -R $uid.$gid $new_dir; ".
293 Suspends this account by prefixing *SUSPENDED* to the password. If there is an
294 error, returns the error, otherwise returns false.
296 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
302 my %hash = $self->hash;
303 unless ( $hash{_password} =~ /^\*SUSPENDED\* / ) {
304 $hash{_password} = '*SUSPENDED* '.$hash{_password};
305 my $new = new FS::svc_acct ( \%hash );
306 $new->replace($self);
308 ''; #no error (already suspended)
314 Unsuspends this account by removing *SUSPENDED* from the password. If there is
315 an error, returns the error, otherwise returns false.
317 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
323 my %hash = $self->hash;
324 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
325 $hash{_password} = $1;
326 my $new = new FS::svc_acct ( \%hash );
327 $new->replace($self);
329 ''; #no error (already unsuspended)
335 Just returns false (no error) for now.
337 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
342 ''; #stub (no error) - taken care of in delete
347 Checks all fields to make sure this is a valid service. If there is an error,
348 returns the error, otherwise returns false. Called by the insert and replace
351 Sets any fixed values; see L<FS::part_svc>.
358 my($recref) = $self->hashref;
360 $recref->{svcnum} =~ /^(\d*)$/ or return "Illegal svcnum";
361 $recref->{svcnum} = $1;
365 my($svcnum)=$self->getfield('svcnum');
367 my($cust_svc)=qsearchs('cust_svc',{'svcnum'=>$svcnum});
368 return "Unknown svcnum" unless $cust_svc;
369 $svcpart=$cust_svc->svcpart;
371 $svcpart=$self->getfield('svcpart');
373 my($part_svc)=qsearchs('part_svc',{'svcpart'=>$svcpart});
374 return "Unkonwn svcpart" unless $part_svc;
376 #set fixed fields from part_svc
378 foreach $field ( fields('svc_acct') ) {
379 if ( $part_svc->getfield('svc_acct__'. $field. '_flag') eq 'F' ) {
380 $self->setfield($field,$part_svc->getfield('svc_acct__'. $field) );
384 my($ulen)=$self->dbdef_table->column('username')->length;
385 $recref->{username} =~ /^([a-z0-9_\-]{2,$ulen})$/
386 or return "Illegal username";
387 $recref->{username} = $1;
388 $recref->{username} =~ /[a-z]/ or return "Illegal username";
390 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum";
391 $recref->{popnum} = $1;
392 return "Unkonwn popnum" unless
393 ! $recref->{popnum} ||
394 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
396 unless ( $part_svc->getfield('svc_acct__uid_flag') eq 'F' ) {
398 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
399 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
401 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
402 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
403 #not all systems use gid=uid
404 #you can set a fixed gid in part_svc
406 return "Only root can have uid 0"
407 if $recref->{uid} == 0 && $recref->{username} ne 'root';
410 return $error if $error=$self->ut_textn('finger');
412 $recref->{dir} =~ /^([\/\w\-]*)$/
413 or return "Illegal directory";
414 $recref->{dir} = $1 ||
415 $dir_prefix . '/' . $recref->{username}
416 #$dir_prefix . '/' . substr($recref->{username},0,1). '/' . $recref->{username}
419 unless ( $recref->{username} eq 'sync' ) {
421 if ( $shell = (grep $_ eq $recref->{shell}, @shells)[0] ) {
422 $recref->{shell} = $shell;
424 return "Illegal shell ". $self->shell;
427 $recref->{shell} = '/bin/sync';
430 $recref->{quota} =~ /^(\d*)$/ or return "Illegal quota (unimplemented)";
431 $recref->{quota} = $1;
434 $recref->{gid} ne '' ?
435 return "Can't have gid without uid" : ( $recref->{gid}='' );
436 $recref->{finger} ne '' ?
437 return "Can't have finger-name without uid" : ( $recref->{finger}='' );
438 $recref->{dir} ne '' ?
439 return "Can't have directory without uid" : ( $recref->{dir}='' );
440 $recref->{shell} ne '' ?
441 return "Can't have shell without uid" : ( $recref->{shell}='' );
442 $recref->{quota} ne '' ?
443 return "Can't have quota without uid" : ( $recref->{quota}='' );
446 unless ( $part_svc->getfield('svc_acct__slipip_flag') eq 'F' ) {
447 unless ( $recref->{slipip} eq '0e0' ) {
448 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
449 or return "Illegal slipip". $self->slipip;
450 $recref->{slipip} = $1;
452 $recref->{slipip} = '0e0';
457 #arbitrary RADIUS stuff; allow ut_textn for now
458 foreach ( grep /^radius_/, fields('svc_acct') ) {
462 #generate a password if it is blank
463 $recref->{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) )
464 unless ( $recref->{_password} );
466 #if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,16})$/ ) {
467 if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,8})$/ ) {
468 $recref->{_password} = $1.$3;
469 #uncomment this to encrypt password immediately upon entry, or run
470 #bin/crypt_pw in cron to give new users a window during which their
471 #password is available to techs, for faxing, etc. (also be aware of
473 #$recref->{password} = $1.
474 # crypt($3,$saltset[int(rand(64))].$saltset[int(rand(64))]
476 } elsif ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([\w\.\/]{13,24})$/ ) {
477 $recref->{_password} = $1.$3;
478 } elsif ( $recref->{_password} eq '*' ) {
479 $recref->{_password} = '*';
481 return "Illegal password";
491 $Id: svc_acct.pm,v 1.3 1998-12-29 11:59:52 ivan Exp $
495 The remote commands should be configurable.
497 The new method should set defaults from part_svc (like the check method
500 The bits which ssh should fork before doing so.
502 The $recref stuff in sub check should be cleaned up.
506 L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>,
507 L<FS::SSH>, L<ssh>, L<FS::svc_acct_pop>, schema.html from the base
512 ivan@voicenet.com 97-jul-16 - 21
514 rewrite (among other things, now know about part_svc) ivan@sisd.com 98-mar-8
516 Changed 'password' to '_password' because Pg6.3 reserves the password word
517 bmccane@maxbaud.net 98-apr-3
519 username length and shell no longer hardcoded ivan@sisd.com 98-jun-28
521 eww but needed: ignore uid duplicates for 'fax' and 'hylafax'
522 ivan@sisd.com 98-jun-29
524 $nossh_hack ivan@sisd.com 98-jul-13
526 protections against UID/GID of 0 for incorrectly-setup RDBMSs (also
527 in bin/svc_acct.export) ivan@sisd.com 98-jul-13
529 arbitrary radius attributes ivan@sisd.com 98-aug-13
531 /var/spool/freeside/conf/shellmachine ivan@sisd.com 98-aug-13
533 pod and FS::conf ivan@sisd.com 98-sep-22
535 $Log: svc_acct.pm,v $
536 Revision 1.3 1998-12-29 11:59:52 ivan
537 mostly properly OO, some work still to be done with svc_ stuff
539 Revision 1.2 1998/11/13 09:56:55 ivan
540 change configuration file layout to support multiple distinct databases (with
541 own set of config files, export, etc.)