aboutsummaryrefslogtreecommitdiff
path: root/src/lib/krb5/krb/bld_princ.c
blob: f77d9135e7a2ed65beec2b76d09ee4bb5e93c1a3 (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
/*
 * $Source$
 * $Author$
 *
 * Copyright 1991 by the Massachusetts Institute of Technology.
 * All Rights Reserved.
 *
 * For copying and distribution information, please see the file
 * <krb5/copyright.h>.
 *
 * Build a principal from a list of strings
 */

#if !defined(lint) && !defined(SABER)
static char rcsid_bld_princ_c [] =
"$Id$";
#endif	/* !lint & !SABER */

/* Need <krb5/config.h> for STDARG_PROTOTYPES */
#include <krb5/krb5.h>

#if __STDC__ || defined(STDARG_PROTOTYPES)
#include <stdarg.h>
#else
#include <varargs.h>
#endif

#include <krb5/ext-proto.h>

krb5_error_code
krb5_build_principal_va(princ, rlen, realm, ap)
krb5_principal *princ;
int rlen;
const char *realm;
va_list ap;
{
    register int i, count = 0;
    register char *next;
    krb5_principal princ_ret;

    /* guess at an initial sufficent count of 2 pieces */
    count = 2 + 2;		/* plus 2 for realm & null terminator */

    /* get space for array and realm, and insert realm */
    princ_ret = (krb5_principal) malloc(sizeof(*princ_ret) * (count));
    if (!princ_ret)
	return ENOMEM;
    if (!(princ_ret[0] = (krb5_data *) malloc(sizeof(*princ_ret[0])))) {
	xfree(princ_ret);
	return ENOMEM;
    }
    princ_ret[0]->length = rlen;
    princ_ret[0]->data = malloc(rlen);
    if (!princ_ret[0]->data) {
	xfree(princ_ret[0]);
	xfree(princ_ret);
	return ENOMEM;
    }
    memcpy(princ_ret[0]->data, realm, rlen);

    /* process rest of components */

    for (i = 1, next = va_arg(ap, char *);
	 next;
	 next = va_arg(ap, char *), i++) {
	if (i == count-1) {
	    /* not big enough.  realloc the array */
	    krb5_principal p_tmp;
	    p_tmp = (krb5_principal) realloc((char *)princ_ret, sizeof(*princ_ret)*(count*2));
	    if (!p_tmp)
		goto free_out;
	    princ_ret = p_tmp;
	    count *= 2;
	}
	if (!(princ_ret[i] =
	      (krb5_data *) malloc(sizeof(*princ_ret[i])))) {
	free_out:
	    for (i--; i >= 0; i--) {
		xfree(princ_ret[i]->data);
		xfree(princ_ret[i]);
	    }
	    xfree(princ_ret);
	    return (ENOMEM);
	}
	princ_ret[i]->length = strlen(next);
	princ_ret[i]->data = strdup(next);
	if (!princ_ret[i]->data) {
	    xfree(princ_ret[i]);
	    goto free_out;
	}
    }
    princ_ret[i] = 0;			/* put a null as the last entry */
    *princ = princ_ret;
    return 0;
}

krb5_error_code
#if __STDC__ || defined(STDARG_PROTOTYPES)
krb5_build_principal(krb5_principal *princ, int rlen, const char *realm, ...)
#else
krb5_build_principal(princ, rlen, realm, va_alist)
krb5_principal *princ;
int rlen;
const char *realm;
va_dcl
#endif
{
    va_list ap;
    krb5_error_code retval;

#if __STDC__ || defined(STDARG_PROTOTYPES)
    va_start(ap, realm);
#else
    va_start(ap);
#endif
    retval = krb5_build_principal_va(princ, rlen, realm, ap);
    va_end(ap);
    return retval;
}