s3: Use talloc_tos() in yield_connection()
[sfrench/samba-autobuild/.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
22 /****************************************************************************
23  Delete a connection record.
24 ****************************************************************************/
25
26 bool yield_connection(connection_struct *conn, const char *name)
27 {
28         struct db_record *rec;
29         NTSTATUS status;
30
31         DEBUG(3,("Yielding connection to %s\n",name));
32
33         rec = connections_fetch_entry(talloc_tos(), conn, name);
34         if (rec == NULL) {
35                 DEBUG(0, ("connections_fetch_entry failed\n"));
36                 return False;
37         }
38
39         status = rec->delete_rec(rec);
40         if (!NT_STATUS_IS_OK(status)) {
41                 DEBUG( NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND) ? 3 : 0,
42                        ("deleting connection record returned %s\n",
43                         nt_errstr(status)));
44         }
45
46         TALLOC_FREE(rec);
47         return NT_STATUS_IS_OK(status);
48 }
49
50 struct count_stat {
51         pid_t mypid;
52         int curr_connections;
53         const char *name;
54         bool Clear;
55 };
56
57 /****************************************************************************
58  Count the entries belonging to a service in the connection db.
59 ****************************************************************************/
60
61 static int count_fn(struct db_record *rec,
62                     const struct connections_key *ckey,
63                     const struct connections_data *crec,
64                     void *udp)
65 {
66         struct count_stat *cs = (struct count_stat *)udp;
67
68         if (crec->cnum == -1) {
69                 return 0;
70         }
71
72         /* If the pid was not found delete the entry from connections.tdb */
73
74         if (cs->Clear && !process_exists(crec->pid) && (errno == ESRCH)) {
75                 NTSTATUS status;
76                 DEBUG(2,("pid %s doesn't exist - deleting connections %d [%s]\n",
77                          procid_str_static(&crec->pid), crec->cnum,
78                          crec->servicename));
79
80                 status = rec->delete_rec(rec);
81                 if (!NT_STATUS_IS_OK(status)) {
82                         DEBUG(0,("count_fn: tdb_delete failed with error %s\n",
83                                  nt_errstr(status)));
84                 }
85                 return 0;
86         }
87
88         if (strequal(crec->servicename, cs->name))
89                 cs->curr_connections++;
90
91         return 0;
92 }
93
94 /****************************************************************************
95  Claim an entry in the connections database.
96 ****************************************************************************/
97
98 int count_current_connections( const char *sharename, bool clear  )
99 {
100         struct count_stat cs;
101
102         cs.mypid = sys_getpid();
103         cs.curr_connections = 0;
104         cs.name = sharename;
105         cs.Clear = clear;
106
107         /*
108          * This has a race condition, but locking the chain before hand is worse
109          * as it leads to deadlock.
110          */
111
112         if (connections_forall(count_fn, &cs) == -1) {
113                 DEBUG(0,("count_current_connections: traverse of "
114                          "connections.tdb failed\n"));
115                 return False;
116         }
117
118         return cs.curr_connections;
119 }
120
121 /****************************************************************************
122  Claim an entry in the connections database.
123 ****************************************************************************/
124
125 bool claim_connection(connection_struct *conn, const char *name,
126                       uint32 msg_flags)
127 {
128         struct db_record *rec;
129         struct connections_data crec;
130         TDB_DATA dbuf;
131         NTSTATUS status;
132         char addr[INET6_ADDRSTRLEN];
133
134         DEBUG(5,("claiming [%s]\n", name));
135
136         if (!(rec = connections_fetch_entry(talloc_tos(), conn, name))) {
137                 DEBUG(0, ("connections_fetch_entry failed\n"));
138                 return False;
139         }
140
141         /* fill in the crec */
142         ZERO_STRUCT(crec);
143         crec.magic = 0x280267;
144         crec.pid = procid_self();
145         crec.cnum = conn?conn->cnum:-1;
146         if (conn) {
147                 crec.uid = conn->server_info->utok.uid;
148                 crec.gid = conn->server_info->utok.gid;
149                 strlcpy(crec.servicename, lp_servicename(SNUM(conn)),
150                         sizeof(crec.servicename));
151         }
152         crec.start = time(NULL);
153         crec.bcast_msg_flags = msg_flags;
154
155         strlcpy(crec.machine,get_remote_machine_name(),sizeof(crec.machine));
156         strlcpy(crec.addr,conn?conn->client_address:
157                         client_addr(get_client_fd(),addr,sizeof(addr)),
158                 sizeof(crec.addr));
159
160         dbuf.dptr = (uint8 *)&crec;
161         dbuf.dsize = sizeof(crec);
162
163         status = rec->store(rec, dbuf, TDB_REPLACE);
164
165         TALLOC_FREE(rec);
166
167         if (!NT_STATUS_IS_OK(status)) {
168                 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",
169                          nt_errstr(status)));
170                 return False;
171         }
172
173         return True;
174 }
175
176 bool register_message_flags(bool doreg, uint32 msg_flags)
177 {
178         struct db_record *rec;
179         struct connections_data *pcrec;
180         NTSTATUS status;
181
182         DEBUG(10,("register_message_flags: %s flags 0x%x\n",
183                 doreg ? "adding" : "removing",
184                 (unsigned int)msg_flags ));
185
186         if (!(rec = connections_fetch_entry(NULL, NULL, ""))) {
187                 DEBUG(0, ("connections_fetch_entry failed\n"));
188                 return False;
189         }
190
191         if (rec->value.dsize != sizeof(struct connections_data)) {
192                 DEBUG(0,("register_message_flags: Got wrong record size\n"));
193                 TALLOC_FREE(rec);
194                 return False;
195         }
196
197         pcrec = (struct connections_data *)rec->value.dptr;
198         if (doreg)
199                 pcrec->bcast_msg_flags |= msg_flags;
200         else
201                 pcrec->bcast_msg_flags &= ~msg_flags;
202
203         status = rec->store(rec, rec->value, TDB_REPLACE);
204
205         if (!NT_STATUS_IS_OK(status)) {
206                 DEBUG(0,("register_message_flags: tdb_store failed: %s.\n",
207                          nt_errstr(status)));
208                 TALLOC_FREE(rec);
209                 return False;
210         }
211
212         DEBUG(10,("register_message_flags: new flags 0x%x\n",
213                 (unsigned int)pcrec->bcast_msg_flags ));
214
215         TALLOC_FREE(rec);
216
217         return True;
218 }