clnt_perror.c revision 1.28
11.28Schristos/*	$NetBSD: clnt_perror.c,v 1.28 2008/04/25 17:44:44 christos 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.28Schristos__RCSID("$NetBSD: clnt_perror.c,v 1.28 2008/04/25 17:44:44 christos 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.10Schristosstatic char *_buf __P((void));
731.10Schristosstatic char *auth_errmsg __P((enum auth_stat));
741.10Schristos
751.1Scgdstatic char *
761.1Scgd_buf()
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.1Scgdclnt_sperror(rpch, s)
901.13Slukem	CLIENT *rpch;
911.25Scgd	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.1Scgdclnt_perror(rpch, s)
1831.13Slukem	CLIENT *rpch;
1841.25Scgd	const char *s;
1851.1Scgd{
1861.18Slukem
1871.18Slukem	_DIAGASSERT(rpch != NULL);
1881.18Slukem	_DIAGASSERT(s != NULL);
1891.18Slukem
1901.14Slukem	(void) fprintf(stderr, "%s\n", clnt_sperror(rpch,s));
1911.1Scgd}
1921.1Scgd
1931.6Sjtcstatic const char *const rpc_errlist[] = {
1941.27Syamt	[RPC_SUCCESS] =			"RPC: Success",
1951.27Syamt	[RPC_CANTENCODEARGS] =		"RPC: Can't encode arguments",
1961.27Syamt	[RPC_CANTDECODERES] =		"RPC: Can't decode result",
1971.27Syamt	[RPC_CANTSEND] =		"RPC: Unable to send",
1981.27Syamt	[RPC_CANTRECV] =		"RPC: Unable to receive",
1991.27Syamt	[RPC_TIMEDOUT] =		"RPC: Timed out",
2001.27Syamt	[RPC_VERSMISMATCH] =		"RPC: Incompatible versions of RPC",
2011.27Syamt	[RPC_AUTHERROR] = 		"RPC: Authentication error",
2021.27Syamt	[RPC_PROGUNAVAIL] =		"RPC: Program unavailable",
2031.27Syamt	[RPC_PROGVERSMISMATCH] =	"RPC: Program/version mismatch",
2041.27Syamt	[RPC_PROCUNAVAIL] =		"RPC: Procedure unavailable",
2051.27Syamt	[RPC_CANTDECODEARGS] =		"RPC: Server can't decode arguments",
2061.27Syamt	[RPC_SYSTEMERROR] =		"RPC: Remote system error",
2071.27Syamt	[RPC_UNKNOWNHOST] =		"RPC: Unknown host",
2081.27Syamt	[RPC_PMAPFAILURE] =		"RPC: Port mapper failure",
2091.27Syamt	[RPC_PROGNOTREGISTERED] =	"RPC: Program not registered",
2101.27Syamt	[RPC_FAILED] =			"RPC: Failed (unspecified error)",
2111.27Syamt	[RPC_UNKNOWNPROTO] =		"RPC: Unknown protocol",
2121.27Syamt	[RPC_UNKNOWNADDR] =		"RPC: Remote address unknown",
2131.27Syamt	[RPC_TLIERROR] =		"RPC: Misc error in the TLI library",
2141.27Syamt	[RPC_NOBROADCAST] =		"RPC: Broadcasting not supported",
2151.27Syamt	[RPC_N2AXLATEFAILURE] =		"RPC: Name -> addr translation failed",
2161.27Syamt	[RPC_INPROGRESS] =		"RPC: In progress",
2171.27Syamt	[RPC_STALERACHANDLE] =		"RPC: Stale handle",
2181.1Scgd};
2191.1Scgd
2201.1Scgd
2211.1Scgd/*
2221.1Scgd * This interface for use by clntrpc
2231.1Scgd */
2241.1Scgdchar *
2251.1Scgdclnt_sperrno(stat)
2261.1Scgd	enum clnt_stat stat;
2271.1Scgd{
2281.6Sjtc	unsigned int errnum = stat;
2291.27Syamt	const char *msg;
2301.6Sjtc
2311.27Syamt	msg = NULL;
2321.27Syamt	if (errnum < (sizeof(rpc_errlist)/sizeof(rpc_errlist[0]))) {
2331.27Syamt		msg = rpc_errlist[errnum];
2341.27Syamt	}
2351.27Syamt	if (msg == NULL) {
2361.27Syamt		msg = "RPC: (unknown error code)";
2371.27Syamt	}
2381.27Syamt	return __UNCONST(msg);
2391.1Scgd}
2401.1Scgd
2411.1Scgdvoid
2421.1Scgdclnt_perrno(num)
2431.1Scgd	enum clnt_stat num;
2441.1Scgd{
2451.14Slukem	(void) fprintf(stderr, "%s\n", clnt_sperrno(num));
2461.1Scgd}
2471.1Scgd
2481.1Scgd
2491.1Scgdchar *
2501.1Scgdclnt_spcreateerror(s)
2511.25Scgd	const char *s;
2521.1Scgd{
2531.18Slukem	char *str;
2541.22Sdrochner	size_t len, i;
2551.1Scgd
2561.18Slukem	_DIAGASSERT(s != NULL);
2571.18Slukem
2581.22Sdrochner	str = _buf(); /* side effect: sets "buflen" */
2591.13Slukem	if (str == 0)
2601.13Slukem		return(0);
2611.22Sdrochner	len = buflen;
2621.9Smrg	i = snprintf(str, len, "%s: ", s);
2631.9Smrg	len -= i;
2641.9Smrg	(void)strncat(str, clnt_sperrno(rpc_createerr.cf_stat), len - 1);
2651.1Scgd	switch (rpc_createerr.cf_stat) {
2661.1Scgd	case RPC_PMAPFAILURE:
2671.9Smrg		(void) strncat(str, " - ", len - 1);
2681.9Smrg		(void) strncat(str,
2691.9Smrg		    clnt_sperrno(rpc_createerr.cf_error.re_status), len - 4);
2701.1Scgd		break;
2711.1Scgd
2721.1Scgd	case RPC_SYSTEMERROR:
2731.9Smrg		(void)strncat(str, " - ", len - 1);
2741.9Smrg		(void)strncat(str, strerror(rpc_createerr.cf_error.re_errno),
2751.9Smrg		    len - 4);
2761.10Schristos		break;
2771.10Schristos
2781.10Schristos	case RPC_CANTSEND:
2791.10Schristos	case RPC_CANTDECODERES:
2801.10Schristos	case RPC_CANTENCODEARGS:
2811.10Schristos	case RPC_SUCCESS:
2821.10Schristos	case RPC_UNKNOWNPROTO:
2831.10Schristos	case RPC_PROGNOTREGISTERED:
2841.10Schristos	case RPC_FAILED:
2851.10Schristos	case RPC_UNKNOWNHOST:
2861.10Schristos	case RPC_CANTDECODEARGS:
2871.10Schristos	case RPC_PROCUNAVAIL:
2881.10Schristos	case RPC_PROGVERSMISMATCH:
2891.10Schristos	case RPC_PROGUNAVAIL:
2901.10Schristos	case RPC_AUTHERROR:
2911.10Schristos	case RPC_VERSMISMATCH:
2921.10Schristos	case RPC_TIMEDOUT:
2931.10Schristos	case RPC_CANTRECV:
2941.24Sfvdl	default:
2951.1Scgd		break;
2961.1Scgd	}
2971.1Scgd	return (str);
2981.1Scgd}
2991.1Scgd
3001.1Scgdvoid
3011.1Scgdclnt_pcreateerror(s)
3021.25Scgd	const char *s;
3031.1Scgd{
3041.18Slukem
3051.18Slukem	_DIAGASSERT(s != NULL);
3061.18Slukem
3071.14Slukem	(void) fprintf(stderr, "%s\n", clnt_spcreateerror(s));
3081.1Scgd}
3091.1Scgd
3101.6Sjtcstatic const char *const auth_errlist[] = {
3111.6Sjtc	"Authentication OK",			/* 0 - AUTH_OK */
3121.6Sjtc	"Invalid client credential",		/* 1 - AUTH_BADCRED */
3131.6Sjtc	"Server rejected credential",		/* 2 - AUTH_REJECTEDCRED */
3141.6Sjtc	"Invalid client verifier", 		/* 3 - AUTH_BADVERF */
3151.6Sjtc	"Server rejected verifier", 		/* 4 - AUTH_REJECTEDVERF */
3161.6Sjtc	"Client credential too weak",		/* 5 - AUTH_TOOWEAK */
3171.6Sjtc	"Invalid server verifier",		/* 6 - AUTH_INVALIDRESP */
3181.6Sjtc	"Failed (unspecified error)"		/* 7 - AUTH_FAILED */
3191.1Scgd};
3201.1Scgd
3211.1Scgdstatic char *
3221.1Scgdauth_errmsg(stat)
3231.1Scgd	enum auth_stat stat;
3241.1Scgd{
3251.6Sjtc	unsigned int errnum = stat;
3261.6Sjtc
3271.6Sjtc	if (errnum < (sizeof(auth_errlist)/sizeof(auth_errlist[0])))
3281.26Schristos		return __UNCONST(auth_errlist[errnum]);
3291.1Scgd
3301.1Scgd	return(NULL);
3311.1Scgd}
332