imapext-2007
diff APOPtools/apoppasswd @ 1:28a55bc1110c
[mq]: imapext
author | yuuji@gentei.org |
---|---|
date | Mon, 14 Sep 2009 19:23:11 +0900 |
parents | |
children | d741b3ecc917 |
line diff
1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/APOPtools/apoppasswd Mon Sep 14 19:23:11 2009 +0900 1.3 @@ -0,0 +1,210 @@ 1.4 +#!/usr/local/bin/perl 1.5 +# Customize these variables. 1.6 +# If you change APOPFILEBASE, change the same variable in apopcall.c too. 1.7 +# See http://www.gentei.org/~yuuji/software/imapext/ 1.8 + 1.9 +$HOME=$ENV{"HOME"}; 1.10 + 1.11 +$DEFAULTMAILDIR = "Maildir"; # Must be same as ../src/osdep/unix/Makefile 1.12 + 1.13 +$APOPFILEBASE = ".apop"; # "$HOME/$APOPFILEBASE" is the password file 1.14 +# $APOPFILEBASE = "$DEFAULTMAILDIR/apop"; 1.15 +# $APOPFILEBASE = "Mail/apop"; 1.16 + 1.17 +$ENCODER = "cat"; 1.18 +# $ENCODER = "gzip"; 1.19 +# $ENCODER = "uuencode $$|gzip"; 1.20 + 1.21 +$DECODER = "cat"; 1.22 +# $DECODER = "gzip -dc"; 1.23 +# $DECODER = "gzip -dc | uudecode"; 1.24 + 1.25 +$DOTQMAIL = ".qmail"; # qmail 1.26 +# $DOTQMAIL = ".forward"; # Postfix 1.27 + 1.28 +$XADDR_DELIM = "-"; # qmail 1.29 +# $XADDR_DELIM = "+"; # Postfix 1.30 + 1.31 +$HERE = "."; # qmail 1.32 +# $HERE = "~"; # Postfix 1.33 + 1.34 +$EXT = ""; 1.35 +$force = 0; 1.36 +$base = 0; 1.37 + 1.38 +$APOPFILE = "$HOME/$APOPFILEBASE"; 1.39 + 1.40 +sub handler { 1.41 + system "stty echo"; 1.42 + print STDERR "Abort:\n"; 1.43 + exit 1; 1.44 +} 1.45 + 1.46 +$SIG{'INT'} = $SIG{'KILL'} = $SIG{'QUIT'} = $SIG{'HUP'} = 'handler'; 1.47 + 1.48 +while ($_=$ARGV[0], /^-.+/ && shift) { 1.49 + if (/^-e/) { 1.50 + $APOPFILE .= $XADDR_DELIM . ($EXT=shift); 1.51 + } elsif (/^-b/) { 1.52 + $base++; 1.53 + } elsif (/^-c/) { 1.54 + $create++; 1.55 + } elsif (/^-s/) { 1.56 + $stream++; 1.57 + # and exit; 1.58 + } elsif (/^-h/) { 1.59 + &usage; # and exit 1.60 + } 1.61 +} 1.62 + 1.63 +sub checkmaildir { 1.64 + local($dotqmail) = ("$HOME/$DOTQMAIL"); 1.65 + local($maildir) = ($DEFAULTMAILDIR); # default 1.66 + $dotqmail .= "$XADDR_DELIM$EXT" if $EXT; 1.67 + $maildir .= "-$EXT" if $EXT; 1.68 + unless (-f "$dotqmail") { 1.69 + if ($create) { 1.70 + if (open(DQMAIL, "> $dotqmail")) { 1.71 + print DQMAIL "$HERE/$maildir/\n"; 1.72 + print "File [$dotqmail] created\n"; 1.73 + close(DQMAIL); 1.74 + } 1.75 + } else { 1.76 + print "$dotqmail file does not exist.\n"; # should go to stdout 1.77 + print "Your should create $maildir first!\n"; 1.78 + print "(-c option automatically makes it)\n"; 1.79 + exit 1; 1.80 + } 1.81 + } 1.82 + if (-s $dotqmail) { 1.83 + $maildir=''; 1.84 + if (open(DQMAIL, "< $dotqmail")) { 1.85 + while (<DQMAIL>) { 1.86 + s/[\r\n \t]*$//g; 1.87 + next if /#/; 1.88 + next unless m,\./.*/,; 1.89 + chop; # strip trailing "/" 1.90 + $maildir = $_; 1.91 + last; 1.92 + } 1.93 + close(DQMAIL); 1.94 + $maildir = $DEFAULTMAILDIR if $maildir eq ''; 1.95 + unless (-d "$HOME/$maildir" 1.96 + && -d "$HOME/$maildir/new" 1.97 + && -d "$HOME/$maildir/cur" 1.98 + && -d "$HOME/$maildir/tmp") { 1.99 + if ($create) { 1.100 + mkdir "$HOME/$maildir", 0700; 1.101 + mkdir "$HOME/$maildir/new", 0700; 1.102 + mkdir "$HOME/$maildir/cur", 0700; 1.103 + mkdir "$HOME/$maildir/tmp", 0700; 1.104 + print "Maildir [$maildir/] created\n"; 1.105 + } else { 1.106 + print "Maildir($maildir) does not exist\n"; 1.107 + print "Your should do maildirmake $maildir first!\n"; 1.108 + print "(-c option automatically makes it)\n"; 1.109 + exit 1; 1.110 + } 1.111 + } 1.112 + } 1.113 + } 1.114 +} 1.115 + 1.116 +sub usage { 1.117 + local($mydir, $myname) = ($0 =~ m,(.*)/(.*),); 1.118 + print<<_EOU_; 1.119 +$myname Change Mail password for imap-4.7+qmailapop 1.120 +Usage: $myname [options] 1.121 +Options are... 1.122 + -e EXT Set target email address to "user-EXT" 1.123 + -c If no .qmail file and Maildir, create them 1.124 + 1.125 +_EOU_ 1.126 + exit 0; 1.127 +} 1.128 + 1.129 +if ($stream) { 1.130 + &stream; 1.131 + exit; # not reached 1.132 +} 1.133 +$OK=0; 1.134 +until ($OK) { 1.135 + system "stty -echo"; 1.136 + print STDERR "Enter APOP Password: "; 1.137 + $new1 = <>; 1.138 + print STDERR "\n"; 1.139 + if (length($new1) == 1) { 1.140 + print STDERR "Canceled\n"; 1.141 + exit 1; 1.142 + } elsif (length($new1) < 9) { 1.143 + print STDERR "Password is too short! Please use more than 8 chars.\n"; 1.144 + next; 1.145 + } 1.146 + print STDERR "Again APOP Password: "; 1.147 + $new2 = <>; 1.148 + if ($new1 eq $new2) { 1.149 + $OK=1; 1.150 + } else { 1.151 + print STDERR "\nPassword mismatch! Try again.\n"; 1.152 + } 1.153 +} 1.154 +#OK 1.155 +&checkmaildir; 1.156 +system "stty echo"; 1.157 +open(NP, "| $ENCODER > $APOPFILE") || die "Cannot write on $APOPFILE\n"; 1.158 +print NP "$new1"; 1.159 +close(NP); 1.160 +chmod 0600, $APOPFILE; 1.161 +print STDERR "\nUpdated APOP password successfully.\n"; 1.162 + 1.163 +sub stream { # Must match with old password 1.164 + local($PASS, $old, $new1, $new2, $master) = (0); 1.165 + local($masterfile) = ($APOPFILE); 1.166 + $masterfile = "$HOME/$APOPFILEBASE" if $base; 1.167 + exit 1 if ($> == 0); 1.168 + while (<>) { 1.169 + chop; 1.170 + if (/^PASS (.*)$/i) { 1.171 + $old = $1; 1.172 + } elsif (/^NEW (.*)/i) { 1.173 + $new1 = $1; 1.174 + } elsif (/^NEW2 (.*)/i) { 1.175 + $new2 = $1; 1.176 + } 1.177 + last if ("$new1" ne "" && "$new2" ne ""); 1.178 + } 1.179 + if (-s $APOPFILE || ($base && -f $masterfile)) { # Already exist 1.180 + if (open(OLD, "$DECODER $masterfile |")) { 1.181 + ($master = <OLD>) =~ s/[\n\r]$//g; 1.182 + close(OLD); 1.183 + } else { 1.184 + print "Old password file corrupted.\n"; 1.185 + print "Please ask to administrator.\n"; 1.186 + exit 1; 1.187 + } 1.188 + if ($master ne $old) { 1.189 + print "Illegal password\nBye\n"; 1.190 + exit 1; 1.191 + } 1.192 + } 1.193 + if ($new1 ne $new2) { 1.194 + print "Password(new) mismatch\nBye\n"; 1.195 + exit 1; 1.196 + } 1.197 + # OK, now begin to create! 1.198 + &checkmaildir; 1.199 + if (open(P, "| $ENCODER > $APOPFILE")) { 1.200 + # open success 1.201 + print P "$new1\n"; 1.202 + close(P); 1.203 + chmod 0600, $APOPFILE; 1.204 + if (-s $APOPFILE) { 1.205 + print "Success!\n"; 1.206 + exit 0; 1.207 + } 1.208 + } else { 1.209 + print "Cannot output to $APOPFILE\nBye\n"; 1.210 + exit 1; 1.211 + } 1.212 + exit 0; 1.213 +}