4 use vars qw(@ISA $nossh_hack $conf $dir_prefix @shells
5 $shellmachine @saltset @pw_set);
7 use FS::Record qw( qsearchs fields );
13 @ISA = qw( FS::svc_Common );
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 = new FS::svc_acct \%hash;
37 $record = new 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::svc_Common. 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">.
96 sub table { 'svc_acct'; }
100 Adds this account to the database. If there is an error, returns the error,
101 otherwise returns false.
103 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
104 defined. An FS::cust_svc record will be created and inserted.
106 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
107 username, uid, and dir fields are defined, the command
109 useradd -d $dir -m -s $shell -u $uid $username
111 is executed on shellmachine via ssh. This behaviour can be surpressed by
112 setting $FS::svc_acct::nossh_hack true.
120 local $SIG{HUP} = 'IGNORE';
121 local $SIG{INT} = 'IGNORE';
122 local $SIG{QUIT} = 'IGNORE';
123 local $SIG{TERM} = 'IGNORE';
124 local $SIG{TSTP} = 'IGNORE';
125 local $SIG{PIPE} = 'IGNORE';
127 $error = $self->check;
128 return $error if $error;
130 return "Username ". $self->username. " in use"
131 if qsearchs( 'svc_acct', { 'username' => $self->username } );
133 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
134 return "Unkonwn svcpart" unless $part_svc;
136 if $part_svc->svc_acct__uid_flag ne 'F'
137 && qsearchs( 'svc_acct', { 'uid' => $self->uid } )
138 && $self->username !~ /^(hyla)?fax$/
141 $error = $self->SUPER::insert;
142 return $error if $error;
144 my ( $username, $uid, $dir, $shell ) = (
156 ssh("root\@$shellmachine",
157 "useradd -d $dir -m -s $shell -u $uid $username"
160 #ssh("root\@$shellmachine","/bin/mkdir $dir; /bin/chmod 711 $dir; ".
161 # "/bin/cp -p /etc/skel/.* $dir 2>/dev/null; ".
162 # "/bin/cp -pR /etc/skel/Maildir $dir 2>/dev/null; ".
163 # "/bin/chown -R $uid $dir") unless $nossh_hack;
171 Deletes this account from the database. If there is an error, returns the
172 error, otherwise returns false.
174 The corresponding FS::cust_svc record will be deleted as well.
176 If the configuration value (see L<FS::Conf>) shellmachine exists, the command:
180 is executed on shellmachine via ssh. This behaviour can be surpressed by
181 setting $FS::svc_acct::nossh_hack true.
189 local $SIG{HUP} = 'IGNORE';
190 local $SIG{INT} = 'IGNORE';
191 local $SIG{QUIT} = 'IGNORE';
192 local $SIG{TERM} = 'IGNORE';
193 local $SIG{TSTP} = 'IGNORE';
194 local $SIG{PIPE} = 'IGNORE';
196 $error = $self->SUPER::delete;
197 return $error if $error;
199 my $username = $self->username;
200 if ( $username && $shellmachine && ! $nossh_hack ) {
201 ssh("root\@$shellmachine","userdel $username");
207 =item replace OLD_RECORD
209 Replaces OLD_RECORD with this one in the database. If there is an error,
210 returns the error, otherwise returns false.
212 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
213 dir field has changed, the command:
220 find . -depth -print | cpio -pdm $new_dir;
222 chown -R $uid.$gid $new_dir;
226 is executed on shellmachine via ssh. This behaviour can be surpressed by
227 setting $FS::svc_acct::nossh_hack true.
232 my ( $new, $old ) = ( shift, shift );
235 return "Username in use"
236 if $old->username ne $new->username &&
237 qsearchs( 'svc_acct', { 'username' => $new->username } );
239 return "Can't change uid!" if $old->uid != $new->uid;
241 #change homdir when we change username
242 $new->setfield('dir', '') if $old->username ne $new->username;
244 local $SIG{HUP} = 'IGNORE';
245 local $SIG{INT} = 'IGNORE';
246 local $SIG{QUIT} = 'IGNORE';
247 local $SIG{TERM} = 'IGNORE';
248 local $SIG{TSTP} = 'IGNORE';
249 local $SIG{PIPE} = 'IGNORE';
251 $error = $new->SUPER::replace($old);
252 return $error if $error;
254 my ( $old_dir, $new_dir ) = ( $old->getfield('dir'), $new->getfield('dir') );
255 my ( $uid, $gid) = ( $new->getfield('uid'), $new->getfield('gid') );
258 && $old_dir ne $new_dir
261 ssh("root\@$shellmachine","[ -d $old_dir ] && ".
262 "( chmod u+t $old_dir; ". #turn off qmail delivery
263 "umask 022; mkdir $new_dir; cd $old_dir; ".
264 "find . -depth -print | cpio -pdm $new_dir; ".
265 "chmod u-t $new_dir; chown -R $uid.$gid $new_dir; ".
276 Suspends this account by prefixing *SUSPENDED* to the password. If there is an
277 error, returns the error, otherwise returns false.
279 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
285 my %hash = $self->hash;
286 unless ( $hash{_password} =~ /^\*SUSPENDED\* / ) {
287 $hash{_password} = '*SUSPENDED* '.$hash{_password};
288 my $new = new FS::svc_acct ( \%hash );
289 $new->replace($self);
291 ''; #no error (already suspended)
297 Unsuspends this account by removing *SUSPENDED* from the password. If there is
298 an error, returns the error, otherwise returns false.
300 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
306 my %hash = $self->hash;
307 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
308 $hash{_password} = $1;
309 my $new = new FS::svc_acct ( \%hash );
310 $new->replace($self);
312 ''; #no error (already unsuspended)
318 Just returns false (no error) for now.
320 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
324 Checks all fields to make sure this is a valid service. If there is an error,
325 returns the error, otherwise returns false. Called by the insert and replace
328 Sets any fixed values; see L<FS::part_svc>.
335 my($recref) = $self->hashref;
337 my $x = $self->setfixed;
338 return $x unless ref($x);
341 my $ulen =$self->dbdef_table->column('username')->length;
342 $recref->{username} =~ /^([a-z0-9_\-]{2,$ulen})$/
343 or return "Illegal username";
344 $recref->{username} = $1;
345 $recref->{username} =~ /[a-z]/ or return "Illegal username";
347 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum";
348 $recref->{popnum} = $1;
349 return "Unkonwn popnum" unless
350 ! $recref->{popnum} ||
351 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
353 unless ( $part_svc->getfield('svc_acct__uid_flag') eq 'F' ) {
355 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
356 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
358 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
359 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
360 #not all systems use gid=uid
361 #you can set a fixed gid in part_svc
363 return "Only root can have uid 0"
364 if $recref->{uid} == 0 && $recref->{username} ne 'root';
367 return $error if $error=$self->ut_textn('finger');
369 $recref->{dir} =~ /^([\/\w\-]*)$/
370 or return "Illegal directory";
371 $recref->{dir} = $1 ||
372 $dir_prefix . '/' . $recref->{username}
373 #$dir_prefix . '/' . substr($recref->{username},0,1). '/' . $recref->{username}
376 unless ( $recref->{username} eq 'sync' ) {
378 if ( $shell = (grep $_ eq $recref->{shell}, @shells)[0] ) {
379 $recref->{shell} = $shell;
381 return "Illegal shell ". $self->shell;
384 $recref->{shell} = '/bin/sync';
387 $recref->{quota} =~ /^(\d*)$/ or return "Illegal quota (unimplemented)";
388 $recref->{quota} = $1;
391 $recref->{gid} ne '' ?
392 return "Can't have gid without uid" : ( $recref->{gid}='' );
393 $recref->{finger} ne '' ?
394 return "Can't have finger-name without uid" : ( $recref->{finger}='' );
395 $recref->{dir} ne '' ?
396 return "Can't have directory without uid" : ( $recref->{dir}='' );
397 $recref->{shell} ne '' ?
398 return "Can't have shell without uid" : ( $recref->{shell}='' );
399 $recref->{quota} ne '' ?
400 return "Can't have quota without uid" : ( $recref->{quota}='' );
403 unless ( $part_svc->getfield('svc_acct__slipip_flag') eq 'F' ) {
404 unless ( $recref->{slipip} eq '0e0' ) {
405 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
406 or return "Illegal slipip". $self->slipip;
407 $recref->{slipip} = $1;
409 $recref->{slipip} = '0e0';
414 #arbitrary RADIUS stuff; allow ut_textn for now
415 foreach ( grep /^radius_/, fields('svc_acct') ) {
419 #generate a password if it is blank
420 $recref->{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) )
421 unless ( $recref->{_password} );
423 #if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,16})$/ ) {
424 if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,8})$/ ) {
425 $recref->{_password} = $1.$3;
426 #uncomment this to encrypt password immediately upon entry, or run
427 #bin/crypt_pw in cron to give new users a window during which their
428 #password is available to techs, for faxing, etc. (also be aware of
430 #$recref->{password} = $1.
431 # crypt($3,$saltset[int(rand(64))].$saltset[int(rand(64))]
433 } elsif ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([\w\.\/]{13,24})$/ ) {
434 $recref->{_password} = $1.$3;
435 } elsif ( $recref->{_password} eq '*' ) {
436 $recref->{_password} = '*';
438 return "Illegal password";
448 $Id: svc_acct.pm,v 1.7 1999-04-07 14:37:37 ivan Exp $
452 The remote commands should be configurable.
454 The bits which ssh should fork before doing so.
456 The $recref stuff in sub check should be cleaned up.
460 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
461 L<FS::part_svc>, L<FS::cust_pkg>, L<FS::SSH>, L<ssh>, L<FS::svc_acct_pop>,
462 schema.html from the base documentation.
466 ivan@voicenet.com 97-jul-16 - 21
468 rewrite (among other things, now know about part_svc) ivan@sisd.com 98-mar-8
470 Changed 'password' to '_password' because Pg6.3 reserves the password word
471 bmccane@maxbaud.net 98-apr-3
473 username length and shell no longer hardcoded ivan@sisd.com 98-jun-28
475 eww but needed: ignore uid duplicates for 'fax' and 'hylafax'
476 ivan@sisd.com 98-jun-29
478 $nossh_hack ivan@sisd.com 98-jul-13
480 protections against UID/GID of 0 for incorrectly-setup RDBMSs (also
481 in bin/svc_acct.export) ivan@sisd.com 98-jul-13
483 arbitrary radius attributes ivan@sisd.com 98-aug-13
485 /var/spool/freeside/conf/shellmachine ivan@sisd.com 98-aug-13
487 pod and FS::conf ivan@sisd.com 98-sep-22
489 $Log: svc_acct.pm,v $
490 Revision 1.7 1999-04-07 14:37:37 ivan
491 use FS::part_svc and FS::svc_acct_pop to avoid warnings
493 Revision 1.6 1999/01/25 12:26:15 ivan
494 yet more mod_perl stuff
496 Revision 1.5 1999/01/18 21:58:09 ivan
497 esthetic: eq and ne were used in a few places instead of == and !=
499 Revision 1.4 1998/12/30 00:30:45 ivan
500 svc_ stuff is more properly OO - has a common superclass FS::svc_Common
502 Revision 1.2 1998/11/13 09:56:55 ivan
503 change configuration file layout to support multiple distinct databases (with
504 own set of config files, export, etc.)