1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.waarp.ftp.simpleimpl.file;
19
20 import java.io.File;
21
22 import org.waarp.common.command.NextCommandReply;
23 import org.waarp.common.command.ReplyCode;
24 import org.waarp.common.command.exception.Reply421Exception;
25 import org.waarp.common.command.exception.Reply502Exception;
26 import org.waarp.common.command.exception.Reply530Exception;
27 import org.waarp.common.logging.WaarpLogger;
28 import org.waarp.common.logging.WaarpLoggerFactory;
29 import org.waarp.ftp.core.command.FtpCommandCode;
30 import org.waarp.ftp.core.file.FtpDir;
31 import org.waarp.ftp.core.session.FtpSession;
32 import org.waarp.ftp.filesystembased.FilesystemBasedFtpAuth;
33 import org.waarp.ftp.simpleimpl.config.FileBasedConfiguration;
34
35
36
37
38
39
40
41
42 public class FileBasedAuth extends FilesystemBasedFtpAuth {
43
44
45
46 private static final WaarpLogger logger = WaarpLoggerFactory
47 .getLogger(FileBasedAuth.class);
48
49
50
51
52 private SimpleAuth currentAuth = null;
53
54
55
56
57 public FileBasedAuth(FtpSession session) {
58 super(session);
59 }
60
61 @Override
62 protected void businessClean() {
63 currentAuth = null;
64 }
65
66
67
68
69
70
71
72
73
74
75
76 @Override
77 protected NextCommandReply setBusinessUser(String user)
78 throws Reply421Exception, Reply530Exception {
79 SimpleAuth auth = ((FileBasedConfiguration) ((FtpSession) getSession())
80 .getConfiguration()).getSimpleAuth(user);
81 if (auth == null) {
82 setIsIdentified(false);
83 currentAuth = null;
84 throw new Reply530Exception("User name not allowed");
85 }
86 currentAuth = auth;
87
88 return new NextCommandReply(FtpCommandCode.PASS,
89 ReplyCode.REPLY_331_USER_NAME_OKAY_NEED_PASSWORD, null);
90 }
91
92
93
94
95
96
97
98
99
100
101
102
103
104 @Override
105 protected NextCommandReply setBusinessPassword(String password)
106 throws Reply421Exception, Reply530Exception {
107 if (currentAuth == null) {
108 setIsIdentified(false);
109 throw new Reply530Exception("PASS needs a USER first");
110 }
111 if (currentAuth.isPasswordValid(password)) {
112 if (user.equals("test")) {
113
114 try {
115 return setAccount("test");
116 } catch (Reply502Exception e) {
117 }
118 }
119 return new NextCommandReply(FtpCommandCode.ACCT,
120 ReplyCode.REPLY_332_NEED_ACCOUNT_FOR_LOGIN, null);
121 }
122 throw new Reply530Exception("Password is not valid");
123 }
124
125
126
127
128
129
130
131
132
133
134
135
136
137 @Override
138 protected NextCommandReply setBusinessAccount(String account)
139 throws Reply421Exception, Reply530Exception {
140 if (currentAuth == null) {
141 throw new Reply530Exception("ACCT needs a USER first");
142 }
143 if (currentAuth.isAccountValid(account)) {
144
145 setIsIdentified(true);
146 logger.info("User {} is authentified with account {}", user,
147 account);
148 return new NextCommandReply(FtpCommandCode.NOOP,
149 ReplyCode.REPLY_230_USER_LOGGED_IN, null);
150 }
151 throw new Reply530Exception("Account is not valid");
152 }
153
154 public boolean isBusinessPathValid(String newPath) {
155 if (newPath == null) {
156 return false;
157 }
158 return newPath.startsWith(getBusinessPath());
159 }
160
161 @Override
162 protected String setBusinessRootFromAuth() throws Reply421Exception {
163 String path = null;
164 if (account == null) {
165 path = FtpDir.SEPARATOR + user;
166 } else {
167 path = FtpDir.SEPARATOR + user + FtpDir.SEPARATOR +
168 account;
169 }
170 String fullpath = getAbsolutePath(path);
171 File file = new File(fullpath);
172 if (!file.isDirectory()) {
173 throw new Reply421Exception("Filesystem not ready");
174 }
175 return path;
176 }
177
178 public boolean isAdmin() {
179 return currentAuth.isAdmin();
180 }
181 }