r23221: Fix a bad merge, avoid a memory leak and tdb lock problem
[sfrench/samba-autobuild/.git] / source / smbd / connection.c
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 2 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, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include "includes.h"
22
23 /****************************************************************************
24  Delete a connection record.
25 ****************************************************************************/
26
27 BOOL yield_connection(connection_struct *conn, const char *name)
28 {
29         struct db_record *rec;
30         NTSTATUS status;
31
32         DEBUG(3,("Yielding connection to %s\n",name));
33
34         if (!(rec = connections_fetch_entry(NULL, conn, name))) {
35                 DEBUG(0, ("connections_fetch_entry failed\n"));
36                 return False;
37         }
38
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)));
44         }
45
46         TALLOC_FREE(rec);
47         return NT_STATUS_IS_OK(status);
48 }
49
50 struct count_stat {
51         pid_t mypid;
52         int curr_connections;
53         const char *name;
54         BOOL Clear;
55 };
56
57 /****************************************************************************
58  Count the entries belonging to a service in the connection db.
59 ****************************************************************************/
60
61 static int count_fn(struct db_record *rec,
62                     const struct connections_key *ckey,
63                     const struct connections_data *crec,
64                     void *udp)
65 {
66         struct count_stat *cs = (struct count_stat *)udp;
67  
68         if (crec->cnum == -1) {
69                 return 0;
70         }
71
72         /* If the pid was not found delete the entry from connections.tdb */
73
74         if (cs->Clear && !process_exists(crec->pid) && (errno == ESRCH)) {
75                 NTSTATUS status;
76                 DEBUG(2,("pid %s doesn't exist - deleting connections %d [%s]\n",
77                          procid_str_static(&crec->pid), crec->cnum,
78                          crec->servicename));
79
80                 status = rec->delete_rec(rec);
81                 if (!NT_STATUS_IS_OK(status)) {
82                         DEBUG(0,("count_fn: tdb_delete failed with error %s\n",
83                                  nt_errstr(status)));
84                 }
85                 return 0;
86         }
87  
88         if (cs->name) {
89                 /* We are counting all the connections to a given share. */
90                 if (strequal(crec->servicename, cs->name)) {
91                         cs->curr_connections++;
92                 }
93         } else {
94                 /* We are counting all the connections. Static registrations
95                  * like the lpq backgroud process and the smbd daemon process
96                  * have a cnum of -1, so won't be counted here.
97                  */
98                 cs->curr_connections++;
99         }
100
101         return 0;
102 }
103
104 /****************************************************************************
105  Claim an entry in the connections database.
106 ****************************************************************************/
107
108 int count_current_connections( const char *sharename, BOOL clear  )
109 {
110         struct count_stat cs;
111
112         cs.mypid = sys_getpid();
113         cs.curr_connections = 0;
114         cs.name = sharename;
115         cs.Clear = clear;
116
117         /*
118          * This has a race condition, but locking the chain before hand is worse
119          * as it leads to deadlock.
120          */
121
122         if (connections_forall(count_fn, &cs) == -1) {
123                 DEBUG(0,("count_current_connections: traverse of "
124                          "connections.tdb failed\n"));
125                 DEBUGADD(0, ("count_current_connections: connection count of %d might not be accurate",
126                             cs.curr_connections));
127         }
128
129         /* If the traverse failed part-way through, we at least return
130          * as many connections as we had already counted. If it failed
131          * right at the start, we will return 0, which is about all we
132          * can do anywway.
133          */
134
135         return cs.curr_connections;
136 }
137
138 /****************************************************************************
139  Count the number of connections open across all shares.
140 ****************************************************************************/
141
142 int count_all_current_connections(void)
143 {
144         return count_current_connections(NULL, True /* clear stale entries */);
145 }
146
147 /****************************************************************************
148  Claim an entry in the connections database.
149 ****************************************************************************/
150
151 BOOL claim_connection(connection_struct *conn, const char *name,
152                       uint32 msg_flags)
153 {
154         struct db_record *rec;
155         struct connections_data crec;
156         TDB_DATA dbuf;
157         NTSTATUS status;
158
159         DEBUG(5,("claiming [%s]\n", name));
160
161         if (!(rec = connections_fetch_entry(NULL, conn, name))) {
162                 DEBUG(0, ("connections_fetch_entry failed\n"));
163                 return False;
164         }
165
166         /* fill in the crec */
167         ZERO_STRUCT(crec);
168         crec.magic = 0x280267;
169         crec.pid = procid_self();
170         crec.cnum = conn?conn->cnum:-1;
171         if (conn) {
172                 crec.uid = conn->uid;
173                 crec.gid = conn->gid;
174                 strlcpy(crec.servicename, lp_servicename(SNUM(conn)),
175                         sizeof(crec.servicename));
176         }
177         crec.start = time(NULL);
178         crec.bcast_msg_flags = msg_flags;
179         
180         strlcpy(crec.machine,get_remote_machine_name(),sizeof(crec.machine));
181         strlcpy(crec.addr,conn?conn->client_address:client_addr(),
182                 sizeof(crec.addr));
183
184         dbuf.dptr = (uint8 *)&crec;
185         dbuf.dsize = sizeof(crec);
186
187         status = rec->store(rec, dbuf, TDB_REPLACE);
188
189         TALLOC_FREE(rec);
190
191         if (!NT_STATUS_IS_OK(status)) {
192                 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",
193                          nt_errstr(status)));
194                 return False;
195         }
196
197         return True;
198 }
199
200 BOOL register_message_flags(BOOL doreg, uint32 msg_flags)
201 {
202         struct db_record *rec;
203         struct connections_data *pcrec;
204         NTSTATUS status;
205
206         DEBUG(10,("register_message_flags: %s flags 0x%x\n",
207                 doreg ? "adding" : "removing",
208                 (unsigned int)msg_flags ));
209
210         if (!(rec = connections_fetch_entry(NULL, NULL, NULL))) {
211                 DEBUG(0, ("connections_fetch_entry failed\n"));
212                 return False;
213         }
214
215         if (rec->value.dsize != sizeof(struct connections_data)) {
216                 DEBUG(0,("register_message_flags: Got wrong record size\n"));
217                 TALLOC_FREE(rec);
218                 return False;
219         }
220
221         pcrec = (struct connections_data *)rec->value.dptr;
222         if (doreg)
223                 pcrec->bcast_msg_flags |= msg_flags;
224         else
225                 pcrec->bcast_msg_flags &= ~msg_flags;
226
227         status = rec->store(rec, rec->value, TDB_REPLACE);
228
229         TALLOC_FREE(rec);
230
231         if (!NT_STATUS_IS_OK(status)) {
232                 DEBUG(0,("register_message_flags: tdb_store failed: %s.\n",
233                          nt_errstr(status)));
234                 return False;
235         }
236
237         DEBUG(10,("register_message_flags: new flags 0x%x\n",
238                 (unsigned int)pcrec->bcast_msg_flags ));
239
240         return True;
241 }
242
243 /*********************************************************************
244 *********************************************************************/
245
246 static TDB_DATA* make_pipe_rec_key( struct pipe_open_rec *prec )
247 {
248         TDB_DATA *kbuf = NULL;
249         fstring key_string;
250         
251         if ( !prec )
252                 return NULL;
253         
254         if ( (kbuf = TALLOC_P(prec, TDB_DATA)) == NULL ) {
255                 return NULL;
256         }
257         
258         snprintf( key_string, sizeof(key_string), "%s/%d/%d",
259                 prec->name, procid_to_pid(&prec->pid), prec->pnum );
260                 
261         *kbuf = string_term_tdb_data(talloc_strdup(prec, key_string));
262         if (kbuf->dptr == NULL )
263                 return NULL;
264
265         return kbuf;
266 }
267
268 /*********************************************************************
269 *********************************************************************/
270
271 static void fill_pipe_open_rec( struct pipe_open_rec *prec, smb_np_struct *p )
272 {
273         prec->pid = pid_to_procid(sys_getpid());
274         prec->pnum = p->pnum;
275         prec->uid = geteuid();
276         fstrcpy( prec->name, p->name );
277
278         return;
279 }
280
281 /*********************************************************************
282 *********************************************************************/
283
284 BOOL store_pipe_opendb( smb_np_struct *p )
285 {
286         struct db_record *dbrec;
287         struct pipe_open_rec *prec;
288         TDB_DATA *key;
289         TDB_DATA data;
290         BOOL ret = False;
291         
292         if ( (prec = TALLOC_P( NULL, struct pipe_open_rec)) == NULL ) {
293                 DEBUG(0,("store_pipe_opendb: talloc failed!\n"));
294                 return False;
295         }
296         
297         fill_pipe_open_rec( prec, p );
298         if ( (key = make_pipe_rec_key( prec )) == NULL ) {
299                 goto done;
300         }
301         
302         data.dptr = (uint8 *)prec;
303         data.dsize = sizeof(struct pipe_open_rec);
304
305         if (!(dbrec = connections_fetch_record(prec, *key))) {
306                 DEBUG(0, ("connections_fetch_record failed\n"));
307                 goto done;
308         }
309
310         ret = NT_STATUS_IS_OK(dbrec->store(dbrec, data, TDB_REPLACE));
311         
312 done:
313         TALLOC_FREE( prec );    
314         return ret;
315 }
316
317 /*********************************************************************
318 *********************************************************************/
319
320 BOOL delete_pipe_opendb( smb_np_struct *p )
321 {
322         struct db_record *dbrec;
323         struct pipe_open_rec *prec;
324         TDB_DATA *key;
325         BOOL ret = False;
326         
327         if ( (prec = TALLOC_P( NULL, struct pipe_open_rec)) == NULL ) {
328                 DEBUG(0,("store_pipe_opendb: talloc failed!\n"));
329                 return False;
330         }
331         
332         fill_pipe_open_rec( prec, p );
333         if ( (key = make_pipe_rec_key( prec )) == NULL ) {
334                 goto done;
335         }
336         
337         if (!(dbrec = connections_fetch_record(prec, *key))) {
338                 DEBUG(0, ("connections_fetch_record failed\n"));
339                 goto done;
340         }
341
342         ret = NT_STATUS_IS_OK(dbrec->delete_rec(dbrec));
343         
344 done:
345         TALLOC_FREE( prec );
346         return ret;
347 }