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->httpHasRange = 1;
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 if (u->sess != NULL) {
00150 #ifdef WITH_NEON
00151
00152 ne_session_destroy(u->sess);
00153 #endif
00154 u->sess = NULL;
00155 }
00156 u->buf = _free(u->buf);
00157 u->url = _free(u->url);
00158 u->scheme = _free((void *)u->scheme);
00159 u->user = _free((void *)u->user);
00160 u->password = _free((void *)u->password);
00161 u->host = _free((void *)u->host);
00162 u->portstr = _free((void *)u->portstr);
00163 u->proxyu = _free((void *)u->proxyu);
00164 u->proxyh = _free((void *)u->proxyh);
00165
00166 u = _free(u);
00167 return NULL;
00168 }
00169
00170
00171 void urlFreeCache(void)
00172 {
00173 if (_url_cache) {
00174 int i;
00175 for (i = 0; i < _url_count; i++) {
00176 if (_url_cache[i] == NULL) continue;
00177 _url_cache[i] = urlFree(_url_cache[i], "_url_cache");
00178 if (_url_cache[i])
00179 fprintf(stderr,
00180 _("warning: _url_cache[%d] %p nrefs(%d) != 1 (%s %s)\n"),
00181 i, _url_cache[i], _url_cache[i]->nrefs,
00182 (_url_cache[i]->host ? _url_cache[i]->host : ""),
00183 (_url_cache[i]->scheme ? _url_cache[i]->scheme : ""));
00184 }
00185 }
00186 _url_cache = _free(_url_cache);
00187 _url_count = 0;
00188 }
00189
00190
00191 static int urlStrcmp( const char * str1, const char * str2)
00192
00193 {
00194 if (str1)
00195 if (str2)
00196 return strcmp(str1, str2);
00197 if (str1 != str2)
00198 return -1;
00199 return 0;
00200 }
00201
00202
00203
00204 static void urlFind( urlinfo * uret, int mustAsk)
00205
00206
00207 {
00208 urlinfo u;
00209 int ucx;
00210 int i = 0;
00211
00212 if (uret == NULL)
00213 return;
00214
00215 u = *uret;
00216 URLSANE(u);
00217
00218 ucx = -1;
00219 for (i = 0; i < _url_count; i++) {
00220 urlinfo ou = NULL;
00221 if (_url_cache == NULL || (ou = _url_cache[i]) == NULL) {
00222 if (ucx < 0)
00223 ucx = i;
00224 continue;
00225 }
00226
00227
00228
00229
00230
00231 if (urlStrcmp(u->scheme, ou->scheme))
00232 continue;
00233 if (urlStrcmp(u->host, ou->host))
00234 continue;
00235 if (urlStrcmp(u->user, ou->user))
00236 continue;
00237 if (urlStrcmp(u->portstr, ou->portstr))
00238 continue;
00239 break;
00240 }
00241
00242 if (i == _url_count) {
00243 if (ucx < 0) {
00244 ucx = _url_count++;
00245 _url_cache = xrealloc(_url_cache, sizeof(*_url_cache) * _url_count);
00246 }
00247 if (_url_cache)
00248 _url_cache[ucx] = urlLink(u, "_url_cache (miss)");
00249 u = urlFree(u, "urlSplit (urlFind miss)");
00250 } else {
00251 ucx = i;
00252 u = urlFree(u, "urlSplit (urlFind hit)");
00253 }
00254
00255
00256
00257 if (_url_cache)
00258 u = urlLink(_url_cache[ucx], "_url_cache");
00259 *uret = u;
00260
00261 u = urlFree(u, "_url_cache (urlFind)");
00262
00263
00264
00265 u->proxyp = -1;
00266 u->proxyh = _free(u->proxyh);
00267
00268
00269 if (u->urltype == URL_IS_FTP) {
00270
00271 if (mustAsk || (u->user != NULL && u->password == NULL)) {
00272 const char * host = (u->host ? u->host : "");
00273 const char * user = (u->user ? u->user : "");
00274 char * prompt;
00275 prompt = alloca(strlen(host) + strlen(user) + 256);
00276 sprintf(prompt, _("Password for %s@%s: "), user, host);
00277 u->password = _free(u->password);
00278
00279 u->password = getpass(prompt) ;
00280
00281 if (u->password)
00282 u->password = xstrdup(u->password);
00283 }
00284
00285 if (u->proxyh == NULL) {
00286 const char *proxy = rpmExpand("%{_ftpproxy}", NULL);
00287 if (proxy && *proxy != '%') {
00288
00289 const char * host = (u->host ? u->host : "");
00290 const char *uu = (u->user ? u->user : "anonymous");
00291 char *nu = xmalloc(strlen(uu) + sizeof("@") + strlen(host));
00292 (void) stpcpy( stpcpy( stpcpy(nu, uu), "@"), host);
00293 u->proxyu = nu;
00294 u->proxyh = xstrdup(proxy);
00295 }
00296 proxy = _free(proxy);
00297 }
00298
00299 if (u->proxyp < 0) {
00300 const char *proxy = rpmExpand("%{_ftpport}", NULL);
00301 if (proxy && *proxy != '%') {
00302 char *end = NULL;
00303 int port = strtol(proxy, &end, 0);
00304 if (!(end && *end == '\0')) {
00305 fprintf(stderr, _("error: %sport must be a number\n"),
00306 (u->scheme ? u->scheme : ""));
00307 return;
00308 }
00309 u->proxyp = port;
00310 }
00311 proxy = _free(proxy);
00312 }
00313 }
00314
00315
00316 if (u->urltype == URL_IS_HTTP || u->urltype == URL_IS_HTTPS || u->urltype == URL_IS_HKP) {
00317
00318 if (u->proxyh == NULL) {
00319 const char *proxy = rpmExpand("%{_httpproxy}", NULL);
00320 if (proxy && *proxy != '%')
00321 u->proxyh = xstrdup(proxy);
00322 proxy = _free(proxy);
00323 }
00324
00325 if (u->proxyp < 0) {
00326 const char *proxy = rpmExpand("%{_httpport}", NULL);
00327 if (proxy && *proxy != '%') {
00328 char *end;
00329 int port = strtol(proxy, &end, 0);
00330 if (!(end && *end == '\0')) {
00331 fprintf(stderr, _("error: %sport must be a number\n"),
00332 (u->scheme ? u->scheme : ""));
00333 return;
00334 }
00335 u->proxyp = port;
00336 }
00337 proxy = _free(proxy);
00338 }
00339
00340 }
00341
00342 return;
00343 }
00344
00345
00346
00349
00350 static struct urlstring {
00351
00352 const char * leadin;
00353 urltype ret;
00354 } urlstrings[] = {
00355 { "file://", URL_IS_PATH },
00356 { "ftp://", URL_IS_FTP },
00357 { "hkp://", URL_IS_HKP },
00358 { "http://", URL_IS_HTTP },
00359 { "https://", URL_IS_HTTPS },
00360 { "-", URL_IS_DASH },
00361 { NULL, URL_IS_UNKNOWN }
00362 };
00363
00364 urltype urlIsURL(const char * url)
00365 {
00366 struct urlstring *us;
00367
00368
00369 if (url && *url) {
00370 for (us = urlstrings; us->leadin != NULL; us++) {
00371 if (strncmp(url, us->leadin, strlen(us->leadin)))
00372 continue;
00373 return us->ret;
00374 }
00375 }
00376
00377
00378 return URL_IS_UNKNOWN;
00379 }
00380
00381
00382
00383 urltype urlPath(const char * url, const char ** pathp)
00384 {
00385 const char *path;
00386 int urltype;
00387
00388 path = url;
00389 urltype = urlIsURL(url);
00390
00391 switch (urltype) {
00392 case URL_IS_FTP:
00393 url += sizeof("ftp://") - 1;
00394 path = strchr(url, '/');
00395 if (path == NULL) path = url + strlen(url);
00396 break;
00397 case URL_IS_PATH:
00398 url += sizeof("file://") - 1;
00399 path = strchr(url, '/');
00400 if (path == NULL) path = url + strlen(url);
00401 break;
00402 case URL_IS_HKP:
00403 url += sizeof("hkp://") - 1;
00404 path = strchr(url, '/');
00405 if (path == NULL) path = url + strlen(url);
00406 break;
00407 case URL_IS_HTTP:
00408 url += sizeof("http://") - 1;
00409 path = strchr(url, '/');
00410 if (path == NULL) path = url + strlen(url);
00411 break;
00412 case URL_IS_HTTPS:
00413 url += sizeof("https://") - 1;
00414 path = strchr(url, '/');
00415 if (path == NULL) path = url + strlen(url);
00416 break;
00417 case URL_IS_UNKNOWN:
00418 if (path == NULL) path = "";
00419 break;
00420 case URL_IS_DASH:
00421 path = "";
00422 break;
00423 }
00424
00425 if (pathp)
00426
00427 *pathp = path;
00428
00429 return urltype;
00430 }
00431
00432
00433
00434
00435
00436
00437
00438
00439
00440
00441 int urlSplit(const char * url, urlinfo *uret)
00442 {
00443 urlinfo u;
00444 char *myurl;
00445 char *s, *se, *f, *fe;
00446
00447 if (uret == NULL)
00448 return -1;
00449 if ((u = urlNew("urlSplit")) == NULL)
00450 return -1;
00451
00452 if ((se = s = myurl = xstrdup(url)) == NULL) {
00453 u = urlFree(u, "urlSplit (error #1)");
00454 return -1;
00455 }
00456
00457 u->url = xstrdup(url);
00458 u->urltype = urlIsURL(url);
00459
00460 while (1) {
00461
00462 while (*se && *se != '/') se++;
00463
00464 if (*se && (se != s) && se[-1] == ':' && se[0] == '/' && se[1] == '/') {
00465 se[-1] = '\0';
00466 u->scheme = xstrdup(s);
00467 se += 2;
00468 s = se++;
00469 continue;
00470 }
00471
00472
00473 *se = '\0';
00474 break;
00475 }
00476
00477
00478 fe = f = s;
00479 while (*fe && *fe != '@') fe++;
00480
00481 if (*fe == '@') {
00482 s = fe + 1;
00483 *fe = '\0';
00484
00485 while (fe > f && *fe != ':') fe--;
00486 if (*fe == ':') {
00487 *fe++ = '\0';
00488 u->password = xstrdup(fe);
00489 }
00490 u->user = xstrdup(f);
00491 }
00492
00493
00494
00495 fe = f = s;
00496 if (strchr(fe, '[') && strchr(fe, ']'))
00497 {
00498 fe = strchr(f, ']');
00499 *f++ = '\0';
00500 *fe++ = '\0';
00501 }
00502 while (*fe && *fe != ':') fe++;
00503 if (*fe == ':') {
00504 *fe++ = '\0';
00505 u->portstr = xstrdup(fe);
00506 if (u->portstr != NULL && u->portstr[0] != '\0') {
00507 char *end;
00508 u->port = strtol(u->portstr, &end, 0);
00509 if (!(end && *end == '\0')) {
00510 rpmMessage(RPMMESS_ERROR, _("url port must be a number\n"));
00511 myurl = _free(myurl);
00512 u = urlFree(u, "urlSplit (error #3)");
00513 return -1;
00514 }
00515 }
00516 }
00517 u->host = xstrdup(f);
00518
00519 if (u->port < 0 && u->scheme != NULL) {
00520 struct servent *serv;
00521
00522
00523 serv = getservbyname(u->scheme, "tcp");
00524
00525 if (serv != NULL)
00526 u->port = ntohs(serv->s_port);
00527 else if (u->urltype == URL_IS_FTP)
00528 u->port = IPPORT_FTP;
00529 else if (u->urltype == URL_IS_HKP)
00530 u->port = IPPORT_PGPKEYSERVER;
00531 else if (u->urltype == URL_IS_HTTP)
00532 u->port = IPPORT_HTTP;
00533 else if (u->urltype == URL_IS_HTTPS)
00534 u->port = IPPORT_HTTPS;
00535 }
00536
00537 myurl = _free(myurl);
00538 if (uret) {
00539 *uret = u;
00540
00541 urlFind(uret, 0);
00542
00543 }
00544 return 0;
00545 }
00546
00547
00548
00549 int urlGetFile(const char * url, const char * dest)
00550 {
00551 int rc;
00552 FD_t sfd = NULL;
00553 FD_t tfd = NULL;
00554 const char * sfuPath = NULL;
00555 int urlType = urlPath(url, &sfuPath);
00556
00557 if (*sfuPath == '\0')
00558 return FTPERR_UNKNOWN;
00559
00560 sfd = Fopen(url, "r.ufdio");
00561 if (sfd == NULL || Ferror(sfd)) {
00562 rpmMessage(RPMMESS_DEBUG, _("failed to open %s: %s\n"), url, Fstrerror(sfd));
00563 rc = FTPERR_UNKNOWN;
00564 goto exit;
00565 }
00566
00567 if (dest == NULL) {
00568 if ((dest = strrchr(sfuPath, '/')) != NULL)
00569 dest++;
00570 else
00571 dest = sfuPath;
00572 }
00573
00574 if (dest == NULL)
00575 return FTPERR_UNKNOWN;
00576
00577 tfd = Fopen(dest, "w.ufdio");
00578 if (_url_debug)
00579 fprintf(stderr, "*** urlGetFile sfd %p %s tfd %p %s\n", sfd, url, (tfd ? tfd : NULL), dest);
00580 if (tfd == NULL || Ferror(tfd)) {
00581
00582 rpmMessage(RPMMESS_DEBUG, _("failed to create %s: %s\n"), dest, Fstrerror(tfd));
00583 rc = FTPERR_UNKNOWN;
00584 goto exit;
00585 }
00586
00587 switch (urlType) {
00588 case URL_IS_HTTPS:
00589 case URL_IS_HTTP:
00590 case URL_IS_HKP:
00591 case URL_IS_FTP:
00592 case URL_IS_PATH:
00593 case URL_IS_DASH:
00594 case URL_IS_UNKNOWN:
00595 if ((rc = ufdGetFile(sfd, tfd))) {
00596 (void) Unlink(dest);
00597
00598 (void) Fclose(sfd) ;
00599 }
00600 sfd = NULL;
00601 break;
00602 default:
00603 rc = FTPERR_UNKNOWN;
00604 break;
00605 }
00606
00607 exit:
00608 if (tfd)
00609 (void) Fclose(tfd);
00610 if (sfd)
00611 (void) Fclose(sfd);
00612
00613 return rc;
00614 }