001: package net.wastl.webmail.server;
002:
003: import net.wastl.webmail.xml.*;
004: import net.wastl.webmail.misc.*;
005: import net.wastl.webmail.config.*;
006: import net.wastl.webmail.server.http.*;
007: import net.wastl.webmail.exceptions.*;
008:
009: import java.net.*;
010: import java.util.*;
011: import javax.mail.*;
012: import javax.servlet.http.*;
013:
014: import org.w3c.dom.*;
015:
016: import org.webengruven.webmail.auth.*;
017:
018: /**
019: * AdminSession.java
020: *
021: * Created: Thu Sep 9 18:24:05 1999
022: *
023: * Copyright (C) 2000 Sebastian Schaffert
024: *
025: * This program is free software; you can redistribute it and/or
026: * modify it under the terms of the GNU General Public License
027: * as published by the Free Software Foundation; either version 2
028: * of the License, or (at your option) any later version.
029: *
030: * This program is distributed in the hope that it will be useful,
031: * but WITHOUT ANY WARRANTY; without even the implied warranty of
032: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
033: * GNU General Public License for more details.
034: *
035: * You should have received a copy of the GNU General Public License
036: * along with this program; if not, write to the Free Software
037: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
038: */
039: /**
040: *
041: * @author Sebastian Schaffert
042: * @version
043: */
044: /* 9/24/2000 devink -- updated for new challenge/response authentication */
045: public class AdminSession implements HTTPSession {
046:
047: /** When has the session been last accessed? */
048: private long last_access;
049: /** The session-ID for this session */
050: private String session_code;
051: /** Parent WebMailServer */
052: protected WebMailServer parent;
053:
054: protected InetAddress remote;
055: private String remote_agent;
056: private String remote_accepts;
057:
058: protected XMLAdminModel model;
059:
060: protected HttpSession sess = null;
061:
062: protected boolean running_as_servlet = false;
063:
064: protected String selected_domain = "";
065: protected String selected_user = "";
066:
067: protected boolean is_logged_out = false;
068:
069: public AdminSession(WebMailServer parent, Object parm,
070: HTTPRequestHeader h) throws InvalidPasswordException,
071: WebMailException {
072: try {
073: Class srvltreq = Class
074: .forName("javax.servlet.http.HttpServletRequest");
075: if (srvltreq.isInstance(parm)) {
076: running_as_servlet = true;
077: javax.servlet.http.HttpServletRequest req = (javax.servlet.http.HttpServletRequest) parm;
078: this .sess = req.getSession(false);
079: session_code = ((javax.servlet.http.HttpSession) sess)
080: .getId();
081: try {
082: remote = InetAddress.getByName(req.getRemoteHost());
083: } catch (UnknownHostException e) {
084: try {
085: remote = InetAddress.getByName(req
086: .getRemoteAddr());
087: } catch (Exception ex) {
088: try {
089: remote = InetAddress.getByName("localhost");
090: } catch (Exception ex2) {
091: }
092: }
093: }
094: } else {
095: throw new Exception(
096: "Running as Servlet but not a valid ServletRequest");
097: }
098: } catch (Throwable t) {
099: this .remote = (InetAddress) parm;
100: session_code = Helper.calcSessionCode(remote, h);
101: }
102: doInit(parent, h);
103:
104: }
105:
106: protected void doInit(WebMailServer parent, HTTPRequestHeader h)
107: throws InvalidPasswordException, WebMailException {
108: this .parent = parent;
109: last_access = System.currentTimeMillis();
110: remote_agent = h.getHeader("User-Agent").replace('\n', ' ');
111: remote_accepts = h.getHeader("Accept").replace('\n', ' ');
112: //env=new Hashtable();
113: model = parent.getStorage().createXMLAdminModel();
114: login(h);
115: parent.getStorage().log(Storage.LOG_INFO,
116: "WebMail: New Session (" + session_code + ")");
117:
118: setEnv();
119: }
120:
121: public void login(HTTPRequestHeader h)
122: throws InvalidPasswordException {
123: String passwd = parent.getStorage().getConfig("ADMIN PASSWORD");
124: if (!Helper.crypt(passwd, h.getContent("password")).equals(
125: passwd)) {
126: throw new InvalidPasswordException();
127: }
128: login();
129: System.err.println("Ok");
130: }
131:
132: public void login() {
133: setLastAccess();
134: setEnv();
135: }
136:
137: public void logout() {
138: if (!is_logged_out) {
139: if (sess != null) {
140: try {
141: sess.invalidate();
142: } catch (Exception ex) {
143: }
144: }
145: if (parent.getSession(getSessionCode()) != null) {
146: parent.removeSession(this );
147: }
148: }
149: is_logged_out = true;
150: }
151:
152: public boolean isLoggedOut() {
153: return is_logged_out;
154: }
155:
156: public String getSessionCode() {
157: return session_code;
158: }
159:
160: public Locale getLocale() {
161: return Locale.getDefault();
162: }
163:
164: public long getLastAccess() {
165: return last_access;
166: }
167:
168: public void setLastAccess() {
169: last_access = System.currentTimeMillis();
170: }
171:
172: public String getEnv(String key) {
173: return model.getStateVar(key);
174: }
175:
176: public void selectUser(String user) {
177: try {
178: selected_user = user;
179: System.err.println("Selecting user " + user);
180: XMLUserData ud = parent.getStorage().getUserData(user,
181: selected_domain, "");
182: System.err.println("Done.");
183: model.importUserData(ud.getUserData());
184: } catch (InvalidPasswordException e) {
185: } catch (UserDataException e) {
186: }
187: }
188:
189: public void clearUser() {
190: selected_user = "";
191: model.clearUserData();
192: }
193:
194: public void deleteUser(String user) {
195: parent.getStorage().deleteUserData(user, selected_domain);
196: // Refresh information
197: selectDomain(selected_domain);
198: }
199:
200: /* 10/22/2000 devink -- added to suport new authentication changes. */
201: /** This does all the necessary setup to edit the currently selected
202: * user.
203: */
204: public void setupUserEdit() throws WebMailException {
205: XMLUserData ud;
206: AuthDisplayMngr adm;
207:
208: ud = parent.getStorage().getUserData(selected_user,
209: selected_domain, "");
210: adm = parent.getStorage().getAuthenticator()
211: .getAuthDisplayMngr();
212:
213: adm.setPassChangeVars(ud, model);
214: model.setStateVar("pass change tmpl", adm.getPassChangeTmpl());
215: }
216:
217: public void setException(Exception ex) {
218: model.setException(ex);
219: }
220:
221: /**
222: * Change the settings for a specific user.
223: * This method will check for changes to a user's configuration and save the new user configuration.
224: * Note that this should not be done when a user session is still active!
225: * @param h Header parsed from AdministratorPlugin
226: */
227: public void changeUser(HTTPRequestHeader head)
228: throws WebMailException {
229: XMLUserData user = parent.getStorage().getUserData(
230: selected_user, selected_domain, "", false);
231:
232: Enumeration contentkeys = head.getContentKeys();
233: user.resetBoolVars();
234: while (contentkeys.hasMoreElements()) {
235: String key = ((String) contentkeys.nextElement())
236: .toLowerCase();
237: if (key.startsWith("intvar")) {
238: try {
239: long value = Long.parseLong(head.getContent(key));
240: user.setIntVar(key.substring(7), value);
241: } catch (NumberFormatException ex) {
242: System.err
243: .println("Warning: Remote provided illegal intvar in request header: \n("
244: + key
245: + ","
246: + head.getContent(key)
247: + ")");
248: }
249: } else if (key.startsWith("boolvar")) {
250: boolean value = head.getContent(key).toUpperCase()
251: .equals("ON");
252: user.setBoolVar(key.substring(8), value);
253: }
254: }
255:
256: user.setSignature(head.getContent("user signature"));
257: user.setFullName(head.getContent("user full name"));
258: user.setEmail(head.getContent("user email"));
259: if (!head.getContent("user password").equals("")) {
260: net.wastl.webmail.server.Authenticator auth = parent
261: .getStorage().getAuthenticator();
262: if (auth.canChangePassword()) {
263: try {
264: auth.changePassword(user, head
265: .getContent("user password"), head
266: .getContent("user password"));
267: } catch (InvalidPasswordException e) {
268: /* XXX Not sure this is the right exception */
269: // Modified by exce, start
270: /**
271: throw new InvalidDataException(parent.getStorage().getStringResource("EX NO CHANGE PASSWORD", Locale.getDefault()));
272: **/
273: throw new InvalidDataException(parent.getStorage()
274: .getStringResource("EX NO CHANGE PASSWORD",
275: parent.getDefaultLocale()));
276: // Modified by exce, end
277: }
278: } else {
279: throw new InvalidDataException(parent.getStorage()
280: .getStringResource("EX NO CHANGE PASSWORD",
281: Locale.getDefault()));
282: }
283: }
284: user.setPreferredLocale(head.getContent("user language"));
285:
286: parent.getStorage()
287: .saveUserData(selected_user, selected_domain);
288:
289: selectUser(selected_user);
290: selectDomain(selected_domain);
291:
292: }
293:
294: public void selectDomain(String domain) {
295: model.setStateVar("selected domain",domain);
296:
297: selected_domain=domain;
298:
299: Enumeration enum=parent.getStorage().getUsers(domain);
300: model.removeAllStateVars("user");
301: while(enum.hasMoreElements()) {
302: model.addStateVar("user",(String)enum.nextElement());
303: }
304: }
305:
306: public void setEnv(String key, String value) {
307: //env.put(key,value);
308: model.setStateVar(key, value);
309: }
310:
311: public void setEnv() {
312: model.setStateVar("session id", session_code);
313: model.setStateVar("base uri", parent.getBasePath());
314: model.setStateVar("img base uri", parent.getBasePath());
315: model.setStateVar("uptime", parent.getUptime() / 1000 + "");
316: model.update();
317:
318: // Here we must initialize which choices are available for ChoiceConfigParameters!
319: XMLSystemData sysdata = parent.getStorage().getSystemData();
320: sysdata.initChoices();
321:
322: if (running_as_servlet) {
323: model.setStateVar("servlet status", parent.toString());
324: } else {
325: model.setStateVar("http server status",
326: ((StatusServer) parent.getServer("HTTP"))
327: .getStatus());
328: model.setStateVar("ssl server status",
329: ((StatusServer) parent.getServer("SSL"))
330: .getStatus());
331: }
332: model.setStateVar("storage status", parent.getStorage()
333: .toString());
334:
335: /*
336: Generate a list of active sessions with some additional information
337: (idle time, session code, active mail connections, ...)
338: */
339: XMLCommon.genericRemoveAll(model.getStateData(), "SESSION");
340: Enumeration e = parent.getSessions();
341: if (e != null && e.hasMoreElements()) {
342: while (e.hasMoreElements()) {
343: String name = (String) e.nextElement();
344: HTTPSession h = parent.getSession(name);
345: if (h instanceof WebMailSession) {
346: WebMailSession w = (WebMailSession) h;
347:
348: Element sess_elem = model
349: .addStateElement("SESSION");
350: sess_elem.setAttribute("type", "user");
351:
352: sess_elem.appendChild(model.createTextElement(
353: "SESS_USER", w.getUserName()));
354: sess_elem.appendChild(model.createTextElement(
355: "SESS_CODE", w.getSessionCode()));
356: sess_elem.appendChild(model.createTextElement(
357: "SESS_ADDRESS", w.getRemoteAddress()
358: .toString()));
359: sess_elem.appendChild(model.createStateVar(
360: "idle time",
361: (System.currentTimeMillis() - w
362: .getLastAccess())
363: / 1000 + ""));
364:
365: Enumeration keys = w.getActiveConnections().keys();
366: while (keys.hasMoreElements()) {
367: String next = (String) keys.nextElement();
368: try {
369: sess_elem
370: .appendChild(model
371: .createTextElement(
372: "SESS_CONN",
373: ((Folder) w
374: .getActiveConnections()
375: .get(next))
376: .getURLName()
377: + ""));
378: } catch (Exception ex) {
379: sess_elem.appendChild(model
380: .createTextElement("SESS_CONN",
381: "Error while fetching connection "
382: + next));
383: }
384: }
385: /* If the remote is admin and we are not the remote! */
386: // && !h.getSessionCode().equals(session_code)
387: } else if (h instanceof AdminSession) {
388: Element sess_elem = model
389: .addStateElement("SESSION");
390: sess_elem.setAttribute("type", "admin");
391:
392: sess_elem.appendChild(model.createTextElement(
393: "SESS_USER", "Administrator"));
394: sess_elem.appendChild(model.createTextElement(
395: "SESS_ADDRESS", h.getRemoteAddress()
396: .toString()));
397: sess_elem.appendChild(model.createTextElement(
398: "SESS_CODE", h.getSessionCode()));
399: sess_elem.appendChild(model.createStateVar(
400: "idle time",
401: (System.currentTimeMillis() - h
402: .getLastAccess())
403: / 1000 + ""));
404:
405: }
406: }
407: }
408:
409: // Add all languages to the state
410: model.removeAllStateVars("language");
411: String lang = parent.getConfig("languages");
412: StringTokenizer tok = new StringTokenizer(lang, " ");
413: while (tok.hasMoreTokens()) {
414: String t = tok.nextToken();
415: model.addStateVar("language", t);
416: }
417:
418: model.removeAllStateVars("protocol");
419: Provider[] stores = parent.getStoreProviders();
420: for (int i = 0; i < stores.length; i++) {
421: model.addStateVar("protocol", stores[i].getProtocol());
422: }
423:
424: }
425:
426: public InetAddress getRemoteAddress() {
427: return remote;
428: }
429:
430: public long getTimeout() {
431: return 600000;
432: }
433:
434: public void timeoutOccured() {
435: }
436:
437: public void saveData() {
438: }
439:
440: public Document getModel() {
441: return model.getRoot();
442: }
443: } // AdminSession
|