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/>.
21 #include "smbd/smbd.h"
22 #include "smbd/globals.h"
23 #include "dbwrap/dbwrap.h"
25 #include "../lib/tsocket/tsocket.h"
27 /****************************************************************************
28 Delete a connection record.
29 ****************************************************************************/
31 bool yield_connection(connection_struct
*conn
, const char *name
)
33 struct db_record
*rec
;
36 DEBUG(3,("Yielding connection to %s\n",name
));
38 rec
= connections_fetch_entry(talloc_tos(), conn
, name
);
40 DEBUG(0, ("connections_fetch_entry failed\n"));
44 status
= rec
->delete_rec(rec
);
45 if (!NT_STATUS_IS_OK(status
)) {
46 DEBUG( NT_STATUS_EQUAL(status
, NT_STATUS_NOT_FOUND
) ? 3 : 0,
47 ("deleting connection record returned %s\n",
52 return NT_STATUS_IS_OK(status
);
61 /****************************************************************************
62 Count the entries belonging to a service in the connection db.
63 ****************************************************************************/
65 static int count_fn(struct db_record
*rec
,
66 const struct connections_key
*ckey
,
67 const struct connections_data
*crec
,
70 struct count_stat
*cs
= (struct count_stat
*)udp
;
72 if (crec
->cnum
== -1) {
76 /* If the pid was not found delete the entry from connections.tdb */
78 if (cs
->Clear
&& !process_exists(crec
->pid
) && (errno
== ESRCH
)) {
80 DEBUG(2,("pid %s doesn't exist - deleting connections %d [%s]\n",
81 procid_str_static(&crec
->pid
), crec
->cnum
,
84 status
= rec
->delete_rec(rec
);
85 if (!NT_STATUS_IS_OK(status
)) {
86 DEBUG(0,("count_fn: tdb_delete failed with error %s\n",
92 if (strequal(crec
->servicename
, cs
->name
))
93 cs
->curr_connections
++;
98 /****************************************************************************
99 Claim an entry in the connections database.
100 ****************************************************************************/
102 int count_current_connections( const char *sharename
, bool clear
)
104 struct count_stat cs
;
107 cs
.curr_connections
= 0;
112 * This has a race condition, but locking the chain before hand is worse
113 * as it leads to deadlock.
117 * become_root() because we might have to open connections.tdb
118 * via ctdb, which is not possible without root.
121 ret
= connections_forall(count_fn
, &cs
);
125 DEBUG(0,("count_current_connections: traverse of "
126 "connections.tdb failed\n"));
130 return cs
.curr_connections
;
133 /****************************************************************************
134 Claim an entry in the connections database.
135 ****************************************************************************/
137 bool claim_connection(connection_struct
*conn
, const char *name
)
139 struct db_record
*rec
;
140 struct connections_data crec
;
145 DEBUG(5,("claiming [%s]\n", name
));
147 if (!(rec
= connections_fetch_entry(talloc_tos(), conn
, name
))) {
148 DEBUG(0, ("connections_fetch_entry failed\n"));
152 /* Make clear that we require the optional unix_token in the source3 code */
153 SMB_ASSERT(conn
->session_info
->unix_token
);
155 /* fill in the crec */
157 crec
.magic
= 0x280267;
158 crec
.pid
= sconn_server_id(conn
->sconn
);
159 crec
.cnum
= conn
->cnum
;
160 crec
.uid
= conn
->session_info
->unix_token
->uid
;
161 crec
.gid
= conn
->session_info
->unix_token
->gid
;
162 strlcpy(crec
.servicename
, lp_servicename(SNUM(conn
)),
163 sizeof(crec
.servicename
));
164 crec
.start
= time(NULL
);
166 raddr
= tsocket_address_inet_addr_string(conn
->sconn
->remote_address
,
172 strlcpy(crec
.machine
,get_remote_machine_name(),sizeof(crec
.machine
));
173 strlcpy(crec
.addr
, raddr
, sizeof(crec
.addr
));
175 dbuf
.dptr
= (uint8
*)&crec
;
176 dbuf
.dsize
= sizeof(crec
);
178 status
= rec
->store(rec
, dbuf
, TDB_REPLACE
);
182 if (!NT_STATUS_IS_OK(status
)) {
183 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",