xref: /freebsd/lib/libc/rpc/clnt_perror.c (revision 47dd1d1b619cc035b82b49a91a25544309ff95ae)
1 /*	$NetBSD: clnt_perror.c,v 1.24 2000/06/02 23:11:07 fvdl Exp $	*/
2 
3 
4 /*-
5  * SPDX-License-Identifier: BSD-3-Clause
6  *
7  * Copyright (c) 2009, Sun Microsystems, Inc.
8  * All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions are met:
12  * - Redistributions of source code must retain the above copyright notice,
13  *   this list of conditions and the following disclaimer.
14  * - Redistributions in binary form must reproduce the above copyright notice,
15  *   this list of conditions and the following disclaimer in the documentation
16  *   and/or other materials provided with the distribution.
17  * - Neither the name of Sun Microsystems, Inc. nor the names of its
18  *   contributors may be used to endorse or promote products derived
19  *   from this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
25  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31  * POSSIBILITY OF SUCH DAMAGE.
32  */
33 
34 #if defined(LIBC_SCCS) && !defined(lint)
35 static char *sccsid2 = "@(#)clnt_perror.c 1.15 87/10/07 Copyr 1984 Sun Micro";
36 static char *sccsid = "@(#)clnt_perror.c	2.1 88/07/29 4.0 RPCSRC";
37 #endif
38 #include <sys/cdefs.h>
39 __FBSDID("$FreeBSD$");
40 
41 /*
42  * clnt_perror.c
43  *
44  * Copyright (C) 1984, Sun Microsystems, Inc.
45  *
46  */
47 #include "namespace.h"
48 #include <assert.h>
49 #include <stdio.h>
50 #include <stdlib.h>
51 #include <string.h>
52 
53 #include <rpc/rpc.h>
54 #include <rpc/types.h>
55 #include <rpc/auth.h>
56 #include <rpc/clnt.h>
57 #include "un-namespace.h"
58 
59 static char *buf;
60 
61 static char *_buf(void);
62 static char *auth_errmsg(enum auth_stat);
63 #define CLNT_PERROR_BUFLEN 256
64 
65 static char *
66 _buf(void)
67 {
68 
69 	if (buf == NULL)
70 		buf = malloc(CLNT_PERROR_BUFLEN);
71 	return (buf);
72 }
73 
74 /*
75  * Print reply error info
76  */
77 char *
78 clnt_sperror(CLIENT *rpch, const char *s)
79 {
80 	struct rpc_err e;
81 	char *err;
82 	char *str;
83 	char *strstart;
84 	size_t len, i;
85 
86 	assert(rpch != NULL);
87 	assert(s != NULL);
88 
89 	str = _buf(); /* side effect: sets CLNT_PERROR_BUFLEN */
90 	if (str == NULL)
91 		return (0);
92 	len = CLNT_PERROR_BUFLEN;
93 	strstart = str;
94 	CLNT_GETERR(rpch, &e);
95 
96 	if ((i = snprintf(str, len, "%s: ", s)) > 0) {
97 		str += i;
98 		len -= i;
99 	}
100 
101 	(void)strncpy(str, clnt_sperrno(e.re_status), len - 1);
102 	i = strlen(str);
103 	str += i;
104 	len -= i;
105 
106 	switch (e.re_status) {
107 	case RPC_SUCCESS:
108 	case RPC_CANTENCODEARGS:
109 	case RPC_CANTDECODERES:
110 	case RPC_TIMEDOUT:
111 	case RPC_PROGUNAVAIL:
112 	case RPC_PROCUNAVAIL:
113 	case RPC_CANTDECODEARGS:
114 	case RPC_SYSTEMERROR:
115 	case RPC_UNKNOWNHOST:
116 	case RPC_UNKNOWNPROTO:
117 	case RPC_PMAPFAILURE:
118 	case RPC_PROGNOTREGISTERED:
119 	case RPC_FAILED:
120 		break;
121 
122 	case RPC_CANTSEND:
123 	case RPC_CANTRECV:
124 		i = snprintf(str, len, "; errno = %s", strerror(e.re_errno));
125 		if (i > 0) {
126 			str += i;
127 			len -= i;
128 		}
129 		break;
130 
131 	case RPC_VERSMISMATCH:
132 		i = snprintf(str, len, "; low version = %u, high version = %u",
133 			e.re_vers.low, e.re_vers.high);
134 		if (i > 0) {
135 			str += i;
136 			len -= i;
137 		}
138 		break;
139 
140 	case RPC_AUTHERROR:
141 		err = auth_errmsg(e.re_why);
142 		i = snprintf(str, len, "; why = ");
143 		if (i > 0) {
144 			str += i;
145 			len -= i;
146 		}
147 		if (err != NULL) {
148 			i = snprintf(str, len, "%s",err);
149 		} else {
150 			i = snprintf(str, len,
151 				"(unknown authentication error - %d)",
152 				(int) e.re_why);
153 		}
154 		if (i > 0) {
155 			str += i;
156 			len -= i;
157 		}
158 		break;
159 
160 	case RPC_PROGVERSMISMATCH:
161 		i = snprintf(str, len, "; low version = %u, high version = %u",
162 			e.re_vers.low, e.re_vers.high);
163 		if (i > 0) {
164 			str += i;
165 			len -= i;
166 		}
167 		break;
168 
169 	default:	/* unknown */
170 		i = snprintf(str, len, "; s1 = %u, s2 = %u",
171 			e.re_lb.s1, e.re_lb.s2);
172 		if (i > 0) {
173 			str += i;
174 			len -= i;
175 		}
176 		break;
177 	}
178 	strstart[CLNT_PERROR_BUFLEN-1] = '\0';
179 	return(strstart) ;
180 }
181 
182 void
183 clnt_perror(CLIENT *rpch, const char *s)
184 {
185 
186 	assert(rpch != NULL);
187 	assert(s != NULL);
188 
189 	(void) fprintf(stderr, "%s\n", clnt_sperror(rpch,s));
190 }
191 
192 static const char *const rpc_errlist[] = {
193 	"RPC: Success",				/*  0 - RPC_SUCCESS */
194 	"RPC: Can't encode arguments",		/*  1 - RPC_CANTENCODEARGS */
195 	"RPC: Can't decode result",		/*  2 - RPC_CANTDECODERES */
196 	"RPC: Unable to send",			/*  3 - RPC_CANTSEND */
197 	"RPC: Unable to receive",		/*  4 - RPC_CANTRECV */
198 	"RPC: Timed out",			/*  5 - RPC_TIMEDOUT */
199 	"RPC: Incompatible versions of RPC",	/*  6 - RPC_VERSMISMATCH */
200 	"RPC: Authentication error",		/*  7 - RPC_AUTHERROR */
201 	"RPC: Program unavailable",		/*  8 - RPC_PROGUNAVAIL */
202 	"RPC: Program/version mismatch",	/*  9 - RPC_PROGVERSMISMATCH */
203 	"RPC: Procedure unavailable",		/* 10 - RPC_PROCUNAVAIL */
204 	"RPC: Server can't decode arguments",	/* 11 - RPC_CANTDECODEARGS */
205 	"RPC: Remote system error",		/* 12 - RPC_SYSTEMERROR */
206 	"RPC: Unknown host",			/* 13 - RPC_UNKNOWNHOST */
207 	"RPC: Port mapper failure",		/* 14 - RPC_PMAPFAILURE */
208 	"RPC: Program not registered",		/* 15 - RPC_PROGNOTREGISTERED */
209 	"RPC: Failed (unspecified error)",	/* 16 - RPC_FAILED */
210 	"RPC: Unknown protocol"			/* 17 - RPC_UNKNOWNPROTO */
211 };
212 
213 
214 /*
215  * This interface for use by clntrpc
216  */
217 char *
218 clnt_sperrno(enum clnt_stat stat)
219 {
220 	unsigned int errnum = stat;
221 
222 	if (errnum < (sizeof(rpc_errlist)/sizeof(rpc_errlist[0])))
223 		/* LINTED interface problem */
224 		return (char *)rpc_errlist[errnum];
225 
226 	return ("RPC: (unknown error code)");
227 }
228 
229 void
230 clnt_perrno(enum clnt_stat num)
231 {
232 	(void) fprintf(stderr, "%s\n", clnt_sperrno(num));
233 }
234 
235 
236 char *
237 clnt_spcreateerror(const char *s)
238 {
239 	char *str;
240 	size_t len, i;
241 
242 	assert(s != NULL);
243 
244 	str = _buf(); /* side effect: sets CLNT_PERROR_BUFLEN */
245 	if (str == NULL)
246 		return(0);
247 	len = CLNT_PERROR_BUFLEN;
248 	i = snprintf(str, len, "%s: ", s);
249 	if (i > 0)
250 		len -= i;
251 	(void)strncat(str, clnt_sperrno(rpc_createerr.cf_stat), len - 1);
252 	switch (rpc_createerr.cf_stat) {
253 	case RPC_PMAPFAILURE:
254 		(void) strncat(str, " - ", len - 1);
255 		(void) strncat(str,
256 		    clnt_sperrno(rpc_createerr.cf_error.re_status), len - 4);
257 		break;
258 
259 	case RPC_SYSTEMERROR:
260 		(void)strncat(str, " - ", len - 1);
261 		(void)strncat(str, strerror(rpc_createerr.cf_error.re_errno),
262 		    len - 4);
263 		break;
264 
265 	case RPC_CANTSEND:
266 	case RPC_CANTDECODERES:
267 	case RPC_CANTENCODEARGS:
268 	case RPC_SUCCESS:
269 	case RPC_UNKNOWNPROTO:
270 	case RPC_PROGNOTREGISTERED:
271 	case RPC_FAILED:
272 	case RPC_UNKNOWNHOST:
273 	case RPC_CANTDECODEARGS:
274 	case RPC_PROCUNAVAIL:
275 	case RPC_PROGVERSMISMATCH:
276 	case RPC_PROGUNAVAIL:
277 	case RPC_AUTHERROR:
278 	case RPC_VERSMISMATCH:
279 	case RPC_TIMEDOUT:
280 	case RPC_CANTRECV:
281 	default:
282 		break;
283 	}
284 	str[CLNT_PERROR_BUFLEN-1] = '\0';
285 	return (str);
286 }
287 
288 void
289 clnt_pcreateerror(const char *s)
290 {
291 
292 	assert(s != NULL);
293 
294 	(void) fprintf(stderr, "%s\n", clnt_spcreateerror(s));
295 }
296 
297 static const char *const auth_errlist[] = {
298 	"Authentication OK",			/* 0 - AUTH_OK */
299 	"Invalid client credential",		/* 1 - AUTH_BADCRED */
300 	"Server rejected credential",		/* 2 - AUTH_REJECTEDCRED */
301 	"Invalid client verifier", 		/* 3 - AUTH_BADVERF */
302 	"Server rejected verifier", 		/* 4 - AUTH_REJECTEDVERF */
303 	"Client credential too weak",		/* 5 - AUTH_TOOWEAK */
304 	"Invalid server verifier",		/* 6 - AUTH_INVALIDRESP */
305 	"Failed (unspecified error)",		/* 7 - AUTH_FAILED */
306 	"Kerberos generic error",		/* 8 - AUTH_KERB_GENERIC*/
307 	"Kerberos credential expired",		/* 9 - AUTH_TIMEEXPIRE */
308 	"Bad kerberos ticket file",		/* 10 - AUTH_TKT_FILE */
309 	"Can't decode kerberos authenticator",	/* 11 - AUTH_DECODE */
310 	"Address wrong in kerberos ticket",	/* 12 - AUTH_NET_ADDR */
311 	"GSS-API crediential problem",		/* 13 - RPCSEC_GSS_CREDPROBLEM */
312 	"GSS-API context problem"		/* 14 - RPCSEC_GSS_CTXPROBLEM */
313 };
314 
315 static char *
316 auth_errmsg(enum auth_stat stat)
317 {
318 	unsigned int errnum = stat;
319 
320 	if (errnum < (sizeof(auth_errlist)/sizeof(auth_errlist[0])))
321 		/* LINTED interface problem */
322 		return (char *)auth_errlist[errnum];
323 
324 	return(NULL);
325 }
326