Back to index

glibc  2.9
clnt_perr.c
Go to the documentation of this file.
00001 /* @(#)clnt_perror.c 2.1 88/07/29 4.0 RPCSRC */
00002 /*
00003  * Sun RPC is a product of Sun Microsystems, Inc. and is provided for
00004  * unrestricted use provided that this legend is included on all tape
00005  * media and as a part of the software program in whole or part.  Users
00006  * may copy or modify Sun RPC without charge, but are not authorized
00007  * to license or distribute it to anyone else except as part of a product or
00008  * program developed by the user.
00009  *
00010  * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE
00011  * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR
00012  * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE.
00013  *
00014  * Sun RPC is provided with no support and without any obligation on the
00015  * part of Sun Microsystems, Inc. to assist in its use, correction,
00016  * modification or enhancement.
00017  *
00018  * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE
00019  * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC
00020  * OR ANY PART THEREOF.
00021  *
00022  * In no event will Sun Microsystems, Inc. be liable for any lost revenue
00023  * or profits or other special, indirect and consequential damages, even if
00024  * Sun has been advised of the possibility of such damages.
00025  *
00026  * Sun Microsystems, Inc.
00027  * 2550 Garcia Avenue
00028  * Mountain View, California  94043
00029  */
00030 #if !defined(lint) && defined(SCCSIDS)
00031 static char sccsid[] = "@(#)clnt_perror.c 1.15 87/10/07 Copyr 1984 Sun Micro";
00032 #endif
00033 
00034 /*
00035  * clnt_perror.c
00036  *
00037  * Copyright (C) 1984, Sun Microsystems, Inc.
00038  *
00039  */
00040 #include <stdio.h>
00041 #include <string.h>
00042 #include <libintl.h>
00043 #include <rpc/rpc.h>
00044 
00045 #ifdef USE_IN_LIBIO
00046 # include <wchar.h>
00047 # include <libio/iolibio.h>
00048 #endif
00049 
00050 static char *auth_errmsg (enum auth_stat stat) internal_function;
00051 
00052 #ifdef _RPC_THREAD_SAFE_
00053 /*
00054  * Making buf a preprocessor macro requires renaming the local
00055  * buf variable in a few functions.  Overriding a global variable
00056  * with a local variable of the same name is a bad idea, anyway.
00057  */
00058 #define buf RPC_THREAD_VARIABLE(clnt_perr_buf_s)
00059 #else
00060 static char *buf;
00061 #endif
00062 
00063 /*
00064  * Print reply error info
00065  */
00066 char *
00067 clnt_sperror (CLIENT * rpch, const char *msg)
00068 {
00069   struct rpc_err e;
00070   CLNT_GETERR (rpch, &e);
00071 
00072   const char *errstr = clnt_sperrno (e.re_status);
00073 
00074   char chrbuf[1024];
00075   char *str;
00076   char *tmpstr;
00077   int res;
00078   switch (e.re_status)
00079     {
00080     case RPC_SUCCESS:
00081     case RPC_CANTENCODEARGS:
00082     case RPC_CANTDECODERES:
00083     case RPC_TIMEDOUT:
00084     case RPC_PROGUNAVAIL:
00085     case RPC_PROCUNAVAIL:
00086     case RPC_CANTDECODEARGS:
00087     case RPC_SYSTEMERROR:
00088     case RPC_UNKNOWNHOST:
00089     case RPC_UNKNOWNPROTO:
00090     case RPC_PMAPFAILURE:
00091     case RPC_PROGNOTREGISTERED:
00092     case RPC_FAILED:
00093       res = __asprintf (&str, "%s: %s\n", msg, errstr);
00094       break;
00095 
00096     case RPC_CANTSEND:
00097     case RPC_CANTRECV:
00098       res = __asprintf (&str, "%s: %s; errno = %s\n",
00099                      msg, errstr, __strerror_r (e.re_errno,
00100                                              chrbuf, sizeof chrbuf));
00101       break;
00102 
00103     case RPC_VERSMISMATCH:
00104       res = __asprintf (&str,
00105                      _("%s: %s; low version = %lu, high version = %lu"),
00106                      msg, errstr, e.re_vers.low, e.re_vers.high);
00107       break;
00108 
00109     case RPC_AUTHERROR:
00110       tmpstr = auth_errmsg (e.re_why);
00111       if (tmpstr != NULL)
00112        res = __asprintf (&str, _("%s: %s; why = %s\n"), msg, errstr, tmpstr);
00113       else
00114        res = __asprintf (&str, _("\
00115 %s: %s; why = (unknown authentication error - %d)\n"),
00116                        msg, errstr, (int) e.re_why);
00117       break;
00118 
00119     case RPC_PROGVERSMISMATCH:
00120       res = __asprintf (&str,
00121                      _("%s: %s; low version = %lu, high version = %lu"),
00122                      msg, errstr, e.re_vers.low, e.re_vers.high);
00123       break;
00124 
00125     default:                /* unknown */
00126       res = __asprintf (&str, "%s: %s; s1 = %lu, s2 = %lu",
00127                      msg, errstr, e.re_lb.s1, e.re_lb.s2);
00128       break;
00129     }
00130 
00131   if (res < 0)
00132     return NULL;
00133 
00134   char *oldbuf = buf;
00135   buf = str;
00136   free (oldbuf);
00137 
00138   return str;
00139 }
00140 libc_hidden_def (clnt_sperror)
00141 
00142 void
00143 clnt_perror (CLIENT * rpch, const char *msg)
00144 {
00145   (void) __fxprintf (NULL, "%s", clnt_sperror (rpch, msg));
00146 }
00147 libc_hidden_def (clnt_perror)
00148 
00149 
00150 struct rpc_errtab
00151 {
00152   enum clnt_stat status;
00153   unsigned int message_off;
00154 };
00155 
00156 static const char rpc_errstr[] =
00157 {
00158 #define RPC_SUCCESS_IDX            0
00159   N_("RPC: Success")
00160   "\0"
00161 #define RPC_CANTENCODEARGS_IDX     (RPC_SUCCESS_IDX + sizeof "RPC: Success")
00162   N_("RPC: Can't encode arguments")
00163   "\0"
00164 #define RPC_CANTDECODERES_IDX      (RPC_CANTENCODEARGS_IDX \
00165                              + sizeof "RPC: Can't encode arguments")
00166   N_("RPC: Can't decode result")
00167   "\0"
00168 #define RPC_CANTSEND_IDX    (RPC_CANTDECODERES_IDX \
00169                              + sizeof "RPC: Can't decode result")
00170   N_("RPC: Unable to send")
00171   "\0"
00172 #define RPC_CANTRECV_IDX    (RPC_CANTSEND_IDX \
00173                              + sizeof "RPC: Unable to send")
00174   N_("RPC: Unable to receive")
00175   "\0"
00176 #define RPC_TIMEDOUT_IDX    (RPC_CANTRECV_IDX \
00177                              + sizeof "RPC: Unable to receive")
00178   N_("RPC: Timed out")
00179   "\0"
00180 #define RPC_VERSMISMATCH_IDX       (RPC_TIMEDOUT_IDX \
00181                              + sizeof "RPC: Timed out")
00182   N_("RPC: Incompatible versions of RPC")
00183   "\0"
00184 #define RPC_AUTHERROR_IDX   (RPC_VERSMISMATCH_IDX \
00185                              + sizeof "RPC: Incompatible versions of RPC")
00186   N_("RPC: Authentication error")
00187   "\0"
00188 #define RPC_PROGUNAVAIL_IDX        (RPC_AUTHERROR_IDX \
00189                              + sizeof "RPC: Authentication error")
00190   N_("RPC: Program unavailable")
00191   "\0"
00192 #define RPC_PROGVERSMISMATCH_IDX (RPC_PROGUNAVAIL_IDX \
00193                               + sizeof "RPC: Program unavailable")
00194   N_("RPC: Program/version mismatch")
00195   "\0"
00196 #define RPC_PROCUNAVAIL_IDX (RPC_PROGVERSMISMATCH_IDX \
00197                              + sizeof "RPC: Program/version mismatch")
00198   N_("RPC: Procedure unavailable")
00199   "\0"
00200 #define RPC_CANTDECODEARGS_IDX     (RPC_PROCUNAVAIL_IDX \
00201                              + sizeof "RPC: Procedure unavailable")
00202   N_("RPC: Server can't decode arguments")
00203   "\0"
00204 #define RPC_SYSTEMERROR_IDX (RPC_CANTDECODEARGS_IDX \
00205                              + sizeof "RPC: Server can't decode arguments")
00206   N_("RPC: Remote system error")
00207   "\0"
00208 #define RPC_UNKNOWNHOST_IDX (RPC_SYSTEMERROR_IDX \
00209                              + sizeof "RPC: Remote system error")
00210   N_("RPC: Unknown host")
00211   "\0"
00212 #define RPC_UNKNOWNPROTO_IDX       (RPC_UNKNOWNHOST_IDX \
00213                              + sizeof "RPC: Unknown host")
00214   N_("RPC: Unknown protocol")
00215   "\0"
00216 #define RPC_PMAPFAILURE_IDX (RPC_UNKNOWNPROTO_IDX \
00217                              + sizeof "RPC: Unknown protocol")
00218   N_("RPC: Port mapper failure")
00219   "\0"
00220 #define RPC_PROGNOTREGISTERED_IDX (RPC_PMAPFAILURE_IDX \
00221                                + sizeof "RPC: Port mapper failure")
00222   N_("RPC: Program not registered")
00223   "\0"
00224 #define RPC_FAILED_IDX             (RPC_PROGNOTREGISTERED_IDX \
00225                              + sizeof "RPC: Program not registered")
00226   N_("RPC: Failed (unspecified error)")
00227 };
00228 
00229 static const struct rpc_errtab rpc_errlist[] =
00230 {
00231   { RPC_SUCCESS, RPC_SUCCESS_IDX },
00232   { RPC_CANTENCODEARGS, RPC_CANTENCODEARGS_IDX },
00233   { RPC_CANTDECODERES, RPC_CANTDECODERES_IDX },
00234   { RPC_CANTSEND, RPC_CANTSEND_IDX },
00235   { RPC_CANTRECV, RPC_CANTRECV_IDX },
00236   { RPC_TIMEDOUT, RPC_TIMEDOUT_IDX },
00237   { RPC_VERSMISMATCH, RPC_VERSMISMATCH_IDX },
00238   { RPC_AUTHERROR, RPC_AUTHERROR_IDX },
00239   { RPC_PROGUNAVAIL, RPC_PROGUNAVAIL_IDX },
00240   { RPC_PROGVERSMISMATCH, RPC_PROGVERSMISMATCH_IDX },
00241   { RPC_PROCUNAVAIL, RPC_PROCUNAVAIL_IDX },
00242   { RPC_CANTDECODEARGS, RPC_CANTDECODEARGS_IDX },
00243   { RPC_SYSTEMERROR, RPC_SYSTEMERROR_IDX },
00244   { RPC_UNKNOWNHOST, RPC_UNKNOWNHOST_IDX },
00245   { RPC_UNKNOWNPROTO, RPC_UNKNOWNPROTO_IDX },
00246   { RPC_PMAPFAILURE, RPC_PMAPFAILURE_IDX },
00247   { RPC_PROGNOTREGISTERED, RPC_PROGNOTREGISTERED_IDX },
00248   { RPC_FAILED, RPC_FAILED_IDX }
00249 };
00250 
00251 
00252 /*
00253  * This interface for use by clntrpc
00254  */
00255 char *
00256 clnt_sperrno (enum clnt_stat stat)
00257 {
00258   size_t i;
00259 
00260   for (i = 0; i < sizeof (rpc_errlist) / sizeof (struct rpc_errtab); i++)
00261     {
00262       if (rpc_errlist[i].status == stat)
00263        {
00264          return _(rpc_errstr + rpc_errlist[i].message_off);
00265        }
00266     }
00267   return _("RPC: (unknown error code)");
00268 }
00269 libc_hidden_def (clnt_sperrno)
00270 
00271 void
00272 clnt_perrno (enum clnt_stat num)
00273 {
00274   (void) __fxprintf (NULL, "%s", clnt_sperrno (num));
00275 }
00276 
00277 
00278 char *
00279 clnt_spcreateerror (const char *msg)
00280 {
00281   struct rpc_createerr *ce = &get_rpc_createerr ();
00282 
00283   char chrbuf[1024];
00284   const char *connector = "";
00285   const char *errstr = "";
00286   switch (ce->cf_stat)
00287     {
00288     case RPC_PMAPFAILURE:
00289       connector = " - ";
00290       errstr = clnt_sperrno (ce->cf_error.re_status);
00291       break;
00292 
00293     case RPC_SYSTEMERROR:
00294       connector = " - ";
00295       errstr = __strerror_r (ce->cf_error.re_errno, chrbuf, sizeof chrbuf);
00296       break;
00297 
00298     default:
00299       break;
00300     }
00301 
00302   char *str;
00303   if (__asprintf (&str, "%s: %s%s%s\n",
00304                 msg, clnt_sperrno (ce->cf_stat), connector, errstr) < 0)
00305     return NULL;
00306 
00307   char *oldbuf = buf;
00308   buf = str;
00309   free (oldbuf);
00310 
00311   return str;
00312 }
00313 libc_hidden_def (clnt_spcreateerror)
00314 
00315 void
00316 clnt_pcreateerror (const char *msg)
00317 {
00318   (void) __fxprintf (NULL, "%s", clnt_spcreateerror (msg));
00319 }
00320 
00321 struct auth_errtab
00322 {
00323   enum auth_stat status;
00324   unsigned int message_off;
00325 };
00326 
00327 static const char auth_errstr[] =
00328 {
00329 #define AUTH_OK_IDX         0
00330    N_("Authentication OK")
00331    "\0"
00332 #define AUTH_BADCRED_IDX    (AUTH_OK_IDX + sizeof "Authentication OK")
00333    N_("Invalid client credential")
00334    "\0"
00335 #define AUTH_REJECTEDCRED_IDX      (AUTH_BADCRED_IDX \
00336                              + sizeof "Invalid client credential")
00337    N_("Server rejected credential")
00338    "\0"
00339 #define AUTH_BADVERF_IDX    (AUTH_REJECTEDCRED_IDX \
00340                              + sizeof "Server rejected credential")
00341    N_("Invalid client verifier")
00342    "\0"
00343 #define AUTH_REJECTEDVERF_IDX      (AUTH_BADVERF_IDX \
00344                              + sizeof "Invalid client verifier")
00345    N_("Server rejected verifier")
00346    "\0"
00347 #define AUTH_TOOWEAK_IDX    (AUTH_REJECTEDVERF_IDX \
00348                              + sizeof "Server rejected verifier")
00349    N_("Client credential too weak")
00350    "\0"
00351 #define AUTH_INVALIDRESP_IDX       (AUTH_TOOWEAK_IDX \
00352                              + sizeof "Client credential too weak")
00353    N_("Invalid server verifier")
00354    "\0"
00355 #define AUTH_FAILED_IDX            (AUTH_INVALIDRESP_IDX \
00356                              + sizeof "Invalid server verifier")
00357    N_("Failed (unspecified error)")
00358 };
00359 
00360 static const struct auth_errtab auth_errlist[] =
00361 {
00362   { AUTH_OK, AUTH_OK_IDX },
00363   { AUTH_BADCRED, AUTH_BADCRED_IDX },
00364   { AUTH_REJECTEDCRED, AUTH_REJECTEDCRED_IDX },
00365   { AUTH_BADVERF, AUTH_BADVERF_IDX },
00366   { AUTH_REJECTEDVERF, AUTH_REJECTEDVERF_IDX },
00367   { AUTH_TOOWEAK, AUTH_TOOWEAK_IDX },
00368   { AUTH_INVALIDRESP, AUTH_INVALIDRESP_IDX },
00369   { AUTH_FAILED, AUTH_FAILED_IDX }
00370 };
00371 
00372 static char *
00373 internal_function
00374 auth_errmsg (enum auth_stat stat)
00375 {
00376   size_t i;
00377 
00378   for (i = 0; i < sizeof (auth_errlist) / sizeof (struct auth_errtab); i++)
00379     {
00380       if (auth_errlist[i].status == stat)
00381        {
00382          return _(auth_errstr + auth_errlist[i].message_off);
00383        }
00384     }
00385   return NULL;
00386 }
00387 
00388 
00389 libc_freeres_fn (free_mem)
00390 {
00391   /* Not libc_freeres_ptr, since buf is a macro.  */
00392   free (buf);
00393 }