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