sysdb_services.c revision ae765daed93b64b9b4177466a2191561b6734e63
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher Stephen Gallagher <sgallagh@redhat.com>
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher Copyright (C) 2012 Red Hat
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher This program is free software; you can redistribute it and/or modify
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher it under the terms of the GNU General Public License as published by
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher the Free Software Foundation; either version 3 of the License, or
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher (at your option) any later version.
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher This program is distributed in the hope that it will be useful,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher but WITHOUT ANY WARRANTY; without even the implied warranty of
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher GNU General Public License for more details.
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher You should have received a copy of the GNU General Public License
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher along with this program. If not, see <http://www.gnu.org/licenses/>.
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallaghersysdb_svc_remove_alias(struct sysdb_ctx *sysdb,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher static const char *attrs[] = SYSDB_SVC_ATTRS;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher base_dn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sss_filter_sanitize(tmp_ctx, name, &sanitized_name);
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce ret = sss_filter_sanitize(tmp_ctx, proto, &sanitized_proto);
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce lret = ldb_search(sysdb->ldb, tmp_ctx, &res, base_dn,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher static const char *attrs[] = SYSDB_SVC_ATTRS;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher base_dn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sss_filter_sanitize(tmp_ctx, proto, &sanitized_proto);
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce lret = ldb_search(sysdb->ldb, tmp_ctx, &res, base_dn,
fd555d130dc733509347fa096a2cb858b014a196Simo Sorce (unsigned int) port);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallaghersysdb_store_service(struct sysdb_ctx *sysdb,
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek const char *name;
3a59cbd0b7b9c5dd3c62ac1679876070c264d80fMichal Zidek unsigned int i;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Check that the port is unique
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher * If the port appears for any service other than
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher * the one matching the primary_name, we need to
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher * remove them so that getservbyport() can work
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher * properly. Last entry saved to the cache should
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher * always "win".
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sysdb_getservbyport(tmp_ctx, sysdb, port, NULL, &res);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Somehow the cache has multiple entries with
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher * the same port. This is corrupted. We'll delete
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher * them all to sort it out.
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ("Corrupt cache entry [%s] detected. Deleting\n",
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sysdb_delete_entry(sysdb, res->msgs[i]->dn, true);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ("Could not delete corrupt cache entry [%s]\n",
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Check whether this is the same name as we're currently
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher * saving to the cache.
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher name = ldb_msg_find_attr_as_string(res->msgs[0],
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (!name || strcmp(name, primary_name) != 0) {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ("A service with no name?\n"));
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Corrupted */
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Either this is a corrupt entry or it's another service
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher * claiming ownership of this port. In order to account
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher * for port reassignments, we need to delete the old entry.
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ("Corrupt or replaced cache entry [%s] detected. "
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sysdb_delete_entry(sysdb, res->msgs[0]->dn, true);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ("Could not delete cache entry [%s]\n",
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Ok, ports should now be unique. Now look
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher * the service up by name to determine if we
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher * need to update existing entries or modify
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sysdb_getservbyname(tmp_ctx, sysdb, primary_name, NULL, &res);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher } else if (ret != ENOENT) { /* Found entries */
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Check whether this is the same name as we're currently
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher * saving to the cache.
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher name = ldb_msg_find_attr_as_string(res->msgs[i],
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Corrupted */
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ("A service with no name?\n"));
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ("Corrupt cache entry [%s] detected. Deleting\n",
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sysdb_delete_entry(sysdb, res->msgs[i]->dn, true);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ("Could not delete corrupt cache entry [%s]\n",
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher } else if (strcmp(name, primary_name) == 0) {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* This is the same service name, so we need
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher * to update this entry with the values
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ("Two existing services with the same name: [%s]? "
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher "Deleting both.\n",
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Delete the entry from the previous pass */
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sysdb_delete_entry(sysdb, update_dn, true);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ("Could not delete cache entry [%s]\n",
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Delete the new entry as well */
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sysdb_delete_entry(sysdb, res->msgs[i]->dn, true);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ("Could not delete cache entry [%s]\n",
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher update_dn = talloc_steal(tmp_ctx, res->msgs[i]->dn);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Another service is claiming this name as an alias.
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher * In order to account for aliases being promoted to
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher * primary names, we need to make sure to remove the
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher * old alias entry.
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Update the existing entry */
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sysdb_svc_update(sysdb, update_dn, port, aliases, protocols);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Add a new entry */
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher ret = sysdb_svc_add(tmp_ctx, sysdb, primary_name, port,
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher /* Set the cache timeout */
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sysdb_attrs_add_time_t(attrs, SYSDB_LAST_UPDATE, now);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sysdb_attrs_add_time_t(attrs, SYSDB_CACHE_EXPIRE,
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher ret = sysdb_set_entry_attr(sysdb, update_dn, attrs, SYSDB_MOD_REP);
e299638926171e0e92a36122aeff6611cd52418dStephen Gallagher ret = sysdb_remove_attrs(sysdb, primary_name,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ("Could not remove missing attributes: [%s]\n",
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher DEBUG(SSSDBG_CRIT_FAILURE, ("Could not cancel transaction\n"));
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallaghersysdb_svc_dn(struct sysdb_ctx *sysdb, TALLOC_CTX *mem_ctx,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sysdb_dn_sanitize(NULL, name, &clean_name);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher dn = ldb_dn_new_fmt(mem_ctx, sysdb->ldb, SYSDB_TMPL_SVC,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher unsigned long i;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher msg->dn = sysdb_svc_dn(sysdb, msg, sysdb->domain->name, primary_name);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Objectclass */
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Set the primary name */
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Set the port number */
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* If this service has any aliases, include them */
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Set the name aliases */
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher lret = ldb_msg_add_empty(msg, SYSDB_NAME_ALIAS,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher for (i=0; aliases[i]; i++) {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher lret = ldb_msg_add_string(msg, SYSDB_NAME_ALIAS, aliases[i]);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Set the protocols */
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher lret = ldb_msg_add_empty(msg, SYSDB_SVC_PROTO,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher for (i=0; protocols[i]; i++) {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher lret = ldb_msg_add_string(msg, SYSDB_SVC_PROTO, protocols[i]);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* creation time */
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = add_ulong(msg, LDB_FLAG_MOD_ADD, SYSDB_CREATE_TIME,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ("Error: %d (%s)\n", ret, strerror(ret)));
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher unsigned int i;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Update the port */
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Update the aliases */
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher lret = ldb_msg_add_empty(msg, SYSDB_NAME_ALIAS, SYSDB_MOD_REP, NULL);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher for (i = 0; aliases[i]; i++) {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher lret = ldb_msg_add_fmt(msg, SYSDB_NAME_ALIAS, "%s", aliases[i]);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Update the protocols */
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher lret = ldb_msg_add_empty(msg, SYSDB_SVC_PROTO, SYSDB_MOD_REP, NULL);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher for (i = 0; protocols[i]; i++) {
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher lret = ldb_msg_add_fmt(msg, SYSDB_SVC_PROTO, "%s", protocols[i]);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ("Error: %d (%s)\n", ret, strerror(ret)));
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallaghersysdb_svc_remove_alias(struct sysdb_ctx *sysdb,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ("Error: %d (%s)\n", ret, strerror(ret)));
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher unsigned int i;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sysdb_getservbyname(tmp_ctx, sysdb, name, proto, &res);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (ret != EOK && ret != ENOENT) goto done;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Doesn't exist in the DB. Nothing to do */
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sysdb_getservbyport(tmp_ctx, sysdb, port, proto, &res);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher if (ret != EOK && ret != ENOENT) goto done;
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* Doesn't exist in the DB. Nothing to do */
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher /* There should only be one matching entry,
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher * but if there are multiple, we should delete
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher * them all to de-corrupt the DB.
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ret = sysdb_delete_entry(sysdb, res->msgs[i]->dn, false);
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ("Could not cancel transaction\n"));
4c1bf6607060cea867fccf667063c028dfd51e96Stephen Gallagher ("Error: %d (%s)\n", ret, strerror(ret)));
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher static const char *attrs[] = SYSDB_SVC_ATTRS;
1e7c355a2d36eb7b942b5111c96eb2a2285d49ccStephen Gallagher base_dn = ldb_dn_new_fmt(tmp_ctx, sysdb->ldb,