summaryrefslogtreecommitdiff
path: root/dpkg-split/info.c
blob: 58d115365de3747586d1abaaf66043a6e28bc1e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
/*
 * dpkg-split - splitting and joining of multipart *.deb archives
 * info.c - information about split archives
 *
 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
 * Copyright © 2008-2012 Guillem Jover <guillem@debian.org>
 *
 * This is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

#include <config.h>
#include <compat.h>

#include <sys/stat.h>

#include <errno.h>
#include <limits.h>
#include <string.h>
#include <unistd.h>
#include <ar.h>
#include <inttypes.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>

#include <dpkg/i18n.h>
#include <dpkg/c-ctype.h>
#include <dpkg/dpkg.h>
#include <dpkg/dpkg-db.h>
#include <dpkg/fdio.h>
#include <dpkg/ar.h>
#include <dpkg/options.h>

#include "dpkg-split.h"

static intmax_t
parse_intmax(const char *value, const char *fn, const char *what)
{
  intmax_t r;
  char *endp;

  errno = 0;
  r = strtoimax(value, &endp, 10);
  if (value == endp || *endp)
    ohshit(_("file '%.250s' is corrupt - bad digit (code %d) in %s"),
           fn, *endp, what);
  if (r < 0 || errno == ERANGE)
    ohshit(_("file '%s' is corrupt; out of range integer in %s"), fn, what);
  return r;
}

static char *nextline(char **ripp, const char *fn, const char *what) {
  char *newline, *rip;

  rip= *ripp;
  if (!rip)
    ohshit(_("file '%.250s' is corrupt - %.250s missing"), fn, what);
  newline= strchr(rip,'\n');
  if (!newline)
    ohshit(_("file '%.250s' is corrupt - missing newline after %.250s"),
           fn, what);
  *ripp= newline+1;
  while (newline > rip && c_isspace(newline[-1]))
    newline--;
  *newline = '\0';
  return rip;
}

/**
 * Read a deb-split part archive.
 *
 * @return Part info (nfmalloc'd) if was an archive part and we read it,
 *         NULL if it wasn't.
 */
struct partinfo *
read_info(struct dpkg_ar *ar, struct partinfo *ir)
{
  static char *readinfobuf= NULL;
  static size_t readinfobuflen= 0;

  size_t thisilen;
  intmax_t templong;
  char magicbuf[sizeof(DPKG_AR_MAGIC) - 1], *rip, *partnums, *slash;
  const char *err;
  struct dpkg_ar_hdr arh;
  ssize_t rc;

  rc = fd_read(ar->fd, magicbuf, sizeof(magicbuf));
  if (rc != sizeof(magicbuf)) {
    if (rc < 0)
      ohshite(_("error reading %.250s"), ar->name);
    else
      return NULL;
  }
  if (memcmp(magicbuf, DPKG_AR_MAGIC, sizeof(magicbuf)))
    return NULL;

  rc = fd_read(ar->fd, &arh, sizeof(arh));
  if (rc != sizeof(arh))
    read_fail(rc, ar->name, "ar header");

  dpkg_ar_normalize_name(&arh);

  if (strncmp(arh.ar_name, PARTMAGIC, sizeof(arh.ar_name)) != 0)
    return NULL;
  if (dpkg_ar_member_is_illegal(&arh))
    ohshit(_("file '%.250s' is corrupt - bad magic at end of first header"),
           ar->name);
  thisilen = dpkg_ar_member_get_size(ar, &arh);
  if (thisilen >= readinfobuflen) {
    readinfobuflen = thisilen + 2;
    readinfobuf= m_realloc(readinfobuf,readinfobuflen);
  }
  rc = fd_read(ar->fd, readinfobuf, thisilen + (thisilen & 1));
  if (rc != (ssize_t)(thisilen + (thisilen & 1)))
    read_fail(rc, ar->name, "reading header member");
  if (thisilen & 1) {
    int c = readinfobuf[thisilen + 1];

    if (c != '\n')
      ohshit(_("file '%.250s' is corrupt - bad padding character (code %d)"),
             ar->name, c);
  }
  readinfobuf[thisilen] = '\0';
  if (memchr(readinfobuf,0,thisilen))
    ohshit(_("file '%.250s' is corrupt - nulls in info section"), ar->name);

  ir->filename = ar->name;

  rip= readinfobuf;
  err = deb_version_parse(&ir->fmtversion,
                          nextline(&rip, ar->name, _("format version number")));
  if (err)
    ohshit(_("file '%.250s' has invalid format version: %s"), ar->name, err);
  if (ir->fmtversion.major != 2)
    ohshit(_("file '%.250s' is format version %d.%d; get a newer dpkg-split"),
           ar->name, ir->fmtversion.major, ir->fmtversion.minor);

  ir->package = nfstrsave(nextline(&rip, ar->name, _("package name")));
  ir->version = nfstrsave(nextline(&rip, ar->name, _("package version number")));
  ir->md5sum = nfstrsave(nextline(&rip, ar->name, _("package file MD5 checksum")));
  if (strlen(ir->md5sum) != MD5HASHLEN ||
      strspn(ir->md5sum, "0123456789abcdef") != MD5HASHLEN)
    ohshit(_("file '%.250s' is corrupt - bad MD5 checksum '%.250s'"),
           ar->name, ir->md5sum);

  ir->orglength = parse_intmax(nextline(&rip, ar->name, _("archive total size")),
                               ar->name, _("archive total size"));
  ir->maxpartlen = parse_intmax(nextline(&rip, ar->name, _("archive part offset")),
                                ar->name, _("archive part offset"));

  partnums = nextline(&rip, ar->name, _("archive part numbers"));
  slash= strchr(partnums,'/');
  if (!slash)
    ohshit(_("file '%.250s' is corrupt - no slash between archive part numbers"), ar->name);
  *slash++ = '\0';

  templong = parse_intmax(slash, ar->name, _("number of archive parts"));
  if (templong <= 0 || templong > INT_MAX)
    ohshit(_("file '%.250s' is corrupt - bad number of archive parts"), ar->name);
  ir->maxpartn= templong;
  templong = parse_intmax(partnums, ar->name, _("archive parts number"));
  if (templong <= 0 || templong > ir->maxpartn)
    ohshit(_("file '%.250s' is corrupt - bad archive part number"), ar->name);
  ir->thispartn= templong;

  /* If the package was created with dpkg 1.16.1 or later it will include
   * the architecture. */
  if (*rip != '\0')
    ir->arch = nfstrsave(nextline(&rip, ar->name, _("package architecture")));
  else
    ir->arch = NULL;

  rc = fd_read(ar->fd, &arh, sizeof(arh));
  if (rc != sizeof(arh))
    read_fail(rc, ar->name, "reading data part member ar header");

  dpkg_ar_normalize_name(&arh);

  if (dpkg_ar_member_is_illegal(&arh))
    ohshit(_("file '%.250s' is corrupt - bad magic at end of second header"),
           ar->name);
  if (strncmp(arh.ar_name,"data",4))
    ohshit(_("file '%.250s' is corrupt - second member is not data member"),
           ar->name);

  ir->thispartlen = dpkg_ar_member_get_size(ar, &arh);
  ir->thispartoffset= (ir->thispartn-1)*ir->maxpartlen;

  if (ir->maxpartn != (ir->orglength+ir->maxpartlen-1)/ir->maxpartlen)
    ohshit(_("file '%.250s' is corrupt - wrong number of parts for quoted sizes"),
           ar->name);
  if (ir->thispartlen !=
      (ir->thispartn == ir->maxpartn
       ? ir->orglength - ir->thispartoffset : ir->maxpartlen))
    ohshit(_("file '%.250s' is corrupt - size is wrong for quoted part number"),
           ar->name);

  ir->filesize = (strlen(DPKG_AR_MAGIC) +
                  sizeof(arh) + thisilen + (thisilen & 1) +
                  sizeof(arh) + ir->thispartlen + (ir->thispartlen & 1));

  if (S_ISREG(ar->mode)) {
    /* Don't do this check if it's coming from a pipe or something.  It's
     * only an extra sanity check anyway. */
    if (ar->size < ir->filesize)
      ohshit(_("file '%.250s' is corrupt - too short"), ar->name);
  }

  ir->headerlen = strlen(DPKG_AR_MAGIC) +
                  sizeof(arh) + thisilen + (thisilen & 1) + sizeof(arh);

  return ir;
}

void mustgetpartinfo(const char *filename, struct partinfo *ri) {
  struct dpkg_ar *part;

  part = dpkg_ar_open(filename);
  if (!part)
    ohshite(_("cannot open archive part file '%.250s'"), filename);
  if (!read_info(part, ri))
    ohshite(_("file '%.250s' is not an archive part"), filename);
  dpkg_ar_close(part);
}

void print_info(const struct partinfo *pi) {
  printf(_("%s:\n"
         "    Part format version:            %d.%d\n"
         "    Part of package:                %s\n"
         "        ... version:                %s\n"
         "        ... architecture:           %s\n"
         "        ... MD5 checksum:           %s\n"
         "        ... length:                 %jd bytes\n"
         "        ... split every:            %jd bytes\n"
         "    Part number:                    %d/%d\n"
         "    Part length:                    %jd bytes\n"
         "    Part offset:                    %jd bytes\n"
         "    Part file size (used portion):  %jd bytes\n\n"),
         pi->filename,
         pi->fmtversion.major, pi->fmtversion.minor,
         pi->package,
         pi->version,
         pi->arch ? pi->arch : C_("architecture", "<unknown>"),
         pi->md5sum,
         (intmax_t)pi->orglength,
         (intmax_t)pi->maxpartlen,
         pi->thispartn,
         pi->maxpartn,
         (intmax_t)pi->thispartlen,
         (intmax_t)pi->thispartoffset,
         (intmax_t)pi->filesize);
}

int
do_info(const char *const *argv)
{
  const char *thisarg;
  struct partinfo *pi, ps;
  struct dpkg_ar *part;

  if (!*argv)
    badusage(_("--%s requires one or more part file arguments"),
             cipaction->olong);

  while ((thisarg= *argv++)) {
    part = dpkg_ar_open(thisarg);
    if (!part)
      ohshite(_("cannot open archive part file '%.250s'"), thisarg);
    pi = read_info(part, &ps);
    dpkg_ar_close(part);
    if (pi) {
      print_info(pi);
    } else {
      printf(_("file '%s' is not an archive part\n"), thisarg);
    }
    m_output(stdout, _("<standard output>"));
  }

  return 0;
}