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