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