11.30Stron/*	$NetBSD: clnt_perror.c,v 1.30 2013/03/11 20:19:29 tron Exp $	*/
21.5Scgd
31.1Scgd/*
41.30Stron * Copyright (c) 2010, Oracle America, Inc.
51.30Stron *
61.30Stron * Redistribution and use in source and binary forms, with or without
71.30Stron * modification, are permitted provided that the following conditions are
81.30Stron * met:
91.30Stron *
101.30Stron *     * Redistributions of source code must retain the above copyright
111.30Stron *       notice, this list of conditions and the following disclaimer.
121.30Stron *     * Redistributions in binary form must reproduce the above
131.30Stron *       copyright notice, this list of conditions and the following
141.30Stron *       disclaimer in the documentation and/or other materials
151.30Stron *       provided with the distribution.
161.30Stron *     * Neither the name of the "Oracle America, Inc." nor the names of its
171.30Stron *       contributors may be used to endorse or promote products derived
181.30Stron *       from this software without specific prior written permission.
191.30Stron *
201.30Stron *   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
211.30Stron *   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
221.30Stron *   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
231.30Stron *   FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
241.30Stron *   COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
251.30Stron *   INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
261.30Stron *   DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
271.30Stron *   GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
281.30Stron *   INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
291.30Stron *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
301.30Stron *   NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
311.30Stron *   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
321.1Scgd */
331.1Scgd
341.10Schristos#include <sys/cdefs.h>
351.1Scgd#if defined(LIBC_SCCS) && !defined(lint)
361.10Schristos#if 0
371.10Schristosstatic char *sccsid = "@(#)clnt_perror.c 1.15 87/10/07 Copyr 1984 Sun Micro";
381.10Schristosstatic char *sccsid = "@(#)clnt_perror.c	2.1 88/07/29 4.0 RPCSRC";
391.10Schristos#else
401.30Stron__RCSID("$NetBSD: clnt_perror.c,v 1.30 2013/03/11 20:19:29 tron Exp $");
411.10Schristos#endif
421.1Scgd#endif
431.1Scgd
441.1Scgd/*
451.1Scgd * clnt_perror.c
461.1Scgd *
471.1Scgd * Copyright (C) 1984, Sun Microsystems, Inc.
481.1Scgd *
491.1Scgd */
501.11Sjtc#include "namespace.h"
511.14Slukem
521.18Slukem#include <assert.h>
531.1Scgd#include <stdio.h>
541.3Scgd#include <stdlib.h>
551.1Scgd#include <string.h>
561.14Slukem
571.1Scgd#include <rpc/rpc.h>
581.1Scgd#include <rpc/types.h>
591.1Scgd#include <rpc/auth.h>
601.1Scgd#include <rpc/clnt.h>
611.11Sjtc
621.11Sjtc#ifdef __weak_alias
631.23Smycroft__weak_alias(clnt_pcreateerror,_clnt_pcreateerror)
641.23Smycroft__weak_alias(clnt_perrno,_clnt_perrno)
651.23Smycroft__weak_alias(clnt_perror,_clnt_perror)
661.23Smycroft__weak_alias(clnt_spcreateerror,_clnt_spcreateerror)
671.23Smycroft__weak_alias(clnt_sperrno,_clnt_sperrno)
681.23Smycroft__weak_alias(clnt_sperror,_clnt_sperror)
691.11Sjtc#endif
701.1Scgd
711.1Scgdstatic char *buf;
721.17Schristosstatic size_t buflen;
731.1Scgd
741.29Smattstatic char *_buf(void);
751.29Smattstatic char *auth_errmsg(enum auth_stat);
761.10Schristos
771.1Scgdstatic char *
781.29Smatt_buf(void)
791.1Scgd{
801.1Scgd
811.16Slukem	buflen = 256;
821.13Slukem	if (buf == 0)
831.28Schristos		buf = malloc(buflen);
841.1Scgd	return (buf);
851.1Scgd}
861.1Scgd
871.1Scgd/*
881.1Scgd * Print reply error info
891.1Scgd */
901.1Scgdchar *
911.29Smattclnt_sperror(CLIENT *rpch, const char *s)
921.1Scgd{
931.1Scgd	struct rpc_err e;
941.1Scgd	char *err;
951.18Slukem	char *str;
961.21Sexplorer	char *strstart;
971.22Sdrochner	size_t len, i;
981.1Scgd
991.18Slukem	_DIAGASSERT(rpch != NULL);
1001.18Slukem	_DIAGASSERT(s != NULL);
1011.18Slukem
1021.22Sdrochner	str = _buf(); /* side effect: sets "buflen" */
1031.13Slukem	if (str == 0)
1041.13Slukem		return (0);
1051.22Sdrochner	len = buflen;
1061.21Sexplorer	strstart = str;
1071.1Scgd	CLNT_GETERR(rpch, &e);
1081.1Scgd
1091.9Smrg	i = snprintf(str, len, "%s: ", s);
1101.9Smrg	str += i;
1111.9Smrg	len -= i;
1121.9Smrg
1131.9Smrg	(void)strncpy(str, clnt_sperrno(e.re_status), len - 1);
1141.9Smrg	i = strlen(str);
1151.9Smrg	str += i;
1161.9Smrg	len -= i;
1171.1Scgd
1181.1Scgd	switch (e.re_status) {
1191.1Scgd	case RPC_SUCCESS:
1201.1Scgd	case RPC_CANTENCODEARGS:
1211.1Scgd	case RPC_CANTDECODERES:
1221.1Scgd	case RPC_TIMEDOUT:
1231.1Scgd	case RPC_PROGUNAVAIL:
1241.1Scgd	case RPC_PROCUNAVAIL:
1251.1Scgd	case RPC_CANTDECODEARGS:
1261.1Scgd	case RPC_SYSTEMERROR:
1271.1Scgd	case RPC_UNKNOWNHOST:
1281.1Scgd	case RPC_UNKNOWNPROTO:
1291.1Scgd	case RPC_PMAPFAILURE:
1301.1Scgd	case RPC_PROGNOTREGISTERED:
1311.1Scgd	case RPC_FAILED:
1321.1Scgd		break;
1331.1Scgd
1341.1Scgd	case RPC_CANTSEND:
1351.1Scgd	case RPC_CANTRECV:
1361.16Slukem		i = snprintf(str, len, "; errno = %s", strerror(e.re_errno));
1371.9Smrg		str += i;
1381.9Smrg		len -= i;
1391.1Scgd		break;
1401.1Scgd
1411.1Scgd	case RPC_VERSMISMATCH:
1421.16Slukem		i = snprintf(str, len, "; low version = %u, high version = %u",
1431.1Scgd			e.re_vers.low, e.re_vers.high);
1441.9Smrg		str += i;
1451.9Smrg		len -= i;
1461.1Scgd		break;
1471.1Scgd
1481.1Scgd	case RPC_AUTHERROR:
1491.1Scgd		err = auth_errmsg(e.re_why);
1501.9Smrg		i = snprintf(str, len, "; why = ");
1511.9Smrg		str += i;
1521.9Smrg		len -= i;
1531.1Scgd		if (err != NULL) {
1541.9Smrg			i = snprintf(str, len, "%s",err);
1551.1Scgd		} else {
1561.9Smrg			i = snprintf(str, len,
1571.1Scgd				"(unknown authentication error - %d)",
1581.1Scgd				(int) e.re_why);
1591.1Scgd		}
1601.9Smrg		str += i;
1611.9Smrg		len -= i;
1621.1Scgd		break;
1631.1Scgd
1641.1Scgd	case RPC_PROGVERSMISMATCH:
1651.16Slukem		i = snprintf(str, len, "; low version = %u, high version = %u",
1661.1Scgd			e.re_vers.low, e.re_vers.high);
1671.9Smrg		str += i;
1681.9Smrg		len -= i;
1691.1Scgd		break;
1701.1Scgd
1711.1Scgd	default:	/* unknown */
1721.16Slukem		i = snprintf(str, len, "; s1 = %u, s2 = %u",
1731.1Scgd			e.re_lb.s1, e.re_lb.s2);
1741.9Smrg		str += i;
1751.9Smrg		len -= i;
1761.1Scgd		break;
1771.1Scgd	}
1781.1Scgd	return(strstart) ;
1791.1Scgd}
1801.1Scgd
1811.1Scgdvoid
1821.29Smattclnt_perror(CLIENT *rpch, const char *s)
1831.1Scgd{
1841.18Slukem
1851.18Slukem	_DIAGASSERT(rpch != NULL);
1861.18Slukem	_DIAGASSERT(s != NULL);
1871.18Slukem
1881.14Slukem	(void) fprintf(stderr, "%s\n", clnt_sperror(rpch,s));
1891.1Scgd}
1901.1Scgd
1911.6Sjtcstatic const char *const rpc_errlist[] = {
1921.27Syamt	[RPC_SUCCESS] =			"RPC: Success",
1931.27Syamt	[RPC_CANTENCODEARGS] =		"RPC: Can't encode arguments",
1941.27Syamt	[RPC_CANTDECODERES] =		"RPC: Can't decode result",
1951.27Syamt	[RPC_CANTSEND] =		"RPC: Unable to send",
1961.27Syamt	[RPC_CANTRECV] =		"RPC: Unable to receive",
1971.27Syamt	[RPC_TIMEDOUT] =		"RPC: Timed out",
1981.27Syamt	[RPC_VERSMISMATCH] =		"RPC: Incompatible versions of RPC",
1991.27Syamt	[RPC_AUTHERROR] = 		"RPC: Authentication error",
2001.27Syamt	[RPC_PROGUNAVAIL] =		"RPC: Program unavailable",
2011.27Syamt	[RPC_PROGVERSMISMATCH] =	"RPC: Program/version mismatch",
2021.27Syamt	[RPC_PROCUNAVAIL] =		"RPC: Procedure unavailable",
2031.27Syamt	[RPC_CANTDECODEARGS] =		"RPC: Server can't decode arguments",
2041.27Syamt	[RPC_SYSTEMERROR] =		"RPC: Remote system error",
2051.27Syamt	[RPC_UNKNOWNHOST] =		"RPC: Unknown host",
2061.27Syamt	[RPC_PMAPFAILURE] =		"RPC: Port mapper failure",
2071.27Syamt	[RPC_PROGNOTREGISTERED] =	"RPC: Program not registered",
2081.27Syamt	[RPC_FAILED] =			"RPC: Failed (unspecified error)",
2091.27Syamt	[RPC_UNKNOWNPROTO] =		"RPC: Unknown protocol",
2101.27Syamt	[RPC_UNKNOWNADDR] =		"RPC: Remote address unknown",
2111.27Syamt	[RPC_TLIERROR] =		"RPC: Misc error in the TLI library",
2121.27Syamt	[RPC_NOBROADCAST] =		"RPC: Broadcasting not supported",
2131.27Syamt	[RPC_N2AXLATEFAILURE] =		"RPC: Name -> addr translation failed",
2141.27Syamt	[RPC_INPROGRESS] =		"RPC: In progress",
2151.27Syamt	[RPC_STALERACHANDLE] =		"RPC: Stale handle",
2161.1Scgd};
2171.1Scgd
2181.1Scgd
2191.1Scgd/*
2201.1Scgd * This interface for use by clntrpc
2211.1Scgd */
2221.1Scgdchar *
2231.29Smattclnt_sperrno(enum clnt_stat stat)
2241.1Scgd{
2251.6Sjtc	unsigned int errnum = stat;
2261.27Syamt	const char *msg;
2271.6Sjtc
2281.27Syamt	msg = NULL;
2291.27Syamt	if (errnum < (sizeof(rpc_errlist)/sizeof(rpc_errlist[0]))) {
2301.27Syamt		msg = rpc_errlist[errnum];
2311.27Syamt	}
2321.27Syamt	if (msg == NULL) {
2331.27Syamt		msg = "RPC: (unknown error code)";
2341.27Syamt	}
2351.27Syamt	return __UNCONST(msg);
2361.1Scgd}
2371.1Scgd
2381.1Scgdvoid
2391.29Smattclnt_perrno(enum clnt_stat num)
2401.1Scgd{
2411.14Slukem	(void) fprintf(stderr, "%s\n", clnt_sperrno(num));
2421.1Scgd}
2431.1Scgd
2441.1Scgd
2451.1Scgdchar *
2461.29Smattclnt_spcreateerror(const char *s)
2471.1Scgd{
2481.18Slukem	char *str;
2491.22Sdrochner	size_t len, i;
2501.1Scgd
2511.18Slukem	_DIAGASSERT(s != NULL);
2521.18Slukem
2531.22Sdrochner	str = _buf(); /* side effect: sets "buflen" */
2541.13Slukem	if (str == 0)
2551.13Slukem		return(0);
2561.22Sdrochner	len = buflen;
2571.9Smrg	i = snprintf(str, len, "%s: ", s);
2581.9Smrg	len -= i;
2591.9Smrg	(void)strncat(str, clnt_sperrno(rpc_createerr.cf_stat), len - 1);
2601.1Scgd	switch (rpc_createerr.cf_stat) {
2611.1Scgd	case RPC_PMAPFAILURE:
2621.9Smrg		(void) strncat(str, " - ", len - 1);
2631.9Smrg		(void) strncat(str,
2641.9Smrg		    clnt_sperrno(rpc_createerr.cf_error.re_status), len - 4);
2651.1Scgd		break;
2661.1Scgd
2671.1Scgd	case RPC_SYSTEMERROR:
2681.9Smrg		(void)strncat(str, " - ", len - 1);
2691.9Smrg		(void)strncat(str, strerror(rpc_createerr.cf_error.re_errno),
2701.9Smrg		    len - 4);
2711.10Schristos		break;
2721.10Schristos
2731.10Schristos	case RPC_CANTSEND:
2741.10Schristos	case RPC_CANTDECODERES:
2751.10Schristos	case RPC_CANTENCODEARGS:
2761.10Schristos	case RPC_SUCCESS:
2771.10Schristos	case RPC_UNKNOWNPROTO:
2781.10Schristos	case RPC_PROGNOTREGISTERED:
2791.10Schristos	case RPC_FAILED:
2801.10Schristos	case RPC_UNKNOWNHOST:
2811.10Schristos	case RPC_CANTDECODEARGS:
2821.10Schristos	case RPC_PROCUNAVAIL:
2831.10Schristos	case RPC_PROGVERSMISMATCH:
2841.10Schristos	case RPC_PROGUNAVAIL:
2851.10Schristos	case RPC_AUTHERROR:
2861.10Schristos	case RPC_VERSMISMATCH:
2871.10Schristos	case RPC_TIMEDOUT:
2881.10Schristos	case RPC_CANTRECV:
2891.24Sfvdl	default:
2901.1Scgd		break;
2911.1Scgd	}
2921.1Scgd	return (str);
2931.1Scgd}
2941.1Scgd
2951.1Scgdvoid
2961.29Smattclnt_pcreateerror(const char *s)
2971.1Scgd{
2981.18Slukem
2991.18Slukem	_DIAGASSERT(s != NULL);
3001.18Slukem
3011.14Slukem	(void) fprintf(stderr, "%s\n", clnt_spcreateerror(s));
3021.1Scgd}
3031.1Scgd
3041.6Sjtcstatic const char *const auth_errlist[] = {
3051.6Sjtc	"Authentication OK",			/* 0 - AUTH_OK */
3061.6Sjtc	"Invalid client credential",		/* 1 - AUTH_BADCRED */
3071.6Sjtc	"Server rejected credential",		/* 2 - AUTH_REJECTEDCRED */
3081.6Sjtc	"Invalid client verifier", 		/* 3 - AUTH_BADVERF */
3091.6Sjtc	"Server rejected verifier", 		/* 4 - AUTH_REJECTEDVERF */
3101.6Sjtc	"Client credential too weak",		/* 5 - AUTH_TOOWEAK */
3111.6Sjtc	"Invalid server verifier",		/* 6 - AUTH_INVALIDRESP */
3121.6Sjtc	"Failed (unspecified error)"		/* 7 - AUTH_FAILED */
3131.1Scgd};
3141.1Scgd
3151.1Scgdstatic char *
3161.29Smattauth_errmsg(enum auth_stat stat)
3171.1Scgd{
3181.6Sjtc	unsigned int errnum = stat;
3191.6Sjtc
3201.29Smatt	if (errnum < __arraycount(auth_errlist))
3211.26Schristos		return __UNCONST(auth_errlist[errnum]);
3221.1Scgd
3231.1Scgd	return(NULL);
3241.1Scgd}
325