libcli:smb: Zero sensitive memory after use
[Samba.git] / source3 / rpc_server / fssd.c
blob3116679179ada83dd0eea50e08c1878fdfa8bba9
1 /*
2 * File Server Shadow-Copy Daemon
4 * Copyright (C) David Disseldorp 2012-2015
6 * Based on epmd.c:
7 * Copyright (c) 2011 Andreas Schneider <asn@samba.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, see <http://www.gnu.org/licenses/>.
23 #include "includes.h"
25 #include "ntdomain.h"
26 #include "messages.h"
28 #include "librpc/rpc/dcerpc_ep.h"
29 #include "../librpc/gen_ndr/srv_fsrvp.h"
30 #include "rpc_server/rpc_server.h"
31 #include "rpc_server/rpc_sock_helper.h"
32 #include "rpc_server/fss/srv_fss_agent.h"
34 #define DAEMON_NAME "fssd"
36 void start_fssd(struct tevent_context *ev_ctx,
37 struct messaging_context *msg_ctx);
39 static void fssd_reopen_logs(void)
41 char *lfile = lp_logfile(NULL);
42 int rc;
44 if (lfile == NULL || lfile[0] == '\0') {
45 rc = asprintf(&lfile, "%s/log.%s", get_dyn_LOGFILEBASE(), DAEMON_NAME);
46 if (rc > 0) {
47 lp_set_logfile(lfile);
48 SAFE_FREE(lfile);
50 } else {
51 if (strstr(lfile, DAEMON_NAME) == NULL) {
52 rc = asprintf(&lfile, "%s.%s", lp_logfile(NULL), DAEMON_NAME);
53 if (rc > 0) {
54 lp_set_logfile(lfile);
55 SAFE_FREE(lfile);
60 reopen_logs();
63 static void fssd_smb_conf_updated(struct messaging_context *msg,
64 void *private_data,
65 uint32_t msg_type,
66 struct server_id server_id,
67 DATA_BLOB *data)
69 DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
70 change_to_root_user();
71 fssd_reopen_logs();
74 static void fssd_sig_term_handler(struct tevent_context *ev,
75 struct tevent_signal *se,
76 int signum,
77 int count,
78 void *siginfo,
79 void *private_data)
81 rpc_FileServerVssAgent_shutdown();
83 exit_server_cleanly("termination signal");
86 static void fssd_setup_sig_term_handler(struct tevent_context *ev_ctx)
88 struct tevent_signal *se;
90 se = tevent_add_signal(ev_ctx,
91 ev_ctx,
92 SIGTERM, 0,
93 fssd_sig_term_handler,
94 NULL);
95 if (se == NULL) {
96 exit_server("failed to setup SIGTERM handler");
100 static void fssd_sig_hup_handler(struct tevent_context *ev,
101 struct tevent_signal *se,
102 int signum,
103 int count,
104 void *siginfo,
105 void *private_data)
107 change_to_root_user();
109 DEBUG(1,("reopening logs after SIGHUP\n"));
110 fssd_reopen_logs();
113 static void fssd_setup_sig_hup_handler(struct tevent_context *ev_ctx,
114 struct messaging_context *msg_ctx)
116 struct tevent_signal *se;
118 se = tevent_add_signal(ev_ctx,
119 ev_ctx,
120 SIGHUP, 0,
121 fssd_sig_hup_handler,
122 msg_ctx);
123 if (se == NULL) {
124 exit_server("failed to setup SIGHUP handler");
128 static bool fss_shutdown_cb(void *ptr)
130 srv_fssa_cleanup();
131 return true;
134 static bool fss_init_cb(void *ptr)
136 NTSTATUS status;
137 struct messaging_context *msg_ctx;
139 msg_ctx = talloc_get_type_abort(ptr, struct messaging_context);
140 status = srv_fssa_start(msg_ctx);
141 return NT_STATUS_IS_OK(status);
144 void start_fssd(struct tevent_context *ev_ctx,
145 struct messaging_context *msg_ctx)
147 struct rpc_srv_callbacks fss_cb;
148 NTSTATUS status;
149 pid_t pid;
150 bool ok;
151 int rc;
153 fss_cb.init = fss_init_cb;
154 fss_cb.shutdown = fss_shutdown_cb;
155 fss_cb.private_data = msg_ctx;
157 DEBUG(1, ("Forking File Server Shadow-copy Daemon\n"));
159 pid = fork();
161 if (pid == -1) {
162 DEBUG(0, ("failed to fork file server shadow-copy daemon [%s], "
163 "aborting ...\n", strerror(errno)));
164 exit(1);
167 if (pid) {
168 /* parent */
169 return;
172 /* child */
173 status = smbd_reinit_after_fork(msg_ctx, ev_ctx, true, NULL);
174 if (!NT_STATUS_IS_OK(status)) {
175 DEBUG(0,("reinit_after_fork() failed\n"));
176 smb_panic("reinit_after_fork() failed");
179 fssd_reopen_logs();
181 fssd_setup_sig_term_handler(ev_ctx);
182 fssd_setup_sig_hup_handler(ev_ctx, msg_ctx);
184 messaging_register(msg_ctx,
185 ev_ctx,
186 MSG_SMB_CONF_UPDATED,
187 fssd_smb_conf_updated);
189 status = rpc_FileServerVssAgent_init(&fss_cb);
190 if (!NT_STATUS_IS_OK(status)) {
191 DEBUG(0, ("Failed to register fssd rpc interface! (%s)\n",
192 nt_errstr(status)));
193 exit(1);
196 /* case is normalized by smbd on connection */
197 ok = setup_named_pipe_socket("fssagentrpc", ev_ctx, msg_ctx);
198 if (!ok) {
199 DEBUG(0, ("Failed to open fssd named pipe!\n"));
200 exit(1);
203 DEBUG(1, ("File Server Shadow-copy Daemon Started (%d)\n",
204 (int)getpid()));
206 /* loop forever */
207 rc = tevent_loop_wait(ev_ctx);
209 /* should not be reached */
210 DEBUG(0,("tevent_loop_wait() exited with %d - %s\n",
211 rc, (rc == 0) ? "out of events" : strerror(errno)));
213 exit(1);