1 /*
2    Unix SMB/CIFS implementation.
3 
4    endpoint server for the echo pipe
5 
6    Copyright (C) Andrew Tridgell 2003
7    Copyright (C) Stefan (metze) Metzmacher 2005
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 #include "system/filesys.h"
25 #include "rpc_server/dcerpc_server.h"
26 #include "librpc/gen_ndr/ndr_echo.h"
27 #include "lib/events/events.h"
28 
29 #define DCESRV_INTERFACE_RPCECHO_BIND(context, iface) \
30        dcesrv_interface_rpcecho_bind(context, iface)
dcesrv_interface_rpcecho_bind(struct dcesrv_connection_context * context,const struct dcesrv_interface * iface)31 static NTSTATUS dcesrv_interface_rpcecho_bind(struct dcesrv_connection_context *context,
32 					      const struct dcesrv_interface *iface)
33 {
34 	return dcesrv_interface_bind_allow_connect(context, iface);
35 }
36 
dcesrv_echo_AddOne(struct dcesrv_call_state * dce_call,TALLOC_CTX * mem_ctx,struct echo_AddOne * r)37 static NTSTATUS dcesrv_echo_AddOne(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct echo_AddOne *r)
38 {
39 	*r->out.out_data = r->in.in_data + 1;
40 	return NT_STATUS_OK;
41 }
42 
dcesrv_echo_EchoData(struct dcesrv_call_state * dce_call,TALLOC_CTX * mem_ctx,struct echo_EchoData * r)43 static NTSTATUS dcesrv_echo_EchoData(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct echo_EchoData *r)
44 {
45 	if (!r->in.len) {
46 		return NT_STATUS_OK;
47 	}
48 
49 	r->out.out_data = (uint8_t *)talloc_memdup(mem_ctx, r->in.in_data, r->in.len);
50 	if (!r->out.out_data) {
51 		return NT_STATUS_NO_MEMORY;
52 	}
53 
54 	return NT_STATUS_OK;
55 }
56 
dcesrv_echo_SinkData(struct dcesrv_call_state * dce_call,TALLOC_CTX * mem_ctx,struct echo_SinkData * r)57 static NTSTATUS dcesrv_echo_SinkData(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct echo_SinkData *r)
58 {
59 	return NT_STATUS_OK;
60 }
61 
dcesrv_echo_SourceData(struct dcesrv_call_state * dce_call,TALLOC_CTX * mem_ctx,struct echo_SourceData * r)62 static NTSTATUS dcesrv_echo_SourceData(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct echo_SourceData *r)
63 {
64 	unsigned int i;
65 
66 	r->out.data = talloc_array(mem_ctx, uint8_t, r->in.len);
67 	if (!r->out.data) {
68 		return NT_STATUS_NO_MEMORY;
69 	}
70 
71 	for (i=0;i<r->in.len;i++) {
72 		r->out.data[i] = i;
73 	}
74 
75 	return NT_STATUS_OK;
76 }
77 
dcesrv_echo_TestCall(struct dcesrv_call_state * dce_call,TALLOC_CTX * mem_ctx,struct echo_TestCall * r)78 static NTSTATUS dcesrv_echo_TestCall(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct echo_TestCall *r)
79 {
80 	*r->out.s2 = talloc_strdup(mem_ctx, r->in.s1);
81 	if (r->in.s1 && !*r->out.s2) {
82 		return NT_STATUS_NO_MEMORY;
83 	}
84 	return NT_STATUS_OK;
85 }
86 
dcesrv_echo_TestCall2(struct dcesrv_call_state * dce_call,TALLOC_CTX * mem_ctx,struct echo_TestCall2 * r)87 static NTSTATUS dcesrv_echo_TestCall2(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct echo_TestCall2 *r)
88 {
89 	r->out.info = talloc(mem_ctx, union echo_Info);
90 	if (!r->out.info) {
91 		return NT_STATUS_NO_MEMORY;
92 	}
93 
94 	switch (r->in.level) {
95 	case 1:
96 		r->out.info->info1.v = 10;
97 		break;
98 	case 2:
99 		r->out.info->info2.v = 20;
100 		break;
101 	case 3:
102 		r->out.info->info3.v = 30;
103 		break;
104 	case 4:
105 		r->out.info->info4.v = 40;
106 		break;
107 	case 5:
108 		r->out.info->info5.v1 = 50;
109 		r->out.info->info5.v2 = 60;
110 		break;
111 	case 6:
112 		r->out.info->info6.v1 = 70;
113 		r->out.info->info6.info1.v= 80;
114 		break;
115 	case 7:
116 		r->out.info->info7.v1 = 80;
117 		r->out.info->info7.info4.v = 90;
118 		break;
119 	default:
120 		return NT_STATUS_INVALID_LEVEL;
121 	}
122 
123 	return NT_STATUS_OK;
124 }
125 
dcesrv_echo_TestEnum(struct dcesrv_call_state * dce_call,TALLOC_CTX * mem_ctx,struct echo_TestEnum * r)126 static NTSTATUS dcesrv_echo_TestEnum(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct echo_TestEnum *r)
127 {
128 	r->out.foo2->e1 = ECHO_ENUM2;
129 	return NT_STATUS_OK;
130 }
131 
dcesrv_echo_TestSurrounding(struct dcesrv_call_state * dce_call,TALLOC_CTX * mem_ctx,struct echo_TestSurrounding * r)132 static NTSTATUS dcesrv_echo_TestSurrounding(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct echo_TestSurrounding *r)
133 {
134 	if (!r->in.data) {
135 		r->out.data = NULL;
136 		return NT_STATUS_OK;
137 	}
138 
139 	r->out.data = talloc(mem_ctx, struct echo_Surrounding);
140 	if (!r->out.data) {
141 		return NT_STATUS_NO_MEMORY;
142 	}
143 	r->out.data->x = 2 * r->in.data->x;
144 	r->out.data->surrounding = talloc_zero_array(mem_ctx, uint16_t, r->out.data->x);
145 	if (!r->out.data->surrounding) {
146 		return NT_STATUS_NO_MEMORY;
147 	}
148 
149 	return NT_STATUS_OK;
150 }
151 
dcesrv_echo_TestDoublePointer(struct dcesrv_call_state * dce_call,TALLOC_CTX * mem_ctx,struct echo_TestDoublePointer * r)152 static uint16_t dcesrv_echo_TestDoublePointer(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct echo_TestDoublePointer *r)
153 {
154 	if (!*r->in.data)
155 		return 0;
156 	if (!**r->in.data)
157 		return 0;
158 	return ***r->in.data;
159 }
160 
161 struct echo_TestSleep_private {
162 	struct dcesrv_call_state *dce_call;
163 	struct echo_TestSleep *r;
164 };
165 
echo_TestSleep_handler(struct tevent_context * ev,struct tevent_timer * te,struct timeval t,void * private_data)166 static void echo_TestSleep_handler(struct tevent_context *ev, struct tevent_timer *te,
167 				   struct timeval t, void *private_data)
168 {
169 	struct echo_TestSleep_private *p = talloc_get_type(private_data,
170 							   struct echo_TestSleep_private);
171 	struct echo_TestSleep *r = p->r;
172 	NTSTATUS status;
173 
174 	r->out.result = r->in.seconds;
175 
176 	status = dcesrv_reply(p->dce_call);
177 	if (!NT_STATUS_IS_OK(status)) {
178 		DEBUG(0,("echo_TestSleep_handler: dcesrv_reply() failed - %s\n",
179 			nt_errstr(status)));
180 	}
181 }
182 
dcesrv_echo_TestSleep(struct dcesrv_call_state * dce_call,TALLOC_CTX * mem_ctx,struct echo_TestSleep * r)183 static long dcesrv_echo_TestSleep(struct dcesrv_call_state *dce_call, TALLOC_CTX *mem_ctx, struct echo_TestSleep *r)
184 {
185 	struct echo_TestSleep_private *p;
186 
187 	if (!(dce_call->state_flags & DCESRV_CALL_STATE_FLAG_MAY_ASYNC)) {
188 		/* we're not allowed to reply async */
189 		sleep(r->in.seconds);
190 		return r->in.seconds;
191 	}
192 
193 	/* we're allowed to reply async */
194 	p = talloc(mem_ctx, struct echo_TestSleep_private);
195 	if (!p) {
196 		return 0;
197 	}
198 
199 	p->dce_call	= dce_call;
200 	p->r		= r;
201 
202 	tevent_add_timer(dce_call->event_ctx, p,
203 			timeval_add(&dce_call->time, r->in.seconds, 0),
204 			echo_TestSleep_handler, p);
205 
206 	dce_call->state_flags |= DCESRV_CALL_STATE_FLAG_ASYNC;
207 	return 0;
208 }
209 
210 /* include the generated boilerplate */
211 #include "librpc/gen_ndr/ndr_echo_s.c"
212