r6015: Add testprogs/ directory and original rpcecho sources
[samba.git] / testprogs / win32 / rpcecho / client.c
1 /* 
2    RPC echo client.
3
4    Copyright (C) Tim Potter 2003
5    
6    This program is free software; you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation; either version 2 of the License, or
9    (at your option) any later version.
10    
11    This program is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15    
16    You should have received a copy of the GNU General Public License
17    along with this program; if not, write to the Free Software
18    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <ctype.h>
24 #include "rpcecho.h"
25
26 void main(int argc, char **argv)
27 {
28         RPC_STATUS status;
29         char *binding = NULL;
30         const char *username=NULL;
31         const char *password=NULL;
32         const char *domain=NULL;
33         unsigned sec_options = 0;
34
35         argv += 1;
36         argc -= 1;
37
38         while (argc > 2 && argv[0][0] == '-') {
39                 const char *option;
40
41                 switch (argv[0][1]) {
42                 case 'e':
43                         binding = argv[1];
44                         break;
45                 case 'u':
46                         username = argv[1];
47                         break;
48                 case 'p':
49                         password = argv[1];
50                         break;
51                 case 'd':
52                         domain = argv[1];
53                         break;
54                 case '-':
55                         option = &argv[0][2];
56                         if (strcmp(option, "sign") == 0) {
57                                 if (sec_options == RPC_C_AUTHN_LEVEL_PKT_PRIVACY) {
58                                         printf("You must choose sign or seal, not both\n");
59                                         exit(1);
60                                 }
61                                 sec_options = RPC_C_AUTHN_LEVEL_PKT_INTEGRITY;
62                         } else if (strcmp(option, "seal") == 0) {
63                                 if (sec_options == RPC_C_AUTHN_LEVEL_PKT_INTEGRITY) {
64                                         printf("You must choose sign or seal, not both\n");
65                                         exit(1);
66                                 }
67                                 sec_options = RPC_C_AUTHN_LEVEL_PKT_PRIVACY;
68                         } else {
69                                 printf("Bad security option '%s'\n", option);
70                                 exit(1);
71                         }
72                         argv++;
73                         argc--;
74                         continue;
75                 default:
76                         printf("Bad option -%c\n", argv[0][0]);
77                         exit(1);
78                 }
79                 argv += 2;
80                 argc -= 2;
81         }
82
83         if (argc < 2) {
84                 printf("Usage: client [options] hostname cmd [args]\n\n");
85                 printf("Where hostname is the name of the host to connect to,\n");
86                 printf("and cmd is the command to execute with optional args:\n\n");
87                 printf("\taddone num\tAdd one to num and return the result\n");
88                 printf("\techodata size\tSend an array of size bytes and receive it back\n");
89                 printf("\tsinkdata size\tSend an array of size bytes\n");
90                 printf("\tsourcedata size\tReceive an array of size bytes\n");
91                 printf("\ttest\trun testcall\n");
92                 printf("\noptions:\n");
93                 printf("\t-u username -d domain -p password -e endpoint\n");
94                 printf("\t--sign --seal\n");
95                 printf("\nExamples:\n");
96                 printf("\tclient HOSTNAME addone 3\n");
97                 printf("\tclient 192.168.115.1 addone 3\n");
98                 printf("\tclient -e ncacn_np:HOSTNAME[\\\\pipe\\\\rpcecho] addone 3\n");
99                 printf("\tclient -e ncacn_ip_tcp:192.168.115.1 addone 3\n");
100                 printf("\tclient -e ncacn_ip_tcp:192.168.115.1 -u tridge -d MYDOMAIN -p PASSWORD addone 3\n");
101                 exit(0);
102         }
103
104
105         if (!binding) {
106                 char *network_address = argv[0];
107
108                 argc--;
109                 argv++;
110
111                 status = RpcStringBindingCompose(
112                         NULL, /* uuid */
113                         "ncacn_np",
114                         network_address,
115                         "\\pipe\\rpcecho",
116                         NULL, /* options */
117                         &binding);
118
119                 if (status) {
120                         printf("RpcStringBindingCompose returned %d\n", status);
121                         exit(status);
122                 }
123         }
124
125         printf("Endpoint is %s\n", binding);
126
127         status = RpcBindingFromStringBinding(
128                         binding,
129                         &rpcecho_IfHandle);
130
131         if (status) {
132                 printf("RpcBindingFromStringBinding returned %d\n", status);
133                 exit(status);
134         }
135
136         if (username) {
137                 SEC_WINNT_AUTH_IDENTITY ident = { username, strlen(username),
138                                                   domain, strlen(domain),
139                                                   password, strlen(password),
140                                                   SEC_WINNT_AUTH_IDENTITY_ANSI };
141
142                 status = RpcBindingSetAuthInfo(rpcecho_IfHandle, NULL,
143                                                sec_options,
144                                                RPC_C_AUTHN_WINNT,
145                                                &ident, 0);
146                 if (status) {
147                         printf ("RpcBindingSetAuthInfo failed: 0x%x\n", status);
148                         exit (1);
149                 }
150         }
151
152
153         while (argc > 0) {
154         RpcTryExcept {
155
156                 /* Add one to a number */
157
158                 if (strcmp(argv[0], "addone") == 0) {
159                         int arg, result;
160
161                         if (argc < 2) {
162                                 printf("Usage: addone num\n");
163                                 exit(1);
164                         }
165
166                         arg = atoi(argv[1]);
167
168                         AddOne(arg, &result);
169                         printf("%d + 1 = %d\n", arg, result);
170
171                         argc -= 2;
172                         argv += 2;
173                         continue;
174                 }
175
176                 /* Echo an array */
177
178                 if (strcmp(argv[0], "echodata") == 0) {
179                         int arg, i;
180                         char *indata, *outdata;
181
182                         if (argc < 2) {
183                                 printf("Usage: echo num\n");
184                                 exit(1);
185                         }
186
187                         arg = atoi(argv[1]);
188
189                         if ((indata = malloc(arg)) == NULL) {
190                                 printf("Error allocating %d bytes for input\n", arg);
191                                 exit(1);
192                         }
193
194                         if ((outdata = malloc(arg)) == NULL) {
195                                 printf("Error allocating %d bytes for output\n", arg);
196                                 exit(1);
197                         }
198
199                         for (i = 0; i < arg; i++)
200                                 indata[i] = i & 0xff;
201
202                         EchoData(arg, indata, outdata);
203
204                         printf("echo %d\n", arg);
205
206                         for (i = 0; i < arg; i++) {
207                                 if (indata[i] != outdata[i]) {
208                                         printf("data mismatch at offset %d, %d != %d\n",
209                                                 i, indata[i], outdata[i]);
210                                         exit(0);
211                                 }
212                         }
213
214                         argc -= 2;
215                         argv += 2;
216                         continue;
217                 }
218
219                 if (strcmp(argv[0], "sinkdata") == 0) {
220                         int arg, i;
221                         char *indata;
222
223                         if (argc < 2) {
224                                 printf("Usage: sinkdata num\n");
225                                 exit(1);
226                         }
227
228                         arg = atoi(argv[1]);            
229
230                         if ((indata = malloc(arg)) == NULL) {
231                                 printf("Error allocating %d bytes for input\n", arg);
232                                 exit(1);
233                         }                       
234
235                         for (i = 0; i < arg; i++)
236                                 indata[i] = i & 0xff;
237
238                         SinkData(arg, indata);
239
240                         printf("sinkdata %d\n", arg);
241                         argc -= 2;
242                         argv += 2;
243                         continue;
244                 }
245
246                 if (strcmp(argv[0], "sourcedata") == 0) {
247                         int arg, i;
248                         unsigned char *outdata;
249
250                         if (argc < 2) {
251                                 printf("Usage: sourcedata num\n");
252                                 exit(1);
253                         }
254
255                         arg = atoi(argv[1]);            
256
257                         if ((outdata = malloc(arg)) == NULL) {
258                                 printf("Error allocating %d bytes for output\n", arg);
259                                 exit(1);
260                         }                       
261
262                         SourceData(arg, outdata);
263
264                         printf("sourcedata %d\n", arg);
265
266                         for (i = 0; i < arg; i++) {
267                                 if (outdata[i] != (i & 0xff)) {
268                                         printf("data mismatch at offset %d, %d != %d\n",
269                                                 i, outdata[i], i & 0xff);
270                                 }
271                         }
272
273                         argc -= 2;
274                         argv += 2;
275                         continue;
276                 }
277
278                 if (strcmp(argv[0], "test") == 0) {
279                         printf("no TestCall\n");
280
281                         argc -= 1;
282                         argv += 1;
283                         continue;
284                 }
285
286
287                 if (strcmp(argv[0], "enum") == 0) {
288                         enum echo_Enum1 v = ECHO_ENUM1;
289                         echo_Enum2 e2;
290                         echo_Enum3 e3;
291
292                         e2.e1 = 76;
293                         e2.e2 = ECHO_ENUM1_32;
294                         e3.e1 = ECHO_ENUM2;
295                         
296                         argc -= 1;
297                         argv += 1;
298
299                         echo_TestEnum(&v, &e2, &e3);
300                         
301                         continue;
302                 }
303
304                 if (strcmp(argv[0], "sleep") == 0) {
305                         long arg, result;
306
307                         if (argc < 2) {
308                                 printf("Usage: sleep num\n");
309                                 exit(1);
310                         }
311
312                         arg = atoi(argv[1]);
313
314 //                      result = TestSleep(arg);
315 //                      printf("Slept for %d seconds\n", result);
316                         printf("Sleep disabled (need async code)\n");
317
318                         argc -= 2;
319                         argv += 2;
320                         continue;
321                 }
322
323                 printf("Invalid command '%s'\n", argv[0]);
324                 goto done;
325
326         } RpcExcept(1) {
327                 unsigned long ex;
328
329                 ex = RpcExceptionCode();
330                 printf("Runtime error 0x%x\n", ex);
331         } RpcEndExcept
332                   }
333
334 done:
335
336         status = RpcStringFree(&binding);
337
338         if (status) {
339                 printf("RpcStringFree returned %d\n", status);
340                 exit(status);
341         }
342
343         status = RpcBindingFree(&rpcecho_IfHandle);
344
345         if (status) {
346                 printf("RpcBindingFree returned %d\n", status);
347                 exit(status);
348         }
349
350         exit(0);
351 }