01: /*
02: *
03: * Jsmtpd, Java SMTP daemon
04: * Copyright (C) 2005 Jean-Francois POUX, jf.poux@laposte.net
05: *
06: * This program is free software; you can redistribute it and/or
07: * modify it under the terms of the GNU General Public License
08: * as published by the Free Software Foundation; either version 2
09: * of the License, or (at your option) any later version.
10: *
11: * This program is distributed in the hope that it will be useful,
12: * but WITHOUT ANY WARRANTY; without even the implied warranty of
13: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14: * GNU General Public License for more details.
15: *
16: * You should have received a copy of the GNU General Public License
17: * along with this program; if not, write to the Free Software
18: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19: *
20: */
21: package org.jsmtpd.plugins.smtpExtension;
22:
23: import java.security.MessageDigest;
24: import java.security.NoSuchAlgorithmException;
25: import java.util.HashMap;
26: import java.util.Map;
27:
28: import org.jsmtpd.core.common.PluginInitException;
29: import org.jsmtpd.tools.Base64Helper;
30: import org.jsmtpd.tools.ByteArrayTool;
31:
32: /**
33: * @author Jean-Francois POUX
34: */
35: public class BasicSmtpAuth extends SmtpAuthenticator {
36:
37: private Map<String, byte[]> users = new HashMap<String, byte[]>();
38: private MessageDigest md;
39:
40: protected boolean performAuth(String login, byte[] password) {
41: if (!users.containsKey(login))
42: return false;
43:
44: byte[] hash = (byte[]) users.get(login);
45:
46: return ByteArrayTool.compare(hash, md.digest(password));
47: }
48:
49: private void addPlainUser(String user, String password)
50: throws Exception {
51: MessageDigest md5 = MessageDigest.getInstance("MD5");
52: users.put(user, md5.digest(password.getBytes()));
53: }
54:
55: private void addMD5User(String user, String md5base64password) {
56: users.put(user, Base64Helper.decode(md5base64password));
57: }
58:
59: public void setPlainUser(String in) throws Exception {
60: String[] tmp = in.split(",");
61: addPlainUser(tmp[0], tmp[1]);
62: }
63:
64: public void setMD5User(String in) {
65: String[] tmp = in.split(",");
66: addMD5User(tmp[0], tmp[1]);
67: }
68:
69: @Override
70: public void initPlugin() throws PluginInitException {
71: try {
72: md = MessageDigest.getInstance("md5");
73: } catch (NoSuchAlgorithmException e) {
74: throw new PluginInitException("md5 not available");
75: }
76: super.initPlugin();
77: }
78: }
|