1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
|
/*
* prof_set.c --- routines that expose the public interfaces for
* inserting, updating and deleting items from the profile.
*
* WARNING: These routines only look at the first file opened in the
* profile. It's not clear how to handle multiple files, actually.
* In the future it may be necessary to modify this public interface,
* or possibly add higher level functions to support this correctly.
*
* WARNING: We're not yet doing locking yet, either.
*
*/
#include "prof_int.h"
#include <stdio.h>
#include <string.h>
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#include <errno.h>
static errcode_t rw_setup(profile_t profile)
{
prf_file_t file;
errcode_t retval = 0;
if (!profile)
return PROF_NO_PROFILE;
if (profile->magic != PROF_MAGIC_PROFILE)
return PROF_MAGIC_PROFILE;
file = profile->first_file;
if (!(file->data->flags & PROFILE_FILE_RW))
return PROF_READ_ONLY;
retval = profile_lock_global();
if (retval)
return retval;
/* Don't update the file if we've already made modifications */
if (file->data->flags & PROFILE_FILE_DIRTY) {
profile_unlock_global();
return 0;
}
if ((file->data->flags & PROFILE_FILE_SHARED) != 0) {
prf_data_t new_data;
new_data = profile_make_prf_data(file->data->filespec);
if (new_data == NULL) {
retval = ENOMEM;
} else {
retval = k5_mutex_init(&new_data->lock);
if (retval == 0) {
new_data->root = NULL;
new_data->flags = file->data->flags & ~PROFILE_FILE_SHARED;
new_data->timestamp = 0;
new_data->upd_serial = file->data->upd_serial;
}
}
if (retval != 0) {
profile_unlock_global();
free(new_data);
return retval;
}
profile_dereference_data_locked(file->data);
file->data = new_data;
}
profile_unlock_global();
retval = profile_update_file(file);
return retval;
}
/*
* Delete or update a particular child node
*
* ADL - 2/23/99, rewritten TYT 2/25/99
*/
errcode_t KRB5_CALLCONV
profile_update_relation(profile_t profile, const char **names,
const char *old_value, const char *new_value)
{
errcode_t retval;
struct profile_node *section, *node;
void *state;
const char **cpp;
retval = rw_setup(profile);
if (retval)
return retval;
if (names == 0 || names[0] == 0 || names[1] == 0)
return PROF_BAD_NAMESET;
if (!old_value || !*old_value)
return PROF_EINVAL;
retval = k5_mutex_lock(&profile->first_file->data->lock);
if (retval)
return retval;
section = profile->first_file->data->root;
for (cpp = names; cpp[1]; cpp++) {
state = 0;
retval = profile_find_node(section, *cpp, 0, 1,
&state, §ion);
if (retval) {
k5_mutex_unlock(&profile->first_file->data->lock);
return retval;
}
}
state = 0;
retval = profile_find_node(section, *cpp, old_value, 0, &state, &node);
if (retval == 0) {
if (new_value)
retval = profile_set_relation_value(node, new_value);
else
retval = profile_remove_node(node);
}
if (retval == 0)
profile->first_file->data->flags |= PROFILE_FILE_DIRTY;
k5_mutex_unlock(&profile->first_file->data->lock);
return retval;
}
/*
* Clear a particular all of the relations with a specific name.
*
* TYT - 2/25/99
*/
errcode_t KRB5_CALLCONV
profile_clear_relation(profile_t profile, const char **names)
{
errcode_t retval;
struct profile_node *section, *node;
void *state;
const char **cpp;
retval = rw_setup(profile);
if (retval)
return retval;
if (names == 0 || names[0] == 0 || names[1] == 0)
return PROF_BAD_NAMESET;
section = profile->first_file->data->root;
for (cpp = names; cpp[1]; cpp++) {
state = 0;
retval = profile_find_node(section, *cpp, 0, 1,
&state, §ion);
if (retval)
return retval;
}
state = 0;
do {
retval = profile_find_node(section, *cpp, 0, 0, &state, &node);
if (retval)
return retval;
retval = profile_remove_node(node);
if (retval)
return retval;
} while (state);
profile->first_file->data->flags |= PROFILE_FILE_DIRTY;
return 0;
}
/*
* Rename a particular section; if the new_section name is NULL,
* delete it.
*
* ADL - 2/23/99, rewritten TYT 2/25/99
*/
errcode_t KRB5_CALLCONV
profile_rename_section(profile_t profile, const char **names,
const char *new_name)
{
errcode_t retval;
struct profile_node *section, *node;
void *state;
const char **cpp;
retval = rw_setup(profile);
if (retval)
return retval;
if (names == 0 || names[0] == 0 || names[1] == 0)
return PROF_BAD_NAMESET;
retval = k5_mutex_lock(&profile->first_file->data->lock);
if (retval)
return retval;
section = profile->first_file->data->root;
for (cpp = names; cpp[1]; cpp++) {
state = 0;
retval = profile_find_node(section, *cpp, 0, 1,
&state, §ion);
if (retval) {
k5_mutex_unlock(&profile->first_file->data->lock);
return retval;
}
}
state = 0;
retval = profile_find_node(section, *cpp, 0, 1, &state, &node);
if (retval == 0) {
if (new_name)
retval = profile_rename_node(node, new_name);
else
retval = profile_remove_node(node);
}
if (retval == 0)
profile->first_file->data->flags |= PROFILE_FILE_DIRTY;
k5_mutex_unlock(&profile->first_file->data->lock);
return retval;
}
/*
* Insert a new relation. If the new_value argument is NULL, then
* create a new section instead.
*
* Note: if the intermediate sections do not exist, this function will
* automatically create them.
*
* ADL - 2/23/99, rewritten TYT 2/25/99
*/
errcode_t KRB5_CALLCONV
profile_add_relation(profile_t profile, const char **names,
const char *new_value)
{
errcode_t retval;
struct profile_node *section;
const char **cpp;
void *state;
retval = rw_setup(profile);
if (retval)
return retval;
if (names == 0 || names[0] == 0 || names[1] == 0)
return PROF_BAD_NAMESET;
retval = k5_mutex_lock(&profile->first_file->data->lock);
if (retval)
return retval;
section = profile->first_file->data->root;
for (cpp = names; cpp[1]; cpp++) {
state = 0;
retval = profile_find_node(section, *cpp, 0, 1,
&state, §ion);
if (retval == PROF_NO_SECTION)
retval = profile_add_node(section, *cpp, 0, §ion);
if (retval) {
k5_mutex_unlock(&profile->first_file->data->lock);
return retval;
}
}
if (new_value == 0) {
retval = profile_find_node(section, *cpp, 0, 1, &state, 0);
if (retval == 0) {
k5_mutex_unlock(&profile->first_file->data->lock);
return PROF_EXISTS;
} else if (retval != PROF_NO_SECTION) {
k5_mutex_unlock(&profile->first_file->data->lock);
return retval;
}
}
retval = profile_add_node(section, *cpp, new_value, 0);
if (retval) {
k5_mutex_unlock(&profile->first_file->data->lock);
return retval;
}
profile->first_file->data->flags |= PROFILE_FILE_DIRTY;
k5_mutex_unlock(&profile->first_file->data->lock);
return 0;
}
|