s4-auth: Use kerberos util functions in srv_keytab
[Samba.git] / source3 / lib / server_id_db_util.c
blobead9ed3d3ffd0911b94dbecb599eed3b4f56a861
1 /*
2 * Unix SMB/CIFS implementation.
3 * Utils around server_id_db with more dependencies
4 * Copyright (C) Volker Lendecke 2014
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 3 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
20 #include "replace.h"
21 #include "server_id_db_util.h"
22 #include "serverid.h"
24 static int server_id_db_check_exclusive(
25 struct server_id_db *db, const char *name,
26 unsigned num_servers, struct server_id *servers);
28 int server_id_db_set_exclusive(struct server_id_db *db, const char *name)
30 int ret;
31 unsigned num_servers;
32 struct server_id *servers;
34 ret = server_id_db_add(db, name);
35 if (ret != 0) {
36 return ret;
39 ret = server_id_db_lookup(db, name, talloc_tos(),
40 &num_servers, &servers);
41 if (ret != 0) {
42 goto done;
46 * Remove entries from the server_id_db for processes that have died
47 * and could not clean up. This is racy, as two processes could
48 * simultaneously try to register a name. Both would succeed in the
49 * server_id_db_add call, and both would see their peer active during
50 * the check_exclusive call. Both would get an EEXIST, and nobody
51 * would be able to register itself. But this is okay, as this is
52 * meant to be a cleanup routine, and normally only one daemon should
53 * start up at a time anyway. Getting this "right" would mean we would
54 * have to add locking to server_id_db, or add a dependency on
55 * serverids_exist to server_id_db. Both are too heavy-weight for my
56 * taste.
59 ret = server_id_db_check_exclusive(db, name, num_servers, servers);
60 TALLOC_FREE(servers);
62 done:
63 if (ret != 0) {
64 server_id_db_remove(db, name);
66 return ret;
69 static int server_id_db_check_exclusive(
70 struct server_id_db *db, const char *name,
71 unsigned num_servers, struct server_id *servers)
73 struct server_id me = server_id_db_pid(db);
74 bool exists[num_servers];
75 bool ok;
76 int i;
78 ok = serverids_exist(servers, num_servers, exists);
79 if (!ok) {
80 return ENOMEM;
83 for (i=0; i<num_servers; i++) {
84 int ret;
86 if (server_id_same_process(&me, &servers[i])) {
88 * I am always around ... :-)
90 continue;
93 if (exists[i]) {
94 return EEXIST;
97 ret = server_id_db_prune_name(db, name, servers[i]);
98 if (ret != 0) {
99 return ret;
103 return 0;