ipa_hbac_users.c revision 1dd195b9a3df01a0ef51e9f963201f1f79d1f90b
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen Stephen Gallagher <sgallagh@redhat.com>
5ef7efd45b1adf3a09cf9c229cf0a3d3d54405a2Timo Sirainen Copyright (C) 2011 Red Hat
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen This program is free software; you can redistribute it and/or modify
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen it under the terms of the GNU General Public License as published by
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen the Free Software Foundation; either version 3 of the License, or
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen (at your option) any later version.
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen This program is distributed in the hope that it will be useful,
b66484774d4059fa10671cbc50b6489fa40b117fTimo Sirainen but WITHOUT ANY WARRANTY; without even the implied warranty of
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen GNU General Public License for more details.
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen You should have received a copy of the GNU General Public License
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen along with this program. If not, see <http://www.gnu.org/licenses/>.
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen/* Returns EOK and populates groupname if
0dffa25d211be541ee3c953b23566a1a990789dfTimo Sirainen * the group_dn is actually a group.
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen * Returns ENOENT if group_dn does not point
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen * at a a group.
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen * Returns EINVAL if there is a parsing error.
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen * Returns ENOMEM as appropriate
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen /* This is an IPA-specific hack. It may not
e3aeeb634245e80d4f643f8d2eea11d6b72336d8Timo Sirainen * work for non-IPA servers and will need to
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen * be changed if SSSD ever supports HBAC on
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen * a non-IPA server.
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen dn = ldb_dn_new(mem_ctx, sysdb_ctx_get_ldb(sysdb), group_dn);
e3aeeb634245e80d4f643f8d2eea11d6b72336d8Timo Sirainen /* RDN, groups, accounts, and at least one DC= */
e3aeeb634245e80d4f643f8d2eea11d6b72336d8Timo Sirainen /* If it's fewer, it's not a group DN */
e3aeeb634245e80d4f643f8d2eea11d6b72336d8Timo Sirainen /* If the RDN name is 'cn' */
e2eac5bb5637c2d4aaf453389750740931822b92Timo Sirainen /* Shouldn't happen if ldb_dn_validate()
e2eac5bb5637c2d4aaf453389750740931822b92Timo Sirainen * passed, but we'll be careful.
99695d99930b35c2bac85d52e976b44cf8485d83Timo Sirainen /* RDN has the wrong attribute name.
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen * It's not a group.
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen /* and the second component is "cn=groups" */
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen group_comp_name = ldb_dn_get_component_name(dn, 1);
eddd9bf1a1369aea4a2715f6be1137da6d17d293Timo Sirainen /* The second component name is not "cn" */
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen group_comp_val = ldb_dn_get_component_val(dn, 1);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen /* The second component value is not "groups" */
99695d99930b35c2bac85d52e976b44cf8485d83Timo Sirainen /* and the third component is "accounts" */
ad48319996942463675b53877092ab7e13a7a75aTimo Sirainen account_comp_name = ldb_dn_get_component_name(dn, 2);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen if (strcasecmp("cn", account_comp_name) != 0) {
e3aeeb634245e80d4f643f8d2eea11d6b72336d8Timo Sirainen /* The third component name is not "cn" */
7631f16156aca373004953fe6b01a7f343fb47e0Timo Sirainen account_comp_val = ldb_dn_get_component_val(dn, 2);
5ef7efd45b1adf3a09cf9c229cf0a3d3d54405a2Timo Sirainen /* The third component value is not "accounts" */
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen /* Then the value of the RDN is the group name */
7a54d58280aad8a64f266c61273ea1e8dff511a3Timo Sirainen new_users = talloc_zero(tmp_ctx, struct hbac_rule_element);
1e73a28edcf5ec105d238a7d7c95c390e8c84c8fTimo Sirainen DEBUG(7, ("Processing users for rule [%s]\n", rule_name));
7a54d58280aad8a64f266c61273ea1e8dff511a3Timo Sirainen ret = hbac_get_category(rule_attrs, IPA_USER_CATEGORY,
b66484774d4059fa10671cbc50b6489fa40b117fTimo Sirainen DEBUG(1, ("Could not identify user categories\n"));
1e73a28edcf5ec105d238a7d7c95c390e8c84c8fTimo Sirainen if (new_users->category & HBAC_CATEGORY_ALL) {
b66484774d4059fa10671cbc50b6489fa40b117fTimo Sirainen /* Short-cut to the exit */
1e73a28edcf5ec105d238a7d7c95c390e8c84c8fTimo Sirainen ret = sysdb_attrs_get_el(rule_attrs, IPA_MEMBER_USER, &el);
1e73a28edcf5ec105d238a7d7c95c390e8c84c8fTimo Sirainen DEBUG(4, ("No user specified, rule will never apply.\n"));
b66484774d4059fa10671cbc50b6489fa40b117fTimo Sirainen const char *,
1e73a28edcf5ec105d238a7d7c95c390e8c84c8fTimo Sirainen const char *,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen member_user = (const char *)el->values[i].data;
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen ret = sss_filter_sanitize(tmp_ctx, member_user, &member_dn);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen filter = talloc_asprintf(member_dn, "(%s=%s)",
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen /* First check if this is a user */
8cf32443413f811d514123c5c74c95c87594b0e3Timo Sirainen ret = sysdb_search_users(tmp_ctx, sysdb, filter, attrs, &count, &msgs);
d6a1fa1d65c6d1996937802c2482c0f14dd821a7Timo Sirainen DEBUG(1, ("Original DN matched multiple users. Skipping \n"));
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen /* Original DN matched a single user. Get the username */
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen name = ldb_msg_find_attr_as_string(msgs[0], SYSDB_NAME, NULL);
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen new_users->names[num_users] = talloc_strdup(new_users->names,
d9a7e950a9cd21f2b4a90ec7759fca9e8fcc7995Timo Sirainen /* Check if it is a group instead */
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen DEBUG(1, ("Original DN matched multiple groups. "
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen "Skipping\n"));
17d77154db31c5c9020004da172b071c2f11e662Timo Sirainen /* Original DN matched a single group. Get the groupname */
17d77154db31c5c9020004da172b071c2f11e662Timo Sirainen name = ldb_msg_find_attr_as_string(msgs[0], SYSDB_NAME, NULL);
9f19a50d5966643c4d1c5ca06868ac2ad31bc4d5Timo Sirainen DEBUG(8, ("Added POSIX group [%s] to rule [%s]\n",
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen /* If the group still matches the group pattern,
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen * we can assume it is a non-POSIX group.
eb0816090cf5a549280ad783b9aa6fec199d36baTimo Sirainen ret = get_ipa_groupname(new_users->groups, sysdb, member_user,
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen DEBUG(8, ("Added non-POSIX group [%s] to rule [%s]\n",
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen /* Not a group, so we don't care about it */
c6a57378d3c54988f525f81e19c0c5d132a0770dTimo Sirainen DEBUG(1, ("[%s] does not map to either a user or group. "
goto done;
goto done;
done:
return ret;