Merger krb5-config and libtinfo to SAMBA_3_0
[Samba/gebeck_regimport.git] / source3 / smbd / connection.c
bloba7636e889e330a6a8d1a39ef42919bb8c2bd7fcf
1 /*
2 Unix SMB/CIFS implementation.
3 connection claim routines
4 Copyright (C) Andrew Tridgell 1998
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.
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.
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.
21 #include "includes.h"
23 static TDB_CONTEXT *tdb;
25 /****************************************************************************
26 Return the connection tdb context (used for message send all).
27 ****************************************************************************/
29 TDB_CONTEXT *conn_tdb_ctx(void)
31 if (!tdb)
32 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
33 O_RDWR | O_CREAT, 0644);
35 return tdb;
38 static void make_conn_key(connection_struct *conn, const char *name, TDB_DATA *pkbuf, struct connections_key *pkey)
40 ZERO_STRUCTP(pkey);
41 pkey->pid = sys_getpid();
42 pkey->cnum = conn?conn->cnum:-1;
43 fstrcpy(pkey->name, name);
45 pkbuf->dptr = (char *)pkey;
46 pkbuf->dsize = sizeof(*pkey);
49 /****************************************************************************
50 Delete a connection record.
51 ****************************************************************************/
53 BOOL yield_connection(connection_struct *conn, const char *name)
55 struct connections_key key;
56 TDB_DATA kbuf;
58 if (!tdb)
59 return False;
61 DEBUG(3,("Yielding connection to %s\n",name));
63 make_conn_key(conn, name, &kbuf, &key);
65 if (tdb_delete(tdb, kbuf) != 0) {
66 int dbg_lvl = (!conn && (tdb_error(tdb) == TDB_ERR_NOEXIST)) ? 3 : 0;
67 DEBUG(dbg_lvl,("yield_connection: tdb_delete for name %s failed with error %s.\n",
68 name, tdb_errorstr(tdb) ));
69 return (False);
72 return(True);
75 struct count_stat {
76 pid_t mypid;
77 int curr_connections;
78 char *name;
79 BOOL Clear;
82 /****************************************************************************
83 Count the entries belonging to a service in the connection db.
84 ****************************************************************************/
86 static int count_fn( TDB_CONTEXT *the_tdb, TDB_DATA kbuf, TDB_DATA dbuf, void *udp)
88 struct connections_data crec;
89 struct count_stat *cs = (struct count_stat *)udp;
91 if (dbuf.dsize != sizeof(crec))
92 return 0;
94 memcpy(&crec, dbuf.dptr, sizeof(crec));
96 if (crec.cnum == -1)
97 return 0;
99 /* If the pid was not found delete the entry from connections.tdb */
101 if (cs->Clear && !process_exists(crec.pid) && (errno == ESRCH)) {
102 DEBUG(2,("pid %u doesn't exist - deleting connections %d [%s]\n",
103 (unsigned int)crec.pid, crec.cnum, crec.name));
104 if (tdb_delete(the_tdb, kbuf) != 0)
105 DEBUG(0,("count_fn: tdb_delete failed with error %s\n", tdb_errorstr(tdb) ));
106 return 0;
109 if (strequal(crec.name, cs->name))
110 cs->curr_connections++;
112 return 0;
115 /****************************************************************************
116 Claim an entry in the connections database.
117 ****************************************************************************/
119 BOOL claim_connection(connection_struct *conn, const char *name,int max_connections,BOOL Clear, uint32 msg_flags)
121 struct connections_key key;
122 struct connections_data crec;
123 TDB_DATA kbuf, dbuf;
125 if (!tdb)
126 tdb = tdb_open_log(lock_path("connections.tdb"), 0, TDB_CLEAR_IF_FIRST|TDB_DEFAULT,
127 O_RDWR | O_CREAT, 0644);
129 if (!tdb)
130 return False;
133 * Enforce the max connections parameter.
136 if (max_connections > 0) {
137 struct count_stat cs;
139 cs.mypid = sys_getpid();
140 cs.curr_connections = 0;
141 cs.name = lp_servicename(SNUM(conn));
142 cs.Clear = Clear;
145 * This has a race condition, but locking the chain before hand is worse
146 * as it leads to deadlock.
149 if (tdb_traverse(tdb, count_fn, &cs) == -1) {
150 DEBUG(0,("claim_connection: traverse of connections.tdb failed with error %s.\n",
151 tdb_errorstr(tdb) ));
152 return False;
155 if (cs.curr_connections >= max_connections) {
156 DEBUG(1,("claim_connection: Max connections (%d) exceeded for %s\n",
157 max_connections, name ));
158 return False;
162 DEBUG(5,("claiming %s %d\n",name,max_connections));
164 make_conn_key(conn, name, &kbuf, &key);
166 /* fill in the crec */
167 ZERO_STRUCT(crec);
168 crec.magic = 0x280267;
169 crec.pid = sys_getpid();
170 crec.cnum = conn?conn->cnum:-1;
171 if (conn) {
172 crec.uid = conn->uid;
173 crec.gid = conn->gid;
174 StrnCpy(crec.name,
175 lp_servicename(SNUM(conn)),sizeof(crec.name)-1);
177 crec.start = time(NULL);
178 crec.bcast_msg_flags = msg_flags;
180 StrnCpy(crec.machine,get_remote_machine_name(),sizeof(crec.machine)-1);
181 StrnCpy(crec.addr,conn?conn->client_address:client_addr(),sizeof(crec.addr)-1);
183 dbuf.dptr = (char *)&crec;
184 dbuf.dsize = sizeof(crec);
186 if (tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) != 0) {
187 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",
188 tdb_errorstr(tdb) ));
189 return False;
192 return True;
195 BOOL register_message_flags(BOOL doreg, uint32 msg_flags)
197 struct connections_key key;
198 struct connections_data *pcrec;
199 TDB_DATA kbuf, dbuf;
201 if (!tdb)
202 return False;
204 DEBUG(10,("register_message_flags: %s flags 0x%x\n",
205 doreg ? "adding" : "removing",
206 (unsigned int)msg_flags ));
208 make_conn_key(NULL, "", &kbuf, &key);
210 dbuf = tdb_fetch(tdb, kbuf);
211 if (!dbuf.dptr) {
212 DEBUG(0,("register_message_flags: tdb_fetch failed\n"));
213 return False;
216 pcrec = (struct connections_data *)dbuf.dptr;
217 pcrec->bcast_msg_flags = msg_flags;
218 if (doreg)
219 pcrec->bcast_msg_flags |= msg_flags;
220 else
221 pcrec->bcast_msg_flags &= ~msg_flags;
223 if (tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) != 0) {
224 DEBUG(0,("register_message_flags: tdb_store failed with error %s.\n",
225 tdb_errorstr(tdb) ));
226 SAFE_FREE(dbuf.dptr);
227 return False;
230 DEBUG(10,("register_message_flags: new flags 0x%x\n",
231 (unsigned int)pcrec->bcast_msg_flags ));
233 SAFE_FREE(dbuf.dptr);
234 return True;