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