common.c (310059) | common.c (315902) |
---|---|
1/*- 2 * Copyright (c) 1998-2016 Dag-Erling Sm��rgrav 3 * Copyright (c) 2013 Michael Gmelin <freebsd@grem.de> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: --- 14 unchanged lines hidden (view full) --- 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30#include <sys/cdefs.h> | 1/*- 2 * Copyright (c) 1998-2016 Dag-Erling Sm��rgrav 3 * Copyright (c) 2013 Michael Gmelin <freebsd@grem.de> 4 * All rights reserved. 5 * 6 * Redistribution and use in source and binary forms, with or without 7 * modification, are permitted provided that the following conditions 8 * are met: --- 14 unchanged lines hidden (view full) --- 23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 */ 29 30#include <sys/cdefs.h> |
31__FBSDID("$FreeBSD: stable/11/lib/libfetch/common.c 310059 2016-12-14 14:20:32Z des $"); | 31__FBSDID("$FreeBSD: stable/11/lib/libfetch/common.c 315902 2017-03-24 14:19:52Z des $"); |
32 33#include <sys/param.h> 34#include <sys/socket.h> 35#include <sys/time.h> 36#include <sys/uio.h> 37 38#include <netinet/in.h> 39 --- 108 unchanged lines hidden (view full) --- 148 break; 149 case ETIMEDOUT: 150 fetchLastErrCode = FETCH_TIMEOUT; 151 break; 152 case ECONNREFUSED: 153 case EHOSTDOWN: 154 fetchLastErrCode = FETCH_DOWN; 155 break; | 32 33#include <sys/param.h> 34#include <sys/socket.h> 35#include <sys/time.h> 36#include <sys/uio.h> 37 38#include <netinet/in.h> 39 --- 108 unchanged lines hidden (view full) --- 148 break; 149 case ETIMEDOUT: 150 fetchLastErrCode = FETCH_TIMEOUT; 151 break; 152 case ECONNREFUSED: 153 case EHOSTDOWN: 154 fetchLastErrCode = FETCH_DOWN; 155 break; |
156default: | 156 default: |
157 fetchLastErrCode = FETCH_UNKNOWN; 158 } 159 snprintf(fetchLastErrString, MAXERRSTRING, "%s", strerror(errno)); 160} 161 162 163/* 164 * Emit status message --- 78 unchanged lines hidden (view full) --- 243/* 244 * Resolve an address 245 */ 246struct addrinfo * 247fetch_resolve(const char *addr, int port, int af) 248{ 249 char hbuf[256], sbuf[8]; 250 struct addrinfo hints, *res; | 157 fetchLastErrCode = FETCH_UNKNOWN; 158 } 159 snprintf(fetchLastErrString, MAXERRSTRING, "%s", strerror(errno)); 160} 161 162 163/* 164 * Emit status message --- 78 unchanged lines hidden (view full) --- 243/* 244 * Resolve an address 245 */ 246struct addrinfo * 247fetch_resolve(const char *addr, int port, int af) 248{ 249 char hbuf[256], sbuf[8]; 250 struct addrinfo hints, *res; |
251 const char *sep, *host, *service; | 251 const char *hb, *he, *sep; 252 const char *host, *service; |
252 int err, len; 253 | 253 int err, len; 254 |
254 /* split address if necessary */ 255 err = EAI_SYSTEM; 256 if ((sep = strchr(addr, ':')) != NULL) { | 255 /* first, check for a bracketed IPv6 address */ 256 if (*addr == '[') { 257 hb = addr + 1; 258 if ((sep = strchr(hb, ']')) == NULL) { 259 errno = EINVAL; 260 goto syserr; 261 } 262 he = sep++; 263 } else { 264 hb = addr; 265 sep = strchrnul(hb, ':'); 266 he = sep; 267 } 268 269 /* see if we need to copy the host name */ 270 if (*he != '\0') { |
257 len = snprintf(hbuf, sizeof(hbuf), | 271 len = snprintf(hbuf, sizeof(hbuf), |
258 "%.*s", (int)(sep - addr), addr); | 272 "%.*s", (int)(he - hb), hb); |
259 if (len < 0) | 273 if (len < 0) |
260 return (NULL); | 274 goto syserr; |
261 if (len >= (int)sizeof(hbuf)) { 262 errno = ENAMETOOLONG; | 275 if (len >= (int)sizeof(hbuf)) { 276 errno = ENAMETOOLONG; |
263 fetch_syserr(); 264 return (NULL); | 277 goto syserr; |
265 } 266 host = hbuf; | 278 } 279 host = hbuf; |
267 service = sep + 1; 268 } else if (port != 0) { | 280 } else { 281 host = hb; 282 } 283 284 /* was it followed by a service name? */ 285 if (*sep == '\0' && port != 0) { |
269 if (port < 1 || port > 65535) { 270 errno = EINVAL; | 286 if (port < 1 || port > 65535) { 287 errno = EINVAL; |
271 fetch_syserr(); 272 return (NULL); | 288 goto syserr; |
273 } | 289 } |
274 if (snprintf(sbuf, sizeof(sbuf), "%d", port) < 0) { 275 fetch_syserr(); 276 return (NULL); 277 } 278 host = addr; | 290 if (snprintf(sbuf, sizeof(sbuf), "%d", port) < 0) 291 goto syserr; |
279 service = sbuf; | 292 service = sbuf; |
293 } else if (*sep != '\0') { 294 service = sep; |
|
280 } else { | 295 } else { |
281 host = addr; | |
282 service = NULL; 283 } 284 285 /* resolve */ 286 memset(&hints, 0, sizeof(hints)); 287 hints.ai_family = af; 288 hints.ai_socktype = SOCK_STREAM; 289 hints.ai_flags = AI_ADDRCONFIG; 290 if ((err = getaddrinfo(host, service, &hints, &res)) != 0) { 291 netdb_seterr(err); 292 return (NULL); 293 } 294 return (res); | 296 service = NULL; 297 } 298 299 /* resolve */ 300 memset(&hints, 0, sizeof(hints)); 301 hints.ai_family = af; 302 hints.ai_socktype = SOCK_STREAM; 303 hints.ai_flags = AI_ADDRCONFIG; 304 if ((err = getaddrinfo(host, service, &hints, &res)) != 0) { 305 netdb_seterr(err); 306 return (NULL); 307 } 308 return (res); |
309syserr: 310 fetch_syserr(); 311 return (NULL); |
|
295} 296 297 298 299/* 300 * Bind a socket to a specific local address 301 */ 302int --- 63 unchanged lines hidden (view full) --- 366 if ((err = connect(sd, sai->ai_addr, sai->ai_addrlen)) == 0) 367 break; 368 /* clean up before next attempt */ 369 close(sd); 370 sd = -1; 371 } 372 if (err != 0) { 373 if (verbose) | 312} 313 314 315 316/* 317 * Bind a socket to a specific local address 318 */ 319int --- 63 unchanged lines hidden (view full) --- 383 if ((err = connect(sd, sai->ai_addr, sai->ai_addrlen)) == 0) 384 break; 385 /* clean up before next attempt */ 386 close(sd); 387 sd = -1; 388 } 389 if (err != 0) { 390 if (verbose) |
374 fetch_info("failed to connect to %s:%s", host, port); | 391 fetch_info("failed to connect to %s:%d", host, port); |
375 goto syserr; 376 } 377 378 if ((conn = fetch_reopen(sd)) == NULL) 379 goto syserr; 380 if (cais != NULL) 381 freeaddrinfo(cais); 382 if (sais != NULL) --- 951 unchanged lines hidden (view full) --- 1334{ 1335 static char word[1024]; 1336 1337 if (fscanf(f, " %1023s ", word) != 1) 1338 return (NULL); 1339 return (word); 1340} 1341 | 392 goto syserr; 393 } 394 395 if ((conn = fetch_reopen(sd)) == NULL) 396 goto syserr; 397 if (cais != NULL) 398 freeaddrinfo(cais); 399 if (sais != NULL) --- 951 unchanged lines hidden (view full) --- 1351{ 1352 static char word[1024]; 1353 1354 if (fscanf(f, " %1023s ", word) != 1) 1355 return (NULL); 1356 return (word); 1357} 1358 |
1342/* 1343 * Get authentication data for a URL from .netrc 1344 */ 1345int 1346fetch_netrc_auth(struct url *url) | 1359static int 1360fetch_netrc_open(void) |
1347{ | 1361{ |
1362 const char *p; |
|
1348 char fn[PATH_MAX]; | 1363 char fn[PATH_MAX]; |
1349 const char *word; 1350 char *p; 1351 FILE *f; | |
1352 1353 if ((p = getenv("NETRC")) != NULL) { 1354 if (snprintf(fn, sizeof(fn), "%s", p) >= (int)sizeof(fn)) { 1355 fetch_info("$NETRC specifies a file name " 1356 "longer than PATH_MAX"); 1357 return (-1); 1358 } 1359 } else { 1360 if ((p = getenv("HOME")) != NULL) { 1361 struct passwd *pwd; 1362 1363 if ((pwd = getpwuid(getuid())) == NULL || 1364 (p = pwd->pw_dir) == NULL) 1365 return (-1); 1366 } 1367 if (snprintf(fn, sizeof(fn), "%s/.netrc", p) >= (int)sizeof(fn)) 1368 return (-1); 1369 } 1370 | 1364 1365 if ((p = getenv("NETRC")) != NULL) { 1366 if (snprintf(fn, sizeof(fn), "%s", p) >= (int)sizeof(fn)) { 1367 fetch_info("$NETRC specifies a file name " 1368 "longer than PATH_MAX"); 1369 return (-1); 1370 } 1371 } else { 1372 if ((p = getenv("HOME")) != NULL) { 1373 struct passwd *pwd; 1374 1375 if ((pwd = getpwuid(getuid())) == NULL || 1376 (p = pwd->pw_dir) == NULL) 1377 return (-1); 1378 } 1379 if (snprintf(fn, sizeof(fn), "%s/.netrc", p) >= (int)sizeof(fn)) 1380 return (-1); 1381 } 1382 |
1371 if ((f = fopen(fn, "r")) == NULL) | 1383 return (open(fn, O_RDONLY)); 1384} 1385 1386/* 1387 * Get authentication data for a URL from .netrc 1388 */ 1389int 1390fetch_netrc_auth(struct url *url) 1391{ 1392 const char *word; 1393 FILE *f; 1394 1395 if (url->netrcfd == -2) 1396 url->netrcfd = fetch_netrc_open(); 1397 if (url->netrcfd < 0) |
1372 return (-1); | 1398 return (-1); |
1399 if ((f = fdopen(url->netrcfd, "r")) == NULL) 1400 return (-1); 1401 rewind(f); |
|
1373 while ((word = fetch_read_word(f)) != NULL) { 1374 if (strcmp(word, "default") == 0) { 1375 DEBUG(fetch_info("Using default .netrc settings")); 1376 break; 1377 } 1378 if (strcmp(word, "machine") == 0 && 1379 (word = fetch_read_word(f)) != NULL && 1380 strcasecmp(word, url->host) == 0) { --- 85 unchanged lines hidden --- | 1402 while ((word = fetch_read_word(f)) != NULL) { 1403 if (strcmp(word, "default") == 0) { 1404 DEBUG(fetch_info("Using default .netrc settings")); 1405 break; 1406 } 1407 if (strcmp(word, "machine") == 0 && 1408 (word = fetch_read_word(f)) != NULL && 1409 strcasecmp(word, url->host) == 0) { --- 85 unchanged lines hidden --- |