1 /*
2  * libdpkg - Debian packaging suite library routines
3  * parse.c - database file parsing, main package/field loop
4  *
5  * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6  * Copyright © 2006, 2008-2015 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/types.h>
26 #include <sys/stat.h>
27 #ifdef USE_MMAP
28 #include <sys/mman.h>
29 #endif
30 
31 #include <fcntl.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <stdarg.h>
35 #include <stdlib.h>
36 #include <stdio.h>
37 
38 #include <dpkg/macros.h>
39 #include <dpkg/i18n.h>
40 #include <dpkg/c-ctype.h>
41 #include <dpkg/dpkg.h>
42 #include <dpkg/dpkg-db.h>
43 #include <dpkg/string.h>
44 #include <dpkg/pkg.h>
45 #include <dpkg/parsedump.h>
46 #include <dpkg/fdio.h>
47 #include <dpkg/buffer.h>
48 
49 /**
50  * Fields information.
51  */
52 const struct fieldinfo fieldinfos[]= {
53   /* Note: Capitalization of field name strings is important. */
54   { FIELD("Package"),          f_name,            w_name                                     },
55   { FIELD("Essential"),        f_boolean,         w_booleandefno,   PKGIFPOFF(essential)     },
56   { FIELD("Status"),           f_status,          w_status                                   },
57   { FIELD("Priority"),         f_priority,        w_priority                                 },
58   { FIELD("Section"),          f_section,         w_section                                  },
59   { FIELD("Installed-Size"),   f_charfield,       w_charfield,      PKGIFPOFF(installedsize) },
60   { FIELD("Origin"),           f_charfield,       w_charfield,      PKGIFPOFF(origin)        },
61   { FIELD("Maintainer"),       f_charfield,       w_charfield,      PKGIFPOFF(maintainer)    },
62   { FIELD("Bugs"),             f_charfield,       w_charfield,      PKGIFPOFF(bugs)          },
63   { FIELD("Architecture"),     f_architecture,    w_architecture                             },
64   { FIELD("Multi-Arch"),       f_multiarch,       w_multiarch,      PKGIFPOFF(multiarch)     },
65   { FIELD("Source"),           f_charfield,       w_charfield,      PKGIFPOFF(source)        },
66   { FIELD("Version"),          f_version,         w_version,        PKGIFPOFF(version)       },
67   { FIELD("Config-Version"),   f_configversion,   w_configversion                            },
68   { FIELD("Replaces"),         f_dependency,      w_dependency,     dep_replaces             },
69   { FIELD("Provides"),         f_dependency,      w_dependency,     dep_provides             },
70   { FIELD("Depends"),          f_dependency,      w_dependency,     dep_depends              },
71   { FIELD("Pre-Depends"),      f_dependency,      w_dependency,     dep_predepends           },
72   { FIELD("Recommends"),       f_dependency,      w_dependency,     dep_recommends           },
73   { FIELD("Suggests"),         f_dependency,      w_dependency,     dep_suggests             },
74   { FIELD("Breaks"),           f_dependency,      w_dependency,     dep_breaks               },
75   { FIELD("Conflicts"),        f_dependency,      w_dependency,     dep_conflicts            },
76   { FIELD("Enhances"),         f_dependency,      w_dependency,     dep_enhances             },
77   { FIELD("Conffiles"),        f_conffiles,       w_conffiles                                },
78   { FIELD("Filename"),         f_archives,        w_archives,       ARCHIVEFOFF(name)        },
79   { FIELD("Size"),             f_archives,        w_archives,       ARCHIVEFOFF(size)        },
80   { FIELD("MD5sum"),           f_archives,        w_archives,       ARCHIVEFOFF(md5sum)      },
81   { FIELD("MSDOS-Filename"),   f_archives,        w_archives,       ARCHIVEFOFF(msdosname)   },
82   { FIELD("Description"),      f_charfield,       w_charfield,      PKGIFPOFF(description)   },
83   { FIELD("Triggers-Pending"), f_trigpend,        w_trigpend                                 },
84   { FIELD("Triggers-Awaited"), f_trigaw,          w_trigaw                                   },
85   /* Note that aliases are added to the nicknames table. */
86   { FIELD("Revision"),         f_revision,        w_null                                     },
87   { FIELD("Recommended"),      f_dependency,      w_null                                     },
88   { FIELD("Optional"),         f_dependency,      w_null                                     },
89   { FIELD("Class"),            f_priority,        w_null                                     },
90   { FIELD("Package-Revision"), f_revision,        w_null                                     },
91   { FIELD("Package_Revision"), f_revision,        w_null                                     },
92   { NULL                                                                                     }
93 };
94 
95 /**
96  * Package object being parsed.
97  *
98  * Structure used to hold the parsed data for the package being constructed,
99  * before it gets properly inserted into the package database.
100  */
101 struct pkg_parse_object {
102   struct pkginfo *pkg;
103   struct pkgbin *pkgbin;
104 };
105 
106 /**
107  * Parse the field and value into the package being constructed.
108  */
109 static void
pkg_parse_field(struct parsedb_state * ps,struct field_state * fs,void * parse_obj)110 pkg_parse_field(struct parsedb_state *ps, struct field_state *fs,
111                 void *parse_obj)
112 {
113   struct pkg_parse_object *pkg_obj = parse_obj;
114   const struct fieldinfo *fip;
115   int *ip;
116 
117   for (fip = fieldinfos, ip = fs->fieldencountered; fip->name; fip++, ip++)
118     if (fip->namelen == (size_t)fs->fieldlen &&
119         strncasecmp(fip->name, fs->fieldstart, fs->fieldlen) == 0)
120       break;
121   if (fip->name) {
122     if ((*ip)++)
123       parse_error(ps,
124                   _("duplicate value for '%s' field"), fip->name);
125 
126     varbuf_reset(&fs->value);
127     varbuf_add_buf(&fs->value, fs->valuestart, fs->valuelen);
128     varbuf_end_str(&fs->value);
129 
130     fip->rcall(pkg_obj->pkg, pkg_obj->pkgbin, ps, fs->value.buf, fip);
131   } else {
132     struct arbitraryfield *arp, **larpp;
133 
134     if (fs->fieldlen < 2)
135       parse_error(ps,
136                   _("user-defined field name '%.*s' too short"),
137                   fs->fieldlen, fs->fieldstart);
138     larpp = &pkg_obj->pkgbin->arbs;
139     while ((arp = *larpp) != NULL) {
140       if (strncasecmp(arp->name, fs->fieldstart, fs->fieldlen) == 0 &&
141           strlen(arp->name) == (size_t)fs->fieldlen)
142         parse_error(ps,
143                    _("duplicate value for user-defined field '%.*s'"),
144                    fs->fieldlen, fs->fieldstart);
145       larpp = &arp->next;
146     }
147     arp = nfmalloc(sizeof(*arp));
148     arp->name = nfstrnsave(fs->fieldstart, fs->fieldlen);
149     arp->value = nfstrnsave(fs->valuestart, fs->valuelen);
150     arp->next = NULL;
151     *larpp = arp;
152   }
153 }
154 
155 /**
156  * Verify and fixup the package structure being constructed.
157  */
158 static void
pkg_parse_verify(struct parsedb_state * ps,struct pkginfo * pkg,struct pkgbin * pkgbin)159 pkg_parse_verify(struct parsedb_state *ps,
160                  struct pkginfo *pkg, struct pkgbin *pkgbin)
161 {
162   struct dependency *dep;
163   struct deppossi *dop;
164 
165   parse_must_have_field(ps, pkg->set->name, "Package");
166 
167   /* XXX: We need to check for status != PKG_STAT_HALFINSTALLED as while
168    * unpacking an unselected package, it will not have yet all data in
169    * place. But we cannot check for > PKG_STAT_HALFINSTALLED as
170    * PKG_STAT_CONFIGFILES always should have those fields. */
171   if ((ps->flags & pdb_recordavailable) ||
172       (pkg->status != PKG_STAT_NOTINSTALLED &&
173        pkg->status != PKG_STAT_HALFINSTALLED)) {
174     parse_ensure_have_field(ps, &pkgbin->description, "Description");
175     parse_ensure_have_field(ps, &pkgbin->maintainer, "Maintainer");
176     parse_must_have_field(ps, pkgbin->version.version, "Version");
177   }
178 
179   /* XXX: Versions before dpkg 1.10.19 did not preserve the Architecture
180    * field in the status file. So there's still live systems with packages
181    * in PKG_STAT_CONFIGFILES, ignore those too for now. */
182   if ((ps->flags & pdb_recordavailable) ||
183       pkg->status > PKG_STAT_HALFINSTALLED) {
184     /* We always want usable architecture information (as long as the package
185      * is in such a state that it make sense), so that it can be used safely
186      * on string comparisons and the like. */
187     if (pkgbin->arch->type == DPKG_ARCH_NONE)
188       parse_warn(ps, _("missing '%s' field"), "Architecture");
189     else if (pkgbin->arch->type == DPKG_ARCH_EMPTY)
190       parse_warn(ps, _("empty value for '%s' field"), "Architecture");
191   }
192   /* Mark missing architectures as empty, to distinguish these from
193    * unused slots in the db. */
194   if (pkgbin->arch->type == DPKG_ARCH_NONE)
195     pkgbin->arch = dpkg_arch_get(DPKG_ARCH_EMPTY);
196 
197   if (pkgbin->arch->type == DPKG_ARCH_EMPTY &&
198       pkgbin->multiarch == PKG_MULTIARCH_SAME)
199     parse_error(ps, _("package has '%s' field but is missing architecture"),
200                 "Multi-Arch: same");
201   if (pkgbin->arch->type == DPKG_ARCH_ALL &&
202       pkgbin->multiarch == PKG_MULTIARCH_SAME)
203     parse_error(ps, _("package has '%s' field but is architecture '%s'"),
204                 "Multi-Arch: same", "all");
205 
206   /* Generate the cached fully qualified package name representation. */
207   pkgbin->pkgname_archqual = pkgbin_name_archqual(pkg, pkgbin);
208 
209   /* Initialize deps to be arch-specific unless stated otherwise. */
210   for (dep = pkgbin->depends; dep; dep = dep->next)
211     for (dop = dep->list; dop; dop = dop->next)
212       if (!dop->arch)
213         dop->arch = pkgbin->arch;
214 
215   /*
216    * Check the Config-Version information:
217    *
218    * If there is a Config-Version it is definitely to be used, but there
219    * should not be one if the package is ‘installed’ or ‘triggers-pending’
220    * (in which case the Version will be copied) or if the package is
221    * ‘not-installed’ (in which case there is no Config-Version).
222    */
223   if (!(ps->flags & pdb_recordavailable)) {
224     if (pkg->configversion.version) {
225       if (pkg->status == PKG_STAT_INSTALLED ||
226           pkg->status == PKG_STAT_NOTINSTALLED ||
227           pkg->status == PKG_STAT_TRIGGERSPENDING)
228         parse_error(ps,
229                     _("'%s' field present for package with inappropriate '%s' field"),
230                     "Config-Version", "Status");
231     } else {
232       if (pkg->status == PKG_STAT_INSTALLED ||
233           pkg->status == PKG_STAT_TRIGGERSPENDING)
234         pkg->configversion = pkgbin->version;
235     }
236   }
237 
238   if (pkg->trigaw.head &&
239       (pkg->status <= PKG_STAT_CONFIGFILES ||
240        pkg->status >= PKG_STAT_TRIGGERSPENDING))
241     parse_error(ps,
242                 _("package has status %s but triggers are awaited"),
243                 pkg_status_name(pkg));
244   else if (pkg->status == PKG_STAT_TRIGGERSAWAITED && !pkg->trigaw.head)
245     parse_error(ps,
246                 _("package has status %s but no triggers awaited"),
247                 pkg_status_name(pkg));
248 
249   if (pkg->trigpend_head &&
250       !(pkg->status == PKG_STAT_TRIGGERSPENDING ||
251         pkg->status == PKG_STAT_TRIGGERSAWAITED))
252     parse_error(ps,
253                 _("package has status %s but triggers are pending"),
254                 pkg_status_name(pkg));
255   else if (pkg->status == PKG_STAT_TRIGGERSPENDING && !pkg->trigpend_head)
256     parse_error(ps,
257                 _("package has status %s but no triggers pending"),
258                 pkg_status_name(pkg));
259 
260   /* FIXME: There was a bug that could make a not-installed package have
261    * conffiles, so we check for them here and remove them (rather than
262    * calling it an error, which will do at some point). */
263   if (!(ps->flags & pdb_recordavailable) &&
264       pkg->status == PKG_STAT_NOTINSTALLED &&
265       pkgbin->conffiles) {
266     parse_warn(ps,
267                _("package has status %s and has conffiles, forgetting them"),
268                pkg_status_name(pkg));
269     pkgbin->conffiles = NULL;
270   }
271 
272   /* XXX: Mark not-installed leftover packages for automatic removal on
273    * next database dump. This code can be removed after dpkg 1.16.x, when
274    * there's guarantee that no leftover is found on the status file on
275    * major distributions. */
276   if (!(ps->flags & pdb_recordavailable) &&
277       pkg->status == PKG_STAT_NOTINSTALLED &&
278       pkg->eflag == PKG_EFLAG_OK &&
279       (pkg->want == PKG_WANT_PURGE ||
280        pkg->want == PKG_WANT_DEINSTALL ||
281        pkg->want == PKG_WANT_HOLD)) {
282     pkg_set_want(pkg, PKG_WANT_UNKNOWN);
283   }
284 
285   /* XXX: Mark not-installed non-arch-qualified selections for automatic
286    * removal, as they do not make sense in a multiarch enabled world, and
287    * might cause those selections to be unreferencable from command-line
288    * interfaces when there's other more specific selections. */
289   if (ps->type == pdb_file_status &&
290       pkg->status == PKG_STAT_NOTINSTALLED &&
291       pkg->eflag == PKG_EFLAG_OK &&
292       pkg->want == PKG_WANT_INSTALL &&
293       pkgbin->arch->type == DPKG_ARCH_EMPTY)
294     pkg_set_want(pkg, PKG_WANT_UNKNOWN);
295 
296   /* XXX: Versions before dpkg 1.13.10 did not blank the Origin and Bugs
297    * fields, so there can be packages that should be garbage collected but
298    * are lingering around. Blank them to make sure we will forget all about
299    * them on the next database dump. */
300   if (!(ps->flags & pdb_recordavailable) &&
301       pkg->status == PKG_STAT_NOTINSTALLED &&
302       pkg->eflag == PKG_EFLAG_OK &&
303       pkg->want == PKG_WANT_UNKNOWN) {
304     pkgbin_blank(pkgbin);
305   }
306 }
307 
308 struct pkgcount {
309   int single;
310   int multi;
311   int total;
312 };
313 
314 static void
parse_count_pkg_instance(struct pkgcount * count,struct pkginfo * pkg,struct pkgbin * pkgbin)315 parse_count_pkg_instance(struct pkgcount *count,
316                          struct pkginfo *pkg, struct pkgbin *pkgbin)
317 {
318   if (pkg->status == PKG_STAT_NOTINSTALLED)
319      return;
320 
321   if (pkgbin->multiarch == PKG_MULTIARCH_SAME)
322     count->multi++;
323   else
324     count->single++;
325 
326   count->total++;
327 }
328 
329 /**
330  * Lookup the package set slot for the parsed package.
331  *
332  * Perform various checks, to make sure the database is always in a sane
333  * state, and to not allow breaking it.
334  */
335 static struct pkgset *
parse_find_set_slot(struct parsedb_state * ps,struct pkginfo * new_pkg,struct pkgbin * new_pkgbin)336 parse_find_set_slot(struct parsedb_state *ps,
337                     struct pkginfo *new_pkg, struct pkgbin *new_pkgbin)
338 {
339   struct pkgcount count = { .single = 0, .multi = 0, .total = 0 };
340   struct pkgset *set;
341   struct pkginfo *pkg;
342 
343   set = pkg_hash_find_set(new_pkg->set->name);
344 
345   /* Sanity checks: verify that the db is in a consistent state. */
346 
347   if (ps->type == pdb_file_status)
348     parse_count_pkg_instance(&count, new_pkg, new_pkgbin);
349 
350   count.total = 0;
351 
352   for (pkg = &set->pkg; pkg; pkg = pkg->arch_next)
353     parse_count_pkg_instance(&count, pkg, &pkg->installed);
354 
355   if (count.single > 1)
356     parse_error(ps, _("multiple non-coinstallable package instances present; "
357                       "most probably due to an upgrade from an unofficial dpkg"));
358 
359   if (count.single > 0 && count.multi > 0)
360     parse_error(ps, _("mixed non-coinstallable and coinstallable package "
361                       "instances present; most probably due to an upgrade "
362                       "from an unofficial dpkg"));
363 
364   if (pkgset_installed_instances(set) != count.total)
365     internerr("in-core pkgset '%s' with inconsistent number of instances",
366               set->name);
367 
368   return set;
369 }
370 
371 /**
372  * Lookup the package slot for the parsed package.
373  *
374  * Cross-grading (i.e. switching arch) is only possible when parsing an
375  * update entry or when installing a new package.
376  *
377  * Most of the time each pkginfo in a pkgset has the same architecture for
378  * both the installed and available pkgbin members. But when cross-grading
379  * there's going to be a temporary discrepancy, because we reuse the single
380  * instance and fill the available pkgbin with the candidate pkgbin, until
381  * that is copied over the installed pkgbin.
382  *
383  * If there's 0 or > 1 package instances, then we match against the pkginfo
384  * slot architecture, because cross-grading is just not possible.
385  *
386  * If there's 1 instance, we are cross-grading and both installed and
387  * candidate are not PKG_MULTIARCH_SAME, we have to reuse the existing single
388  * slot regardless of the arch differing between the two. If we are not
389  * cross-grading, then we use the entry with the matching arch.
390  */
391 static struct pkginfo *
parse_find_pkg_slot(struct parsedb_state * ps,struct pkginfo * new_pkg,struct pkgbin * new_pkgbin)392 parse_find_pkg_slot(struct parsedb_state *ps,
393                     struct pkginfo *new_pkg, struct pkgbin *new_pkgbin)
394 {
395   struct pkgset *db_set;
396   struct pkginfo *db_pkg;
397 
398   db_set = parse_find_set_slot(ps, new_pkg, new_pkgbin);
399 
400   if (ps->type == pdb_file_available) {
401     /* If there's a single package installed and the new package is not
402      * “Multi-Arch: same”, then we preserve the previous behaviour of
403      * possible architecture switch, for example from native to all. */
404     if (pkgset_installed_instances(db_set) == 1 &&
405         new_pkgbin->multiarch != PKG_MULTIARCH_SAME)
406       return pkg_hash_get_singleton(db_set);
407     else
408       return pkg_hash_get_pkg(db_set, new_pkgbin->arch);
409   } else {
410     bool selection = false;
411 
412     /* If the package is part of the status file, and it's not installed
413      * then this means it's just a selection. */
414     if (ps->type == pdb_file_status && new_pkg->status == PKG_STAT_NOTINSTALLED)
415       selection = true;
416 
417     /* Verify we don't allow something that will mess up the db. */
418     if (pkgset_installed_instances(db_set) > 1 &&
419         !selection && new_pkgbin->multiarch != PKG_MULTIARCH_SAME)
420       ohshit(_("package %s (%s) with field '%s: %s' is not co-installable "
421                "with %s which has multiple installed instances"),
422              pkgbin_name(new_pkg, new_pkgbin, pnaw_always),
423              versiondescribe(&new_pkgbin->version, vdew_nonambig),
424              "Multi-Arch", multiarchinfos[new_pkgbin->multiarch].name,
425              db_set->name);
426 
427     /* If we are parsing the status file, use a slot per arch. */
428     if (ps->type == pdb_file_status)
429       return pkg_hash_get_pkg(db_set, new_pkgbin->arch);
430 
431     /* If we are doing an update, from the log or a new package, then
432      * handle cross-grades. */
433     if (pkgset_installed_instances(db_set) == 1) {
434       db_pkg = pkg_hash_get_singleton(db_set);
435 
436       if (db_pkg->installed.multiarch == PKG_MULTIARCH_SAME &&
437           new_pkgbin->multiarch == PKG_MULTIARCH_SAME)
438         return pkg_hash_get_pkg(db_set, new_pkgbin->arch);
439       else
440         return db_pkg;
441     } else {
442       return pkg_hash_get_pkg(db_set, new_pkgbin->arch);
443     }
444   }
445 }
446 
447 /**
448  * Copy into the in-core database the package being constructed.
449  */
450 static void
pkg_parse_copy(struct parsedb_state * ps,struct pkginfo * dst_pkg,struct pkgbin * dst_pkgbin,struct pkginfo * src_pkg,struct pkgbin * src_pkgbin)451 pkg_parse_copy(struct parsedb_state *ps,
452                struct pkginfo *dst_pkg, struct pkgbin *dst_pkgbin,
453                struct pkginfo *src_pkg, struct pkgbin *src_pkgbin)
454 {
455   /* Copy the priority and section across, but don't overwrite existing
456    * values if the pdb_weakclassification flag is set. */
457   if (str_is_set(src_pkg->section) &&
458       !((ps->flags & pdb_weakclassification) &&
459         str_is_set(dst_pkg->section)))
460     dst_pkg->section = src_pkg->section;
461   if (src_pkg->priority != PKG_PRIO_UNKNOWN &&
462       !((ps->flags & pdb_weakclassification) &&
463         dst_pkg->priority != PKG_PRIO_UNKNOWN)) {
464     dst_pkg->priority = src_pkg->priority;
465     if (src_pkg->priority == PKG_PRIO_OTHER)
466       dst_pkg->otherpriority = src_pkg->otherpriority;
467   }
468 
469   /* Sort out the dependency mess. */
470   copy_dependency_links(dst_pkg, &dst_pkgbin->depends, src_pkgbin->depends,
471                         (ps->flags & pdb_recordavailable) ? true : false);
472 
473   /* Copy across data. */
474   memcpy(dst_pkgbin, src_pkgbin, sizeof(struct pkgbin));
475   if (!(ps->flags & pdb_recordavailable)) {
476     struct trigaw *ta;
477 
478     pkg_set_want(dst_pkg, src_pkg->want);
479     pkg_copy_eflags(dst_pkg, src_pkg);
480     pkg_set_status(dst_pkg, src_pkg->status);
481     dst_pkg->configversion = src_pkg->configversion;
482     dst_pkg->archives = NULL;
483 
484     dst_pkg->trigpend_head = src_pkg->trigpend_head;
485     dst_pkg->trigaw = src_pkg->trigaw;
486     for (ta = dst_pkg->trigaw.head; ta; ta = ta->sameaw.next) {
487       if (ta->aw != src_pkg)
488         internerr("trigger awaited package %s and origin package %s not linked properly",
489                   pkg_name(ta->aw, pnaw_always),
490                   pkgbin_name(src_pkg, src_pkgbin, pnaw_always));
491       ta->aw = dst_pkg;
492       /* ->othertrigaw_head is updated by trig_note_aw in *(pkg_hash_find())
493        * rather than in dst_pkg. */
494     }
495   } else if (!(ps->flags & pdb_ignore_archives)) {
496     dst_pkg->archives = src_pkg->archives;
497   }
498 }
499 
500 /**
501  * Return a descriptive parser type.
502  */
503 static enum parsedbtype
parse_get_type(struct parsedb_state * ps,enum parsedbflags flags)504 parse_get_type(struct parsedb_state *ps, enum parsedbflags flags)
505 {
506   if (flags & pdb_recordavailable) {
507     if (flags & pdb_single_stanza)
508       return pdb_file_control;
509     else
510       return pdb_file_available;
511   } else {
512     if (flags & pdb_single_stanza)
513       return pdb_file_update;
514     else
515       return pdb_file_status;
516   }
517 }
518 
519 /**
520  * Create a new deb822 parser context.
521  */
522 struct parsedb_state *
parsedb_new(const char * filename,int fd,enum parsedbflags flags)523 parsedb_new(const char *filename, int fd, enum parsedbflags flags)
524 {
525   struct parsedb_state *ps;
526 
527   ps = m_malloc(sizeof(*ps));
528   ps->err = DPKG_ERROR_OBJECT;
529   ps->filename = filename;
530   ps->type = parse_get_type(ps, flags);
531   ps->flags = flags;
532   ps->fd = fd;
533   ps->lno = 0;
534   ps->pkg = NULL;
535   ps->pkgbin = NULL;
536 
537   return ps;
538 }
539 
540 /**
541  * Open a file for deb822 parsing.
542  */
543 struct parsedb_state *
parsedb_open(const char * filename,enum parsedbflags flags)544 parsedb_open(const char *filename, enum parsedbflags flags)
545 {
546   struct parsedb_state *ps;
547   int fd;
548 
549   /* Special case stdin handling. */
550   if (flags & pdb_dash_is_stdin && strcmp(filename, "-") == 0)
551     return parsedb_new(filename, STDIN_FILENO, flags);
552 
553   fd = open(filename, O_RDONLY);
554   if (fd == -1)
555     ohshite(_("failed to open package info file '%.255s' for reading"),
556             filename);
557 
558   ps = parsedb_new(filename, fd, flags | pdb_close_fd);
559 
560   push_cleanup(cu_closefd, ~ehflag_normaltidy, 1, &ps->fd);
561 
562   return ps;
563 }
564 
565 /**
566  * Load data for package deb822 style parsing.
567  */
568 void
parsedb_load(struct parsedb_state * ps)569 parsedb_load(struct parsedb_state *ps)
570 {
571   struct stat st;
572 
573   if (fstat(ps->fd, &st) == -1)
574     ohshite(_("can't stat package info file '%.255s'"), ps->filename);
575 
576   if (S_ISFIFO(st.st_mode)) {
577     struct varbuf buf = VARBUF_INIT;
578     struct dpkg_error err;
579     off_t size;
580 
581     size = fd_vbuf_copy(ps->fd, &buf, -1, &err);
582     if (size < 0)
583       ohshit(_("reading package info file '%s': %s"), ps->filename, err.str);
584 
585     varbuf_end_str(&buf);
586 
587     ps->dataptr = varbuf_detach(&buf);
588     ps->endptr = ps->dataptr + size;
589   } else if (st.st_size > 0) {
590 #ifdef USE_MMAP
591     ps->dataptr = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, ps->fd, 0);
592     if (ps->dataptr == MAP_FAILED)
593       ohshite(_("can't mmap package info file '%.255s'"), ps->filename);
594 #else
595     ps->dataptr = m_malloc(st.st_size);
596 
597     if (fd_read(ps->fd, ps->dataptr, st.st_size) < 0)
598       ohshite(_("reading package info file '%.255s'"), ps->filename);
599 #endif
600     ps->endptr = ps->dataptr + st.st_size;
601   } else {
602     ps->dataptr = ps->endptr = NULL;
603   }
604   ps->data = ps->dataptr;
605 }
606 
607 /**
608  * Parse an RFC-822 style stanza.
609  */
610 bool
parse_stanza(struct parsedb_state * ps,struct field_state * fs,parse_field_func * parse_field,void * parse_obj)611 parse_stanza(struct parsedb_state *ps, struct field_state *fs,
612              parse_field_func *parse_field, void *parse_obj)
613 {
614   int c;
615 
616   /* Skip adjacent new lines. */
617   while (!parse_at_eof(ps)) {
618     c = parse_getc(ps);
619     if (c != '\n' && c != MSDOS_EOF_CHAR)
620       break;
621     ps->lno++;
622   }
623 
624   /* Nothing relevant parsed, bail out. */
625   if (parse_at_eof(ps))
626     return false;
627 
628   /* Loop per field. */
629   for (;;) {
630     bool blank_line;
631 
632     /* Scan field name. */
633     fs->fieldstart = ps->dataptr - 1;
634     while (!parse_at_eof(ps) && !c_isspace(c) && c != ':' && c != MSDOS_EOF_CHAR)
635       c = parse_getc(ps);
636     fs->fieldlen = ps->dataptr - fs->fieldstart - 1;
637     if (fs->fieldlen == 0)
638       parse_error(ps,  _("empty field name"));
639     if (fs->fieldstart[0] == '-')
640       parse_error(ps,  _("field name '%.*s' cannot start with hyphen"),
641                   fs->fieldlen, fs->fieldstart);
642 
643     /* Skip spaces before ‘:’. */
644     while (!parse_at_eof(ps) && c != '\n' && c_isspace(c))
645       c = parse_getc(ps);
646 
647     /* Validate ‘:’. */
648     if (parse_at_eof(ps))
649       parse_error(ps, _("end of file after field name '%.*s'"),
650                   fs->fieldlen, fs->fieldstart);
651     if (c == '\n')
652       parse_error(ps,
653                   _("newline in field name '%.*s'"), fs->fieldlen, fs->fieldstart);
654     if (c == MSDOS_EOF_CHAR)
655       parse_error(ps, _("MSDOS end of file (^Z) in field name '%.*s'"),
656                   fs->fieldlen, fs->fieldstart);
657     if (c != ':')
658       parse_error(ps,
659                   _("field name '%.*s' must be followed by colon"),
660                   fs->fieldlen, fs->fieldstart);
661 
662     /* Skip space after ‘:’ but before value and EOL. */
663     while (!parse_at_eof(ps)) {
664       c = parse_getc(ps);
665       if (c == '\n' || !c_isspace(c))
666         break;
667     }
668     if (parse_at_eof(ps))
669       parse_error(ps, _("end of file before value of field '%.*s' (missing final newline)"),
670                   fs->fieldlen, fs->fieldstart);
671     if (c == MSDOS_EOF_CHAR)
672       parse_error(ps, _("MSDOS end of file (^Z) in value of field '%.*s' (missing newline?)"),
673                   fs->fieldlen, fs->fieldstart);
674 
675     blank_line = false;
676 
677     /* Scan field value. */
678     fs->valuestart = ps->dataptr - 1;
679     for (;;) {
680       if (c == '\n' || c == MSDOS_EOF_CHAR) {
681         if (blank_line) {
682           if (ps->flags & pdb_lax_stanza_parser)
683             parse_warn(ps, _("blank line in value of field '%.*s'"),
684                        fs->fieldlen, fs->fieldstart);
685           else
686             parse_error(ps, _("blank line in value of field '%.*s'"),
687                         fs->fieldlen, fs->fieldstart);
688         }
689         ps->lno++;
690 
691         if (parse_at_eof(ps))
692           break;
693         c = parse_getc(ps);
694 
695         /* Found double EOL, or start of new field. */
696         if (parse_at_eof(ps) || c == '\n' || !c_isspace(c))
697           break;
698 
699         parse_ungetc(c, ps);
700         blank_line = true;
701       } else if (blank_line && !c_isspace(c)) {
702         blank_line = false;
703       }
704 
705       if (parse_at_eof(ps))
706         parse_error(ps, _("end of file during value of field '%.*s' (missing final newline)"),
707                     fs->fieldlen, fs->fieldstart);
708 
709       c = parse_getc(ps);
710     }
711     fs->valuelen = ps->dataptr - fs->valuestart - 1;
712 
713     /* Trim ending space on value. */
714     while (fs->valuelen && c_isspace(*(fs->valuestart + fs->valuelen - 1)))
715       fs->valuelen--;
716 
717     parse_field(ps, fs, parse_obj);
718 
719     if (parse_at_eof(ps) || c == '\n' || c == MSDOS_EOF_CHAR)
720       break;
721   } /* Loop per field. */
722 
723   if (c == '\n')
724     ps->lno++;
725 
726   return true;
727 }
728 
729 /**
730  * Teardown a package deb822 parser context.
731  */
732 void
parsedb_close(struct parsedb_state * ps)733 parsedb_close(struct parsedb_state *ps)
734 {
735   if (ps->flags & pdb_close_fd) {
736     pop_cleanup(ehflag_normaltidy);
737 
738     if (close(ps->fd))
739       ohshite(_("failed to close after read: '%.255s'"), ps->filename);
740   }
741 
742   if (ps->data != NULL) {
743 #ifdef USE_MMAP
744     munmap(ps->data, ps->endptr - ps->data);
745 #else
746     free(ps->data);
747 #endif
748   }
749   free(ps);
750 }
751 
752 /**
753  * Parse deb822 style package data from a buffer.
754  *
755  * donep may be NULL.
756  * If donep is not NULL only one package's information is expected.
757  */
758 int
parsedb_parse(struct parsedb_state * ps,struct pkginfo ** donep)759 parsedb_parse(struct parsedb_state *ps, struct pkginfo **donep)
760 {
761   struct pkgset tmp_set;
762   struct pkginfo *new_pkg, *db_pkg;
763   struct pkgbin *new_pkgbin, *db_pkgbin;
764   struct pkg_parse_object pkg_obj;
765   int fieldencountered[array_count(fieldinfos)];
766   int pdone;
767   struct field_state fs;
768 
769   memset(&fs, 0, sizeof(fs));
770   fs.fieldencountered = fieldencountered;
771 
772   new_pkg = &tmp_set.pkg;
773   if (ps->flags & pdb_recordavailable)
774     new_pkgbin = &new_pkg->available;
775   else
776     new_pkgbin = &new_pkg->installed;
777 
778   ps->pkg = new_pkg;
779   ps->pkgbin = new_pkgbin;
780 
781   pkg_obj.pkg = new_pkg;
782   pkg_obj.pkgbin = new_pkgbin;
783 
784   pdone= 0;
785 
786   /* Loop per package. */
787   for (;;) {
788     memset(fieldencountered, 0, sizeof(fieldencountered));
789     pkgset_blank(&tmp_set);
790 
791     if (!parse_stanza(ps, &fs, pkg_parse_field, &pkg_obj))
792       break;
793 
794     if (pdone && donep)
795       parse_error(ps,
796                   _("several package info entries found, only one allowed"));
797 
798     pkg_parse_verify(ps, new_pkg, new_pkgbin);
799 
800     db_pkg = parse_find_pkg_slot(ps, new_pkg, new_pkgbin);
801     if (ps->flags & pdb_recordavailable)
802       db_pkgbin = &db_pkg->available;
803     else
804       db_pkgbin = &db_pkg->installed;
805 
806     if (((ps->flags & pdb_ignoreolder) || ps->type == pdb_file_available) &&
807         dpkg_version_is_informative(&db_pkgbin->version) &&
808         dpkg_version_compare(&new_pkgbin->version, &db_pkgbin->version) < 0)
809       continue;
810 
811     pkg_parse_copy(ps, db_pkg, db_pkgbin, new_pkg, new_pkgbin);
812 
813     if (donep)
814       *donep = db_pkg;
815     pdone++;
816     if (parse_at_eof(ps))
817       break;
818   }
819 
820   varbuf_destroy(&fs.value);
821   if (donep && !pdone)
822     ohshit(_("no package information in '%.255s'"), ps->filename);
823 
824   return pdone;
825 }
826 
827 /**
828  * Parse a deb822 style file.
829  *
830  * donep may be NULL.
831  * If donep is not NULL only one package's information is expected.
832  */
833 int
parsedb(const char * filename,enum parsedbflags flags,struct pkginfo ** pkgp)834 parsedb(const char *filename, enum parsedbflags flags, struct pkginfo **pkgp)
835 {
836   struct parsedb_state *ps;
837   int count;
838 
839   ps = parsedb_open(filename, flags);
840   parsedb_load(ps);
841   count = parsedb_parse(ps, pkgp);
842   parsedb_close(ps);
843 
844   return count;
845 }
846 
847 /**
848  * Copy dependency links structures.
849  *
850  * This routine is used to update the ‘reverse’ dependency pointers when
851  * new ‘forwards’ information has been constructed. It first removes all
852  * the links based on the old information. The old information starts in
853  * *updateme; after much brou-ha-ha the reverse structures are created
854  * and *updateme is set to the value from newdepends.
855  *
856  * @param pkg The package we're doing this for. This is used to construct
857  *        correct uplinks.
858  * @param updateme The forwards dependency pointer that we are to update.
859  *        This starts out containing the old forwards info, which we use to
860  *        unthread the old reverse links. After we're done it is updated.
861  * @param newdepends The value that we ultimately want to have in updateme.
862  * @param available The pkgbin to modify, available or installed.
863  *
864  * It is likely that the backward pointer for the package in question
865  * (‘depended’) will be updated by this routine, but this will happen by
866  * the routine traversing the dependency data structures. It doesn't need
867  * to be told where to update that; I just mention it as something that
868  * one should be cautious about.
869  */
copy_dependency_links(struct pkginfo * pkg,struct dependency ** updateme,struct dependency * newdepends,bool available)870 void copy_dependency_links(struct pkginfo *pkg,
871                            struct dependency **updateme,
872                            struct dependency *newdepends,
873                            bool available)
874 {
875   struct dependency *dyp;
876   struct deppossi *dop, **revdeps;
877 
878   /* Delete ‘backward’ (‘depended’) links from other packages to
879    * dependencies listed in old version of this one. We do this by
880    * going through all the dependencies in the old version of this
881    * one and following them down to find which deppossi nodes to
882    * remove. */
883   for (dyp= *updateme; dyp; dyp= dyp->next) {
884     for (dop= dyp->list; dop; dop= dop->next) {
885       if (dop->rev_prev)
886         dop->rev_prev->rev_next = dop->rev_next;
887       else
888         if (available)
889           dop->ed->depended.available = dop->rev_next;
890         else
891           dop->ed->depended.installed = dop->rev_next;
892       if (dop->rev_next)
893         dop->rev_next->rev_prev = dop->rev_prev;
894     }
895   }
896 
897   /* Now fill in new ‘ed’ links from other packages to dependencies
898    * listed in new version of this one, and set our uplinks correctly. */
899   for (dyp= newdepends; dyp; dyp= dyp->next) {
900     dyp->up= pkg;
901     for (dop= dyp->list; dop; dop= dop->next) {
902       revdeps = available ? &dop->ed->depended.available :
903                             &dop->ed->depended.installed;
904       dop->rev_next = *revdeps;
905       dop->rev_prev = NULL;
906       if (*revdeps)
907         (*revdeps)->rev_prev = dop;
908       *revdeps = dop;
909     }
910   }
911 
912   /* Finally, we fill in the new value. */
913   *updateme= newdepends;
914 }
915