Home | History | Annotate | Line # | Download | only in lib
      1 /* $NetBSD: lib.h,v 1.13 2025/05/09 13:26:38 wiz 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 /*
    235  * To improve performance when handling lists containing a large number of
    236  * packages, it can be beneficial to use hashed lookups to avoid excessive
    237  * strcmp() calls when searching for existing entries.
    238  *
    239  * The simple hashing function below uses the first 3 characters of either a
    240  * pattern match or package name (as they are guaranteed to exist).
    241  *
    242  * Based on pkgsrc package names across the tree, this can still result in
    243  * somewhat uneven distribution due to high numbers of packages beginning with
    244  * "p5-", "php", "py-" etc, and so there are diminishing returns when trying to
    245  * use a hash size larger than around 16 or so.
    246  */
    247 #define PKG_HASH_SIZE		16
    248 #define PKG_HASH_ENTRY(x)	(((unsigned char)(x)[0] \
    249 				+ (unsigned char)(x)[1] * 257 \
    250 				+ (unsigned char)(x)[2] * 65537) \
    251 				& (PKG_HASH_SIZE - 1))
    252 
    253 struct pkg_vulnerabilities {
    254 	size_t	entries;
    255 	char	**vulnerability;
    256 	char	**classification;
    257 	char	**advisory;
    258 };
    259 
    260 /* If URLlength()>0, then there is a ftp:// or http:// in the string,
    261  * and this must be an URL. Hide this behind a more obvious name. */
    262 #define IS_URL(str)	(URLlength(str) > 0)
    263 
    264 #define IS_STDIN(str)	((str) != NULL && !strcmp((str), "-"))
    265 #define IS_FULLPATH(str)	((str) != NULL && (str)[0] == '/')
    266 
    267 /* Conflict handling (conflicts.c) */
    268 int	some_installed_package_conflicts_with(const char *, const char *, char **, char **);
    269 
    270 
    271 /* Prototypes */
    272 /* Misc */
    273 void    show_version(void) __attribute__ ((noreturn));
    274 int	fexec(const char *, ...);
    275 int	fexec_skipempty(const char *, ...);
    276 int	fcexec(const char *, const char *, ...);
    277 int	pfcexec(const char *, const char *, const char **);
    278 
    279 /* variables file handling */
    280 
    281 char   *var_get(const char *, const char *);
    282 char   *var_get_memory(const char *, const char *);
    283 int	var_set(const char *, const char *, const char *);
    284 int     var_copy_list(const char *, const char **);
    285 
    286 /* automatically installed as dependency */
    287 
    288 Boolean	is_automatic_installed(const char *);
    289 int	mark_as_automatic_installed(const char *, int);
    290 
    291 /* String */
    292 const char *basename_of(const char *);
    293 const char *dirname_of(const char *);
    294 const char *suffix_of(const char *);
    295 int     pkg_match(const char *, const char *);
    296 int	pkg_order(const char *, const char *, const char *);
    297 int     ispkgpattern(const char *);
    298 int	quick_pkg_match(const char *, const char *);
    299 
    300 /* Iterator functions */
    301 int	iterate_pkg_generic_src(int (*)(const char *, void *), void *,
    302 				const char *(*)(void *),void *);
    303 int	iterate_local_pkg_dir(const char *, int, int, int (*)(const char *, void *),
    304 			      void *);
    305 int	iterate_pkg_db(int (*)(const char *, void *), void *);
    306 
    307 int	add_installed_pkgs_by_basename(const char *, lpkg_head_t *);
    308 int	add_installed_pkgs_by_pattern(const char *, lpkg_head_t *);
    309 char	*find_best_matching_installed_pkg(const char *, int);
    310 char	*find_best_matching_file(const char *, const char *, int, int);
    311 int	match_installed_pkgs(const char *, int (*)(const char *, void *), void *);
    312 int	match_local_files(const char *, int, int, const char *, int (*cb)(const char *, void *), void *);
    313 
    314 /* File */
    315 Boolean fexists(const char *);
    316 Boolean isdir(const char *);
    317 Boolean islinktodir(const char *);
    318 Boolean isemptydir(const char *);
    319 Boolean isemptyfile(const char *);
    320 Boolean isfile(const char *);
    321 Boolean isbrokenlink(const char *);
    322 Boolean isempty(const char *);
    323 int     URLlength(const char *);
    324 Boolean make_preserve_name(char *, size_t, const char *, const char *);
    325 void    remove_files(const char *, const char *);
    326 int     format_cmd(char *, size_t, const char *, const char *, const char *);
    327 
    328 int	recursive_remove(const char *, int);
    329 
    330 void	add_pkgdir(const char *, const char *, const char *);
    331 void	delete_pkgdir(const char *, const char *, const char *);
    332 int	has_pkgdir(const char *);
    333 
    334 /* pkg_io.c: Local and remote archive handling */
    335 struct archive;
    336 struct archive_entry;
    337 
    338 struct archive *prepare_archive(void);
    339 struct archive *open_archive(const char *, char **);
    340 struct archive *find_archive(const char *, int, char **);
    341 void	process_pkg_path(void);
    342 struct url *find_best_package(const char *, const char *, int);
    343 
    344 /* Packing list */
    345 plist_t *new_plist_entry(void);
    346 plist_t *last_plist(package_t *);
    347 plist_t *find_plist(package_t *, pl_ent_t);
    348 char   *find_plist_option(package_t *, const char *);
    349 void    plist_delete(package_t *, Boolean, pl_ent_t, char *);
    350 void    free_plist(package_t *);
    351 void    mark_plist(package_t *);
    352 void    csum_plist_entry(char *, plist_t *);
    353 void    add_plist(package_t *, pl_ent_t, const char *);
    354 void    add_plist_top(package_t *, pl_ent_t, const char *);
    355 void    delete_plist(package_t *, Boolean, pl_ent_t, char *);
    356 void    write_plist(package_t *, FILE *, char *);
    357 void	stringify_plist(package_t *, char **, size_t *, const char *);
    358 void	parse_plist(package_t *, const char *);
    359 void    read_plist(package_t *, FILE *);
    360 void    append_plist(package_t *, FILE *);
    361 int     delete_package(Boolean, package_t *, Boolean, const char *);
    362 
    363 /* Package Database */
    364 int     pkgdb_open(int);
    365 void    pkgdb_close(void);
    366 int     pkgdb_store(const char *, const char *);
    367 char   *pkgdb_retrieve(const char *);
    368 int	pkgdb_dump(void);
    369 int     pkgdb_remove(const char *);
    370 int	pkgdb_remove_pkg(const char *);
    371 char   *pkgdb_refcount_dir(void);
    372 char   *pkgdb_get_database(void);
    373 const char   *pkgdb_get_dir(void);
    374 /*
    375  * Priorities:
    376  * 0 builtin default
    377  * 1 config file
    378  * 2 environment
    379  * 3 command line
    380  */
    381 void	pkgdb_set_dir(const char *, int);
    382 char   *pkgdb_pkg_dir(const char *);
    383 char   *pkgdb_pkg_file(const char *, const char *);
    384 
    385 /* List of packages functions */
    386 lpkg_t *alloc_lpkg(const char *);
    387 lpkg_t *find_on_queue(lpkg_head_t *, const char *);
    388 void    free_lpkg(lpkg_t *);
    389 
    390 /* Read pkg_vulnerabilities from file */
    391 struct pkg_vulnerabilities *read_pkg_vulnerabilities_file(const char *, int, int);
    392 /* Read pkg_vulnerabilities from memory */
    393 struct pkg_vulnerabilities *read_pkg_vulnerabilities_memory(void *, size_t, int);
    394 void free_pkg_vulnerabilities(struct pkg_vulnerabilities *);
    395 int audit_package(struct pkg_vulnerabilities *, const char *, const char *,
    396     int, int);
    397 
    398 /* Parse configuration file */
    399 void pkg_install_config(void);
    400 /* Print configuration variable */
    401 void pkg_install_show_variable(const char *);
    402 
    403 /* Package signature creation and validation */
    404 int pkg_verify_signature(const char *, struct archive **, struct archive_entry **, char **);
    405 int pkg_full_signature_check(const char *, struct archive **);
    406 #ifdef HAVE_SSL
    407 void pkg_sign_x509(const char *, const char *, const char *, const char *);
    408 #endif
    409 
    410 void pkg_sign_gpg(const char *, const char *);
    411 
    412 #ifdef HAVE_SSL
    413 /* PKCS7 signing/verification */
    414 int easy_pkcs7_verify(const char *, size_t, const char *, size_t,
    415     const char *, int);
    416 int easy_pkcs7_sign(const char *, size_t, char **, size_t *, const char *,
    417     const char *);
    418 #endif
    419 
    420 int gpg_verify(const char *, size_t, const char *, const char *, size_t);
    421 int detached_gpg_sign(const char *, size_t, char **, size_t *, const char *,
    422     const char *);
    423 
    424 /* License handling */
    425 int add_licenses(const char *);
    426 int acceptable_license(const char *);
    427 int acceptable_pkg_license(const char *);
    428 void load_license_lists(void);
    429 
    430 /* Helper functions for memory allocation */
    431 char *xstrdup(const char *);
    432 void *xrealloc(void *, size_t);
    433 void *xcalloc(size_t, size_t);
    434 void *xmalloc(size_t);
    435 #if defined(__GNUC__) && __GNUC__ >= 2
    436 char	*xasprintf(const char *, ...)
    437 			   __attribute__((__format__(__printf__, 1, 2)));
    438 #else
    439 char	*xasprintf(const char *, ...);
    440 #endif
    441 
    442 /* Externs */
    443 extern Boolean Verbose;
    444 extern Boolean Fake;
    445 extern Boolean Force;
    446 extern Boolean HideStdout;
    447 extern const char *cert_chain_file;
    448 extern const char *certs_packages;
    449 extern const char *certs_pkg_vulnerabilities;
    450 extern const char *check_eol;
    451 extern const char *check_os_version;
    452 extern const char *check_vulnerabilities;
    453 extern const char *config_file;
    454 extern const char *config_pkg_dbdir;
    455 extern const char *config_pkg_path;
    456 extern const char *config_pkg_refcount_dbdir;
    457 extern const char *do_license_check;
    458 extern const char *verified_installation;
    459 extern const char *gpg_cmd;
    460 extern const char *gpg_keyring_pkgvuln;
    461 extern const char *gpg_keyring_sign;
    462 extern const char *gpg_keyring_verify;
    463 extern const char *gpg_sign_as;
    464 extern char fetch_flags[];
    465 
    466 extern const char *pkg_vulnerabilities_dir;
    467 extern const char *pkg_vulnerabilities_file;
    468 extern const char *pkg_vulnerabilities_url;
    469 extern const char *ignore_advisories;
    470 extern const char tnf_vulnerability_base[];
    471 
    472 extern const char *acceptable_licenses;
    473 extern const char *default_acceptable_licenses;
    474 
    475 #endif				/* _INST_LIB_LIB_H_ */
    476