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