# HG changeset patch # User František Kučera # Date 1394450426 -3600 # Node ID 5d23fa316c1c9decea7db2c8482fda17f0f24709 # Parent 80e56bfc227e69aeb82207a747a4a254cf76d848 eJabberd-auth: ověřování uživatelů proti SQL databázi diff -r 80e56bfc227e -r 5d23fa316c1c java/eJabberd-auth/build.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/eJabberd-auth/build.xml Mon Mar 10 12:20:26 2014 +0100 @@ -0,0 +1,74 @@ + + + + + + + + + + + Builds, tests, and runs the project eJabberd-auth. + + + diff -r 80e56bfc227e -r 5d23fa316c1c java/eJabberd-auth/manifest.mf --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/eJabberd-auth/manifest.mf Mon Mar 10 12:20:26 2014 +0100 @@ -0,0 +1,3 @@ +Manifest-Version: 1.0 +X-COMMENT: Main-Class will be added automatically by build + diff -r 80e56bfc227e -r 5d23fa316c1c java/eJabberd-auth/nbproject/build-impl.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/eJabberd-auth/nbproject/build-impl.xml Mon Mar 10 12:20:26 2014 +0100 @@ -0,0 +1,1411 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set src.dir + Must set test.src.dir + Must set build.dir + Must set dist.dir + Must set build.classes.dir + Must set dist.javadoc.dir + Must set build.test.classes.dir + Must set build.test.results.dir + Must set build.classes.excludes + Must set dist.jar + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + No tests executed. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must set JVM to use for profiling in profiler.info.jvm + Must set profiler agent JVM arguments in profiler.info.jvmargs.agent + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + + + + + + java -cp "${run.classpath.with.dist.jar}" ${main.class} + + + + + + + + + + + + + + + + + + + + + + + + + To run this application from the command line without Ant, try: + + java -jar "${dist.jar.resolved}" + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + Must select one file in the IDE or set run.class + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set debug.class + + + + + Must select one file in the IDE or set debug.class + + + + + Must set fix.includes + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + Must select one file in the IDE or set profile.class + This target only works when run from inside the NetBeans IDE. + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + This target only works when run from inside the NetBeans IDE. + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select one file in the IDE or set run.class + + + + + + Must select some files in the IDE or set test.includes + + + + + Must select one file in the IDE or set run.class + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Must select some files in the IDE or set javac.includes + + + + + + + + + + + + + + + + + + + + Some tests failed; see details above. + + + + + + + + + Must select some files in the IDE or set test.includes + + + + Some tests failed; see details above. + + + + Must select some files in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + Some tests failed; see details above. + + + + + Must select one file in the IDE or set test.class + + + + Must select one file in the IDE or set test.class + Must select some method in the IDE or set test.method + + + + + + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + Must select one file in the IDE or set applet.url + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff -r 80e56bfc227e -r 5d23fa316c1c java/eJabberd-auth/nbproject/genfiles.properties --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/eJabberd-auth/nbproject/genfiles.properties Mon Mar 10 12:20:26 2014 +0100 @@ -0,0 +1,8 @@ +build.xml.data.CRC32=9c33367f +build.xml.script.CRC32=c5fdca80 +build.xml.stylesheet.CRC32=28e38971@1.56.1.46 +# This file is used by a NetBeans-based IDE to track changes in generated files such as build-impl.xml. +# Do not edit this file. You may delete it but then the IDE will never regenerate such files for you. +nbproject/build-impl.xml.data.CRC32=9c33367f +nbproject/build-impl.xml.script.CRC32=212d389f +nbproject/build-impl.xml.stylesheet.CRC32=c6d2a60f@1.56.1.46 diff -r 80e56bfc227e -r 5d23fa316c1c java/eJabberd-auth/nbproject/project.properties --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/eJabberd-auth/nbproject/project.properties Mon Mar 10 12:20:26 2014 +0100 @@ -0,0 +1,71 @@ +annotation.processing.enabled=true +annotation.processing.enabled.in.editor=false +annotation.processing.processor.options= +annotation.processing.processors.list= +annotation.processing.run.all.processors=true +annotation.processing.source.output=${build.generated.sources.dir}/ap-source-output +build.classes.dir=${build.dir}/classes +build.classes.excludes=**/*.java,**/*.form +# This directory is removed when the project is cleaned: +build.dir=build +build.generated.dir=${build.dir}/generated +build.generated.sources.dir=${build.dir}/generated-sources +# Only compile against the classpath explicitly listed here: +build.sysclasspath=ignore +build.test.classes.dir=${build.dir}/test/classes +build.test.results.dir=${build.dir}/test/results +# Uncomment to specify the preferred debugger connection transport: +#debug.transport=dt_socket +debug.classpath=\ + ${run.classpath} +debug.test.classpath=\ + ${run.test.classpath} +# This directory is removed when the project is cleaned: +dist.dir=dist +dist.jar=${dist.dir}/eJabberd-auth.jar +dist.javadoc.dir=${dist.dir}/javadoc +excludes= +includes=** +jar.compress=false +javac.classpath= +# Space-separated list of extra javac options +javac.compilerargs= +javac.deprecation=false +javac.processorpath=\ + ${javac.classpath} +javac.source=1.7 +javac.target=1.7 +javac.test.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +javac.test.processorpath=\ + ${javac.test.classpath} +javadoc.additionalparam= +javadoc.author=false +javadoc.encoding=${source.encoding} +javadoc.noindex=false +javadoc.nonavbar=false +javadoc.notree=false +javadoc.private=false +javadoc.splitindex=true +javadoc.use=true +javadoc.version=false +javadoc.windowtitle= +main.class=cz.frantovo.ejabberd.auth.EJabberdAuth +manifest.file=manifest.mf +meta.inf.dir=${src.dir}/META-INF +mkdist.disabled=false +platform.active=default_platform +run.classpath=\ + ${javac.classpath}:\ + ${build.classes.dir} +# Space-separated list of JVM arguments used when running the project. +# You may also define separate properties like run-sys-prop.name=value instead of -Dname=value. +# To set system properties for unit tests define test-sys-prop.name=value: +run.jvmargs= +run.test.classpath=\ + ${javac.test.classpath}:\ + ${build.test.classes.dir} +source.encoding=UTF-8 +src.dir=src +test.src.dir=test diff -r 80e56bfc227e -r 5d23fa316c1c java/eJabberd-auth/nbproject/project.xml --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/eJabberd-auth/nbproject/project.xml Mon Mar 10 12:20:26 2014 +0100 @@ -0,0 +1,15 @@ + + + org.netbeans.modules.java.j2seproject + + + eJabberd-auth + + + + + + + + + diff -r 80e56bfc227e -r 5d23fa316c1c java/eJabberd-auth/src/cz/frantovo/ejabberd/auth/ChybaZápisuVýsledku.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/eJabberd-auth/src/cz/frantovo/ejabberd/auth/ChybaZápisuVýsledku.java Mon Mar 10 12:20:26 2014 +0100 @@ -0,0 +1,29 @@ +/** + * 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; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class ChybaZápisuVýsledku extends Exception { + + public ChybaZápisuVýsledku(Throwable cause) { + super(cause); + } +} diff -r 80e56bfc227e -r 5d23fa316c1c java/eJabberd-auth/src/cz/frantovo/ejabberd/auth/ChybaČteníVstupu.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/java/eJabberd-auth/src/cz/frantovo/ejabberd/auth/ChybaČteníVstupu.java Mon Mar 10 12:20:26 2014 +0100 @@ -0,0 +1,29 @@ +/** + * 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; + +/** + * + * @author Ing. František Kučera (frantovo.cz) + */ +public class ChybaČteníVstupu extends Exception { + + public ChybaČteníVstupu(Throwable cause) { + super(cause); + } +} 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"); + } +}