1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package openr66.context.authentication;
22
23 import goldengate.common.command.NextCommandReply;
24 import goldengate.common.command.exception.Reply421Exception;
25 import goldengate.common.command.exception.Reply530Exception;
26 import goldengate.common.database.DbSession;
27 import goldengate.common.database.exception.GoldenGateDatabaseException;
28 import goldengate.common.file.DirInterface;
29 import goldengate.common.file.filesystembased.FilesystemBasedAuthImpl;
30 import goldengate.common.logging.GgInternalLogger;
31 import goldengate.common.logging.GgInternalLoggerFactory;
32
33 import java.io.File;
34
35 import openr66.context.R66Session;
36 import openr66.database.DbConstant;
37 import openr66.database.data.DbHostAuth;
38 import openr66.protocol.configuration.Configuration;
39
40
41
42
43
44 public class R66Auth extends FilesystemBasedAuthImpl {
45
46
47
48 private static final GgInternalLogger logger = GgInternalLoggerFactory
49 .getLogger(R66Auth.class);
50
51
52
53
54 private DbHostAuth currentAuth = null;
55
56
57
58 private boolean isAdmin = false;
59
60
61
62 public R66Auth(R66Session session) {
63 super(session);
64 }
65
66
67
68
69
70
71
72
73 @Override
74 protected void businessClean() {
75 currentAuth = null;
76 isAdmin = false;
77 }
78
79
80
81
82
83
84
85 @Override
86 public String getBaseDirectory() {
87 return Configuration.configuration.baseDirectory;
88 }
89
90
91
92
93
94
95
96 @Override
97 protected NextCommandReply setBusinessPassword(String arg0)
98 throws Reply421Exception, Reply530Exception {
99 throw new Reply421Exception("Command not valid");
100 }
101
102
103
104
105
106
107
108
109
110
111
112 public boolean connection(DbSession dbSession, String hostId, byte[] arg0)
113 throws Reply530Exception, Reply421Exception {
114 DbHostAuth auth = R66Auth
115 .getServerAuth(dbSession, hostId);
116 if (auth == null) {
117 logger.error("Cannot find authentication for "+hostId);
118 setIsIdentified(false);
119 currentAuth = null;
120 throw new Reply530Exception("HostId not allowed");
121 }
122 currentAuth = auth;
123 if (currentAuth.isKeyValid(arg0)) {
124 setIsIdentified(true);
125 user = hostId;
126 setRootFromAuth();
127 getSession().getDir().initAfterIdentification();
128 isAdmin = currentAuth.isAdminrole();
129 return true;
130 }
131 throw new Reply530Exception("Key is not valid for this HostId");
132 }
133
134
135
136
137
138
139 public boolean isKeyValid(byte[] key) {
140 return currentAuth.isKeyValid(key);
141 }
142
143
144
145
146
147
148
149
150
151 private void setRootFromAuth() throws Reply421Exception {
152 rootFromAuth = setBusinessRootFromAuth();
153 if (rootFromAuth == null) {
154 rootFromAuth = DirInterface.SEPARATOR;
155 }
156 }
157
158
159
160
161
162
163
164 @Override
165 protected String setBusinessRootFromAuth() throws Reply421Exception {
166 String path = null;
167 String fullpath = getAbsolutePath(path);
168 File file = new File(fullpath);
169 if (!file.isDirectory()) {
170 throw new Reply421Exception("Filesystem not ready");
171 }
172 return path;
173 }
174
175
176
177
178
179
180
181 @Override
182 protected NextCommandReply setBusinessUser(String arg0)
183 throws Reply421Exception, Reply530Exception {
184 throw new Reply421Exception("Command not valid");
185 }
186
187
188
189
190
191
192 @Override
193 public boolean isAdmin() {
194 return isAdmin;
195 }
196
197
198
199
200 public boolean isSsl() {
201 return currentAuth.isSsl();
202 }
203
204
205
206
207
208
209
210 @Override
211 public boolean isBusinessPathValid(String newPath) {
212 if (newPath == null) {
213 return false;
214 }
215 return true;
216 }
217
218 @Override
219 public String toString() {
220 return "Auth:" +isIdentified+" "+
221 (currentAuth != null? currentAuth.toString()
222 : "no Internal Auth");
223 }
224
225
226
227
228
229
230 public static DbHostAuth getServerAuth(DbSession dbSession, String server) {
231 DbHostAuth auth = null;
232 try {
233 auth = new DbHostAuth(dbSession, server);
234 } catch (GoldenGateDatabaseException e) {
235 logger.warn("Cannot find the authentication", e);
236 return null;
237 }
238 return auth;
239 }
240
241
242
243
244
245
246 public void specialNoSessionAuth(boolean isSSL, String hostid) {
247 this.isIdentified = true;
248 DbHostAuth auth = R66Auth.getServerAuth(DbConstant.admin.session,
249 hostid);
250 currentAuth = auth;
251 setIsIdentified(true);
252 user = auth.getHostid();
253 try {
254 setRootFromAuth();
255 } catch (Reply421Exception e) {
256 }
257 getSession().getDir().initAfterIdentification();
258 isAdmin = isSSL;
259 if (isSSL) {
260 this.user = Configuration.configuration.ADMINNAME;
261 }
262 }
263 }