rpmio/url.c

Go to the documentation of this file.
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 /*@access FD_t@*/               /* XXX compared with NULL */
00016 /*@access urlinfo@*/
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 /*@unchecked@*/
00034 int _url_iobuf_size = RPMURL_IOBUF_SIZE;
00035 
00038 /*@unchecked@*/
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 /*@unchecked@*/
00049 /*@only@*/ /*@null@*/
00050 urlinfo *_url_cache = NULL;
00051 
00054 /*@unchecked@*/
00055 int _url_count = 0;
00056 
00062 /*@unused@*/ static inline /*@null@*/ void *
00063 _free(/*@only@*/ /*@null@*/ const void * p) /*@modifies 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 /*@-modfilesys@*/
00074 URLDBGREFS(0, (stderr, "--> url %p ++ %d %s at %s:%u\n", u, u->nrefs, msg, file, line));
00075 /*@=modfilesys@*/
00076     /*@-refcounttrans@*/ return u; /*@=refcounttrans@*/
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         /*@-refcounttrans -retalias@*/ return u; /*@=refcounttrans =retalias@*/
00107     if (u->ctrl) {
00108 #ifndef NOTYET
00109         void * fp = fdGetFp(u->ctrl);
00110         /*@-branchstate@*/
00111         if (fp) {
00112             fdPush(u->ctrl, fpio, fp, -1);   /* Push fpio onto stack */
00113             (void) Fclose(u->ctrl);
00114         } else if (fdio->_fileno(u->ctrl) >= 0)
00115             xx = fdio->close(u->ctrl);
00116         /*@=branchstate@*/
00117 #else
00118         (void) Fclose(u->ctrl);
00119 #endif
00120 
00121         u->ctrl = fdio->_fdderef(u->ctrl, "persist ctrl (urlFree)", file, line);
00122         /*@-usereleased@*/
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         /*@=usereleased@*/
00128     }
00129     if (u->data) {
00130 #ifndef NOTYET
00131         void * fp = fdGetFp(u->data);
00132         if (fp) {
00133             fdPush(u->data, fpio, fp, -1);   /* Push fpio onto stack */
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         /*@-usereleased@*/
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         /*@=usereleased@*/
00148     }
00149     if (u->sess != NULL) {
00150         /* HACK: neon include has prototype. */
00151         u->sess = NULL;
00152     }
00153     u->buf = _free(u->buf);
00154     u->url = _free(u->url);
00155     u->scheme = _free((void *)u->scheme);
00156     u->user = _free((void *)u->user);
00157     u->password = _free((void *)u->password);
00158     u->host = _free((void *)u->host);
00159     u->portstr = _free((void *)u->portstr);
00160     u->proxyu = _free((void *)u->proxyu);
00161     u->proxyh = _free((void *)u->proxyh);
00162 
00163     /*@-refcounttrans@*/ u = _free(u); /*@-refcounttrans@*/
00164     return NULL;
00165 }
00166 
00167 /*@-boundswrite@*/
00168 void urlFreeCache(void)
00169 {
00170     if (_url_cache) {
00171         int i;
00172         for (i = 0; i < _url_count; i++) {
00173             if (_url_cache[i] == NULL) continue;
00174             _url_cache[i] = urlFree(_url_cache[i], "_url_cache");
00175             if (_url_cache[i])
00176                 fprintf(stderr,
00177                         _("warning: _url_cache[%d] %p nrefs(%d) != 1 (%s %s)\n"),
00178                         i, _url_cache[i], _url_cache[i]->nrefs,
00179                         (_url_cache[i]->host ? _url_cache[i]->host : ""),
00180                         (_url_cache[i]->scheme ? _url_cache[i]->scheme : ""));
00181         }
00182     }
00183     _url_cache = _free(_url_cache);
00184     _url_count = 0;
00185 }
00186 /*@=boundswrite@*/
00187 
00188 static int urlStrcmp(/*@null@*/ const char * str1, /*@null@*/ const char * str2)
00189         /*@*/
00190 {
00191     if (str1)
00192         if (str2)
00193             return strcmp(str1, str2);
00194     if (str1 != str2)
00195         return -1;
00196     return 0;
00197 }
00198 
00199 /*@-boundswrite@*/
00200 /*@-mods@*/
00201 static void urlFind(/*@null@*/ /*@in@*/ /*@out@*/ urlinfo * uret, int mustAsk)
00202         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
00203         /*@modifies *uret, rpmGlobalMacroContext, fileSystem, internalState @*/
00204 {
00205     urlinfo u;
00206     int ucx;
00207     int i = 0;
00208 
00209     if (uret == NULL)
00210         return;
00211 
00212     u = *uret;
00213     URLSANE(u);
00214 
00215     ucx = -1;
00216     for (i = 0; i < _url_count; i++) {
00217         urlinfo ou = NULL;
00218         if (_url_cache == NULL || (ou = _url_cache[i]) == NULL) {
00219             if (ucx < 0)
00220                 ucx = i;
00221             continue;
00222         }
00223 
00224         /* Check for cache-miss condition. A cache miss is
00225          *    a) both items are not NULL and don't compare.
00226          *    b) either of the items is not NULL.
00227          */
00228         if (urlStrcmp(u->scheme, ou->scheme))
00229             continue;
00230         if (urlStrcmp(u->host, ou->host))
00231             continue;
00232         if (urlStrcmp(u->user, ou->user))
00233             continue;
00234         if (urlStrcmp(u->portstr, ou->portstr))
00235             continue;
00236         break;  /* Found item in cache */
00237     }
00238 
00239     if (i == _url_count) {
00240         if (ucx < 0) {
00241             ucx = _url_count++;
00242             _url_cache = xrealloc(_url_cache, sizeof(*_url_cache) * _url_count);
00243         }
00244         if (_url_cache)         /* XXX always true */
00245             _url_cache[ucx] = urlLink(u, "_url_cache (miss)");
00246         u = urlFree(u, "urlSplit (urlFind miss)");
00247     } else {
00248         ucx = i;
00249         u = urlFree(u, "urlSplit (urlFind hit)");
00250     }
00251 
00252     /* This URL is now cached. */
00253 
00254     if (_url_cache)             /* XXX always true */
00255         u = urlLink(_url_cache[ucx], "_url_cache");
00256     *uret = u;
00257     /*@-usereleased@*/
00258     u = urlFree(u, "_url_cache (urlFind)");
00259     /*@=usereleased@*/
00260 
00261     /* Zap proxy host and port in case they have been reset */
00262     u->proxyp = -1;
00263     u->proxyh = _free(u->proxyh);
00264 
00265     /* Perform one-time FTP initialization */
00266     if (u->urltype == URL_IS_FTP) {
00267 
00268         if (mustAsk || (u->user != NULL && u->password == NULL)) {
00269             const char * host = (u->host ? u->host : "");
00270             const char * user = (u->user ? u->user : "");
00271             char * prompt;
00272             prompt = alloca(strlen(host) + strlen(user) + 256);
00273             sprintf(prompt, _("Password for %s@%s: "), user, host);
00274             u->password = _free(u->password);
00275 /*@-dependenttrans -moduncon @*/
00276             u->password = /*@-unrecog@*/ getpass(prompt) /*@=unrecog@*/;
00277 /*@=dependenttrans =moduncon @*/
00278             if (u->password)
00279                 u->password = xstrdup(u->password);
00280         }
00281 
00282         if (u->proxyh == NULL) {
00283             const char *proxy = rpmExpand("%{_ftpproxy}", NULL);
00284             if (proxy && *proxy != '%') {
00285 /*@observer@*/
00286                 const char * host = (u->host ? u->host : "");
00287                 const char *uu = (u->user ? u->user : "anonymous");
00288                 char *nu = xmalloc(strlen(uu) + sizeof("@") + strlen(host));
00289                 (void) stpcpy( stpcpy( stpcpy(nu, uu), "@"), host);
00290                 u->proxyu = nu;
00291                 u->proxyh = xstrdup(proxy);
00292             }
00293             proxy = _free(proxy);
00294         }
00295 
00296         if (u->proxyp < 0) {
00297             const char *proxy = rpmExpand("%{_ftpport}", NULL);
00298             if (proxy && *proxy != '%') {
00299                 char *end = NULL;
00300                 int port = strtol(proxy, &end, 0);
00301                 if (!(end && *end == '\0')) {
00302                     fprintf(stderr, _("error: %sport must be a number\n"),
00303                         (u->scheme ? u->scheme : ""));
00304                     return;
00305                 }
00306                 u->proxyp = port;
00307             }
00308             proxy = _free(proxy);
00309         }
00310     }
00311 
00312     /* Perform one-time HTTP initialization */
00313     if (u->urltype == URL_IS_HTTP || u->urltype == URL_IS_HTTPS || u->urltype == URL_IS_HKP) {
00314 
00315         if (u->proxyh == NULL) {
00316             const char *proxy = rpmExpand("%{_httpproxy}", NULL);
00317             if (proxy && *proxy != '%')
00318                 u->proxyh = xstrdup(proxy);
00319             proxy = _free(proxy);
00320         }
00321 
00322         if (u->proxyp < 0) {
00323             const char *proxy = rpmExpand("%{_httpport}", NULL);
00324             if (proxy && *proxy != '%') {
00325                 char *end;
00326                 int port = strtol(proxy, &end, 0);
00327                 if (!(end && *end == '\0')) {
00328                     fprintf(stderr, _("error: %sport must be a number\n"),
00329                         (u->scheme ? u->scheme : ""));
00330                     return;
00331                 }
00332                 u->proxyp = port;
00333             }
00334             proxy = _free(proxy);
00335         }
00336 
00337     }
00338 
00339     return;
00340 }
00341 /*@=mods@*/
00342 /*@=boundswrite@*/
00343 
00346 /*@observer@*/ /*@unchecked@*/
00347 static struct urlstring {
00348 /*@observer@*/ /*@null@*/
00349     const char * leadin;
00350     urltype     ret;
00351 } urlstrings[] = {
00352     { "file://",        URL_IS_PATH },
00353     { "ftp://",         URL_IS_FTP },
00354     { "-",              URL_IS_DASH },
00355     { NULL,             URL_IS_UNKNOWN }
00356 };
00357 
00358 urltype urlIsURL(const char * url)
00359 {
00360     struct urlstring *us;
00361 
00362 /*@-boundsread@*/
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 /*@=boundsread@*/
00371 
00372     return URL_IS_UNKNOWN;
00373 }
00374 
00375 /*@-boundswrite@*/
00376 /* Return path portion of url (or pointer to NUL if url == NULL) */
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     /*@-branchstate@*/
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     /*@=branchstate@*/
00419     if (pathp)
00420         /*@-observertrans@*/
00421         *pathp = path;
00422         /*@=observertrans@*/
00423     return urltype;
00424 }
00425 /*@=boundswrite@*/
00426 
00427 /*
00428  * Split URL into components. The URL can look like
00429  *      scheme://user:password@host:port/path
00430  */
00431 /*@-bounds@*/
00432 /*@-modfilesys@*/
00433 int urlSplit(const char * url, urlinfo *uret)
00434 {
00435     urlinfo u;
00436     char *myurl;
00437     char *s, *se, *f, *fe;
00438 
00439     if (uret == NULL)
00440         return -1;
00441     if ((u = urlNew("urlSplit")) == NULL)
00442         return -1;
00443 
00444     if ((se = s = myurl = xstrdup(url)) == NULL) {
00445         u = urlFree(u, "urlSplit (error #1)");
00446         return -1;
00447     }
00448 
00449     u->url = xstrdup(url);
00450     u->urltype = urlIsURL(url);
00451 
00452     while (1) {
00453         /* Point to end of next item */
00454         while (*se && *se != '/') se++;
00455         /* Item was scheme. Save scheme and go for the rest ...*/
00456         if (*se && (se != s) && se[-1] == ':' && se[0] == '/' && se[1] == '/') {
00457                 se[-1] = '\0';
00458             u->scheme = xstrdup(s);
00459             se += 2;    /* skip over "//" */
00460             s = se++;
00461             continue;
00462         }
00463         
00464         /* Item was everything-but-path. Continue parse on rest */
00465         *se = '\0';
00466         break;
00467     }
00468 
00469     /* Look for ...@host... */
00470     fe = f = s;
00471     while (*fe && *fe != '@') fe++;
00472     /*@-branchstate@*/
00473     if (*fe == '@') {
00474         s = fe + 1;
00475         *fe = '\0';
00476         /* Look for user:password@host... */
00477         while (fe > f && *fe != ':') fe--;
00478         if (*fe == ':') {
00479             *fe++ = '\0';
00480             u->password = xstrdup(fe);
00481         }
00482         u->user = xstrdup(f);
00483     }
00484     /*@=branchstate@*/
00485 
00486     /* Look for ...host:port */
00487     fe = f = s;
00488     while (*fe && *fe != ':') fe++;
00489     if (*fe == ':') {
00490         *fe++ = '\0';
00491         u->portstr = xstrdup(fe);
00492         if (u->portstr != NULL && u->portstr[0] != '\0') {
00493             char *end;
00494             u->port = strtol(u->portstr, &end, 0);
00495             if (!(end && *end == '\0')) {
00496                 rpmMessage(RPMMESS_ERROR, _("url port must be a number\n"));
00497                 myurl = _free(myurl);
00498                 u = urlFree(u, "urlSplit (error #3)");
00499                 return -1;
00500             }
00501         }
00502     }
00503     u->host = xstrdup(f);
00504 
00505     if (u->port < 0 && u->scheme != NULL) {
00506         struct servent *serv;
00507 /*@-multithreaded -moduncon @*/
00508         /* HACK hkp:// might lookup "pgpkeyserver" */
00509         serv = getservbyname(u->scheme, "tcp");
00510 /*@=multithreaded =moduncon @*/
00511         if (serv != NULL)
00512             u->port = ntohs(serv->s_port);
00513         else if (u->urltype == URL_IS_FTP)
00514             u->port = IPPORT_FTP;
00515         else if (u->urltype == URL_IS_HKP)
00516             u->port = IPPORT_PGPKEYSERVER;
00517         else if (u->urltype == URL_IS_HTTP)
00518             u->port = IPPORT_HTTP;
00519         else if (u->urltype == URL_IS_HTTPS)
00520             u->port = IPPORT_HTTPS;
00521     }
00522 
00523     myurl = _free(myurl);
00524     if (uret) {
00525         *uret = u;
00526 /*@-globs -mods @*/ /* FIX: rpmGlobalMacroContext not in <rpmlib.h> */
00527         urlFind(uret, 0);
00528 /*@=globs =mods @*/
00529     }
00530     return 0;
00531 }
00532 /*@=modfilesys@*/
00533 /*@=bounds@*/
00534 
00535 int urlGetFile(const char * url, const char * dest)
00536 {
00537     int rc;
00538     FD_t sfd = NULL;
00539     FD_t tfd = NULL;
00540     const char * sfuPath = NULL;
00541     int urlType = urlPath(url, &sfuPath);
00542 
00543     if (*sfuPath == '\0')
00544         return FTPERR_UNKNOWN;
00545         
00546     sfd = Fopen(url, "r.ufdio");
00547     if (sfd == NULL || Ferror(sfd)) {
00548         rpmMessage(RPMMESS_DEBUG, _("failed to open %s: %s\n"), url, Fstrerror(sfd));
00549         rc = FTPERR_UNKNOWN;
00550         goto exit;
00551     }
00552 
00553     if (dest == NULL) {
00554         if ((dest = strrchr(sfuPath, '/')) != NULL)
00555             dest++;
00556         else
00557             dest = sfuPath;
00558     }
00559 
00560     if (dest == NULL)
00561         return FTPERR_UNKNOWN;
00562 
00563     tfd = Fopen(dest, "w.ufdio");
00564 if (_url_debug)
00565 fprintf(stderr, "*** urlGetFile sfd %p %s tfd %p %s\n", sfd, url, (tfd ? tfd : NULL), dest);
00566     if (tfd == NULL || Ferror(tfd)) {
00567         /* XXX Fstrerror */
00568         rpmMessage(RPMMESS_DEBUG, _("failed to create %s: %s\n"), dest, Fstrerror(tfd));
00569         rc = FTPERR_UNKNOWN;
00570         goto exit;
00571     }
00572 
00573     switch (urlType) {
00574     case URL_IS_HTTPS:
00575     case URL_IS_HTTP:
00576     case URL_IS_HKP:
00577     case URL_IS_FTP:
00578     case URL_IS_PATH:
00579     case URL_IS_DASH:
00580     case URL_IS_UNKNOWN:
00581         if ((rc = ufdGetFile(sfd, tfd))) {
00582             (void) Unlink(dest);
00583             /* XXX FIXME: sfd possibly closed by copyData */
00584             /*@-usereleased@*/ (void) Fclose(sfd) /*@=usereleased@*/ ;
00585         }
00586         sfd = NULL;     /* XXX Fclose(sfd) done by ufdGetFile */
00587         break;
00588     default:
00589         rc = FTPERR_UNKNOWN;
00590         break;
00591     }
00592 
00593 exit:
00594     if (tfd)
00595         (void) Fclose(tfd);
00596     if (sfd)
00597         (void) Fclose(sfd);
00598 
00599     return rc;
00600 }

Generated on Tue Feb 19 22:53:26 2008 for rpm by  doxygen 1.5.1