1 #pragma ident "%Z%%M% %I% %E% SMI"
3 * prof_set.c --- routines that expose the public interfaces for
4 * inserting, updating and deleting items from the profile.
6 * WARNING: These routines only look at the first file opened in the
7 * profile. It's not clear how to handle multiple files, actually.
8 * In the future it may be necessary to modify this public interface,
9 * or possibly add higher level functions to support this correctly.
11 * WARNING: We're not yet doing locking yet, either.
24 static errcode_t
rw_setup(profile_t profile
)
30 return PROF_NO_PROFILE
;
32 if (profile
->magic
!= PROF_MAGIC_PROFILE
)
33 return PROF_MAGIC_PROFILE
;
35 file
= profile
->first_file
;
37 retval
= profile_lock_global();
41 /* Don't update the file if we've already made modifications */
42 if (file
->data
->flags
& PROFILE_FILE_DIRTY
) {
43 profile_unlock_global();
47 if ((file
->data
->flags
& PROFILE_FILE_SHARED
) != 0) {
49 new_data
= profile_make_prf_data(file
->data
->filespec
);
50 if (new_data
== NULL
) {
53 retval
= k5_mutex_init(&new_data
->lock
);
55 new_data
->root
= NULL
;
56 new_data
->flags
= file
->data
->flags
& ~PROFILE_FILE_SHARED
;
57 new_data
->timestamp
= 0;
58 new_data
->upd_serial
= file
->data
->upd_serial
;
63 profile_unlock_global();
67 profile_dereference_data_locked(file
->data
);
68 file
->data
= new_data
;
71 profile_unlock_global();
72 retval
= profile_update_file(file
);
79 * Delete or update a particular child node
81 * ADL - 2/23/99, rewritten TYT 2/25/99
83 errcode_t KRB5_CALLCONV
84 profile_update_relation(profile_t profile
, const char **names
,
85 const char *old_value
, const char *new_value
)
88 struct profile_node
*section
, *node
;
92 retval
= rw_setup(profile
);
96 if (names
== 0 || names
[0] == 0 || names
[1] == 0)
97 return PROF_BAD_NAMESET
;
99 if (!old_value
|| !*old_value
)
102 retval
= k5_mutex_lock(&profile
->first_file
->data
->lock
);
105 section
= profile
->first_file
->data
->root
;
106 for (cpp
= names
; cpp
[1]; cpp
++) {
108 retval
= profile_find_node(section
, *cpp
, 0, 1,
111 k5_mutex_unlock(&profile
->first_file
->data
->lock
);
117 retval
= profile_find_node(section
, *cpp
, old_value
, 0, &state
, &node
);
120 retval
= profile_set_relation_value(node
, new_value
);
122 retval
= profile_remove_node(node
);
125 profile
->first_file
->data
->flags
|= PROFILE_FILE_DIRTY
;
126 k5_mutex_unlock(&profile
->first_file
->data
->lock
);
132 * Clear a particular all of the relations with a specific name.
136 errcode_t KRB5_CALLCONV
137 profile_clear_relation(profile_t profile
, const char **names
)
140 struct profile_node
*section
, *node
;
144 retval
= rw_setup(profile
);
148 if (names
== 0 || names
[0] == 0 || names
[1] == 0)
149 return PROF_BAD_NAMESET
;
151 section
= profile
->first_file
->data
->root
;
152 for (cpp
= names
; cpp
[1]; cpp
++) {
154 retval
= profile_find_node(section
, *cpp
, 0, 1,
162 retval
= profile_find_node(section
, *cpp
, 0, 0, &state
, &node
);
165 retval
= profile_remove_node(node
);
170 profile
->first_file
->data
->flags
|= PROFILE_FILE_DIRTY
;
176 * Rename a particular section; if the new_section name is NULL,
179 * ADL - 2/23/99, rewritten TYT 2/25/99
181 errcode_t KRB5_CALLCONV
182 profile_rename_section(profile_t profile
, const char **names
,
183 const char *new_name
)
186 struct profile_node
*section
, *node
;
190 retval
= rw_setup(profile
);
194 if (names
== 0 || names
[0] == 0 || names
[1] == 0)
195 return PROF_BAD_NAMESET
;
197 retval
= k5_mutex_lock(&profile
->first_file
->data
->lock
);
200 section
= profile
->first_file
->data
->root
;
201 for (cpp
= names
; cpp
[1]; cpp
++) {
203 retval
= profile_find_node(section
, *cpp
, 0, 1,
206 k5_mutex_unlock(&profile
->first_file
->data
->lock
);
212 retval
= profile_find_node(section
, *cpp
, 0, 1, &state
, &node
);
215 retval
= profile_rename_node(node
, new_name
);
217 retval
= profile_remove_node(node
);
220 profile
->first_file
->data
->flags
|= PROFILE_FILE_DIRTY
;
221 k5_mutex_unlock(&profile
->first_file
->data
->lock
);
226 * Insert a new relation. If the new_value argument is NULL, then
227 * create a new section instead.
229 * Note: if the intermediate sections do not exist, this function will
230 * automatically create them.
232 * ADL - 2/23/99, rewritten TYT 2/25/99
234 errcode_t KRB5_CALLCONV
235 profile_add_relation(profile_t profile
, const char **names
,
236 const char *new_value
)
239 struct profile_node
*section
;
243 retval
= rw_setup(profile
);
247 if (names
== 0 || names
[0] == 0 || names
[1] == 0)
248 return PROF_BAD_NAMESET
;
250 retval
= k5_mutex_lock(&profile
->first_file
->data
->lock
);
253 section
= profile
->first_file
->data
->root
;
254 for (cpp
= names
; cpp
[1]; cpp
++) {
256 retval
= profile_find_node(section
, *cpp
, 0, 1,
258 if (retval
== PROF_NO_SECTION
)
259 retval
= profile_add_node(section
, *cpp
, 0, §ion
);
261 k5_mutex_unlock(&profile
->first_file
->data
->lock
);
266 if (new_value
== 0) {
267 retval
= profile_find_node(section
, *cpp
, 0, 1, &state
, 0);
269 k5_mutex_unlock(&profile
->first_file
->data
->lock
);
271 } else if (retval
!= PROF_NO_SECTION
) {
272 k5_mutex_unlock(&profile
->first_file
->data
->lock
);
277 retval
= profile_add_node(section
, *cpp
, new_value
, 0);
279 k5_mutex_unlock(&profile
->first_file
->data
->lock
);
283 profile
->first_file
->data
->flags
|= PROFILE_FILE_DIRTY
;
284 k5_mutex_unlock(&profile
->first_file
->data
->lock
);