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