librazor/rpm.c
author J. Ali Harlow <ali@juiblex.co.uk>
Mon Mar 05 09:43:14 2018 +0000 (2018-03-05)
changeset 493 06f70d662e39
parent 475 008c75a5e08d
permissions -rw-r--r--
Add a test for relocations that are valid paths but would be invalid URIs if mis-interpreted
     1 /*
     2  * Copyright (C) 2008  Kristian Høgsberg <krh@redhat.com>
     3  * Copyright (C) 2008  Red Hat, Inc
     4  * Copyright (C) 2009, 2011, 2012, 2014, 2016  J. Ali Harlow <ali@juiblex.co.uk>
     5  *
     6  * This program is free software; you can redistribute it and/or modify
     7  * it under the terms of the GNU General Public License as published by
     8  * the Free Software Foundation; either version 2 of the License, or
     9  * (at your option) any later version.
    10  *
    11  * This program is distributed in the hope that it will be useful,
    12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
    13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    14  * GNU General Public License for more details.
    15  *
    16  * You should have received a copy of the GNU General Public License along
    17  * with this program; if not, write to the Free Software Foundation, Inc.,
    18  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
    19  */
    20 
    21 #include "config.h"
    22 
    23 #include <stdio.h>
    24 #include <stddef.h>
    25 #include <stdlib.h>
    26 #include <string.h>
    27 #include <errno.h>
    28 #if HAVE_SYS_WAIT_H
    29 #include <sys/wait.h>
    30 #endif
    31 #include <fcntl.h>
    32 #include <unistd.h>
    33 #if MSWIN_API
    34 #include <winsock2.h>	/* For ntohl() */
    35 #else
    36 #include <arpa/inet.h>
    37 #endif
    38 #include <limits.h>
    39 #include <zlib.h>
    40 #include <assert.h>
    41 
    42 #include "razor.h"
    43 #include "razor-internal.h"
    44 
    45 #ifndef O_BINARY
    46 #define O_BINARY	0
    47 #endif
    48 
    49 struct rpm_lead {
    50 	unsigned char magic[4];
    51 	unsigned char major, minor;
    52 	short type;
    53 	short archnum;
    54 	char name[66];
    55 	short osnum;
    56 	short signature_type;
    57 	char reserved[16];
    58 };
    59 
    60 #define	RPM_LEAD_SIZE sizeof(struct rpm_lead)
    61 #define	RPM_LEAD_MAGIC "\xED\xAB\xEE\xDB"
    62 
    63 enum {
    64     PIPE	=  1,	/*!< pipe/fifo */
    65     CDEV	=  2,	/*!< character device */
    66     XDIR	=  4,	/*!< directory */
    67     BDEV	=  6,	/*!< block device */
    68     REG		=  8,	/*!< regular file */
    69     LINK	= 10,	/*!< hard link */
    70     SOCK	= 12	/*!< socket */
    71 };
    72 
    73 enum {
    74     RPMSENSE_LESS		= 1 << 1,
    75     RPMSENSE_GREATER		= 1 << 2,
    76     RPMSENSE_EQUAL		= 1 << 3,
    77     RPMSENSE_PREREQ		= 1 << 6,
    78     RPMSENSE_SCRIPT_PRE		= 1 << 9,
    79     RPMSENSE_SCRIPT_POST	= 1 << 10,
    80     RPMSENSE_SCRIPT_PREUN	= 1 << 11,
    81     RPMSENSE_SCRIPT_POSTUN	= 1 << 12,
    82 };
    83 
    84 enum {
    85     RPMTAG_NAME  		= 1000,	/* s */
    86     RPMTAG_VERSION		= 1001,	/* s */
    87     RPMTAG_RELEASE		= 1002,	/* s */
    88     RPMTAG_EPOCH   		= 1003,	/* i */
    89     RPMTAG_SUMMARY		= 1004,	/* s{} */
    90     RPMTAG_DESCRIPTION		= 1005,	/* s{} */
    91     RPMTAG_BUILDTIME		= 1006,	/* i */
    92     RPMTAG_BUILDHOST		= 1007,	/* s */
    93     RPMTAG_INSTALLTIME		= 1008,	/* i */
    94     RPMTAG_SIZE			= 1009,	/* i */
    95     RPMTAG_DISTRIBUTION		= 1010,	/* s */
    96     RPMTAG_VENDOR		= 1011,	/* s */
    97     RPMTAG_GIF			= 1012,	/* x */
    98     RPMTAG_XPM			= 1013,	/* x */
    99     RPMTAG_LICENSE		= 1014,	/* s */
   100     RPMTAG_PACKAGER		= 1015,	/* s */
   101     RPMTAG_GROUP		= 1016,	/* s{} */
   102     RPMTAG_CHANGELOG		= 1017, /*!< s[] internal */
   103     RPMTAG_SOURCE		= 1018,	/* s[] */
   104     RPMTAG_PATCH		= 1019,	/* s[] */
   105     RPMTAG_URL			= 1020,	/* s */
   106     RPMTAG_OS			= 1021,	/* s legacy used int */
   107     RPMTAG_ARCH			= 1022,	/* s legacy used int */
   108     RPMTAG_PREIN		= 1023,	/* s */
   109     RPMTAG_POSTIN		= 1024,	/* s */
   110     RPMTAG_PREUN		= 1025,	/* s */
   111     RPMTAG_POSTUN		= 1026,	/* s */
   112     RPMTAG_OLDFILENAMES		= 1027, /* s[] obsolete */
   113     RPMTAG_FILESIZES		= 1028,	/* i */
   114     RPMTAG_FILESTATES		= 1029, /* c */
   115     RPMTAG_FILEMODES		= 1030,	/* h */
   116     RPMTAG_FILEUIDS		= 1031, /*!< internal */
   117     RPMTAG_FILEGIDS		= 1032, /*!< internal */
   118     RPMTAG_FILERDEVS		= 1033,	/* h */
   119     RPMTAG_FILEMTIMES		= 1034, /* i */
   120     RPMTAG_FILEMD5S		= 1035,	/* s[] */
   121     RPMTAG_FILELINKTOS		= 1036,	/* s[] */
   122     RPMTAG_FILEFLAGS		= 1037,	/* i */
   123     RPMTAG_ROOT			= 1038, /*!< internal - obsolete */
   124     RPMTAG_FILEUSERNAME		= 1039,	/* s[] */
   125     RPMTAG_FILEGROUPNAME	= 1040,	/* s[] */
   126     RPMTAG_EXCLUDE		= 1041, /*!< internal - obsolete */
   127     RPMTAG_EXCLUSIVE		= 1042, /*!< internal - obsolete */
   128     RPMTAG_ICON			= 1043,
   129     RPMTAG_SOURCERPM		= 1044,	/* s */
   130     RPMTAG_FILEVERIFYFLAGS	= 1045,	/* i */
   131     RPMTAG_ARCHIVESIZE		= 1046,	/* i */
   132     RPMTAG_PROVIDENAME		= 1047,	/* s[] */
   133     RPMTAG_REQUIREFLAGS		= 1048,	/* i */
   134     RPMTAG_REQUIRENAME		= 1049,	/* s[] */
   135     RPMTAG_REQUIREVERSION	= 1050,	/* s[] */
   136     RPMTAG_NOSOURCE		= 1051, /*!< internal */
   137     RPMTAG_NOPATCH		= 1052, /*!< internal */
   138     RPMTAG_CONFLICTFLAGS	= 1053, /* i */
   139     RPMTAG_CONFLICTNAME		= 1054,	/* s[] */
   140     RPMTAG_CONFLICTVERSION	= 1055,	/* s[] */
   141     RPMTAG_DEFAULTPREFIX	= 1056, /*!< internal - deprecated */
   142     RPMTAG_BUILDROOT		= 1057, /*!< internal */
   143     RPMTAG_INSTALLPREFIX	= 1058, /*!< internal - deprecated */
   144     RPMTAG_EXCLUDEARCH		= 1059,
   145     RPMTAG_EXCLUDEOS		= 1060,
   146     RPMTAG_EXCLUSIVEARCH	= 1061,
   147     RPMTAG_EXCLUSIVEOS		= 1062,
   148     RPMTAG_AUTOREQPROV		= 1063, /*!< internal */
   149     RPMTAG_RPMVERSION		= 1064,	/* s */
   150     RPMTAG_TRIGGERSCRIPTS	= 1065,	/* s[] */
   151     RPMTAG_TRIGGERNAME		= 1066,	/* s[] */
   152     RPMTAG_TRIGGERVERSION	= 1067,	/* s[] */
   153     RPMTAG_TRIGGERFLAGS		= 1068,	/* i */
   154     RPMTAG_TRIGGERINDEX		= 1069,	/* i */
   155     RPMTAG_VERIFYSCRIPT		= 1079,	/* s */
   156     RPMTAG_CHANGELOGTIME	= 1080,	/* i */
   157     RPMTAG_CHANGELOGNAME	= 1081,	/* s[] */
   158     RPMTAG_CHANGELOGTEXT	= 1082,	/* s[] */
   159     RPMTAG_BROKENMD5		= 1083, /*!< internal - obsolete */
   160     RPMTAG_PREREQ		= 1084, /*!< internal */
   161     RPMTAG_PREINPROG		= 1085,	/* s */
   162     RPMTAG_POSTINPROG		= 1086,	/* s */
   163     RPMTAG_PREUNPROG		= 1087,	/* s */
   164     RPMTAG_POSTUNPROG		= 1088,	/* s */
   165     RPMTAG_BUILDARCHS		= 1089,
   166     RPMTAG_OBSOLETENAME		= 1090,	/* s[] */
   167     RPMTAG_VERIFYSCRIPTPROG	= 1091,	/* s */
   168     RPMTAG_TRIGGERSCRIPTPROG	= 1092,	/* s */
   169     RPMTAG_DOCDIR		= 1093, /*!< internal */
   170     RPMTAG_COOKIE		= 1094,	/* s */
   171     RPMTAG_FILEDEVICES		= 1095,	/* i */
   172     RPMTAG_FILEINODES		= 1096,	/* i */
   173     RPMTAG_FILELANGS		= 1097,	/* s[] */
   174     RPMTAG_PREFIXES		= 1098,	/* s[] */
   175     RPMTAG_INSTPREFIXES		= 1099,	/* s[] */
   176     RPMTAG_TRIGGERIN		= 1100, /*!< internal */
   177     RPMTAG_TRIGGERUN		= 1101, /*!< internal */
   178     RPMTAG_TRIGGERPOSTUN	= 1102, /*!< internal */
   179     RPMTAG_AUTOREQ		= 1103, /*!< internal */
   180     RPMTAG_AUTOPROV		= 1104, /*!< internal */
   181     RPMTAG_CAPABILITY		= 1105, /*!< internal - obsolete */
   182     RPMTAG_SOURCEPACKAGE	= 1106, /*!< i src.rpm header marker */
   183     RPMTAG_OLDORIGFILENAMES	= 1107, /*!< internal - obsolete */
   184     RPMTAG_BUILDPREREQ		= 1108, /*!< internal */
   185     RPMTAG_BUILDREQUIRES	= 1109, /*!< internal */
   186     RPMTAG_BUILDCONFLICTS	= 1110, /*!< internal */
   187     RPMTAG_BUILDMACROS		= 1111, /*!< internal - unused */
   188     RPMTAG_PROVIDEFLAGS		= 1112,	/* i */
   189     RPMTAG_PROVIDEVERSION	= 1113,	/* s[] */
   190     RPMTAG_OBSOLETEFLAGS	= 1114,	/* i */
   191     RPMTAG_OBSOLETEVERSION	= 1115,	/* s[] */
   192     RPMTAG_DIRINDEXES		= 1116,	/* i */
   193     RPMTAG_BASENAMES		= 1117,	/* s[] */
   194     RPMTAG_DIRNAMES		= 1118,	/* s[] */
   195     RPMTAG_ORIGDIRINDEXES	= 1119, /*!< internal */
   196     RPMTAG_ORIGBASENAMES	= 1120, /*!< internal */
   197     RPMTAG_ORIGDIRNAMES		= 1121, /*!< internal */
   198     RPMTAG_OPTFLAGS		= 1122,	/* s */
   199     RPMTAG_DISTURL		= 1123,	/* s */
   200     RPMTAG_PAYLOADFORMAT	= 1124,	/* s */
   201     RPMTAG_PAYLOADCOMPRESSOR	= 1125,	/* s */
   202     RPMTAG_PAYLOADFLAGS		= 1126,	/* s */
   203     RPMTAG_INSTALLCOLOR		= 1127, /*!< i transaction color when installed */
   204     RPMTAG_INSTALLTID		= 1128,	/* i */
   205     RPMTAG_REMOVETID		= 1129,	/* i */
   206     RPMTAG_SHA1RHN		= 1130, /*!< internal - obsolete */
   207     RPMTAG_RHNPLATFORM		= 1131,	/* s */
   208     RPMTAG_PLATFORM		= 1132,	/* s */
   209     RPMTAG_PATCHESNAME		= 1133, /*!< placeholder (SuSE) */
   210     RPMTAG_PATCHESFLAGS		= 1134, /*!< placeholder (SuSE) */
   211     RPMTAG_PATCHESVERSION	= 1135, /*!< placeholder (SuSE) */
   212     RPMTAG_CACHECTIME		= 1136,	/* i */
   213     RPMTAG_CACHEPKGPATH		= 1137,	/* s */
   214     RPMTAG_CACHEPKGSIZE		= 1138,	/* i */
   215     RPMTAG_CACHEPKGMTIME	= 1139,	/* i */
   216     RPMTAG_FILECOLORS		= 1140,	/* i */
   217     RPMTAG_FILECLASS		= 1141,	/* i */
   218     RPMTAG_CLASSDICT		= 1142,	/* s[] */
   219     RPMTAG_FILEDEPENDSX		= 1143,	/* i */
   220     RPMTAG_FILEDEPENDSN		= 1144,	/* i */
   221     RPMTAG_DEPENDSDICT		= 1145,	/* i */
   222     RPMTAG_SOURCEPKGID		= 1146,	/* x */
   223     RPMTAG_FILECONTEXTS		= 1147,	/* s[] */
   224     RPMTAG_FSCONTEXTS		= 1148,	/*!< s[] extension */
   225     RPMTAG_RECONTEXTS		= 1149,	/*!< s[] extension */
   226     RPMTAG_POLICIES		= 1150,	/*!< s[] selinux *.te policy file. */
   227     RPMTAG_PRETRANS		= 1151,	/* s */
   228     RPMTAG_POSTTRANS		= 1152,	/* s */
   229     RPMTAG_PRETRANSPROG		= 1153,	/* s */
   230     RPMTAG_POSTTRANSPROG	= 1154,	/* s */
   231     RPMTAG_DISTTAG		= 1155,	/* s */
   232     RPMTAG_SUGGESTSNAME		= 1156,	/* s[] extension placeholder */
   233     RPMTAG_SUGGESTSVERSION	= 1157,	/* s[] extension placeholder */
   234     RPMTAG_SUGGESTSFLAGS	= 1158,	/* i   extension placeholder */
   235     RPMTAG_ENHANCESNAME		= 1159,	/* s[] extension placeholder */
   236     RPMTAG_ENHANCESVERSION	= 1160,	/* s[] extension placeholder */
   237     RPMTAG_ENHANCESFLAGS	= 1161,	/* i   extension placeholder */
   238     RPMTAG_PRIORITY		= 1162, /* i   extension placeholder */
   239     RPMTAG_CVSID		= 1163, /* s */
   240     RPMTAG_TRIGGERPREIN		= 1171, /*!< internal */
   241 };
   242 
   243 struct rpm_header {
   244 	unsigned char magic[4];
   245 	unsigned char reserved[4];
   246 	int nindex;
   247 	int hsize;
   248 };
   249 
   250 struct rpm_header_index {
   251 	int tag;
   252 	int type;
   253 	int offset;
   254 	int count;
   255 };
   256 
   257 struct razor_rpm {
   258 	struct rpm_header *signature;
   259 	struct rpm_header *header;
   260 	const char **dirs;
   261 	unsigned int n_prefixes;
   262 	const char **prefixes;
   263 	const char *pool;
   264 	void *map;
   265 	size_t size;
   266 	void *payload;
   267 	struct razor_relocations *relocations;
   268 	char *evr;
   269 };
   270 
   271 enum razor_relocation_flags {
   272 	RAZOR_RELOCATION_ACTIVE		= 1 << 0,
   273 };
   274 
   275 struct razor_relocation {
   276 	enum razor_relocation_flags flags;
   277 	size_t oldlen;
   278 	size_t newlen;
   279 	char *oldpath;
   280 	char *newpath;
   281 };
   282 
   283 struct razor_relocations {
   284 	/* Ordered such that if oldpath 1 starts with oldpath 2, then
   285 	 * oldpath 1 is listed first (ie., /usr/bin comes before /usr)
   286 	 * and terminated with a NULL oldpath.
   287 	 */
   288 	struct razor_relocation *relocations;
   289 	int n_relocations;
   290 	char *path;
   291 };
   292 
   293 RAZOR_EXPORT struct razor_relocations *razor_relocations_create(void)
   294 {
   295 	return calloc(1, sizeof(struct razor_relocations));
   296 }
   297 
   298 RAZOR_EXPORT void razor_relocations_add(struct razor_relocations *rr,
   299 			   const char *oldpath, const char *newpath)
   300 {
   301 	int i, found = 0;
   302 	size_t len;
   303 
   304 	if (newpath && !strcmp(oldpath, newpath))
   305 		newpath = NULL;
   306 
   307 	for (i = 0; i < rr->n_relocations; i++) {
   308 		len = rr->relocations[i].oldlen;
   309 		if (!strncmp(rr->relocations[i].oldpath, oldpath, len)) {
   310 			found = !strcmp(rr->relocations[i].oldpath, oldpath);
   311 			break;
   312 		}
   313 	}
   314 
   315 	if (!newpath) {
   316 		if (found) {
   317 			free(rr->relocations[i].oldpath);
   318 			free(rr->relocations[i].newpath);
   319 			do {
   320 				rr->relocations[i] = rr->relocations[i + 1];
   321 			} while (rr->relocations[++i].oldpath);
   322 		}
   323 		return;
   324 	}
   325 
   326 	if (found) {
   327 		free(rr->relocations[i].newpath);
   328 		rr->relocations[i].newpath = strdup(newpath);
   329 		rr->relocations[i].newlen = strlen(newpath);
   330 		return;
   331 	}
   332 
   333 	if (!rr->n_relocations++)
   334 		rr->relocations = calloc(1, sizeof *rr->relocations);
   335 	else {
   336 		rr->relocations = realloc(rr->relocations,
   337 					  rr->n_relocations * sizeof *rr->relocations);
   338 		memmove(rr->relocations + i + 1, rr->relocations + i,
   339 			(rr->n_relocations - i - 1) * sizeof *rr->relocations);
   340 	}
   341 
   342 	rr->relocations[i].flags = 0;
   343 	rr->relocations[i].oldpath = strdup(oldpath);
   344 	rr->relocations[i].newpath = strdup(newpath);
   345 	rr->relocations[i].oldlen = strlen(oldpath);
   346 	rr->relocations[i].newlen = strlen(newpath);
   347 }
   348 
   349 RAZOR_EXPORT void
   350 razor_relocations_set_rpm(struct razor_relocations *rr, struct razor_rpm *rpm)
   351 {
   352 	int i, j;
   353 
   354 	for (i = 0; i < rr->n_relocations; i++) {
   355 		rr->relocations[i].flags &= ~RAZOR_RELOCATION_ACTIVE;
   356 		for (j = 0; j < rpm->n_prefixes; j++)
   357 			if (!strcmp(rpm->prefixes[j],
   358 				    rr->relocations[i].oldpath)) {
   359 				rr->relocations[i].flags |= RAZOR_RELOCATION_ACTIVE;
   360 				break;
   361 			}
   362 	}
   363 }
   364 
   365 RAZOR_EXPORT const char *
   366 razor_relocations_apply(struct razor_relocations *rr, const char *path)
   367 {
   368 	int i;
   369 	size_t len;
   370 
   371 	for (i = 0; i < rr->n_relocations; i++)
   372 		if (rr->relocations[i].flags & RAZOR_RELOCATION_ACTIVE &&
   373 		    !strncmp(path, rr->relocations[i].oldpath,
   374 			     rr->relocations[i].oldlen))
   375 			break;
   376 
   377 	if (i < rr->n_relocations) {
   378 		free(rr->path);
   379 		len = strlen(path + rr->relocations[i].oldlen) +
   380 		      rr->relocations[i].newlen;
   381 		rr->path = malloc(len + 1);
   382 		memcpy(rr->path, rr->relocations[i].newpath,
   383 		       rr->relocations[i].newlen);
   384 		strcpy(rr->path + rr->relocations[i].newlen,
   385 		       path + rr->relocations[i].oldlen);
   386 		return rr->path;
   387 	} else
   388 		return path;
   389 }
   390 
   391 RAZOR_EXPORT void razor_relocations_destroy(struct razor_relocations *rr)
   392 {
   393 	int i;
   394 
   395 	for (i = 0; i < rr->n_relocations; i++) {
   396 	    free(rr->relocations[i].oldpath);
   397 	    free(rr->relocations[i].newpath);
   398 	}
   399 	free(rr->path);
   400 	free(rr->relocations);
   401 	free(rr);
   402 }
   403 
   404 static struct rpm_header_index *
   405 razor_rpm_get_header(struct razor_rpm *rpm, unsigned int tag)
   406 {
   407 	struct rpm_header_index *index, *end;
   408 
   409 	index = (struct rpm_header_index *) (rpm->header + 1);
   410 	end = index + ntohl(rpm->header->nindex);
   411 	while (index < end) {
   412 		if (ntohl(index->tag) == tag)
   413 			return index;
   414 		index++;
   415 	}
   416 
   417 	return NULL;
   418 }
   419 
   420 static const void *
   421 razor_rpm_get_indirect(struct razor_rpm *rpm,
   422 		       unsigned int tag, unsigned int *count)
   423 {
   424 	struct rpm_header_index *index;
   425 
   426 	index = razor_rpm_get_header(rpm, tag);
   427 	if (index != NULL) {
   428 		if (count)
   429 			*count = ntohl(index->count);
   430 
   431 		return rpm->pool + ntohl(index->offset);
   432 	}
   433 
   434 	return NULL;
   435 }
   436 
   437 static uint32_t
   438 rpm_to_razor_flags(uint32_t flags)
   439 {
   440 	uint32_t razor_flags;
   441 
   442 	razor_flags = 0;
   443 	if (flags & RPMSENSE_LESS)
   444 		razor_flags |= RAZOR_PROPERTY_LESS;
   445 	if (flags & RPMSENSE_EQUAL)
   446 		razor_flags |= RAZOR_PROPERTY_EQUAL;
   447 	if (flags & RPMSENSE_GREATER)
   448 		razor_flags |= RAZOR_PROPERTY_GREATER;
   449 
   450 	if (flags & RPMSENSE_SCRIPT_PRE)
   451 		razor_flags |= RAZOR_PROPERTY_PRE;
   452 	if (flags & RPMSENSE_SCRIPT_POST)
   453 		razor_flags |= RAZOR_PROPERTY_POST;
   454 	if (flags & RPMSENSE_SCRIPT_PREUN)
   455 		razor_flags |= RAZOR_PROPERTY_PREUN;
   456 	if (flags & RPMSENSE_SCRIPT_POSTUN)
   457 		razor_flags |= RAZOR_PROPERTY_POSTUN;
   458 	
   459 	return razor_flags;
   460 }
   461 
   462 static void
   463 import_properties(struct razor_importer *importer, uint32_t type,
   464 		  struct razor_rpm *rpm,
   465 		  int name_tag, int version_tag, int flags_tag)
   466 {
   467 	const char *name, *version;
   468 	const uint32_t *flags;
   469 	uint32_t f;
   470 	unsigned int i, count;
   471 
   472 	name = razor_rpm_get_indirect(rpm, name_tag, &count);
   473 	if (name == NULL)
   474 		return;
   475 
   476 	flags = razor_rpm_get_indirect(rpm, flags_tag, &count);
   477 
   478 	version = razor_rpm_get_indirect(rpm, version_tag, &count);
   479 	for (i = 0; i < count; i++) {
   480 		f = rpm_to_razor_flags(ntohl(flags[i]));
   481 		razor_importer_add_property(importer, name, f | type, version);
   482 		name += strlen(name) + 1;
   483 		version += strlen(version) + 1;
   484 	}
   485 }
   486 
   487 static void
   488 import_files(struct razor_importer *importer, struct razor_rpm *rpm)
   489 {
   490 	const char *name;
   491 	const uint32_t *index;
   492 	unsigned int i, count;
   493 	char buffer[256];
   494 
   495 	if (rpm->dirs == NULL)
   496 		return;
   497 
   498 	/* assert: count is the same for all arrays */
   499 	index = razor_rpm_get_indirect(rpm, RPMTAG_DIRINDEXES, &count);
   500 	name = razor_rpm_get_indirect(rpm, RPMTAG_BASENAMES, &count);
   501 	for (i = 0; i < count; i++) {
   502 		snprintf(buffer, sizeof buffer,
   503 			 "%s%s", rpm->dirs[ntohl(*index)], name);
   504 		razor_importer_add_file(importer, buffer);
   505 		name += strlen(name) + 1;
   506 		index++;
   507 	}
   508 }
   509 
   510 static void
   511 razor_rpm_build_evr(struct razor_rpm *rpm)
   512 {
   513 	const char *version, *release;
   514 	const uint32_t *epoch;
   515 	char evr[128], buf[16];
   516 
   517 	epoch = razor_rpm_get_indirect(rpm, RPMTAG_EPOCH, NULL);
   518 	version = razor_rpm_get_indirect(rpm, RPMTAG_VERSION, NULL);
   519 	release = razor_rpm_get_indirect(rpm, RPMTAG_RELEASE, NULL);
   520 	if (epoch)
   521 		snprintf(buf, sizeof buf, "%lu", (unsigned long)ntohl(*epoch));
   522 	razor_build_evr(evr, sizeof evr, epoch ? buf : NULL, version, release);
   523 	rpm->evr = strdup(evr);
   524 }
   525 
   526 static const char *
   527 razor_rpm_get_details_string(struct razor_rpm *rpm, enum razor_detail_type type)
   528 {
   529 	switch(type) {
   530 	case RAZOR_DETAIL_NAME:
   531 		return razor_rpm_get_indirect(rpm, RPMTAG_NAME, NULL);
   532 
   533 	case RAZOR_DETAIL_VERSION:
   534 		if (!rpm->evr)
   535 			razor_rpm_build_evr(rpm);
   536 		return rpm->evr;
   537 
   538 	case RAZOR_DETAIL_ARCH:
   539 		return razor_rpm_get_indirect(rpm, RPMTAG_ARCH, NULL);
   540 
   541 	case RAZOR_DETAIL_SUMMARY:
   542 		return razor_rpm_get_indirect(rpm, RPMTAG_SUMMARY, NULL);
   543 
   544 	case RAZOR_DETAIL_DESCRIPTION:
   545 		return razor_rpm_get_indirect(rpm, RPMTAG_DESCRIPTION, NULL);
   546 
   547 	case RAZOR_DETAIL_URL:
   548 		return razor_rpm_get_indirect(rpm, RPMTAG_URL, NULL);
   549 
   550 	case RAZOR_DETAIL_LICENSE:
   551 		return razor_rpm_get_indirect(rpm, RPMTAG_LICENSE, NULL);
   552 
   553 	case RAZOR_DETAIL_PREUNPROG:
   554 		return razor_rpm_get_indirect(rpm, RPMTAG_PREUNPROG, NULL);
   555 
   556 	case RAZOR_DETAIL_PREUN:
   557 		return razor_rpm_get_indirect(rpm, RPMTAG_PREUN, NULL);
   558 
   559 	case RAZOR_DETAIL_POSTUNPROG:
   560 		return razor_rpm_get_indirect(rpm, RPMTAG_POSTUNPROG, NULL);
   561 
   562 	case RAZOR_DETAIL_POSTUN:
   563 		return razor_rpm_get_indirect(rpm, RPMTAG_POSTUN, NULL);
   564 
   565 	default:
   566 		fprintf(stderr, "type %u not found\n", type);
   567 		return NULL;
   568 	}
   569 }
   570 
   571 static const char *const *
   572 razor_rpm_get_details_array(struct razor_rpm *rpm, enum razor_detail_type type)
   573 {
   574 	switch(type) {
   575 	case RAZOR_DETAIL_PREFIXES:
   576 		return rpm->prefixes;
   577 
   578 	default:
   579 		/* Impossible */
   580 		fprintf(stderr, "type %u not found\n", type);
   581 		return NULL;
   582 	}
   583 }
   584 
   585 void
   586 razor_rpm_get_details_varg(struct razor_rpm *rpm, va_list args)
   587 {
   588 	int i;
   589 	enum razor_detail_type type;
   590 	const char **string;
   591 	const char *const **array;
   592 
   593 	for (i = 0;; i += 2) {
   594 		type = va_arg(args, enum razor_detail_type);
   595 		if (type == RAZOR_DETAIL_LAST)
   596 			break;
   597 		if (type == RAZOR_DETAIL_PREFIXES) {
   598 			array = va_arg(args, const char *const **);
   599 			*array = razor_rpm_get_details_array(rpm, type);
   600 		} else {
   601 			string = va_arg(args, const char **);
   602 			*string = razor_rpm_get_details_string(rpm, type);
   603 		}
   604         }
   605 }
   606 
   607 RAZOR_EXPORT void
   608 razor_rpm_get_details(struct razor_rpm *rpm, ...)
   609 {
   610 	va_list args;
   611 
   612 	assert(rpm != NULL);
   613 
   614 	va_start(args, rpm);
   615 	razor_rpm_get_details_varg(rpm, args);
   616 	va_end(args);
   617 }
   618 
   619 RAZOR_EXPORT struct razor_rpm *
   620 razor_rpm_open(const char *uri, struct razor_error **error)
   621 {
   622 	struct rpm_lead *lead;
   623 	struct razor_rpm *rpm;
   624 	struct rpm_header_index *base, *index;
   625 	unsigned int count, i, nindex, hsize;
   626 	const char *name, *prefix;
   627 
   628 	assert (uri != NULL);
   629 
   630 	rpm = zalloc(sizeof *rpm);
   631 	if (rpm == NULL) {
   632 		razor_set_error(error, RAZOR_POSIX_ERROR, ENOMEM, NULL,
   633 				"Not enough memory");
   634 		return NULL;
   635 	}
   636 	memset(rpm, 0, sizeof *rpm);
   637 
   638 	rpm->map = razor_uri_get_contents(uri, &rpm->size, 0, error);
   639 	if (!rpm->map) {
   640 		free(rpm);
   641 		return NULL;
   642 	}
   643 
   644 	lead = rpm->map;
   645 	if (rpm->size < RPM_LEAD_SIZE ||
   646 	    strncmp((char *)lead->magic,RPM_LEAD_MAGIC,4) || lead->major != 3) {
   647 		razor_rpm_close(rpm);
   648 		razor_set_error(error, RAZOR_GENERAL_ERROR,
   649 				RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
   650 				uri, "Not a recognized RPM format file");
   651 		return NULL;
   652 	}
   653 
   654 	rpm->signature = rpm->map + RPM_LEAD_SIZE;
   655 	nindex = ntohl(rpm->signature->nindex);
   656 	hsize = ntohl(rpm->signature->hsize);
   657 	rpm->header = (void *) (rpm->signature + 1) +
   658 		ALIGN(nindex * sizeof *index + hsize, 8);
   659 	nindex = ntohl(rpm->header->nindex);
   660 	hsize = ntohl(rpm->header->hsize);
   661 	rpm->payload = (void *) (rpm->header + 1) +
   662 		nindex * sizeof *index + hsize;
   663 
   664 	base = (struct rpm_header_index *) (rpm->header + 1);
   665 	rpm->pool = (void *) base + nindex * sizeof *index;
   666 
   667 	/* Look up dir names now so we can index them directly. */
   668 	name = razor_rpm_get_indirect(rpm, RPMTAG_DIRNAMES, &count);
   669 	if (name) {
   670 		rpm->dirs = calloc(count, sizeof *rpm->dirs);
   671 		for (i = 0; i < count; i++) {
   672 			rpm->dirs[i] = name;
   673 			name += strlen(name) + 1;
   674 		}
   675 	} else {
   676 		name = razor_rpm_get_indirect(rpm, RPMTAG_OLDFILENAMES,
   677 					      &count);
   678 		if (name) {
   679 			razor_rpm_close(rpm);
   680 			razor_set_error(error, RAZOR_GENERAL_ERROR,
   681 					RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
   682 					uri, "Old filenames not supported");
   683 			return NULL;
   684 		}
   685 	}
   686 
   687 	prefix = razor_rpm_get_indirect(rpm, RPMTAG_PREFIXES, &count);
   688 	if (prefix) {
   689 		rpm->prefixes = calloc(count + 1, sizeof *rpm->prefixes);
   690 		for (i = 0; i < count; i++) {
   691 			rpm->prefixes[i] = prefix;
   692 			prefix += strlen(prefix) + 1;
   693 		}
   694 		rpm->prefixes[i] = NULL;
   695 		rpm->n_prefixes = count;
   696 	} else {
   697 		prefix = razor_rpm_get_indirect(rpm, RPMTAG_DEFAULTPREFIX,
   698 						&count);
   699 		if (prefix) {
   700 			razor_rpm_close(rpm);
   701 			razor_set_error(error, RAZOR_GENERAL_ERROR,
   702 					RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
   703 					uri, "Default prefix not supported");
   704 			return NULL;
   705 		}
   706 	}
   707 
   708 	return rpm;
   709 }
   710 
   711 RAZOR_EXPORT void razor_rpm_set_relocations(struct razor_rpm *rpm,
   712 					    struct razor_relocations *rr)
   713 {
   714 	assert (rpm != NULL);
   715 
   716 	rpm->relocations = rr;
   717 }
   718 
   719 struct cpio_file_header {
   720 	char magic[6];
   721 	char inode[8];
   722 	char mode[8];
   723 	char uid[8];
   724 	char gid[8];
   725 	char nlink[8];
   726 	char mtime[8];
   727 	char filesize[8];
   728 	char devmajor[8];
   729 	char devminor[8];
   730 	char rdevmajor[8];
   731 	char rdevminor[8];
   732 	char namesize[8];
   733 	char checksum[8];
   734 	char filename[0];
   735 };
   736 
   737 /* gzip flags */
   738 #define ASCII_FLAG   0x01 /* bit 0 set: file probably ascii text */
   739 #define HEAD_CRC     0x02 /* bit 1 set: header CRC present */
   740 #define EXTRA_FIELD  0x04 /* bit 2 set: extra field present */
   741 #define ORIG_NAME    0x08 /* bit 3 set: original file name present */
   742 #define COMMENT      0x10 /* bit 4 set: file comment present */
   743 #define RESERVED     0xE0 /* bits 5..7: reserved */
   744 
   745 struct installer {
   746 	const char *root_uri;
   747 	struct razor_rpm *rpm;
   748 	struct razor_atomic *atomic;
   749 	z_stream stream;
   750 	unsigned char buffer[32768];
   751 	size_t rest, length;
   752 };
   753 
   754 static int
   755 installer_inflate(struct installer *installer)
   756 {
   757 	size_t length;
   758 	int err;
   759 
   760 	if (installer->rest > sizeof installer->buffer)
   761 		length = sizeof installer->buffer;
   762 	else
   763 		length = installer->rest;
   764 
   765 	installer->stream.next_out = installer->buffer;
   766 	installer->stream.avail_out = length;
   767 	err = inflate(&installer->stream, Z_SYNC_FLUSH);
   768 	if (err != Z_OK && err != Z_STREAM_END) {
   769 		razor_atomic_abort(installer->atomic, RAZOR_ZLIB_ERROR, err,
   770 				   "Failed to inflate");
   771 		return -1;
   772 	}
   773 
   774 	installer->rest -= length;
   775 	installer->length = length;
   776 
   777 	return 0;
   778 }
   779 
   780 static int
   781 installer_align(struct installer *installer, size_t size)
   782 {
   783 	unsigned char buffer[4];
   784 	int err;
   785 
   786 	installer->stream.next_out = buffer;
   787 	installer->stream.avail_out =
   788 		(size - installer->stream.total_out) & (size - 1);
   789 
   790 	if (installer->stream.avail_out == 0)
   791 		return 0;
   792 
   793 	err = inflate(&installer->stream, Z_SYNC_FLUSH);
   794 	if (err != Z_OK && err != Z_STREAM_END) {
   795 		razor_atomic_abort(installer->atomic, RAZOR_ZLIB_ERROR, err,
   796 				   "Failed to inflate");
   797 		return -1;
   798 	}
   799 
   800 	return 0;
   801 }
   802 
   803 static int
   804 create_path(struct installer *installer, const char *path, unsigned int mode)
   805 {
   806 	const char *s, *relative;
   807 	char *uri, *buffer;
   808 	int h, ret, is_within_root;
   809 	struct razor_error *tmp_error = NULL;
   810 
   811 	uri = razor_path_to_uri(path);
   812 
   813 	if (str_has_prefix(uri, "file:///"))
   814 		relative = uri + 8;
   815 	else if (str_has_prefix(uri, "file:/"))
   816 		relative = uri + 6;
   817 	else if (str_has_prefix(uri, "file:"))
   818 		relative = uri + 5;
   819 	else if (str_has_prefix(uri, "/"))
   820 		relative = uri + 1;
   821 	else
   822 		relative = uri;
   823 
   824 	buffer = razor_resolve_uri_root(installer->root_uri, relative, 1,
   825 					&tmp_error);
   826 
   827 	if (!buffer) {
   828 		razor_atomic_propagate_error(installer->atomic, tmp_error,
   829 					     NULL);
   830 		free(uri);
   831 		return -1;
   832 	}
   833 
   834 	is_within_root = installer->root_uri &&
   835 			 str_has_prefix(buffer, installer->root_uri);
   836 
   837 	if (is_within_root)
   838 		ret = razor_atomic_make_dirs(installer->atomic,
   839 					     installer->root_uri, relative);
   840 	else
   841 		ret = razor_atomic_make_dirs(installer->atomic, "", buffer);
   842 
   843 	free(uri);
   844 
   845 	if (ret)
   846 		return ret;
   847 
   848 	switch (mode >> 12) {
   849 	case REG:
   850 		/* FIXME: handle the case where a file is already there. */
   851 		h = razor_atomic_create_file(installer->atomic, buffer, mode);
   852 		free(buffer);
   853 		if (h < 0)
   854 			return -1;
   855 		while (installer->rest > 0) {
   856 			if (installer_inflate(installer))
   857 				return -1;
   858 			if (razor_atomic_write(installer->atomic, h,
   859 					       installer->buffer,
   860 					       installer->length))
   861 				return -1;
   862 		}
   863 		ret = razor_atomic_close(installer->atomic, h);
   864 		break;
   865 	case XDIR:
   866 		ret = razor_atomic_create_dir(installer->atomic, buffer, mode);
   867 		free(buffer);
   868 		return ret;
   869 	case LINK:
   870 #if HAVE_SYMLINK
   871 		if (installer_inflate(installer))
   872 			return -1;
   873 		if (installer->length >= sizeof installer->buffer) {
   874 			razor_atomic_abort(installer->atomic,
   875 					   RAZOR_GENERAL_ERROR,
   876 					   RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
   877 					   "Link target too long");
   878 			return -1;
   879 		}
   880 		installer->buffer[installer->length] = '\0';
   881 		ret = razor_atomic_create_symlink(installer->atomic,
   882 						  (const char *)installer->buffer,
   883 						  buffer);
   884 		free(buffer);
   885 		break;
   886 #else
   887 		s = "Symbolic links";
   888 		goto unsupported;
   889 #endif
   890 	case PIPE:
   891 		s = "Named pipes";
   892 unsupported:
   893 		free(buffer);
   894 		buffer = razor_concat(s, " are not supported on this platform",
   895 				      NULL);
   896 		razor_atomic_abort(installer->atomic, RAZOR_GENERAL_ERROR,
   897 				   RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED, buffer);
   898 		free(buffer);
   899 		return -1;
   900 	case CDEV:
   901 		s = "Character devices";
   902 		goto unsupported;
   903 	case BDEV:
   904 		s = "Block devices";
   905 		goto unsupported;
   906 	case SOCK:
   907 		s = "Named sockets";
   908 		goto unsupported;
   909 	default:
   910 		free(buffer);
   911 		razor_atomic_abort(installer->atomic, RAZOR_GENERAL_ERROR,
   912 				   RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
   913 				   "Unknown file type");
   914 		return -1;
   915 	}
   916 
   917 	return ret;
   918 }
   919 
   920 static int chroot_push(const char *root)
   921 {
   922 	int fd;
   923 #if HAVE_CHROOT
   924 	if (geteuid() == 0) {
   925 		fd = open("/", O_RDONLY, 0);
   926 		if (chroot(root) < 0) {
   927 			fprintf(stderr, "failed to chroot to %s: %s\n",
   928 				root, strerror(errno));
   929 			exit(-1);
   930 		}
   931 	} else
   932 #endif
   933 		fd = -1;
   934 	return fd;
   935 }
   936 
   937 static void chroot_pop(int fd)
   938 {
   939 #if HAVE_CHROOT
   940 	if (fd >= 0) {
   941 		fchdir(fd);
   942 		close(fd);
   943 		chroot(".");
   944 	}
   945 #endif
   946 }
   947 
   948 static int
   949 run_script_lua(const char *root_uri, unsigned int script_tag,
   950 	       const char *script, int arg1, struct razor_error **error)
   951 {
   952 	int root_fd, retval;
   953 	const char *name;
   954 #if HAVE_LUA
   955 	char *root;
   956 #endif
   957 
   958 	switch(script_tag) {
   959 		case RPMTAG_PREIN:
   960 			name = "%pre";
   961 			break;
   962 		case RPMTAG_POSTIN:
   963 			name = "%post";
   964 			break;
   965 		case RPMTAG_PREUN:
   966 			name = "%preun";
   967 			break;
   968 		case RPMTAG_POSTUN:
   969 			name = "%postun";
   970 			break;
   971 		default:
   972 			name = "script";
   973 			break;
   974 	}
   975 
   976 #if HAVE_LUA
   977 	root = razor_path_from_uri(root_uri, error);
   978 	if (!root)
   979 		return -1;
   980 	root_fd = root ? chroot_push(root) : -1;
   981 	retval = run_lua_script(root_fd < 0 ? root : NULL, name, script, -1,
   982 				arg1);
   983 	free(root);
   984 	chroot_pop(root_fd);
   985 #else	/* HAVE_LUA */
   986 	razor_set_error(error, RAZOR_GENERAL_ERROR, RAZOR_GENERAL_ERROR_FAILED,
   987 			name, "lua not available to run script");
   988 	retval = -1;
   989 #endif	/* HAVE_LUA */
   990 
   991 	return retval;
   992 }
   993 
   994 static int
   995 run_script_external(const char *root_uri, const char *program,
   996 		    const char *script, int arg1, struct razor_error **error)
   997 {
   998 	int root_fd, retval;
   999 	FILE *fp;
  1000 	char buf[32], *command, *root;
  1001 
  1002 	if (program == NULL) {
  1003 #if MSWIN_API
  1004 		program = getenv("COMSPEC");
  1005 		if (program) {
  1006 			program = strchr(program, '=');
  1007 			if (program)
  1008 				program++;
  1009 		}
  1010 		if (!program)
  1011 			program = "c:\\windows\\system32\\cmd.exe";
  1012 #else
  1013 		program = "/bin/sh";
  1014 #endif
  1015 	}
  1016 
  1017 	root = razor_path_from_uri(root_uri, error);
  1018 	if (!root)
  1019 		return -1;
  1020 	root_fd = chroot_push(root);
  1021 	free(root);
  1022 	if (arg1 >= 0) {
  1023 		sprintf(buf, "%d", arg1);
  1024 		command = malloc(strlen(program) + strlen(buf) + 2);
  1025 		sprintf(command, "%s %s", program, buf);
  1026 	} else
  1027 		command = strdup(program);
  1028 	fp = popen(command, "w");
  1029 	free(command);
  1030 
  1031 	if (!fp) {
  1032 		razor_set_error_posix(error, program);
  1033 		retval = -1;
  1034 	} else if (script && fwrite(script, strlen(script), 1, fp) != 1) {
  1035 		razor_set_error_posix(error,
  1036 				      "failed to write script to program");
  1037 		retval = -1;
  1038 	} else
  1039 		retval = 0;
  1040 
  1041 	if (fp)
  1042 		pclose(fp);
  1043 	chroot_pop(root_fd);
  1044 
  1045 	return retval;
  1046 }
  1047 
  1048 static int
  1049 run_script(struct installer *installer, unsigned int program_tag,
  1050 	   unsigned int script_tag, int arg1, struct razor_error **error)
  1051 {
  1052 	int i, retval;
  1053 	struct razor_rpm *rpm = installer->rpm;
  1054 	const char *script = NULL, *program = NULL, *prefix;
  1055 	char buf[32];
  1056 	struct environment env;
  1057 
  1058 	program = razor_rpm_get_indirect(rpm, program_tag, NULL);
  1059 	script = razor_rpm_get_indirect(rpm, script_tag, NULL);
  1060 	if (program == NULL && script == NULL)
  1061 		return 0;
  1062 
  1063 	if (rpm->relocations) {
  1064 		environment_init(&env);
  1065 		for(i = 0; i < rpm->n_prefixes; i++) {
  1066 			prefix = razor_relocations_apply(rpm->relocations,
  1067 							 rpm->prefixes[i]);
  1068 			sprintf(buf, "RPM_INSTALL_PREFIX%d", i);
  1069 			environment_add_variable(&env, buf, prefix);
  1070 		}
  1071 		environment_set(&env);
  1072 	}
  1073 
  1074 	if (program && strcmp(program, "<lua>") == 0)
  1075 		retval = run_script_lua(installer->root_uri, script_tag,
  1076 					script, arg1, error);
  1077 	else
  1078 		retval = run_script_external(installer->root_uri, program,
  1079 					     script, arg1, error);
  1080 
  1081 	if (rpm->relocations) {
  1082 		environment_unset(&env);
  1083 		environment_release(&env);
  1084 	}
  1085 
  1086 	return retval;
  1087 }
  1088 
  1089 int
  1090 razor_run_script(const char *root_uri, enum razor_property_flags script,
  1091 		 const char *program, const char *body, int arg1,
  1092 		 struct razor_error **error)
  1093 {
  1094 	int retval;
  1095 	unsigned int script_tag;
  1096 
  1097 	if (program && !*program)
  1098 		program = NULL;
  1099 	if (body && !*body)
  1100 		body = NULL;
  1101 	if (program == NULL && body == NULL)
  1102 		return 0;
  1103 
  1104 	if (program && strcmp(program, "<lua>") == 0)
  1105 	{
  1106 		switch(script) {
  1107 		case RAZOR_PROPERTY_PRE:
  1108 			script_tag = RPMTAG_PREIN;
  1109 			break;
  1110 		case RAZOR_PROPERTY_POST:
  1111 			script_tag = RPMTAG_POSTIN;
  1112 			break;
  1113 		case RAZOR_PROPERTY_PREUN:
  1114 			script_tag = RPMTAG_PREUN;
  1115 			break;
  1116 		case RAZOR_PROPERTY_POSTUN:
  1117 			script_tag = RPMTAG_POSTUN;
  1118 			break;
  1119 		default:
  1120 			script_tag = 0;
  1121 			break;
  1122 		}
  1123 		retval = run_script_lua(root_uri, script_tag, body, arg1, error);
  1124 	}
  1125 	else
  1126 		retval = run_script_external(root_uri, program, body, arg1,
  1127 					     error);
  1128 
  1129 	return retval;
  1130 }
  1131 
  1132 static int
  1133 installer_init(struct installer *installer)
  1134 {
  1135 	unsigned char *gz_header;
  1136 	int method, flags, err;
  1137 	char buffer[32], *s;
  1138 
  1139 	gz_header = installer->rpm->payload;
  1140 	if (gz_header[0] != 0x1f || gz_header[1] != 0x8b) {
  1141 		razor_atomic_abort(installer->atomic, RAZOR_GENERAL_ERROR,
  1142 				   RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
  1143 				   "Payload section doesn't have gz header");
  1144 		return -1;
  1145 	}
  1146 
  1147 	method = gz_header[2];
  1148 	flags = gz_header[3];
  1149 
  1150 	if (method != Z_DEFLATED || flags != 0) {
  1151 		razor_atomic_abort(installer->atomic, RAZOR_GENERAL_ERROR,
  1152 				   RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
  1153 				   "Unknown payload compression method or "
  1154 				   "flags set");
  1155 		return -1;
  1156 	}
  1157 
  1158 	installer->stream.zalloc = NULL;
  1159 	installer->stream.zfree = NULL;
  1160 	installer->stream.opaque = NULL;
  1161 
  1162 	installer->stream.next_in  = gz_header + 10;
  1163 	installer->stream.avail_in =
  1164 		(installer->rpm->map + installer->rpm->size) -
  1165 		(void *) installer->stream.next_in;
  1166 	installer->stream.next_out = NULL;
  1167 	installer->stream.avail_out = 0;
  1168 
  1169 	err = inflateInit2(&installer->stream, -MAX_WBITS);
  1170 	if (err != Z_OK) {
  1171 		sprintf(buffer, "%d", err);
  1172 		s = razor_concat("inflateEnd error: ", buffer, NULL);
  1173 		razor_atomic_abort(installer->atomic, RAZOR_ZLIB_ERROR, err, s);
  1174 		free(s);
  1175 		return -1;
  1176 	}
  1177 
  1178 	return 0;
  1179 }
  1180 
  1181 static int
  1182 installer_finish(struct installer *installer)
  1183 {
  1184 	int err;
  1185 	char buffer[32], *s;
  1186 
  1187 	err = inflateEnd(&installer->stream);
  1188 
  1189 	if (err != Z_OK) {
  1190 		sprintf(buffer, "%d", err);
  1191 		s = razor_concat("inflateEnd error: ", buffer, NULL);
  1192 		razor_atomic_abort(installer->atomic, RAZOR_ZLIB_ERROR, err, s);
  1193 		free(s);
  1194 	}
  1195 
  1196 	return razor_atomic_in_error_state(installer->atomic);
  1197 }
  1198 
  1199 static unsigned long
  1200 fixed_hex_to_ulong(const char *hex, int length)
  1201 {
  1202 	long l;
  1203 	int i;
  1204 
  1205 	for (i = 0, l = 0; i < length; i++) {
  1206 		if (hex[i] < 'a')
  1207 			l = l * 16 + hex[i] - '0';
  1208 		else
  1209 			l = l * 16 + hex[i] - 'a' + 10;
  1210 	}
  1211 
  1212 	return l;
  1213 }
  1214 
  1215 RAZOR_EXPORT int
  1216 razor_rpm_install(struct razor_rpm *rpm, struct razor_atomic *atomic,
  1217 		  const char *root_uri, int install_count,
  1218 		  enum razor_stage_type stage)
  1219 {
  1220 	struct installer installer;
  1221 	struct cpio_file_header *header;
  1222 	unsigned int mode;
  1223 	const char *path;
  1224 	size_t filesize;
  1225 	char *s;
  1226 	int retval = 0, domain, code;
  1227 	struct razor_error *tmp_error = NULL;
  1228 
  1229 	assert (rpm != NULL);
  1230 	assert (root_uri != NULL);
  1231 
  1232 	installer.rpm = rpm;
  1233 	installer.root_uri = root_uri;
  1234 	installer.atomic = atomic;
  1235 
  1236 	/* FIXME: Only do this before a transaction, not per rpm. */
  1237 	if (*root_uri && razor_uri_is_directory(root_uri, &tmp_error) <= 0) {
  1238 		if (tmp_error) {
  1239 			domain = razor_error_get_domain(tmp_error);
  1240 			code = razor_error_get_code(tmp_error);
  1241 			razor_error_free(tmp_error);
  1242 			tmp_error = NULL;
  1243 		} else {
  1244 			domain = RAZOR_POSIX_ERROR;
  1245 			code = ENOTDIR;
  1246 		}
  1247 		s = razor_concat(root_uri, ": Directory does not exist", NULL);
  1248 		razor_atomic_abort(atomic, domain, code, s);
  1249 		free(s);
  1250 		return -1;
  1251 	}
  1252 
  1253 	if (rpm->relocations)
  1254 		razor_relocations_set_rpm(rpm->relocations, rpm);
  1255 
  1256 	if (stage & RAZOR_STAGE_SCRIPTS_PRE) {
  1257 		retval = run_script(&installer, RPMTAG_PREINPROG, RPMTAG_PREIN,
  1258 				    install_count, &tmp_error);
  1259 		if (retval) {
  1260 			razor_atomic_propagate_error(atomic, tmp_error, NULL);
  1261 			tmp_error = NULL;
  1262 		}
  1263 	}
  1264 
  1265 	if (!retval && (stage & RAZOR_STAGE_FILES)) {
  1266 		if (installer_init(&installer))
  1267 			return -1;
  1268 
  1269 		while (installer.stream.avail_in > 0) {
  1270 			installer.rest = sizeof *header;
  1271 			if (installer_inflate(&installer))
  1272 				break;
  1273 
  1274 			header = (struct cpio_file_header *) installer.buffer;
  1275 			mode = fixed_hex_to_ulong(header->mode,
  1276 						  sizeof header->mode);
  1277 			filesize = fixed_hex_to_ulong(header->filesize,
  1278 						      sizeof header->filesize);
  1279 
  1280 			installer.rest =
  1281 			  fixed_hex_to_ulong(header->namesize,
  1282 					     sizeof header->namesize);
  1283 
  1284 			if (installer_inflate(&installer) ||
  1285 			    installer_align(&installer, 4))
  1286 				break;
  1287 
  1288 			path = (const char *) installer.buffer;
  1289 			/* This convention is so lame... */
  1290 			if (strcmp(path, "TRAILER!!!") == 0)
  1291 				break;
  1292 
  1293 			installer.rest = filesize;
  1294 			path++;
  1295 			if (rpm->relocations)
  1296 				path = razor_relocations_apply(rpm->relocations,
  1297 							       path);
  1298 			if (create_path(&installer, path, mode))
  1299 				break;
  1300 			if (installer_align(&installer, 4))
  1301 				break;
  1302 		}
  1303 
  1304 		if (installer_finish(&installer))
  1305 			return -1;
  1306 
  1307 		retval = razor_atomic_in_error_state(atomic);
  1308 	}
  1309 
  1310 	if (!retval && (stage & RAZOR_STAGE_SCRIPTS_POST)) {
  1311 		retval = run_script(&installer, RPMTAG_POSTINPROG,
  1312 				    RPMTAG_POSTIN, install_count, &tmp_error);
  1313 		if (retval) {
  1314 			razor_atomic_propagate_error(atomic, tmp_error, NULL);
  1315 			tmp_error = NULL;
  1316 		}
  1317 	}
  1318 
  1319 	return retval;
  1320 }
  1321 
  1322 RAZOR_EXPORT int
  1323 razor_rpm_close(struct razor_rpm *rpm)
  1324 {
  1325 	int err;
  1326 
  1327 	assert (rpm != NULL);
  1328 
  1329 	free(rpm->dirs);
  1330 	free(rpm->prefixes);
  1331 	err = razor_uri_free_contents(rpm->map, rpm->size);
  1332 	free(rpm->evr);
  1333 	free(rpm);
  1334 
  1335 	return err;
  1336 }
  1337 
  1338 RAZOR_EXPORT int
  1339 razor_importer_add_rpm(struct razor_importer *importer, struct razor_rpm *rpm)
  1340 {
  1341 	const char *name, *version, *arch;
  1342 	const char *summary, *description, *url, *license;
  1343 
  1344 	assert (importer != NULL);
  1345 	assert (rpm != NULL);
  1346 
  1347 	razor_rpm_get_details(rpm,
  1348 			      RAZOR_DETAIL_NAME, &name,
  1349 			      RAZOR_DETAIL_VERSION, &version,
  1350 			      RAZOR_DETAIL_ARCH, &arch,
  1351 			      RAZOR_DETAIL_SUMMARY, &summary,
  1352 			      RAZOR_DETAIL_DESCRIPTION, &description,
  1353 			      RAZOR_DETAIL_URL, &url,
  1354 			      RAZOR_DETAIL_LICENSE, &license,
  1355 			      RAZOR_DETAIL_LAST);
  1356 
  1357 	razor_importer_begin_package(importer, name, version, arch);
  1358 
  1359 	razor_importer_add_details(importer, summary, description, url,
  1360 				   license);
  1361 
  1362 	import_properties(importer, RAZOR_PROPERTY_REQUIRES, rpm,
  1363 			  RPMTAG_REQUIRENAME,
  1364 			  RPMTAG_REQUIREVERSION,
  1365 			  RPMTAG_REQUIREFLAGS);
  1366 
  1367 	import_properties(importer, RAZOR_PROPERTY_PROVIDES, rpm,
  1368 			  RPMTAG_PROVIDENAME,
  1369 			  RPMTAG_PROVIDEVERSION,
  1370 			  RPMTAG_PROVIDEFLAGS);
  1371 
  1372 	import_properties(importer, RAZOR_PROPERTY_OBSOLETES, rpm,
  1373 			  RPMTAG_OBSOLETENAME,
  1374 			  RPMTAG_OBSOLETEVERSION,
  1375 			  RPMTAG_OBSOLETEFLAGS);
  1376 
  1377 	import_properties(importer, RAZOR_PROPERTY_CONFLICTS, rpm,
  1378 			  RPMTAG_CONFLICTNAME,
  1379 			  RPMTAG_CONFLICTVERSION,
  1380 			  RPMTAG_CONFLICTFLAGS);
  1381 
  1382 	import_files(importer, rpm);
  1383 
  1384 	razor_importer_finish_package(importer);
  1385 
  1386 	return 0;
  1387 }