LogService
libdadi: utility tools for distributed applications
|
00001 /****************************************************************************/ 00002 /* Log forwarder implementation - SSH Tunnel implementation */ 00003 /* */ 00004 /* Author(s): */ 00005 /* - Gael Le Mahec (gael.le.mahec@ens-lyon.fr) */ 00006 /* */ 00007 /* This file is part of DIET . */ 00008 /* */ 00009 /* Copyright (C) 2000-2003 ENS Lyon, LIFC, INSA, INRIA and SysFera (2000) */ 00010 /* */ 00011 /* - Frederic.Desprez@ens-lyon.fr (Project Manager) */ 00012 /* - Eddy.Caron@ens-lyon.fr (Technical Manager) */ 00013 /* - Tech@sysfera.com (Maintainer and Technical Support) */ 00014 /* */ 00015 /* This software is a computer program whose purpose is to provide an */ 00016 /* distributed logging services. */ 00017 /* */ 00018 /* */ 00019 /* This software is governed by the CeCILL license under French law and */ 00020 /* abiding by the rules of distribution of free software. You can use, */ 00021 /* modify and/ or redistribute the software under the terms of the CeCILL */ 00022 /* license as circulated by CEA, CNRS and INRIA at the following URL */ 00023 /* "http://www.cecill.info". */ 00024 /* */ 00025 /* As a counterpart to the access to the source code and rights to copy, */ 00026 /* modify and redistribute granted by the license, users are provided */ 00027 /* only with a limited warranty and the software's author, the holder */ 00028 /* of the economic rights, and the successive licensors have only */ 00029 /* limited liability. */ 00030 /* */ 00031 /* In this respect, the user's attention is drawn to the risks */ 00032 /* associated with loading, using, modifying and/or developing or */ 00033 /* reproducing the software by the user in light of its specific status */ 00034 /* of free software, that may mean that it is complicated to */ 00035 /* manipulate, and that also therefore means that it is reserved for */ 00036 /* developers and experienced professionals having in-depth computer */ 00037 /* knowledge. Users are therefore encouraged to load and test the */ 00038 /* software's suitability as regards their requirements in conditions */ 00039 /* enabling the security of their systems and/or data to be ensured and, */ 00040 /* more generally, to use and operate it in the same conditions as */ 00041 /* regards security. */ 00042 /* */ 00043 /* The fact that you are presently reading this means that you have had */ 00044 /* knowledge of the CeCILL license and that you accept its terms. */ 00045 /* */ 00046 /****************************************************************************/ 00047 00048 #ifndef SSHTUNNEL_HH 00049 #define SSHTUNNEL_HH 00050 00051 #include <string> 00052 00053 #include <csignal> 00054 #include <unistd.h> 00055 00056 class SSHConnection { 00057 public: 00058 SSHConnection(); 00059 00060 SSHConnection(const std::string& sshHost, const std::string& sshPort, 00061 const std::string& login, const std::string& keyPath, 00062 const std::string& sshPath); 00063 00064 const std::string& 00065 getSshHost() const; 00066 00067 const std::string& 00068 getSshPath() const; 00069 00070 const std::string& 00071 getSshPort() const; 00072 00073 const std::string& 00074 getSshLogin() const; 00075 00076 const std::string& 00077 getSshKeyPath() const; 00078 00079 const std::string& 00080 getSshOptions() const; 00081 00082 void 00083 setSshHost(const std::string& host); 00084 00085 void 00086 setSshPath(const std::string& path); 00087 00088 void 00089 setSshPort(const std::string& port); 00090 00091 void 00092 setSshPort(const int port); 00093 00094 void 00095 setSshLogin(const std::string& login); 00096 00097 void 00098 setSshKeyPath(const std::string& path); 00099 00100 void 00101 setSshOptions(const std::string& options); 00102 protected: 00103 /* Get the default user login and private key. */ 00104 static std::string 00105 userLogin(); 00106 00107 static std::string 00108 userKey(); 00109 private: 00110 /* SSH executable path. */ 00111 std::string sshPath; 00112 /* SSH connection params. */ 00113 std::string login; 00114 std::string keyPath; 00115 std::string sshHost; 00116 std::string sshPort; 00117 std::string options; 00118 }; 00119 00120 class SSHTunnel : public SSHConnection { 00121 public: 00122 SSHTunnel(); 00123 /* Constructor for bi-directionnal SSH tunnel. */ 00124 SSHTunnel(const std::string& sshHost, 00125 const std::string& remoteHost, 00126 const std::string& localPortFrom, 00127 const std::string& remotePortTo, 00128 const std::string& remotePortFrom, 00129 const std::string& localPortTo, 00130 const bool createTo = true, 00131 const bool createFrom = true, 00132 const std::string& sshPath = "/usr/bin/ssh", 00133 const std::string& sshPort = "22", 00134 const std::string& login = userLogin(), 00135 const std::string& keyPath = userKey()); 00136 00137 /* Constructor for unidirectionnal SSH tunnel. */ 00138 SSHTunnel(const std::string& sshHost, 00139 const std::string& remoteHost, 00140 const std::string& localPortFrom, 00141 const std::string& remotePortTo, 00142 const bool createTo = true, 00143 const std::string& sshPath = "/usr/bin/ssh", 00144 const std::string& serverPort = "22", 00145 const std::string& login = userLogin(), 00146 const std::string& keyPath = userKey()); 00147 00148 ~SSHTunnel(); 00149 00150 void 00151 open(); 00152 00153 void 00154 close(); 00155 00156 const std::string& 00157 getRemoteHost() const; 00158 00159 int 00160 getLocalPortFrom() const; 00161 00162 int 00163 getLocalPortTo() const; 00164 00165 int 00166 getRemotePortFrom() const; 00167 00168 int 00169 getRemotePortTo() const; 00170 00171 void 00172 setRemoteHost(const std::string& host); 00173 00174 void 00175 setLocalPortFrom(const std::string& port); 00176 00177 void 00178 setLocalPortFrom(const int port); 00179 00180 void 00181 setRemotePortTo(const std::string& port); 00182 00183 void 00184 setRemotePortTo(const int port); 00185 00186 void 00187 setRemotePortFrom(const std::string& port); 00188 00189 void 00190 setRemotePortFrom(const int port); 00191 00192 void 00193 setLocalPortTo(const std::string& port); 00194 00195 void 00196 setLocalPortTo(const int port); 00197 00198 void 00199 setWaitingTime(const unsigned int time); 00200 00201 void 00202 createTunnelTo(const bool create); 00203 00204 void 00205 createTunnelFrom(const bool create); 00206 00207 private: 00208 /* Format strings for ssh commands. */ 00209 static std::string cmdFormat; 00210 static std::string cmdFormatDefault; 00211 static std::string localFormat; 00212 static std::string remoteFormat; 00213 static std::string keyFormat; 00214 /* Tunnel configuration. */ 00215 bool createFrom; 00216 bool createTo; 00217 unsigned int waitingTime; 00218 std::string localPortTo; 00219 std::string localPortFrom; 00220 std::string remoteHost; 00221 std::string remotePortTo; 00222 std::string remotePortFrom; 00223 00224 /* Process pid. */ 00225 pid_t pid; 00226 00227 std::string 00228 makeCmd(); 00229 }; 00230 00231 /* Copy a file using scp. */ 00232 class SSHCopy : public SSHConnection { 00233 public: 00234 SSHCopy(const std::string& sshHost, 00235 const std::string& remoteFilename, 00236 const std::string& localFilename); 00237 bool 00238 getFile() const; 00239 00240 bool 00241 putFile() const; 00242 00243 private: 00244 std::string remoteFilename; 00245 std::string localFilename; 00246 00247 /* Process pid. */ 00248 mutable pid_t pid; 00249 }; 00250 00251 00252 std::string 00253 freeTCPport(); 00254 00255 #endif