1 /*-
2  * Copyright (c) 2011-2020 Baptiste Daroussin <bapt@FreeBSD.org>
3  * Copyright (c) 2011-2012 Julien Laffaye <jlaffaye@FreeBSD.org>
4  * Copyright (c) 2013 Matthew Seaman <matthew@FreeBSD.org>
5  * Copyright (c) 2013-2017 Vsevolod Stakhov <vsevolod@FreeBSD.org>
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  * 1. Redistributions of source code must retain the above copyright
11  *    notice, this list of conditions and the following disclaimer
12  *    in this position and unchanged.
13  * 2. Redistributions in binary form must reproduce the above copyright
14  *    notice, this list of conditions and the following disclaimer in the
15  *    documentation and/or other materials provided with the distribution.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20  * IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27  */
28 
29 #ifndef _PKG_PRIVATE_H
30 #define _PKG_PRIVATE_H
31 
32 #include "bsd_compat.h"
33 
34 #include <sys/param.h>
35 #include <sys/cdefs.h>
36 #include <sys/types.h>
37 
38 #include <archive.h>
39 #include <sqlite3.h>
40 #include <stdbool.h>
41 #include <uthash.h>
42 #include <utlist.h>
43 #include <ucl.h>
44 
45 #include "xmalloc.h"
46 #include "private/utils.h"
47 
48 #define UCL_COUNT(obj) ((obj)?((obj)->len):0)
49 
50 #define PKG_NUM_SCRIPTS 9
51 #define PKG_NUM_LUA_SCRIPTS 5
52 
53 #define PKG_HASH_SEP '~'
54 #define PKG_HASH_SEPSTR "~"
55 
56 #define PKG_HASH_DIR "Hashed"
57 
58 /*
59  * Some compatibility checks
60  */
61 #ifndef MAXLOGNAME
62 # ifdef LOGIN_NAME_MAX
63 # define MAXLOGNAME LOGIN_NAME_MAX
64 # else
65 # define MAXLOGNAME 64
66 # endif
67 #endif
68 #ifndef __unused
69 # ifdef __GNUC__
70 # define __unused __attribute__ ((__unused__))
71 # else
72 # define __unused
73 # endif
74 #endif
75 
76 #ifndef nitems
77 #define nitems(x)       (sizeof((x)) / sizeof((x)[0]))
78 #endif
79 
80 #ifndef roundup2
81 #define	roundup2(x, y)	(((x)+((y)-1))&(~((y)-1))) /* if y is powers of two */
82 #endif
83 
84 #if ARCHIVE_VERSION_NUMBER < 3000002
85 #define archive_write_add_filter_xz(a) archive_write_set_compression_xz(a)
86 #define archive_write_add_filter_bzip2(a) archive_write_set_compression_bzip2(a)
87 #define archive_write_add_filter_gzip(a) archive_write_set_compression_gzip(a)
88 #define archive_write_add_filter_none(a) archive_write_set_compression_none(a)
89 #define archive_read_support_filter_all(a) archive_read_support_compression_all(a)
90 #define archive_read_support_filter_none(a) archive_read_support_compression_none(a)
91 #define archive_read_free archive_read_finish
92 #define archive_write_free archive_write_finish
93 #define archive_entry_perm archive_entry_mode
94 
95 #ifndef UF_NOUNLINK
96 #define UF_NOUNLINK 0
97 #endif
98 
99 #ifndef SF_NOUNLINK
100 #define SF_NOUNLINK 0
101 #endif
102 
103 #endif
104 
105 #define EXTRACT_ARCHIVE_FLAGS  (ARCHIVE_EXTRACT_OWNER |ARCHIVE_EXTRACT_PERM | \
106 		ARCHIVE_EXTRACT_TIME | ARCHIVE_EXTRACT_ACL | \
107 		ARCHIVE_EXTRACT_FFLAGS|ARCHIVE_EXTRACT_XATTR)
108 
109 
110 #define HASH_FREE(data, free_func) do {      \
111 	__typeof(data) hf1, hf2;                    \
112 	HASH_ITER(hh, data, hf1, hf2) {            \
113 		HASH_DEL(data, hf1);               \
114 		free_func(hf1);                    \
115 	}                                          \
116 	data = NULL;                               \
117 } while (0)
118 
119 #define LL_FREE2(head, free_func, next) do {   \
120 	__typeof(head) l1, l2;                 \
121 	LL_FOREACH_SAFE2(head, l1, l2, next) {       \
122 		LL_DELETE2(head, l1, next);          \
123 		free_func(l1);                \
124 	}                                     \
125 	head = NULL;                          \
126 } while (0)
127 #define LL_FREE(head, free_func) LL_FREE2(head, free_func, next)
128 
129 #define DL_FREE2(head, free_func, prev, next) do {   \
130 	__typeof(head) l1, l2;                 \
131 	DL_FOREACH_SAFE2(head, l1, l2, next) {       \
132 		DL_DELETE2(head, l1, prev, next);          \
133 		free_func(l1);                \
134 	}                                     \
135 	head = NULL;                          \
136 } while (0)
137 #define DL_FREE(head, free_func) DL_FREE2(head, free_func, prev, next)
138 
139 #define HASH_NEXT(hash, data) do {            \
140 		if (data == NULL)             \
141 			data = hash;          \
142 		else                          \
143 			data = data->hh.next; \
144 		if (data == NULL)             \
145 			return (EPKG_END);    \
146 		else                          \
147 			return (EPKG_OK);     \
148 	} while (0)
149 
150 #define KHASH_MAP_INIT_STRING(name, khval_t)                                             \
151 	KHASH_INIT(name, kh_cstr_t, khval_t, 1, string_hash_func, kh_str_hash_equal)
152 
153 #define kh_string_next(head, data) do {                  \
154 	khint_t k;                                       \
155 	if (head == NULL)                                \
156 		return (EPKG_END);                       \
157 	if (data == NULL) {                              \
158 		k = kh_begin(head);                      \
159 	} else {                                         \
160 		k = kh_get_strings(head, (data));        \
161 		k++;                                     \
162 	}                                                \
163 	while (k != kh_end(head) && !kh_exist(head, k))  \
164 		k++;                                     \
165 	if (k == kh_end(head))                           \
166 		return (EPKG_END);                       \
167 	data = kh_value(head, k);                        \
168 	return (EPKG_OK);                                \
169 } while (0)
170 
171 #define kh_next(name, head, data, attrib) do {           \
172 	khint_t k;                                       \
173 	if (head == NULL)                                \
174 		return (EPKG_END);                       \
175 	if (data == NULL) {                              \
176 		k = kh_begin(head);                      \
177 	} else {                                         \
178 		k = kh_get_##name(head, (data)->attrib); \
179 		k++;                                     \
180 	}                                                \
181 	while (k != kh_end(head) && !kh_exist(head, k))  \
182 		k++;                                     \
183 	if (k == kh_end(head)) {                         \
184 		data = NULL;                             \
185 		return (EPKG_END);                       \
186 	}                                                \
187 	data = kh_value(head, k);                        \
188 	return (EPKG_OK);                                \
189 } while (0)
190 
191 #define kh_free(name, head, type, free_func) do {			\
192 	if (head) {							\
193 		type *_todelete;					\
194 		kh_foreach_value(head, _todelete, free_func(_todelete));\
195 		kh_destroy_##name(head);				\
196 		head = NULL;						\
197 	}								\
198 } while (0)
199 
200 #define kh_contains(name, h, v) ((h)?(kh_get_##name(h, v) != kh_end(h)):false)
201 
202 #define kh_each_value(h, vvar, code)							\
203 	for (khint_t __i = kh_begin(h); h != NULL && __i != kh_end(h); __i++) {		\
204 		if (!kh_exist(h, __i)) continue;					\
205 		(vvar) = kh_val(h, __i);						\
206 		code;									\
207 	}
208 
209 #define kh_count(h) ((h)?((h)->size):0)
210 
211 #define kh_safe_add(name, h, val, k) do {		\
212 	int __ret;					\
213 	khint_t __i;					\
214 	if (!h) h = kh_init_##name();			\
215 	__i = kh_put_##name(h, k, &__ret);		\
216 	if (__ret != 0)					\
217 		kh_val(h, __i) = val;			\
218 } while (0)
219 
220 #define kh_add(name, h, val, k, free_func) do {		\
221 	int __ret;					\
222 	khint_t __i;					\
223 	if (!h) h = kh_init_##name();			\
224 	__i = kh_put_##name(h, k, &__ret);		\
225 	if (__ret != 0)					\
226 		kh_val(h, __i) = val;			\
227 	else						\
228 		free_func(val);				\
229 } while (0)
230 
231 #define kh_find(name, h, k, ret) do {			\
232 	khint_t __k;					\
233 	ret = NULL;					\
234 	if (h != NULL) {				\
235 		__k = kh_get(name, h, k);		\
236 		if (__k != kh_end(h)) {			\
237 			ret = kh_value(h, __k);		\
238 		}					\
239 	}						\
240 } while (0)
241 
242 KHASH_MAP_INIT_STR(pkg_deps, struct pkg_dep *);
243 KHASH_MAP_INIT_STR(pkg_files, struct pkg_file *);
244 KHASH_MAP_INIT_STR(pkg_dirs, struct pkg_dir *);
245 KHASH_MAP_INIT_STR(pkg_config_files, struct pkg_config_file *);
246 KHASH_MAP_INIT_STR(strings, char *);
247 KHASH_MAP_INIT_STR(pkg_options, struct pkg_option *);
248 KHASH_MAP_INIT_STR(pkg_conflicts, struct pkg_conflict *);
249 
250 struct pkg_ctx {
251 	int eventpipe;
252 	int64_t debug_level;
253 	bool developer_mode;
254 	const char *pkg_rootdir;
255 	const char *dbdir;
256 	const char *cachedir;
257 	int compression_level;
258 	int rootfd;
259 	int cachedirfd;
260 	int dbdirfd;
261 	int pkg_dbdirfd;
262 	int osversion;
263 	bool archive_symlink;
264 	bool backup_libraries;
265 	const char *backup_library_path;
266 	bool triggers;
267 	const char *triggers_path;
268 	kh_strings_t *touched_dir_hash;
269 	bool defer_triggers;
270 	bool repo_accept_legacy_pkg;
271 };
272 
273 extern struct pkg_ctx ctx;
274 
275 struct pkg_repo_it;
276 struct pkg_repo;
277 struct pkg_message;
278 struct pkg_lua_script;
279 
280 struct pkg {
281 	bool		 direct;
282 	bool		 locked;
283 	bool		 automatic;
284 	bool		 vital;
285 	int64_t		 id;
286 	xstring		*scripts[PKG_NUM_SCRIPTS];
287 	struct pkg_lua_script	*lua_scripts[PKG_NUM_LUA_SCRIPTS];
288 	char			*name;
289 	char			*origin;
290 	char			*version;
291 	char			*old_version;
292 	char			*maintainer;
293 	char			*www;
294 	char			*arch;
295 	char			*abi;
296 	char			*uid;
297 	char			*digest;
298 	char			*old_digest;
299 	struct pkg_message	*message;
300 	char			*prefix;
301 	char			*comment;
302 	char			*desc;
303 	char			*sum;
304 	char			*repopath;
305 	char			*reponame;
306 	char			*repourl;
307 	char			*reason;
308 	char			*dep_formula;
309 	lic_t			 licenselogic;
310 	int64_t			 pkgsize;
311 	int64_t			 flatsize;
312 	int64_t			 old_flatsize;
313 	int64_t			 timestamp;
314 	kh_pkg_deps_t		*depshash;
315 	struct pkg_dep		*depends;
316 	kh_pkg_deps_t		*rdepshash;
317 	struct pkg_dep		*rdepends;
318 	kh_strings_t		*categories;
319 	kh_strings_t		*licenses;
320 	kh_pkg_files_t		*filehash;
321 	struct pkg_file		*files;
322 	kh_pkg_dirs_t		*dirhash;
323 	struct pkg_dir		*dirs;
324 	kh_pkg_options_t	*optionshash;
325 	struct pkg_option	*options;
326 	kh_strings_t		*users;
327 	kh_strings_t		*groups;
328 	kh_strings_t		*shlibs_required;
329 	kh_strings_t		*shlibs_provided;
330 	kh_pkg_conflicts_t	*conflictshash;
331 	struct pkg_conflict	*conflicts;
332 	kh_strings_t		*provides;
333 	kh_strings_t		*requires;
334 	kh_pkg_config_files_t	*config_files;
335 	struct pkg_kv		*annotations;
336 	unsigned			flags;
337 	int		rootfd;
338 	char		rootpath[MAXPATHLEN];
339 	char		**dir_to_del;
340 	size_t		dir_to_del_cap;
341 	size_t		dir_to_del_len;
342 	pkg_t		 type;
343 	struct pkg_repo		*repo;
344 };
345 
346 typedef enum {
347 	SCRIPT_UNKNOWN = 0,
348 	SCRIPT_SHELL,
349 	SCRIPT_LUA,
350 } script_type_t;
351 
352 struct trigger {
353 	char *name;
354 	ucl_object_t *path;
355 	ucl_object_t *path_glob;
356 	ucl_object_t *path_regex;
357 	struct {
358 		char *script;
359 		int type;
360 		bool sandbox;
361 	} script;
362 	struct {
363 		char *script;
364 		int type;
365 		bool sandbox;
366 	} cleanup;
367 	kh_strings_t *matched;
368 	struct trigger *prev, *next;
369 };
370 
371 struct pkg_create {
372 	bool overwrite;
373 	bool expand_manifest;
374 	int compression_level;
375 	pkg_formats format;
376 	time_t timestamp;
377 	const char *rootdir;
378 	const char *outdir;
379 };
380 
381 struct pkg_dep {
382 	char		*origin;
383 	char		*name;
384 	char		*version;
385 	char		*uid;
386 	bool		 locked;
387 	struct pkg_dep		*alt_next, *alt_prev; /* Chain of alternatives */
388 	struct pkg_dep	*next, *prev;
389 };
390 
391 typedef enum {
392 	PKG_FILE_NONE = 0,
393 	PKG_FILE_EXIST,
394 	PKG_FILE_SAVE,
395 } file_previous_t;
396 
397 typedef enum {
398 	PKG_MESSAGE_ALWAYS = 0,
399 	PKG_MESSAGE_INSTALL,
400 	PKG_MESSAGE_REMOVE,
401 	PKG_MESSAGE_UPGRADE,
402 } pkg_message_t;
403 
404 struct pkg_message {
405 	char			*str;
406 	char			*minimum_version;
407 	char			*maximum_version;
408 	pkg_message_t		 type;
409 	struct pkg_message	*next, *prev;
410 };
411 
412 struct pkg_lua_script {
413 	char			*script;
414 	struct pkg_lua_script	*next, *prev;
415 };
416 
417 enum pkg_conflict_type {
418 	PKG_CONFLICT_ALL = 0,
419 	PKG_CONFLICT_REMOTE_LOCAL,
420 	PKG_CONFLICT_REMOTE_REMOTE,
421 	PKG_CONFLICT_LOCAL_LOCAL
422 };
423 
424 struct pkg_conflict {
425 	char *uid;
426 	char *digest;
427 	enum pkg_conflict_type type;
428 	struct pkg_conflict *next, *prev;
429 };
430 
431 typedef enum {
432 	MERGE_NOTNEEDED = 0,
433 	MERGE_FAILED,
434 	MERGE_SUCCESS,
435 	MERGE_NOT_LOCAL,
436 } merge_status;
437 
438 struct pkg_config_file {
439 	char path[MAXPATHLEN];
440 	char *content;
441 	char *newcontent;
442 	merge_status status;
443 };
444 
445 struct pkg_file {
446 	char		 path[MAXPATHLEN];
447 	int64_t		 size;
448 	char		*sum;
449 	char		 uname[MAXLOGNAME];
450 	char		 gname[MAXLOGNAME];
451 	mode_t		 perm;
452 	uid_t		 uid;
453 	gid_t		 gid;
454 	char		 temppath[MAXPATHLEN];
455 	u_long		 fflags;
456 	struct pkg_config_file *config;
457 	struct timespec	 time[2];
458 	struct pkg_file	*next, *prev;
459 	file_previous_t	 previous;
460 };
461 
462 struct pkg_dir {
463 	char		 path[MAXPATHLEN];
464 	char		 uname[MAXLOGNAME];
465 	char		 gname[MAXLOGNAME];
466 	mode_t		 perm;
467 	u_long		 fflags;
468 	uid_t		 uid;
469 	gid_t		 gid;
470 	bool		 noattrs;
471 	struct timespec	 time[2];
472 	struct pkg_dir	*next, *prev;
473 };
474 
475 struct pkg_option {
476 	char	*key;
477 	char	*value;
478 	char	*default_value;
479 	char	*description;
480 	struct pkg_option *next, *prev;
481 };
482 
483 struct http_mirror {
484 	struct url *url;
485 	struct http_mirror *next;
486 	bool reldoc;
487 };
488 
489 struct pkg_repo_meta_key {
490 	char *pubkey;
491 	char *pubkey_type; /* TODO: should be enumeration */
492 	char *name;
493 	UT_hash_handle hh;
494 };
495 
496 typedef enum pkg_checksum_type_e {
497 	PKG_HASH_TYPE_SHA256_BASE32 = 0,
498 	PKG_HASH_TYPE_SHA256_HEX,
499 	PKG_HASH_TYPE_BLAKE2_BASE32,
500 	PKG_HASH_TYPE_SHA256_RAW,
501 	PKG_HASH_TYPE_BLAKE2_RAW,
502 	PKG_HASH_TYPE_BLAKE2S_BASE32,
503 	PKG_HASH_TYPE_BLAKE2S_RAW,
504 	PKG_HASH_TYPE_UNKNOWN
505 } pkg_checksum_type_t;
506 
507 static const char repo_meta_file[] = "meta";
508 
509 struct pkg_repo_meta {
510 
511 	char *maintainer;
512 	char *source;
513 
514 	pkg_formats packing_format;
515 	pkg_checksum_type_t digest_format;
516 
517 	char *digests;
518 	char *digests_archive;
519 	char *manifests;
520 	char *manifests_archive;
521 	char *filesite;
522 	char *filesite_archive;
523 	char *conflicts;
524 	char *conflicts_archive;
525 	char *fulldb;
526 	char *fulldb_archive;
527 
528 	char *source_identifier;
529 	int64_t revision;
530 
531 	struct pkg_repo_meta_key *keys;
532 
533 	time_t eol;
534 
535 	int version;
536 	char *repopath;
537 	bool hash;
538 	bool hash_symlink;
539 };
540 
541 struct pkg_repo_it_ops {
542 	int (*next)(struct pkg_repo_it *it, struct pkg **pkg_p, unsigned flags);
543 	void (*free)(struct pkg_repo_it *it);
544 	void (*reset)(struct pkg_repo_it *it);
545 };
546 
547 struct pkg_repo_it {
548 	struct pkg_repo *repo;
549 	struct pkg_repo_it_ops *ops;
550 	int flags;
551 	void *data;
552 };
553 
554 struct pkg_repo_ops {
555 	const char *type;
556 	/* Accessing repo */
557 	int (*init)(struct pkg_repo *);
558 	int (*access)(struct pkg_repo *, unsigned);
559 	int (*open)(struct pkg_repo *, unsigned);
560 	int (*create)(struct pkg_repo *);
561 	int (*close)(struct pkg_repo *, bool);
562 
563 	/* Updating repo */
564 	int (*update)(struct pkg_repo *, bool);
565 
566 	/* Query repo */
567 	struct pkg_repo_it * (*query)(struct pkg_repo *,
568 					const char*, const char *, match_t);
569 	struct pkg_repo_it * (*shlib_required)(struct pkg_repo *,
570 					const char *);
571 	struct pkg_repo_it * (*shlib_provided)(struct pkg_repo *,
572 					const char *);
573 	struct pkg_repo_it * (*required)(struct pkg_repo *,
574 					const char *);
575 	struct pkg_repo_it * (*provided)(struct pkg_repo *,
576 					const char *);
577 	struct pkg_repo_it * (*search)(struct pkg_repo *, const char *, match_t,
578 					pkgdb_field field, pkgdb_field sort);
579 
580 	int64_t (*stat)(struct pkg_repo *, pkg_stats_t type);
581 
582 	int (*ensure_loaded)(struct pkg_repo *repo, struct pkg *pkg, unsigned flags);
583 
584 	/* Fetch package from repo */
585 	int (*get_cached_name)(struct pkg_repo *, struct pkg *,
586 					char *dest, size_t destlen);
587 	int (*fetch_pkg)(struct pkg_repo *, struct pkg *);
588 	int (*mirror_pkg)(struct pkg_repo *repo, struct pkg *pkg,
589 		const char *destdir);
590 };
591 
592 typedef enum _pkg_repo_flags {
593 	REPO_FLAGS_USE_IPV4 = (1U << 0),
594 	REPO_FLAGS_USE_IPV6 = (1U << 1)
595 } pkg_repo_flags;
596 
597 struct pkg_repo {
598 	struct pkg_repo_ops *ops;
599 
600 	char *name;
601 	char *url;
602 	char *pubkey;
603 	mirror_t mirror_type;
604 	union {
605 		struct dns_srvinfo *srv;
606 		struct http_mirror *http;
607 	};
608 	signature_t signature_type;
609 	char *fingerprints;
610 	FILE *fh;
611 	FILE *ssh;
612 	bool silent;
613 
614 	struct fingerprint *trusted_fp;
615 	struct fingerprint *revoked_fp;
616 
617 	struct {
618 		int in;
619 		int out;
620 		pid_t pid;
621 	} sshio;
622 
623 	struct pkg_repo_meta *meta;
624 
625 	bool enable;
626 	UT_hash_handle hh;
627 
628 	unsigned int priority;
629 
630 	pkg_repo_flags flags;
631 	struct pkg_kv *env;
632 
633 	/* Opaque repository data */
634 	void *priv;
635 };
636 
637 struct keyword {
638 	/* 64 is more than enough for this */
639 	char keyword[64];
640 	struct action *actions;
641 	UT_hash_handle hh;
642 };
643 
644 struct plist {
645 	char last_file[MAXPATHLEN];
646 	const char *stage;
647 	int stagefd;
648 	bool in_include;
649 	int plistdirfd;
650 	char prefix[MAXPATHLEN];
651 	xstring *pre_install_buf;
652 	xstring *post_install_buf;
653 	xstring *pre_deinstall_buf;
654 	xstring *post_deinstall_buf;
655 	struct pkg *pkg;
656 	char *uname;
657 	char *gname;
658 	const char *slash;
659 	int64_t flatsize;
660 	hardlinks_t *hardlinks;
661 	mode_t perm;
662 	struct {
663 		char *buf;
664 		char **patterns;
665 		size_t len;
666 		size_t cap;
667 	} post_patterns;
668 	struct keyword *keywords;
669 };
670 
671 struct file_attr {
672 	char *owner;
673 	char *group;
674 	mode_t mode;
675 	u_long fflags;
676 };
677 
678 struct action {
679 	int (*perform)(struct plist *, char *, struct file_attr *);
680 	struct action *next, *prev;
681 };
682 
683 /* sql helpers */
684 
685 typedef struct _sql_prstmt {
686 	sqlite3_stmt	*stmt;
687 	const char	*sql;
688 	const char	*argtypes;
689 } sql_prstmt;
690 
691 #define STMT(x) (sql_prepared_statements[(x)].stmt)
692 #define SQL(x)  (sql_prepared_statements[(x)].sql)
693 
694 /**
695  * rc script actions
696  */
697 typedef enum {
698 	PKG_RC_START = 0,
699 	PKG_RC_STOP
700 } pkg_rc_attr;
701 
702 struct os_info {
703 	int osversion;
704 	char *name;
705 	char *version;
706 	char *version_major;
707 	char *version_minor;
708 	char *arch;
709 };
710 
711 int pkg_get_myarch(char *pkgarch, size_t sz, struct os_info *);
712 int pkg_get_myarch_legacy(char *pkgarch, size_t sz);
713 
714 /**
715  * Remove and unregister the package.
716  * @param pkg An installed package to delete
717  * @param db An opened pkgdb
718  * @param force If set to one, the function will not fail if the package is
719  * required by other packages.
720  * @return An error code.
721  */
722 int pkg_delete(struct pkg *pkg, struct pkgdb *db, unsigned flags);
723 #define PKG_DELETE_FORCE (1<<0)
724 #define PKG_DELETE_UPGRADE (1<<1)
725 #define PKG_DELETE_NOSCRIPT (1<<2)
726 #define PKG_DELETE_CONFLICT (1<<3)
727 
728 int pkg_fetch_file_to_fd(struct pkg_repo *repo, const char *url, int dest,
729     time_t *t, ssize_t offset, int64_t size, bool silent);
730 int pkg_repo_fetch_package(struct pkg *pkg);
731 int pkg_repo_mirror_package(struct pkg *pkg, const char *destdir);
732 int pkg_repo_fetch_remote_extract_fd(struct pkg_repo *repo,
733     const char *filename, time_t *t, int *rc, size_t *sz);
734 int pkg_repo_meta_dump_fd(struct pkg_repo_meta *target, const int fd);
735 int pkg_repo_fetch_meta(struct pkg_repo *repo, time_t *t);
736 
737 struct pkg_repo_meta *pkg_repo_meta_default(void);
738 int pkg_repo_meta_load(const int fd, struct pkg_repo_meta **target);
739 void pkg_repo_meta_free(struct pkg_repo_meta *meta);
740 ucl_object_t * pkg_repo_meta_to_ucl(struct pkg_repo_meta *meta);
741 bool pkg_repo_meta_is_special_file(const char *file, struct pkg_repo_meta *meta);
742 bool pkg_repo_meta_is_old_file(const char *file, struct pkg_repo_meta *meta);
743 
744 typedef enum {
745 	HASH_UNKNOWN,
746 	HASH_SHA256,
747 	HASH_BLAKE2
748 } hash_t;
749 
750 struct fingerprint {
751 	hash_t type;
752 	char hash[BUFSIZ];
753 	UT_hash_handle hh;
754 };
755 int pkg_repo_load_fingerprints(struct pkg_repo *repo);
756 
757 
758 int pkg_start_stop_rc_scripts(struct pkg *, pkg_rc_attr attr);
759 
760 int pkg_script_run(struct pkg *, pkg_script type, bool upgrade);
761 int pkg_lua_script_run(struct pkg *, pkg_lua_script type, bool upgrade);
762 ucl_object_t *pkg_lua_script_to_ucl(struct pkg_lua_script *);
763 int pkg_script_run_child(int pid, int *pstat, int inputfd, const char* script_name);
764 
765 int pkg_open2(struct pkg **p, struct archive **a, struct archive_entry **ae,
766 	      const char *path, struct pkg_manifest_key *keys, int flags, int fd);
767 
768 int pkg_validate(struct pkg *pkg, struct pkgdb *db);
769 
770 void pkg_list_free(struct pkg *, pkg_list);
771 
772 struct pkg_kv *pkg_kv_new(const char *key, const char *val);
773 void pkg_kv_free(struct pkg_kv *);
774 
775 void pkg_dep_free(struct pkg_dep *);
776 void pkg_file_free(struct pkg_file *);
777 void pkg_option_free(struct pkg_option *);
778 void pkg_conflict_free(struct pkg_conflict *);
779 void pkg_config_file_free(struct pkg_config_file *);
780 
781 int pkg_jobs_resolv(struct pkg_jobs *jobs);
782 
783 struct packing;
784 
785 int packing_init(struct packing **pack, const char *path, pkg_formats format, int clevel, time_t timestamp, bool overwrite, bool archive_symlink);
786 int packing_append_file_attr(struct packing *pack, const char *filepath,
787      const char *newpath, const char *uname, const char *gname, mode_t perm,
788      u_long fflags);
789 int packing_append_buffer(struct packing *pack, const char *buffer,
790 			  const char *path, int size);
791 int packing_append_tree(struct packing *pack, const char *treepath,
792 			const char *newroot);
793 void packing_get_filename(struct packing *pack, const char *filename);
794 void packing_finish(struct packing *pack);
795 pkg_formats packing_format_from_string(const char *str);
796 const char* packing_format_to_string(pkg_formats format);
797 bool packing_is_valid_format(const char *str);
798 
799 int pkg_delete_files(struct pkg *pkg, unsigned force);
800 int pkg_delete_dirs(struct pkgdb *db, struct pkg *pkg, struct pkg *p);
801 
802 /* pkgdb commands */
803 int sql_exec(sqlite3 *, const char *, ...);
804 int get_pragma(sqlite3 *, const char *sql, int64_t *res, bool silence);
805 
806 int pkgdb_register_pkg(struct pkgdb *db, struct pkg *pkg, int forced, const char *);
807 int pkgdb_update_shlibs_required(struct pkg *pkg, int64_t package_id, sqlite3 *s);
808 int pkgdb_update_shlibs_provided(struct pkg *pkg, int64_t package_id, sqlite3 *s);
809 int pkgdb_update_provides(struct pkg *pkg, int64_t package_id, sqlite3 *s);
810 int pkgdb_update_requires(struct pkg *pkg, int64_t package_id, sqlite3 *s);
811 int pkgdb_insert_annotations(struct pkg *pkg, int64_t package_id, sqlite3 *s);
812 int pkgdb_register_finale(struct pkgdb *db, int retcode, const char *);
813 int pkgdb_set_pkg_digest(struct pkgdb *db, struct pkg *pkg);
814 int pkgdb_is_dir_used(struct pkgdb *db, struct pkg *p, const char *dir, int64_t *res);
815 int pkgdb_file_set_cksum(struct pkgdb *db, struct pkg_file *file, const char *sha256);
816 
817 
818 int pkg_emit_manifest_buf(struct pkg*, xstring *, short, char **);
819 int pkg_emit_filelist(struct pkg *, FILE *);
820 
821 bool ucl_object_emit_buf(const ucl_object_t *obj, enum ucl_emitter emit_type,
822     xstring **buf);
823 bool ucl_object_emit_file(const ucl_object_t *obj, enum ucl_emitter emit_type,
824     FILE *);
825 
826 pkg_object* pkg_emit_object(struct pkg *pkg, short flags);
827 
828 int pkg_checksum_generate(struct pkg *pkg, char *dest, size_t destlen,
829     pkg_checksum_type_t type, bool inc_scripts, bool inc_version, bool inc_files);
830 
831 /*
832  * Calculates checksum for any data.
833  * Caller must free resulting hash after usage
834  */
835 unsigned char * pkg_checksum_data(const unsigned char *in, size_t inlen,
836 	pkg_checksum_type_t type);
837 unsigned char *pkg_checksum_fd(int fd, pkg_checksum_type_t type);
838 unsigned char *pkg_checksum_file(const char *path, pkg_checksum_type_t type);
839 unsigned char *pkg_checksum_fileat(int fd, const char *path,
840     pkg_checksum_type_t type);
841 unsigned char *pkg_checksum_symlink(const char *path,
842     pkg_checksum_type_t type);
843 unsigned char *pkg_checksum_symlinkat(int fd, const char *path,
844     pkg_checksum_type_t type);
845 int pkg_checksum_validate_file(const char *path, const  char *sum);
846 int pkg_checksum_validate_fileat(int fd, const char *path, const  char *sum);
847 
848 bool pkg_checksum_is_valid(const char *cksum, size_t clen);
849 pkg_checksum_type_t pkg_checksum_get_type(const char *cksum, size_t clen);
850 pkg_checksum_type_t pkg_checksum_file_get_type(const char *cksum, size_t clen);
851 pkg_checksum_type_t pkg_checksum_type_from_string(const char *name);
852 const char* pkg_checksum_type_to_string(pkg_checksum_type_t type);
853 size_t pkg_checksum_type_size(pkg_checksum_type_t type);
854 int pkg_checksum_calculate(struct pkg *pkg, struct pkgdb *db, bool inc_scripts,
855     bool inc_version, bool inc_files);
856 char *pkg_checksum_generate_file(const char *path, pkg_checksum_type_t type);
857 char *pkg_checksum_generate_fileat(int fd, const char *path,
858     pkg_checksum_type_t type);
859 
860 int pkg_add_upgrade(struct pkgdb *db, const char *path, unsigned flags,
861     struct pkg_manifest_key *keys, const char *location,
862     struct pkg *rp, struct pkg *lp);
863 void pkg_delete_dir(struct pkg *pkg, struct pkg_dir *dir);
864 void pkg_delete_file(struct pkg *pkg, struct pkg_file *file, unsigned force);
865 int pkg_open_root_fd(struct pkg *pkg);
866 void pkg_add_dir_to_del(struct pkg *pkg, const char *file, const char *dir);
867 struct plist *plist_new(struct pkg *p, const char *stage);
868 int plist_parse_line(struct plist *p, char *line);
869 char *extract_keywords(char *line, char **keyword, struct file_attr **attr);
870 struct file_attr *parse_keyword_args(char *args, char *keyword);
871 void plist_free(struct plist *);
872 int pkg_appendscript(struct pkg *pkg, const char *cmd, pkg_script type);
873 void free_file_attr(struct file_attr *a);
874 
875 int pkg_add_lua_script(struct pkg *pkg, const char *data, pkg_lua_script type);
876 int pkg_addscript(struct pkg *pkg, const char *data, pkg_script type);
877 int pkg_addfile(struct pkg *pkg, const char *path, const char *sha256,
878     bool check_duplicates);
879 int pkg_addfile_attr(struct pkg *pkg, const char *path, const char *sha256,
880     const char *uname, const char *gname, mode_t perm, u_long fflags,
881     bool check_duplicates);
882 
883 int pkg_adddir(struct pkg *pkg, const char *path, bool check_duplicates);
884 int pkg_adddir_attr(struct pkg *pkg, const char *path, const char *uname,
885     const char *gname, mode_t perm, u_long fflags, bool check_duplicates);
886 
887 int pkg_addstring(kh_strings_t **s, const char *value, const char *title);
888 int pkg_kv_add(struct pkg_kv **kv, const char *key, const char *value, const char *title);
889 const char *pkg_kv_get(struct pkg_kv *const*kv, const char *key);
890 int pkg_adduser(struct pkg *pkg, const char *name);
891 int pkg_addgroup(struct pkg *pkg, const char *group);
892 int pkg_addshlib_required(struct pkg *pkg, const char *name);
893 int pkg_addshlib_provided(struct pkg *pkg, const char *name);
894 int pkg_addconflict(struct pkg *pkg, const char *name);
895 int pkg_addprovide(struct pkg *pkg, const char *name);
896 int pkg_addrequire(struct pkg *pkg, const char *name);
897 int pkg_addconfig_file(struct pkg *pkg, const char *name, const char *buf);
898 
899 int pkg_addoption(struct pkg *pkg, const char *name, const char *value);
900 int pkg_addoption_default(struct pkg *pkg, const char *key, const char *default_value);
901 int pkg_addoption_description(struct pkg *pkg, const char *key, const char *description);
902 
903 int pkg_arch_to_legacy(const char *arch, char *dest, size_t sz);
904 bool pkg_is_config_file(struct pkg *p, const char *path, const struct pkg_file **file, struct pkg_config_file **cfile);
905 int pkg_message_from_ucl(struct pkg *pkg, const ucl_object_t *obj);
906 int pkg_message_from_str(struct pkg *pkg, const char *str, size_t len);
907 ucl_object_t* pkg_message_to_ucl(const struct pkg *pkg);
908 int pkg_lua_script_from_ucl(struct pkg *pkg, const ucl_object_t *obj, pkg_lua_script);
909 char* pkg_message_to_str(struct pkg *pkg);
910 
911 int metalog_open(const char *metalog);
912 void metalog_add(int type, const char *path, const char *uname,
913     const char *gname, int mode, unsigned long fflags, const char *link);
914 void metalog_close();
915 enum pkg_metalog_type {
916 	PKG_METALOG_FILE = 0,
917 	PKG_METALOG_DIR,
918 	PKG_METALOG_LINK,
919 };
920 
921 int pkg_set_from_fileat(int fd, struct pkg *pkg, pkg_attr attr, const char *file, bool trimcr);
922 void pkg_rollback_cb(void *);
923 void pkg_rollback_pkg(struct pkg *);
924 int pkg_add_fromdir(struct pkg *, const char *);
925 struct pkg_dep* pkg_adddep_chain(struct pkg_dep *chain,
926 		struct pkg *pkg, const char *name, const char *origin, const
927 		char *version, bool locked);
928 void backup_library(struct pkgdb *, struct pkg *, const char *);
929 int suggest_arch(struct pkg *, bool);
930 int set_attrsat(int fd, const char *path, mode_t perm, uid_t uid, gid_t gid, const struct timespec *ats, const struct timespec *mts);
931 
932 struct trigger *triggers_load(bool cleanup_only);
933 int triggers_execute(struct trigger *cleanup_triggers);
934 void append_touched_file(const char *path);
935 
936 #endif
937