c2718d4d7035908234f5ecf446f4a3db601fe782
[abartlet/samba.git/.git] / source3 / 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 TDB_CONTEXT *tdb;
24
25 /****************************************************************************
26  Return the connection tdb context (used for message send all).
27 ****************************************************************************/
28
29 TDB_CONTEXT *conn_tdb_ctx(void)
30 {
31         if (!tdb)
32                 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT, 
33                                O_RDWR | O_CREAT, 0644);
34
35         return tdb;
36 }
37
38 static void make_conn_key(connection_struct *conn, const char *name, TDB_DATA *pkbuf, struct connections_key *pkey)
39 {
40         ZERO_STRUCTP(pkey);
41         ZERO_STRUCTP(pkbuf);
42         pkey->pid = sys_getpid();
43         pkey->cnum = conn?conn->cnum:-1;
44         fstrcpy(pkey->name, name);
45
46         pkbuf->dptr = (char *)pkey;
47         pkbuf->dsize = sizeof(*pkey);
48 }
49
50 /****************************************************************************
51  Delete a connection record.
52 ****************************************************************************/
53
54 BOOL yield_connection(connection_struct *conn, const char *name)
55 {
56         struct connections_key key;
57         TDB_DATA kbuf;
58
59         if (!tdb)
60                 return False;
61
62         DEBUG(3,("Yielding connection to %s\n",name));
63
64         make_conn_key(conn, name, &kbuf, &key);
65
66         if (tdb_delete(tdb, kbuf) != 0) {
67                 int dbg_lvl = (!conn && (tdb_error(tdb) == TDB_ERR_NOEXIST)) ? 3 : 0;
68                 DEBUG(dbg_lvl,("yield_connection: tdb_delete for name %s failed with error %s.\n",
69                         name, tdb_errorstr(tdb) ));
70                 return (False);
71         }
72
73         return(True);
74 }
75
76 struct count_stat {
77         pid_t mypid;
78         int curr_connections;
79         char *name;
80         BOOL Clear;
81 };
82
83 /****************************************************************************
84  Count the entries belonging to a service in the connection db.
85 ****************************************************************************/
86
87 static int count_fn( TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *udp)
88 {
89         struct connections_data crec;
90         struct count_stat *cs = (struct count_stat *)udp;
91  
92         if (dbuf.dsize != sizeof(crec))
93                 return 0;
94
95         memcpy(&crec, dbuf.dptr, sizeof(crec));
96  
97         if (crec.cnum == -1)
98                 return 0;
99
100         /* If the pid was not found delete the entry from connections.tdb */
101
102         if (cs->Clear && !process_exists(crec.pid) && (errno == ESRCH)) {
103                 DEBUG(2,("pid %u doesn't exist - deleting connections %d [%s]\n",
104                         (unsigned int)crec.pid, crec.cnum, crec.name));
105                 if (tdb_delete(the_tdb, kbuf) != 0)
106                         DEBUG(0,("count_fn: tdb_delete failed with error %s\n", tdb_errorstr(tdb) ));
107                 return 0;
108         }
109
110         if (strequal(crec.name, cs->name))
111                 cs->curr_connections++;
112
113         return 0;
114 }
115
116 /****************************************************************************
117  Claim an entry in the connections database.
118 ****************************************************************************/
119
120 BOOL claim_connection(connection_struct *conn, const char *name,int max_connections,BOOL Clear, uint32 msg_flags)
121 {
122         struct connections_key key;
123         struct connections_data crec;
124         TDB_DATA kbuf, dbuf;
125
126         if (!tdb)
127                 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT, 
128                                O_RDWR | O_CREAT, 0644);
129
130         if (!tdb)
131                 return False;
132
133         /*
134          * Enforce the max connections parameter.
135          */
136
137         if (max_connections > 0) {
138                 struct count_stat cs;
139
140                 cs.mypid = sys_getpid();
141                 cs.curr_connections = 0;
142                 cs.name = lp_servicename(SNUM(conn));
143                 cs.Clear = Clear;
144
145                 /*
146                  * This has a race condition, but locking the chain before hand is worse
147                  * as it leads to deadlock.
148                  */
149
150                 if (tdb_traverse(tdb, count_fn, &cs) == -1) {
151                         DEBUG(0,("claim_connection: traverse of connections.tdb failed with error %s.\n",
152                                 tdb_errorstr(tdb) ));
153                         return False;
154                 }
155
156                 if (cs.curr_connections >= max_connections) {
157                         DEBUG(1,("claim_connection: Max connections (%d) exceeded for %s\n",
158                                 max_connections, name ));
159                         return False;
160                 }
161         }
162
163         DEBUG(5,("claiming %s %d\n",name,max_connections));
164
165         make_conn_key(conn, name, &kbuf, &key);
166
167         /* fill in the crec */
168         ZERO_STRUCT(crec);
169         crec.magic = 0x280267;
170         crec.pid = sys_getpid();
171         crec.cnum = conn?conn->cnum:-1;
172         if (conn) {
173                 crec.uid = conn->uid;
174                 crec.gid = conn->gid;
175                 safe_strcpy(crec.name,
176                             lp_servicename(SNUM(conn)),sizeof(crec.name)-1);
177         }
178         crec.start = time(NULL);
179         crec.bcast_msg_flags = msg_flags;
180         
181         safe_strcpy(crec.machine,get_remote_machine_name(),sizeof(crec.machine)-1);
182         safe_strcpy(crec.addr,conn?conn->client_address:client_addr(),sizeof(crec.addr)-1);
183
184         dbuf.dptr = (char *)&crec;
185         dbuf.dsize = sizeof(crec);
186
187         if (tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) != 0) {
188                 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",
189                         tdb_errorstr(tdb) ));
190                 return False;
191         }
192
193         return True;
194 }
195
196 BOOL register_message_flags(BOOL doreg, uint32 msg_flags)
197 {
198         struct connections_key key;
199         struct connections_data *pcrec;
200         TDB_DATA kbuf, dbuf;
201
202         if (!tdb)
203                 return False;
204
205         DEBUG(10,("register_message_flags: %s flags 0x%x\n",
206                 doreg ? "adding" : "removing",
207                 (unsigned int)msg_flags ));
208
209         make_conn_key(NULL, "", &kbuf, &key);
210
211         dbuf = tdb_fetch(tdb, kbuf);
212         if (!dbuf.dptr) {
213                 DEBUG(0,("register_message_flags: tdb_fetch failed\n"));
214                 return False;
215         }
216
217         pcrec = (struct connections_data *)dbuf.dptr;
218         pcrec->bcast_msg_flags = msg_flags;
219         if (doreg)
220                 pcrec->bcast_msg_flags |= msg_flags;
221         else
222                 pcrec->bcast_msg_flags &= ~msg_flags;
223
224         if (tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) != 0) {
225                 DEBUG(0,("register_message_flags: tdb_store failed with error %s.\n",
226                         tdb_errorstr(tdb) ));
227                 SAFE_FREE(dbuf.dptr);
228                 return False;
229         }
230
231         DEBUG(10,("register_message_flags: new flags 0x%x\n",
232                 (unsigned int)pcrec->bcast_msg_flags ));
233
234         SAFE_FREE(dbuf.dptr);
235         return True;
236 }