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