1 /* $OpenBSD: clnt_perror.c,v 1.19 2006/09/17 17:00:38 thib Exp $ */ 2 /* 3 * Sun RPC is a product of Sun Microsystems, Inc. and is provided for 4 * unrestricted use provided that this legend is included on all tape 5 * media and as a part of the software program in whole or part. Users 6 * may copy or modify Sun RPC without charge, but are not authorized 7 * to license or distribute it to anyone else except as part of a product or 8 * program developed by the user. 9 * 10 * SUN RPC IS PROVIDED AS IS WITH NO WARRANTIES OF ANY KIND INCLUDING THE 11 * WARRANTIES OF DESIGN, MERCHANTIBILITY AND FITNESS FOR A PARTICULAR 12 * PURPOSE, OR ARISING FROM A COURSE OF DEALING, USAGE OR TRADE PRACTICE. 13 * 14 * Sun RPC is provided with no support and without any obligation on the 15 * part of Sun Microsystems, Inc. to assist in its use, correction, 16 * modification or enhancement. 17 * 18 * SUN MICROSYSTEMS, INC. SHALL HAVE NO LIABILITY WITH RESPECT TO THE 19 * INFRINGEMENT OF COPYRIGHTS, TRADE SECRETS OR ANY PATENTS BY SUN RPC 20 * OR ANY PART THEREOF. 21 * 22 * In no event will Sun Microsystems, Inc. be liable for any lost revenue 23 * or profits or other special, indirect and consequential damages, even if 24 * Sun has been advised of the possibility of such damages. 25 * 26 * Sun Microsystems, Inc. 27 * 2550 Garcia Avenue 28 * Mountain View, California 94043 29 */ 30 31 /* 32 * clnt_perror.c 33 * 34 * Copyright (C) 1984, Sun Microsystems, Inc. 35 * 36 */ 37 #include <stdio.h> 38 #include <stdlib.h> 39 #include <string.h> 40 #include <rpc/rpc.h> 41 #include <rpc/types.h> 42 #include <rpc/auth.h> 43 #include <rpc/clnt.h> 44 45 static char *auth_errmsg(enum auth_stat stat); 46 #define CLNT_PERROR_BUFLEN 256 47 48 static char *buf; 49 50 static char * 51 _buf(void) 52 { 53 54 if (buf == NULL) 55 buf = (char *)malloc(CLNT_PERROR_BUFLEN); 56 return (buf); 57 } 58 59 /* 60 * Print reply error info 61 */ 62 char * 63 clnt_sperror(CLIENT *rpch, char *s) 64 { 65 char *err, *str = _buf(), *strstart; 66 struct rpc_err e; 67 int ret, len = CLNT_PERROR_BUFLEN; 68 69 strstart = str; 70 if (str == NULL) 71 return (0); 72 CLNT_GETERR(rpch, &e); 73 74 ret = snprintf(str, len, "%s: %s", s, clnt_sperrno(e.re_status)); 75 if (ret == -1) 76 ret = 0; 77 else if (ret >= len) 78 ret = len; 79 str += ret; 80 len -= ret; 81 if (str > strstart + CLNT_PERROR_BUFLEN) 82 goto truncated; 83 84 switch (e.re_status) { 85 case RPC_SUCCESS: 86 case RPC_CANTENCODEARGS: 87 case RPC_CANTDECODERES: 88 case RPC_TIMEDOUT: 89 case RPC_PROGUNAVAIL: 90 case RPC_PROCUNAVAIL: 91 case RPC_CANTDECODEARGS: 92 case RPC_SYSTEMERROR: 93 case RPC_UNKNOWNHOST: 94 case RPC_UNKNOWNPROTO: 95 case RPC_PMAPFAILURE: 96 case RPC_PROGNOTREGISTERED: 97 case RPC_FAILED: 98 break; 99 100 case RPC_CANTSEND: 101 case RPC_CANTRECV: 102 snprintf(str, len, "; errno = %s", strerror(e.re_errno)); 103 break; 104 105 case RPC_VERSMISMATCH: 106 snprintf(str, len, "; low version = %u, high version = %u", 107 e.re_vers.low, e.re_vers.high); 108 break; 109 110 case RPC_AUTHERROR: 111 ret = snprintf(str, len, "; why = "); 112 if (ret == -1) 113 ret = 0; 114 else if (ret >= len) 115 ret = len; 116 str += ret; 117 len -= ret; 118 if (str > strstart + CLNT_PERROR_BUFLEN) 119 goto truncated; 120 err = auth_errmsg(e.re_why); 121 if (err != NULL) { 122 snprintf(str, len, "%s", err); 123 } else { 124 snprintf(str, len, 125 "(unknown authentication error - %d)", 126 (int) e.re_why); 127 } 128 break; 129 130 case RPC_PROGVERSMISMATCH: 131 snprintf(str, len, "; low version = %u, high version = %u", 132 e.re_vers.low, e.re_vers.high); 133 break; 134 135 default: /* unknown */ 136 snprintf(str, len, "; s1 = %u, s2 = %u", 137 e.re_lb.s1, e.re_lb.s2); 138 break; 139 } 140 strstart[CLNT_PERROR_BUFLEN-2] = '\0'; 141 strlcat(strstart, "\n", CLNT_PERROR_BUFLEN); 142 return (strstart); 143 144 truncated: 145 snprintf(strstart + CLNT_PERROR_BUFLEN - 5, 5, "...\n"); 146 return (strstart); 147 } 148 149 void 150 clnt_perror(CLIENT *rpch, char *s) 151 { 152 (void) fprintf(stderr, "%s", clnt_sperror(rpch, s)); 153 } 154 155 static const char *const rpc_errlist[] = { 156 "RPC: Success", /* 0 - RPC_SUCCESS */ 157 "RPC: Can't encode arguments", /* 1 - RPC_CANTENCODEARGS */ 158 "RPC: Can't decode result", /* 2 - RPC_CANTDECODERES */ 159 "RPC: Unable to send", /* 3 - RPC_CANTSEND */ 160 "RPC: Unable to receive", /* 4 - RPC_CANTRECV */ 161 "RPC: Timed out", /* 5 - RPC_TIMEDOUT */ 162 "RPC: Incompatible versions of RPC", /* 6 - RPC_VERSMISMATCH */ 163 "RPC: Authentication error", /* 7 - RPC_AUTHERROR */ 164 "RPC: Program unavailable", /* 8 - RPC_PROGUNAVAIL */ 165 "RPC: Program/version mismatch", /* 9 - RPC_PROGVERSMISMATCH */ 166 "RPC: Procedure unavailable", /* 10 - RPC_PROCUNAVAIL */ 167 "RPC: Server can't decode arguments", /* 11 - RPC_CANTDECODEARGS */ 168 "RPC: Remote system error", /* 12 - RPC_SYSTEMERROR */ 169 "RPC: Unknown host", /* 13 - RPC_UNKNOWNHOST */ 170 "RPC: Port mapper failure", /* 14 - RPC_PMAPFAILURE */ 171 "RPC: Program not registered", /* 15 - RPC_PROGNOTREGISTERED */ 172 "RPC: Failed (unspecified error)", /* 16 - RPC_FAILED */ 173 "RPC: Unknown protocol" /* 17 - RPC_UNKNOWNPROTO */ 174 }; 175 176 177 /* 178 * This interface for use by clntrpc 179 */ 180 char * 181 clnt_sperrno(enum clnt_stat stat) 182 { 183 unsigned int errnum = stat; 184 185 if (errnum < (sizeof(rpc_errlist)/sizeof(rpc_errlist[0]))) 186 return (char *)rpc_errlist[errnum]; 187 188 return ("RPC: (unknown error code)"); 189 } 190 191 void 192 clnt_perrno(enum clnt_stat num) 193 { 194 (void) fprintf(stderr, "%s\n", clnt_sperrno(num)); 195 } 196 197 198 char * 199 clnt_spcreateerror(char *s) 200 { 201 char *str = _buf(); 202 203 if (str == NULL) 204 return (0); 205 206 switch (rpc_createerr.cf_stat) { 207 case RPC_PMAPFAILURE: 208 (void) snprintf(str, CLNT_PERROR_BUFLEN, "%s: %s - %s\n", s, 209 clnt_sperrno(rpc_createerr.cf_stat), 210 clnt_sperrno(rpc_createerr.cf_error.re_status)); 211 break; 212 213 case RPC_SYSTEMERROR: 214 (void) snprintf(str, CLNT_PERROR_BUFLEN, "%s: %s - %s\n", s, 215 clnt_sperrno(rpc_createerr.cf_stat), 216 strerror(rpc_createerr.cf_error.re_errno)); 217 break; 218 219 default: 220 (void) snprintf(str, CLNT_PERROR_BUFLEN, "%s: %s\n", s, 221 clnt_sperrno(rpc_createerr.cf_stat)); 222 break; 223 } 224 str[CLNT_PERROR_BUFLEN-2] = '\n'; 225 str[CLNT_PERROR_BUFLEN-1] = '\0'; 226 return (str); 227 } 228 229 void 230 clnt_pcreateerror(char *s) 231 { 232 (void) fprintf(stderr, "%s", clnt_spcreateerror(s)); 233 } 234 235 static const char *const auth_errlist[] = { 236 "Authentication OK", /* 0 - AUTH_OK */ 237 "Invalid client credential", /* 1 - AUTH_BADCRED */ 238 "Server rejected credential", /* 2 - AUTH_REJECTEDCRED */ 239 "Invalid client verifier", /* 3 - AUTH_BADVERF */ 240 "Server rejected verifier", /* 4 - AUTH_REJECTEDVERF */ 241 "Client credential too weak", /* 5 - AUTH_TOOWEAK */ 242 "Invalid server verifier", /* 6 - AUTH_INVALIDRESP */ 243 "Failed (unspecified error)" /* 7 - AUTH_FAILED */ 244 }; 245 246 static char * 247 auth_errmsg(enum auth_stat stat) 248 { 249 unsigned int errnum = stat; 250 251 if (errnum < (sizeof(auth_errlist)/sizeof(auth_errlist[0]))) 252 return (char *)auth_errlist[errnum]; 253 254 return (NULL); 255 } 256