sss_tc_utf8.c revision de5fa34860886ad68fba5e739987e16c342e8f14
50c04f297d76a57ead2fa6b73845f7563b1fc788sf/*
50c04f297d76a57ead2fa6b73845f7563b1fc788sf Authors:
50c04f297d76a57ead2fa6b73845f7563b1fc788sf Jakub Hrozek <jhrozek@redhat.com>
50c04f297d76a57ead2fa6b73845f7563b1fc788sf
50c04f297d76a57ead2fa6b73845f7563b1fc788sf Copyright (C) 2011 Red Hat
50c04f297d76a57ead2fa6b73845f7563b1fc788sf
50c04f297d76a57ead2fa6b73845f7563b1fc788sf This program is free software; you can redistribute it and/or modify
50c04f297d76a57ead2fa6b73845f7563b1fc788sf it under the terms of the GNU General Public License as published by
50c04f297d76a57ead2fa6b73845f7563b1fc788sf the Free Software Foundation; either version 3 of the License, or
50c04f297d76a57ead2fa6b73845f7563b1fc788sf (at your option) any later version.
50c04f297d76a57ead2fa6b73845f7563b1fc788sf
50c04f297d76a57ead2fa6b73845f7563b1fc788sf This program is distributed in the hope that it will be useful,
50c04f297d76a57ead2fa6b73845f7563b1fc788sf but WITHOUT ANY WARRANTY; without even the implied warranty of
50c04f297d76a57ead2fa6b73845f7563b1fc788sf MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
50c04f297d76a57ead2fa6b73845f7563b1fc788sf GNU General Public License for more details.
50c04f297d76a57ead2fa6b73845f7563b1fc788sf
50c04f297d76a57ead2fa6b73845f7563b1fc788sf You should have received a copy of the GNU General Public License
50c04f297d76a57ead2fa6b73845f7563b1fc788sf along with this program. If not, see <http://www.gnu.org/licenses/>.
50c04f297d76a57ead2fa6b73845f7563b1fc788sf*/
50c04f297d76a57ead2fa6b73845f7563b1fc788sf
50c04f297d76a57ead2fa6b73845f7563b1fc788sf#include <talloc.h>
50c04f297d76a57ead2fa6b73845f7563b1fc788sf#include "util/util.h"
50c04f297d76a57ead2fa6b73845f7563b1fc788sf#include "util/sss_utf8.h"
50c04f297d76a57ead2fa6b73845f7563b1fc788sf
50c04f297d76a57ead2fa6b73845f7563b1fc788sfchar *
50c04f297d76a57ead2fa6b73845f7563b1fc788sfsss_tc_utf8_str_tolower(TALLOC_CTX *mem_ctx, const char *s)
50c04f297d76a57ead2fa6b73845f7563b1fc788sf{
50c04f297d76a57ead2fa6b73845f7563b1fc788sf size_t nlen;
50c04f297d76a57ead2fa6b73845f7563b1fc788sf uint8_t *ret;
50c04f297d76a57ead2fa6b73845f7563b1fc788sf
50c04f297d76a57ead2fa6b73845f7563b1fc788sf ret = sss_tc_utf8_tolower(mem_ctx, (const uint8_t *) s, strlen(s), &nlen);
1538dfa9300693372892a358260e1dcdbf1138eapoirier if (!ret) return NULL;
50c04f297d76a57ead2fa6b73845f7563b1fc788sf
50c04f297d76a57ead2fa6b73845f7563b1fc788sf ret = talloc_realloc(mem_ctx, ret, uint8_t, nlen+1);
50c04f297d76a57ead2fa6b73845f7563b1fc788sf if (!ret) return NULL;
bed3c2e56e8f3328e780200466b9d009093db468sf
50c04f297d76a57ead2fa6b73845f7563b1fc788sf ret[nlen] = '\0';
bed3c2e56e8f3328e780200466b9d009093db468sf return (char *) ret;
50c04f297d76a57ead2fa6b73845f7563b1fc788sf}
bed3c2e56e8f3328e780200466b9d009093db468sf
50c04f297d76a57ead2fa6b73845f7563b1fc788sfuint8_t *
50c04f297d76a57ead2fa6b73845f7563b1fc788sfsss_tc_utf8_tolower(TALLOC_CTX *mem_ctx, const uint8_t *s, size_t len, size_t *_nlen)
50c04f297d76a57ead2fa6b73845f7563b1fc788sf{
50c04f297d76a57ead2fa6b73845f7563b1fc788sf uint8_t *lower;
50c04f297d76a57ead2fa6b73845f7563b1fc788sf uint8_t *ret;
50c04f297d76a57ead2fa6b73845f7563b1fc788sf size_t nlen;
50c04f297d76a57ead2fa6b73845f7563b1fc788sf
50c04f297d76a57ead2fa6b73845f7563b1fc788sf lower = sss_utf8_tolower(s, len, &nlen);
50c04f297d76a57ead2fa6b73845f7563b1fc788sf if (!lower) return NULL;
50c04f297d76a57ead2fa6b73845f7563b1fc788sf
50c04f297d76a57ead2fa6b73845f7563b1fc788sf ret = talloc_memdup(mem_ctx, lower, nlen);
50c04f297d76a57ead2fa6b73845f7563b1fc788sf sss_utf8_free(lower);
bed3c2e56e8f3328e780200466b9d009093db468sf if (!ret) return NULL;
50c04f297d76a57ead2fa6b73845f7563b1fc788sf
50c04f297d76a57ead2fa6b73845f7563b1fc788sf *_nlen = nlen;
50c04f297d76a57ead2fa6b73845f7563b1fc788sf return ret;
50c04f297d76a57ead2fa6b73845f7563b1fc788sf}
50c04f297d76a57ead2fa6b73845f7563b1fc788sf
50c04f297d76a57ead2fa6b73845f7563b1fc788sferrno_t sss_filter_sanitize_for_dom(TALLOC_CTX *mem_ctx,
50c04f297d76a57ead2fa6b73845f7563b1fc788sf const char *input,
50c04f297d76a57ead2fa6b73845f7563b1fc788sf struct sss_domain_info *dom,
50c04f297d76a57ead2fa6b73845f7563b1fc788sf char **sanitized,
bed3c2e56e8f3328e780200466b9d009093db468sf char **lc_sanitized)
bed3c2e56e8f3328e780200466b9d009093db468sf{
50c04f297d76a57ead2fa6b73845f7563b1fc788sf int ret;
50c04f297d76a57ead2fa6b73845f7563b1fc788sf
50c04f297d76a57ead2fa6b73845f7563b1fc788sf ret = sss_filter_sanitize(mem_ctx, input, sanitized);
50c04f297d76a57ead2fa6b73845f7563b1fc788sf if (ret != EOK) {
50c04f297d76a57ead2fa6b73845f7563b1fc788sf DEBUG(SSSDBG_OP_FAILURE, "sss_filter_sanitize failed.\n");
50c04f297d76a57ead2fa6b73845f7563b1fc788sf return ret;
50c04f297d76a57ead2fa6b73845f7563b1fc788sf }
50c04f297d76a57ead2fa6b73845f7563b1fc788sf
50c04f297d76a57ead2fa6b73845f7563b1fc788sf if (dom->case_sensitive) {
50c04f297d76a57ead2fa6b73845f7563b1fc788sf *lc_sanitized = talloc_strdup(mem_ctx, *sanitized);
bed3c2e56e8f3328e780200466b9d009093db468sf } else {
bed3c2e56e8f3328e780200466b9d009093db468sf *lc_sanitized = sss_tc_utf8_str_tolower(mem_ctx, *sanitized);
bed3c2e56e8f3328e780200466b9d009093db468sf }
bed3c2e56e8f3328e780200466b9d009093db468sf
bed3c2e56e8f3328e780200466b9d009093db468sf if (*lc_sanitized == NULL) {
50c04f297d76a57ead2fa6b73845f7563b1fc788sf DEBUG(SSSDBG_OP_FAILURE, "%s failed.\n",
50c04f297d76a57ead2fa6b73845f7563b1fc788sf dom->case_sensitive ?
50c04f297d76a57ead2fa6b73845f7563b1fc788sf "talloc_strdup" :
50c04f297d76a57ead2fa6b73845f7563b1fc788sf "sss_tc_utf8_str_tolower");
50c04f297d76a57ead2fa6b73845f7563b1fc788sf return ENOMEM;
50c04f297d76a57ead2fa6b73845f7563b1fc788sf }
50c04f297d76a57ead2fa6b73845f7563b1fc788sf
50c04f297d76a57ead2fa6b73845f7563b1fc788sf return EOK;
50c04f297d76a57ead2fa6b73845f7563b1fc788sf}
9c1260efa52c82c2a58e5b5f20cd6902563d95f5rbowen