first cut at using the tdb code for the connections structure, the
[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
27 extern int DEBUGLEVEL;
28
29 /****************************************************************************
30 open the connections database
31 ****************************************************************************/
32 TDB_CONTEXT *open_db(char *name)
33 {
34         pstring fname;
35
36         pstrcpy(fname,lp_lockdir());
37         trim_string(fname,"","/");
38         
39         if (!directory_exist(fname,NULL)) {
40                 mkdir(fname,0755);
41         }
42         
43         pstrcat(fname,"/connections.tdb");
44         
45         return tdb_open(fname, 0, O_RDWR | O_CREAT, 0644);
46 }
47
48
49
50 /****************************************************************************
51 delete a connection record
52 ****************************************************************************/
53 BOOL yield_connection(connection_struct *conn,char *name,int max_connections)
54 {
55         struct connections_key key;
56         TDB_DATA kbuf;
57         TDB_CONTEXT *tdb;
58
59         if (!(tdb = open_db(name))) return False;
60
61         DEBUG(3,("Yielding connection to %s\n",name));
62
63         ZERO_STRUCT(key);
64         key.pid = getpid();
65         if (conn) key.cnum = conn->cnum;
66         fstrcpy(key.name, name);
67
68         kbuf.dptr = (char *)&key;
69         kbuf.dsize = sizeof(key);
70
71         tdb_delete(tdb, kbuf);
72         tdb_close(tdb);
73         return(True);
74 }
75
76
77 /****************************************************************************
78 claim an entry in the connections database
79 ****************************************************************************/
80 int delete_dead(TDB_CONTEXT *tdb, TDB_DATA kbuf, TDB_DATA dbuf)
81 {
82         struct connections_key key;
83         memcpy(&key, kbuf.dptr, sizeof(key));
84         if (!process_exists(key.pid)) tdb_delete(tdb, kbuf);
85         return 0;
86 }
87
88
89 /****************************************************************************
90 claim an entry in the connections database
91 ****************************************************************************/
92 BOOL claim_connection(connection_struct *conn,char *name,int max_connections,BOOL Clear)
93 {
94         struct connections_key key;
95         struct connections_data crec;
96         TDB_DATA kbuf, dbuf;
97         TDB_CONTEXT *tdb;
98         extern int Client;
99
100         if (max_connections <= 0)
101                 return(True);
102         
103         if (!(tdb = open_db(name))) return False;
104
105         DEBUG(5,("claiming %s %d\n",name,max_connections));
106
107         ZERO_STRUCT(key);
108         key.pid = getpid();
109         key.cnum = conn?conn->cnum:-1;
110         fstrcpy(key.name, name);
111
112         kbuf.dptr = (char *)&key;
113         kbuf.dsize = sizeof(key);
114
115         if (Clear) {
116                 tdb_traverse(tdb, delete_dead);
117         }
118
119         /* fill in the crec */
120         ZERO_STRUCT(crec);
121         crec.magic = 0x280267;
122         crec.pid = getpid();
123         crec.cnum = conn?conn->cnum:-1;
124         if (conn) {
125                 crec.uid = conn->uid;
126                 crec.gid = conn->gid;
127                 StrnCpy(crec.name,
128                         lp_servicename(SNUM(conn)),sizeof(crec.name)-1);
129         }
130         crec.start = time(NULL);
131         
132         StrnCpy(crec.machine,remote_machine,sizeof(crec.machine)-1);
133         StrnCpy(crec.addr,conn?conn->client_address:client_addr(Client),sizeof(crec.addr)-1);
134
135         dbuf.dptr = (char *)&crec;
136         dbuf.dsize = sizeof(crec);
137
138         if (tdb_store(tdb, kbuf, dbuf, TDB_REPLACE) != 0) return False;
139
140         tdb_close(tdb);
141
142         return True;
143 }