support for radius check attributes (except importing). poorly documented.
[freeside.git] / bin / svc_acct.export
1 #!/usr/bin/perl -w
2 #
3 # $Id: svc_acct.export,v 1.15 2000-07-06 08:57:28 ivan Exp $
4 #
5 # Create and export password files: passwd, passwd.adjunct, shadow,
6 # acp_passwd, acp_userinfo, acp_dialup, users
7 #
8 # ivan@voicenet.com late august/september 96
9 # (the password encryption bits were from melody)
10 #
11 # use a temporary copy of svc_acct to minimize lock time on the real file,
12 # and skip blank entries.
13 #
14 # ivan@voicenet.com 96-Oct-6
15 #
16 # change users / acp_dialup file formats
17 # ivan@voicenet.com 97-jan-28-31
18 #
19 # change priority (after copies) to 19, not 10
20 # ivan@voicenet.com 97-feb-5
21 #
22 # added exit if stuff is already locked 97-apr-15
23 #
24 # rewrite ivan@sisd.com 98-mar-9
25 #
26 # Changed 'password' to '_password' because Pg6.3 reserves this word
27 # Added code to create a FreeBSD style master.passwd file
28 #   bmccane@maxbaud.net 98-Apr-3
29 #
30 # don't export non-root 0 UID's, even if they get put in the database
31 # ivan@sisd.com 98-jul-14
32 #
33 # Uses Idle_Timeout, Port_Limit, Framed_Netmask and Framed_Route if they
34 # exist; need some way to support arbitrary radius fields.  also 
35 # /var/spool/freeside/conf/ ivan@sisd.com 98-jul-26, aug-9
36 #
37 # OOPS!  added arbitrary radius fields (pry 98-aug-16) but forgot to say so.
38 # ivan@sisd.com 98-sep-18
39
40 # $Log: svc_acct.export,v $
41 # Revision 1.15  2000-07-06 08:57:28  ivan
42 # support for radius check attributes (except importing).  poorly documented.
43 #
44 # Revision 1.14  2000/06/29 15:01:25  ivan
45 # another silly typo in svc_acct.export
46 #
47 # Revision 1.13  2000/06/28 12:37:28  ivan
48 # add support for config option textradiusprepend
49 #
50 # Revision 1.12  2000/06/15 14:07:02  ivan
51 # added ICRADIUS radreply table support, courtesy of Kenny Elliott
52 #
53 # Revision 1.11  2000/03/06 16:00:39  ivan
54 # sync up with working versoin
55 #
56 # Revision 1.2  1998/12/10 07:23:15  ivan
57 # use FS::Conf, need user (for datasrc)
58 #
59
60 use strict;
61 use vars qw($conf);
62 use Fcntl qw(:flock);
63 use IO::Handle;
64 use FS::Conf;
65 use FS::SSH qw(scp ssh);
66 use FS::UID qw(adminsuidsetup datasrc dbh);
67 use FS::Record qw(qsearch fields);
68 use FS::svc_acct;
69
70 my $user = shift or die &usage;
71 adminsuidsetup $user;
72
73 $conf = new FS::Conf;
74
75 my @shellmachines = $conf->config('shellmachines')
76   if $conf->exists('shellmachines');
77
78 my @bsdshellmachines = $conf->config('bsdshellmachines')
79   if $conf->exists('bsdshellmachines');
80
81 my @nismachines = $conf->config('nismachines')
82   if $conf->exists('nismachines');
83
84 my @erpcdmachines = $conf->config('erpcdmachines')
85   if $conf->exists('erpcdmachines');
86
87 my @radiusmachines = $conf->config('radiusmachines')
88   if $conf->exists('radiusmachines');
89
90 my $icradiusmachines = $conf->exists('icradiusmachines');
91 my @icradiusmachines = $conf->config('icradiusmachines') if $icradiusmachines;
92 my $icradius_mysqldest =
93   $conf->config('icradius_mysqldest') || "/usr/local/var/"
94     if $icradiusmachines;
95 my $icradius_mysqlsource =
96   $conf->config('icradius_mysqlsource') || "/usr/local/var/freeside"
97     if $icradiusmachines;
98 my $icradius_dbh = dbh; #could eventually get it from a config file if you're
99                         #not running MySQL for your Freeside database
100
101 my $textradiusprepend = $conf->config('textradiusprepend');
102
103 my(@saltset)= ( 'a'..'z' , 'A'..'Z' , '0'..'9' , '.' , '/' );
104 require 5.004; #srand(time|$$);
105
106 my $spooldir = "/usr/local/etc/freeside/export.". datasrc;
107 my $spoollock = "/usr/local/etc/freeside/svc_acct.export.lock.". datasrc;
108
109 open(EXPORT,"+>>$spoollock") or die "Can't open $spoollock: $!";
110 select(EXPORT); $|=1; select(STDOUT);
111 unless ( flock(EXPORT,LOCK_EX|LOCK_NB) ) {
112   seek(EXPORT,0,0);
113   my($pid)=<EXPORT>;
114   chop($pid);
115   #no reason to start loct of blocking processes
116   die "Is another export process running under pid $pid?\n";
117 }
118 seek(EXPORT,0,0);
119 print EXPORT $$,"\n";
120
121 my(@svc_acct)=qsearch('svc_acct',{});
122
123 ( open(MASTER,">$spooldir/master.passwd")
124   and flock(MASTER,LOCK_EX|LOCK_NB)
125 ) or die "Can't open $spooldir/master.passwd: $!";
126 ( open(PASSWD,">$spooldir/passwd")
127   and flock(PASSWD,LOCK_EX|LOCK_NB)  
128 ) or die "Can't open $spooldir/passwd: $!";
129 ( open(SHADOW,">$spooldir/shadow")
130   and flock(SHADOW,LOCK_EX|LOCK_NB) 
131 ) or die "Can't open $spooldir/shadow: $!";
132 ( open(ACP_PASSWD,">$spooldir/acp_passwd") 
133   and flock (ACP_PASSWD,LOCK_EX|LOCK_NB)
134 ) or die "Can't open $spooldir/acp_passwd: $!";
135 ( open (ACP_DIALUP,">$spooldir/acp_dialup")
136   and flock(ACP_DIALUP,LOCK_EX|LOCK_NB)
137 ) or die "Can't open $spooldir/acp_dialup: $!";
138 ( open (USERS,">$spooldir/users")
139   and flock(USERS,LOCK_EX|LOCK_NB)
140 ) or die "Can't open $spooldir/users: $!";
141
142 chmod 0644, "$spooldir/passwd",
143             "$spooldir/acp_dialup",
144 ;
145 chmod 0600, "$spooldir/master.passwd",
146             "$spooldir/acp_passwd",
147             "$spooldir/shadow",
148             "$spooldir/users",
149 ;
150
151 if ( $icradiusmachines ) {
152   my $sth = $icradius_dbh->prepare("DELETE FROM radcheck");
153   $sth->execute or die "Can't reset radcheck table: ". $sth->errstr;
154   my $sth2 = $icradius_dbh->prepare("DELETE FROM radreply");
155   $sth2->execute or die "Can't reset radreply table: ". $sth2->errstr;
156 }
157
158 setpriority(0,0,10);
159
160 my($svc_acct);
161 foreach $svc_acct (@svc_acct) {
162
163   my($password)=$svc_acct->getfield('_password');
164   my($cpassword,$rpassword);
165   if ( ( length($password) <= 8 )
166        && ( $password ne '*' )
167        && ( $password ne '' )
168      ) {
169     $cpassword=crypt($password,
170                      $saltset[int(rand(64))].$saltset[int(rand(64))]
171     );
172     $rpassword=$password;
173   } else {
174     $cpassword=$password;
175     $rpassword='UNIX';
176   }
177
178   if ( $svc_acct->uid  =~ /^(\d+)$/ ) {
179
180     die "Non-root user ". $svc_acct->username. " has 0 UID!"
181       if $svc_acct->uid == 0 && $svc_acct->username ne 'root';
182
183     ###
184     # FORMAT OF FreeBSD MASTER PASSWD FILE HERE
185     print MASTER join(":",
186       $svc_acct->username,              # User name
187       $cpassword,                       # Encrypted password
188       $svc_acct->uid,                   # User ID
189       $svc_acct->gid,                   # Group ID
190       "",                               # Login Class
191       "0",                              # Password Change Time
192       "0",                              # Password Expiration Time
193       $svc_acct->finger,                # Users name
194       $svc_acct->dir,                   # Users home directory
195       $svc_acct->shell,                 # shell
196     ), "\n" ;
197
198     ###
199     # FORMAT OF THE PASSWD FILE HERE
200     print PASSWD join(":",
201       $svc_acct->username,
202       'x', # "##". $svc_acct->$username,
203       $svc_acct->uid,
204       $svc_acct->gid,
205       $svc_acct->finger,
206       $svc_acct->dir,
207       $svc_acct->shell,
208     ), "\n";
209
210     ###
211     # FORMAT OF THE SHADOW FILE HERE
212     print SHADOW join(":",
213       $svc_acct->username,
214       $cpassword,
215       '',
216       '',
217       '',
218       '',
219       '',
220       '',
221       '',
222     ), "\n";
223
224   }
225
226   if ( $svc_acct->slipip ne '' ) {
227
228     ###
229     # FORMAT OF THE ACP_* FILES HERE
230     print ACP_PASSWD join(":",
231       $svc_acct->username,
232       $cpassword,
233       "0",
234       "0",
235       "",
236       "",
237       "",
238     ), "\n";
239
240     my($ip)=$svc_acct->slipip;
241
242     unless ( $ip eq '0.0.0.0' || $svc_acct->slipip eq '0e0' ) {
243       print ACP_DIALUP $svc_acct->username, "\t*\t", $svc_acct->slipip, "\n";
244     }
245
246     my %radreply = $svc_acct->radius_reply;
247     my %radcheck = $svc_acct->radius_check;
248
249     my $radcheck = join ", ", map { qq($_ = "$radcheck{$_}") } keys %radcheck;
250     $radcheck .= ", " if $radcheck;
251
252     ###
253     # FORMAT OF THE USERS FILE HERE
254     print USERS
255       $svc_acct->username,
256       qq(\t${textradiusprepend}),
257       $radcheck,
258       qq(Password = "$rpassword"\n\t),
259       join ",\n\t", map { qq($_ = "$radreply{$_}") } keys %radreply;
260     
261     if ( $ip && $ip ne '0e0' ) {
262       #print USERS qq(,\n\tFramed-Address = "$ip"\n\n);
263       print USERS qq(,\n\tFramed-IP-Address = "$ip"\n\n);
264     } else {
265       print USERS qq(\n\n);
266     }
267
268     ###
269     # ICRADIUS export
270     if ( $icradiusmachines ) {
271
272       my $sth = $icradius_dbh->prepare(
273         "INSERT INTO radcheck ( id, UserName, Attribute, Value ) VALUES ( ".
274         join(", ", map { $icradius_dbh->quote( $_ ) } (
275           '',
276           $svc_acct->username,
277           "Password",
278           $svc_acct->_password,
279         ) ). " )"
280       );
281       $sth->execute or die "Can't insert into radcheck table: ". $sth->errstr;
282
283       foreach my $attribute ( keys %radcheck ) {
284         my $sth = $icradius_dbh->prepare(
285           "INSERT INTO radcheck ( id, UserName, Attribute, Value ) VALUES ( ".
286           join(", ", map { $icradius_dbh->quote( $_ ) } (
287             '',
288             $svc_acct->username,
289             $attribute
290             $radcheck{$attribute},
291           ) ). " )"
292         );
293         $sth->execute or die "Can't insert into radcheck table: ". $sth->errstr;
294       }
295
296       foreach my $attribute ( keys %radreply ) {
297         my $sth = $icradius_dbh->prepare(
298           "INSERT INTO radreply (id, UserName, Attribute, Value) VALUES ( ".
299           join(", ", map { $icradius_dbh->quote( $_ ) } (
300             '',
301             $svc_acct->username,
302             $attribute,
303             $radreply{$attribute},
304           ) ). " )"
305         );
306         $sth->execute or die "Can't insert into radreply table: ". $sth->errstr;
307       }
308
309     }
310
311   }
312
313 }
314
315 flock(MASTER,LOCK_UN);
316 flock(PASSWD,LOCK_UN);
317 flock(SHADOW,LOCK_UN);
318 flock(ACP_DIALUP,LOCK_UN);
319 flock(ACP_PASSWD,LOCK_UN);
320 flock(USERS,LOCK_UN);
321
322 close MASTER;
323 close PASSWD;
324 close SHADOW;
325 close ACP_DIALUP;
326 close ACP_PASSWD;
327 close USERS;
328
329 ###
330 # export stuff
331 #
332
333 my($shellmachine);
334 foreach $shellmachine (@shellmachines) {
335   scp("$spooldir/passwd","root\@$shellmachine:/etc/passwd.new")
336     == 0 or die "scp error: $!";
337   scp("$spooldir/shadow","root\@$shellmachine:/etc/shadow.new")
338     == 0 or die "scp error: $!";
339   ssh("root\@$shellmachine",
340     "( ".
341       "mv /etc/passwd.new /etc/passwd; ".
342       "mv /etc/shadow.new /etc/shadow; ".
343     " )"
344   )
345     == 0 or die "ssh error: $!";
346 }
347
348 my($bsdshellmachine);
349 foreach $bsdshellmachine (@bsdshellmachines) {
350   scp("$spooldir/passwd","root\@$bsdshellmachine:/etc/passwd.new")
351     == 0 or die "scp error: $!";
352   scp("$spooldir/master.passwd","root\@$bsdshellmachine:/etc/master.passwd.new")
353     == 0 or die "scp error: $!";
354   ssh("root\@$bsdshellmachine",
355     "( ".
356       "mv /etc/passwd.new /etc/passwd; ".
357       "mv /etc/master.passwd.new /etc/master.passwd; ".
358     " )"
359   )
360     == 0 or die "ssh error: $!";
361 }
362
363 my($nismachine);
364 foreach $nismachine (@nismachines) {
365   scp("$spooldir/passwd","root\@$nismachine:/etc/global/passwd")
366     == 0 or die "scp error: $!";
367   scp("$spooldir/shadow","root\@$nismachine:/etc/global/shadow")
368     == 0 or die "scp error: $!";
369   ssh("root\@$nismachine",
370     "( ".
371       "cd /var/yp; make; ".
372     " )"
373   )
374     == 0 or die "ssh error: $!";
375 }
376
377 my($erpcdmachine);
378 foreach $erpcdmachine (@erpcdmachines) {
379   scp("$spooldir/acp_passwd","root\@$erpcdmachine:/usr/annex/acp_passwd")
380     == 0 or die "scp error: $!";
381   scp("$spooldir/acp_dialup","root\@$erpcdmachine:/usr/annex/acp_dialup")
382     == 0 or die "scp error: $!";
383   ssh("root\@$erpcdmachine",
384     "( ".
385       "kill -USR1 \`cat /usr/annex/erpcd.pid\'".
386     " )"
387   )
388     == 0 or die "ssh error: $!";
389 }
390
391 my($radiusmachine);
392 foreach $radiusmachine (@radiusmachines) {
393   scp("$spooldir/users","root\@$radiusmachine:/etc/raddb/users")
394     == 0 or die "scp error: $!";
395   ssh("root\@$radiusmachine",
396     "( ".
397       "builddbm".
398     " )"
399   )
400     == 0 or die "ssh error: $!";
401 }
402
403 foreach my $icradiusmachine ( @icradiusmachines ) {
404   my( $machine, $db, $user, $pass ) = split(/\s+/, $icradiusmachine);
405   chdir $icradius_mysqlsource or die "Can't cd $icradius_mysqlsource: $!";
406   open(WRITER,"|ssh root\@$machine mysql -v --user=$user -p $db");
407   my $oldfh = select WRITER; $|=1; select $oldfh;
408   print WRITER "$pass\n";
409   sleep 2;
410   print WRITER "LOCK TABLES radcheck WRITE, radreply WRITE;\n";
411   foreach my $file ( glob("radcheck.*") ) {
412     scp($file,"root\@$machine:$icradius_mysqldest/$db/$file");
413   }
414   foreach my $file ( glob("radreply.*") ) {
415     scp($file,"root\@$machine:$icradius_mysqldest/$db/$file");
416   }
417   close WRITER;
418 }
419
420 unlink $spoollock;
421 flock(EXPORT,LOCK_UN);
422 close EXPORT;
423
424 #
425
426 sub usage {
427   die "Usage:\n\n  svc_acct.export user\n";
428 }
429