b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek/*
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek SSSD
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek KCM Server - the mainloop and server setup
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek Copyright (C) Red Hat, 2016
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek This program is free software; you can redistribute it and/or modify
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek it under the terms of the GNU General Public License as published by
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek the Free Software Foundation; either version 3 of the License, or
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek (at your option) any later version.
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek This program is distributed in the hope that it will be useful,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek but WITHOUT ANY WARRANTY; without even the implied warranty of
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek GNU General Public License for more details.
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek You should have received a copy of the GNU General Public License
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek along with this program. If not, see <http://www.gnu.org/licenses/>.
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek*/
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek#include "config.h"
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek#include <popt.h>
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek#include "responder/kcm/kcm.h"
70fe6e2bb398b8669ad1aebeaf0abcbffc307475Jakub Hrozek#include "responder/kcm/kcmsrv_ccache.h"
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek#include "responder/kcm/kcmsrv_pvt.h"
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek#include "responder/common/responder.h"
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek#include "util/util.h"
2c10819750a8d920ab755eba1278e6e20e684e93Sumit Bose#include "util/sss_krb5.h"
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek#define DEFAULT_KCM_FD_LIMIT 2048
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek#ifndef SSS_KCM_SOCKET_NAME
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek#define SSS_KCM_SOCKET_NAME DEFAULT_KCM_SOCKET_PATH
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek#endif
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozekstatic int kcm_responder_ctx_destructor(void *ptr)
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek{
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek struct resp_ctx *rctx = talloc_get_type(ptr, struct resp_ctx);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek /* mark that we are shutting down the responder, so it is propagated
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek * into underlying contexts that are freed right before rctx */
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek DEBUG(SSSDBG_TRACE_FUNC, "Responder is being shut down\n");
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek rctx->shutting_down = true;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek return 0;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek}
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozekstatic errno_t kcm_get_ccdb_be(struct kcm_ctx *kctx)
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek{
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek errno_t ret;
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek char *str_db;
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek ret = confdb_get_string(kctx->rctx->cdb,
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek kctx->rctx,
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek kctx->rctx->confdb_service_path,
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek CONFDB_KCM_DB,
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek "secrets",
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek &str_db);
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek if (ret != EOK) {
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek DEBUG(SSSDBG_OP_FAILURE,
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek "Cannot get the KCM database type [%d]: %s\n",
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek ret, strerror(ret));
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek return ret;
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek }
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek DEBUG(SSSDBG_CONF_SETTINGS, "KCM database type: %s\n", str_db);
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek if (strcasecmp(str_db, "memory") == 0) {
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek kctx->cc_be = CCDB_BE_MEMORY;
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek return EOK;
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek } else if (strcasecmp(str_db, "secrets") == 0) {
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek kctx->cc_be = CCDB_BE_SECRETS;
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek return EOK;
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek }
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE, "Unexpected KCM database type %s\n", str_db);
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek return EOK;
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek}
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozekstatic int kcm_get_config(struct kcm_ctx *kctx)
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek{
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek int ret;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek char *sock_name;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek ret = confdb_get_int(kctx->rctx->cdb,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek CONFDB_KCM_CONF_ENTRY,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek CONFDB_SERVICE_FD_LIMIT,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek DEFAULT_KCM_FD_LIMIT,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek &kctx->fd_limit);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek if (ret != EOK) {
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek "Failed to get file descriptors limit\n");
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek goto done;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek }
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek ret = confdb_get_int(kctx->rctx->cdb,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek kctx->rctx->confdb_service_path,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek CONFDB_RESPONDER_CLI_IDLE_TIMEOUT,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek CONFDB_RESPONDER_CLI_IDLE_DEFAULT_TIMEOUT,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek &kctx->rctx->client_idle_timeout);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek if (ret != EOK) {
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek DEBUG(SSSDBG_OP_FAILURE,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek "Cannot get the client idle timeout [%d]: %s\n",
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek ret, strerror(ret));
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek goto done;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek }
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek /* Ensure that the client timeout is at least ten seconds */
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek if (kctx->rctx->client_idle_timeout < 10) {
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek kctx->rctx->client_idle_timeout = 10;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek }
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek ret = confdb_get_string(kctx->rctx->cdb,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek kctx->rctx,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek kctx->rctx->confdb_service_path,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek CONFDB_KCM_SOCKET,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek SSS_KCM_SOCKET_NAME,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek &sock_name);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek if (ret != EOK) {
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek DEBUG(SSSDBG_OP_FAILURE,
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek "Cannot get KCM socket path [%d]: %s\n",
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek ret, strerror(ret));
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek goto done;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek }
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek kctx->rctx->sock_name = sock_name;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek ret = kcm_get_ccdb_be(kctx);
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek if (ret != EOK) {
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek DEBUG(SSSDBG_OP_FAILURE,
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek "Cannot get KCM ccache DB [%d]: %s\n",
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek ret, strerror(ret));
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek goto done;
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek }
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
e89ba95737202d551db2c9524127e6c4cf308796Jakub Hrozek if (kctx->cc_be == CCDB_BE_SECRETS) {
e89ba95737202d551db2c9524127e6c4cf308796Jakub Hrozek ret = responder_setup_idle_timeout_config(kctx->rctx);
e89ba95737202d551db2c9524127e6c4cf308796Jakub Hrozek if (ret != EOK) {
e89ba95737202d551db2c9524127e6c4cf308796Jakub Hrozek DEBUG(SSSDBG_MINOR_FAILURE,
e89ba95737202d551db2c9524127e6c4cf308796Jakub Hrozek "Cannot set up idle responder timeout\n");
e89ba95737202d551db2c9524127e6c4cf308796Jakub Hrozek /* Not fatal */
e89ba95737202d551db2c9524127e6c4cf308796Jakub Hrozek }
e89ba95737202d551db2c9524127e6c4cf308796Jakub Hrozek }
e89ba95737202d551db2c9524127e6c4cf308796Jakub Hrozek
2b5518eeaacc6245cfa77ee4a7086f16208060fcJakub Hrozek kctx->qctx = kcm_ops_queue_create(kctx);
2b5518eeaacc6245cfa77ee4a7086f16208060fcJakub Hrozek if (ret != EOK) {
2b5518eeaacc6245cfa77ee4a7086f16208060fcJakub Hrozek DEBUG(SSSDBG_OP_FAILURE,
2b5518eeaacc6245cfa77ee4a7086f16208060fcJakub Hrozek "Cannot create KCM request queue [%d]: %s\n",
2b5518eeaacc6245cfa77ee4a7086f16208060fcJakub Hrozek ret, strerror(ret));
2b5518eeaacc6245cfa77ee4a7086f16208060fcJakub Hrozek goto done;
2b5518eeaacc6245cfa77ee4a7086f16208060fcJakub Hrozek }
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek ret = EOK;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozekdone:
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek return ret;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek}
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozekstatic int kcm_data_destructor(void *ptr)
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek{
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek struct kcm_resp_ctx *kcm_data = talloc_get_type(ptr, struct kcm_resp_ctx);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek if (kcm_data != NULL) {
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek krb5_free_context(kcm_data->k5c);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek }
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek return 0;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek}
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
70fe6e2bb398b8669ad1aebeaf0abcbffc307475Jakub Hrozekstatic struct kcm_resp_ctx *kcm_data_setup(TALLOC_CTX *mem_ctx,
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek struct tevent_context *ev,
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek enum kcm_ccdb_be cc_be)
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek{
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek struct kcm_resp_ctx *kcm_data;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek krb5_error_code kret;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek kcm_data = talloc_zero(mem_ctx, struct kcm_resp_ctx);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek if (kcm_data == NULL) {
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE, "fatal error initializing kcm data\n");
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek return NULL;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek }
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek kcm_data->db = kcm_ccdb_init(kcm_data, ev, cc_be);
70fe6e2bb398b8669ad1aebeaf0abcbffc307475Jakub Hrozek if (kcm_data->db == NULL) {
70fe6e2bb398b8669ad1aebeaf0abcbffc307475Jakub Hrozek talloc_free(kcm_data);
70fe6e2bb398b8669ad1aebeaf0abcbffc307475Jakub Hrozek return NULL;
70fe6e2bb398b8669ad1aebeaf0abcbffc307475Jakub Hrozek }
70fe6e2bb398b8669ad1aebeaf0abcbffc307475Jakub Hrozek
2c10819750a8d920ab755eba1278e6e20e684e93Sumit Bose kret = sss_krb5_init_context(&kcm_data->k5c);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek if (kret != EOK) {
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek talloc_free(kcm_data);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek return NULL;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek }
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek talloc_set_destructor((TALLOC_CTX*)kcm_data, kcm_data_destructor);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek return kcm_data;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek}
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozekstatic int kcm_process_init(TALLOC_CTX *mem_ctx,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek struct tevent_context *ev,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek struct confdb_ctx *cdb)
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek{
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek struct resp_ctx *rctx;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek struct kcm_ctx *kctx;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek int ret;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek rctx = talloc_zero(mem_ctx, struct resp_ctx);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek if (rctx == NULL) {
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE, "fatal error initializing resp_ctx\n");
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek return ENOMEM;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek }
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek rctx->ev = ev;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek rctx->cdb = cdb;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek rctx->confdb_service_path = CONFDB_KCM_CONF_ENTRY;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek rctx->shutting_down = false;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek rctx->lfd = -1;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek rctx->priv_lfd = -1;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek talloc_set_destructor((TALLOC_CTX*)rctx, kcm_responder_ctx_destructor);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek kctx = talloc_zero(rctx, struct kcm_ctx);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek if (kctx == NULL) {
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE, "fatal error initializing kcm_ctx\n");
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek ret = ENOMEM;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek goto fail;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek }
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek kctx->rctx = rctx;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek kctx->rctx->pvt_ctx = kctx;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek ret = kcm_get_config(kctx);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek if (ret != EOK) {
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE, "fatal error getting KCM config\n");
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek goto fail;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek }
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
35c9dfe9ba78d3a635cd1af0fb6349ba44344623Jakub Hrozek kctx->kcm_data = kcm_data_setup(kctx, ev, kctx->cc_be);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek if (kctx->kcm_data == NULL) {
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek DEBUG(SSSDBG_FATAL_FAILURE,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek "fatal error initializing responder data\n");
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek ret = EIO;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek goto fail;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek }
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek /* Set up file descriptor limits */
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek responder_set_fd_limit(kctx->fd_limit);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek ret = activate_unix_sockets(rctx, kcm_connection_setup);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek if (ret != EOK) goto fail;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek DEBUG(SSSDBG_TRACE_FUNC, "KCM Initialization complete\n");
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek return EOK;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozekfail:
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek talloc_free(rctx);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek return ret;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek}
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozekint main(int argc, const char *argv[])
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek{
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek int opt;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek poptContext pc;
cb75b275d15beedd1fdecc1f8ced657fba282218Lukas Slebodnik char *opt_logger = NULL;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek struct main_context *main_ctx;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek int ret;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek uid_t uid;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek gid_t gid;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek struct poptOption long_options[] = {
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek POPT_AUTOHELP
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek SSSD_MAIN_OPTS
cb75b275d15beedd1fdecc1f8ced657fba282218Lukas Slebodnik SSSD_LOGGER_OPTS
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek SSSD_SERVER_OPTS(uid, gid)
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek POPT_TABLEEND
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek };
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
677a31351c80453d9ce006481364399a96312052René Genz /* Set debug level to invalid value so we can decide if -d 0 was used. */
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek debug_level = SSSDBG_INVALID;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek umask(DFL_RSP_UMASK);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek pc = poptGetContext(argv[0], argc, argv, long_options, 0);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek while((opt = poptGetNextOpt(pc)) != -1) {
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek switch(opt) {
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek default:
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek fprintf(stderr, "\nInvalid option %s: %s\n\n",
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek poptBadOption(pc, 0), poptStrerror(opt));
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek poptPrintUsage(pc, stderr, 0);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek return 1;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek }
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek }
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek poptFreeContext(pc);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek DEBUG_INIT(debug_level);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
677a31351c80453d9ce006481364399a96312052René Genz /* set up things like debug, signals, daemonization, etc. */
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek debug_log_file = "sssd_kcm";
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
cb75b275d15beedd1fdecc1f8ced657fba282218Lukas Slebodnik sss_set_logger(opt_logger);
cb75b275d15beedd1fdecc1f8ced657fba282218Lukas Slebodnik
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek ret = server_setup("sssd[kcm]", 0, uid, gid, CONFDB_KCM_CONF_ENTRY,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek &main_ctx);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek if (ret != EOK) return 2;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek ret = die_if_parent_died();
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek if (ret != EOK) {
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek /* This is not fatal, don't return */
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek DEBUG(SSSDBG_OP_FAILURE,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek "Could not set up to exit when parent process does\n");
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek }
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek ret = kcm_process_init(main_ctx,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek main_ctx->event_ctx,
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek main_ctx->confdb_ctx);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek if (ret != EOK) return 3;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek /* loop on main */
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek server_loop(main_ctx);
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek return 0;
b9c563c29243291f40489bb0dcbf3946fca72d58Jakub Hrozek}