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