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