1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
|
# -*-Perl-*-
################################################################
###
### GetPass.pm
###
### Author: Internet Message Group <img@mew.org>
### Created: Apr 30, 1997
### Revised: Feb 28, 2000
###
my $PM_VERSION = "IM::GetPass.pm version 20000228(IM140)";
package IM::GetPass;
require 5.003;
require Exporter;
use IM::Config;
use IM::Util;
use integer;
use strict;
use vars qw(@ISA @EXPORT);
@ISA = qw(Exporter);
@EXPORT = qw(getpass getpass_interact
loadpass savepass connect_agent talk_agent findpass);
=head1 NAME
GetPass - Get password from tty or ...
=head1 SYNOPSIS
($pass, $agtfound, $interact) = getpass('imap', $auth, $host, $user);
=head1 DESCRIPTION
=cut
sub getpass ($$$$) {
my($proto, $auth, $host, $user) = @_;
my $pass = '';
my $agtfound = 0;
my $interact = 0;
if (&usepwagent()) {
$pass = &loadpass($proto, $auth, $host, $user);
$agtfound = 1 if ($pass ne '');
}
if ($pass eq '' && &usepwfiles()) {
$pass = &findpass($proto, $auth, $host, $user);
}
my $prompt = lc("$proto/$auth:$user\@$host");
if ($pass eq '') {
$pass = &getpass_interact("Password ($prompt): ");
$interact = 1;
}
return ($pass, $agtfound, $interact);
}
sub getpass_interact ($) {
my ($prompt) = @_;
my ($secret, $termios, $c_lflag);
if (! -t STDIN) {
# stty is not effective for Mule since it's not terminal base.
# Anyway, Mew never echos back even if getpass echos back.
} elsif (eval 'require POSIX' & ! win95p() ) {
import POSIX qw(termios_h);
$termios = new POSIX::Termios;
$termios->getattr(fileno(STDIN));
$c_lflag = $termios->getlflag;
$termios->setlflag($c_lflag & ~&POSIX::ECHO);
$termios->setattr(fileno(STDIN), &POSIX::TCSANOW);
} elsif (unixp()) { # non-POSIX-ish UNIX.
# stty might be available.
my ($OldPath) = $ENV{'PATH'}; # for SUID version
$ENV{'PATH'} = '/bin:/usr/bin';
system('/bin/stty -echo'); # Ignore errors.
$ENV{'PATH'} = $OldPath;
}
# POSIX doesn't exist for Win95, sigh.
print STDERR $prompt;
flush('STDERR');
chomp($secret = <STDIN>);
print STDERR "\n";
flush('STDERR');
if (! -t STDIN) {
# no operation
} elsif (defined $termios) { # POSIX-ish
$termios->setlflag($c_lflag);
$termios->setattr(fileno(STDIN), &POSIX::TCSANOW);
} elsif (unixp()) { # non-POSIX-ish UNIX.
my ($OldPath) = $ENV{'PATH'}; # for SUID version
$ENV{'PATH'} = '/bin:/usr/bin';
system('/bin/stty echo'); # Ignore errors.
$ENV{'PATH'} = $OldPath;
}
return $secret;
}
sub loadpass ($$$$) {
my ($proto, $auth, $path, $user) = @_;
local ($_);
my $key = &connect_agent(0);
return '' if ($key eq '');
my @keys = unpack('C*', $key);
my $pass = &talk_agent("LOAD\t$proto\t$auth\t$path\t$user\n");
if ($pass =~ /^PASS\t(.*)/) {
my @tmp1 = unpack('C*', pack('H*', $1));
my $sum1 = $keys[0];
foreach (@tmp1) {
$sum1 += $keys[1];
my $tmp2 = $_;
$_ -= $sum1;
$_ &= 0xff;
$sum1 = $tmp2;
}
return pack('C*', @tmp1);
} else {
return '';
}
}
sub savepass ($$$$$) {
my ($proto, $auth, $path, $user, $pass) = @_;
local ($_);
my $key = &connect_agent(0);
return '' if ($key eq '');
my @keys = unpack('C*', $key);
my @tmp1 = unpack('C*', $pass);
my $sum1 = $keys[0];
foreach (@tmp1) {
$sum1 += $_ + $keys[1];
$sum1 &= 0xff;
$_ = $sum1;
}
$pass = unpack('H*', pack('C*', @tmp1));
&talk_agent("SAVE\t$proto\t$auth\t$path\t$user\nPASS\t$pass\n", 0);
}
sub connect_agent ($) {
my ($surpresserror) = shift;
require Socket && import Socket;
my $realuser = im_getlogin();
unless ($realuser) {
im_warn("pwagent: can not get login name\n") unless ($surpresserror);
return '';
}
my $dir = "/tmp/im-$realuser";
my $port = &pwagentport();
if ($port > 0) {
unless (socket(SOCK, &AF_INET, &SOCK_STREAM, 0)) {
im_warn("pwagent: socket: $!\n") unless ($surpresserror);
return '';
}
my $sin = sockaddr_in($port, inet_aton('127.0.0.1'));
unless (connect(SOCK, $sin)) {
im_warn("pwagent: connect: $!\n") unless ($surpresserror);
return '';
}
} else {
my $name = "$dir/pw";
unless (-S $name) {
im_warn("pwagent: can not access to socket: $name\n")
unless ($surpresserror);
return '';
}
my ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev) = stat($dir);
if ($mode & 0077) {
im_warn("pwagent: invalid mode: $dir\n") unless ($surpresserror);
return '';
}
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev) = stat($name);
if ($mode & 0077) {
im_warn("pwagent: invalid mode: $name\n") unless ($surpresserror);
return '';
}
unless (socket(SOCK, &AF_UNIX, &SOCK_STREAM, 0)) {
im_warn("pwagent: socket: $!\n") unless ($surpresserror);
return '';
}
my $sun = sockaddr_un($name);
unless (connect(SOCK, $sun)) {
im_warn("pwagent: connect: $!\n") unless ($surpresserror);
return '';
}
}
select(SOCK); $| = 1; select(STDOUT);
my $res = <SOCK>;
chomp($res);
return $res;
}
sub talk_agent ($) {
my ($msg) = shift;
print SOCK $msg;
my $res = <SOCK>;
shutdown (SOCK, 2);
close(SOCK);
chomp($res);
return $res;
}
sub findpass($$$$) {
my ($proto, $auth, $host, $user) = @_;
local ($_);
my ($passfile);
foreach $passfile (split(',', &pwfiles())) {
$passfile = &expand_path($passfile);
next unless (open (PASSFILE, "<$passfile"));
while (<PASSFILE>) {
chomp;
next if (/^(#.*)?$/);
# s/\s+(\#.*)?$//; # remove comments
if (/^(\S+)\s+(\S+)\s+(\S+)\s+(\S.+)$/) {
my ($tmp_host, $tmp_user, $tmp_pass) = ($2, $3, $4);
my ($tmp_proto, $tmp_auth) = split('/', $1);
if (($tmp_proto eq $proto)
&& ($tmp_auth eq $auth)
&& ($tmp_host eq $host)
&& ($tmp_user eq $user)) {
close (PASSFILE);
return $tmp_pass;
}
}
}
close (PASSFILE);
}
return '';
}
1;
### Copyright (C) 1997, 1998, 1999 IM developing team
### All rights reserved.
###
### Redistribution and use in source and binary forms, with or without
### modification, are permitted provided that the following conditions
### are met:
###
### 1. Redistributions of source code must retain the above copyright
### notice, this list of conditions and the following disclaimer.
### 2. Redistributions in binary form must reproduce the above copyright
### notice, this list of conditions and the following disclaimer in the
### documentation and/or other materials provided with the distribution.
### 3. Neither the name of the team nor the names of its contributors
### may be used to endorse or promote products derived from this software
### without specific prior written permission.
###
### THIS SOFTWARE IS PROVIDED BY THE TEAM AND CONTRIBUTORS ``AS IS'' AND
### ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
### IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
### PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE TEAM OR CONTRIBUTORS BE
### LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
### CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
### SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
### BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
### WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
### OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
### IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|