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