Removed version number from file header.
[ira/wip.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 extern fstring remote_machine;
24 static TDB_CONTEXT *tdb;
25
26 /****************************************************************************
27  Return the connection tdb context (used for message send all).
28 ****************************************************************************/
29
30 TDB_CONTEXT *conn_tdb_ctx(void)
31 {
32         return tdb;
33 }
34
35 /****************************************************************************
36  Delete a connection record.
37 ****************************************************************************/
38
39 BOOL yield_connection(connection_struct *conn,char *name)
40 {
41         struct connections_key key;
42         TDB_DATA kbuf;
43
44         if (!tdb) return False;
45
46         DEBUG(3,("Yielding connection to %s\n",name));
47
48         ZERO_STRUCT(key);
49         key.pid = sys_getpid();
50         key.cnum = conn?conn->cnum:-1;
51         fstrcpy(key.name, name);
52
53         kbuf.dptr = (char *)&key;
54         kbuf.dsize = sizeof(key);
55
56         if (tdb_delete(tdb, kbuf) != 0) {
57                 int dbg_lvl = (!conn && (tdb_error(tdb) == TDB_ERR_NOEXIST)) ? 3 : 0;
58                 DEBUG(dbg_lvl,("yield_connection: tdb_delete for name %s failed with error %s.\n",
59                         name, tdb_errorstr(tdb) ));
60                 return (False);
61         }
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
92         if (cs->Clear && !process_exists(crec.pid) && (errno == ESRCH)) {
93                 DEBUG(2,("pid %u doesn't exist - deleting connections %d [%s]\n",
94                         (unsigned int)crec.pid, crec.cnum, crec.name));
95                 if (tdb_delete(the_tdb, kbuf) != 0)
96                         DEBUG(0,("count_fn: tdb_delete failed with error %s\n", tdb_errorstr(tdb) ));
97                 return 0;
98         }
99
100         if (strequal(crec.name, cs->name))
101                 cs->curr_connections++;
102
103         return 0;
104 }
105
106 /****************************************************************************
107  Claim an entry in the connections database.
108 ****************************************************************************/
109
110 BOOL claim_connection(connection_struct *conn,char *name,int max_connections,BOOL Clear)
111 {
112         struct connections_key key;
113         struct connections_data crec;
114         TDB_DATA kbuf, dbuf;
115
116         if (!tdb) {
117                 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT, 
118                                O_RDWR | O_CREAT, 0644);
119         }
120         if (!tdb)
121                 return False;
122
123         /*
124          * Enforce the max connections parameter.
125          */
126
127         if (max_connections > 0) {
128                 struct count_stat cs;
129
130                 cs.mypid = sys_getpid();
131                 cs.curr_connections = 0;
132                 cs.name = lp_servicename(SNUM(conn));
133                 cs.Clear = Clear;
134
135                 /*
136                  * This has a race condition, but locking the chain before hand is worse
137                  * as it leads to deadlock.
138                  */
139
140                 if (tdb_traverse(tdb, count_fn, &cs) == -1) {
141                         DEBUG(0,("claim_connection: traverse of connections.tdb failed with error %s.\n",
142                                 tdb_errorstr(tdb) ));
143                         return False;
144                 }
145
146                 if (cs.curr_connections >= max_connections) {
147                         DEBUG(1,("claim_connection: Max connections (%d) exceeded for %s\n",
148                                 max_connections, name ));
149                         return False;
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
160         kbuf.dptr = (char *)&key;
161         kbuf.dsize = sizeof(key);
162
163         /* fill in the crec */
164         ZERO_STRUCT(crec);
165         crec.magic = 0x280267;
166         crec.pid = sys_getpid();
167         crec.cnum = conn?conn->cnum:-1;
168         if (conn) {
169                 crec.uid = conn->uid;
170                 crec.gid = conn->gid;
171                 StrnCpy(crec.name,
172                         lp_servicename(SNUM(conn)),sizeof(crec.name)-1);
173         }
174         crec.start = time(NULL);
175         
176         StrnCpy(crec.machine,remote_machine,sizeof(crec.machine)-1);
177         StrnCpy(crec.addr,conn?conn->client_address:client_addr(),sizeof(crec.addr)-1);
178
179         dbuf.dptr = (char *)&crec;
180         dbuf.dsize = sizeof(crec);
181
182         if (tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) != 0) {
183                 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",
184                         tdb_errorstr(tdb) ));
185                 return False;
186         }
187
188         return True;
189 }