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