summaryrefslogtreecommitdiffstats
path: root/src/providers/files/files_id.c
blob: 41314c66b1e435b51fe0b9bc18779c11ad261773 (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
/*
    SSSD

    files_id.c - Identity operaions on the files provider

    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 "providers/data_provider/dp.h"
#include "providers/files/files_private.h"

struct files_account_info_handler_state {
    struct dp_reply_std reply;

    struct files_id_ctx *id_ctx;
};

struct tevent_req *
files_account_info_handler_send(TALLOC_CTX *mem_ctx,
                                struct files_id_ctx *id_ctx,
                                struct dp_id_data *data,
                                struct dp_req_params *params)
{
    struct files_account_info_handler_state *state;
    struct tevent_req *req;
    struct tevent_req **update_req = NULL;
    bool needs_update;
    errno_t ret;

    req = tevent_req_create(mem_ctx, &state,
                            struct files_account_info_handler_state);
    if (req == NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "tevent_req_create() failed\n");
        return NULL;
    }
    state->id_ctx = id_ctx;

    switch (data->entry_type & BE_REQ_TYPE_MASK) {
    case BE_REQ_USER:
        if (data->filter_type != BE_FILTER_ENUM) {
            DEBUG(SSSDBG_CRIT_FAILURE,
                  "Unexpected user filter type: %d\n", data->filter_type);
            ret = EINVAL;
            goto immediate;
        }
        update_req = &id_ctx->users_req;
        needs_update = id_ctx->updating_passwd ? true : false;
        break;
    case BE_REQ_GROUP:
        if (data->filter_type != BE_FILTER_ENUM) {
            DEBUG(SSSDBG_CRIT_FAILURE,
                  "Unexpected group filter type: %d\n", data->filter_type);
            ret = EINVAL;
            goto immediate;
        }
        update_req = &id_ctx->groups_req;
        needs_update = id_ctx->updating_groups ? true : false;
        break;
    case BE_REQ_INITGROUPS:
        if (data->filter_type != BE_FILTER_NAME) {
            DEBUG(SSSDBG_CRIT_FAILURE,
                  "Unexpected initgr filter type: %d\n", data->filter_type);
            ret = EINVAL;
            goto immediate;
        }
        if (strcmp(data->filter_value, DP_REQ_OPT_FILES_INITGR) != 0) {
            DEBUG(SSSDBG_CRIT_FAILURE,
                  "Unexpected initgr filter value: %d\n", data->filter_type);
            ret = EINVAL;
            goto immediate;
        }
        update_req = &id_ctx->initgroups_req;
        needs_update = id_ctx->updating_groups || id_ctx->updating_passwd \
                       ? true \
                       : false;
        break;
    default:
        DEBUG(SSSDBG_CRIT_FAILURE,
              "Unexpected entry type: %d\n", data->entry_type & BE_REQ_TYPE_MASK);
        ret = EINVAL;
        goto immediate;
    }

    if (needs_update == false) {
        DEBUG(SSSDBG_TRACE_LIBS, "The files domain no longer needs an update\n");
        ret = EOK;
        goto immediate;
    }

    if (*update_req != NULL) {
        DEBUG(SSSDBG_CRIT_FAILURE, "BUG: Received a concurrent update!\n");
        ret = EAGAIN;
        goto immediate;
    }

    /* id_ctx now must mark the requests as updated when the inotify-induced
     * update finishes
     */
    *update_req = req;
    return req;

immediate:
    dp_reply_std_set(&state->reply, DP_ERR_DECIDE, ret, NULL);

    if (ret == EOK) {
        tevent_req_done(req);
    } else {
        tevent_req_error(req, ret);
    }

    tevent_req_post(req, params->ev);
    return req;
}

static void finish_update_req(struct tevent_req **update_req,
                              errno_t ret)
{
    if (*update_req == NULL) {
        return;
    }

    if (ret != EOK) {
        tevent_req_error(*update_req, ret);
    } else {
        tevent_req_done(*update_req);
    }
    *update_req = NULL;
}

void files_account_info_finished(struct files_id_ctx *id_ctx,
                                 int req_type,
                                 errno_t ret)
{
    switch (req_type) {
    case BE_REQ_USER:
        finish_update_req(&id_ctx->users_req, ret);
        if (id_ctx->updating_groups == false) {
            finish_update_req(&id_ctx->initgroups_req, ret);
        }
        break;
    case BE_REQ_GROUP:
        finish_update_req(&id_ctx->groups_req, ret);
        if (id_ctx->updating_passwd == false) {
            finish_update_req(&id_ctx->initgroups_req, ret);
        }
        break;
    default:
        DEBUG(SSSDBG_CRIT_FAILURE,
               "Unexpected req_type %d\n", req_type);
        return;
    }
}

errno_t files_account_info_handler_recv(TALLOC_CTX *mem_ctx,
                                        struct tevent_req *req,
                                        struct dp_reply_std *data)
{
    struct files_account_info_handler_state *state = NULL;

    state = tevent_req_data(req, struct files_account_info_handler_state);

    TEVENT_REQ_RETURN_ON_ERROR(req);

    *data = state->reply;
    return EOK;
}