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