1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.waarp.gateway.ftp.file;
22
23 import org.waarp.common.command.NextCommandReply;
24 import org.waarp.common.command.ReplyCode;
25 import org.waarp.common.command.exception.Reply421Exception;
26 import org.waarp.common.command.exception.Reply530Exception;
27 import org.waarp.common.database.DbConstant;
28 import org.waarp.common.file.DirInterface;
29 import org.waarp.common.logging.WaarpLogger;
30 import org.waarp.common.logging.WaarpLoggerFactory;
31 import org.waarp.ftp.core.command.FtpCommandCode;
32 import org.waarp.ftp.core.session.FtpSession;
33 import org.waarp.ftp.filesystembased.FilesystemBasedFtpAuth;
34 import org.waarp.ftp.filesystembased.FilesystemBasedFtpRestart;
35 import org.waarp.gateway.ftp.config.FileBasedConfiguration;
36 import org.waarp.gateway.ftp.exec.AbstractExecutor.CommandExecutor;
37 import org.waarp.gateway.kernel.session.HttpAuthInterface;
38
39 import java.io.File;
40
41
42
43
44
45
46 public class FileBasedAuth extends FilesystemBasedFtpAuth
47 implements HttpAuthInterface {
48
49
50
51 private static final WaarpLogger logger =
52 WaarpLoggerFactory.getLogger(FileBasedAuth.class);
53
54
55
56
57 private SimpleAuth currentAuth;
58
59
60
61
62 private long specialId = DbConstant.ILLEGALVALUE;
63
64
65
66
67 public FileBasedAuth(final FtpSession session) {
68 super(session);
69 }
70
71 @Override
72 protected final void businessClean() {
73 currentAuth = null;
74 }
75
76
77
78
79
80
81
82
83
84
85
86
87
88 @Override
89 protected final NextCommandReply setBusinessUser(final String user)
90 throws Reply530Exception {
91 final SimpleAuth auth =
92 ((FileBasedConfiguration) ((FtpSession) getSession()).getConfiguration()).getSimpleAuth(
93 user);
94 if (auth == null) {
95 setIsIdentified(false);
96 currentAuth = null;
97 throw new Reply530Exception("User name not allowed");
98 }
99 currentAuth = auth;
100 return new NextCommandReply(FtpCommandCode.PASS,
101 ReplyCode.REPLY_331_USER_NAME_OKAY_NEED_PASSWORD,
102 null);
103 }
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121 @Override
122 protected final NextCommandReply setBusinessPassword(final String password)
123 throws Reply530Exception {
124 if (currentAuth == null) {
125 setIsIdentified(false);
126 throw new Reply530Exception("PASS needs a USER first");
127 }
128 if (currentAuth.isPasswordValid(password)) {
129 return new NextCommandReply(FtpCommandCode.ACCT,
130 ReplyCode.REPLY_332_NEED_ACCOUNT_FOR_LOGIN,
131 null);
132 }
133 throw new Reply530Exception("Password is not valid");
134 }
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153 @Override
154 protected final NextCommandReply setBusinessAccount(final String account)
155 throws Reply530Exception {
156 if (currentAuth == null) {
157 throw new Reply530Exception("ACCT needs a USER first");
158 }
159 if (currentAuth.isAccountValid(account)) {
160 setIsIdentified(true);
161 logger.info("User {} is authentified with account {}", user, account);
162 return new NextCommandReply(FtpCommandCode.NOOP,
163 ReplyCode.REPLY_230_USER_LOGGED_IN, null);
164 }
165 throw new Reply530Exception("Account is not valid");
166 }
167
168 @Override
169 public final boolean isBusinessPathValid(final String newPath) {
170 if (newPath == null) {
171 return false;
172 }
173 return newPath.startsWith(getBusinessPath());
174 }
175
176 @Override
177 protected final String setBusinessRootFromAuth() throws Reply421Exception {
178 final String path;
179 if (account == null) {
180 path = DirInterface.SEPARATOR + user;
181 } else {
182 path = DirInterface.SEPARATOR + user + DirInterface.SEPARATOR + account;
183 }
184 final String fullpath = getAbsolutePath(path);
185 final File file = new File(fullpath);
186 if (!file.isDirectory()) {
187 throw new Reply421Exception("Filesystem not ready");
188 }
189 return path;
190 }
191
192 @Override
193 public final boolean isAdmin() {
194 if (currentAuth == null) {
195 return false;
196 }
197 return currentAuth.isAdmin();
198 }
199
200
201
202
203
204
205 public final void specialNoSessionAuth(final String hostid) {
206 isIdentified = true;
207 final SimpleAuth auth =
208 new SimpleAuth(hostid, hostid, null, null, 0, null, 0);
209 currentAuth = auth;
210 setIsIdentified(true);
211 user = auth.getUser();
212 account = auth.getUser();
213 ((FtpSession) getSession()).setSpecialInit(this, new FileBasedDir(
214 (FtpSession) getSession()), new FilesystemBasedFtpRestart(
215 (FtpSession) getSession()));
216 try {
217 setBusinessRootFromAuth();
218 } catch (final Reply421Exception ignored) {
219
220 }
221 getSession().getDir().initAfterIdentification();
222 currentAuth.setAdmin(true);
223 }
224
225
226
227
228 public final long getSpecialId() {
229 return specialId;
230 }
231
232
233
234
235 public final void setSpecialId(final long specialId) {
236 this.specialId = specialId;
237 }
238
239
240
241
242 @Override
243 public final CommandExecutor getCommandExecutor() {
244 return currentAuth.getCommandExecutor();
245 }
246 }