KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > drftpd > master > usermanager > glftpd > GlftpdUserManager


1 /*
2  * This file is part of DrFTPD, Distributed FTP Daemon.
3  *
4  * DrFTPD is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * DrFTPD is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with DrFTPD; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package net.sf.drftpd.master.usermanager.glftpd;
19
20 import java.io.BufferedReader JavaDoc;
21 import java.io.File JavaDoc;
22 import java.io.FileNotFoundException JavaDoc;
23 import java.io.FileReader JavaDoc;
24 import java.io.IOException JavaDoc;
25 import java.util.ArrayList JavaDoc;
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.List JavaDoc;
29 import java.util.Properties JavaDoc;
30 import java.util.StringTokenizer JavaDoc;
31
32 import net.sf.drftpd.DuplicateElementException;
33 import net.sf.drftpd.master.ConnectionManager;
34 import net.sf.drftpd.master.usermanager.NoSuchUserException;
35 import net.sf.drftpd.master.usermanager.User;
36 import net.sf.drftpd.master.usermanager.UserFileException;
37 import net.sf.drftpd.master.usermanager.UserManager;
38
39 import org.apache.log4j.Logger;
40
41 /**
42  * @author mog
43  * @author zubov
44  * @version $Id: GlftpdUserManager.java,v 1.14 2004/05/12 00:45:09 mog Exp $
45  */

46 public class GlftpdUserManager implements UserManager {
47     private static final Logger logger =
48         Logger.getLogger(GlftpdUserManager.class.getName());
49
50     private File JavaDoc userpathFile;
51
52     private String JavaDoc userdirpath;
53     private File JavaDoc passwdfile;
54     /**
55      * Constructor for GlftpdUserManager.
56      *
57      * Used properties:
58      * <pre>
59      * glftpd.root
60      * glftpd.users
61      * glftpd.passwd
62      * </pre>
63      */

64     public GlftpdUserManager() {
65         this(System.getProperties());
66     }
67     
68     public GlftpdUserManager(Properties JavaDoc cfg) {
69         // super();
70
//this.root = root;
71
String JavaDoc glftpdRoot = cfg.getProperty("glftpd.root", ".");
72         userdirpath =
73             cfg.getProperty("glftpd.users", glftpdRoot + "/ftp-data/users/");
74         userpathFile = new File JavaDoc(userdirpath);
75         passwdfile =
76             new File JavaDoc(
77                 cfg.getProperty("glftpd.passwd", glftpdRoot + "/etc/passwd"));
78     }
79
80     /**
81      * @see net.sf.drftpd.master.UserManager#save(User)
82      */

83     void save(User user) throws IOException JavaDoc {
84         throw new UnsupportedOperationException JavaDoc();
85     }
86
87     /**
88      * @see net.sf.drftpd.master.usermanager.UserManager#create(java.lang.String)
89      */

90     public User create(String JavaDoc username) {
91         throw new UnsupportedOperationException JavaDoc();
92     }
93
94     void load(User user) throws NoSuchUserException, IOException JavaDoc {
95         GlftpdUser gluser = null;
96         if (user instanceof GlftpdUser)
97             gluser = (GlftpdUser) user;
98
99         {
100             BufferedReader JavaDoc in;
101             try {
102                 in =
103                     new BufferedReader JavaDoc(
104                         new FileReader JavaDoc(getUserfilepath(user.getUsername())));
105             } catch (FileNotFoundException JavaDoc ex) {
106                 throw new NoSuchUserException(ex.toString());
107             }
108
109             //empty "stuff"
110

111             String JavaDoc param[], line, arg;
112             int temp = 0;
113             try {
114                 while (true) {
115                     try {
116                         if ((line = in.readLine()) == null)
117                             break;
118                         param = line.split(" ");
119                         arg = line.substring(param[0].length() + 1);
120                     } catch (IOException JavaDoc ex) {
121                         ex.printStackTrace();
122                         break;
123                     }
124                     if ("USER".equals(param[0])) {
125                         user.setComment(arg);
126 // } else if ("HOMEDIR".equals(param[0])) {
127
// user.setHomeDirectory(arg);
128
} else if ("GENERAL".equals(param[0])) {
129                         // GENERAL: WKLY_ALLOTMENT, IDLE_TIME, MAX_DLSPEED, MAX_ULSPEED
130
gluser.setWeeklyAllotment(
131                             Integer.parseInt(param[1].substring(2)));
132                         int idleTime = Integer.parseInt(param[2]) * 60;
133                         if (idleTime < 0)
134                             idleTime = 0;
135                         user.setIdleTime(idleTime);
136                         //user.setMaxDownloadRate(Integer.parseInt(param[3]));
137
//user.setMaxUploadRate(Integer.parseInt(param[4]));
138
} else if ("LOGINS".equals(param[0])) {
139                         // max logins per account, max logins from the same IP,
140

141                         // max simult. downloads, max simult. uploads -1 is unlimited
142
user.setMaxLogins(Integer.parseInt(param[1]));
143                         user.setMaxLoginsPerIP(Integer.parseInt(param[2]));
144                         user.setMaxSimDownloads(Integer.parseInt(param[3]));
145                         user.setMaxSimUploads(Integer.parseInt(param[4]));
146                     } else if ("FLAGS".equals(param[0])) {
147                         if (arg.indexOf('1') != -1)
148                             user.addGroup("siteop");
149                         if (arg.indexOf('2') != -1)
150                             user.addGroup("gadmin");
151                         if (arg.indexOf('3') != -1)
152                             user.addGroup("glock");
153                         if (arg.indexOf('4') != -1)
154                             user.addGroup("exempt");
155                         if (arg.indexOf('5') != -1)
156                             user.addGroup("color");
157                         if (arg.indexOf('6') != -1)
158                             user.addGroup("deleted");
159                         if (arg.indexOf('7') != -1)
160                             user.addGroup("useredit");
161                         if (arg.indexOf('8') != -1)
162                             user.addGroup("anonymous");
163                         if (arg.indexOf('A') != -1)
164                             user.addGroup("nuke");
165                         if (arg.indexOf('B') != -1)
166                             user.addGroup("unnuke");
167                         if (arg.indexOf('C') != -1)
168                             user.addGroup("undupe");
169                         if (arg.indexOf('D') != -1)
170                             user.addGroup("kick");
171                         if (arg.indexOf('E') != -1)
172                             user.addGroup("kill");
173                         if (arg.indexOf('F') != -1)
174                             user.addGroup("take");
175                         if (arg.indexOf('G') != -1)
176                             user.addGroup("give");
177                         if (arg.indexOf('H') != -1)
178                             user.addGroup("users");
179                         if (arg.indexOf('J') != -1)
180                             user.addGroup("cust1");
181                         if (arg.indexOf('K') != -1)
182                             user.addGroup("cust2");
183                         if (arg.indexOf('L') != -1)
184                             user.addGroup("cust3");
185                         if (arg.indexOf('M') != -1)
186                             user.addGroup("cust4");
187                         if (arg.indexOf('N') != -1)
188                             user.addGroup("cust5");
189                     } else if ("TAGLINE".equals(param[0])) {
190                         user.setTagline(arg);
191                     //} else if ("DIR".equals(param[0])) {
192
// DIR is the start-up dir for this user
193
// user.setHomeDirectory(arg);
194
} else if ("CREDITS".equals(param[0])) {
195                         user.setCredits(Long.parseLong(param[1]) * 1000);
196                     } else if ("RATIO".equals(param[0])) {
197                         user.setRatio(Float.parseFloat(param[1]));
198
199                         // Xfer information: FILES, KILOBYTES, SECONDS
200
} else if ("ALLUP".equals(param[0])) {
201                             user.setUploadedFiles(Integer.parseInt(param[1]));
202                             user.setUploadedBytes(Long.parseLong(param[2]) * 1000);
203                             user.setUploadedSeconds(Integer.parseInt(param[3]));
204                         } else if ("MONTHUP".equals(param[0])) {
205                             user.setUploadedFilesMonth(Integer.parseInt(param[1]));
206                             user.setUploadedBytesMonth(
207                                 Long.parseLong(param[2]) * 1000);
208                             user.setUploadedSecondsMonth(
209                                 Integer.parseInt(param[3]));
210                         } else if ("WKUP".equals(param[0])) {
211                             user.setUploadedFilesWeek(Integer.parseInt(param[1]));
212                             user.setUploadedBytesWeek(
213                                 Long.parseLong(param[2]) * 1000);
214                             user.setUploadedSecondsWeek(Integer.parseInt(param[3]));
215                         } else if ("DAYUP".equals(param[0])) {
216                             user.setUploadedFilesDay(Integer.parseInt(param[1]));
217                             user.setUploadedBytesDay(
218                                 Long.parseLong(param[2]) * 1000);
219                             user.setUploadedSecondsDay(Integer.parseInt(param[3]));
220                         
221                         } else if ("ALLDN".equals(param[0])) {
222                             user.setDownloadedFiles(Integer.parseInt(param[1]));
223                             user.setDownloadedBytes(
224                                 Long.parseLong(param[2]) * 1000);
225                             user.setDownloadedSeconds(Integer.parseInt(param[3]));
226                         } else if ("MONTHDN".equals(param[0])) {
227                             user.setDownloadedFilesMonth(
228                                 Integer.parseInt(param[1]));
229                             user.setDownloadedBytesMonth(
230                                 Long.parseLong(param[2]) * 1000);
231                             user.setDownloadedSecondsMonth(
232                                 Integer.parseInt(param[3]));
233                         } else if ("WKDN".equals(param[0])) {
234                             user.setDownloadedFilesMonth(
235                                 Integer.parseInt(param[1]));
236                             user.setDownloadedBytesMonth(
237                                 Long.parseLong(param[2]) * 1000);
238                             user.setDownloadedSecondsMonth(
239                                 Integer.parseInt(param[3]));
240                         } else if ("DAYDN".equals(param[0])) {
241                             user.setDownloadedFilesDay(Integer.parseInt(param[1]));
242                             user.setDownloadedBytesDay(
243                                 Long.parseLong(param[2]) * 1000);
244                             user.setDownloadedSecondsDay(
245                                 Integer.parseInt(param[3]));
246                         } else if ("TIME".equals(param[0])) {
247                             // TIME: Login Times, Last_On, Time Limit, Time on Today
248
user.setLogins(Integer.parseInt(param[1]));
249                             user.setLastAccessTime(Integer.parseInt(param[2]));
250                             //user.setTimelimit(Integer.parseInt(param[3]));
251
//user.setTimeToday(Integer.parseInt(param[4]));
252
} else if ("NUKE".equals(param[0])) {
253                             // NUKE: Last Nuked, Times Nuked, Total MBytes Nuked
254
user.setLastNuked(Integer.parseInt(param[1]));
255                             user.setTimesNuked(Integer.parseInt(param[2]));
256                             user.setNukedBytes(Long.parseLong(param[3]) * 1000000);
257                         
258                     } else if ("SLOTS".equals(param[0])) {
259                         //group slots, group leech slots
260
gluser.setGroupSlots(Short.parseShort(param[1]));
261                         gluser.setGroupLeechSlots(Short.parseShort(param[2]));
262                     } else if ("TIMEFRAME".equals(param[0])) {
263                     } else if ("GROUP".equals(param[0])) {
264                         if (temp == 0) {
265                             user.setGroup(arg);
266                             temp = 1;
267                         }
268                         user.addGroup(arg);
269                     } else if ("PRIVATE".equals(param[0])) {
270                         gluser.addPrivateGroup(arg);
271                     } else if ("IP".equals(param[0])) {
272                         user.addIPMask(arg);
273                     } else if (param[0].startsWith("#")) {
274                         //ignore comments
275
} else {
276                         logger.info("Unrecognized userfile entry: " + line);
277                     }
278                 }
279             } catch (DuplicateElementException e) {
280                 logger.warn("", e);
281             } finally {
282                 in.close();
283             }
284         }
285         {
286             BufferedReader JavaDoc in;
287             try {
288                 in = new BufferedReader JavaDoc(new FileReader JavaDoc(passwdfile));
289             } catch (FileNotFoundException JavaDoc ex) {
290                 throw new RuntimeException JavaDoc(ex.toString());
291             }
292             String JavaDoc line;
293             boolean foundpassword = false;
294             try {
295                 while ((line = in.readLine()) != null) {
296                     StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(line, ":");
297                     String JavaDoc username2 = st.nextToken();
298                     if (user.getUsername().equals(username2)) {
299                         gluser.setUnixPassword(st.nextToken());
300                         foundpassword = true;
301                         break;
302                     }
303                 }
304             } finally {
305                 in.close();
306             }
307             if (!foundpassword) {
308                 logger.warn("couldn't find a password in " + passwdfile);
309                 return;
310             }
311         }
312         // return user
313
}
314
315     public User getUserByNameUnchecked(String JavaDoc username)
316         throws UserFileException, NoSuchUserException {
317         if (!new File JavaDoc(getUserfilepath(username)).exists()) {
318             throw new NoSuchUserException("No userfile for user " + username);
319         }
320         GlftpdUser user = new GlftpdUser(username);
321         try {
322             load(user);
323             //throws CorruptUserFileException, NoSuchUserException
324
} catch (IOException JavaDoc e) {
325             throw new UserFileException(e);
326         }
327         return user;
328     }
329
330     public boolean exists(String JavaDoc name) {
331         throw new UnsupportedOperationException JavaDoc();
332     }
333
334     private String JavaDoc getUserfilepath(String JavaDoc username) {
335         return userdirpath + "/" + username;
336     }
337
338     public void saveAll() throws UserFileException {
339         throw new UnsupportedOperationException JavaDoc();
340     }
341     public Collection JavaDoc getAllUsersByGroup(String JavaDoc group)
342         throws UserFileException {
343         Collection JavaDoc users = getAllUsers();
344         for (Iterator JavaDoc iter = users.iterator(); iter.hasNext();) {
345             GlftpdUser user = (GlftpdUser) iter.next();
346             if (!user.getGroupName().equals(group))
347                 iter.remove();
348         }
349         return users;
350     }
351     public List JavaDoc getAllUsers() throws UserFileException {
352         if (!userpathFile.exists())
353             throw new UserFileException(userpathFile + " not found");
354         String JavaDoc userpaths[] = userpathFile.list();
355         ArrayList JavaDoc users = new ArrayList JavaDoc();
356
357         for (int i = 0; i < userpaths.length; i++) {
358             String JavaDoc userpath = userpaths[i];
359             logger.debug(userpath);
360             if (userpath.endsWith(".xml"))
361                 continue;
362             if (!new File JavaDoc(getUserfilepath(userpath)).isFile())
363                 continue;
364             if (userpath.endsWith(".user")) //default.user
365
continue;
366             logger.debug("add " + userpath);
367             try {
368                 users.add(getUserByName(userpath));
369                 // throws IOException
370
} catch (NoSuchUserException e) {
371                 throw new UserFileException("", e);
372             }
373         }
374         return users;
375     }
376     public Collection JavaDoc getAllGroups() throws UserFileException {
377         throw new UnsupportedOperationException JavaDoc();
378
379         /*Collection users = this.getAllUsers();
380         ArrayList ret = new ArrayList();
381         
382         for (Iterator iter = users.iterator(); iter.hasNext();) {
383             User myUser = (User) iter.next();
384             Collection myGroups = myUser.getGroups();
385             for (Iterator iterator = myGroups.iterator();
386                 iterator.hasNext();
387                 ) {
388                 String myGroup = (String) iterator.next();
389                 if (!ret.contains(myGroup))
390                     ret.add(myGroup);
391             }
392         }
393         
394         return ret;
395         */

396     }
397
398     public void init(ConnectionManager mgr) {
399     }
400
401     public User getUserByName(String JavaDoc username)
402         throws NoSuchUserException, UserFileException {
403         User user = getUserByNameUnchecked(username);
404         if (user.isDeleted())
405             throw new NoSuchUserException(user.getUsername() + " is deleted");
406         return user;
407     }
408
409 }
410
Popular Tags