xref: /freebsd/lib/libc/rpc/clnt_perror.c (revision 0c43d89a0d8e976ca494d4837f4c1f3734d2c300)
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 *sccsid = "from: @(#)clnt_perror.c 1.15 87/10/07 Copyr 1984 Sun Micro";*/
32 /*static char *sccsid = "from: @(#)clnt_perror.c	2.1 88/07/29 4.0 RPCSRC";*/
33 static char *rcsid = "$Id: clnt_perror.c,v 1.1 1993/10/27 05:40:20 paul Exp $";
34 #endif
35 
36 /*
37  * clnt_perror.c
38  *
39  * Copyright (C) 1984, Sun Microsystems, Inc.
40  *
41  */
42 #include <stdio.h>
43 #include <string.h>
44 #include <rpc/rpc.h>
45 #include <rpc/types.h>
46 #include <rpc/auth.h>
47 #include <rpc/clnt.h>
48 
49 static char *auth_errmsg();
50 
51 static char *buf;
52 
53 static char *
54 _buf()
55 {
56 
57 	if (buf == 0)
58 		buf = (char *)malloc(256);
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 	void clnt_perrno();
72 	char *err;
73 	char *str = _buf();
74 	char *strstart = str;
75 
76 	if (str == 0)
77 		return (0);
78 	CLNT_GETERR(rpch, &e);
79 
80 	(void) sprintf(str, "%s: ", s);
81 	str += strlen(str);
82 
83 	(void) strcpy(str, clnt_sperrno(e.re_status));
84 	str += strlen(str);
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 		(void) sprintf(str, "; errno = %s",
105 		    strerror(e.re_errno));
106 		str += strlen(str);
107 		break;
108 
109 	case RPC_VERSMISMATCH:
110 		(void) sprintf(str,
111 			"; low version = %lu, high version = %lu",
112 			e.re_vers.low, e.re_vers.high);
113 		str += strlen(str);
114 		break;
115 
116 	case RPC_AUTHERROR:
117 		err = auth_errmsg(e.re_why);
118 		(void) sprintf(str,"; why = ");
119 		str += strlen(str);
120 		if (err != NULL) {
121 			(void) sprintf(str, "%s",err);
122 		} else {
123 			(void) sprintf(str,
124 				"(unknown authentication error - %d)",
125 				(int) e.re_why);
126 		}
127 		str += strlen(str);
128 		break;
129 
130 	case RPC_PROGVERSMISMATCH:
131 		(void) sprintf(str,
132 			"; low version = %lu, high version = %lu",
133 			e.re_vers.low, e.re_vers.high);
134 		str += strlen(str);
135 		break;
136 
137 	default:	/* unknown */
138 		(void) sprintf(str,
139 			"; s1 = %lu, s2 = %lu",
140 			e.re_lb.s1, e.re_lb.s2);
141 		str += strlen(str);
142 		break;
143 	}
144 	(void) sprintf(str, "\n");
145 	return(strstart) ;
146 }
147 
148 void
149 clnt_perror(rpch, s)
150 	CLIENT *rpch;
151 	char *s;
152 {
153 	(void) fprintf(stderr,"%s",clnt_sperror(rpch,s));
154 }
155 
156 
157 struct rpc_errtab {
158 	enum clnt_stat status;
159 	char *message;
160 };
161 
162 static struct rpc_errtab  rpc_errlist[] = {
163 	{ RPC_SUCCESS,
164 		"RPC: Success" },
165 	{ RPC_CANTENCODEARGS,
166 		"RPC: Can't encode arguments" },
167 	{ RPC_CANTDECODERES,
168 		"RPC: Can't decode result" },
169 	{ RPC_CANTSEND,
170 		"RPC: Unable to send" },
171 	{ RPC_CANTRECV,
172 		"RPC: Unable to receive" },
173 	{ RPC_TIMEDOUT,
174 		"RPC: Timed out" },
175 	{ RPC_VERSMISMATCH,
176 		"RPC: Incompatible versions of RPC" },
177 	{ RPC_AUTHERROR,
178 		"RPC: Authentication error" },
179 	{ RPC_PROGUNAVAIL,
180 		"RPC: Program unavailable" },
181 	{ RPC_PROGVERSMISMATCH,
182 		"RPC: Program/version mismatch" },
183 	{ RPC_PROCUNAVAIL,
184 		"RPC: Procedure unavailable" },
185 	{ RPC_CANTDECODEARGS,
186 		"RPC: Server can't decode arguments" },
187 	{ RPC_SYSTEMERROR,
188 		"RPC: Remote system error" },
189 	{ RPC_UNKNOWNHOST,
190 		"RPC: Unknown host" },
191 	{ RPC_UNKNOWNPROTO,
192 		"RPC: Unknown protocol" },
193 	{ RPC_PMAPFAILURE,
194 		"RPC: Port mapper failure" },
195 	{ RPC_PROGNOTREGISTERED,
196 		"RPC: Program not registered"},
197 	{ RPC_FAILED,
198 		"RPC: Failed (unspecified error)"}
199 };
200 
201 
202 /*
203  * This interface for use by clntrpc
204  */
205 char *
206 clnt_sperrno(stat)
207 	enum clnt_stat stat;
208 {
209 	int i;
210 
211 	for (i = 0; i < sizeof(rpc_errlist)/sizeof(struct rpc_errtab); i++) {
212 		if (rpc_errlist[i].status == stat) {
213 			return (rpc_errlist[i].message);
214 		}
215 	}
216 	return ("RPC: (unknown error code)");
217 }
218 
219 void
220 clnt_perrno(num)
221 	enum clnt_stat num;
222 {
223 	(void) fprintf(stderr,"%s",clnt_sperrno(num));
224 }
225 
226 
227 char *
228 clnt_spcreateerror(s)
229 	char *s;
230 {
231 	extern int sys_nerr;
232 	char *str = _buf();
233 
234 	if (str == 0)
235 		return(0);
236 	(void) sprintf(str, "%s: ", s);
237 	(void) strcat(str, clnt_sperrno(rpc_createerr.cf_stat));
238 	switch (rpc_createerr.cf_stat) {
239 	case RPC_PMAPFAILURE:
240 		(void) strcat(str, " - ");
241 		(void) strcat(str,
242 		    clnt_sperrno(rpc_createerr.cf_error.re_status));
243 		break;
244 
245 	case RPC_SYSTEMERROR:
246 		(void) strcat(str, " - ");
247 		if (rpc_createerr.cf_error.re_errno > 0
248 		    && rpc_createerr.cf_error.re_errno < sys_nerr)
249 			(void) strcat(str,
250 			    strerror(rpc_createerr.cf_error.re_errno));
251 		else
252 			(void) sprintf(&str[strlen(str)], "Error %d",
253 			    rpc_createerr.cf_error.re_errno);
254 		break;
255 	}
256 	(void) strcat(str, "\n");
257 	return (str);
258 }
259 
260 void
261 clnt_pcreateerror(s)
262 	char *s;
263 {
264 	(void) fprintf(stderr,"%s",clnt_spcreateerror(s));
265 }
266 
267 struct auth_errtab {
268 	enum auth_stat status;
269 	char *message;
270 };
271 
272 static struct auth_errtab auth_errlist[] = {
273 	{ AUTH_OK,
274 		"Authentication OK" },
275 	{ AUTH_BADCRED,
276 		"Invalid client credential" },
277 	{ AUTH_REJECTEDCRED,
278 		"Server rejected credential" },
279 	{ AUTH_BADVERF,
280 		"Invalid client verifier" },
281 	{ AUTH_REJECTEDVERF,
282 		"Server rejected verifier" },
283 	{ AUTH_TOOWEAK,
284 		"Client credential too weak" },
285 	{ AUTH_INVALIDRESP,
286 		"Invalid server verifier" },
287 	{ AUTH_FAILED,
288 		"Failed (unspecified error)" },
289 };
290 
291 static char *
292 auth_errmsg(stat)
293 	enum auth_stat stat;
294 {
295 	int i;
296 
297 	for (i = 0; i < sizeof(auth_errlist)/sizeof(struct auth_errtab); i++) {
298 		if (auth_errlist[i].status == stat) {
299 			return(auth_errlist[i].message);
300 		}
301 	}
302 	return(NULL);
303 }
304