a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina/*
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina Authors:
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina Pavel Březina <pbrezina@redhat.com>
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina Copyright (C) 2016 Red Hat
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina This program is free software; you can redistribute it and/or modify
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina it under the terms of the GNU General Public License as published by
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina the Free Software Foundation; either version 3 of the License, or
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina (at your option) any later version.
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina This program is distributed in the hope that it will be useful,
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina but WITHOUT ANY WARRANTY; without even the implied warranty of
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina GNU General Public License for more details.
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina You should have received a copy of the GNU General Public License
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina along with this program. If not, see <http://www.gnu.org/licenses/>.
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina*/
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina#include <tevent.h>
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina#include "sbus/sssd_dbus.h"
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina#include "providers/data_provider/dp_private.h"
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina#include "providers/backend.h"
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina#include "util/sss_utf8.h"
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina#include "util/util.h"
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březinastatic const char *dp_err_to_string(int dp_err_type)
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina{
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina switch (dp_err_type) {
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina case DP_ERR_OK:
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina return "Success";
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina case DP_ERR_OFFLINE:
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina return "Provider is Offline";
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina case DP_ERR_TIMEOUT:
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina return "Request timed out";
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina case DP_ERR_FATAL:
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina return "Internal Error";
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina default:
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina break;
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina }
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina return "Unknown Error";
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina}
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březinastatic const char *safe_be_req_err_msg(const char *msg_in,
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina int dp_err_type)
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina{
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina bool ok;
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina if (msg_in == NULL) {
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina /* No custom error, just use default */
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina return dp_err_to_string(dp_err_type);
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina }
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina ok = sss_utf8_check((const uint8_t *) msg_in,
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina strlen(msg_in));
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina if (!ok) {
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina DEBUG(SSSDBG_MINOR_FAILURE,
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina "Back end message [%s] contains invalid non-UTF8 character, " \
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina "using default\n", msg_in);
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina return dp_err_to_string(dp_err_type);
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina }
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina return msg_in;
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina}
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březinavoid dp_req_reply_std(const char *request_name,
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina struct sbus_request *sbus_req,
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina struct dp_reply_std *reply)
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina{
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina const char *safe_err_msg;
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina safe_err_msg = safe_be_req_err_msg(reply->message, reply->dp_error);
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina DP_REQ_DEBUG(SSSDBG_TRACE_LIBS, request_name, "Returning [%s]: %d,%d,%s",
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina dp_err_to_string(reply->dp_error), reply->dp_error,
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina reply->error, reply->message);
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina sbus_request_return_and_finish(sbus_req,
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina DBUS_TYPE_UINT16, &reply->dp_error,
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina DBUS_TYPE_UINT32, &reply->error,
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina DBUS_TYPE_STRING, &safe_err_msg,
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina DBUS_TYPE_INVALID);
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina}
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březinavoid dp_reply_std_set(struct dp_reply_std *reply,
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina int dp_error,
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina int error,
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina const char *msg)
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina{
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina const char *def_msg;
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina if (dp_error == DP_ERR_DECIDE) {
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina switch (error) {
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina case EOK:
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina dp_error = DP_ERR_OK;
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina break;
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina case ERR_OFFLINE:
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina dp_error = DP_ERR_OFFLINE;
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina break;
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina case ETIMEDOUT:
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina dp_error = DP_ERR_TIMEOUT;
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina break;
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina default:
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina dp_error = DP_ERR_FATAL;
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina break;
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina }
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina }
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina switch (dp_error) {
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina case DP_ERR_OK:
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina def_msg = "Success";
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina break;
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina case DP_ERR_OFFLINE:
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina def_msg = "Offline";
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina break;
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina default:
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina def_msg = sss_strerror(error);
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina break;
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina }
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina if (dp_error == DP_ERR_OK && error != EOK) {
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina DEBUG(SSSDBG_MINOR_FAILURE, "DP Error is OK on failed request?\n");
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina }
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina reply->dp_error = dp_error;
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina reply->error = error;
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina reply->message = msg == NULL ? def_msg : msg;
a1bf79449204ce9a5392b9d09b953a6bdf53a122Pavel Březina}