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