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