xref: /openbsd-src/lib/libc/rpc/clnt_perror.c (revision a28daedfc357b214be5c701aa8ba8adb29a7f1c2)
1 /*	$OpenBSD: clnt_perror.c,v 1.20 2007/09/12 08:16:02 moritz Exp $ */
2 /*
3  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
4  * unrestricted use provided that this legend is included on all tape
5  * media and as a part of the software program in whole or part.  Users
6  * may copy or modify Sun RPC without charge, but are not authorized
7  * to license or distribute it to anyone else except as part of a product or
8  * program developed by the user.
9  *
10  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
11  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
12  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
13  *
14  * Sun RPC is provided with no support and without any obligation on the
15  * part of Sun Microsystems, Inc. to assist in its use, correction,
16  * modification or enhancement.
17  *
18  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
19  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
20  * OR ANY PART THEREOF.
21  *
22  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
23  * or profits or other special, indirect and consequential damages, even if
24  * Sun has been advised of the possibility of such damages.
25  *
26  * Sun Microsystems, Inc.
27  * 2550 Garcia Avenue
28  * Mountain View, California  94043
29  */
30 
31 /*
32  * clnt_perror.c
33  *
34  * Copyright (C) 1984, Sun Microsystems, Inc.
35  *
36  */
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <rpc/rpc.h>
41 #include <rpc/types.h>
42 #include <rpc/auth.h>
43 #include <rpc/clnt.h>
44 
45 static char *auth_errmsg(enum auth_stat stat);
46 #define CLNT_PERROR_BUFLEN 256
47 
48 static char *buf;
49 
50 static char *
51 _buf(void)
52 {
53 
54 	if (buf == NULL)
55 		buf = (char *)malloc(CLNT_PERROR_BUFLEN);
56 	return (buf);
57 }
58 
59 /*
60  * Print reply error info
61  */
62 char *
63 clnt_sperror(CLIENT *rpch, char *s)
64 {
65 	char *err, *str = _buf(), *strstart;
66 	struct rpc_err e;
67 	int ret, len = CLNT_PERROR_BUFLEN;
68 
69 	strstart = str;
70 	if (str == NULL)
71 		return (0);
72 	CLNT_GETERR(rpch, &e);
73 
74 	ret = snprintf(str, len, "%s: %s", s, clnt_sperrno(e.re_status));
75 	if (ret == -1)
76 		ret = 0;
77 	else if (ret >= len)
78 		goto truncated;
79 	str += ret;
80 	len -= ret;
81 
82 	switch (e.re_status) {
83 	case RPC_SUCCESS:
84 	case RPC_CANTENCODEARGS:
85 	case RPC_CANTDECODERES:
86 	case RPC_TIMEDOUT:
87 	case RPC_PROGUNAVAIL:
88 	case RPC_PROCUNAVAIL:
89 	case RPC_CANTDECODEARGS:
90 	case RPC_SYSTEMERROR:
91 	case RPC_UNKNOWNHOST:
92 	case RPC_UNKNOWNPROTO:
93 	case RPC_PMAPFAILURE:
94 	case RPC_PROGNOTREGISTERED:
95 	case RPC_FAILED:
96 		break;
97 
98 	case RPC_CANTSEND:
99 	case RPC_CANTRECV:
100 		ret = snprintf(str, len, "; errno = %s", strerror(e.re_errno));
101 		if (ret == -1 || ret >= len)
102 			goto truncated;
103 		break;
104 
105 	case RPC_VERSMISMATCH:
106 		ret = snprintf(str, len,
107 		    "; low version = %u, high version = %u",
108 		    e.re_vers.low, e.re_vers.high);
109 		if (ret == -1 || ret >= len)
110 			goto truncated;
111 		break;
112 
113 	case RPC_AUTHERROR:
114 		ret = snprintf(str, len, "; why = ");
115 		if (ret == -1)
116 			ret = 0;
117 		else if (ret >= len)
118 			goto truncated;
119 		str += ret;
120 		len -= ret;
121 		err = auth_errmsg(e.re_why);
122 		if (err != NULL) {
123 			ret = snprintf(str, len, "%s", err);
124 			if (ret == -1 || ret >= len)
125 				goto truncated;
126 		} else {
127 			ret = snprintf(str, len,
128 			    "(unknown authentication error - %d)",
129 			    (int) e.re_why);
130 			if (ret == -1 || ret >= len)
131 				goto truncated;
132 		}
133 		break;
134 
135 	case RPC_PROGVERSMISMATCH:
136 		ret = snprintf(str, len,
137 		    "; low version = %u, high version = %u",
138 		    e.re_vers.low, e.re_vers.high);
139 		if (ret == -1 || ret >= len)
140 			goto truncated;
141 		break;
142 
143 	default:	/* unknown */
144 		ret = snprintf(str, len, "; s1 = %u, s2 = %u",
145 		    e.re_lb.s1, e.re_lb.s2);
146 		if (ret == -1 || ret >= len)
147 			goto truncated;
148 		break;
149 	}
150 	if (strlcat(strstart, "\n", CLNT_PERROR_BUFLEN) >= CLNT_PERROR_BUFLEN)
151 		goto truncated;
152 	return (strstart);
153 
154 truncated:
155 	snprintf(strstart + CLNT_PERROR_BUFLEN - 5, 5, "...\n");
156 	return (strstart);
157 }
158 
159 void
160 clnt_perror(CLIENT *rpch, char *s)
161 {
162 	(void) fprintf(stderr, "%s", clnt_sperror(rpch, s));
163 }
164 
165 static const char *const rpc_errlist[] = {
166 	"RPC: Success",				/*  0 - RPC_SUCCESS */
167 	"RPC: Can't encode arguments",		/*  1 - RPC_CANTENCODEARGS */
168 	"RPC: Can't decode result",		/*  2 - RPC_CANTDECODERES */
169 	"RPC: Unable to send",			/*  3 - RPC_CANTSEND */
170 	"RPC: Unable to receive",		/*  4 - RPC_CANTRECV */
171 	"RPC: Timed out",			/*  5 - RPC_TIMEDOUT */
172 	"RPC: Incompatible versions of RPC",	/*  6 - RPC_VERSMISMATCH */
173 	"RPC: Authentication error",		/*  7 - RPC_AUTHERROR */
174 	"RPC: Program unavailable",		/*  8 - RPC_PROGUNAVAIL */
175 	"RPC: Program/version mismatch",	/*  9 - RPC_PROGVERSMISMATCH */
176 	"RPC: Procedure unavailable",		/* 10 - RPC_PROCUNAVAIL */
177 	"RPC: Server can't decode arguments",	/* 11 - RPC_CANTDECODEARGS */
178 	"RPC: Remote system error",		/* 12 - RPC_SYSTEMERROR */
179 	"RPC: Unknown host",			/* 13 - RPC_UNKNOWNHOST */
180 	"RPC: Port mapper failure",		/* 14 - RPC_PMAPFAILURE */
181 	"RPC: Program not registered",		/* 15 - RPC_PROGNOTREGISTERED */
182 	"RPC: Failed (unspecified error)",	/* 16 - RPC_FAILED */
183 	"RPC: Unknown protocol"			/* 17 - RPC_UNKNOWNPROTO */
184 };
185 
186 
187 /*
188  * This interface for use by clntrpc
189  */
190 char *
191 clnt_sperrno(enum clnt_stat stat)
192 {
193 	unsigned int errnum = stat;
194 
195 	if (errnum < (sizeof(rpc_errlist)/sizeof(rpc_errlist[0])))
196 		return (char *)rpc_errlist[errnum];
197 
198 	return ("RPC: (unknown error code)");
199 }
200 
201 void
202 clnt_perrno(enum clnt_stat num)
203 {
204 	(void) fprintf(stderr, "%s\n", clnt_sperrno(num));
205 }
206 
207 
208 char *
209 clnt_spcreateerror(char *s)
210 {
211 	char *str = _buf();
212 
213 	if (str == NULL)
214 		return (0);
215 
216 	switch (rpc_createerr.cf_stat) {
217 	case RPC_PMAPFAILURE:
218 		(void) snprintf(str, CLNT_PERROR_BUFLEN, "%s: %s - %s\n", s,
219 		    clnt_sperrno(rpc_createerr.cf_stat),
220 		    clnt_sperrno(rpc_createerr.cf_error.re_status));
221 		break;
222 
223 	case RPC_SYSTEMERROR:
224 		(void) snprintf(str, CLNT_PERROR_BUFLEN, "%s: %s - %s\n", s,
225 		    clnt_sperrno(rpc_createerr.cf_stat),
226 		    strerror(rpc_createerr.cf_error.re_errno));
227 		break;
228 
229 	default:
230 		(void) snprintf(str, CLNT_PERROR_BUFLEN, "%s: %s\n", s,
231 		    clnt_sperrno(rpc_createerr.cf_stat));
232 		break;
233 	}
234 	str[CLNT_PERROR_BUFLEN-2] = '\n';
235 	str[CLNT_PERROR_BUFLEN-1] = '\0';
236 	return (str);
237 }
238 
239 void
240 clnt_pcreateerror(char *s)
241 {
242 	(void) fprintf(stderr, "%s", clnt_spcreateerror(s));
243 }
244 
245 static const char *const auth_errlist[] = {
246 	"Authentication OK",			/* 0 - AUTH_OK */
247 	"Invalid client credential",		/* 1 - AUTH_BADCRED */
248 	"Server rejected credential",		/* 2 - AUTH_REJECTEDCRED */
249 	"Invalid client verifier", 		/* 3 - AUTH_BADVERF */
250 	"Server rejected verifier", 		/* 4 - AUTH_REJECTEDVERF */
251 	"Client credential too weak",		/* 5 - AUTH_TOOWEAK */
252 	"Invalid server verifier",		/* 6 - AUTH_INVALIDRESP */
253 	"Failed (unspecified error)"		/* 7 - AUTH_FAILED */
254 };
255 
256 static char *
257 auth_errmsg(enum auth_stat stat)
258 {
259 	unsigned int errnum = stat;
260 
261 	if (errnum < (sizeof(auth_errlist)/sizeof(auth_errlist[0])))
262 		return (char *)auth_errlist[errnum];
263 
264 	return (NULL);
265 }
266