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