2 * libdpkg - Debian packaging suite library routines
3 * fields.c - parsing of all the different fields, when reading in
5 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2001 Wichert Akkerman
7 * Copyright © 2006-2015 Guillem Jover <guillem@debian.org>
8 * Copyright © 2009 Canonical Ltd.
9 * Copyright © 2011 Linaro Limited
10 * Copyright © 2011 Raphaël Hertzog <hertzog@debian.org>
12 * This is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program. If not, see <https://www.gnu.org/licenses/>.
32 #include <dpkg/i18n.h>
33 #include <dpkg/c-ctype.h>
34 #include <dpkg/dpkg.h>
35 #include <dpkg/dpkg-db.h>
36 #include <dpkg/arch.h>
37 #include <dpkg/string.h>
38 #include <dpkg/path.h>
39 #include <dpkg/parsedump.h>
40 #include <dpkg/pkg-spec.h>
41 #include <dpkg/triglib.h>
44 * Flags to parse a name associated to a value.
47 /** Expect no more words (default). */
49 /** Expect another word after the parsed name. */
51 /** Do not fail if there is no name with an associated value found. */
52 PARSE_NV_FALLBACK
= 2,
56 * Parses a name and returns its associated value.
58 * Gets a pointer to the string to parse in @a strp, and modifies the pointer
59 * to the string to point to the end of the parsed text. If no value is found
60 * for the name and @a flags is set to #PARSE_NV_FALLBACK then @a strp is set
61 * to NULL and returns -1, otherwise a parse error is emitted.
64 parse_nv(struct parsedb_state
*ps
, enum parse_nv_mode parse_mode
,
65 const char **strp
, const struct namevalue
*nv_head
)
67 const char *str_start
= *strp
, *str_end
;
68 const struct namevalue
*nv
;
71 dpkg_error_destroy(&ps
->err
);
73 if (str_start
[0] == '\0')
74 return dpkg_put_error(&ps
->err
, _("is missing a value"));
76 nv
= namevalue_find_by_name(nv_head
, str_start
);
78 /* We got no match, skip further string validation. */
79 if (parse_mode
!= PARSE_NV_FALLBACK
)
80 return dpkg_put_error(&ps
->err
, _("has invalid value '%.50s'"), str_start
);
85 str_end
= str_start
+ nv
->length
;
86 while (c_isspace(str_end
[0]))
90 if (parse_mode
!= PARSE_NV_NEXT
&& str_is_set(str_end
))
91 return dpkg_put_error(&ps
->err
, _("has trailing junk"));
100 f_name(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
101 struct parsedb_state
*ps
,
102 const char *value
, const struct fieldinfo
*fip
)
106 e
= pkg_name_is_illegal(value
);
108 parse_error(ps
, _("invalid package name in '%s' field: %s"), fip
->name
, e
);
109 /* We use the new name, as pkg_hash_find_set() may have done a tolower for us. */
110 pkg
->set
->name
= pkg_hash_find_set(value
)->name
;
114 f_archives(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
115 struct parsedb_state
*ps
,
116 const char *value
, const struct fieldinfo
*fip
)
118 struct archivedetails
*fdp
, **fdpp
;
123 parse_error(ps
, _("empty archive details '%s' field"), fip
->name
);
124 if (!(ps
->flags
& pdb_recordavailable
))
126 _("archive details '%s' field not allowed in status file"),
128 allowextend
= !pkg
->archives
;
129 fdpp
= &pkg
->archives
;
130 cpos
= nfstrsave(value
);
133 while (*space
&& !c_isspace(*space
))
141 _("too many values in archive details '%s' field "
142 "(compared to others)"), fip
->name
);
143 fdp
= nfmalloc(sizeof(*fdp
));
145 fdp
->name
= fdp
->msdosname
= fdp
->size
= fdp
->md5sum
= NULL
;
148 STRUCTFIELD(fdp
, fip
->integer
, const char *) = cpos
;
150 while (*space
&& c_isspace(*space
))
156 _("too few values in archive details '%s' field "
157 "(compared to others)"), fip
->name
);
161 f_charfield(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
162 struct parsedb_state
*ps
,
163 const char *value
, const struct fieldinfo
*fip
)
166 STRUCTFIELD(pkgbin
, fip
->integer
, char *) = nfstrsave(value
);
170 f_boolean(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
171 struct parsedb_state
*ps
,
172 const char *value
, const struct fieldinfo
*fip
)
179 boolean
= parse_nv(ps
, PARSE_NV_LAST
, &value
, booleaninfos
);
180 if (dpkg_has_error(&ps
->err
))
181 parse_error(ps
, _("boolean (yes/no) '%s' field: %s"),
182 fip
->name
, ps
->err
.str
);
184 STRUCTFIELD(pkgbin
, fip
->integer
, bool) = boolean
;
188 f_multiarch(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
189 struct parsedb_state
*ps
,
190 const char *value
, const struct fieldinfo
*fip
)
197 multiarch
= parse_nv(ps
, PARSE_NV_LAST
, &value
, multiarchinfos
);
198 if (dpkg_has_error(&ps
->err
))
199 parse_error(ps
, _("quadstate (foreign/allowed/same/no) '%s' field: %s"),
200 fip
->name
, ps
->err
.str
);
201 STRUCTFIELD(pkgbin
, fip
->integer
, int) = multiarch
;
205 f_architecture(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
206 struct parsedb_state
*ps
,
207 const char *value
, const struct fieldinfo
*fip
)
209 pkgbin
->arch
= dpkg_arch_find(value
);
210 if (pkgbin
->arch
->type
== DPKG_ARCH_ILLEGAL
)
211 parse_warn(ps
, _("'%s' is not a valid architecture name in '%s' field: %s"),
212 value
, fip
->name
, dpkg_arch_name_is_illegal(value
));
216 f_section(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
217 struct parsedb_state
*ps
,
218 const char *value
, const struct fieldinfo
*fip
)
221 pkg
->section
= nfstrsave(value
);
225 f_priority(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
226 struct parsedb_state
*ps
,
227 const char *value
, const struct fieldinfo
*fip
)
229 const char *str
= value
;
234 priority
= parse_nv(ps
, PARSE_NV_FALLBACK
, &str
, priorityinfos
);
235 if (dpkg_has_error(&ps
->err
))
236 parse_error(ps
, _("word in '%s' field: %s"), fip
->name
, ps
->err
.str
);
239 pkg
->priority
= PKG_PRIO_OTHER
;
240 pkg
->otherpriority
= nfstrsave(value
);
242 pkg
->priority
= priority
;
247 f_obs_class(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
248 struct parsedb_state
*ps
,
249 const char *value
, const struct fieldinfo
*fip
)
251 parse_warn(ps
, _("obsolete '%s' field used"), fip
->name
);
252 f_priority(pkg
, pkgbin
, ps
, value
, fip
);
256 f_status(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
257 struct parsedb_state
*ps
,
258 const char *value
, const struct fieldinfo
*fip
)
260 if (ps
->flags
& pdb_rejectstatus
)
262 _("value for '%s' field not allowed in this context"),
264 if (ps
->flags
& pdb_recordavailable
)
267 pkg
->want
= parse_nv(ps
, PARSE_NV_NEXT
, &value
, wantinfos
);
268 if (dpkg_has_error(&ps
->err
))
269 parse_error(ps
, _("first (want) word in '%s' field: %s"),
270 fip
->name
, ps
->err
.str
);
271 pkg
->eflag
= parse_nv(ps
, PARSE_NV_NEXT
, &value
, eflaginfos
);
272 if (dpkg_has_error(&ps
->err
))
273 parse_error(ps
, _("second (error) word in '%s' field: %s"),
274 fip
->name
, ps
->err
.str
);
275 pkg
->status
= parse_nv(ps
, PARSE_NV_LAST
, &value
, statusinfos
);
276 if (dpkg_has_error(&ps
->err
))
277 parse_error(ps
, _("third (status) word in '%s' field: %s"),
278 fip
->name
, ps
->err
.str
);
282 f_version(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
283 struct parsedb_state
*ps
,
284 const char *value
, const struct fieldinfo
*fip
)
286 if (parse_db_version(ps
, &pkgbin
->version
, value
) < 0)
287 parse_problem(ps
, _("'%s' field value '%.250s'"), fip
->name
, value
);
291 f_obs_revision(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
292 struct parsedb_state
*ps
,
293 const char *value
, const struct fieldinfo
*fip
)
297 parse_warn(ps
, _("obsolete '%s' field used"), fip
->name
);
299 if (str_is_set(pkgbin
->version
.revision
)) {
300 newversion
= nfmalloc(strlen(pkgbin
->version
.version
) +
301 strlen(pkgbin
->version
.revision
) + 2);
302 sprintf(newversion
, "%s-%s", pkgbin
->version
.version
,
303 pkgbin
->version
.revision
);
304 pkgbin
->version
.version
= newversion
;
306 pkgbin
->version
.revision
= nfstrsave(value
);
310 f_configversion(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
311 struct parsedb_state
*ps
,
312 const char *value
, const struct fieldinfo
*fip
)
314 if (ps
->flags
& pdb_rejectstatus
)
316 _("value for '%s' field not allowed in this context"),
318 if (ps
->flags
& pdb_recordavailable
)
321 if (parse_db_version(ps
, &pkg
->configversion
, value
) < 0)
322 parse_problem(ps
, _("'%s' field value '%.250s'"), fip
->name
, value
);
326 * The code in f_conffiles ensures that value[-1] == ' ', which is helpful.
328 static void conffvalue_lastword(const char *value
, const char *from
,
330 const char **word_start_r
, int *word_len_r
,
331 const char **new_from_r
,
332 struct parsedb_state
*ps
)
336 if (from
<= value
+1) goto malformed
;
337 for (lastspc
= from
-1; *lastspc
!= ' '; lastspc
--);
338 if (lastspc
<= value
+1 || lastspc
>= endent
-1) goto malformed
;
340 *new_from_r
= lastspc
;
341 *word_start_r
= lastspc
+ 1;
342 *word_len_r
= (int)(from
- *word_start_r
);
347 _("value for '%s' field has malformed line '%.*s'"),
348 "Conffiles", (int)min(endent
- value
, 250), value
);
352 f_conffiles(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
353 struct parsedb_state
*ps
,
354 const char *value
, const struct fieldinfo
*fip
)
356 static const char obsolete_str
[]= "obsolete";
357 static const char remove_on_upgrade_str
[] = "remove-on-upgrade";
358 struct conffile
**lastp
;
360 lastp
= &pkgbin
->conffiles
;
362 struct conffile
*newlink
;
363 const char *endent
, *endfn
, *hashstart
;
365 int c
, namelen
, hashlen
;
366 bool obsolete
, remove_on_upgrade
;
369 if (c
== '\n') continue;
372 _("value for '%s' field has line starting with non-space '%c'"),
374 for (endent
= value
; (c
= *endent
) != '\0' && c
!= '\n'; endent
++) ;
375 conffvalue_lastword(value
, endent
, endent
,
376 &hashstart
, &hashlen
, &endfn
,
378 remove_on_upgrade
= (hashlen
== sizeof(remove_on_upgrade_str
) - 1 &&
379 memcmp(hashstart
, remove_on_upgrade_str
, hashlen
) == 0);
380 if (remove_on_upgrade
)
381 conffvalue_lastword(value
, endfn
, endent
, &hashstart
, &hashlen
, &endfn
,
384 obsolete
= (hashlen
== sizeof(obsolete_str
)-1 &&
385 memcmp(hashstart
, obsolete_str
, hashlen
) == 0);
387 conffvalue_lastword(value
, endfn
, endent
,
388 &hashstart
, &hashlen
, &endfn
,
390 newlink
= nfmalloc(sizeof(*newlink
));
391 value
= path_skip_slash_dotslash(value
);
392 namelen
= (int)(endfn
-value
);
395 _("root or empty directory listed as a conffile in '%s' field"),
397 newptr
= nfmalloc(namelen
+2);
399 memcpy(newptr
+1,value
,namelen
);
400 newptr
[namelen
+1] = '\0';
401 newlink
->name
= newptr
;
402 newptr
= nfmalloc(hashlen
+1);
403 memcpy(newptr
, hashstart
, hashlen
);
404 newptr
[hashlen
] = '\0';
405 newlink
->hash
= newptr
;
406 newlink
->obsolete
= obsolete
;
407 newlink
->remove_on_upgrade
= remove_on_upgrade
;
410 lastp
= &newlink
->next
;
416 f_dependency(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
417 struct parsedb_state
*ps
,
418 const char *value
, const struct fieldinfo
*fip
)
420 const char *p
, *emsg
;
421 static struct varbuf depname
, version
;
423 struct dependency
**ldypp
;
425 /* Empty fields are ignored. */
430 ldypp
= &pkgbin
->depends
;
432 ldypp
= &(*ldypp
)->next
;
434 /* Loop creating new struct dependency's. */
436 struct deppossi
**ldopp
;
437 struct dependency
*dyp
;
439 dyp
= nfmalloc(sizeof(*dyp
));
440 /* Set this to NULL for now, as we don't know what our real
441 * struct pkginfo address (in the database) is going to be yet. */
443 dyp
->next
= NULL
; *ldypp
= dyp
; ldypp
= &dyp
->next
;
444 dyp
->list
= NULL
; ldopp
= &dyp
->list
;
445 dyp
->type
= fip
->integer
;
447 /* Loop creating new struct deppossi's. */
449 const char *depnamestart
;
451 struct deppossi
*dop
;
454 /* Skip over package name characters. */
455 while (*p
&& !c_isspace(*p
) && *p
!= ':' && *p
!= '(' && *p
!= ',' &&
458 depnamelength
= p
- depnamestart
;
459 if (depnamelength
== 0)
461 _("'%s' field, missing package name, or garbage where "
462 "package name expected"), fip
->name
);
464 varbuf_set_buf(&depname
, depnamestart
, depnamelength
);
466 emsg
= pkg_name_is_illegal(depname
.buf
);
469 _("'%s' field, invalid package name '%.255s': %s"),
470 fip
->name
, depname
.buf
, emsg
);
471 dop
= nfmalloc(sizeof(*dop
));
473 dop
->ed
= pkg_hash_find_set(depname
.buf
);
474 dop
->next
= NULL
; *ldopp
= dop
; ldopp
= &dop
->next
;
476 /* Don't link this (which is after all only ‘new_pkg’ from
477 * the main parsing loop in parsedb) into the depended on
478 * packages' lists yet. This will be done later when we
479 * install this (in parse.c). For the moment we do the
480 * ‘forward’ links in deppossi (‘ed’) only, and the ‘backward’
481 * links from the depended on packages to dop are left undone. */
482 dop
->rev_next
= NULL
;
483 dop
->rev_prev
= NULL
;
485 dop
->cyclebreak
= false;
487 /* See if we have an architecture qualifier. */
489 static struct varbuf arch
;
490 const char *archstart
;
494 while (*p
&& !c_isspace(*p
) && *p
!= '(' && *p
!= ',' && *p
!= '|')
496 archlength
= p
- archstart
;
498 parse_error(ps
, _("'%s' field, missing architecture name, or garbage "
499 "where architecture name expected"), fip
->name
);
501 varbuf_set_buf(&arch
, archstart
, archlength
);
503 dop
->arch_is_implicit
= false;
504 dop
->arch
= dpkg_arch_find(arch
.buf
);
506 if (dop
->arch
->type
== DPKG_ARCH_ILLEGAL
)
507 emsg
= dpkg_arch_name_is_illegal(arch
.buf
);
509 parse_error(ps
, _("'%s' field, reference to '%.255s': "
510 "invalid architecture name '%.255s': %s"),
511 fip
->name
, depname
.buf
, arch
.buf
, emsg
);
512 } else if (fip
->integer
== dep_conflicts
|| fip
->integer
== dep_breaks
||
513 fip
->integer
== dep_replaces
) {
514 /* Conflicts/Breaks/Replaces get an implicit "any" arch qualifier. */
515 dop
->arch_is_implicit
= true;
516 dop
->arch
= dpkg_arch_get(DPKG_ARCH_WILDCARD
);
518 /* Otherwise use the pkgbin architecture, which will be assigned to
519 * later on by parse.c, once we can guarantee we have parsed it from
520 * the control stanza. */
521 dop
->arch_is_implicit
= true;
525 /* Skip whitespace after package name. */
526 while (c_isspace(*p
))
529 /* See if we have a versioned relation. */
532 const char *versionstart
;
536 while (c_isspace(*p
))
539 if (c1
== '<' || c1
== '>') {
543 dop
->verrel
= (c1
== '<') ? DPKG_RELATION_LT
: DPKG_RELATION_GT
;
545 dop
->verrel
|= DPKG_RELATION_EQ
;
547 } else if (c2
== c1
) {
548 /* Either ‘<<’ or ‘>>’. */
550 } else if (c2
== '<' || c2
== '>') {
552 _("'%s' field, reference to '%.255s':\n"
553 " bad version relationship %c%c"),
554 fip
->name
, depname
.buf
, c1
, c2
);
555 dop
->verrel
= DPKG_RELATION_NONE
;
558 _("'%s' field, reference to '%.255s':\n"
559 " '%c' is obsolete, use '%c=' or '%c%c' instead"),
560 fip
->name
, depname
.buf
, c1
, c1
, c1
, c1
);
561 dop
->verrel
|= DPKG_RELATION_EQ
;
563 } else if (c1
== '=') {
564 dop
->verrel
= DPKG_RELATION_EQ
;
568 _("'%s' field, reference to '%.255s':\n"
569 " implicit exact match on version number, "
570 "suggest using '=' instead"),
571 fip
->name
, depname
.buf
);
572 dop
->verrel
= DPKG_RELATION_EQ
;
574 if ((dop
->verrel
!= DPKG_RELATION_EQ
) && (fip
->integer
== dep_provides
))
576 _("only exact versions may be used for '%s' field"),
579 if (!c_isspace(*p
) && !c_isalnum(*p
)) {
581 _("'%s' field, reference to '%.255s':\n"
582 " version value starts with non-alphanumeric, "
583 "suggest adding a space"),
584 fip
->name
, depname
.buf
);
586 /* Skip spaces between the relation and the version. */
587 while (c_isspace(*p
))
591 while (*p
&& *p
!= ')' && *p
!= '(') {
596 versionlength
= p
- versionstart
;
597 while (c_isspace(*p
))
601 _("'%s' field, reference to '%.255s': "
602 "version unterminated"), fip
->name
, depname
.buf
);
605 _("'%s' field, reference to '%.255s': "
606 "version contains '%c' instead of '%c'"),
607 fip
->name
, depname
.buf
, *p
, ')');
608 varbuf_set_buf(&version
, versionstart
, versionlength
);
609 if (parse_db_version(ps
, &dop
->version
, version
.buf
) < 0)
611 _("'%s' field, reference to '%.255s': version '%s'"),
612 fip
->name
, depname
.buf
, version
.buf
);
614 while (c_isspace(*p
))
617 dop
->verrel
= DPKG_RELATION_NONE
;
618 dpkg_version_blank(&dop
->version
);
620 if (!*p
|| *p
== ',') break;
623 _("'%s' field, syntax error after reference to package '%.255s'"),
624 fip
->name
, dop
->ed
->name
);
625 if (fip
->integer
== dep_conflicts
||
626 fip
->integer
== dep_breaks
||
627 fip
->integer
== dep_provides
||
628 fip
->integer
== dep_replaces
)
629 parse_error(ps
, _("alternatives ('|') not allowed in '%s' field"),
632 while (c_isspace(*p
))
637 while (c_isspace(*p
))
643 f_obs_dependency(struct pkginfo
*pkg
, struct pkgbin
*pkgbin
,
644 struct parsedb_state
*ps
,
645 const char *value
, const struct fieldinfo
*fip
)
647 parse_warn(ps
, _("obsolete '%s' field used"), fip
->name
);
648 f_dependency(pkg
, pkgbin
, ps
, value
, fip
);
652 scan_word(const char **valp
)
654 static struct varbuf word
;
655 const char *p
, *start
, *end
;
671 if (*p
&& !c_iswhite(*p
)) {
679 varbuf_set_buf(&word
, start
, end
- start
);
687 f_trigpend(struct pkginfo
*pend
, struct pkgbin
*pkgbin
,
688 struct parsedb_state
*ps
,
689 const char *value
, const struct fieldinfo
*fip
)
693 if (ps
->flags
& pdb_rejectstatus
)
695 _("value for '%s' field not allowed in this context"),
698 while ((word
= scan_word(&value
))) {
701 emsg
= trig_name_is_illegal(word
);
704 _("illegal pending trigger name '%.255s': %s"), word
, emsg
);
706 if (!trig_note_pend_core(pend
, nfstrsave(word
)))
708 _("duplicate pending trigger '%.255s'"), word
);
713 f_trigaw(struct pkginfo
*aw
, struct pkgbin
*pkgbin
,
714 struct parsedb_state
*ps
,
715 const char *value
, const struct fieldinfo
*fip
)
719 if (ps
->flags
& pdb_rejectstatus
)
721 _("value for '%s' field not allowed in this context"),
724 while ((word
= scan_word(&value
))) {
725 struct pkginfo
*pend
;
726 struct dpkg_error err
;
728 pend
= pkg_spec_parse_pkg(word
, &err
);
731 _("illegal package name in awaited trigger '%.255s': %s"),
734 if (!trig_note_aw(pend
, aw
))
736 _("duplicate awaited trigger package '%.255s'"), word
);
738 trig_awaited_pend_enqueue(pend
);