| 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 <stdio.h> |
| 38 | #include <stdlib.h> |
| 39 | #include <string.h> |
| 40 | #include "libnfs-zdr.h" |
| 41 | #include "libnfs.h" |
| 42 | #include "libnfs-raw.h" |
| 43 | #include "libnfs-raw-mount.h" |
| 44 | #include "libnfs-raw-nfs.h" |
| 45 | #include "libnfs-raw-portmap.h" |
| 46 | #include "libnfs-raw-rquota.h" |
| 47 | |
| 48 | struct client { |
| 49 | int is_finished; |
| 50 | }; |
| 51 | |
| 52 | void pmap2_dump_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
| 53 | { |
| 54 | struct client *client = private_data; |
| 55 | struct pmap2_dump_result *dr = data; |
| 56 | struct pmap2_mapping_list *list = dr->list; |
| 57 | |
| 58 | if (status == RPC_STATUS_ERROR) { |
| 59 | printf("PORTMAP2/DUMP call failed with \"%s\"\n", (char *)data); |
| 60 | exit(10); |
| 61 | } |
| 62 | if (status != RPC_STATUS_SUCCESS) { |
| 63 | printf("PORTMAP2/DUMP call failed, status:%d\n", status); |
| 64 | exit(10); |
| 65 | } |
| 66 | |
| 67 | printf("PORTMAP2/DUMP:\n"); |
| 68 | while (list) { |
| 69 | printf(" Prog:%d Vers:%d Protocol:%d Port:%d\n", |
| 70 | list->map.prog, |
| 71 | list->map.vers, |
| 72 | list->map.prot, |
| 73 | list->map.port); |
| 74 | list = list->next; |
| 75 | } |
| 76 | client->is_finished = 1; |
| 77 | } |
| 78 | |
| 79 | void pmap3_dump_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
| 80 | { |
| 81 | struct client *client = private_data; |
| 82 | struct pmap3_dump_result *dr = data; |
| 83 | struct pmap3_mapping_list *list = dr->list; |
| 84 | |
| 85 | if (status == RPC_STATUS_ERROR) { |
| 86 | printf("PORTMAP3/DUMP call failed with \"%s\"\n", (char *)data); |
| 87 | exit(10); |
| 88 | } |
| 89 | if (status != RPC_STATUS_SUCCESS) { |
| 90 | printf("PORTMAP3/DUMP call failed, status:%d\n", status); |
| 91 | exit(10); |
| 92 | } |
| 93 | |
| 94 | printf("PORTMAP3/DUMP:\n"); |
| 95 | while (list) { |
| 96 | printf(" Prog:%d Vers:%d Netid:%s Addr:%s Owner:%s\n", |
| 97 | list->map.prog, |
| 98 | list->map.vers, |
| 99 | list->map.netid, |
| 100 | list->map.addr, |
| 101 | list->map.owner); |
| 102 | list = list->next; |
| 103 | } |
| 104 | client->is_finished = 1; |
| 105 | } |
| 106 | |
| 107 | void pmap2_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
| 108 | { |
| 109 | struct client *client = private_data; |
| 110 | |
| 111 | if (status == RPC_STATUS_ERROR) { |
| 112 | printf("PORTMAP2/NULL call failed with \"%s\"\n", (char *)data); |
| 113 | exit(10); |
| 114 | } |
| 115 | if (status != RPC_STATUS_SUCCESS) { |
| 116 | printf("PORTMAP2/NULL call failed, status:%d\n", status); |
| 117 | exit(10); |
| 118 | } |
| 119 | |
| 120 | printf("PORTMAP2/NULL responded and server is alive\n"); |
| 121 | client->is_finished = 1; |
| 122 | } |
| 123 | |
| 124 | void pmap3_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
| 125 | { |
| 126 | struct client *client = private_data; |
| 127 | |
| 128 | if (status == RPC_STATUS_ERROR) { |
| 129 | printf("PORTMAP3/NULL call failed with \"%s\"\n", (char *)data); |
| 130 | exit(10); |
| 131 | } |
| 132 | if (status != RPC_STATUS_SUCCESS) { |
| 133 | printf("PORTMAP3/NULL call failed, status:%d\n", status); |
| 134 | exit(10); |
| 135 | } |
| 136 | |
| 137 | printf("PORTMAP3/NULL responded and server is alive\n"); |
| 138 | client->is_finished = 1; |
| 139 | } |
| 140 | |
| 141 | void pmap_null_cb(struct rpc_context *rpc, int status, void *data, void *private_data) |
| 142 | { |
| 143 | struct client *client = private_data; |
| 144 | |
| 145 | if (status == RPC_STATUS_ERROR) { |
| 146 | printf("PORTMAP/NULL call failed with \"%s\"\n", (char *)data); |
| 147 | exit(10); |
| 148 | } |
| 149 | if (status != RPC_STATUS_SUCCESS) { |
| 150 | printf("PORTMAP/NULL call failed, status:%d\n", status); |
| 151 | exit(10); |
| 152 | } |
| 153 | |
| 154 | client->is_finished = 1; |
| 155 | } |
| 156 | |
| 157 | void pmap_connect_cb(struct rpc_context *rpc, int status, void *data _U_, void *private_data) |
| 158 | { |
| 159 | struct client *client = private_data; |
| 160 | |
| 161 | if (status != RPC_STATUS_SUCCESS) { |
| 162 | printf("connection to portmapper failed\n"); |
| 163 | exit(10); |
| 164 | } |
| 165 | |
| 166 | if (rpc_pmap2_null_async(rpc, pmap_null_cb, client) != 0) { |
| 167 | printf("Failed to send null request\n"); |
| 168 | exit(10); |
| 169 | } |
| 170 | } |
| 171 | |
| 172 | |
| 173 | static void wait_until_finished(struct rpc_context *rpc, struct client *client) |
| 174 | { |
| 175 | struct pollfd pfd; |
| 176 | |
| 177 | client->is_finished = 0; |
| 178 | for (;;) { |
| 179 | pfd.fd = rpc_get_fd(rpc); |
| 180 | pfd.events = rpc_which_events(rpc); |
| 181 | |
| 182 | if (poll(&pfd, 1, -1) < 0) { |
| 183 | printf("Poll failed"); |
| 184 | exit(10); |
| 185 | } |
| 186 | if (rpc_service(rpc, pfd.revents) < 0) { |
| 187 | printf("rpc_service failed\n"); |
| 188 | break; |
| 189 | } |
| 190 | if (client->is_finished) { |
| 191 | break; |
| 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
| 196 | int main(int argc _U_, char *argv[] _U_) |
| 197 | { |
| 198 | struct rpc_context *rpc; |
| 199 | struct client client; |
| 200 | char *server = NULL; |
| 201 | int i; |
| 202 | int null2 = 0; |
| 203 | int dump2 = 0; |
| 204 | int null3 = 0; |
| 205 | int dump3 = 0; |
| 206 | int command_found = 0; |
| 207 | |
| 208 | rpc = rpc_init_context(); |
| 209 | if (rpc == NULL) { |
| 210 | printf("failed to init context\n"); |
| 211 | exit(10); |
| 212 | } |
| 213 | |
| 214 | for (i = 1; i < argc; i++) { |
| 215 | if (!strcmp(argv[i], "dump2")) { |
| 216 | dump2 = 1; |
| 217 | command_found++; |
| 218 | } else if (!strcmp(argv[i], "null2")) { |
| 219 | null2 = 1; |
| 220 | command_found++; |
| 221 | } else if (!strcmp(argv[i], "dump3")) { |
| 222 | dump3 = 1; |
| 223 | command_found++; |
| 224 | } else if (!strcmp(argv[i], "null3")) { |
| 225 | null3 = 1; |
| 226 | command_found++; |
| 227 | } else { |
| 228 | server = argv[i]; |
| 229 | } |
| 230 | } |
| 231 | if (command_found == 0 || server == NULL) { |
| 232 | fprintf(stderr, "Usage: portmap-client <command*> <server>\n"); |
| 233 | exit(10); |
| 234 | } |
| 235 | |
| 236 | if (rpc_connect_async(rpc, server, 111, pmap_connect_cb, &client) != 0) { |
| 237 | printf("Failed to start connection\n"); |
| 238 | exit(10); |
| 239 | } |
| 240 | wait_until_finished(rpc, &client); |
| 241 | |
| 242 | if (null2) { |
| 243 | if (rpc_pmap2_null_async(rpc, pmap2_null_cb, &client) != 0) { |
| 244 | printf("Failed to send NULL2 request\n"); |
| 245 | exit(10); |
| 246 | } |
| 247 | wait_until_finished(rpc, &client); |
| 248 | } |
| 249 | if (dump2) { |
| 250 | if (rpc_pmap2_dump_async(rpc, pmap2_dump_cb, &client) != 0) { |
| 251 | printf("Failed to send DUMP2 request\n"); |
| 252 | exit(10); |
| 253 | } |
| 254 | wait_until_finished(rpc, &client); |
| 255 | } |
| 256 | if (null3) { |
| 257 | if (rpc_pmap3_null_async(rpc, pmap3_null_cb, &client) != 0) { |
| 258 | printf("Failed to send NULL3 request\n"); |
| 259 | exit(10); |
| 260 | } |
| 261 | wait_until_finished(rpc, &client); |
| 262 | } |
| 263 | if (dump3) { |
| 264 | if (rpc_pmap3_dump_async(rpc, pmap3_dump_cb, &client) != 0) { |
| 265 | printf("Failed to send DUMP3 request\n"); |
| 266 | exit(10); |
| 267 | } |
| 268 | wait_until_finished(rpc, &client); |
| 269 | } |
| 270 | |
| 271 | |
| 272 | rpc_destroy_context(rpc); |
| 273 | rpc=NULL; |
| 274 | return 0; |
| 275 | } |