da904e0a04a33c44cf0d24b96791392243147269
[kai/samba.git] / source3 / 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 extern fstring remote_machine;
25 static TDB_CONTEXT *tdb;
26
27 /****************************************************************************
28  Return the connection tdb context (used for message send all).
29 ****************************************************************************/
30
31 TDB_CONTEXT *conn_tdb_ctx(void)
32 {
33         return tdb;
34 }
35
36 /****************************************************************************
37  Delete a connection record.
38 ****************************************************************************/
39
40 BOOL yield_connection(connection_struct *conn,char *name)
41 {
42         struct connections_key key;
43         TDB_DATA kbuf;
44
45         if (!tdb) return False;
46
47         DEBUG(3,("Yielding connection to %s\n",name));
48
49         ZERO_STRUCT(key);
50         key.pid = sys_getpid();
51         key.cnum = conn?conn->cnum:-1;
52         fstrcpy(key.name, name);
53
54         kbuf.dptr = (char *)&key;
55         kbuf.dsize = sizeof(key);
56
57         if (tdb_delete(tdb, kbuf) != 0) {
58                 int dbg_lvl = (!conn && (tdb_error(tdb) == TDB_ERR_NOEXIST)) ? 3 : 0;
59                 DEBUG(dbg_lvl,("yield_connection: tdb_delete for name %s failed with error %s.\n",
60                         name, tdb_errorstr(tdb) ));
61                 return (False);
62         }
63
64         return(True);
65 }
66
67 struct count_stat {
68         pid_t mypid;
69         int curr_connections;
70         char *name;
71         BOOL Clear;
72 };
73
74 /****************************************************************************
75  Count the entries belonging to a service in the connection db.
76 ****************************************************************************/
77
78 static int count_fn( TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *udp)
79 {
80         struct connections_data crec;
81         struct count_stat *cs = (struct count_stat *)udp;
82  
83         if (dbuf.dsize != sizeof(crec))
84                 return 0;
85
86         memcpy(&crec, dbuf.dptr, sizeof(crec));
87  
88     if (crec.cnum == -1)
89                 return 0;
90
91         /* If the pid was not found delete the entry from connections.tdb */
92
93         if (cs->Clear && !process_exists(crec.pid) && (errno == ESRCH)) {
94                 DEBUG(2,("pid %u doesn't exist - deleting connections %d [%s]\n",
95                         (unsigned int)crec.pid, crec.cnum, crec.name));
96                 if (tdb_delete(the_tdb, kbuf) != 0)
97                         DEBUG(0,("count_fn: tdb_delete failed with error %s\n", tdb_errorstr(tdb) ));
98                 return 0;
99         }
100
101         if (strequal(crec.name, cs->name))
102                 cs->curr_connections++;
103
104         return 0;
105 }
106
107 /****************************************************************************
108  Claim an entry in the connections database.
109 ****************************************************************************/
110
111 BOOL claim_connection(connection_struct *conn,char *name,int max_connections,BOOL Clear)
112 {
113         struct connections_key key;
114         struct connections_data crec;
115         TDB_DATA kbuf, dbuf;
116
117         if (!tdb) {
118                 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT, 
119                                O_RDWR | O_CREAT, 0644);
120         }
121         if (!tdb)
122                 return False;
123
124         /*
125          * Enforce the max connections parameter.
126          */
127
128         if (max_connections > 0) {
129                 struct count_stat cs;
130
131                 cs.mypid = sys_getpid();
132                 cs.curr_connections = 0;
133                 cs.name = lp_servicename(SNUM(conn));
134                 cs.Clear = Clear;
135
136                 /*
137                  * This has a race condition, but locking the chain before hand is worse
138                  * as it leads to deadlock.
139                  */
140
141                 if (tdb_traverse(tdb, count_fn, &cs) == -1) {
142                         DEBUG(0,("claim_connection: traverse of connections.tdb failed with error %s.\n",
143                                 tdb_errorstr(tdb) ));
144                         return False;
145                 }
146
147                 if (cs.curr_connections >= max_connections) {
148                         DEBUG(1,("claim_connection: Max connections (%d) exceeded for %s\n",
149                                 max_connections, name ));
150                         return False;
151                 }
152         }
153
154         DEBUG(5,("claiming %s %d\n",name,max_connections));
155
156         ZERO_STRUCT(key);
157         key.pid = sys_getpid();
158         key.cnum = conn?conn->cnum:-1;
159         fstrcpy(key.name, name);
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                 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",
185                         tdb_errorstr(tdb) ));
186                 return False;
187         }
188
189         return True;
190 }