common.c revision 108579
1/*- 2 * Copyright (c) 1998 Dag-Erling Co�dan Sm�rgrav 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 8 * 1. Redistributions of source code must retain the above copyright 9 * notice, this list of conditions and the following disclaimer 10 * in this position and unchanged. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 3. The name of the author may not be used to endorse or promote products 15 * derived from this software without specific prior written permission 16 * 17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 27 */ 28 29#include <sys/cdefs.h> 30__FBSDID("$FreeBSD: head/lib/libfetch/common.c 108579 2003-01-03 02:45:10Z des $"); 31 32#include <sys/param.h> 33#include <sys/socket.h> 34#include <sys/time.h> 35#include <sys/uio.h> 36#include <netinet/in.h> 37 38#include <errno.h> 39#include <netdb.h> 40#include <stdarg.h> 41#include <stdlib.h> 42#include <stdio.h> 43#include <string.h> 44#include <unistd.h> 45 46#include "fetch.h" 47#include "common.h" 48 49 50/*** Local data **************************************************************/ 51 52/* 53 * Error messages for resolver errors 54 */ 55static struct fetcherr _netdb_errlist[] = { 56 { EAI_NODATA, FETCH_RESOLV, "Host not found" }, 57 { EAI_AGAIN, FETCH_TEMP, "Transient resolver failure" }, 58 { EAI_FAIL, FETCH_RESOLV, "Non-recoverable resolver failure" }, 59 { EAI_NONAME, FETCH_RESOLV, "No address record" }, 60 { -1, FETCH_UNKNOWN, "Unknown resolver error" } 61}; 62 63/* End-of-Line */ 64static const char ENDL[2] = "\r\n"; 65 66 67/*** Error-reporting functions ***********************************************/ 68 69/* 70 * Map error code to string 71 */ 72static struct fetcherr * 73_fetch_finderr(struct fetcherr *p, int e) 74{ 75 while (p->num != -1 && p->num != e) 76 p++; 77 return (p); 78} 79 80/* 81 * Set error code 82 */ 83void 84_fetch_seterr(struct fetcherr *p, int e) 85{ 86 p = _fetch_finderr(p, e); 87 fetchLastErrCode = p->cat; 88 snprintf(fetchLastErrString, MAXERRSTRING, "%s", p->string); 89} 90 91/* 92 * Set error code according to errno 93 */ 94void 95_fetch_syserr(void) 96{ 97 switch (errno) { 98 case 0: 99 fetchLastErrCode = FETCH_OK; 100 break; 101 case EPERM: 102 case EACCES: 103 case EROFS: 104 case EAUTH: 105 case ENEEDAUTH: 106 fetchLastErrCode = FETCH_AUTH; 107 break; 108 case ENOENT: 109 case EISDIR: /* XXX */ 110 fetchLastErrCode = FETCH_UNAVAIL; 111 break; 112 case ENOMEM: 113 fetchLastErrCode = FETCH_MEMORY; 114 break; 115 case EBUSY: 116 case EAGAIN: 117 fetchLastErrCode = FETCH_TEMP; 118 break; 119 case EEXIST: 120 fetchLastErrCode = FETCH_EXISTS; 121 break; 122 case ENOSPC: 123 fetchLastErrCode = FETCH_FULL; 124 break; 125 case EADDRINUSE: 126 case EADDRNOTAVAIL: 127 case ENETDOWN: 128 case ENETUNREACH: 129 case ENETRESET: 130 case EHOSTUNREACH: 131 fetchLastErrCode = FETCH_NETWORK; 132 break; 133 case ECONNABORTED: 134 case ECONNRESET: 135 fetchLastErrCode = FETCH_ABORT; 136 break; 137 case ETIMEDOUT: 138 fetchLastErrCode = FETCH_TIMEOUT; 139 break; 140 case ECONNREFUSED: 141 case EHOSTDOWN: 142 fetchLastErrCode = FETCH_DOWN; 143 break; 144default: 145 fetchLastErrCode = FETCH_UNKNOWN; 146 } 147 snprintf(fetchLastErrString, MAXERRSTRING, "%s", strerror(errno)); 148} 149 150 151/* 152 * Emit status message 153 */ 154void 155_fetch_info(const char *fmt, ...) 156{ 157 va_list ap; 158 159 va_start(ap, fmt); 160 vfprintf(stderr, fmt, ap); 161 va_end(ap); 162 fputc('\n', stderr); 163} 164 165 166/*** Network-related utility functions ***************************************/ 167 168/* 169 * Return the default port for a scheme 170 */ 171int 172_fetch_default_port(const char *scheme) 173{ 174 struct servent *se; 175 176 if ((se = getservbyname(scheme, "tcp")) != NULL) 177 return (ntohs(se->s_port)); 178 if (strcasecmp(scheme, SCHEME_FTP) == 0) 179 return (FTP_DEFAULT_PORT); 180 if (strcasecmp(scheme, SCHEME_HTTP) == 0) 181 return (HTTP_DEFAULT_PORT); 182 return (0); 183} 184 185/* 186 * Return the default proxy port for a scheme 187 */ 188int 189_fetch_default_proxy_port(const char *scheme) 190{ 191 if (strcasecmp(scheme, SCHEME_FTP) == 0) 192 return (FTP_DEFAULT_PROXY_PORT); 193 if (strcasecmp(scheme, SCHEME_HTTP) == 0) 194 return (HTTP_DEFAULT_PROXY_PORT); 195 return (0); 196} 197 198 199/* 200 * Create a connection for an existing descriptor. 201 */ 202conn_t * 203_fetch_reopen(int sd) 204{ 205 conn_t *conn; 206 207 /* allocate and fill connection structure */ 208 if ((conn = calloc(1, sizeof *conn)) == NULL) 209 return (NULL); 210 conn->sd = sd; 211 ++conn->ref; 212 return (conn); 213} 214 215 216/* 217 * Bump a connection's reference count. 218 */ 219conn_t * 220_fetch_ref(conn_t *conn) 221{ 222 223 ++conn->ref; 224 return (conn); 225} 226 227 228/* 229 * Establish a TCP connection to the specified port on the specified host. 230 */ 231conn_t * 232_fetch_connect(const char *host, int port, int af, int verbose) 233{ 234 conn_t *conn; 235 char pbuf[10]; 236 struct addrinfo hints, *res, *res0; 237 int sd, err; 238 239 DEBUG(fprintf(stderr, "---> %s:%d\n", host, port)); 240 241 if (verbose) 242 _fetch_info("looking up %s", host); 243 244 /* look up host name and set up socket address structure */ 245 snprintf(pbuf, sizeof(pbuf), "%d", port); 246 memset(&hints, 0, sizeof(hints)); 247 hints.ai_family = af; 248 hints.ai_socktype = SOCK_STREAM; 249 hints.ai_protocol = 0; 250 if ((err = getaddrinfo(host, pbuf, &hints, &res0)) != 0) { 251 _netdb_seterr(err); 252 return (NULL); 253 } 254 255 if (verbose) 256 _fetch_info("connecting to %s:%d", host, port); 257 258 /* try to connect */ 259 for (sd = -1, res = res0; res; res = res->ai_next) { 260 if ((sd = socket(res->ai_family, res->ai_socktype, 261 res->ai_protocol)) == -1) 262 continue; 263 if (connect(sd, res->ai_addr, res->ai_addrlen) != -1) 264 break; 265 close(sd); 266 sd = -1; 267 } 268 freeaddrinfo(res0); 269 if (sd == -1) { 270 _fetch_syserr(); 271 return (NULL); 272 } 273 274 if ((conn = _fetch_reopen(sd)) == NULL) { 275 _fetch_syserr(); 276 close(sd); 277 } 278 return (conn); 279} 280 281 282/* 283 * Enable SSL on a connection. 284 */ 285int 286_fetch_ssl(conn_t *conn, int verbose) 287{ 288 289#ifdef WITH_SSL 290 /* Init the SSL library and context */ 291 if (!SSL_library_init()){ 292 fprintf(stderr, "SSL library init failed\n"); 293 return (-1); 294 } 295 296 SSL_load_error_strings(); 297 298 conn->ssl_meth = SSLv23_client_method(); 299 conn->ssl_ctx = SSL_CTX_new(conn->ssl_meth); 300 SSL_CTX_set_mode(conn->ssl_ctx, SSL_MODE_AUTO_RETRY); 301 302 conn->ssl = SSL_new(conn->ssl_ctx); 303 if (conn->ssl == NULL){ 304 fprintf(stderr, "SSL context creation failed\n"); 305 return (-1); 306 } 307 SSL_set_fd(conn->ssl, conn->sd); 308 if (SSL_connect(conn->ssl) == -1){ 309 ERR_print_errors_fp(stderr); 310 return (-1); 311 } 312 313 if (verbose) { 314 X509_NAME *name; 315 char *str; 316 317 fprintf(stderr, "SSL connection established using %s\n", 318 SSL_get_cipher(conn->ssl)); 319 conn->ssl_cert = SSL_get_peer_certificate(conn->ssl); 320 name = X509_get_subject_name(conn->ssl_cert); 321 str = X509_NAME_oneline(name, 0, 0); 322 printf("Certificate subject: %s\n", str); 323 free(str); 324 name = X509_get_issuer_name(conn->ssl_cert); 325 str = X509_NAME_oneline(name, 0, 0); 326 printf("Certificate issuer: %s\n", str); 327 free(str); 328 } 329 330 return (0); 331#else 332 (void)conn; 333 (void)verbose; 334 fprintf(stderr, "SSL support disabled\n"); 335 return (-1); 336#endif 337} 338 339 340/* 341 * Read a character from a connection w/ timeout 342 */ 343ssize_t 344_fetch_read(conn_t *conn, char *buf, size_t len) 345{ 346 struct timeval now, timeout, wait; 347 fd_set readfds; 348 ssize_t rlen, total; 349 int r; 350 351 if (fetchTimeout) { 352 FD_ZERO(&readfds); 353 gettimeofday(&timeout, NULL); 354 timeout.tv_sec += fetchTimeout; 355 } 356 357 total = 0; 358 while (len > 0) { 359 while (fetchTimeout && !FD_ISSET(conn->sd, &readfds)) { 360 FD_SET(conn->sd, &readfds); 361 gettimeofday(&now, NULL); 362 wait.tv_sec = timeout.tv_sec - now.tv_sec; 363 wait.tv_usec = timeout.tv_usec - now.tv_usec; 364 if (wait.tv_usec < 0) { 365 wait.tv_usec += 1000000; 366 wait.tv_sec--; 367 } 368 if (wait.tv_sec < 0) { 369 errno = ETIMEDOUT; 370 _fetch_syserr(); 371 return (-1); 372 } 373 errno = 0; 374 r = select(conn->sd + 1, &readfds, NULL, NULL, &wait); 375 if (r == -1) { 376 if (errno == EINTR && fetchRestartCalls) 377 continue; 378 _fetch_syserr(); 379 return (-1); 380 } 381 } 382#ifdef WITH_SSL 383 if (conn->ssl != NULL) 384 rlen = SSL_read(conn->ssl, buf, len); 385 else 386#endif 387 rlen = read(conn->sd, buf, len); 388 if (rlen == 0) 389 break; 390 if (rlen < 0) { 391 if (errno == EINTR && fetchRestartCalls) 392 continue; 393 return (-1); 394 } 395 len -= rlen; 396 buf += rlen; 397 total += rlen; 398 } 399 return (total); 400} 401 402 403/* 404 * Read a line of text from a connection w/ timeout 405 */ 406#define MIN_BUF_SIZE 1024 407 408int 409_fetch_getln(conn_t *conn) 410{ 411 char *tmp; 412 size_t tmpsize; 413 ssize_t len; 414 char c; 415 416 if (conn->buf == NULL) { 417 if ((conn->buf = malloc(MIN_BUF_SIZE)) == NULL) { 418 errno = ENOMEM; 419 return (-1); 420 } 421 conn->bufsize = MIN_BUF_SIZE; 422 } 423 424 conn->buf[0] = '\0'; 425 conn->buflen = 0; 426 427 do { 428 len = _fetch_read(conn, &c, 1); 429 if (len == -1) 430 return (-1); 431 if (len == 0) 432 break; 433 conn->buf[conn->buflen++] = c; 434 if (conn->buflen == conn->bufsize) { 435 tmp = conn->buf; 436 tmpsize = conn->bufsize * 2 + 1; 437 if ((tmp = realloc(tmp, tmpsize)) == NULL) { 438 errno = ENOMEM; 439 return (-1); 440 } 441 conn->buf = tmp; 442 conn->bufsize = tmpsize; 443 } 444 } while (c != '\n'); 445 446 conn->buf[conn->buflen] = '\0'; 447 DEBUG(fprintf(stderr, "<<< %s", conn->buf)); 448 return (0); 449} 450 451 452/* 453 * Write to a connection w/ timeout 454 */ 455ssize_t 456_fetch_write(conn_t *conn, const char *buf, size_t len) 457{ 458 struct iovec iov; 459 460 iov.iov_base = __DECONST(char *, buf); 461 iov.iov_len = len; 462 return _fetch_writev(conn, &iov, 1); 463} 464 465/* 466 * Write a vector to a connection w/ timeout 467 * Note: can modify the iovec. 468 */ 469ssize_t 470_fetch_writev(conn_t *conn, struct iovec *iov, int iovcnt) 471{ 472 struct timeval now, timeout, wait; 473 fd_set writefds; 474 ssize_t wlen, total; 475 int r; 476 477 if (fetchTimeout) { 478 FD_ZERO(&writefds); 479 gettimeofday(&timeout, NULL); 480 timeout.tv_sec += fetchTimeout; 481 } 482 483 total = 0; 484 while (iovcnt > 0) { 485 while (fetchTimeout && !FD_ISSET(conn->sd, &writefds)) { 486 FD_SET(conn->sd, &writefds); 487 gettimeofday(&now, NULL); 488 wait.tv_sec = timeout.tv_sec - now.tv_sec; 489 wait.tv_usec = timeout.tv_usec - now.tv_usec; 490 if (wait.tv_usec < 0) { 491 wait.tv_usec += 1000000; 492 wait.tv_sec--; 493 } 494 if (wait.tv_sec < 0) { 495 errno = ETIMEDOUT; 496 _fetch_syserr(); 497 return (-1); 498 } 499 errno = 0; 500 r = select(conn->sd + 1, NULL, &writefds, NULL, &wait); 501 if (r == -1) { 502 if (errno == EINTR && fetchRestartCalls) 503 continue; 504 return (-1); 505 } 506 } 507 errno = 0; 508#ifdef WITH_SSL 509 if (conn->ssl != NULL) 510 wlen = SSL_write(conn->ssl, 511 iov->iov_base, iov->iov_len); 512 else 513#endif 514 wlen = writev(conn->sd, iov, iovcnt); 515 if (wlen == 0) { 516 /* we consider a short write a failure */ 517 errno = EPIPE; 518 _fetch_syserr(); 519 return (-1); 520 } 521 if (wlen < 0) { 522 if (errno == EINTR && fetchRestartCalls) 523 continue; 524 return (-1); 525 } 526 total += wlen; 527 while (iovcnt > 0 && wlen >= (ssize_t)iov->iov_len) { 528 wlen -= iov->iov_len; 529 iov++; 530 iovcnt--; 531 } 532 if (iovcnt > 0) { 533 iov->iov_len -= wlen; 534 iov->iov_base = __DECONST(char *, iov->iov_base) + wlen; 535 } 536 } 537 return (total); 538} 539 540 541/* 542 * Write a line of text to a connection w/ timeout 543 */ 544int 545_fetch_putln(conn_t *conn, const char *str, size_t len) 546{ 547 struct iovec iov[2]; 548 int ret; 549 550 DEBUG(fprintf(stderr, ">>> %s\n", str)); 551 iov[0].iov_base = __DECONST(char *, str); 552 iov[0].iov_len = len; 553 iov[1].iov_base = __DECONST(char *, ENDL); 554 iov[1].iov_len = sizeof ENDL; 555 if (len == 0) 556 ret = _fetch_writev(conn, &iov[1], 1); 557 else 558 ret = _fetch_writev(conn, iov, 2); 559 if (ret == -1) 560 return (-1); 561 return (0); 562} 563 564 565/* 566 * Close connection 567 */ 568int 569_fetch_close(conn_t *conn) 570{ 571 int ret; 572 573 if (--conn->ref > 0) 574 return (0); 575 ret = close(conn->sd); 576 free(conn); 577 return (ret); 578} 579 580 581/*** Directory-related utility functions *************************************/ 582 583int 584_fetch_add_entry(struct url_ent **p, int *size, int *len, 585 const char *name, struct url_stat *us) 586{ 587 struct url_ent *tmp; 588 589 if (*p == NULL) { 590 *size = 0; 591 *len = 0; 592 } 593 594 if (*len >= *size - 1) { 595 tmp = realloc(*p, (*size * 2 + 1) * sizeof **p); 596 if (tmp == NULL) { 597 errno = ENOMEM; 598 _fetch_syserr(); 599 return (-1); 600 } 601 *size = (*size * 2 + 1); 602 *p = tmp; 603 } 604 605 tmp = *p + *len; 606 snprintf(tmp->name, PATH_MAX, "%s", name); 607 bcopy(us, &tmp->stat, sizeof *us); 608 609 (*len)++; 610 (++tmp)->name[0] = 0; 611 612 return (0); 613} 614