summaryrefslogtreecommitdiffstats
path: root/src/providers/data_provider/dp_reply_std.c
blob: fc576151ee6320921caaa7df3e8c098a73f736ef (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
/*
    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 <tevent.h>

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

static const char *dp_err_to_string(int dp_err_type)
{
    switch (dp_err_type) {
    case DP_ERR_OK:
        return "Success";
    case DP_ERR_OFFLINE:
        return "Provider is Offline";
    case DP_ERR_TIMEOUT:
        return "Request timed out";
    case DP_ERR_FATAL:
        return "Internal Error";
    default:
        break;
    }

    return "Unknown Error";
}

static const char *safe_be_req_err_msg(const char *msg_in,
                                       int dp_err_type)
{
    bool ok;

    if (msg_in == NULL) {
        /* No custom error, just use default */
        return dp_err_to_string(dp_err_type);
    }

    ok = sss_utf8_check((const uint8_t *) msg_in,
                        strlen(msg_in));
    if (!ok) {
        DEBUG(SSSDBG_MINOR_FAILURE,
              "Back end message [%s] contains invalid non-UTF8 character, " \
              "using default\n", msg_in);
        return dp_err_to_string(dp_err_type);
    }

    return msg_in;
}

void dp_req_reply_std(const char *request_name,
                      struct sbus_request *sbus_req,
                      struct dp_reply_std *reply)
{
    const char *safe_err_msg;

    safe_err_msg = safe_be_req_err_msg(reply->message, reply->dp_error);

    DP_REQ_DEBUG(SSSDBG_TRACE_LIBS, request_name, "Returning [%s]: %d,%d,%s",
                 dp_err_to_string(reply->dp_error), reply->dp_error,
                 reply->error, reply->message);

    sbus_request_return_and_finish(sbus_req,
                                   DBUS_TYPE_UINT16, &reply->dp_error,
                                   DBUS_TYPE_UINT32, &reply->error,
                                   DBUS_TYPE_STRING, &safe_err_msg,
                                   DBUS_TYPE_INVALID);
}

void dp_reply_std_set(struct dp_reply_std *reply,
                      int dp_error,
                      int error,
                      const char *msg)
{
    const char *def_msg;

    if (dp_error == DP_ERR_DECIDE) {
        switch (error) {
        case EOK:
            dp_error = DP_ERR_OK;
            break;
        case ERR_OFFLINE:
            dp_error = DP_ERR_OFFLINE;
            break;
        case ETIMEDOUT:
            dp_error = DP_ERR_TIMEOUT;
            break;
        default:
            dp_error = DP_ERR_FATAL;
            break;
        }
    }

    switch (dp_error) {
    case DP_ERR_OK:
        def_msg = "Success";
        break;
    case DP_ERR_OFFLINE:
        def_msg = "Offline";
        break;
    default:
        def_msg = sss_strerror(error);
        break;
    }

    if (dp_error == DP_ERR_OK && error != EOK) {
        DEBUG(SSSDBG_MINOR_FAILURE, "DP Error is OK on failed request?\n");
    }

    reply->dp_error = dp_error;
    reply->error = error;
    reply->message = msg == NULL ? def_msg : msg;
}