re-enable prompting
[freeside.git] / bin / shadow.reimport
1 #!/usr/bin/perl -w
2 #
3 # -d: dry-run: make no changes
4 # -r: replace: overwrite existing passwords (otherwise only "*" passwords will
5 #              be changed)
6
7 use strict;
8 use vars qw(%part_svc);
9 use Getopt::Std;
10 use Term::Query qw(query);
11 use Net::SCP qw(iscp);
12 use FS::UID qw(adminsuidsetup datasrc);
13 use FS::Record qw(qsearch qsearchs);
14 use FS::svc_acct;
15 use FS::part_svc;
16
17 use vars qw($opt_d $opt_r);
18 getopts("dr");
19
20 my $user = shift or die &usage;
21 adminsuidsetup $user;
22
23 push @FS::svc_acct::shells, qw(/bin/sync /sbin/shutdown /bin/halt /sbin/halt); #others?
24
25 my($spooldir)="/usr/local/etc/freeside/export.". datasrc;
26
27 #$FS::svc_acct::nossh_hack = 1;
28 $FS::svc_Common::noexport_hack = 1;
29
30 ###
31
32 %part_svc=map { $_->svcpart, $_ } qsearch('part_svc',{'svcdb'=>'svc_acct'});
33
34 die "No services with svcdb svc_acct!\n" unless %part_svc;
35
36 print "\n\n", &menu_svc, "\n", <<END;
37 Enter part number to import.
38 END
39 my($shell_svcpart)=&getpart;
40
41 print "\n\n", <<END;
42 Enter the location and name of your _user_ shadow file, for example
43 "mail.isp.com:/etc/shadow" or "bsd.isp.com:/etc/master.passwd"
44 END
45 my($loc_shadow)=&getvalue(":");
46 iscp("root\@$loc_shadow", "$spooldir/shadow.import");
47
48 sub menu_svc {
49   ( join "\n", map "$_: ".$part_svc{$_}->svc, sort keys %part_svc ). "\n";
50 }
51 sub getpart {
52   $^W=0; # Term::Query isn't -w-safe
53   my $return = query "Enter part number:", 'irk', [ keys %part_svc ];
54   $^W=1;
55   $return;
56 }
57 sub getvalue {
58   my $prompt = shift;
59   $^W=0; # Term::Query isn't -w-safe
60   my $return = query $prompt, '';
61   $^W=1;
62   $return;
63 }
64
65 print "\n\n";
66
67 ###
68
69 open(SHADOW,"<$spooldir/shadow.import");
70
71 my($line, $updated);
72 while (<SHADOW>) {
73   $line++;
74   chop;
75   my($username,$password)=split(/:/);
76
77   my @svc_acct = grep { $_->cust_svc->svcpart == $shell_svcpart } 
78                  qsearch('svc_acct', { 'username' => $username } );
79
80   next unless @svc_acct;
81
82   if ( scalar(@svc_acct) > 1 ) {
83     die "more than one $username found!\n";
84     next;
85   }
86
87   my $svc_acct = shift @svc_acct;
88
89   next unless $svc_acct->_password eq '*' || $opt_r;
90
91   next if $svc_acct->_password eq $password;
92   next if $svc_acct->_password =~ /^\*SUSPENDED\*/;
93
94   my $new_svc_acct = new FS::svc_acct( { $svc_acct->hash } );
95   $new_svc_acct->_password($password);
96   #warn "$username: ". $svc_acct->_password. " -> $password\n";
97   warn "changing password for $username\n";
98   unless ( $opt_d ) {
99     my $error = $new_svc_acct->replace($svc_acct);
100     die "$username: $error" if $error;
101   }
102
103   $updated++;
104
105 }
106
107 warn "$updated of $line passwords changed\n";
108
109 sub usage {
110   die "Usage:\n\n  shadow.reimport [ -d ] [ -r ] user\n";
111 }
112