LogService
libdadi: utility tools for distributed applications
SSHTunnel.hh
1 /****************************************************************************/
2 /* Log forwarder implementation - SSH Tunnel implementation */
3 /* */
4 /* Author(s): */
5 /* - Gael Le Mahec (gael.le.mahec@ens-lyon.fr) */
6 /* */
7 /* This file is part of DIET . */
8 /* */
9 /* Copyright (C) 2000-2003 ENS Lyon, LIFC, INSA, INRIA and SysFera (2000) */
10 /* */
11 /* - Frederic.Desprez@ens-lyon.fr (Project Manager) */
12 /* - Eddy.Caron@ens-lyon.fr (Technical Manager) */
13 /* - Tech@sysfera.com (Maintainer and Technical Support) */
14 /* */
15 /* This software is a computer program whose purpose is to provide an */
16 /* distributed logging services. */
17 /* */
18 /* */
19 /* This software is governed by the CeCILL license under French law and */
20 /* abiding by the rules of distribution of free software. You can use, */
21 /* modify and/ or redistribute the software under the terms of the CeCILL */
22 /* license as circulated by CEA, CNRS and INRIA at the following URL */
23 /* "http://www.cecill.info". */
24 /* */
25 /* As a counterpart to the access to the source code and rights to copy, */
26 /* modify and redistribute granted by the license, users are provided */
27 /* only with a limited warranty and the software's author, the holder */
28 /* of the economic rights, and the successive licensors have only */
29 /* limited liability. */
30 /* */
31 /* In this respect, the user's attention is drawn to the risks */
32 /* associated with loading, using, modifying and/or developing or */
33 /* reproducing the software by the user in light of its specific status */
34 /* of free software, that may mean that it is complicated to */
35 /* manipulate, and that also therefore means that it is reserved for */
36 /* developers and experienced professionals having in-depth computer */
37 /* knowledge. Users are therefore encouraged to load and test the */
38 /* software's suitability as regards their requirements in conditions */
39 /* enabling the security of their systems and/or data to be ensured and, */
40 /* more generally, to use and operate it in the same conditions as */
41 /* regards security. */
42 /* */
43 /* The fact that you are presently reading this means that you have had */
44 /* knowledge of the CeCILL license and that you accept its terms. */
45 /* */
46 /****************************************************************************/
47 
48 #ifndef SSHTUNNEL_HH
49 #define SSHTUNNEL_HH
50 
51 #include <string>
52 
53 #include <csignal>
54 #include <unistd.h>
55 
57 public:
58  SSHConnection();
59 
60  SSHConnection(const std::string& sshHost, const std::string& sshPort,
61  const std::string& login, const std::string& keyPath,
62  const std::string& sshPath);
63 
64  const std::string&
65  getSshHost() const;
66 
67  const std::string&
68  getSshPath() const;
69 
70  const std::string&
71  getSshPort() const;
72 
73  const std::string&
74  getSshLogin() const;
75 
76  const std::string&
77  getSshKeyPath() const;
78 
79  const std::string&
80  getSshOptions() const;
81 
82  void
83  setSshHost(const std::string& host);
84 
85  void
86  setSshPath(const std::string& path);
87 
88  void
89  setSshPort(const std::string& port);
90 
91  void
92  setSshPort(const int port);
93 
94  void
95  setSshLogin(const std::string& login);
96 
97  void
98  setSshKeyPath(const std::string& path);
99 
100  void
101  setSshOptions(const std::string& options);
102 protected:
103  /* Get the default user login and private key. */
104  static std::string
105  userLogin();
106 
107  static std::string
108  userKey();
109 private:
110  /* SSH executable path. */
111  std::string sshPath;
112  /* SSH connection params. */
113  std::string login;
114  std::string keyPath;
115  std::string sshHost;
116  std::string sshPort;
117  std::string options;
118 };
119 
120 class SSHTunnel : public SSHConnection {
121 public:
122  SSHTunnel();
123  /* Constructor for bi-directionnal SSH tunnel. */
124  SSHTunnel(const std::string& sshHost,
125  const std::string& remoteHost,
126  const std::string& localPortFrom,
127  const std::string& remotePortTo,
128  const std::string& remotePortFrom,
129  const std::string& localPortTo,
130  const bool createTo = true,
131  const bool createFrom = true,
132  const std::string& sshPath = "/usr/bin/ssh",
133  const std::string& sshPort = "22",
134  const std::string& login = userLogin(),
135  const std::string& keyPath = userKey());
136 
137  /* Constructor for unidirectionnal SSH tunnel. */
138  SSHTunnel(const std::string& sshHost,
139  const std::string& remoteHost,
140  const std::string& localPortFrom,
141  const std::string& remotePortTo,
142  const bool createTo = true,
143  const std::string& sshPath = "/usr/bin/ssh",
144  const std::string& serverPort = "22",
145  const std::string& login = userLogin(),
146  const std::string& keyPath = userKey());
147 
148  ~SSHTunnel();
149 
150  void
151  open();
152 
153  void
154  close();
155 
156  const std::string&
157  getRemoteHost() const;
158 
159  int
160  getLocalPortFrom() const;
161 
162  int
163  getLocalPortTo() const;
164 
165  int
166  getRemotePortFrom() const;
167 
168  int
169  getRemotePortTo() const;
170 
171  void
172  setRemoteHost(const std::string& host);
173 
174  void
175  setLocalPortFrom(const std::string& port);
176 
177  void
178  setLocalPortFrom(const int port);
179 
180  void
181  setRemotePortTo(const std::string& port);
182 
183  void
184  setRemotePortTo(const int port);
185 
186  void
187  setRemotePortFrom(const std::string& port);
188 
189  void
190  setRemotePortFrom(const int port);
191 
192  void
193  setLocalPortTo(const std::string& port);
194 
195  void
196  setLocalPortTo(const int port);
197 
198  void
199  setWaitingTime(const unsigned int time);
200 
201  void
202  createTunnelTo(const bool create);
203 
204  void
205  createTunnelFrom(const bool create);
206 
207 private:
208  /* Format strings for ssh commands. */
209  static std::string cmdFormat;
210  static std::string cmdFormatDefault;
211  static std::string localFormat;
212  static std::string remoteFormat;
213  static std::string keyFormat;
214  /* Tunnel configuration. */
215  bool createFrom;
216  bool createTo;
217  unsigned int waitingTime;
218  std::string localPortTo;
219  std::string localPortFrom;
220  std::string remoteHost;
221  std::string remotePortTo;
222  std::string remotePortFrom;
223 
224  /* Process pid. */
225  pid_t pid;
226 
227  std::string
228  makeCmd();
229 };
230 
231 /* Copy a file using scp. */
232 class SSHCopy : public SSHConnection {
233 public:
234  SSHCopy(const std::string& sshHost,
235  const std::string& remoteFilename,
236  const std::string& localFilename);
237  bool
238  getFile() const;
239 
240  bool
241  putFile() const;
242 
243 private:
244  std::string remoteFilename;
245  std::string localFilename;
246 
247  /* Process pid. */
248  mutable pid_t pid;
249 };
250 
251 
252 std::string
253 freeTCPport();
254 
255 #endif
Definition: SSHTunnel.hh:232
Definition: SSHTunnel.hh:120
Definition: SSHTunnel.hh:56