r3468: split out dcerpc_server.h
[abartlet/samba.git/.git] / source4 / rpc_server / remote / dcesrv_remote.c
1 /* 
2    Unix SMB/CIFS implementation.
3    remote dcerpc operations
4
5    Copyright (C) Stefan (metze) Metzmacher 2004
6    
7    This program is free software; you can redistribute it and/or modify
8    it under the terms of the GNU General Public License as published by
9    the Free Software Foundation; either version 2 of the License, or
10    (at your option) any later version.
11    
12    This program is distributed in the hope that it will be useful,
13    but WITHOUT ANY WARRANTY; without even the implied warranty of
14    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15    GNU General Public License for more details.
16    
17    You should have received a copy of the GNU General Public License
18    along with this program; if not, write to the Free Software
19    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22 #include "includes.h"
23 #include "rpc_server/dcerpc_server.h"
24
25 struct dcesrv_remote_private {
26         struct dcerpc_pipe *c_pipe;
27         void *private;  
28 };
29
30 static NTSTATUS remote_op_bind(struct dcesrv_call_state *dce_call, const struct dcesrv_interface *iface)
31 {
32         NTSTATUS status;
33         struct dcesrv_remote_private *private;
34         const char *binding = lp_parm_string(-1, "dcerpc_remote", "binding");
35
36         if (!binding) {
37                 printf("You must specify a ncacn binding string\n");
38                 return NT_STATUS_INVALID_PARAMETER;
39         }
40
41         private = talloc_p(dce_call->conn, struct dcesrv_remote_private);
42         if (!private) {
43                 return NT_STATUS_NO_MEMORY;     
44         }
45
46         status = dcerpc_pipe_connect(&(private->c_pipe), binding, iface->ndr->uuid, iface->ndr->if_version,
47                                      lp_workgroup(), 
48                                      lp_parm_string(-1, "dcerpc_remote", "username"),
49                                      lp_parm_string(-1, "dcerpc_remote", "password"));
50
51         dce_call->conn->private = private;
52
53         return NT_STATUS_OK;    
54 }
55
56 static void remote_op_unbind(struct dcesrv_connection *dce_conn, const struct dcesrv_interface *iface)
57 {
58         struct dcesrv_remote_private *private = dce_conn->private;
59
60         dcerpc_pipe_close(private->c_pipe);
61
62         return; 
63 }
64
65 static NTSTATUS remote_op_dispatch(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, void *r)
66 {
67         struct dcesrv_remote_private *private = dce_call->conn->private;
68         NTSTATUS status;
69         uint16_t opnum = dce_call->pkt.u.request.opnum;
70         const char *name = dce_call->conn->iface->ndr->calls[opnum].name;
71         ndr_push_flags_fn_t ndr_push_fn = dce_call->conn->iface->ndr->calls[opnum].ndr_push;
72         ndr_pull_flags_fn_t ndr_pull_fn = dce_call->conn->iface->ndr->calls[opnum].ndr_pull;
73         ndr_print_function_t ndr_print_fn = dce_call->conn->iface->ndr->calls[opnum].ndr_print;
74         size_t struct_size = dce_call->conn->iface->ndr->calls[opnum].struct_size;
75
76         if (private->c_pipe->flags & DCERPC_DEBUG_PRINT_IN) {
77                 ndr_print_function_debug(ndr_print_fn, name, NDR_IN | NDR_SET_VALUES, r);               
78         }
79
80         status = dcerpc_ndr_request(private->c_pipe, opnum, mem_ctx,
81                                     (ndr_push_flags_fn_t) ndr_push_fn,
82                                     (ndr_pull_flags_fn_t) ndr_pull_fn,
83                                     r, struct_size);
84
85         if (!NT_STATUS_IS_OK(status)) {
86                 DEBUG(0,("dcesrv_remote: call[%s] failed with: %s!\n",name, nt_errstr(status)));
87                 return status;
88         }
89
90         if (NT_STATUS_IS_OK(status) && (private->c_pipe->flags & DCERPC_DEBUG_PRINT_OUT)) {
91                 ndr_print_function_debug(ndr_print_fn, name, NDR_OUT, r);               
92         }
93
94         return status;
95 }
96
97 static NTSTATUS remote_register_one_iface(struct dcesrv_context *dce_ctx, const struct dcesrv_interface *iface)
98 {
99         int i;
100
101         for (i=0;i<iface->ndr->endpoints->count;i++) {
102                 NTSTATUS ret;
103                 const char *name = iface->ndr->endpoints->names[i];
104
105                 ret = dcesrv_interface_register(dce_ctx, name, iface, NULL);
106                 if (!NT_STATUS_IS_OK(ret)) {
107                         DEBUG(1,("remote_op_init_server: failed to register endpoint '%s'\n",name));
108                         return ret;
109                 }
110         }
111
112         return NT_STATUS_OK;
113 }
114
115 static NTSTATUS remote_op_init_server(struct dcesrv_context *dce_ctx, const struct dcesrv_endpoint_server *ep_server)
116 {
117         int i;
118         char **ifaces = str_list_make(lp_parm_string(-1,"dcerpc_remote","interfaces"),NULL);
119
120         if (!ifaces) {
121                 DEBUG(3,("remote_op_init_server: no interfaces configured\n"));
122                 return NT_STATUS_OK;
123         }
124
125         for (i=0;ifaces[i];i++) {
126                 NTSTATUS ret;
127                 struct dcesrv_interface iface;
128                 
129                 if (!ep_server->interface_by_name(&iface, ifaces[i])) {
130                         DEBUG(0,("remote_op_init_server: failed to find interface = '%s'\n", ifaces[i]));
131                         str_list_free(&ifaces);
132                         return NT_STATUS_UNSUCCESSFUL;
133                 }
134
135                 ret = remote_register_one_iface(dce_ctx, &iface);
136                 if (!NT_STATUS_IS_OK(ret)) {
137                         DEBUG(0,("remote_op_init_server: failed to register interface = '%s'\n", ifaces[i]));
138                         str_list_free(&ifaces);
139                         return ret;
140                 }
141         }
142
143         str_list_free(&ifaces);
144         return NT_STATUS_OK;
145 }
146
147 static BOOL remote_fill_interface(struct dcesrv_interface *iface, const struct dcerpc_interface_table *if_tabl)
148 {
149         iface->ndr = if_tabl;
150
151         iface->bind = remote_op_bind;
152         iface->unbind = remote_op_unbind;
153         iface->dispatch = remote_op_dispatch;
154
155         return True;
156 }
157
158 static BOOL remote_op_interface_by_uuid(struct dcesrv_interface *iface, const char *uuid, uint32_t if_version)
159 {
160         int i;
161
162         for (i=0;dcerpc_pipes[i];i++) {
163                 if (dcerpc_pipes[i]->if_version == if_version &&
164                         strcmp(dcerpc_pipes[i]->uuid, uuid)==0) {
165                         return remote_fill_interface(iface, dcerpc_pipes[i]);
166                 }
167         }
168
169         return False;   
170 }
171
172 static BOOL remote_op_interface_by_name(struct dcesrv_interface *iface, const char *name)
173 {
174         int i;
175
176         for (i=0;dcerpc_pipes[i];i++) {
177                 if (strcmp(dcerpc_pipes[i]->name, name)==0) {
178                         return remote_fill_interface(iface, dcerpc_pipes[i]);
179                 }
180         }
181
182         return False;   
183 }
184
185 NTSTATUS dcerpc_remote_init(void)
186 {
187         NTSTATUS ret;
188         struct dcesrv_endpoint_server ep_server;
189
190         ZERO_STRUCT(ep_server);
191
192         /* fill in our name */
193         ep_server.name = "remote";
194
195         /* fill in all the operations */
196         ep_server.init_server = remote_op_init_server;
197
198         ep_server.interface_by_uuid = remote_op_interface_by_uuid;
199         ep_server.interface_by_name = remote_op_interface_by_name;
200
201         /* register ourselves with the DCERPC subsystem. */
202         ret = register_backend("dcerpc", &ep_server);
203         if (!NT_STATUS_IS_OK(ret)) {
204                 DEBUG(0,("Failed to register 'remote' endpoint server!\n"));
205                 return ret;
206         }
207
208         return ret;
209 }