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