xref: /freebsd/crypto/krb5/src/util/profile/prof_int.h (revision 7f2fe78b9dd5f51c821d771b63d2e096f6fd49e9)
1 /*
2  * prof-int.h
3  */
4 
5 #include "k5-platform.h"
6 #include "k5-thread.h"
7 #include "k5-plugin.h"
8 
9 #include <time.h>
10 
11 #if defined(__MACH__) && defined(__APPLE__)
12 #include <TargetConditionals.h>
13 #define PROFILE_SUPPORTS_FOREIGN_NEWLINES
14 #endif
15 
16 #include "com_err.h"
17 #include "profile.h"
18 
19 typedef long prf_magic_t;
20 
21 /*
22  * This is the structure which stores the profile information for a
23  * particular configuration file.
24  *
25  * Locking strategy:
26  * - filespec, fslen are fixed after creation
27  * - refcount and next should only be tweaked with the global lock held
28  * - other fields can be tweaked after grabbing the in-struct lock
29  */
30 struct _prf_data_t {
31 	prf_magic_t	magic;
32 	k5_mutex_t	lock;
33 	struct profile_node *root;
34 	time_t		last_stat;
35 	time_t		timestamp; /* time tree was last updated from file */
36 	unsigned long	frac_ts;   /* fractional part of timestamp, if any */
37 	int		flags;	/* r/w, dirty */
38 	int		upd_serial; /* incremented when data changes */
39 
40 	size_t		fslen;
41 
42 	/* Some separation between fields controlled by different
43 	   mutexes.  Theoretically, both could be accessed at the same
44 	   time from different threads on different CPUs with separate
45 	   caches.  Don't let the threads clobber each other's
46 	   changes.  One mutex controlling the whole thing would be
47 	   better, but sufficient separation might suffice.
48 
49 	   This is icky.  I just hope it's adequate.
50 
51 	   For next major release, fix this.  */
52 	union { double d; void *p; uint64_t ll; k5_mutex_t m; } pad;
53 
54 	int		refcount; /* prf_file_t references */
55 	struct _prf_data_t *next;
56 	/* Was: "profile_filespec_t filespec".  Now: flexible char
57 	   array ... except, we need to work in C89, so an array
58 	   length must be specified.  */
59 	const char	filespec[sizeof("/etc/krb5.conf")];
60 };
61 
62 typedef struct _prf_data_t *prf_data_t;
63 prf_data_t profile_make_prf_data(const char *);
64 
65 struct _prf_file_t {
66 	prf_magic_t	magic;
67 	struct _prf_data_t	*data;
68 	struct _prf_file_t *next;
69 };
70 
71 typedef struct _prf_file_t *prf_file_t;
72 
73 /*
74  * The profile flags
75  */
76 #define PROFILE_FILE_NO_RELOAD		0x0001
77 #define PROFILE_FILE_DIRTY		0x0002
78 #define PROFILE_FILE_SHARED		0x0004
79 
80 struct _prf_lib_handle_t {
81 	k5_mutex_t lock;
82 	int refcount;
83 	struct plugin_file_handle *plugin_handle;
84 };
85 
86 typedef struct _prf_lib_handle_t *prf_lib_handle_t;
87 
88 /*
89  * This structure defines the high-level, user visible profile_t
90  * object, which is used as a handle by users who need to query some
91  * configuration file(s)
92  */
93 struct _profile_t {
94 	prf_magic_t	magic;
95 	prf_file_t	first_file;
96 
97 	/* If non-null, use vtable operations instead of native ones. */
98 	struct profile_vtable *vt;
99 	void		*cbdata;
100 	prf_lib_handle_t lib_handle;
101 };
102 
103 /*
104  * Used by the profile iterator in prof_get.c
105  */
106 #define PROFILE_ITER_LIST_SECTION	0x0001
107 #define PROFILE_ITER_SECTIONS_ONLY	0x0002
108 #define PROFILE_ITER_RELATIONS_ONLY	0x0004
109 
110 #define PROFILE_ITER_FINAL_SEEN		0x0100
111 
112 /*
113  * Check if a filespec is last in a list (NULL on UNIX, invalid FSSpec on MacOS
114  */
115 
116 #define	PROFILE_LAST_FILESPEC(x) (((x) == NULL) || ((x)[0] == '\0'))
117 
118 /* profile_parse.c */
119 
120 errcode_t profile_parse_file
121 	(FILE *f, struct profile_node **root, char **ret_modspec);
122 
123 errcode_t profile_process_directory
124 	(const char *dirname, struct profile_node **root);
125 
126 errcode_t profile_write_tree_file
127 	(struct profile_node *root, FILE *dstfile);
128 
129 errcode_t profile_write_tree_to_buffer
130 	(struct profile_node *root, char **buf);
131 
132 
133 /* prof_tree.c */
134 
135 void profile_free_node
136 	(struct profile_node *relation);
137 
138 errcode_t profile_create_node
139 	(const char *name, const char *value,
140 		   struct profile_node **ret_node);
141 
142 errcode_t profile_verify_node
143 	(struct profile_node *node);
144 
145 errcode_t profile_add_node
146 	(struct profile_node *section,
147 		    const char *name, const char *value,
148 		    struct profile_node **ret_node);
149 
150 errcode_t profile_make_node_final
151 	(struct profile_node *node);
152 
153 int profile_is_node_final
154 	(struct profile_node *node);
155 
156 const char *profile_get_node_name
157 	(struct profile_node *node);
158 
159 const char *profile_get_node_value
160 	(struct profile_node *node);
161 
162 errcode_t profile_find_node
163 	(struct profile_node *section,
164 		    const char *name, const char *value,
165 		    int section_flag, void **state,
166 		    struct profile_node **node);
167 
168 errcode_t profile_find_node_relation
169 	(struct profile_node *section,
170 		    const char *name, void **state,
171 		    char **ret_name, char **value);
172 
173 errcode_t profile_find_node_subsection
174 	(struct profile_node *section,
175 		    const char *name, void **state,
176 		    char **ret_name, struct profile_node **subsection);
177 
178 errcode_t profile_get_node_parent
179 	(struct profile_node *section,
180 		   struct profile_node **parent);
181 
182 errcode_t profile_delete_node_relation
183 	(struct profile_node *section, const char *name);
184 
185 errcode_t profile_find_node_name
186 	(struct profile_node *section, void **state,
187 		    char **ret_name);
188 
189 errcode_t profile_node_iterator_create
190 	(profile_t profile, const char *const *names,
191 		   int flags, void **ret_iter);
192 
193 void profile_node_iterator_free
194 	(void	**iter_p);
195 
196 errcode_t profile_node_iterator
197 	(void	**iter_p, struct profile_node **ret_node,
198 		   char **ret_name, char **ret_value);
199 
200 errcode_t profile_remove_node
201 	(struct profile_node *node);
202 
203 errcode_t profile_set_relation_value
204 	(struct profile_node *node, const char *new_value);
205 
206 errcode_t profile_rename_node
207 	(struct profile_node *node, const char *new_name);
208 
209 /* prof_file.c */
210 
211 errcode_t KRB5_CALLCONV profile_copy (profile_t, profile_t *);
212 
213 errcode_t profile_open_file
214 	(const_profile_filespec_t file, prf_file_t *ret_prof,
215 	 char **ret_modspec);
216 
217 #define profile_update_file(P, M) profile_update_file_data((P)->data, M)
218 errcode_t profile_update_file_data
219 	(prf_data_t profile, char **ret_modspec);
220 #define profile_update_file_locked(P, M) profile_update_file_data_locked((P)->data, M)
221 errcode_t profile_update_file_data_locked
222 	(prf_data_t data, char **ret_modspec);
223 
224 #define profile_flush_file(P) (((P) && (P)->magic == PROF_MAGIC_FILE) ? profile_flush_file_data((P)->data) : PROF_MAGIC_FILE)
225 errcode_t profile_flush_file_data
226 	(prf_data_t data);
227 
228 #define profile_flush_file_to_file(P,F) (((P) && (P)->magic == PROF_MAGIC_FILE) ? profile_flush_file_data_to_file((P)->data, (F)) : PROF_MAGIC_FILE)
229 errcode_t profile_flush_file_data_to_file
230 	(prf_data_t data, const char *outfile);
231 
232 errcode_t profile_flush_file_data_to_buffer
233 	(prf_data_t data, char **bufp);
234 
235 void profile_free_file
236 	(prf_file_t profile);
237 
238 errcode_t profile_close_file
239 	(prf_file_t profile);
240 
241 int profile_file_is_writable
242 	(prf_file_t profile);
243 
244 void profile_dereference_data (prf_data_t);
245 void profile_dereference_data_locked (prf_data_t);
246 
247 void profile_lock_global (void);
248 void profile_unlock_global (void);
249 
250 /* prof_init.c -- included from profile.h */
251 errcode_t profile_ser_size
252         (const char *, profile_t, size_t *);
253 
254 errcode_t profile_ser_externalize
255         (const char *, profile_t, unsigned char **, size_t *);
256 
257 errcode_t profile_ser_internalize
258         (const char *, profile_t *, unsigned char **, size_t *);
259 
260 /* prof_get.c */
261 
262 errcode_t profile_get_value
263 	(profile_t profile, const char **names, char **ret_value);
264 /* Others included from profile.h */
265 
266 /* prof_set.c -- included from profile.h */
267