fail_over.c revision ea347b1e07843f7dfc2a0880e2283ee3e63caf4b
/*
SSSD
Fail over helper functions.
Authors:
Martin Nagy <mnagy@redhat.com>
Jakub Hrozek <jhrozek@redhat.com>
Copyright (C) Red Hat, Inc 2010
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <errno.h>
#include <stdbool.h>
#include <strings.h>
#include <talloc.h>
#include "util/dlinklist.h"
#include "util/refcount.h"
#include "providers/fail_over.h"
#include "resolv/async_resolv.h"
#define DEFAULT_PORT_STATUS PORT_NEUTRAL
#define DEFAULT_SRV_STATUS SRV_NEUTRAL
enum srv_lookup_status {
SRV_NEUTRAL, /* We didn't try this SRV lookup yet */
SRV_RESOLVED, /* This SRV lookup is resolved */
SRV_NOT_RESOLVED, /* Could not resolve this SRV lookup */
SRV_EXPIRED /* Need to refresh the SRV query */
};
struct fo_ctx {
struct fo_service *service_list;
struct server_common *server_common_list;
struct fo_options *opts;
};
struct fo_service {
struct fo_service *prev;
struct fo_service *next;
char *name;
struct fo_server *active_server;
struct fo_server *last_tried_server;
struct fo_server *server_list;
};
struct fo_server {
void *user_data;
int port;
int port_status;
struct fo_service *service;
struct timeval last_status_change;
struct server_common *common;
};
struct server_common {
struct server_common *prev;
struct server_common *next;
char *name;
struct resolve_service_request *request_list;
int server_status;
struct timeval last_status_change;
};
struct srv_data {
char *dns_domain;
char *proto;
char *srv;
int srv_lookup_status;
struct timeval last_status_change;
};
struct resolve_service_request {
struct resolve_service_request *prev;
struct resolve_service_request *next;
struct server_common *server_common;
struct tevent_req *req;
};
struct status {
int value;
struct timeval last_change;
};
struct fo_ctx *
{
return NULL;
}
return NULL;
}
return ctx;
}
static const char *
{
switch (status) {
case PORT_NEUTRAL:
return "neutral";
case PORT_WORKING:
return "working";
case PORT_NOT_WORKING:
return "not working";
}
return "unknown port status";
}
static const char *
{
switch (status) {
case SRV_NEUTRAL:
return "neutral";
case SRV_RESOLVED:
return "resolved";
case SRV_NOT_RESOLVED:
return "not resolved";
case SRV_EXPIRED:
return "expired";
}
return "unknown SRV lookup status";
}
static const char *
{
switch (status) {
case SERVER_NAME_NOT_RESOLVED:
return "name not resolved";
case SERVER_RESOLVING_NAME:
return "resolving name";
case SERVER_NAME_RESOLVED:
return "name resolved";
case SERVER_WORKING:
return "working";
case SERVER_NOT_WORKING:
return "not working";
}
return "unknown server status";
}
int fo_is_srv_lookup(struct fo_server *s)
{
return s && s->srv_data;
}
static char *
{
char *query;
if (!fo_is_srv_lookup(server)) {
return NULL;
}
return query;
}
static struct fo_server *
{
}
}
}
}
/* add back the meta server to denote SRV lookup */
}
return meta;
}
static enum srv_lookup_status
{
switch(data->srv_lookup_status) {
case SRV_EXPIRED:
case SRV_NEUTRAL:
break;
case SRV_RESOLVED:
break;
case SRV_NOT_RESOLVED:
break;
default:
}
}
return data->srv_lookup_status;
}
static void
{
}
/*
* This function will return the status of the server. If the status was
* last updated a long time ago, we will first reset the status.
*/
static enum server_status
{
return SERVER_NAME_RESOLVED;
SERVER_NAME(server)));
}
}
}
/*
* This function will return the status of the service. If the status was
* last updated a long time ago, we will first reset the status.
*/
static enum port_status
{
}
}
return server->port_status;
}
static int
{
return 0;
return 1;
}
static int
{
if (!server_works(server))
return 0;
return 0;
return 1;
}
static int
{
return 0;
}
int
struct fo_service **_service)
{
struct fo_service *service;
int ret;
if (_service) {
}
return EEXIST;
return ret;
}
return ENOMEM;
return ENOMEM;
}
if (_service) {
}
return EOK;
}
int
struct fo_service **_service)
{
struct fo_service *service;
return EOK;
}
}
return ENOENT;
}
static int
struct server_common **_common)
{
struct server_common *common;
return ENOMEM;
return EOK;
}
}
return ENOENT;
}
static int server_common_destructor(void *memptr)
{
struct server_common *common;
if (common->request_list) {
return -1;
}
return 0;
}
static struct server_common *
{
struct server_common *common;
return NULL;
return NULL;
}
return common;
}
int
{
continue;
if (fo_is_srv_lookup(server)) {
return EEXIST;
}
}
}
return ENOMEM;
/* add the SRV-specific data */
return ENOMEM;
return ENOMEM;
if (dns_domain) {
return ENOMEM;
}
return EOK;
}
static struct fo_server *
{
int ret;
return NULL;
return NULL;
}
return NULL;
}
}
return server;
}
int
{
int count = 0;
count++;
}
return count;
}
int
void *user_data)
{
continue;
return EEXIST;
return EEXIST;
}
}
if (!server) {
return ENOMEM;
}
return EOK;
}
static int
{
/* If we already have a working server, use that one. */
if (service_works(server)) {
goto done;
}
}
/*
* Otherwise iterate through the server list.
*/
/* First, try servers after the last one we tried. */
if (service_works(server)) {
goto done;
}
}
}
/* If none were found, try at the start. */
if (service_works(server)) {
goto done;
}
break;
}
}
return ENOENT;
done:
return EOK;
}
static int
{
return 0;
}
static int
{
struct resolve_service_request *request;
return ENOMEM;
}
return ENOMEM;
}
return EOK;
}
/*******************************************************************
* Get server to connect to. *
*******************************************************************/
struct resolve_service_state {
struct resolv_ctx *resolv;
struct tevent_context *ev;
};
/* Forward declarations for SRV resolving */
static struct tevent_req *
static int
struct tevent_req *
struct fo_service *service)
{
int ret;
struct tevent_req *req;
struct tevent_req *subreq;
struct resolve_service_state *state;
return NULL;
goto done;
}
if (fo_is_srv_lookup(server)) {
/* Don't know the server yet, must do a SRV lookup */
goto done;
}
req);
return req;
}
/* This is a regular server, just do hostname lookup */
if (fo_resolve_service_server(req)) {
}
done:
}
return req;
}
enum server_status status);
/* SRV resolving finished, see if we got server to work with */
static void
{
struct tevent_req);
struct resolve_service_state);
int ret;
if (ret) {
return;
}
}
static bool
{
struct resolve_service_state);
struct tevent_req *subreq;
int ret;
case SERVER_NAME_NOT_RESOLVED: /* Request name resolution. */
return true;
}
/* FALLTHROUGH */
case SERVER_RESOLVING_NAME:
/* Name resolution is already under way. Just add ourselves into the
* waiting queue so we get notified after the operation is finished. */
return true;
}
break;
default: /* The name is already resolved. Return immediately. */
return true;
}
return false;
}
static void
{
struct tevent_req);
struct resolve_service_state);
struct server_common *common;
int resolv_status;
struct resolve_service_request *request;
int ret;
}
} else {
}
/* Take care of all requests for this server. */
if (resolv_status) {
/* FIXME FIXME: resolv_status is an ARES error.
* but any caller will expect classic error codes.
* also the send() function may return ENOENT, so this mix
* IS explosive (ENOENT = 2 = ARES_EFORMER) */
} else {
}
}
}
int
{
struct resolve_service_state *state;
/* always return the server if asked for, otherwise the caller
* cannot mark it as faulty in case we return an error */
if (server)
return EOK;
}
/*******************************************************************
* Resolve the server to connect to using a SRV query. *
*******************************************************************/
struct tevent_context *ev,
struct resolv_ctx *resolv);
char **dns_domain);
struct resolve_srv_state {
struct fo_service *service;
struct resolv_ctx *resolv;
struct tevent_context *ev;
};
static struct tevent_req *
{
int ret;
struct tevent_req *req;
struct tevent_req *subreq;
struct resolve_srv_state *state;
int status;
return NULL;
switch(status) {
case SRV_EXPIRED: /* Need a refresh */
/* FALLTHROUGH */
case SRV_NEUTRAL: /* Request SRV lookup */
/* we need to look up our DNS domain first */
goto done;
}
break;
}
/* we know the DNS domain, just do the lookup */
break;
case SRV_NOT_RESOLVED: /* query could not be resolved but don't retry yet */
goto done;
case SRV_RESOLVED: /* The query is resolved and valid. Return. */
return req;
default:
goto done;
}
done:
}
return req;
}
static void
{
struct tevent_req);
struct resolve_srv_state);
int ret;
if (ret) {
return;
}
}
static void
{
struct resolve_srv_state);
char *query;
struct tevent_req *subreq;
if (!query) {
return;
}
return;
}
}
static void
{
struct tevent_req);
struct resolve_srv_state);
struct ares_srv_reply *reply_list;
struct ares_srv_reply *reply;
int ret;
int resolv_status;
goto fail;
}
goto fail;
}
break;
}
}
if (!server) {
goto fail;
}
}
if (srv_list) {
}
return;
} else {
goto fail;
}
fail:
}
static int
{
struct resolve_srv_state);
/* always return the server if asked for, otherwise the caller
* cannot mark it as faulty in case we return an error */
if (server) {
}
return EOK;
}
/*******************************************************************
* Get Fully Qualified Domain Name of the host machine *
*******************************************************************/
struct resolve_get_domain_state {
char *fqdn;
char hostname[HOST_NAME_MAX];
};
struct tevent_req *
struct tevent_context *ev,
struct resolv_ctx *resolv)
{
int ret;
struct resolve_get_domain_state *state;
if (!req) {
return NULL;
}
if (ret) {
return NULL;
}
if (!subreq) {
return NULL;
}
return req;
}
{
struct tevent_req);
struct resolve_get_domain_state);
int ret;
if (ret) {
"resolver returned: [%d]: %s\n",
/* We'll proceed with hostname in this case */
} else {
}
}
char **dns_domain)
{
struct resolve_get_domain_state);
char *fqdn;
char *domptr;
/* If the FQDN did not contain a dot or the dot was the last character
* (broken DNS server perhaps */
} else {
domptr++;
}
if (*dns_domain == NULL) {
return ENOMEM;
}
return EOK;
}
static void
enum server_status status)
{
}
void
{
return;
}
}
void
{
if (status == PORT_WORKING) {
}
}
{
if (!service) {
return;
}
if (!server) {
return;
}
service->active_server = 0;
}
}
void *
{
}
int
{
}
{
if (fo_is_srv_lookup(server)) {
return "SRV lookup meta-server";
}
return "unknown name";
}
}
struct hostent *
{
return NULL;
}
}