source: trunk/samba/source/smbd/connection.c@ 77

Last change on this file since 77 was 77, checked in by Paul Smedley, 18 years ago

Update trunk to 3.2.0pre1

File size: 8.4 KB
Line 
1/*
2 Unix SMB/CIFS implementation.
3 connection claim routines
4 Copyright (C) Andrew Tridgell 1998
5
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.
10
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.
15
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/>.
18*/
19
20#include "includes.h"
21
22/****************************************************************************
23 Delete a connection record.
24****************************************************************************/
25
26BOOL yield_connection(connection_struct *conn, const char *name)
27{
28 struct db_record *rec;
29 NTSTATUS status;
30
31 DEBUG(3,("Yielding connection to %s\n",name));
32
33 if (!(rec = connections_fetch_entry(NULL, conn, name))) {
34 DEBUG(0, ("connections_fetch_entry failed\n"));
35 return False;
36 }
37
38 status = rec->delete_rec(rec);
39 if (!NT_STATUS_IS_OK(status)) {
40 DEBUG( NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND) ? 3 : 0,
41 ("deleting connection record returned %s\n",
42 nt_errstr(status)));
43 }
44
45 TALLOC_FREE(rec);
46 return NT_STATUS_IS_OK(status);
47}
48
49struct count_stat {
50 pid_t mypid;
51 int curr_connections;
52 const char *name;
53 BOOL Clear;
54};
55
56/****************************************************************************
57 Count the entries belonging to a service in the connection db.
58****************************************************************************/
59
60static int count_fn(struct db_record *rec,
61 const struct connections_key *ckey,
62 const struct connections_data *crec,
63 void *udp)
64{
65 struct count_stat *cs = (struct count_stat *)udp;
66
67 if (crec->cnum == -1) {
68 return 0;
69 }
70
71 /* If the pid was not found delete the entry from connections.tdb */
72
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));
78
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)));
83 }
84 return 0;
85 }
86
87 if (strequal(crec->servicename, cs->name))
88 cs->curr_connections++;
89
90 return 0;
91}
92
93/****************************************************************************
94 Claim an entry in the connections database.
95****************************************************************************/
96
97int count_current_connections( const char *sharename, BOOL clear )
98{
99 struct count_stat cs;
100
101 cs.mypid = sys_getpid();
102 cs.curr_connections = 0;
103 cs.name = sharename;
104 cs.Clear = clear;
105
106 /*
107 * This has a race condition, but locking the chain before hand is worse
108 * as it leads to deadlock.
109 */
110
111 if (connections_forall(count_fn, &cs) == -1) {
112 DEBUG(0,("count_current_connections: traverse of "
113 "connections.tdb failed\n"));
114 return False;
115 }
116
117 return cs.curr_connections;
118}
119
120/****************************************************************************
121 Claim an entry in the connections database.
122****************************************************************************/
123
124BOOL claim_connection(connection_struct *conn, const char *name,
125 uint32 msg_flags)
126{
127 struct db_record *rec;
128 struct connections_data crec;
129 TDB_DATA dbuf;
130 NTSTATUS status;
131
132 DEBUG(5,("claiming [%s]\n", name));
133
134 if (!(rec = connections_fetch_entry(NULL, conn, name))) {
135 DEBUG(0, ("connections_fetch_entry failed\n"));
136 return False;
137 }
138
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->uid;
146 crec.gid = conn->gid;
147 strlcpy(crec.servicename, lp_servicename(SNUM(conn)),
148 sizeof(crec.servicename));
149 }
150 crec.start = time(NULL);
151 crec.bcast_msg_flags = msg_flags;
152
153 strlcpy(crec.machine,get_remote_machine_name(),sizeof(crec.machine));
154 strlcpy(crec.addr,conn?conn->client_address:client_addr(),
155 sizeof(crec.addr));
156
157 dbuf.dptr = (uint8 *)&crec;
158 dbuf.dsize = sizeof(crec);
159
160 status = rec->store(rec, dbuf, TDB_REPLACE);
161
162 TALLOC_FREE(rec);
163
164 if (!NT_STATUS_IS_OK(status)) {
165 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",
166 nt_errstr(status)));
167 return False;
168 }
169
170 return True;
171}
172
173BOOL register_message_flags(BOOL doreg, uint32 msg_flags)
174{
175 struct db_record *rec;
176 struct connections_data *pcrec;
177 NTSTATUS status;
178
179 DEBUG(10,("register_message_flags: %s flags 0x%x\n",
180 doreg ? "adding" : "removing",
181 (unsigned int)msg_flags ));
182
183 if (!(rec = connections_fetch_entry(NULL, NULL, ""))) {
184 DEBUG(0, ("connections_fetch_entry failed\n"));
185 return False;
186 }
187
188 if (rec->value.dsize != sizeof(struct connections_data)) {
189 DEBUG(0,("register_message_flags: Got wrong record size\n"));
190 TALLOC_FREE(rec);
191 return False;
192 }
193
194 pcrec = (struct connections_data *)rec->value.dptr;
195 if (doreg)
196 pcrec->bcast_msg_flags |= msg_flags;
197 else
198 pcrec->bcast_msg_flags &= ~msg_flags;
199
200 status = rec->store(rec, rec->value, TDB_REPLACE);
201
202 if (!NT_STATUS_IS_OK(status)) {
203 DEBUG(0,("register_message_flags: tdb_store failed: %s.\n",
204 nt_errstr(status)));
205 TALLOC_FREE(rec);
206 return False;
207 }
208
209 DEBUG(10,("register_message_flags: new flags 0x%x\n",
210 (unsigned int)pcrec->bcast_msg_flags ));
211
212 TALLOC_FREE(rec);
213
214 return True;
215}
216
217/*********************************************************************
218*********************************************************************/
219
220static TDB_DATA* make_pipe_rec_key( struct pipe_open_rec *prec )
221{
222 TDB_DATA *kbuf = NULL;
223 fstring key_string;
224
225 if ( !prec )
226 return NULL;
227
228 if ( (kbuf = TALLOC_P(prec, TDB_DATA)) == NULL ) {
229 return NULL;
230 }
231
232 snprintf( key_string, sizeof(key_string), "%s/%d/%d",
233 prec->name, procid_to_pid(&prec->pid), prec->pnum );
234
235 *kbuf = string_term_tdb_data(talloc_strdup(prec, key_string));
236 if (kbuf->dptr == NULL )
237 return NULL;
238
239 return kbuf;
240}
241
242/*********************************************************************
243*********************************************************************/
244
245static void fill_pipe_open_rec( struct pipe_open_rec *prec, smb_np_struct *p )
246{
247 prec->pid = pid_to_procid(sys_getpid());
248 prec->pnum = p->pnum;
249 prec->uid = geteuid();
250 fstrcpy( prec->name, p->name );
251
252 return;
253}
254
255/*********************************************************************
256*********************************************************************/
257
258BOOL store_pipe_opendb( smb_np_struct *p )
259{
260 struct db_record *dbrec;
261 struct pipe_open_rec *prec;
262 TDB_DATA *key;
263 TDB_DATA data;
264 BOOL ret = False;
265
266 if ( (prec = TALLOC_P( NULL, struct pipe_open_rec)) == NULL ) {
267 DEBUG(0,("store_pipe_opendb: talloc failed!\n"));
268 return False;
269 }
270
271 fill_pipe_open_rec( prec, p );
272 if ( (key = make_pipe_rec_key( prec )) == NULL ) {
273 goto done;
274 }
275
276 data.dptr = (uint8 *)prec;
277 data.dsize = sizeof(struct pipe_open_rec);
278
279 if (!(dbrec = connections_fetch_record(prec, *key))) {
280 DEBUG(0, ("connections_fetch_record failed\n"));
281 goto done;
282 }
283
284 ret = NT_STATUS_IS_OK(dbrec->store(dbrec, data, TDB_REPLACE));
285
286done:
287 TALLOC_FREE( prec );
288 return ret;
289}
290
291/*********************************************************************
292*********************************************************************/
293
294BOOL delete_pipe_opendb( smb_np_struct *p )
295{
296 struct db_record *dbrec;
297 struct pipe_open_rec *prec;
298 TDB_DATA *key;
299 BOOL ret = False;
300
301 if ( (prec = TALLOC_P( NULL, struct pipe_open_rec)) == NULL ) {
302 DEBUG(0,("store_pipe_opendb: talloc failed!\n"));
303 return False;
304 }
305
306 fill_pipe_open_rec( prec, p );
307 if ( (key = make_pipe_rec_key( prec )) == NULL ) {
308 goto done;
309 }
310
311 if (!(dbrec = connections_fetch_record(prec, *key))) {
312 DEBUG(0, ("connections_fetch_record failed\n"));
313 goto done;
314 }
315
316 ret = NT_STATUS_IS_OK(dbrec->delete_rec(dbrec));
317
318done:
319 TALLOC_FREE( prec );
320 return ret;
321}
Note: See TracBrowser for help on using the repository browser.