xref: /dragonfly/lib/libc/rpc/clnt_perror.c (revision 1847e88f)
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  * @(#)clnt_perror.c 1.15 87/10/07 Copyr 1984 Sun Micro
30  * @(#)clnt_perror.c	2.1 88/07/29 4.0 RPCSRC
31  * $FreeBSD: src/lib/libc/rpc/clnt_perror.c,v 1.11.2.1 2000/08/23 00:02:04 jhb Exp $
32  * $DragonFly: src/lib/libc/rpc/clnt_perror.c,v 1.5 2005/11/13 12:27:04 swildner Exp $
33  */
34 
35 /*
36  * clnt_perror.c
37  *
38  * Copyright (C) 1984, Sun Microsystems, Inc.
39  *
40  */
41 #include <stdio.h>
42 #include <stdlib.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 #define CLNT_PERROR_BUFLEN 256
51 
52 static char *buf;
53 
54 static char *
55 _buf(void)
56 {
57 
58 	if (buf == 0)
59 		buf = (char *)malloc(CLNT_PERROR_BUFLEN);
60 	return (buf);
61 }
62 
63 /*
64  * Print reply error info
65  */
66 char *
67 clnt_sperror(CLIENT *rpch, const char *s)
68 {
69 	struct rpc_err e;
70 	char *err;
71 	char *str = _buf();
72 	char *strstart = str;
73 
74 	if (str == 0)
75 		return (0);
76 	CLNT_GETERR(rpch, &e);
77 
78 	snprintf(str, CLNT_PERROR_BUFLEN, "%s: %s", s, clnt_sperrno(e.re_status));
79 	str += strlen(str);
80 
81 	switch (e.re_status) {
82 	case RPC_SUCCESS:
83 	case RPC_CANTENCODEARGS:
84 	case RPC_CANTDECODERES:
85 	case RPC_TIMEDOUT:
86 	case RPC_PROGUNAVAIL:
87 	case RPC_PROCUNAVAIL:
88 	case RPC_CANTDECODEARGS:
89 	case RPC_SYSTEMERROR:
90 	case RPC_UNKNOWNHOST:
91 	case RPC_UNKNOWNPROTO:
92 	case RPC_PMAPFAILURE:
93 	case RPC_PROGNOTREGISTERED:
94 	case RPC_FAILED:
95 		break;
96 
97 	case RPC_CANTSEND:
98 	case RPC_CANTRECV:
99 		snprintf(str, CLNT_PERROR_BUFLEN - (str - strstart),
100 			"; errno = %s\n", strerror(e.re_errno));
101 		break;
102 
103 	case RPC_VERSMISMATCH:
104 		sprintf(str,
105 			"; low version = %lu, high version = %lu\n",
106 			(u_long)e.re_vers.low, (u_long)e.re_vers.high);
107 		break;
108 
109 	case RPC_AUTHERROR:
110 		err = auth_errmsg(e.re_why);
111 		sprintf(str,"; why = ");
112 		str += strlen(str);
113 		if (err != NULL) {
114 			sprintf(str, "%s\n",err);
115 		} else {
116 			sprintf(str,
117 				"(unknown authentication error - %d)\n",
118 				(int) e.re_why);
119 		}
120 		break;
121 
122 	case RPC_PROGVERSMISMATCH:
123 		sprintf(str,
124 			"; low version = %lu, high version = %lu\n",
125 			(u_long)e.re_vers.low, (u_long)e.re_vers.high);
126 		break;
127 
128 	default:	/* unknown */
129 		sprintf(str,
130 			"; s1 = %lu, s2 = %lu\n",
131 			(long)e.re_lb.s1, (long)e.re_lb.s2);
132 		break;
133 	}
134 	if (strstart[CLNT_PERROR_BUFLEN-2] != '\0') {
135 		strstart[CLNT_PERROR_BUFLEN-2] = '\n';
136 		strstart[CLNT_PERROR_BUFLEN-1] = '\0';
137 	}
138 	return(strstart) ;
139 }
140 
141 void
142 clnt_perror(CLIENT *rpch, const char *s)
143 {
144 	fprintf(stderr,"%s\n",clnt_sperror(rpch,s));
145 }
146 
147 
148 static const char *const rpc_errlist[] = {
149 	"RPC: Success",				/*  0 - RPC_SUCCESS */
150 	"RPC: Can't encode arguments",		/*  1 - RPC_CANTENCODEARGS */
151 	"RPC: Can't decode result",		/*  2 - RPC_CANTDECODERES */
152 	"RPC: Unable to send",			/*  3 - RPC_CANTSEND */
153 	"RPC: Unable to receive",		/*  4 - RPC_CANTRECV */
154 	"RPC: Timed out",			/*  5 - RPC_TIMEDOUT */
155 	"RPC: Incompatible versions of RPC",	/*  6 - RPC_VERSMISMATCH */
156 	"RPC: Authentication error",		/*  7 - RPC_AUTHERROR */
157 	"RPC: Program unavailable",		/*  8 - RPC_PROGUNAVAIL */
158 	"RPC: Program/version mismatch",	/*  9 - RPC_PROGVERSMISMATCH */
159 	"RPC: Procedure unavailable",		/* 10 - RPC_PROCUNAVAIL */
160 	"RPC: Server can't decode arguments",	/* 11 - RPC_CANTDECODEARGS */
161 	"RPC: Remote system error",		/* 12 - RPC_SYSTEMERROR */
162 	"RPC: Unknown host",			/* 13 - RPC_UNKNOWNHOST */
163 	"RPC: Port mapper failure",		/* 14 - RPC_PMAPFAILURE */
164 	"RPC: Program not registered",		/* 15 - RPC_PROGNOTREGISTERED */
165 	"RPC: Failed (unspecified error)",	/* 16 - RPC_FAILED */
166 	"RPC: Unknown protocol"			/* 17 - RPC_UNKNOWNPROTO */
167 };
168 
169 
170 /*
171  * This interface for use by clntrpc
172  */
173 char *
174 clnt_sperrno(enum clnt_stat stat)
175 {
176 	unsigned int errnum = stat;
177 
178 	if (errnum < (sizeof(rpc_errlist)/sizeof(rpc_errlist[0])))
179 		return (char *)rpc_errlist[errnum];
180 
181 	return ("RPC: (unknown error code)");
182 }
183 
184 void
185 clnt_perrno(enum clnt_stat num)
186 {
187 	fprintf(stderr,"%s\n",clnt_sperrno(num));
188 }
189 
190 
191 char *
192 clnt_spcreateerror(const char *s)
193 {
194 	char *str = _buf();
195 
196 	if (str == 0)
197 		return(0);
198 	switch (rpc_createerr.cf_stat) {
199 	case RPC_PMAPFAILURE:
200 		snprintf(str, CLNT_PERROR_BUFLEN, "%s: %s - %s\n", s,
201 		    clnt_sperrno(rpc_createerr.cf_stat),
202 		    clnt_sperrno(rpc_createerr.cf_error.re_status));
203 		break;
204 
205 	case RPC_SYSTEMERROR:
206 		snprintf(str, CLNT_PERROR_BUFLEN, "%s: %s - %s\n", s,
207 		    clnt_sperrno(rpc_createerr.cf_stat),
208 		    strerror(rpc_createerr.cf_error.re_errno));
209 		break;
210 	default:
211 		snprintf(str, CLNT_PERROR_BUFLEN, "%s: %s\n", s,
212 		clnt_sperrno(rpc_createerr.cf_stat));
213 		break;
214 	}
215 	str[CLNT_PERROR_BUFLEN-2] = '\n';
216 	str[CLNT_PERROR_BUFLEN-1] = '\0';
217 	return (str);
218 }
219 
220 void
221 clnt_pcreateerror(const char *s)
222 {
223 	fprintf(stderr,"%s\n",clnt_spcreateerror(s));
224 }
225 
226 static const char *const auth_errlist[] = {
227 	"Authentication OK",			/* 0 - AUTH_OK */
228 	"Invalid client credential",		/* 1 - AUTH_BADCRED */
229 	"Server rejected credential",		/* 2 - AUTH_REJECTEDCRED */
230 	"Invalid client verifier",		/* 3 - AUTH_BADVERF */
231 	"Server rejected verifier",		/* 4 - AUTH_REJECTEDVERF */
232 	"Client credential too weak",		/* 5 - AUTH_TOOWEAK */
233 	"Invalid server verifier",		/* 6 - AUTH_INVALIDRESP */
234 	"Failed (unspecified error)"		/* 7 - AUTH_FAILED */
235 };
236 
237 static char *
238 auth_errmsg(enum auth_stat stat)
239 {
240 	unsigned int errnum = stat;
241 
242 	if (errnum < (sizeof(auth_errlist)/sizeof(auth_errlist[0])))
243 		return (char *)auth_errlist[errnum];
244 
245 	return(NULL);
246 }
247