sysdb_autofs.c revision dd7192379e5fc5bb852863e60ad4b6a20c5da183
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek/*
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek Authors:
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek Jakub Hrozek <jhrozek@redhat.com>
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek Copyright (C) 2012 Red Hat
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek This program is free software; you can redistribute it and/or modify
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek it under the terms of the GNU General Public License as published by
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek the Free Software Foundation; either version 3 of the License, or
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek (at your option) any later version.
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek This program is distributed in the hope that it will be useful,
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek but WITHOUT ANY WARRANTY; without even the implied warranty of
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek GNU General Public License for more details.
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek You should have received a copy of the GNU General Public License
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek along with this program. If not, see <http://www.gnu.org/licenses/>.
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek*/
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#include <talloc.h>
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#include "db/sysdb.h"
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#include "db/sysdb_private.h"
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#include "db/sysdb_autofs.h"
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek#define SYSDB_TMPL_AUTOFS_ENTRY SYSDB_NAME"=%s,"SYSDB_TMPL_CUSTOM
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozekstatic struct ldb_dn *
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozeksysdb_autofsmap_dn(TALLOC_CTX *mem_ctx,
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek struct sysdb_ctx *sysdb,
5cd4414fce1e0eb4133dfc6fc828bf25c8a959f9Lukas Slebodnik const char *map_name)
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce{
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek return sysdb_custom_dn(sysdb, mem_ctx,
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek sysdb->domain, map_name, AUTOFS_MAP_SUBDIR);
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek}
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozekstatic struct ldb_dn *
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozeksysdb_autofsentry_dn(TALLOC_CTX *mem_ctx,
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek struct sysdb_ctx *sysdb,
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek const char *map_name,
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek const char *entry_name,
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek const char *entry_value)
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek{
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek errno_t ret;
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek TALLOC_CTX *tmp_ctx;
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek char *clean_name;
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov char *clean_value;
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov const char *rdn;
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov struct ldb_dn *dn = NULL;
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek tmp_ctx = talloc_new(NULL);
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek if (!tmp_ctx) {
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek return NULL;
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek }
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek ret = sysdb_dn_sanitize(tmp_ctx, entry_name, &clean_name);
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek if (ret != EOK) {
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek goto done;
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek }
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek ret = sysdb_dn_sanitize(tmp_ctx, entry_value, &clean_value);
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek if (ret != EOK) {
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek goto done;
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek }
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov rdn = talloc_asprintf(tmp_ctx, "%s%s", clean_name, clean_value);
83bf46f4066e3d5e838a32357c201de9bd6ecdfdNikolai Kondrashov if (!rdn) {
a3c8390d19593b1e5277d95bfb4ab206d4785150Nikolai Kondrashov goto done;
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek }
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek dn = ldb_dn_new_fmt(mem_ctx, sysdb->ldb, SYSDB_TMPL_AUTOFS_ENTRY,
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek rdn, map_name, AUTOFS_MAP_SUBDIR,
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek sysdb->domain->name);
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozek
3b08dec5ee634f83ee18e1753d5ffe0ac5e3c458Jakub Hrozekdone:
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce talloc_free(tmp_ctx);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce return dn;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce}
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorceerrno_t
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorcesysdb_save_autofsmap(struct sysdb_ctx *sysdb_ctx,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce const char *name,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce const char *autofsmapname,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce struct sysdb_attrs *attrs,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce int cache_timeout,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce time_t now)
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce{
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce errno_t ret;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce TALLOC_CTX *tmp_ctx;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce DEBUG(SSSDBG_TRACE_FUNC, ("Adding autofs map %s\n", autofsmapname));
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce tmp_ctx = talloc_new(NULL);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (!tmp_ctx) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce return ENOMEM;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (!attrs) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce attrs = sysdb_new_attrs(tmp_ctx);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (!attrs) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = ENOMEM;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto done;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = sysdb_attrs_add_string(attrs, SYSDB_OBJECTCLASS,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce SYSDB_AUTOFS_MAP_OC);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret != EOK) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce DEBUG(SSSDBG_OP_FAILURE, ("Could not set map object class [%d]: %s\n",
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret, strerror(ret)));
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto done;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = sysdb_attrs_add_string(attrs, SYSDB_AUTOFS_MAP_NAME, autofsmapname);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret != EOK) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce DEBUG(SSSDBG_OP_FAILURE, ("Could not set map name [%d]: %s\n",
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret, strerror(ret)));
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto done;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = sysdb_attrs_add_string(attrs, SYSDB_NAME, name);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret != EOK) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce DEBUG(SSSDBG_OP_FAILURE, ("Could not set name attribute [%d]: %s\n",
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret, strerror(ret)));
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto done;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = sysdb_attrs_add_time_t(attrs, SYSDB_LAST_UPDATE, now);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce DEBUG(SSSDBG_OP_FAILURE, ("Could not set sysdb lastUpdate [%d]: %s\n",
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret, strerror(ret)));
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto done;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = sysdb_attrs_add_time_t(attrs, SYSDB_CACHE_EXPIRE,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ((cache_timeout) ?
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce (now + cache_timeout) : 0));
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce DEBUG(SSSDBG_OP_FAILURE, ("Could not set sysdb cache expire [%d]: %s\n",
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret, strerror(ret)));
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto done;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = sysdb_store_custom(sysdb_ctx, sysdb_ctx->domain, name, AUTOFS_MAP_SUBDIR, attrs);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret != EOK) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce DEBUG(SSSDBG_OP_FAILURE, ("sysdb_store_custom failed [%d]: %s\n",
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret, strerror(ret)));
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto done;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = EOK;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorcedone:
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce talloc_free(tmp_ctx);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce return ret;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce}
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorceerrno_t
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorcesysdb_delete_autofsmap(struct sysdb_ctx *sysdb_ctx,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce const char *name)
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce{
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce DEBUG(SSSDBG_TRACE_FUNC, ("Deleting autofs map %s\n", name));
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce return sysdb_delete_custom(sysdb_ctx, name, AUTOFS_MAP_SUBDIR);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce}
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorceerrno_t
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorcesysdb_get_map_byname(TALLOC_CTX *mem_ctx,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce struct sysdb_ctx *sysdb,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce const char *map_name,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce struct ldb_message **_map)
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce{
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce errno_t ret;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce TALLOC_CTX *tmp_ctx;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce const char *filter;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce char *safe_map_name;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce size_t count;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce struct ldb_message **msgs;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce const char *attrs[] = { SYSDB_OBJECTCLASS,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce SYSDB_CACHE_EXPIRE,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce SYSDB_LAST_UPDATE,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce SYSDB_AUTOFS_MAP_NAME,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce SYSDB_MEMBER,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce NULL };
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce tmp_ctx = talloc_new(NULL);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (!tmp_ctx) return ENOMEM;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = sss_filter_sanitize(tmp_ctx, map_name, &safe_map_name);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret != EOK) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce DEBUG(SSSDBG_CRIT_FAILURE,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ("Cannot sanitize map [%s] error [%d]: %s\n",
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce map_name, ret, strerror(ret)));
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto done;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce filter = talloc_asprintf(tmp_ctx, "(&(objectclass=%s)(%s=%s))",
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce SYSDB_AUTOFS_MAP_OC, SYSDB_NAME, safe_map_name);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (!filter) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = ENOMEM;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto done;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = sysdb_search_custom(tmp_ctx, sysdb, filter,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce AUTOFS_MAP_SUBDIR, attrs,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce &count, &msgs);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret != EOK && ret != ENOENT) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce DEBUG(SSSDBG_CRIT_FAILURE,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ("Error looking up autofs map [%s]", safe_map_name));
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto done;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce } else if (ret == ENOENT) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce DEBUG(SSSDBG_TRACE_FUNC, ("No such map\n"));
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce *_map = NULL;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto done;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (count != 1) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce DEBUG(SSSDBG_CRIT_FAILURE,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ("More than one map named %s\n", safe_map_name));
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto done;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce *_map = talloc_steal(mem_ctx, msgs[0]);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = EOK;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorcedone:
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce talloc_free(tmp_ctx);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce return ret;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce}
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorceerrno_t
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorcesysdb_save_autofsentry(struct sysdb_ctx *sysdb_ctx,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce const char *map,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce const char *key,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce const char *value,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce struct sysdb_attrs *attrs)
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce{
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce errno_t ret;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce TALLOC_CTX *tmp_ctx;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce struct ldb_message *msg;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce struct ldb_dn *dn;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce const char *name;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce DEBUG(SSSDBG_TRACE_FUNC,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ("Adding autofs entry [%s] - [%s]\n", key, value));
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce tmp_ctx = talloc_new(NULL);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (!tmp_ctx) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce return ENOMEM;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (!attrs) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce attrs = sysdb_new_attrs(tmp_ctx);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (!attrs) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = ENOMEM;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto done;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = sysdb_attrs_add_string(attrs, SYSDB_OBJECTCLASS,
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce SYSDB_AUTOFS_ENTRY_OC);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret != EOK) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce DEBUG(SSSDBG_OP_FAILURE, ("Could not set entry object class [%d]: %s\n",
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret, strerror(ret)));
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto done;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = sysdb_attrs_add_string(attrs, SYSDB_AUTOFS_ENTRY_KEY, key);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret != EOK) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce DEBUG(SSSDBG_OP_FAILURE, ("Could not set entry key [%d]: %s\n",
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret, strerror(ret)));
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto done;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = sysdb_attrs_add_string(attrs, SYSDB_AUTOFS_ENTRY_VALUE, value);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (ret != EOK) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce DEBUG(SSSDBG_OP_FAILURE, ("Could not set entry key [%d]: %s\n",
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret, strerror(ret)));
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto done;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce name = talloc_asprintf(tmp_ctx, "%s%s", key, value);
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce if (!name) {
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce ret = ENOMEM;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce goto done;
625bb2ddf15e8f305a53afa44e87f2146fa930afSimo Sorce }
ret = sysdb_attrs_add_string(attrs, SYSDB_NAME, name);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, ("Could not set name attribute [%d]: %s\n",
ret, strerror(ret)));
goto done;
}
dn = sysdb_autofsentry_dn(tmp_ctx, sysdb_ctx, map, key, value);
if (!dn) {
ret = ENOMEM;
goto done;
}
msg = ldb_msg_new(tmp_ctx);
if (!msg) {
ret = ENOMEM;
goto done;
}
msg->dn = dn;
msg->elements = attrs->a;
msg->num_elements = attrs->num;
ret = ldb_add(sysdb_ctx->ldb, msg);
ret = sysdb_error_to_errno(ret);
done:
talloc_free(tmp_ctx);
return ret;
}
errno_t
sysdb_del_autofsentry(struct sysdb_ctx *sysdb_ctx,
const char *map,
const char *key,
const char *value)
{
struct ldb_dn *dn;
errno_t ret;
dn = sysdb_autofsentry_dn(sysdb_ctx, sysdb_ctx, map, key, value);
if (!dn) {
return ENOMEM;
}
ret = sysdb_delete_entry(sysdb_ctx, dn, true);
talloc_free(dn);
return ret;
}
errno_t
sysdb_autofs_entries_by_map(TALLOC_CTX *mem_ctx,
struct sysdb_ctx *sysdb,
const char *mapname,
size_t *_count,
struct ldb_message ***_entries)
{
errno_t ret;
TALLOC_CTX *tmp_ctx;
char *filter;
const char *attrs[] = { SYSDB_AUTOFS_ENTRY_KEY,
SYSDB_AUTOFS_ENTRY_VALUE,
NULL };
size_t count;
struct ldb_message **msgs;
struct ldb_dn *mapdn;
DEBUG(SSSDBG_TRACE_FUNC, ("Getting entries for map %s\n", mapname));
tmp_ctx = talloc_new(NULL);
if (!tmp_ctx) {
return ENOMEM;
}
mapdn = sysdb_autofsmap_dn(tmp_ctx, sysdb, mapname);
if (!mapdn) {
ret = ENOMEM;
goto done;
}
filter = talloc_asprintf(tmp_ctx, "(objectclass=%s)",
SYSDB_AUTOFS_ENTRY_OC);
if (!filter) {
ret = ENOMEM;
goto done;
}
ret = sysdb_search_entry(tmp_ctx, sysdb, mapdn, LDB_SCOPE_ONELEVEL,
filter, attrs, &count, &msgs);
if (ret != EOK && ret != ENOENT) {
DEBUG(SSSDBG_OP_FAILURE, ("sysdb search failed: %d\n", ret));
goto done;
} else if (ret == ENOENT) {
DEBUG(SSSDBG_TRACE_FUNC, ("No entries for the map\n"));
*_count = 0;
*_entries = NULL;
goto done;
}
*_count = count;
*_entries = talloc_steal(mem_ctx, msgs);
ret = EOK;
DEBUG(SSSDBG_TRACE_INTERNAL, ("found %d entries for map %s\n",
count, mapname));
done:
talloc_free(tmp_ctx);
return ret;
}
errno_t
sysdb_set_autofsmap_attr(struct sysdb_ctx *sysdb,
const char *name,
struct sysdb_attrs *attrs,
int mod_op)
{
errno_t ret;
struct ldb_dn *dn;
TALLOC_CTX *tmp_ctx;
tmp_ctx = talloc_new(NULL);
if (!tmp_ctx) {
return ENOMEM;
}
dn = sysdb_autofsmap_dn(tmp_ctx, sysdb, name);
if (!dn) {
ret = ENOMEM;
goto done;
}
ret = sysdb_set_entry_attr(sysdb, dn, attrs, mod_op);
done:
talloc_free(tmp_ctx);
return ret;
}
errno_t
sysdb_invalidate_autofs_maps(struct sysdb_ctx *sysdb)
{
errno_t ret;
TALLOC_CTX *tmp_ctx;
const char *filter;
struct sysdb_attrs *sys_attrs = NULL;
const char *attrs[] = { SYSDB_OBJECTCLASS,
SYSDB_NAME,
SYSDB_CACHE_EXPIRE,
NULL };
size_t count;
struct ldb_message **msgs;
const char *name;
bool in_transaction = false;
int sret;
int i;
tmp_ctx = talloc_new(NULL);
if (!tmp_ctx) return ENOMEM;
filter = talloc_asprintf(tmp_ctx, "(&(objectclass=%s)(%s=*))",
SYSDB_AUTOFS_MAP_OC, SYSDB_NAME);
if (!filter) {
ret = ENOMEM;
goto done;
}
ret = sysdb_search_custom(tmp_ctx, sysdb, filter,
AUTOFS_MAP_SUBDIR, attrs,
&count, &msgs);
if (ret != EOK && ret != ENOENT) {
DEBUG(SSSDBG_CRIT_FAILURE,
("Error looking up autofs maps"));
goto done;
} else if (ret == ENOENT) {
ret = EOK;
goto done;
}
sys_attrs = sysdb_new_attrs(tmp_ctx);
if (!sys_attrs) {
ret = ENOMEM;
goto done;
}
ret = sysdb_attrs_add_time_t(sys_attrs, SYSDB_CACHE_EXPIRE, 1);
if (ret != EOK) {
goto done;
}
ret = sysdb_transaction_start(sysdb);
if (ret != EOK) {
DEBUG(SSSDBG_CRIT_FAILURE, ("Failed to start transaction\n"));
goto done;
}
in_transaction = true;
for (i = 0; i < count; i++) {
name = ldb_msg_find_attr_as_string(msgs[i], SYSDB_NAME, NULL);
if (!name) {
DEBUG(SSSDBG_MINOR_FAILURE, ("A map with no name?\n"));
continue;
}
ret = sysdb_set_autofsmap_attr(sysdb, name, sys_attrs, SYSDB_MOD_REP);
if (ret != EOK) {
DEBUG(SSSDBG_MINOR_FAILURE, ("Could not expire map %s\n", name));
continue;
}
}
ret = sysdb_transaction_commit(sysdb);
if (ret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, ("Could not commit transaction\n"));
goto done;
}
in_transaction = false;
ret = EOK;
done:
if (in_transaction) {
sret = sysdb_transaction_cancel(sysdb);
if (sret != EOK) {
DEBUG(SSSDBG_OP_FAILURE, ("Could not cancel transaction\n"));
}
}
talloc_free(tmp_ctx);
return ret;
}