| 1 | /* |
| 2 | Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2014 |
| 3 | |
| 4 | This program is free software; you can redistribute it and/or modify |
| 5 | it under the terms of the GNU General Public License as published by |
| 6 | the Free Software Foundation; either version 3 of the License, or |
| 7 | (at your option) any later version. |
| 8 | |
| 9 | This program is distributed in the hope that it will be useful, |
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | GNU General Public License for more details. |
| 13 | |
| 14 | You should have received a copy of the GNU General Public License |
| 15 | along with this program; if not, see <http://www.gnu.org/licenses/>. |
| 16 | */ |
| 17 | |
| 18 | /* Example program using the lowlevel raw interface. |
| 19 | * This allow accurate control of the exact commands that are being used. |
| 20 | */ |
| 21 | #ifdef HAVE_CONFIG_H |
| 22 | #include "config.h" |
| 23 | #endif |
| 24 | |
| 25 | #ifdef WIN32 |
| 26 | #include "win32_compat.h" |
| 27 | #endif |
| 28 | |
| 29 | #ifdef HAVE_POLL_H |
| 30 | #include <poll.h> |
| 31 | #endif |
| 32 | |
| 33 | #ifdef HAVE_NETINET_IN_H |
| 34 | #include <netinet/in.h> |
| 35 | #endif |
| 36 | |
| 37 | #include <netdb.h> |
| 38 | #include <stdio.h> |
| 39 | #include <stdlib.h> |
| 40 | #include <string.h> |
| 41 | #include <sys/socket.h> |
| 42 | #include <time.h> |
| 43 | #include "libnfs-zdr.h" |
| 44 | #include "libnfs.h" |
| 45 | #include "libnfs-raw.h" |
| 46 | #include "libnfs-raw-mount.h" |
| 47 | #include "libnfs-raw-nfs.h" |
| 48 | #include "libnfs-raw-portmap.h" |
| 49 | #include "libnfs-raw-rquota.h" |
| 50 | |
| 51 | struct client { |
| 52 | int is_finished; |
| 53 | }; |
| 54 | |
| 55 | void pmap2_dump_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
| 56 | { |
| 57 | struct client *client = private_data; |
| 58 | struct pmap2_dump_result *dr = data; |
| 59 | struct pmap2_mapping_list *list = dr->list; |
| 60 | |
| 61 | if (status == RPC_STATUS_ERROR) { |
| 62 | printf("PORTMAP2/DUMP call failed with \"%s\"\n", (char *)data); |
| 63 | exit(10); |
| 64 | } |
| 65 | if (status != RPC_STATUS_SUCCESS) { |
| 66 | printf("PORTMAP2/DUMP call failed, status:%d\n", status); |
| 67 | exit(10); |
| 68 | } |
| 69 | |
| 70 | printf("PORTMAP2/DUMP:\n"); |
| 71 | while (list) { |
| 72 | printf(" Prog:%d Vers:%d Protocol:%d Port:%d\n", |
| 73 | list->map.prog, |
| 74 | list->map.vers, |
| 75 | list->map.prot, |
| 76 | list->map.port); |
| 77 | list = list->next; |
| 78 | } |
| 79 | client->is_finished = 1; |
| 80 | } |
| 81 | |
| 82 | void pmap3_dump_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
| 83 | { |
| 84 | struct client *client = private_data; |
| 85 | struct pmap3_dump_result *dr = data; |
| 86 | struct pmap3_mapping_list *list = dr->list; |
| 87 | |
| 88 | if (status == RPC_STATUS_ERROR) { |
| 89 | printf("PORTMAP3/DUMP call failed with \"%s\"\n", (char *)data); |
| 90 | exit(10); |
| 91 | } |
| 92 | if (status != RPC_STATUS_SUCCESS) { |
| 93 | printf("PORTMAP3/DUMP call failed, status:%d\n", status); |
| 94 | exit(10); |
| 95 | } |
| 96 | |
| 97 | printf("PORTMAP3/DUMP:\n"); |
| 98 | while (list) { |
| 99 | printf(" Prog:%d Vers:%d Netid:%s Addr:%s Owner:%s\n", |
| 100 | list->map.prog, |
| 101 | list->map.vers, |
| 102 | list->map.netid, |
| 103 | list->map.addr, |
| 104 | list->map.owner); |
| 105 | list = list->next; |
| 106 | } |
| 107 | client->is_finished = 1; |
| 108 | } |
| 109 | |
| 110 | void pmap3_getaddr_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
| 111 | { |
| 112 | struct client *client = private_data; |
| 113 | struct pmap3_string_result *gar = data; |
| 114 | |
| 115 | if (status == RPC_STATUS_ERROR) { |
| 116 | printf("PORTMAP3/GETADDR call failed with \"%s\"\n", (char *)data); |
| 117 | exit(10); |
| 118 | } |
| 119 | if (status != RPC_STATUS_SUCCESS) { |
| 120 | printf("PORTMAP3/GETADDR call failed, status:%d\n", status); |
| 121 | exit(10); |
| 122 | } |
| 123 | |
| 124 | printf("PORTMAP3/GETADDR:\n"); |
| 125 | printf(" Addr:%s\n", gar->addr); |
| 126 | |
| 127 | client->is_finished = 1; |
| 128 | } |
| 129 | |
| 130 | void pmap3_set_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
| 131 | { |
| 132 | struct client *client = private_data; |
| 133 | uint32_t res = *(uint32_t *)data; |
| 134 | |
| 135 | if (status == RPC_STATUS_ERROR) { |
| 136 | printf("PORTMAP3/SET call failed with \"%s\"\n", (char *)data); |
| 137 | exit(10); |
| 138 | } |
| 139 | if (status != RPC_STATUS_SUCCESS) { |
| 140 | printf("PORTMAP3/SET call failed, status:%d\n", status); |
| 141 | exit(10); |
| 142 | } |
| 143 | |
| 144 | printf("PORTMAP3/SET:\n"); |
| 145 | printf(" Res:%d\n", res); |
| 146 | |
| 147 | client->is_finished = 1; |
| 148 | } |
| 149 | |
| 150 | void pmap3_unset_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
| 151 | { |
| 152 | struct client *client = private_data; |
| 153 | uint32_t res = *(uint32_t *)data; |
| 154 | |
| 155 | if (status == RPC_STATUS_ERROR) { |
| 156 | printf("PORTMAP3/UNSET call failed with \"%s\"\n", (char *)data); |
| 157 | exit(10); |
| 158 | } |
| 159 | if (status != RPC_STATUS_SUCCESS) { |
| 160 | printf("PORTMAP3/UNSET call failed, status:%d\n", status); |
| 161 | exit(10); |
| 162 | } |
| 163 | |
| 164 | printf("PORTMAP3/UNSET:\n"); |
| 165 | printf(" Res:%d\n", res); |
| 166 | |
| 167 | client->is_finished = 1; |
| 168 | } |
| 169 | |
| 170 | void pmap3_gettime_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
| 171 | { |
| 172 | struct client *client = private_data; |
| 173 | time_t t = *(uint32_t *)data; |
| 174 | |
| 175 | if (status == RPC_STATUS_ERROR) { |
| 176 | printf("PORTMAP3/GETTIME call failed with \"%s\"\n", (char *)data); |
| 177 | exit(10); |
| 178 | } |
| 179 | if (status != RPC_STATUS_SUCCESS) { |
| 180 | printf("PORTMAP3/GETTIME call failed, status:%d\n", status); |
| 181 | exit(10); |
| 182 | } |
| 183 | |
| 184 | printf("PORTMAP3/GETTIME:\n"); |
| 185 | printf(" Time:%d %s\n", (int)t, ctime(&t)); |
| 186 | |
| 187 | client->is_finished = 1; |
| 188 | } |
| 189 | |
| 190 | void pmap3_uaddr2taddr_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
| 191 | { |
| 192 | struct client *client = private_data; |
| 193 | struct pmap3_netbuf *nb = data; |
| 194 | struct sockaddr_storage *ss; |
| 195 | char host[256], port[6]; |
| 196 | int i; |
| 197 | |
| 198 | if (status == RPC_STATUS_ERROR) { |
| 199 | printf("PORTMAP3/UADDR2TADDR call failed with \"%s\"\n", (char *)data); |
| 200 | exit(10); |
| 201 | } |
| 202 | if (status != RPC_STATUS_SUCCESS) { |
| 203 | printf("PORTMAP3/UADDR2TADDR call failed, status:%d\n", status); |
| 204 | exit(10); |
| 205 | } |
| 206 | |
| 207 | printf("PORTMAP3/UADDR2TADDR:\n"); |
| 208 | printf(" MaxLen:%d\n", nb->maxlen); |
| 209 | printf(" "); |
| 210 | for (i = 0; i < nb->maxlen; i++) { |
| 211 | printf("%02x ", nb->buf.buf_val[i]); |
| 212 | if (i %16 == 15) { |
| 213 | printf("\n "); |
| 214 | } |
| 215 | } |
| 216 | printf("\n"); |
| 217 | printf(" ---\n"); |
| 218 | ss = (struct sockaddr_storage *)&nb->buf.buf_val[0]; |
| 219 | getnameinfo((struct sockaddr *)ss, sizeof(struct sockaddr_storage), |
| 220 | &host[0], sizeof(host), &port[0], sizeof(port), |
| 221 | NI_NUMERICHOST|NI_NUMERICSERV); |
| 222 | switch (ss->ss_family) { |
| 223 | case AF_INET: |
| 224 | printf(" IPv4: %s:%s\n", &host[0], &port[0]); |
| 225 | break; |
| 226 | case AF_INET6: |
| 227 | printf(" IPv6: %s:%s\n", &host[0], &port[0]); |
| 228 | break; |
| 229 | } |
| 230 | client->is_finished = 1; |
| 231 | } |
| 232 | |
| 233 | void pmap2_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
| 234 | { |
| 235 | struct client *client = private_data; |
| 236 | |
| 237 | if (status == RPC_STATUS_ERROR) { |
| 238 | printf("PORTMAP2/NULL call failed with \"%s\"\n", (char *)data); |
| 239 | exit(10); |
| 240 | } |
| 241 | if (status != RPC_STATUS_SUCCESS) { |
| 242 | printf("PORTMAP2/NULL call failed, status:%d\n", status); |
| 243 | exit(10); |
| 244 | } |
| 245 | |
| 246 | printf("PORTMAP2/NULL responded and server is alive\n"); |
| 247 | client->is_finished = 1; |
| 248 | } |
| 249 | |
| 250 | void pmap3_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
| 251 | { |
| 252 | struct client *client = private_data; |
| 253 | |
| 254 | if (status == RPC_STATUS_ERROR) { |
| 255 | printf("PORTMAP3/NULL call failed with \"%s\"\n", (char *)data); |
| 256 | exit(10); |
| 257 | } |
| 258 | if (status != RPC_STATUS_SUCCESS) { |
| 259 | printf("PORTMAP3/NULL call failed, status:%d\n", status); |
| 260 | exit(10); |
| 261 | } |
| 262 | |
| 263 | printf("PORTMAP3/NULL responded and server is alive\n"); |
| 264 | client->is_finished = 1; |
| 265 | } |
| 266 | |
| 267 | void pmap_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
| 268 | { |
| 269 | struct client *client = private_data; |
| 270 | |
| 271 | if (status == RPC_STATUS_ERROR) { |
| 272 | printf("PORTMAP/NULL call failed with \"%s\"\n", (char *)data); |
| 273 | exit(10); |
| 274 | } |
| 275 | if (status != RPC_STATUS_SUCCESS) { |
| 276 | printf("PORTMAP/NULL call failed, status:%d\n", status); |
| 277 | exit(10); |
| 278 | } |
| 279 | |
| 280 | client->is_finished = 1; |
| 281 | } |
| 282 | |
| 283 | void pmap_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data) |
| 284 | { |
| 285 | struct client *client = private_data; |
| 286 | |
| 287 | if (status != RPC_STATUS_SUCCESS) { |
| 288 | printf("connection to portmapper failed\n"); |
| 289 | exit(10); |
| 290 | } |
| 291 | |
| 292 | if (rpc_pmap2_null_async(rpc, pmap_null_cb, client) != 0) { |
| 293 | printf("Failed to send null request\n"); |
| 294 | exit(10); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | |
| 299 | static void wait_until_finished(struct rpc_context *rpc, struct client *client) |
| 300 | { |
| 301 | struct pollfd pfd; |
| 302 | |
| 303 | client->is_finished = 0; |
| 304 | for (;;) { |
| 305 | pfd.fd = rpc_get_fd(rpc); |
| 306 | pfd.events = rpc_which_events(rpc); |
| 307 | |
| 308 | if (poll(&pfd, 1, -1) < 0) { |
| 309 | printf("Poll failed"); |
| 310 | exit(10); |
| 311 | } |
| 312 | if (rpc_service(rpc, pfd.revents) < 0) { |
| 313 | printf("rpc_service failed\n"); |
| 314 | break; |
| 315 | } |
| 316 | if (client->is_finished) { |
| 317 | break; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | int main(int argc _U_, char *argv[] _U_) |
| 323 | { |
| 324 | struct rpc_context *rpc; |
| 325 | struct client client; |
| 326 | char *server = NULL; |
| 327 | int i; |
| 328 | int null2 = 0; |
| 329 | int dump2 = 0; |
| 330 | int null3 = 0; |
| 331 | int set3 = 0; |
| 332 | int unset3 = 0; |
| 333 | int getaddr3 = 0; |
| 334 | int dump3 = 0; |
| 335 | int gettime3 = 0; |
| 336 | int u2t3 = 0; |
| 337 | int command_found = 0; |
| 338 | |
| 339 | int set3prog, set3vers; |
| 340 | char *set3netid, *set3addr, *set3owner; |
| 341 | int unset3prog, unset3vers; |
| 342 | char *unset3netid, *unset3addr, *unset3owner; |
| 343 | int getaddr3prog, getaddr3vers; |
| 344 | char *getaddr3netid, *getaddr3addr, *getaddr3owner; |
| 345 | char *u2t3string; |
| 346 | |
| 347 | rpc = rpc_init_context(); |
| 348 | if (rpc == NULL) { |
| 349 | printf("failed to init context\n"); |
| 350 | exit(10); |
| 351 | } |
| 352 | |
| 353 | for (i = 1; i < argc; i++) { |
| 354 | if (!strcmp(argv[i], "dump2")) { |
| 355 | dump2 = 1; |
| 356 | command_found++; |
| 357 | } else if (!strcmp(argv[i], "null2")) { |
| 358 | null2 = 1; |
| 359 | command_found++; |
| 360 | } else if (!strcmp(argv[i], "dump3")) { |
| 361 | dump3 = 1; |
| 362 | command_found++; |
| 363 | } else if (!strcmp(argv[i], "gettime3")) { |
| 364 | gettime3 = 1; |
| 365 | command_found++; |
| 366 | } else if (!strcmp(argv[i], "u2t3")) { |
| 367 | u2t3 = 1; |
| 368 | u2t3string = argv[++i]; |
| 369 | command_found++; |
| 370 | } else if (!strcmp(argv[i], "getaddr3")) { |
| 371 | getaddr3 = 1; |
| 372 | getaddr3prog = atoi(argv[++i]); |
| 373 | getaddr3vers = atoi(argv[++i]); |
| 374 | getaddr3netid = argv[++i]; |
| 375 | getaddr3addr = argv[++i]; |
| 376 | getaddr3owner = argv[++i]; |
| 377 | command_found++; |
| 378 | } else if (!strcmp(argv[i], "set3")) { |
| 379 | set3 = 1; |
| 380 | set3prog = atoi(argv[++i]); |
| 381 | set3vers = atoi(argv[++i]); |
| 382 | set3netid = argv[++i]; |
| 383 | set3addr = argv[++i]; |
| 384 | set3owner = argv[++i]; |
| 385 | command_found++; |
| 386 | } else if (!strcmp(argv[i], "null3")) { |
| 387 | null3 = 1; |
| 388 | command_found++; |
| 389 | } else { |
| 390 | server = argv[i]; |
| 391 | } |
| 392 | } |
| 393 | if (command_found == 0 || server == NULL) { |
| 394 | fprintf(stderr, "Usage: portmap-client <command*> <server>\n"); |
| 395 | exit(10); |
| 396 | } |
| 397 | |
| 398 | if (rpc_connect_async(rpc, server, 111, pmap_connect_cb, &client) != 0) { |
| 399 | printf("Failed to start connection\n"); |
| 400 | exit(10); |
| 401 | } |
| 402 | wait_until_finished(rpc, &client); |
| 403 | |
| 404 | if (null2) { |
| 405 | if (rpc_pmap2_null_async(rpc, pmap2_null_cb, &client) != 0) { |
| 406 | printf("Failed to send NULL2 request\n"); |
| 407 | exit(10); |
| 408 | } |
| 409 | wait_until_finished(rpc, &client); |
| 410 | } |
| 411 | if (dump2) { |
| 412 | if (rpc_pmap2_dump_async(rpc, pmap2_dump_cb, &client) != 0) { |
| 413 | printf("Failed to send DUMP2 request\n"); |
| 414 | exit(10); |
| 415 | } |
| 416 | wait_until_finished(rpc, &client); |
| 417 | } |
| 418 | if (null3) { |
| 419 | if (rpc_pmap3_null_async(rpc, pmap3_null_cb, &client) != 0) { |
| 420 | printf("Failed to send NULL3 request\n"); |
| 421 | exit(10); |
| 422 | } |
| 423 | wait_until_finished(rpc, &client); |
| 424 | } |
| 425 | if (dump3) { |
| 426 | if (rpc_pmap3_dump_async(rpc, pmap3_dump_cb, &client) != 0) { |
| 427 | printf("Failed to send DUMP3 request\n"); |
| 428 | exit(10); |
| 429 | } |
| 430 | wait_until_finished(rpc, &client); |
| 431 | } |
| 432 | if (gettime3) { |
| 433 | if (rpc_pmap3_gettime_async(rpc, pmap3_gettime_cb, &client) != 0) { |
| 434 | printf("Failed to send GETTIME3 request\n"); |
| 435 | exit(10); |
| 436 | } |
| 437 | wait_until_finished(rpc, &client); |
| 438 | } |
| 439 | if (u2t3) { |
| 440 | if (rpc_pmap3_uaddr2taddr_async(rpc, u2t3string, pmap3_uaddr2taddr_cb, &client) != 0) { |
| 441 | printf("Failed to send UADDR2TADDR3 request\n"); |
| 442 | exit(10); |
| 443 | } |
| 444 | wait_until_finished(rpc, &client); |
| 445 | } |
| 446 | if (getaddr3) { |
| 447 | struct pmap3_mapping map; |
| 448 | |
| 449 | map.prog = getaddr3prog; |
| 450 | map.vers = getaddr3vers; |
| 451 | map.netid = getaddr3netid; |
| 452 | map.addr = getaddr3addr; |
| 453 | map.owner = getaddr3owner; |
| 454 | if (rpc_pmap3_getaddr_async(rpc, &map, pmap3_getaddr_cb, &client) != 0) { |
| 455 | printf("Failed to send GETADDR3 request\n"); |
| 456 | exit(10); |
| 457 | } |
| 458 | wait_until_finished(rpc, &client); |
| 459 | } |
| 460 | if (set3) { |
| 461 | struct pmap3_mapping map; |
| 462 | |
| 463 | map.prog = set3prog; |
| 464 | map.vers = set3vers; |
| 465 | map.netid = set3netid; |
| 466 | map.addr = set3addr; |
| 467 | map.owner = set3owner; |
| 468 | if (rpc_pmap3_set_async(rpc, &map, pmap3_set_cb, &client) != 0) { |
| 469 | printf("Failed to send SET3 request\n"); |
| 470 | exit(10); |
| 471 | } |
| 472 | wait_until_finished(rpc, &client); |
| 473 | } |
| 474 | if (unset3) { |
| 475 | struct pmap3_mapping map; |
| 476 | |
| 477 | map.prog = unset3prog; |
| 478 | map.vers = unset3vers; |
| 479 | map.netid = unset3netid; |
| 480 | map.addr = unset3addr; |
| 481 | map.owner = unset3owner; |
| 482 | if (rpc_pmap3_unset_async(rpc, &map, pmap3_unset_cb, &client) != 0) { |
| 483 | printf("Failed to send UNSET3 request\n"); |
| 484 | exit(10); |
| 485 | } |
| 486 | wait_until_finished(rpc, &client); |
| 487 | } |
| 488 | |
| 489 | |
| 490 | rpc_destroy_context(rpc); |
| 491 | rpc=NULL; |
| 492 | return 0; |
| 493 | } |