From bdd74a017f4f1456b63e3b621064adde8e739ac8 Mon Sep 17 00:00:00 2001 From: Nalin Dahyabhai Date: Thu, 27 Mar 2008 14:36:34 -0400 Subject: try to clean this up a bit --- src/portmap.c | 35 ++++++++++++++++++++++++++++------- 1 file changed, 28 insertions(+), 7 deletions(-) (limited to 'src/portmap.c') 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 #include -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"); -- cgit