imapext-2007

diff APOPtools/apoppasswd @ 4:d741b3ecc917

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

UW-IMAP'd extensions by yuuji