xref: /freebsd/usr.sbin/sysconf/sysconf_priv.h (revision 0380d016d9ba407af12eb743b7c5806d49ddf848)
1 /*
2  * Copyright (c) 2013-2026 Devin Teske <dteske@FreeBSD.org>
3  * Copyright (c) 2021-2026 Faraz Vahedi <kfv@FreeBSD.org>
4  *
5  * SPDX-License-Identifier: BSD-2-Clause
6  */
7 
8 #ifndef _SYSCONF_PRIV_H_
9 #define _SYSCONF_PRIV_H_
10 
11 #ifdef __FreeBSD__
12 #include <sys/param.h>
13 #include <sys/capsicum.h>
14 #include <sys/jail.h>
15 #include <sys/linker.h>
16 #include <sys/sysctl.h>
17 
18 #include <capsicum_helpers.h>
19 #include <jail.h>
20 #endif
21 
22 #include <bsdconf.h>
23 #include <dirent.h>
24 #include <err.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <inttypes.h>
28 #include <limits.h>
29 #include <stdbool.h>
30 #include <stdint.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 
36 #ifndef __unused
37 #define __unused	/* not all compilers support attributes */
38 #endif
39 
40 #define SYSCONF_VERSION	"1.1 2026-09-15"
41 
42 /* getopt(3) optstring; shared by parse_options() and find_target() */
43 #define OPTSTRING	"AacdDEeFf:hij:k:lLnNqR:svVx"
44 
45 /* One assignment step recorded for `-V' trails */
46 struct trail_step {
47 	size_t		fileidx;	/* index into conf_files */
48 	uint32_t	line;		/* 1-based line in that file */
49 	enum bsdconf_op	op;		/* operator of this statement */
50 	char		*piece;		/* unquoted fragment */
51 	char		*effective;	/* value after applying the op */
52 };
53 
54 /*
55  * One make(1) assignment statement for `name-=word' strikes: the last
56  * statement whose piece contains the word is rewritten (or removed if
57  * the piece becomes empty).
58  */
59 struct make_assign {
60 	size_t		fileidx;
61 	uint32_t	line;
62 	enum bsdconf_op	op;
63 	char		*piece;
64 };
65 
66 /* Requests to process (one per name argument) */
67 struct request {
68 	const char	*name;		/* directive name */
69 	const char	*newvalue;	/* value to write (NULL for reads) */
70 	enum bsdconf_op	op;		/* assignment operator (writes) */
71 	char		listop;		/* `+' append / `-' strike words */
72 	char		*value;		/* value read from file(s) */
73 	int		found;		/* directive seen while parsing */
74 	int		append_present; /* identical make `+=value' line */
75 	int		remove;		/* nonzero for `-x' removals */
76 	int		srcidx;		/* conf file that last set the value
77 					   (authoritative); -1 if unset */
78 	uint32_t	line;		/* 1-based line of that definition */
79 	unsigned char	*infile;	/* per-file presence map */
80 	struct trail_step *trail;	/* `-V' assignment steps */
81 	size_t		ntrail;
82 	struct make_assign *assigns;	/* make `-=': per-statement pieces */
83 	size_t		nassigns;
84 };
85 
86 /* Union of all directives, for `-a' against multi-file targets */
87 struct dumpent {
88 	char		*name;		/* directive name */
89 	char		*value;		/* last value seen */
90 	int		srcidx;		/* conf file that last set the value */
91 	uint32_t	line;		/* 1-based line of that definition */
92 	struct trail_step *trail;	/* `-V' assignment steps */
93 	size_t		ntrail;
94 };
95 
96 /* Request / dump state (defined in sysconf.c) */
97 extern struct request	*reqs;
98 extern unsigned int	nreqs;
99 extern struct dumpent	*dumps;
100 extern size_t		ndumps;
101 extern size_t		dumpsize;
102 
103 /* Resolved target state */
104 extern char		**conf_files;
105 extern size_t		nconf_files;
106 extern size_t		conf_scanidx;
107 extern size_t		write_idx;
108 extern int		defaults_idx;
109 extern enum bsdconf_format format;
110 
111 /* Display / mode flags */
112 extern const char	*pgm;
113 extern bool		check;
114 extern bool		defaults_only;
115 extern bool		dump_all;
116 extern bool		existing_only;
117 extern bool		ignore_unknown;
118 extern bool		list_all;
119 extern bool		list_files;
120 extern bool		name_only;
121 extern bool		quiet;
122 extern bool		remove_mode;
123 extern bool		set_knobs;
124 extern bool		show_desc;
125 extern bool		show_equals;
126 extern bool		show_file;
127 extern bool		value_only;
128 extern bool		verbose;
129 extern bool		verbose_trail;
130 extern bool		with_defaults;
131 
132 /* Option arguments */
133 extern const char	*file;
134 extern bool		file_stdin;
135 extern const char	*jailname;
136 extern const char	*module;
137 extern const char	*rootdir;
138 
139 /* sysconf_print.c */
140 int		 make_knob_name_p(const char *_name);
141 int		 make_knob_p(const char *_name);
142 void		 warn_with_equals_no(const char *_name, const char *_value,
143 		    const char *_path, uint32_t _line);
144 void		 print_pair(const char *_path, const char *_directive,
145 		    const char *_value);
146 const char	*op_str(enum bsdconf_op _op);
147 int		 trail_push(struct trail_step **_trailp, size_t *_ntrailp,
148 		    size_t _fileidx, uint32_t _line, enum bsdconf_op _op,
149 		    const char *_piece, const char *_effective);
150 void		 trail_free(struct trail_step *_trail, size_t _ntrail);
151 void		 print_trail(const char *_name,
152 		    const struct trail_step *_trail, size_t _ntrail);
153 void		 print_write_echo(const char *_path, uint32_t _line,
154 		    const char *_name, enum bsdconf_op _op, const char *_oldv,
155 		    const char *_newv, int _changed, int _added, int _removed);
156 
157 /* sysconf_resolve.c */
158 char		*defaults_path(void);
159 void		 resolve_target(const char *_target);
160 int		 do_list(void);
161 
162 /* sysconf_scan.c */
163 char		*make_apply(enum bsdconf_op _op, const char *_old,
164 		    const char *_piece);
165 int		 scan_pass(int _sandbox);
166 
167 /* sysconf_edit_assign.c */
168 void		 split_request(char *_arg, struct request *_req, int _remove);
169 int		 list_has(const char *_list, const char *_word, size_t _wlen);
170 char		*list_merge(const char *_current, const char *_words, int _op);
171 int		 merge_list_requests(void);
172 
173 /* sysconf_edit_make.c */
174 int		 make_strike_p(const struct request *_req);
175 int		 make_assign_push(struct request *_req, enum bsdconf_op _op,
176 		    const char *_piece, uint32_t _line);
177 int		 do_make_strikes(int _check_only);
178 
179 /* sysconf_edit_write.c */
180 int		 do_checks(void);
181 int		 do_writes(void);
182 
183 /* sysconf_query_value.c */
184 int		 do_reads(void);
185 
186 /* sysconf_query_desc.c */
187 void		 load_descriptions(void);
188 void		 print_desc(const char *_directive);
189 int		 describe_defaults(void);
190 
191 /* sysconf_query_sysctl.c */
192 #if defined(__FreeBSD__)
193 int		 sysctl_descr(const char *_name, char *_buf, size_t _bufsize);
194 int		 describe_sysctl(void);
195 int		 sysctl_writable(const char *_name, const char *_value,
196 		    int _quiet_unknown);
197 #endif
198 
199 /* sysconf.c */
200 void		 usage(void);
201 void		 help(void);
202 
203 #endif /* !_SYSCONF_PRIV_H_ */
204