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