File: portmap-client.c

package info (click to toggle)
libnfs 5.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,648 kB
  • sloc: ansic: 39,600; sh: 1,654; makefile: 315; xml: 178
file content (586 lines) | stat: -rw-r--r-- 14,381 bytes parent folder | download
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
/* 
   Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2014
   
   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 3 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, see <http://www.gnu.org/licenses/>.
*/

/* Example program using the lowlevel raw interface.
 * This allow accurate control of the exact commands that are being used.
 */
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef WIN32
#include <win32/win32_compat.h>
#pragma comment(lib, "ws2_32.lib")
WSADATA wsaData;
#endif

#ifdef HAVE_POLL_H
#include <poll.h>
#endif

#ifdef HAVE_NETINET_IN_H
#include <netinet/in.h>
#endif

#ifdef HAVE_NETDB_H
#include <netdb.h>
#endif

#include <stdio.h>

#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif

#ifdef HAVE_STRING_H
#include <string.h>
#endif

#ifdef HAVE_SYS_SOCKET_H
#include <sys/socket.h>
#endif

#include <time.h>
#include "libnfs.h"
#include "libnfs-raw.h"
#include "libnfs-raw-mount.h"
#include "libnfs-raw-nfs.h"
#include "libnfs-raw-portmap.h"
#include "libnfs-raw-rquota.h"

struct client {
       int is_finished;
};

void pmap2_dump_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
{
	struct client *client = private_data;
	struct pmap2_dump_result *dr = data;
	struct pmap2_mapping_list *list = dr->list;

	if (status == RPC_STATUS_ERROR) {
		printf("PORTMAP2/DUMP call failed with \"%s\"\n", (char *)data);
		exit(10);
	}
	if (status != RPC_STATUS_SUCCESS) {
		printf("PORTMAP2/DUMP call failed, status:%d\n", status);
		exit(10);
	}

	printf("PORTMAP2/DUMP:\n");
	while (list) {
		printf("	Prog:%d Vers:%d Protocol:%d Port:%d\n",
			list->map.prog,
			list->map.vers,
			list->map.prot,
			list->map.port);
		list = list->next;
	}
	client->is_finished = 1;
}

void pmap3_dump_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
{
	struct client *client = private_data;
	struct pmap3_dump_result *dr = data;
	struct pmap3_mapping_list *list = dr->list;

	if (status == RPC_STATUS_ERROR) {
		printf("PORTMAP3/DUMP call failed with \"%s\"\n", (char *)data);
		exit(10);
	}
	if (status != RPC_STATUS_SUCCESS) {
		printf("PORTMAP3/DUMP call failed, status:%d\n", status);
		exit(10);
	}

	printf("PORTMAP3/DUMP:\n");
	while (list) {
		printf("	Prog:%d Vers:%d Netid:%s Addr:%s Owner:%s\n",
			list->map.prog,
			list->map.vers,
			list->map.netid,
			list->map.addr,
			list->map.owner);
		list = list->next;
	}
	client->is_finished = 1;
}

void pmap3_getaddr_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
{
	struct client *client = private_data;
	struct pmap3_string_result *gar = data;

	if (status == RPC_STATUS_ERROR) {
		printf("PORTMAP3/GETADDR call failed with \"%s\"\n", (char *)data);
		exit(10);
	}
	if (status != RPC_STATUS_SUCCESS) {
		printf("PORTMAP3/GETADDR call failed, status:%d\n", status);
		exit(10);
	}

	printf("PORTMAP3/GETADDR:\n");
	printf("	Addr:%s\n", gar->addr);

	client->is_finished = 1;
}

void pmap2_set_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
{
	struct client *client = private_data;
	uint32_t res = *(uint32_t *)data;

	if (status == RPC_STATUS_ERROR) {
		printf("PORTMAP2/SET call failed with \"%s\"\n", (char *)data);
		exit(10);
	}
	if (status != RPC_STATUS_SUCCESS) {
		printf("PORTMAP2/SET call failed, status:%d\n", status);
		exit(10);
	}

	printf("PORTMAP2/SET:\n");
	printf("	Res:%d\n", res);

	client->is_finished = 1;
}

void pmap2_unset_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
{
	struct client *client = private_data;
	uint32_t res = *(uint32_t *)data;

	if (status == RPC_STATUS_ERROR) {
		printf("PORTMAP2/UNSET call failed with \"%s\"\n", (char *)data);
		exit(10);
	}
	if (status != RPC_STATUS_SUCCESS) {
		printf("PORTMAP2/UNSET call failed, status:%d\n", status);
		exit(10);
	}

	printf("PORTMAP2/UNSET:\n");
	printf("	Res:%d\n", res);

	client->is_finished = 1;
}

void pmap3_set_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
{
	struct client *client = private_data;
	uint32_t res = *(uint32_t *)data;

	if (status == RPC_STATUS_ERROR) {
		printf("PORTMAP3/SET call failed with \"%s\"\n", (char *)data);
		exit(10);
	}
	if (status != RPC_STATUS_SUCCESS) {
		printf("PORTMAP3/SET call failed, status:%d\n", status);
		exit(10);
	}

	printf("PORTMAP3/SET:\n");
	printf("	Res:%d\n", res);

	client->is_finished = 1;
}

void pmap3_unset_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
{
	struct client *client = private_data;
	uint32_t res = *(uint32_t *)data;

	if (status == RPC_STATUS_ERROR) {
		printf("PORTMAP3/UNSET call failed with \"%s\"\n", (char *)data);
		exit(10);
	}
	if (status != RPC_STATUS_SUCCESS) {
		printf("PORTMAP3/UNSET call failed, status:%d\n", status);
		exit(10);
	}

	printf("PORTMAP3/UNSET:\n");
	printf("	Res:%d\n", res);

	client->is_finished = 1;
}

void pmap3_gettime_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
{
	struct client *client = private_data;
	time_t t = *(uint32_t *)data;

	if (status == RPC_STATUS_ERROR) {
		printf("PORTMAP3/GETTIME call failed with \"%s\"\n", (char *)data);
		exit(10);
	}
	if (status != RPC_STATUS_SUCCESS) {
		printf("PORTMAP3/GETTIME call failed, status:%d\n", status);
		exit(10);
	}

	printf("PORTMAP3/GETTIME:\n");
	printf("	Time:%d %s\n", (int)t, ctime(&t));

	client->is_finished = 1;
}

void pmap3_uaddr2taddr_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
{
	struct client *client = private_data;
	struct pmap3_netbuf *nb = data;
	struct sockaddr_storage *ss;
	char host[256], port[6];
	int i;

	if (status == RPC_STATUS_ERROR) {
		printf("PORTMAP3/UADDR2TADDR call failed with \"%s\"\n", (char *)data);
		exit(10);
	}
	if (status != RPC_STATUS_SUCCESS) {
		printf("PORTMAP3/UADDR2TADDR call failed, status:%d\n", status);
		exit(10);
	}

	printf("PORTMAP3/UADDR2TADDR:\n");
	printf("	MaxLen:%d\n", nb->maxlen);
	printf("        ");
	for (i = 0; i < nb->maxlen; i++) {
		printf("%02x ", nb->buf.buf_val[i]);
		if (i %16 == 15) {
			printf("\n        ");
		}
	}
	printf("\n");
	printf("        ---\n");
	ss = (struct sockaddr_storage *)&nb->buf.buf_val[0];
	getnameinfo((struct sockaddr *)ss, sizeof(struct sockaddr_storage),
		&host[0], sizeof(host), &port[0], sizeof(port),
		NI_NUMERICHOST|NI_NUMERICSERV);
	switch (ss->ss_family) {
	case AF_INET:
		printf("        IPv4: %s:%s\n", &host[0], &port[0]);
		break;
	case AF_INET6:
		printf("        IPv6: %s:%s\n", &host[0], &port[0]);
		break;
	}
	client->is_finished = 1;
}

void pmap2_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
{
	struct client *client = private_data;

	if (status == RPC_STATUS_ERROR) {
		printf("PORTMAP2/NULL call failed with \"%s\"\n", (char *)data);
		exit(10);
	}
	if (status != RPC_STATUS_SUCCESS) {
		printf("PORTMAP2/NULL call failed, status:%d\n", status);
		exit(10);
	}

	printf("PORTMAP2/NULL responded and server is alive\n");
	client->is_finished = 1;
}

void pmap3_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
{
	struct client *client = private_data;

	if (status == RPC_STATUS_ERROR) {
		printf("PORTMAP3/NULL call failed with \"%s\"\n", (char *)data);
		exit(10);
	}
	if (status != RPC_STATUS_SUCCESS) {
		printf("PORTMAP3/NULL call failed, status:%d\n", status);
		exit(10);
	}

	printf("PORTMAP3/NULL responded and server is alive\n");
	client->is_finished = 1;
}

void pmap_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data)
{
	struct client *client = private_data;

	if (status == RPC_STATUS_ERROR) {
		printf("PORTMAP/NULL call failed with \"%s\"\n", (char *)data);
		exit(10);
	}
	if (status != RPC_STATUS_SUCCESS) {
		printf("PORTMAP/NULL call failed, status:%d\n", status);
		exit(10);
	}

	client->is_finished = 1;
}

void pmap_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data)
{
	struct client *client = private_data;

	if (status != RPC_STATUS_SUCCESS) {
		printf("connection to portmapper failed\n");
		exit(10);
	}

	if (rpc_pmap2_null_async(rpc, pmap_null_cb, client) != 0) {
		printf("Failed to send null request\n");
		exit(10);
	}
}


static void wait_until_finished(struct rpc_context *rpc, struct client *client)
{
	struct pollfd pfd;

	client->is_finished = 0;
	for (;;) {
		pfd.fd = rpc_get_fd(rpc);
		pfd.events = rpc_which_events(rpc);

		if (poll(&pfd, 1, -1) < 0) {
			printf("Poll failed");
			exit(10);
		}
		if (rpc_service(rpc, pfd.revents) < 0) {
			printf("rpc_service failed\n");
			break;
		}
		if (client->is_finished) {
			break;
		}
	}
}

int main(int argc _U_, char *argv[] _U_)
{
	struct rpc_context *rpc;
	struct client client;
	char *server = NULL;
	int i;
	int null2 = 0;
	int dump2 = 0;
	int null3 = 0;
	int set2 = 0;
	int unset2 = 0;
	int set3 = 0;
	int unset3 = 0;
	int getaddr3 = 0;
	int dump3 = 0;
	int gettime3 = 0;
	int u2t3 = 0;
	int command_found = 0;

	int set2prog, set2vers, set2prot, set2port;
	int unset2prog, unset2vers, unset2prot, unset2port;
	int set3prog, set3vers;
	char *set3netid, *set3addr, *set3owner;
	int unset3prog, unset3vers;
	char *unset3netid, *unset3addr, *unset3owner;
	int getaddr3prog, getaddr3vers;
	char *getaddr3netid, *getaddr3addr, *getaddr3owner;
	char *u2t3string;

#ifdef WIN32
	if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
		printf("Failed to start Winsock2\n");
		exit(10);
	}
#endif

	rpc = rpc_init_context();
	if (rpc == NULL) {
		printf("failed to init context\n");
		exit(10);
	}

	for (i = 1; i < argc; i++) {
		if (!strcmp(argv[i], "dump2")) {
			dump2 = 1;
			command_found++;
		} else if (!strcmp(argv[i], "null2")) {
			null2 = 1;
			command_found++;
		} else if (!strcmp(argv[i], "set2")) {
			set2 = 1;
			set2prog = atoi(argv[++i]);
			set2vers = atoi(argv[++i]);
			set2prot = atoi(argv[++i]);
			set2port = atoi(argv[++i]);
			command_found++;
		} else if (!strcmp(argv[i], "unset2")) {
			unset2 = 1;
			unset2prog = atoi(argv[++i]);
			unset2vers = atoi(argv[++i]);
			unset2prot = atoi(argv[++i]);
			unset2port = atoi(argv[++i]);
			command_found++;
		} else if (!strcmp(argv[i], "dump3")) {
			dump3 = 1;
			command_found++;
		} else if (!strcmp(argv[i], "gettime3")) {
			gettime3 = 1;
			command_found++;
		} else if (!strcmp(argv[i], "u2t3")) {
			u2t3 = 1;
			u2t3string = argv[++i];
			command_found++;
		} else if (!strcmp(argv[i], "getaddr3")) {
			getaddr3 = 1;
			getaddr3prog = atoi(argv[++i]);
			getaddr3vers = atoi(argv[++i]);
			getaddr3netid = argv[++i];
			getaddr3addr  = argv[++i];
			getaddr3owner = argv[++i];
			command_found++;
		} else if (!strcmp(argv[i], "set3")) {
			set3 = 1;
			set3prog = atoi(argv[++i]);
			set3vers = atoi(argv[++i]);
			set3netid = argv[++i];
			set3addr  = argv[++i];
			set3owner = argv[++i];
			command_found++;
		} else if (!strcmp(argv[i], "null3")) {
			null3 = 1;
			command_found++;
		} else {
			server = argv[i];
		}
	}
	if (command_found == 0 || server == NULL) {
		fprintf(stderr, "Usage: portmap-client <command*> <server>\n");
		exit(10);
	}

	if (rpc_connect_async(rpc, server, 111, pmap_connect_cb, &client) != 0) {
		printf("Failed to start connection\n");
		exit(10);
	}
	wait_until_finished(rpc, &client);

	if (null2) {
		if (rpc_pmap2_null_async(rpc, pmap2_null_cb, &client) != 0) {
			printf("Failed to send NULL2 request\n");
			exit(10);
		}
		wait_until_finished(rpc, &client);
	}
	if (dump2) {
		if (rpc_pmap2_dump_async(rpc, pmap2_dump_cb, &client) != 0) {
			printf("Failed to send DUMP2 request\n");
			exit(10);
		}
		wait_until_finished(rpc, &client);
	}
	if (null3) {
		if (rpc_pmap3_null_async(rpc, pmap3_null_cb, &client) != 0) {
			printf("Failed to send NULL3 request\n");
			exit(10);
		}
		wait_until_finished(rpc, &client);
	}
	if (dump3) {
		if (rpc_pmap3_dump_async(rpc, pmap3_dump_cb, &client) != 0) {
			printf("Failed to send DUMP3 request\n");
			exit(10);
		}
		wait_until_finished(rpc, &client);
	}
	if (gettime3) {
		if (rpc_pmap3_gettime_async(rpc, pmap3_gettime_cb, &client) != 0) {
			printf("Failed to send GETTIME3 request\n");
			exit(10);
		}
		wait_until_finished(rpc, &client);
	}
	if (u2t3) {
		if (rpc_pmap3_uaddr2taddr_async(rpc, u2t3string, pmap3_uaddr2taddr_cb, &client) != 0) {
			printf("Failed to send UADDR2TADDR3 request\n");
			exit(10);
		}
		wait_until_finished(rpc, &client);
	}
	if (getaddr3) {
		struct pmap3_mapping map;

		map.prog  = getaddr3prog;
		map.vers  = getaddr3vers;
		map.netid = getaddr3netid;
		map.addr  = getaddr3addr;
		map.owner = getaddr3owner;
		if (rpc_pmap3_getaddr_async(rpc, &map, pmap3_getaddr_cb, &client) != 0) {
			printf("Failed to send GETADDR3 request\n");
			exit(10);
		}
		wait_until_finished(rpc, &client);
	}
	if (set2) {
		if (rpc_pmap2_set_async(rpc, set2prog, set2vers, set2prot, set2port, pmap2_set_cb, &client) != 0) {
			printf("Failed to send SET2 request\n");
			exit(10);
		}
		wait_until_finished(rpc, &client);
	}
	if (unset2) {
		if (rpc_pmap2_unset_async(rpc, unset2prog, unset2vers, unset2prot, unset2port, pmap2_unset_cb, &client) != 0) {
			printf("Failed to send UNSET2 request\n");
			exit(10);
		}
		wait_until_finished(rpc, &client);
	}
	if (set3) {
		struct pmap3_mapping map;

		map.prog  = set3prog;
		map.vers  = set3vers;
		map.netid = set3netid;
		map.addr  = set3addr;
		map.owner = set3owner;
		if (rpc_pmap3_set_async(rpc, &map, pmap3_set_cb, &client) != 0) {
			printf("Failed to send SET3 request\n");
			exit(10);
		}
		wait_until_finished(rpc, &client);
	}
	if (unset3) {
		struct pmap3_mapping map;

		map.prog  = unset3prog;
		map.vers  = unset3vers;
		map.netid = unset3netid;
		map.addr  = unset3addr;
		map.owner = unset3owner;
		if (rpc_pmap3_unset_async(rpc, &map, pmap3_unset_cb, &client) != 0) {
			printf("Failed to send UNSET3 request\n");
			exit(10);
		}
		wait_until_finished(rpc, &client);
	}

	
	rpc_destroy_context(rpc);
	rpc=NULL;
	return 0;
}