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