summaryrefslogtreecommitdiffstats
path: root/src/providers/data_provider/dp_target_sudo.c
blob: 37add97fe3e5ba5c71e90f088f08a6cacc45247b (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
/*
    Authors:
        Pavel Březina <pbrezina@redhat.com>

    Copyright (C) 2016 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 <talloc.h>
#include <tevent.h>

#include "sbus/sssd_dbus.h"
#include "providers/data_provider/dp_private.h"
#include "providers/data_provider/dp_iface.h"
#include "providers/backend.h"
#include "util/util.h"

static errno_t dp_sudo_parse_message(TALLOC_CTX *mem_ctx,
                                     DBusMessage *msg,
                                     uint32_t *_dp_flags,
                                     uint32_t *_sudo_type,
                                     char ***_rules)
{
    DBusError error;
    DBusMessageIter iter;
    DBusMessageIter array_iter;
    uint32_t dp_flags;
    uint32_t sudo_type;
    uint32_t num_rules;
    const char *rule;
    char **rules = NULL;
    uint32_t i;
    errno_t ret;

    dbus_error_init(&error);
    dbus_message_iter_init(msg, &iter);

    /* get dp flags */
    if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Failed, to parse the message!\n");
        ret = EIO;
        goto done;
    }

    dbus_message_iter_get_basic(&iter, &dp_flags);
    dbus_message_iter_next(&iter); /* step behind the request type */

    /* get type of the request */
    if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32) {
        DEBUG(SSSDBG_CRIT_FAILURE, "Failed, to parse the message!\n");
        ret = EIO;
        goto done;
    }

    dbus_message_iter_get_basic(&iter, &sudo_type);
    dbus_message_iter_next(&iter); /* step behind the request type */

    /* get additional arguments according to the request type */
    switch (sudo_type) {
    case BE_REQ_SUDO_FULL:
        /* no arguments required */
        break;
    case BE_REQ_SUDO_RULES:
        /* additional arguments:
         * rules_num
         * rules[rules_num]
         */
        /* read rules_num */
        if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_UINT32) {
            DEBUG(SSSDBG_CRIT_FAILURE, "Failed, to parse the message!\n");
            ret = EIO;
            goto done;
        }

        dbus_message_iter_get_basic(&iter, &num_rules);

        rules = talloc_zero_array(mem_ctx, char *, num_rules + 1);
        if (rules == NULL) {
            DEBUG(SSSDBG_CRIT_FAILURE, "talloc_array() failed.\n");
            ret = ENOMEM;
            goto done;
        }

        dbus_message_iter_next(&iter);

        if (dbus_message_iter_get_arg_type(&iter) != DBUS_TYPE_ARRAY) {
           DEBUG(SSSDBG_CRIT_FAILURE, "Failed, to parse the message!\n");
            ret = EIO;
            goto done;
        }

        dbus_message_iter_recurse(&iter, &array_iter);

        /* read the rules */
       for (i = 0; i < num_rules; i++) {
            if (dbus_message_iter_get_arg_type(&array_iter)
                    != DBUS_TYPE_STRING) {
                DEBUG(SSSDBG_CRIT_FAILURE, "Failed, to parse the message!\n");
                ret = EIO;
                goto done;
            }

            dbus_message_iter_get_basic(&array_iter, &rule);
            rules[i] = talloc_strdup(rules, rule);
            if (rules[i] == NULL) {
                DEBUG(SSSDBG_CRIT_FAILURE, "talloc_strdup() failed.\n");
                ret = ENOMEM;
                goto done;
            }

            dbus_message_iter_next(&array_iter);
        }

        rules[num_rules] = NULL;

        break;
    default:
        DEBUG(SSSDBG_CRIT_FAILURE, "Invalid request type %d\n", sudo_type);
        return EINVAL;
    }

    *_dp_flags = dp_flags;
    *_sudo_type = sudo_type;
    *_rules = rules;

    ret = EOK;

done:
   if (ret != EOK) {
        talloc_free(rules);
    }

    return ret;
}

static const char *dp_sudo_get_key(uint32_t type)
{
    switch (type) {
    case BE_REQ_SUDO_FULL:
        return "full-refresh";
    case BE_REQ_SUDO_RULES:
        return NULL;
    }

    return NULL;
}

static const char *dp_sudo_get_name(uint32_t type)
{
    switch (type) {
    case BE_REQ_SUDO_FULL:
        return "SUDO Full Refresh";
    case BE_REQ_SUDO_RULES:
        return "SUDO Rules Refresh";
    }

    return NULL;
}

errno_t dp_sudo_handler(struct sbus_request *sbus_req, void *dp_cli)
{
    struct dp_sudo_data *data;
    uint32_t dp_flags;
    const char *key;
    const char *name;
    errno_t ret;

    data = talloc_zero(sbus_req, struct dp_sudo_data);
    if (data == NULL) {
        return ENOMEM;
    }

    ret = dp_sudo_parse_message(data, sbus_req->message, &dp_flags,
                                &data->type, &data->rules);
    if (ret != EOK) {
        return ret;
    }

    key = dp_sudo_get_key(data->type);
    name = dp_sudo_get_name(data->type);

    dp_req_with_reply(dp_cli, NULL, name, key, sbus_req, DPT_SUDO,
                      DPM_SUDO_HANDLER, dp_flags, data,
                      dp_req_reply_std, struct dp_reply_std);

    return EOK;
}