Libinfo-78.tar.gz
[apple/libinfo.git] / rpc.subproj / clnt_perror.c
1 /*
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
12 * this file.
13 *
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
20 * under the License.
21 *
22 * @APPLE_LICENSE_HEADER_END@
23 */
24 /*
25 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
26 * unrestricted use provided that this legend is included on all tape
27 * media and as a part of the software program in whole or part. Users
28 * may copy or modify Sun RPC without charge, but are not authorized
29 * to license or distribute it to anyone else except as part of a product or
30 * program developed by the user.
31 *
32 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
33 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
34 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
35 *
36 * Sun RPC is provided with no support and without any obligation on the
37 * part of Sun Microsystems, Inc. to assist in its use, correction,
38 * modification or enhancement.
39 *
40 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
41 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
42 * OR ANY PART THEREOF.
43 *
44 * In no event will Sun Microsystems, Inc. be liable for any lost revenue
45 * or profits or other special, indirect and consequential damages, even if
46 * Sun has been advised of the possibility of such damages.
47 *
48 * Sun Microsystems, Inc.
49 * 2550 Garcia Avenue
50 * Mountain View, California 94043
51 */
52
53 #if defined(LIBC_SCCS) && !defined(lint)
54 /*static char *sccsid = "from: @(#)clnt_perror.c 1.15 87/10/07 Copyr 1984 Sun Micro";*/
55 /*static char *sccsid = "from: @(#)clnt_perror.c 2.1 88/07/29 4.0 RPCSRC";*/
56 static char *rcsid = "$Id: clnt_perror.c,v 1.2 1999/10/14 21:56:53 wsanchez Exp $";
57 #endif
58
59 /*
60 * clnt_perror.c
61 *
62 * Copyright (C) 1984, Sun Microsystems, Inc.
63 *
64 */
65 #include <stdio.h>
66 #include <string.h>
67 #include <rpc/rpc.h>
68 #include <rpc/types.h>
69 #include <rpc/auth.h>
70 #include <rpc/clnt.h>
71
72 static char *auth_errmsg();
73
74 static char *buf;
75
76 static char *
77 _buf()
78 {
79
80 if (buf == 0)
81 buf = (char *)malloc(256);
82 return (buf);
83 }
84
85 /*
86 * Print reply error info
87 */
88 char *
89 clnt_sperror(rpch, s)
90 CLIENT *rpch;
91 char *s;
92 {
93 struct rpc_err e;
94 void clnt_perrno();
95 char *err;
96 char *str = _buf();
97 char *strstart = str;
98
99 if (str == 0)
100 return (0);
101 CLNT_GETERR(rpch, &e);
102
103 (void) sprintf(str, "%s: ", s);
104 str += strlen(str);
105
106 (void) strcpy(str, clnt_sperrno(e.re_status));
107 str += strlen(str);
108
109 switch (e.re_status) {
110 case RPC_SUCCESS:
111 case RPC_CANTENCODEARGS:
112 case RPC_CANTDECODERES:
113 case RPC_TIMEDOUT:
114 case RPC_PROGUNAVAIL:
115 case RPC_PROCUNAVAIL:
116 case RPC_CANTDECODEARGS:
117 case RPC_SYSTEMERROR:
118 case RPC_UNKNOWNHOST:
119 case RPC_UNKNOWNPROTO:
120 case RPC_PMAPFAILURE:
121 case RPC_PROGNOTREGISTERED:
122 case RPC_FAILED:
123 break;
124
125 case RPC_CANTSEND:
126 case RPC_CANTRECV:
127 (void) sprintf(str, "; errno = %s",
128 strerror(e.re_errno));
129 str += strlen(str);
130 break;
131
132 case RPC_VERSMISMATCH:
133 (void) sprintf(str,
134 "; low version = %lu, high version = %lu",
135 e.re_vers.low, e.re_vers.high);
136 str += strlen(str);
137 break;
138
139 case RPC_AUTHERROR:
140 err = auth_errmsg(e.re_why);
141 (void) sprintf(str,"; why = ");
142 str += strlen(str);
143 if (err != NULL) {
144 (void) sprintf(str, "%s",err);
145 } else {
146 (void) sprintf(str,
147 "(unknown authentication error - %d)",
148 (int) e.re_why);
149 }
150 str += strlen(str);
151 break;
152
153 case RPC_PROGVERSMISMATCH:
154 (void) sprintf(str,
155 "; low version = %lu, high version = %lu",
156 e.re_vers.low, e.re_vers.high);
157 str += strlen(str);
158 break;
159
160 default: /* unknown */
161 (void) sprintf(str,
162 "; s1 = %lu, s2 = %lu",
163 e.re_lb.s1, e.re_lb.s2);
164 str += strlen(str);
165 break;
166 }
167 (void) sprintf(str, "\n");
168 return(strstart) ;
169 }
170
171 void
172 clnt_perror(rpch, s)
173 CLIENT *rpch;
174 char *s;
175 {
176 (void) fprintf(stderr,"%s",clnt_sperror(rpch,s));
177 }
178
179
180 struct rpc_errtab {
181 enum clnt_stat status;
182 char *message;
183 };
184
185 static struct rpc_errtab rpc_errlist[] = {
186 { RPC_SUCCESS,
187 "RPC: Success" },
188 { RPC_CANTENCODEARGS,
189 "RPC: Can't encode arguments" },
190 { RPC_CANTDECODERES,
191 "RPC: Can't decode result" },
192 { RPC_CANTSEND,
193 "RPC: Unable to send" },
194 { RPC_CANTRECV,
195 "RPC: Unable to receive" },
196 { RPC_TIMEDOUT,
197 "RPC: Timed out" },
198 { RPC_VERSMISMATCH,
199 "RPC: Incompatible versions of RPC" },
200 { RPC_AUTHERROR,
201 "RPC: Authentication error" },
202 { RPC_PROGUNAVAIL,
203 "RPC: Program unavailable" },
204 { RPC_PROGVERSMISMATCH,
205 "RPC: Program/version mismatch" },
206 { RPC_PROCUNAVAIL,
207 "RPC: Procedure unavailable" },
208 { RPC_CANTDECODEARGS,
209 "RPC: Server can't decode arguments" },
210 { RPC_SYSTEMERROR,
211 "RPC: Remote system error" },
212 { RPC_UNKNOWNHOST,
213 "RPC: Unknown host" },
214 { RPC_UNKNOWNPROTO,
215 "RPC: Unknown protocol" },
216 { RPC_PMAPFAILURE,
217 "RPC: Port mapper failure" },
218 { RPC_PROGNOTREGISTERED,
219 "RPC: Program not registered"},
220 { RPC_FAILED,
221 "RPC: Failed (unspecified error)"}
222 };
223
224
225 /*
226 * This interface for use by clntrpc
227 */
228 char *
229 clnt_sperrno(stat)
230 enum clnt_stat stat;
231 {
232 int i;
233
234 for (i = 0; i < sizeof(rpc_errlist)/sizeof(struct rpc_errtab); i++) {
235 if (rpc_errlist[i].status == stat) {
236 return (rpc_errlist[i].message);
237 }
238 }
239 return ("RPC: (unknown error code)");
240 }
241
242 void
243 clnt_perrno(num)
244 enum clnt_stat num;
245 {
246 (void) fprintf(stderr,"%s",clnt_sperrno(num));
247 }
248
249
250 char *
251 clnt_spcreateerror(s)
252 char *s;
253 {
254 extern int sys_nerr;
255 char *str = _buf();
256
257 if (str == 0)
258 return(0);
259 (void) sprintf(str, "%s: ", s);
260 (void) strcat(str, clnt_sperrno(rpc_createerr.cf_stat));
261 switch (rpc_createerr.cf_stat) {
262 case RPC_PMAPFAILURE:
263 (void) strcat(str, " - ");
264 (void) strcat(str,
265 clnt_sperrno(rpc_createerr.cf_error.re_status));
266 break;
267
268 case RPC_SYSTEMERROR:
269 (void) strcat(str, " - ");
270 if (rpc_createerr.cf_error.re_errno > 0
271 && rpc_createerr.cf_error.re_errno < sys_nerr)
272 (void) strcat(str,
273 strerror(rpc_createerr.cf_error.re_errno));
274 else
275 (void) sprintf(&str[strlen(str)], "Error %d",
276 rpc_createerr.cf_error.re_errno);
277 break;
278 }
279 (void) strcat(str, "\n");
280 return (str);
281 }
282
283 void
284 clnt_pcreateerror(s)
285 char *s;
286 {
287 (void) fprintf(stderr,"%s",clnt_spcreateerror(s));
288 }
289
290 struct auth_errtab {
291 enum auth_stat status;
292 char *message;
293 };
294
295 static struct auth_errtab auth_errlist[] = {
296 { AUTH_OK,
297 "Authentication OK" },
298 { AUTH_BADCRED,
299 "Invalid client credential" },
300 { AUTH_REJECTEDCRED,
301 "Server rejected credential" },
302 { AUTH_BADVERF,
303 "Invalid client verifier" },
304 { AUTH_REJECTEDVERF,
305 "Server rejected verifier" },
306 { AUTH_TOOWEAK,
307 "Client credential too weak" },
308 { AUTH_INVALIDRESP,
309 "Invalid server verifier" },
310 { AUTH_FAILED,
311 "Failed (unspecified error)" },
312 };
313
314 static char *
315 auth_errmsg(stat)
316 enum auth_stat stat;
317 {
318 int i;
319
320 for (i = 0; i < sizeof(auth_errlist)/sizeof(struct auth_errtab); i++) {
321 if (auth_errlist[i].status == stat) {
322 return(auth_errlist[i].message);
323 }
324 }
325 return(NULL);
326 }