carp: add carp_group_demote_adj()
[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  * $NetBSD: clnt_perror.c,v 1.24 2000/06/02 23:11:07 fvdl Exp $
32  * $FreeBSD: src/lib/libc/rpc/clnt_perror.c,v 1.17 2004/10/16 06:11:34 obrien Exp $
33  * $DragonFly: src/lib/libc/rpc/clnt_perror.c,v 1.5 2005/11/13 12:27:04 swildner Exp $
34  */
35
36 /*
37  * clnt_perror.c
38  *
39  * Copyright (C) 1984, Sun Microsystems, Inc.
40  *
41  */
42 #include "namespace.h"
43 #include <assert.h>
44 #include <stdio.h>
45 #include <stdlib.h>
46 #include <string.h>
47
48 #include <rpc/rpc.h>
49 #include <rpc/types.h>
50 #include <rpc/auth.h>
51 #include <rpc/clnt.h>
52 #include "un-namespace.h"
53
54 static char *buf;
55
56 static char *_buf(void);
57 static char *auth_errmsg(enum auth_stat);
58 #define CLNT_PERROR_BUFLEN 256
59
60 static char *
61 _buf(void)
62 {
63
64         if (buf == 0)
65                 buf = (char *)malloc(CLNT_PERROR_BUFLEN);
66         return (buf);
67 }
68
69 /*
70  * Print reply error info
71  */
72 char *
73 clnt_sperror(CLIENT *rpch, const char *s)
74 {
75         struct rpc_err e;
76         char *err;
77         char *str;
78         char *strstart;
79         size_t len, i;
80
81         assert(rpch != NULL);
82         assert(s != NULL);
83
84         str = _buf(); /* side effect: sets CLNT_PERROR_BUFLEN */
85         if (str == 0)
86                 return (0);
87         len = CLNT_PERROR_BUFLEN;
88         strstart = str;
89         CLNT_GETERR(rpch, &e);
90
91         if ((i = snprintf(str, len, "%s: ", s)) > 0) {
92                 str += i;
93                 len -= i;
94         }
95
96         strncpy(str, clnt_sperrno(e.re_status), len - 1);
97         i = strlen(str);
98         str += i;
99         len -= i;
100
101         switch (e.re_status) {
102         case RPC_SUCCESS:
103         case RPC_CANTENCODEARGS:
104         case RPC_CANTDECODERES:
105         case RPC_TIMEDOUT:
106         case RPC_PROGUNAVAIL:
107         case RPC_PROCUNAVAIL:
108         case RPC_CANTDECODEARGS:
109         case RPC_SYSTEMERROR:
110         case RPC_UNKNOWNHOST:
111         case RPC_UNKNOWNPROTO:
112         case RPC_PMAPFAILURE:
113         case RPC_PROGNOTREGISTERED:
114         case RPC_FAILED:
115                 break;
116
117         case RPC_CANTSEND:
118         case RPC_CANTRECV:
119                 i = snprintf(str, len, "; errno = %s", strerror(e.re_errno));
120                 if (i > 0) {
121                         str += i;
122                         len -= i;
123                 }
124                 break;
125
126         case RPC_VERSMISMATCH:
127                 i = snprintf(str, len, "; low version = %u, high version = %u",
128                         e.re_vers.low, e.re_vers.high);
129                 if (i > 0) {
130                         str += i;
131                         len -= i;
132                 }
133                 break;
134
135         case RPC_AUTHERROR:
136                 err = auth_errmsg(e.re_why);
137                 i = snprintf(str, len, "; why = ");
138                 if (i > 0) {
139                         str += i;
140                         len -= i;
141                 }
142                 if (err != NULL) {
143                         i = snprintf(str, len, "%s",err);
144                 } else {
145                         i = snprintf(str, len,
146                                 "(unknown authentication error - %d)",
147                                 (int) e.re_why);
148                 }
149                 if (i > 0) {
150                         str += i;
151                         len -= i;
152                 }
153                 break;
154
155         case RPC_PROGVERSMISMATCH:
156                 i = snprintf(str, len, "; low version = %u, high version = %u",
157                         e.re_vers.low, e.re_vers.high);
158                 if (i > 0) {
159                         str += i;
160                         len -= i;
161                 }
162                 break;
163
164         default:        /* unknown */
165                 i = snprintf(str, len, "; s1 = %u, s2 = %u",
166                         e.re_lb.s1, e.re_lb.s2);
167                 if (i > 0) {
168                         str += i;
169                         len -= i;
170                 }
171                 break;
172         }
173         strstart[CLNT_PERROR_BUFLEN-1] = '\0';
174         return(strstart) ;
175 }
176
177 void
178 clnt_perror(CLIENT *rpch, const char *s)
179 {
180
181         assert(rpch != NULL);
182         assert(s != NULL);
183
184         fprintf(stderr, "%s\n", clnt_sperror(rpch,s));
185 }
186
187 static const char *const rpc_errlist[] = {
188         "RPC: Success",                         /*  0 - RPC_SUCCESS */
189         "RPC: Can't encode arguments",          /*  1 - RPC_CANTENCODEARGS */
190         "RPC: Can't decode result",             /*  2 - RPC_CANTDECODERES */
191         "RPC: Unable to send",                  /*  3 - RPC_CANTSEND */
192         "RPC: Unable to receive",               /*  4 - RPC_CANTRECV */
193         "RPC: Timed out",                       /*  5 - RPC_TIMEDOUT */
194         "RPC: Incompatible versions of RPC",    /*  6 - RPC_VERSMISMATCH */
195         "RPC: Authentication error",            /*  7 - RPC_AUTHERROR */
196         "RPC: Program unavailable",             /*  8 - RPC_PROGUNAVAIL */
197         "RPC: Program/version mismatch",        /*  9 - RPC_PROGVERSMISMATCH */
198         "RPC: Procedure unavailable",           /* 10 - RPC_PROCUNAVAIL */
199         "RPC: Server can't decode arguments",   /* 11 - RPC_CANTDECODEARGS */
200         "RPC: Remote system error",             /* 12 - RPC_SYSTEMERROR */
201         "RPC: Unknown host",                    /* 13 - RPC_UNKNOWNHOST */
202         "RPC: Port mapper failure",             /* 14 - RPC_PMAPFAILURE */
203         "RPC: Program not registered",          /* 15 - RPC_PROGNOTREGISTERED */
204         "RPC: Failed (unspecified error)",      /* 16 - RPC_FAILED */
205         "RPC: Unknown protocol"                 /* 17 - RPC_UNKNOWNPROTO */
206 };
207
208
209 /*
210  * This interface for use by clntrpc
211  */
212 char *
213 clnt_sperrno(enum clnt_stat stat)
214 {
215         unsigned int errnum = stat;
216
217         if (errnum < (sizeof(rpc_errlist)/sizeof(rpc_errlist[0])))
218                 /* LINTED interface problem */
219                 return (char *)rpc_errlist[errnum];
220
221         return ("RPC: (unknown error code)");
222 }
223
224 void
225 clnt_perrno(enum clnt_stat num)
226 {
227         fprintf(stderr, "%s\n", clnt_sperrno(num));
228 }
229
230
231 char *
232 clnt_spcreateerror(const char *s)
233 {
234         char *str;
235         size_t len, i;
236
237         assert(s != NULL);
238
239         str = _buf(); /* side effect: sets CLNT_PERROR_BUFLEN */
240         if (str == 0)
241                 return(0);
242         len = CLNT_PERROR_BUFLEN;
243         i = snprintf(str, len, "%s: ", s);
244         if (i > 0)
245                 len -= i;
246         strncat(str, clnt_sperrno(rpc_createerr.cf_stat), len - 1);
247         switch (rpc_createerr.cf_stat) {
248         case RPC_PMAPFAILURE:
249                 strncat(str, " - ", len - 1);
250                 strncat(str,
251                     clnt_sperrno(rpc_createerr.cf_error.re_status), len - 4);
252                 break;
253
254         case RPC_SYSTEMERROR:
255                 strncat(str, " - ", len - 1);
256                 strncat(str, strerror(rpc_createerr.cf_error.re_errno),
257                     len - 4);
258                 break;
259
260         case RPC_CANTSEND:
261         case RPC_CANTDECODERES:
262         case RPC_CANTENCODEARGS:
263         case RPC_SUCCESS:
264         case RPC_UNKNOWNPROTO:
265         case RPC_PROGNOTREGISTERED:
266         case RPC_FAILED:
267         case RPC_UNKNOWNHOST:
268         case RPC_CANTDECODEARGS:
269         case RPC_PROCUNAVAIL:
270         case RPC_PROGVERSMISMATCH:
271         case RPC_PROGUNAVAIL:
272         case RPC_AUTHERROR:
273         case RPC_VERSMISMATCH:
274         case RPC_TIMEDOUT:
275         case RPC_CANTRECV:
276         default:
277                 break;
278         }
279         str[CLNT_PERROR_BUFLEN-1] = '\0';
280         return (str);
281 }
282
283 void
284 clnt_pcreateerror(const char *s)
285 {
286
287         assert(s != NULL);
288
289         fprintf(stderr, "%s\n", clnt_spcreateerror(s));
290 }
291
292 static const char *const auth_errlist[] = {
293         "Authentication OK",                    /* 0 - AUTH_OK */
294         "Invalid client credential",            /* 1 - AUTH_BADCRED */
295         "Server rejected credential",           /* 2 - AUTH_REJECTEDCRED */
296         "Invalid client verifier",              /* 3 - AUTH_BADVERF */
297         "Server rejected verifier",             /* 4 - AUTH_REJECTEDVERF */
298         "Client credential too weak",           /* 5 - AUTH_TOOWEAK */
299         "Invalid server verifier",              /* 6 - AUTH_INVALIDRESP */
300         "Failed (unspecified error)"            /* 7 - AUTH_FAILED */
301 };
302
303 static char *
304 auth_errmsg(enum auth_stat stat)
305 {
306         unsigned int errnum = stat;
307
308         if (errnum < (sizeof(auth_errlist)/sizeof(auth_errlist[0])))
309                 /* LINTED interface problem */
310                 return (char *)auth_errlist[errnum];
311
312         return(NULL);
313 }