fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor/*
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor SSSD
fd9abdda70912b99b24e3bf1a38f26fde908a74cnd
fd9abdda70912b99b24e3bf1a38f26fde908a74cnd Utility functions related to ID information
fd9abdda70912b99b24e3bf1a38f26fde908a74cnd
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor Copyright (C) Jan Zeleny <jzeleny@redhat.com> 2012
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor This program is free software; you can redistribute it and/or modify
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor it under the terms of the GNU General Public License as published by
96ad5d81ee4a2cc66a4ae19893efc8aa6d06fae7jailletc the Free Software Foundation; either version 3 of the License, or
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor (at your option) any later version.
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen This program is distributed in the hope that it will be useful,
2e545ce2450a9953665f701bb05350f0d3f26275nd but WITHOUT ANY WARRANTY; without even the implied warranty of
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
d29d9ab4614ff992b0e8de6e2b88d52b6f1f153erbowen GNU General Public License for more details.
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor You should have received a copy of the GNU General Public License
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor along with this program. If not, see <http://www.gnu.org/licenses/>.
af33a4994ae2ff15bc67d19ff1a7feb906745bf8rbowen*/
3f08db06526d6901aa08c110b5bc7dde6bc39905nd
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor#include "util/util.h"
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor#include "util/sss_nss.h"
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor
3f08db06526d6901aa08c110b5bc7dde6bc39905ndchar *expand_homedir_template(TALLOC_CTX *mem_ctx,
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor const char *template,
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor bool case_sensitive,
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor struct sss_nss_homedir_ctx *homedir_ctx)
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor{
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor char *copy;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor char *p;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor char *n;
f086b4b402fa9a2fefc7dda85de2a3cc1cd0a654rjung char *result = NULL;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor char *res = NULL;
0a05fab9aadd37834734ffe106fc8ad4488fb3e3rbowen TALLOC_CTX *tmp_ctx = NULL;
0a05fab9aadd37834734ffe106fc8ad4488fb3e3rbowen const char *orig = NULL;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor char *username = NULL;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor if (template == NULL) {
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor DEBUG(SSSDBG_CRIT_FAILURE, "Missing template.\n");
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor return NULL;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor }
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor if (homedir_ctx == NULL) {
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor DEBUG(SSSDBG_CRIT_FAILURE, "Missing home directory data.\n");
cd8142d105a47b208d863bea7cdb540727a5f02digalic return NULL;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor }
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor tmp_ctx = talloc_new(NULL);
cd8142d105a47b208d863bea7cdb540727a5f02digalic if (!tmp_ctx) return NULL;
cd8142d105a47b208d863bea7cdb540727a5f02digalic
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor copy = talloc_strdup(tmp_ctx, template);
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor if (copy == NULL) {
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor DEBUG(SSSDBG_CRIT_FAILURE, "talloc_strdup failed.\n");
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor goto done;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor }
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor result = talloc_strdup(tmp_ctx, "");
4aa603e6448b99f9371397d439795c91a93637eand if (result == NULL) {
4aa603e6448b99f9371397d439795c91a93637eand DEBUG(SSSDBG_CRIT_FAILURE, "talloc_strdup failed.\n");
d5d58e8346ef3553dfd7d5f51078a9b268729b3frbowen goto done;
f4cbda69df0490c6deaacb8d04f103d200ddd183nd }
f4cbda69df0490c6deaacb8d04f103d200ddd183nd
f4cbda69df0490c6deaacb8d04f103d200ddd183nd p = copy;
f4cbda69df0490c6deaacb8d04f103d200ddd183nd while ( (n = strchr(p, '%')) != NULL) {
f4cbda69df0490c6deaacb8d04f103d200ddd183nd *n = '\0';
f4cbda69df0490c6deaacb8d04f103d200ddd183nd n++;
f4cbda69df0490c6deaacb8d04f103d200ddd183nd if ( *n == '\0' ) {
f4cbda69df0490c6deaacb8d04f103d200ddd183nd DEBUG(SSSDBG_CRIT_FAILURE, "format error, single %% at the end of "
f4cbda69df0490c6deaacb8d04f103d200ddd183nd "the template.\n");
f4cbda69df0490c6deaacb8d04f103d200ddd183nd goto done;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor }
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor switch( *n ) {
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor case 'u':
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor if (homedir_ctx->username == NULL) {
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor DEBUG(SSSDBG_CRIT_FAILURE,
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor "Cannot expand user name template because user name "
1e8959e8dfe29a452d93cb8c5dfdf4686f522fc1nd "is empty.\n");
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor goto done;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor }
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor username = sss_output_name(tmp_ctx, homedir_ctx->username,
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor case_sensitive, 0);
48c64aeceef385e19025b384bd719b2a9789592dnd if (username == NULL) {
48c64aeceef385e19025b384bd719b2a9789592dnd goto done;
30471a4650391f57975f60bbb6e4a90be7b284bfhumbedooh }
1f1b6bf13313fdd14a45e52e553d3ff28689b717coar
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor result = talloc_asprintf_append(result, "%s%s", p, username);
e3e52ec6993de04bb0100e098ce0a569c7001382lgentis talloc_free(username);
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor break;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor case 'U':
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor if (homedir_ctx->uid == 0) {
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor DEBUG(SSSDBG_CRIT_FAILURE, "Cannot expand uid template "
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor "because uid is invalid.\n");
fed47023e9be04c612b5f6d4a5ee2b8e7c587181rbowen goto done;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor }
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor result = talloc_asprintf_append(result, "%s%d", p,
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor homedir_ctx->uid);
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor break;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor case 'd':
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor if (homedir_ctx->domain == NULL) {
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor DEBUG(SSSDBG_CRIT_FAILURE, "Cannot expand domain name "
4aa603e6448b99f9371397d439795c91a93637eand "template because domain name "
4aa603e6448b99f9371397d439795c91a93637eand "is empty.\n");
d5d58e8346ef3553dfd7d5f51078a9b268729b3frbowen goto done;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor }
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor result = talloc_asprintf_append(result, "%s%s", p,
4aa603e6448b99f9371397d439795c91a93637eand homedir_ctx->domain);
d5d58e8346ef3553dfd7d5f51078a9b268729b3frbowen break;
4aa603e6448b99f9371397d439795c91a93637eand
d5d58e8346ef3553dfd7d5f51078a9b268729b3frbowen case 'f':
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor if (homedir_ctx->domain == NULL
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor || homedir_ctx->username == NULL) {
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor DEBUG(SSSDBG_CRIT_FAILURE, "Cannot expand fully qualified "
e3e52ec6993de04bb0100e098ce0a569c7001382lgentis "name template because domain "
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor "or user name is empty.\n");
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor goto done;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor }
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor username = sss_output_name(tmp_ctx, homedir_ctx->username,
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor case_sensitive, 0);
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor if (username == NULL) {
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor goto done;
fed47023e9be04c612b5f6d4a5ee2b8e7c587181rbowen }
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor result = talloc_asprintf_append(result, "%s%s@%s", p,
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor username, homedir_ctx->domain);
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor talloc_free(username);
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor break;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor case 'o':
4aa603e6448b99f9371397d439795c91a93637eand if (homedir_ctx->original == NULL) {
4aa603e6448b99f9371397d439795c91a93637eand DEBUG(SSSDBG_CRIT_FAILURE,
d5d58e8346ef3553dfd7d5f51078a9b268729b3frbowen "Original home directory for %s is not available, "
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor "using empty string\n", homedir_ctx->username);
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor orig = "";
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor } else {
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor orig = homedir_ctx->original;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor }
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor result = talloc_asprintf_append(result, "%s%s", p, orig);
4aa603e6448b99f9371397d439795c91a93637eand break;
4aa603e6448b99f9371397d439795c91a93637eand
d5d58e8346ef3553dfd7d5f51078a9b268729b3frbowen case 'F':
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor if (homedir_ctx->flatname == NULL) {
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor DEBUG(SSSDBG_CRIT_FAILURE, "Cannot expand domain name "
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor "template because domain flat "
e3e52ec6993de04bb0100e098ce0a569c7001382lgentis "name is empty.\n");
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor goto done;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor }
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor result = talloc_asprintf_append(result, "%s%s", p,
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor homedir_ctx->flatname);
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor break;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor case 'H':
fed47023e9be04c612b5f6d4a5ee2b8e7c587181rbowen if (homedir_ctx->config_homedir_substr == NULL) {
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor DEBUG(SSSDBG_CRIT_FAILURE,
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor "Cannot expand home directory substring template "
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor "substring is empty.\n");
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor goto done;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor }
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor result = talloc_asprintf_append(result, "%s%s", p,
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor homedir_ctx->config_homedir_substr);
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor break;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor case 'P':
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor if (homedir_ctx->upn == NULL) {
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor DEBUG(SSSDBG_CRIT_FAILURE,
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor "Cannot expand user principal name template "
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor "string is empty.\n");
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor goto done;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor }
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor result = talloc_asprintf_append(result, "%s%s", p,
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor homedir_ctx->upn);
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor break;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor case '%':
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor result = talloc_asprintf_append(result, "%s%%", p);
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor break;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor default:
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor DEBUG(SSSDBG_CRIT_FAILURE, "format error, unknown template "
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor "[%%%c].\n", *n);
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor goto done;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor }
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor if (result == NULL) {
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf_append failed.\n");
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor goto done;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor }
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor p = n + 1;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor }
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor result = talloc_asprintf_append(result, "%s", p);
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor if (result == NULL) {
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor DEBUG(SSSDBG_CRIT_FAILURE, "talloc_asprintf_append failed.\n");
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor goto done;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor }
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor res = talloc_move(mem_ctx, &result);
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzordone:
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor talloc_zfree(tmp_ctx);
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor return res;
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor}
fbad7185dd78ec6e09c5b191693deda9d4bfa08cgryzor