dpkg (1.18.25) stretch; urgency=medium
[dpkg] / lib / dpkg / fields.c
CommitLineData
1479465f
GJ
1/*
2 * libdpkg - Debian packaging suite library routines
3 * fields.c - parsing of all the different fields, when reading in
4 *
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>
11 *
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.
16 *
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.
21 *
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/>.
24 */
25
26#include <config.h>
27#include <compat.h>
28
29#include <string.h>
30#include <stdio.h>
31
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>
42
43/**
44 * Flags to parse a name associated to a value.
45 */
46enum parse_nv_flags {
47 /** Expect no more words (default). */
48 PARSE_NV_LAST = 0,
49 /** Expect another word after the parsed name. */
50 PARSE_NV_NEXT = DPKG_BIT(0),
51 /** Do not fail if there is no name with an associated value found. */
52 PARSE_NV_FALLBACK = DPKG_BIT(1),
53};
54
55/**
56 * Parses a name and returns its associated value.
57 *
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 #PARSE_NV_FALLBACK is set in @a flags then @a strp is set
61 * to NULL and returns -1, otherwise a parse error is emitted.
62 */
63static int
64parse_nv(struct parsedb_state *ps, enum parse_nv_flags flags,
65 const char **strp, const struct namevalue *nv_head, const char *what)
66{
67 const char *str_start = *strp, *str_end;
68 const struct namevalue *nv;
69 int value;
70
71 if (str_start[0] == '\0')
72 parse_error(ps, _("%s is missing"), what);
73
74 nv = namevalue_find_by_name(nv_head, str_start);
75 if (nv == NULL) {
76 /* We got no match, skip further string validation. */
77 if (!(flags & PARSE_NV_FALLBACK))
78 parse_error(ps, _("'%.50s' is not allowed for %s"), str_start, what);
79
80 str_end = NULL;
81 value = -1;
82 } else {
83 str_end = str_start + nv->length;
84 while (c_isspace(str_end[0]))
85 str_end++;
86 value = nv->value;
87 }
88
89 if (!(flags & PARSE_NV_NEXT) && str_is_set(str_end))
90 parse_error(ps, _("junk after %s"), what);
91
92 *strp = str_end;
93
94 return value;
95}
96
97void
98f_name(struct pkginfo *pkg, struct pkgbin *pkgbin,
99 struct parsedb_state *ps,
100 const char *value, const struct fieldinfo *fip)
101{
102 const char *e;
103
104 e = pkg_name_is_illegal(value);
105 if (e != NULL)
106 parse_error(ps, _("invalid package name (%.250s)"), e);
107 /* We use the new name, as pkg_db_find_set() may have done a tolower for us. */
108 pkg->set->name = pkg_db_find_set(value)->name;
109}
110
111void
112f_filecharf(struct pkginfo *pkg, struct pkgbin *pkgbin,
113 struct parsedb_state *ps,
114 const char *value, const struct fieldinfo *fip)
115{
116 struct filedetails *fdp, **fdpp;
117 char *cpos, *space;
118 int allowextend;
119
120 if (!*value)
121 parse_error(ps, _("empty file details field '%s'"), fip->name);
122 if (!(ps->flags & pdb_recordavailable))
123 parse_error(ps,
124 _("file details field '%s' not allowed in status file"),
125 fip->name);
126 allowextend = !pkg->files;
127 fdpp = &pkg->files;
128 cpos= nfstrsave(value);
129 while (*cpos) {
130 space = cpos;
131 while (*space && !c_isspace(*space))
132 space++;
133 if (*space)
134 *space++ = '\0';
135 fdp= *fdpp;
136 if (!fdp) {
137 if (!allowextend)
138 parse_error(ps,
139 _("too many values in file details field '%s' "
140 "(compared to others)"), fip->name);
141 fdp= nfmalloc(sizeof(struct filedetails));
142 fdp->next= NULL;
143 fdp->name= fdp->msdosname= fdp->size= fdp->md5sum= NULL;
144 *fdpp= fdp;
145 }
146 STRUCTFIELD(fdp, fip->integer, const char *) = cpos;
147 fdpp= &fdp->next;
148 while (*space && c_isspace(*space))
149 space++;
150 cpos= space;
151 }
152 if (*fdpp)
153 parse_error(ps,
154 _("too few values in file details field '%s' "
155 "(compared to others)"), fip->name);
156}
157
158void
159f_charfield(struct pkginfo *pkg, struct pkgbin *pkgbin,
160 struct parsedb_state *ps,
161 const char *value, const struct fieldinfo *fip)
162{
163 if (*value)
164 STRUCTFIELD(pkgbin, fip->integer, char *) = nfstrsave(value);
165}
166
167void
168f_boolean(struct pkginfo *pkg, struct pkgbin *pkgbin,
169 struct parsedb_state *ps,
170 const char *value, const struct fieldinfo *fip)
171{
172 bool boolean;
173
174 if (!*value)
175 return;
176
177 boolean = parse_nv(ps, PARSE_NV_LAST, &value, booleaninfos,
178 _("yes/no in boolean field"));
179 STRUCTFIELD(pkgbin, fip->integer, bool) = boolean;
180}
181
182void
183f_multiarch(struct pkginfo *pkg, struct pkgbin *pkgbin,
184 struct parsedb_state *ps,
185 const char *value, const struct fieldinfo *fip)
186{
187 int multiarch;
188
189 if (!*value)
190 return;
191
192 multiarch = parse_nv(ps, PARSE_NV_LAST, &value, multiarchinfos,
193 _("foreign/allowed/same/no in quadstate field"));
194 STRUCTFIELD(pkgbin, fip->integer, int) = multiarch;
195}
196
197void
198f_architecture(struct pkginfo *pkg, struct pkgbin *pkgbin,
199 struct parsedb_state *ps,
200 const char *value, const struct fieldinfo *fip)
201{
202 pkgbin->arch = dpkg_arch_find(value);
203 if (pkgbin->arch->type == DPKG_ARCH_ILLEGAL)
204 parse_warn(ps, _("'%s' is not a valid architecture name: %s"),
205 value, dpkg_arch_name_is_illegal(value));
206}
207
208void
209f_section(struct pkginfo *pkg, struct pkgbin *pkgbin,
210 struct parsedb_state *ps,
211 const char *value, const struct fieldinfo *fip)
212{
213 if (!*value) return;
214 pkg->section = nfstrsave(value);
215}
216
217void
218f_priority(struct pkginfo *pkg, struct pkgbin *pkgbin,
219 struct parsedb_state *ps,
220 const char *value, const struct fieldinfo *fip)
221{
222 const char *str = value;
223 int priority;
224
225 if (!*value) return;
226
227 priority = parse_nv(ps, PARSE_NV_LAST | PARSE_NV_FALLBACK, &str,
228 priorityinfos, _("word in 'Priority' field"));
229
230 if (str == NULL) {
231 pkg->priority = PKG_PRIO_OTHER;
232 pkg->otherpriority = nfstrsave(value);
233 } else {
234 pkg->priority = priority;
235 }
236}
237
238void
239f_status(struct pkginfo *pkg, struct pkgbin *pkgbin,
240 struct parsedb_state *ps,
241 const char *value, const struct fieldinfo *fip)
242{
243 if (ps->flags & pdb_rejectstatus)
244 parse_error(ps,
245 _("value for '%s' field not allowed in this context"),
246 "Status");
247 if (ps->flags & pdb_recordavailable)
248 return;
249
250 pkg->want = parse_nv(ps, PARSE_NV_NEXT, &value, wantinfos,
251 _("first (want) word in 'Status' field"));
252 pkg->eflag = parse_nv(ps, PARSE_NV_NEXT, &value, eflaginfos,
253 _("second (error) word in 'Status' field"));
254 pkg->status = parse_nv(ps, PARSE_NV_LAST, &value, statusinfos,
255 _("third (status) word in 'Status' field"));
256}
257
258void
259f_version(struct pkginfo *pkg, struct pkgbin *pkgbin,
260 struct parsedb_state *ps,
261 const char *value, const struct fieldinfo *fip)
262{
263 parse_db_version(ps, &pkgbin->version, value,
264 _("error in '%s' field string '%.250s'"),
265 "Version", value);
266}
267
268void
269f_revision(struct pkginfo *pkg, struct pkgbin *pkgbin,
270 struct parsedb_state *ps,
271 const char *value, const struct fieldinfo *fip)
272{
273 char *newversion;
274
275 parse_warn(ps,
276 _("obsolete '%s' or '%s' field used"),
277 "Revision", "Package-Revision");
278 if (!*value) return;
279 if (str_is_set(pkgbin->version.revision)) {
280 newversion = nfmalloc(strlen(pkgbin->version.version) +
281 strlen(pkgbin->version.revision) + 2);
282 sprintf(newversion, "%s-%s", pkgbin->version.version,
283 pkgbin->version.revision);
284 pkgbin->version.version = newversion;
285 }
286 pkgbin->version.revision = nfstrsave(value);
287}
288
289void
290f_configversion(struct pkginfo *pkg, struct pkgbin *pkgbin,
291 struct parsedb_state *ps,
292 const char *value, const struct fieldinfo *fip)
293{
294 if (ps->flags & pdb_rejectstatus)
295 parse_error(ps,
296 _("value for '%s' field not allowed in this context"),
297 "Config-Version");
298 if (ps->flags & pdb_recordavailable)
299 return;
300
301 parse_db_version(ps, &pkg->configversion, value,
302 _("error in '%s' field string '%.250s'"),
303 "Config-Version", value);
304
305}
306
307/*
308 * The code in f_conffiles ensures that value[-1] == ' ', which is helpful.
309 */
310static void conffvalue_lastword(const char *value, const char *from,
311 const char *endent,
312 const char **word_start_r, int *word_len_r,
313 const char **new_from_r,
314 struct parsedb_state *ps)
315{
316 const char *lastspc;
317
318 if (from <= value+1) goto malformed;
319 for (lastspc= from-1; *lastspc != ' '; lastspc--);
320 if (lastspc <= value+1 || lastspc >= endent-1) goto malformed;
321
322 *new_from_r= lastspc;
323 *word_start_r= lastspc + 1;
324 *word_len_r= (int)(from - *word_start_r);
325 return;
326
327malformed:
328 parse_error(ps,
329 _("value for '%s' field has malformed line '%.*s'"),
330 "Conffiles", (int)min(endent - value, 250), value);
331}
332
333void
334f_conffiles(struct pkginfo *pkg, struct pkgbin *pkgbin,
335 struct parsedb_state *ps,
336 const char *value, const struct fieldinfo *fip)
337{
338 static const char obsolete_str[]= "obsolete";
339 struct conffile **lastp, *newlink;
340 const char *endent, *endfn, *hashstart;
341 int c, namelen, hashlen;
342 bool obsolete;
343 char *newptr;
344
345 lastp = &pkgbin->conffiles;
346 while (*value) {
347 c= *value++;
348 if (c == '\n') continue;
349 if (c != ' ')
350 parse_error(ps,
351 _("value for '%s' has line starting with non-space '%c'"),
352 "Conffiles", c);
353 for (endent = value; (c = *endent) != '\0' && c != '\n'; endent++) ;
354 conffvalue_lastword(value, endent, endent,
355 &hashstart, &hashlen, &endfn,
356 ps);
357 obsolete= (hashlen == sizeof(obsolete_str)-1 &&
358 memcmp(hashstart, obsolete_str, hashlen) == 0);
359 if (obsolete)
360 conffvalue_lastword(value, endfn, endent,
361 &hashstart, &hashlen, &endfn,
362 ps);
363 newlink= nfmalloc(sizeof(struct conffile));
364 value = path_skip_slash_dotslash(value);
365 namelen= (int)(endfn-value);
366 if (namelen <= 0)
367 parse_error(ps,
368 _("root or null directory is listed as a conffile"));
369 newptr = nfmalloc(namelen+2);
370 newptr[0]= '/';
371 memcpy(newptr+1,value,namelen);
372 newptr[namelen+1] = '\0';
373 newlink->name= newptr;
374 newptr= nfmalloc(hashlen+1);
375 memcpy(newptr, hashstart, hashlen);
376 newptr[hashlen] = '\0';
377 newlink->hash= newptr;
378 newlink->obsolete= obsolete;
379 newlink->next =NULL;
380 *lastp= newlink;
381 lastp= &newlink->next;
382 value= endent;
383 }
384}
385
386void
387f_dependency(struct pkginfo *pkg, struct pkgbin *pkgbin,
388 struct parsedb_state *ps,
389 const char *value, const struct fieldinfo *fip)
390{
391 char c1, c2;
392 const char *p, *emsg;
393 const char *depnamestart, *versionstart;
394 int depnamelength, versionlength;
395 static struct varbuf depname, version;
396
397 struct dependency *dyp, **ldypp;
398 struct deppossi *dop, **ldopp;
399
400 /* Empty fields are ignored. */
401 if (!*value)
402 return;
403 p= value;
404
405 ldypp = &pkgbin->depends;
406 while (*ldypp)
407 ldypp = &(*ldypp)->next;
408
409 /* Loop creating new struct dependency's. */
410 for (;;) {
411 dyp= nfmalloc(sizeof(struct dependency));
412 /* Set this to NULL for now, as we don't know what our real
413 * struct pkginfo address (in the database) is going to be yet. */
414 dyp->up = NULL;
415 dyp->next= NULL; *ldypp= dyp; ldypp= &dyp->next;
416 dyp->list= NULL; ldopp= &dyp->list;
417 dyp->type= fip->integer;
418
419 /* Loop creating new struct deppossi's. */
420 for (;;) {
421 depnamestart= p;
422 /* Skip over package name characters. */
423 while (*p && !c_isspace(*p) && *p != ':' && *p != '(' && *p != ',' &&
424 *p != '|')
425 p++;
426 depnamelength= p - depnamestart ;
427 if (depnamelength == 0)
428 parse_error(ps,
429 _("'%s' field, missing package name, or garbage where "
430 "package name expected"), fip->name);
431
432 varbuf_reset(&depname);
433 varbuf_add_buf(&depname, depnamestart, depnamelength);
434 varbuf_end_str(&depname);
435
436 emsg = pkg_name_is_illegal(depname.buf);
437 if (emsg)
438 parse_error(ps,
439 _("'%s' field, invalid package name '%.255s': %s"),
440 fip->name, depname.buf, emsg);
441 dop= nfmalloc(sizeof(struct deppossi));
442 dop->up= dyp;
443 dop->ed = pkg_db_find_set(depname.buf);
444 dop->next= NULL; *ldopp= dop; ldopp= &dop->next;
445
446 /* Don't link this (which is after all only ‘new_pkg’ from
447 * the main parsing loop in parsedb) into the depended on
448 * packages' lists yet. This will be done later when we
449 * install this (in parse.c). For the moment we do the
450 * ‘forward’ links in deppossi (‘ed’) only, and the ‘backward’
451 * links from the depended on packages to dop are left undone. */
452 dop->rev_next = NULL;
453 dop->rev_prev = NULL;
454
455 dop->cyclebreak = false;
456
457 /* See if we have an architecture qualifier. */
458 if (*p == ':') {
459 static struct varbuf arch;
460 const char *archstart;
461 int archlength;
462
463 archstart = ++p;
464 while (*p && !c_isspace(*p) && *p != '(' && *p != ',' && *p != '|')
465 p++;
466 archlength = p - archstart;
467 if (archlength == 0)
468 parse_error(ps, _("'%s' field, missing architecture name, or garbage "
469 "where architecture name expected"), fip->name);
470
471 varbuf_reset(&arch);
472 varbuf_add_buf(&arch, archstart, archlength);
473 varbuf_end_str(&arch);
474
475 dop->arch_is_implicit = false;
476 dop->arch = dpkg_arch_find(arch.buf);
477
478 if (dop->arch->type == DPKG_ARCH_ILLEGAL)
479 emsg = dpkg_arch_name_is_illegal(arch.buf);
480 if (emsg)
481 parse_error(ps, _("'%s' field, reference to '%.255s': "
482 "invalid architecture name '%.255s': %s"),
483 fip->name, depname.buf, arch.buf, emsg);
484 } else if (fip->integer == dep_conflicts || fip->integer == dep_breaks ||
485 fip->integer == dep_replaces) {
486 /* Conflicts/Breaks/Replaces get an implicit "any" arch qualifier. */
487 dop->arch_is_implicit = true;
488 dop->arch = dpkg_arch_get(DPKG_ARCH_WILDCARD);
489 } else {
490 /* Otherwise use the pkgbin architecture, which will be assigned to
491 * later on by parse.c, once we can guarantee we have parsed it from
492 * the control stanza. */
493 dop->arch_is_implicit = true;
494 dop->arch = NULL;
495 }
496
497 /* Skip whitespace after package name. */
498 while (c_isspace(*p))
499 p++;
500
501 /* See if we have a versioned relation. */
502 if (*p == '(') {
503 p++;
504 while (c_isspace(*p))
505 p++;
506 c1= *p;
507 if (c1 == '<' || c1 == '>') {
508 c2= *++p;
509 dop->verrel = (c1 == '<') ? DPKG_RELATION_LT : DPKG_RELATION_GT;
510 if (c2 == '=') {
511 dop->verrel |= DPKG_RELATION_EQ;
512 p++;
513 } else if (c2 == c1) {
514 /* Either ‘<<’ or ‘>>’. */
515 p++;
516 } else if (c2 == '<' || c2 == '>') {
517 parse_error(ps,
518 _("'%s' field, reference to '%.255s':\n"
519 " bad version relationship %c%c"),
520 fip->name, depname.buf, c1, c2);
521 dop->verrel = DPKG_RELATION_NONE;
522 } else {
523 parse_warn(ps,
524 _("'%s' field, reference to '%.255s':\n"
525 " '%c' is obsolete, use '%c=' or '%c%c' instead"),
526 fip->name, depname.buf, c1, c1, c1, c1);
527 dop->verrel |= DPKG_RELATION_EQ;
528 }
529 } else if (c1 == '=') {
530 dop->verrel = DPKG_RELATION_EQ;
531 p++;
532 } else {
533 parse_warn(ps,
534 _("'%s' field, reference to '%.255s':\n"
535 " implicit exact match on version number, "
536 "suggest using '=' instead"),
537 fip->name, depname.buf);
538 dop->verrel = DPKG_RELATION_EQ;
539 }
540 if ((dop->verrel != DPKG_RELATION_EQ) && (fip->integer == dep_provides))
541 parse_warn(ps,
542 _("only exact versions may be used for '%s' field"),
543 "Provides");
544
545 if (!c_isspace(*p) && !c_isalnum(*p)) {
546 parse_warn(ps,
547 _("'%s' field, reference to '%.255s':\n"
548 " version value starts with non-alphanumeric, "
549 "suggest adding a space"),
550 fip->name, depname.buf);
551 }
552 /* Skip spaces between the relation and the version. */
553 while (c_isspace(*p))
554 p++;
555
556 versionstart= p;
557 while (*p && *p != ')' && *p != '(') {
558 if (c_isspace(*p))
559 break;
560 p++;
561 }
562 versionlength= p - versionstart;
563 while (c_isspace(*p))
564 p++;
565 if (*p == '(')
566 parse_error(ps,
567 _("'%s' field, reference to '%.255s': "
568 "version contains '%c'"), fip->name, depname.buf, ')');
569 else if (*p != ')')
570 parse_error(ps,
571 _("'%s' field, reference to '%.255s': "
572 "version contains '%c'"), fip->name, depname.buf, ' ');
573 else if (*p == '\0')
574 parse_error(ps,
575 _("'%s' field, reference to '%.255s': "
576 "version unterminated"), fip->name, depname.buf);
577 varbuf_reset(&version);
578 varbuf_add_buf(&version, versionstart, versionlength);
579 varbuf_end_str(&version);
580 parse_db_version(ps, &dop->version, version.buf,
581 _("'%s' field, reference to '%.255s': "
582 "error in version"), fip->name, depname.buf);
583 p++;
584 while (c_isspace(*p))
585 p++;
586 } else {
587 dop->verrel = DPKG_RELATION_NONE;
588 dpkg_version_blank(&dop->version);
589 }
590 if (!*p || *p == ',') break;
591 if (*p != '|')
592 parse_error(ps,
593 _("'%s' field, syntax error after reference to package '%.255s'"),
594 fip->name, dop->ed->name);
595 if (fip->integer == dep_conflicts ||
596 fip->integer == dep_breaks ||
597 fip->integer == dep_provides ||
598 fip->integer == dep_replaces)
599 parse_error(ps,
600 _("alternatives ('|') not allowed in %s field"), fip->name);
601 p++;
602 while (c_isspace(*p))
603 p++;
604 }
605 if (!*p) break;
606 p++;
607 while (c_isspace(*p))
608 p++;
609 }
610}
611
612static const char *
613scan_word(const char **valp)
614{
615 static struct varbuf word;
616 const char *p, *start, *end;
617
618 p = *valp;
619 for (;;) {
620 if (!*p) {
621 *valp = p;
622 return NULL;
623 }
624 if (c_iswhite(*p)) {
625 p++;
626 continue;
627 }
628 start = p;
629 break;
630 }
631 for (;;) {
632 if (*p && !c_iswhite(*p)) {
633 p++;
634 continue;
635 }
636 end = p;
637 break;
638 }
639
640 varbuf_reset(&word);
641 varbuf_add_buf(&word, start, end - start);
642 varbuf_end_str(&word);
643
644 *valp = p;
645
646 return word.buf;
647}
648
649void
650f_trigpend(struct pkginfo *pend, struct pkgbin *pkgbin,
651 struct parsedb_state *ps,
652 const char *value, const struct fieldinfo *fip)
653{
654 const char *word, *emsg;
655
656 if (ps->flags & pdb_rejectstatus)
657 parse_error(ps,
658 _("value for '%s' field not allowed in this context"),
659 "Triggers-Pending");
660
661 while ((word = scan_word(&value))) {
662 emsg = trig_name_is_illegal(word);
663 if (emsg)
664 parse_error(ps,
665 _("illegal pending trigger name '%.255s': %s"), word, emsg);
666
667 if (!trig_note_pend_core(pend, nfstrsave(word)))
668 parse_error(ps,
669 _("duplicate pending trigger '%.255s'"), word);
670 }
671}
672
673void
674f_trigaw(struct pkginfo *aw, struct pkgbin *pkgbin,
675 struct parsedb_state *ps,
676 const char *value, const struct fieldinfo *fip)
677{
678 const char *word;
679 struct pkginfo *pend;
680
681 if (ps->flags & pdb_rejectstatus)
682 parse_error(ps,
683 _("value for '%s' field not allowed in this context"),
684 "Triggers-Awaited");
685
686 while ((word = scan_word(&value))) {
687 struct dpkg_error err;
688
689 pend = pkg_spec_parse_pkg(word, &err);
690 if (pend == NULL)
691 parse_error(ps,
692 _("illegal package name in awaited trigger '%.255s': %s"),
693 word, err.str);
694
695 if (!trig_note_aw(pend, aw))
696 parse_error(ps,
697 _("duplicate awaited trigger package '%.255s'"), word);
698
699 trig_awaited_pend_enqueue(pend);
700 }
701}