summaryrefslogtreecommitdiffstats
path: root/src/portmap.c
diff options
context:
space:
mode:
authorNalin Dahyabhai <nalin.dahyabhai@pobox.com>2008-03-27 14:36:34 -0400
committerNalin Dahyabhai <nalin.dahyabhai@pobox.com>2008-03-27 14:36:34 -0400
commitbdd74a017f4f1456b63e3b621064adde8e739ac8 (patch)
tree7071af1dc370360398b78fe6c5a1ae2767d39a60 /src/portmap.c
parentf6c1481d5db27b637f0b95131e3acefd08d2495e (diff)
downloadslapi-nis-bdd74a017f4f1456b63e3b621064adde8e739ac8.tar.gz
slapi-nis-bdd74a017f4f1456b63e3b621064adde8e739ac8.tar.xz
slapi-nis-bdd74a017f4f1456b63e3b621064adde8e739ac8.zip
try to clean this up a bit
Diffstat (limited to 'src/portmap.c')
-rw-r--r--src/portmap.c35
1 files changed, 28 insertions, 7 deletions
diff --git a/src/portmap.c b/src/portmap.c
index 07e32ef..ebb85de 100644
--- a/src/portmap.c
+++ b/src/portmap.c
@@ -18,7 +18,8 @@
#ifdef PORTMAP_MAIN
#include <stdarg.h>
#include <unistd.h>
-int slapi_log_error(int i, char *f, char *fmt, ...)
+int
+slapi_log_error(int i, char *f, char *fmt, ...)
{
va_list va;
va_start(va, fmt);
@@ -56,7 +57,7 @@ portmap_register_work(int resv_sock,
XDR portmap_xdrs, auth_xdrs;
struct rpc_msg msg;
struct pmap map;
- bool_t ret;
+ bool_t ret = FALSE;
struct sockaddr addr;
struct sockaddr_in addr4;
union {
@@ -68,12 +69,9 @@ portmap_register_work(int resv_sock,
static u_long xid;
memset(&addr, 0, sizeof(addr));
- memset(&addr4, 0, sizeof(addr4));
addr.sa_family = AF_UNSPEC;
- addr4.sin_family = AF_INET;
- addr4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
- addr4.sin_port = htons(PMAPPORT);
+ /* Build the RPC header. */
memset(&msg, 0, sizeof(msg));
msg.rm_xid = xid = (time(NULL) ^ port ^ protocol ^ proc);
msg.rm_direction = CALL;
@@ -82,17 +80,20 @@ portmap_register_work(int resv_sock,
msg.rm_call.cb_vers = PMAPVERS;
msg.rm_call.cb_proc = proc;
+ /* Build an authenticator. */
xdrmem_create(&auth_xdrs, auth_buf, sizeof(auth_buf), XDR_ENCODE);
auth = authnone_create();
auth_marshall(auth, &auth_xdrs);
msg.rm_call.cb_cred = auth->ah_cred;
msg.rm_call.cb_verf = auth->ah_verf;
+ /* Populate the arguments for this request. */
map.pm_prog = program;
map.pm_vers = version;
map.pm_prot = protocol;
map.pm_port = port;
+ /* Encode the header and the arguments, then clean up temporaries. */
xdrmem_create(&portmap_xdrs, portmap_buf, sizeof(portmap_buf),
XDR_ENCODE);
xdr_callmsg(&portmap_xdrs, &msg);
@@ -103,25 +104,39 @@ portmap_register_work(int resv_sock,
xdr_destroy(&portmap_xdrs);
memset(&portmap_xdrs, 0, sizeof(portmap_xdrs));
- client_addrlen = 0;
+ /* Connect to the local portmapper. */
+ memset(&addr4, 0, sizeof(addr4));
+ addr4.sin_family = AF_INET;
+ addr4.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
+ addr4.sin_port = htons(PMAPPORT);
connect(resv_sock, (struct sockaddr*) &addr4, sizeof(addr4));
+
+ /* Transmit our request. Retry a few times if it doesn't go through. */
for (i = 1; i < 32; i *= 2) {
+ /* Try to send our request. If there's any problem,
+ * immediately retry. */
if (send(resv_sock, &portmap_buf, portmap_length,
0) != portmap_length) {
slapi_log_error(SLAPI_LOG_PLUGIN, "XXX",
"error sending request to portmap\n");
continue;
}
+
+ /* Wait for a response. */
pollfd.fd = resv_sock;
pollfd.events = POLLIN | POLLERR;
if ((poll(&pollfd, 1, i * 1000) > 0) &&
(pollfd.revents & POLLIN)) {
+ /* Read the response. */
client_addrlen = sizeof(client_addr);
reply_length = recvfrom(resv_sock,
reply_buf, sizeof(reply_buf), 0,
(struct sockaddr *)&client_addr,
&client_addrlen);
+ /* Decode the response. */
if (reply_length > 0) {
+ /* Decode an RPC header and the returned
+ * boolean from the buffer. */
memset(&msg, 0, sizeof(msg));
xdrmem_create(&portmap_xdrs,
reply_buf, reply_length,
@@ -141,6 +156,7 @@ portmap_register_work(int resv_sock,
}
}
+ /* Disconnect from the portmapper, but keep our socket around. */
connect(resv_sock, &addr, sizeof(addr));
if (i == 32) {
slapi_log_error(SLAPI_LOG_PLUGIN, "XXX",
@@ -148,6 +164,8 @@ portmap_register_work(int resv_sock,
return FALSE;
}
+ /* Check that the portmapper didn't just reject the request out of
+ * hand. */
if (msg.rm_reply.rp_stat != MSG_ACCEPTED) {
slapi_log_error(SLAPI_LOG_PLUGIN, "XXX",
"portmap request not accepted\n");
@@ -196,6 +214,7 @@ portmap_register_work(int resv_sock,
xdr_destroy(&portmap_xdrs);
}
+ /* Validate the portmapper's credentials. */
auth = authunix_create_default();
if (auth_validate(auth, &msg.rm_reply.rp_acpt.ar_verf)) {
slapi_log_error(SLAPI_LOG_PLUGIN, "XXX",
@@ -206,6 +225,7 @@ portmap_register_work(int resv_sock,
}
auth_destroy(auth);
+ /* Check if we the portmapper gave us a reply argument. */
if (msg.rm_reply.rp_acpt.ar_stat != SUCCESS) {
slapi_log_error(SLAPI_LOG_PLUGIN, "XXX",
"portmap request not processed\n");
@@ -213,6 +233,7 @@ portmap_register_work(int resv_sock,
return FALSE;
}
+ /* Check what happened. */
if (ret) {
slapi_log_error(SLAPI_LOG_PLUGIN, "XXX",
"portmap request succeeded\n");