2ronwalf/*
2ronwalf SSSD
2ronwalf
2ronwalf Data Provider Helpers
2ronwalf
2ronwalf Copyright (C) Stephen Gallagher <sgallagh@redhat.com> 2009
2ronwalf
2ronwalf This program is free software; you can redistribute it and/or modify
2ronwalf it under the terms of the GNU General Public License as published by
2ronwalf the Free Software Foundation; either version 3 of the License, or
2ronwalf (at your option) any later version.
2ronwalf
2ronwalf This program is distributed in the hope that it will be useful,
2ronwalf but WITHOUT ANY WARRANTY; without even the implied warranty of
2ronwalf MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2ronwalf GNU General Public License for more details.
2ronwalf
2ronwalf You should have received a copy of the GNU General Public License
2ronwalf along with this program. If not, see <http://www.gnu.org/licenses/>.
2ronwalf*/
2ronwalf
2ronwalf/* Needed for res_init() */
2ronwalf#include <netinet/in.h>
2ronwalf#include <arpa/nameser.h>
2ronwalf#include <resolv.h>
2ronwalf
2ronwalf#include "util/util.h"
2ronwalf#include "confdb/confdb.h"
2ronwalf#include "sbus/sssd_dbus.h"
2ronwalf#include "sbus/sbus_client.h"
2ronwalf#include "monitor/monitor_interfaces.h"
2ronwalf
2ronwalfint monitor_get_sbus_address(TALLOC_CTX *mem_ctx, char **address)
2ronwalf{
2ronwalf char *default_address;
2ronwalf
2ronwalf *address = NULL;
2ronwalf default_address = talloc_asprintf(mem_ctx, "unix:path=%s/%s",
2ronwalf PIPE_PATH, SSSD_SERVICE_PIPE);
2ronwalf if (default_address == NULL) {
2ronwalf return ENOMEM;
2ronwalf }
2ronwalf
2ronwalf *address = default_address;
2ronwalf return EOK;
2ronwalf}
2ronwalf
2ronwalfstatic void id_callback(DBusPendingCall *pending, void *ptr)
2ronwalf{
2ronwalf DBusMessage *reply;
2ronwalf DBusError dbus_error;
2ronwalf dbus_bool_t ret;
2ronwalf dbus_uint16_t mon_ver;
2ronwalf int type;
2ronwalf
2ronwalf dbus_error_init(&dbus_error);
2ronwalf
2ronwalf reply = dbus_pending_call_steal_reply(pending);
2ronwalf if (!reply) {
2ronwalf /* reply should never be null. This function shouldn't be called
2ronwalf * until reply is valid or timeout has occurred. If reply is NULL
2ronwalf * here, something is seriously wrong and we should bail out.
2ronwalf */
2ronwalf DEBUG(SSSDBG_FATAL_FAILURE,
14daenzerorama "Severe error. A reply callback was called but no"
2ronwalf " reply was received and no timeout occurred\n");
2ronwalf
2ronwalf /* FIXME: Destroy this connection? */
2ronwalf goto done;
14daenzerorama }
14daenzerorama
14daenzerorama type = dbus_message_get_type(reply);
14daenzerorama switch (type) {
14daenzerorama case DBUS_MESSAGE_TYPE_METHOD_RETURN:
14daenzerorama ret = dbus_message_get_args(reply, &dbus_error,
14daenzerorama DBUS_TYPE_UINT16, &mon_ver,
2ronwalf DBUS_TYPE_INVALID);
2ronwalf if (!ret) {
2ronwalf DEBUG(SSSDBG_CRIT_FAILURE, "Failed to parse message\n");
2ronwalf if (dbus_error_is_set(&dbus_error)) dbus_error_free(&dbus_error);
2ronwalf /* FIXME: Destroy this connection? */
2ronwalf goto done;
2ronwalf }
2ronwalf
2ronwalf DEBUG(SSSDBG_CONF_SETTINGS,
2ronwalf "Got id ack and version (%d) from Monitor\n", mon_ver);
24daenzerorama
24daenzerorama break;
2ronwalf
2ronwalf case DBUS_MESSAGE_TYPE_ERROR:
24daenzerorama DEBUG(SSSDBG_FATAL_FAILURE,"The Monitor returned an error [%s]\n",
2ronwalf dbus_message_get_error_name(reply));
24daenzerorama /* Falling through to default intentionally*/
24daenzerorama default:
2ronwalf /*
2ronwalf * Timeout or other error occurred or something
2ronwalf * unexpected happened.
2ronwalf * It doesn't matter which, because either way we
2ronwalf * know that this connection isn't trustworthy.
2ronwalf * We'll destroy it now.
2ronwalf */
2ronwalf
2ronwalf /* FIXME: Destroy this connection? */
2ronwalf break;
2ronwalf }
2ronwalf
2ronwalfdone:
2ronwalf dbus_pending_call_unref(pending);
2ronwalf dbus_message_unref(reply);
2ronwalf}
2ronwalf
2ronwalfstatic int monitor_common_send_id(struct sbus_connection *conn,
2ronwalf const char *name,
2ronwalf uint16_t version,
2ronwalf uint16_t type)
2ronwalf{
2ronwalf DBusMessage *msg;
2ronwalf dbus_bool_t ret;
2ronwalf int retval;
2ronwalf
2ronwalf /* create the message */
2ronwalf msg = dbus_message_new_method_call(NULL,
2ronwalf MON_SRV_PATH,
2ronwalf MON_SRV_IFACE,
2ronwalf MON_SRV_IFACE_REGISTERSERVICE);
2ronwalf if (msg == NULL) {
2ronwalf DEBUG(SSSDBG_FATAL_FAILURE, "Out of memory?!\n");
2ronwalf return ENOMEM;
2ronwalf }
2ronwalf
2ronwalf DEBUG(SSSDBG_CONF_SETTINGS, "Sending ID: (%s,%d)\n", name, version);
2ronwalf
2ronwalf ret = dbus_message_append_args(msg,
2ronwalf DBUS_TYPE_STRING, &name,
2ronwalf DBUS_TYPE_UINT16, &version,
2ronwalf DBUS_TYPE_UINT16, &type,
2ronwalf DBUS_TYPE_INVALID);
2ronwalf if (!ret) {
2ronwalf DEBUG(SSSDBG_CRIT_FAILURE, "Failed to build message\n");
2ronwalf return EIO;
2ronwalf }
2ronwalf
2ronwalf retval = sbus_conn_send(conn, msg, 3000,
2ronwalf id_callback,
2ronwalf NULL, NULL);
2ronwalf dbus_message_unref(msg);
2ronwalf return retval;
2ronwalf}
2ronwalf
2ronwalfint monitor_common_res_init(struct sbus_request *dbus_req, void *data)
10daenzerorama{
10daenzerorama int ret;
10daenzerorama
10daenzerorama ret = res_init();
2ronwalf if(ret != 0) {
2ronwalf return EIO;
2ronwalf }
2ronwalf
2ronwalf /* Send an empty reply to acknowledge receipt */
2ronwalf return sbus_request_return_and_finish(dbus_req, DBUS_TYPE_INVALID);
2ronwalf}
2ronwalf
2ronwalferrno_t sss_monitor_init(TALLOC_CTX *mem_ctx,
2ronwalf struct tevent_context *ev,
2ronwalf struct mon_cli_iface *mon_iface,
2ronwalf const char *svc_name,
2ronwalf uint16_t svc_version,
2ronwalf uint16_t svc_type,
2ronwalf void *pvt,
2ronwalf time_t *last_request_time,
2ronwalf struct sbus_connection **mon_conn)
2ronwalf{
2ronwalf errno_t ret;
2ronwalf char *sbus_address;
2ronwalf struct sbus_connection *conn;
2ronwalf
2ronwalf /* Set up SBUS connection to the monitor */
2ronwalf ret = monitor_get_sbus_address(NULL, &sbus_address);
2ronwalf if (ret != EOK) {
2ronwalf DEBUG(SSSDBG_FATAL_FAILURE, "Could not locate monitor address.\n");
2ronwalf return ret;
2ronwalf }
2ronwalf
2ronwalf ret = sbus_client_init(mem_ctx, ev, sbus_address, last_request_time, &conn);
2ronwalf if (ret != EOK) {
2ronwalf DEBUG(SSSDBG_FATAL_FAILURE, "Failed to connect to monitor services.\n");
2ronwalf talloc_free(sbus_address);
2ronwalf return ret;
2ronwalf }
2ronwalf talloc_free(sbus_address);
2ronwalf
2ronwalf ret = sbus_conn_register_iface(conn, &mon_iface->vtable, MONITOR_PATH, pvt);
2ronwalf if (ret != EOK) {
2ronwalf DEBUG(SSSDBG_FATAL_FAILURE, "Failed to export monitor client.\n");
2ronwalf return ret;
2ronwalf }
2ronwalf
7daenzerorama /* Identify ourselves to the monitor */
7daenzerorama ret = monitor_common_send_id(conn, svc_name, svc_version, svc_type);
7daenzerorama if (ret != EOK) {
7daenzerorama DEBUG(SSSDBG_FATAL_FAILURE, "Failed to identify to the monitor!\n");
7daenzerorama return ret;
7daenzerorama }
7daenzerorama
7daenzerorama *mon_conn = conn;
7daenzerorama
7daenzerorama return EOK;
7daenzerorama}
7daenzerorama