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