1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 
3 #include <errno.h>
4 #include <fcntl.h>
5 #include <locale.h>
6 #include <stdio.h>
7 #include <sys/mman.h>
8 #include <sys/stat.h>
9 #include <sys/types.h>
10 #include <unistd.h>
11 
12 #include "sd-id128.h"
13 
14 #include "alloc-util.h"
15 #include "catalog.h"
16 #include "conf-files.h"
17 #include "fd-util.h"
18 #include "fileio.h"
19 #include "hashmap.h"
20 #include "log.h"
21 #include "memory-util.h"
22 #include "mkdir.h"
23 #include "path-util.h"
24 #include "siphash24.h"
25 #include "sort-util.h"
26 #include "sparse-endian.h"
27 #include "strbuf.h"
28 #include "string-util.h"
29 #include "strv.h"
30 #include "tmpfile-util.h"
31 
32 const char * const catalog_file_dirs[] = {
33         "/usr/local/lib/systemd/catalog/",
34         "/usr/lib/systemd/catalog/",
35         NULL
36 };
37 
38 #define CATALOG_SIGNATURE { 'R', 'H', 'H', 'H', 'K', 'S', 'L', 'P' }
39 
40 typedef struct CatalogHeader {
41         uint8_t signature[8];  /* "RHHHKSLP" */
42         le32_t compatible_flags;
43         le32_t incompatible_flags;
44         le64_t header_size;
45         le64_t n_items;
46         le64_t catalog_item_size;
47 } CatalogHeader;
48 
49 typedef struct CatalogItem {
50         sd_id128_t id;
51         char language[32]; /* One byte is used for termination, so the maximum allowed
52                             * length of the string is actually 31 bytes. */
53         le64_t offset;
54 } CatalogItem;
55 
catalog_hash_func(const CatalogItem * i,struct siphash * state)56 static void catalog_hash_func(const CatalogItem *i, struct siphash *state) {
57         siphash24_compress(&i->id, sizeof(i->id), state);
58         siphash24_compress_string(i->language, state);
59 }
60 
catalog_compare_func(const CatalogItem * a,const CatalogItem * b)61 static int catalog_compare_func(const CatalogItem *a, const CatalogItem *b) {
62         unsigned k;
63         int r;
64 
65         for (k = 0; k < ELEMENTSOF(b->id.bytes); k++) {
66                 r = CMP(a->id.bytes[k], b->id.bytes[k]);
67                 if (r != 0)
68                         return r;
69         }
70 
71         return strcmp(a->language, b->language);
72 }
73 
74 DEFINE_HASH_OPS(catalog_hash_ops, CatalogItem, catalog_hash_func, catalog_compare_func);
75 
next_header(const char ** s)76 static bool next_header(const char **s) {
77         const char *e;
78 
79         e = strchr(*s, '\n');
80 
81         /* Unexpected end */
82         if (!e)
83                 return false;
84 
85         /* End of headers */
86         if (e == *s)
87                 return false;
88 
89         *s = e + 1;
90         return true;
91 }
92 
skip_header(const char * s)93 static const char *skip_header(const char *s) {
94         while (next_header(&s))
95                 ;
96         return s;
97 }
98 
combine_entries(const char * one,const char * two)99 static char *combine_entries(const char *one, const char *two) {
100         const char *b1, *b2;
101         size_t l1, l2, n;
102         char *dest, *p;
103 
104         /* Find split point of headers to body */
105         b1 = skip_header(one);
106         b2 = skip_header(two);
107 
108         l1 = strlen(one);
109         l2 = strlen(two);
110         dest = new(char, l1 + l2 + 1);
111         if (!dest) {
112                 log_oom();
113                 return NULL;
114         }
115 
116         p = dest;
117 
118         /* Headers from @one */
119         n = b1 - one;
120         p = mempcpy(p, one, n);
121 
122         /* Headers from @two, these will only be found if not present above */
123         n = b2 - two;
124         p = mempcpy(p, two, n);
125 
126         /* Body from @one */
127         n = l1 - (b1 - one);
128         if (n > 0)
129                 p = mempcpy(p, b1, n);
130         /* Body from @two */
131         else {
132                 n = l2 - (b2 - two);
133                 p = mempcpy(p, b2, n);
134         }
135 
136         assert(p - dest <= (ptrdiff_t)(l1 + l2));
137         p[0] = '\0';
138         return dest;
139 }
140 
finish_item(OrderedHashmap * h,sd_id128_t id,const char * language,char * payload,size_t payload_size)141 static int finish_item(
142                 OrderedHashmap *h,
143                 sd_id128_t id,
144                 const char *language,
145                 char *payload, size_t payload_size) {
146 
147         _cleanup_free_ CatalogItem *i = NULL;
148         _cleanup_free_ char *prev = NULL, *combined = NULL;
149 
150         assert(h);
151         assert(payload);
152         assert(payload_size > 0);
153 
154         i = new0(CatalogItem, 1);
155         if (!i)
156                 return log_oom();
157 
158         i->id = id;
159         if (language) {
160                 assert(strlen(language) > 1 && strlen(language) < 32);
161                 strcpy(i->language, language);
162         }
163 
164         prev = ordered_hashmap_get(h, i);
165         if (prev) {
166                 /* Already have such an item, combine them */
167                 combined = combine_entries(payload, prev);
168                 if (!combined)
169                         return log_oom();
170 
171                 if (ordered_hashmap_update(h, i, combined) < 0)
172                         return log_oom();
173                 combined = NULL;
174         } else {
175                 /* A new item */
176                 combined = memdup(payload, payload_size + 1);
177                 if (!combined)
178                         return log_oom();
179 
180                 if (ordered_hashmap_put(h, i, combined) < 0)
181                         return log_oom();
182                 i = NULL;
183                 combined = NULL;
184         }
185 
186         return 0;
187 }
188 
catalog_file_lang(const char * filename,char ** lang)189 int catalog_file_lang(const char* filename, char **lang) {
190         char *beg, *end, *_lang;
191 
192         end = endswith(filename, ".catalog");
193         if (!end)
194                 return 0;
195 
196         beg = end - 1;
197         while (beg > filename && !IN_SET(*beg, '.', '/') && end - beg < 32)
198                 beg--;
199 
200         if (*beg != '.' || end <= beg + 1)
201                 return 0;
202 
203         _lang = strndup(beg + 1, end - beg - 1);
204         if (!_lang)
205                 return -ENOMEM;
206 
207         *lang = _lang;
208         return 1;
209 }
210 
catalog_entry_lang(const char * filename,unsigned line,const char * t,const char * deflang,char ** ret)211 static int catalog_entry_lang(
212                 const char* filename,
213                 unsigned line,
214                 const char* t,
215                 const char* deflang,
216                 char **ret) {
217 
218         size_t c;
219         char *z;
220 
221         c = strlen(t);
222         if (c < 2)
223                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
224                                        "[%s:%u] Language too short.", filename, line);
225         if (c > 31)
226                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
227                                        "[%s:%u] language too long.", filename, line);
228 
229         if (deflang) {
230                 if (streq(t, deflang)) {
231                         log_warning("[%s:%u] language specified unnecessarily", filename, line);
232                         return 0;
233                 }
234 
235                 log_warning("[%s:%u] language differs from default for file", filename, line);
236         }
237 
238         z = strdup(t);
239         if (!z)
240                 return -ENOMEM;
241 
242         *ret = z;
243         return 0;
244 }
245 
catalog_import_file(OrderedHashmap * h,const char * path)246 int catalog_import_file(OrderedHashmap *h, const char *path) {
247         _cleanup_fclose_ FILE *f = NULL;
248         _cleanup_free_ char *payload = NULL;
249         size_t payload_size = 0;
250         unsigned n = 0;
251         sd_id128_t id;
252         _cleanup_free_ char *deflang = NULL, *lang = NULL;
253         bool got_id = false, empty_line = true;
254         int r;
255 
256         assert(h);
257         assert(path);
258 
259         f = fopen(path, "re");
260         if (!f)
261                 return log_error_errno(errno, "Failed to open file %s: %m", path);
262 
263         r = catalog_file_lang(path, &deflang);
264         if (r < 0)
265                 log_error_errno(r, "Failed to determine language for file %s: %m", path);
266         if (r == 1)
267                 log_debug("File %s has language %s.", path, deflang);
268 
269         for (;;) {
270                 _cleanup_free_ char *line = NULL;
271                 size_t line_len;
272 
273                 r = read_line(f, LONG_LINE_MAX, &line);
274                 if (r < 0)
275                         return log_error_errno(r, "Failed to read file %s: %m", path);
276                 if (r == 0)
277                         break;
278 
279                 n++;
280 
281                 if (isempty(line)) {
282                         empty_line = true;
283                         continue;
284                 }
285 
286                 if (strchr(COMMENTS, line[0]))
287                         continue;
288 
289                 if (empty_line &&
290                     strlen(line) >= 2+1+32 &&
291                     line[0] == '-' &&
292                     line[1] == '-' &&
293                     line[2] == ' ' &&
294                     IN_SET(line[2+1+32], ' ', '\0')) {
295 
296                         bool with_language;
297                         sd_id128_t jd;
298 
299                         /* New entry */
300 
301                         with_language = line[2+1+32] != '\0';
302                         line[2+1+32] = '\0';
303 
304                         if (sd_id128_from_string(line + 2 + 1, &jd) >= 0) {
305 
306                                 if (got_id) {
307                                         if (payload_size == 0)
308                                                 return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
309                                                                        "[%s:%u] No payload text.",
310                                                                        path,
311                                                                        n);
312 
313                                         r = finish_item(h, id, lang ?: deflang, payload, payload_size);
314                                         if (r < 0)
315                                                 return r;
316 
317                                         lang = mfree(lang);
318                                         payload_size = 0;
319                                 }
320 
321                                 if (with_language) {
322                                         char *t;
323 
324                                         t = strstrip(line + 2 + 1 + 32 + 1);
325                                         r = catalog_entry_lang(path, n, t, deflang, &lang);
326                                         if (r < 0)
327                                                 return r;
328                                 }
329 
330                                 got_id = true;
331                                 empty_line = false;
332                                 id = jd;
333 
334                                 continue;
335                         }
336                 }
337 
338                 /* Payload */
339                 if (!got_id)
340                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
341                                                "[%s:%u] Got payload before ID.",
342                                                path, n);
343 
344                 line_len = strlen(line);
345                 if (!GREEDY_REALLOC(payload, payload_size + (empty_line ? 1 : 0) + line_len + 1 + 1))
346                         return log_oom();
347 
348                 if (empty_line)
349                         payload[payload_size++] = '\n';
350                 memcpy(payload + payload_size, line, line_len);
351                 payload_size += line_len;
352                 payload[payload_size++] = '\n';
353                 payload[payload_size] = '\0';
354 
355                 empty_line = false;
356         }
357 
358         if (got_id) {
359                 if (payload_size == 0)
360                         return log_error_errno(SYNTHETIC_ERRNO(EINVAL),
361                                                "[%s:%u] No payload text.",
362                                                path, n);
363 
364                 r = finish_item(h, id, lang ?: deflang, payload, payload_size);
365                 if (r < 0)
366                         return r;
367         }
368 
369         return 0;
370 }
371 
write_catalog(const char * database,struct strbuf * sb,CatalogItem * items,size_t n)372 static int64_t write_catalog(
373                 const char *database,
374                 struct strbuf *sb,
375                 CatalogItem *items,
376                 size_t n) {
377 
378         _cleanup_fclose_ FILE *w = NULL;
379         _cleanup_free_ char *p = NULL;
380         CatalogHeader header;
381         size_t k;
382         int r;
383 
384         r = mkdir_parents(database, 0755);
385         if (r < 0)
386                 return log_error_errno(r, "Failed to create parent directories of %s: %m", database);
387 
388         r = fopen_temporary(database, &w, &p);
389         if (r < 0)
390                 return log_error_errno(r, "Failed to open database for writing: %s: %m",
391                                        database);
392 
393         header = (CatalogHeader) {
394                 .signature = CATALOG_SIGNATURE,
395                 .header_size = htole64(CONST_ALIGN_TO(sizeof(CatalogHeader), 8)),
396                 .catalog_item_size = htole64(sizeof(CatalogItem)),
397                 .n_items = htole64(n),
398         };
399 
400         r = -EIO;
401 
402         k = fwrite(&header, 1, sizeof(header), w);
403         if (k != sizeof(header)) {
404                 log_error("%s: failed to write header.", p);
405                 goto error;
406         }
407 
408         k = fwrite(items, 1, n * sizeof(CatalogItem), w);
409         if (k != n * sizeof(CatalogItem)) {
410                 log_error("%s: failed to write database.", p);
411                 goto error;
412         }
413 
414         k = fwrite(sb->buf, 1, sb->len, w);
415         if (k != sb->len) {
416                 log_error("%s: failed to write strings.", p);
417                 goto error;
418         }
419 
420         r = fflush_and_check(w);
421         if (r < 0) {
422                 log_error_errno(r, "%s: failed to write database: %m", p);
423                 goto error;
424         }
425 
426         (void) fchmod(fileno(w), 0644);
427 
428         if (rename(p, database) < 0) {
429                 r = log_error_errno(errno, "rename (%s -> %s) failed: %m", p, database);
430                 goto error;
431         }
432 
433         return ftello(w);
434 
435 error:
436         (void) unlink(p);
437         return r;
438 }
439 
catalog_update(const char * database,const char * root,const char * const * dirs)440 int catalog_update(const char* database, const char* root, const char* const* dirs) {
441         _cleanup_strv_free_ char **files = NULL;
442         _cleanup_(strbuf_freep) struct strbuf *sb = NULL;
443         _cleanup_ordered_hashmap_free_free_free_ OrderedHashmap *h = NULL;
444         _cleanup_free_ CatalogItem *items = NULL;
445         ssize_t offset;
446         char *payload;
447         CatalogItem *i;
448         unsigned n;
449         int r;
450         int64_t sz;
451 
452         h = ordered_hashmap_new(&catalog_hash_ops);
453         sb = strbuf_new();
454         if (!h || !sb)
455                 return log_oom();
456 
457         r = conf_files_list_strv(&files, ".catalog", root, 0, dirs);
458         if (r < 0)
459                 return log_error_errno(r, "Failed to get catalog files: %m");
460 
461         STRV_FOREACH(f, files) {
462                 log_debug("Reading file '%s'", *f);
463                 r = catalog_import_file(h, *f);
464                 if (r < 0)
465                         return log_error_errno(r, "Failed to import file '%s': %m", *f);
466         }
467 
468         if (ordered_hashmap_size(h) <= 0) {
469                 log_info("No items in catalog.");
470                 return 0;
471         } else
472                 log_debug("Found %u items in catalog.", ordered_hashmap_size(h));
473 
474         items = new(CatalogItem, ordered_hashmap_size(h));
475         if (!items)
476                 return log_oom();
477 
478         n = 0;
479         ORDERED_HASHMAP_FOREACH_KEY(payload, i, h) {
480                 log_debug("Found " SD_ID128_FORMAT_STR ", language %s",
481                           SD_ID128_FORMAT_VAL(i->id),
482                           isempty(i->language) ? "C" : i->language);
483 
484                 offset = strbuf_add_string(sb, payload, strlen(payload));
485                 if (offset < 0)
486                         return log_oom();
487 
488                 i->offset = htole64((uint64_t) offset);
489                 items[n++] = *i;
490         }
491 
492         assert(n == ordered_hashmap_size(h));
493         typesafe_qsort(items, n, catalog_compare_func);
494 
495         strbuf_complete(sb);
496 
497         sz = write_catalog(database, sb, items, n);
498         if (sz < 0)
499                 return log_error_errno(sz, "Failed to write %s: %m", database);
500 
501         log_debug("%s: wrote %u items, with %zu bytes of strings, %"PRIi64" total size.",
502                   database, n, sb->len, sz);
503         return 0;
504 }
505 
open_mmap(const char * database,int * _fd,struct stat * _st,void ** _p)506 static int open_mmap(const char *database, int *_fd, struct stat *_st, void **_p) {
507         _cleanup_close_ int fd = -1;
508         const CatalogHeader *h;
509         struct stat st;
510         void *p;
511 
512         assert(_fd);
513         assert(_st);
514         assert(_p);
515 
516         fd = open(database, O_RDONLY|O_CLOEXEC);
517         if (fd < 0)
518                 return -errno;
519 
520         if (fstat(fd, &st) < 0)
521                 return -errno;
522 
523         if (st.st_size < (off_t) sizeof(CatalogHeader) || file_offset_beyond_memory_size(st.st_size))
524                 return -EINVAL;
525 
526         p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
527         if (p == MAP_FAILED)
528                 return -errno;
529 
530         h = p;
531         if (memcmp(h->signature, (const uint8_t[]) CATALOG_SIGNATURE, sizeof(h->signature)) != 0 ||
532             le64toh(h->header_size) < sizeof(CatalogHeader) ||
533             le64toh(h->catalog_item_size) < sizeof(CatalogItem) ||
534             h->incompatible_flags != 0 ||
535             le64toh(h->n_items) <= 0 ||
536             st.st_size < (off_t) (le64toh(h->header_size) + le64toh(h->catalog_item_size) * le64toh(h->n_items))) {
537                 munmap(p, st.st_size);
538                 return -EBADMSG;
539         }
540 
541         *_fd = TAKE_FD(fd);
542         *_st = st;
543         *_p = p;
544 
545         return 0;
546 }
547 
find_id(void * p,sd_id128_t id)548 static const char *find_id(void *p, sd_id128_t id) {
549         CatalogItem *f = NULL, key = { .id = id };
550         const CatalogHeader *h = p;
551         const char *loc;
552 
553         loc = setlocale(LC_MESSAGES, NULL);
554         if (!isempty(loc) && !STR_IN_SET(loc, "C", "POSIX")) {
555                 size_t len;
556 
557                 len = strcspn(loc, ".@");
558                 if (len > sizeof(key.language) - 1)
559                         log_debug("LC_MESSAGES value too long, ignoring: \"%.*s\"", (int) len, loc);
560                 else {
561                         strncpy(key.language, loc, len);
562                         key.language[len] = '\0';
563 
564                         f = bsearch(&key,
565                                     (const uint8_t*) p + le64toh(h->header_size),
566                                     le64toh(h->n_items),
567                                     le64toh(h->catalog_item_size),
568                                     (comparison_fn_t) catalog_compare_func);
569                         if (!f) {
570                                 char *e;
571 
572                                 e = strchr(key.language, '_');
573                                 if (e) {
574                                         *e = 0;
575                                         f = bsearch(&key,
576                                                     (const uint8_t*) p + le64toh(h->header_size),
577                                                     le64toh(h->n_items),
578                                                     le64toh(h->catalog_item_size),
579                                                     (comparison_fn_t) catalog_compare_func);
580                                 }
581                         }
582                 }
583         }
584 
585         if (!f) {
586                 zero(key.language);
587                 f = bsearch(&key,
588                             (const uint8_t*) p + le64toh(h->header_size),
589                             le64toh(h->n_items),
590                             le64toh(h->catalog_item_size),
591                             (comparison_fn_t) catalog_compare_func);
592         }
593 
594         if (!f)
595                 return NULL;
596 
597         return (const char*) p +
598                 le64toh(h->header_size) +
599                 le64toh(h->n_items) * le64toh(h->catalog_item_size) +
600                 le64toh(f->offset);
601 }
602 
catalog_get(const char * database,sd_id128_t id,char ** _text)603 int catalog_get(const char* database, sd_id128_t id, char **_text) {
604         _cleanup_close_ int fd = -1;
605         void *p = NULL;
606         struct stat st = {};
607         char *text = NULL;
608         int r;
609         const char *s;
610 
611         assert(_text);
612 
613         r = open_mmap(database, &fd, &st, &p);
614         if (r < 0)
615                 return r;
616 
617         s = find_id(p, id);
618         if (!s) {
619                 r = -ENOENT;
620                 goto finish;
621         }
622 
623         text = strdup(s);
624         if (!text) {
625                 r = -ENOMEM;
626                 goto finish;
627         }
628 
629         *_text = text;
630         r = 0;
631 
632 finish:
633         if (p)
634                 munmap(p, st.st_size);
635 
636         return r;
637 }
638 
find_header(const char * s,const char * header)639 static char *find_header(const char *s, const char *header) {
640 
641         for (;;) {
642                 const char *v;
643 
644                 v = startswith(s, header);
645                 if (v) {
646                         v += strspn(v, WHITESPACE);
647                         return strndup(v, strcspn(v, NEWLINE));
648                 }
649 
650                 if (!next_header(&s))
651                         return NULL;
652         }
653 }
654 
dump_catalog_entry(FILE * f,sd_id128_t id,const char * s,bool oneline)655 static void dump_catalog_entry(FILE *f, sd_id128_t id, const char *s, bool oneline) {
656         if (oneline) {
657                 _cleanup_free_ char *subject = NULL, *defined_by = NULL;
658 
659                 subject = find_header(s, "Subject:");
660                 defined_by = find_header(s, "Defined-By:");
661 
662                 fprintf(f, SD_ID128_FORMAT_STR " %s: %s\n",
663                         SD_ID128_FORMAT_VAL(id),
664                         strna(defined_by), strna(subject));
665         } else
666                 fprintf(f, "-- " SD_ID128_FORMAT_STR "\n%s\n",
667                         SD_ID128_FORMAT_VAL(id), s);
668 }
669 
catalog_list(FILE * f,const char * database,bool oneline)670 int catalog_list(FILE *f, const char *database, bool oneline) {
671         _cleanup_close_ int fd = -1;
672         void *p = NULL;
673         struct stat st;
674         const CatalogHeader *h;
675         const CatalogItem *items;
676         int r;
677         unsigned n;
678         sd_id128_t last_id;
679         bool last_id_set = false;
680 
681         r = open_mmap(database, &fd, &st, &p);
682         if (r < 0)
683                 return r;
684 
685         h = p;
686         items = (const CatalogItem*) ((const uint8_t*) p + le64toh(h->header_size));
687 
688         for (n = 0; n < le64toh(h->n_items); n++) {
689                 const char *s;
690 
691                 if (last_id_set && sd_id128_equal(last_id, items[n].id))
692                         continue;
693 
694                 assert_se(s = find_id(p, items[n].id));
695 
696                 dump_catalog_entry(f, items[n].id, s, oneline);
697 
698                 last_id_set = true;
699                 last_id = items[n].id;
700         }
701 
702         munmap(p, st.st_size);
703 
704         return 0;
705 }
706 
catalog_list_items(FILE * f,const char * database,bool oneline,char ** items)707 int catalog_list_items(FILE *f, const char *database, bool oneline, char **items) {
708         int r = 0;
709 
710         STRV_FOREACH(item, items) {
711                 sd_id128_t id;
712                 int k;
713                 _cleanup_free_ char *msg = NULL;
714 
715                 k = sd_id128_from_string(*item, &id);
716                 if (k < 0) {
717                         log_error_errno(k, "Failed to parse id128 '%s': %m", *item);
718                         if (r == 0)
719                                 r = k;
720                         continue;
721                 }
722 
723                 k = catalog_get(database, id, &msg);
724                 if (k < 0) {
725                         log_full_errno(k == -ENOENT ? LOG_NOTICE : LOG_ERR, k,
726                                        "Failed to retrieve catalog entry for '%s': %m", *item);
727                         if (r == 0)
728                                 r = k;
729                         continue;
730                 }
731 
732                 dump_catalog_entry(f, id, msg, oneline);
733         }
734 
735         return r;
736 }
737