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