smbd: make canonicalize_snapshot_path() public
[samba.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/rpc/dcesrv_core.h"
30 #include "printing/notify.h"
31 #include "printing.h"
32 #include "serverid.h"
33 #include "messages.h"
34 #include "passdb.h"
35 #include "../lib/util/pidfile.h"
36 #include "smbprofile.h"
37 #include "libcli/auth/netlogon_creds_cli.h"
38 #include "lib/gencache.h"
39 #include "rpc_server/rpc_config.h"
40
41 static struct files_struct *log_writeable_file_fn(
42         struct files_struct *fsp, void *private_data)
43 {
44         bool *found = (bool *)private_data;
45         char *path;
46
47         if (!fsp->fsp_flags.can_write) {
48                 return NULL;
49         }
50         if (!(*found)) {
51                 DEBUG(0, ("Writable files open at exit:\n"));
52                 *found = true;
53         }
54
55         path = talloc_asprintf(talloc_tos(), "%s/%s", fsp->conn->connectpath,
56                                smb_fname_str_dbg(fsp->fsp_name));
57         if (path == NULL) {
58                 DEBUGADD(0, ("<NOMEM>\n"));
59         }
60
61         DEBUGADD(0, ("%s\n", path));
62
63         TALLOC_FREE(path);
64         return NULL;
65 }
66
67 /****************************************************************************
68  Exit the server.
69 ****************************************************************************/
70
71 /* Reasons for shutting down a server process. */
72 enum server_exit_reason { SERVER_EXIT_NORMAL, SERVER_EXIT_ABNORMAL };
73
74 static void exit_server_common(enum server_exit_reason how,
75         const char *reason) _NORETURN_;
76
77 static void exit_server_common(enum server_exit_reason how,
78         const char *reason)
79 {
80         struct smbXsrv_client *client = global_smbXsrv_client;
81         struct smbXsrv_connection *xconn = NULL;
82         struct smbd_server_connection *sconn = NULL;
83         struct messaging_context *msg_ctx = global_messaging_context();
84
85         if (client != NULL) {
86                 sconn = client->sconn;
87                 xconn = client->connections;
88         }
89
90         if (!exit_firsttime)
91                 exit(0);
92         exit_firsttime = false;
93
94         change_to_root_user();
95
96
97         /*
98          * Here we typically have just one connection
99          */
100         for (; xconn != NULL; xconn = xconn->next) {
101                 /*
102                  * This is typically the disconnect for the only
103                  * (or with multi-channel last) connection of the client
104                  */
105                 if (NT_STATUS_IS_OK(xconn->transport.status)) {
106                         switch (how) {
107                         case SERVER_EXIT_ABNORMAL:
108                                 xconn->transport.status = NT_STATUS_INTERNAL_ERROR;
109                                 break;
110                         case SERVER_EXIT_NORMAL:
111                                 xconn->transport.status = NT_STATUS_LOCAL_DISCONNECT;
112                                 break;
113                         }
114                 }
115                 DO_PROFILE_INC(disconnect);
116         }
117
118         change_to_root_user();
119
120         if (sconn != NULL) {
121                 if (lp_log_writeable_files_on_exit()) {
122                         bool found = false;
123                         files_forall(sconn, log_writeable_file_fn, &found);
124                 }
125         }
126
127         change_to_root_user();
128
129         if (client != NULL) {
130                 NTSTATUS status;
131
132                 /*
133                  * Note: this is a no-op for smb2 as
134                  * conn->tcon_table is empty
135                  */
136                 status = smb1srv_tcon_disconnect_all(client);
137                 if (!NT_STATUS_IS_OK(status)) {
138                         DEBUG(0,("Server exit (%s)\n",
139                                 (reason ? reason : "normal exit")));
140                         DEBUG(0, ("exit_server_common: "
141                                   "smb1srv_tcon_disconnect_all() failed (%s) - "
142                                   "triggering cleanup\n", nt_errstr(status)));
143                 }
144
145                 status = smbXsrv_session_logoff_all(client);
146                 if (!NT_STATUS_IS_OK(status)) {
147                         DEBUG(0,("Server exit (%s)\n",
148                                 (reason ? reason : "normal exit")));
149                         DEBUG(0, ("exit_server_common: "
150                                   "smbXsrv_session_logoff_all() failed (%s) - "
151                                   "triggering cleanup\n", nt_errstr(status)));
152                 }
153         }
154
155         change_to_root_user();
156
157         if (client != NULL) {
158                 struct smbXsrv_connection *xconn_next = NULL;
159
160                 for (xconn = client->connections;
161                      xconn != NULL;
162                      xconn = xconn_next) {
163                         xconn_next = xconn->next;
164                         DLIST_REMOVE(client->connections, xconn);
165                         TALLOC_FREE(xconn);
166                 }
167         }
168
169         change_to_root_user();
170
171         /* 3 second timeout. */
172         print_notify_send_messages(msg_ctx, 3);
173
174 #ifdef USE_DMAPI
175         /* Destroy Samba DMAPI session only if we are master smbd process */
176         if (am_parent) {
177                 if (!dmapi_destroy_session()) {
178                         DEBUG(0,("Unable to close Samba DMAPI session\n"));
179                 }
180         }
181 #endif
182
183         if (am_parent && sconn != NULL) {
184                 dcesrv_shutdown_registered_ep_servers(sconn->dce_ctx);
185
186                 global_dcesrv_context_free();
187         }
188
189         /*
190          * we need to force the order of freeing the following,
191          * because smbd_msg_ctx is not a talloc child of smbd_server_conn.
192          */
193         if (client != NULL) {
194                 TALLOC_FREE(client->sconn);
195         }
196         sconn = NULL;
197         xconn = NULL;
198         client = NULL;
199         netlogon_creds_cli_close_global_db();
200         TALLOC_FREE(global_smbXsrv_client);
201         smbprofile_dump();
202         global_messaging_context_free();
203         global_event_context_free();
204         TALLOC_FREE(smbd_memcache_ctx);
205
206         locking_end();
207         printing_end();
208
209         if (how != SERVER_EXIT_NORMAL) {
210
211                 smb_panic(reason);
212
213                 /* Notreached. */
214                 exit(1);
215         } else {
216                 DEBUG(3,("Server exit (%s)\n",
217                         (reason ? reason : "normal exit")));
218                 if (am_parent) {
219                         pidfile_unlink(lp_pid_directory(), "smbd");
220                 }
221         }
222
223         exit(0);
224 }
225
226 void smbd_exit_server(const char *const explanation)
227 {
228         exit_server_common(SERVER_EXIT_ABNORMAL, explanation);
229 }
230
231 void smbd_exit_server_cleanly(const char *const explanation)
232 {
233         exit_server_common(SERVER_EXIT_NORMAL, explanation);
234 }
235
236 /*
237  * reinit_after_fork() wrapper that should be called when forking from
238  * smbd.
239  */
240 NTSTATUS smbd_reinit_after_fork(struct messaging_context *msg_ctx,
241                                 struct tevent_context *ev_ctx,
242                                 bool parent_longlived, const char *comment)
243 {
244         NTSTATUS ret;
245         am_parent = NULL;
246         ret = reinit_after_fork(msg_ctx, ev_ctx, parent_longlived, comment);
247         initialize_password_db(true, ev_ctx);
248         return ret;
249 }