s4-dsdb: Ensure we have indexing enabled during the provision
[Samba.git] / source3 / smbd / connection.c
blobd334e8bdc1e70595ecfacc3f4fca2f24cee2f71f
1 /*
2 Unix SMB/CIFS implementation.
3 connection claim routines
4 Copyright (C) Andrew Tridgell 1998
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 "includes.h"
21 #include "smbd/smbd.h"
22 #include "smbd/globals.h"
23 #include "dbwrap/dbwrap.h"
24 #include "auth.h"
25 #include "../lib/tsocket/tsocket.h"
26 #include "messages.h"
27 #include "lib/conn_tdb.h"
29 /****************************************************************************
30 Delete a connection record.
31 ****************************************************************************/
33 bool yield_connection(connection_struct *conn, const char *name)
35 struct db_record *rec;
36 NTSTATUS status;
38 DEBUG(3,("Yielding connection to %s\n",name));
40 rec = connections_fetch_entry(talloc_tos(), conn, name);
41 if (rec == NULL) {
42 DEBUG(0, ("connections_fetch_entry failed\n"));
43 return False;
46 status = dbwrap_record_delete(rec);
47 if (!NT_STATUS_IS_OK(status)) {
48 DEBUG( NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND) ? 3 : 0,
49 ("deleting connection record returned %s\n",
50 nt_errstr(status)));
53 TALLOC_FREE(rec);
54 return NT_STATUS_IS_OK(status);
57 struct count_stat {
58 int curr_connections;
59 const char *name;
60 bool Clear;
63 /****************************************************************************
64 Count the entries belonging to a service in the connection db.
65 ****************************************************************************/
67 static int count_fn(struct db_record *rec,
68 const struct connections_key *ckey,
69 const struct connections_data *crec,
70 void *udp)
72 struct count_stat *cs = (struct count_stat *)udp;
74 if (crec->cnum == TID_FIELD_INVALID) {
75 return 0;
78 /* If the pid was not found delete the entry from connections.tdb */
80 if (cs->Clear && !process_exists(crec->pid) && (errno == ESRCH)) {
81 NTSTATUS status;
82 DEBUG(2,("pid %s doesn't exist - deleting connections %d [%s]\n",
83 procid_str_static(&crec->pid), crec->cnum,
84 crec->servicename));
86 status = dbwrap_record_delete(rec);
87 if (!NT_STATUS_IS_OK(status)) {
88 DEBUG(0,("count_fn: tdb_delete failed with error %s\n",
89 nt_errstr(status)));
91 return 0;
94 if (strequal(crec->servicename, cs->name))
95 cs->curr_connections++;
97 return 0;
100 /****************************************************************************
101 Claim an entry in the connections database.
102 ****************************************************************************/
104 int count_current_connections( const char *sharename, bool clear )
106 struct count_stat cs;
107 int ret;
109 cs.curr_connections = 0;
110 cs.name = sharename;
111 cs.Clear = clear;
114 * This has a race condition, but locking the chain before hand is worse
115 * as it leads to deadlock.
119 * become_root() because we might have to open connections.tdb
120 * via ctdb, which is not possible without root.
122 become_root();
123 ret = connections_forall(count_fn, &cs);
124 unbecome_root();
126 if (ret < 0) {
127 DEBUG(0,("count_current_connections: traverse of "
128 "connections.tdb failed\n"));
129 return 0;
132 return cs.curr_connections;
135 bool connections_snum_used(struct smbd_server_connection *unused, int snum)
137 int active;
139 active = count_current_connections(lp_servicename(talloc_tos(), snum),
140 true);
141 if (active > 0) {
142 return true;
145 return false;
148 /****************************************************************************
149 Claim an entry in the connections database.
150 ****************************************************************************/
152 bool claim_connection(connection_struct *conn, const char *name)
154 struct db_record *rec;
155 struct connections_data crec;
156 char *raddr;
157 TDB_DATA dbuf;
158 NTSTATUS status;
160 DEBUG(5,("claiming [%s]\n", name));
162 if (!(rec = connections_fetch_entry(talloc_tos(), conn, name))) {
163 DEBUG(0, ("connections_fetch_entry failed\n"));
164 return False;
167 /* Make clear that we require the optional unix_token in the source3 code */
168 SMB_ASSERT(conn->session_info->unix_token);
170 /* fill in the crec */
171 ZERO_STRUCT(crec);
172 crec.magic = 0x280267;
173 crec.pid = messaging_server_id(conn->sconn->msg_ctx);
174 crec.cnum = conn->cnum;
175 crec.uid = conn->session_info->unix_token->uid;
176 crec.gid = conn->session_info->unix_token->gid;
177 strlcpy(crec.servicename, lp_servicename(rec, SNUM(conn)),
178 sizeof(crec.servicename));
179 crec.start = time(NULL);
181 raddr = tsocket_address_inet_addr_string(conn->sconn->remote_address,
182 rec);
183 if (raddr == NULL) {
184 return false;
187 strlcpy(crec.machine,get_remote_machine_name(),sizeof(crec.machine));
188 strlcpy(crec.addr, raddr, sizeof(crec.addr));
190 dbuf.dptr = (uint8 *)&crec;
191 dbuf.dsize = sizeof(crec);
193 status = dbwrap_record_store(rec, dbuf, TDB_REPLACE);
195 TALLOC_FREE(rec);
197 if (!NT_STATUS_IS_OK(status)) {
198 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",
199 nt_errstr(status)));
200 return False;
203 return True;