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