imapext-2007

view APOPtools/apoppasswd @ 1:28a55bc1110c

[mq]: imapext
author yuuji@gentei.org
date Mon, 14 Sep 2009 19:23:11 +0900
parents
children d741b3ecc917
line source
1 #!/usr/local/bin/perl
2 # Customize these variables.
3 # If you change APOPFILEBASE, change the same variable in apopcall.c too.
4 # See http://www.gentei.org/~yuuji/software/imapext/
6 $HOME=$ENV{"HOME"};
8 $DEFAULTMAILDIR = "Maildir"; # Must be same as ../src/osdep/unix/Makefile
10 $APOPFILEBASE = ".apop"; # "$HOME/$APOPFILEBASE" is the password file
11 # $APOPFILEBASE = "$DEFAULTMAILDIR/apop";
12 # $APOPFILEBASE = "Mail/apop";
14 $ENCODER = "cat";
15 # $ENCODER = "gzip";
16 # $ENCODER = "uuencode $$|gzip";
18 $DECODER = "cat";
19 # $DECODER = "gzip -dc";
20 # $DECODER = "gzip -dc | uudecode";
22 $DOTQMAIL = ".qmail"; # qmail
23 # $DOTQMAIL = ".forward"; # Postfix
25 $XADDR_DELIM = "-"; # qmail
26 # $XADDR_DELIM = "+"; # Postfix
28 $HERE = "."; # qmail
29 # $HERE = "~"; # Postfix
31 $EXT = "";
32 $force = 0;
33 $base = 0;
35 $APOPFILE = "$HOME/$APOPFILEBASE";
37 sub handler {
38 system "stty echo";
39 print STDERR "Abort:\n";
40 exit 1;
41 }
43 $SIG{'INT'} = $SIG{'KILL'} = $SIG{'QUIT'} = $SIG{'HUP'} = 'handler';
45 while ($_=$ARGV[0], /^-.+/ && shift) {
46 if (/^-e/) {
47 $APOPFILE .= $XADDR_DELIM . ($EXT=shift);
48 } elsif (/^-b/) {
49 $base++;
50 } elsif (/^-c/) {
51 $create++;
52 } elsif (/^-s/) {
53 $stream++;
54 # and exit;
55 } elsif (/^-h/) {
56 &usage; # and exit
57 }
58 }
60 sub checkmaildir {
61 local($dotqmail) = ("$HOME/$DOTQMAIL");
62 local($maildir) = ($DEFAULTMAILDIR); # default
63 $dotqmail .= "$XADDR_DELIM$EXT" if $EXT;
64 $maildir .= "-$EXT" if $EXT;
65 unless (-f "$dotqmail") {
66 if ($create) {
67 if (open(DQMAIL, "> $dotqmail")) {
68 print DQMAIL "$HERE/$maildir/\n";
69 print "File [$dotqmail] created\n";
70 close(DQMAIL);
71 }
72 } else {
73 print "$dotqmail file does not exist.\n"; # should go to stdout
74 print "Your should create $maildir first!\n";
75 print "(-c option automatically makes it)\n";
76 exit 1;
77 }
78 }
79 if (-s $dotqmail) {
80 $maildir='';
81 if (open(DQMAIL, "< $dotqmail")) {
82 while (<DQMAIL>) {
83 s/[\r\n \t]*$//g;
84 next if /#/;
85 next unless m,\./.*/,;
86 chop; # strip trailing "/"
87 $maildir = $_;
88 last;
89 }
90 close(DQMAIL);
91 $maildir = $DEFAULTMAILDIR if $maildir eq '';
92 unless (-d "$HOME/$maildir"
93 && -d "$HOME/$maildir/new"
94 && -d "$HOME/$maildir/cur"
95 && -d "$HOME/$maildir/tmp") {
96 if ($create) {
97 mkdir "$HOME/$maildir", 0700;
98 mkdir "$HOME/$maildir/new", 0700;
99 mkdir "$HOME/$maildir/cur", 0700;
100 mkdir "$HOME/$maildir/tmp", 0700;
101 print "Maildir [$maildir/] created\n";
102 } else {
103 print "Maildir($maildir) does not exist\n";
104 print "Your should do maildirmake $maildir first!\n";
105 print "(-c option automatically makes it)\n";
106 exit 1;
107 }
108 }
109 }
110 }
111 }
113 sub usage {
114 local($mydir, $myname) = ($0 =~ m,(.*)/(.*),);
115 print<<_EOU_;
116 $myname Change Mail password for imap-4.7+qmailapop
117 Usage: $myname [options]
118 Options are...
119 -e EXT Set target email address to "user-EXT"
120 -c If no .qmail file and Maildir, create them
122 _EOU_
123 exit 0;
124 }
126 if ($stream) {
127 &stream;
128 exit; # not reached
129 }
130 $OK=0;
131 until ($OK) {
132 system "stty -echo";
133 print STDERR "Enter APOP Password: ";
134 $new1 = <>;
135 print STDERR "\n";
136 if (length($new1) == 1) {
137 print STDERR "Canceled\n";
138 exit 1;
139 } elsif (length($new1) < 9) {
140 print STDERR "Password is too short! Please use more than 8 chars.\n";
141 next;
142 }
143 print STDERR "Again APOP Password: ";
144 $new2 = <>;
145 if ($new1 eq $new2) {
146 $OK=1;
147 } else {
148 print STDERR "\nPassword mismatch! Try again.\n";
149 }
150 }
151 #OK
152 &checkmaildir;
153 system "stty echo";
154 open(NP, "| $ENCODER > $APOPFILE") || die "Cannot write on $APOPFILE\n";
155 print NP "$new1";
156 close(NP);
157 chmod 0600, $APOPFILE;
158 print STDERR "\nUpdated APOP password successfully.\n";
160 sub stream { # Must match with old password
161 local($PASS, $old, $new1, $new2, $master) = (0);
162 local($masterfile) = ($APOPFILE);
163 $masterfile = "$HOME/$APOPFILEBASE" if $base;
164 exit 1 if ($> == 0);
165 while (<>) {
166 chop;
167 if (/^PASS (.*)$/i) {
168 $old = $1;
169 } elsif (/^NEW (.*)/i) {
170 $new1 = $1;
171 } elsif (/^NEW2 (.*)/i) {
172 $new2 = $1;
173 }
174 last if ("$new1" ne "" && "$new2" ne "");
175 }
176 if (-s $APOPFILE || ($base && -f $masterfile)) { # Already exist
177 if (open(OLD, "$DECODER $masterfile |")) {
178 ($master = <OLD>) =~ s/[\n\r]$//g;
179 close(OLD);
180 } else {
181 print "Old password file corrupted.\n";
182 print "Please ask to administrator.\n";
183 exit 1;
184 }
185 if ($master ne $old) {
186 print "Illegal password\nBye\n";
187 exit 1;
188 }
189 }
190 if ($new1 ne $new2) {
191 print "Password(new) mismatch\nBye\n";
192 exit 1;
193 }
194 # OK, now begin to create!
195 &checkmaildir;
196 if (open(P, "| $ENCODER > $APOPFILE")) {
197 # open success
198 print P "$new1\n";
199 close(P);
200 chmod 0600, $APOPFILE;
201 if (-s $APOPFILE) {
202 print "Success!\n";
203 exit 0;
204 }
205 } else {
206 print "Cannot output to $APOPFILE\nBye\n";
207 exit 1;
208 }
209 exit 0;
210 }

UW-IMAP'd extensions by yuuji