sysdb_selinux.c revision 4c11f752e1f10cf5740d53a3206bb795e9e34fe8
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen System Database - SELinux support
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen Copyright (C) Jan Zeleny <jzeleny@redhat.com> 2012
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen This program is free software; you can redistribute it and/or modify
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen it under the terms of the GNU General Public License as published by
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen the Free Software Foundation; either version 3 of the License, or
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen (at your option) any later version.
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen This program is distributed in the hope that it will be useful,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen but WITHOUT ANY WARRANTY; without even the implied warranty of
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen GNU General Public License for more details.
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen You should have received a copy of the GNU General Public License
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen along with this program. If not, see <http://www.gnu.org/licenses/>.
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* Some generic routines */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen rm_msg->elements = talloc_zero_array(rm_msg, struct ldb_message_element,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = sysdb_attrs_get_string(new_attrs, old_msg->elements[i].name, &tmp_str);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen rm_msg->elements[rm_msg->num_elements] = old_msg->elements[i];
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen rm_msg->elements[rm_msg->num_elements].flags = LDB_FLAG_MOD_DELETE;
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainensysdb_add_selinux_entity(struct sysdb_ctx *sysdb,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = sysdb_attrs_add_string(attrs, SYSDB_OBJECTCLASS, objectclass);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen DEBUG(SSSDBG_OP_FAILURE, ("Could not set map object class [%d]: %s\n",
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = sysdb_attrs_add_time_t(attrs, SYSDB_CREATE_TIME, now);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen DEBUG(SSSDBG_TRACE_LIBS, ("Error: %d (%s)\n", ret, strerror(ret)));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenstatic errno_t sysdb_store_selinux_entity(struct sysdb_ctx *sysdb,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = sysdb_attrs_get_string(attrs, SYSDB_NAME, &name);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = sysdb_dn_sanitize(tmp_ctx, name, &clean_name);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen dn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb, SYSDB_TMPL_SEUSERMAP,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen dn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb, SYSDB_TMPL_SELINUX_BASE,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (type != SELINUX_CONFIG && type != SELINUX_USER_MAP) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen DEBUG(SSSDBG_CRIT_FAILURE, ("Bad SELinux entity type: [%d]\n", type));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = sysdb_attrs_add_time_t(attrs, SYSDB_LAST_UPDATE, now);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = sysdb_search_selinux_config(tmp_ctx, sysdb, NULL, &msg);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = sysdb_search_selinux_usermap_by_mapname(tmp_ctx, sysdb, name,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = sysdb_add_selinux_entity(sysdb, dn, objectclass, attrs, now);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = sysdb_set_entry_attr(sysdb, dn, attrs, SYSDB_MOD_REP);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* Now delete attributes which are no longer present */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = get_rm_msg(tmp_ctx, msg, attrs, &rm_msg);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen DEBUG(SSSDBG_OP_FAILURE, ("Could not commit transaction\n"));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen DEBUG(SSSDBG_OP_FAILURE, ("Could not cancel transaction\n"));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen DEBUG(SSSDBG_MINOR_FAILURE, ("Error: %d (%s)\n", ret, strerror(ret)));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenerrno_t sysdb_store_selinux_usermap(struct sysdb_ctx *sysdb,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen return sysdb_store_selinux_entity(sysdb, attrs, SELINUX_USER_MAP);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenerrno_t sysdb_store_selinux_config(struct sysdb_ctx *sysdb,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen attrs = talloc_zero(NULL, struct sysdb_attrs);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = sysdb_attrs_add_string(attrs, SYSDB_SELINUX_DEFAULT_USER,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = sysdb_attrs_add_string(attrs, SYSDB_SELINUX_DEFAULT_ORDER,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = sysdb_store_selinux_entity(sysdb, attrs, SELINUX_CONFIG);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen/* --- SYSDB SELinux search routines --- */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenerrno_t sysdb_search_selinux_usermap_by_mapname(TALLOC_CTX *mem_ctx,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char **attrs,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = sysdb_dn_sanitize(tmp_ctx, name, &clean_name);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen basedn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb, SYSDB_TMPL_SEUSERMAP,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = sysdb_search_entry(tmp_ctx, sysdb, basedn, LDB_SCOPE_BASE, NULL,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen DEBUG(SSSDBG_TRACE_FUNC, ("No such entry\n"));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen DEBUG(SSSDBG_TRACE_FUNC, ("Error: %d (%s)\n", ret, strerror(ret)));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenerrno_t sysdb_search_selinux_usermap_by_username(TALLOC_CTX *mem_ctx,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* Now extract user attributes */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = sss_selinux_extract_user(tmp_ctx, sysdb, username, &user);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* Now extract all SELinux user maps */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen basedn = ldb_dn_new_fmt(tmp_ctx, sysdb_ctx_get_ldb(sysdb),
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen filter = talloc_asprintf(tmp_ctx, "(objectclass=%s)", SYSDB_SELINUX_USERMAP_CLASS);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = sysdb_search_entry(tmp_ctx, sysdb, basedn, LDB_SCOPE_SUBTREE, filter,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen /* Now filter those that match */
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen tmp_attrs = talloc_zero(tmp_ctx, struct sysdb_attrs);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen usermaps = talloc_zero_array(tmp_ctx, struct ldb_message *, msgs_count + 1);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen for (i = 0; i < msgs_count; i++) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen if (sss_selinux_match(tmp_attrs, user, NULL)) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen usermaps[usermaps_cnt] = talloc_steal(usermaps, msgs[i]);
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainenerrno_t sysdb_search_selinux_config(TALLOC_CTX *mem_ctx,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char **attrs,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen const char *def_attrs[] = { SYSDB_SELINUX_DEFAULT_USER,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen basedn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb, SYSDB_TMPL_SELINUX_BASE,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen ret = sysdb_search_entry(tmp_ctx, sysdb, basedn, LDB_SCOPE_BASE, NULL,
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen DEBUG(SSSDBG_TRACE_FUNC, ("No SELinux root entry found\n"));
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen } else if (ret) {
0cb2e8eb55e70f8ebe1e8349bdf49e4cbe5d8834Timo Sirainen DEBUG(SSSDBG_TRACE_FUNC, ("Error: %d (%s)\n", ret, strerror(ret)));