Skip to content

Commit

Permalink
* ensure memory is released if operations fail (in authunix_create(),
Browse files Browse the repository at this point in the history
  xdr_callmsg(), xprt_register(), svc_tcp(), svc_udp(), etc)
* don't attempt to close a socket filedescriptor if it's -1 (some from
  freebsd, some i found)
* make the initial xid a little more random (from freebsd)
* fix some spelos and tyops in comments (some from freebsd)
* use warn() instead of warnx() for many errors; the user probably
  wants to know what the error code was.
* knf & whitespace nitpicks
  • Loading branch information
lukem committed Jan 20, 1999
1 parent 19fdc4a commit 26133e5
Show file tree
Hide file tree
Showing 21 changed files with 176 additions and 125 deletions.
8 changes: 4 additions & 4 deletions lib/libc/rpc/auth_none.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: auth_none.c,v 1.10 1998/07/26 11:39:26 mycroft Exp $ */
/* $NetBSD: auth_none.c,v 1.11 1999/01/20 11:37:34 lukem Exp $ */

/*
* Sun RPC is a product of Sun Microsystems, Inc. and is provided for
Expand Down Expand Up @@ -35,7 +35,7 @@
static char *sccsid = "@(#)auth_none.c 1.19 87/08/11 Copyr 1984 Sun Micro";
static char *sccsid = "@(#)auth_none.c 2.1 88/07/29 4.0 RPCSRC";
#else
__RCSID("$NetBSD: auth_none.c,v 1.10 1998/07/26 11:39:26 mycroft Exp $");
__RCSID("$NetBSD: auth_none.c,v 1.11 1999/01/20 11:37:34 lukem Exp $");
#endif
#endif

Expand Down Expand Up @@ -102,8 +102,8 @@ authnone_create()
ap->no_client.ah_cred = ap->no_client.ah_verf = _null_auth;
ap->no_client.ah_ops = &ops;
xdrs = &xdr_stream;
xdrmem_create(xdrs, ap->marshalled_client, (u_int)MAX_MARSHEL_SIZE,
XDR_ENCODE);
xdrmem_create(xdrs, ap->marshalled_client,
(u_int)MAX_MARSHEL_SIZE, XDR_ENCODE);
(void)xdr_opaque_auth(xdrs, &ap->no_client.ah_cred);
(void)xdr_opaque_auth(xdrs, &ap->no_client.ah_verf);
ap->mcnt = XDR_GETPOS(xdrs);
Expand Down
26 changes: 20 additions & 6 deletions lib/libc/rpc/auth_unix.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: auth_unix.c,v 1.13 1998/11/15 17:24:07 christos Exp $ */
/* $NetBSD: auth_unix.c,v 1.14 1999/01/20 11:37:34 lukem Exp $ */

/*
* Sun RPC is a product of Sun Microsystems, Inc. and is provided for
Expand Down Expand Up @@ -35,7 +35,7 @@
static char *sccsid = "@(#)auth_unix.c 1.19 87/08/11 Copyr 1984 Sun Micro";
static char *sccsid = "@(#)auth_unix.c 2.2 88/08/01 4.0 RPCSRC";
#else
__RCSID("$NetBSD: auth_unix.c,v 1.13 1998/11/15 17:24:07 christos Exp $");
__RCSID("$NetBSD: auth_unix.c,v 1.14 1999/01/20 11:37:34 lukem Exp $");
#endif
#endif

Expand Down Expand Up @@ -125,24 +125,26 @@ authunix_create(machname, uid, gid, len, aup_gids)
/*
* Allocate and set up auth handle
*/
au = NULL;
auth = (AUTH *)mem_alloc(sizeof(*auth));
#ifndef KERNEL
if (auth == NULL) {
warnx("authunix_create: out of memory");
return (NULL);
goto cleanup_authunix_create;
}
#endif
au = (struct audata *)mem_alloc(sizeof(*au));
#ifndef KERNEL
if (au == NULL) {
warnx("authunix_create: out of memory");
return (NULL);
goto cleanup_authunix_create;
}
#endif
auth->ah_ops = &auth_unix_ops;
auth->ah_private = au;
auth->ah_verf = au->au_shcred = _null_auth;
au->au_shfaults = 0;
au->au_origcred.oa_base = NULL;

/*
* fill in param struct from the given params
Expand All @@ -168,7 +170,7 @@ authunix_create(machname, uid, gid, len, aup_gids)
#else
if ((au->au_origcred.oa_base = mem_alloc((u_int) len)) == NULL) {
warnx("authunix_create: out of memory");
return (NULL);
goto cleanup_authunix_create;
}
#endif
memmove(au->au_origcred.oa_base, mymem, (size_t)len);
Expand All @@ -179,6 +181,17 @@ authunix_create(machname, uid, gid, len, aup_gids)
auth->ah_cred = au->au_origcred;
marshal_new_auth(auth);
return (auth);
#ifndef KERNEL
cleanup_authunix_create:
if (auth)
mem_free(auth, sizeof(*auth));
if (au) {
if (au->au_origcred.oa_base)
mem_free(au->au_origcred.oa_base, (u_int)len);
mem_free(au, sizeof(*au));
}
return (NULL);
#endif
}

/*
Expand Down Expand Up @@ -238,7 +251,8 @@ authunix_validate(auth, verf)

if (verf->oa_flavor == AUTH_SHORT) {
au = AUTH_PRIVATE(auth);
xdrmem_create(&xdrs, verf->oa_base, verf->oa_length, XDR_DECODE);
xdrmem_create(&xdrs, verf->oa_base, verf->oa_length,
XDR_DECODE);

if (au->au_shcred.oa_base != NULL) {
mem_free(au->au_shcred.oa_base,
Expand Down
5 changes: 2 additions & 3 deletions lib/libc/rpc/authunix_prot.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: authunix_prot.c,v 1.7 1998/02/13 05:52:14 lukem Exp $ */
/* $NetBSD: authunix_prot.c,v 1.8 1999/01/20 11:37:34 lukem Exp $ */

/*
* Sun RPC is a product of Sun Microsystems, Inc. and is provided for
Expand Down Expand Up @@ -35,7 +35,7 @@
static char *sccsid = "@(#)authunix_prot.c 1.15 87/08/11 Copyr 1984 Sun Micro";
static char *sccsid = "@(#)authunix_prot.c 2.1 88/07/29 4.0 RPCSRC";
#else
__RCSID("$NetBSD: authunix_prot.c,v 1.7 1998/02/13 05:52:14 lukem Exp $");
__RCSID("$NetBSD: authunix_prot.c,v 1.8 1999/01/20 11:37:34 lukem Exp $");
#endif
#endif

Expand Down Expand Up @@ -76,4 +76,3 @@ xdr_authunix_parms(xdrs, p)
}
return (FALSE);
}

8 changes: 4 additions & 4 deletions lib/libc/rpc/bindresvport.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: bindresvport.c,v 1.13 1998/11/15 17:32:41 christos Exp $ */
/* $NetBSD: bindresvport.c,v 1.14 1999/01/20 11:37:35 lukem Exp $ */

/*
* Sun RPC is a product of Sun Microsystems, Inc. and is provided for
Expand Down Expand Up @@ -35,7 +35,7 @@
static char *sccsid = "@(#)bindresvport.c 1.8 88/02/08 SMI";
static char *sccsid = "@(#)bindresvport.c 2.2 88/07/29 4.0 RPCSRC";
#else
__RCSID("$NetBSD: bindresvport.c,v 1.13 1998/11/15 17:32:41 christos Exp $");
__RCSID("$NetBSD: bindresvport.c,v 1.14 1999/01/20 11:37:35 lukem Exp $");
#endif
#endif

Expand Down Expand Up @@ -107,8 +107,8 @@ bindresvport(sd, sin)
}

if (sin != &myaddr) { /* What did the kernel assign? */
if (getsockname(sd, (struct sockaddr *)(void *)sin, &sinlen)
< 0)
if (getsockname(sd, (struct sockaddr *)(void *)sin,
&sinlen) < 0)
errno = saved_errno;
return (res);
}
Expand Down
20 changes: 8 additions & 12 deletions lib/libc/rpc/clnt_perror.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: clnt_perror.c,v 1.15 1998/11/15 17:32:42 christos Exp $ */
/* $NetBSD: clnt_perror.c,v 1.16 1999/01/20 11:37:35 lukem Exp $ */

/*
* Sun RPC is a product of Sun Microsystems, Inc. and is provided for
Expand Down Expand Up @@ -35,7 +35,7 @@
static char *sccsid = "@(#)clnt_perror.c 1.15 87/10/07 Copyr 1984 Sun Micro";
static char *sccsid = "@(#)clnt_perror.c 2.1 88/07/29 4.0 RPCSRC";
#else
__RCSID("$NetBSD: clnt_perror.c,v 1.15 1998/11/15 17:32:42 christos Exp $");
__RCSID("$NetBSD: clnt_perror.c,v 1.16 1999/01/20 11:37:35 lukem Exp $");
#endif
#endif

Expand Down Expand Up @@ -75,9 +75,9 @@ static char *
_buf()
{

if (buf == 0)
buf = (char *)malloc(256);
buflen = 256;
if (buf == 0)
buf = (char *)malloc(buflen);
return (buf);
}

Expand Down Expand Up @@ -126,15 +126,13 @@ clnt_sperror(rpch, s)

case RPC_CANTSEND:
case RPC_CANTRECV:
i = snprintf(str, len, "; errno = %s",
strerror(e.re_errno));
i = snprintf(str, len, "; errno = %s", strerror(e.re_errno));
str += i;
len -= i;
break;

case RPC_VERSMISMATCH:
i = snprintf(str, len,
"; low version = %u, high version = %u",
i = snprintf(str, len, "; low version = %u, high version = %u",
e.re_vers.low, e.re_vers.high);
str += i;
len -= i;
Expand All @@ -157,16 +155,14 @@ clnt_sperror(rpch, s)
break;

case RPC_PROGVERSMISMATCH:
i = snprintf(str, len,
"; low version = %u, high version = %u",
i = snprintf(str, len, "; low version = %u, high version = %u",
e.re_vers.low, e.re_vers.high);
str += i;
len -= i;
break;

default: /* unknown */
i = snprintf(str, len,
"; s1 = %u, s2 = %u",
i = snprintf(str, len, "; s1 = %u, s2 = %u",
e.re_lb.s1, e.re_lb.s2);
str += i;
len -= i;
Expand Down
7 changes: 3 additions & 4 deletions lib/libc/rpc/clnt_raw.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: clnt_raw.c,v 1.13 1998/11/15 17:27:35 christos Exp $ */
/* $NetBSD: clnt_raw.c,v 1.14 1999/01/20 11:37:35 lukem Exp $ */

/*
* Sun RPC is a product of Sun Microsystems, Inc. and is provided for
Expand Down Expand Up @@ -35,7 +35,7 @@
static char *sccsid = "@(#)clnt_raw.c 1.22 87/08/11 Copyr 1984 Sun Micro";
static char *sccsid = "@(#)clnt_raw.c 2.2 88/08/01 4.0 RPCSRC";
#else
__RCSID("$NetBSD: clnt_raw.c,v 1.13 1998/11/15 17:27:35 christos Exp $");
__RCSID("$NetBSD: clnt_raw.c,v 1.14 1999/01/20 11:37:35 lukem Exp $");
#endif
#endif

Expand Down Expand Up @@ -254,8 +254,7 @@ clntraw_freeres(cl, xdr_res, res_ptr)
XDR *xdrs = &clp->xdr_stream;
bool_t rval;

if (clp == 0)
{
if (clp == 0) {
rval = (bool_t) RPC_FAILED;
return (rval);
}
Expand Down
11 changes: 6 additions & 5 deletions lib/libc/rpc/clnt_simple.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: clnt_simple.c,v 1.14 1998/11/15 17:25:39 christos Exp $ */
/* $NetBSD: clnt_simple.c,v 1.15 1999/01/20 11:37:35 lukem Exp $ */

/*
* Sun RPC is a product of Sun Microsystems, Inc. and is provided for
Expand Down Expand Up @@ -35,7 +35,7 @@
static char *sccsid = "@(#)clnt_simple.c 1.35 87/08/11 Copyr 1984 Sun Micro";
static char *sccsid = "@(#)clnt_simple.c 2.2 88/08/01 4.0 RPCSRC";
#else
__RCSID("$NetBSD: clnt_simple.c,v 1.14 1998/11/15 17:25:39 christos Exp $");
__RCSID("$NetBSD: clnt_simple.c,v 1.15 1999/01/20 11:37:35 lukem Exp $");
#endif
#endif

Expand Down Expand Up @@ -94,12 +94,13 @@ callrpc(host, prognum, versnum, procnum, inproc, in, outproc, out)
crp->oldhost[0] = 0;
crp->socket = RPC_ANYSOCK;
}
if (crp->valid && crp->oldprognum == prognum && crp->oldversnum == versnum
&& strcmp(crp->oldhost, host) == 0) {
if (crp->valid && crp->oldprognum == prognum
&& crp->oldversnum == versnum && strcmp(crp->oldhost, host) == 0) {
/* reuse old client */
} else {
crp->valid = 0;
(void)close(crp->socket);
if (crp->socket != -1)
(void)close(crp->socket);
crp->socket = RPC_ANYSOCK;
if (crp->client) {
clnt_destroy(crp->client);
Expand Down
32 changes: 21 additions & 11 deletions lib/libc/rpc/clnt_tcp.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/* $NetBSD: clnt_tcp.c,v 1.15 1998/11/15 17:29:17 christos Exp $ */
/* $NetBSD: clnt_tcp.c,v 1.16 1999/01/20 11:37:36 lukem Exp $ */

/*
* Sun RPC is a product of Sun Microsystems, Inc. and is provided for
Expand Down Expand Up @@ -35,7 +35,7 @@
static char *sccsid = "@(#)clnt_tcp.c 1.37 87/10/05 Copyr 1984 Sun Micro";
static char *sccsid = "@(#)clnt_tcp.c 2.2 88/08/01 4.0 RPCSRC";
#else
__RCSID("$NetBSD: clnt_tcp.c,v 1.15 1998/11/15 17:29:17 christos Exp $");
__RCSID("$NetBSD: clnt_tcp.c,v 1.16 1999/01/20 11:37:36 lukem Exp $");
#endif
#endif

Expand Down Expand Up @@ -127,8 +127,8 @@ struct ct_data {
* consulted for the right port number.
* NB: *sockp is copied into a private area.
* NB: It is the clients responsibility to close *sockp.
* NB: The rpch->cl_auth is set null authentication. Caller may wish to set this
* something more useful.
* NB: The rpch->cl_auth is set null authentication. Caller may wish to set
* this something more useful.
*/
CLIENT *
clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
Expand All @@ -143,6 +143,10 @@ clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
struct ct_data *ct = NULL;
struct timeval now;
struct rpc_msg call_msg;
static u_int32_t disrupt;

if (disrupt == 0)
disrupt = (u_int32_t)(long)raddr;

h = (CLIENT *)mem_alloc(sizeof(*h));
if (h == NULL) {
Expand All @@ -164,7 +168,8 @@ clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
*/
if (raddr->sin_port == 0) {
u_short port;
if ((port = pmap_getport(raddr, prog, vers, IPPROTO_TCP)) == 0) {
if ((port = pmap_getport(raddr, prog, vers, IPPROTO_TCP))
== 0) {
mem_free(ct, sizeof(struct ct_data));
mem_free(h, sizeof(CLIENT));
return ((CLIENT *)NULL);
Expand All @@ -183,7 +188,8 @@ clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
sizeof(*raddr)) < 0)) {
rpc_createerr.cf_stat = RPC_SYSTEMERROR;
rpc_createerr.cf_error.re_errno = errno;
(void)close(*sockp);
if (*sockp != -1)
(void)close(*sockp);
goto fooy;
}
ct->ct_closeit = TRUE;
Expand All @@ -203,14 +209,15 @@ clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
* Initialize call message
*/
(void)gettimeofday(&now, (struct timezone *)0);
call_msg.rm_xid = (u_int32_t)(getpid() ^ now.tv_sec ^ now.tv_usec);
call_msg.rm_xid =
(u_int32_t)((++disrupt) ^ getpid() ^ now.tv_sec ^ now.tv_usec);
call_msg.rm_direction = CALL;
call_msg.rm_call.cb_rpcvers = RPC_MSG_VERSION;
call_msg.rm_call.cb_prog = (u_int32_t)prog;
call_msg.rm_call.cb_vers = (u_int32_t)vers;

/*
* pre-serialize the staic part of the call msg and stash it away
* pre-serialize the static part of the call msg and stash it away
*/
xdrmem_create(&(ct->ct_xdrs), ct->ct_u.ct_mcallc, MCALL_MSG_SIZE,
XDR_ENCODE);
Expand Down Expand Up @@ -240,7 +247,8 @@ clnttcp_create(raddr, prog, vers, sockp, sendsz, recvsz)
*/
if (ct)
mem_free(ct, sizeof(struct ct_data));
mem_free(h, sizeof(CLIENT));
if (h)
mem_free(h, sizeof(CLIENT));
return ((CLIENT *)NULL);
}

Expand Down Expand Up @@ -320,7 +328,8 @@ clnttcp_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout)
*/
_seterr_reply(&reply_msg, &(ct->ct_error));
if (ct->ct_error.re_status == RPC_SUCCESS) {
if (! AUTH_VALIDATE(h->cl_auth, &reply_msg.acpted_rply.ar_verf)) {
if (! AUTH_VALIDATE(h->cl_auth,
&reply_msg.acpted_rply.ar_verf)) {
ct->ct_error.re_status = RPC_AUTHERROR;
ct->ct_error.re_why = AUTH_INVALIDRESP;
} else if (! (*xdr_results)(xdrs, results_ptr)) {
Expand All @@ -330,7 +339,8 @@ clnttcp_call(h, proc, xdr_args, args_ptr, xdr_results, results_ptr, timeout)
/* free verifier ... */
if (reply_msg.acpted_rply.ar_verf.oa_base != NULL) {
xdrs->x_op = XDR_FREE;
(void)xdr_opaque_auth(xdrs, &(reply_msg.acpted_rply.ar_verf));
(void)xdr_opaque_auth(xdrs,
&(reply_msg.acpted_rply.ar_verf));
}
} /* end successful completion */
else {
Expand Down
Loading

0 comments on commit 26133e5

Please sign in to comment.