summaryrefslogtreecommitdiff
path: root/testprogs/win32/rpcecho/client.c
blob: 1724f482c388c15350609183e158f57d88b629fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/* 
   RPC echo client.

   Copyright (C) Tim Potter 2003
   
   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.
   
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.
   
   You should have received a copy of the GNU General Public License
   along with this program; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
*/

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include "rpcecho.h"

void main(int argc, char **argv)
{
	RPC_STATUS status;
	char *binding = NULL;
	const char *username=NULL;
	const char *password=NULL;
	const char *domain=NULL;
	unsigned sec_options = 0;

	argv += 1;
	argc -= 1;

	while (argc > 2 && argv[0][0] == '-') {
		const char *option;

		switch (argv[0][1]) {
		case 'e':
			binding = argv[1];
			break;
		case 'u':
			username = argv[1];
			break;
		case 'p':
			password = argv[1];
			break;
		case 'd':
			domain = argv[1];
			break;
		case '-':
			option = &argv[0][2];
			if (strcmp(option, "sign") == 0) {
				if (sec_options == RPC_C_AUTHN_LEVEL_PKT_PRIVACY) {
					printf("You must choose sign or seal, not both\n");
					exit(1);
				}
				sec_options = RPC_C_AUTHN_LEVEL_PKT_INTEGRITY;
			} else if (strcmp(option, "seal") == 0) {
				if (sec_options == RPC_C_AUTHN_LEVEL_PKT_INTEGRITY) {
					printf("You must choose sign or seal, not both\n");
					exit(1);
				}
				sec_options = RPC_C_AUTHN_LEVEL_PKT_PRIVACY;
			} else {
				printf("Bad security option '%s'\n", option);
				exit(1);
			}
			argv++;
			argc--;
			continue;
		default:
			printf("Bad option -%c\n", argv[0][0]);
			exit(1);
		}
		argv += 2;
		argc -= 2;
	}

	if (argc < 2) {
		printf("Usage: client [options] hostname cmd [args]\n\n");
		printf("Where hostname is the name of the host to connect to,\n");
		printf("and cmd is the command to execute with optional args:\n\n");
		printf("\taddone num\tAdd one to num and return the result\n");
		printf("\techodata size\tSend an array of size bytes and receive it back\n");
		printf("\tsinkdata size\tSend an array of size bytes\n");
		printf("\tsourcedata size\tReceive an array of size bytes\n");
		printf("\ttest\trun testcall\n");
		printf("\noptions:\n");
		printf("\t-u username -d domain -p password -e endpoint\n");
		printf("\t--sign --seal\n");
		printf("\nExamples:\n");
		printf("\tclient HOSTNAME addone 3\n");
		printf("\tclient 192.168.115.1 addone 3\n");
		printf("\tclient -e ncacn_np:HOSTNAME[\\\\pipe\\\\rpcecho] addone 3\n");
		printf("\tclient -e ncacn_ip_tcp:192.168.115.1 addone 3\n");
		printf("\tclient -e ncacn_ip_tcp:192.168.115.1 -u tridge -d MYDOMAIN -p PASSWORD addone 3\n");
		exit(0);
	}


	if (!binding) {
		char *network_address = argv[0];

		argc--;
		argv++;

		status = RpcStringBindingCompose(
			NULL, /* uuid */
			"ncacn_np",
			network_address,
			"\\pipe\\rpcecho",
			NULL, /* options */
			&binding);

		if (status) {
			printf("RpcStringBindingCompose returned %d\n", status);
			exit(status);
		}
	}

	printf("Endpoint is %s\n", binding);

	status = RpcBindingFromStringBinding(
			binding,
			&rpcecho_IfHandle);

	if (status) {
		printf("RpcBindingFromStringBinding returned %d\n", status);
		exit(status);
	}

	if (username) {
		SEC_WINNT_AUTH_IDENTITY ident = { username, strlen(username),
						  domain, strlen(domain),
						  password, strlen(password),
						  SEC_WINNT_AUTH_IDENTITY_ANSI };

		status = RpcBindingSetAuthInfo(rpcecho_IfHandle, NULL,
					       sec_options,
					       RPC_C_AUTHN_WINNT,
					       &ident, 0);
		if (status) {
			printf ("RpcBindingSetAuthInfo failed: 0x%x\n", status);
			exit (1);
		}
	}


	while (argc > 0) {
	RpcTryExcept {

		/* Add one to a number */

		if (strcmp(argv[0], "addone") == 0) {
			int arg, result;

			if (argc < 2) {
				printf("Usage: addone num\n");
				exit(1);
			}

			arg = atoi(argv[1]);

			AddOne(arg, &result);
			printf("%d + 1 = %d\n", arg, result);

			argc -= 2;
			argv += 2;
			continue;
		}

		/* Echo an array */

		if (strcmp(argv[0], "echodata") == 0) {
			int arg, i;
			char *indata, *outdata;

			if (argc < 2) {
				printf("Usage: echo num\n");
				exit(1);
			}

			arg = atoi(argv[1]);

			if ((indata = malloc(arg)) == NULL) {
				printf("Error allocating %d bytes for input\n", arg);
				exit(1);
			}

			if ((outdata = malloc(arg)) == NULL) {
				printf("Error allocating %d bytes for output\n", arg);
				exit(1);
			}

			for (i = 0; i < arg; i++)
				indata[i] = i & 0xff;

			EchoData(arg, indata, outdata);

			printf("echo %d\n", arg);

			for (i = 0; i < arg; i++) {
				if (indata[i] != outdata[i]) {
					printf("data mismatch at offset %d, %d != %d\n",
						i, indata[i], outdata[i]);
					exit(0);
				}
			}

			argc -= 2;
			argv += 2;
			continue;
		}

		if (strcmp(argv[0], "sinkdata") == 0) {
			int arg, i;
			char *indata;

			if (argc < 2) {
				printf("Usage: sinkdata num\n");
				exit(1);
			}

			arg = atoi(argv[1]);		

			if ((indata = malloc(arg)) == NULL) {
				printf("Error allocating %d bytes for input\n", arg);
				exit(1);
			}			

			for (i = 0; i < arg; i++)
				indata[i] = i & 0xff;

			SinkData(arg, indata);

			printf("sinkdata %d\n", arg);
			argc -= 2;
			argv += 2;
			continue;
		}

		if (strcmp(argv[0], "sourcedata") == 0) {
			int arg, i;
			unsigned char *outdata;

			if (argc < 2) {
				printf("Usage: sourcedata num\n");
				exit(1);
			}

			arg = atoi(argv[1]);		

			if ((outdata = malloc(arg)) == NULL) {
				printf("Error allocating %d bytes for output\n", arg);
				exit(1);
			}			

			SourceData(arg, outdata);

			printf("sourcedata %d\n", arg);

			for (i = 0; i < arg; i++) {
				if (outdata[i] != (i & 0xff)) {
					printf("data mismatch at offset %d, %d != %d\n",
						i, outdata[i], i & 0xff);
				}
			}

			argc -= 2;
			argv += 2;
			continue;
		}

		if (strcmp(argv[0], "test") == 0) {
			printf("no TestCall\n");

			argc -= 1;
			argv += 1;
			continue;
		}


		if (strcmp(argv[0], "enum") == 0) {
			enum echo_Enum1 v = ECHO_ENUM1;
			echo_Enum2 e2;
			echo_Enum3 e3;

			e2.e1 = 76;
			e2.e2 = ECHO_ENUM1_32;
			e3.e1 = ECHO_ENUM2;
			
			argc -= 1;
			argv += 1;

			echo_TestEnum(&v, &e2, &e3);
			
			continue;
		}

		if (strcmp(argv[0], "sleep") == 0) {
			long arg, result;

			if (argc < 2) {
				printf("Usage: sleep num\n");
				exit(1);
			}

			arg = atoi(argv[1]);

//			result = TestSleep(arg);
//			printf("Slept for %d seconds\n", result);
			printf("Sleep disabled (need async code)\n");

			argc -= 2;
			argv += 2;
			continue;
		}

		printf("Invalid command '%s'\n", argv[0]);
		goto done;

	} RpcExcept(1) {
		unsigned long ex;

		ex = RpcExceptionCode();
		printf("Runtime error 0x%x\n", ex);
	} RpcEndExcept
		  }

done:

	status = RpcStringFree(&binding);

	if (status) {
		printf("RpcStringFree returned %d\n", status);
		exit(status);
	}

	status = RpcBindingFree(&rpcecho_IfHandle);

	if (status) {
		printf("RpcBindingFree returned %d\n", status);
		exit(status);
	}

	exit(0);
}