r23784: use the GPLv3 boilerplate as recommended by the FSF and the license text
[tprouty/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 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
26 BOOL 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
49 struct 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
60 static 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 (cs->name) {
88                 /* We are counting all the connections to a given share. */
89                 if (strequal(crec->servicename, cs->name)) {
90                         cs->curr_connections++;
91                 }
92         } else {
93                 /* We are counting all the connections. Static registrations
94                  * like the lpq backgroud process and the smbd daemon process
95                  * have a cnum of -1, so won't be counted here.
96                  */
97                 cs->curr_connections++;
98         }
99
100         return 0;
101 }
102
103 /****************************************************************************
104  Claim an entry in the connections database.
105 ****************************************************************************/
106
107 int count_current_connections( const char *sharename, BOOL clear  )
108 {
109         struct count_stat cs;
110
111         cs.mypid = sys_getpid();
112         cs.curr_connections = 0;
113         cs.name = sharename;
114         cs.Clear = clear;
115
116         /*
117          * This has a race condition, but locking the chain before hand is worse
118          * as it leads to deadlock.
119          */
120
121         if (connections_forall(count_fn, &cs) == -1) {
122                 DEBUG(0,("count_current_connections: traverse of "
123                          "connections.tdb failed\n"));
124                 DEBUGADD(0, ("count_current_connections: connection count of %d might not be accurate",
125                             cs.curr_connections));
126         }
127
128         /* If the traverse failed part-way through, we at least return
129          * as many connections as we had already counted. If it failed
130          * right at the start, we will return 0, which is about all we
131          * can do anywway.
132          */
133
134         return cs.curr_connections;
135 }
136
137 /****************************************************************************
138  Count the number of connections open across all shares.
139 ****************************************************************************/
140
141 int count_all_current_connections(void)
142 {
143         return count_current_connections(NULL, True /* clear stale entries */);
144 }
145
146 /****************************************************************************
147  Claim an entry in the connections database.
148 ****************************************************************************/
149
150 BOOL claim_connection(connection_struct *conn, const char *name,
151                       uint32 msg_flags)
152 {
153         struct db_record *rec;
154         struct connections_data crec;
155         TDB_DATA dbuf;
156         NTSTATUS status;
157
158         DEBUG(5,("claiming [%s]\n", name));
159
160         if (!(rec = connections_fetch_entry(NULL, conn, name))) {
161                 DEBUG(0, ("connections_fetch_entry failed\n"));
162                 return False;
163         }
164
165         /* fill in the crec */
166         ZERO_STRUCT(crec);
167         crec.magic = 0x280267;
168         crec.pid = procid_self();
169         crec.cnum = conn?conn->cnum:-1;
170         if (conn) {
171                 crec.uid = conn->uid;
172                 crec.gid = conn->gid;
173                 strlcpy(crec.servicename, lp_servicename(SNUM(conn)),
174                         sizeof(crec.servicename));
175         }
176         crec.start = time(NULL);
177         crec.bcast_msg_flags = msg_flags;
178         
179         strlcpy(crec.machine,get_remote_machine_name(),sizeof(crec.machine));
180         strlcpy(crec.addr,conn?conn->client_address:client_addr(),
181                 sizeof(crec.addr));
182
183         dbuf.dptr = (uint8 *)&crec;
184         dbuf.dsize = sizeof(crec);
185
186         status = rec->store(rec, dbuf, TDB_REPLACE);
187
188         TALLOC_FREE(rec);
189
190         if (!NT_STATUS_IS_OK(status)) {
191                 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",
192                          nt_errstr(status)));
193                 return False;
194         }
195
196         return True;
197 }
198
199 BOOL register_message_flags(BOOL doreg, uint32 msg_flags)
200 {
201         struct db_record *rec;
202         struct connections_data *pcrec;
203         NTSTATUS status;
204
205         DEBUG(10,("register_message_flags: %s flags 0x%x\n",
206                 doreg ? "adding" : "removing",
207                 (unsigned int)msg_flags ));
208
209         if (!(rec = connections_fetch_entry(NULL, NULL, NULL))) {
210                 DEBUG(0, ("connections_fetch_entry failed\n"));
211                 return False;
212         }
213
214         if (rec->value.dsize != sizeof(struct connections_data)) {
215                 DEBUG(0,("register_message_flags: Got wrong record size\n"));
216                 TALLOC_FREE(rec);
217                 return False;
218         }
219
220         pcrec = (struct connections_data *)rec->value.dptr;
221         if (doreg)
222                 pcrec->bcast_msg_flags |= msg_flags;
223         else
224                 pcrec->bcast_msg_flags &= ~msg_flags;
225
226         status = rec->store(rec, rec->value, TDB_REPLACE);
227
228         TALLOC_FREE(rec);
229
230         if (!NT_STATUS_IS_OK(status)) {
231                 DEBUG(0,("register_message_flags: tdb_store failed: %s.\n",
232                          nt_errstr(status)));
233                 return False;
234         }
235
236         DEBUG(10,("register_message_flags: new flags 0x%x\n",
237                 (unsigned int)pcrec->bcast_msg_flags ));
238
239         return True;
240 }
241
242 /*********************************************************************
243 *********************************************************************/
244
245 static TDB_DATA* make_pipe_rec_key( struct pipe_open_rec *prec )
246 {
247         TDB_DATA *kbuf = NULL;
248         fstring key_string;
249         
250         if ( !prec )
251                 return NULL;
252         
253         if ( (kbuf = TALLOC_P(prec, TDB_DATA)) == NULL ) {
254                 return NULL;
255         }
256         
257         snprintf( key_string, sizeof(key_string), "%s/%d/%d",
258                 prec->name, procid_to_pid(&prec->pid), prec->pnum );
259                 
260         *kbuf = string_term_tdb_data(talloc_strdup(prec, key_string));
261         if (kbuf->dptr == NULL )
262                 return NULL;
263
264         return kbuf;
265 }
266
267 /*********************************************************************
268 *********************************************************************/
269
270 static void fill_pipe_open_rec( struct pipe_open_rec *prec, smb_np_struct *p )
271 {
272         prec->pid = pid_to_procid(sys_getpid());
273         prec->pnum = p->pnum;
274         prec->uid = geteuid();
275         fstrcpy( prec->name, p->name );
276
277         return;
278 }
279
280 /*********************************************************************
281 *********************************************************************/
282
283 BOOL store_pipe_opendb( smb_np_struct *p )
284 {
285         struct db_record *dbrec;
286         struct pipe_open_rec *prec;
287         TDB_DATA *key;
288         TDB_DATA data;
289         BOOL ret = False;
290         
291         if ( (prec = TALLOC_P( NULL, struct pipe_open_rec)) == NULL ) {
292                 DEBUG(0,("store_pipe_opendb: talloc failed!\n"));
293                 return False;
294         }
295         
296         fill_pipe_open_rec( prec, p );
297         if ( (key = make_pipe_rec_key( prec )) == NULL ) {
298                 goto done;
299         }
300         
301         data.dptr = (uint8 *)prec;
302         data.dsize = sizeof(struct pipe_open_rec);
303
304         if (!(dbrec = connections_fetch_record(prec, *key))) {
305                 DEBUG(0, ("connections_fetch_record failed\n"));
306                 goto done;
307         }
308
309         ret = NT_STATUS_IS_OK(dbrec->store(dbrec, data, TDB_REPLACE));
310         
311 done:
312         TALLOC_FREE( prec );    
313         return ret;
314 }
315
316 /*********************************************************************
317 *********************************************************************/
318
319 BOOL delete_pipe_opendb( smb_np_struct *p )
320 {
321         struct db_record *dbrec;
322         struct pipe_open_rec *prec;
323         TDB_DATA *key;
324         BOOL ret = False;
325         
326         if ( (prec = TALLOC_P( NULL, struct pipe_open_rec)) == NULL ) {
327                 DEBUG(0,("store_pipe_opendb: talloc failed!\n"));
328                 return False;
329         }
330         
331         fill_pipe_open_rec( prec, p );
332         if ( (key = make_pipe_rec_key( prec )) == NULL ) {
333                 goto done;
334         }
335         
336         if (!(dbrec = connections_fetch_record(prec, *key))) {
337                 DEBUG(0, ("connections_fetch_record failed\n"));
338                 goto done;
339         }
340
341         ret = NT_STATUS_IS_OK(dbrec->delete_rec(dbrec));
342         
343 done:
344         TALLOC_FREE( prec );
345         return ret;
346 }