From f54b271376b23cb968eafb9ffd5100c6dadad2a7 Mon Sep 17 00:00:00 2001 From: Jan Cholasta Date: Fri, 26 Apr 2013 09:40:53 +0200 Subject: [PATCH 3/6] UTIL: Add function sss_names_init_from_args This function allows initializing sss_names_ctx using a regular expression and fully qualified format string specified in its arguments. --- src/util/usertools.c | 107 +++++++++++++++++++++++++++++++-------------------- src/util/util.h | 7 ++++ 2 files changed, 73 insertions(+), 41 deletions(-) diff --git a/src/util/usertools.c b/src/util/usertools.c index 7323d9f8260580f32b4ab55c8c2db5bd7eec20ed..91110f263657de9ba53ed305e7c4710eb006bec6 100644 --- a/src/util/usertools.c +++ b/src/util/usertools.c @@ -135,13 +135,11 @@ done: #endif } -int sss_names_init(TALLOC_CTX *mem_ctx, struct confdb_ctx *cdb, - const char *domain, struct sss_names_ctx **out) +int sss_names_init_from_args(TALLOC_CTX *mem_ctx, const char *re_pattern, + const char *fq_fmt, struct sss_names_ctx **out) { struct sss_names_ctx *ctx; - TALLOC_CTX *tmpctx = NULL; const char *errstr; - char *conf_path; int errval; int errpos; int ret; @@ -150,6 +148,49 @@ int sss_names_init(TALLOC_CTX *mem_ctx, struct confdb_ctx *cdb, if (!ctx) return ENOMEM; talloc_set_destructor(ctx, sss_names_ctx_destructor); + ctx->re_pattern = talloc_strdup(ctx, re_pattern); + if (ctx->re_pattern == NULL) { + ret = ENOMEM; + goto done; + } + + DEBUG(SSSDBG_CONF_SETTINGS, ("Using re [%s].\n", ctx->re_pattern)); + + ctx->fq_fmt = talloc_strdup(ctx, fq_fmt); + if (ctx->fq_fmt == NULL) { + ret = ENOMEM; + goto done; + } + + ctx->re = pcre_compile2(ctx->re_pattern, + NAME_DOMAIN_PATTERN_OPTIONS, + &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_names_init(TALLOC_CTX *mem_ctx, struct confdb_ctx *cdb, + const char *domain, struct sss_names_ctx **out) +{ + TALLOC_CTX *tmpctx = NULL; + char *conf_path; + char *re_pattern; + char *fq_fmt; + int ret; + tmpctx = talloc_new(NULL); if (tmpctx == NULL) { ret = ENOMEM; @@ -162,19 +203,19 @@ int sss_names_init(TALLOC_CTX *mem_ctx, struct confdb_ctx *cdb, goto done; } - ret = confdb_get_string(cdb, ctx, conf_path, - CONFDB_NAME_REGEX, NULL, &ctx->re_pattern); + ret = confdb_get_string(cdb, tmpctx, conf_path, + CONFDB_NAME_REGEX, NULL, &re_pattern); if (ret != EOK) goto done; /* If not found in the domain, look in globals */ - if (ctx->re_pattern == NULL) { - ret = confdb_get_string(cdb, ctx, CONFDB_MONITOR_CONF_ENTRY, - CONFDB_NAME_REGEX, NULL, &ctx->re_pattern); + if (re_pattern == NULL) { + ret = confdb_get_string(cdb, tmpctx, CONFDB_MONITOR_CONF_ENTRY, + CONFDB_NAME_REGEX, NULL, &re_pattern); if (ret != EOK) goto done; } - if (ctx->re_pattern == NULL) { - ret = get_id_provider_default_re(ctx, cdb, conf_path, &ctx->re_pattern); + if (re_pattern == NULL) { + ret = get_id_provider_default_re(tmpctx, cdb, conf_path, &re_pattern); if (ret != EOK) { DEBUG(SSSDBG_OP_FAILURE, ("Failed to get provider default regular " \ "expression for domain [%s].\n", domain)); @@ -182,10 +223,10 @@ int sss_names_init(TALLOC_CTX *mem_ctx, struct confdb_ctx *cdb, } } - if (!ctx->re_pattern) { - ctx->re_pattern = talloc_strdup(ctx, - "(?P[^@]+)@?(?P[^@]*$)"); - if (!ctx->re_pattern) { + if (!re_pattern) { + re_pattern = talloc_strdup(tmpctx, + "(?P[^@]+)@?(?P[^@]*$)"); + if (!re_pattern) { ret = ENOMEM; goto done; } @@ -195,49 +236,33 @@ int sss_names_init(TALLOC_CTX *mem_ctx, struct confdb_ctx *cdb, "not support non-unique named subpatterns.\n")); DEBUG(2, ("Please make sure that your pattern [%s] only contains " "subpatterns with a unique name and uses " - "the Python syntax (?P).\n", ctx->re_pattern)); + "the Python syntax (?P).\n", re_pattern)); #endif } - DEBUG(SSSDBG_CONF_SETTINGS, ("Using re [%s].\n", ctx->re_pattern)); - - ret = confdb_get_string(cdb, ctx, conf_path, - CONFDB_FULL_NAME_FORMAT, NULL, &ctx->fq_fmt); + ret = confdb_get_string(cdb, tmpctx, conf_path, + CONFDB_FULL_NAME_FORMAT, NULL, &fq_fmt); if (ret != EOK) goto done; /* If not found in the domain, look in globals */ - if (ctx->fq_fmt == NULL) { - ret = confdb_get_string(cdb, ctx, CONFDB_MONITOR_CONF_ENTRY, - CONFDB_FULL_NAME_FORMAT, NULL, &ctx->fq_fmt); + if (fq_fmt == NULL) { + ret = confdb_get_string(cdb, tmpctx, CONFDB_MONITOR_CONF_ENTRY, + CONFDB_FULL_NAME_FORMAT, NULL, &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) { + if (!fq_fmt) { + fq_fmt = talloc_strdup(tmpctx, "%1$s@%2$s"); + if (!fq_fmt) { ret = ENOMEM; goto done; } } - ctx->re = pcre_compile2(ctx->re_pattern, - NAME_DOMAIN_PATTERN_OPTIONS, - &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; + ret = sss_names_init_from_args(mem_ctx, re_pattern, fq_fmt, out); done: talloc_free(tmpctx); - if (ret != EOK) { - talloc_free(ctx); - } return ret; } diff --git a/src/util/util.h b/src/util/util.h index 33725f63591a4d165a084c1fd361f9651e80e50b..49dc850c36cedd83755034367357fab41bd32ec6 100644 --- a/src/util/util.h +++ b/src/util/util.h @@ -401,6 +401,13 @@ struct sss_names_ctx { pcre *re; }; +/* initialize sss_names_ctx directly from arguments */ +int sss_names_init_from_args(TALLOC_CTX *mem_ctx, + const char *re_pattern, + const char *fq_fmt, + struct sss_names_ctx **out); + +/* initialize sss_names_ctx from domain configuration */ int sss_names_init(TALLOC_CTX *mem_ctx, struct confdb_ctx *cdb, const char *domain, -- 1.8.2.1