xref: /netbsd-src/external/bsd/pkg_install/dist/lib/lib.h (revision bdc22b2e01993381dcefeff2bc9b56ca75a4235c)
1 /* $NetBSD: lib.h,v 1.9 2018/03/25 04:04:36 sevan Exp $ */
2 
3 /* from FreeBSD Id: lib.h,v 1.25 1997/10/08 07:48:03 charnier Exp */
4 
5 /*
6  * FreeBSD install - a package for the installation and maintainance
7  * of non-core utilities.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in the
16  *    documentation and/or other materials provided with the distribution.
17  *
18  * Jordan K. Hubbard
19  * 18 July 1993
20  *
21  * Include and define various things wanted by the library routines.
22  *
23  */
24 
25 #ifndef _INST_LIB_LIB_H_
26 #define _INST_LIB_LIB_H_
27 
28 #if HAVE_CONFIG_H
29 #include "config.h"
30 #endif
31 #include <nbcompat.h>
32 #if HAVE_SYS_STAT_H
33 #include <sys/stat.h>
34 #endif
35 #if HAVE_SYS_FILE_H
36 #include <sys/file.h>
37 #endif
38 #if HAVE_SYS_QUEUE_H
39 #include <sys/queue.h>
40 #endif
41 
42 #if HAVE_CTYPE_H
43 #include <ctype.h>
44 #endif
45 #if HAVE_DIRENT_H
46 #include <dirent.h>
47 #endif
48 #if HAVE_STDIO_H
49 #include <stdio.h>
50 #endif
51 #if HAVE_STDLIB_H
52 #include <stdlib.h>
53 #endif
54 #if HAVE_STDARG_H
55 #include <stdarg.h>
56 #endif
57 #if HAVE_STRING_H
58 #include <string.h>
59 #endif
60 #if HAVE_UNISTD_H
61 #include <unistd.h>
62 #endif
63 
64 /* Macros */
65 #ifndef __UNCONST
66 #define __UNCONST(a)	((void *)(unsigned long)(const void *)(a))
67 #endif
68 
69 #define SUCCESS	(0)
70 #define	FAIL	(-1)
71 
72 #ifndef TRUE
73 #define TRUE	(1)
74 #endif
75 
76 #ifndef FALSE
77 #define FALSE	(0)
78 #endif
79 
80 #ifndef OPSYS_NAME
81 #define OPSYS_NAME "NetBSD"
82 #endif
83 
84 #ifndef DEF_UMASK
85 #define DEF_UMASK 022
86 #endif
87 
88 #ifndef	PATH_MAX
89 #  ifdef MAXPATHLEN
90 #    define PATH_MAX	MAXPATHLEN
91 #  else
92 #    define PATH_MAX	1024
93 #  endif
94 #endif
95 
96 enum {
97 	MaxPathSize = PATH_MAX
98 };
99 
100 /* The names of our "special" files */
101 #define CONTENTS_FNAME		"+CONTENTS"
102 #define COMMENT_FNAME		"+COMMENT"
103 #define DESC_FNAME		"+DESC"
104 #define INSTALL_FNAME		"+INSTALL"
105 #define DEINSTALL_FNAME		"+DEINSTALL"
106 #define REQUIRED_BY_FNAME	"+REQUIRED_BY"
107 #define REQUIRED_BY_FNAME_TMP	"+REQUIRED_BY.tmp"
108 #define DISPLAY_FNAME		"+DISPLAY"
109 #define MTREE_FNAME		"+MTREE_DIRS"
110 #define BUILD_VERSION_FNAME	"+BUILD_VERSION"
111 #define BUILD_INFO_FNAME	"+BUILD_INFO"
112 #define INSTALLED_INFO_FNAME	"+INSTALLED_INFO"
113 #define SIZE_PKG_FNAME		"+SIZE_PKG"
114 #define SIZE_ALL_FNAME		"+SIZE_ALL"
115 #define PRESERVE_FNAME		"+PRESERVE"
116 
117 /* The names of special variables */
118 #define AUTOMATIC_VARNAME	"automatic"
119 
120 /* Prefix for extended PLIST cmd */
121 #define CMD_CHAR		'@'
122 
123 /* The name of the "prefix" environment variable given to scripts */
124 #define PKG_PREFIX_VNAME	"PKG_PREFIX"
125 
126 /* The name of the "destdir" environment variable given to scripts */
127 #define PKG_DESTDIR_VNAME	"PKG_DESTDIR"
128 
129 /*
130  * The name of the "metadatadir" environment variable given to scripts.
131  * This variable holds the location of the +-files for this package.
132  */
133 #define PKG_METADATA_DIR_VNAME	"PKG_METADATA_DIR"
134 
135 /*
136  * The name of the environment variable holding the location to the
137  * reference-counts database directory.
138  */
139 #define PKG_REFCOUNT_DBDIR_VNAME	"PKG_REFCOUNT_DBDIR"
140 
141 #define	PKG_PATTERN_MAX	MaxPathSize	/* max length of pattern, including nul */
142 #define	PKG_SUFFIX_MAX	10	/* max length of suffix, including nul */
143 
144 enum {
145 	ReadWrite,
146 	ReadOnly
147 };
148 
149 
150 /* Enumerated constants for plist entry types */
151 typedef enum pl_ent_t {
152 	PLIST_SHOW_ALL = -1,
153 	PLIST_FILE,		/*  0 */
154 	PLIST_CWD,		/*  1 */
155 	PLIST_CMD,		/*  2 */
156 	PLIST_CHMOD,		/*  3 */
157 	PLIST_CHOWN,		/*  4 */
158 	PLIST_CHGRP,		/*  5 */
159 	PLIST_COMMENT,		/*  6 */
160 	PLIST_IGNORE,		/*  7 */
161 	PLIST_NAME,		/*  8 */
162 	PLIST_UNEXEC,		/*  9 */
163 	PLIST_SRC,		/* 10 */
164 	PLIST_DISPLAY,		/* 11 */
165 	PLIST_PKGDEP,		/* 12 */
166 	PLIST_DIR_RM,		/* 13 */
167 	PLIST_OPTION,		/* 14 */
168 	PLIST_PKGCFL,		/* 15 */
169 	PLIST_BLDDEP,		/* 16 */
170 	PLIST_PKGDIR		/* 17 */
171 }       pl_ent_t;
172 
173 /* Enumerated constants for build info */
174 typedef enum bi_ent_t {
175 	BI_OPSYS,		/*  0 */
176 	BI_OS_VERSION,		/*  1 */
177 	BI_MACHINE_ARCH,	/*  2 */
178 	BI_IGNORE_RECOMMENDED,	/*  3 */
179 	BI_USE_ABI_DEPENDS,	/*  4 */
180 	BI_LICENSE,		/*  5 */
181 	BI_PKGTOOLS_VERSION,	/*  6 */
182 	BI_ENUM_COUNT		/*  7 */
183 }	bi_ent_t;
184 
185 /* Types */
186 typedef unsigned int Boolean;
187 
188 /* This structure describes a packing list entry */
189 typedef struct plist_t {
190 	struct plist_t *prev;	/* previous entry */
191 	struct plist_t *next;	/* next entry */
192 	char   *name;		/* name of entry */
193 	Boolean marked;		/* whether entry has been marked */
194 	pl_ent_t type;		/* type of entry */
195 }       plist_t;
196 
197 /* This structure describes a package's complete packing list */
198 typedef struct package_t {
199 	plist_t *head;		/* head of list */
200 	plist_t *tail;		/* tail of list */
201 }       package_t;
202 
203 #define SYMLINK_HEADER	"Symlink:"
204 #define CHECKSUM_HEADER	"MD5:"
205 
206 enum {
207 	ChecksumHeaderLen = 4,	/* strlen(CHECKSUM_HEADER) */
208 	SymlinkHeaderLen = 8,	/* strlen(SYMLINK_HEADER) */
209 	ChecksumLen = 16,
210 	LegibleChecksumLen = 33
211 };
212 
213 /* List of files */
214 typedef struct _lfile_t {
215         TAILQ_ENTRY(_lfile_t) lf_link;
216         char *lf_name;
217 } lfile_t;
218 TAILQ_HEAD(_lfile_head_t, _lfile_t);
219 typedef struct _lfile_head_t lfile_head_t;
220 #define	LFILE_ADD(lfhead,lfp,str) do {		\
221 	lfp = xmalloc(sizeof(lfile_t));		\
222 	lfp->lf_name = str;			\
223 	TAILQ_INSERT_TAIL(lfhead,lfp,lf_link);	\
224 	} while(0)
225 
226 /* List of packages */
227 typedef struct _lpkg_t {
228 	TAILQ_ENTRY(_lpkg_t) lp_link;
229 	char   *lp_name;
230 }       lpkg_t;
231 TAILQ_HEAD(_lpkg_head_t, _lpkg_t);
232 typedef struct _lpkg_head_t lpkg_head_t;
233 
234 struct pkg_vulnerabilities {
235 	size_t	entries;
236 	char	**vulnerability;
237 	char	**classification;
238 	char	**advisory;
239 };
240 
241 /* If URLlength()>0, then there is a ftp:// or http:// in the string,
242  * and this must be an URL. Hide this behind a more obvious name. */
243 #define IS_URL(str)	(URLlength(str) > 0)
244 
245 #define IS_STDIN(str)	((str) != NULL && !strcmp((str), "-"))
246 #define IS_FULLPATH(str)	((str) != NULL && (str)[0] == '/')
247 
248 /* Conflict handling (conflicts.c) */
249 int	some_installed_package_conflicts_with(const char *, const char *, char **, char **);
250 
251 
252 /* Prototypes */
253 /* Misc */
254 void    show_version(void);
255 int	fexec(const char *, ...);
256 int	fexec_skipempty(const char *, ...);
257 int	fcexec(const char *, const char *, ...);
258 int	pfcexec(const char *, const char *, const char **);
259 
260 /* variables file handling */
261 
262 char   *var_get(const char *, const char *);
263 char   *var_get_memory(const char *, const char *);
264 int	var_set(const char *, const char *, const char *);
265 int     var_copy_list(const char *, const char **);
266 
267 /* automatically installed as dependency */
268 
269 Boolean	is_automatic_installed(const char *);
270 int	mark_as_automatic_installed(const char *, int);
271 
272 /* String */
273 const char *basename_of(const char *);
274 const char *dirname_of(const char *);
275 const char *suffix_of(const char *);
276 int     pkg_match(const char *, const char *);
277 int	pkg_order(const char *, const char *, const char *);
278 int     ispkgpattern(const char *);
279 int	quick_pkg_match(const char *, const char *);
280 
281 /* Iterator functions */
282 int	iterate_pkg_generic_src(int (*)(const char *, void *), void *,
283 				const char *(*)(void *),void *);
284 int	iterate_local_pkg_dir(const char *, int, int, int (*)(const char *, void *),
285 			      void *);
286 int	iterate_pkg_db(int (*)(const char *, void *), void *);
287 
288 int	add_installed_pkgs_by_basename(const char *, lpkg_head_t *);
289 int	add_installed_pkgs_by_pattern(const char *, lpkg_head_t *);
290 char	*find_best_matching_installed_pkg(const char *);
291 char	*find_best_matching_file(const char *, const char *, int, int);
292 int	match_installed_pkgs(const char *, int (*)(const char *, void *), void *);
293 int	match_local_files(const char *, int, int, const char *, int (*cb)(const char *, void *), void *);
294 
295 /* File */
296 Boolean fexists(const char *);
297 Boolean isdir(const char *);
298 Boolean islinktodir(const char *);
299 Boolean isemptydir(const char *);
300 Boolean isemptyfile(const char *);
301 Boolean isfile(const char *);
302 Boolean isbrokenlink(const char *);
303 Boolean isempty(const char *);
304 int     URLlength(const char *);
305 Boolean make_preserve_name(char *, size_t, const char *, const char *);
306 void    remove_files(const char *, const char *);
307 int     format_cmd(char *, size_t, const char *, const char *, const char *);
308 
309 int	recursive_remove(const char *, int);
310 
311 void	add_pkgdir(const char *, const char *, const char *);
312 void	delete_pkgdir(const char *, const char *, const char *);
313 int	has_pkgdir(const char *);
314 
315 /* pkg_io.c: Local and remote archive handling */
316 struct archive;
317 struct archive_entry;
318 
319 struct archive *prepare_archive(void);
320 struct archive *open_archive(const char *, char **);
321 struct archive *find_archive(const char *, int, char **);
322 void	process_pkg_path(void);
323 struct url *find_best_package(const char *, const char *, int);
324 
325 /* Packing list */
326 plist_t *new_plist_entry(void);
327 plist_t *last_plist(package_t *);
328 plist_t *find_plist(package_t *, pl_ent_t);
329 char   *find_plist_option(package_t *, const char *);
330 void    plist_delete(package_t *, Boolean, pl_ent_t, char *);
331 void    free_plist(package_t *);
332 void    mark_plist(package_t *);
333 void    csum_plist_entry(char *, plist_t *);
334 void    add_plist(package_t *, pl_ent_t, const char *);
335 void    add_plist_top(package_t *, pl_ent_t, const char *);
336 void    delete_plist(package_t *, Boolean, pl_ent_t, char *);
337 void    write_plist(package_t *, FILE *, char *);
338 void	stringify_plist(package_t *, char **, size_t *, const char *);
339 void	parse_plist(package_t *, const char *);
340 void    read_plist(package_t *, FILE *);
341 void    append_plist(package_t *, FILE *);
342 int     delete_package(Boolean, package_t *, Boolean, const char *);
343 
344 /* Package Database */
345 int     pkgdb_open(int);
346 void    pkgdb_close(void);
347 int     pkgdb_store(const char *, const char *);
348 char   *pkgdb_retrieve(const char *);
349 int	pkgdb_dump(void);
350 int     pkgdb_remove(const char *);
351 int	pkgdb_remove_pkg(const char *);
352 char   *pkgdb_refcount_dir(void);
353 char   *pkgdb_get_database(void);
354 const char   *pkgdb_get_dir(void);
355 /*
356  * Priorities:
357  * 0 builtin default
358  * 1 config file
359  * 2 environment
360  * 3 command line
361  */
362 void	pkgdb_set_dir(const char *, int);
363 char   *pkgdb_pkg_dir(const char *);
364 char   *pkgdb_pkg_file(const char *, const char *);
365 
366 /* List of packages functions */
367 lpkg_t *alloc_lpkg(const char *);
368 lpkg_t *find_on_queue(lpkg_head_t *, const char *);
369 void    free_lpkg(lpkg_t *);
370 
371 /* Read pkg_vulnerabilities from file */
372 struct pkg_vulnerabilities *read_pkg_vulnerabilities_file(const char *, int, int);
373 /* Read pkg_vulnerabilities from memory */
374 struct pkg_vulnerabilities *read_pkg_vulnerabilities_memory(void *, size_t, int);
375 void free_pkg_vulnerabilities(struct pkg_vulnerabilities *);
376 int audit_package(struct pkg_vulnerabilities *, const char *, const char *,
377     int, int);
378 
379 /* Parse configuration file */
380 void pkg_install_config(void);
381 /* Print configuration variable */
382 void pkg_install_show_variable(const char *);
383 
384 /* Package signature creation and validation */
385 int pkg_verify_signature(const char *, struct archive **, struct archive_entry **, char **);
386 int pkg_full_signature_check(const char *, struct archive **);
387 #ifdef HAVE_SSL
388 void pkg_sign_x509(const char *, const char *, const char *, const char *);
389 #endif
390 
391 void pkg_sign_gpg(const char *, const char *);
392 
393 #ifdef HAVE_SSL
394 /* PKCS7 signing/verification */
395 int easy_pkcs7_verify(const char *, size_t, const char *, size_t,
396     const char *, int);
397 int easy_pkcs7_sign(const char *, size_t, char **, size_t *, const char *,
398     const char *);
399 #endif
400 
401 int gpg_verify(const char *, size_t, const char *, const char *, size_t);
402 int detached_gpg_sign(const char *, size_t, char **, size_t *, const char *,
403     const char *);
404 
405 /* License handling */
406 int add_licenses(const char *);
407 int acceptable_license(const char *);
408 int acceptable_pkg_license(const char *);
409 void load_license_lists(void);
410 
411 /* Helper functions for memory allocation */
412 char *xstrdup(const char *);
413 void *xrealloc(void *, size_t);
414 void *xcalloc(size_t, size_t);
415 void *xmalloc(size_t);
416 #if defined(__GNUC__) && __GNUC__ >= 2
417 char	*xasprintf(const char *, ...)
418 			   __attribute__((__format__(__printf__, 1, 2)));
419 #else
420 char	*xasprintf(const char *, ...);
421 #endif
422 
423 /* Externs */
424 extern Boolean Verbose;
425 extern Boolean Fake;
426 extern Boolean Force;
427 extern const char *cert_chain_file;
428 extern const char *certs_packages;
429 extern const char *certs_pkg_vulnerabilities;
430 extern const char *check_eol;
431 extern const char *check_vulnerabilities;
432 extern const char *config_file;
433 extern const char *config_pkg_dbdir;
434 extern const char *config_pkg_path;
435 extern const char *config_pkg_refcount_dbdir;
436 extern const char *do_license_check;
437 extern const char *verified_installation;
438 extern const char *gpg_cmd;
439 extern const char *gpg_keyring_pkgvuln;
440 extern const char *gpg_keyring_sign;
441 extern const char *gpg_keyring_verify;
442 extern const char *gpg_sign_as;
443 extern char fetch_flags[];
444 
445 extern const char *pkg_vulnerabilities_dir;
446 extern const char *pkg_vulnerabilities_file;
447 extern const char *pkg_vulnerabilities_url;
448 extern const char *ignore_advisories;
449 extern const char tnf_vulnerability_base[];
450 
451 extern const char *acceptable_licenses;
452 extern const char *default_acceptable_licenses;
453 
454 #endif				/* _INST_LIB_LIB_H_ */
455