sudosrv.c revision 2827b0d03f7b6bafa504d22a5d7ca39cbda048b3
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek/*
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek Authors:
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek Pavel Březina <pbrezina@redhat.com>
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek Copyright (C) 2011 Red Hat
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek This program is free software; you can redistribute it and/or modify
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek it under the terms of the GNU General Public License as published by
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek the Free Software Foundation; either version 3 of the License, or
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek (at your option) any later version.
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek This program is distributed in the hope that it will be useful,
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek but WITHOUT ANY WARRANTY; without even the implied warranty of
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek GNU General Public License for more details.
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek You should have received a copy of the GNU General Public License
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek along with this program. If not, see <http://www.gnu.org/licenses/>.
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek*/
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek#include <popt.h>
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek#include "util/util.h"
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek#include "confdb/confdb.h"
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek#include "monitor/monitor_interfaces.h"
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek#include "responder/common/responder.h"
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek#include "responder/sudo/sudosrv_private.h"
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek#include "providers/data_provider.h"
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek
1a542b3698d8c42cf075b722f8838f106eb09fccPavel Březinastruct sbus_method monitor_sudo_methods[] = {
1a542b3698d8c42cf075b722f8838f106eb09fccPavel Březina { MON_CLI_METHOD_PING, monitor_common_pong },
1a542b3698d8c42cf075b722f8838f106eb09fccPavel Březina { MON_CLI_METHOD_RES_INIT, monitor_common_res_init },
44749ce0c1fee9babee80060fa0db99eebb2ab51Pavel Březina { MON_CLI_METHOD_ROTATE, responder_logrotate },
1a542b3698d8c42cf075b722f8838f106eb09fccPavel Březina { NULL, NULL }
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek};
fb4e4c4eb6a6dc732370584f70d23dd4a2c5c7b6Pavel Březina
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozekstruct sbus_interface monitor_sudo_interface = {
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek MONITOR_INTERFACE,
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek MONITOR_PATH,
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek SBUS_DEFAULT_VTABLE,
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek monitor_sudo_methods,
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek NULL
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek};
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozekstatic struct sbus_method sudo_dp_methods[] = {
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek { NULL, NULL }
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek};
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozekstruct sbus_interface sudo_dp_interface = {
f6171b2bc954a367f316853ab71090eb213bdee3Pavel Březina DP_INTERFACE,
f6171b2bc954a367f316853ab71090eb213bdee3Pavel Březina DP_PATH,
f6171b2bc954a367f316853ab71090eb213bdee3Pavel Březina SBUS_DEFAULT_VTABLE,
f6171b2bc954a367f316853ab71090eb213bdee3Pavel Březina sudo_dp_methods,
f7af8c5b369938725e47585c641ae5b017d442a1Pavel Březina NULL
f6171b2bc954a367f316853ab71090eb213bdee3Pavel Březina};
f6171b2bc954a367f316853ab71090eb213bdee3Pavel Březina
f6171b2bc954a367f316853ab71090eb213bdee3Pavel Březinastatic void sudo_dp_reconnect_init(struct sbus_connection *conn,
f6171b2bc954a367f316853ab71090eb213bdee3Pavel Březina int status,
f6171b2bc954a367f316853ab71090eb213bdee3Pavel Březina void *pvt)
f6171b2bc954a367f316853ab71090eb213bdee3Pavel Březina{
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek struct be_conn *be_conn = talloc_get_type(pvt, struct be_conn);
c9aab1c04c399ca2d1abef74f6df22ced34983dcPavel Březina int ret;
c9aab1c04c399ca2d1abef74f6df22ced34983dcPavel Březina
c9aab1c04c399ca2d1abef74f6df22ced34983dcPavel Březina /* Did we reconnect successfully? */
c9aab1c04c399ca2d1abef74f6df22ced34983dcPavel Březina if (status == SBUS_RECONNECT_SUCCESS) {
c9aab1c04c399ca2d1abef74f6df22ced34983dcPavel Březina DEBUG(SSSDBG_TRACE_FUNC, ("Reconnected to the Data Provider.\n"));
c9aab1c04c399ca2d1abef74f6df22ced34983dcPavel Březina
c9aab1c04c399ca2d1abef74f6df22ced34983dcPavel Březina /* Identify ourselves to the data provider */
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek ret = dp_common_send_id(be_conn->conn,
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek DATA_PROVIDER_VERSION,
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek "SUDO");
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek /* all fine */
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek if (ret == EOK) {
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek handle_requests_after_reconnect();
b0fa48b0d612b46a86e45f8e4b5d9feae9784c2bSimo Sorce return;
b0fa48b0d612b46a86e45f8e4b5d9feae9784c2bSimo Sorce }
b0fa48b0d612b46a86e45f8e4b5d9feae9784c2bSimo Sorce }
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek /* Failed to reconnect */
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE, ("Could not reconnect to %s provider.\n",
b0fa48b0d612b46a86e45f8e4b5d9feae9784c2bSimo Sorce be_conn->domain->name));
b0fa48b0d612b46a86e45f8e4b5d9feae9784c2bSimo Sorce}
b0fa48b0d612b46a86e45f8e4b5d9feae9784c2bSimo Sorce
b0fa48b0d612b46a86e45f8e4b5d9feae9784c2bSimo Sorceint sudo_process_init(TALLOC_CTX *mem_ctx,
b0fa48b0d612b46a86e45f8e4b5d9feae9784c2bSimo Sorce struct tevent_context *ev,
b0fa48b0d612b46a86e45f8e4b5d9feae9784c2bSimo Sorce struct confdb_ctx *cdb)
b0fa48b0d612b46a86e45f8e4b5d9feae9784c2bSimo Sorce{
b0fa48b0d612b46a86e45f8e4b5d9feae9784c2bSimo Sorce struct sss_cmd_table *sudo_cmds;
b0fa48b0d612b46a86e45f8e4b5d9feae9784c2bSimo Sorce struct sudo_ctx *sudo_ctx;
b0fa48b0d612b46a86e45f8e4b5d9feae9784c2bSimo Sorce struct be_conn *iter;
b0fa48b0d612b46a86e45f8e4b5d9feae9784c2bSimo Sorce int ret;
44749ce0c1fee9babee80060fa0db99eebb2ab51Pavel Březina int max_retries;
f5d4b05027acce06e3509ecb68869d1c7ef37180Pavel Březina
b0fa48b0d612b46a86e45f8e4b5d9feae9784c2bSimo Sorce sudo_ctx = talloc_zero(mem_ctx, struct sudo_ctx);
f5d4b05027acce06e3509ecb68869d1c7ef37180Pavel Březina if (!sudo_ctx) {
f5d4b05027acce06e3509ecb68869d1c7ef37180Pavel Březina DEBUG(SSSDBG_FATAL_FAILURE, ("fatal error initializing sudo_ctx\n"));
f5d4b05027acce06e3509ecb68869d1c7ef37180Pavel Březina return ENOMEM;
9675bccabff4e79d224f64611ad9ff3e073b488eSimo Sorce }
f5d4b05027acce06e3509ecb68869d1c7ef37180Pavel Březina
f5d4b05027acce06e3509ecb68869d1c7ef37180Pavel Březina sudo_cmds = get_sudo_cmds();
3f98cdc011bb4e8cd22c088f288b0bcdb6452492Jakub Hrozek ret = sss_process_init(sudo_ctx, ev, cdb,
sudo_cmds,
SSS_SUDO_SOCKET_NAME, NULL,
CONFDB_SUDO_CONF_ENTRY,
SSS_SUDO_SBUS_SERVICE_NAME,
SSS_SUDO_SBUS_SERVICE_VERSION,
&monitor_sudo_interface,
"SUDO",
&sudo_dp_interface,
&sudo_ctx->rctx);
if (ret != EOK) {
return ret;
}
sudo_ctx->rctx->pvt_ctx = sudo_ctx;
/* Enable automatic reconnection to the Data Provider */
ret = confdb_get_int(sudo_ctx->rctx->cdb, sudo_ctx->rctx,
CONFDB_SUDO_CONF_ENTRY,
CONFDB_SERVICE_RECON_RETRIES,
3, &max_retries);
if (ret != EOK) {
DEBUG(SSSDBG_FATAL_FAILURE,
("Failed to set up automatic reconnection\n"));
return ret;
}
for (iter = sudo_ctx->rctx->be_conns; iter; iter = iter->next) {
sbus_reconnect_init(iter->conn, max_retries,
sudo_dp_reconnect_init, iter);
}
DEBUG(SSSDBG_TRACE_FUNC, ("SUDO Initialization complete\n"));
return EOK;
}
int main(int argc, const char *argv[])
{
int opt;
poptContext pc;
struct main_context *main_ctx;
int ret;
struct poptOption long_options[] = {
POPT_AUTOHELP
SSSD_MAIN_OPTS
POPT_TABLEEND
};
/* Set debug level to invalid value so we can deside if -d 0 was used. */
debug_level = SSSDBG_INVALID;
pc = poptGetContext(argv[0], argc, argv, long_options, 0);
while((opt = poptGetNextOpt(pc)) != -1) {
switch(opt) {
default:
fprintf(stderr, "\nInvalid option %s: %s\n\n",
poptBadOption(pc, 0), poptStrerror(opt));
poptPrintUsage(pc, stderr, 0);
return 1;
}
}
poptFreeContext(pc);
CONVERT_AND_SET_DEBUG_LEVEL(debug_level);
/* set up things like debug, signals, daemonization, etc... */
debug_log_file = "sssd_sudo";
ret = server_setup("sssd[sudo]", 0, CONFDB_SUDO_CONF_ENTRY, &main_ctx);
if (ret != EOK) {
return 2;
}
ret = die_if_parent_died();
if (ret != EOK) {
/* This is not fatal, don't return */
DEBUG(SSSDBG_OP_FAILURE, ("Could not set up to exit "
"when parent process does\n"));
}
ret = sudo_process_init(main_ctx,
main_ctx->event_ctx,
main_ctx->confdb_ctx);
if (ret != EOK) {
return 3;
}
/* loop on main */
server_loop(main_ctx);
return 0;
}