751a6387fb6c6b59d8e9e0b156e439ec61ae7988
[ambi/samba-autobuild/.git] / source3 / rpc_server / rpc_service_setup.c
1 /*
2  *  Unix SMB/CIFS implementation.
3  *
4  *  SMBD RPC service callbacks
5  *
6  *  Copyright (c) 2011      Andreas Schneider <asn@samba.org>
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU General Public License as published by
10  *  the Free Software Foundation; either version 3 of the License, or
11  *  (at your option) any later version.
12  *
13  *  This program is distributed in the hope that it will be useful,
14  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
15  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  *  GNU General Public License for more details.
17  *
18  *  You should have received a copy of the GNU General Public License
19  *  along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21
22 #include "includes.h"
23 #include "ntdomain.h"
24
25 #include "../librpc/gen_ndr/ndr_epmapper_c.h"
26 #include "../librpc/gen_ndr/srv_epmapper.h"
27 #include "../librpc/gen_ndr/srv_srvsvc.h"
28 #include "../librpc/gen_ndr/srv_winreg.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_svcctl.h"
40 #include "../librpc/gen_ndr/srv_wkssvc.h"
41
42 #include "printing/nt_printing_migrate_internal.h"
43 #include "rpc_server/eventlog/srv_eventlog_reg.h"
44 #include "rpc_server/svcctl/srv_svcctl_reg.h"
45 #include "rpc_server/spoolss/srv_spoolss_nt.h"
46 #include "rpc_server/svcctl/srv_svcctl_nt.h"
47
48 #include "librpc/rpc/dcerpc_ep.h"
49 #include "rpc_server/rpc_sock_helper.h"
50 #include "rpc_server/rpc_service_setup.h"
51 #include "rpc_server/rpc_ep_register.h"
52 #include "rpc_server/rpc_server.h"
53 #include "rpc_server/rpc_config.h"
54 #include "rpc_server/rpc_modules.h"
55 #include "rpc_server/epmapper/srv_epmapper.h"
56
57 static_decl_rpc;
58
59 /* Common routine for embedded RPC servers */
60 bool rpc_setup_embedded(struct tevent_context *ev_ctx,
61                         struct messaging_context *msg_ctx,
62                         const struct ndr_interface_table *t,
63                         const char *pipe_name)
64 {
65         struct dcerpc_binding_vector *v;
66         enum rpc_service_mode_e epm_mode = rpc_epmapper_mode();
67         NTSTATUS status;
68
69         /* Registration of ncacn_np services is problematic.  The
70          * ev_ctx passed in here is passed down to all children of the
71          * smbd process, and if the end point mapper ever goes away,
72          * they will all attempt to re-register.  But we want to test
73          * the code for now, so it is enabled in on environment in
74          * make test */
75         if (epm_mode != RPC_SERVICE_MODE_DISABLED && 
76             (lp_parm_bool(-1, "rpc_server", "register_embedded_np", false))) {
77                 status = dcerpc_binding_vector_new(talloc_tos(), &v);
78                 if (!NT_STATUS_IS_OK(status)) {
79                         return false;
80                 }
81
82                 status = dcerpc_binding_vector_add_np_default(t, v);
83                 if (!NT_STATUS_IS_OK(status)) {
84                         return false;
85                 }
86
87                 status = rpc_ep_register(ev_ctx,
88                                          msg_ctx,
89                                          t,
90                                          v);
91                 if (!NT_STATUS_IS_OK(status)) {
92                         return false;
93                 }
94         }
95
96         return true;
97 }
98
99 static bool rpc_setup_winreg(struct tevent_context *ev_ctx,
100                              struct messaging_context *msg_ctx)
101 {
102         const struct ndr_interface_table *t = &ndr_table_winreg;
103         const char *pipe_name = "winreg";
104         NTSTATUS status;
105         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
106         if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
107                 return true;
108         }
109
110         status = rpc_winreg_init(NULL);
111         if (!NT_STATUS_IS_OK(status)) {
112                 return false;
113         }
114
115         return rpc_setup_embedded(ev_ctx, msg_ctx, t, pipe_name);
116 }
117
118 static bool rpc_setup_srvsvc(struct tevent_context *ev_ctx,
119                              struct messaging_context *msg_ctx)
120 {
121         const struct ndr_interface_table *t = &ndr_table_srvsvc;
122         const char *pipe_name = "srvsvc";
123         NTSTATUS status;
124         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
125         if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
126                 return true;
127         }
128
129         status = rpc_srvsvc_init(NULL);
130         if (!NT_STATUS_IS_OK(status)) {
131                 return false;
132         }
133
134         return rpc_setup_embedded(ev_ctx, msg_ctx, t, pipe_name);
135 }
136
137 static bool rpc_setup_lsarpc(struct tevent_context *ev_ctx,
138                              struct messaging_context *msg_ctx)
139 {
140         const struct ndr_interface_table *t = &ndr_table_lsarpc;
141         const char *pipe_name = "lsarpc";
142         enum rpc_daemon_type_e lsasd_type = rpc_lsasd_daemon();
143         NTSTATUS status;
144         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
145         if (service_mode != RPC_SERVICE_MODE_EMBEDDED || lsasd_type != RPC_DAEMON_EMBEDDED) {
146                 return true;
147         }
148
149         status = rpc_lsarpc_init(NULL);
150         if (!NT_STATUS_IS_OK(status)) {
151                 return false;
152         }
153
154         return rpc_setup_embedded(ev_ctx, msg_ctx, t, pipe_name);
155 }
156
157 static bool rpc_setup_samr(struct tevent_context *ev_ctx,
158                            struct messaging_context *msg_ctx)
159 {
160         const struct ndr_interface_table *t = &ndr_table_samr;
161         const char *pipe_name = "samr";
162         enum rpc_daemon_type_e lsasd_type = rpc_lsasd_daemon();
163         NTSTATUS status;
164         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
165         if (service_mode != RPC_SERVICE_MODE_EMBEDDED || lsasd_type != RPC_DAEMON_EMBEDDED) {
166                 return true;
167         }
168
169         status = rpc_samr_init(NULL);
170         if (!NT_STATUS_IS_OK(status)) {
171                 return false;
172         }
173
174         return rpc_setup_embedded(ev_ctx, msg_ctx, t, pipe_name);
175 }
176
177 static bool rpc_setup_netlogon(struct tevent_context *ev_ctx,
178                                struct messaging_context *msg_ctx)
179 {
180         const struct ndr_interface_table *t = &ndr_table_netlogon;
181         const char *pipe_name = "netlogon";
182         enum rpc_daemon_type_e lsasd_type = rpc_lsasd_daemon();
183         NTSTATUS status;
184         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
185         if (service_mode != RPC_SERVICE_MODE_EMBEDDED || lsasd_type != RPC_DAEMON_EMBEDDED) {
186                 return true;
187         }
188
189         status = rpc_netlogon_init(NULL);
190         if (!NT_STATUS_IS_OK(status)) {
191                 return false;
192         }
193
194         return rpc_setup_embedded(ev_ctx, msg_ctx, t, pipe_name);
195 }
196
197 static bool rpc_setup_netdfs(struct tevent_context *ev_ctx,
198                              struct messaging_context *msg_ctx)
199 {
200         const struct ndr_interface_table *t = &ndr_table_netdfs;
201         const char *pipe_name = "netdfs";
202         NTSTATUS status;
203         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
204         if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
205                 return true;
206         }
207
208         status = rpc_netdfs_init(NULL);
209         if (!NT_STATUS_IS_OK(status)) {
210                 return false;
211         }
212
213         return rpc_setup_embedded(ev_ctx, msg_ctx, t, pipe_name);
214 }
215
216 #ifdef DEVELOPER
217 static bool rpc_setup_rpcecho(struct tevent_context *ev_ctx,
218                               struct messaging_context *msg_ctx)
219 {
220         const struct ndr_interface_table *t = &ndr_table_rpcecho;
221         const char *pipe_name = "rpcecho";
222         NTSTATUS status;
223         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
224         if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
225                 return true;
226         }
227
228         status = rpc_rpcecho_init(NULL);
229         if (!NT_STATUS_IS_OK(status)) {
230                 return false;
231         }
232
233         return rpc_setup_embedded(ev_ctx, msg_ctx, t, pipe_name);
234 }
235 #endif
236
237 static bool rpc_setup_dssetup(struct tevent_context *ev_ctx,
238                               struct messaging_context *msg_ctx)
239 {
240         const struct ndr_interface_table *t = &ndr_table_dssetup;
241         const char *pipe_name = "dssetup";
242         NTSTATUS status;
243         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
244         if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
245                 return true;
246         }
247
248         status = rpc_dssetup_init(NULL);
249         if (!NT_STATUS_IS_OK(status)) {
250                 return false;
251         }
252
253         return rpc_setup_embedded(ev_ctx, msg_ctx, t, pipe_name);
254 }
255
256 static bool rpc_setup_wkssvc(struct tevent_context *ev_ctx,
257                              struct messaging_context *msg_ctx)
258 {
259         const struct ndr_interface_table *t = &ndr_table_wkssvc;
260         const char *pipe_name = "wkssvc";
261         NTSTATUS status;
262         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
263         if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
264                 return true;
265         }
266
267         status = rpc_wkssvc_init(NULL);
268         if (!NT_STATUS_IS_OK(status)) {
269                 return false;
270         }
271
272         return rpc_setup_embedded(ev_ctx, msg_ctx, t, pipe_name);
273 }
274
275 static bool spoolss_init_cb(void *ptr)
276 {
277         struct messaging_context *msg_ctx =
278                 talloc_get_type_abort(ptr, struct messaging_context);
279         bool ok;
280
281         /*
282          * Migrate the printers first.
283          */
284         ok = nt_printing_tdb_migrate(msg_ctx);
285         if (!ok) {
286                 return false;
287         }
288
289         return true;
290 }
291
292 static bool spoolss_shutdown_cb(void *ptr)
293 {
294         srv_spoolss_cleanup();
295
296         return true;
297 }
298
299 static bool rpc_setup_spoolss(struct tevent_context *ev_ctx,
300                               struct messaging_context *msg_ctx)
301 {
302         const struct ndr_interface_table *t = &ndr_table_spoolss;
303         struct rpc_srv_callbacks spoolss_cb;
304         enum rpc_daemon_type_e spoolss_type = rpc_spoolss_daemon();
305         NTSTATUS status = NT_STATUS_UNSUCCESSFUL;
306         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
307
308         if (lp__disable_spoolss()) {
309                 return true;
310         }
311
312         if (service_mode != RPC_SERVICE_MODE_EMBEDDED || spoolss_type != RPC_DAEMON_EMBEDDED) {
313                 return true;
314         }
315
316         spoolss_cb.init         = spoolss_init_cb;
317         spoolss_cb.shutdown     = spoolss_shutdown_cb;
318         spoolss_cb.private_data = msg_ctx;
319
320         status = rpc_spoolss_init(&spoolss_cb);
321         if (!NT_STATUS_IS_OK(status)) {
322                 return false;
323         }
324
325         return rpc_setup_embedded(ev_ctx, msg_ctx, t, NULL);
326 }
327
328 static bool svcctl_init_cb(void *ptr)
329 {
330         struct messaging_context *msg_ctx =
331                 talloc_get_type_abort(ptr, struct messaging_context);
332         bool ok;
333
334         /* initialize the control hooks */
335         init_service_op_table();
336
337         ok = svcctl_init_winreg(msg_ctx);
338         if (!ok) {
339                 return false;
340         }
341
342         return true;
343 }
344
345 static bool svcctl_shutdown_cb(void *ptr)
346 {
347         shutdown_service_op_table();
348
349         return true;
350 }
351
352 static bool rpc_setup_svcctl(struct tevent_context *ev_ctx,
353                              struct messaging_context *msg_ctx)
354 {
355         const struct ndr_interface_table *t = &ndr_table_svcctl;
356         const char *pipe_name = "svcctl";
357         struct rpc_srv_callbacks svcctl_cb;
358         NTSTATUS status;
359         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
360         if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
361                 return true;
362         }
363
364         svcctl_cb.init         = svcctl_init_cb;
365         svcctl_cb.shutdown     = svcctl_shutdown_cb;
366         svcctl_cb.private_data = msg_ctx;
367
368         status = rpc_svcctl_init(&svcctl_cb);
369         if (!NT_STATUS_IS_OK(status)) {
370                 return false;
371         }
372
373         return rpc_setup_embedded(ev_ctx, msg_ctx, t, pipe_name);
374 }
375
376 static bool rpc_setup_ntsvcs(struct tevent_context *ev_ctx,
377                              struct messaging_context *msg_ctx)
378 {
379         const struct ndr_interface_table *t = &ndr_table_ntsvcs;
380         NTSTATUS status;
381         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
382         if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
383                 return true;
384         }
385
386         status = rpc_ntsvcs_init(NULL);
387         if (!NT_STATUS_IS_OK(status)) {
388                 return false;
389         }
390
391         return rpc_setup_embedded(ev_ctx, msg_ctx, t, NULL);
392 }
393
394 static bool eventlog_init_cb(void *ptr)
395 {
396         struct messaging_context *msg_ctx =
397                 talloc_get_type_abort(ptr, struct messaging_context);
398         bool ok;
399
400         ok = eventlog_init_winreg(msg_ctx);
401         if (!ok) {
402                 return false;
403         }
404
405         return true;
406 }
407
408 static bool rpc_setup_eventlog(struct tevent_context *ev_ctx,
409                                struct messaging_context *msg_ctx)
410 {
411         const struct ndr_interface_table *t = &ndr_table_eventlog;
412         struct rpc_srv_callbacks eventlog_cb;
413         NTSTATUS status;
414         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
415         if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
416                 return true;
417         }
418
419         eventlog_cb.init         = eventlog_init_cb;
420         eventlog_cb.shutdown     = NULL;
421         eventlog_cb.private_data = msg_ctx;
422
423         status = rpc_eventlog_init(&eventlog_cb);
424         if (!NT_STATUS_IS_OK(status)) {
425                 return false;
426         }
427
428         return rpc_setup_embedded(ev_ctx, msg_ctx, t, NULL);
429 }
430
431 static bool rpc_setup_initshutdown(struct tevent_context *ev_ctx,
432                                    struct messaging_context *msg_ctx)
433 {
434         const struct ndr_interface_table *t = &ndr_table_initshutdown;
435         NTSTATUS status;
436         enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
437         if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
438                 return true;
439         }
440
441         status = rpc_initshutdown_init(NULL);
442         if (!NT_STATUS_IS_OK(status)) {
443                 return false;
444         }
445
446         return rpc_setup_embedded(ev_ctx, msg_ctx, t, NULL);
447 }
448
449 bool dcesrv_ep_setup(struct tevent_context *ev_ctx,
450                      struct messaging_context *msg_ctx)
451 {
452         TALLOC_CTX *tmp_ctx;
453         bool ok;
454         init_module_fn *mod_init_fns = NULL;
455
456         tmp_ctx = talloc_stackframe();
457         if (tmp_ctx == NULL) {
458                 return false;
459         }
460
461         ok = rpc_setup_winreg(ev_ctx, msg_ctx);
462         if (!ok) {
463                 goto done;
464         }
465
466         ok = rpc_setup_srvsvc(ev_ctx, msg_ctx);
467         if (!ok) {
468                 goto done;
469         }
470
471         ok = rpc_setup_lsarpc(ev_ctx, msg_ctx);
472         if (!ok) {
473                 goto done;
474         }
475
476         ok = rpc_setup_samr(ev_ctx, msg_ctx);
477         if (!ok) {
478                 goto done;
479         }
480
481         ok = rpc_setup_netlogon(ev_ctx, msg_ctx);
482         if (!ok) {
483                 goto done;
484         }
485
486         ok = rpc_setup_netdfs(ev_ctx, msg_ctx);
487         if (!ok) {
488                 goto done;
489         }
490
491 #ifdef DEVELOPER
492         ok = rpc_setup_rpcecho(ev_ctx, msg_ctx);
493         if (!ok) {
494                 goto done;
495         }
496 #endif
497
498         ok = rpc_setup_dssetup(ev_ctx, msg_ctx);
499         if (!ok) {
500                 goto done;
501         }
502
503         ok = rpc_setup_wkssvc(ev_ctx, msg_ctx);
504         if (!ok) {
505                 goto done;
506         }
507
508         ok = rpc_setup_spoolss(ev_ctx, msg_ctx);
509         if (!ok) {
510                 goto done;
511         }
512
513         ok = rpc_setup_svcctl(ev_ctx, msg_ctx);
514         if (!ok) {
515                 goto done;
516         }
517
518         ok = rpc_setup_ntsvcs(ev_ctx, msg_ctx);
519         if (!ok) {
520                 goto done;
521         }
522
523         ok = rpc_setup_eventlog(ev_ctx, msg_ctx);
524         if (!ok) {
525                 goto done;
526         }
527
528         ok = rpc_setup_initshutdown(ev_ctx, msg_ctx);
529         if (!ok) {
530                 goto done;
531         }
532
533         /* Initialize static subsystems */
534         static_init_rpc;
535
536         /* Initialize shared modules */
537         mod_init_fns = load_samba_modules(tmp_ctx, "rpc");
538         if ((mod_init_fns == NULL) && (errno != ENOENT)) {
539                 /*
540                  * ENOENT means the directory doesn't exist which can happen if
541                  * all modules are static. So ENOENT is ok, everything else is
542                  * not ok.
543                  */
544                 DBG_ERR("Loading shared RPC modules failed [%s]\n",
545                         strerror(errno));
546                 ok = false;
547                 goto done;
548         }
549
550         ok = run_init_functions(mod_init_fns);
551         if (!ok) {
552                 DBG_ERR("Initializing shared RPC modules failed\n");
553                 goto done;
554         }
555
556         ok = setup_rpc_modules(ev_ctx, msg_ctx);
557         if (!ok) {
558                 DBG_ERR("Shared RPC modules setup failed\n");
559                 goto done;
560         }
561
562 done:
563         talloc_free(tmp_ctx);
564         return ok;
565 }
566
567 /* vim: set ts=8 sw=8 noet cindent ft=c.doxygen: */