source4/smbd: refactor the process model for prefork
[bbaumbach/samba-autobuild/.git] / source4 / rpc_server / service_rpc.c
1 /* 
2    Unix SMB/CIFS implementation.
3
4    smbd-specific dcerpc server code
5
6    Copyright (C) Andrew Tridgell 2003-2005
7    Copyright (C) Stefan (metze) Metzmacher 2004-2005
8    Copyright (C) Jelmer Vernooij <jelmer@samba.org> 2004,2007
9    
10    This program is free software; you can redistribute it and/or modify
11    it under the terms of the GNU General Public License as published by
12    the Free Software Foundation; either version 3 of the License, or
13    (at your option) any later version.
14    
15    This program is distributed in the hope that it will be useful,
16    but WITHOUT ANY WARRANTY; without even the implied warranty of
17    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18    GNU General Public License for more details.
19    
20    You should have received a copy of the GNU General Public License
21    along with this program.  If not, see <http://www.gnu.org/licenses/>.
22 */
23
24 #include "includes.h"
25 #include "librpc/gen_ndr/ndr_dcerpc.h"
26 #include "auth/auth.h"
27 #include "../lib/util/dlinklist.h"
28 #include "rpc_server/dcerpc_server.h"
29 #include "rpc_server/dcerpc_server_proto.h"
30 #include "system/filesys.h"
31 #include "lib/messaging/irpc.h"
32 #include "system/network.h"
33 #include "lib/socket/netif.h"
34 #include "param/param.h"
35 #include "../lib/tsocket/tsocket.h"
36 #include "librpc/rpc/dcerpc_proto.h"
37 #include "../lib/util/tevent_ntstatus.h"
38 #include "libcli/raw/smb.h"
39 #include "../libcli/named_pipe_auth/npa_tstream.h"
40 #include "smbd/process_model.h"
41
42 NTSTATUS server_service_rpc_init(TALLOC_CTX *);
43
44 /*
45   open the dcerpc server sockets
46 */
47 static void dcesrv_task_init(struct task_server *task)
48 {
49         NTSTATUS status;
50         struct dcesrv_context *dce_ctx;
51         struct dcesrv_endpoint *e;
52         const struct model_ops *single_model_ops;
53
54         dcerpc_server_init(task->lp_ctx);
55
56         task_server_set_title(task, "task[dcesrv]");
57
58         /*
59          * run the rpc server as a single process to allow for shard
60          * handles, and sharing of ldb contexts.
61          *
62          * We make an exception for NETLOGON below, and this follows
63          * whatever the top level is.
64          */
65         single_model_ops = process_model_startup("single");
66         if (!single_model_ops) goto failed;
67
68         status = dcesrv_init_context(task->event_ctx,
69                                      task->lp_ctx,
70                                      lpcfg_dcerpc_endpoint_servers(task->lp_ctx),
71                                      &dce_ctx);
72         if (!NT_STATUS_IS_OK(status)) goto failed;
73
74         /* Make sure the directory for NCALRPC exists */
75         if (!directory_exist(lpcfg_ncalrpc_dir(task->lp_ctx))) {
76                 mkdir(lpcfg_ncalrpc_dir(task->lp_ctx), 0755);
77         }
78
79         for (e=dce_ctx->endpoint_list;e;e=e->next) {
80                 const struct model_ops *this_model_ops = single_model_ops;
81
82                 enum dcerpc_transport_t transport =
83                         dcerpc_binding_get_transport(e->ep_description);
84                 const char *transport_str
85                         = derpc_transport_string_by_transport(transport);
86
87                 struct dcesrv_if_list *iface_list;
88
89                 /*
90                  * Ensure that -Msingle sets e->use_single_process for
91                  * consistency
92                  */
93
94                 if (task->model_ops == single_model_ops) {
95                         e->use_single_process = true;
96                 }
97
98                 if (transport == NCACN_HTTP) {
99                         /*
100                          * We don't support ncacn_http yet
101                          */
102                         continue;
103
104                         /*
105                          * For the next two cases, what we are trying
106                          * to do is put the NETLOGON server into the
107                          * standard process model, not single, as it
108                          * has no shared handles and takes a very high
109                          * load.  We only do this for ncacn_np and
110                          * ncacn_ip_tcp as otherwise it is too hard as
111                          * all servers share a socket for ncalrpc and
112                          * unix.
113                          */
114                 } else if (e->use_single_process == false) {
115                         this_model_ops = task->model_ops;
116                 }
117
118                 status = dcesrv_add_ep(dce_ctx, task->lp_ctx, e, task->event_ctx,
119                                        this_model_ops, task->process_context);
120                 if (!NT_STATUS_IS_OK(status)) {
121                         goto failed;
122                 }
123
124                 DEBUG(5,("Added endpoint on %s "
125                          "using process model %s for",
126                          transport_str,
127                          this_model_ops->name));
128
129                 for (iface_list = e->interface_list;
130                      iface_list != NULL;
131                      iface_list = iface_list->next) {
132                         DEBUGADD(5, (" %s", iface_list->iface.name));
133                 }
134                 DEBUGADD(5, ("\n"));
135         }
136
137         irpc_add_name(task->msg_ctx, "rpc_server");
138         return;
139 failed:
140         task_server_terminate(task, "Failed to startup dcerpc server task", true);      
141 }
142
143 NTSTATUS server_service_rpc_init(TALLOC_CTX *ctx)
144 {
145         struct service_details details = {
146                 /* 
147                  * This is a SNOWFLAKE, but sadly one that we
148                  * will have to keep for now.  The RPC server
149                  * code above overstamps the SINGLE process model
150                  * most of the time, but we need to be in forking
151                  * mode by defult to get a forking NETLOGON server
152                  */
153                 .inhibit_fork_on_accept = false,
154                 .inhibit_pre_fork = true
155         };
156         return register_server_service(ctx, "rpc", dcesrv_task_init, &details);
157 }