1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.waarp.gateway.ftp.file;
18
19 import java.io.File;
20
21 import org.waarp.common.command.NextCommandReply;
22 import org.waarp.common.command.ReplyCode;
23 import org.waarp.common.command.exception.Reply421Exception;
24 import org.waarp.common.command.exception.Reply530Exception;
25 import org.waarp.common.logging.WaarpLogger;
26 import org.waarp.common.logging.WaarpLoggerFactory;
27 import org.waarp.ftp.core.command.FtpCommandCode;
28 import org.waarp.ftp.core.file.FtpDir;
29 import org.waarp.ftp.core.session.FtpSession;
30 import org.waarp.ftp.filesystembased.FilesystemBasedFtpAuth;
31 import org.waarp.ftp.filesystembased.FilesystemBasedFtpRestart;
32 import org.waarp.gateway.ftp.config.FileBasedConfiguration;
33 import org.waarp.gateway.ftp.database.DbConstant;
34 import org.waarp.gateway.kernel.exec.AbstractExecutor.CommandExecutor;
35 import org.waarp.gateway.kernel.session.HttpAuthInterface;
36
37
38
39
40
41
42
43
44 public class FileBasedAuth extends FilesystemBasedFtpAuth implements HttpAuthInterface {
45
46
47
48 private static final WaarpLogger logger = WaarpLoggerFactory
49 .getLogger(FileBasedAuth.class);
50
51
52
53
54 private SimpleAuth currentAuth = null;
55
56
57
58
59 private long specialId = DbConstant.ILLEGALVALUE;
60
61
62
63
64 public FileBasedAuth(FtpSession session) {
65 super(session);
66 }
67
68 @Override
69 protected void businessClean() {
70 currentAuth = null;
71 }
72
73
74
75
76
77
78
79
80
81
82
83 @Override
84 protected NextCommandReply setBusinessUser(String user)
85 throws Reply421Exception, Reply530Exception {
86 SimpleAuth auth = ((FileBasedConfiguration) ((FtpSession) getSession())
87 .getConfiguration()).getSimpleAuth(user);
88 if (auth == null) {
89 setIsIdentified(false);
90 currentAuth = null;
91 throw new Reply530Exception("User name not allowed");
92 }
93 currentAuth = auth;
94
95 return new NextCommandReply(FtpCommandCode.PASS,
96 ReplyCode.REPLY_331_USER_NAME_OKAY_NEED_PASSWORD, null);
97 }
98
99
100
101
102
103
104
105
106
107
108
109
110
111 @Override
112 protected NextCommandReply setBusinessPassword(String password)
113 throws Reply421Exception, Reply530Exception {
114 if (currentAuth == null) {
115 setIsIdentified(false);
116 throw new Reply530Exception("PASS needs a USER first");
117 }
118 if (currentAuth.isPasswordValid(password)) {
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 if (currentAuth == null)
180 return false;
181 return currentAuth.isAdmin();
182 }
183
184
185
186
187
188
189 public void specialNoSessionAuth(String hostid) {
190 this.isIdentified = true;
191 SimpleAuth auth = new SimpleAuth(hostid, hostid, null, null, 0, null, 0);
192 currentAuth = auth;
193 setIsIdentified(true);
194 user = auth.getUser();
195 account = auth.getUser();
196 ((FtpSession) getSession()).setSpecialInit(this,
197 new FileBasedDir(((FtpSession) getSession())),
198 new FilesystemBasedFtpRestart(((FtpSession) getSession())));
199 try {
200 setBusinessRootFromAuth();
201 } catch (Reply421Exception e) {
202 }
203 getSession().getDir().initAfterIdentification();
204 currentAuth.setAdmin(true);
205 }
206
207
208
209
210 public long getSpecialId() {
211 return specialId;
212 }
213
214
215
216
217
218 public void setSpecialId(long specialId) {
219 this.specialId = specialId;
220 }
221
222
223
224
225
226 public CommandExecutor getCommandExecutor() {
227 return this.currentAuth.getCommandExecutor();
228 }
229 }