| 1 | package Mail::POP3::Security::User; | ||
| 2 | |||
| 3 | use strict; | ||
| 4 | |||
| 5 | =head2 check_password_pam | ||
| 6 | |||
| 7 | Class method. Params: | ||
| 8 | |||
| 9 | =over | ||
| 10 | |||
| 11 | =item $config | ||
| 12 | |||
| 13 | =item $user_name | ||
| 14 | |||
| 15 | =item $fqdn | ||
| 16 | |||
| 17 | =item $supplied_password | ||
| 18 | |||
| 19 | =back | ||
| 20 | |||
| 21 | =cut | ||
| 22 | |||
| 23 | sub check_password_pam { | ||
| 24 | my ($class, $config, $user_name, $fqdn, $supplied_password) = @_; | ||
| 25 | require Authen::PAM; | ||
| 26 | my $pamh = Authen::PAM->new( | ||
| 27 | $config->{mpopd_pam_service}, | ||
| 28 | $user_name, | ||
| 29 | sub { | ||
| 30 | my @res; | ||
| 31 | while ( @_ ) { | ||
| 32 | my $code = shift; | ||
| 33 | my $msg = shift; | ||
| 34 | my $ans = ""; | ||
| 35 | $ans = $user_name if $code == PAM_PROMPT_ECHO_ON(); | ||
| 36 | $ans = $supplied_password if $code == PAM_PROMPT_ECHO_OFF(); | ||
| 37 | push @res, (PAM_SUCCESS(),$ans); | ||
| 38 | } | ||
| 39 | push @res, PAM_SUCCESS(); | ||
| 40 | return @res; | ||
| 41 | }, | ||
| 42 | ) || die "Error during PAM init"; | ||
| 43 | return $pamh->pam_authenticate == 0; | ||
| 44 | } | ||
| 45 | |||
| 46 | =head2 check_password_system_md5_nopam | ||
| 47 | |||
| 48 | See L</check_password_pam> for params. | ||
| 49 | |||
| 50 | =cut | ||
| 51 | |||
| 52 | sub check_password_system_md5_nopam { | ||
| 53 | my ($class, $config, $user_name, $fqdn, $supplied_password) = @_; | ||
| 54 | my $crypted = (getpwnam $user_name)[1]; | ||
| 55 | my ($salt) = $crypted =~ /^\$1\$(.{8})\$/; | ||
| 56 | return unix_md5_crypt($supplied_password, $salt) eq $crypted; | ||
| 57 | } | ||
| 58 | |||
| 59 | =head2 check_user_system | ||
| 60 | |||
| 61 | =over | ||
| 62 | |||
| 63 | =item $config | ||
| 64 | |||
| 65 | =item $user_name | ||
| 66 | |||
| 67 | =item $fqdn | ||
| 68 | |||
| 69 | =back | ||
| 70 | |||
| 71 | =cut | ||
| 72 | |||
| 73 | sub check_user_system { | ||
| 74 | my ($class, $config, $user_name, $fqdn) = @_; | ||
| 75 | () = getpwnam $user_name; | ||
| 76 | } | ||
| 77 | |||
| 78 | =head2 check_password_system | ||
| 79 | |||
| 80 | See L</check_password_pam> for params. | ||
| 81 | |||
| 82 | =cut | ||
| 83 | |||
| 84 | sub check_password_system { | ||
| 85 | my ($class, $config, $user_name, $fqdn, $supplied_password) = @_; | ||
| 86 | my $crypted = (getpwnam $user_name)[1]; | ||
| 87 | if (defined($config->{shadow})) { | ||
| 88 | ($crypted) = $class->get_user_from_file($user_name, $config->{shadow}); | ||
| 89 | } | ||
| 90 | crypt($supplied_password, $crypted) eq $crypted; | ||
| 91 | } | ||
| 92 | |||
| 93 | =head2 get_userid_system | ||
| 94 | |||
| 95 | =over | ||
| 96 | |||
| 97 | =item $user_name | ||
| 98 | |||
| 99 | =back | ||
| 100 | |||
| 101 | =cut | ||
| 102 | |||
| 103 | sub get_userid_system { | ||
| 104 | my ($class, $user_name) = @_; | ||
| 105 | scalar getpwnam $user_name; | ||
| 106 | } | ||
| 107 | |||
| 108 | =head2 check_user_vdomain | ||
| 109 | |||
| 110 | Read a non-system password file for domain-name hashed | ||
| 111 | mail boxes. Format: username:password:uid, e.g. | ||
| 112 | |||
| 113 | markjt:$1$d56geIhf$agr7nng92bgf32:100 | ||
| 114 | |||
| 115 | The uid should correspond to the system 'mail' user or | ||
| 116 | a special 'mpopd' system user in /etc/passwd | ||
| 117 | |||
| 118 | Class method. Params: | ||
| 119 | |||
| 120 | =over | ||
| 121 | |||
| 122 | =item $config | ||
| 123 | |||
| 124 | =item $user_name | ||
| 125 | |||
| 126 | =item $fqdn | ||
| 127 | |||
| 128 | =back | ||
| 129 | |||
| 130 | =cut | ||
| 131 | |||
| 132 | =head2 check_user_vdomain | ||
| 133 | |||
| 134 | See L</check_user_system> for params. | ||
| 135 | |||
| 136 | =cut | ||
| 137 | |||
| 138 | sub check_user_vdomain { | ||
| 139 | my ($class, $config, $user_name, $fqdn) = @_; | ||
| 140 | my ($initial) = $fqdn =~ /^(.)/; | ||
| 141 | my $vdomain_dir = "$config->{host_mail_path}/$initial/$fqdn"; | ||
| 142 | () = $class->get_user_from_file( | ||
| 143 | $user_name, | ||
| 144 | "$vdomain_dir/$config->{userlist}", | ||
| 145 | ); | ||
| 146 | } | ||
| 147 | |||
| 148 | =head2 check_password_vdomain | ||
| 149 | |||
| 150 | See L</check_password_system> for params. | ||
| 151 | |||
| 152 | =cut | ||
| 153 | |||
| 154 | sub check_password_vdomain { | ||
| 155 | my ($class, $config, $user_name, $fqdn, $supplied_password) = @_; | ||
| 156 | my ($initial) = $fqdn =~ /^(.)/; | ||
| 157 | my $vdomain_dir = "$config->{host_mail_path}/$initial/$fqdn"; | ||
| 158 | my ($crypted, $user_id) = $class->get_user_from_file( | ||
| 159 | $user_name, | ||
| 160 | "$vdomain_dir/$config->{userlist}", | ||
| 161 | ); | ||
| 162 | crypt($supplied_password, $crypted) eq $crypted; | ||
| 163 | } | ||
| 164 | |||
| 165 | =head2 get_userid_vdomain | ||
| 166 | |||
| 167 | See L</get_userid_system> for params. | ||
| 168 | |||
| 169 | =cut | ||
| 170 | |||
| 171 | sub get_userid_vdomain { | ||
| 172 | my ($class, $config, $user_name, $fqdn, $supplied_password) = @_; | ||
| 173 | my ($initial) = $fqdn =~ /^(.)/; | ||
| 174 | my $vdomain_dir = "$config->{host_mail_path}/$initial/$fqdn"; | ||
| 175 | my ($crypted, $user_id) = $class->get_user_from_file( | ||
| 176 | $user_name, | ||
| 177 | "$vdomain_dir/$config->{userlist}", | ||
| 178 | ); | ||
| 179 | $user_id; | ||
| 180 | } | ||
| 181 | |||
| 182 | =head2 get_user_from_file | ||
| 183 | |||
| 184 | Get the user's crypt/crypt_MD5 password and numeric uid. | ||
| 185 | Class method. Params: | ||
| 186 | |||
| 187 | =over | ||
| 188 | |||
| 189 | =item $user_name | ||
| 190 | |||
| 191 | =item $user_file | ||
| 192 | |||
| 193 | =back | ||
| 194 | |||
| 195 | =cut | ||
| 196 | |||
| 197 | sub get_user_from_file { | ||
| 198 | my ($class, $user_name, $user_file) = @_; | ||
| 199 | my ($crypted, $user_id); | ||
| 200 | local *FH; | ||
| 201 | open FH, $user_file or $class->force_shutdown("Could not open $user_file"); | ||
| 202 | while (<FH>) { | ||
| 203 | if (/^$user_name:(.+?):(\d+):/) { | ||
| 204 | $crypted = $1; | ||
| 205 | $user_id = $2; | ||
| 206 | last; | ||
| 207 | } | ||
| 208 | } | ||
| 209 | close FH; | ||
| 210 | return unless defined $crypted; | ||
| 211 | ($crypted, $user_id); | ||
| 212 | } | ||
| 213 | |||
| 214 | 1; |