001: /*
002: * This file is part of DrFTPD, Distributed FTP Daemon.
003: *
004: * DrFTPD is free software; you can redistribute it and/or modify
005: * it under the terms of the GNU General Public License as published by
006: * the Free Software Foundation; either version 2 of the License, or
007: * (at your option) any later version.
008: *
009: * DrFTPD is distributed in the hope that it will be useful,
010: * but WITHOUT ANY WARRANTY; without even the implied warranty of
011: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
012: * GNU General Public License for more details.
013: *
014: * You should have received a copy of the GNU General Public License
015: * along with DrFTPD; if not, write to the Free Software
016: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
017: */
018: package net.sf.drftpd.master.usermanager.xstream;
019:
020: import com.thoughtworks.xstream.XStream;
021: import com.thoughtworks.xstream.io.xml.DomDriver;
022:
023: import net.sf.drftpd.DuplicateElementException;
024: import net.sf.drftpd.FatalException;
025: import net.sf.drftpd.ObjectExistsException;
026:
027: import org.apache.log4j.Level;
028: import org.apache.log4j.Logger;
029:
030: import org.drftpd.GlobalContext;
031: import org.drftpd.commands.UserManagement;
032: import org.drftpd.dynamicdata.KeyNotFoundException;
033: import org.drftpd.master.ConnectionManager;
034: import org.drftpd.usermanager.NoSuchUserException;
035: import org.drftpd.usermanager.User;
036: import org.drftpd.usermanager.UserExistsException;
037: import org.drftpd.usermanager.UserFileException;
038: import org.drftpd.usermanager.UserManager;
039:
040: import java.io.File;
041: import java.io.FileNotFoundException;
042: import java.io.FileReader;
043: import java.io.IOException;
044:
045: import java.util.ArrayList;
046: import java.util.Collection;
047: import java.util.Hashtable;
048: import java.util.Iterator;
049:
050: /**
051: * @author mog
052: * @version $Id: XStreamUserManager.java 983 2005-02-11 02:18:43Z zubov $
053: */
054: public class XStreamUserManager implements UserManager {
055: private static final Logger logger = Logger
056: .getLogger(XStreamUserManager.class.getName());
057: private ConnectionManager _connManager;
058: String userpath = "users/xstream/";
059: File userpathFile = new File(userpath);
060: Hashtable users = new Hashtable();
061:
062: public XStreamUserManager() throws UserFileException {
063: if (!userpathFile.exists() && !userpathFile.mkdirs()) {
064: throw new UserFileException(new IOException(
065: "Error creating folders: " + userpathFile));
066: }
067:
068: String[] userfilenames = userpathFile.list();
069: int numUsers = 0;
070:
071: for (int i = 0; i < userfilenames.length; i++) {
072: String string = userfilenames[i];
073:
074: if (string.endsWith(".xml")) {
075: numUsers++;
076: }
077: }
078:
079: if (numUsers == 0) {
080: User user = create("drftpd");
081: user.setGroup("drftpd");
082: user.setPassword("drftpd");
083:
084: try {
085: user.addIPMask("*@127.0.0.1");
086: } catch (DuplicateElementException e) {
087: }
088:
089: try {
090: user.addSecondaryGroup("siteop");
091: } catch (DuplicateElementException e1) {
092: }
093:
094: user.commit();
095: }
096: }
097:
098: public User create(String username) throws UserFileException {
099: try {
100: getUserByName(username);
101:
102: //bad
103: throw new ObjectExistsException("User already exists");
104: } catch (IOException e) {
105: //bad
106: throw new UserFileException(e);
107: } catch (NoSuchUserException e) {
108: //good
109: }
110:
111: XStreamUser user = new XStreamUser(this , username);
112: users.put(user.getName(), user);
113:
114: return user;
115: }
116:
117: public boolean exists(String username) {
118: return getUserFile(username).exists();
119: }
120:
121: public Collection getAllGroups() throws UserFileException {
122: Collection users = this .getAllUsers();
123: ArrayList ret = new ArrayList();
124:
125: for (Iterator iter = users.iterator(); iter.hasNext();) {
126: User myUser = (User) iter.next();
127: Collection myGroups = myUser.getGroups();
128:
129: for (Iterator iterator = myGroups.iterator(); iterator
130: .hasNext();) {
131: String myGroup = (String) iterator.next();
132:
133: if (!ret.contains(myGroup)) {
134: ret.add(myGroup);
135: }
136: }
137: }
138:
139: return ret;
140: }
141:
142: public Collection getAllUsers() throws UserFileException {
143: ArrayList users = new ArrayList();
144:
145: String[] userpaths = userpathFile.list();
146:
147: for (int i = 0; i < userpaths.length; i++) {
148: String userpath = userpaths[i];
149:
150: if (!userpath.endsWith(".xml")) {
151: continue;
152: }
153:
154: String username = userpath.substring(0, userpath.length()
155: - ".xml".length());
156:
157: try {
158: users
159: .add((XStreamUser) getUserByNameUnchecked(username));
160:
161: // throws IOException
162: } catch (NoSuchUserException e) {
163: } // continue
164: }
165:
166: return users;
167: }
168:
169: public Collection getAllUsersByGroup(String group)
170: throws UserFileException {
171: Collection users = getAllUsers();
172:
173: for (Iterator iter = users.iterator(); iter.hasNext();) {
174: XStreamUser user = (XStreamUser) iter.next();
175:
176: if (!user.getGroup().equals(group)) {
177: iter.remove();
178: }
179: }
180:
181: return users;
182: }
183:
184: public User getUserByNameUnchecked(String username)
185: throws NoSuchUserException, UserFileException {
186: try {
187: XStreamUser user = (XStreamUser) users.get(username);
188:
189: if (user != null) {
190: return user;
191: }
192:
193: XStream inp = new XStream(new DomDriver());
194: FileReader in;
195:
196: try {
197: in = new FileReader(getUserFile(username));
198: } catch (FileNotFoundException ex) {
199: throw new NoSuchUserException("No such user");
200: }
201:
202: try {
203: user = (XStreamUser) inp.fromXML(in);
204:
205: //throws RuntimeException
206: user.setUserManager(this );
207: users.put(user.getName(), user);
208: //user.reset(_connManager.getGlobalContext());
209:
210: return user;
211: } catch (Exception e) {
212: throw new FatalException(e);
213: } finally {
214: in.close();
215: }
216: } catch (Throwable ex) {
217: if (ex instanceof NoSuchUserException) {
218: throw (NoSuchUserException) ex;
219: }
220:
221: throw new UserFileException("Error loading " + username, ex);
222: }
223: }
224:
225: public User getUserByName(String username)
226: throws NoSuchUserException, UserFileException {
227: XStreamUser user = (XStreamUser) getUserByNameUnchecked(username);
228:
229: if (user.isDeleted()) {
230: throw new NoSuchUserException(user.getName()
231: + " is deleted");
232: }
233:
234: user.reset(_connManager.getGlobalContext());
235:
236: return user;
237: }
238:
239: public File getUserFile(String username) {
240: return new File(userpath + username + ".xml");
241: }
242:
243: void remove(XStreamUser user) {
244: this .users.remove(user.getName());
245: }
246:
247: void rename(XStreamUser oldUser, String newUsername)
248: throws UserExistsException {
249: if (users.contains(newUsername)) {
250: throw new UserExistsException("user " + newUsername
251: + " exists");
252: }
253:
254: users.remove(oldUser.getName());
255: users.put(newUsername, oldUser);
256: }
257:
258: public void saveAll() throws UserFileException {
259: logger.log(Level.INFO, "Saving userfiles: " + users);
260:
261: for (Iterator iter = users.values().iterator(); iter.hasNext();) {
262: Object obj = iter.next();
263:
264: if (!(obj instanceof XStreamUser)) {
265: throw new ClassCastException(
266: "not instanceof XStreamUser");
267: }
268:
269: XStreamUser user = (XStreamUser) obj;
270: user.commit();
271: }
272: }
273:
274: public void init(GlobalContext mgr) {
275: _connManager = mgr.getConnectionManager();
276: }
277:
278: public User getUserByNameIncludeDeleted(String argument)
279: throws NoSuchUserException, UserFileException {
280: return getUserByName(argument);
281: }
282:
283: public User getUserByIdent(String ident)
284: throws NoSuchUserException, UserFileException {
285: for (Iterator iter = getAllUsers().iterator(); iter.hasNext();) {
286: User user = (User) iter.next();
287: try {
288: String uident = (String) user.getKeyedMap().getObject(
289: UserManagement.IRCIDENT);
290: if (uident.equals(ident)) {
291: return user;
292: }
293: } catch (KeyNotFoundException e1) {
294: }
295: }
296: throw new NoSuchUserException("No user found with ident = "
297: + ident);
298: }
299: }
|