s3-rpc_server: enforce packet level authentication for iremotewinspool server
[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 "serverid.h"
26 #include "ntdomain.h"
27 #include "messages.h"
28
29 #include "librpc/rpc/dcerpc_ep.h"
30 #include "../librpc/gen_ndr/srv_fsrvp.h"
31 #include "rpc_server/rpc_server.h"
32 #include "rpc_server/rpc_sock_helper.h"
33 #include "rpc_server/fss/srv_fss_agent.h"
34
35 #define DAEMON_NAME "fssd"
36
37 void start_fssd(struct tevent_context *ev_ctx,
38                 struct messaging_context *msg_ctx);
39
40 static void fssd_reopen_logs(void)
41 {
42         char *lfile = lp_logfile(NULL);
43         int rc;
44
45         if (lfile == NULL || lfile[0] == '\0') {
46                 rc = asprintf(&lfile, "%s/log.%s", get_dyn_LOGFILEBASE(), DAEMON_NAME);
47                 if (rc > 0) {
48                         lp_set_logfile(lfile);
49                         SAFE_FREE(lfile);
50                 }
51         } else {
52                 if (strstr(lfile, DAEMON_NAME) == NULL) {
53                         rc = asprintf(&lfile, "%s.%s", lp_logfile(NULL), DAEMON_NAME);
54                         if (rc > 0) {
55                                 lp_set_logfile(lfile);
56                                 SAFE_FREE(lfile);
57                         }
58                 }
59         }
60
61         reopen_logs();
62 }
63
64 static void fssd_smb_conf_updated(struct messaging_context *msg,
65                                   void *private_data,
66                                   uint32_t msg_type,
67                                   struct server_id server_id,
68                                   DATA_BLOB *data)
69 {
70         DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
71         change_to_root_user();
72         fssd_reopen_logs();
73 }
74
75 static void fssd_sig_term_handler(struct tevent_context *ev,
76                                   struct tevent_signal *se,
77                                   int signum,
78                                   int count,
79                                   void *siginfo,
80                                   void *private_data)
81 {
82         rpc_FileServerVssAgent_shutdown();
83
84         exit_server_cleanly("termination signal");
85 }
86
87 static void fssd_setup_sig_term_handler(struct tevent_context *ev_ctx)
88 {
89         struct tevent_signal *se;
90
91         se = tevent_add_signal(ev_ctx,
92                                ev_ctx,
93                                SIGTERM, 0,
94                                fssd_sig_term_handler,
95                                NULL);
96         if (se == NULL) {
97                 exit_server("failed to setup SIGTERM handler");
98         }
99 }
100
101 static void fssd_sig_hup_handler(struct tevent_context *ev,
102                                     struct tevent_signal *se,
103                                     int signum,
104                                     int count,
105                                     void *siginfo,
106                                     void *private_data)
107 {
108         change_to_root_user();
109
110         DEBUG(1,("reopening logs after SIGHUP\n"));
111         fssd_reopen_logs();
112 }
113
114 static void fssd_setup_sig_hup_handler(struct tevent_context *ev_ctx,
115                                        struct messaging_context *msg_ctx)
116 {
117         struct tevent_signal *se;
118
119         se = tevent_add_signal(ev_ctx,
120                                ev_ctx,
121                                SIGHUP, 0,
122                                fssd_sig_hup_handler,
123                                msg_ctx);
124         if (se == NULL) {
125                 exit_server("failed to setup SIGHUP handler");
126         }
127 }
128
129 static bool fss_shutdown_cb(void *ptr)
130 {
131         srv_fssa_cleanup();
132         return true;
133 }
134
135 static bool fss_init_cb(void *ptr)
136 {
137         NTSTATUS status;
138         struct messaging_context *msg_ctx;
139
140         msg_ctx = talloc_get_type_abort(ptr, struct messaging_context);
141         status = srv_fssa_start(msg_ctx);
142         return NT_STATUS_IS_OK(status);
143 }
144
145 void start_fssd(struct tevent_context *ev_ctx,
146                 struct messaging_context *msg_ctx)
147 {
148         struct rpc_srv_callbacks fss_cb;
149         NTSTATUS status;
150         pid_t pid;
151         bool ok;
152         int rc;
153
154         fss_cb.init = fss_init_cb;
155         fss_cb.shutdown = fss_shutdown_cb;
156         fss_cb.private_data = msg_ctx;
157
158         DEBUG(1, ("Forking File Server Shadow-copy Daemon\n"));
159
160         pid = fork();
161
162         if (pid == -1) {
163                 DEBUG(0, ("failed to fork file server shadow-copy daemon [%s], "
164                           "aborting ...\n", strerror(errno)));
165                 exit(1);
166         }
167
168         if (pid) {
169                 /* parent */
170                 return;
171         }
172
173         /* child */
174         status = smbd_reinit_after_fork(msg_ctx, ev_ctx, true, NULL);
175         if (!NT_STATUS_IS_OK(status)) {
176                 DEBUG(0,("reinit_after_fork() failed\n"));
177                 smb_panic("reinit_after_fork() failed");
178         }
179
180         fssd_reopen_logs();
181
182         fssd_setup_sig_term_handler(ev_ctx);
183         fssd_setup_sig_hup_handler(ev_ctx, msg_ctx);
184
185         ok = serverid_register(messaging_server_id(msg_ctx),
186                                FLAG_MSG_GENERAL |
187                                FLAG_MSG_PRINT_GENERAL);
188         if (!ok) {
189                 DEBUG(0, ("Failed to register serverid in fssd!\n"));
190                 exit(1);
191         }
192
193         messaging_register(msg_ctx,
194                            ev_ctx,
195                            MSG_SMB_CONF_UPDATED,
196                            fssd_smb_conf_updated);
197
198         status = rpc_FileServerVssAgent_init(&fss_cb);
199         if (!NT_STATUS_IS_OK(status)) {
200                 DEBUG(0, ("Failed to register fssd rpc interface! (%s)\n",
201                           nt_errstr(status)));
202                 exit(1);
203         }
204
205         /* case is normalized by smbd on connection */
206         ok = setup_named_pipe_socket("fssagentrpc", ev_ctx, msg_ctx);
207         if (!ok) {
208                 DEBUG(0, ("Failed to open fssd named pipe!\n"));
209                 exit(1);
210         }
211
212         DEBUG(1, ("File Server Shadow-copy Daemon Started (%d)\n",
213                   (int)getpid()));
214
215         /* loop forever */
216         rc = tevent_loop_wait(ev_ctx);
217
218         /* should not be reached */
219         DEBUG(0,("tevent_loop_wait() exited with %d - %s\n",
220                  rc, (rc == 0) ? "out of events" : strerror(errno)));
221
222         exit(1);
223 }