From 65f90758d4df5581bc94746d47a1548ed8ff738b Mon Sep 17 00:00:00 2001 From: Simo Sorce Date: Mon, 6 Apr 2009 15:54:00 -0400 Subject: Unify name parsing and reposnder headers Use common sss_parse_name function in all responders Simplify responder headers by combining common,cmd,dp in one header and add name parse structure as part of the common responder context. --- server/responder/common/responder.h | 119 +++++++++++++++++++++++++ server/responder/common/responder_cmd.c | 2 +- server/responder/common/responder_cmd.h | 87 ------------------ server/responder/common/responder_common.c | 136 +++++++++++++++++++++++------ server/responder/common/responder_common.h | 26 ------ server/responder/common/responder_dp.c | 2 +- server/responder/common/responder_dp.h | 3 - 7 files changed, 231 insertions(+), 144 deletions(-) create mode 100644 server/responder/common/responder.h delete mode 100644 server/responder/common/responder_cmd.h delete mode 100644 server/responder/common/responder_common.h delete mode 100644 server/responder/common/responder_dp.h (limited to 'server/responder/common') diff --git a/server/responder/common/responder.h b/server/responder/common/responder.h new file mode 100644 index 00000000..4b6bfeee --- /dev/null +++ b/server/responder/common/responder.h @@ -0,0 +1,119 @@ +/* + SSSD + + SSS Client Responder, header file + + Copyright (C) Simo Sorce 2008 + + This program is free software; you can redistribute it and/or modify + 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 . +*/ + +#ifndef __SSS_RESPONDER_H__ +#define __SSS_RESPONDER_H__ + +#include +#include +#include +#include "config.h" +#include "talloc.h" +#include "tevent.h" +#include "ldb.h" +#include "sbus/sssd_dbus.h" +#include "../sss_client/sss_cli.h" +#include "util/btreemap.h" + +/* needed until nsssrv.h is updated */ +struct cli_request { + + /* original request from the wire */ + struct sss_packet *in; + + /* reply data */ + struct sss_packet *out; +}; + +struct sss_names_ctx { + char *re_pattern; + char *fq_fmt; + + pcre *re; +}; + +struct resp_ctx { + struct tevent_context *ev; + struct tevent_fd *lfde; + int lfd; + struct tevent_fd *priv_lfde; + int priv_lfd; + struct sysdb_ctx *sysdb; + struct confdb_ctx *cdb; + const char *sock_name; + const char *priv_sock_name; + struct service_sbus_ctx *ss_ctx; + struct service_sbus_ctx *dp_ctx; + + struct btreemap *domain_map; + char *default_domain; + + struct sbus_method *sss_sbus_methods; + struct sss_cmd_table *sss_cmds; + const char *sss_pipe_name; + const char *confdb_service_path; + struct sbus_method *dp_methods; + + struct sss_names_ctx *names; + + void *pvt_ctx; +}; + +struct cli_ctx { + struct tevent_context *ev; + struct resp_ctx *rctx; + int cfd; + struct tevent_fd *cfde; + struct sockaddr_un addr; + struct cli_request *creq; + int priv; +}; + +struct sss_cmd_table { + enum sss_cli_command cmd; + int (*fn)(struct cli_ctx *cctx); +}; + +/* responder_common.c */ +int sss_process_init(TALLOC_CTX *mem_ctx, + struct tevent_context *ev, + struct confdb_ctx *cdb, + struct sbus_method sss_sbus_methods[], + struct sss_cmd_table sss_cmds[], + const char *sss_pipe_name, + const char *sss_priv_pipe_name, + const char *confdb_service_path, + struct sbus_method dp_methods[], + struct resp_ctx **responder_ctx); + +int sss_parse_name(TALLOC_CTX *memctx, + struct sss_names_ctx *snctx, + const char *orig, char **domain, char **name); + +/* responder_cmd.c */ +int sss_cmd_execute(struct cli_ctx *cctx, struct sss_cmd_table *sss_cmds); +void sss_cmd_done(struct cli_ctx *cctx, void *freectx); +int sss_cmd_get_version(struct cli_ctx *cctx); + +/* responder_dp.c */ +int sss_dp_init(struct resp_ctx *rctx, struct sbus_method dp_methods[]); + +#endif /* __SSS_RESPONDER_H__ */ diff --git a/server/responder/common/responder_cmd.c b/server/responder/common/responder_cmd.c index a434b3e1..33caa032 100644 --- a/server/responder/common/responder_cmd.c +++ b/server/responder/common/responder_cmd.c @@ -20,7 +20,7 @@ */ #include #include "util/util.h" -#include "responder/common/responder_cmd.h" +#include "responder/common/responder.h" #include "responder/common/responder_packet.h" diff --git a/server/responder/common/responder_cmd.h b/server/responder/common/responder_cmd.h deleted file mode 100644 index eb3bd082..00000000 --- a/server/responder/common/responder_cmd.h +++ /dev/null @@ -1,87 +0,0 @@ -/* - SSSD - - SSS Client Responder, header file - - Copyright (C) Simo Sorce 2008 - - This program is free software; you can redistribute it and/or modify - 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 . -*/ - -#ifndef __SSSSRV_CMD_H__ -#define __SSSSRV_CMD_H__ - -#include -#include -#include "talloc.h" -#include "tevent.h" -#include "ldb.h" -#include "../sss_client/sss_cli.h" -#include "util/btreemap.h" - -/* needed until nsssrv.h is updated */ -struct cli_request { - - /* original request from the wire */ - struct sss_packet *in; - - /* reply data */ - struct sss_packet *out; -}; - -struct resp_ctx { - struct tevent_context *ev; - struct tevent_fd *lfde; - int lfd; - struct tevent_fd *priv_lfde; - int priv_lfd; - struct sysdb_ctx *sysdb; - struct confdb_ctx *cdb; - const char *sock_name; - const char *priv_sock_name; - struct service_sbus_ctx *ss_ctx; - struct service_sbus_ctx *dp_ctx; - - struct btreemap *domain_map; - char *default_domain; - - struct sbus_method *sss_sbus_methods; - struct sss_cmd_table *sss_cmds; - const char *sss_pipe_name; - const char *confdb_service_path; - struct sbus_method *dp_methods; - - void *pvt_ctx; -}; - -struct cli_ctx { - struct tevent_context *ev; - struct resp_ctx *rctx; - int cfd; - struct tevent_fd *cfde; - struct sockaddr_un addr; - struct cli_request *creq; - int priv; -}; - -struct sss_cmd_table { - enum sss_cli_command cmd; - int (*fn)(struct cli_ctx *cctx); -}; - -int sss_cmd_execute(struct cli_ctx *cctx, struct sss_cmd_table *sss_cmds); -void sss_cmd_done(struct cli_ctx *cctx, void *freectx); -int sss_cmd_get_version(struct cli_ctx *cctx); - -#endif /* __SSSSRV_CMD_H__ */ diff --git a/server/responder/common/responder_common.c b/server/responder/common/responder_common.c index fa024d53..855c78a7 100644 --- a/server/responder/common/responder_common.c +++ b/server/responder/common/responder_common.c @@ -36,15 +36,15 @@ #include "dbus/dbus.h" #include "sbus/sssd_dbus.h" #include "util/btreemap.h" -#include "responder/common/responder_common.h" +#include "responder/common/responder.h" #include "responder/common/responder_packet.h" -#include "responder/common/responder_cmd.h" -#include "responder/common/responder_dp.h" #include "providers/data_provider.h" #include "monitor/monitor_sbus.h" #include "monitor/monitor_interfaces.h" #include "sbus/sbus_client.h" +#define NAMES_CONFIG "config/names" + static void set_nonblocking(int fd) { unsigned v; @@ -487,6 +487,62 @@ done: return retval; } +int sss_names_init(struct resp_ctx *rctx) +{ + struct sss_names_ctx *ctx; + const char *errstr; + int errval; + int errpos; + int ret; + + ctx = talloc_zero(rctx, struct sss_names_ctx); + if (!ctx) return ENOMEM; + + ret = confdb_get_string(rctx->cdb, ctx, NAMES_CONFIG, + "re-expression", NULL, &ctx->re_pattern); + if (ret != EOK) goto done; + + if (!ctx->re_pattern) { + ctx->re_pattern = talloc_strdup(ctx, + "(?[^@]+)@?(?[^@]*$)"); + if (!ctx->re_pattern) { + ret = ENOMEM; + goto done; + } + } + + ret = confdb_get_string(rctx->cdb, ctx, NAMES_CONFIG, + "full-name-format", NULL, &ctx->fq_fmt); + if (ret != EOK) goto done; + + if (!ctx->fq_fmt) { + ctx->fq_fmt = talloc_strdup(ctx, "%1$s@%2$s"); + if (!ctx->fq_fmt) { + ret = ENOMEM; + goto done; + } + } + + ctx->re = pcre_compile2(ctx->re_pattern, + PCRE_DUPNAMES | PCRE_EXTENDED, + &errval, &errstr, &errpos, NULL); + if (!ctx->re) { + DEBUG(1, ("Invalid Regular Expression pattern at position %d." + " (Error: %d [%s])\n", errpos, errval, errstr)); + ret = EFAULT; + goto done; + } + + rctx->names = ctx; + ret = EOK; + +done: + if (ret != EOK) { + talloc_free(ctx); + } + return ret; +} + int sss_process_init(TALLOC_CTX *mem_ctx, struct tevent_context *ev, struct confdb_ctx *cdb, @@ -539,6 +595,12 @@ int sss_process_init(TALLOC_CTX *mem_ctx, return ret; } + ret = sss_names_init(rctx); + if (ret != EOK) { + DEBUG(0, ("fatal error initializing regex data\n")); + return ret; + } + /* after all initializations we are ready to listen on our socket */ ret = set_unix_socket(rctx); if (ret != EOK) { @@ -553,33 +615,55 @@ int sss_process_init(TALLOC_CTX *mem_ctx, } int sss_parse_name(TALLOC_CTX *memctx, - const char *fullname, - struct btreemap *domain_map, - const char **domain, const char **name) { - char *delim; - struct btreemap *node; - int ret; + struct sss_names_ctx *snctx, + const char *orig, char **domain, char **name) +{ + pcre *re = snctx->re; + const char *result; + int ovec[30]; + int origlen; + int ret, strnum; - if ((delim = strchr(fullname, SSS_DOMAIN_DELIM)) != NULL) { - - /* Check for registered domain */ - ret = btreemap_search_key(domain_map, (void *)(delim+1), &node); - if (ret != BTREEMAP_FOUND) { - /* No such domain was registered. Return EINVAL. - * TODO: alternative approach? - * Alternatively, we could simply fail down to - * below, treating the entire construct as the - * full name if the domain is unspecified. - */ - return EINVAL; - } + origlen = strlen(orig); + + ret = pcre_exec(re, NULL, orig, origlen, 0, PCRE_NOTEMPTY, ovec, 30); + if (ret < 0) { + DEBUG(2, ("PCRE Matching error, %d\n", ret)); + return EINVAL; + } - *name = talloc_strndup(memctx, fullname, delim-fullname); - *domain = talloc_strdup(memctx, delim+1); + if (ret == 0) { + DEBUG(1, ("Too many matches, the pattern is invalid.\n")); } - else { - *name = talloc_strdup(memctx, fullname); + + strnum = ret; + + result = NULL; + ret = pcre_get_named_substring(re, orig, ovec, strnum, "name", &result); + if (ret < 0 || !result) { + DEBUG(2, ("Name not found!\n")); + return EINVAL; + } + *name = talloc_strdup(memctx, result); + pcre_free_substring(result); + if (!*name) return ENOMEM; + + + result = NULL; + ret = pcre_get_named_substring(re, orig, ovec, strnum, "domain", &result); + if (ret < 0 || !result) { + DEBUG(4, ("Domain not provided!\n")); *domain = NULL; + } else { + /* ignore "" string */ + if (*result) { + *domain = talloc_strdup(memctx, result); + pcre_free_substring(result); + if (!*domain) return ENOMEM; + } else { + pcre_free_substring(result); + *domain = NULL; + } } return EOK; diff --git a/server/responder/common/responder_common.h b/server/responder/common/responder_common.h deleted file mode 100644 index 8a7af08a..00000000 --- a/server/responder/common/responder_common.h +++ /dev/null @@ -1,26 +0,0 @@ -#include "config.h" -#include "sbus/sssd_dbus.h" -#include "responder/common/responder_cmd.h" -#include "util/btreemap.h" - -/* SSS_DOMAIN_DELIM can be specified in config.h */ -#ifndef SSS_DOMAIN_DELIM -#define SSS_DOMAIN_DELIM '@' -#endif - - -int sss_process_init(TALLOC_CTX *mem_ctx, - struct tevent_context *ev, - struct confdb_ctx *cdb, - struct sbus_method sss_sbus_methods[], - struct sss_cmd_table sss_cmds[], - const char *sss_pipe_name, - const char *sss_priv_pipe_name, - const char *confdb_service_path, - struct sbus_method dp_methods[], - struct resp_ctx **responder_ctx); - -int sss_parse_name(TALLOC_CTX *memctx, - const char *fullname, - struct btreemap *domain_map, - const char **domain, const char **name); diff --git a/server/responder/common/responder_dp.c b/server/responder/common/responder_dp.c index 937b48dc..7fc7e0b3 100644 --- a/server/responder/common/responder_dp.c +++ b/server/responder/common/responder_dp.c @@ -3,7 +3,7 @@ #include #include "util/util.h" #include "responder/common/responder_packet.h" -#include "responder/common/responder_common.h" +#include "responder/common/responder.h" #include "providers/data_provider.h" #include "sbus/sbus_client.h" #include "providers/dp_sbus.h" diff --git a/server/responder/common/responder_dp.h b/server/responder/common/responder_dp.h deleted file mode 100644 index 528490fc..00000000 --- a/server/responder/common/responder_dp.h +++ /dev/null @@ -1,3 +0,0 @@ -#include "sbus/sssd_dbus.h" - -int sss_dp_init(struct resp_ctx *rctx, struct sbus_method dp_methods[]); -- cgit