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

Generated on Fri Aug 11 11:29:28 2006 for rpm by  doxygen 1.4.7