Branch data Line data Source code
1 : : /*
2 : : * dpkg-split - splitting and joining of multipart *.deb archives
3 : : * info.c - information about split archives
4 : : *
5 : : * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 : : * Copyright © 2008-2012 Guillem Jover <guillem@debian.org>
7 : : *
8 : : * This is free software; you can redistribute it and/or modify
9 : : * it under the terms of the GNU General Public License as published by
10 : : * the Free Software Foundation; either version 2 of the License, or
11 : : * (at your option) any later version.
12 : : *
13 : : * This is distributed in the hope that it will be useful,
14 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 : : * GNU General Public License for more details.
17 : : *
18 : : * You should have received a copy of the GNU General Public License
19 : : * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 : : */
21 : :
22 : : #include <config.h>
23 : : #include <compat.h>
24 : :
25 : : #include <sys/stat.h>
26 : :
27 : : #include <errno.h>
28 : : #include <limits.h>
29 : : #include <string.h>
30 : : #include <unistd.h>
31 : : #include <inttypes.h>
32 : : #include <stdint.h>
33 : : #include <stdlib.h>
34 : : #include <stdio.h>
35 : :
36 : : #include <dpkg/i18n.h>
37 : : #include <dpkg/c-ctype.h>
38 : : #include <dpkg/dpkg.h>
39 : : #include <dpkg/dpkg-db.h>
40 : : #include <dpkg/fdio.h>
41 : : #include <dpkg/ar.h>
42 : : #include <dpkg/options.h>
43 : :
44 : : #include "dpkg-split.h"
45 : :
46 : : static intmax_t
47 : 76 : parse_intmax(const char *value, const char *fn, const char *what)
48 : : {
49 : : intmax_t ret;
50 : : char *endp;
51 : :
52 : 76 : errno = 0;
53 : 76 : ret = strtoimax(value, &endp, 10);
54 [ + - - + ]: 76 : if (value == endp || *endp)
55 : 0 : ohshit(_("file '%.250s' is corrupt - bad digit (code %d) in %s"),
56 : 0 : fn, *endp, what);
57 [ + - - + ]: 76 : if (ret < 0 || errno == ERANGE)
58 : 0 : ohshit(_("file '%s' is corrupt; out of range integer in %s"), fn, what);
59 : :
60 : 76 : return ret;
61 : : }
62 : :
63 : 152 : static char *nextline(char **ripp, const char *fn, const char *what) {
64 : : char *newline, *rip;
65 : :
66 : 152 : rip= *ripp;
67 [ - + ]: 152 : if (!rip)
68 : 0 : ohshit(_("file '%.250s' is corrupt - %.250s missing"), fn, what);
69 : 152 : newline= strchr(rip,'\n');
70 [ - + ]: 152 : if (!newline)
71 : 0 : ohshit(_("file '%.250s' is corrupt - missing newline after %.250s"),
72 : : fn, what);
73 : 152 : *ripp= newline+1;
74 : :
75 : 152 : str_rtrim_spaces(rip, newline);
76 : :
77 : 152 : return rip;
78 : : }
79 : :
80 : : /**
81 : : * Read a deb-split part archive.
82 : : *
83 : : * @return Part info (nfmalloc'd) if was an archive part and we read it,
84 : : * NULL if it wasn't.
85 : : */
86 : : struct partinfo *
87 : 20 : read_info(struct dpkg_ar *ar, struct partinfo *ir)
88 : : {
89 : : static struct varbuf format_member = VARBUF_INIT;
90 : :
91 : : size_t thisilen;
92 : : intmax_t templong;
93 : : char magicbuf[sizeof(DPKG_AR_MAGIC) - 1], *rip, *partnums, *slash;
94 : : const char *err;
95 : : struct dpkg_ar_hdr arh;
96 : : ssize_t rc;
97 : :
98 : 20 : rc = fd_read(ar->fd, magicbuf, sizeof(magicbuf));
99 [ - + ]: 20 : if (rc != sizeof(magicbuf)) {
100 [ # # ]: 0 : if (rc < 0)
101 : 0 : ohshite(_("error reading %.250s"), ar->name);
102 : : else
103 : 0 : return NULL;
104 : : }
105 [ - + ]: 20 : if (memcmp(magicbuf, DPKG_AR_MAGIC, sizeof(magicbuf)))
106 : 0 : return NULL;
107 : :
108 : 20 : rc = fd_read(ar->fd, &arh, sizeof(arh));
109 [ - + ]: 20 : if (rc != sizeof(arh))
110 : 0 : read_fail(rc, ar->name, "ar header");
111 : :
112 : 20 : dpkg_ar_normalize_name(&arh);
113 : :
114 [ + + ]: 20 : if (strncmp(arh.ar_name, PARTMAGIC, sizeof(arh.ar_name)) != 0)
115 : 1 : return NULL;
116 [ - + ]: 19 : if (dpkg_ar_member_is_illegal(&arh))
117 : 0 : ohshit(_("file '%.250s' is corrupt - bad magic at end of first header"),
118 : : ar->name);
119 : 19 : thisilen = dpkg_ar_member_get_size(ar, &arh);
120 : :
121 : 19 : varbuf_reset(&format_member);
122 : 19 : varbuf_grow(&format_member, thisilen + 2);
123 : :
124 : 19 : rc = fd_read(ar->fd, format_member.buf, thisilen + (thisilen & 1));
125 [ - + ]: 19 : if (rc != (ssize_t)(thisilen + (thisilen & 1)))
126 : 0 : read_fail(rc, ar->name, "reading header member");
127 [ + + ]: 19 : if (thisilen & 1) {
128 : 16 : int c = format_member.buf[thisilen];
129 : :
130 [ - + ]: 16 : if (c != '\n')
131 : 0 : ohshit(_("file '%.250s' is corrupt - bad padding character (code %d)"),
132 : : ar->name, c);
133 : : }
134 : 19 : varbuf_trunc(&format_member, thisilen);
135 [ - + ]: 19 : if (memchr(format_member.buf, 0, thisilen))
136 : 0 : ohshit(_("file '%.250s' is corrupt - nulls in info section"), ar->name);
137 : :
138 : 19 : ir->filename = ar->name;
139 : :
140 : 19 : rip = format_member.buf;
141 : 19 : err = deb_version_parse(&ir->fmtversion,
142 : 19 : nextline(&rip, ar->name, _("format version number")));
143 [ - + ]: 19 : if (err)
144 : 0 : ohshit(_("file '%.250s' has invalid format version: %s"), ar->name, err);
145 [ - + ]: 19 : if (ir->fmtversion.major != 2)
146 : 0 : ohshit(_("file '%.250s' is format version %d.%d; get a newer dpkg-split"),
147 : : ar->name, ir->fmtversion.major, ir->fmtversion.minor);
148 : :
149 : 19 : ir->package = nfstrsave(nextline(&rip, ar->name, _("package name")));
150 : 19 : ir->version = nfstrsave(nextline(&rip, ar->name, _("package version number")));
151 : 19 : ir->md5sum = nfstrsave(nextline(&rip, ar->name, _("package file MD5 checksum")));
152 [ + - ]: 19 : if (strlen(ir->md5sum) != MD5HASHLEN ||
153 [ - + ]: 19 : strspn(ir->md5sum, "0123456789abcdef") != MD5HASHLEN)
154 : 0 : ohshit(_("file '%.250s' is corrupt - bad MD5 checksum '%.250s'"),
155 : : ar->name, ir->md5sum);
156 : :
157 : 19 : ir->orglength = parse_intmax(nextline(&rip, ar->name, _("archive total size")),
158 : 19 : ar->name, _("archive total size"));
159 : 19 : ir->maxpartlen = parse_intmax(nextline(&rip, ar->name, _("archive part offset")),
160 : 19 : ar->name, _("archive part offset"));
161 : :
162 : 19 : partnums = nextline(&rip, ar->name, _("archive part numbers"));
163 : 19 : slash= strchr(partnums,'/');
164 [ - + ]: 19 : if (!slash)
165 : 0 : ohshit(_("file '%.250s' is corrupt - no slash between archive part numbers"), ar->name);
166 : 19 : *slash++ = '\0';
167 : :
168 : 19 : templong = parse_intmax(slash, ar->name, _("number of archive parts"));
169 [ + - - + ]: 19 : if (templong <= 0 || templong > INT_MAX)
170 : 0 : ohshit(_("file '%.250s' is corrupt - bad number of archive parts"), ar->name);
171 : 19 : ir->maxpartn= templong;
172 : 19 : templong = parse_intmax(partnums, ar->name, _("archive parts number"));
173 [ + - - + ]: 19 : if (templong <= 0 || templong > ir->maxpartn)
174 : 0 : ohshit(_("file '%.250s' is corrupt - bad archive part number"), ar->name);
175 : 19 : ir->thispartn= templong;
176 : :
177 : : /* If the package was created with dpkg 1.16.1 or later it will include
178 : : * the architecture. */
179 [ + - ]: 19 : if (*rip != '\0')
180 : 19 : ir->arch = nfstrsave(nextline(&rip, ar->name, _("package architecture")));
181 : : else
182 : 0 : ir->arch = NULL;
183 : :
184 : 19 : rc = fd_read(ar->fd, &arh, sizeof(arh));
185 [ + + ]: 19 : if (rc != sizeof(arh))
186 : 1 : read_fail(rc, ar->name, "reading data part member ar header");
187 : :
188 : 18 : dpkg_ar_normalize_name(&arh);
189 : :
190 [ - + ]: 18 : if (dpkg_ar_member_is_illegal(&arh))
191 : 0 : ohshit(_("file '%.250s' is corrupt - bad magic at end of second header"),
192 : : ar->name);
193 [ + + ]: 18 : if (strncmp(arh.ar_name,"data",4))
194 : 1 : ohshit(_("file '%.250s' is corrupt - second member is not data member"),
195 : : ar->name);
196 : :
197 : 17 : ir->thispartlen = dpkg_ar_member_get_size(ar, &arh);
198 : 17 : ir->thispartoffset= (ir->thispartn-1)*ir->maxpartlen;
199 : :
200 [ - + ]: 17 : if (ir->maxpartn != (ir->orglength+ir->maxpartlen-1)/ir->maxpartlen)
201 : 0 : ohshit(_("file '%.250s' is corrupt - wrong number of parts for quoted sizes"),
202 : : ar->name);
203 [ - + ]: 17 : if (ir->thispartlen !=
204 : 17 : (ir->thispartn == ir->maxpartn
205 [ + + ]: 17 : ? ir->orglength - ir->thispartoffset : ir->maxpartlen))
206 : 0 : ohshit(_("file '%.250s' is corrupt - size is wrong for quoted part number"),
207 : : ar->name);
208 : :
209 : 17 : ir->filesize = (strlen(DPKG_AR_MAGIC) +
210 : 17 : sizeof(arh) + thisilen + (thisilen & 1) +
211 : 17 : sizeof(arh) + ir->thispartlen + (ir->thispartlen & 1));
212 : :
213 [ + - ]: 17 : if (S_ISREG(ar->mode)) {
214 : : /* Don't do this check if it's coming from a pipe or something. It's
215 : : * only an extra sanity check anyway. */
216 [ - + ]: 17 : if (ar->size < ir->filesize)
217 : 0 : ohshit(_("file '%.250s' is corrupt - too short"), ar->name);
218 : : }
219 : :
220 : 17 : ir->headerlen = strlen(DPKG_AR_MAGIC) +
221 : 17 : sizeof(arh) + thisilen + (thisilen & 1) + sizeof(arh);
222 : :
223 : 17 : return ir;
224 : : }
225 : :
226 : 10 : void mustgetpartinfo(const char *filename, struct partinfo *ri) {
227 : : struct dpkg_ar *part;
228 : :
229 : 10 : part = dpkg_ar_open(filename);
230 [ - + ]: 10 : if (!part)
231 : 0 : ohshite(_("cannot open archive part file '%.250s'"), filename);
232 [ - + ]: 10 : if (!read_info(part, ri))
233 : 0 : ohshite(_("file '%.250s' is not an archive part"), filename);
234 : 10 : dpkg_ar_close(part);
235 : 10 : }
236 : :
237 : 7 : void print_info(const struct partinfo *pi) {
238 : 7 : printf(_("%s:\n"
239 : : " Part format version: %d.%d\n"
240 : : " Part of package: %s\n"
241 : : " ... version: %s\n"
242 : : " ... architecture: %s\n"
243 : : " ... MD5 checksum: %s\n"
244 : : " ... length: %jd bytes\n"
245 : : " ... split every: %jd bytes\n"
246 : : " Part number: %d/%d\n"
247 : : " Part length: %jd bytes\n"
248 : : " Part offset: %jd bytes\n"
249 : : " Part file size (used portion): %jd bytes\n\n"),
250 : 7 : pi->filename,
251 : 7 : pi->fmtversion.major, pi->fmtversion.minor,
252 : 7 : pi->package,
253 : 7 : pi->version,
254 : 0 : pi->arch ? pi->arch : C_("architecture", "<unknown>"),
255 : 7 : pi->md5sum,
256 : 7 : (intmax_t)pi->orglength,
257 : 7 : (intmax_t)pi->maxpartlen,
258 : 7 : pi->thispartn,
259 : 7 : pi->maxpartn,
260 : 7 : (intmax_t)pi->thispartlen,
261 : 7 : (intmax_t)pi->thispartoffset,
262 [ + - ]: 7 : (intmax_t)pi->filesize);
263 : 7 : }
264 : :
265 : : int
266 : 10 : do_info(const char *const *argv)
267 : : {
268 : : const char *thisarg;
269 : :
270 [ - + ]: 10 : if (!*argv)
271 : 0 : badusage(_("--%s requires one or more part file arguments"),
272 : 0 : cipaction->olong);
273 : :
274 [ + + ]: 18 : while ((thisarg= *argv++)) {
275 : : struct partinfo *pi, ps;
276 : : struct dpkg_ar *part;
277 : :
278 : 10 : part = dpkg_ar_open(thisarg);
279 [ - + ]: 10 : if (!part)
280 : 0 : ohshite(_("cannot open archive part file '%.250s'"), thisarg);
281 : 10 : pi = read_info(part, &ps);
282 : 8 : dpkg_ar_close(part);
283 [ + + ]: 8 : if (pi) {
284 : 7 : print_info(pi);
285 : : } else {
286 : 1 : printf(_("file '%s' is not an archive part\n"), thisarg);
287 : : }
288 : 8 : m_output(stdout, _("<standard output>"));
289 : : }
290 : :
291 : 8 : return 0;
292 : : }
|