summaryrefslogtreecommitdiffstats
path: root/src/responder/sudo/sudosrv_query.c
blob: fcf01333ec8599bad7569cc389cecbdf30b9ba6d (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
/*
    Authors:
        Pavel Březina <pbrezina@redhat.com>

    Copyright (C) 2011 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 <string.h>
#include <errno.h>
#include <talloc.h>

#include "util/util.h"
#include "responder/sudo/sudosrv.h"
#include "responder/sudo/sudosrv_private.h"

/*
 * Reads null-terminated string.
 */
int sudosrv_query_parse_string(TALLOC_CTX *mem_ctx,
                               char *query,
                               int query_length,
                               int start_pos,
                               int *_end_pos,
                               char **_string)
{
    TALLOC_CTX *tmp_ctx = NULL;
    char *current_pos = query + start_pos;
    char *string = NULL;
    int ret = EOK;

    if (start_pos > query_length) {
        DEBUG(SSSDBG_CRIT_FAILURE, ("Query is too short\n"));
        return EINVAL;
    }

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, ("talloc_new() failed\n"));
        ret = ENOMEM;
        goto done;
    }

    if (*current_pos != '\0') {
        string = talloc_strdup(tmp_ctx, current_pos);
        if (string == NULL) {
            DEBUG(SSSDBG_CRIT_FAILURE, ("talloc_strdup() failed\n"));
            return ENOMEM;
        }

        current_pos = strchr(current_pos, '\0');
        if (current_pos == NULL) {
            ret = ESPIPE;
            goto done;
        }
    }

    /* go one char past \0 */
    current_pos++;

    *_end_pos = current_pos - query;
    *_string = talloc_steal(mem_ctx, string);

    ret = EOK;

done:
    talloc_free(tmp_ctx);
    return ret;
}

/*
 * Reads array in format:
 * value1\0value2\0...\0\0
 */
int sudosrv_query_parse_array(TALLOC_CTX *mem_ctx,
                              char *query,
                              int query_length,
                              int start_pos,
                              int *_end_pos,
                              int *_count,
                              char ***_array)
{
    TALLOC_CTX *tmp_ctx = NULL;
    char *current_pos = query + start_pos;
    char **array = NULL;
    int count = 0;
    int ret = EOK;

    if (start_pos > query_length) {
        DEBUG(SSSDBG_CRIT_FAILURE, ("Query is too short\n"));
        return EINVAL;
    }

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, ("talloc_new() failed\n"));
        ret = ENOMEM;
        goto done;
    }

    if (*current_pos != '\0') {
        /* array with at least one element */
        do {
            count++;

            array = talloc_realloc(tmp_ctx, array, char*, count);
            if (array == NULL) {
                DEBUG(SSSDBG_CRIT_FAILURE, ("talloc_realloc() failed\n"));
                ret = ENOMEM;
                goto done;
            }

            array[count - 1] = talloc_strdup(array, current_pos);
            if (array[count - 1] == NULL) {
                DEBUG(SSSDBG_CRIT_FAILURE, ("talloc_strdup() failed\n"));
                return ENOMEM;
            }

            /* find end of this element */
            current_pos = strchr(current_pos, '\0');
            if (current_pos == NULL) {
                ret = ESPIPE;
                goto done;
            }

            /* go to the start of the next element (one char past \0) */
            current_pos++;
        } while (*current_pos != '\0');
    } else {
        /* special case for empty arrays - go one char past \0 */
        current_pos++;
    }

    /* go to the next array */
    current_pos++;

    if (_count != NULL) {
        *_count = count;
    }
    *_array = talloc_steal(mem_ctx, array);
    *_end_pos = current_pos - query;

    ret = EOK;

done:
    talloc_free(tmp_ctx);
    return ret;
}

/*
 * Appends null-terminated string array.
 */
int sudosrv_response_append_array(TALLOC_CTX *mem_ctx,
                                  char **array,
                                  uint8_t **_response,
                                  size_t *_response_length)
{
    size_t response_length = *_response_length;
    uint8_t *response = *_response;
    TALLOC_CTX *tmp_ctx = NULL;
    char **iter = NULL;
    int ret = EOK;

    tmp_ctx = talloc_new(NULL);
    if (tmp_ctx == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, ("talloc_new() failed"));
        ret = ENOMEM;
        goto done;
    }

    if (array == NULL || *array == NULL) {
        /* empty array - append two \0 */
        ret = sudosrv_response_append_string(tmp_ctx, "\0\0", 2,
                                             &response, &response_length);
        if (ret != EOK) {
            goto done;
        }
    } else {
        for (iter = array; *iter != NULL; iter++) {
            /* append element with ending \0 */
            ret = sudosrv_response_append_string(tmp_ctx, *iter,
                                                 strlen(*iter) + 1,
                                                 &response, &response_length);
            if (ret != EOK) {
                goto done;
            }
        }

        /* append \0 to mark end of this array */
        ret = sudosrv_response_append_string(tmp_ctx, "\0", 1,
                                             &response, &response_length);
        if (ret != EOK) {
            goto done;
        }
    }

    *_response_length = response_length;
    *_response = talloc_steal(mem_ctx, response);

    ret = EOK;

done:
    talloc_free(tmp_ctx);
    return ret;
}

int sudosrv_response_append_string(TALLOC_CTX *mem_ctx,
                                   const char *str,
                                   size_t str_length,
                                   uint8_t **_response,
                                   size_t *_response_length)
{
    size_t response_length = *_response_length;
    uint8_t *response = *_response;

    response = talloc_realloc(mem_ctx, response, uint8_t,
                              response_length + (str_length * sizeof(char)));
    if (response == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, ("talloc_realloc() failed\n"));
        return ENOMEM;
    }
    memcpy(response + response_length, str, str_length);
    response_length += str_length;

    *_response = response;
    *_response_length = response_length;

    return EOK;
}

int sudosrv_response_append_int(TALLOC_CTX *mem_ctx,
                                int number,
                                uint8_t **_response,
                                size_t *_response_length)
{
    size_t response_length = *_response_length;
    uint8_t *response = *_response;

    response = talloc_realloc(mem_ctx, response, uint8_t,
                              response_length + sizeof(int));
    if (response == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, ("talloc_realloc() failed\n"));
        return ENOMEM;
    }
    SAFEALIGN_SET_VALUE(response + response_length,
                        number, int, &response_length);

    *_response = response;
    *_response_length = response_length;

    return EOK;
}