ee5087f0aaabdbd72f006eaaa2c0edccfc61833f
[oweals/busybox.git] / archival / rpm.c
1 /* vi: set sw=4 ts=4: */
2 /*
3  * Mini rpm applet for busybox
4  *
5  * Copyright (C) 2001,2002 by Laurence Anderson
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  */
21
22 #include "busybox.h"
23 #include "unarchive.h"
24 #include <stdio.h>
25 #include <unistd.h>
26 #include <signal.h>
27 #include <stdlib.h>
28 #include <fcntl.h>
29 #include <netinet/in.h> /* For ntohl & htonl function */
30 #include <string.h> /* For strncmp */
31 #include <sys/mman.h> /* For mmap */
32 #include <time.h> /* For ctime */
33
34 #define RPM_HEADER_MAGIC "\216\255\350"
35 #define RPM_CHAR_TYPE           1
36 #define RPM_INT8_TYPE           2
37 #define RPM_INT16_TYPE          3
38 #define RPM_INT32_TYPE          4
39 /* #define RPM_INT64_TYPE       5   ---- These aren't supported (yet) */
40 #define RPM_STRING_TYPE         6
41 #define RPM_BIN_TYPE            7
42 #define RPM_STRING_ARRAY_TYPE   8
43 #define RPM_I18NSTRING_TYPE     9
44
45 #define RPMTAG_NAME                     1000
46 #define RPMTAG_VERSION                  1001
47 #define RPMTAG_RELEASE                  1002
48 #define RPMTAG_SUMMARY                  1004
49 #define RPMTAG_DESCRIPTION              1005
50 #define RPMTAG_BUILDTIME                1006
51 #define RPMTAG_BUILDHOST                1007
52 #define RPMTAG_SIZE                     1009
53 #define RPMTAG_VENDOR                   1011
54 #define RPMTAG_LICENSE                  1014
55 #define RPMTAG_PACKAGER                 1015
56 #define RPMTAG_GROUP                    1016
57 #define RPMTAG_URL                      1020
58 #define RPMTAG_PREIN                    1023
59 #define RPMTAG_POSTIN                   1024
60 #define RPMTAG_FILEFLAGS                1037
61 #define RPMTAG_FILEUSERNAME             1039
62 #define RPMTAG_FILEGROUPNAME            1040
63 #define RPMTAG_SOURCERPM                1044
64 #define RPMTAG_PREINPROG                1085
65 #define RPMTAG_POSTINPROG               1086
66 #define RPMTAG_PREFIXS                  1098
67 #define RPMTAG_DIRINDEXES               1116
68 #define RPMTAG_BASENAMES                1117
69 #define RPMTAG_DIRNAMES                 1118
70 #define RPMFILE_CONFIG                  (1 << 0)
71 #define RPMFILE_DOC                     (1 << 1)
72
73 enum rpm_functions_e {
74         rpm_query = 1,
75         rpm_install = 2,
76         rpm_query_info = 4,
77         rpm_query_package = 8,
78         rpm_query_list = 16,
79         rpm_query_list_doc = 32,
80         rpm_query_list_config = 64
81 };
82
83 typedef struct {
84         u_int32_t tag; /* 4 byte tag */
85         u_int32_t type; /* 4 byte type */
86         u_int32_t offset; /* 4 byte offset */
87         u_int32_t count; /* 4 byte count */
88 } rpm_index;
89
90 static void *map;
91 static rpm_index **mytags;
92 static int tagcount;
93
94 void extract_cpio_gz(int fd);
95 rpm_index **rpm_gettags(int fd, int *num_tags);
96 int bsearch_rpmtag(const void *key, const void *item);
97 char *rpm_getstring(int tag, int itemindex);
98 int rpm_getint(int tag, int itemindex);
99 int rpm_getcount(int tag);
100 void exec_script(int progtag, int datatag, char *prefix);
101 void fileaction_dobackup(char *filename, int fileref);
102 void fileaction_setowngrp(char *filename, int fileref);
103 void fileaction_list(char *filename, int itemno);
104 void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref));
105
106 int rpm_main(int argc, char **argv)
107 {
108         int opt = 0, func = 0, rpm_fd, offset;
109
110         while ((opt = getopt(argc, argv, "iqpldc")) != -1) {
111                 switch (opt) {
112                 case 'i': // First arg: Install mode, with q: Information
113                         if (!func) func |= rpm_install;
114                         else func |= rpm_query_info;
115                         break;
116                 case 'q': // First arg: Query mode
117                         if (!func) func |= rpm_query;
118                         else show_usage();
119                         break;
120                 case 'p': // Query a package
121                         func |= rpm_query_package;
122                         break;
123                 case 'l': // List files in a package
124                         func |= rpm_query_list;
125                         break;
126                 case 'd': // List doc files in a package (implies list)
127                         func |= rpm_query_list;
128                         func |= rpm_query_list_doc;
129                         break;
130                 case 'c': // List config files in a package (implies list)
131                         func |= rpm_query_list;
132                         func |= rpm_query_list_config;
133                         break;
134                 default:
135                         show_usage();
136                 }
137         }
138
139         if (optind == argc) show_usage();
140         while (optind < argc) {
141                 rpm_fd = xopen(argv[optind], O_RDONLY);
142                 mytags = rpm_gettags(rpm_fd, (int *) &tagcount);
143                 offset = lseek(rpm_fd, 0, SEEK_CUR);
144                 if (!mytags) { printf("Error reading rpm header\n"); exit(-1); }
145                 map = mmap(0, offset > getpagesize() ? (offset + offset % getpagesize()) : getpagesize(), PROT_READ, MAP_SHARED, rpm_fd, 0); // Mimimum is one page
146                 if (func & rpm_install) {
147                         loop_through_files(RPMTAG_BASENAMES, fileaction_dobackup); /* Backup any config files */
148                         extract_cpio_gz(rpm_fd); // Extact the archive
149                         loop_through_files(RPMTAG_BASENAMES, fileaction_setowngrp); /* Set the correct file uid/gid's */
150                 } else if (func & rpm_query && func & rpm_query_package) {
151                         if (!((func & rpm_query_info) || (func & rpm_query_list))) { // If just a straight query, just give package name
152                                 printf("%s-%s-%s\n", rpm_getstring(RPMTAG_NAME, 0), rpm_getstring(RPMTAG_VERSION, 0), rpm_getstring(RPMTAG_RELEASE, 0));
153                         }
154                         if (func & rpm_query_info) {
155                                 /* Do the nice printout */
156                                 time_t bdate_time;
157                                 struct tm *bdate;
158                                 char bdatestring[50];
159                                 printf("Name        : %-29sRelocations: %s\n", rpm_getstring(RPMTAG_NAME, 0), rpm_getstring(RPMTAG_PREFIXS, 0) ? rpm_getstring(RPMTAG_PREFIXS, 0) : "(not relocateable)");
160                                 printf("Version     : %-34sVendor: %s\n", rpm_getstring(RPMTAG_VERSION, 0), rpm_getstring(RPMTAG_VENDOR, 0) ? rpm_getstring(RPMTAG_VENDOR, 0) : "(none)");
161                                 bdate_time = rpm_getint(RPMTAG_BUILDTIME, 0);
162                                 bdate = localtime((time_t *) &bdate_time);
163                                 strftime(bdatestring, 50, "%a %d %b %Y %T %Z", bdate);
164                                 printf("Release     : %-30sBuild Date: %s\n", rpm_getstring(RPMTAG_RELEASE, 0), bdatestring);
165                                 printf("Install date: %-30sBuild Host: %s\n", "(not installed)", rpm_getstring(RPMTAG_BUILDHOST, 0));
166                                 printf("Group       : %-30sSource RPM: %s\n", rpm_getstring(RPMTAG_GROUP, 0), rpm_getstring(RPMTAG_SOURCERPM, 0));
167                                 printf("Size        : %-33dLicense: %s\n", rpm_getint(RPMTAG_SIZE, 0), rpm_getstring(RPMTAG_LICENSE, 0));
168                                 printf("URL         : %s\n", rpm_getstring(RPMTAG_URL, 0));
169                                 printf("Summary     : %s\n", rpm_getstring(RPMTAG_SUMMARY, 0));
170                                 printf("Description :\n%s\n", rpm_getstring(RPMTAG_DESCRIPTION, 0));
171                         }
172                         if (func & rpm_query_list) {
173                                 int count, it, flags;
174                                 count = rpm_getcount(RPMTAG_BASENAMES);
175                                 for (it = 0; it < count; it++) {
176                                         flags = rpm_getint(RPMTAG_FILEFLAGS, it);
177                                         switch ((func & rpm_query_list_doc) + (func & rpm_query_list_config))
178                                         {
179                                                 case rpm_query_list_doc: if (!(flags & RPMFILE_DOC)) continue; break;
180                                                 case rpm_query_list_config: if (!(flags & RPMFILE_CONFIG)) continue; break;
181                                                 case rpm_query_list_doc + rpm_query_list_config: if (!((flags & RPMFILE_CONFIG) || (flags & RPMFILE_DOC))) continue; break;
182                                         }
183                                         printf("%s%s\n", rpm_getstring(RPMTAG_DIRNAMES, rpm_getint(RPMTAG_DIRINDEXES, it)), rpm_getstring(RPMTAG_BASENAMES, it));
184                                 }
185                         }
186                 }
187                 optind++;
188                 free (mytags);
189         }
190         return 0;
191 }
192
193 void extract_cpio_gz(int fd) {
194         archive_handle_t *archive_handle;
195         unsigned char magic[2];
196
197         /* Initialise */
198         archive_handle = init_handle();
199         archive_handle->read = read_gz;
200         archive_handle->seek = seek_by_char;
201         //archive_handle->action_header = header_list;
202         archive_handle->action_data = data_extract_all;
203         archive_handle->flags |= ARCHIVE_PRESERVE_DATE;
204         archive_handle->flags |= ARCHIVE_CREATE_LEADING_DIRS;
205         archive_handle->src_fd = fd;
206         archive_handle->offset = 0;
207         
208         xread_all(archive_handle->src_fd, &magic, 2);
209         if ((magic[0] != 0x1f) || (magic[1] != 0x8b)) {
210                 error_msg_and_die("Invalid gzip magic");
211         }
212         check_header_gzip(archive_handle->src_fd);      
213         chdir("/"); // Install RPM's to root
214
215         GZ_gzReadOpen(archive_handle->src_fd, 0, 0);
216         while (get_header_cpio(archive_handle) == EXIT_SUCCESS);
217         GZ_gzReadClose();
218         
219         check_trailer_gzip(archive_handle->src_fd);
220 }
221
222
223 rpm_index **rpm_gettags(int fd, int *num_tags)
224 {
225         rpm_index **tags = calloc(200, sizeof(struct rpmtag *)); /* We should never need mode than 200, and realloc later */
226         int pass, tagindex = 0;
227         lseek(fd, 96, SEEK_CUR); /* Seek past the unused lead */
228
229         for (pass = 0; pass < 2; pass++) { /* 1st pass is the signature headers, 2nd is the main stuff */
230                 struct {
231                         char magic[3]; /* 3 byte magic: 0x8e 0xad 0xe8 */
232                         u_int8_t version; /* 1 byte version number */
233                         u_int32_t reserved; /* 4 bytes reserved */
234                         u_int32_t entries; /* Number of entries in header (4 bytes) */
235                         u_int32_t size; /* Size of store (4 bytes) */
236                 } header;
237                 rpm_index *tmpindex;
238                 int storepos;
239
240                 read(fd, &header, sizeof(header));
241                 if (strncmp((char *) &header.magic, RPM_HEADER_MAGIC, 3) != 0) return NULL; /* Invalid magic */
242                 if (header.version != 1) return NULL; /* This program only supports v1 headers */
243                 header.size = ntohl(header.size);
244                 header.entries = ntohl(header.entries);
245                 storepos = lseek(fd,0,SEEK_CUR) + header.entries * 16;
246
247                 while (header.entries--) {
248                         tmpindex = tags[tagindex++] = malloc(sizeof(rpm_index));
249                         read(fd, tmpindex, sizeof(rpm_index));
250                         tmpindex->tag = ntohl(tmpindex->tag); tmpindex->type = ntohl(tmpindex->type); tmpindex->count = ntohl(tmpindex->count);
251                         tmpindex->offset = storepos + ntohl(tmpindex->offset);
252                         if (pass==0) tmpindex->tag -= 743;
253                 }
254                 lseek(fd, header.size, SEEK_CUR); /* Seek past store */
255                 if (pass==0) lseek(fd, (8 - (lseek(fd,0,SEEK_CUR) % 8)) % 8, SEEK_CUR); /* Skip padding to 8 byte boundary after reading signature headers */
256         }
257         tags = realloc(tags, tagindex * sizeof(struct rpmtag *)); /* realloc tags to save space */
258         *num_tags = tagindex;
259         return tags; /* All done, leave the file at the start of the gzipped cpio archive */
260 }
261
262 int bsearch_rpmtag(const void *key, const void *item)
263 {
264         rpm_index **tmp = (rpm_index **) item;
265         return ((int) key - tmp[0]->tag);
266 }
267
268 int rpm_getcount(int tag)
269 {
270         rpm_index **found;
271         found = bsearch((void *) tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
272         if (!found) return 0;
273         else return found[0]->count;
274 }
275
276 char *rpm_getstring(int tag, int itemindex)
277 {
278         rpm_index **found;
279         found = bsearch((void *) tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
280         if (!found || itemindex >= found[0]->count) return NULL;
281         if (found[0]->type == RPM_STRING_TYPE || found[0]->type == RPM_I18NSTRING_TYPE || found[0]->type == RPM_STRING_ARRAY_TYPE) {
282                 int n;
283                 char *tmpstr = (char *) (map + found[0]->offset);
284                 for (n=0; n < itemindex; n++) tmpstr = tmpstr + strlen(tmpstr) + 1;
285                 return tmpstr;
286         } else return NULL;
287 }
288
289 int rpm_getint(int tag, int itemindex)
290 {
291         rpm_index **found;
292         int n, *tmpint;
293         found = bsearch((void *) tag, mytags, tagcount, sizeof(struct rpmtag *), bsearch_rpmtag);
294         if (!found || itemindex >= found[0]->count) return -1;
295         tmpint = (int *) (map + found[0]->offset);
296         if (found[0]->type == RPM_INT32_TYPE) {
297                 for (n=0; n<itemindex; n++) tmpint = (int *) ((void *) tmpint + 4);
298                 return ntohl(*tmpint);
299         } else if (found[0]->type == RPM_INT16_TYPE) {
300                 for (n=0; n<itemindex; n++) tmpint = (int *) ((void *) tmpint + 2);
301                 return ntohs(*tmpint);
302         } else if (found[0]->type == RPM_INT8_TYPE) {
303                 for (n=0; n<itemindex; n++) tmpint = (int *) ((void *) tmpint + 1);
304                 return ntohs(*tmpint);
305         } else return -1;
306 }
307
308 void fileaction_dobackup(char *filename, int fileref)
309 {
310         struct stat oldfile;
311         int stat_res;
312         char *newname;
313         if (rpm_getint(RPMTAG_FILEFLAGS, fileref) & RPMFILE_CONFIG) { /* Only need to backup config files */
314                 stat_res = lstat (filename, &oldfile);
315                 if (stat_res == 0 && S_ISREG(oldfile.st_mode)) { /* File already exists  - really should check MD5's etc to see if different */
316                         newname = xstrdup(filename);
317                         newname = strcat(newname, ".rpmorig");
318                         copy_file(filename, newname, FILEUTILS_RECUR | FILEUTILS_PRESERVE_STATUS);
319                         remove_file(filename, FILEUTILS_RECUR | FILEUTILS_FORCE);
320                         free(newname);
321                 }
322         }
323 }
324
325 void fileaction_setowngrp(char *filename, int fileref)
326 {
327         int uid, gid;
328         uid = my_getpwnam(rpm_getstring(RPMTAG_FILEUSERNAME, fileref));
329         gid = my_getgrnam(rpm_getstring(RPMTAG_FILEGROUPNAME, fileref));
330         chown (filename, uid, gid);
331 }
332
333 void fileaction_list(char *filename, int fileref)
334 {
335         printf("%s\n", filename);
336 }
337
338 void loop_through_files(int filetag, void (*fileaction)(char *filename, int fileref))
339 {
340         int count = 0;
341         char *filename, *tmp_dirname, *tmp_basename;
342         while (rpm_getstring(filetag, count)) {
343                 tmp_dirname = rpm_getstring(RPMTAG_DIRNAMES, rpm_getint(RPMTAG_DIRINDEXES, count)); /* 1st put on the directory */
344                 tmp_basename = rpm_getstring(RPMTAG_BASENAMES, count);
345                 filename = xmalloc(strlen(tmp_basename) + strlen(tmp_dirname) + 1);
346                 strcpy(filename, tmp_dirname); /* First the directory name */
347                 strcat(filename, tmp_basename); /* then the filename */
348                 fileaction(filename, count++);
349                 free(filename);
350         }
351 }