summaryrefslogtreecommitdiffstats
path: root/src/krb5_plugin/common/utils.c
blob: 2a0ecc03a396cedcb51517e05c5dac868180c097 (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
/*
    Authors:
        Pavel Březina <pbrezina@redhat.com>

    Copyright (C) 2023 Red Hat

    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "config.h"

#include <errno.h>
#include <stdlib.h>
#include <string.h>
#include <jansson.h>
#include <krb5/preauth_plugin.h>

#include "krb5_plugin/common/utils.h"

void
sss_string_array_free(char **array)
{
    size_t i;

    if (array == NULL) {
        return;
    }

    for (i = 0; array[i] != NULL; i++) {
        free(array[i]);
    }

    free(array);
}

char **
sss_string_array_copy(char **array)
{
    char **copy;
    size_t i;

    for (i = 0; array[i] != NULL; i++) {
        /* Just count. */
    }

    copy = calloc(i + 1, sizeof(char *));
    if (copy == NULL) {
        return NULL;
    }

    for (i = 0; array[i] != NULL; i++) {
        copy[i] = strdup(array[i]);
        if (copy[i] == NULL) {
            sss_string_array_free(copy);
            return NULL;
        }
    }

    copy[i] = NULL;

    return copy;
}

char **
sss_json_array_to_strings(json_t *jarray)
{
    const char *strval;
    char **array;
    json_t *jval;
    size_t i;

    if (!json_is_array(jarray)) {
        return NULL;
    }

    array = calloc(json_array_size(jarray) + 1, sizeof(char *));
    if (array == NULL) {
        return NULL;
    }

    json_array_foreach(jarray, i, jval) {
        strval = json_string_value(jval);
        if (strval == NULL) {
            goto fail;
        }

        array[i] = strdup(strval);
        if (array[i] == NULL) {
            goto fail;
        }
    }

    return array;

fail:
    sss_string_array_free(array);

    return NULL;
}

json_t *
sss_strings_to_json_array(char **array)
{
    json_t *jarray;
    json_t *jstr;
    size_t i;
    int jret;

    jarray = json_array();
    if (jarray == NULL) {
        return NULL;
    }

    if (array == NULL) {
        return jarray;
    }

    for (i = 0; array[i] != NULL; i++) {
        jstr = json_string(array[i]);
        if (jstr == NULL) {
            goto fail;
        }

        jret = json_array_append_new(jarray, jstr);
        if (jret != 0) {
            goto fail;
        }
    }

    return jarray;

fail:
    json_decref(jarray);

    return NULL;
}

void *
sss_radius_message_decode(const char *prefix,
                          sss_radius_message_decode_fn fn,
                          const char *str)
{
    size_t prefix_len;

    if (str == NULL) {
        return NULL;
    }

    prefix_len = strlen(prefix);
    if (strncmp(str, prefix, prefix_len) != 0) {
        return NULL;
    }

    return fn(str + prefix_len);
}

char *
sss_radius_message_encode(const char *prefix,
                          sss_radius_message_encode_fn fn,
                          const void *data)
{
    char *json_str;
    char *str;
    int aret;

    json_str = fn(data);
    if (json_str == NULL) {
        return NULL;
    }

    aret = asprintf(&str, "%s%s", prefix, json_str);
    free(json_str);
    if (aret < 0) {
        return NULL;
    }

    return str;
}

krb5_pa_data *
sss_radius_encode_padata(krb5_preauthtype patype,
                         sss_radius_message_encode_fn fn,
                         const void *data)
{
    krb5_pa_data *padata;
    char *str;

    str = fn(data);
    if (str == NULL) {
        return NULL;
    }

    padata = malloc(sizeof(krb5_pa_data));
    if (padata == NULL) {
        free(str);
        return NULL;
    }

    padata->pa_type = patype;
    padata->contents = (krb5_octet*)str;
    padata->length = strlen(str) + 1;

    return padata;
}

void *
sss_radius_decode_padata(sss_radius_message_decode_fn fn,
                         krb5_pa_data *padata)
{
    if (padata->length == 0 || padata->contents == NULL) {
        return NULL;
    }

    /* contents is NULL terminated string */
    if (padata->contents[padata->length - 1] != '\0') {
        return NULL;
    }

    return fn((const char*)padata->contents);
}

krb5_pa_data **
sss_radius_encode_padata_array(krb5_preauthtype patype,
                               sss_radius_message_encode_fn fn,
                               const void *data)
{
    krb5_pa_data **array;

    array = calloc(2, sizeof(krb5_pa_data *));
    if (array == NULL) {
        return NULL;
    }

    array[0] = sss_radius_encode_padata(patype, fn, data);
    array[1] = NULL;

    if (array[0] == NULL) {
        free(array);
        return NULL;
    }

    return array;
}