Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / lib / libc / rpc / clnt_perror.c
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.2 2003/06/17 04:26:44 dillon 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()
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(rpch, s)
68         CLIENT *rpch;
69         char *s;
70 {
71         struct rpc_err e;
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) snprintf(str, CLNT_PERROR_BUFLEN, "%s: %s", s, clnt_sperrno(e.re_status));
81         str += strlen(str);
82
83         switch (e.re_status) {
84         case RPC_SUCCESS:
85         case RPC_CANTENCODEARGS:
86         case RPC_CANTDECODERES:
87         case RPC_TIMEDOUT:
88         case RPC_PROGUNAVAIL:
89         case RPC_PROCUNAVAIL:
90         case RPC_CANTDECODEARGS:
91         case RPC_SYSTEMERROR:
92         case RPC_UNKNOWNHOST:
93         case RPC_UNKNOWNPROTO:
94         case RPC_PMAPFAILURE:
95         case RPC_PROGNOTREGISTERED:
96         case RPC_FAILED:
97                 break;
98
99         case RPC_CANTSEND:
100         case RPC_CANTRECV:
101                 (void) snprintf(str, CLNT_PERROR_BUFLEN - (str - strstart),
102                         "; errno = %s\n", strerror(e.re_errno));
103                 break;
104
105         case RPC_VERSMISMATCH:
106                 (void) sprintf(str,
107                         "; low version = %lu, high version = %lu\n",
108                         (u_long)e.re_vers.low, (u_long)e.re_vers.high);
109                 break;
110
111         case RPC_AUTHERROR:
112                 err = auth_errmsg(e.re_why);
113                 (void) sprintf(str,"; why = ");
114                 str += strlen(str);
115                 if (err != NULL) {
116                         (void) sprintf(str, "%s\n",err);
117                 } else {
118                         (void) sprintf(str,
119                                 "(unknown authentication error - %d)\n",
120                                 (int) e.re_why);
121                 }
122                 break;
123
124         case RPC_PROGVERSMISMATCH:
125                 (void) sprintf(str,
126                         "; low version = %lu, high version = %lu\n",
127                         (u_long)e.re_vers.low, (u_long)e.re_vers.high);
128                 break;
129
130         default:        /* unknown */
131                 (void) sprintf(str,
132                         "; s1 = %lu, s2 = %lu\n",
133                         (long)e.re_lb.s1, (long)e.re_lb.s2);
134                 break;
135         }
136         strstart[CLNT_PERROR_BUFLEN-2] = '\n';
137         strstart[CLNT_PERROR_BUFLEN-1] = '\0';
138         return(strstart) ;
139 }
140
141 void
142 clnt_perror(rpch, s)
143         CLIENT *rpch;
144         char *s;
145 {
146         (void) fprintf(stderr,"%s\n",clnt_sperror(rpch,s));
147 }
148
149
150 static const char *const rpc_errlist[] = {
151         "RPC: Success",                         /*  0 - RPC_SUCCESS */
152         "RPC: Can't encode arguments",          /*  1 - RPC_CANTENCODEARGS */
153         "RPC: Can't decode result",             /*  2 - RPC_CANTDECODERES */
154         "RPC: Unable to send",                  /*  3 - RPC_CANTSEND */
155         "RPC: Unable to receive",               /*  4 - RPC_CANTRECV */
156         "RPC: Timed out",                       /*  5 - RPC_TIMEDOUT */
157         "RPC: Incompatible versions of RPC",    /*  6 - RPC_VERSMISMATCH */
158         "RPC: Authentication error",            /*  7 - RPC_AUTHERROR */
159         "RPC: Program unavailable",             /*  8 - RPC_PROGUNAVAIL */
160         "RPC: Program/version mismatch",        /*  9 - RPC_PROGVERSMISMATCH */
161         "RPC: Procedure unavailable",           /* 10 - RPC_PROCUNAVAIL */
162         "RPC: Server can't decode arguments",   /* 11 - RPC_CANTDECODEARGS */
163         "RPC: Remote system error",             /* 12 - RPC_SYSTEMERROR */
164         "RPC: Unknown host",                    /* 13 - RPC_UNKNOWNHOST */
165         "RPC: Port mapper failure",             /* 14 - RPC_PMAPFAILURE */
166         "RPC: Program not registered",          /* 15 - RPC_PROGNOTREGISTERED */
167         "RPC: Failed (unspecified error)",      /* 16 - RPC_FAILED */
168         "RPC: Unknown protocol"                 /* 17 - RPC_UNKNOWNPROTO */
169 };
170
171
172 /*
173  * This interface for use by clntrpc
174  */
175 char *
176 clnt_sperrno(stat)
177         enum clnt_stat stat;
178 {
179         unsigned int errnum = stat;
180
181         if (errnum < (sizeof(rpc_errlist)/sizeof(rpc_errlist[0])))
182                 return (char *)rpc_errlist[errnum];
183
184         return ("RPC: (unknown error code)");
185 }
186
187 void
188 clnt_perrno(num)
189         enum clnt_stat num;
190 {
191         (void) fprintf(stderr,"%s\n",clnt_sperrno(num));
192 }
193
194
195 char *
196 clnt_spcreateerror(s)
197         char *s;
198 {
199         char *str = _buf();
200
201         if (str == 0)
202                 return(0);
203         switch (rpc_createerr.cf_stat) {
204         case RPC_PMAPFAILURE:
205                 (void) snprintf(str, CLNT_PERROR_BUFLEN, "%s: %s - %s\n", s,
206                     clnt_sperrno(rpc_createerr.cf_stat),
207                     clnt_sperrno(rpc_createerr.cf_error.re_status));
208                 break;
209
210         case RPC_SYSTEMERROR:
211                 (void) snprintf(str, CLNT_PERROR_BUFLEN, "%s: %s - %s\n", s,
212                     clnt_sperrno(rpc_createerr.cf_stat),
213                     strerror(rpc_createerr.cf_error.re_errno));
214                 break;
215         default:
216                 (void) snprintf(str, CLNT_PERROR_BUFLEN, "%s: %s\n", s,
217                 clnt_sperrno(rpc_createerr.cf_stat));
218                 break;
219         }
220         str[CLNT_PERROR_BUFLEN-2] = '\n';
221         str[CLNT_PERROR_BUFLEN-1] = '\0';
222         return (str);
223 }
224
225 void
226 clnt_pcreateerror(s)
227         char *s;
228 {
229         (void) fprintf(stderr,"%s\n",clnt_spcreateerror(s));
230 }
231
232 static const char *const auth_errlist[] = {
233         "Authentication OK",                    /* 0 - AUTH_OK */
234         "Invalid client credential",            /* 1 - AUTH_BADCRED */
235         "Server rejected credential",           /* 2 - AUTH_REJECTEDCRED */
236         "Invalid client verifier",              /* 3 - AUTH_BADVERF */
237         "Server rejected verifier",             /* 4 - AUTH_REJECTEDVERF */
238         "Client credential too weak",           /* 5 - AUTH_TOOWEAK */
239         "Invalid server verifier",              /* 6 - AUTH_INVALIDRESP */
240         "Failed (unspecified error)"            /* 7 - AUTH_FAILED */
241 };
242
243 static char *
244 auth_errmsg(stat)
245         enum auth_stat stat;
246 {
247         unsigned int errnum = stat;
248
249         if (errnum < (sizeof(auth_errlist)/sizeof(auth_errlist[0])))
250                 return (char *)auth_errlist[errnum];
251
252         return(NULL);
253 }