dce035b5eb29fa684dfe0b8a58a197a61a229256
[platform/upstream/curl.git] / lib / ssh.h
1 #ifndef HEADER_CURL_SSH_H
2 #define HEADER_CURL_SSH_H
3 /***************************************************************************
4  *                                  _   _ ____  _
5  *  Project                     ___| | | |  _ \| |
6  *                             / __| | | | |_) | |
7  *                            | (__| |_| |  _ <| |___
8  *                             \___|\___/|_| \_\_____|
9  *
10  * Copyright (C) 1998 - 2011, 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 http://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 "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  ***************************************************************************/
35 typedef 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_HOST_INIT,
48   SSH_AUTH_HOST,
49   SSH_AUTH_KEY_INIT,
50   SSH_AUTH_KEY,
51   SSH_AUTH_DONE,
52   SSH_SFTP_INIT,
53   SSH_SFTP_REALPATH,   /* Last state in SSH-CONNECT */
54
55   SSH_SFTP_QUOTE_INIT, /* First state in SFTP-DO */
56   SSH_SFTP_POSTQUOTE_INIT, /* (Possibly) First state in SFTP-DONE */
57   SSH_SFTP_QUOTE,
58   SSH_SFTP_NEXT_QUOTE,
59   SSH_SFTP_QUOTE_STAT,
60   SSH_SFTP_QUOTE_SETSTAT,
61   SSH_SFTP_QUOTE_SYMLINK,
62   SSH_SFTP_QUOTE_MKDIR,
63   SSH_SFTP_QUOTE_RENAME,
64   SSH_SFTP_QUOTE_RMDIR,
65   SSH_SFTP_QUOTE_UNLINK,
66   SSH_SFTP_TRANS_INIT,
67   SSH_SFTP_UPLOAD_INIT,
68   SSH_SFTP_CREATE_DIRS_INIT,
69   SSH_SFTP_CREATE_DIRS,
70   SSH_SFTP_CREATE_DIRS_MKDIR,
71   SSH_SFTP_READDIR_INIT,
72   SSH_SFTP_READDIR,
73   SSH_SFTP_READDIR_LINK,
74   SSH_SFTP_READDIR_BOTTOM,
75   SSH_SFTP_READDIR_DONE,
76   SSH_SFTP_DOWNLOAD_INIT,
77   SSH_SFTP_DOWNLOAD_STAT, /* Last state in SFTP-DO */
78   SSH_SFTP_CLOSE,    /* Last state in SFTP-DONE */
79   SSH_SFTP_SHUTDOWN, /* First state in SFTP-DISCONNECT */
80   SSH_SCP_TRANS_INIT, /* First state in SCP-DO */
81   SSH_SCP_UPLOAD_INIT,
82   SSH_SCP_DOWNLOAD_INIT,
83   SSH_SCP_DONE,
84   SSH_SCP_SEND_EOF,
85   SSH_SCP_WAIT_EOF,
86   SSH_SCP_WAIT_CLOSE,
87   SSH_SCP_CHANNEL_FREE,   /* Last state in SCP-DONE */
88   SSH_SESSION_DISCONNECT, /* First state in SCP-DISCONNECT */
89   SSH_SESSION_FREE,       /* Last state in SCP/SFTP-DISCONNECT */
90   SSH_QUIT,
91   SSH_LAST  /* never used */
92 } sshstate;
93
94 /* this struct is used in the HandleData struct which is part of the
95    SessionHandle, which means this is used on a per-easy handle basis.
96    Everything that is strictly related to a connection is banned from this
97    struct. */
98 struct SSHPROTO {
99   char *path;                  /* the path we operate on */
100 };
101
102 /* ssh_conn is used for struct connection-oriented data in the connectdata
103    struct */
104 struct ssh_conn {
105   const char *authlist;       /* List of auth. methods, managed by libssh2 */
106 #ifdef USE_LIBSSH2
107   const char *passphrase;     /* pass-phrase to use */
108   char *rsa_pub;              /* path name */
109   char *rsa;                  /* path name */
110   bool authed;                /* the connection has been authenticated fine */
111   sshstate state;             /* always use ssh.c:state() to change state! */
112   sshstate nextstate;         /* the state to goto after stopping */
113   CURLcode actualcode;        /* the actual error code */
114   struct curl_slist *quote_item; /* for the quote option */
115   char *quote_path1;          /* two generic pointers for the QUOTE stuff */
116   char *quote_path2;
117   LIBSSH2_SFTP_ATTRIBUTES quote_attrs; /* used by the SFTP_QUOTE state */
118   bool acceptfail;            /* used by the SFTP_QUOTE (continue if
119                                  quote command fails) */
120   char *homedir;              /* when doing SFTP we figure out home dir in the
121                                  connect phase */
122
123   /* Here's a set of struct members used by the SFTP_READDIR state */
124   LIBSSH2_SFTP_ATTRIBUTES readdir_attrs;
125   char *readdir_filename;
126   char *readdir_longentry;
127   int readdir_len, readdir_totalLen, readdir_currLen;
128   char *readdir_line;
129   char *readdir_linkPath;
130   /* end of READDIR stuff */
131
132   int secondCreateDirs;         /* counter use by the code to see if the
133                                    second attempt has been made to change
134                                    to/create a directory */
135   char *slash_pos;              /* used by the SFTP_CREATE_DIRS state */
136   LIBSSH2_SESSION *ssh_session; /* Secure Shell session */
137   LIBSSH2_CHANNEL *ssh_channel; /* Secure Shell channel handle */
138   LIBSSH2_SFTP *sftp_session;   /* SFTP handle */
139   LIBSSH2_SFTP_HANDLE *sftp_handle;
140   int orig_waitfor;             /* default READ/WRITE bits wait for */
141
142   /* note that HAVE_LIBSSH2_KNOWNHOST_API is a define set in the libssh2.h
143      header */
144 #ifdef HAVE_LIBSSH2_KNOWNHOST_API
145   LIBSSH2_KNOWNHOSTS *kh;
146 #endif
147 #endif /* USE_LIBSSH2 */
148 };
149
150 #ifdef USE_LIBSSH2
151
152 #if !defined(LIBSSH2_VERSION_NUM) || (LIBSSH2_VERSION_NUM < 0x001000)
153 #  error "SCP/SFTP protocols require libssh2 0.16 or later"
154 #endif
155
156 #if defined(LIBSSH2_VERSION_NUM) && (LIBSSH2_VERSION_NUM >= 0x010000)
157 #  define HAVE_LIBSSH2_SFTP_SEEK64 1
158 #else
159 #  undef HAVE_LIBSSH2_SFTP_SEEK64
160 #endif
161
162 #if defined(LIBSSH2_VERSION_NUM) && (LIBSSH2_VERSION_NUM >= 0x010206)
163 #  define HAVE_LIBSSH2_SCP_SEND64 1
164 #else
165 #  undef HAVE_LIBSSH2_SCP_SEND64
166 #endif
167
168
169 extern const struct Curl_handler Curl_handler_scp;
170 extern const struct Curl_handler Curl_handler_sftp;
171
172 #endif /* USE_LIBSSH2 */
173
174 #endif /* HEADER_CURL_SSH_H */