merge from 2.2
[sfrench/samba-autobuild/.git] / source / 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 BOOL yield_connection(connection_struct *conn,char *name,int max_connections)
43 {
44         struct connections_key key;
45         TDB_DATA kbuf;
46
47         if (!tdb) return False;
48
49         DEBUG(3,("Yielding connection to %s\n",name));
50
51         ZERO_STRUCT(key);
52         key.pid = sys_getpid();
53         key.cnum = conn?conn->cnum:-1;
54         fstrcpy(key.name, name);
55         dos_to_unix(key.name, True);           /* Convert key to unix-codepage */
56
57         kbuf.dptr = (char *)&key;
58         kbuf.dsize = sizeof(key);
59
60         tdb_delete(tdb, kbuf);
61
62         return(True);
63 }
64
65
66 /****************************************************************************
67 claim an entry in the connections database
68 ****************************************************************************/
69 BOOL claim_connection(connection_struct *conn,char *name,int max_connections,BOOL Clear)
70 {
71         struct connections_key key;
72         struct connections_data crec;
73         TDB_DATA kbuf, dbuf;
74
75         if (!tdb) {
76                 tdb = tdb_open(lock_path("connections.tdb"), 0, TDB_CLEAR_IF_FIRST, 
77                                O_RDWR | O_CREAT, 0644);
78         }
79         if (!tdb) return False;
80
81         DEBUG(5,("claiming %s %d\n",name,max_connections));
82
83         ZERO_STRUCT(key);
84         key.pid = sys_getpid();
85         key.cnum = conn?conn->cnum:-1;
86         fstrcpy(key.name, name);
87         dos_to_unix(key.name, True);           /* Convert key to unix-codepage */
88
89         kbuf.dptr = (char *)&key;
90         kbuf.dsize = sizeof(key);
91
92         /* fill in the crec */
93         ZERO_STRUCT(crec);
94         crec.magic = 0x280267;
95         crec.pid = sys_getpid();
96         crec.cnum = conn?conn->cnum:-1;
97         if (conn) {
98                 crec.uid = conn->uid;
99                 crec.gid = conn->gid;
100                 StrnCpy(crec.name,
101                         lp_servicename(SNUM(conn)),sizeof(crec.name)-1);
102         }
103         crec.start = time(NULL);
104         
105         StrnCpy(crec.machine,remote_machine,sizeof(crec.machine)-1);
106         StrnCpy(crec.addr,conn?conn->client_address:client_addr(),sizeof(crec.addr)-1);
107
108         dbuf.dptr = (char *)&crec;
109         dbuf.dsize = sizeof(crec);
110
111         if (tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) != 0) return False;
112
113         return True;
114 }
115