common.c revision 97891
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 97891 2002-06-05 21:35:35Z 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 * Create a connection for an existing descriptor. 200 */ 201conn_t * 202_fetch_reopen(int sd) 203{ 204 conn_t *conn; 205 206 /* allocate and fill connection structure */ 207 if ((conn = calloc(1, sizeof *conn)) == NULL) 208 return (NULL); 209 conn->sd = sd; 210 return (conn); 211} 212 213 214/* 215 * Establish a TCP connection to the specified port on the specified host. 216 */ 217conn_t * 218_fetch_connect(const char *host, int port, int af, int verbose) 219{ 220 conn_t *conn; 221 char pbuf[10]; 222 struct addrinfo hints, *res, *res0; 223 int sd, err; 224 225 DEBUG(fprintf(stderr, "---> %s:%d\n", host, port)); 226 227 if (verbose) 228 _fetch_info("looking up %s", host); 229 230 /* look up host name and set up socket address structure */ 231 snprintf(pbuf, sizeof(pbuf), "%d", port); 232 memset(&hints, 0, sizeof(hints)); 233 hints.ai_family = af; 234 hints.ai_socktype = SOCK_STREAM; 235 hints.ai_protocol = 0; 236 if ((err = getaddrinfo(host, pbuf, &hints, &res0)) != 0) { 237 _netdb_seterr(err); 238 return (NULL); 239 } 240 241 if (verbose) 242 _fetch_info("connecting to %s:%d", host, port); 243 244 /* try to connect */ 245 for (sd = -1, res = res0; res; res = res->ai_next) { 246 if ((sd = socket(res->ai_family, res->ai_socktype, 247 res->ai_protocol)) == -1) 248 continue; 249 if (connect(sd, res->ai_addr, res->ai_addrlen) != -1) 250 break; 251 close(sd); 252 sd = -1; 253 } 254 freeaddrinfo(res0); 255 if (sd == -1) { 256 _fetch_syserr(); 257 return (NULL); 258 } 259 260 if ((conn = _fetch_reopen(sd)) == NULL) 261 close(sd); 262 return (conn); 263} 264 265 266/* 267 * Enable SSL on a connection. 268 */ 269int 270_fetch_ssl(conn_t *conn, int verbose) 271{ 272 273#ifdef WITH_SSL 274 /* Init the SSL library and context */ 275 if (!SSL_library_init()){ 276 fprintf(stderr, "SSL library init failed\n"); 277 return (-1); 278 } 279 280 SSL_load_error_strings(); 281 282 conn->ssl_meth = SSLv23_client_method(); 283 conn->ssl_ctx = SSL_CTX_new(conn->ssl_meth); 284 285 conn->ssl = SSL_new(conn->ssl_ctx); 286 if (conn->ssl == NULL){ 287 fprintf(stderr, "SSL context creation failed\n"); 288 return (-1); 289 } 290 SSL_set_fd(conn->ssl, conn->sd); 291 if (SSL_connect(conn->ssl) == -1){ 292 ERR_print_errors_fp(stderr); 293 return (-1); 294 } 295 296 if (verbose) { 297 X509_NAME *name; 298 char *str; 299 300 fprintf(stderr, "SSL connection established using %s\n", 301 SSL_get_cipher(conn->ssl)); 302 conn->ssl_cert = SSL_get_peer_certificate(conn->ssl); 303 name = X509_get_subject_name(conn->ssl_cert); 304 str = X509_NAME_oneline(name, 0, 0); 305 printf("Certificate subject: %s\n", str); 306 free(str); 307 name = X509_get_issuer_name(conn->ssl_cert); 308 str = X509_NAME_oneline(name, 0, 0); 309 printf("Certificate issuer: %s\n", str); 310 free(str); 311 } 312 313 return (0); 314#else 315 (void)conn; 316 (void)verbose; 317 fprintf(stderr, "SSL support disabled\n"); 318 return (-1); 319#endif 320} 321 322/* 323 * Read a character from a connection w/ timeout 324 */ 325ssize_t 326_fetch_read(conn_t *conn, char *buf, size_t len) 327{ 328 struct timeval now, timeout, wait; 329 fd_set readfds; 330 ssize_t rlen, total; 331 int r; 332 333 if (fetchTimeout) { 334 FD_ZERO(&readfds); 335 gettimeofday(&timeout, NULL); 336 timeout.tv_sec += fetchTimeout; 337 } 338 339 total = 0; 340 while (len > 0) { 341 while (fetchTimeout && !FD_ISSET(conn->sd, &readfds)) { 342 FD_SET(conn->sd, &readfds); 343 gettimeofday(&now, NULL); 344 wait.tv_sec = timeout.tv_sec - now.tv_sec; 345 wait.tv_usec = timeout.tv_usec - now.tv_usec; 346 if (wait.tv_usec < 0) { 347 wait.tv_usec += 1000000; 348 wait.tv_sec--; 349 } 350 if (wait.tv_sec < 0) 351 return (rlen); 352 errno = 0; 353 r = select(conn->sd + 1, &readfds, NULL, NULL, &wait); 354 if (r == -1) { 355 if (errno == EINTR && fetchRestartCalls) 356 continue; 357 return (-1); 358 } 359 } 360#ifdef WITH_SSL 361 if (conn->ssl != NULL) 362 rlen = SSL_read(conn->ssl, buf, len); 363 else 364#endif 365 rlen = read(conn->sd, buf, len); 366 if (rlen == 0) 367 break; 368 if (rlen < 0) { 369 if (errno == EINTR && fetchRestartCalls) 370 continue; 371 return (-1); 372 } 373 len -= rlen; 374 buf += rlen; 375 total += rlen; 376 } 377 return (total); 378} 379 380/* 381 * Read a line of text from a connection w/ timeout 382 */ 383#define MIN_BUF_SIZE 1024 384 385int 386_fetch_getln(conn_t *conn) 387{ 388 char *tmp; 389 size_t tmpsize; 390 char c; 391 392 if (conn->buf == NULL) { 393 if ((conn->buf = malloc(MIN_BUF_SIZE)) == NULL) { 394 errno = ENOMEM; 395 return (-1); 396 } 397 conn->bufsize = MIN_BUF_SIZE; 398 } 399 400 conn->buf[0] = '\0'; 401 conn->buflen = 0; 402 403 do { 404 if (_fetch_read(conn, &c, 1) == -1) 405 return (-1); 406 conn->buf[conn->buflen++] = c; 407 if (conn->buflen == conn->bufsize) { 408 tmp = conn->buf; 409 tmpsize = conn->bufsize * 2 + 1; 410 if ((tmp = realloc(tmp, tmpsize)) == NULL) { 411 errno = ENOMEM; 412 return (-1); 413 } 414 conn->buf = tmp; 415 conn->bufsize = tmpsize; 416 } 417 } while (c != '\n'); 418 419 conn->buf[conn->buflen] = '\0'; 420 DEBUG(fprintf(stderr, "<<< %s", conn->buf)); 421 return (0); 422} 423 424 425/* 426 * Write to a connection w/ timeout 427 */ 428ssize_t 429_fetch_write(conn_t *conn, const char *buf, size_t len) 430{ 431 struct timeval now, timeout, wait; 432 fd_set writefds; 433 ssize_t wlen, total; 434 int r; 435 436 if (fetchTimeout) { 437 FD_ZERO(&writefds); 438 gettimeofday(&timeout, NULL); 439 timeout.tv_sec += fetchTimeout; 440 } 441 442 while (len > 0) { 443 while (fetchTimeout && !FD_ISSET(conn->sd, &writefds)) { 444 FD_SET(conn->sd, &writefds); 445 gettimeofday(&now, NULL); 446 wait.tv_sec = timeout.tv_sec - now.tv_sec; 447 wait.tv_usec = timeout.tv_usec - now.tv_usec; 448 if (wait.tv_usec < 0) { 449 wait.tv_usec += 1000000; 450 wait.tv_sec--; 451 } 452 if (wait.tv_sec < 0) { 453 errno = ETIMEDOUT; 454 return (-1); 455 } 456 errno = 0; 457 r = select(conn->sd + 1, NULL, &writefds, NULL, &wait); 458 if (r == -1) { 459 if (errno == EINTR && fetchRestartCalls) 460 continue; 461 return (-1); 462 } 463 } 464 errno = 0; 465#ifdef WITH_SSL 466 if (conn->ssl != NULL) 467 wlen = SSL_write(conn->ssl, buf, len); 468 else 469#endif 470 wlen = write(conn->sd, buf, len); 471 if (wlen == 0) 472 /* we consider a short write a failure */ 473 return (-1); 474 if (wlen < 0) { 475 if (errno == EINTR && fetchRestartCalls) 476 continue; 477 return (-1); 478 } 479 len -= wlen; 480 buf += wlen; 481 total += wlen; 482 } 483 return (total); 484} 485 486/* 487 * Write a line of text to a connection w/ timeout 488 */ 489int 490_fetch_putln(conn_t *conn, const char *str, size_t len) 491{ 492 if (_fetch_write(conn, str, len) == -1 || 493 _fetch_write(conn, ENDL, sizeof ENDL) == -1) 494 return (-1); 495 return (0); 496} 497 498 499/* 500 * Close connection 501 */ 502int 503_fetch_close(conn_t *conn) 504{ 505 int ret; 506 507 ret = close(conn->sd); 508 free(conn); 509 return (ret); 510} 511 512 513/*** Directory-related utility functions *************************************/ 514 515int 516_fetch_add_entry(struct url_ent **p, int *size, int *len, 517 const char *name, struct url_stat *us) 518{ 519 struct url_ent *tmp; 520 521 if (*p == NULL) { 522 *size = 0; 523 *len = 0; 524 } 525 526 if (*len >= *size - 1) { 527 tmp = realloc(*p, (*size * 2 + 1) * sizeof **p); 528 if (tmp == NULL) { 529 errno = ENOMEM; 530 _fetch_syserr(); 531 return (-1); 532 } 533 *size = (*size * 2 + 1); 534 *p = tmp; 535 } 536 537 tmp = *p + *len; 538 snprintf(tmp->name, PATH_MAX, "%s", name); 539 bcopy(us, &tmp->stat, sizeof *us); 540 541 (*len)++; 542 (++tmp)->name[0] = 0; 543 544 return (0); 545} 546