s3: include smbd/smbd.h where needed.
[nivanova/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 #include "smbd/smbd.h"
22 #include "smbd/globals.h"
23 #include "dbwrap.h"
24
25 /****************************************************************************
26  Delete a connection record.
27 ****************************************************************************/
28
29 bool yield_connection(connection_struct *conn, const char *name)
30 {
31         struct db_record *rec;
32         NTSTATUS status;
33
34         DEBUG(3,("Yielding connection to %s\n",name));
35
36         rec = connections_fetch_entry(talloc_tos(), conn, name);
37         if (rec == NULL) {
38                 DEBUG(0, ("connections_fetch_entry failed\n"));
39                 return False;
40         }
41
42         status = rec->delete_rec(rec);
43         if (!NT_STATUS_IS_OK(status)) {
44                 DEBUG( NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND) ? 3 : 0,
45                        ("deleting connection record returned %s\n",
46                         nt_errstr(status)));
47         }
48
49         TALLOC_FREE(rec);
50         return NT_STATUS_IS_OK(status);
51 }
52
53 struct count_stat {
54         int curr_connections;
55         const char *name;
56         bool Clear;
57 };
58
59 /****************************************************************************
60  Count the entries belonging to a service in the connection db.
61 ****************************************************************************/
62
63 static int count_fn(struct db_record *rec,
64                     const struct connections_key *ckey,
65                     const struct connections_data *crec,
66                     void *udp)
67 {
68         struct count_stat *cs = (struct count_stat *)udp;
69
70         if (crec->cnum == -1) {
71                 return 0;
72         }
73
74         /* If the pid was not found delete the entry from connections.tdb */
75
76         if (cs->Clear && !process_exists(crec->pid) && (errno == ESRCH)) {
77                 NTSTATUS status;
78                 DEBUG(2,("pid %s doesn't exist - deleting connections %d [%s]\n",
79                          procid_str_static(&crec->pid), crec->cnum,
80                          crec->servicename));
81
82                 status = rec->delete_rec(rec);
83                 if (!NT_STATUS_IS_OK(status)) {
84                         DEBUG(0,("count_fn: tdb_delete failed with error %s\n",
85                                  nt_errstr(status)));
86                 }
87                 return 0;
88         }
89
90         if (strequal(crec->servicename, cs->name))
91                 cs->curr_connections++;
92
93         return 0;
94 }
95
96 /****************************************************************************
97  Claim an entry in the connections database.
98 ****************************************************************************/
99
100 int count_current_connections( const char *sharename, bool clear  )
101 {
102         struct count_stat cs;
103
104         cs.curr_connections = 0;
105         cs.name = sharename;
106         cs.Clear = clear;
107
108         /*
109          * This has a race condition, but locking the chain before hand is worse
110          * as it leads to deadlock.
111          */
112
113         if (connections_forall(count_fn, &cs) == -1) {
114                 DEBUG(0,("count_current_connections: traverse of "
115                          "connections.tdb failed\n"));
116                 return False;
117         }
118
119         return cs.curr_connections;
120 }
121
122 /****************************************************************************
123  Claim an entry in the connections database.
124 ****************************************************************************/
125
126 bool claim_connection(connection_struct *conn, const char *name)
127 {
128         struct db_record *rec;
129         struct connections_data crec;
130         TDB_DATA dbuf;
131         NTSTATUS status;
132
133         DEBUG(5,("claiming [%s]\n", name));
134
135         if (!(rec = connections_fetch_entry(talloc_tos(), conn, name))) {
136                 DEBUG(0, ("connections_fetch_entry failed\n"));
137                 return False;
138         }
139
140         /* fill in the crec */
141         ZERO_STRUCT(crec);
142         crec.magic = 0x280267;
143         crec.pid = sconn_server_id(conn->sconn);
144         crec.cnum = conn->cnum;
145         crec.uid = conn->session_info->utok.uid;
146         crec.gid = conn->session_info->utok.gid;
147         strlcpy(crec.servicename, lp_servicename(SNUM(conn)),
148                 sizeof(crec.servicename));
149         crec.start = time(NULL);
150
151         strlcpy(crec.machine,get_remote_machine_name(),sizeof(crec.machine));
152         strlcpy(crec.addr, conn->sconn->client_id.addr, sizeof(crec.addr));
153
154         dbuf.dptr = (uint8 *)&crec;
155         dbuf.dsize = sizeof(crec);
156
157         status = rec->store(rec, dbuf, TDB_REPLACE);
158
159         TALLOC_FREE(rec);
160
161         if (!NT_STATUS_IS_OK(status)) {
162                 DEBUG(0,("claim_connection: tdb_store failed with error %s.\n",
163                          nt_errstr(status)));
164                 return False;
165         }
166
167         return True;
168 }