xref: /freebsd/lib/libc/rpc/clnt_perror.c (revision 2008043f386721d58158e37e0d7e50df8095942d)
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 /*
39  * clnt_perror.c
40  *
41  * Copyright (C) 1984, Sun Microsystems, Inc.
42  *
43  */
44 #include "namespace.h"
45 #include <assert.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 
50 #include <rpc/rpc.h>
51 #include <rpc/types.h>
52 #include <rpc/auth.h>
53 #include <rpc/clnt.h>
54 #include "un-namespace.h"
55 
56 static char *buf;
57 
58 static char *_buf(void);
59 static char *auth_errmsg(enum auth_stat);
60 #define CLNT_PERROR_BUFLEN 256
61 
62 static char *
63 _buf(void)
64 {
65 
66 	if (buf == NULL)
67 		buf = malloc(CLNT_PERROR_BUFLEN);
68 	return (buf);
69 }
70 
71 /*
72  * Print reply error info
73  */
74 char *
75 clnt_sperror(CLIENT *rpch, const char *s)
76 {
77 	struct rpc_err e;
78 	char *err;
79 	char *str;
80 	char *strstart;
81 	size_t len, i;
82 
83 	assert(rpch != NULL);
84 	assert(s != NULL);
85 
86 	str = _buf(); /* side effect: sets CLNT_PERROR_BUFLEN */
87 	if (str == NULL)
88 		return (0);
89 	len = CLNT_PERROR_BUFLEN;
90 	strstart = str;
91 	CLNT_GETERR(rpch, &e);
92 
93 	if ((i = snprintf(str, len, "%s: ", s)) > 0) {
94 		str += i;
95 		len -= i;
96 	}
97 
98 	(void)strncpy(str, clnt_sperrno(e.re_status), len - 1);
99 	i = strlen(str);
100 	str += i;
101 	len -= i;
102 
103 	switch (e.re_status) {
104 	case RPC_SUCCESS:
105 	case RPC_CANTENCODEARGS:
106 	case RPC_CANTDECODERES:
107 	case RPC_TIMEDOUT:
108 	case RPC_PROGUNAVAIL:
109 	case RPC_PROCUNAVAIL:
110 	case RPC_CANTDECODEARGS:
111 	case RPC_SYSTEMERROR:
112 	case RPC_UNKNOWNHOST:
113 	case RPC_UNKNOWNPROTO:
114 	case RPC_PMAPFAILURE:
115 	case RPC_PROGNOTREGISTERED:
116 	case RPC_FAILED:
117 		break;
118 
119 	case RPC_CANTSEND:
120 	case RPC_CANTRECV:
121 		i = snprintf(str, len, "; errno = %s", strerror(e.re_errno));
122 		if (i > 0) {
123 			str += i;
124 			len -= i;
125 		}
126 		break;
127 
128 	case RPC_VERSMISMATCH:
129 		i = snprintf(str, len, "; low version = %u, high version = %u",
130 			e.re_vers.low, e.re_vers.high);
131 		if (i > 0) {
132 			str += i;
133 			len -= i;
134 		}
135 		break;
136 
137 	case RPC_AUTHERROR:
138 		err = auth_errmsg(e.re_why);
139 		i = snprintf(str, len, "; why = ");
140 		if (i > 0) {
141 			str += i;
142 			len -= i;
143 		}
144 		if (err != NULL) {
145 			i = snprintf(str, len, "%s",err);
146 		} else {
147 			i = snprintf(str, len,
148 				"(unknown authentication error - %d)",
149 				(int) e.re_why);
150 		}
151 		if (i > 0) {
152 			str += i;
153 			len -= i;
154 		}
155 		break;
156 
157 	case RPC_PROGVERSMISMATCH:
158 		i = snprintf(str, len, "; low version = %u, high version = %u",
159 			e.re_vers.low, e.re_vers.high);
160 		if (i > 0) {
161 			str += i;
162 			len -= i;
163 		}
164 		break;
165 
166 	default:	/* unknown */
167 		i = snprintf(str, len, "; s1 = %u, s2 = %u",
168 			e.re_lb.s1, e.re_lb.s2);
169 		if (i > 0) {
170 			str += i;
171 			len -= i;
172 		}
173 		break;
174 	}
175 	strstart[CLNT_PERROR_BUFLEN-1] = '\0';
176 	return(strstart) ;
177 }
178 
179 void
180 clnt_perror(CLIENT *rpch, const char *s)
181 {
182 
183 	assert(rpch != NULL);
184 	assert(s != NULL);
185 
186 	(void) fprintf(stderr, "%s\n", clnt_sperror(rpch,s));
187 }
188 
189 static const char *const rpc_errlist[] = {
190 	"RPC: Success",				/*  0 - RPC_SUCCESS */
191 	"RPC: Can't encode arguments",		/*  1 - RPC_CANTENCODEARGS */
192 	"RPC: Can't decode result",		/*  2 - RPC_CANTDECODERES */
193 	"RPC: Unable to send",			/*  3 - RPC_CANTSEND */
194 	"RPC: Unable to receive",		/*  4 - RPC_CANTRECV */
195 	"RPC: Timed out",			/*  5 - RPC_TIMEDOUT */
196 	"RPC: Incompatible versions of RPC",	/*  6 - RPC_VERSMISMATCH */
197 	"RPC: Authentication error",		/*  7 - RPC_AUTHERROR */
198 	"RPC: Program unavailable",		/*  8 - RPC_PROGUNAVAIL */
199 	"RPC: Program/version mismatch",	/*  9 - RPC_PROGVERSMISMATCH */
200 	"RPC: Procedure unavailable",		/* 10 - RPC_PROCUNAVAIL */
201 	"RPC: Server can't decode arguments",	/* 11 - RPC_CANTDECODEARGS */
202 	"RPC: Remote system error",		/* 12 - RPC_SYSTEMERROR */
203 	"RPC: Unknown host",			/* 13 - RPC_UNKNOWNHOST */
204 	"RPC: Port mapper failure",		/* 14 - RPC_PMAPFAILURE */
205 	"RPC: Program not registered",		/* 15 - RPC_PROGNOTREGISTERED */
206 	"RPC: Failed (unspecified error)",	/* 16 - RPC_FAILED */
207 	"RPC: Unknown protocol"			/* 17 - RPC_UNKNOWNPROTO */
208 };
209 
210 
211 /*
212  * This interface for use by clntrpc
213  */
214 char *
215 clnt_sperrno(enum clnt_stat stat)
216 {
217 	unsigned int errnum = stat;
218 
219 	if (errnum < (sizeof(rpc_errlist)/sizeof(rpc_errlist[0])))
220 		/* LINTED interface problem */
221 		return (char *)rpc_errlist[errnum];
222 
223 	return ("RPC: (unknown error code)");
224 }
225 
226 void
227 clnt_perrno(enum clnt_stat num)
228 {
229 	(void) fprintf(stderr, "%s\n", clnt_sperrno(num));
230 }
231 
232 
233 char *
234 clnt_spcreateerror(const char *s)
235 {
236 	char *str;
237 	size_t len, i;
238 
239 	assert(s != NULL);
240 
241 	str = _buf(); /* side effect: sets CLNT_PERROR_BUFLEN */
242 	if (str == NULL)
243 		return(0);
244 	len = CLNT_PERROR_BUFLEN;
245 	i = snprintf(str, len, "%s: ", s);
246 	if (i > 0)
247 		len -= i;
248 	(void)strncat(str, clnt_sperrno(rpc_createerr.cf_stat), len - 1);
249 	switch (rpc_createerr.cf_stat) {
250 	case RPC_PMAPFAILURE:
251 		(void) strncat(str, " - ", len - 1);
252 		(void) strncat(str,
253 		    clnt_sperrno(rpc_createerr.cf_error.re_status), len - 4);
254 		break;
255 
256 	case RPC_SYSTEMERROR:
257 		(void)strncat(str, " - ", len - 1);
258 		(void)strncat(str, strerror(rpc_createerr.cf_error.re_errno),
259 		    len - 4);
260 		break;
261 
262 	case RPC_CANTSEND:
263 	case RPC_CANTDECODERES:
264 	case RPC_CANTENCODEARGS:
265 	case RPC_SUCCESS:
266 	case RPC_UNKNOWNPROTO:
267 	case RPC_PROGNOTREGISTERED:
268 	case RPC_FAILED:
269 	case RPC_UNKNOWNHOST:
270 	case RPC_CANTDECODEARGS:
271 	case RPC_PROCUNAVAIL:
272 	case RPC_PROGVERSMISMATCH:
273 	case RPC_PROGUNAVAIL:
274 	case RPC_AUTHERROR:
275 	case RPC_VERSMISMATCH:
276 	case RPC_TIMEDOUT:
277 	case RPC_CANTRECV:
278 	default:
279 		break;
280 	}
281 	str[CLNT_PERROR_BUFLEN-1] = '\0';
282 	return (str);
283 }
284 
285 void
286 clnt_pcreateerror(const char *s)
287 {
288 
289 	assert(s != NULL);
290 
291 	(void) fprintf(stderr, "%s\n", clnt_spcreateerror(s));
292 }
293 
294 static const char *const auth_errlist[] = {
295 	"Authentication OK",			/* 0 - AUTH_OK */
296 	"Invalid client credential",		/* 1 - AUTH_BADCRED */
297 	"Server rejected credential",		/* 2 - AUTH_REJECTEDCRED */
298 	"Invalid client verifier", 		/* 3 - AUTH_BADVERF */
299 	"Server rejected verifier", 		/* 4 - AUTH_REJECTEDVERF */
300 	"Client credential too weak",		/* 5 - AUTH_TOOWEAK */
301 	"Invalid server verifier",		/* 6 - AUTH_INVALIDRESP */
302 	"Failed (unspecified error)",		/* 7 - AUTH_FAILED */
303 	"Kerberos generic error",		/* 8 - AUTH_KERB_GENERIC*/
304 	"Kerberos credential expired",		/* 9 - AUTH_TIMEEXPIRE */
305 	"Bad kerberos ticket file",		/* 10 - AUTH_TKT_FILE */
306 	"Can't decode kerberos authenticator",	/* 11 - AUTH_DECODE */
307 	"Address wrong in kerberos ticket",	/* 12 - AUTH_NET_ADDR */
308 	"GSS-API crediential problem",		/* 13 - RPCSEC_GSS_CREDPROBLEM */
309 	"GSS-API context problem"		/* 14 - RPCSEC_GSS_CTXPROBLEM */
310 };
311 
312 static char *
313 auth_errmsg(enum auth_stat stat)
314 {
315 	unsigned int errnum = stat;
316 
317 	if (errnum < (sizeof(auth_errlist)/sizeof(auth_errlist[0])))
318 		/* LINTED interface problem */
319 		return (char *)auth_errlist[errnum];
320 
321 	return(NULL);
322 }
323