summaryrefslogtreecommitdiff
path: root/src/providers/ldap/sdap_range.c
blob: c4bf435393cc6d26b37b1d2719656d5daca71189 (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
/*
    SSSD

    Authors:
        Stephen Gallagher <sgallagh@redhat.com>

    Copyright (C) 2012 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 "providers/ldap/sdap_range.h"
#include "util/util.h"
#include "util/strtonum.h"

#define SDAP_RANGE_STRING "range="

errno_t sdap_parse_range(TALLOC_CTX *mem_ctx,
                         const char *attr_desc,
                         char **base_attr,
                         uint32_t *range_offset,
                         bool disable_range_retrieval)
{
    errno_t ret;
    TALLOC_CTX *tmp_ctx;
    char *endptr;
    char *end_range;
    char *base;
    size_t rangestringlen = sizeof(SDAP_RANGE_STRING) - 1;

    *range_offset = 0;

    tmp_ctx = talloc_new(NULL);
    if (!tmp_ctx) return ENOMEM;

    /* The base_attr is the portion before the semicolon (if it exists) */
    endptr = strchr(attr_desc, ';');
    if (endptr == NULL) {
        /* Not a ranged attribute. Just copy the attribute desc */
        *base_attr = talloc_strdup(mem_ctx, attr_desc);
        if (!*base_attr) {
            ret = ENOMEM;
        } else {
            ret = EOK;
        }
        DEBUG(SSSDBG_TRACE_INTERNAL,
              ("No sub-attributes for [%s]\n", attr_desc));
        goto done;
    }

    /* This is a complex attribute. First get the base attribute name */
    base = talloc_strndup(tmp_ctx, attr_desc,
                          endptr - attr_desc);
    if (!base) {
        ret = ENOMEM;
        goto done;
    }
    DEBUG(SSSDBG_TRACE_LIBS,
          ("Base attribute of [%s] is [%s]\n",
           attr_desc, base));

    /* Next, determine if this is a ranged attribute */
    if (strncmp(endptr+1, SDAP_RANGE_STRING, rangestringlen) != 0) {
        /* This is some other sub-attribute. We'll just return the whole
         * thing in case it's dealt with elsewhere.
         */
        *base_attr = talloc_strdup(mem_ctx, attr_desc);
        if (!*base_attr) {
            ret = ENOMEM;
        } else {
            ret = EOK;
        }
        DEBUG(SSSDBG_TRACE_LIBS,
              ("[%s] contains sub-attribute other than a range, returning whole\n",
               attr_desc));
        goto done;
    } else if (disable_range_retrieval) {
        /* This is range sub-attribute, but we want to ignore it.
         */
        *base_attr = talloc_strdup(mem_ctx, attr_desc);
        if (!*base_attr) {
            ret = ENOMEM;
        } else {
            ret = ECANCELED;
        }
        goto done;
    }

    /* Get the end of the range */
    end_range = strchr(endptr + rangestringlen +1, '-');
    if (!end_range) {
        ret = EINVAL;
        DEBUG(SSSDBG_MINOR_FAILURE,
              ("Cannot find hyphen in [%s]\n",
               endptr + rangestringlen +1));
        goto done;
    }
    end_range++; /* advance past the hyphen */

    if (*end_range == '*') {
        /* this was the last iteration of range retrievals */
        *base_attr = talloc_steal(mem_ctx, base);
        *range_offset = 0;
        DEBUG(SSSDBG_TRACE_LIBS,
              ("[%s] contained the last set of values for this attribute\n",
               attr_desc));
        ret = EOK;
        goto done;
    }

    *range_offset = strtouint32(end_range, &endptr, 10);
    if (*endptr != '\0') {
        *range_offset = 0;
        ret = errno;
        DEBUG(SSSDBG_MINOR_FAILURE,
              ("[%s] did not parse as an unsigned integer: [%s]\n",
               end_range, strerror(ret)));
        goto done;
    }
    (*range_offset)++;

    *base_attr = talloc_steal(mem_ctx, base);
    DEBUG(SSSDBG_TRACE_LIBS,
          ("Parsed range values: [%s][%d]\n",
           base, *range_offset));

    ret = EAGAIN;
done:
    talloc_free(tmp_ctx);
    return ret;
}