diff -r 80e56bfc227e -r 5d23fa316c1c java/eJabberd-auth/src/cz/frantovo/ejabberd/auth/EJabberdAuth.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/eJabberd-auth/src/cz/frantovo/ejabberd/auth/EJabberdAuth.java Mon Mar 10 12:20:26 2014 +0100 @@ -0,0 +1,209 @@ +/** + * SQL-DK + * Copyright © 2014 František Kučera (frantovo.cz) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package cz.frantovo.ejabberd.auth; + +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; +import java.io.PrintStream; +import java.sql.Connection; +import java.sql.DriverManager; +import java.sql.PreparedStatement; +import java.sql.ResultSet; +import java.sql.SQLException; +import java.util.Properties; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class EJabberdAuth { + + public static final String NASTAVENÍ_DB_URL = "db.url"; + public static final String NASTAVENÍ_DB_JMÉNO = "db.jmeno"; + public static final String NASTAVENÍ_DB_HESLO = "db.heslo"; + public static final String NASTAVENÍ_DOMÉNA = "domena"; + public static final String NASTAVENÍ_LOG = "log"; + public static final String VÝCHOZÍ_LOG = "/tmp/eJabberd-auth.log"; + private static final int ANO = 1; + private static final int NE = 0; + private final DataInputStream in; + private final DataOutputStream out; + private final PrintStream err; + private final String našeDoména; + private final String dbUrl; + private final String dbJméno; + private final String dbHeslo; + + public static void main(String[] args) throws IOException, ChybaZápisuVýsledku, ChybaČteníVstupu { + + try (FileInputStream nastaveníFis = new FileInputStream(args[0])) { + + Properties nastavení = new Properties(); + nastavení.load(nastaveníFis); + + try (FileOutputStream err = new FileOutputStream(nastavení.getProperty(NASTAVENÍ_LOG, VÝCHOZÍ_LOG))) { + EJabberdAuth a = new EJabberdAuth(System.in, System.out, err, nastavení); + a.start(); + } + } + } + + public EJabberdAuth(InputStream in, OutputStream out, OutputStream err, Properties nastavení) { + this.in = new DataInputStream(in); + this.out = new DataOutputStream(out); + this.err = new PrintStream(err); + + this.našeDoména = nastavení.getProperty(NASTAVENÍ_DOMÉNA); + this.dbUrl = nastavení.getProperty(NASTAVENÍ_DB_URL); + this.dbJméno = nastavení.getProperty(NASTAVENÍ_DB_JMÉNO); + this.dbHeslo = nastavení.getProperty(NASTAVENÍ_DB_HESLO); + } + + public void start() throws ChybaZápisuVýsledku, ChybaČteníVstupu { + while (true) { + String text = načtiText(); + zpracuj(text); + } + } + + private Connection getDB() throws SQLException { + return DriverManager.getConnection(dbUrl, dbJméno, dbHeslo); + } + + private String načtiText() throws ChybaČteníVstupu { + try { + short délka = in.readShort(); + byte[] bajty = new byte[délka]; + in.read(bajty); + return new String(bajty); + } catch (Exception e) { + throw new ChybaČteníVstupu(e); + } + } + + private void zpracuj(String text) throws ChybaZápisuVýsledku { + + String[] prvky = text.split(":"); + + final String operace; + final String uživatel; + final String doména; + final String heslo; + + if (prvky.length < 3) { + zapišVýsledek(NE); + err.println("Chybný počet parametrů: " + prvky.length); + } else { + operace = prvky[0]; + uživatel = prvky[1]; + doména = prvky[2]; + heslo = (prvky.length > 3) ? prvky[3] : null; + + err.println(operace + ":" + uživatel + ":" + doména + (heslo == null ? "" : ":*")); + + switch (operace) { + case "auth": + ověřHeslo(uživatel, doména, heslo); + break; + case "isuser": + ověřExistenci(uživatel, doména); + break; + case "setpass": + case "tryregister": + case "removeuser": + case "removeuser3": + default: + zapišVýsledek(NE); + } + } + } + + private void zapišVýsledek(int výsledek) throws ChybaZápisuVýsledku { + try { + err.println("výsledek: " + výsledek); + + out.writeShort(2); + out.writeShort(výsledek); + out.flush(); + } catch (Exception e) { + throw new ChybaZápisuVýsledku(e); + } + } + + private void ověřHeslo(String uživatel, String doména, String heslo) throws ChybaZápisuVýsledku { + if (ověřDoménu(doména)) { + + try (Connection db = getDB()) { + try (PreparedStatement ps = db.prepareStatement("SELECT over_heslo(?,?)")) { + int i = 1; + ps.setString(i++, uživatel); + ps.setString(i++, heslo); + try (ResultSet rs = ps.executeQuery()) { + rs.next(); + int výsledek = rs.getBoolean(1) ? ANO : NE; + zapišVýsledek(výsledek); + } + } + } catch (SQLException e) { + logujSQLException(e); + zapišVýsledek(NE); + } + + } else { + zapišVýsledek(NE); + } + } + + private void ověřExistenci(String uživatel, String doména) throws ChybaZápisuVýsledku { + if (ověřDoménu(doména)) { + + try (Connection db = getDB()) { + try (PreparedStatement ps = db.prepareStatement("SELECT over_existenci_uzivatele(?)")) { + int i = 1; + ps.setString(i++, uživatel); + try (ResultSet rs = ps.executeQuery()) { + rs.next(); + int výsledek = rs.getBoolean(1) ? ANO : NE; + zapišVýsledek(výsledek); + } + } + } catch (SQLException e) { + logujSQLException(e); + zapišVýsledek(NE); + } + + } else { + zapišVýsledek(NE); + } + } + + private boolean ověřDoménu(String doména) { + return našeDoména.equalsIgnoreCase(doména); + } + + private void logujSQLException(SQLException e) { + err.println("-- SQLException:"); + e.printStackTrace(err); + err.println("-- SQLException"); + } +}