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