Implemented max connections in a similar way to 2.0.x (scan of connection db).
[sfrench/samba-autobuild/.git] / source / smbd / connection.c
1 /* 
2    Unix SMB/Netbios implementation.
3    Version 1.9.
4    connection claim routines
5    Copyright (C) Andrew Tridgell 1998
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23
24
25 extern fstring remote_machine;
26 static TDB_CONTEXT *tdb;
27
28 extern int DEBUGLEVEL;
29
30 /****************************************************************************
31  Return the connection tdb context (used for message send all).
32 ****************************************************************************/
33
34 TDB_CONTEXT *conn_tdb_ctx(void)
35 {
36         return tdb;
37 }
38
39 /****************************************************************************
40  Delete a connection record.
41 ****************************************************************************/
42
43 BOOL yield_connection(connection_struct *conn,char *name,int max_connections)
44 {
45         struct connections_key key;
46         TDB_DATA kbuf;
47
48         if (!tdb) return False;
49
50         DEBUG(3,("Yielding connection to %s\n",name));
51
52         ZERO_STRUCT(key);
53         key.pid = sys_getpid();
54         key.cnum = conn?conn->cnum:-1;
55         fstrcpy(key.name, name);
56         dos_to_unix(key.name, True);           /* Convert key to unix-codepage */
57
58         kbuf.dptr = (char *)&key;
59         kbuf.dsize = sizeof(key);
60
61         tdb_delete(tdb, kbuf);
62
63         return(True);
64 }
65
66 struct count_stat {
67         pid_t mypid;
68         int curr_connections;
69         char *name;
70 };
71
72 /****************************************************************************
73  Count the entries belonging to a service in the connection db.
74 ****************************************************************************/
75
76 static int count_fn( TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *udp)
77 {
78         struct connections_data crec;
79         struct count_stat *cs = (struct count_stat *)udp;
80  
81         memcpy(&crec, dbuf.dptr, sizeof(crec));
82  
83     if (crec.cnum == -1)
84                 return 0;
85
86         /* if the pid was not found delete the entry from connections.tdb */
87         if (!process_exists(crec.pid) && (errno == ESRCH)) {
88                 DEBUG(2,("pid %u doesn't exist - deleting connections %d [%s]\n",
89                         (unsigned int)crec.pid, crec.cnum, crec.name));
90                 tdb_delete(the_tdb, kbuf);
91                 return 0;
92         }
93
94         if (strequal(crec.name, cs->name))
95                 cs->curr_connections++;
96
97         return 0;
98 }
99
100 /****************************************************************************
101  Claim an entry in the connections database.
102 ****************************************************************************/
103
104 BOOL claim_connection(connection_struct *conn,char *name,int max_connections,BOOL Clear)
105 {
106         struct connections_key key;
107         struct connections_data crec;
108         TDB_DATA kbuf, dbuf;
109         BOOL db_locked = False;
110         BOOL ret = True;
111
112         if (!tdb) {
113                 tdb = tdb_open(lock_path("connections.tdb"), 0, TDB_CLEAR_IF_FIRST, 
114                                O_RDWR | O_CREAT, 0644);
115         }
116         if (!tdb)
117                 return False;
118
119         /*
120          * Enforce the max connections parameter.
121          */
122
123         if (max_connections > 0) {
124                 struct count_stat cs;
125
126                 cs.mypid = sys_getpid();
127                 cs.curr_connections = 0;
128                 cs.name = lp_servicename(SNUM(conn));
129
130                 /*
131                  * Go through and count the connections with the db locked. This is
132                  * slow but essentially what 2.0.x did. JRA.
133                  */
134
135                 if (tdb_lockall(tdb))
136                         return False;
137
138                 db_locked = True;
139
140                 if (tdb_traverse(tdb, count_fn, &cs)) {
141                         ret = False;
142                         goto out;
143                 }
144
145                 if (cs.curr_connections >= max_connections) {
146                         DEBUG(1,("claim_connection: Max connections (%d) exceeded for %s\n",
147                                 max_connections, name ));
148                         ret = False;
149                         goto out;
150                 }
151         }
152
153         DEBUG(5,("claiming %s %d\n",name,max_connections));
154
155         ZERO_STRUCT(key);
156         key.pid = sys_getpid();
157         key.cnum = conn?conn->cnum:-1;
158         fstrcpy(key.name, name);
159         dos_to_unix(key.name, True);           /* Convert key to unix-codepage */
160
161         kbuf.dptr = (char *)&key;
162         kbuf.dsize = sizeof(key);
163
164         /* fill in the crec */
165         ZERO_STRUCT(crec);
166         crec.magic = 0x280267;
167         crec.pid = sys_getpid();
168         crec.cnum = conn?conn->cnum:-1;
169         if (conn) {
170                 crec.uid = conn->uid;
171                 crec.gid = conn->gid;
172                 StrnCpy(crec.name,
173                         lp_servicename(SNUM(conn)),sizeof(crec.name)-1);
174         }
175         crec.start = time(NULL);
176         
177         StrnCpy(crec.machine,remote_machine,sizeof(crec.machine)-1);
178         StrnCpy(crec.addr,conn?conn->client_address:client_addr(),sizeof(crec.addr)-1);
179
180         dbuf.dptr = (char *)&crec;
181         dbuf.dsize = sizeof(crec);
182
183         if (tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) != 0)
184                 ret = False;
185
186   out:
187
188         if (db_locked)
189                 tdb_unlockall(tdb);
190
191         return ret;
192 }
193