• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #ifndef HEADER_CURL_SSH_H
2 #define HEADER_CURL_SSH_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
11  *
12  * This software is licensed as described in the file COPYING, which
13  * you should have received as part of this distribution. The terms
14  * are also available at https://curl.se/docs/copyright.html.
15  *
16  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
17  * copies of the Software, and permit persons to whom the Software is
18  * furnished to do so, under the terms of the COPYING file.
19  *
20  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
21  * KIND, either express or implied.
22  *
23  * SPDX-License-Identifier: curl
24  *
25  ***************************************************************************/
26 
27 #include "curl_setup.h"
28 
29 #if defined(USE_LIBSSH2)
30 #include <libssh2.h>
31 #include <libssh2_sftp.h>
32 #elif defined(USE_LIBSSH)
33 /* in 0.10.0 or later, ignore deprecated warnings */
34 #define SSH_SUPPRESS_DEPRECATED
35 #include <libssh/libssh.h>
36 #include <libssh/sftp.h>
37 #elif defined(USE_WOLFSSH)
38 #include <wolfssh/ssh.h>
39 #include <wolfssh/wolfsftp.h>
40 #endif
41 
42 #include "curl_path.h"
43 
44 /****************************************************************************
45  * SSH unique setup
46  ***************************************************************************/
47 typedef enum {
48   SSH_NO_STATE = -1,  /* Used for "nextState" so say there is none */
49   SSH_STOP = 0,       /* do nothing state, stops the state machine */
50 
51   SSH_INIT,           /* First state in SSH-CONNECT */
52   SSH_S_STARTUP,      /* Session startup */
53   SSH_HOSTKEY,        /* verify hostkey */
54   SSH_AUTHLIST,
55   SSH_AUTH_PKEY_INIT,
56   SSH_AUTH_PKEY,
57   SSH_AUTH_PASS_INIT,
58   SSH_AUTH_PASS,
59   SSH_AUTH_AGENT_INIT, /* initialize then wait for connection to agent */
60   SSH_AUTH_AGENT_LIST, /* ask for list then wait for entire list to come */
61   SSH_AUTH_AGENT,      /* attempt one key at a time */
62   SSH_AUTH_HOST_INIT,
63   SSH_AUTH_HOST,
64   SSH_AUTH_KEY_INIT,
65   SSH_AUTH_KEY,
66   SSH_AUTH_GSSAPI,
67   SSH_AUTH_DONE,
68   SSH_SFTP_INIT,
69   SSH_SFTP_REALPATH,   /* Last state in SSH-CONNECT */
70 
71   SSH_SFTP_QUOTE_INIT, /* First state in SFTP-DO */
72   SSH_SFTP_POSTQUOTE_INIT, /* (Possibly) First state in SFTP-DONE */
73   SSH_SFTP_QUOTE,
74   SSH_SFTP_NEXT_QUOTE,
75   SSH_SFTP_QUOTE_STAT,
76   SSH_SFTP_QUOTE_SETSTAT,
77   SSH_SFTP_QUOTE_SYMLINK,
78   SSH_SFTP_QUOTE_MKDIR,
79   SSH_SFTP_QUOTE_RENAME,
80   SSH_SFTP_QUOTE_RMDIR,
81   SSH_SFTP_QUOTE_UNLINK,
82   SSH_SFTP_QUOTE_STATVFS,
83   SSH_SFTP_GETINFO,
84   SSH_SFTP_FILETIME,
85   SSH_SFTP_TRANS_INIT,
86   SSH_SFTP_UPLOAD_INIT,
87   SSH_SFTP_CREATE_DIRS_INIT,
88   SSH_SFTP_CREATE_DIRS,
89   SSH_SFTP_CREATE_DIRS_MKDIR,
90   SSH_SFTP_READDIR_INIT,
91   SSH_SFTP_READDIR,
92   SSH_SFTP_READDIR_LINK,
93   SSH_SFTP_READDIR_BOTTOM,
94   SSH_SFTP_READDIR_DONE,
95   SSH_SFTP_DOWNLOAD_INIT,
96   SSH_SFTP_DOWNLOAD_STAT, /* Last state in SFTP-DO */
97   SSH_SFTP_CLOSE,    /* Last state in SFTP-DONE */
98   SSH_SFTP_SHUTDOWN, /* First state in SFTP-DISCONNECT */
99   SSH_SCP_TRANS_INIT, /* First state in SCP-DO */
100   SSH_SCP_UPLOAD_INIT,
101   SSH_SCP_DOWNLOAD_INIT,
102   SSH_SCP_DOWNLOAD,
103   SSH_SCP_DONE,
104   SSH_SCP_SEND_EOF,
105   SSH_SCP_WAIT_EOF,
106   SSH_SCP_WAIT_CLOSE,
107   SSH_SCP_CHANNEL_FREE,   /* Last state in SCP-DONE */
108   SSH_SESSION_DISCONNECT, /* First state in SCP-DISCONNECT */
109   SSH_SESSION_FREE,       /* Last state in SCP/SFTP-DISCONNECT */
110   SSH_QUIT,
111   SSH_LAST  /* never used */
112 } sshstate;
113 
114 #define CURL_PATH_MAX 1024
115 
116 /* this struct is used in the HandleData struct which is part of the
117    Curl_easy, which means this is used on a per-easy handle basis.
118    Everything that is strictly related to a connection is banned from this
119    struct. */
120 struct SSHPROTO {
121   char *path;                  /* the path we operate on */
122 #ifdef USE_LIBSSH2
123   struct dynbuf readdir_link;
124   struct dynbuf readdir;
125   char readdir_filename[CURL_PATH_MAX + 1];
126   char readdir_longentry[CURL_PATH_MAX + 1];
127 
128   LIBSSH2_SFTP_ATTRIBUTES quote_attrs; /* used by the SFTP_QUOTE state */
129 
130   /* Here's a set of struct members used by the SFTP_READDIR state */
131   LIBSSH2_SFTP_ATTRIBUTES readdir_attrs;
132 #endif
133 };
134 
135 /* ssh_conn is used for struct connection-oriented data in the connectdata
136    struct */
137 struct ssh_conn {
138   const char *authlist;       /* List of auth. methods, managed by libssh2 */
139 
140   /* common */
141   const char *passphrase;     /* pass-phrase to use */
142   char *rsa_pub;              /* strdup'ed public key file */
143   char *rsa;                  /* strdup'ed private key file */
144   bool authed;                /* the connection has been authenticated fine */
145   bool acceptfail;            /* used by the SFTP_QUOTE (continue if
146                                  quote command fails) */
147   sshstate state;             /* always use ssh.c:state() to change state! */
148   sshstate nextstate;         /* the state to goto after stopping */
149   CURLcode actualcode;        /* the actual error code */
150   struct curl_slist *quote_item; /* for the quote option */
151   char *quote_path1;          /* two generic pointers for the QUOTE stuff */
152   char *quote_path2;
153 
154   char *homedir;              /* when doing SFTP we figure out home dir in the
155                                  connect phase */
156   /* end of READDIR stuff */
157 
158   int secondCreateDirs;         /* counter use by the code to see if the
159                                    second attempt has been made to change
160                                    to/create a directory */
161   int orig_waitfor;             /* default READ/WRITE bits wait for */
162   char *slash_pos;              /* used by the SFTP_CREATE_DIRS state */
163 
164 #if defined(USE_LIBSSH)
165   char *readdir_linkPath;
166   size_t readdir_len;
167   struct dynbuf readdir_buf;
168 /* our variables */
169   unsigned kbd_state; /* 0 or 1 */
170   ssh_key privkey;
171   ssh_key pubkey;
172   unsigned int auth_methods;
173   ssh_session ssh_session;
174   ssh_scp scp_session;
175   sftp_session sftp_session;
176   sftp_file sftp_file;
177   sftp_dir sftp_dir;
178 
179   unsigned sftp_recv_state; /* 0 or 1 */
180 #if LIBSSH_VERSION_INT > SSH_VERSION_INT(0, 11, 0)
181   sftp_aio sftp_aio;
182   unsigned sftp_send_state; /* 0 or 1 */
183 #endif
184   int sftp_file_index; /* for async read */
185   sftp_attributes readdir_attrs; /* used by the SFTP readdir actions */
186   sftp_attributes readdir_link_attrs; /* used by the SFTP readdir actions */
187   sftp_attributes quote_attrs; /* used by the SFTP_QUOTE state */
188 
189   const char *readdir_filename; /* points within readdir_attrs */
190   const char *readdir_longentry;
191   char *readdir_tmp;
192 #elif defined(USE_LIBSSH2)
193   LIBSSH2_SESSION *ssh_session; /* Secure Shell session */
194   LIBSSH2_CHANNEL *ssh_channel; /* Secure Shell channel handle */
195   LIBSSH2_SFTP *sftp_session;   /* SFTP handle */
196   LIBSSH2_SFTP_HANDLE *sftp_handle;
197 
198 #ifndef CURL_DISABLE_PROXY
199   /* for HTTPS proxy storage */
200   Curl_recv *tls_recv;
201   Curl_send *tls_send;
202 #endif
203 
204 #ifdef HAVE_LIBSSH2_AGENT_API
205   LIBSSH2_AGENT *ssh_agent;     /* proxy to ssh-agent/pageant */
206   struct libssh2_agent_publickey *sshagent_identity,
207                                  *sshagent_prev_identity;
208 #endif
209 
210   /* note that HAVE_LIBSSH2_KNOWNHOST_API is a define set in the libssh2.h
211      header */
212 #ifdef HAVE_LIBSSH2_KNOWNHOST_API
213   LIBSSH2_KNOWNHOSTS *kh;
214 #endif
215 #elif defined(USE_WOLFSSH)
216   WOLFSSH *ssh_session;
217   WOLFSSH_CTX *ctx;
218   word32 handleSz;
219   byte handle[WOLFSSH_MAX_HANDLE];
220   curl_off_t offset;
221 #endif /* USE_LIBSSH */
222 };
223 
224 #if defined(USE_LIBSSH2)
225 
226 /* Feature detection based on version numbers to better work with
227    non-configure platforms */
228 
229 #if !defined(LIBSSH2_VERSION_NUM) || (LIBSSH2_VERSION_NUM < 0x001000)
230 #  error "SCP/SFTP protocols require libssh2 0.16 or later"
231 #endif
232 
233 #if LIBSSH2_VERSION_NUM >= 0x010000
234 #define HAVE_LIBSSH2_SFTP_SEEK64 1
235 #endif
236 
237 #if LIBSSH2_VERSION_NUM >= 0x010100
238 #define HAVE_LIBSSH2_VERSION 1
239 #endif
240 
241 #if LIBSSH2_VERSION_NUM >= 0x010205
242 #define HAVE_LIBSSH2_INIT 1
243 #define HAVE_LIBSSH2_EXIT 1
244 #endif
245 
246 #if LIBSSH2_VERSION_NUM >= 0x010206
247 #define HAVE_LIBSSH2_KNOWNHOST_CHECKP 1
248 #define HAVE_LIBSSH2_SCP_SEND64 1
249 #endif
250 
251 #if LIBSSH2_VERSION_NUM >= 0x010208
252 #define HAVE_LIBSSH2_SESSION_HANDSHAKE 1
253 #endif
254 
255 #ifdef HAVE_LIBSSH2_VERSION
256 /* get it runtime if possible */
257 #define CURL_LIBSSH2_VERSION libssh2_version(0)
258 #else
259 /* use build-time if runtime not possible */
260 #define CURL_LIBSSH2_VERSION LIBSSH2_VERSION
261 #endif
262 
263 #endif /* USE_LIBSSH2 */
264 
265 #ifdef USE_SSH
266 
267 extern const struct Curl_handler Curl_handler_scp;
268 extern const struct Curl_handler Curl_handler_sftp;
269 
270 /* generic SSH backend functions */
271 CURLcode Curl_ssh_init(void);
272 void Curl_ssh_cleanup(void);
273 void Curl_ssh_version(char *buffer, size_t buflen);
274 void Curl_ssh_attach(struct Curl_easy *data,
275                      struct connectdata *conn);
276 #else
277 /* for non-SSH builds */
278 #define Curl_ssh_cleanup()
279 #define Curl_ssh_attach(x,y)
280 #define Curl_ssh_init() 0
281 #endif
282 
283 #endif /* HEADER_CURL_SSH_H */
284