summaryrefslogtreecommitdiffstats
path: root/src/util/profile/prof_init.c
blob: c0d10f689fd572ad250ddbf962d2a21ec03907b7 (plain)
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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/*
 * prof_init.c --- routines that manipulate the user-visible profile_t
 * 	object.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>

#include "prof_int.h"

/* Find a 4-byte integer type */
#if	(SIZEOF_SHORT == 4)
typedef short	prof_int32;
#elif	(SIZEOF_INT == 4)
typedef int	prof_int32;
#elif	(SIZEOF_LONG == 4)
typedef	int	prof_int32;
#else	/* SIZEOF_LONG == 4 */
error(do not have a 4-byte integer type)
#endif	/* SIZEOF_LONG == 4 */

errcode_t profile_init(filenames, ret_profile)
	const char **filenames;
	profile_t *ret_profile;
{
	const char **fn;
	profile_t profile;
	prf_file_t  new_file, last = 0;
	errcode_t retval;

	initialize_prof_error_table();
	
	profile = malloc(sizeof(struct _profile_t));
	if (!profile)
		return ENOMEM;
	memset(profile, 0, sizeof(struct _profile_t));
	profile->magic = PROF_MAGIC_PROFILE;

	for (fn = filenames; *fn; fn++) {
		retval = profile_open_file(*fn, &new_file);
		if (retval) {
			profile_release(profile);
			return retval;
		}
		if (last)
			last->next = new_file;
		else
			profile->first_file = new_file;
		last = new_file;
	}
	*ret_profile = profile;
	return 0;
}

void profile_release(profile)
	profile_t	profile;
{
	prf_file_t	p, next;

	for (p = profile->first_file; p; p = next) {
		next = p->next;
		profile_close_file(p);
	}
	profile->magic = 0;
	free(profile);
}

struct string_list {
	char	**list;
	int	num;
	int	max;
};

static errcode_t init_list(list)
	struct string_list *list;
{
	list->num = 0;
	list->max = 10;
	list->list = malloc(list->max * sizeof(char *));
	if (list->list == 0)
		return ENOMEM;
	list->list[0] = 0;
	return 0;
}

static void free_list(list)
    struct string_list *list;
{
    char	**cp;
    
    for (cp = list->list; *cp; cp++)
	free(*cp);
    free(list->list);
    list->num = list->max = 0;
    list->list = 0;
}

static errcode_t add_to_list(list, str)
	struct string_list *list;
	const char	*str;
{
	char *newstr;
	
	if (list->num+1 >= list->max) {
		list->max += 5;
		list->list = realloc(list->list, list->max * sizeof(char *));
		if (list->list == 0)
			return ENOMEM;
	}
	newstr = malloc(strlen(str)+1);
	if (newstr == 0)
		return ENOMEM;
	strcpy(newstr, str);

	list->list[list->num++] = newstr;
	list->list[list->num] = 0;
	return 0;
}
	
/*
 * XXX this version only works to get values from the first file.
 * To do more than that means we have to implement some "interesting"
 * code to do the section searching.
 */
errcode_t profile_get_values(profile, names, ret_values)
    profile_t	profile;
    const char	**names;
    char		***ret_values;
{
    prf_file_t	file;
    errcode_t	retval;
    struct profile_node *section;
    void		*state;
    char		*value;
    struct string_list values;
    const char		**cpp;

    if (profile == 0)
	return PROF_NO_PROFILE;

    if (names == 0 || names[0] == 0 || names[1] == 0)
	return PROF_BAD_NAMESET;

    init_list(&values);

    file = profile->first_file;
    retval = profile_update_file(file);
    if (retval)
	goto cleanup;
    
    section = file->root;

    for (cpp = names; cpp[1]; cpp++) {
	state = 0;
	retval = profile_find_node_subsection(section, *cpp,
					      &state, 0, &section);
	if (retval)
	    goto cleanup;
    }

    state = 0;
    do {
	retval = profile_find_node_relation(section, *cpp, &state, 0, &value);
	if (retval)
	    goto cleanup;
	add_to_list(&values, value);
    } while (state);

    *ret_values = values.list;
    return 0;
cleanup:
    free_list(&values);
    return retval;
}	

/*
 * XXX this version only works to get values from the first file.
 * To do more than that means we have to implement some "interesting"
 * code to do the section searching.
 */
errcode_t profile_get_first_values(profile, names, ret_values)
    profile_t	profile;
    const char	**names;
    char		***ret_values;
{
    prf_file_t	file;
    errcode_t	retval;
    struct profile_node *section;
    void		*state;
    char		*value;
    struct string_list values;
    const char		**cpp;
    char			*dummyvalue;
    char			*secname;
    const char			*mynames[3];
    

    if (profile == 0)
	return PROF_NO_PROFILE;

    if (names == 0 || names[0] == 0)
	return PROF_BAD_NAMESET;

    init_list(&values);

    file = profile->first_file;
    retval = profile_update_file(file);
    if (retval)
	goto cleanup;
    
    section = file->root;

    state = 0;
	retval = profile_find_node_subsection(section, *names, &state, &secname, &section);

    do {
	retval = profile_find_node_name(section, &state, &value);
	if (retval)
	    goto cleanup;
	add_to_list(&values, value);
    } while (state);

    *ret_values = values.list;
    return 0;
cleanup:
    free_list(&values);
    return retval;
}	

/*
 * XXX this version only works to get values from the first file.
 */
static errcode_t profile_get_value(profile, names, ret_value)
    profile_t	profile;
    const char	**names;
    char	**ret_value;
{
    prf_file_t	file;
    errcode_t	retval;
    struct profile_node *section;
    void		*state;
    char		*value;
    const char		**cpp;

    if (names == 0 || names[0] == 0 || names[1] == 0)
	return PROF_BAD_NAMESET;

    file = profile->first_file;
    retval = profile_update_file(file);
    if (retval)
	goto cleanup;
    
    section = file->root;

    for (cpp = names; cpp[1]; cpp++) {
	state = 0;
	retval = profile_find_node_subsection(section, *cpp,
					      &state, 0, &section);
	if (retval)
	    goto cleanup;
    }

    state = 0;
    retval = profile_find_node_relation(section, *cpp, &state, 0, &value);
    if (retval)
	goto cleanup;

    *ret_value = value;
    return 0;
cleanup:
    return retval;
}

errcode_t profile_get_string(profile, name, subname, subsubname,
			     def_val, ret_string)
    profile_t	profile;
    const char	*name, *subname, *subsubname;
    const char	*def_val;
    char 	**ret_string;
{
    const char	*value;
    errcode_t	retval;
    const char	*names[4];

    if (profile) {
	names[0] = name;
	names[1] = subname;
	names[2] = subsubname;
	names[3] = 0;
	retval = profile_get_value(profile, names, &value);
	if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION)
	    value = def_val;
	else if (retval)
	    return retval;
    } else
	value = def_val;
    
    if (value) {
	*ret_string = malloc(strlen(value)+1);
	if (*ret_string == 0)
	    return ENOMEM;
	strcpy(*ret_string, value);
    } else
	*ret_string = 0;
    return 0;
}

errcode_t profile_get_integer(profile, name, subname, subsubname,
			      def_val, ret_int)
    profile_t	profile;
    const char	*name, *subname, *subsubname;
    int		def_val;
    int		*ret_int;
{
   char	*value;
   errcode_t	retval;
    const char	*names[4];

   if (profile == 0) {
       *ret_int = def_val;
       return 0;
   }

   names[0] = name;
   names[1] = subname;
   names[2] = subsubname;
   names[3] = 0;
   retval = profile_get_value(profile, names, &value);
   if (retval == PROF_NO_SECTION || retval == PROF_NO_RELATION) {
       *ret_int = def_val;
       return 0;
   } else if (retval)
       return retval;
   
   *ret_int = atoi(value);
   return 0;
}

errcode_t profile_ser_size(unused, profile, sizep)
    const char *unused;
    profile_t	profile;
    size_t	*sizep;
{
    size_t	required;
    prf_file_t	pfp;

    /*
     * ARGH - We want to avoid having to include k5-int.h.  We ASSuME that
     * krb5_int32 is 4 bytes in length.
     *
     * krb5_int32 for header
     * krb5_int32 for number of files.
     * krb5_int32 for trailer
     */
    required = 3*sizeof(prof_int32);
    for (pfp = profile->first_file; pfp; pfp = pfp->next) {
	required += sizeof(prof_int32);
	if (pfp->filename)
	    required += strlen(pfp->filename);
    }
    *sizep += required;
    return 0;
}

static void pack_int32(oval, bufpp, remainp)
    prof_int32		oval;
    unsigned char	**bufpp;
    size_t		*remainp;
{
    (*bufpp)[0] = (unsigned char) ((oval >> 24) & 0xff);
    (*bufpp)[1] = (unsigned char) ((oval >> 16) & 0xff);
    (*bufpp)[2] = (unsigned char) ((oval >> 8) & 0xff);
    (*bufpp)[3] = (unsigned char) (oval & 0xff);
    *bufpp += sizeof(prof_int32);
    *remainp -= sizeof(prof_int32);
}

errcode_t profile_ser_externalize(unused, profile, bufpp, remainp)
    const char		*unused;
    profile_t		profile;
    unsigned char	**bufpp;
    size_t		*remainp;
{
    errcode_t		retval;
    size_t		required;
    unsigned char	*bp;
    size_t		remain;
    prf_file_t		pfp;
    prof_int32		fcount, slen;

    required = 0;
    bp = *bufpp;
    remain = *remainp;
    retval = EINVAL;
    if (profile) {
	retval = ENOMEM;
	(void) profile_ser_size(unused, profile, &required);
	if (required <= remain) {
	    fcount = 0;
	    for (pfp = profile->first_file; pfp; pfp = pfp->next)
		fcount++;
	    pack_int32(PROF_MAGIC_PROFILE, &bp, &remain);
	    pack_int32(fcount, &bp, &remain);
	    for (pfp = profile->first_file; pfp; pfp = pfp->next) {
		slen = (pfp->filename) ?
		    (prof_int32) strlen(pfp->filename) : 0;
		pack_int32(slen, &bp, &remain);
		if (slen) {
		    memcpy(bp, pfp->filename, (size_t) slen);
		    bp += slen;
		    remain -= (size_t) slen;
		}
	    }
	    pack_int32(PROF_MAGIC_PROFILE, &bp, &remain);
	    retval = 0;
	    *bufpp = bp;
	    *remainp = remain;
	}
    }
    return(retval);
}

static int unpack_int32(intp, bufpp, remainp)
    prof_int32		*intp;
    unsigned char	**bufpp;
    size_t		*remainp;
{
    if (*remainp >= sizeof(prof_int32)) {
	*intp = (((prof_int32) (*bufpp)[0] << 24) |
		 ((prof_int32) (*bufpp)[1] << 16) |
		 ((prof_int32) (*bufpp)[2] << 8) |
		 ((prof_int32) (*bufpp)[3]));
	*bufpp += sizeof(prof_int32);
	*remainp -= sizeof(prof_int32);
	return 0;
    }
    else
	return 1;
}

errcode_t profile_ser_internalize(unused, profilep, bufpp, remainp)
    const char		*unused;
    profile_t		*profilep;
    unsigned char	**bufpp;
    size_t		*remainp;
{
    errcode_t		retval;
    unsigned char	*bp;
    size_t		remain;
    int			i;
    prof_int32		fcount, tmp;
    char		**flist;

    bp = *bufpp;
    remain = *remainp;
    retval = EINVAL;

    if (remain >= 12)
	(void) unpack_int32(&tmp, &bp, &remain);
    else
	tmp = 0;
    if (tmp == PROF_MAGIC_PROFILE) {
	(void) unpack_int32(&fcount, &bp, &remain);
	retval = ENOMEM;
	if (!fcount ||
	    (flist = (char **) malloc(sizeof(char *) * (fcount + 1)))) {
	    memset(flist, 0, sizeof(char *) * (fcount+1));
	    for (i=0; i<fcount; i++) {
		if (!unpack_int32(&tmp, &bp, &remain)) {
		    if ((flist[i] = (char *) malloc((size_t) (tmp+1)))) {
			memcpy(flist[i], bp, (size_t) tmp);
			flist[i][tmp] = '\0';
			bp += tmp;
			remain -= (size_t) tmp;
		    }
		    else
			break;
		}
		else
		    break;
	    }
	    if ((i == fcount) &&
		!unpack_int32(&tmp, &bp, &remain) &&
		(tmp == PROF_MAGIC_PROFILE))
		retval = profile_init((const char **)flist, profilep);

	    if (!retval) {
		*bufpp = bp;
		*remainp = remain;
	    }

	    for (i=0; i<fcount; i++) {
		if (flist[i])
		    free(flist[i]);
	    }
	    free(flist);
	}
    }
    return(retval);
}