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 #ifdef WITH_NEON
00151         /* HACK: neon include has prototype. */
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     /*@-refcounttrans@*/ u = _free(u); /*@-refcounttrans@*/
00167     return NULL;
00168 }
00169 
00170 /*@-boundswrite@*/
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 /*@=boundswrite@*/
00190 
00191 static int urlStrcmp(/*@null@*/ const char * str1, /*@null@*/ 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 /*@-boundswrite@*/
00203 /*@-mods@*/
00204 static void urlFind(/*@null@*/ /*@in@*/ /*@out@*/ urlinfo * uret, int mustAsk)
00205         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
00206         /*@modifies *uret, rpmGlobalMacroContext, fileSystem, internalState @*/
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         /* Check for cache-miss condition. A cache miss is
00228          *    a) both items are not NULL and don't compare.
00229          *    b) either of the items is not NULL.
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;  /* Found item in cache */
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)         /* XXX always true */
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     /* This URL is now cached. */
00256 
00257     if (_url_cache)             /* XXX always true */
00258         u = urlLink(_url_cache[ucx], "_url_cache");
00259     *uret = u;
00260     /*@-usereleased@*/
00261     u = urlFree(u, "_url_cache (urlFind)");
00262     /*@=usereleased@*/
00263 
00264     /* Zap proxy host and port in case they have been reset */
00265     u->proxyp = -1;
00266     u->proxyh = _free(u->proxyh);
00267 
00268     /* Perform one-time FTP initialization */
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 /*@-dependenttrans -moduncon @*/
00279             u->password = /*@-unrecog@*/ getpass(prompt) /*@=unrecog@*/;
00280 /*@=dependenttrans =moduncon @*/
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 /*@observer@*/
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     /* Perform one-time HTTP initialization */
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 /*@=mods@*/
00345 /*@=boundswrite@*/
00346 
00349 /*@observer@*/ /*@unchecked@*/
00350 static struct urlstring {
00351 /*@observer@*/ /*@null@*/
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 /*@-boundsread@*/
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 /*@=boundsread@*/
00377 
00378     return URL_IS_UNKNOWN;
00379 }
00380 
00381 /*@-boundswrite@*/
00382 /* Return path portion of url (or pointer to NUL if url == NULL) */
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     /*@-branchstate@*/
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     /*@=branchstate@*/
00425     if (pathp)
00426         /*@-observertrans@*/
00427         *pathp = path;
00428         /*@=observertrans@*/
00429     return urltype;
00430 }
00431 /*@=boundswrite@*/
00432 
00433 /*
00434  * Split URL into components. The URL can look like
00435  *      scheme://user:password@host:port/path
00436   * or as in RFC2732 for IPv6 address
00437   *    service://user:password@[ip:v6:ad:dr:es:s]:port/path
00438  */
00439 /*@-bounds@*/
00440 /*@-modfilesys@*/
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         /* Point to end of next item */
00462         while (*se && *se != '/') se++;
00463         /* Item was scheme. Save scheme and go for the rest ...*/
00464         if (*se && (se != s) && se[-1] == ':' && se[0] == '/' && se[1] == '/') {
00465                 se[-1] = '\0';
00466             u->scheme = xstrdup(s);
00467             se += 2;    /* skip over "//" */
00468             s = se++;
00469             continue;
00470         }
00471         
00472         /* Item was everything-but-path. Continue parse on rest */
00473         *se = '\0';
00474         break;
00475     }
00476 
00477     /* Look for ...@host... */
00478     fe = f = s;
00479     while (*fe && *fe != '@') fe++;
00480     /*@-branchstate@*/
00481     if (*fe == '@') {
00482         s = fe + 1;
00483         *fe = '\0';
00484         /* Look for user:password@host... */
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     /*@=branchstate@*/
00493 
00494     /* Look for ...host:port or [v6addr]:port*/
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 /*@-multithreaded -moduncon @*/
00522         /* HACK hkp:// might lookup "pgpkeyserver" */
00523         serv = getservbyname(u->scheme, "tcp");
00524 /*@=multithreaded =moduncon @*/
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 /*@-globs -mods @*/ /* FIX: rpmGlobalMacroContext not in <rpmlib.h> */
00541         urlFind(uret, 0);
00542 /*@=globs =mods @*/
00543     }
00544     return 0;
00545 }
00546 /*@=modfilesys@*/
00547 /*@=bounds@*/
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         /* XXX Fstrerror */
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             /* XXX FIXME: sfd possibly closed by copyData */
00598             /*@-usereleased@*/ (void) Fclose(sfd) /*@=usereleased@*/ ;
00599         }
00600         sfd = NULL;     /* XXX Fclose(sfd) done by ufdGetFile */
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 }

Generated on Fri Oct 12 08:44:55 2007 for rpm by  doxygen 1.5.2