Main Page | Modules | Data Structures | File List | Data Fields | Globals | Related Pages

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->allow = RPMURL_SERVER_HASRANGE;
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     u->buf = _free(u->buf);
00150     u->url = _free(u->url);
00151     u->scheme = _free((void *)u->scheme);
00152     u->user = _free((void *)u->user);
00153     u->password = _free((void *)u->password);
00154     u->host = _free((void *)u->host);
00155     u->portstr = _free((void *)u->portstr);
00156     u->proxyu = _free((void *)u->proxyu);
00157     u->proxyh = _free((void *)u->proxyh);
00158 
00159     /*@-refcounttrans@*/ u = _free(u); /*@-refcounttrans@*/
00160     return NULL;
00161 }
00162 
00163 /*@-boundswrite@*/
00164 void urlFreeCache(void)
00165 {
00166     if (_url_cache) {
00167         int i;
00168         for (i = 0; i < _url_count; i++) {
00169             if (_url_cache[i] == NULL) continue;
00170             _url_cache[i] = urlFree(_url_cache[i], "_url_cache");
00171             if (_url_cache[i])
00172                 fprintf(stderr,
00173                         _("warning: _url_cache[%d] %p nrefs(%d) != 1 (%s %s)\n"),
00174                         i, _url_cache[i], _url_cache[i]->nrefs,
00175                         (_url_cache[i]->host ? _url_cache[i]->host : ""),
00176                         (_url_cache[i]->scheme ? _url_cache[i]->scheme : ""));
00177         }
00178     }
00179     _url_cache = _free(_url_cache);
00180     _url_count = 0;
00181 }
00182 /*@=boundswrite@*/
00183 
00184 static int urlStrcmp(/*@null@*/ const char * str1, /*@null@*/ const char * str2)
00185         /*@*/
00186 {
00187     if (str1)
00188         if (str2)
00189             return strcmp(str1, str2);
00190     if (str1 != str2)
00191         return -1;
00192     return 0;
00193 }
00194 
00195 /*@-boundswrite@*/
00196 /*@-mods@*/
00197 static void urlFind(/*@null@*/ /*@in@*/ /*@out@*/ urlinfo * uret, int mustAsk)
00198         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
00199         /*@modifies *uret, rpmGlobalMacroContext, fileSystem, internalState @*/
00200 {
00201     urlinfo u;
00202     int ucx;
00203     int i = 0;
00204 
00205     if (uret == NULL)
00206         return;
00207 
00208     u = *uret;
00209     URLSANE(u);
00210 
00211     ucx = -1;
00212     for (i = 0; i < _url_count; i++) {
00213         urlinfo ou = NULL;
00214         if (_url_cache == NULL || (ou = _url_cache[i]) == NULL) {
00215             if (ucx < 0)
00216                 ucx = i;
00217             continue;
00218         }
00219 
00220         /* Check for cache-miss condition. A cache miss is
00221          *    a) both items are not NULL and don't compare.
00222          *    b) either of the items is not NULL.
00223          */
00224         if (urlStrcmp(u->scheme, ou->scheme))
00225             continue;
00226         if (urlStrcmp(u->host, ou->host))
00227             continue;
00228         if (urlStrcmp(u->user, ou->user))
00229             continue;
00230         if (urlStrcmp(u->portstr, ou->portstr))
00231             continue;
00232         break;  /* Found item in cache */
00233     }
00234 
00235     if (i == _url_count) {
00236         if (ucx < 0) {
00237             ucx = _url_count++;
00238             _url_cache = xrealloc(_url_cache, sizeof(*_url_cache) * _url_count);
00239         }
00240         if (_url_cache)         /* XXX always true */
00241             _url_cache[ucx] = urlLink(u, "_url_cache (miss)");
00242         u = urlFree(u, "urlSplit (urlFind miss)");
00243     } else {
00244         ucx = i;
00245         u = urlFree(u, "urlSplit (urlFind hit)");
00246     }
00247 
00248     /* This URL is now cached. */
00249 
00250     if (_url_cache)             /* XXX always true */
00251         u = urlLink(_url_cache[ucx], "_url_cache");
00252     *uret = u;
00253     /*@-usereleased@*/
00254     u = urlFree(u, "_url_cache (urlFind)");
00255     /*@=usereleased@*/
00256 
00257     /* Zap proxy host and port in case they have been reset */
00258     u->proxyp = -1;
00259     u->proxyh = _free(u->proxyh);
00260 
00261     /* Perform one-time FTP initialization */
00262     if (u->urltype == URL_IS_FTP) {
00263 
00264         if (mustAsk || (u->user != NULL && u->password == NULL)) {
00265             const char * host = (u->host ? u->host : "");
00266             const char * user = (u->user ? u->user : "");
00267             char * prompt;
00268             prompt = alloca(strlen(host) + strlen(user) + 256);
00269             sprintf(prompt, _("Password for %s@%s: "), user, host);
00270             u->password = _free(u->password);
00271 /*@-dependenttrans -moduncon @*/
00272             u->password = /*@-unrecog@*/ getpass(prompt) /*@=unrecog@*/;
00273 /*@=dependenttrans =moduncon @*/
00274             if (u->password)
00275                 u->password = xstrdup(u->password);
00276         }
00277 
00278         if (u->proxyh == NULL) {
00279             const char *proxy = rpmExpand("%{_ftpproxy}", NULL);
00280             if (proxy && *proxy != '%') {
00281 /*@observer@*/
00282                 const char * host = (u->host ? u->host : "");
00283                 const char *uu = (u->user ? u->user : "anonymous");
00284                 char *nu = xmalloc(strlen(uu) + sizeof("@") + strlen(host));
00285                 (void) stpcpy( stpcpy( stpcpy(nu, uu), "@"), host);
00286                 u->proxyu = nu;
00287                 u->proxyh = xstrdup(proxy);
00288             }
00289             proxy = _free(proxy);
00290         }
00291 
00292         if (u->proxyp < 0) {
00293             const char *proxy = rpmExpand("%{_ftpport}", NULL);
00294             if (proxy && *proxy != '%') {
00295                 char *end = NULL;
00296                 int port = strtol(proxy, &end, 0);
00297                 if (!(end && *end == '\0')) {
00298                     fprintf(stderr, _("error: %sport must be a number\n"),
00299                         (u->scheme ? u->scheme : ""));
00300                     return;
00301                 }
00302                 u->proxyp = port;
00303             }
00304             proxy = _free(proxy);
00305         }
00306     }
00307 
00308     /* Perform one-time HTTP initialization */
00309     if (u->urltype == URL_IS_HTTP || u->urltype == URL_IS_HTTPS || u->urltype == URL_IS_HKP) {
00310 
00311         if (u->proxyh == NULL) {
00312             const char *proxy = rpmExpand("%{_httpproxy}", NULL);
00313             if (proxy && *proxy != '%')
00314                 u->proxyh = xstrdup(proxy);
00315             proxy = _free(proxy);
00316         }
00317 
00318         if (u->proxyp < 0) {
00319             const char *proxy = rpmExpand("%{_httpport}", NULL);
00320             if (proxy && *proxy != '%') {
00321                 char *end;
00322                 int port = strtol(proxy, &end, 0);
00323                 if (!(end && *end == '\0')) {
00324                     fprintf(stderr, _("error: %sport must be a number\n"),
00325                         (u->scheme ? u->scheme : ""));
00326                     return;
00327                 }
00328                 u->proxyp = port;
00329             }
00330             proxy = _free(proxy);
00331         }
00332 
00333     }
00334 
00335     return;
00336 }
00337 /*@=mods@*/
00338 /*@=boundswrite@*/
00339 
00342 /*@observer@*/ /*@unchecked@*/
00343 static struct urlstring {
00344 /*@observer@*/ /*@null@*/
00345     const char * leadin;
00346     urltype     ret;
00347 } urlstrings[] = {
00348     { "file://",        URL_IS_PATH },
00349     { "ftp://",         URL_IS_FTP },
00350     { "-",              URL_IS_DASH },
00351     { NULL,             URL_IS_UNKNOWN }
00352 };
00353 
00354 urltype urlIsURL(const char * url)
00355 {
00356     struct urlstring *us;
00357 
00358 /*@-boundsread@*/
00359     if (url && *url) {
00360         for (us = urlstrings; us->leadin != NULL; us++) {
00361             if (strncmp(url, us->leadin, strlen(us->leadin)))
00362                 continue;
00363             return us->ret;
00364         }
00365     }
00366 /*@=boundsread@*/
00367 
00368     return URL_IS_UNKNOWN;
00369 }
00370 
00371 /*@-boundswrite@*/
00372 /* Return path portion of url (or pointer to NUL if url == NULL) */
00373 urltype urlPath(const char * url, const char ** pathp)
00374 {
00375     const char *path;
00376     int urltype;
00377 
00378     path = url;
00379     urltype = urlIsURL(url);
00380     /*@-branchstate@*/
00381     switch (urltype) {
00382     case URL_IS_FTP:
00383         url += sizeof("ftp://") - 1;
00384         path = strchr(url, '/');
00385         if (path == NULL) path = url + strlen(url);
00386         break;
00387     case URL_IS_PATH:
00388         url += sizeof("file://") - 1;
00389         path = strchr(url, '/');
00390         if (path == NULL) path = url + strlen(url);
00391         break;
00392     case URL_IS_HKP:
00393         url += sizeof("hkp://") - 1;
00394         path = strchr(url, '/');
00395         if (path == NULL) path = url + strlen(url);
00396         break;
00397     case URL_IS_HTTP:
00398         url += sizeof("http://") - 1;
00399         path = strchr(url, '/');
00400         if (path == NULL) path = url + strlen(url);
00401         break;
00402     case URL_IS_HTTPS:
00403         url += sizeof("https://") - 1;
00404         path = strchr(url, '/');
00405         if (path == NULL) path = url + strlen(url);
00406         break;
00407     case URL_IS_UNKNOWN:
00408         if (path == NULL) path = "";
00409         break;
00410     case URL_IS_DASH:
00411         path = "";
00412         break;
00413     }
00414     /*@=branchstate@*/
00415     if (pathp)
00416         /*@-observertrans@*/
00417         *pathp = path;
00418         /*@=observertrans@*/
00419     return urltype;
00420 }
00421 /*@=boundswrite@*/
00422 
00423 /*
00424  * Split URL into components. The URL can look like
00425  *      scheme://user:password@host:port/path
00426   * or as in RFC2732 for IPv6 address
00427   *    service://user:password@[ip:v6:ad:dr:es:s]:port/path
00428  */
00429 /*@-bounds@*/
00430 /*@-modfilesys@*/
00431 int urlSplit(const char * url, urlinfo *uret)
00432 {
00433     urlinfo u;
00434     char *myurl;
00435     char *s, *se, *f, *fe;
00436 
00437     if (uret == NULL)
00438         return -1;
00439     if ((u = urlNew("urlSplit")) == NULL)
00440         return -1;
00441 
00442     if ((se = s = myurl = xstrdup(url)) == NULL) {
00443         u = urlFree(u, "urlSplit (error #1)");
00444         return -1;
00445     }
00446 
00447     u->url = xstrdup(url);
00448     u->urltype = urlIsURL(url);
00449 
00450     while (1) {
00451         /* Point to end of next item */
00452         while (*se && *se != '/') se++;
00453         /* Item was scheme. Save scheme and go for the rest ...*/
00454         if (*se && (se != s) && se[-1] == ':' && se[0] == '/' && se[1] == '/') {
00455                 se[-1] = '\0';
00456             u->scheme = xstrdup(s);
00457             se += 2;    /* skip over "//" */
00458             s = se++;
00459             continue;
00460         }
00461         
00462         /* Item was everything-but-path. Continue parse on rest */
00463         *se = '\0';
00464         break;
00465     }
00466 
00467     /* Look for ...@host... */
00468     fe = f = s;
00469     while (*fe && *fe != '@') fe++;
00470     /*@-branchstate@*/
00471     if (*fe == '@') {
00472         s = fe + 1;
00473         *fe = '\0';
00474         /* Look for user:password@host... */
00475         while (fe > f && *fe != ':') fe--;
00476         if (*fe == ':') {
00477             *fe++ = '\0';
00478             u->password = xstrdup(fe);
00479         }
00480         u->user = xstrdup(f);
00481     }
00482     /*@=branchstate@*/
00483 
00484     /* Look for ...host:port or [v6addr]:port*/
00485     fe = f = s;
00486     if (strchr(fe, '[') && strchr(fe, ']'))
00487     {
00488             fe = strchr(f, ']');
00489             *f++ = '\0';
00490             *fe++ = '\0';
00491     }
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");
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     /* XXX this can fail if directory in path does not exist. */
00568     tfd = Fopen(dest, "w");
00569 if (_url_debug)
00570 fprintf(stderr, "*** urlGetFile sfd %p %s tfd %p %s\n", sfd, url, (tfd ? tfd : NULL), dest);
00571     if (tfd == NULL || Ferror(tfd)) {
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 Mon Aug 3 15:25:07 2009 for rpm by  doxygen 1.4.4