clnt_perror.c revision 1.29
11.29Smatt/*	$NetBSD: clnt_perror.c,v 1.29 2012/03/20 17:14:50 matt Exp $	*/
21.5Scgd
31.1Scgd/*
41.1Scgd * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
51.1Scgd * unrestricted use provided that this legend is included on all tape
61.1Scgd * media and as a part of the software program in whole or part.  Users
71.1Scgd * may copy or modify Sun RPC without charge, but are not authorized
81.1Scgd * to license or distribute it to anyone else except as part of a product or
91.1Scgd * program developed by the user.
101.1Scgd *
111.1Scgd * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
121.1Scgd * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
131.1Scgd * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
141.1Scgd *
151.1Scgd * Sun RPC is provided with no support and without any obligation on the
161.1Scgd * part of Sun Microsystems, Inc. to assist in its use, correction,
171.1Scgd * modification or enhancement.
181.1Scgd *
191.1Scgd * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
201.1Scgd * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
211.1Scgd * OR ANY PART THEREOF.
221.1Scgd *
231.1Scgd * In no event will Sun Microsystems, Inc. be liable for any lost revenue
241.1Scgd * or profits or other special, indirect and consequential damages, even if
251.1Scgd * Sun has been advised of the possibility of such damages.
261.1Scgd *
271.1Scgd * Sun Microsystems, Inc.
281.1Scgd * 2550 Garcia Avenue
291.1Scgd * Mountain View, California  94043
301.1Scgd */
311.1Scgd
321.10Schristos#include <sys/cdefs.h>
331.1Scgd#if defined(LIBC_SCCS) && !defined(lint)
341.10Schristos#if 0
351.10Schristosstatic char *sccsid = "@(#)clnt_perror.c 1.15 87/10/07 Copyr 1984 Sun Micro";
361.10Schristosstatic char *sccsid = "@(#)clnt_perror.c	2.1 88/07/29 4.0 RPCSRC";
371.10Schristos#else
381.29Smatt__RCSID("$NetBSD: clnt_perror.c,v 1.29 2012/03/20 17:14:50 matt Exp $");
391.10Schristos#endif
401.1Scgd#endif
411.1Scgd
421.1Scgd/*
431.1Scgd * clnt_perror.c
441.1Scgd *
451.1Scgd * Copyright (C) 1984, Sun Microsystems, Inc.
461.1Scgd *
471.1Scgd */
481.11Sjtc#include "namespace.h"
491.14Slukem
501.18Slukem#include <assert.h>
511.1Scgd#include <stdio.h>
521.3Scgd#include <stdlib.h>
531.1Scgd#include <string.h>
541.14Slukem
551.1Scgd#include <rpc/rpc.h>
561.1Scgd#include <rpc/types.h>
571.1Scgd#include <rpc/auth.h>
581.1Scgd#include <rpc/clnt.h>
591.11Sjtc
601.11Sjtc#ifdef __weak_alias
611.23Smycroft__weak_alias(clnt_pcreateerror,_clnt_pcreateerror)
621.23Smycroft__weak_alias(clnt_perrno,_clnt_perrno)
631.23Smycroft__weak_alias(clnt_perror,_clnt_perror)
641.23Smycroft__weak_alias(clnt_spcreateerror,_clnt_spcreateerror)
651.23Smycroft__weak_alias(clnt_sperrno,_clnt_sperrno)
661.23Smycroft__weak_alias(clnt_sperror,_clnt_sperror)
671.11Sjtc#endif
681.1Scgd
691.1Scgdstatic char *buf;
701.17Schristosstatic size_t buflen;
711.1Scgd
721.29Smattstatic char *_buf(void);
731.29Smattstatic char *auth_errmsg(enum auth_stat);
741.10Schristos
751.1Scgdstatic char *
761.29Smatt_buf(void)
771.1Scgd{
781.1Scgd
791.16Slukem	buflen = 256;
801.13Slukem	if (buf == 0)
811.28Schristos		buf = malloc(buflen);
821.1Scgd	return (buf);
831.1Scgd}
841.1Scgd
851.1Scgd/*
861.1Scgd * Print reply error info
871.1Scgd */
881.1Scgdchar *
891.29Smattclnt_sperror(CLIENT *rpch, const char *s)
901.1Scgd{
911.1Scgd	struct rpc_err e;
921.1Scgd	char *err;
931.18Slukem	char *str;
941.21Sexplorer	char *strstart;
951.22Sdrochner	size_t len, i;
961.1Scgd
971.18Slukem	_DIAGASSERT(rpch != NULL);
981.18Slukem	_DIAGASSERT(s != NULL);
991.18Slukem
1001.22Sdrochner	str = _buf(); /* side effect: sets "buflen" */
1011.13Slukem	if (str == 0)
1021.13Slukem		return (0);
1031.22Sdrochner	len = buflen;
1041.21Sexplorer	strstart = str;
1051.1Scgd	CLNT_GETERR(rpch, &e);
1061.1Scgd
1071.9Smrg	i = snprintf(str, len, "%s: ", s);
1081.9Smrg	str += i;
1091.9Smrg	len -= i;
1101.9Smrg
1111.9Smrg	(void)strncpy(str, clnt_sperrno(e.re_status), len - 1);
1121.9Smrg	i = strlen(str);
1131.9Smrg	str += i;
1141.9Smrg	len -= i;
1151.1Scgd
1161.1Scgd	switch (e.re_status) {
1171.1Scgd	case RPC_SUCCESS:
1181.1Scgd	case RPC_CANTENCODEARGS:
1191.1Scgd	case RPC_CANTDECODERES:
1201.1Scgd	case RPC_TIMEDOUT:
1211.1Scgd	case RPC_PROGUNAVAIL:
1221.1Scgd	case RPC_PROCUNAVAIL:
1231.1Scgd	case RPC_CANTDECODEARGS:
1241.1Scgd	case RPC_SYSTEMERROR:
1251.1Scgd	case RPC_UNKNOWNHOST:
1261.1Scgd	case RPC_UNKNOWNPROTO:
1271.1Scgd	case RPC_PMAPFAILURE:
1281.1Scgd	case RPC_PROGNOTREGISTERED:
1291.1Scgd	case RPC_FAILED:
1301.1Scgd		break;
1311.1Scgd
1321.1Scgd	case RPC_CANTSEND:
1331.1Scgd	case RPC_CANTRECV:
1341.16Slukem		i = snprintf(str, len, "; errno = %s", strerror(e.re_errno));
1351.9Smrg		str += i;
1361.9Smrg		len -= i;
1371.1Scgd		break;
1381.1Scgd
1391.1Scgd	case RPC_VERSMISMATCH:
1401.16Slukem		i = snprintf(str, len, "; low version = %u, high version = %u",
1411.1Scgd			e.re_vers.low, e.re_vers.high);
1421.9Smrg		str += i;
1431.9Smrg		len -= i;
1441.1Scgd		break;
1451.1Scgd
1461.1Scgd	case RPC_AUTHERROR:
1471.1Scgd		err = auth_errmsg(e.re_why);
1481.9Smrg		i = snprintf(str, len, "; why = ");
1491.9Smrg		str += i;
1501.9Smrg		len -= i;
1511.1Scgd		if (err != NULL) {
1521.9Smrg			i = snprintf(str, len, "%s",err);
1531.1Scgd		} else {
1541.9Smrg			i = snprintf(str, len,
1551.1Scgd				"(unknown authentication error - %d)",
1561.1Scgd				(int) e.re_why);
1571.1Scgd		}
1581.9Smrg		str += i;
1591.9Smrg		len -= i;
1601.1Scgd		break;
1611.1Scgd
1621.1Scgd	case RPC_PROGVERSMISMATCH:
1631.16Slukem		i = snprintf(str, len, "; low version = %u, high version = %u",
1641.1Scgd			e.re_vers.low, e.re_vers.high);
1651.9Smrg		str += i;
1661.9Smrg		len -= i;
1671.1Scgd		break;
1681.1Scgd
1691.1Scgd	default:	/* unknown */
1701.16Slukem		i = snprintf(str, len, "; s1 = %u, s2 = %u",
1711.1Scgd			e.re_lb.s1, e.re_lb.s2);
1721.9Smrg		str += i;
1731.9Smrg		len -= i;
1741.1Scgd		break;
1751.1Scgd	}
1761.1Scgd	return(strstart) ;
1771.1Scgd}
1781.1Scgd
1791.1Scgdvoid
1801.29Smattclnt_perror(CLIENT *rpch, const char *s)
1811.1Scgd{
1821.18Slukem
1831.18Slukem	_DIAGASSERT(rpch != NULL);
1841.18Slukem	_DIAGASSERT(s != NULL);
1851.18Slukem
1861.14Slukem	(void) fprintf(stderr, "%s\n", clnt_sperror(rpch,s));
1871.1Scgd}
1881.1Scgd
1891.6Sjtcstatic const char *const rpc_errlist[] = {
1901.27Syamt	[RPC_SUCCESS] =			"RPC: Success",
1911.27Syamt	[RPC_CANTENCODEARGS] =		"RPC: Can't encode arguments",
1921.27Syamt	[RPC_CANTDECODERES] =		"RPC: Can't decode result",
1931.27Syamt	[RPC_CANTSEND] =		"RPC: Unable to send",
1941.27Syamt	[RPC_CANTRECV] =		"RPC: Unable to receive",
1951.27Syamt	[RPC_TIMEDOUT] =		"RPC: Timed out",
1961.27Syamt	[RPC_VERSMISMATCH] =		"RPC: Incompatible versions of RPC",
1971.27Syamt	[RPC_AUTHERROR] = 		"RPC: Authentication error",
1981.27Syamt	[RPC_PROGUNAVAIL] =		"RPC: Program unavailable",
1991.27Syamt	[RPC_PROGVERSMISMATCH] =	"RPC: Program/version mismatch",
2001.27Syamt	[RPC_PROCUNAVAIL] =		"RPC: Procedure unavailable",
2011.27Syamt	[RPC_CANTDECODEARGS] =		"RPC: Server can't decode arguments",
2021.27Syamt	[RPC_SYSTEMERROR] =		"RPC: Remote system error",
2031.27Syamt	[RPC_UNKNOWNHOST] =		"RPC: Unknown host",
2041.27Syamt	[RPC_PMAPFAILURE] =		"RPC: Port mapper failure",
2051.27Syamt	[RPC_PROGNOTREGISTERED] =	"RPC: Program not registered",
2061.27Syamt	[RPC_FAILED] =			"RPC: Failed (unspecified error)",
2071.27Syamt	[RPC_UNKNOWNPROTO] =		"RPC: Unknown protocol",
2081.27Syamt	[RPC_UNKNOWNADDR] =		"RPC: Remote address unknown",
2091.27Syamt	[RPC_TLIERROR] =		"RPC: Misc error in the TLI library",
2101.27Syamt	[RPC_NOBROADCAST] =		"RPC: Broadcasting not supported",
2111.27Syamt	[RPC_N2AXLATEFAILURE] =		"RPC: Name -> addr translation failed",
2121.27Syamt	[RPC_INPROGRESS] =		"RPC: In progress",
2131.27Syamt	[RPC_STALERACHANDLE] =		"RPC: Stale handle",
2141.1Scgd};
2151.1Scgd
2161.1Scgd
2171.1Scgd/*
2181.1Scgd * This interface for use by clntrpc
2191.1Scgd */
2201.1Scgdchar *
2211.29Smattclnt_sperrno(enum clnt_stat stat)
2221.1Scgd{
2231.6Sjtc	unsigned int errnum = stat;
2241.27Syamt	const char *msg;
2251.6Sjtc
2261.27Syamt	msg = NULL;
2271.27Syamt	if (errnum < (sizeof(rpc_errlist)/sizeof(rpc_errlist[0]))) {
2281.27Syamt		msg = rpc_errlist[errnum];
2291.27Syamt	}
2301.27Syamt	if (msg == NULL) {
2311.27Syamt		msg = "RPC: (unknown error code)";
2321.27Syamt	}
2331.27Syamt	return __UNCONST(msg);
2341.1Scgd}
2351.1Scgd
2361.1Scgdvoid
2371.29Smattclnt_perrno(enum clnt_stat num)
2381.1Scgd{
2391.14Slukem	(void) fprintf(stderr, "%s\n", clnt_sperrno(num));
2401.1Scgd}
2411.1Scgd
2421.1Scgd
2431.1Scgdchar *
2441.29Smattclnt_spcreateerror(const char *s)
2451.1Scgd{
2461.18Slukem	char *str;
2471.22Sdrochner	size_t len, i;
2481.1Scgd
2491.18Slukem	_DIAGASSERT(s != NULL);
2501.18Slukem
2511.22Sdrochner	str = _buf(); /* side effect: sets "buflen" */
2521.13Slukem	if (str == 0)
2531.13Slukem		return(0);
2541.22Sdrochner	len = buflen;
2551.9Smrg	i = snprintf(str, len, "%s: ", s);
2561.9Smrg	len -= i;
2571.9Smrg	(void)strncat(str, clnt_sperrno(rpc_createerr.cf_stat), len - 1);
2581.1Scgd	switch (rpc_createerr.cf_stat) {
2591.1Scgd	case RPC_PMAPFAILURE:
2601.9Smrg		(void) strncat(str, " - ", len - 1);
2611.9Smrg		(void) strncat(str,
2621.9Smrg		    clnt_sperrno(rpc_createerr.cf_error.re_status), len - 4);
2631.1Scgd		break;
2641.1Scgd
2651.1Scgd	case RPC_SYSTEMERROR:
2661.9Smrg		(void)strncat(str, " - ", len - 1);
2671.9Smrg		(void)strncat(str, strerror(rpc_createerr.cf_error.re_errno),
2681.9Smrg		    len - 4);
2691.10Schristos		break;
2701.10Schristos
2711.10Schristos	case RPC_CANTSEND:
2721.10Schristos	case RPC_CANTDECODERES:
2731.10Schristos	case RPC_CANTENCODEARGS:
2741.10Schristos	case RPC_SUCCESS:
2751.10Schristos	case RPC_UNKNOWNPROTO:
2761.10Schristos	case RPC_PROGNOTREGISTERED:
2771.10Schristos	case RPC_FAILED:
2781.10Schristos	case RPC_UNKNOWNHOST:
2791.10Schristos	case RPC_CANTDECODEARGS:
2801.10Schristos	case RPC_PROCUNAVAIL:
2811.10Schristos	case RPC_PROGVERSMISMATCH:
2821.10Schristos	case RPC_PROGUNAVAIL:
2831.10Schristos	case RPC_AUTHERROR:
2841.10Schristos	case RPC_VERSMISMATCH:
2851.10Schristos	case RPC_TIMEDOUT:
2861.10Schristos	case RPC_CANTRECV:
2871.24Sfvdl	default:
2881.1Scgd		break;
2891.1Scgd	}
2901.1Scgd	return (str);
2911.1Scgd}
2921.1Scgd
2931.1Scgdvoid
2941.29Smattclnt_pcreateerror(const char *s)
2951.1Scgd{
2961.18Slukem
2971.18Slukem	_DIAGASSERT(s != NULL);
2981.18Slukem
2991.14Slukem	(void) fprintf(stderr, "%s\n", clnt_spcreateerror(s));
3001.1Scgd}
3011.1Scgd
3021.6Sjtcstatic const char *const auth_errlist[] = {
3031.6Sjtc	"Authentication OK",			/* 0 - AUTH_OK */
3041.6Sjtc	"Invalid client credential",		/* 1 - AUTH_BADCRED */
3051.6Sjtc	"Server rejected credential",		/* 2 - AUTH_REJECTEDCRED */
3061.6Sjtc	"Invalid client verifier", 		/* 3 - AUTH_BADVERF */
3071.6Sjtc	"Server rejected verifier", 		/* 4 - AUTH_REJECTEDVERF */
3081.6Sjtc	"Client credential too weak",		/* 5 - AUTH_TOOWEAK */
3091.6Sjtc	"Invalid server verifier",		/* 6 - AUTH_INVALIDRESP */
3101.6Sjtc	"Failed (unspecified error)"		/* 7 - AUTH_FAILED */
3111.1Scgd};
3121.1Scgd
3131.1Scgdstatic char *
3141.29Smattauth_errmsg(enum auth_stat stat)
3151.1Scgd{
3161.6Sjtc	unsigned int errnum = stat;
3171.6Sjtc
3181.29Smatt	if (errnum < __arraycount(auth_errlist))
3191.26Schristos		return __UNCONST(auth_errlist[errnum]);
3201.1Scgd
3211.1Scgd	return(NULL);
3221.1Scgd}
323