s4:provision - Updated FDS schema mapping.
[Samba/kamenim.git] / source3 / smbd / connection.c
blobe2b01d057943943c54e00099b1c695a30a0bdede
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"
22 /****************************************************************************
23 Delete a connection record.
24 ****************************************************************************/
26 bool yield_connection(connection_struct *conn, const char *name)
28 struct db_record *rec;
29 NTSTATUS status;
31 DEBUG(3,("Yielding connection to %s\n",name));
33 rec = connections_fetch_entry(talloc_tos(), conn, name);
34 if (rec == NULL) {
35 DEBUG(0, ("connections_fetch_entry failed\n"));
36 return False;
39 status = rec->delete_rec(rec);
40 if (!NT_STATUS_IS_OK(status)) {
41 DEBUG( NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND) ? 3 : 0,
42 ("deleting connection record returned %s\n",
43 nt_errstr(status)));
46 TALLOC_FREE(rec);
47 return NT_STATUS_IS_OK(status);
50 struct count_stat {
51 int curr_connections;
52 const char *name;
53 bool Clear;
56 /****************************************************************************
57 Count the entries belonging to a service in the connection db.
58 ****************************************************************************/
60 static int count_fn(struct db_record *rec,
61 const struct connections_key *ckey,
62 const struct connections_data *crec,
63 void *udp)
65 struct count_stat *cs = (struct count_stat *)udp;
67 if (crec->cnum == -1) {
68 return 0;
71 /* If the pid was not found delete the entry from connections.tdb */
73 if (cs->Clear && !process_exists(crec->pid) && (errno == ESRCH)) {
74 NTSTATUS status;
75 DEBUG(2,("pid %s doesn't exist - deleting connections %d [%s]\n",
76 procid_str_static(&crec->pid), crec->cnum,
77 crec->servicename));
79 status = rec->delete_rec(rec);
80 if (!NT_STATUS_IS_OK(status)) {
81 DEBUG(0,("count_fn: tdb_delete failed with error %s\n",
82 nt_errstr(status)));
84 return 0;
87 if (strequal(crec->servicename, cs->name))
88 cs->curr_connections++;
90 return 0;
93 /****************************************************************************
94 Claim an entry in the connections database.
95 ****************************************************************************/
97 int count_current_connections( const char *sharename, bool clear )
99 struct count_stat cs;
101 cs.curr_connections = 0;
102 cs.name = sharename;
103 cs.Clear = clear;
106 * This has a race condition, but locking the chain before hand is worse
107 * as it leads to deadlock.
110 if (connections_forall(count_fn, &cs) == -1) {
111 DEBUG(0,("count_current_connections: traverse of "
112 "connections.tdb failed\n"));
113 return False;
116 return cs.curr_connections;
119 /****************************************************************************
120 Claim an entry in the connections database.
121 ****************************************************************************/
123 bool claim_connection(connection_struct *conn, const char *name,
124 uint32 msg_flags)
126 struct db_record *rec;
127 struct connections_data crec;
128 TDB_DATA dbuf;
129 NTSTATUS status;
130 char addr[INET6_ADDRSTRLEN];
132 DEBUG(5,("claiming [%s]\n", name));
134 if (!(rec = connections_fetch_entry(talloc_tos(), conn, name))) {
135 DEBUG(0, ("connections_fetch_entry failed\n"));
136 return False;
139 /* fill in the crec */
140 ZERO_STRUCT(crec);
141 crec.magic = 0x280267;
142 crec.pid = procid_self();
143 crec.cnum = conn?conn->cnum:-1;
144 if (conn) {
145 crec.uid = conn->server_info->utok.uid;
146 crec.gid = conn->server_info->utok.gid;
147 strlcpy(crec.servicename, lp_servicename(SNUM(conn)),
148 sizeof(crec.servicename));
150 crec.start = time(NULL);
151 crec.bcast_msg_flags = msg_flags;
153 strlcpy(crec.machine,get_remote_machine_name(),sizeof(crec.machine));
154 strlcpy(crec.addr,conn?conn->client_address:
155 client_addr(get_client_fd(),addr,sizeof(addr)),
156 sizeof(crec.addr));
158 dbuf.dptr = (uint8 *)&crec;
159 dbuf.dsize = sizeof(crec);
161 status = rec->store(rec, dbuf, TDB_REPLACE);
163 TALLOC_FREE(rec);
165 if (!NT_STATUS_IS_OK(status)) {
166 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",
167 nt_errstr(status)));
168 return False;
171 return True;
174 bool register_message_flags(bool doreg, uint32 msg_flags)
176 struct db_record *rec;
177 struct connections_data *pcrec;
178 NTSTATUS status;
180 DEBUG(10,("register_message_flags: %s flags 0x%x\n",
181 doreg ? "adding" : "removing",
182 (unsigned int)msg_flags ));
184 if (!(rec = connections_fetch_entry(NULL, NULL, ""))) {
185 DEBUG(0, ("connections_fetch_entry failed\n"));
186 return False;
189 if (rec->value.dsize != sizeof(struct connections_data)) {
190 DEBUG(0,("register_message_flags: Got wrong record size\n"));
191 TALLOC_FREE(rec);
192 return False;
195 pcrec = (struct connections_data *)rec->value.dptr;
196 if (doreg)
197 pcrec->bcast_msg_flags |= msg_flags;
198 else
199 pcrec->bcast_msg_flags &= ~msg_flags;
201 status = rec->store(rec, rec->value, TDB_REPLACE);
203 if (!NT_STATUS_IS_OK(status)) {
204 DEBUG(0,("register_message_flags: tdb_store failed: %s.\n",
205 nt_errstr(status)));
206 TALLOC_FREE(rec);
207 return False;
210 DEBUG(10,("register_message_flags: new flags 0x%x\n",
211 (unsigned int)pcrec->bcast_msg_flags ));
213 TALLOC_FREE(rec);
215 return True;