00001
00005 #include "system.h"
00006
00007 #include <netinet/in.h>
00008
00009 #include <rpmmacro.h>
00010 #include <rpmmessages.h>
00011 #include <rpmio_internal.h>
00012
00013 #include "debug.h"
00014
00015
00016
00017
00018 #ifndef IPPORT_FTP
00019 #define IPPORT_FTP 21
00020 #endif
00021 #ifndef IPPORT_HTTP
00022 #define IPPORT_HTTP 80
00023 #endif
00024 #ifndef IPPORT_HTTPS
00025 #define IPPORT_HTTPS 443
00026 #endif
00027 #ifndef IPPORT_PGPKEYSERVER
00028 #define IPPORT_PGPKEYSERVER 11371
00029 #endif
00030
00033
00034 int _url_iobuf_size = RPMURL_IOBUF_SIZE;
00035
00038
00039 int _url_debug = 0;
00040
00041 #define URLDBG(_f, _m, _x) if ((_url_debug | (_f)) & (_m)) fprintf _x
00042
00043 #define URLDBGIO(_f, _x) URLDBG((_f), RPMURL_DEBUG_IO, _x)
00044 #define URLDBGREFS(_f, _x) URLDBG((_f), RPMURL_DEBUG_REFS, _x)
00045
00048
00049
00050 urlinfo *_url_cache = NULL;
00051
00054
00055 int _url_count = 0;
00056
00062 static inline void *
00063 _free( const void * p)
00064 {
00065 if (p != NULL) free((void *)p);
00066 return NULL;
00067 }
00068
00069 urlinfo XurlLink(urlinfo u, const char *msg, const char *file, unsigned line)
00070 {
00071 URLSANE(u);
00072 u->nrefs++;
00073
00074 URLDBGREFS(0, (stderr, "--> url %p ++ %d %s at %s:%u\n", u, u->nrefs, msg, file, line));
00075
00076 return u;
00077 }
00078
00079 urlinfo XurlNew(const char *msg, const char *file, unsigned line)
00080 {
00081 urlinfo u;
00082 if ((u = xmalloc(sizeof(*u))) == NULL)
00083 return NULL;
00084 memset(u, 0, sizeof(*u));
00085 u->proxyp = -1;
00086 u->port = -1;
00087 u->urltype = URL_IS_UNKNOWN;
00088 u->ctrl = NULL;
00089 u->data = NULL;
00090 u->bufAlloced = 0;
00091 u->buf = NULL;
00092 u->allow = RPMURL_SERVER_HASRANGE;
00093 u->httpVersion = 0;
00094 u->nrefs = 0;
00095 u->magic = URLMAGIC;
00096 return XurlLink(u, msg, file, line);
00097 }
00098
00099 urlinfo XurlFree(urlinfo u, const char *msg, const char *file, unsigned line)
00100 {
00101 int xx;
00102
00103 URLSANE(u);
00104 URLDBGREFS(0, (stderr, "--> url %p -- %d %s at %s:%u\n", u, u->nrefs, msg, file, line));
00105 if (--u->nrefs > 0)
00106 return u;
00107 if (u->ctrl) {
00108 #ifndef NOTYET
00109 void * fp = fdGetFp(u->ctrl);
00110
00111 if (fp) {
00112 fdPush(u->ctrl, fpio, fp, -1);
00113 (void) Fclose(u->ctrl);
00114 } else if (fdio->_fileno(u->ctrl) >= 0)
00115 xx = fdio->close(u->ctrl);
00116
00117 #else
00118 (void) Fclose(u->ctrl);
00119 #endif
00120
00121 u->ctrl = fdio->_fdderef(u->ctrl, "persist ctrl (urlFree)", file, line);
00122
00123 if (u->ctrl)
00124 fprintf(stderr, _("warning: u %p ctrl %p nrefs != 0 (%s %s)\n"),
00125 u, u->ctrl, (u->host ? u->host : ""),
00126 (u->scheme ? u->scheme : ""));
00127
00128 }
00129 if (u->data) {
00130 #ifndef NOTYET
00131 void * fp = fdGetFp(u->data);
00132 if (fp) {
00133 fdPush(u->data, fpio, fp, -1);
00134 (void) Fclose(u->data);
00135 } else if (fdio->_fileno(u->data) >= 0)
00136 xx = fdio->close(u->data);
00137 #else
00138 (void) Fclose(u->ctrl);
00139 #endif
00140
00141 u->data = fdio->_fdderef(u->data, "persist data (urlFree)", file, line);
00142
00143 if (u->data)
00144 fprintf(stderr, _("warning: u %p data %p nrefs != 0 (%s %s)\n"),
00145 u, u->data, (u->host ? u->host : ""),
00146 (u->scheme ? u->scheme : ""));
00147
00148 }
00149 xx = davFree(u);
00150 u->buf = _free(u->buf);
00151 u->url = _free(u->url);
00152 u->scheme = _free((void *)u->scheme);
00153 u->user = _free((void *)u->user);
00154 u->password = _free((void *)u->password);
00155 u->host = _free((void *)u->host);
00156 u->portstr = _free((void *)u->portstr);
00157 u->proxyu = _free((void *)u->proxyu);
00158 u->proxyh = _free((void *)u->proxyh);
00159
00160 u = _free(u);
00161 return NULL;
00162 }
00163
00164
00165 void urlFreeCache(void)
00166 {
00167 if (_url_cache) {
00168 int i;
00169 for (i = 0; i < _url_count; i++) {
00170 if (_url_cache[i] == NULL) continue;
00171 _url_cache[i] = urlFree(_url_cache[i], "_url_cache");
00172 if (_url_cache[i])
00173 fprintf(stderr,
00174 _("warning: _url_cache[%d] %p nrefs(%d) != 1 (%s %s)\n"),
00175 i, _url_cache[i], _url_cache[i]->nrefs,
00176 (_url_cache[i]->host ? _url_cache[i]->host : ""),
00177 (_url_cache[i]->scheme ? _url_cache[i]->scheme : ""));
00178 }
00179 }
00180 _url_cache = _free(_url_cache);
00181 _url_count = 0;
00182 }
00183
00184
00185 static int urlStrcmp( const char * str1, const char * str2)
00186
00187 {
00188 if (str1)
00189 if (str2)
00190 return strcmp(str1, str2);
00191 if (str1 != str2)
00192 return -1;
00193 return 0;
00194 }
00195
00196
00197
00198 static void urlFind( urlinfo * uret, int mustAsk)
00199
00200
00201 {
00202 urlinfo u;
00203 int ucx;
00204 int i = 0;
00205
00206 if (uret == NULL)
00207 return;
00208
00209 u = *uret;
00210 URLSANE(u);
00211
00212 ucx = -1;
00213 for (i = 0; i < _url_count; i++) {
00214 urlinfo ou = NULL;
00215 if (_url_cache == NULL || (ou = _url_cache[i]) == NULL) {
00216 if (ucx < 0)
00217 ucx = i;
00218 continue;
00219 }
00220
00221
00222
00223
00224
00225 if (urlStrcmp(u->scheme, ou->scheme))
00226 continue;
00227 if (urlStrcmp(u->host, ou->host))
00228 continue;
00229 if (urlStrcmp(u->user, ou->user))
00230 continue;
00231 if (urlStrcmp(u->portstr, ou->portstr))
00232 continue;
00233 break;
00234 }
00235
00236 if (i == _url_count) {
00237 if (ucx < 0) {
00238 ucx = _url_count++;
00239 _url_cache = xrealloc(_url_cache, sizeof(*_url_cache) * _url_count);
00240 }
00241 if (_url_cache)
00242 _url_cache[ucx] = urlLink(u, "_url_cache (miss)");
00243 u = urlFree(u, "urlSplit (urlFind miss)");
00244 } else {
00245 ucx = i;
00246 u = urlFree(u, "urlSplit (urlFind hit)");
00247 }
00248
00249
00250
00251 if (_url_cache)
00252 u = urlLink(_url_cache[ucx], "_url_cache");
00253 *uret = u;
00254
00255 u = urlFree(u, "_url_cache (urlFind)");
00256
00257
00258
00259 u->proxyp = -1;
00260 u->proxyh = _free(u->proxyh);
00261
00262
00263 if (u->urltype == URL_IS_FTP) {
00264
00265 if (mustAsk || (u->user != NULL && u->password == NULL)) {
00266 const char * host = (u->host ? u->host : "");
00267 const char * user = (u->user ? u->user : "");
00268 char * prompt;
00269 prompt = alloca(strlen(host) + strlen(user) + 256);
00270 sprintf(prompt, _("Password for %s@%s: "), user, host);
00271 u->password = _free(u->password);
00272
00273 u->password = Getpass(prompt);
00274
00275 if (u->password)
00276 u->password = xstrdup(u->password);
00277 }
00278
00279 if (u->proxyh == NULL) {
00280 const char *proxy = rpmExpand("%{_ftpproxy}", NULL);
00281 if (proxy && *proxy != '%') {
00282
00283 const char * host = (u->host ? u->host : "");
00284 const char *uu = (u->user ? u->user : "anonymous");
00285 char *nu = xmalloc(strlen(uu) + sizeof("@") + strlen(host));
00286 (void) stpcpy( stpcpy( stpcpy(nu, uu), "@"), host);
00287 u->proxyu = nu;
00288 u->proxyh = xstrdup(proxy);
00289 }
00290 proxy = _free(proxy);
00291 }
00292
00293 if (u->proxyp < 0) {
00294 const char *proxy = rpmExpand("%{_ftpport}", NULL);
00295 if (proxy && *proxy != '%') {
00296 char *end = NULL;
00297 int port = strtol(proxy, &end, 0);
00298 if (!(end && *end == '\0')) {
00299 fprintf(stderr, _("error: %sport must be a number\n"),
00300 (u->scheme ? u->scheme : ""));
00301 return;
00302 }
00303 u->proxyp = port;
00304 }
00305 proxy = _free(proxy);
00306 }
00307 }
00308
00309
00310 if (u->urltype == URL_IS_HTTP || u->urltype == URL_IS_HTTPS || u->urltype == URL_IS_HKP) {
00311
00312 if (u->proxyh == NULL) {
00313 const char *proxy = rpmExpand("%{_httpproxy}", NULL);
00314 if (proxy && *proxy != '%')
00315 u->proxyh = xstrdup(proxy);
00316 proxy = _free(proxy);
00317 }
00318
00319 if (u->proxyp < 0) {
00320 const char *proxy = rpmExpand("%{_httpport}", NULL);
00321 if (proxy && *proxy != '%') {
00322 char *end;
00323 int port = strtol(proxy, &end, 0);
00324 if (!(end && *end == '\0')) {
00325 fprintf(stderr, _("error: %sport must be a number\n"),
00326 (u->scheme ? u->scheme : ""));
00327 return;
00328 }
00329 u->proxyp = port;
00330 }
00331 proxy = _free(proxy);
00332 }
00333
00334 }
00335
00336 return;
00337 }
00338
00339
00340
00343
00344 static struct urlstring {
00345
00346 const char * leadin;
00347 urltype ret;
00348 } urlstrings[] = {
00349 { "file://", URL_IS_PATH },
00350 { "ftp://", URL_IS_FTP },
00351 { "hkp://", URL_IS_HKP },
00352 { "http://", URL_IS_HTTP },
00353 { "https://", URL_IS_HTTPS },
00354 { "-", URL_IS_DASH },
00355 { NULL, URL_IS_UNKNOWN }
00356 };
00357
00358 urltype urlIsURL(const char * url)
00359 {
00360 struct urlstring *us;
00361
00362
00363 if (url && *url) {
00364 for (us = urlstrings; us->leadin != NULL; us++) {
00365 if (strncmp(url, us->leadin, strlen(us->leadin)))
00366 continue;
00367 return us->ret;
00368 }
00369 }
00370
00371
00372 return URL_IS_UNKNOWN;
00373 }
00374
00375
00376
00377 urltype urlPath(const char * url, const char ** pathp)
00378 {
00379 const char *path;
00380 int urltype;
00381
00382 path = url;
00383 urltype = urlIsURL(url);
00384
00385 switch (urltype) {
00386 case URL_IS_FTP:
00387 url += sizeof("ftp://") - 1;
00388 path = strchr(url, '/');
00389 if (path == NULL) path = url + strlen(url);
00390 break;
00391 case URL_IS_PATH:
00392 url += sizeof("file://") - 1;
00393 path = strchr(url, '/');
00394 if (path == NULL) path = url + strlen(url);
00395 break;
00396 case URL_IS_HKP:
00397 url += sizeof("hkp://") - 1;
00398 path = strchr(url, '/');
00399 if (path == NULL) path = url + strlen(url);
00400 break;
00401 case URL_IS_HTTP:
00402 url += sizeof("http://") - 1;
00403 path = strchr(url, '/');
00404 if (path == NULL) path = url + strlen(url);
00405 break;
00406 case URL_IS_HTTPS:
00407 url += sizeof("https://") - 1;
00408 path = strchr(url, '/');
00409 if (path == NULL) path = url + strlen(url);
00410 break;
00411 case URL_IS_UNKNOWN:
00412 if (path == NULL) path = "";
00413 break;
00414 case URL_IS_DASH:
00415 path = "";
00416 break;
00417 }
00418
00419 if (pathp)
00420
00421 *pathp = path;
00422
00423 return urltype;
00424 }
00425
00426
00427
00428
00429
00430
00431
00432
00433
00434
00435 int urlSplit(const char * url, urlinfo *uret)
00436 {
00437 urlinfo u;
00438 char *myurl;
00439 char *s, *se, *f, *fe;
00440
00441 if (uret == NULL)
00442 return -1;
00443 if ((u = urlNew("urlSplit")) == NULL)
00444 return -1;
00445
00446 if ((se = s = myurl = xstrdup(url)) == NULL) {
00447 u = urlFree(u, "urlSplit (error #1)");
00448 return -1;
00449 }
00450
00451 u->url = xstrdup(url);
00452 u->urltype = urlIsURL(url);
00453
00454 while (1) {
00455
00456 while (*se && *se != '/') se++;
00457
00458 if (*se && (se != s) && se[-1] == ':' && se[0] == '/' && se[1] == '/') {
00459 se[-1] = '\0';
00460 u->scheme = xstrdup(s);
00461 se += 2;
00462 s = se++;
00463 continue;
00464 }
00465
00466
00467 *se = '\0';
00468 break;
00469 }
00470
00471
00472 fe = f = s;
00473 while (*fe && *fe != '@') fe++;
00474
00475 if (*fe == '@') {
00476 s = fe + 1;
00477 *fe = '\0';
00478
00479 while (fe > f && *fe != ':') fe--;
00480 if (*fe == ':') {
00481 *fe++ = '\0';
00482 u->password = xstrdup(fe);
00483 }
00484 u->user = xstrdup(f);
00485 }
00486
00487
00488
00489 fe = f = s;
00490 if (strchr(fe, '[') && strchr(fe, ']'))
00491 {
00492 fe = strchr(f, ']');
00493 *f++ = '\0';
00494 *fe++ = '\0';
00495 }
00496 while (*fe && *fe != ':') fe++;
00497 if (*fe == ':') {
00498 *fe++ = '\0';
00499 u->portstr = xstrdup(fe);
00500 if (u->portstr != NULL && u->portstr[0] != '\0') {
00501 char *end;
00502 u->port = strtol(u->portstr, &end, 0);
00503 if (!(end && *end == '\0')) {
00504 rpmMessage(RPMMESS_ERROR, _("url port must be a number\n"));
00505 myurl = _free(myurl);
00506 u = urlFree(u, "urlSplit (error #3)");
00507 return -1;
00508 }
00509 }
00510 }
00511 u->host = xstrdup(f);
00512
00513 if (u->port < 0 && u->scheme != NULL) {
00514 struct servent *serv;
00515
00516
00517 serv = getservbyname(u->scheme, "tcp");
00518
00519 if (serv != NULL)
00520 u->port = ntohs(serv->s_port);
00521 else if (u->urltype == URL_IS_FTP)
00522 u->port = IPPORT_FTP;
00523 else if (u->urltype == URL_IS_HKP)
00524 u->port = IPPORT_PGPKEYSERVER;
00525 else if (u->urltype == URL_IS_HTTP)
00526 u->port = IPPORT_HTTP;
00527 else if (u->urltype == URL_IS_HTTPS)
00528 u->port = IPPORT_HTTPS;
00529 }
00530
00531 myurl = _free(myurl);
00532 if (uret) {
00533 *uret = u;
00534
00535 urlFind(uret, 0);
00536
00537 }
00538 return 0;
00539 }
00540
00541
00542
00543 int urlGetFile(const char * url, const char * dest)
00544 {
00545 int rc;
00546 FD_t sfd = NULL;
00547 FD_t tfd = NULL;
00548 const char * sfuPath = NULL;
00549 int urlType = urlPath(url, &sfuPath);
00550
00551 if (*sfuPath == '\0')
00552 return FTPERR_UNKNOWN;
00553
00554 sfd = Fopen(url, "r");
00555 if (sfd == NULL || Ferror(sfd)) {
00556 rpmMessage(RPMMESS_DEBUG, _("failed to open %s: %s\n"), url, Fstrerror(sfd));
00557 rc = FTPERR_UNKNOWN;
00558 goto exit;
00559 }
00560
00561 if (dest == NULL) {
00562 if ((dest = strrchr(sfuPath, '/')) != NULL)
00563 dest++;
00564 else
00565 dest = sfuPath;
00566 }
00567
00568 if (dest == NULL)
00569 return FTPERR_UNKNOWN;
00570
00571
00572 tfd = Fopen(dest, "w");
00573 if (_url_debug)
00574 fprintf(stderr, "*** urlGetFile sfd %p %s tfd %p %s\n", sfd, url, (tfd ? tfd : NULL), dest);
00575 if (tfd == NULL || Ferror(tfd)) {
00576 rpmMessage(RPMMESS_DEBUG, _("failed to create %s: %s\n"), dest, Fstrerror(tfd));
00577 rc = FTPERR_UNKNOWN;
00578 goto exit;
00579 }
00580
00581 switch (urlType) {
00582 case URL_IS_HTTPS:
00583 case URL_IS_HTTP:
00584 case URL_IS_HKP:
00585 case URL_IS_FTP:
00586 case URL_IS_PATH:
00587 case URL_IS_DASH:
00588 case URL_IS_UNKNOWN:
00589 if ((rc = ufdGetFile(sfd, tfd))) {
00590 (void) Unlink(dest);
00591
00592 (void) Fclose(sfd) ;
00593 }
00594 sfd = NULL;
00595 break;
00596 default:
00597 rc = FTPERR_UNKNOWN;
00598 break;
00599 }
00600
00601 exit:
00602 if (tfd)
00603 (void) Fclose(tfd);
00604 if (sfd)
00605 (void) Fclose(sfd);
00606
00607 return rc;
00608 }