4 use vars qw( @ISA $nossh_hack $conf $dir_prefix @shells $usernamemin
5 $usernamemax $passwordmin
6 $shellmachine @saltset @pw_set);
8 use FS::Record qw( qsearchs fields );
14 @ISA = qw( FS::svc_Common );
16 #ask FS::UID to run this stuff for us later
17 $FS::UID::callback{'FS::svc_acct'} = sub {
19 $dir_prefix = $conf->config('home');
20 @shells = $conf->config('shells');
21 $shellmachine = $conf->config('shellmachine');
22 $usernamemin = $conf->config('usernamemin') || 2;
23 $usernamemax = $conf->config('usernamemax');
24 $passwordmin = $conf->config('passwordmin') || 6;
27 @saltset = ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
28 @pw_set = ( 'a'..'z', 'A'..'Z', '0'..'9', '(', ')', '#', '!', '.', ',' );
30 #not needed in 5.004 #srand($$|time);
34 FS::svc_acct - Object methods for svc_acct records
40 $record = new FS::svc_acct \%hash;
41 $record = new FS::svc_acct { 'column' => 'value' };
43 $error = $record->insert;
45 $error = $new_record->replace($old_record);
47 $error = $record->delete;
49 $error = $record->check;
51 $error = $record->suspend;
53 $error = $record->unsuspend;
55 $error = $record->cancel;
59 An FS::svc_acct object represents an account. FS::svc_acct inherits from
60 FS::svc_Common. The following fields are currently supported:
64 =item svcnum - primary key (assigned automatcially for new accounts)
68 =item _password - generated if blank
70 =item popnum - Point of presence (see L<FS::svc_acct_pop>)
78 =item dir - set automatically if blank (and uid is not)
82 =item quota - (unimplementd)
84 =item slipip - IP address
86 =item radius_I<Radius_Attribute> - I<Radius-Attribute>
96 Creates a new account. To add the account to the database, see L<"insert">.
100 sub table { 'svc_acct'; }
104 Adds this account to the database. If there is an error, returns the error,
105 otherwise returns false.
107 The additional fields pkgnum and svcpart (see L<FS::cust_svc>) should be
108 defined. An FS::cust_svc record will be created and inserted.
110 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
111 username, uid, and dir fields are defined, the command
113 useradd -d $dir -m -s $shell -u $uid $username
115 is executed on shellmachine via ssh. This behaviour can be surpressed by
116 setting $FS::svc_acct::nossh_hack true.
124 local $SIG{HUP} = 'IGNORE';
125 local $SIG{INT} = 'IGNORE';
126 local $SIG{QUIT} = 'IGNORE';
127 local $SIG{TERM} = 'IGNORE';
128 local $SIG{TSTP} = 'IGNORE';
129 local $SIG{PIPE} = 'IGNORE';
131 $error = $self->check;
132 return $error if $error;
134 return "Username ". $self->username. " in use"
135 if qsearchs( 'svc_acct', { 'username' => $self->username } );
137 my $part_svc = qsearchs( 'part_svc', { 'svcpart' => $self->svcpart } );
138 return "Unkonwn svcpart" unless $part_svc;
140 if $part_svc->svc_acct__uid_flag ne 'F'
141 && qsearchs( 'svc_acct', { 'uid' => $self->uid } )
142 && $self->username !~ /^(hyla)?fax$/
145 $error = $self->SUPER::insert;
146 return $error if $error;
148 my ( $username, $uid, $dir, $shell ) = (
160 ssh("root\@$shellmachine",
161 "useradd -d $dir -m -s $shell -u $uid $username"
164 #ssh("root\@$shellmachine","/bin/mkdir $dir; /bin/chmod 711 $dir; ".
165 # "/bin/cp -p /etc/skel/.* $dir 2>/dev/null; ".
166 # "/bin/cp -pR /etc/skel/Maildir $dir 2>/dev/null; ".
167 # "/bin/chown -R $uid $dir") unless $nossh_hack;
175 Deletes this account from the database. If there is an error, returns the
176 error, otherwise returns false.
178 The corresponding FS::cust_svc record will be deleted as well.
180 If the configuration value (see L<FS::Conf>) shellmachine exists, the command:
184 is executed on shellmachine via ssh. This behaviour can be surpressed by
185 setting $FS::svc_acct::nossh_hack true.
193 local $SIG{HUP} = 'IGNORE';
194 local $SIG{INT} = 'IGNORE';
195 local $SIG{QUIT} = 'IGNORE';
196 local $SIG{TERM} = 'IGNORE';
197 local $SIG{TSTP} = 'IGNORE';
198 local $SIG{PIPE} = 'IGNORE';
200 $error = $self->SUPER::delete;
201 return $error if $error;
203 my $username = $self->username;
204 if ( $username && $shellmachine && ! $nossh_hack ) {
205 ssh("root\@$shellmachine","userdel $username");
211 =item replace OLD_RECORD
213 Replaces OLD_RECORD with this one in the database. If there is an error,
214 returns the error, otherwise returns false.
216 If the configuration value (see L<FS::Conf>) shellmachine exists, and the
217 dir field has changed, the command:
224 find . -depth -print | cpio -pdm $new_dir;
226 chown -R $uid.$gid $new_dir;
230 is executed on shellmachine via ssh. This behaviour can be surpressed by
231 setting $FS::svc_acct::nossh_hack true.
236 my ( $new, $old ) = ( shift, shift );
239 return "Username in use"
240 if $old->username ne $new->username &&
241 qsearchs( 'svc_acct', { 'username' => $new->username } );
243 return "Can't change uid!" if $old->uid != $new->uid;
245 #change homdir when we change username
246 $new->setfield('dir', '') if $old->username ne $new->username;
248 local $SIG{HUP} = 'IGNORE';
249 local $SIG{INT} = 'IGNORE';
250 local $SIG{QUIT} = 'IGNORE';
251 local $SIG{TERM} = 'IGNORE';
252 local $SIG{TSTP} = 'IGNORE';
253 local $SIG{PIPE} = 'IGNORE';
255 $error = $new->SUPER::replace($old);
256 return $error if $error;
258 my ( $old_dir, $new_dir ) = ( $old->getfield('dir'), $new->getfield('dir') );
259 my ( $uid, $gid) = ( $new->getfield('uid'), $new->getfield('gid') );
262 && $old_dir ne $new_dir
265 ssh("root\@$shellmachine","[ -d $old_dir ] && ".
266 "( chmod u+t $old_dir; ". #turn off qmail delivery
267 "umask 022; mkdir $new_dir; cd $old_dir; ".
268 "find . -depth -print | cpio -pdm $new_dir; ".
269 "chmod u-t $new_dir; chown -R $uid.$gid $new_dir; ".
280 Suspends this account by prefixing *SUSPENDED* to the password. If there is an
281 error, returns the error, otherwise returns false.
283 Called by the suspend method of FS::cust_pkg (see L<FS::cust_pkg>).
289 my %hash = $self->hash;
290 unless ( $hash{_password} =~ /^\*SUSPENDED\* / ) {
291 $hash{_password} = '*SUSPENDED* '.$hash{_password};
292 my $new = new FS::svc_acct ( \%hash );
293 $new->replace($self);
295 ''; #no error (already suspended)
301 Unsuspends this account by removing *SUSPENDED* from the password. If there is
302 an error, returns the error, otherwise returns false.
304 Called by the unsuspend method of FS::cust_pkg (see L<FS::cust_pkg>).
310 my %hash = $self->hash;
311 if ( $hash{_password} =~ /^\*SUSPENDED\* (.*)$/ ) {
312 $hash{_password} = $1;
313 my $new = new FS::svc_acct ( \%hash );
314 $new->replace($self);
316 ''; #no error (already unsuspended)
322 Just returns false (no error) for now.
324 Called by the cancel method of FS::cust_pkg (see L<FS::cust_pkg>).
328 Checks all fields to make sure this is a valid service. If there is an error,
329 returns the error, otherwise returns false. Called by the insert and replace
332 Sets any fixed values; see L<FS::part_svc>.
339 my($recref) = $self->hashref;
341 my $x = $self->setfixed;
342 return $x unless ref($x);
345 my $ulen = $usernamemax || $self->dbdef_table->column('username')->length;
346 $recref->{username} =~ /^([a-z0-9_\-\.]{$usernamemin,$ulen})$/
347 or return "Illegal username";
348 $recref->{username} = $1;
349 $recref->{username} =~ /[a-z]/ or return "Illegal username";
351 $recref->{popnum} =~ /^(\d*)$/ or return "Illegal popnum: ".$recref->{popnum};
352 $recref->{popnum} = $1;
353 return "Unkonwn popnum" unless
354 ! $recref->{popnum} ||
355 qsearchs('svc_acct_pop',{'popnum'=> $recref->{popnum} } );
357 unless ( $part_svc->getfield('svc_acct__uid_flag') eq 'F' ) {
359 $recref->{uid} =~ /^(\d*)$/ or return "Illegal uid";
360 $recref->{uid} = $1 eq '' ? $self->unique('uid') : $1;
362 $recref->{gid} =~ /^(\d*)$/ or return "Illegal gid";
363 $recref->{gid} = $1 eq '' ? $recref->{uid} : $1;
364 #not all systems use gid=uid
365 #you can set a fixed gid in part_svc
367 return "Only root can have uid 0"
368 if $recref->{uid} == 0 && $recref->{username} ne 'root';
371 return $error if $error=$self->ut_textn('finger');
373 $recref->{dir} =~ /^([\/\w\-]*)$/
374 or return "Illegal directory";
375 $recref->{dir} = $1 ||
376 $dir_prefix . '/' . $recref->{username}
377 #$dir_prefix . '/' . substr($recref->{username},0,1). '/' . $recref->{username}
380 unless ( $recref->{username} eq 'sync' ) {
382 if ( $shell = (grep $_ eq $recref->{shell}, @shells)[0] ) {
383 $recref->{shell} = $shell;
385 return "Illegal shell \`". $self->shell. "\'; ".
386 $conf->dir. "/shells contains: @shells";
389 $recref->{shell} = '/bin/sync';
392 $recref->{quota} =~ /^(\d*)$/ or return "Illegal quota (unimplemented)";
393 $recref->{quota} = $1;
396 $recref->{gid} ne '' ?
397 return "Can't have gid without uid" : ( $recref->{gid}='' );
398 $recref->{finger} ne '' ?
399 return "Can't have finger-name without uid" : ( $recref->{finger}='' );
400 $recref->{dir} ne '' ?
401 return "Can't have directory without uid" : ( $recref->{dir}='' );
402 $recref->{shell} ne '' ?
403 return "Can't have shell without uid" : ( $recref->{shell}='' );
404 $recref->{quota} ne '' ?
405 return "Can't have quota without uid" : ( $recref->{quota}='' );
408 unless ( $part_svc->getfield('svc_acct__slipip_flag') eq 'F' ) {
409 unless ( $recref->{slipip} eq '0e0' ) {
410 $recref->{slipip} =~ /^(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})$/
411 or return "Illegal slipip". $self->slipip;
412 $recref->{slipip} = $1;
414 $recref->{slipip} = '0e0';
419 #arbitrary RADIUS stuff; allow ut_textn for now
420 foreach ( grep /^radius_/, fields('svc_acct') ) {
424 #generate a password if it is blank
425 $recref->{_password} = join('',map($pw_set[ int(rand $#pw_set) ], (0..7) ) )
426 unless ( $recref->{_password} );
428 #if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{4,16})$/ ) {
429 if ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([^\t\n]{$passwordmin,8})$/ ) {
430 $recref->{_password} = $1.$3;
431 #uncomment this to encrypt password immediately upon entry, or run
432 #bin/crypt_pw in cron to give new users a window during which their
433 #password is available to techs, for faxing, etc. (also be aware of
435 #$recref->{password} = $1.
436 # crypt($3,$saltset[int(rand(64))].$saltset[int(rand(64))]
438 } elsif ( $recref->{_password} =~ /^((\*SUSPENDED\* )?)([\w\.\/]{13,24})$/ ) {
439 $recref->{_password} = $1.$3;
440 } elsif ( $recref->{_password} eq '*' ) {
441 $recref->{_password} = '*';
443 return "Illegal password";
453 $Id: svc_acct.pm,v 1.3 2000-03-06 16:38:42 ivan Exp $
457 The remote commands should be configurable.
459 The bits which ssh should fork before doing so.
461 The $recref stuff in sub check should be cleaned up.
465 L<FS::svc_Common>, L<FS::Record>, L<FS::Conf>, L<FS::cust_svc>,
466 L<FS::part_svc>, L<FS::cust_pkg>, L<FS::SSH>, L<ssh>, L<FS::svc_acct_pop>,
467 schema.html from the base documentation.