usertools.c revision 62def404cb14e02d2903c68fb730c5281ad902fe
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder/*
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder SSSD
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder User tools
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder Copyright (C) Stephen Gallagher <sgallagh@redhat.com> 2009
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder This program is free software; you can redistribute it and/or modify
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder it under the terms of the GNU General Public License as published by
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder the Free Software Foundation; either version 3 of the License, or
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder (at your option) any later version.
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder This program is distributed in the hope that it will be useful,
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder but WITHOUT ANY WARRANTY; without even the implied warranty of
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder GNU General Public License for more details.
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder You should have received a copy of the GNU General Public License
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder along with this program. If not, see <http://www.gnu.org/licenses/>.
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder*/
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder#include <pwd.h>
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder#include <pcre.h>
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder#include <errno.h>
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder#include <talloc.h>
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder#include "confdb/confdb.h"
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder#include "util/util.h"
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder#ifdef HAVE_LIBPCRE_LESSER_THAN_7
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder#define NAME_DOMAIN_PATTERN_OPTIONS (PCRE_EXTENDED)
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder#else
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder#define NAME_DOMAIN_PATTERN_OPTIONS (PCRE_DUPNAMES | PCRE_EXTENDED)
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder#endif
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maederchar *get_username_from_uid(TALLOC_CTX *mem_ctx, uid_t uid)
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder{
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder char *username;
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder struct passwd *pwd;
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder pwd = getpwuid(uid);
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder if (!pwd) return NULL;
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder username = talloc_strdup(mem_ctx, pwd->pw_name);
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder return username;
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder}
06f58a67e6df999858bf4f97d5e0786956562d29Christian Maeder
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maederstatic int sss_names_ctx_destructor(struct sss_names_ctx *snctx)
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder{
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder if (snctx->re) {
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder pcre_free(snctx->re);
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder snctx->re = NULL;
d4aed7a2eea6b546c0d9520d85038addb7beb12fChristian Maeder }
d4aed7a2eea6b546c0d9520d85038addb7beb12fChristian Maeder return 0;
d4aed7a2eea6b546c0d9520d85038addb7beb12fChristian Maeder}
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maederint sss_names_init(TALLOC_CTX *mem_ctx, struct confdb_ctx *cdb, struct sss_names_ctx **out)
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder{
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder struct sss_names_ctx *ctx;
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder const char *errstr;
d4aed7a2eea6b546c0d9520d85038addb7beb12fChristian Maeder int errval;
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder int errpos;
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder int ret;
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder ctx = talloc_zero(mem_ctx, struct sss_names_ctx);
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder if (!ctx) return ENOMEM;
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder talloc_set_destructor(ctx, sss_names_ctx_destructor);
bb2c1beb7ab66a49627a2a34df80864a3c65cc83Christian Maeder
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder ret = confdb_get_string(cdb, ctx, CONFDB_MONITOR_CONF_ENTRY,
bb2c1beb7ab66a49627a2a34df80864a3c65cc83Christian Maeder CONFDB_MONITOR_NAME_REGEX, NULL, &ctx->re_pattern);
bb2c1beb7ab66a49627a2a34df80864a3c65cc83Christian Maeder if (ret != EOK) goto done;
bb2c1beb7ab66a49627a2a34df80864a3c65cc83Christian Maeder
bb2c1beb7ab66a49627a2a34df80864a3c65cc83Christian Maeder if (!ctx->re_pattern) {
bb2c1beb7ab66a49627a2a34df80864a3c65cc83Christian Maeder ctx->re_pattern = talloc_strdup(ctx,
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder "(?P<name>[^@]+)@?(?P<domain>[^@]*$)");
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder if (!ctx->re_pattern) {
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder ret = ENOMEM;
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder goto done;
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder }
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder#ifdef HAVE_LIBPCRE_LESSER_THAN_7
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder } else {
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder DEBUG(2, ("This binary was build with a version of libpcre that does "
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder "not support non-unique named subpatterns.\n"));
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder DEBUG(2, ("Please make sure that your pattern [%s] only contains "
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder "subpatterns with a unique name and uses "
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder "the Python syntax (?P<name>).\n", ctx->re_pattern));
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder#endif
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder }
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder ret = confdb_get_string(cdb, ctx, CONFDB_MONITOR_CONF_ENTRY,
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder CONFDB_MONITOR_FULL_NAME_FORMAT, NULL, &ctx->fq_fmt);
d4aed7a2eea6b546c0d9520d85038addb7beb12fChristian Maeder if (ret != EOK) goto done;
d4aed7a2eea6b546c0d9520d85038addb7beb12fChristian Maeder
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder if (!ctx->fq_fmt) {
d4aed7a2eea6b546c0d9520d85038addb7beb12fChristian Maeder ctx->fq_fmt = talloc_strdup(ctx, "%1$s@%2$s");
d4aed7a2eea6b546c0d9520d85038addb7beb12fChristian Maeder if (!ctx->fq_fmt) {
d4aed7a2eea6b546c0d9520d85038addb7beb12fChristian Maeder ret = ENOMEM;
d4aed7a2eea6b546c0d9520d85038addb7beb12fChristian Maeder goto done;
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder }
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder }
38824a7dba4f7d82532afec67e0b594a5af5d76bChristian Maeder
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder ctx->re = pcre_compile2(ctx->re_pattern,
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder NAME_DOMAIN_PATTERN_OPTIONS,
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder &errval, &errstr, &errpos, NULL);
a1a48072301767054f2a9ff7ccf8974b0d6a6a28Christian Maeder if (!ctx->re) {
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder DEBUG(1, ("Invalid Regular Expression pattern at position %d."
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder " (Error: %d [%s])\n", errpos, errval, errstr));
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder ret = EFAULT;
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder goto done;
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder }
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder *out = ctx;
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder ret = EOK;
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maeder
5b3e0bbb6a776c60dc14113435a44e7b13d2fa01Christian Maederdone:
36a493b7eec0f9d719674296c26afe7fd9bfe327Christian Maeder if (ret != EOK) {
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder talloc_free(ctx);
7fe976d9f9c4af1aa7636c568d9919859523de0aChristian Maeder }
38f35f2c4a3b6a778f4f68e7af047a174e93abbeChristian Maeder return ret;
38f35f2c4a3b6a778f4f68e7af047a174e93abbeChristian Maeder}
13140d161d2d2d11d87283d01d57ee3a738a833dChristian Maeder
38f35f2c4a3b6a778f4f68e7af047a174e93abbeChristian Maederint sss_parse_name(TALLOC_CTX *memctx,
38f35f2c4a3b6a778f4f68e7af047a174e93abbeChristian Maeder struct sss_names_ctx *snctx,
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder const char *orig, char **domain, char **name)
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder{
38f35f2c4a3b6a778f4f68e7af047a174e93abbeChristian Maeder pcre *re = snctx->re;
38f35f2c4a3b6a778f4f68e7af047a174e93abbeChristian Maeder const char *result;
38f35f2c4a3b6a778f4f68e7af047a174e93abbeChristian Maeder int ovec[30];
38f35f2c4a3b6a778f4f68e7af047a174e93abbeChristian Maeder int origlen;
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder int ret, strnum;
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder origlen = strlen(orig);
38f35f2c4a3b6a778f4f68e7af047a174e93abbeChristian Maeder
13140d161d2d2d11d87283d01d57ee3a738a833dChristian Maeder ret = pcre_exec(re, NULL, orig, origlen, 0, PCRE_NOTEMPTY, ovec, 30);
13140d161d2d2d11d87283d01d57ee3a738a833dChristian Maeder if (ret < 0) {
13140d161d2d2d11d87283d01d57ee3a738a833dChristian Maeder DEBUG(2, ("PCRE Matching error, %d\n", ret));
13140d161d2d2d11d87283d01d57ee3a738a833dChristian Maeder return EINVAL;
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder }
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder if (ret == 0) {
0c92a39a4adf3c1cbe173e3b16c65c159a1ce612Christian Maeder DEBUG(1, ("Too many matches, the pattern is invalid.\n"));
13140d161d2d2d11d87283d01d57ee3a738a833dChristian Maeder }
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;
}
char *
sss_get_cased_name(TALLOC_CTX *mem_ctx,
const char *orig_name,
bool case_sensitive)
{
return case_sensitive ? talloc_strdup(mem_ctx, orig_name) :
sss_tc_utf8_str_tolower(mem_ctx, orig_name);
}
errno_t
sss_get_cased_name_list(TALLOC_CTX *mem_ctx, const char * const *orig,
bool case_sensitive, const char ***_cased)
{
const char **out;
size_t num, i;
if (orig == NULL) {
*_cased = NULL;
return EOK;
}
for (num=0; orig[num]; num++); /* count the num of strings */
if (num == 0) {
*_cased = NULL;
return EOK;
}
out = talloc_array(mem_ctx, const char *, num + 1);
if (out == NULL) {
return ENOMEM;
}
for (i = 0; i < num; i++) {
out[i] = sss_get_cased_name(out, orig[i], case_sensitive);
if (out[i] == NULL) {
talloc_free(out);
return ENOMEM;
}
}
out[num] = NULL;
*_cased = out;
return EOK;
}