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.jsx;
019:
020: import JSX.ObjIn;
021:
022: import net.sf.drftpd.DuplicateElementException;
023: import net.sf.drftpd.FatalException;
024: import net.sf.drftpd.FileExistsException;
025:
026: import org.apache.log4j.Level;
027: import org.apache.log4j.Logger;
028:
029: import org.drftpd.GlobalContext;
030: import org.drftpd.commands.UserManagement;
031: import org.drftpd.dynamicdata.KeyNotFoundException;
032:
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: JSXUserManager.java 984 2005-02-11 02:22:16Z zubov $
053: */
054: public class JSXUserManager implements UserManager {
055: private static final Logger logger = Logger
056: .getLogger(JSXUserManager.class.getName());
057: private GlobalContext _gctx;
058: String userpath = "users/jsx/";
059: File userpathFile = new File(userpath);
060: Hashtable users = new Hashtable();
061:
062: public JSXUserManager() 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: user.getKeyedMap().setObject(UserManagement.RATIO,
084: new Float(0));
085:
086: try {
087: user.addIPMask("*@127.0.0.1");
088: user.addIPMask("*@0:0:0:0:0:0:0:1");
089: } catch (DuplicateElementException e) {
090: }
091:
092: try {
093: user.addSecondaryGroup("siteop");
094: } catch (DuplicateElementException e1) {
095: }
096:
097: user.commit();
098: }
099: }
100:
101: public User create(String username) throws UserFileException {
102: try {
103: getUserByName(username);
104:
105: //bad
106: throw new FileExistsException("User already exists");
107: } catch (IOException e) {
108: //bad
109: throw new UserFileException(e);
110: } catch (NoSuchUserException e) {
111: //good
112: }
113:
114: JSXUser user = new JSXUser(this , username);
115: users.put(user.getName(), user);
116:
117: return user;
118: }
119:
120: public boolean exists(String username) {
121: return getUserFile(username).exists();
122: }
123:
124: public Collection getAllGroups() throws UserFileException {
125: Collection users = this .getAllUsers();
126: ArrayList ret = new ArrayList();
127:
128: for (Iterator iter = users.iterator(); iter.hasNext();) {
129: User myUser = (User) iter.next();
130: Collection myGroups = myUser.getGroups();
131:
132: for (Iterator iterator = myGroups.iterator(); iterator
133: .hasNext();) {
134: String myGroup = (String) iterator.next();
135:
136: if (!ret.contains(myGroup)) {
137: ret.add(myGroup);
138: }
139: }
140: }
141:
142: return ret;
143: }
144:
145: public Collection getAllUsers() throws UserFileException {
146: ArrayList users = new ArrayList();
147:
148: String[] userpaths = userpathFile.list();
149:
150: for (int i = 0; i < userpaths.length; i++) {
151: String userpath = userpaths[i];
152:
153: if (!userpath.endsWith(".xml")) {
154: continue;
155: }
156:
157: String username = userpath.substring(0, userpath.length()
158: - ".xml".length());
159:
160: try {
161: users.add((JSXUser) getUserByNameUnchecked(username));
162:
163: // throws IOException
164: } catch (NoSuchUserException e) {
165: } // continue
166: }
167:
168: return users;
169: }
170:
171: public Collection getAllUsersByGroup(String group)
172: throws UserFileException {
173: Collection users = getAllUsers();
174:
175: for (Iterator iter = users.iterator(); iter.hasNext();) {
176: JSXUser user = (JSXUser) iter.next();
177:
178: if (!user.isMemberOf(group)) {
179: iter.remove();
180: }
181: }
182:
183: return users;
184: }
185:
186: public User getUserByNameUnchecked(String username)
187: throws NoSuchUserException, UserFileException {
188: try {
189: JSXUser user = (JSXUser) users.get(username);
190:
191: if (user != null) {
192: return user;
193: }
194:
195: ObjIn in;
196:
197: try {
198: in = new ObjIn(new FileReader(getUserFile(username)));
199: } catch (FileNotFoundException ex) {
200: throw new NoSuchUserException("No such user");
201: }
202:
203: try {
204: user = (JSXUser) in.readObject();
205:
206: //throws RuntimeException
207: user.setUserManager(this );
208: users.put(user.getName(), user);
209: //user.reset(getGlobalContext());
210:
211: return user;
212: } catch (ClassNotFoundException e) {
213: throw new FatalException(e);
214: } finally {
215: in.close();
216: }
217: } catch (Throwable ex) {
218: if (ex instanceof NoSuchUserException) {
219: throw (NoSuchUserException) ex;
220: }
221:
222: throw new UserFileException("Error loading " + username, ex);
223: }
224: }
225:
226: private GlobalContext getGlobalContext() {
227: return _gctx;
228: }
229:
230: public User getUserByName(String username)
231: throws NoSuchUserException, UserFileException {
232: JSXUser user = (JSXUser) getUserByNameUnchecked(username);
233:
234: if (user.isDeleted()) {
235: throw new NoSuchUserException(user.getName()
236: + " is deleted");
237: }
238:
239: user.reset(getGlobalContext());
240:
241: return user;
242: }
243:
244: public File getUserFile(String username) {
245: return new File(userpath + username + ".xml");
246: }
247:
248: void remove(JSXUser user) {
249: this .users.remove(user.getName());
250: }
251:
252: void rename(JSXUser oldUser, String newUsername)
253: throws UserExistsException {
254: if (users.contains(newUsername)) {
255: throw new UserExistsException("user " + newUsername
256: + " exists");
257: }
258:
259: users.remove(oldUser.getName());
260: users.put(newUsername, oldUser);
261: }
262:
263: public void saveAll() throws UserFileException {
264: logger.log(Level.INFO, "Saving userfiles");
265:
266: for (Iterator iter = users.values().iterator(); iter.hasNext();) {
267: Object obj = iter.next();
268:
269: if (!(obj instanceof JSXUser)) {
270: throw new ClassCastException(
271: "Only accepts JSXUser objects");
272: }
273:
274: JSXUser user = (JSXUser) obj;
275: user.commit();
276: }
277: }
278:
279: public void init(GlobalContext gctx) {
280: _gctx = gctx;
281: }
282:
283: public User getUserByNameIncludeDeleted(String argument)
284: throws NoSuchUserException, UserFileException {
285: return getUserByName(argument);
286: }
287:
288: public User getUserByIdent(String ident)
289: throws NoSuchUserException, UserFileException {
290: for (Iterator iter = getAllUsers().iterator(); iter.hasNext();) {
291: User user = (User) iter.next();
292: try {
293: String uident = (String) user.getKeyedMap().getObject(
294: UserManagement.IRCIDENT);
295: if (uident.equals(ident)) {
296: return user;
297: }
298: } catch (KeyNotFoundException e1) {
299: }
300: }
301: throw new NoSuchUserException("No user found with ident = "
302: + ident);
303: }
304: }
|