Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 1 | /* |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 2 | * nanoftp.c: basic FTP client support |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 3 | * |
| 4 | * Reference: RFC 959 |
| 5 | */ |
| 6 | |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 7 | #ifdef TESTING |
| 8 | #define STANDALONE |
| 9 | #define HAVE_STDLIB_H |
| 10 | #define HAVE_UNISTD_H |
| 11 | #define HAVE_SYS_SOCKET_H |
| 12 | #define HAVE_NETINET_IN_H |
| 13 | #define HAVE_NETDB_H |
| 14 | #define HAVE_SYS_TIME_H |
| 15 | #else /* STANDALONE */ |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 16 | #ifdef WIN32 |
Daniel Veillard | f341f93 | 2000-02-02 14:52:08 +0000 | [diff] [blame] | 17 | #define INCLUDE_WINSOCK |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 18 | #include "win32config.h" |
| 19 | #else |
| 20 | #include "config.h" |
| 21 | #endif |
Daniel Veillard | 32bc74e | 2000-07-14 14:49:25 +0000 | [diff] [blame] | 22 | #endif /* STANDALONE */ |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 23 | |
Daniel Veillard | b71379b | 2000-10-09 12:30:39 +0000 | [diff] [blame] | 24 | #include <libxml/xmlversion.h> |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 25 | |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 26 | #ifdef LIBXML_FTP_ENABLED |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 27 | #include <stdio.h> |
| 28 | #include <string.h> |
| 29 | |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 30 | #ifdef HAVE_STDLIB_H |
| 31 | #include <stdlib.h> |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 32 | #endif |
| 33 | #ifdef HAVE_UNISTD_H |
| 34 | #include <unistd.h> |
| 35 | #endif |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 36 | #ifdef HAVE_SYS_SOCKET_H |
| 37 | #include <sys/socket.h> |
| 38 | #endif |
James Henstridge | f3be931 | 2000-01-28 13:59:21 +0000 | [diff] [blame] | 39 | #ifdef HAVE_NETINET_IN_H |
| 40 | #include <netinet/in.h> |
| 41 | #endif |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 42 | #ifdef HAVE_ARPA_INET_H |
| 43 | #include <arpa/inet.h> |
| 44 | #endif |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 45 | #ifdef HAVE_NETDB_H |
| 46 | #include <netdb.h> |
| 47 | #endif |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 48 | #ifdef HAVE_FCNTL_H |
| 49 | #include <fcntl.h> |
| 50 | #endif |
| 51 | #ifdef HAVE_ERRNO_H |
| 52 | #include <errno.h> |
| 53 | #endif |
| 54 | #ifdef HAVE_SYS_TIME_H |
| 55 | #include <sys/time.h> |
| 56 | #endif |
| 57 | #ifdef HAVE_SYS_SELECT_H |
| 58 | #include <sys/select.h> |
| 59 | #endif |
Daniel Veillard | 5feb849 | 2000-02-02 17:15:36 +0000 | [diff] [blame] | 60 | #ifdef HAVE_STRINGS_H |
| 61 | #include <strings.h> |
| 62 | #endif |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 63 | |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 64 | #include <libxml/xmlmemory.h> |
| 65 | #include <libxml/nanoftp.h> |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 66 | #include <libxml/xmlerror.h> |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 67 | |
| 68 | /* #define DEBUG_FTP 1 */ |
| 69 | #ifdef STANDALONE |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 70 | #ifndef DEBUG_FTP |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 71 | #define DEBUG_FTP 1 |
| 72 | #endif |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 73 | #endif |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 74 | |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 75 | /** |
| 76 | * A couple portability macros |
| 77 | */ |
| 78 | #ifndef _WINSOCKAPI_ |
| 79 | #define closesocket(s) close(s) |
| 80 | #define SOCKET int |
| 81 | #endif |
| 82 | |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 83 | static char hostname[100]; |
| 84 | |
| 85 | #define FTP_COMMAND_OK 200 |
| 86 | #define FTP_SYNTAX_ERROR 500 |
| 87 | #define FTP_GET_PASSWD 331 |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 88 | #define FTP_BUF_SIZE 512 |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 89 | |
| 90 | typedef struct xmlNanoFTPCtxt { |
| 91 | char *protocol; /* the protocol name */ |
| 92 | char *hostname; /* the host name */ |
| 93 | int port; /* the port */ |
| 94 | char *path; /* the path within the URL */ |
| 95 | char *user; /* user string */ |
| 96 | char *passwd; /* passwd string */ |
| 97 | struct sockaddr_in ftpAddr; /* the socket address struct */ |
| 98 | int passive; /* currently we support only passive !!! */ |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 99 | SOCKET controlFd; /* the file descriptor for the control socket */ |
| 100 | SOCKET dataFd; /* the file descriptor for the data socket */ |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 101 | int state; /* WRITE / READ / CLOSED */ |
| 102 | int returnValue; /* the protocol return value */ |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 103 | /* buffer for data received from the control connection */ |
| 104 | char controlBuf[FTP_BUF_SIZE + 1]; |
| 105 | int controlBufIndex; |
| 106 | int controlBufUsed; |
| 107 | int controlBufAnswer; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 108 | } xmlNanoFTPCtxt, *xmlNanoFTPCtxtPtr; |
| 109 | |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 110 | static int initialized = 0; |
| 111 | static char *proxy = NULL; /* the proxy name if any */ |
| 112 | static int proxyPort = 0; /* the proxy port if any */ |
| 113 | static char *proxyUser = NULL; /* user for proxy authentication */ |
| 114 | static char *proxyPasswd = NULL;/* passwd for proxy authentication */ |
| 115 | static int proxyType = 0; /* uses TYPE or a@b ? */ |
| 116 | |
| 117 | /** |
| 118 | * xmlNanoFTPInit: |
| 119 | * |
| 120 | * Initialize the FTP protocol layer. |
| 121 | * Currently it just checks for proxy informations, |
| 122 | * and get the hostname |
| 123 | */ |
| 124 | |
| 125 | void |
| 126 | xmlNanoFTPInit(void) { |
| 127 | const char *env; |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 128 | #ifdef _WINSOCKAPI_ |
| 129 | WSADATA wsaData; |
| 130 | #endif |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 131 | |
| 132 | if (initialized) |
| 133 | return; |
| 134 | |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 135 | #ifdef _WINSOCKAPI_ |
| 136 | if (WSAStartup(MAKEWORD(1, 1), &wsaData) != 0) |
| 137 | return; |
| 138 | #endif |
| 139 | |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 140 | gethostname(hostname, sizeof(hostname)); |
| 141 | |
| 142 | proxyPort = 21; |
| 143 | env = getenv("no_proxy"); |
| 144 | if (env != NULL) |
| 145 | return; |
| 146 | env = getenv("ftp_proxy"); |
| 147 | if (env != NULL) { |
| 148 | xmlNanoFTPScanProxy(env); |
| 149 | } else { |
| 150 | env = getenv("FTP_PROXY"); |
| 151 | if (env != NULL) { |
| 152 | xmlNanoFTPScanProxy(env); |
| 153 | } |
| 154 | } |
| 155 | env = getenv("ftp_proxy_user"); |
| 156 | if (env != NULL) { |
| 157 | proxyUser = xmlMemStrdup(env); |
| 158 | } |
| 159 | env = getenv("ftp_proxy_password"); |
| 160 | if (env != NULL) { |
| 161 | proxyPasswd = xmlMemStrdup(env); |
| 162 | } |
| 163 | initialized = 1; |
| 164 | } |
| 165 | |
| 166 | /** |
| 167 | * xmlNanoFTPClenup: |
| 168 | * |
| 169 | * Cleanup the FTP protocol layer. This cleanup proxy informations. |
| 170 | */ |
| 171 | |
| 172 | void |
| 173 | xmlNanoFTPCleanup(void) { |
| 174 | if (proxy != NULL) { |
| 175 | xmlFree(proxy); |
| 176 | proxy = NULL; |
| 177 | } |
| 178 | if (proxyUser != NULL) { |
| 179 | xmlFree(proxyUser); |
| 180 | proxyUser = NULL; |
| 181 | } |
| 182 | if (proxyPasswd != NULL) { |
| 183 | xmlFree(proxyPasswd); |
| 184 | proxyPasswd = NULL; |
| 185 | } |
| 186 | hostname[0] = 0; |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 187 | #ifdef _WINSOCKAPI_ |
| 188 | if (initialized) |
| 189 | WSACleanup(); |
| 190 | #endif |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 191 | initialized = 0; |
| 192 | return; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * xmlNanoFTPProxy: |
| 197 | * @host: the proxy host name |
| 198 | * @port: the proxy port |
| 199 | * @user: the proxy user name |
| 200 | * @passwd: the proxy password |
| 201 | * @type: the type of proxy 1 for using SITE, 2 for USER a@b |
| 202 | * |
| 203 | * Setup the FTP proxy informations. |
| 204 | * This can also be done by using ftp_proxy ftp_proxy_user and |
| 205 | * ftp_proxy_password environment variables. |
| 206 | */ |
| 207 | |
| 208 | void |
| 209 | xmlNanoFTPProxy(const char *host, int port, const char *user, |
| 210 | const char *passwd, int type) { |
| 211 | if (proxy != NULL) |
| 212 | xmlFree(proxy); |
| 213 | if (proxyUser != NULL) |
| 214 | xmlFree(proxyUser); |
| 215 | if (proxyPasswd != NULL) |
| 216 | xmlFree(proxyPasswd); |
| 217 | if (host) |
| 218 | proxy = xmlMemStrdup(host); |
| 219 | if (user) |
| 220 | proxyUser = xmlMemStrdup(user); |
| 221 | if (passwd) |
| 222 | proxyPasswd = xmlMemStrdup(passwd); |
| 223 | proxyPort = port; |
| 224 | proxyType = type; |
| 225 | } |
| 226 | |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 227 | /** |
| 228 | * xmlNanoFTPScanURL: |
| 229 | * @ctx: an FTP context |
| 230 | * @URL: The URL used to initialize the context |
| 231 | * |
| 232 | * (Re)Initialize an FTP context by parsing the URL and finding |
| 233 | * the protocol host port and path it indicates. |
| 234 | */ |
| 235 | |
| 236 | static void |
| 237 | xmlNanoFTPScanURL(void *ctx, const char *URL) { |
| 238 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 239 | const char *cur = URL; |
| 240 | char buf[4096]; |
| 241 | int index = 0; |
| 242 | int port = 0; |
| 243 | |
| 244 | if (ctxt->protocol != NULL) { |
| 245 | xmlFree(ctxt->protocol); |
| 246 | ctxt->protocol = NULL; |
| 247 | } |
| 248 | if (ctxt->hostname != NULL) { |
| 249 | xmlFree(ctxt->hostname); |
| 250 | ctxt->hostname = NULL; |
| 251 | } |
| 252 | if (ctxt->path != NULL) { |
| 253 | xmlFree(ctxt->path); |
| 254 | ctxt->path = NULL; |
| 255 | } |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 256 | if (URL == NULL) return; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 257 | buf[index] = 0; |
| 258 | while (*cur != 0) { |
| 259 | if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) { |
| 260 | buf[index] = 0; |
| 261 | ctxt->protocol = xmlMemStrdup(buf); |
| 262 | index = 0; |
| 263 | cur += 3; |
| 264 | break; |
| 265 | } |
| 266 | buf[index++] = *cur++; |
| 267 | } |
| 268 | if (*cur == 0) return; |
| 269 | |
| 270 | buf[index] = 0; |
| 271 | while (1) { |
| 272 | if (cur[0] == ':') { |
| 273 | buf[index] = 0; |
| 274 | ctxt->hostname = xmlMemStrdup(buf); |
| 275 | index = 0; |
| 276 | cur += 1; |
| 277 | while ((*cur >= '0') && (*cur <= '9')) { |
| 278 | port *= 10; |
| 279 | port += *cur - '0'; |
| 280 | cur++; |
| 281 | } |
| 282 | if (port != 0) ctxt->port = port; |
| 283 | while ((cur[0] != '/') && (*cur != 0)) |
| 284 | cur++; |
| 285 | break; |
| 286 | } |
| 287 | if ((*cur == '/') || (*cur == 0)) { |
| 288 | buf[index] = 0; |
| 289 | ctxt->hostname = xmlMemStrdup(buf); |
| 290 | index = 0; |
| 291 | break; |
| 292 | } |
| 293 | buf[index++] = *cur++; |
| 294 | } |
| 295 | if (*cur == 0) |
| 296 | ctxt->path = xmlMemStrdup("/"); |
| 297 | else { |
Daniel Veillard | 726e879 | 2000-01-30 20:04:29 +0000 | [diff] [blame] | 298 | index = 0; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 299 | buf[index] = 0; |
Daniel Veillard | 726e879 | 2000-01-30 20:04:29 +0000 | [diff] [blame] | 300 | while (*cur != 0) |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 301 | buf[index++] = *cur++; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 302 | buf[index] = 0; |
| 303 | ctxt->path = xmlMemStrdup(buf); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | /** |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 308 | * xmlNanoFTPUpdateURL: |
| 309 | * @ctx: an FTP context |
| 310 | * @URL: The URL used to update the context |
| 311 | * |
| 312 | * Update an FTP context by parsing the URL and finding |
| 313 | * new path it indicates. If there is an error in the |
| 314 | * protocol, hostname, port or other information, the |
| 315 | * error is raised. It indicates a new connection has to |
| 316 | * be established. |
| 317 | * |
| 318 | * Returns 0 if Ok, -1 in case of error (other host). |
| 319 | */ |
| 320 | |
| 321 | int |
| 322 | xmlNanoFTPUpdateURL(void *ctx, const char *URL) { |
| 323 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 324 | const char *cur = URL; |
| 325 | char buf[4096]; |
| 326 | int index = 0; |
| 327 | int port = 0; |
| 328 | |
| 329 | if (URL == NULL) |
| 330 | return(-1); |
| 331 | if (ctxt == NULL) |
| 332 | return(-1); |
| 333 | if (ctxt->protocol == NULL) |
| 334 | return(-1); |
| 335 | if (ctxt->hostname == NULL) |
| 336 | return(-1); |
| 337 | buf[index] = 0; |
| 338 | while (*cur != 0) { |
| 339 | if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) { |
| 340 | buf[index] = 0; |
| 341 | if (strcmp(ctxt->protocol, buf)) |
| 342 | return(-1); |
| 343 | index = 0; |
| 344 | cur += 3; |
| 345 | break; |
| 346 | } |
| 347 | buf[index++] = *cur++; |
| 348 | } |
| 349 | if (*cur == 0) |
| 350 | return(-1); |
| 351 | |
| 352 | buf[index] = 0; |
| 353 | while (1) { |
| 354 | if (cur[0] == ':') { |
| 355 | buf[index] = 0; |
| 356 | if (strcmp(ctxt->hostname, buf)) |
| 357 | return(-1); |
| 358 | index = 0; |
| 359 | cur += 1; |
| 360 | while ((*cur >= '0') && (*cur <= '9')) { |
| 361 | port *= 10; |
| 362 | port += *cur - '0'; |
| 363 | cur++; |
| 364 | } |
| 365 | if (port != ctxt->port) |
| 366 | return(-1); |
| 367 | while ((cur[0] != '/') && (*cur != 0)) |
| 368 | cur++; |
| 369 | break; |
| 370 | } |
| 371 | if ((*cur == '/') || (*cur == 0)) { |
| 372 | buf[index] = 0; |
| 373 | if (strcmp(ctxt->hostname, buf)) |
| 374 | return(-1); |
| 375 | index = 0; |
| 376 | break; |
| 377 | } |
| 378 | buf[index++] = *cur++; |
| 379 | } |
| 380 | if (ctxt->path != NULL) { |
| 381 | xmlFree(ctxt->path); |
| 382 | ctxt->path = NULL; |
| 383 | } |
| 384 | |
| 385 | if (*cur == 0) |
| 386 | ctxt->path = xmlMemStrdup("/"); |
| 387 | else { |
Daniel Veillard | 726e879 | 2000-01-30 20:04:29 +0000 | [diff] [blame] | 388 | index = 0; |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 389 | buf[index] = 0; |
Daniel Veillard | 726e879 | 2000-01-30 20:04:29 +0000 | [diff] [blame] | 390 | while (*cur != 0) |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 391 | buf[index++] = *cur++; |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 392 | buf[index] = 0; |
| 393 | ctxt->path = xmlMemStrdup(buf); |
| 394 | } |
| 395 | return(0); |
| 396 | } |
| 397 | |
| 398 | /** |
| 399 | * xmlNanoFTPScanProxy: |
| 400 | * @URL: The proxy URL used to initialize the proxy context |
| 401 | * |
| 402 | * (Re)Initialize the FTP Proxy context by parsing the URL and finding |
| 403 | * the protocol host port it indicates. |
| 404 | * Should be like ftp://myproxy/ or ftp://myproxy:3128/ |
| 405 | * A NULL URL cleans up proxy informations. |
| 406 | */ |
| 407 | |
| 408 | void |
| 409 | xmlNanoFTPScanProxy(const char *URL) { |
| 410 | const char *cur = URL; |
| 411 | char buf[4096]; |
| 412 | int index = 0; |
| 413 | int port = 0; |
| 414 | |
| 415 | if (proxy != NULL) { |
| 416 | xmlFree(proxy); |
| 417 | proxy = NULL; |
| 418 | } |
| 419 | if (proxyPort != 0) { |
| 420 | proxyPort = 0; |
| 421 | } |
| 422 | #ifdef DEBUG_FTP |
| 423 | if (URL == NULL) |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 424 | xmlGenericError(xmlGenericErrorContext, "Removing FTP proxy info\n"); |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 425 | else |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 426 | xmlGenericError(xmlGenericErrorContext, "Using FTP proxy %s\n", URL); |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 427 | #endif |
| 428 | if (URL == NULL) return; |
| 429 | buf[index] = 0; |
| 430 | while (*cur != 0) { |
| 431 | if ((cur[0] == ':') && (cur[1] == '/') && (cur[2] == '/')) { |
| 432 | buf[index] = 0; |
| 433 | index = 0; |
| 434 | cur += 3; |
| 435 | break; |
| 436 | } |
| 437 | buf[index++] = *cur++; |
| 438 | } |
| 439 | if (*cur == 0) return; |
| 440 | |
| 441 | buf[index] = 0; |
| 442 | while (1) { |
| 443 | if (cur[0] == ':') { |
| 444 | buf[index] = 0; |
| 445 | proxy = xmlMemStrdup(buf); |
| 446 | index = 0; |
| 447 | cur += 1; |
| 448 | while ((*cur >= '0') && (*cur <= '9')) { |
| 449 | port *= 10; |
| 450 | port += *cur - '0'; |
| 451 | cur++; |
| 452 | } |
| 453 | if (port != 0) proxyPort = port; |
| 454 | while ((cur[0] != '/') && (*cur != 0)) |
| 455 | cur++; |
| 456 | break; |
| 457 | } |
| 458 | if ((*cur == '/') || (*cur == 0)) { |
| 459 | buf[index] = 0; |
| 460 | proxy = xmlMemStrdup(buf); |
| 461 | index = 0; |
| 462 | break; |
| 463 | } |
| 464 | buf[index++] = *cur++; |
| 465 | } |
| 466 | } |
| 467 | |
| 468 | /** |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 469 | * xmlNanoFTPNewCtxt: |
| 470 | * @URL: The URL used to initialize the context |
| 471 | * |
| 472 | * Allocate and initialize a new FTP context. |
| 473 | * |
| 474 | * Returns an FTP context or NULL in case of error. |
| 475 | */ |
| 476 | |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 477 | void* |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 478 | xmlNanoFTPNewCtxt(const char *URL) { |
| 479 | xmlNanoFTPCtxtPtr ret; |
| 480 | |
| 481 | ret = (xmlNanoFTPCtxtPtr) xmlMalloc(sizeof(xmlNanoFTPCtxt)); |
| 482 | if (ret == NULL) return(NULL); |
| 483 | |
| 484 | memset(ret, 0, sizeof(xmlNanoFTPCtxt)); |
| 485 | ret->port = 21; |
| 486 | ret->passive = 1; |
| 487 | ret->returnValue = 0; |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 488 | ret->controlBufIndex = 0; |
| 489 | ret->controlBufUsed = 0; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 490 | |
| 491 | if (URL != NULL) |
| 492 | xmlNanoFTPScanURL(ret, URL); |
| 493 | |
| 494 | return(ret); |
| 495 | } |
| 496 | |
| 497 | /** |
| 498 | * xmlNanoFTPFreeCtxt: |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 499 | * @ctx: an FTP context |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 500 | * |
| 501 | * Frees the context after closing the connection. |
| 502 | */ |
| 503 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 504 | void |
| 505 | xmlNanoFTPFreeCtxt(void * ctx) { |
| 506 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 507 | if (ctxt == NULL) return; |
| 508 | if (ctxt->hostname != NULL) xmlFree(ctxt->hostname); |
| 509 | if (ctxt->protocol != NULL) xmlFree(ctxt->protocol); |
| 510 | if (ctxt->path != NULL) xmlFree(ctxt->path); |
| 511 | ctxt->passive = 1; |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 512 | if (ctxt->controlFd >= 0) closesocket(ctxt->controlFd); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 513 | ctxt->controlFd = -1; |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 514 | ctxt->controlBufIndex = -1; |
| 515 | ctxt->controlBufUsed = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 516 | xmlFree(ctxt); |
| 517 | } |
| 518 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 519 | /** |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 520 | * xmlNanoFTPParseResponse: |
| 521 | * @ctx: the FTP connection context |
| 522 | * @buf: the buffer containing the response |
| 523 | * @len: the buffer length |
| 524 | * |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 525 | * Parsing of the server answer, we just extract the code. |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 526 | * |
| 527 | * returns 0 for errors |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 528 | * +XXX for last line of response |
| 529 | * -XXX for response to be continued |
| 530 | */ |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 531 | static int |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 532 | xmlNanoFTPParseResponse(void *ctx, char *buf, int len) { |
| 533 | int val = 0; |
| 534 | |
| 535 | if (len < 3) return(-1); |
| 536 | if ((*buf >= '0') && (*buf <= '9')) |
| 537 | val = val * 10 + (*buf - '0'); |
| 538 | else |
| 539 | return(0); |
| 540 | buf++; |
| 541 | if ((*buf >= '0') && (*buf <= '9')) |
| 542 | val = val * 10 + (*buf - '0'); |
| 543 | else |
| 544 | return(0); |
| 545 | buf++; |
| 546 | if ((*buf >= '0') && (*buf <= '9')) |
| 547 | val = val * 10 + (*buf - '0'); |
| 548 | else |
| 549 | return(0); |
| 550 | buf++; |
| 551 | if (*buf == '-') |
| 552 | return(-val); |
| 553 | return(val); |
| 554 | } |
| 555 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 556 | /** |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 557 | * xmlNanoFTPGetMore: |
| 558 | * @ctx: an FTP context |
| 559 | * |
| 560 | * Read more information from the FTP control connection |
| 561 | * Returns the number of bytes read, < 0 indicates an error |
| 562 | */ |
| 563 | static int |
| 564 | xmlNanoFTPGetMore(void *ctx) { |
| 565 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 566 | int len; |
| 567 | int size; |
| 568 | |
| 569 | if ((ctxt->controlBufIndex < 0) || (ctxt->controlBufIndex > FTP_BUF_SIZE)) { |
| 570 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 571 | xmlGenericError(xmlGenericErrorContext, |
| 572 | "xmlNanoFTPGetMore : controlBufIndex = %d\n", |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 573 | ctxt->controlBufIndex); |
| 574 | #endif |
| 575 | return(-1); |
| 576 | } |
| 577 | |
| 578 | if ((ctxt->controlBufUsed < 0) || (ctxt->controlBufUsed > FTP_BUF_SIZE)) { |
| 579 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 580 | xmlGenericError(xmlGenericErrorContext, |
| 581 | "xmlNanoFTPGetMore : controlBufUsed = %d\n", |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 582 | ctxt->controlBufUsed); |
| 583 | #endif |
| 584 | return(-1); |
| 585 | } |
| 586 | if (ctxt->controlBufIndex > ctxt->controlBufUsed) { |
| 587 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 588 | xmlGenericError(xmlGenericErrorContext, |
| 589 | "xmlNanoFTPGetMore : controlBufIndex > controlBufUsed %d > %d\n", |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 590 | ctxt->controlBufIndex, ctxt->controlBufUsed); |
| 591 | #endif |
| 592 | return(-1); |
| 593 | } |
| 594 | |
| 595 | /* |
| 596 | * First pack the control buffer |
| 597 | */ |
| 598 | if (ctxt->controlBufIndex > 0) { |
| 599 | memmove(&ctxt->controlBuf[0], &ctxt->controlBuf[ctxt->controlBufIndex], |
| 600 | ctxt->controlBufUsed - ctxt->controlBufIndex); |
| 601 | ctxt->controlBufUsed -= ctxt->controlBufIndex; |
| 602 | ctxt->controlBufIndex = 0; |
| 603 | } |
| 604 | size = FTP_BUF_SIZE - ctxt->controlBufUsed; |
| 605 | if (size == 0) { |
| 606 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 607 | xmlGenericError(xmlGenericErrorContext, |
| 608 | "xmlNanoFTPGetMore : buffer full %d \n", ctxt->controlBufUsed); |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 609 | #endif |
| 610 | return(0); |
| 611 | } |
| 612 | |
| 613 | /* |
| 614 | * Read the amount left on teh control connection |
| 615 | */ |
| 616 | if ((len = recv(ctxt->controlFd, &ctxt->controlBuf[ctxt->controlBufIndex], |
| 617 | size, 0)) < 0) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 618 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 619 | ctxt->controlFd = -1; |
| 620 | return(-1); |
| 621 | } |
| 622 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 623 | xmlGenericError(xmlGenericErrorContext, |
| 624 | "xmlNanoFTPGetMore : read %d [%d - %d]\n", len, |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 625 | ctxt->controlBufUsed, ctxt->controlBufUsed + len); |
| 626 | #endif |
| 627 | ctxt->controlBufUsed += len; |
| 628 | ctxt->controlBuf[ctxt->controlBufUsed] = 0; |
| 629 | |
| 630 | return(len); |
| 631 | } |
| 632 | |
| 633 | /** |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 634 | * xmlNanoFTPReadResponse: |
| 635 | * @ctx: an FTP context |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 636 | * |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 637 | * Read the response from the FTP server after a command. |
| 638 | * Returns the code number |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 639 | */ |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 640 | static int |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 641 | xmlNanoFTPReadResponse(void *ctx) { |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 642 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 643 | char *ptr, *end; |
| 644 | int len; |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 645 | int res = -1, cur = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 646 | |
| 647 | get_more: |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 648 | /* |
| 649 | * Assumes everything up to controlBuf[controlBufIndex] has been read |
| 650 | * and analyzed. |
| 651 | */ |
| 652 | len = xmlNanoFTPGetMore(ctx); |
Daniel Veillard | a6d8eb6 | 2000-12-27 10:46:47 +0000 | [diff] [blame] | 653 | if (len < 0) { |
| 654 | return(-1); |
| 655 | } |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 656 | if ((ctxt->controlBufUsed == 0) && (len == 0)) { |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 657 | return(-1); |
| 658 | } |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 659 | ptr = &ctxt->controlBuf[ctxt->controlBufIndex]; |
| 660 | end = &ctxt->controlBuf[ctxt->controlBufUsed]; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 661 | |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 662 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 663 | xmlGenericError(xmlGenericErrorContext, |
| 664 | "\n<<<\n%s\n--\n", ptr); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 665 | #endif |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 666 | while (ptr < end) { |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 667 | cur = xmlNanoFTPParseResponse(ctxt, ptr, end - ptr); |
| 668 | if (cur > 0) { |
| 669 | /* |
| 670 | * Successfully scanned the control code, scratch |
| 671 | * till the end of the line, but keep the index to be |
| 672 | * able to analyze the result if needed. |
| 673 | */ |
| 674 | res = cur; |
| 675 | ptr += 3; |
| 676 | ctxt->controlBufAnswer = ptr - ctxt->controlBuf; |
| 677 | while ((ptr < end) && (*ptr != '\n')) ptr++; |
| 678 | if (*ptr == '\n') ptr++; |
| 679 | if (*ptr == '\r') ptr++; |
| 680 | break; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 681 | } |
| 682 | while ((ptr < end) && (*ptr != '\n')) ptr++; |
| 683 | if (ptr >= end) { |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 684 | ctxt->controlBufIndex = ctxt->controlBufUsed; |
| 685 | goto get_more; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 686 | } |
| 687 | if (*ptr != '\r') ptr++; |
| 688 | } |
| 689 | |
| 690 | if (res < 0) goto get_more; |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 691 | ctxt->controlBufIndex = ptr - ctxt->controlBuf; |
| 692 | #ifdef DEBUG_FTP |
| 693 | ptr = &ctxt->controlBuf[ctxt->controlBufIndex]; |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 694 | xmlGenericError(xmlGenericErrorContext, "\n---\n%s\n--\n", ptr); |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 695 | #endif |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 696 | |
| 697 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 698 | xmlGenericError(xmlGenericErrorContext, "Got %d\n", res); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 699 | #endif |
| 700 | return(res / 100); |
| 701 | } |
| 702 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 703 | /** |
| 704 | * xmlNanoFTPGetResponse: |
| 705 | * @ctx: an FTP context |
| 706 | * |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 707 | * Get the response from the FTP server after a command. |
| 708 | * Returns the code number |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 709 | */ |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 710 | |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 711 | int |
| 712 | xmlNanoFTPGetResponse(void *ctx) { |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 713 | int res; |
| 714 | |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 715 | res = xmlNanoFTPReadResponse(ctx); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 716 | |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 717 | return(res); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 718 | } |
| 719 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 720 | /** |
| 721 | * xmlNanoFTPCheckResponse: |
| 722 | * @ctx: an FTP context |
| 723 | * |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 724 | * Check if there is a response from the FTP server after a command. |
| 725 | * Returns the code number, or 0 |
| 726 | */ |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 727 | |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 728 | int |
| 729 | xmlNanoFTPCheckResponse(void *ctx) { |
| 730 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 731 | fd_set rfd; |
| 732 | struct timeval tv; |
| 733 | |
| 734 | tv.tv_sec = 0; |
| 735 | tv.tv_usec = 0; |
| 736 | FD_ZERO(&rfd); |
| 737 | FD_SET(ctxt->controlFd, &rfd); |
| 738 | switch(select(ctxt->controlFd + 1, &rfd, NULL, NULL, &tv)) { |
| 739 | case 0: |
| 740 | return(0); |
| 741 | case -1: |
| 742 | #ifdef DEBUG_FTP |
| 743 | perror("select"); |
| 744 | #endif |
| 745 | return(-1); |
| 746 | |
| 747 | } |
| 748 | |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 749 | return(xmlNanoFTPReadResponse(ctx)); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 750 | } |
| 751 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 752 | /** |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 753 | * Send the user authentification |
| 754 | */ |
| 755 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 756 | static int |
| 757 | xmlNanoFTPSendUser(void *ctx) { |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 758 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 759 | char buf[200]; |
| 760 | int len; |
| 761 | int res; |
| 762 | |
| 763 | if (ctxt->user == NULL) |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 764 | sprintf(buf, "USER anonymous\r\n"); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 765 | else |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 766 | #ifdef HAVE_SNPRINTF |
| 767 | snprintf(buf, sizeof(buf), "USER %s\r\n", ctxt->user); |
| 768 | #else |
| 769 | sprintf(buf, "USER %s\r\n", ctxt->user); |
| 770 | #endif |
| 771 | buf[sizeof(buf) - 1] = 0; |
| 772 | len = strlen(buf); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 773 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 774 | xmlGenericError(xmlGenericErrorContext, buf); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 775 | #endif |
| 776 | res = send(ctxt->controlFd, buf, len, 0); |
| 777 | if (res < 0) return(res); |
| 778 | return(0); |
| 779 | } |
| 780 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 781 | /** |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 782 | * Send the password authentification |
| 783 | */ |
| 784 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 785 | static int |
| 786 | xmlNanoFTPSendPasswd(void *ctx) { |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 787 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 788 | char buf[200]; |
| 789 | int len; |
| 790 | int res; |
| 791 | |
| 792 | if (ctxt->passwd == NULL) |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 793 | #ifdef HAVE_SNPRINTF |
| 794 | snprintf(buf, sizeof(buf), "PASS libxml@%s\r\n", hostname); |
| 795 | #else |
| 796 | sprintf(buf, "PASS libxml@%s\r\n", hostname); |
| 797 | #endif |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 798 | else |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 799 | #ifdef HAVE_SNPRINTF |
| 800 | snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd); |
| 801 | #else |
| 802 | sprintf(buf, "PASS %s\r\n", ctxt->passwd); |
| 803 | #endif |
| 804 | buf[sizeof(buf) - 1] = 0; |
| 805 | len = strlen(buf); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 806 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 807 | xmlGenericError(xmlGenericErrorContext, buf); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 808 | #endif |
| 809 | res = send(ctxt->controlFd, buf, len, 0); |
| 810 | if (res < 0) return(res); |
| 811 | return(0); |
| 812 | } |
| 813 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 814 | /** |
| 815 | * xmlNanoFTPQuit: |
| 816 | * @ctx: an FTP context |
| 817 | * |
| 818 | * Send a QUIT command to the server |
| 819 | * |
| 820 | * Returns -1 in case of error, 0 otherwise |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 821 | */ |
| 822 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 823 | |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 824 | int |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 825 | xmlNanoFTPQuit(void *ctx) { |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 826 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 827 | char buf[200]; |
| 828 | int len; |
| 829 | int res; |
| 830 | |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 831 | sprintf(buf, "QUIT\r\n"); |
| 832 | len = strlen(buf); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 833 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 834 | xmlGenericError(xmlGenericErrorContext, buf); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 835 | #endif |
| 836 | res = send(ctxt->controlFd, buf, len, 0); |
| 837 | return(0); |
| 838 | } |
| 839 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 840 | /** |
| 841 | * xmlNanoFTPConnect: |
| 842 | * @ctx: an FTP context |
| 843 | * |
| 844 | * Tries to open a control connection |
| 845 | * |
| 846 | * Returns -1 in case of error, 0 otherwise |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 847 | */ |
| 848 | |
| 849 | int |
| 850 | xmlNanoFTPConnect(void *ctx) { |
| 851 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 852 | struct hostent *hp; |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 853 | int port; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 854 | int res; |
| 855 | |
| 856 | if (ctxt == NULL) |
| 857 | return(-1); |
| 858 | if (ctxt->hostname == NULL) |
| 859 | return(-1); |
| 860 | |
| 861 | /* |
| 862 | * do the blocking DNS query. |
| 863 | */ |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 864 | if (proxy) |
| 865 | hp = gethostbyname(proxy); |
| 866 | else |
| 867 | hp = gethostbyname(ctxt->hostname); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 868 | if (hp == NULL) |
| 869 | return(-1); |
| 870 | |
| 871 | /* |
| 872 | * Prepare the socket |
| 873 | */ |
| 874 | memset(&ctxt->ftpAddr, 0, sizeof(ctxt->ftpAddr)); |
| 875 | ctxt->ftpAddr.sin_family = AF_INET; |
| 876 | memcpy(&ctxt->ftpAddr.sin_addr, hp->h_addr_list[0], hp->h_length); |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 877 | if (proxy) { |
| 878 | port = proxyPort; |
| 879 | } else { |
| 880 | port = ctxt->port; |
| 881 | } |
| 882 | if (port == 0) |
| 883 | port = 21; |
| 884 | ctxt->ftpAddr.sin_port = htons(port); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 885 | ctxt->controlFd = socket(AF_INET, SOCK_STREAM, 0); |
| 886 | if (ctxt->controlFd < 0) |
| 887 | return(-1); |
| 888 | |
| 889 | /* |
| 890 | * Do the connect. |
| 891 | */ |
| 892 | if (connect(ctxt->controlFd, (struct sockaddr *) &ctxt->ftpAddr, |
| 893 | sizeof(struct sockaddr_in)) < 0) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 894 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 895 | ctxt->controlFd = -1; |
| 896 | return(-1); |
| 897 | } |
| 898 | |
| 899 | /* |
| 900 | * Wait for the HELLO from the server. |
| 901 | */ |
| 902 | res = xmlNanoFTPGetResponse(ctxt); |
| 903 | if (res != 2) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 904 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 905 | ctxt->controlFd = -1; |
| 906 | return(-1); |
| 907 | } |
| 908 | |
| 909 | /* |
| 910 | * State diagram for the login operation on the FTP server |
| 911 | * |
| 912 | * Reference: RFC 959 |
| 913 | * |
| 914 | * 1 |
| 915 | * +---+ USER +---+------------->+---+ |
| 916 | * | B |---------->| W | 2 ---->| E | |
| 917 | * +---+ +---+------ | -->+---+ |
| 918 | * | | | | | |
| 919 | * 3 | | 4,5 | | | |
| 920 | * -------------- ----- | | | |
| 921 | * | | | | | |
| 922 | * | | | | | |
| 923 | * | --------- | |
| 924 | * | 1| | | | |
| 925 | * V | | | | |
| 926 | * +---+ PASS +---+ 2 | ------>+---+ |
| 927 | * | |---------->| W |------------->| S | |
| 928 | * +---+ +---+ ---------->+---+ |
| 929 | * | | | | | |
| 930 | * 3 | |4,5| | | |
| 931 | * -------------- -------- | |
| 932 | * | | | | | |
| 933 | * | | | | | |
| 934 | * | ----------- |
| 935 | * | 1,3| | | | |
| 936 | * V | 2| | | |
| 937 | * +---+ ACCT +---+-- | ----->+---+ |
| 938 | * | |---------->| W | 4,5 -------->| F | |
| 939 | * +---+ +---+------------->+---+ |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 940 | * |
| 941 | * Of course in case of using a proxy this get really nasty and is not |
| 942 | * standardized at all :-( |
| 943 | */ |
| 944 | if (proxy) { |
| 945 | int len; |
| 946 | char buf[400]; |
| 947 | |
| 948 | if (proxyUser != NULL) { |
| 949 | /* |
| 950 | * We need proxy auth |
| 951 | */ |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 952 | #ifdef HAVE_SNPRINTF |
| 953 | snprintf(buf, sizeof(buf), "USER %s\r\n", proxyUser); |
| 954 | #else |
| 955 | sprintf(buf, "USER %s\r\n", proxyUser); |
| 956 | #endif |
| 957 | buf[sizeof(buf) - 1] = 0; |
| 958 | len = strlen(buf); |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 959 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 960 | xmlGenericError(xmlGenericErrorContext, buf); |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 961 | #endif |
| 962 | res = send(ctxt->controlFd, buf, len, 0); |
| 963 | if (res < 0) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 964 | closesocket(ctxt->controlFd); |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 965 | ctxt->controlFd = -1; |
| 966 | return(res); |
| 967 | } |
| 968 | res = xmlNanoFTPGetResponse(ctxt); |
| 969 | switch (res) { |
| 970 | case 2: |
| 971 | if (proxyPasswd == NULL) |
| 972 | break; |
| 973 | case 3: |
| 974 | if (proxyPasswd != NULL) |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 975 | #ifdef HAVE_SNPRINTF |
| 976 | snprintf(buf, sizeof(buf), "PASS %s\r\n", proxyPasswd); |
| 977 | #else |
| 978 | sprintf(buf, "PASS %s\r\n", proxyPasswd); |
| 979 | #endif |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 980 | else |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 981 | #ifdef HAVE_SNPRINTF |
| 982 | snprintf(buf, sizeof(buf), "PASS libxml@%s\r\n", |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 983 | hostname); |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 984 | #else |
| 985 | sprintf(buf, "PASS libxml@%s\r\n", hostname); |
| 986 | #endif |
| 987 | buf[sizeof(buf) - 1] = 0; |
| 988 | len = strlen(buf); |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 989 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 990 | xmlGenericError(xmlGenericErrorContext, buf); |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 991 | #endif |
| 992 | res = send(ctxt->controlFd, buf, len, 0); |
| 993 | if (res < 0) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 994 | closesocket(ctxt->controlFd); |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 995 | ctxt->controlFd = -1; |
| 996 | return(res); |
| 997 | } |
| 998 | res = xmlNanoFTPGetResponse(ctxt); |
| 999 | if (res > 3) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1000 | closesocket(ctxt->controlFd); |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1001 | ctxt->controlFd = -1; |
| 1002 | return(-1); |
| 1003 | } |
| 1004 | break; |
| 1005 | case 1: |
| 1006 | break; |
| 1007 | case 4: |
| 1008 | case 5: |
| 1009 | case -1: |
| 1010 | default: |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1011 | closesocket(ctxt->controlFd); |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1012 | ctxt->controlFd = -1; |
| 1013 | return(-1); |
| 1014 | } |
| 1015 | } |
| 1016 | |
| 1017 | /* |
| 1018 | * We assume we don't need more authentication to the proxy |
| 1019 | * and that it succeeded :-\ |
| 1020 | */ |
| 1021 | switch (proxyType) { |
| 1022 | case 0: |
| 1023 | /* we will try in seqence */ |
| 1024 | case 1: |
| 1025 | /* Using SITE command */ |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 1026 | #ifdef HAVE_SNPRINTF |
| 1027 | snprintf(buf, sizeof(buf), "SITE %s\r\n", ctxt->hostname); |
| 1028 | #else |
| 1029 | sprintf(buf, "SITE %s\r\n", ctxt->hostname); |
| 1030 | #endif |
| 1031 | buf[sizeof(buf) - 1] = 0; |
| 1032 | len = strlen(buf); |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1033 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1034 | xmlGenericError(xmlGenericErrorContext, buf); |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1035 | #endif |
| 1036 | res = send(ctxt->controlFd, buf, len, 0); |
| 1037 | if (res < 0) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1038 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1039 | ctxt->controlFd = -1; |
| 1040 | return(res); |
| 1041 | } |
| 1042 | res = xmlNanoFTPGetResponse(ctxt); |
| 1043 | if (res == 2) { |
| 1044 | /* we assume it worked :-\ 1 is error for SITE command */ |
| 1045 | proxyType = 1; |
| 1046 | break; |
| 1047 | } |
| 1048 | if (proxyType == 1) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1049 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1050 | ctxt->controlFd = -1; |
| 1051 | return(-1); |
| 1052 | } |
| 1053 | case 2: |
| 1054 | /* USER user@host command */ |
| 1055 | if (ctxt->user == NULL) |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 1056 | #ifdef HAVE_SNPRINTF |
| 1057 | snprintf(buf, sizeof(buf), "USER anonymous@%s\r\n", |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1058 | ctxt->hostname); |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 1059 | #else |
| 1060 | sprintf(buf, "USER anonymous@%s\r\n", ctxt->hostname); |
| 1061 | #endif |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1062 | else |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 1063 | #ifdef HAVE_SNPRINTF |
| 1064 | snprintf(buf, sizeof(buf), "USER %s@%s\r\n", |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1065 | ctxt->user, ctxt->hostname); |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 1066 | #else |
| 1067 | sprintf(buf, "USER %s@%s\r\n", |
| 1068 | ctxt->user, ctxt->hostname); |
| 1069 | #endif |
| 1070 | buf[sizeof(buf) - 1] = 0; |
| 1071 | len = strlen(buf); |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1072 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1073 | xmlGenericError(xmlGenericErrorContext, buf); |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1074 | #endif |
| 1075 | res = send(ctxt->controlFd, buf, len, 0); |
| 1076 | if (res < 0) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1077 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1078 | ctxt->controlFd = -1; |
| 1079 | return(res); |
| 1080 | } |
| 1081 | res = xmlNanoFTPGetResponse(ctxt); |
| 1082 | if ((res == 1) || (res == 2)) { |
| 1083 | /* we assume it worked :-\ */ |
| 1084 | proxyType = 2; |
| 1085 | return(0); |
| 1086 | } |
| 1087 | if (ctxt->passwd == NULL) |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 1088 | #ifdef HAVE_SNPRINTF |
| 1089 | snprintf(buf, sizeof(buf), "PASS libxml@%s\r\n", hostname); |
| 1090 | #else |
| 1091 | sprintf(buf, "PASS libxml@%s\r\n", hostname); |
| 1092 | #endif |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1093 | else |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 1094 | #ifdef HAVE_SNPRINTF |
| 1095 | snprintf(buf, sizeof(buf), "PASS %s\r\n", ctxt->passwd); |
| 1096 | #else |
| 1097 | sprintf(buf, "PASS %s\r\n", ctxt->passwd); |
| 1098 | #endif |
| 1099 | buf[sizeof(buf) - 1] = 0; |
| 1100 | len = strlen(buf); |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1101 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1102 | xmlGenericError(xmlGenericErrorContext, buf); |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1103 | #endif |
| 1104 | res = send(ctxt->controlFd, buf, len, 0); |
| 1105 | if (res < 0) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1106 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1107 | ctxt->controlFd = -1; |
| 1108 | return(res); |
| 1109 | } |
| 1110 | res = xmlNanoFTPGetResponse(ctxt); |
| 1111 | if ((res == 1) || (res == 2)) { |
| 1112 | /* we assume it worked :-\ */ |
| 1113 | proxyType = 2; |
| 1114 | return(0); |
| 1115 | } |
| 1116 | if (proxyType == 2) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1117 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1118 | ctxt->controlFd = -1; |
| 1119 | return(-1); |
| 1120 | } |
| 1121 | case 3: |
| 1122 | /* |
| 1123 | * If you need support for other Proxy authentication scheme |
| 1124 | * send the code or at least the sequence in use. |
| 1125 | */ |
| 1126 | default: |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1127 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1128 | ctxt->controlFd = -1; |
| 1129 | return(-1); |
| 1130 | } |
| 1131 | } |
| 1132 | /* |
| 1133 | * Non-proxy handling. |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1134 | */ |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1135 | res = xmlNanoFTPSendUser(ctxt); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1136 | if (res < 0) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1137 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1138 | ctxt->controlFd = -1; |
| 1139 | return(-1); |
| 1140 | } |
| 1141 | res = xmlNanoFTPGetResponse(ctxt); |
| 1142 | switch (res) { |
| 1143 | case 2: |
| 1144 | return(0); |
| 1145 | case 3: |
| 1146 | break; |
| 1147 | case 1: |
| 1148 | case 4: |
| 1149 | case 5: |
| 1150 | case -1: |
| 1151 | default: |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1152 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1153 | ctxt->controlFd = -1; |
| 1154 | return(-1); |
| 1155 | } |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1156 | res = xmlNanoFTPSendPasswd(ctxt); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1157 | if (res < 0) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1158 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1159 | ctxt->controlFd = -1; |
| 1160 | return(-1); |
| 1161 | } |
| 1162 | res = xmlNanoFTPGetResponse(ctxt); |
| 1163 | switch (res) { |
| 1164 | case 2: |
Daniel Veillard | 5feb849 | 2000-02-02 17:15:36 +0000 | [diff] [blame] | 1165 | break; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1166 | case 3: |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1167 | xmlGenericError(xmlGenericErrorContext, |
| 1168 | "FTP server asking for ACCNT on anonymous\n"); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1169 | case 1: |
| 1170 | case 4: |
| 1171 | case 5: |
| 1172 | case -1: |
| 1173 | default: |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1174 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1175 | ctxt->controlFd = -1; |
| 1176 | return(-1); |
| 1177 | } |
| 1178 | |
| 1179 | return(0); |
| 1180 | } |
| 1181 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1182 | /** |
| 1183 | * xmlNanoFTPConnectTo: |
| 1184 | * @server: an FTP server name |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 1185 | * @port: the port (use 21 if 0) |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1186 | * |
| 1187 | * Tries to open a control connection to the given server/port |
| 1188 | * |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 1189 | * Returns an fTP context or NULL if it failed |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1190 | */ |
| 1191 | |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 1192 | void* |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1193 | xmlNanoFTPConnectTo(const char *server, int port) { |
| 1194 | xmlNanoFTPCtxtPtr ctxt; |
| 1195 | int res; |
| 1196 | |
| 1197 | xmlNanoFTPInit(); |
| 1198 | if (server == NULL) |
| 1199 | return(NULL); |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 1200 | ctxt = (xmlNanoFTPCtxtPtr) xmlNanoFTPNewCtxt(NULL); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1201 | ctxt->hostname = xmlMemStrdup(server); |
| 1202 | if (port != 0) |
| 1203 | ctxt->port = port; |
| 1204 | res = xmlNanoFTPConnect(ctxt); |
| 1205 | if (res < 0) { |
| 1206 | xmlNanoFTPFreeCtxt(ctxt); |
| 1207 | return(NULL); |
| 1208 | } |
| 1209 | return(ctxt); |
| 1210 | } |
| 1211 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1212 | /** |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 1213 | * xmlNanoFTPCwd: |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1214 | * @ctx: an FTP context |
| 1215 | * @directory: a directory on the server |
| 1216 | * |
| 1217 | * Tries to change the remote directory |
| 1218 | * |
| 1219 | * Returns -1 incase of error, 1 if CWD worked, 0 if it failed |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1220 | */ |
| 1221 | |
| 1222 | int |
| 1223 | xmlNanoFTPCwd(void *ctx, char *directory) { |
| 1224 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 1225 | char buf[400]; |
| 1226 | int len; |
| 1227 | int res; |
| 1228 | |
| 1229 | /* |
| 1230 | * Expected response code for CWD: |
| 1231 | * |
| 1232 | * CWD |
| 1233 | * 250 |
| 1234 | * 500, 501, 502, 421, 530, 550 |
| 1235 | */ |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 1236 | #ifdef HAVE_SNPRINTF |
| 1237 | snprintf(buf, sizeof(buf), "CWD %s\r\n", directory); |
| 1238 | #else |
| 1239 | sprintf(buf, "CWD %s\r\n", directory); |
| 1240 | #endif |
| 1241 | buf[sizeof(buf) - 1] = 0; |
| 1242 | len = strlen(buf); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1243 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1244 | xmlGenericError(xmlGenericErrorContext, buf); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1245 | #endif |
| 1246 | res = send(ctxt->controlFd, buf, len, 0); |
| 1247 | if (res < 0) return(res); |
| 1248 | res = xmlNanoFTPGetResponse(ctxt); |
| 1249 | if (res == 4) { |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1250 | return(-1); |
| 1251 | } |
| 1252 | if (res == 2) return(1); |
| 1253 | if (res == 5) { |
| 1254 | return(0); |
| 1255 | } |
| 1256 | return(0); |
| 1257 | } |
| 1258 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1259 | /** |
| 1260 | * xmlNanoFTPGetConnection: |
| 1261 | * @ctx: an FTP context |
| 1262 | * |
| 1263 | * Try to open a data connection to the server. Currently only |
| 1264 | * passive mode is supported. |
| 1265 | * |
| 1266 | * Returns -1 incase of error, 0 otherwise |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1267 | */ |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1268 | |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1269 | int |
| 1270 | xmlNanoFTPGetConnection(void *ctx) { |
| 1271 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 1272 | char buf[200], *cur; |
| 1273 | int len, i; |
| 1274 | int res; |
| 1275 | unsigned char ad[6], *adp, *portp; |
| 1276 | unsigned int temp[6]; |
| 1277 | struct sockaddr_in dataAddr; |
Daniel Veillard | b0426ca | 2000-10-11 23:39:43 +0000 | [diff] [blame] | 1278 | SOCKLEN_T dataAddrLen; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1279 | |
| 1280 | ctxt->dataFd = socket (AF_INET, SOCK_STREAM, IPPROTO_TCP); |
| 1281 | if (ctxt->dataFd < 0) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1282 | xmlGenericError(xmlGenericErrorContext, |
| 1283 | "xmlNanoFTPGetConnection: failed to create socket\n"); |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1284 | return(-1); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1285 | } |
| 1286 | dataAddrLen = sizeof(dataAddr); |
| 1287 | memset(&dataAddr, 0, dataAddrLen); |
| 1288 | dataAddr.sin_family = AF_INET; |
| 1289 | |
| 1290 | if (ctxt->passive) { |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 1291 | sprintf(buf, "PASV\r\n"); |
| 1292 | len = strlen(buf); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1293 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1294 | xmlGenericError(xmlGenericErrorContext, buf); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1295 | #endif |
| 1296 | res = send(ctxt->controlFd, buf, len, 0); |
| 1297 | if (res < 0) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1298 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1299 | return(res); |
| 1300 | } |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 1301 | res = xmlNanoFTPReadResponse(ctx); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1302 | if (res != 2) { |
| 1303 | if (res == 5) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1304 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1305 | return(-1); |
| 1306 | } else { |
| 1307 | /* |
| 1308 | * retry with an active connection |
| 1309 | */ |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1310 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1311 | ctxt->passive = 0; |
| 1312 | } |
| 1313 | } |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 1314 | cur = &ctxt->controlBuf[ctxt->controlBufAnswer]; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1315 | while (((*cur < '0') || (*cur > '9')) && *cur != '\0') cur++; |
Daniel Veillard | bf43275 | 2000-11-12 15:56:56 +0000 | [diff] [blame] | 1316 | if (sscanf(cur, "%u,%u,%u,%u,%u,%u", &temp[0], &temp[1], &temp[2], |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1317 | &temp[3], &temp[4], &temp[5]) != 6) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1318 | xmlGenericError(xmlGenericErrorContext, |
| 1319 | "Invalid answer to PASV\n"); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1320 | if (ctxt->dataFd != -1) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1321 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1322 | } |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1323 | return(-1); |
| 1324 | } |
| 1325 | for (i=0; i<6; i++) ad[i] = (unsigned char) (temp[i] & 0xff); |
| 1326 | memcpy(&dataAddr.sin_addr, &ad[0], 4); |
| 1327 | memcpy(&dataAddr.sin_port, &ad[4], 2); |
| 1328 | if (connect(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1329 | xmlGenericError(xmlGenericErrorContext, |
| 1330 | "Failed to create a data connection\n"); |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1331 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1332 | return (-1); |
| 1333 | } |
| 1334 | } else { |
| 1335 | getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen); |
| 1336 | dataAddr.sin_port = 0; |
| 1337 | if (bind(ctxt->dataFd, (struct sockaddr *) &dataAddr, dataAddrLen) < 0) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1338 | xmlGenericError(xmlGenericErrorContext, |
| 1339 | "Failed to bind a port\n"); |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1340 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1341 | return (-1); |
| 1342 | } |
| 1343 | getsockname(ctxt->dataFd, (struct sockaddr *) &dataAddr, &dataAddrLen); |
| 1344 | |
| 1345 | if (listen(ctxt->dataFd, 1) < 0) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1346 | xmlGenericError(xmlGenericErrorContext, |
| 1347 | "Could not listen on port %d\n", |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1348 | ntohs(dataAddr.sin_port)); |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1349 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1350 | return (-1); |
| 1351 | } |
| 1352 | adp = (unsigned char *) &dataAddr.sin_addr; |
| 1353 | portp = (unsigned char *) &dataAddr.sin_port; |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 1354 | #ifdef HAVE_SNPRINTF |
| 1355 | snprintf(buf, sizeof(buf), "PORT %d,%d,%d,%d,%d,%d\r\n", |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1356 | adp[0] & 0xff, adp[1] & 0xff, adp[2] & 0xff, adp[3] & 0xff, |
| 1357 | portp[0] & 0xff, portp[1] & 0xff); |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 1358 | #else |
| 1359 | sprintf(buf, "PORT %d,%d,%d,%d,%d,%d\r\n", |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1360 | adp[0] & 0xff, adp[1] & 0xff, adp[2] & 0xff, adp[3] & 0xff, |
| 1361 | portp[0] & 0xff, portp[1] & 0xff); |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 1362 | #endif |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1363 | buf[sizeof(buf) - 1] = 0; |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 1364 | len = strlen(buf); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1365 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1366 | xmlGenericError(xmlGenericErrorContext, buf); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1367 | #endif |
| 1368 | |
| 1369 | res = send(ctxt->controlFd, buf, len, 0); |
| 1370 | if (res < 0) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1371 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1372 | return(res); |
| 1373 | } |
| 1374 | res = xmlNanoFTPGetResponse(ctxt); |
| 1375 | if (res != 2) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1376 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1377 | return(-1); |
| 1378 | } |
| 1379 | } |
| 1380 | return(ctxt->dataFd); |
| 1381 | |
| 1382 | } |
| 1383 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1384 | /** |
| 1385 | * xmlNanoFTPCloseConnection: |
| 1386 | * @ctx: an FTP context |
| 1387 | * |
| 1388 | * Close the data connection from the server |
| 1389 | * |
| 1390 | * Returns -1 incase of error, 0 otherwise |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1391 | */ |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1392 | |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1393 | int |
| 1394 | xmlNanoFTPCloseConnection(void *ctx) { |
| 1395 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 1396 | int res; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1397 | fd_set rfd, efd; |
| 1398 | struct timeval tv; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1399 | |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1400 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1401 | tv.tv_sec = 15; |
| 1402 | tv.tv_usec = 0; |
| 1403 | FD_ZERO(&rfd); |
| 1404 | FD_SET(ctxt->controlFd, &rfd); |
| 1405 | FD_ZERO(&efd); |
| 1406 | FD_SET(ctxt->controlFd, &efd); |
| 1407 | res = select(ctxt->controlFd + 1, &rfd, NULL, &efd, &tv); |
| 1408 | if (res < 0) { |
| 1409 | #ifdef DEBUG_FTP |
| 1410 | perror("select"); |
| 1411 | #endif |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1412 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1413 | return(-1); |
| 1414 | } |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1415 | if (res == 0) { |
Daniel Veillard | 2f2bf41 | 2000-08-20 15:11:02 +0000 | [diff] [blame] | 1416 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1417 | xmlGenericError(xmlGenericErrorContext, |
| 1418 | "xmlNanoFTPCloseConnection: timeout\n"); |
Daniel Veillard | 2f2bf41 | 2000-08-20 15:11:02 +0000 | [diff] [blame] | 1419 | #endif |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1420 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1421 | } else { |
| 1422 | res = xmlNanoFTPGetResponse(ctxt); |
| 1423 | if (res != 2) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1424 | closesocket(ctxt->controlFd); ctxt->controlFd = -1; |
Daniel Veillard | cf46199 | 2000-03-14 18:30:20 +0000 | [diff] [blame] | 1425 | return(-1); |
| 1426 | } |
| 1427 | } |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1428 | return(0); |
| 1429 | } |
| 1430 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1431 | /** |
| 1432 | * xmlNanoFTPParseList: |
| 1433 | * @list: some data listing received from the server |
| 1434 | * @callback: the user callback |
| 1435 | * @userData: the user callback data |
| 1436 | * |
| 1437 | * Parse at most one entry from the listing. |
| 1438 | * |
| 1439 | * Returns -1 incase of error, the lenght of data parsed otherwise |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1440 | */ |
| 1441 | |
| 1442 | static int |
| 1443 | xmlNanoFTPParseList(const char *list, ftpListCallback callback, void *userData) { |
| 1444 | const char *cur = list; |
| 1445 | char filename[151]; |
| 1446 | char attrib[11]; |
| 1447 | char owner[11]; |
| 1448 | char group[11]; |
| 1449 | char month[4]; |
| 1450 | int year = 0; |
| 1451 | int minute = 0; |
| 1452 | int hour = 0; |
| 1453 | int day = 0; |
| 1454 | unsigned long size = 0; |
| 1455 | int links = 0; |
| 1456 | int i; |
| 1457 | |
| 1458 | if (!strncmp(cur, "total", 5)) { |
| 1459 | cur += 5; |
| 1460 | while (*cur == ' ') cur++; |
| 1461 | while ((*cur >= '0') && (*cur <= '9')) |
| 1462 | links = (links * 10) + (*cur++ - '0'); |
| 1463 | while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r')) |
| 1464 | cur++; |
| 1465 | return(cur - list); |
| 1466 | } else if (*list == '+') { |
| 1467 | return(0); |
| 1468 | } else { |
| 1469 | while ((*cur == ' ') || (*cur == '\n') || (*cur == '\r')) |
| 1470 | cur++; |
| 1471 | if (*cur == 0) return(0); |
| 1472 | i = 0; |
| 1473 | while (*cur != ' ') { |
| 1474 | if (i < 10) |
| 1475 | attrib[i++] = *cur; |
| 1476 | cur++; |
| 1477 | if (*cur == 0) return(0); |
| 1478 | } |
| 1479 | attrib[10] = 0; |
| 1480 | while (*cur == ' ') cur++; |
| 1481 | if (*cur == 0) return(0); |
| 1482 | while ((*cur >= '0') && (*cur <= '9')) |
| 1483 | links = (links * 10) + (*cur++ - '0'); |
| 1484 | while (*cur == ' ') cur++; |
| 1485 | if (*cur == 0) return(0); |
| 1486 | i = 0; |
| 1487 | while (*cur != ' ') { |
| 1488 | if (i < 10) |
| 1489 | owner[i++] = *cur; |
| 1490 | cur++; |
| 1491 | if (*cur == 0) return(0); |
| 1492 | } |
| 1493 | owner[i] = 0; |
| 1494 | while (*cur == ' ') cur++; |
| 1495 | if (*cur == 0) return(0); |
| 1496 | i = 0; |
| 1497 | while (*cur != ' ') { |
| 1498 | if (i < 10) |
| 1499 | group[i++] = *cur; |
| 1500 | cur++; |
| 1501 | if (*cur == 0) return(0); |
| 1502 | } |
| 1503 | group[i] = 0; |
| 1504 | while (*cur == ' ') cur++; |
| 1505 | if (*cur == 0) return(0); |
| 1506 | while ((*cur >= '0') && (*cur <= '9')) |
| 1507 | size = (size * 10) + (*cur++ - '0'); |
| 1508 | while (*cur == ' ') cur++; |
| 1509 | if (*cur == 0) return(0); |
| 1510 | i = 0; |
| 1511 | while (*cur != ' ') { |
| 1512 | if (i < 3) |
| 1513 | month[i++] = *cur; |
| 1514 | cur++; |
| 1515 | if (*cur == 0) return(0); |
| 1516 | } |
| 1517 | month[i] = 0; |
| 1518 | while (*cur == ' ') cur++; |
| 1519 | if (*cur == 0) return(0); |
| 1520 | while ((*cur >= '0') && (*cur <= '9')) |
| 1521 | day = (day * 10) + (*cur++ - '0'); |
| 1522 | while (*cur == ' ') cur++; |
| 1523 | if (*cur == 0) return(0); |
| 1524 | if ((cur[1] == 0) || (cur[2] == 0)) return(0); |
| 1525 | if ((cur[1] == ':') || (cur[2] == ':')) { |
| 1526 | while ((*cur >= '0') && (*cur <= '9')) |
| 1527 | hour = (hour * 10) + (*cur++ - '0'); |
| 1528 | if (*cur == ':') cur++; |
| 1529 | while ((*cur >= '0') && (*cur <= '9')) |
| 1530 | minute = (minute * 10) + (*cur++ - '0'); |
| 1531 | } else { |
| 1532 | while ((*cur >= '0') && (*cur <= '9')) |
| 1533 | year = (year * 10) + (*cur++ - '0'); |
| 1534 | } |
| 1535 | while (*cur == ' ') cur++; |
| 1536 | if (*cur == 0) return(0); |
| 1537 | i = 0; |
| 1538 | while ((*cur != '\n') && (*cur != '\r')) { |
| 1539 | if (i < 150) |
| 1540 | filename[i++] = *cur; |
| 1541 | cur++; |
| 1542 | if (*cur == 0) return(0); |
| 1543 | } |
| 1544 | filename[i] = 0; |
| 1545 | if ((*cur != '\n') && (*cur != '\r')) |
| 1546 | return(0); |
| 1547 | while ((*cur == '\n') || (*cur == '\r')) |
| 1548 | cur++; |
| 1549 | } |
| 1550 | if (callback != NULL) { |
| 1551 | callback(userData, filename, attrib, owner, group, size, links, |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1552 | year, month, day, hour, minute); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1553 | } |
| 1554 | return(cur - list); |
| 1555 | } |
| 1556 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1557 | /** |
| 1558 | * xmlNanoFTPList: |
| 1559 | * @ctx: an FTP context |
| 1560 | * @callback: the user callback |
| 1561 | * @userData: the user callback data |
| 1562 | * @filename: optional files to list |
| 1563 | * |
| 1564 | * Do a listing on the server. All files info are passed back |
| 1565 | * in the callbacks. |
| 1566 | * |
| 1567 | * Returns -1 incase of error, 0 otherwise |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1568 | */ |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1569 | |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1570 | int |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1571 | xmlNanoFTPList(void *ctx, ftpListCallback callback, void *userData, |
| 1572 | char *filename) { |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1573 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 1574 | char buf[4096 + 1]; |
| 1575 | int len, res; |
| 1576 | int index = 0, base; |
| 1577 | fd_set rfd, efd; |
| 1578 | struct timeval tv; |
| 1579 | |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1580 | if (filename == NULL) { |
| 1581 | if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1) |
| 1582 | return(-1); |
| 1583 | ctxt->dataFd = xmlNanoFTPGetConnection(ctxt); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1584 | if (ctxt->dataFd == -1) |
| 1585 | return(-1); |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 1586 | sprintf(buf, "LIST -L\r\n"); |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1587 | } else { |
| 1588 | if (filename[0] != '/') { |
| 1589 | if (xmlNanoFTPCwd(ctxt, ctxt->path) < 1) |
| 1590 | return(-1); |
| 1591 | } |
| 1592 | ctxt->dataFd = xmlNanoFTPGetConnection(ctxt); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1593 | if (ctxt->dataFd == -1) |
| 1594 | return(-1); |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 1595 | #ifdef HAVE_SNPRINTF |
| 1596 | snprintf(buf, sizeof(buf), "LIST -L %s\r\n", filename); |
| 1597 | #else |
| 1598 | sprintf(buf, "LIST -L %s\r\n", filename); |
| 1599 | #endif |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1600 | } |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 1601 | buf[sizeof(buf) - 1] = 0; |
| 1602 | len = strlen(buf); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1603 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1604 | xmlGenericError(xmlGenericErrorContext, buf); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1605 | #endif |
| 1606 | res = send(ctxt->controlFd, buf, len, 0); |
| 1607 | if (res < 0) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1608 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1609 | return(res); |
| 1610 | } |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 1611 | res = xmlNanoFTPReadResponse(ctxt); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1612 | if (res != 1) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1613 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1614 | return(-res); |
| 1615 | } |
| 1616 | |
| 1617 | do { |
| 1618 | tv.tv_sec = 1; |
| 1619 | tv.tv_usec = 0; |
| 1620 | FD_ZERO(&rfd); |
| 1621 | FD_SET(ctxt->dataFd, &rfd); |
| 1622 | FD_ZERO(&efd); |
| 1623 | FD_SET(ctxt->dataFd, &efd); |
| 1624 | res = select(ctxt->dataFd + 1, &rfd, NULL, &efd, &tv); |
| 1625 | if (res < 0) { |
| 1626 | #ifdef DEBUG_FTP |
| 1627 | perror("select"); |
| 1628 | #endif |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1629 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1630 | return(-1); |
| 1631 | } |
| 1632 | if (res == 0) { |
| 1633 | res = xmlNanoFTPCheckResponse(ctxt); |
| 1634 | if (res < 0) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1635 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1636 | ctxt->dataFd = -1; |
| 1637 | return(-1); |
| 1638 | } |
| 1639 | if (res == 2) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1640 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1641 | return(0); |
| 1642 | } |
| 1643 | |
| 1644 | continue; |
| 1645 | } |
| 1646 | |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1647 | if ((len = recv(ctxt->dataFd, &buf[index], sizeof(buf) - (index + 1), 0)) < 0) { |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1648 | #ifdef DEBUG_FTP |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1649 | perror("recv"); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1650 | #endif |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1651 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1652 | ctxt->dataFd = -1; |
| 1653 | return(-1); |
| 1654 | } |
| 1655 | #ifdef DEBUG_FTP |
| 1656 | write(1, &buf[index], len); |
| 1657 | #endif |
| 1658 | index += len; |
| 1659 | buf[index] = 0; |
| 1660 | base = 0; |
| 1661 | do { |
| 1662 | res = xmlNanoFTPParseList(&buf[base], callback, userData); |
| 1663 | base += res; |
| 1664 | } while (res > 0); |
| 1665 | |
| 1666 | memmove(&buf[0], &buf[base], index - base); |
| 1667 | index -= base; |
| 1668 | } while (len != 0); |
| 1669 | xmlNanoFTPCloseConnection(ctxt); |
| 1670 | return(0); |
| 1671 | } |
| 1672 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1673 | /** |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1674 | * xmlNanoFTPGetSocket: |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1675 | * @ctx: an FTP context |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1676 | * @filename: the file to retrieve (or NULL if path is in context). |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1677 | * |
| 1678 | * Initiate fetch of the given file from the server. |
| 1679 | * |
| 1680 | * Returns the socket for the data connection, or <0 in case of error |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1681 | */ |
| 1682 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1683 | |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1684 | int |
| 1685 | xmlNanoFTPGetSocket(void *ctx, const char *filename) { |
| 1686 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 1687 | char buf[300]; |
| 1688 | int res, len; |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1689 | if ((filename == NULL) && (ctxt->path == NULL)) |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1690 | return(-1); |
| 1691 | ctxt->dataFd = xmlNanoFTPGetConnection(ctxt); |
Daniel Veillard | be80396 | 2000-06-28 23:40:59 +0000 | [diff] [blame] | 1692 | if (ctxt->dataFd == -1) |
| 1693 | return(-1); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1694 | |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 1695 | sprintf(buf, "TYPE I\r\n"); |
| 1696 | len = strlen(buf); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1697 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1698 | xmlGenericError(xmlGenericErrorContext, buf); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1699 | #endif |
| 1700 | res = send(ctxt->controlFd, buf, len, 0); |
| 1701 | if (res < 0) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1702 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1703 | return(res); |
| 1704 | } |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 1705 | res = xmlNanoFTPReadResponse(ctxt); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1706 | if (res != 2) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1707 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1708 | return(-res); |
| 1709 | } |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1710 | if (filename == NULL) |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 1711 | #ifdef HAVE_SNPRINTF |
| 1712 | snprintf(buf, sizeof(buf), "RETR %s\r\n", ctxt->path); |
| 1713 | #else |
| 1714 | sprintf(buf, "RETR %s\r\n", ctxt->path); |
| 1715 | #endif |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1716 | else |
Daniel Veillard | 39c7d71 | 2000-09-10 16:14:55 +0000 | [diff] [blame] | 1717 | #ifdef HAVE_SNPRINTF |
| 1718 | snprintf(buf, sizeof(buf), "RETR %s\r\n", filename); |
| 1719 | #else |
| 1720 | sprintf(buf, "RETR %s\r\n", filename); |
| 1721 | #endif |
| 1722 | buf[sizeof(buf) - 1] = 0; |
| 1723 | len = strlen(buf); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1724 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1725 | xmlGenericError(xmlGenericErrorContext, buf); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1726 | #endif |
| 1727 | res = send(ctxt->controlFd, buf, len, 0); |
| 1728 | if (res < 0) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1729 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1730 | return(res); |
| 1731 | } |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 1732 | res = xmlNanoFTPReadResponse(ctxt); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1733 | if (res != 1) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1734 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1735 | return(-res); |
| 1736 | } |
| 1737 | return(ctxt->dataFd); |
| 1738 | } |
| 1739 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1740 | /** |
| 1741 | * xmlNanoFTPGet: |
| 1742 | * @ctx: an FTP context |
| 1743 | * @callback: the user callback |
| 1744 | * @userData: the user callback data |
| 1745 | * @filename: the file to retrieve |
| 1746 | * |
| 1747 | * Fetch the given file from the server. All data are passed back |
| 1748 | * in the callbacks. The last callback has a size of 0 block. |
| 1749 | * |
| 1750 | * Returns -1 incase of error, 0 otherwise |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1751 | */ |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1752 | |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1753 | int |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1754 | xmlNanoFTPGet(void *ctx, ftpDataCallback callback, void *userData, |
| 1755 | const char *filename) { |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1756 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 1757 | char buf[4096]; |
| 1758 | int len = 0, res; |
| 1759 | fd_set rfd; |
| 1760 | struct timeval tv; |
| 1761 | |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1762 | if ((filename == NULL) && (ctxt->path == NULL)) |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1763 | return(-1); |
| 1764 | if (callback == NULL) |
| 1765 | return(-1); |
| 1766 | if (xmlNanoFTPGetSocket(ctxt, filename) < 0) |
| 1767 | return(-1); |
| 1768 | |
| 1769 | do { |
| 1770 | tv.tv_sec = 1; |
| 1771 | tv.tv_usec = 0; |
| 1772 | FD_ZERO(&rfd); |
| 1773 | FD_SET(ctxt->dataFd, &rfd); |
| 1774 | res = select(ctxt->dataFd + 1, &rfd, NULL, NULL, &tv); |
| 1775 | if (res < 0) { |
| 1776 | #ifdef DEBUG_FTP |
| 1777 | perror("select"); |
| 1778 | #endif |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1779 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1780 | return(-1); |
| 1781 | } |
| 1782 | if (res == 0) { |
| 1783 | res = xmlNanoFTPCheckResponse(ctxt); |
| 1784 | if (res < 0) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1785 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1786 | ctxt->dataFd = -1; |
| 1787 | return(-1); |
| 1788 | } |
| 1789 | if (res == 2) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1790 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1791 | return(0); |
| 1792 | } |
| 1793 | |
| 1794 | continue; |
| 1795 | } |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1796 | if ((len = recv(ctxt->dataFd, buf, sizeof(buf), 0)) < 0) { |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1797 | callback(userData, buf, len); |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1798 | closesocket(ctxt->dataFd); ctxt->dataFd = -1; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1799 | return(-1); |
| 1800 | } |
| 1801 | callback(userData, buf, len); |
| 1802 | } while (len != 0); |
| 1803 | |
| 1804 | return(xmlNanoFTPCloseConnection(ctxt)); |
| 1805 | } |
| 1806 | |
| 1807 | /** |
| 1808 | * xmlNanoFTPRead: |
| 1809 | * @ctx: the FTP context |
| 1810 | * @dest: a buffer |
| 1811 | * @len: the buffer length |
| 1812 | * |
| 1813 | * This function tries to read @len bytes from the existing FTP connection |
| 1814 | * and saves them in @dest. This is a blocking call. |
| 1815 | * |
| 1816 | * Returns the number of byte read. 0 is an indication of an end of connection. |
| 1817 | * -1 indicates a parameter error. |
| 1818 | */ |
| 1819 | int |
| 1820 | xmlNanoFTPRead(void *ctx, void *dest, int len) { |
| 1821 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 1822 | |
| 1823 | if (ctx == NULL) return(-1); |
| 1824 | if (ctxt->dataFd < 0) return(0); |
| 1825 | if (dest == NULL) return(-1); |
| 1826 | if (len <= 0) return(0); |
| 1827 | |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1828 | len = recv(ctxt->dataFd, dest, len, 0); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1829 | #ifdef DEBUG_FTP |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1830 | xmlGenericError(xmlGenericErrorContext, "Recvd %d bytes\n", len); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1831 | #endif |
| 1832 | if (len <= 0) { |
| 1833 | xmlNanoFTPCloseConnection(ctxt); |
| 1834 | } |
| 1835 | return(len); |
| 1836 | } |
| 1837 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1838 | /** |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1839 | * xmlNanoFTPOpen: |
| 1840 | * @URL: the URL to the resource |
| 1841 | * |
| 1842 | * Start to fetch the given ftp:// resource |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1843 | * |
| 1844 | * Returns an FTP context, or NULL |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1845 | */ |
| 1846 | |
Daniel Veillard | 0604743 | 2000-04-24 11:33:38 +0000 | [diff] [blame] | 1847 | void* |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1848 | xmlNanoFTPOpen(const char *URL) { |
| 1849 | xmlNanoFTPCtxtPtr ctxt; |
| 1850 | int sock; |
| 1851 | |
| 1852 | xmlNanoFTPInit(); |
| 1853 | if (URL == NULL) return(NULL); |
| 1854 | if (strncmp("ftp://", URL, 6)) return(NULL); |
| 1855 | |
Daniel Veillard | 4970326 | 2000-07-10 10:27:46 +0000 | [diff] [blame] | 1856 | ctxt = (xmlNanoFTPCtxtPtr) xmlNanoFTPNewCtxt(URL); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1857 | if (ctxt == NULL) return(NULL); |
| 1858 | if (xmlNanoFTPConnect(ctxt) < 0) { |
| 1859 | xmlNanoFTPFreeCtxt(ctxt); |
| 1860 | return(NULL); |
| 1861 | } |
| 1862 | sock = xmlNanoFTPGetSocket(ctxt, ctxt->path); |
| 1863 | if (sock < 0) { |
| 1864 | xmlNanoFTPFreeCtxt(ctxt); |
| 1865 | return(NULL); |
| 1866 | } |
| 1867 | return(ctxt); |
| 1868 | } |
| 1869 | |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1870 | /** |
| 1871 | * xmlNanoFTPClose: |
| 1872 | * @ctx: an FTP context |
| 1873 | * |
| 1874 | * Close the connection and both control and transport |
| 1875 | * |
| 1876 | * Returns -1 incase of error, 0 otherwise |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1877 | */ |
| 1878 | |
| 1879 | int |
| 1880 | xmlNanoFTPClose(void *ctx) { |
| 1881 | xmlNanoFTPCtxtPtr ctxt = (xmlNanoFTPCtxtPtr) ctx; |
| 1882 | |
| 1883 | if (ctxt == NULL) |
| 1884 | return(-1); |
| 1885 | |
| 1886 | if (ctxt->dataFd >= 0) { |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1887 | closesocket(ctxt->dataFd); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1888 | ctxt->dataFd = -1; |
| 1889 | } |
| 1890 | if (ctxt->controlFd >= 0) { |
Daniel Veillard | aeea04f | 2000-01-25 19:27:27 +0000 | [diff] [blame] | 1891 | xmlNanoFTPQuit(ctxt); |
Daniel Veillard | 2f971a2 | 2000-10-12 23:26:32 +0000 | [diff] [blame] | 1892 | closesocket(ctxt->controlFd); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1893 | ctxt->controlFd = -1; |
| 1894 | } |
| 1895 | xmlNanoFTPFreeCtxt(ctxt); |
| 1896 | return(0); |
| 1897 | } |
| 1898 | |
| 1899 | #ifdef STANDALONE |
| 1900 | /************************************************************************ |
| 1901 | * * |
| 1902 | * Basic test in Standalone mode * |
| 1903 | * * |
| 1904 | ************************************************************************/ |
| 1905 | void ftpList(void *userData, const char *filename, const char* attrib, |
| 1906 | const char *owner, const char *group, unsigned long size, int links, |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1907 | int year, const char *month, int day, int hour, int minute) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1908 | xmlGenericError(xmlGenericErrorContext, |
| 1909 | "%s %s %s %ld %s\n", attrib, owner, group, size, filename); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1910 | } |
| 1911 | void ftpData(void *userData, const char *data, int len) { |
| 1912 | if (userData == NULL) return; |
| 1913 | if (len <= 0) { |
| 1914 | fclose(userData); |
| 1915 | return; |
| 1916 | } |
| 1917 | fwrite(data, len, 1, userData); |
| 1918 | } |
| 1919 | |
| 1920 | int main(int argc, char **argv) { |
| 1921 | void *ctxt; |
| 1922 | FILE *output; |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1923 | char *tstfile = NULL; |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1924 | |
| 1925 | xmlNanoFTPInit(); |
| 1926 | if (argc > 1) { |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1927 | ctxt = xmlNanoFTPNewCtxt(argv[1]); |
| 1928 | if (xmlNanoFTPConnect(ctxt) < 0) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1929 | xmlGenericError(xmlGenericErrorContext, |
| 1930 | "Couldn't connect to %s\n", argv[1]); |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1931 | exit(1); |
| 1932 | } |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1933 | if (argc > 2) |
| 1934 | tstfile = argv[2]; |
| 1935 | } else |
| 1936 | ctxt = xmlNanoFTPConnectTo("localhost", 0); |
| 1937 | if (ctxt == NULL) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1938 | xmlGenericError(xmlGenericErrorContext, |
| 1939 | "Couldn't connect to localhost\n"); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1940 | exit(1); |
| 1941 | } |
Daniel Veillard | e41f2b7 | 2000-01-30 20:00:07 +0000 | [diff] [blame] | 1942 | xmlNanoFTPList(ctxt, ftpList, NULL, tstfile); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1943 | output = fopen("/tmp/tstdata", "w"); |
| 1944 | if (output != NULL) { |
| 1945 | if (xmlNanoFTPGet(ctxt, ftpData, (void *) output, tstfile) < 0) |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1946 | xmlGenericError(xmlGenericErrorContext, |
| 1947 | "Failed to get file\n"); |
Daniel Veillard | da07c34 | 2000-01-25 18:31:22 +0000 | [diff] [blame] | 1948 | |
| 1949 | } |
| 1950 | xmlNanoFTPClose(ctxt); |
| 1951 | xmlMemoryDump(); |
| 1952 | exit(0); |
| 1953 | } |
| 1954 | #endif /* STANDALONE */ |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 1955 | #else /* !LIBXML_FTP_ENABLED */ |
| 1956 | #ifdef STANDALONE |
| 1957 | #include <stdio.h> |
| 1958 | int main(int argc, char **argv) { |
Daniel Veillard | d6d7f7b | 2000-10-25 19:56:55 +0000 | [diff] [blame] | 1959 | xmlGenericError(xmlGenericErrorContext, |
| 1960 | "%s : FTP support not compiled in\n", argv[0]); |
Daniel Veillard | 361d845 | 2000-04-03 19:48:13 +0000 | [diff] [blame] | 1961 | return(0); |
| 1962 | } |
| 1963 | #endif /* STANDALONE */ |
| 1964 | #endif /* LIBXML_FTP_ENABLED */ |