Path: blob/main/crypto/krb5/src/lib/rpc/clnt_perror.c
39536 views
/* @(#)clnt_perror.c 2.1 88/07/29 4.0 RPCSRC */1/*2* Copyright (c) 2010, Oracle America, Inc.3*4* All rights reserved.5*6* Redistribution and use in source and binary forms, with or without7* modification, are permitted provided that the following conditions are met:8*9* * Redistributions of source code must retain the above copyright10* notice, this list of conditions and the following disclaimer.11*12* * Redistributions in binary form must reproduce the above copyright13* notice, this list of conditions and the following disclaimer in14* the documentation and/or other materials provided with the15* distribution.16*17* * Neither the name of the "Oracle America, Inc." nor the names of18* its contributors may be used to endorse or promote products19* derived from this software without specific prior written permission.20*21* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS22* IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED23* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A24* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT25* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,26* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED27* TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR28* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF29* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING30* NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS31* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.32*/33#if !defined(lint) && defined(SCCSIDS)34static char sccsid[] = "@(#)clnt_perror.c 1.15 87/10/07 Copyr 1984 Sun Micro";35#endif3637/*38* clnt_perror.c39*/40#include "autoconf.h"41#include <stdio.h>42#include <string.h>43#include <errno.h>4445#include <gssrpc/types.h>46#include <gssrpc/auth.h>47#include <gssrpc/clnt.h>4849#ifndef HAVE_STRERROR50#ifdef NEED_SYS_ERRLIST51extern char *sys_errlist[];52#endif53extern int sys_nerr;54#undef strerror55#define strerror(N) (((N) > 0 && (N) < sys_nerr) ? sys_errlist[N] : (char *)0)56#endif /* HAVE_STRERROR */57static char *auth_errmsg(enum auth_stat);58596061static char *buf;6263static char *64get_buf(void)65{66if (buf == NULL)67buf = (char *)malloc(BUFSIZ);68return (buf);69}7071/*72* Print reply error info73*/74char *75clnt_sperror(CLIENT *rpch, char *s)76{77struct rpc_err e;78char *err;79char *bufstart = get_buf();80char *str = bufstart;81char *strstart = str;82char *strend;8384if (str == 0)85return (0);86strend = str + BUFSIZ;87CLNT_GETERR(rpch, &e);8889strncpy (str, s, BUFSIZ - 1);90str[BUFSIZ - 1] = 0;91strncat (str, ": ", BUFSIZ - 1 - strlen (bufstart));92str += strlen(str);93strncat (str, clnt_sperrno(e.re_status), BUFSIZ - 1 - strlen (bufstart));94strstart[BUFSIZ - 1] = '\0';95str += strlen(str);9697switch (e.re_status) {98case RPC_SUCCESS:99case RPC_CANTENCODEARGS:100case RPC_CANTDECODERES:101case RPC_TIMEDOUT:102case RPC_PROGUNAVAIL:103case RPC_PROCUNAVAIL:104case RPC_CANTDECODEARGS:105case RPC_SYSTEMERROR:106case RPC_UNKNOWNHOST:107case RPC_UNKNOWNPROTO:108case RPC_PMAPFAILURE:109case RPC_PROGNOTREGISTERED:110case RPC_FAILED:111break;112113case RPC_CANTSEND:114case RPC_CANTRECV:115/* 10 for the string */116if (str - bufstart + 10 + strlen(strerror(e.re_errno)) < BUFSIZ)117(void) snprintf(str, strend-str, "; errno = %s",118strerror(e.re_errno));119str += strlen(str);120break;121122case RPC_VERSMISMATCH:123/* 33 for the string, 22 for the numbers */124if(str - bufstart + 33 + 22 < BUFSIZ)125(void) snprintf(str, strend-str,126"; low version = %lu, high version = %lu",127(u_long) e.re_vers.low,128(u_long) e.re_vers.high);129str += strlen(str);130break;131132case RPC_AUTHERROR:133err = auth_errmsg(e.re_why);134/* 8 for the string */135if(str - bufstart + 8 < BUFSIZ)136(void) snprintf(str, strend-str, "; why = ");137str += strlen(str);138if (err != NULL) {139if(str - bufstart + strlen(err) < BUFSIZ)140(void) snprintf(str, strend-str, "%s",err);141} else {142/* 33 for the string, 11 for the number */143if(str - bufstart + 33 + 11 < BUFSIZ)144(void) snprintf(str, strend-str,145"(unknown authentication error - %d)",146(int) e.re_why);147}148str += strlen(str);149break;150151case RPC_PROGVERSMISMATCH:152/* 33 for the string, 22 for the numbers */153if(str - bufstart + 33 + 22 < BUFSIZ)154(void) snprintf(str, strend-str,155"; low version = %lu, high version = %lu",156(u_long) e.re_vers.low,157(u_long) e.re_vers.high);158str += strlen(str);159break;160161default: /* unknown */162/* 14 for the string, 22 for the numbers */163if(str - bufstart + 14 + 22 < BUFSIZ)164(void) snprintf(str, strend-str,165"; s1 = %lu, s2 = %lu",166(u_long) e.re_lb.s1,167(u_long) e.re_lb.s2);168str += strlen(str);169break;170}171if (str - bufstart + 1 < BUFSIZ)172(void) snprintf(str, strend-str, "\n");173return(strstart) ;174}175176void177clnt_perror(CLIENT *rpch, char *s)178{179(void) fprintf(stderr,"%s",clnt_sperror(rpch,s));180}181182183struct rpc_errtab {184enum clnt_stat status;185char *message;186};187188static struct rpc_errtab rpc_errlist[] = {189{ RPC_SUCCESS,190"RPC: Success" },191{ RPC_CANTENCODEARGS,192"RPC: Can't encode arguments" },193{ RPC_CANTDECODERES,194"RPC: Can't decode result" },195{ RPC_CANTSEND,196"RPC: Unable to send" },197{ RPC_CANTRECV,198"RPC: Unable to receive" },199{ RPC_TIMEDOUT,200"RPC: Timed out" },201{ RPC_VERSMISMATCH,202"RPC: Incompatible versions of RPC" },203{ RPC_AUTHERROR,204"RPC: Authentication error" },205{ RPC_PROGUNAVAIL,206"RPC: Program unavailable" },207{ RPC_PROGVERSMISMATCH,208"RPC: Program/version mismatch" },209{ RPC_PROCUNAVAIL,210"RPC: Procedure unavailable" },211{ RPC_CANTDECODEARGS,212"RPC: Server can't decode arguments" },213{ RPC_SYSTEMERROR,214"RPC: Remote system error" },215{ RPC_UNKNOWNHOST,216"RPC: Unknown host" },217{ RPC_UNKNOWNPROTO,218"RPC: Unknown protocol" },219{ RPC_PMAPFAILURE,220"RPC: Port mapper failure" },221{ RPC_PROGNOTREGISTERED,222"RPC: Program not registered"},223{ RPC_FAILED,224"RPC: Failed (unspecified error)"}225};226227228/*229* This interface for use by clntrpc230*/231char *232clnt_sperrno(enum clnt_stat stat)233{234unsigned int i;235236for (i = 0; i < sizeof(rpc_errlist)/sizeof(struct rpc_errtab); i++) {237if (rpc_errlist[i].status == stat) {238return (rpc_errlist[i].message);239}240}241return ("RPC: (unknown error code)");242}243244void245clnt_perrno(enum clnt_stat num)246{247(void) fprintf(stderr,"%s",clnt_sperrno(num));248}249250251char *252clnt_spcreateerror(char *s)253{254char *str = get_buf();255char *strend;256257if (str == 0)258return(0);259strend = str+BUFSIZ;260(void) snprintf(str, strend-str, "%s: ", s);261str[BUFSIZ - 1] = '\0';262(void) strncat(str, clnt_sperrno(rpc_createerr.cf_stat), BUFSIZ - 1);263switch (rpc_createerr.cf_stat) {264case RPC_PMAPFAILURE:265(void) strncat(str, " - ", BUFSIZ - 1 - strlen(str));266(void) strncat(str,267clnt_sperrno(rpc_createerr.cf_error.re_status),268BUFSIZ - 1 - strlen(str));269break;270271case RPC_SYSTEMERROR:272(void) strncat(str, " - ", BUFSIZ - 1 - strlen(str));273{274const char *m = strerror(rpc_createerr.cf_error.re_errno);275if (m)276(void) strncat(str, m, BUFSIZ - 1 - strlen(str));277else278(void) snprintf(&str[strlen(str)], BUFSIZ - strlen(str),279"Error %d",280rpc_createerr.cf_error.re_errno);281}282break;283284case RPC_CANTSEND:285case RPC_CANTDECODERES:286case RPC_CANTENCODEARGS:287case RPC_SUCCESS:288case RPC_UNKNOWNPROTO:289case RPC_PROGNOTREGISTERED:290case RPC_FAILED:291case RPC_UNKNOWNHOST:292case RPC_CANTDECODEARGS:293case RPC_PROCUNAVAIL:294case RPC_PROGVERSMISMATCH:295case RPC_PROGUNAVAIL:296case RPC_AUTHERROR:297case RPC_VERSMISMATCH:298case RPC_TIMEDOUT:299case RPC_CANTRECV:300default:301break;302}303(void) strncat(str, "\n", BUFSIZ - 1 - strlen(str));304return (str);305}306307void308clnt_pcreateerror(char *s)309{310(void) fprintf(stderr,"%s",clnt_spcreateerror(s));311}312313struct auth_errtab {314enum auth_stat status;315char *message;316};317318static struct auth_errtab auth_errlist[] = {319{ AUTH_OK,320"Authentication OK" },321{ AUTH_BADCRED,322"Invalid client credential" },323{ AUTH_REJECTEDCRED,324"Server rejected credential" },325{ AUTH_BADVERF,326"Invalid client verifier" },327{ AUTH_REJECTEDVERF,328"Server rejected verifier" },329{ AUTH_TOOWEAK,330"Client credential too weak" },331{ AUTH_INVALIDRESP,332"Invalid server verifier" },333{ AUTH_FAILED,334"Failed (unspecified error)" },335};336337static char *338auth_errmsg(enum auth_stat stat)339{340unsigned int i;341342for (i = 0; i < sizeof(auth_errlist)/sizeof(struct auth_errtab); i++) {343if (auth_errlist[i].status == stat) {344return(auth_errlist[i].message);345}346}347return(NULL);348}349350351