1#ifndef HEADER_CURL_SSH_H
2#define HEADER_CURL_SSH_H
3/***************************************************************************
4 *                                  _   _ ____  _
5 *  Project                     ___| | | |  _ \| |
6 *                             / __| | | | |_) | |
7 *                            | (__| |_| |  _ <| |___
8 *                             \___|\___/|_| \_\_____|
9 *
10 * Copyright (C) 1998 - 2015, 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.haxx.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 ***************************************************************************/
24
25#include "curl_setup.h"
26
27#ifdef HAVE_LIBSSH2_H
28#include <libssh2.h>
29#include <libssh2_sftp.h>
30#endif /* HAVE_LIBSSH2_H */
31
32/****************************************************************************
33 * SSH unique setup
34 ***************************************************************************/
35typedef enum {
36  SSH_NO_STATE = -1,  /* Used for "nextState" so say there is none */
37  SSH_STOP = 0,       /* do nothing state, stops the state machine */
38
39  SSH_INIT,           /* First state in SSH-CONNECT */
40  SSH_S_STARTUP,      /* Session startup */
41  SSH_HOSTKEY,        /* verify hostkey */
42  SSH_AUTHLIST,
43  SSH_AUTH_PKEY_INIT,
44  SSH_AUTH_PKEY,
45  SSH_AUTH_PASS_INIT,
46  SSH_AUTH_PASS,
47  SSH_AUTH_AGENT_INIT, /* initialize then wait for connection to agent */
48  SSH_AUTH_AGENT_LIST, /* ask for list then wait for entire list to come */
49  SSH_AUTH_AGENT,      /* attempt one key at a time */
50  SSH_AUTH_HOST_INIT,
51  SSH_AUTH_HOST,
52  SSH_AUTH_KEY_INIT,
53  SSH_AUTH_KEY,
54  SSH_AUTH_DONE,
55  SSH_SFTP_INIT,
56  SSH_SFTP_REALPATH,   /* Last state in SSH-CONNECT */
57
58  SSH_SFTP_QUOTE_INIT, /* First state in SFTP-DO */
59  SSH_SFTP_POSTQUOTE_INIT, /* (Possibly) First state in SFTP-DONE */
60  SSH_SFTP_QUOTE,
61  SSH_SFTP_NEXT_QUOTE,
62  SSH_SFTP_QUOTE_STAT,
63  SSH_SFTP_QUOTE_SETSTAT,
64  SSH_SFTP_QUOTE_SYMLINK,
65  SSH_SFTP_QUOTE_MKDIR,
66  SSH_SFTP_QUOTE_RENAME,
67  SSH_SFTP_QUOTE_RMDIR,
68  SSH_SFTP_QUOTE_UNLINK,
69  SSH_SFTP_QUOTE_STATVFS,
70  SSH_SFTP_GETINFO,
71  SSH_SFTP_FILETIME,
72  SSH_SFTP_TRANS_INIT,
73  SSH_SFTP_UPLOAD_INIT,
74  SSH_SFTP_CREATE_DIRS_INIT,
75  SSH_SFTP_CREATE_DIRS,
76  SSH_SFTP_CREATE_DIRS_MKDIR,
77  SSH_SFTP_READDIR_INIT,
78  SSH_SFTP_READDIR,
79  SSH_SFTP_READDIR_LINK,
80  SSH_SFTP_READDIR_BOTTOM,
81  SSH_SFTP_READDIR_DONE,
82  SSH_SFTP_DOWNLOAD_INIT,
83  SSH_SFTP_DOWNLOAD_STAT, /* Last state in SFTP-DO */
84  SSH_SFTP_CLOSE,    /* Last state in SFTP-DONE */
85  SSH_SFTP_SHUTDOWN, /* First state in SFTP-DISCONNECT */
86  SSH_SCP_TRANS_INIT, /* First state in SCP-DO */
87  SSH_SCP_UPLOAD_INIT,
88  SSH_SCP_DOWNLOAD_INIT,
89  SSH_SCP_DONE,
90  SSH_SCP_SEND_EOF,
91  SSH_SCP_WAIT_EOF,
92  SSH_SCP_WAIT_CLOSE,
93  SSH_SCP_CHANNEL_FREE,   /* Last state in SCP-DONE */
94  SSH_SESSION_DISCONNECT, /* First state in SCP-DISCONNECT */
95  SSH_SESSION_FREE,       /* Last state in SCP/SFTP-DISCONNECT */
96  SSH_QUIT,
97  SSH_LAST  /* never used */
98} sshstate;
99
100/* this struct is used in the HandleData struct which is part of the
101   Curl_easy, which means this is used on a per-easy handle basis.
102   Everything that is strictly related to a connection is banned from this
103   struct. */
104struct SSHPROTO {
105  char *path;                  /* the path we operate on */
106};
107
108/* ssh_conn is used for struct connection-oriented data in the connectdata
109   struct */
110struct ssh_conn {
111  const char *authlist;       /* List of auth. methods, managed by libssh2 */
112#ifdef USE_LIBSSH2
113  const char *passphrase;     /* pass-phrase to use */
114  char *rsa_pub;              /* path name */
115  char *rsa;                  /* path name */
116  bool authed;                /* the connection has been authenticated fine */
117  sshstate state;             /* always use ssh.c:state() to change state! */
118  sshstate nextstate;         /* the state to goto after stopping */
119  CURLcode actualcode;        /* the actual error code */
120  struct curl_slist *quote_item; /* for the quote option */
121  char *quote_path1;          /* two generic pointers for the QUOTE stuff */
122  char *quote_path2;
123  LIBSSH2_SFTP_ATTRIBUTES quote_attrs; /* used by the SFTP_QUOTE state */
124  bool acceptfail;            /* used by the SFTP_QUOTE (continue if
125                                 quote command fails) */
126  char *homedir;              /* when doing SFTP we figure out home dir in the
127                                 connect phase */
128
129  /* Here's a set of struct members used by the SFTP_READDIR state */
130  LIBSSH2_SFTP_ATTRIBUTES readdir_attrs;
131  char *readdir_filename;
132  char *readdir_longentry;
133  int readdir_len, readdir_totalLen, readdir_currLen;
134  char *readdir_line;
135  char *readdir_linkPath;
136  /* end of READDIR stuff */
137
138  int secondCreateDirs;         /* counter use by the code to see if the
139                                   second attempt has been made to change
140                                   to/create a directory */
141  char *slash_pos;              /* used by the SFTP_CREATE_DIRS state */
142  LIBSSH2_SESSION *ssh_session; /* Secure Shell session */
143  LIBSSH2_CHANNEL *ssh_channel; /* Secure Shell channel handle */
144  LIBSSH2_SFTP *sftp_session;   /* SFTP handle */
145  LIBSSH2_SFTP_HANDLE *sftp_handle;
146  int orig_waitfor;             /* default READ/WRITE bits wait for */
147
148#ifdef HAVE_LIBSSH2_AGENT_API
149  LIBSSH2_AGENT *ssh_agent;     /* proxy to ssh-agent/pageant */
150  struct libssh2_agent_publickey *sshagent_identity,
151                                 *sshagent_prev_identity;
152#endif
153
154  /* note that HAVE_LIBSSH2_KNOWNHOST_API is a define set in the libssh2.h
155     header */
156#ifdef HAVE_LIBSSH2_KNOWNHOST_API
157  LIBSSH2_KNOWNHOSTS *kh;
158#endif
159#endif /* USE_LIBSSH2 */
160};
161
162#ifdef USE_LIBSSH2
163
164/* Feature detection based on version numbers to better work with
165   non-configure platforms */
166
167#if !defined(LIBSSH2_VERSION_NUM) || (LIBSSH2_VERSION_NUM < 0x001000)
168#  error "SCP/SFTP protocols require libssh2 0.16 or later"
169#endif
170
171#if LIBSSH2_VERSION_NUM >= 0x010000
172#define HAVE_LIBSSH2_SFTP_SEEK64 1
173#endif
174
175#if LIBSSH2_VERSION_NUM >= 0x010100
176#define HAVE_LIBSSH2_VERSION 1
177#endif
178
179#if LIBSSH2_VERSION_NUM >= 0x010205
180#define HAVE_LIBSSH2_INIT 1
181#define HAVE_LIBSSH2_EXIT 1
182#endif
183
184#if LIBSSH2_VERSION_NUM >= 0x010206
185#define HAVE_LIBSSH2_KNOWNHOST_CHECKP 1
186#define HAVE_LIBSSH2_SCP_SEND64 1
187#endif
188
189#if LIBSSH2_VERSION_NUM >= 0x010208
190#define HAVE_LIBSSH2_SESSION_HANDSHAKE 1
191#endif
192
193extern const struct Curl_handler Curl_handler_scp;
194extern const struct Curl_handler Curl_handler_sftp;
195
196#endif /* USE_LIBSSH2 */
197
198#endif /* HEADER_CURL_SSH_H */
199