rpmdb/legacy.c

Go to the documentation of this file.
00001 
00005 #include "system.h"
00006 
00007 #if HAVE_GELF_H
00008 
00009 #include <gelf.h>
00010 
00011 #if !defined(DT_GNU_PRELINKED)
00012 #define DT_GNU_PRELINKED        0x6ffffdf5
00013 #endif
00014 #if !defined(DT_GNU_LIBLIST)
00015 #define DT_GNU_LIBLIST          0x6ffffef9
00016 #endif
00017 
00018 #endif
00019 
00020 #include "rpmio_internal.h"
00021 #include <rpmlib.h>
00022 #include <rpmmacro.h>
00023 #include "misc.h"
00024 #include "legacy.h"
00025 #include "debug.h"
00026 
00027 #define alloca_strdup(_s)       strcpy(alloca(strlen(_s)+1), (_s))
00028 
00036 static int open_dso(const char * path, /*@null@*/ pid_t * pidp, /*@null@*/ size_t *fsizep)
00037         /*@globals rpmGlobalMacroContext, h_errno, fileSystem, internalState @*/
00038         /*@modifies *pidp, *fsizep, rpmGlobalMacroContext,
00039                 fileSystem, internalState @*/
00040 {
00041 /*@only@*/
00042     static const char * cmd = NULL;
00043     static int initted = 0;
00044     int fdno;
00045 
00046     if (!initted) {
00047         cmd = rpmExpand("%{?__prelink_undo_cmd}", NULL);
00048         initted++;
00049     }
00050 
00051 /*@-boundswrite@*/
00052     if (pidp) *pidp = 0;
00053 
00054     if (fsizep) {
00055         struct stat sb, * st = &sb;
00056         if (stat(path, st) < 0)
00057             return -1;
00058         *fsizep = st->st_size;
00059     }
00060 /*@=boundswrite@*/
00061 
00062     fdno = open(path, O_RDONLY);
00063     if (fdno < 0)
00064         return fdno;
00065 
00066 /*@-boundsread@*/
00067     if (!(cmd && *cmd))
00068         return fdno;
00069 /*@=boundsread@*/
00070 
00071 #if HAVE_GELF_H && HAVE_LIBELF
00072  {  Elf *elf = NULL;
00073     Elf_Scn *scn = NULL;
00074     Elf_Data *data = NULL;
00075     GElf_Ehdr ehdr;
00076     GElf_Shdr shdr;
00077     GElf_Dyn dyn;
00078     int bingo;
00079 
00080     (void) elf_version(EV_CURRENT);
00081 
00082 /*@-evalorder@*/
00083     if ((elf = elf_begin (fdno, ELF_C_READ, NULL)) == NULL
00084      || elf_kind(elf) != ELF_K_ELF
00085      || gelf_getehdr(elf, &ehdr) == NULL
00086      || !(ehdr.e_type == ET_DYN || ehdr.e_type == ET_EXEC))
00087         goto exit;
00088 /*@=evalorder@*/
00089 
00090     bingo = 0;
00091     /*@-branchstate -uniondef @*/
00092     while (!bingo && (scn = elf_nextscn(elf, scn)) != NULL) {
00093         (void) gelf_getshdr(scn, &shdr);
00094         if (shdr.sh_type != SHT_DYNAMIC)
00095             continue;
00096         while (!bingo && (data = elf_getdata (scn, data)) != NULL) {
00097             int maxndx = data->d_size / shdr.sh_entsize;
00098             int ndx;
00099 
00100             for (ndx = 0; ndx < maxndx; ++ndx) {
00101                 (void) gelf_getdyn (data, ndx, &dyn);
00102                 if (!(dyn.d_tag == DT_GNU_PRELINKED || dyn.d_tag == DT_GNU_LIBLIST))
00103                     /*@innercontinue@*/ continue;
00104                 bingo = 1;
00105                 /*@innerbreak@*/ break;
00106             }
00107         }
00108     }
00109     /*@=branchstate =uniondef @*/
00110 
00111 /*@-boundswrite@*/
00112     if (pidp != NULL && bingo) {
00113         int pipes[2];
00114         pid_t pid;
00115         int xx;
00116 
00117         xx = close(fdno);
00118         pipes[0] = pipes[1] = -1;
00119         xx = pipe(pipes);
00120         if (!(pid = fork())) {
00121             const char ** av;
00122             int ac;
00123             xx = close(pipes[0]);
00124             xx = dup2(pipes[1], STDOUT_FILENO);
00125             xx = close(pipes[1]);
00126             if (!poptParseArgvString(cmd, &ac, &av)) {
00127                 av[ac-1] = path;
00128                 av[ac] = NULL;
00129                 unsetenv("MALLOC_CHECK_");
00130                 xx = execve(av[0], (char *const *)av+1, environ);
00131             }
00132             _exit(127);
00133         }
00134         *pidp = pid;
00135         fdno = pipes[0];
00136         xx = close(pipes[1]);
00137     }
00138 /*@=boundswrite@*/
00139 
00140 exit:
00141     if (elf) (void) elf_end(elf);
00142  }
00143 #endif
00144 
00145     return fdno;
00146 }
00147 
00148 int domd5(const char * fn, unsigned char * digest, int asAscii, size_t *fsizep)
00149 {
00150     const char * path;
00151     urltype ut = urlPath(fn, &path);
00152     unsigned char * md5sum = NULL;
00153     size_t md5len;
00154     unsigned char buf[32*BUFSIZ];
00155     FD_t fd;
00156     size_t fsize = 0;
00157     pid_t pid = 0;
00158     int rc = 0;
00159     int fdno;
00160     int xx;
00161 
00162 /*@-globs -internalglobs -mods @*/
00163     fdno = open_dso(path, &pid, &fsize);
00164 /*@=globs =internalglobs =mods @*/
00165     if (fdno < 0) {
00166         rc = 1;
00167         goto exit;
00168     }
00169 
00170     /* file to large (32 MB), do not mmap file */
00171     if (fsize > (size_t) 32*1024*1024)
00172       if (ut == URL_IS_PATH || ut == URL_IS_UNKNOWN)
00173         ut = URL_IS_DASH; /* force fd io */
00174 
00175     switch(ut) {
00176     case URL_IS_PATH:
00177     case URL_IS_UNKNOWN:
00178 #ifdef HAVE_MMAP
00179       if (pid == 0) {
00180         DIGEST_CTX ctx;
00181         void * mapped;
00182 
00183         if (fsize) {
00184             mapped = mmap(NULL, fsize, PROT_READ, MAP_SHARED, fdno, 0);
00185             if (mapped == (void *)-1) {
00186                 xx = close(fdno);
00187                 rc = 1;
00188                 break;
00189             }
00190 
00191 #ifdef  MADV_SEQUENTIAL
00192             xx = madvise(mapped, fsize, MADV_SEQUENTIAL);
00193 #endif
00194         }
00195 
00196         ctx = rpmDigestInit(PGPHASHALGO_MD5, RPMDIGEST_NONE);
00197         if (fsize)
00198             xx = rpmDigestUpdate(ctx, mapped, fsize);
00199         xx = rpmDigestFinal(ctx, (void **)&md5sum, &md5len, asAscii);
00200         if (fsize)
00201             xx = munmap(mapped, fsize);
00202         xx = close(fdno);
00203         break;
00204       } /*@fallthrough@*/
00205 #endif
00206     case URL_IS_HTTPS:
00207     case URL_IS_HTTP:
00208     case URL_IS_FTP:
00209     case URL_IS_HKP:
00210     case URL_IS_DASH:
00211     default:
00212         /* Either use the pipe to prelink -y or open the URL. */
00213         fd = (pid != 0) ? fdDup(fdno) : Fopen(fn, "r.ufdio");
00214         (void) close(fdno);
00215         if (fd == NULL || Ferror(fd)) {
00216             rc = 1;
00217             if (fd != NULL)
00218                 (void) Fclose(fd);
00219             break;
00220         }
00221         
00222         fdInitDigest(fd, PGPHASHALGO_MD5, 0);
00223         fsize = 0;
00224         while ((rc = Fread(buf, sizeof(buf[0]), sizeof(buf), fd)) > 0)
00225             fsize += rc;
00226         fdFiniDigest(fd, PGPHASHALGO_MD5, (void **)&md5sum, &md5len, asAscii);
00227         if (Ferror(fd))
00228             rc = 1;
00229 
00230         (void) Fclose(fd);
00231         break;
00232     }
00233 
00234     /* Reap the prelink -y helper. */
00235     if (pid) {
00236         int status;
00237         (void) waitpid(pid, &status, 0);
00238         if (!WIFEXITED(status) || WEXITSTATUS(status))
00239             rc = 1;
00240     }
00241 
00242 exit:
00243 /*@-boundswrite@*/
00244     if (fsizep)
00245         *fsizep = fsize;
00246     if (!rc)
00247         memcpy(digest, md5sum, md5len);
00248 /*@=boundswrite@*/
00249     md5sum = _free(md5sum);
00250 
00251     return rc;
00252 }
00253 
00254 /*@-exportheadervar@*/
00255 /*@unchecked@*/
00256 int _noDirTokens = 0;
00257 /*@=exportheadervar@*/
00258 
00259 /*@-boundsread@*/
00260 static int dncmp(const void * a, const void * b)
00261         /*@*/
00262 {
00263     const char *const * first = a;
00264     const char *const * second = b;
00265     return strcmp(*first, *second);
00266 }
00267 /*@=boundsread@*/
00268 
00269 /*@-bounds@*/
00270 void compressFilelist(Header h)
00271 {
00272     HGE_t hge = (HGE_t)headerGetEntryMinMemory;
00273     HAE_t hae = (HAE_t)headerAddEntry;
00274     HRE_t hre = (HRE_t)headerRemoveEntry;
00275     HFD_t hfd = headerFreeData;
00276     char ** fileNames;
00277     const char ** dirNames;
00278     const char ** baseNames;
00279     int_32 * dirIndexes;
00280     rpmTagType fnt;
00281     int count;
00282     int i, xx;
00283     int dirIndex = -1;
00284 
00285     /*
00286      * This assumes the file list is already sorted, and begins with a
00287      * single '/'. That assumption isn't critical, but it makes things go
00288      * a bit faster.
00289      */
00290 
00291     if (headerIsEntry(h, RPMTAG_DIRNAMES)) {
00292         xx = hre(h, RPMTAG_OLDFILENAMES);
00293         return;         /* Already converted. */
00294     }
00295 
00296     if (!hge(h, RPMTAG_OLDFILENAMES, &fnt, (void **) &fileNames, &count))
00297         return;         /* no file list */
00298     if (fileNames == NULL || count <= 0)
00299         return;
00300 
00301     dirNames = xmalloc(sizeof(*dirNames) * count);      /* worst case */
00302     baseNames = xmalloc(sizeof(*dirNames) * count);
00303     dirIndexes = xmalloc(sizeof(*dirIndexes) * count);
00304 
00305     if (fileNames[0][0] != '/') {
00306         /* HACK. Source RPM, so just do things differently */
00307         dirIndex = 0;
00308         dirNames[dirIndex] = "";
00309         for (i = 0; i < count; i++) {
00310             dirIndexes[i] = dirIndex;
00311             baseNames[i] = fileNames[i];
00312         }
00313         goto exit;
00314     }
00315 
00316     /*@-branchstate@*/
00317     for (i = 0; i < count; i++) {
00318         const char ** needle;
00319         char savechar;
00320         char * baseName;
00321         int len;
00322 
00323         if (fileNames[i] == NULL)       /* XXX can't happen */
00324             continue;
00325         baseName = strrchr(fileNames[i], '/') + 1;
00326         len = baseName - fileNames[i];
00327         needle = dirNames;
00328         savechar = *baseName;
00329         *baseName = '\0';
00330 /*@-compdef@*/
00331         if (dirIndex < 0 ||
00332             (needle = bsearch(&fileNames[i], dirNames, dirIndex + 1, sizeof(dirNames[0]), dncmp)) == NULL) {
00333             char *s = alloca(len + 1);
00334             memcpy(s, fileNames[i], len + 1);
00335             s[len] = '\0';
00336             dirIndexes[i] = ++dirIndex;
00337             dirNames[dirIndex] = s;
00338         } else
00339             dirIndexes[i] = needle - dirNames;
00340 /*@=compdef@*/
00341 
00342         *baseName = savechar;
00343         baseNames[i] = baseName;
00344     }
00345     /*@=branchstate@*/
00346 
00347 exit:
00348     if (count > 0) {
00349         xx = hae(h, RPMTAG_DIRINDEXES, RPM_INT32_TYPE, dirIndexes, count);
00350         xx = hae(h, RPMTAG_BASENAMES, RPM_STRING_ARRAY_TYPE,
00351                         baseNames, count);
00352         xx = hae(h, RPMTAG_DIRNAMES, RPM_STRING_ARRAY_TYPE,
00353                         dirNames, dirIndex + 1);
00354     }
00355 
00356     fileNames = hfd(fileNames, fnt);
00357     free(dirNames);
00358     free(baseNames);
00359     free(dirIndexes);
00360 
00361     xx = hre(h, RPMTAG_OLDFILENAMES);
00362 }
00363 /*@=bounds@*/
00364 
00365 void rpmfiBuildFNames(Header h, rpmTag tagN,
00366         /*@out@*/ const char *** fnp, /*@out@*/ int * fcp)
00367 {
00368     HGE_t hge = (HGE_t)headerGetEntryMinMemory;
00369     HFD_t hfd = headerFreeData;
00370     const char ** baseNames;
00371     const char ** dirNames;
00372     int * dirIndexes;
00373     int count;
00374     const char ** fileNames;
00375     int size;
00376     rpmTag dirNameTag = 0;
00377     rpmTag dirIndexesTag = 0;
00378     rpmTagType bnt, dnt;
00379     char * t;
00380     int i, xx;
00381 
00382     if (tagN == RPMTAG_BASENAMES) {
00383         dirNameTag = RPMTAG_DIRNAMES;
00384         dirIndexesTag = RPMTAG_DIRINDEXES;
00385     } else if (tagN == RPMTAG_ORIGBASENAMES) {
00386         dirNameTag = RPMTAG_ORIGDIRNAMES;
00387         dirIndexesTag = RPMTAG_ORIGDIRINDEXES;
00388     }
00389 
00390     if (!hge(h, tagN, &bnt, (void **) &baseNames, &count)) {
00391         if (fnp) *fnp = NULL;
00392         if (fcp) *fcp = 0;
00393         return;         /* no file list */
00394     }
00395 
00396     xx = hge(h, dirNameTag, &dnt, (void **) &dirNames, NULL);
00397     xx = hge(h, dirIndexesTag, NULL, (void **) &dirIndexes, &count);
00398 
00399     size = sizeof(*fileNames) * count;
00400     for (i = 0; i < count; i++)
00401         size += strlen(baseNames[i]) + strlen(dirNames[dirIndexes[i]]) + 1;
00402 
00403     fileNames = xmalloc(size);
00404     t = ((char *) fileNames) + (sizeof(*fileNames) * count);
00405     /*@-branchstate@*/
00406     for (i = 0; i < count; i++) {
00407         fileNames[i] = t;
00408         t = stpcpy( stpcpy(t, dirNames[dirIndexes[i]]), baseNames[i]);
00409         *t++ = '\0';
00410     }
00411     /*@=branchstate@*/
00412     baseNames = hfd(baseNames, bnt);
00413     dirNames = hfd(dirNames, dnt);
00414 
00415     /*@-branchstate@*/
00416     if (fnp)
00417         *fnp = fileNames;
00418     else
00419         fileNames = _free(fileNames);
00420     /*@=branchstate@*/
00421     if (fcp) *fcp = count;
00422 }
00423 
00424 void expandFilelist(Header h)
00425 {
00426     HAE_t hae = (HAE_t)headerAddEntry;
00427     HRE_t hre = (HRE_t)headerRemoveEntry;
00428     const char ** fileNames = NULL;
00429     int count = 0;
00430     int xx;
00431 
00432     /*@-branchstate@*/
00433     if (!headerIsEntry(h, RPMTAG_OLDFILENAMES)) {
00434         rpmfiBuildFNames(h, RPMTAG_BASENAMES, &fileNames, &count);
00435         if (fileNames == NULL || count <= 0)
00436             return;
00437         xx = hae(h, RPMTAG_OLDFILENAMES, RPM_STRING_ARRAY_TYPE,
00438                         fileNames, count);
00439         fileNames = _free(fileNames);
00440     }
00441     /*@=branchstate@*/
00442 
00443     xx = hre(h, RPMTAG_DIRNAMES);
00444     xx = hre(h, RPMTAG_BASENAMES);
00445     xx = hre(h, RPMTAG_DIRINDEXES);
00446 }
00447 
00448 /*
00449  * Up to rpm 3.0.4, packages implicitly provided their own name-version-release.
00450  * Retrofit an explicit "Provides: name = epoch:version-release.
00451  */
00452 void providePackageNVR(Header h)
00453 {
00454     HGE_t hge = (HGE_t)headerGetEntryMinMemory;
00455     HFD_t hfd = headerFreeData;
00456     const char *name, *version, *release;
00457     int_32 * epoch;
00458     const char *pEVR;
00459     char *p;
00460     int_32 pFlags = RPMSENSE_EQUAL;
00461     const char ** provides = NULL;
00462     const char ** providesEVR = NULL;
00463     rpmTagType pnt, pvt;
00464     int_32 * provideFlags = NULL;
00465     int providesCount;
00466     int i, xx;
00467     int bingo = 1;
00468 
00469     /* Generate provides for this package name-version-release. */
00470     xx = headerNVR(h, &name, &version, &release);
00471     if (!(name && version && release))
00472         return;
00473     pEVR = p = alloca(21 + strlen(version) + 1 + strlen(release) + 1);
00474     *p = '\0';
00475     if (hge(h, RPMTAG_EPOCH, NULL, (void **) &epoch, NULL)) {
00476         sprintf(p, "%d:", *epoch);
00477         while (*p != '\0')
00478             p++;
00479     }
00480     (void) stpcpy( stpcpy( stpcpy(p, version) , "-") , release);
00481 
00482     /*
00483      * Rpm prior to 3.0.3 does not have versioned provides.
00484      * If no provides at all are available, we can just add.
00485      */
00486     if (!hge(h, RPMTAG_PROVIDENAME, &pnt, (void **) &provides, &providesCount))
00487         goto exit;
00488 
00489     /*
00490      * Otherwise, fill in entries on legacy packages.
00491      */
00492     if (!hge(h, RPMTAG_PROVIDEVERSION, &pvt, (void **) &providesEVR, NULL)) {
00493         for (i = 0; i < providesCount; i++) {
00494             char * vdummy = "";
00495             int_32 fdummy = RPMSENSE_ANY;
00496             xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
00497                         &vdummy, 1);
00498             xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
00499                         &fdummy, 1);
00500         }
00501         goto exit;
00502     }
00503 
00504     xx = hge(h, RPMTAG_PROVIDEFLAGS, NULL, (void **) &provideFlags, NULL);
00505 
00506     /*@-nullderef@*/    /* LCL: providesEVR is not NULL */
00507     if (provides && providesEVR && provideFlags)
00508     for (i = 0; i < providesCount; i++) {
00509         if (!(provides[i] && providesEVR[i]))
00510             continue;
00511         if (!(provideFlags[i] == RPMSENSE_EQUAL &&
00512             !strcmp(name, provides[i]) && !strcmp(pEVR, providesEVR[i])))
00513             continue;
00514         bingo = 0;
00515         break;
00516     }
00517     /*@=nullderef@*/
00518 
00519 exit:
00520     provides = hfd(provides, pnt);
00521     providesEVR = hfd(providesEVR, pvt);
00522 
00523     if (bingo) {
00524         xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDENAME, RPM_STRING_ARRAY_TYPE,
00525                 &name, 1);
00526         xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
00527                 &pFlags, 1);
00528         xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
00529                 &pEVR, 1);
00530     }
00531 }
00532 
00533 void legacyRetrofit(Header h, const struct rpmlead * lead)
00534 {
00535     const char * prefix;
00536 
00537     /*
00538      * We don't use these entries (and rpm >= 2 never has) and they are
00539      * pretty misleading. Let's just get rid of them so they don't confuse
00540      * anyone.
00541      */
00542     if (headerIsEntry(h, RPMTAG_FILEUSERNAME))
00543         (void) headerRemoveEntry(h, RPMTAG_FILEUIDS);
00544     if (headerIsEntry(h, RPMTAG_FILEGROUPNAME))
00545         (void) headerRemoveEntry(h, RPMTAG_FILEGIDS);
00546 
00547     /*
00548      * We switched the way we do relocatable packages. We fix some of
00549      * it up here, though the install code still has to be a bit 
00550      * careful. This fixup makes queries give the new values though,
00551      * which is quite handy.
00552      */
00553     /*@=branchstate@*/
00554     if (headerGetEntry(h, RPMTAG_DEFAULTPREFIX, NULL, (void **) &prefix, NULL))
00555     {
00556         const char * nprefix = stripTrailingChar(alloca_strdup(prefix), '/');
00557         (void) headerAddEntry(h, RPMTAG_PREFIXES, RPM_STRING_ARRAY_TYPE,
00558                 &nprefix, 1); 
00559     }
00560     /*@=branchstate@*/
00561 
00562     /*
00563      * The file list was moved to a more compressed format which not
00564      * only saves memory (nice), but gives fingerprinting a nice, fat
00565      * speed boost (very nice). Go ahead and convert old headers to
00566      * the new style (this is a noop for new headers).
00567      */
00568     if (lead->major < 4)
00569         compressFilelist(h);
00570 
00571     /* XXX binary rpms always have RPMTAG_SOURCERPM, source rpms do not */
00572     if (lead->type == RPMLEAD_SOURCE) {
00573         int_32 one = 1;
00574         if (!headerIsEntry(h, RPMTAG_SOURCEPACKAGE))
00575             (void) headerAddEntry(h, RPMTAG_SOURCEPACKAGE, RPM_INT32_TYPE,
00576                                 &one, 1);
00577     } else if (lead->major < 4) {
00578         /* Retrofit "Provide: name = EVR" for binary packages. */
00579         providePackageNVR(h);
00580     }
00581 }

Generated on Thu May 14 13:59:25 2009 for rpm by  doxygen 1.4.7