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, pid_t * pidp, size_t *fsizep)
00037
00038
00039
00040 {
00041
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
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
00061
00062 fdno = open(path, O_RDONLY);
00063 if (fdno < 0)
00064 return fdno;
00065
00066
00067 if (!(cmd && *cmd))
00068 return fdno;
00069
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
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
00089
00090 bingo = 0;
00091
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 continue;
00104 bingo = 1;
00105 break;
00106 }
00107 }
00108 }
00109
00110
00111
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
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
00163 fdno = open_dso(path, &pid, &fsize);
00164
00165 if (fdno < 0) {
00166 rc = 1;
00167 goto exit;
00168 }
00169
00170
00171 if (fsize > (size_t) 32*1024*1024)
00172 if (ut == URL_IS_PATH || ut == URL_IS_UNKNOWN)
00173 ut = URL_IS_DASH;
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 }
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
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
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
00244 if (fsizep)
00245 *fsizep = fsize;
00246 if (!rc)
00247 memcpy(digest, md5sum, md5len);
00248
00249 md5sum = _free(md5sum);
00250
00251 return rc;
00252 }
00253
00254
00255
00256 int _noDirTokens = 0;
00257
00258
00259
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
00268
00269
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
00287
00288
00289
00290
00291 if (headerIsEntry(h, RPMTAG_DIRNAMES)) {
00292 xx = hre(h, RPMTAG_OLDFILENAMES);
00293 return;
00294 }
00295
00296 if (!hge(h, RPMTAG_OLDFILENAMES, &fnt, (void **) &fileNames, &count))
00297 return;
00298 if (fileNames == NULL || count <= 0)
00299 return;
00300
00301 dirNames = alloca(sizeof(*dirNames) * count);
00302 baseNames = alloca(sizeof(*dirNames) * count);
00303 dirIndexes = alloca(sizeof(*dirIndexes) * count);
00304
00305 if (fileNames[0][0] != '/') {
00306
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
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)
00324 continue;
00325 baseName = strrchr(fileNames[i], '/') + 1;
00326 len = baseName - fileNames[i];
00327 needle = dirNames;
00328 savechar = *baseName;
00329 *baseName = '\0';
00330
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
00341
00342 *baseName = savechar;
00343 baseNames[i] = baseName;
00344 }
00345
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
00358 xx = hre(h, RPMTAG_OLDFILENAMES);
00359 }
00360
00361
00362 void rpmfiBuildFNames(Header h, rpmTag tagN,
00363 const char *** fnp, int * fcp)
00364 {
00365 HGE_t hge = (HGE_t)headerGetEntryMinMemory;
00366 HFD_t hfd = headerFreeData;
00367 const char ** baseNames;
00368 const char ** dirNames;
00369 int * dirIndexes;
00370 int count;
00371 const char ** fileNames;
00372 int size;
00373 rpmTag dirNameTag = 0;
00374 rpmTag dirIndexesTag = 0;
00375 rpmTagType bnt, dnt;
00376 char * t;
00377 int i, xx;
00378
00379 if (tagN == RPMTAG_BASENAMES) {
00380 dirNameTag = RPMTAG_DIRNAMES;
00381 dirIndexesTag = RPMTAG_DIRINDEXES;
00382 } else if (tagN == RPMTAG_ORIGBASENAMES) {
00383 dirNameTag = RPMTAG_ORIGDIRNAMES;
00384 dirIndexesTag = RPMTAG_ORIGDIRINDEXES;
00385 }
00386
00387 if (!hge(h, tagN, &bnt, (void **) &baseNames, &count)) {
00388 if (fnp) *fnp = NULL;
00389 if (fcp) *fcp = 0;
00390 return;
00391 }
00392
00393 xx = hge(h, dirNameTag, &dnt, (void **) &dirNames, NULL);
00394 xx = hge(h, dirIndexesTag, NULL, (void **) &dirIndexes, &count);
00395
00396 size = sizeof(*fileNames) * count;
00397 for (i = 0; i < count; i++)
00398 size += strlen(baseNames[i]) + strlen(dirNames[dirIndexes[i]]) + 1;
00399
00400 fileNames = xmalloc(size);
00401 t = ((char *) fileNames) + (sizeof(*fileNames) * count);
00402
00403 for (i = 0; i < count; i++) {
00404 fileNames[i] = t;
00405 t = stpcpy( stpcpy(t, dirNames[dirIndexes[i]]), baseNames[i]);
00406 *t++ = '\0';
00407 }
00408
00409 baseNames = hfd(baseNames, bnt);
00410 dirNames = hfd(dirNames, dnt);
00411
00412
00413 if (fnp)
00414 *fnp = fileNames;
00415 else
00416 fileNames = _free(fileNames);
00417
00418 if (fcp) *fcp = count;
00419 }
00420
00421 void expandFilelist(Header h)
00422 {
00423 HAE_t hae = (HAE_t)headerAddEntry;
00424 HRE_t hre = (HRE_t)headerRemoveEntry;
00425 const char ** fileNames = NULL;
00426 int count = 0;
00427 int xx;
00428
00429
00430 if (!headerIsEntry(h, RPMTAG_OLDFILENAMES)) {
00431 rpmfiBuildFNames(h, RPMTAG_BASENAMES, &fileNames, &count);
00432 if (fileNames == NULL || count <= 0)
00433 return;
00434 xx = hae(h, RPMTAG_OLDFILENAMES, RPM_STRING_ARRAY_TYPE,
00435 fileNames, count);
00436 fileNames = _free(fileNames);
00437 }
00438
00439
00440 xx = hre(h, RPMTAG_DIRNAMES);
00441 xx = hre(h, RPMTAG_BASENAMES);
00442 xx = hre(h, RPMTAG_DIRINDEXES);
00443 }
00444
00445
00446
00447
00448
00449 void providePackageNVR(Header h)
00450 {
00451 HGE_t hge = (HGE_t)headerGetEntryMinMemory;
00452 HFD_t hfd = headerFreeData;
00453 const char *name, *version, *release;
00454 int_32 * epoch;
00455 const char *pEVR;
00456 char *p;
00457 int_32 pFlags = RPMSENSE_EQUAL;
00458 const char ** provides = NULL;
00459 const char ** providesEVR = NULL;
00460 rpmTagType pnt, pvt;
00461 int_32 * provideFlags = NULL;
00462 int providesCount;
00463 int i, xx;
00464 int bingo = 1;
00465
00466
00467 xx = headerNVR(h, &name, &version, &release);
00468 if (!(name && version && release))
00469 return;
00470 pEVR = p = alloca(21 + strlen(version) + 1 + strlen(release) + 1);
00471 *p = '\0';
00472 if (hge(h, RPMTAG_EPOCH, NULL, (void **) &epoch, NULL)) {
00473 sprintf(p, "%d:", *epoch);
00474 while (*p != '\0')
00475 p++;
00476 }
00477 (void) stpcpy( stpcpy( stpcpy(p, version) , "-") , release);
00478
00479
00480
00481
00482
00483 if (!hge(h, RPMTAG_PROVIDENAME, &pnt, (void **) &provides, &providesCount))
00484 goto exit;
00485
00486
00487
00488
00489 if (!hge(h, RPMTAG_PROVIDEVERSION, &pvt, (void **) &providesEVR, NULL)) {
00490 for (i = 0; i < providesCount; i++) {
00491 char * vdummy = "";
00492 int_32 fdummy = RPMSENSE_ANY;
00493 xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
00494 &vdummy, 1);
00495 xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
00496 &fdummy, 1);
00497 }
00498 goto exit;
00499 }
00500
00501 xx = hge(h, RPMTAG_PROVIDEFLAGS, NULL, (void **) &provideFlags, NULL);
00502
00503
00504 if (provides && providesEVR && provideFlags)
00505 for (i = 0; i < providesCount; i++) {
00506 if (!(provides[i] && providesEVR[i]))
00507 continue;
00508 if (!(provideFlags[i] == RPMSENSE_EQUAL &&
00509 !strcmp(name, provides[i]) && !strcmp(pEVR, providesEVR[i])))
00510 continue;
00511 bingo = 0;
00512 break;
00513 }
00514
00515
00516 exit:
00517 provides = hfd(provides, pnt);
00518 providesEVR = hfd(providesEVR, pvt);
00519
00520 if (bingo) {
00521 xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDENAME, RPM_STRING_ARRAY_TYPE,
00522 &name, 1);
00523 xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEFLAGS, RPM_INT32_TYPE,
00524 &pFlags, 1);
00525 xx = headerAddOrAppendEntry(h, RPMTAG_PROVIDEVERSION, RPM_STRING_ARRAY_TYPE,
00526 &pEVR, 1);
00527 }
00528 }
00529
00530 void legacyRetrofit(Header h, const struct rpmlead * lead)
00531 {
00532 const char * prefix;
00533
00534
00535
00536
00537
00538
00539 if (headerIsEntry(h, RPMTAG_FILEUSERNAME))
00540 (void) headerRemoveEntry(h, RPMTAG_FILEUIDS);
00541 if (headerIsEntry(h, RPMTAG_FILEGROUPNAME))
00542 (void) headerRemoveEntry(h, RPMTAG_FILEGIDS);
00543
00544
00545
00546
00547
00548
00549
00550
00551 if (headerGetEntry(h, RPMTAG_DEFAULTPREFIX, NULL, (void **) &prefix, NULL))
00552 {
00553 const char * nprefix = stripTrailingChar(alloca_strdup(prefix), '/');
00554 (void) headerAddEntry(h, RPMTAG_PREFIXES, RPM_STRING_ARRAY_TYPE,
00555 &nprefix, 1);
00556 }
00557
00558
00559
00560
00561
00562
00563
00564
00565 if (lead->major < 4)
00566 compressFilelist(h);
00567
00568
00569 if (lead->type == RPMLEAD_SOURCE) {
00570 int_32 one = 1;
00571 if (!headerIsEntry(h, RPMTAG_SOURCEPACKAGE))
00572 (void) headerAddEntry(h, RPMTAG_SOURCEPACKAGE, RPM_INT32_TYPE,
00573 &one, 1);
00574 } else if (lead->major < 4) {
00575
00576 providePackageNVR(h);
00577 }
00578 }