5a9f4071b4f700fc5b9c80c8fcb2ea25beebbbe1
[samba.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 3 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, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "includes.h"
21 #include "smbd/smbd.h"
22 #include "smbd/globals.h"
23 #include "dbwrap/dbwrap.h"
24 #include "auth.h"
25 #include "../lib/tsocket/tsocket.h"
26
27 /****************************************************************************
28  Delete a connection record.
29 ****************************************************************************/
30
31 bool yield_connection(connection_struct *conn, const char *name)
32 {
33         struct db_record *rec;
34         NTSTATUS status;
35
36         DEBUG(3,("Yielding connection to %s\n",name));
37
38         rec = connections_fetch_entry(talloc_tos(), conn, name);
39         if (rec == NULL) {
40                 DEBUG(0, ("connections_fetch_entry failed\n"));
41                 return False;
42         }
43
44         status = dbwrap_record_delete(rec);
45         if (!NT_STATUS_IS_OK(status)) {
46                 DEBUG( NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND) ? 3 : 0,
47                        ("deleting connection record returned %s\n",
48                         nt_errstr(status)));
49         }
50
51         TALLOC_FREE(rec);
52         return NT_STATUS_IS_OK(status);
53 }
54
55 struct count_stat {
56         int curr_connections;
57         const char *name;
58         bool Clear;
59 };
60
61 /****************************************************************************
62  Count the entries belonging to a service in the connection db.
63 ****************************************************************************/
64
65 static int count_fn(struct db_record *rec,
66                     const struct connections_key *ckey,
67                     const struct connections_data *crec,
68                     void *udp)
69 {
70         struct count_stat *cs = (struct count_stat *)udp;
71
72         if (crec->cnum == -1) {
73                 return 0;
74         }
75
76         /* If the pid was not found delete the entry from connections.tdb */
77
78         if (cs->Clear && !process_exists(crec->pid) && (errno == ESRCH)) {
79                 NTSTATUS status;
80                 DEBUG(2,("pid %s doesn't exist - deleting connections %d [%s]\n",
81                          procid_str_static(&crec->pid), crec->cnum,
82                          crec->servicename));
83
84                 status = dbwrap_record_delete(rec);
85                 if (!NT_STATUS_IS_OK(status)) {
86                         DEBUG(0,("count_fn: tdb_delete failed with error %s\n",
87                                  nt_errstr(status)));
88                 }
89                 return 0;
90         }
91
92         if (strequal(crec->servicename, cs->name))
93                 cs->curr_connections++;
94
95         return 0;
96 }
97
98 /****************************************************************************
99  Claim an entry in the connections database.
100 ****************************************************************************/
101
102 int count_current_connections( const char *sharename, bool clear  )
103 {
104         struct count_stat cs;
105         int ret;
106
107         cs.curr_connections = 0;
108         cs.name = sharename;
109         cs.Clear = clear;
110
111         /*
112          * This has a race condition, but locking the chain before hand is worse
113          * as it leads to deadlock.
114          */
115
116         /*
117          * become_root() because we might have to open connections.tdb
118          * via ctdb, which is not possible without root.
119          */
120         become_root();
121         ret = connections_forall(count_fn, &cs);
122         unbecome_root();
123
124         if (ret < 0) {
125                 DEBUG(0,("count_current_connections: traverse of "
126                          "connections.tdb failed\n"));
127                 return 0;
128         }
129
130         return cs.curr_connections;
131 }
132
133 bool connections_snum_used(struct smbd_server_connection *unused, int snum)
134 {
135         int active;
136
137         active = count_current_connections(lp_servicename(snum), true);
138         if (active > 0) {
139                 return true;
140         }
141
142         return false;
143 }
144
145 /****************************************************************************
146  Claim an entry in the connections database.
147 ****************************************************************************/
148
149 bool claim_connection(connection_struct *conn, const char *name)
150 {
151         struct db_record *rec;
152         struct connections_data crec;
153         char *raddr;
154         TDB_DATA dbuf;
155         NTSTATUS status;
156
157         DEBUG(5,("claiming [%s]\n", name));
158
159         if (!(rec = connections_fetch_entry(talloc_tos(), conn, name))) {
160                 DEBUG(0, ("connections_fetch_entry failed\n"));
161                 return False;
162         }
163
164         /* Make clear that we require the optional unix_token in the source3 code */
165         SMB_ASSERT(conn->session_info->unix_token);
166
167         /* fill in the crec */
168         ZERO_STRUCT(crec);
169         crec.magic = 0x280267;
170         crec.pid = sconn_server_id(conn->sconn);
171         crec.cnum = conn->cnum;
172         crec.uid = conn->session_info->unix_token->uid;
173         crec.gid = conn->session_info->unix_token->gid;
174         strlcpy(crec.servicename, lp_servicename(SNUM(conn)),
175                 sizeof(crec.servicename));
176         crec.start = time(NULL);
177
178         raddr = tsocket_address_inet_addr_string(conn->sconn->remote_address,
179                                                  talloc_tos());
180         if (raddr == NULL) {
181                 return false;
182         }
183
184         strlcpy(crec.machine,get_remote_machine_name(),sizeof(crec.machine));
185         strlcpy(crec.addr, raddr, sizeof(crec.addr));
186
187         dbuf.dptr = (uint8 *)&crec;
188         dbuf.dsize = sizeof(crec);
189
190         status = dbwrap_record_store(rec, dbuf, TDB_REPLACE);
191
192         TALLOC_FREE(rec);
193
194         if (!NT_STATUS_IS_OK(status)) {
195                 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",
196                          nt_errstr(status)));
197                 return False;
198         }
199
200         return True;
201 }