Check sizes of data entries in connections.tdb before deciding they're crecs...
[abartlet/samba.git/.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
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         BOOL Clear;
71 };
72
73 /****************************************************************************
74  Count the entries belonging to a service in the connection db.
75 ****************************************************************************/
76
77 static int count_fn( TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *udp)
78 {
79         struct connections_data crec;
80         struct count_stat *cs = (struct count_stat *)udp;
81  
82         if (dbuf.dsize != sizeof(crec))
83                 return 0;
84
85         memcpy(&crec, dbuf.dptr, sizeof(crec));
86  
87         if (crec.cnum == -1)
88                 return 0;
89
90         /* if the pid was not found delete the entry from connections.tdb */
91         if (cs->Clear && !process_exists(crec.pid)) {
92                 DEBUG(2,("pid %u doesn't exist - deleting connections %d [%s]\n",
93                         (unsigned int)crec.pid, crec.cnum, crec.name));
94                 tdb_delete(the_tdb, kbuf);
95                 return 0;
96         }
97
98         if (cs && strequal(crec.name, cs->name))
99                 cs->curr_connections++;
100
101         return 0;
102 }
103
104 /****************************************************************************
105  Claim an entry in the connections database.
106 ****************************************************************************/
107
108 BOOL claim_connection(connection_struct *conn,char *name,int max_connections,BOOL Clear)
109 {
110         struct connections_key key;
111         struct connections_data crec;
112         TDB_DATA kbuf, dbuf;
113
114         if (!tdb) {
115                 tdb = tdb_open(lock_path("connections.tdb"), 0, TDB_CLEAR_IF_FIRST, 
116                                O_RDWR | O_CREAT, 0644);
117         }
118         if (!tdb)
119                 return False;
120
121         /*
122          * Enforce the max connections parameter.
123          */
124
125         if (max_connections > 0) {
126                 struct count_stat cs;
127
128                 cs.mypid = sys_getpid();
129                 cs.curr_connections = 0;
130                 cs.name = lp_servicename(SNUM(conn));
131                 cs.Clear = Clear;
132
133                 /*
134                  * Go through and count the connections
135                  */
136                 if (tdb_traverse(tdb, count_fn, &cs) == -1) {
137                         DEBUG(0,("claim_connection: traverse of connections.tdb failed.\n"));
138                         return False;
139                 }
140
141                 if (cs.curr_connections >= max_connections) {
142                         DEBUG(1,("claim_connection: Max connections (%d) exceeded for %s\n",
143                                 max_connections, name ));
144                         return False;
145                 }
146         }
147
148         DEBUG(5,("claiming %s %d\n",name,max_connections));
149
150         ZERO_STRUCT(key);
151         key.pid = sys_getpid();
152         key.cnum = conn?conn->cnum:-1;
153         fstrcpy(key.name, name);
154         dos_to_unix(key.name, True);           /* Convert key to unix-codepage */
155
156         kbuf.dptr = (char *)&key;
157         kbuf.dsize = sizeof(key);
158
159         /* fill in the crec */
160         ZERO_STRUCT(crec);
161         crec.magic = 0x280267;
162         crec.pid = sys_getpid();
163         crec.cnum = conn?conn->cnum:-1;
164         if (conn) {
165                 crec.uid = conn->uid;
166                 crec.gid = conn->gid;
167                 StrnCpy(crec.name,
168                         lp_servicename(SNUM(conn)),sizeof(crec.name)-1);
169         }
170         crec.start = time(NULL);
171         
172         StrnCpy(crec.machine,remote_machine,sizeof(crec.machine)-1);
173         StrnCpy(crec.addr,conn?conn->client_address:client_addr(),sizeof(crec.addr)-1);
174
175         dbuf.dptr = (char *)&crec;
176         dbuf.dsize = sizeof(crec);
177
178         if (tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) != 0)
179                 return False;
180
181         return True;
182 }
183
184 #if 0
185 /****************************************************************************
186  Use the count function to clean any dead records. Shouldn't be needed...
187 ****************************************************************************/
188
189 void clean_connection_db(void)
190 {
191         if (tdb_traverse(tdb, count_fn, NULL) == -1)
192                 DEBUG(0,("clean_connection_db: traverse of connections.tdb failed.\n"));
193 }
194 #endif