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