Add a test for relocations that are valid paths but would be invalid URIs if mis-interpreted
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>
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.
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.
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.
34 #include <winsock2.h> /* For ntohl() */
36 #include <arpa/inet.h>
43 #include "razor-internal.h"
50 unsigned char magic[4];
51 unsigned char major, minor;
60 #define RPM_LEAD_SIZE sizeof(struct rpm_lead)
61 #define RPM_LEAD_MAGIC "\xED\xAB\xEE\xDB"
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 */
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,
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 */
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 */
244 unsigned char magic[4];
245 unsigned char reserved[4];
250 struct rpm_header_index {
258 struct rpm_header *signature;
259 struct rpm_header *header;
261 unsigned int n_prefixes;
262 const char **prefixes;
267 struct razor_relocations *relocations;
271 enum razor_relocation_flags {
272 RAZOR_RELOCATION_ACTIVE = 1 << 0,
275 struct razor_relocation {
276 enum razor_relocation_flags flags;
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.
288 struct razor_relocation *relocations;
293 RAZOR_EXPORT struct razor_relocations *razor_relocations_create(void)
295 return calloc(1, sizeof(struct razor_relocations));
298 RAZOR_EXPORT void razor_relocations_add(struct razor_relocations *rr,
299 const char *oldpath, const char *newpath)
304 if (newpath && !strcmp(oldpath, newpath))
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);
317 free(rr->relocations[i].oldpath);
318 free(rr->relocations[i].newpath);
320 rr->relocations[i] = rr->relocations[i + 1];
321 } while (rr->relocations[++i].oldpath);
327 free(rr->relocations[i].newpath);
328 rr->relocations[i].newpath = strdup(newpath);
329 rr->relocations[i].newlen = strlen(newpath);
333 if (!rr->n_relocations++)
334 rr->relocations = calloc(1, sizeof *rr->relocations);
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);
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);
350 razor_relocations_set_rpm(struct razor_relocations *rr, struct razor_rpm *rpm)
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;
365 RAZOR_EXPORT const char *
366 razor_relocations_apply(struct razor_relocations *rr, const char *path)
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))
377 if (i < rr->n_relocations) {
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);
391 RAZOR_EXPORT void razor_relocations_destroy(struct razor_relocations *rr)
395 for (i = 0; i < rr->n_relocations; i++) {
396 free(rr->relocations[i].oldpath);
397 free(rr->relocations[i].newpath);
400 free(rr->relocations);
404 static struct rpm_header_index *
405 razor_rpm_get_header(struct razor_rpm *rpm, unsigned int tag)
407 struct rpm_header_index *index, *end;
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)
421 razor_rpm_get_indirect(struct razor_rpm *rpm,
422 unsigned int tag, unsigned int *count)
424 struct rpm_header_index *index;
426 index = razor_rpm_get_header(rpm, tag);
429 *count = ntohl(index->count);
431 return rpm->pool + ntohl(index->offset);
438 rpm_to_razor_flags(uint32_t flags)
440 uint32_t razor_flags;
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;
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;
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)
467 const char *name, *version;
468 const uint32_t *flags;
470 unsigned int i, count;
472 name = razor_rpm_get_indirect(rpm, name_tag, &count);
476 flags = razor_rpm_get_indirect(rpm, flags_tag, &count);
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;
488 import_files(struct razor_importer *importer, struct razor_rpm *rpm)
491 const uint32_t *index;
492 unsigned int i, count;
495 if (rpm->dirs == NULL)
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;
511 razor_rpm_build_evr(struct razor_rpm *rpm)
513 const char *version, *release;
514 const uint32_t *epoch;
515 char evr[128], buf[16];
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);
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);
527 razor_rpm_get_details_string(struct razor_rpm *rpm, enum razor_detail_type type)
530 case RAZOR_DETAIL_NAME:
531 return razor_rpm_get_indirect(rpm, RPMTAG_NAME, NULL);
533 case RAZOR_DETAIL_VERSION:
535 razor_rpm_build_evr(rpm);
538 case RAZOR_DETAIL_ARCH:
539 return razor_rpm_get_indirect(rpm, RPMTAG_ARCH, NULL);
541 case RAZOR_DETAIL_SUMMARY:
542 return razor_rpm_get_indirect(rpm, RPMTAG_SUMMARY, NULL);
544 case RAZOR_DETAIL_DESCRIPTION:
545 return razor_rpm_get_indirect(rpm, RPMTAG_DESCRIPTION, NULL);
547 case RAZOR_DETAIL_URL:
548 return razor_rpm_get_indirect(rpm, RPMTAG_URL, NULL);
550 case RAZOR_DETAIL_LICENSE:
551 return razor_rpm_get_indirect(rpm, RPMTAG_LICENSE, NULL);
553 case RAZOR_DETAIL_PREUNPROG:
554 return razor_rpm_get_indirect(rpm, RPMTAG_PREUNPROG, NULL);
556 case RAZOR_DETAIL_PREUN:
557 return razor_rpm_get_indirect(rpm, RPMTAG_PREUN, NULL);
559 case RAZOR_DETAIL_POSTUNPROG:
560 return razor_rpm_get_indirect(rpm, RPMTAG_POSTUNPROG, NULL);
562 case RAZOR_DETAIL_POSTUN:
563 return razor_rpm_get_indirect(rpm, RPMTAG_POSTUN, NULL);
566 fprintf(stderr, "type %u not found\n", type);
571 static const char *const *
572 razor_rpm_get_details_array(struct razor_rpm *rpm, enum razor_detail_type type)
575 case RAZOR_DETAIL_PREFIXES:
576 return rpm->prefixes;
580 fprintf(stderr, "type %u not found\n", type);
586 razor_rpm_get_details_varg(struct razor_rpm *rpm, va_list args)
589 enum razor_detail_type type;
591 const char *const **array;
593 for (i = 0;; i += 2) {
594 type = va_arg(args, enum razor_detail_type);
595 if (type == RAZOR_DETAIL_LAST)
597 if (type == RAZOR_DETAIL_PREFIXES) {
598 array = va_arg(args, const char *const **);
599 *array = razor_rpm_get_details_array(rpm, type);
601 string = va_arg(args, const char **);
602 *string = razor_rpm_get_details_string(rpm, type);
608 razor_rpm_get_details(struct razor_rpm *rpm, ...)
615 razor_rpm_get_details_varg(rpm, args);
619 RAZOR_EXPORT struct razor_rpm *
620 razor_rpm_open(const char *uri, struct razor_error **error)
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;
628 assert (uri != NULL);
630 rpm = zalloc(sizeof *rpm);
632 razor_set_error(error, RAZOR_POSIX_ERROR, ENOMEM, NULL,
633 "Not enough memory");
636 memset(rpm, 0, sizeof *rpm);
638 rpm->map = razor_uri_get_contents(uri, &rpm->size, 0, error);
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");
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;
664 base = (struct rpm_header_index *) (rpm->header + 1);
665 rpm->pool = (void *) base + nindex * sizeof *index;
667 /* Look up dir names now so we can index them directly. */
668 name = razor_rpm_get_indirect(rpm, RPMTAG_DIRNAMES, &count);
670 rpm->dirs = calloc(count, sizeof *rpm->dirs);
671 for (i = 0; i < count; i++) {
673 name += strlen(name) + 1;
676 name = razor_rpm_get_indirect(rpm, RPMTAG_OLDFILENAMES,
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");
687 prefix = razor_rpm_get_indirect(rpm, RPMTAG_PREFIXES, &count);
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;
694 rpm->prefixes[i] = NULL;
695 rpm->n_prefixes = count;
697 prefix = razor_rpm_get_indirect(rpm, RPMTAG_DEFAULTPREFIX,
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");
711 RAZOR_EXPORT void razor_rpm_set_relocations(struct razor_rpm *rpm,
712 struct razor_relocations *rr)
714 assert (rpm != NULL);
716 rpm->relocations = rr;
719 struct cpio_file_header {
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 */
746 const char *root_uri;
747 struct razor_rpm *rpm;
748 struct razor_atomic *atomic;
750 unsigned char buffer[32768];
755 installer_inflate(struct installer *installer)
760 if (installer->rest > sizeof installer->buffer)
761 length = sizeof installer->buffer;
763 length = installer->rest;
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");
774 installer->rest -= length;
775 installer->length = length;
781 installer_align(struct installer *installer, size_t size)
783 unsigned char buffer[4];
786 installer->stream.next_out = buffer;
787 installer->stream.avail_out =
788 (size - installer->stream.total_out) & (size - 1);
790 if (installer->stream.avail_out == 0)
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");
804 create_path(struct installer *installer, const char *path, unsigned int mode)
806 const char *s, *relative;
808 int h, ret, is_within_root;
809 struct razor_error *tmp_error = NULL;
811 uri = razor_path_to_uri(path);
813 if (str_has_prefix(uri, "file:///"))
815 else if (str_has_prefix(uri, "file:/"))
817 else if (str_has_prefix(uri, "file:"))
819 else if (str_has_prefix(uri, "/"))
824 buffer = razor_resolve_uri_root(installer->root_uri, relative, 1,
828 razor_atomic_propagate_error(installer->atomic, tmp_error,
834 is_within_root = installer->root_uri &&
835 str_has_prefix(buffer, installer->root_uri);
838 ret = razor_atomic_make_dirs(installer->atomic,
839 installer->root_uri, relative);
841 ret = razor_atomic_make_dirs(installer->atomic, "", buffer);
848 switch (mode >> 12) {
850 /* FIXME: handle the case where a file is already there. */
851 h = razor_atomic_create_file(installer->atomic, buffer, mode);
855 while (installer->rest > 0) {
856 if (installer_inflate(installer))
858 if (razor_atomic_write(installer->atomic, h,
863 ret = razor_atomic_close(installer->atomic, h);
866 ret = razor_atomic_create_dir(installer->atomic, buffer, mode);
871 if (installer_inflate(installer))
873 if (installer->length >= sizeof installer->buffer) {
874 razor_atomic_abort(installer->atomic,
876 RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
877 "Link target too long");
880 installer->buffer[installer->length] = '\0';
881 ret = razor_atomic_create_symlink(installer->atomic,
882 (const char *)installer->buffer,
887 s = "Symbolic links";
894 buffer = razor_concat(s, " are not supported on this platform",
896 razor_atomic_abort(installer->atomic, RAZOR_GENERAL_ERROR,
897 RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED, buffer);
901 s = "Character devices";
911 razor_atomic_abort(installer->atomic, RAZOR_GENERAL_ERROR,
912 RAZOR_GENERAL_ERROR_RPM_UNSUPPORTED,
913 "Unknown file type");
920 static int chroot_push(const char *root)
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));
937 static void chroot_pop(int fd)
949 run_script_lua(const char *root_uri, unsigned int script_tag,
950 const char *script, int arg1, struct razor_error **error)
977 root = razor_path_from_uri(root_uri, error);
980 root_fd = root ? chroot_push(root) : -1;
981 retval = run_lua_script(root_fd < 0 ? root : NULL, name, script, -1,
986 razor_set_error(error, RAZOR_GENERAL_ERROR, RAZOR_GENERAL_ERROR_FAILED,
987 name, "lua not available to run script");
989 #endif /* HAVE_LUA */
995 run_script_external(const char *root_uri, const char *program,
996 const char *script, int arg1, struct razor_error **error)
1000 char buf[32], *command, *root;
1002 if (program == NULL) {
1004 program = getenv("COMSPEC");
1006 program = strchr(program, '=');
1011 program = "c:\\windows\\system32\\cmd.exe";
1013 program = "/bin/sh";
1017 root = razor_path_from_uri(root_uri, error);
1020 root_fd = chroot_push(root);
1023 sprintf(buf, "%d", arg1);
1024 command = malloc(strlen(program) + strlen(buf) + 2);
1025 sprintf(command, "%s %s", program, buf);
1027 command = strdup(program);
1028 fp = popen(command, "w");
1032 razor_set_error_posix(error, program);
1034 } else if (script && fwrite(script, strlen(script), 1, fp) != 1) {
1035 razor_set_error_posix(error,
1036 "failed to write script to program");
1043 chroot_pop(root_fd);
1049 run_script(struct installer *installer, unsigned int program_tag,
1050 unsigned int script_tag, int arg1, struct razor_error **error)
1053 struct razor_rpm *rpm = installer->rpm;
1054 const char *script = NULL, *program = NULL, *prefix;
1056 struct environment env;
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)
1063 if (rpm->relocations) {
1064 environment_init(&env);
1065 for(i = 0; i < rpm->n_prefixes; i++) {
1066 prefix = razor_relocations_apply(rpm->relocations,
1068 sprintf(buf, "RPM_INSTALL_PREFIX%d", i);
1069 environment_add_variable(&env, buf, prefix);
1071 environment_set(&env);
1074 if (program && strcmp(program, "<lua>") == 0)
1075 retval = run_script_lua(installer->root_uri, script_tag,
1076 script, arg1, error);
1078 retval = run_script_external(installer->root_uri, program,
1079 script, arg1, error);
1081 if (rpm->relocations) {
1082 environment_unset(&env);
1083 environment_release(&env);
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)
1095 unsigned int script_tag;
1097 if (program && !*program)
1101 if (program == NULL && body == NULL)
1104 if (program && strcmp(program, "<lua>") == 0)
1107 case RAZOR_PROPERTY_PRE:
1108 script_tag = RPMTAG_PREIN;
1110 case RAZOR_PROPERTY_POST:
1111 script_tag = RPMTAG_POSTIN;
1113 case RAZOR_PROPERTY_PREUN:
1114 script_tag = RPMTAG_PREUN;
1116 case RAZOR_PROPERTY_POSTUN:
1117 script_tag = RPMTAG_POSTUN;
1123 retval = run_script_lua(root_uri, script_tag, body, arg1, error);
1126 retval = run_script_external(root_uri, program, body, arg1,
1133 installer_init(struct installer *installer)
1135 unsigned char *gz_header;
1136 int method, flags, err;
1137 char buffer[32], *s;
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");
1147 method = gz_header[2];
1148 flags = gz_header[3];
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 "
1158 installer->stream.zalloc = NULL;
1159 installer->stream.zfree = NULL;
1160 installer->stream.opaque = NULL;
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;
1169 err = inflateInit2(&installer->stream, -MAX_WBITS);
1171 sprintf(buffer, "%d", err);
1172 s = razor_concat("inflateEnd error: ", buffer, NULL);
1173 razor_atomic_abort(installer->atomic, RAZOR_ZLIB_ERROR, err, s);
1182 installer_finish(struct installer *installer)
1185 char buffer[32], *s;
1187 err = inflateEnd(&installer->stream);
1190 sprintf(buffer, "%d", err);
1191 s = razor_concat("inflateEnd error: ", buffer, NULL);
1192 razor_atomic_abort(installer->atomic, RAZOR_ZLIB_ERROR, err, s);
1196 return razor_atomic_in_error_state(installer->atomic);
1199 static unsigned long
1200 fixed_hex_to_ulong(const char *hex, int length)
1205 for (i = 0, l = 0; i < length; i++) {
1207 l = l * 16 + hex[i] - '0';
1209 l = l * 16 + hex[i] - 'a' + 10;
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)
1220 struct installer installer;
1221 struct cpio_file_header *header;
1226 int retval = 0, domain, code;
1227 struct razor_error *tmp_error = NULL;
1229 assert (rpm != NULL);
1230 assert (root_uri != NULL);
1232 installer.rpm = rpm;
1233 installer.root_uri = root_uri;
1234 installer.atomic = atomic;
1236 /* FIXME: Only do this before a transaction, not per rpm. */
1237 if (*root_uri && razor_uri_is_directory(root_uri, &tmp_error) <= 0) {
1239 domain = razor_error_get_domain(tmp_error);
1240 code = razor_error_get_code(tmp_error);
1241 razor_error_free(tmp_error);
1244 domain = RAZOR_POSIX_ERROR;
1247 s = razor_concat(root_uri, ": Directory does not exist", NULL);
1248 razor_atomic_abort(atomic, domain, code, s);
1253 if (rpm->relocations)
1254 razor_relocations_set_rpm(rpm->relocations, rpm);
1256 if (stage & RAZOR_STAGE_SCRIPTS_PRE) {
1257 retval = run_script(&installer, RPMTAG_PREINPROG, RPMTAG_PREIN,
1258 install_count, &tmp_error);
1260 razor_atomic_propagate_error(atomic, tmp_error, NULL);
1265 if (!retval && (stage & RAZOR_STAGE_FILES)) {
1266 if (installer_init(&installer))
1269 while (installer.stream.avail_in > 0) {
1270 installer.rest = sizeof *header;
1271 if (installer_inflate(&installer))
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);
1281 fixed_hex_to_ulong(header->namesize,
1282 sizeof header->namesize);
1284 if (installer_inflate(&installer) ||
1285 installer_align(&installer, 4))
1288 path = (const char *) installer.buffer;
1289 /* This convention is so lame... */
1290 if (strcmp(path, "TRAILER!!!") == 0)
1293 installer.rest = filesize;
1295 if (rpm->relocations)
1296 path = razor_relocations_apply(rpm->relocations,
1298 if (create_path(&installer, path, mode))
1300 if (installer_align(&installer, 4))
1304 if (installer_finish(&installer))
1307 retval = razor_atomic_in_error_state(atomic);
1310 if (!retval && (stage & RAZOR_STAGE_SCRIPTS_POST)) {
1311 retval = run_script(&installer, RPMTAG_POSTINPROG,
1312 RPMTAG_POSTIN, install_count, &tmp_error);
1314 razor_atomic_propagate_error(atomic, tmp_error, NULL);
1323 razor_rpm_close(struct razor_rpm *rpm)
1327 assert (rpm != NULL);
1330 free(rpm->prefixes);
1331 err = razor_uri_free_contents(rpm->map, rpm->size);
1339 razor_importer_add_rpm(struct razor_importer *importer, struct razor_rpm *rpm)
1341 const char *name, *version, *arch;
1342 const char *summary, *description, *url, *license;
1344 assert (importer != NULL);
1345 assert (rpm != NULL);
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,
1357 razor_importer_begin_package(importer, name, version, arch);
1359 razor_importer_add_details(importer, summary, description, url,
1362 import_properties(importer, RAZOR_PROPERTY_REQUIRES, rpm,
1364 RPMTAG_REQUIREVERSION,
1365 RPMTAG_REQUIREFLAGS);
1367 import_properties(importer, RAZOR_PROPERTY_PROVIDES, rpm,
1369 RPMTAG_PROVIDEVERSION,
1370 RPMTAG_PROVIDEFLAGS);
1372 import_properties(importer, RAZOR_PROPERTY_OBSOLETES, rpm,
1373 RPMTAG_OBSOLETENAME,
1374 RPMTAG_OBSOLETEVERSION,
1375 RPMTAG_OBSOLETEFLAGS);
1377 import_properties(importer, RAZOR_PROPERTY_CONFLICTS, rpm,
1378 RPMTAG_CONFLICTNAME,
1379 RPMTAG_CONFLICTVERSION,
1380 RPMTAG_CONFLICTFLAGS);
1382 import_files(importer, rpm);
1384 razor_importer_finish_package(importer);