View Javadoc

1   /**
2      This file is part of GoldenGate Project (named also GoldenGate or GG).
3   
4      Copyright 2009, Frederic Bregier, and individual contributors by the @author
5      tags. See the COPYRIGHT.txt in the distribution for a full listing of
6      individual contributors.
7   
8      All GoldenGate Project is free software: you can redistribute it and/or 
9      modify it under the terms of the GNU General Public License as published 
10     by the Free Software Foundation, either version 3 of the License, or
11     (at your option) any later version.
12  
13     GoldenGate is distributed in the hope that it will be useful,
14     but WITHOUT ANY WARRANTY; without even the implied warranty of
15     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16     GNU General Public License for more details.
17  
18     You should have received a copy of the GNU General Public License
19     along with GoldenGate .  If not, see <http://www.gnu.org/licenses/>.
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   * @author frederic bregier
42   *
43   */
44  public class R66Auth extends FilesystemBasedAuthImpl {
45      /**
46       * Internal Logger
47       */
48      private static final GgInternalLogger logger = GgInternalLoggerFactory
49              .getLogger(R66Auth.class);
50  
51      /**
52       * Current authentication
53       */
54      private DbHostAuth currentAuth = null;
55      /**
56       * is Admin role
57       */
58      private boolean isAdmin = false;
59      /**
60       * @param session
61       */
62      public R66Auth(R66Session session) {
63          super(session);
64      }
65  
66      /*
67       * (non-Javadoc)
68       *
69       * @see
70       * goldengate.common.file.filesystembased.FilesystemBasedAuthImpl#businessClean
71       * ()
72       */
73      @Override
74      protected void businessClean() {
75          currentAuth = null;
76          isAdmin = false;
77      }
78  
79      /*
80       * (non-Javadoc)
81       *
82       * @seegoldengate.common.file.filesystembased.FilesystemBasedAuthImpl#
83       * getBaseDirectory()
84       */
85      @Override
86      public String getBaseDirectory() {
87          return Configuration.configuration.baseDirectory;
88      }
89  
90      /*
91       * (non-Javadoc)
92       *
93       * @seegoldengate.common.file.filesystembased.FilesystemBasedAuthImpl#
94       * setBusinessPassword(java.lang.String)
95       */
96      @Override
97      protected NextCommandReply setBusinessPassword(String arg0)
98              throws Reply421Exception, Reply530Exception {
99          throw new Reply421Exception("Command not valid");
100     }
101 
102     /**
103      * @param dbSession
104      * @param hostId
105      * @param arg0
106      * @return True if the connection is OK (authentication is OK)
107      * @throws Reply530Exception
108      *             if the authentication is wrong
109      * @throws Reply421Exception
110      *             If the service is not available
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      * @param key
137      * @return True if the key is valid for the current user
138      */
139     public boolean isKeyValid(byte[] key) {
140         return currentAuth.isKeyValid(key);
141     }
142 
143     /**
144      * Set the root relative Path from current status of Authentication (should
145      * be the highest level for the current authentication). If
146      * setBusinessRootFromAuth returns null, by default set /user.
147      *
148      * @exception Reply421Exception
149      *                if the business root is not available
150      */
151     private void setRootFromAuth() throws Reply421Exception {
152         rootFromAuth = setBusinessRootFromAuth();
153         if (rootFromAuth == null) {
154             rootFromAuth = DirInterface.SEPARATOR;
155         }
156     }
157 
158     /*
159      * (non-Javadoc)
160      *
161      * @seegoldengate.common.file.filesystembased.FilesystemBasedAuthImpl#
162      * setBusinessRootFromAuth()
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      * (non-Javadoc)
177      *
178      * @seegoldengate.common.file.filesystembased.FilesystemBasedAuthImpl#
179      * setBusinessUser(java.lang.String)
180      */
181     @Override
182     protected NextCommandReply setBusinessUser(String arg0)
183             throws Reply421Exception, Reply530Exception {
184         throw new Reply421Exception("Command not valid");
185     }
186 
187     /*
188      * (non-Javadoc)
189      *
190      * @see goldengate.common.file.AuthInterface#isAdmin()
191      */
192     @Override
193     public boolean isAdmin() {
194         return isAdmin;
195     }
196     /**
197      *
198      * @return True if the associated host is using SSL
199      */
200     public boolean isSsl() {
201         return currentAuth.isSsl();
202     }
203     /*
204      * (non-Javadoc)
205      *
206      * @see
207      * goldengate.common.file.AuthInterface#isBusinessPathValid(java.lang.String
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      * @param dbSession
227      * @param server
228      * @return the SimpleAuth if any for this user
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      * Special Authentication for local execution
243      * @param isSSL
244      * @param hostid
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 }