Line data Source code
1 : /*
2 : Authors:
3 : Pavel Březina <pbrezina@redhat.com>
4 :
5 : Copyright (C) 2016 Red Hat
6 :
7 : This program is free software; you can redistribute it and/or modify
8 : it under the terms of the GNU General Public License as published by
9 : the Free Software Foundation; either version 3 of the License, or
10 : (at your option) any later version.
11 :
12 : This program is distributed in the hope that it will be useful,
13 : but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 : GNU General Public License for more details.
16 :
17 : You should have received a copy of the GNU General Public License
18 : along with this program. If not, see <http://www.gnu.org/licenses/>.
19 : */
20 :
21 : #include <tevent.h>
22 :
23 : #include "sbus/sssd_dbus.h"
24 : #include "providers/data_provider/dp_private.h"
25 : #include "providers/backend.h"
26 : #include "util/sss_utf8.h"
27 : #include "util/util.h"
28 :
29 0 : static const char *dp_err_to_string(int dp_err_type)
30 : {
31 0 : switch (dp_err_type) {
32 : case DP_ERR_OK:
33 0 : return "Success";
34 : case DP_ERR_OFFLINE:
35 0 : return "Provider is Offline";
36 : case DP_ERR_TIMEOUT:
37 0 : return "Request timed out";
38 : case DP_ERR_FATAL:
39 0 : return "Internal Error";
40 : default:
41 0 : break;
42 : }
43 :
44 0 : return "Unknown Error";
45 : }
46 :
47 0 : static const char *safe_be_req_err_msg(const char *msg_in,
48 : int dp_err_type)
49 : {
50 : bool ok;
51 :
52 0 : if (msg_in == NULL) {
53 : /* No custom error, just use default */
54 0 : return dp_err_to_string(dp_err_type);
55 : }
56 :
57 0 : ok = sss_utf8_check((const uint8_t *) msg_in,
58 : strlen(msg_in));
59 0 : if (!ok) {
60 0 : DEBUG(SSSDBG_MINOR_FAILURE,
61 : "Back end message [%s] contains invalid non-UTF8 character, " \
62 : "using default\n", msg_in);
63 0 : return dp_err_to_string(dp_err_type);
64 : }
65 :
66 0 : return msg_in;
67 : }
68 :
69 0 : void dp_req_reply_std(const char *request_name,
70 : struct sbus_request *sbus_req,
71 : struct dp_reply_std *reply)
72 : {
73 : const char *safe_err_msg;
74 :
75 0 : safe_err_msg = safe_be_req_err_msg(reply->message, reply->dp_error);
76 :
77 0 : DP_REQ_DEBUG(SSSDBG_TRACE_LIBS, request_name, "Returning [%s]: %d,%d,%s",
78 : dp_err_to_string(reply->dp_error), reply->dp_error,
79 : reply->error, reply->message);
80 :
81 0 : sbus_request_return_and_finish(sbus_req,
82 : DBUS_TYPE_UINT16, &reply->dp_error,
83 : DBUS_TYPE_UINT32, &reply->error,
84 : DBUS_TYPE_STRING, &safe_err_msg,
85 : DBUS_TYPE_INVALID);
86 0 : }
87 :
88 0 : void dp_reply_std_set(struct dp_reply_std *reply,
89 : int dp_error,
90 : int error,
91 : const char *msg)
92 : {
93 : const char *def_msg;
94 :
95 0 : if (dp_error == DP_ERR_DECIDE) {
96 0 : switch (error) {
97 : case EOK:
98 0 : dp_error = DP_ERR_OK;
99 0 : break;
100 : case ERR_OFFLINE:
101 0 : dp_error = DP_ERR_OFFLINE;
102 0 : break;
103 : case ETIMEDOUT:
104 0 : dp_error = DP_ERR_TIMEOUT;
105 0 : break;
106 : default:
107 0 : dp_error = DP_ERR_FATAL;
108 0 : break;
109 : }
110 : }
111 :
112 0 : switch (dp_error) {
113 : case DP_ERR_OK:
114 0 : def_msg = "Success";
115 0 : break;
116 : case DP_ERR_OFFLINE:
117 0 : def_msg = "Offline";
118 0 : break;
119 : default:
120 0 : def_msg = sss_strerror(error);
121 0 : break;
122 : }
123 :
124 0 : if (dp_error == DP_ERR_OK && error != EOK) {
125 0 : DEBUG(SSSDBG_MINOR_FAILURE, "DP Error is OK on failed request?\n");
126 : }
127 :
128 0 : reply->dp_error = dp_error;
129 0 : reply->error = error;
130 0 : reply->message = msg == NULL ? def_msg : msg;
131 0 : }
|