Subversion Repositories general

Compare Revisions

Regard whitespace Rev 1081 → Rev 1082

/hostadmiral/trunk/src/ak/hostadmiral/listener/tcp/TcpListener.java
6,9 → 6,13
import java.io.Writer;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.Socket;
import ak.hostadmiral.util.ModelException;
import ak.hostadmiral.util.ConfigInit;
import ak.hostadmiral.util.ConfigUtils;
import ak.hostadmiral.core.model.*;
 
import org.apache.log4j.Logger;
36,16 → 40,25
 
private static String hostname;
private static int port;
private static String password;
protected static Object lock = new Object();
 
public static final String PROTOCOL_NAME = "HostAdmiral_TcpListener";
public static final String PROTOCOL_VERSION = "0.1";
public static final String PROTOCOL_VER_MAJ = "1";
public static final String PROTOCOL_VER_MIN = "0";
public static final String PASSWORD_DIGEST = "crypt";
 
public void init(Map params)
throws ModelException
{
setHostname(((String[])params.get("hostname"))[0]);
setPort(Integer.parseInt(((String[])params.get("port"))[0]));
ConfigUtils cfg = new ConfigUtils(params);
 
// save params
setHostname(cfg.getString("hostname", "127.0.0.1", true, false));
setPort(cfg.getInteger("port", null, false).intValue());
setPassword(cfg.getString("password", null, true, true));
 
// register listeners
UserManager.getInstance().addCreatedListener(this);
UserManager.getInstance().addModifiedListener(this);
UserManager.getInstance().addDeletedListener(this);
87,11 → 100,16
port = port_;
}
 
public static void setPassword(String password_)
{
password = password_;
}
 
protected static String escape(String s)
{
// FIXME: any other problem characters? optimize it?
s = s.replaceAll("\\\\", "\\\\\\\\");
s = s.replaceAll("\0", "\\\\0");
s = s.replaceAll("\\\\", "\\\\\\\\");
s = s.replaceAll("\t", "\\\\t");
s = s.replaceAll("\n", "\\\\n");
s = s.replaceAll("\r", "\\\\r");
104,21 → 122,72
synchronized(lock) {
try {
Socket socket = new Socket(hostname, port);
Writer out = new BufferedWriter(
new OutputStreamWriter(socket.getOutputStream()));
 
if(PROTOCOL_NAME != null) {
out.write(PROTOCOL_NAME);
out.write(" ");
// send request
Writer out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
 
out.write(PROTOCOL_NAME + " " + PROTOCOL_VER_MAJ + "." + PROTOCOL_VER_MIN + "\n");
if(password != null) {
out.write("password=" + password + "\n");
}
if(PROTOCOL_VERSION != null) {
out.write(PROTOCOL_VERSION);
out.write("\n");
}
out.write(message);
out.write("\n\n");
out.close();
out.flush();
socket.shutdownOutput();
 
// get response
BufferedReader in = new BufferedReader(
new InputStreamReader(socket.getInputStream()));
String line;
boolean headerDone = false;
boolean codeDone = false;
while((line = in.readLine()) != null) {
if(!headerDone) {
if(!line.matches("^" + PROTOCOL_NAME + " " + PROTOCOL_VER_MAJ + "\\.\\d+$"))
throw new ModelException(
"Wrong response: wrong header in [" + line + "]");
 
headerDone = true;
}
else if(!codeDone) {
if(line.length() < 3)
throw new ModelException(
"Wrong response: code too short [" + line + "]");
if(line.length() >= 4 && line.charAt(3) != ' ')
throw new ModelException(
"Wrong response: cannot get code from [" + line + "]");
 
int code;
try {
code = Integer.parseInt(line.substring(0, 3));
}
catch(NumberFormatException ex) {
throw new ModelException(
"Wrong response: cannot parse code from [" + line + "]");
}
 
String response = (line.length() >= 5) ? line.substring(4) : null;
 
if(code >= 200 && code < 300) {
logger.info("Response from backend [" + line + "]");
}
else if(code >= 400 && code < 600) {
throw new ModelException(
"Error from backend [" + line + "]");
}
 
codeDone = true;
}
else {
if(!line.equals(""))
throw new ModelException(
"Wrong response: no more lines expected [" + line + "]");
}
}
 
// done
socket.close();
}
catch(Exception ex) {
logger.error("Cannot send message over TCP", ex);
throw new ModelException("Cannot send message over TCP:" + ex.getMessage());
132,26 → 201,26
public void userCreated(User editor, User user)
throws ModelException
{
send("user\tcreate\t" + escape(user.getLogin()) + "\t"
+ escape(/* FIXME user.getPassword() */ "") + "\t"
+ user.getEnabled() + "\t"
+ escape(user.getComment()));
send("user\tcreate\tlogin=" + escape(user.getLogin())
+ "\tpassword=" + escape(user.getPassword(editor, PASSWORD_DIGEST))
+ "\tenabled=" + user.getEnabled()
+ "\tcomment=" + escape(user.getComment()));
}
 
public void userModified(User editor, User user, User oldUser)
throws ModelException
{
send("user\tmodify\t" + escape(oldUser.getLogin()) + "\t"
+ escape(user.getLogin()) + "\t"
+ escape(/* FIXME user.getPassword() */ "") + "\t"
+ user.getEnabled() + "\t"
+ escape(user.getComment()));
send("user\tmodify\toldLogin=" + escape(oldUser.getLogin())
+ "\tlogin=" + escape(user.getLogin())
+ "\tpassword=" + escape(user.getPassword(editor, PASSWORD_DIGEST))
+ "\tenabled=" + user.getEnabled()
+ "\tcomment=" + escape(user.getComment()));
}
 
public void userDeleted(User editor, User user)
throws ModelException
{
send("user\tdelete\t" + escape(user.getLogin()));
send("user\tdelete\tlogin=" + escape(user.getLogin()));
}
 
//=== inet domain =============================================================================
159,24 → 228,24
public void inetDomainCreated(User editor, InetDomain domain)
throws ModelException
{
send("inetDomain\tcreate\t" + escape(domain.getName()) + "\t"
+ domain.getEnabled() + "\t"
+ escape(domain.getComment()));
send("inetDomain\tcreate\tname=" + escape(domain.getName())
+ "\tenabled=" + domain.getEnabled()
+ "\tcomment=" + escape(domain.getComment()));
}
 
public void inetDomainModified(User editor, InetDomain domain, InetDomain oldDomain)
throws ModelException
{
send("inetDomain\tmodify\t" + escape(oldDomain.getName()) + "\t"
+ escape(domain.getName()) + "\t"
+ domain.getEnabled() + "\t"
+ escape(domain.getComment()));
send("inetDomain\tmodify\toldName=" + escape(oldDomain.getName())
+ "\tname=" + escape(domain.getName())
+ "\tenabled=" + domain.getEnabled()
+ "\tcomment=" + escape(domain.getComment()));
}
 
public void inetDomainDeleted(User editor, InetDomain domain)
throws ModelException
{
send("inetDomain\tdelete\t" + escape(domain.getName()));
send("inetDomain\tdelete\tname=" + escape(domain.getName()));
}
 
//=== system user =============================================================================
184,27 → 253,28
public void systemUserCreated(User editor, SystemUser systemUser)
throws ModelException
{
send("systemUser\tcreate\t" + systemUser.getUid() + "\t"
+ escape(systemUser.getName()) + "\t"
+ systemUser.getEnabled() + "\t"
+ escape(systemUser.getComment()));
send("systemUser\tcreate\tuid=" + systemUser.getUid()
+ "\tname=" + escape(systemUser.getName())
+ "\tenabled=" + systemUser.getEnabled()
+ "\tcomment=" + escape(systemUser.getComment()));
}
 
public void systemUserModified(User editor, SystemUser systemUser, SystemUser oldSystemUser)
throws ModelException
{
send("systemUser\tmodify\t" + oldSystemUser.getUid() + "\t"
+ escape(oldSystemUser.getName()) + "\t"
+ systemUser.getUid() + "\t"
+ escape(systemUser.getName()) + "\t"
+ systemUser.getEnabled() + "\t"
+ escape(systemUser.getComment()));
send("systemUser\tmodify\toldUid=" + oldSystemUser.getUid()
+ "\toldName=" + escape(oldSystemUser.getName())
+ "\tuid=" + systemUser.getUid()
+ "\tname=" + escape(systemUser.getName())
+ "\tenabled=" + systemUser.getEnabled()
+ "\tcomment=" + escape(systemUser.getComment()));
}
 
public void systemUserDeleted(User editor, SystemUser systemUser)
throws ModelException
{
send("systemUser\tdelete\t" + systemUser.getUid() + "\t" + escape(systemUser.getName()));
send("systemUser\tdelete\tuid=" + systemUser.getUid()
+ "\tname=" + escape(systemUser.getName()));
}
 
//=== mailbox =================================================================================
212,38 → 282,38
public void mailboxCreated(User editor, Mailbox mailbox)
throws ModelException
{
send("mailbox\tcreate\t" + escape(mailbox.getLogin()) + "\t"
+ escape(/* FIXME user.getPassword() */ "") + "\t"
+ escape(mailbox.getDomain().getName()) + "\t"
+ mailbox.getVirusCheck() + "\t"
+ mailbox.getSpamCheck() + "\t"
+ (mailbox.getSystemUser() == null ? "" : mailbox.getSystemUser().getUid().toString())
+ "\t"
+ mailbox.getEnabled() + "\t"
+ escape(mailbox.getComment()));
send("mailbox\tcreate\tlogin=" + escape(mailbox.getLogin())
+ "\tpassword=" + escape(mailbox.getPassword(editor, PASSWORD_DIGEST))
+ "\tdomain=" + escape(mailbox.getDomain().getName())
+ "\tvirusCheck=" + mailbox.getVirusCheck()
+ "\tspamCheck=" + mailbox.getSpamCheck()
+ "\tsystemUser=" + (mailbox.getSystemUser() == null
? "" : mailbox.getSystemUser().getUid().toString())
+ "\tenabled=" + mailbox.getEnabled()
+ "\tcomment=" + escape(mailbox.getComment()));
}
 
public void mailboxModified(User editor, Mailbox mailbox, Mailbox oldMailbox)
throws ModelException
{
send("mailbox\tmodify\t" + escape(oldMailbox.getLogin()) + "\t"
+ escape(oldMailbox.getDomain().getName()) + "\t"
+ escape(mailbox.getLogin()) + "\t"
+ escape(/* FIXME user.getPassword() */ "") + "\t"
+ escape(mailbox.getDomain().getName()) + "\t"
+ mailbox.getVirusCheck() + "\t"
+ mailbox.getSpamCheck() + "\t"
+ (mailbox.getSystemUser() == null ? "" : mailbox.getSystemUser().getUid().toString())
+ "\t"
+ mailbox.getEnabled() + "\t"
+ escape(mailbox.getComment()));
send("mailbox\tmodify\toldLogin=" + escape(oldMailbox.getLogin())
+ "\toldDomain=" + escape(oldMailbox.getDomain().getName())
+ "\tlogin=" + escape(mailbox.getLogin())
+ "\tpassword=" + escape(mailbox.getPassword(editor, PASSWORD_DIGEST))
+ "\tdomain=" + escape(mailbox.getDomain().getName())
+ "\tvirusCheck=" + mailbox.getVirusCheck()
+ "\tspamCheck=" + mailbox.getSpamCheck()
+ "\tsystemUser=" + (mailbox.getSystemUser() == null
? "" : mailbox.getSystemUser().getUid().toString())
+ "\tenabled=" + mailbox.getEnabled()
+ "\tcomment=" + escape(mailbox.getComment()));
}
 
public void mailboxDeleted(User editor, Mailbox mailbox)
throws ModelException
{
send("mailbox\tdelete\t" + escape(mailbox.getLogin()) + "\t"
+ escape(mailbox.getDomain().getName()));
send("mailbox\tdelete\tlogin=" + escape(mailbox.getLogin())
+ "\tdomain=" + escape(mailbox.getDomain().getName()));
}
 
//=== mail alias ==============================================================================
271,10 → 341,10
public void mailAliasCreated(User editor, MailAlias mailAlias)
throws ModelException
{
send(" mailAlias\tcreate\t" + escape(mailAlias.getAddress()) + "\t"
+ escape(mailAlias.getDomain().getName()) + "\t"
+ mailAlias.getEnabled() + "\t"
+ escape(mailAlias.getComment())
send("mailAlias\tcreate\taddress=" + escape(mailAlias.getAddress())
+ "\tdomain=" + escape(mailAlias.getDomain().getName())
+ "\tenabled=" + mailAlias.getEnabled()
+ "\tcomment=" + escape(mailAlias.getComment())
+ formMailAliasDestinations(editor, mailAlias));
}
 
281,12 → 351,12
public void mailAliasModified(User editor, MailAlias mailAlias, MailAlias oldMailAlias)
throws ModelException
{
send(" mailAlias\tmodify\t" + escape(oldMailAlias.getAddress()) + "\t"
+ escape(oldMailAlias.getDomain().getName()) + "\t"
+ escape(mailAlias.getAddress()) + "\t"
+ escape(mailAlias.getDomain().getName()) + "\t"
+ mailAlias.getEnabled() + "\t"
+ escape(mailAlias.getComment())
send("mailAlias\tmodify\toldAddress=" + escape(oldMailAlias.getAddress())
+ "\toldDomain=" + escape(oldMailAlias.getDomain().getName())
+ "\taddress=" + escape(mailAlias.getAddress())
+ "\tdomain=" + escape(mailAlias.getDomain().getName())
+ "\tenabled=" + mailAlias.getEnabled()
+ "\tcomment=" + escape(mailAlias.getComment())
+ formMailAliasDestinations(editor, mailAlias));
}
 
293,7 → 363,7
public void mailAliasDeleted(User editor, MailAlias mailAlias)
throws ModelException
{
send(" mailAlias\tdelete\t" + escape(mailAlias.getAddress())+ "\t"
+ escape(mailAlias.getDomain().getName()));
send("mailAlias\tdelete\taddress=" + escape(mailAlias.getAddress())
+ "\tdomain=" + escape(mailAlias.getDomain().getName()));
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/PasswordStore.java
13,6 → 13,11
public String getDigest();
 
/**
* returns true if the store is able to return the original password
*/
public boolean getReversable();
 
/**
* to store to persistent store
*/
public String getPassword();
30,4 → 35,10
 
public boolean checkPassword(String password)
throws ModelException;
 
/**
* return the password in plain text if possible
*/
public String getOriginalPassword()
throws UnsupportedOperationException;
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/Mailbox.java
90,6 → 90,26
passwords.add(ps);
}
 
public String getPassword(User editor, String digest)
throws ModelException
{
if(!editableBy(editor))
throw new ModelSecurityException();
 
for(Iterator i = passwords.iterator(); i.hasNext(); ) {
Object o = i.next();
if(!(o instanceof PasswordStore))
throw new ModelException("It's not a password store");
 
PasswordStore ps = (PasswordStore)o;
if(ps.getDigest().equals(digest)) {
return ps.getPassword();
}
}
 
throw new ModelException("Digest " + digest + " not found");
}
 
public void setPassword(User editor, String password)
throws ModelException
{
/hostadmiral/trunk/src/ak/hostadmiral/core/model/PasswordStoreAbstract.java
112,4 → 112,15
theClone.password = password;
return theClone;
}
 
public boolean getReversable()
{
return false;
}
 
public String getOriginalPassword()
throws UnsupportedOperationException
{
throw new UnsupportedOperationException("Not able to restore original password");
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/model/User.java
124,6 → 124,26
return getDefaultPasswordStore().getPassword();
}
 
public String getPassword(User editor, String digest)
throws ModelException
{
if(!partEditableBy(editor))
throw new ModelSecurityException();
 
for(Iterator i = passwords.iterator(); i.hasNext(); ) {
Object o = i.next();
if(!(o instanceof PasswordStore))
throw new ModelException("It's not a password store");
 
PasswordStore ps = (PasswordStore)o;
if(ps.getDigest().equals(digest)) {
return ps.getPassword();
}
}
 
throw new ModelException("Digest " + digest + " not found");
}
 
public void setPassword(User editor, String password)
throws ModelException
{
/hostadmiral/trunk/src/ak/hostadmiral/core/model/PasswordStorePlain.java
20,4 → 20,15
{
return password;
}
 
public boolean getReversable()
{
return true;
}
 
public String getOriginalPassword()
throws UnsupportedOperationException
{
return password;
}
}
/hostadmiral/trunk/src/ak/hostadmiral/core/config/Configurator.java
112,10 → 112,20
params.put(p.getName(), (String[])p.getValues().toArray(new String[0]));
}
 
try {
o.init(params);
}
catch(ModelException ex) {
logger.error("Cannot initialize instance of class "
+ c.getClass().getName(), ex);
}
}
else {
logger.error("Class " + c.getClass().getName()
+ " does not implement ak.hostadmiral.util.ConfigInit interface");
}
}
}
 
private ConfigRoot readConfig(InputStream in)
throws IOException, SAXException
/hostadmiral/trunk/src/ak/hostadmiral/util/ConfigUtils.java
0,0 → 1,81
package ak.hostadmiral.util;
 
import java.util.Map;
 
public class ConfigUtils
{
private Map params;
protected boolean returnDefValue;
 
public ConfigUtils(Map params)
{
this.params = params;
}
 
public String getString(String key, String defValue,
boolean optional, boolean mayBeEmpty)
throws ModelException
{
String s = (String)getObject(key, defValue, optional);
 
if(returnDefValue) return s;
 
if(!mayBeEmpty && s.equals("")) {
throw new ModelException("Configuration parameter '" + key + "' may not be empty");
}
 
return s;
}
 
public Integer getInteger(String key, Integer defValue,
boolean optional)
throws ModelException
{
Object value = getObject(key, defValue, optional);
 
if(returnDefValue) return defValue;
 
return new Integer((String)value);
}
 
protected synchronized Object getObject(String key, Object defValue, boolean optional)
throws ModelException
{
returnDefValue = false;
 
Object obj = params.get(key);
 
if(obj == null) {
if(optional) {
returnDefValue = true;
return defValue;
}
else
throw new ModelException("Configuration parameter '" + key + "' must be present");
}
 
if(!(obj instanceof String[])) {
throw new ModelException("Configuration parameter '" + key
+ "' expected to be an array of strings");
}
 
String[] sa = (String[])obj;
 
if(sa.length == 0) {
if(optional) {
returnDefValue = true;
return defValue;
}
else
throw new ModelException("Value of configuration parameter '"
+ key + "' must be present");
}
 
if(sa.length > 1) {
throw new ModelException("Value of configuration parameter '"
+ key + "' may not be an array");
}
 
return sa[0];
}
}
/hostadmiral/trunk/src/ak/hostadmiral/util/ConfigInit.java
6,6 → 6,7
{
/**
* This method is called by initialization from config file.
* FIMXE give more powerful structure, not just a map
*
* @param params map String -> String[] with pairs of param name -> values
* from the initializaion file