diff options
-rw-r--r-- | server/responder/common/responder.h | 7 | ||||
-rw-r--r-- | server/responder/common/responder_common.c | 114 | ||||
-rw-r--r-- | server/util/usertools.c | 118 | ||||
-rw-r--r-- | server/util/util.h | 16 |
4 files changed, 135 insertions, 120 deletions
diff --git a/server/responder/common/responder.h b/server/responder/common/responder.h index f3d3293d..d901704a 100644 --- a/server/responder/common/responder.h +++ b/server/responder/common/responder.h @@ -53,13 +53,6 @@ struct cli_protocol_version { const char *description; }; -struct sss_names_ctx { - char *re_pattern; - char *fq_fmt; - - pcre *re; -}; - struct resp_ctx { struct tevent_context *ev; struct tevent_fd *lfde; diff --git a/server/responder/common/responder_common.c b/server/responder/common/responder_common.c index 52be7806..825d77e7 100644 --- a/server/responder/common/responder_common.c +++ b/server/responder/common/responder_common.c @@ -43,8 +43,6 @@ #include "monitor/monitor_interfaces.h" #include "sbus/sbus_client.h" -#define NAMES_CONFIG "config/names" - static void set_nonblocking(int fd) { unsigned v; @@ -453,62 +451,6 @@ failed: return EIO; } -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, - "(?<name>[^@]+)@?(?<domain>[^@]*$)"); - 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, @@ -565,7 +507,7 @@ int sss_process_init(TALLOC_CTX *mem_ctx, return ret; } - ret = sss_names_init(rctx); + ret = sss_names_init(rctx, rctx->cdb, &rctx->names); if (ret != EOK) { DEBUG(0, ("fatal error initializing regex data\n")); return ret; @@ -584,57 +526,3 @@ int sss_process_init(TALLOC_CTX *mem_ctx, return EOK; } -int sss_parse_name(TALLOC_CTX *memctx, - 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; - - 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; - } - - if (ret == 0) { - DEBUG(1, ("Too many matches, the pattern is invalid.\n")); - } - - 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/util/usertools.c b/server/util/usertools.c index 07930613..220b52cb 100644 --- a/server/util/usertools.c +++ b/server/util/usertools.c @@ -20,8 +20,15 @@ */ #include <pwd.h> +#include <pcre.h> +#include <errno.h> #include <talloc.h> +#include "confdb/confdb.h" +#include "util/util.h" + +#define NAMES_CONFIG "config/names" + char *get_username_from_uid(TALLOC_CTX *mem_ctx, uid_t uid) { char *username; @@ -33,3 +40,114 @@ char *get_username_from_uid(TALLOC_CTX *mem_ctx, uid_t uid) username = talloc_strdup(mem_ctx, pwd->pw_name); return username; } + +int sss_names_init(TALLOC_CTX *mem_ctx, struct confdb_ctx *cdb, struct sss_names_ctx **out) +{ + struct sss_names_ctx *ctx = NULL; + const char *errstr; + int errval; + int errpos; + int ret; + + ctx = talloc_zero(ctx, struct sss_names_ctx); + if (!ctx) return ENOMEM; + + ret = confdb_get_string(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, + "(?<name>[^@]+)@?(?<domain>[^@]*$)"); + if (!ctx->re_pattern) { + ret = ENOMEM; + goto done; + } + } + + ret = confdb_get_string(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; + } + + *out = ctx; + ret = EOK; + +done: + if (ret != EOK) { + talloc_free(ctx); + } + return ret; +} + +int sss_parse_name(TALLOC_CTX *memctx, + 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; + + 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; + } + + if (ret == 0) { + DEBUG(1, ("Too many matches, the pattern is invalid.\n")); + } + + 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/util/util.h b/server/util/util.h index 82c0be22..c4e3e49d 100644 --- a/server/util/util.h +++ b/server/util/util.h @@ -9,6 +9,7 @@ #include <errno.h> #include <limits.h> #include <time.h> +#include <pcre.h> #include "config.h" #include "talloc.h" #include "tevent.h" @@ -144,4 +145,19 @@ int password_destructor(void *memctx); /* from usertools.c */ char *get_username_from_uid(TALLOC_CTX *mem_ctx, uid_t uid); +struct sss_names_ctx { + char *re_pattern; + char *fq_fmt; + + pcre *re; +}; + +int sss_names_init(TALLOC_CTX *mem_ctx, + struct confdb_ctx *cdb, + struct sss_names_ctx **out); + +int sss_parse_name(TALLOC_CTX *memctx, + struct sss_names_ctx *snctx, + const char *orig, char **domain, char **name); + #endif /* __SSSD_UTIL_H__ */ |