You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
744 lines
28 KiB
744 lines
28 KiB
package de.superx.servlet; |
|
|
|
import java.io.IOException; |
|
import java.lang.reflect.InvocationTargetException; |
|
import java.sql.Connection; |
|
import java.sql.DatabaseMetaData; |
|
import java.sql.Date; |
|
import java.sql.PreparedStatement; |
|
import java.sql.ResultSet; |
|
import java.sql.SQLException; |
|
import java.sql.Statement; |
|
import java.util.HashMap; |
|
import java.util.Hashtable; |
|
import java.util.Iterator; |
|
import java.util.TreeSet; |
|
import java.util.logging.Level; |
|
import java.util.logging.Logger; |
|
|
|
import javax.servlet.http.HttpServletRequest; |
|
import javax.servlet.http.HttpSession; |
|
import javax.xml.transform.TransformerConfigurationException; |
|
import javax.xml.transform.TransformerException; |
|
|
|
import de.memtext.db.NichtAngemeldetException; |
|
import de.memtext.tree.KeyParentEqualException; |
|
import de.memtext.tree.NoMainEntryException; |
|
import de.memtext.util.CryptUtils; |
|
import de.memtext.util.DateUtils; |
|
import de.memtext.util.ServletHelper; |
|
import de.superx.common.AbstractSicht; |
|
import de.superx.common.DBServletException; |
|
import de.superx.common.Sichten; |
|
import de.superx.common.SxFinRechte; |
|
import de.superx.common.SxKontierungsrechte; |
|
import de.superx.common.SxResultRow; |
|
import de.superx.common.SxResultSet; |
|
import de.superx.common.SxUser; |
|
import de.superx.util.SqlStringUtils; |
|
import freemarker.template.TemplateException; |
|
|
|
/** |
|
* Prüft, ob Authentifizierung OK ist. Im request müssen parameter kennung und |
|
* passwort vorhanden sein |
|
* |
|
*/ |
|
public class UserInitializer { |
|
|
|
public static final String MAXIMALE_LOGIN_VERSUCHE_ÜBERSCHRITTEN = "Maximale Login-Versuche überschritten"; |
|
private boolean isFromApplet = false; |
|
|
|
private SxUser user; |
|
private boolean isPasswordChangeDemanded = false; |
|
private Integer userid = new Integer(-1); |
|
|
|
private String username = ""; |
|
private Short adminUser; |
|
protected HttpServletRequest request; |
|
private String mandantenID = "default"; |
|
private String oldsha; |
|
|
|
public UserInitializer(HttpServletRequest request, boolean isFromApplet) throws IOException { |
|
this.request = request; |
|
this.isFromApplet = isFromApplet; |
|
} |
|
|
|
public void perform() throws NichtAngemeldetException, IllegalArgumentException, SecurityException, |
|
TransformerConfigurationException, InstantiationException, IllegalAccessException, |
|
InvocationTargetException, NoSuchMethodException, TransformerException, KeyParentEqualException, |
|
SQLException, DBServletException |
|
|
|
{ |
|
mandantenID = ServletHelper.getParameter(request, "MandantenID"); |
|
if (mandantenID == null) |
|
mandantenID = "default"; |
|
perform(mandantenID); |
|
} |
|
|
|
/** |
|
* Der Authentifizierte User wird auch in die Session gesteckt. |
|
* |
|
* @throws NichtAngemeldetException |
|
* @throws IllegalArgumentException |
|
* @throws SecurityException |
|
* @throws TransformerConfigurationException |
|
* @throws InstantiationException |
|
* @throws IllegalAccessException |
|
* @throws InvocationTargetException |
|
* @throws NoSuchMethodException |
|
* @throws TransformerException |
|
* @throws KeyParentEqualException |
|
* @throws SQLException |
|
* @throws DBServletException |
|
*/ |
|
|
|
public void perform(String mandantenID) throws NichtAngemeldetException, IllegalArgumentException, |
|
SecurityException, TransformerConfigurationException, InstantiationException, IllegalAccessException, |
|
InvocationTargetException, NoSuchMethodException, TransformerException, KeyParentEqualException, |
|
SQLException, DBServletException |
|
|
|
{ |
|
this.mandantenID = mandantenID; |
|
authenticate(mandantenID); |
|
|
|
// Authentifizierung OK |
|
synchronized (this) { |
|
HttpSession session = request.getSession(true); |
|
session.setAttribute("UserID", userid.toString()); |
|
session.setAttribute("sessiontype", "superx"); |
|
session.setAttribute("MandantenID", mandantenID); |
|
// request.getHeaderNames(); |
|
session.setAttribute("User-A", request.getHeader("User-Agent")); |
|
String desiredLocale = ServletHelper.getParameter(request, "locale"); |
|
if (desiredLocale != null) |
|
session.setAttribute("locale", desiredLocale); |
|
// System.out.println(username); |
|
if (SxPools.get(mandantenID).userpool.containsItemWithId(userid)) { |
|
user = (SxUser) SxPools.get(mandantenID).userpool.getById(userid); |
|
} else { |
|
try { |
|
user = initUser(mandantenID, username, userid, adminUser); |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
throw new IllegalArgumentException(e.toString()); |
|
} |
|
} |
|
initPWGültigkeit_Oldshas(); |
|
session.setAttribute("username", user.getName()); |
|
session.setAttribute("user", user); |
|
session.setAttribute("sessiontype", "superx"); |
|
session.setAttribute("UserIsAdmin", user.isAdmin() ? "true" : "false"); |
|
session.setAttribute("UserMaskRights", user.getMaskRights()); |
|
session.setAttribute("UserGroups", user.getGroups()); |
|
session.setAttribute("UserSachgebiete", user.getSachgebiete()); |
|
session.setAttribute("UserGroupnames", user.getGroupnames()); |
|
// System.out.println("user in session "+session.getId()); |
|
if (isPasswordChangeDemanded()) |
|
session.setAttribute("passwordchange", "true"); |
|
} |
|
} |
|
|
|
private void initPWGültigkeit_Oldshas() { |
|
Connection con = null; |
|
|
|
try { |
|
con = SxPools.getConnection(mandantenID); |
|
Statement st = con.createStatement(); |
|
ResultSet rs = st.executeQuery("select passwd_sha from userinfo where tid=" + userid); |
|
while (rs.next()) { |
|
user.setCurrentSha(rs.getString(1)); |
|
} |
|
rs.close(); |
|
rs = st.executeQuery( |
|
"select pw_gueltig_bis,old_sha1,old_sha2,old_sha3,old_sha4,old_sha5,old_sha6,old_sha7 from user_pw where userinfo_id=" |
|
+ userid); |
|
while (rs.next()) { |
|
Date gueltig_bis = rs.getDate(1); |
|
java.util.Date today = new java.util.Date(); |
|
if (gueltig_bis != null && !DateUtils.isDateInFuture(gueltig_bis)) |
|
isPasswordChangeDemanded = true; |
|
user.setOldSha(0, rs.getString(2)); |
|
user.setOldSha(1, rs.getString(3)); |
|
user.setOldSha(2, rs.getString(4)); |
|
user.setOldSha(3, rs.getString(5)); |
|
user.setOldSha(4, rs.getString(6)); |
|
user.setOldSha(5, rs.getString(7)); |
|
user.setOldSha(6, rs.getString(8)); |
|
} |
|
rs.close(); |
|
st.close(); |
|
con.close(); |
|
} catch (Exception e) { |
|
System.out.println(e); |
|
if (con != null) |
|
try { |
|
SxPools.invalidate(mandantenID, con); |
|
} catch (DBServletException e1) { |
|
e1.printStackTrace(); |
|
} |
|
} |
|
} |
|
|
|
/** |
|
* Prüft Anmeldung Parameter kennung passwort |
|
* |
|
* @throws DBServletException |
|
* @throws NichtAngemeldetException |
|
*/ |
|
private void authenticate(String mandantenID) throws DBServletException, NichtAngemeldetException { |
|
adminUser = new Short((short) 0); |
|
TokenChecker tokenChecker; |
|
boolean isRemotePasswordCheck = false; |
|
String passwort = null; |
|
String token = ServletHelper.getParameter(request, "token"); |
|
if (token != null && !token.equals("")) { |
|
tokenChecker = new TokenChecker(token); |
|
tokenChecker.testAll(); |
|
isRemotePasswordCheck = true; |
|
username = tokenChecker.getKennung(); |
|
|
|
} else { |
|
username = ServletHelper.getParameter(request, "kennung"); |
|
passwort = ServletHelper.getParameter(request, "passwort"); |
|
if (passwort==null||passwort.length()==0) passwort="notsecret"; |
|
String login= ServletHelper.getParameter(request, "login"); |
|
if (SuperXManager.isStandaloneLdapActive()&&"superx".equals(login)) |
|
{ |
|
System.out.println("LDAP aktiv aber Passwortkontrolle für "+username+" aus SuperX-Datenbank wegen Parameter login=superx"); |
|
} |
|
if (SuperXManager.isStandaloneLdapActive()&&!"superx".equals(login)) isRemotePasswordCheck=true; |
|
|
|
// MB Provisorium für kern4.9 alte LDAP-Technik via RemoteUser deaktiviert |
|
/* |
|
* if (username == null) { username = request.getRemoteUser(); // LDAP if |
|
* (username == null || username.trim().equals("")) throw new |
|
* NichtAngemeldetException( |
|
* "Keine Anmeldung möglich, da im request keine Kennung angegeben."); } if |
|
* (passwort == null) { if (request.getRemoteUser() != null)// LDAP { |
|
* isRemotePasswordCheck = true; SuperXmlAnmeldung.isRemoteLoginUsed = true; } |
|
* else throw new NichtAngemeldetException( |
|
* "Keine Anmeldung möglich, da im Request kein Passwort angegeben."); } |
|
*/ |
|
// String client_dns = ServletHelper.getParameter(request,"Remote |
|
// Host"); |
|
// if(!passwort.equals(request.getSession().getId())) //kein LDAP |
|
// { |
|
if (isFromApplet) { |
|
// makes sure a session is created /url rewriting if necessary |
|
HttpSession session = request.getSession(false); |
|
if (session != null) { |
|
session.invalidate(); // just in case an old session is |
|
// still |
|
// there |
|
} |
|
} |
|
} |
|
System.out.println("Session authenticated with user=" + username); |
|
// schmeißt NichtAngemeldetException |
|
checkUserInDb(username, isRemotePasswordCheck, passwort); |
|
|
|
} |
|
|
|
/** |
|
* erlaubte Maskentids einlesen- derzeit wird kein Stand berücksichtigt |
|
* Themenbaum aufbauen Sichten einlesen und User in userpool hinterlegen Zugriff |
|
* auch von de.common.ObjectCache.initUser aufgerufen |
|
* |
|
* @param login_name |
|
* @param userid |
|
* @param adminUser |
|
* @throws IllegalArgumentException |
|
* @throws SecurityException |
|
* @throws TransformerConfigurationException |
|
* @throws InstantiationException |
|
* @throws IllegalAccessException |
|
* @throws InvocationTargetException |
|
* @throws NoSuchMethodException |
|
* @throws TransformerException |
|
* @throws KeyParentEqualException |
|
* @throws SQLException |
|
* @throws DBServletException |
|
* @throws NichtAngemeldetException |
|
* @throws IOException |
|
* @throws TemplateException |
|
* @throws NoMainEntryException |
|
* @throws CloneNotSupportedException |
|
*/ |
|
public static SxUser initUser(String mandantenID, String username, Integer userid, Object adminUser) |
|
throws IllegalArgumentException, SecurityException, TransformerConfigurationException, |
|
InstantiationException, IllegalAccessException, InvocationTargetException, NoSuchMethodException, |
|
TransformerException, KeyParentEqualException, SQLException, DBServletException, NichtAngemeldetException, |
|
CloneNotSupportedException, NoMainEntryException, TemplateException, IOException { |
|
SxUser user = new SxUser(mandantenID, username, userid); |
|
user.getSelectionBuffer().setActive(SuperXManager.isSessionFieldDefaultsWanted(mandantenID)); |
|
user.setDBVersion(SxPools.get(mandantenID).getDatabaseMajorVersion(), |
|
SxPools.get(mandantenID).getDatabaseMinorVersion()); |
|
user.setAdmin(adminUser.toString().equals("1")); |
|
|
|
String queryMaskenRechte = "--alle Abfragen für die die Gruppe über Sachgebiete berechtigt \n" |
|
+ "select distinct M.tid,M.name \n" |
|
+ " from maskeninfo M, sachgeb_maske_bez S, group_sachgeb_bez G, user_group_bez U \n" |
|
+ " where M.tid = S.maskeninfo_id \n" + " and S.sachgebiete_id = G.sachgebiete_id \n" |
|
+ " and G.groupinfo_id = U.groupinfo_id \n" + " and U.userinfo_id = " + user.getId() |
|
+ " \n" + " union \n" + " --alle Abfragen für die die Gruppe berechtigt (Einzelrechte) \n" |
|
+ " select distinct M.tid,M.name \n" |
|
+ " from maskeninfo M, group_masken_bez G, user_group_bez U \n" |
|
+ " where M.tid = G.maskeninfo_id \n" + " and G.groupinfo_id = U.groupinfo_id \n" |
|
+ " and U.userinfo_id = " + user.getId() + " \n" + " union \n" |
|
+ "--alle Abfragen aus Sachgebieten für die der Nutzer Rechte hat \n" |
|
+ " select distinct M.tid,M.name \n" + " from maskeninfo M \n" + " where \n" |
|
+ " (M.tid in (select maskeninfo_id from sachgeb_maske_bez S where S.sachgebiete_id in \n" |
|
+ " (select sachgebiete_id from user_sachgeb_bez U where U.userinfo_id=" + user.getId() |
|
+ " ))) \n" + " union \n" |
|
+ "--alle Abfragen für die Einzelrechte in user_masken_bez angegeben \n" |
|
+ " select distinct M.tid,M.name \n" + " from maskeninfo M, user_masken_bez B \n" |
|
+ " where \n" + " B.userinfo_id = " + user.getId() + " \n" |
|
+ " and (M.tid = B.maskeninfo_id \n" + " or B.maskeninfo_id = 0)"; |
|
|
|
SxResultSet rs = ServletUtils.execute("Erlaubte Masken für User " + user.getName() + " (" + user.getId() + ")", |
|
queryMaskenRechte, user.getMandantenID()); |
|
user.initErlaubteMasken(rs); |
|
|
|
rs = ServletUtils.execute("Erlaubte Sachgebiete für User " + user.getName() + " (" + user.getId() + ")", |
|
"select sachgebiete_id from user_sachgeb_bez where userinfo_id=" + user.getId() + " \n" + "union \n" |
|
+ "select sachgebiete_id from group_sachgeb_bez where groupinfo_id in ( \n" |
|
+ "select groupinfo_id from user_group_bez where userinfo_id=" + user.getId() + ")", |
|
user.getMandantenID()); |
|
user.setSachgebiete(rs); |
|
rs = ServletUtils.execute("Einlesen von Gruppen für Benutzer " + user.getId(), |
|
"select tid,name from groupinfo G, user_group_bez U where G.tid=U.groupinfo_id and userinfo_id=" |
|
+ user.getId(), |
|
user.getMandantenID()); |
|
user.setGroups(rs); |
|
try { |
|
rs = ServletUtils.execute("Einlesen von versteckten Feldern für Benutzer " + user.getId(), |
|
"select field_id from group_field_pref where pref='hidden' and groupinfo_id in" |
|
+ " (select groupinfo_id from user_group_bez where userinfo_id=" + user.getId() + ")", |
|
user.getMandantenID()); |
|
|
|
// System.out.println(rs); |
|
user.setHiddenFields(rs); |
|
} catch (Exception e) { |
|
e.printStackTrace(); |
|
} |
|
|
|
// START getSichtartRechteDefinitionen |
|
for (Iterator it = SxPools.get(user.getMandantenID()).getSichtartRechteDefinitionen().iterator(); it |
|
.hasNext();) { |
|
SichtartRechteDefinition sd = (SichtartRechteDefinition) it.next(); |
|
rs = ServletUtils.execute("Einlesen von Sichtart " + sd.getName() + " Rechten für Benutzer " + user.getId(), |
|
sd.getSql(userid), user.getMandantenID()); |
|
|
|
user.getSichten().addSichtartRechte(sd.getName(), sd.isFallback_user_inst(), rs); |
|
} |
|
|
|
// ENDE |
|
// Sichten einlesen |
|
rs = ServletUtils.execute("Einlesen von Sichten für Benutzer " + user.getId(), Sichten.getReadingSql(user), |
|
user.getMandantenID()); |
|
|
|
user.getSichten().init(user.getMandantenID(), rs, SichtServlet.class); |
|
|
|
// Additional Rechte für Sichten |
|
for (Iterator it = SxPools.get(user.getMandantenID()).getSichtartRechteDefinitionen().iterator(); it |
|
.hasNext();) { |
|
SichtartRechteDefinition sd = (SichtartRechteDefinition) it.next(); |
|
|
|
if (sd.hasAdditionalKeysSQL()) { |
|
boolean isSQLOK = true; |
|
String sql = sd.getAdditionalKeysSQL(); |
|
sql = sql.replaceAll("<<UserID>>", user.getId().toString()); |
|
for (Iterator<String> it2 = user.getSichten().getSichtenArten().iterator(); it2.hasNext();) { |
|
String sichtart = it2.next(); |
|
if (sql.indexOf("<<" + sichtart + ">>") > -1) { |
|
String sichtartkeys = user.getSichten().getSichtartKeys(sichtart); |
|
if (sichtartkeys.equals("")) |
|
isSQLOK = false; |
|
sql = sql.replaceAll("<<" + sichtart + ">>", sichtartkeys); |
|
} |
|
} |
|
if (isSQLOK) { |
|
SxResultSet rs2 = ServletUtils.execute("Einlesen ergänzenden Rechten von Sichtart " + sd.getName() |
|
+ " Rechten für Benutzer " + user.getId(), sql, user.getMandantenID()); |
|
user.getSichten().addSichtartRechte(sd.getName(), sd.isFallback_user_inst(), rs2); |
|
// nicht nötig, weil Sichten erst eingelesen werden, wenn |
|
// tatsächlich gebaucht |
|
// user.getSichten().reinit(sd.getName()); |
|
} |
|
} |
|
} |
|
// ende additional Rechte für Sichten |
|
|
|
// spezielle SichtartRechte und fallback |
|
for (Iterator it = SxPools.get(user.getMandantenID()).getSichtartRechteDefinitionen().iterator(); it |
|
.hasNext();) { |
|
SichtartRechteDefinition sd = (SichtartRechteDefinition) it.next(); |
|
if (!sd.isFallback_user_inst()) |
|
continue; |
|
|
|
} |
|
rs = ServletUtils.execute("Einlesen von Sichtrechten für Benutzer " + user.getId(), |
|
"select distinct tid,type from sichten S where " + Sichten.getErlaubteSichtenSql(user), |
|
user.getMandantenID()); |
|
user.getSichten().setAllowedSichten(rs, user.isAdmin()); |
|
|
|
user.initThemenbaum(Themenbaum.class); |
|
int finRightVariant = SxPools.get(mandantenID).getFinRightVariant(); |
|
if (finRightVariant != SxFinRechte.NO_RIGHTS && !user.isAdmin()) { |
|
|
|
if (finRightVariant == SxFinRechte.SIMPLE_RIGHTS) { |
|
rs = ServletUtils.execute("Einlesen von einfachtenFin-Rechten für Benutzer " + user.getId(), |
|
SxFinRechte.getReadingSqlUserKamSimple(user), user.getMandantenID()); |
|
} |
|
if (finRightVariant == SxFinRechte.EXTENDED_RIGHTS) { |
|
rs = ServletUtils.execute("Einlesen von erweiterten Fin-Rechten für Benutzer " + user.getId(), |
|
SxFinRechte.getReadingSqlUserKam(user), user.getMandantenID()); |
|
} |
|
if (finRightVariant == SxFinRechte.INS3_RIGHTS) { |
|
rs = ServletUtils.execute( |
|
"Einlesen von erweiterten Fin-Rechten inkl. ins3 für Benutzer " + user.getId(), |
|
SxFinRechte.getReadingSqlUserKamIns3(user, |
|
SxPools.get(mandantenID).hasFinUserKamRestrictionFields()), // erweitert!! |
|
user.getMandantenID()); |
|
} |
|
SxResultSet rs2 = ServletUtils |
|
.executeALL_el(null, null, "Einlesen von Fin-Rechten proj_to_inst für Benutzer " + user.getId(), |
|
SxFinRechte.getReadingSqlProjToInst(SxPools.get(mandantenID).getDatabaseAbbr(), user), |
|
user.getMandantenID(), true) |
|
.getResultSet(); |
|
|
|
user.initFinRechte(rs, finRightVariant, rs2, SxPools.get(mandantenID).is0FINJoker(), |
|
SxPools.get(mandantenID).getRepository()); |
|
} |
|
// SxPools.get(user.getMandantenID()).userpool.add(user); |
|
// wird wegen Applet-Kompatibilität hier gesetzt |
|
// user.setFieldElementCache(new FieldElementCache()); |
|
// if (SuperXManager.userField1Cache != null |
|
// && !SuperXManager.userField1Cache.equals("")) { |
|
// initUserField1Cache(user); |
|
// } |
|
if (SxPools.get(mandantenID).hasKontierungsrechte()) { |
|
SxResultSet rs3 = ServletUtils |
|
.executeALL_el(null, null, "Einlesen von Kontierungsrechten für Benutzer " + user.getId(), |
|
SxKontierungsrechte.readingSQL + user.getId(), user.getMandantenID(), true) |
|
.getResultSet(); |
|
|
|
user.initKontierungsrechte(rs3); |
|
} |
|
return user; |
|
} |
|
|
|
/** |
|
* not in use |
|
* |
|
* @deprecated |
|
* @param user |
|
* @throws SQLException |
|
* @throws DBServletException |
|
*/ |
|
private static void initUserField1Cache(SxUser user) throws SQLException, DBServletException { |
|
SxResultSet rs = ServletUtils |
|
|
|
.execute("potentielle zu cachende User-Field1-quellen lesen", |
|
"select distinct relation from felderinfo where " |
|
// + SuperXManager.userField1Cache |
|
+ " and art=1 and tid!=23541", |
|
user.getMandantenID()); |
|
|
|
TreeSet sqls = new TreeSet(); |
|
String haushaltsjahr = DateUtils.getYear(new java.sql.Date(new java.util.Date().getTime())); |
|
Hashtable formular = new Hashtable(); |
|
formular.put("Haushaltsjahr", haushaltsjahr); |
|
for (Iterator it = rs.iterator(); it.hasNext();) { |
|
SxResultRow row = (SxResultRow) it.next(); |
|
String sql = (String) row.get(0); |
|
if (sql.trim().indexOf("<<SQL>>") == -1) |
|
continue; |
|
sql = SqlStringUtils.generateSQL("PG", formular, sql);// Database-Abbr |
|
// für |
|
// haushaltsjahr |
|
// egal |
|
try { |
|
HashMap map = new HashMap(); |
|
map.putAll(user.getBasicMap()); |
|
sql = SxPools.get(user.getMandantenID()).getTemplateProcessor().process(map, null, "user Field1 Cache", |
|
sql, null, SxPools.get(user.getMandantenID()).getRepository(), |
|
SxPools.get(user.getMandantenID()).getSqlDialect()); |
|
sql = AbstractSicht.getStrippedSQL(sql); |
|
if (sql.indexOf("<<") == -1) |
|
sqls.add(sql); |
|
} catch (Exception e) { |
|
Logger.getLogger("superx_" + user.getMandantenID()).log(Level.INFO, |
|
"Problem bei Caching des User-Feld-SQLs " + sql + " " + e); |
|
} |
|
|
|
} |
|
FieldElementCache userFieldElementCache = (FieldElementCache) user.getFieldElementCache(); |
|
for (Iterator it = sqls.iterator(); it.hasNext();) { |
|
String sql = (String) it.next(); |
|
Logger.getLogger("superx_" + user.getMandantenID()).log(Level.FINER, " Cache User-Feld-SQL: " + sql); |
|
try { |
|
rs = ServletUtils.executeALL_el(null, null, "Felder-SQL cachen", sql, user.getMandantenID(), true) |
|
.getResultSet(); |
|
userFieldElementCache.add(sql, rs); |
|
} catch (Exception e) { |
|
Logger.getLogger("superx_" + user.getMandantenID()).log(Level.INFO, |
|
"Problem bei Caching des User-Feld-SQLs " + sql + " " + e); |
|
} |
|
} |
|
} |
|
|
|
/** |
|
* Authentifiziert den Benutzer mit vorgegebener Kennung/Passwort. Wenn bereits |
|
* LDAP-Authentifizierung gewesen ist, steht im Passwort die Sessionid, und der |
|
* Username wird aus der Sessionid ausgelesen. Stellt auch fest, ob passwort |
|
* geändert werden muss |
|
* |
|
* |
|
* @param user |
|
* @param isRemotePasswortCheck - wenn schon durch LDAP/HisinOne geprüft, keine |
|
* Kontrolle in db nötig |
|
* @param passwort |
|
* @return |
|
* @throws DBServletException |
|
* @throws NichtAngemeldetException |
|
*/ |
|
private void checkUserInDb(String user, boolean isRemotePasswortCheck, String passwort) |
|
throws DBServletException, NichtAngemeldetException { |
|
String password_sha = ""; |
|
String client_ip = request.getRemoteAddr(); |
|
String client_dns = request.getRemoteHost(); |
|
String protend = ""; |
|
|
|
userid = null; |
|
// System.out.println("c1"); |
|
if (SxPools.get(mandantenID).getSqlDialect().equals("Postgres")) { |
|
protend = ", now());"; |
|
} else { |
|
protend = ", current);"; |
|
} |
|
Connection myConnection = null; |
|
try { |
|
myConnection = SxPools.getConnection(mandantenID); |
|
boolean isUserinfoMitGueltigkeit=checkUserinfoMitGueltigkeit(myConnection); |
|
String select="select tid, passwd_sha, administration, kennwort,akt_versuch,max_versuch from userinfo where benutzer = ? "; |
|
if (isUserinfoMitGueltigkeit) { |
|
select+= "and ( (gueltig_von is null or gueltig_von<=today()) and (gueltig_bis is null or gueltig_bis>=today()) )"; |
|
} |
|
Statement stm = myConnection.createStatement(); |
|
PreparedStatement pst = myConnection |
|
.prepareStatement(select); |
|
pst.setString(1, user); |
|
ResultSet rs = pst.executeQuery(); |
|
while (rs.next()) { |
|
userid = (Integer) rs.getObject(1); |
|
|
|
String kennwortSpalte = (String) rs.getString(4); |
|
if (kennwortSpalte != null) |
|
kennwortSpalte = kennwortSpalte.trim().toLowerCase(); |
|
if (kennwortSpalte != null && (kennwortSpalte.equals("ändern") || kennwortSpalte.equals("aendern")) |
|
&& !isRemotePasswortCheck) |
|
isPasswordChangeDemanded = true; |
|
adminUser = new Short(rs.getShort(3)); |
|
password_sha = (String) rs.getString(2); |
|
if (password_sha == null) |
|
throw new NichtAngemeldetException("Kein Passwort in DB gefunden"); |
|
else |
|
password_sha = password_sha.trim(); |
|
oldsha = password_sha; |
|
int akt_versuch = rs.getInt(5); |
|
int max_versuch = rs.getInt(6); |
|
|
|
if (akt_versuch > max_versuch|| max_versuch==0) { |
|
rs.close(); |
|
pst.close(); |
|
stm.close(); |
|
myConnection.close(); |
|
throw new NichtAngemeldetException(MAXIMALE_LOGIN_VERSUCHE_ÜBERSCHRITTEN); |
|
} |
|
} |
|
rs.close(); |
|
pst.close(); |
|
if (userid == null)// Kein Benutzer gefunden |
|
{ |
|
|
|
stm.execute("insert into protokoll (proto_fkt_id, userinfo_id, " |
|
+ "ip_adresse, client_name, zeitpunkt) values (" + "2, -1,'" + client_ip + "','" + client_dns |
|
+ "'" + protend); |
|
stm.close(); |
|
myConnection.close(); |
|
throw new NichtAngemeldetException("Kein gültiger Benutzer " + user + " in Datenbank vorhanden"); |
|
} |
|
if (passwort == null) |
|
passwort = "dummy"; // bei LDAP oder so |
|
|
|
boolean isPasswordOK = false; |
|
//System.out.println("getAuthType: "+ request.getAuthType()); |
|
//System.out.println("isRemotePasswortCheck: "+ isRemotePasswortCheck); |
|
if(request.getAuthType() !=null && request.getAuthType().equals("shibboleth")) { |
|
isPasswordOK = true; |
|
} else if (isRemotePasswortCheck) { |
|
isPasswordOK = new LdapPasswordChecker().isLdapPasswordOK(false, mandantenID, user, passwort); |
|
|
|
} else { |
|
String encoded_sha = CryptUtils.encodeSHA(passwort); |
|
String encoded_sha2 = CryptUtils.encodeSHA(passwort + CryptUtils.geheimnis1); |
|
String encoded_sha512 = CryptUtils.encodeSHA512(passwort); |
|
if ( password_sha.equals(encoded_sha) || password_sha.equals(encoded_sha2) |
|
|| password_sha.equals(encoded_sha512)) { |
|
isPasswordOK = true; |
|
} |
|
|
|
} |
|
if (isPasswordOK) { |
|
if (SxPools.get(mandantenID).isErweitertesProtokollWanted()) |
|
stm.execute("insert into protokoll (proto_fkt_id, userinfo_id, " |
|
+ "ip_adresse, client_name, zeitpunkt) values (" + "1," + userid.toString() + ",'" |
|
+ client_ip + "','" + client_dns + "'" + protend); |
|
|
|
PreparedStatement pst2 = myConnection |
|
.prepareStatement("update userinfo set akt_versuch=0 " + " where benutzer = ?"); |
|
pst2.setString(1, user); |
|
pst2.executeUpdate(); |
|
pst2.close(); |
|
updateLastLogin(myConnection); |
|
|
|
} else { |
|
stm.execute("insert into protokoll (proto_fkt_id, userinfo_id, " |
|
+ "ip_adresse, client_name, zeitpunkt) values (" + "2," + userid.toString() + ",'" + client_ip |
|
+ "','" + client_dns + "'" + protend); |
|
stm.close(); |
|
versuchszahlErhöhen(user, myConnection); |
|
myConnection.close(); |
|
throw new NichtAngemeldetException("Falsches Passwort für User " + user); |
|
} |
|
// alles OK, Rückgabe an Pool |
|
myConnection.close(); |
|
myConnection = null; |
|
|
|
} catch (SQLException se) { |
|
se.printStackTrace(); |
|
if (myConnection != null) { |
|
SxPools.invalidate(mandantenID, myConnection); |
|
|
|
} |
|
Logger.getLogger("superx_" + mandantenID).log(Level.SEVERE, "SQL-Fehler: " + se.toString()); |
|
throw new DBServletException(se.getMessage()); |
|
} finally { |
|
|
|
} |
|
} |
|
private boolean checkUserinfoMitGueltigkeit(Connection dbConnection) throws SQLException |
|
{ |
|
boolean result=false; |
|
Statement stm = dbConnection.createStatement(); |
|
DatabaseMetaData md = dbConnection.getMetaData(); |
|
|
|
ResultSet rs = md.getColumns(null, null, "userinfo", null); |
|
while (rs.next()) { |
|
if (rs.getString(4).equals("gueltig_von")) { |
|
result=true; |
|
} |
|
} |
|
rs.close(); |
|
return result; |
|
|
|
} |
|
private void updateLastLogin(Connection myConnection) throws SQLException { |
|
PreparedStatement pst3 = myConnection.prepareStatement("select count(*) from user_pw where userinfo_id=?"); |
|
pst3.setInt(1, userid.intValue()); |
|
ResultSet rs = pst3.executeQuery(); |
|
int count = 0; |
|
while (rs.next()) { |
|
count = rs.getInt(1); |
|
} |
|
rs.close(); |
|
pst3.close(); |
|
if (count == 0) { |
|
pst3 = myConnection.prepareStatement("insert into user_pw (userinfo_id,last_login) values (?,now());"); |
|
pst3.setInt(1, userid.intValue()); |
|
pst3.executeUpdate(); |
|
pst3.close(); |
|
} else { |
|
pst3 = myConnection.prepareStatement("update user_pw set last_login=now() where userinfo_id=?"); |
|
pst3.setInt(1, userid.intValue()); |
|
pst3.executeUpdate(); |
|
pst3.close(); |
|
} |
|
|
|
} |
|
|
|
static void versuchszahlErhöhen(String user, Connection myConnection) throws SQLException { |
|
PreparedStatement pst3 = myConnection |
|
.prepareStatement("update userinfo set akt_versuch=akt_versuch+1 " + " where benutzer = ?"); |
|
pst3.setString(1, user); |
|
int rows = pst3.executeUpdate(); |
|
pst3.close(); |
|
} |
|
|
|
public String getUsername() { |
|
return username; |
|
} |
|
|
|
public SxUser getUser() { |
|
return user; |
|
} |
|
|
|
/** |
|
* @return Returns the isPasswordChangeDemanded. checks session attribute also |
|
*/ |
|
public boolean isPasswordChangeDemanded() { |
|
String attrib = "false"; |
|
if (request.getSession() != null) { |
|
attrib = (String) request.getSession().getAttribute("passwordchange"); |
|
if (attrib == null) |
|
attrib = "false"; |
|
} |
|
return isPasswordChangeDemanded || attrib.equals("true"); |
|
} |
|
|
|
/** |
|
* @param isPasswordChangeDemanded The isPasswordChangeDemanded to set. |
|
*/ |
|
public void setPasswordChangeDemanded(boolean isPasswordChangeDemanded) { |
|
this.isPasswordChangeDemanded = isPasswordChangeDemanded; |
|
} |
|
|
|
public static String getPwChangeHtml(String message) { |
|
return de.superx.servlet.SuperXManager.htmlPageHead("Passwort ändern") |
|
|
|
+ "<form action=\"de.superx.servlet.SuperXmlPwChanger\" method=post>" |
|
|
|
+ message + "<br>" |
|
+ "Bisheriges Passwort<br><input type=password name=\"altes_passwort\" maxlength=40 size=20><br><br>" |
|
+ "Neues Passwort<br><input type=password name=\"passwort\" maxlength=40 size=20><br>" |
|
+ "Erneut eingeben<br><input type=password name=\"passwort2\" maxlength=40 size=20><br>" + "<br>" |
|
+ "<input class=\"button is-link\" type=\"submit\" value=\"Abschicken\">" + "</form>" + de.superx.servlet.SuperXManager.pageEnd; |
|
|
|
} |
|
private static boolean isGuestUser(Connection con, String kennung) throws SQLException { |
|
boolean result = false; |
|
try (PreparedStatement pst = con |
|
.prepareStatement("select name from userinfo where benutzer = ?");) { |
|
|
|
pst.setString(1, kennung); |
|
|
|
try (ResultSet rs = pst.executeQuery()) { |
|
if (rs.next()) { |
|
String name = rs.getString("name"); |
|
result = name != null && name.trim().equals("Gastkennung"); |
|
} |
|
} |
|
} |
|
return result; |
|
} |
|
/** |
|
* @return Returns the oldsha. |
|
*/ |
|
public String getOldsha() { |
|
return oldsha; |
|
} |
|
|
|
} |
|
|
|
// Created on 17.03.2005 at 09:09:00
|
|
|