netcmd/ldapcmp: use set instead of list to find missing DNs
[amitay/samba.git] / source3 / rpc_server / fssd.c
1 /*
2  *  File Server Shadow-Copy Daemon
3  *
4  *  Copyright (C) David Disseldorp      2012-2015
5  *
6  *  Based on epmd.c:
7  *  Copyright (c) 2011      Andreas Schneider <asn@samba.org>
8  *
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.
13  *
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.
18  *
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/>.
21  */
22
23 #include "includes.h"
24
25 #include "ntdomain.h"
26 #include "messages.h"
27
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"
33
34 #define DAEMON_NAME "fssd"
35
36 void start_fssd(struct tevent_context *ev_ctx,
37                 struct messaging_context *msg_ctx);
38
39 static void fssd_reopen_logs(void)
40 {
41         char *lfile = lp_logfile(NULL);
42         int rc;
43
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);
49                 }
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);
56                         }
57                 }
58         }
59
60         reopen_logs();
61 }
62
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)
68 {
69         DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
70         change_to_root_user();
71         fssd_reopen_logs();
72 }
73
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)
80 {
81         rpc_FileServerVssAgent_shutdown();
82
83         exit_server_cleanly("termination signal");
84 }
85
86 static void fssd_setup_sig_term_handler(struct tevent_context *ev_ctx)
87 {
88         struct tevent_signal *se;
89
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");
97         }
98 }
99
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)
106 {
107         change_to_root_user();
108
109         DEBUG(1,("reopening logs after SIGHUP\n"));
110         fssd_reopen_logs();
111 }
112
113 static void fssd_setup_sig_hup_handler(struct tevent_context *ev_ctx,
114                                        struct messaging_context *msg_ctx)
115 {
116         struct tevent_signal *se;
117
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");
125         }
126 }
127
128 static bool fss_shutdown_cb(void *ptr)
129 {
130         srv_fssa_cleanup();
131         return true;
132 }
133
134 static bool fss_init_cb(void *ptr)
135 {
136         NTSTATUS status;
137         struct messaging_context *msg_ctx;
138
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);
142 }
143
144 void start_fssd(struct tevent_context *ev_ctx,
145                 struct messaging_context *msg_ctx)
146 {
147         struct rpc_srv_callbacks fss_cb;
148         NTSTATUS status;
149         pid_t pid;
150         bool ok;
151         int rc;
152
153         fss_cb.init = fss_init_cb;
154         fss_cb.shutdown = fss_shutdown_cb;
155         fss_cb.private_data = msg_ctx;
156
157         DEBUG(1, ("Forking File Server Shadow-copy Daemon\n"));
158
159         pid = fork();
160
161         if (pid == -1) {
162                 DEBUG(0, ("failed to fork file server shadow-copy daemon [%s], "
163                           "aborting ...\n", strerror(errno)));
164                 exit(1);
165         }
166
167         if (pid) {
168                 /* parent */
169                 return;
170         }
171
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");
177         }
178
179         fssd_reopen_logs();
180
181         fssd_setup_sig_term_handler(ev_ctx);
182         fssd_setup_sig_hup_handler(ev_ctx, msg_ctx);
183
184         messaging_register(msg_ctx,
185                            ev_ctx,
186                            MSG_SMB_CONF_UPDATED,
187                            fssd_smb_conf_updated);
188
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);
194         }
195
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);
201         }
202
203         DEBUG(1, ("File Server Shadow-copy Daemon Started (%d)\n",
204                   (int)getpid()));
205
206         /* loop forever */
207         rc = tevent_loop_wait(ev_ctx);
208
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)));
212
213         exit(1);
214 }