smbd: disconnect/destroy all connections before calling smbXsrv_session_logoff_all()
[gd/samba-autobuild/.git] / source3 / smbd / server_exit.c
1 /*
2    Unix SMB/CIFS implementation.
3    Main SMB server routines
4    Copyright (C) Andrew Tridgell                1992-1998
5    Copyright (C) Martin Pool                    2002
6    Copyright (C) Jelmer Vernooij                2002-2003
7    Copyright (C) Volker Lendecke                1993-2007
8    Copyright (C) Jeremy Allison                 1993-2007
9    Copyright (C) Andrew Bartlett                2010
10
11    This program is free software; you can redistribute it and/or modify
12    it under the terms of the GNU General Public License as published by
13    the Free Software Foundation; either version 3 of the License, or
14    (at your option) any later version.
15
16    This program is distributed in the hope that it will be useful,
17    but WITHOUT ANY WARRANTY; without even the implied warranty of
18    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19    GNU General Public License for more details.
20
21    You should have received a copy of the GNU General Public License
22    along with this program.  If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "includes.h"
26 #include "smbd/smbd.h"
27 #include "smbd/globals.h"
28 #include "ntdomain.h"
29 #include "../librpc/gen_ndr/srv_dfs.h"
30 #include "../librpc/gen_ndr/srv_dssetup.h"
31 #include "../librpc/gen_ndr/srv_echo.h"
32 #include "../librpc/gen_ndr/srv_eventlog.h"
33 #include "../librpc/gen_ndr/srv_initshutdown.h"
34 #include "../librpc/gen_ndr/srv_lsa.h"
35 #include "../librpc/gen_ndr/srv_netlogon.h"
36 #include "../librpc/gen_ndr/srv_ntsvcs.h"
37 #include "../librpc/gen_ndr/srv_samr.h"
38 #include "../librpc/gen_ndr/srv_spoolss.h"
39 #include "../librpc/gen_ndr/srv_srvsvc.h"
40 #include "../librpc/gen_ndr/srv_svcctl.h"
41 #include "../librpc/gen_ndr/srv_winreg.h"
42 #include "../librpc/gen_ndr/srv_wkssvc.h"
43 #include "printing/notify.h"
44 #include "printing.h"
45 #include "serverid.h"
46 #include "messages.h"
47 #include "passdb.h"
48 #include "../lib/util/pidfile.h"
49 #include "smbprofile.h"
50 #include "libcli/auth/netlogon_creds_cli.h"
51
52 static struct files_struct *log_writeable_file_fn(
53         struct files_struct *fsp, void *private_data)
54 {
55         bool *found = (bool *)private_data;
56         char *path;
57
58         if (!fsp->can_write) {
59                 return NULL;
60         }
61         if (!(*found)) {
62                 DEBUG(0, ("Writable files open at exit:\n"));
63                 *found = true;
64         }
65
66         path = talloc_asprintf(talloc_tos(), "%s/%s", fsp->conn->connectpath,
67                                smb_fname_str_dbg(fsp->fsp_name));
68         if (path == NULL) {
69                 DEBUGADD(0, ("<NOMEM>\n"));
70         }
71
72         DEBUGADD(0, ("%s\n", path));
73
74         TALLOC_FREE(path);
75         return NULL;
76 }
77
78 /****************************************************************************
79  Exit the server.
80 ****************************************************************************/
81
82 /* Reasons for shutting down a server process. */
83 enum server_exit_reason { SERVER_EXIT_NORMAL, SERVER_EXIT_ABNORMAL };
84
85 static void exit_server_common(enum server_exit_reason how,
86         const char *reason) _NORETURN_;
87
88 static void exit_server_common(enum server_exit_reason how,
89         const char *reason)
90 {
91         struct smbXsrv_client *client = global_smbXsrv_client;
92         struct smbXsrv_connection *xconn = NULL;
93         struct smbXsrv_connection *xconn_next = NULL;
94         struct smbd_server_connection *sconn = NULL;
95         struct messaging_context *msg_ctx = server_messaging_context();
96
97         if (client != NULL) {
98                 sconn = client->sconn;
99         }
100
101         if (!exit_firsttime)
102                 exit(0);
103         exit_firsttime = false;
104
105         change_to_root_user();
106
107
108         /*
109          * Here we typically have just one connection
110          */
111         for (xconn = client->connections; xconn != NULL; xconn = xconn_next) {
112                 xconn_next = xconn->next;
113                 DLIST_REMOVE(client->connections, xconn);
114
115                 /*
116                  * This is typically the disconnect for the only
117                  * (or with multi-channel last) connection of the client
118                  */
119                 if (NT_STATUS_IS_OK(xconn->transport.status)) {
120                         switch (how) {
121                         case SERVER_EXIT_ABNORMAL:
122                                 xconn->transport.status = NT_STATUS_INTERNAL_ERROR;
123                                 break;
124                         case SERVER_EXIT_NORMAL:
125                                 xconn->transport.status = NT_STATUS_LOCAL_DISCONNECT;
126                                 break;
127                         }
128                 }
129
130                 TALLOC_FREE(xconn);
131                 DO_PROFILE_INC(disconnect);
132         }
133
134         change_to_root_user();
135
136         if (sconn != NULL) {
137                 if (lp_log_writeable_files_on_exit()) {
138                         bool found = false;
139                         files_forall(sconn, log_writeable_file_fn, &found);
140                 }
141         }
142
143         change_to_root_user();
144
145         if (client != NULL) {
146                 NTSTATUS status;
147
148                 /*
149                  * Note: this is a no-op for smb2 as
150                  * conn->tcon_table is empty
151                  */
152                 status = smb1srv_tcon_disconnect_all(client);
153                 if (!NT_STATUS_IS_OK(status)) {
154                         DEBUG(0,("Server exit (%s)\n",
155                                 (reason ? reason : "normal exit")));
156                         DEBUG(0, ("exit_server_common: "
157                                   "smb1srv_tcon_disconnect_all() failed (%s) - "
158                                   "triggering cleanup\n", nt_errstr(status)));
159                 }
160
161                 status = smbXsrv_session_logoff_all(client);
162                 if (!NT_STATUS_IS_OK(status)) {
163                         DEBUG(0,("Server exit (%s)\n",
164                                 (reason ? reason : "normal exit")));
165                         DEBUG(0, ("exit_server_common: "
166                                   "smbXsrv_session_logoff_all() failed (%s) - "
167                                   "triggering cleanup\n", nt_errstr(status)));
168                 }
169         }
170
171         change_to_root_user();
172
173         /* 3 second timeout. */
174         print_notify_send_messages(msg_ctx, 3);
175
176 #ifdef USE_DMAPI
177         /* Destroy Samba DMAPI session only if we are master smbd process */
178         if (am_parent) {
179                 if (!dmapi_destroy_session()) {
180                         DEBUG(0,("Unable to close Samba DMAPI session\n"));
181                 }
182         }
183 #endif
184
185         if (am_parent) {
186                 rpc_wkssvc_shutdown();
187                 rpc_dssetup_shutdown();
188 #ifdef DEVELOPER
189                 rpc_rpcecho_shutdown();
190 #endif
191                 rpc_netdfs_shutdown();
192                 rpc_initshutdown_shutdown();
193                 rpc_eventlog_shutdown();
194                 rpc_ntsvcs_shutdown();
195                 rpc_svcctl_shutdown();
196                 rpc_spoolss_shutdown();
197
198                 rpc_srvsvc_shutdown();
199                 rpc_winreg_shutdown();
200
201                 rpc_netlogon_shutdown();
202                 rpc_samr_shutdown();
203                 rpc_lsarpc_shutdown();
204         }
205
206         /*
207          * we need to force the order of freeing the following,
208          * because smbd_msg_ctx is not a talloc child of smbd_server_conn.
209          */
210         if (client != NULL) {
211                 TALLOC_FREE(client->sconn);
212         }
213         sconn = NULL;
214         xconn = NULL;
215         client = NULL;
216         netlogon_creds_cli_close_global_db();
217         TALLOC_FREE(global_smbXsrv_client);
218         smbprofile_dump();
219         server_messaging_context_free();
220         server_event_context_free();
221         TALLOC_FREE(smbd_memcache_ctx);
222
223         locking_end();
224         printing_end();
225
226         if (how != SERVER_EXIT_NORMAL) {
227
228                 smb_panic(reason);
229
230                 /* Notreached. */
231                 exit(1);
232         } else {
233                 DEBUG(3,("Server exit (%s)\n",
234                         (reason ? reason : "normal exit")));
235                 if (am_parent) {
236                         pidfile_unlink(lp_pid_directory(), "smbd");
237                 }
238                 gencache_stabilize();
239         }
240
241         exit(0);
242 }
243
244 void smbd_exit_server(const char *const explanation)
245 {
246         exit_server_common(SERVER_EXIT_ABNORMAL, explanation);
247 }
248
249 void smbd_exit_server_cleanly(const char *const explanation)
250 {
251         exit_server_common(SERVER_EXIT_NORMAL, explanation);
252 }
253
254 /*
255  * reinit_after_fork() wrapper that should be called when forking from
256  * smbd.
257  */
258 NTSTATUS smbd_reinit_after_fork(struct messaging_context *msg_ctx,
259                                 struct tevent_context *ev_ctx,
260                                 bool parent_longlived, const char *comment)
261 {
262         NTSTATUS ret;
263         am_parent = NULL;
264         ret = reinit_after_fork(msg_ctx, ev_ctx, parent_longlived, comment);
265         initialize_password_db(true, ev_ctx);
266         return ret;
267 }