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