4 use vars qw(@ISA @EXPORT_OK $nossh_hack $conf $dir_prefix @shells
5 $shellmachine @saltset @pw_set);
8 use FS::Record qw(fields qsearchs);
12 @ISA = qw(FS::Record Exporter);
13 @EXPORT_OK = qw(fields);
15 #ask FS::UID to run this stuff for us later
16 $FS::UID::callback{'FS::svc_acct'} = sub {
18 $dir_prefix = $conf->config('home');
19 @shells = $conf->config('shells');
20 $shellmachine = $conf->config('shellmachine');
23 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
24 @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '!', '.', ',' );
26 #not needed in 5.004 #srand($$|time);
30 FS::svc_acct - Object methods for svc_acct records
36 $record = create FS::svc_acct \%hash;
37 $record = create FS::svc_acct { 'column' => 'value' };
39 $error = $record->insert;
41 $error = $new_record->replace($old_record);
43 $error = $record->delete;
45 $error = $record->check;
47 $error = $record->suspend;
49 $error = $record->unsuspend;
51 $error = $record->cancel;
55 An FS::svc_acct object represents an account. FS::svc_acct inherits from
56 FS::Record. The following fields are currently supported:
60 =item svcnum - primary key (assigned automatcially for new accounts)
64 =item _password - generated if blank
66 =item popnum - Point of presence (see L<FS::svc_acct_pop>)
74 =item dir - set automatically if blank (and uid is not)
78 =item quota - (unimplementd)
80 =item slipip - IP address
82 =item radius_I<Radius_Attribute> - I<Radius-Attribute>
92 Creates a new account. To add the account to the database, see L<"insert">.
97 my($proto,$hashref)=@_;
99 #now in FS::Record::new
101 #foreach $field (fields('svc_acct')) {
102 # $hashref->{$field}='' unless defined $hashref->{$field};
105 $proto->new('svc_acct',$hashref);
111 Adds this account to the database. If there is an error, returns the error,
112 otherwise returns false.
114 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
115 defined. An FS::cust_svc record will be created and inserted.
117 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
118 username, uid, and dir fields are defined, the command
120 useradd -d $dir -m -s $shell -u $uid $username
122 is executed on shellmachine via ssh. This behaviour can be surpressed by
123 setting $FS::svc_acct::nossh_hack true.
131 local $SIG{HUP} = 'IGNORE';
132 local $SIG{INT} = 'IGNORE';
133 local $SIG{QUIT} = 'IGNORE';
134 local $SIG{TERM} = 'IGNORE';
135 local $SIG{TSTP} = 'IGNORE';
138 return $error if $error;
140 return "Username ". $self->username. " in use"
141 if qsearchs('svc_acct',{'username'=> $self->username } );
143 my($part_svc) = qsearchs('part_svc',{ 'svcpart' => $self->svcpart });
144 return "Unkonwn svcpart" unless $part_svc;
146 if $part_svc->svc_acct__uid_flag ne 'F'
147 && qsearchs('svc_acct',{'uid'=> $self->uid } )
148 && $self->username !~ /^(hyla)?fax$/
151 my($svcnum)=$self->svcnum;
154 $cust_svc=create FS::cust_svc ( {
156 'pkgnum' => $self->pkgnum,
157 'svcpart' => $self->svcpart,
159 my($error) = $cust_svc->insert;
160 return $error if $error;
161 $svcnum = $self->svcnum($cust_svc->svcnum);
166 #$cust_svc->del if $cust_svc;
167 $cust_svc->delete if $cust_svc;
171 my($username,$uid,$dir,$shell) = (
183 ssh("root\@$shellmachine",
184 "useradd -d $dir -m -s $shell -u $uid $username"
187 #ssh("root\@$shellmachine","/bin/mkdir $dir; /bin/chmod 711 $dir; ".
188 # "/bin/cp -p /etc/skel/.* $dir 2>/dev/null; ".
189 # "/bin/cp -pR /etc/skel/Maildir $dir 2>/dev/null; ".
190 # "/bin/chown -R $uid $dir") unless $nossh_hack;
198 Deletes this account from the database. If there is an error, returns the
199 error, otherwise returns false.
201 The corresponding FS::cust_svc record will be deleted as well.
203 If the configuration value (see L<FS::Conf>) shellmachine exists, the command:
207 is executed on shellmachine via ssh. This behaviour can be surpressed by
208 setting $FS::svc_acct::nossh_hack true.
216 local $SIG{HUP} = 'IGNORE';
217 local $SIG{INT} = 'IGNORE';
218 local $SIG{QUIT} = 'IGNORE';
219 local $SIG{TERM} = 'IGNORE';
220 local $SIG{TSTP} = 'IGNORE';
222 my($svcnum)=$self->getfield('svcnum');
225 return $error if $error;
227 my($cust_svc)=qsearchs('cust_svc',{'svcnum'=>$svcnum});
228 $error = $cust_svc->del;
229 return $error if $error;
231 my($username) = $self->getfield('username');
232 if ( $username && $shellmachine && ! $nossh_hack ) {
233 ssh("root\@$shellmachine","userdel $username");
239 =item replace OLD_RECORD
241 Replaces OLD_RECORD with this one in the database. If there is an error,
242 returns the error, otherwise returns false.
244 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
245 dir field has changed, the command:
252 find . -depth -print | cpio -pdm $new_dir;
254 chown -R $uid.$gid $new_dir;
258 is executed on shellmachine via ssh. This behaviour can be surpressed by
259 setting $FS::svc_acct::nossh_hack true.
267 return "(Old) Not a svc_acct record!" unless $old->table eq "svc_acct";
268 return "Can't change svcnum!"
269 unless $old->getfield('svcnum') eq $new->getfield('svcnum');
271 return "Username in use"
272 if $old->getfield('username') ne $new->getfield('username') &&
273 qsearchs('svc_acct',{'username'=> $new->getfield('username') } );
275 return "Can't change uid!"
276 if $old->getfield('uid') ne $new->getfield('uid');
278 #change homdir when we change username
279 if ( $old->getfield('username') ne $new->getfield('username') ) {
280 $new->setfield('dir','');
284 return $error if $error;
286 local $SIG{HUP} = 'IGNORE';
287 local $SIG{INT} = 'IGNORE';
288 local $SIG{QUIT} = 'IGNORE';
289 local $SIG{TERM} = 'IGNORE';
290 local $SIG{TSTP} = 'IGNORE';
292 $error = $new->rep($old);
293 return $error if $error;
295 my($old_dir,$new_dir)=( $old->getfield('dir'),$new->getfield('dir') );
296 my($uid,$gid)=( $new->getfield('uid'), $new->getfield('gid') );
299 && $old_dir ne $new_dir
302 ssh("root\@$shellmachine","[ -d $old_dir ] && ".
303 "( chmod u+t $old_dir; ". #turn off qmail delivery
304 "umask 022; mkdir $new_dir; cd $old_dir; ".
305 "find . -depth -print | cpio -pdm $new_dir; ".
306 "chmod u-t $new_dir; chown -R $uid.$gid $new_dir; ".
317 Suspends this account by prefixing *SUSPENDED* to the password. If there is an
318 error, returns the error, otherwise returns false.
320 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
326 my(%hash) = $old->hash;
327 unless ( $hash{_password} =~ /^\*SUSPENDED\* / ) {
328 $hash{_password} = '*SUSPENDED* '.$hash{_password};
329 my($new) = create FS::svc_acct ( \%hash );
330 # $new->replace($old);
331 $new->rep($old); #to avoid password checking :)
333 ''; #no error (already suspended)
340 Unsuspends this account by removing *SUSPENDED* from the password. If there is
341 an error, returns the error, otherwise returns false.
343 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
349 my(%hash) = $old->hash;
350 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
351 $hash{_password} = $1;
352 my($new) = create FS::svc_acct ( \%hash );
353 # $new->replace($old);
354 $new->rep($old); #to avoid password checking :)
356 ''; #no error (already unsuspended)
362 Just returns false (no error) for now.
364 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
368 # Usage: $error = $record -> cancel;
370 ''; #stub (no error) - taken care of in delete
375 Checks all fields to make sure this is a valid service. If there is an error,
376 returns the error, otherwise returns false. Called by the insert and replace
379 Sets any fixed values; see L<FS::part_svc>.
385 return "Not a svc_acct record!" unless $self->table eq "svc_acct";
386 my($recref) = $self->hashref;
388 $recref->{svcnum} =~ /^(\d*)$/ or return "Illegal svcnum";
389 $recref->{svcnum} = $1;
393 my($svcnum)=$self->getfield('svcnum');
395 my($cust_svc)=qsearchs('cust_svc',{'svcnum'=>$svcnum});
396 return "Unknown svcnum" unless $cust_svc;
397 $svcpart=$cust_svc->svcpart;
399 $svcpart=$self->getfield('svcpart');
401 my($part_svc)=qsearchs('part_svc',{'svcpart'=>$svcpart});
402 return "Unkonwn svcpart" unless $part_svc;
404 #set fixed fields from part_svc
406 foreach $field ( fields('svc_acct') ) {
407 if ( $part_svc->getfield('svc_acct__'. $field. '_flag') eq 'F' ) {
408 $self->setfield($field,$part_svc->getfield('svc_acct__'. $field) );
412 my($ulen)=$self->dbdef_table->column('username')->length;
413 $recref->{username} =~ /^([a-z0-9_\-]{2,$ulen})$/
414 or return "Illegal username";
415 $recref->{username} = $1;
416 $recref->{username} =~ /[a-z]/ or return "Illegal username";
418 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum";
419 $recref->{popnum} = $1;
420 return "Unkonwn popnum" unless
421 ! $recref->{popnum} ||
422 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
424 unless ( $part_svc->getfield('svc_acct__uid_flag') eq 'F' ) {
426 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
427 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
429 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
430 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
431 #not all systems use gid=uid
432 #you can set a fixed gid in part_svc
434 return "Only root can have uid 0"
435 if $recref->{uid} == 0 && $recref->{username} ne 'root';
438 return $error if $error=$self->ut_textn('finger');
440 $recref->{dir} =~ /^([\/\w\-]*)$/
441 or return "Illegal directory";
442 $recref->{dir} = $1 ||
443 $dir_prefix . '/' . $recref->{username}
444 #$dir_prefix . '/' . substr($recref->{username},0,1). '/' . $recref->{username}
447 unless ( $recref->{username} eq 'sync' ) {
449 if ( $shell = (grep $_ eq $recref->{shell}, @shells)[0] ) {
450 $recref->{shell} = $shell;
452 return "Illegal shell ". $self->shell;
455 $recref->{shell} = '/bin/sync';
458 $recref->{quota} =~ /^(\d*)$/ or return "Illegal quota (unimplemented)";
459 $recref->{quota} = $1;
462 $recref->{gid} ne '' ?
463 return "Can't have gid without uid" : ( $recref->{gid}='' );
464 $recref->{finger} ne '' ?
465 return "Can't have finger-name without uid" : ( $recref->{finger}='' );
466 $recref->{dir} ne '' ?
467 return "Can't have directory without uid" : ( $recref->{dir}='' );
468 $recref->{shell} ne '' ?
469 return "Can't have shell without uid" : ( $recref->{shell}='' );
470 $recref->{quota} ne '' ?
471 return "Can't have quota without uid" : ( $recref->{quota}='' );
474 unless ( $part_svc->getfield('svc_acct__slipip_flag') eq 'F' ) {
475 unless ( $recref->{slipip} eq '0e0' ) {
476 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
477 or return "Illegal slipip". $self->slipip;
478 $recref->{slipip} = $1;
480 $recref->{slipip} = '0e0';
485 #arbitrary RADIUS stuff; allow ut_textn for now
486 foreach ( grep /^radius_/, fields('svc_acct') ) {
490 #generate a password if it is blank
491 $recref->{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) )
492 unless ( $recref->{_password} );
494 #if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,16})$/ ) {
495 if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,8})$/ ) {
496 $recref->{_password} = $1.$3;
497 #uncomment this to encrypt password immediately upon entry, or run
498 #bin/crypt_pw in cron to give new users a window during which their
499 #password is available to techs, for faxing, etc. (also be aware of
501 #$recref->{password} = $1.
502 # crypt($3,$saltset[int(rand(64))].$saltset[int(rand(64))]
504 } elsif ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([\w\.\/]{13,24})$/ ) {
505 $recref->{_password} = $1.$3;
506 } elsif ( $recref->{_password} eq '*' ) {
507 $recref->{_password} = '*';
509 return "Illegal password";
519 It doesn't properly override FS::Record yet.
521 The remote commands should be configurable.
523 The create method should set defaults from part_svc (like the check method
528 L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>, L<FS::part_svc>, L<FS::cust_pkg>,
529 L<FS::SSH>, L<ssh>, L<FS::svc_acct_pop>, schema.html from the base
534 ivan@voicenet.com 97-jul-16 - 21
536 rewrite (among other things, now know about part_svc) ivan@sisd.com 98-mar-8
538 Changed 'password' to '_password' because Pg6.3 reserves the password word
539 bmccane@maxbaud.net 98-apr-3
541 username length and shell no longer hardcoded ivan@sisd.com 98-jun-28
543 eww but needed: ignore uid duplicates for 'fax' and 'hylafax'
544 ivan@sisd.com 98-jun-29
546 $nossh_hack ivan@sisd.com 98-jul-13
548 protections against UID/GID of 0 for incorrectly-setup RDBMSs (also
549 in bin/svc_acct.export) ivan@sisd.com 98-jul-13
551 arbitrary radius attributes ivan@sisd.com 98-aug-13
553 /var/spool/freeside/conf/shellmachine ivan@sisd.com 98-aug-13
555 pod and FS::conf ivan@sisd.com 98-sep-22
557 $Log: svc_acct.pm,v $
558 Revision 1.2 1998-11-13 09:56:55 ivan
559 change configuration file layout to support multiple distinct databases (with
560 own set of config files, export, etc.)