1 /* $OpenBSD: clnt_perror.c,v 1.22 2009/06/03 03:34:00 schwarze 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[CLNT_PERROR_BUFLEN]; 49 50 /* 51 * Print reply error info 52 */ 53 char * 54 clnt_sperror(CLIENT *rpch, char *s) 55 { 56 char *err, *str = buf; 57 struct rpc_err e; 58 int ret, len = CLNT_PERROR_BUFLEN; 59 60 CLNT_GETERR(rpch, &e); 61 62 ret = snprintf(str, len, "%s: %s", s, clnt_sperrno(e.re_status)); 63 if (ret == -1) 64 ret = 0; 65 else if (ret >= len) 66 goto truncated; 67 str += ret; 68 len -= ret; 69 70 switch (e.re_status) { 71 case RPC_SUCCESS: 72 case RPC_CANTENCODEARGS: 73 case RPC_CANTDECODERES: 74 case RPC_TIMEDOUT: 75 case RPC_PROGUNAVAIL: 76 case RPC_PROCUNAVAIL: 77 case RPC_CANTDECODEARGS: 78 case RPC_SYSTEMERROR: 79 case RPC_UNKNOWNHOST: 80 case RPC_UNKNOWNPROTO: 81 case RPC_PMAPFAILURE: 82 case RPC_PROGNOTREGISTERED: 83 case RPC_FAILED: 84 break; 85 86 case RPC_CANTSEND: 87 case RPC_CANTRECV: 88 ret = snprintf(str, len, "; errno = %s", strerror(e.re_errno)); 89 if (ret == -1 || ret >= len) 90 goto truncated; 91 break; 92 93 case RPC_VERSMISMATCH: 94 ret = snprintf(str, len, 95 "; low version = %u, high version = %u", 96 e.re_vers.low, e.re_vers.high); 97 if (ret == -1 || ret >= len) 98 goto truncated; 99 break; 100 101 case RPC_AUTHERROR: 102 ret = snprintf(str, len, "; why = "); 103 if (ret == -1) 104 ret = 0; 105 else if (ret >= len) 106 goto truncated; 107 str += ret; 108 len -= ret; 109 err = auth_errmsg(e.re_why); 110 if (err != NULL) { 111 ret = snprintf(str, len, "%s", err); 112 if (ret == -1 || ret >= len) 113 goto truncated; 114 } else { 115 ret = snprintf(str, len, 116 "(unknown authentication error - %d)", 117 (int) e.re_why); 118 if (ret == -1 || ret >= len) 119 goto truncated; 120 } 121 break; 122 123 case RPC_PROGVERSMISMATCH: 124 ret = snprintf(str, len, 125 "; low version = %u, high version = %u", 126 e.re_vers.low, e.re_vers.high); 127 if (ret == -1 || ret >= len) 128 goto truncated; 129 break; 130 131 default: /* unknown */ 132 ret = snprintf(str, len, "; s1 = %u, s2 = %u", 133 e.re_lb.s1, e.re_lb.s2); 134 if (ret == -1 || ret >= len) 135 goto truncated; 136 break; 137 } 138 if (strlcat(buf, "\n", CLNT_PERROR_BUFLEN) >= CLNT_PERROR_BUFLEN) 139 goto truncated; 140 return (buf); 141 142 truncated: 143 snprintf(buf + CLNT_PERROR_BUFLEN - 5, 5, "...\n"); 144 return (buf); 145 } 146 147 void 148 clnt_perror(CLIENT *rpch, char *s) 149 { 150 (void) fprintf(stderr, "%s", clnt_sperror(rpch, s)); 151 } 152 153 static const char *const rpc_errlist[] = { 154 "RPC: Success", /* 0 - RPC_SUCCESS */ 155 "RPC: Can't encode arguments", /* 1 - RPC_CANTENCODEARGS */ 156 "RPC: Can't decode result", /* 2 - RPC_CANTDECODERES */ 157 "RPC: Unable to send", /* 3 - RPC_CANTSEND */ 158 "RPC: Unable to receive", /* 4 - RPC_CANTRECV */ 159 "RPC: Timed out", /* 5 - RPC_TIMEDOUT */ 160 "RPC: Incompatible versions of RPC", /* 6 - RPC_VERSMISMATCH */ 161 "RPC: Authentication error", /* 7 - RPC_AUTHERROR */ 162 "RPC: Program unavailable", /* 8 - RPC_PROGUNAVAIL */ 163 "RPC: Program/version mismatch", /* 9 - RPC_PROGVERSMISMATCH */ 164 "RPC: Procedure unavailable", /* 10 - RPC_PROCUNAVAIL */ 165 "RPC: Server can't decode arguments", /* 11 - RPC_CANTDECODEARGS */ 166 "RPC: Remote system error", /* 12 - RPC_SYSTEMERROR */ 167 "RPC: Unknown host", /* 13 - RPC_UNKNOWNHOST */ 168 "RPC: Port mapper failure", /* 14 - RPC_PMAPFAILURE */ 169 "RPC: Program not registered", /* 15 - RPC_PROGNOTREGISTERED */ 170 "RPC: Failed (unspecified error)", /* 16 - RPC_FAILED */ 171 "RPC: Unknown protocol" /* 17 - RPC_UNKNOWNPROTO */ 172 }; 173 174 175 /* 176 * This interface for use by clntrpc 177 */ 178 char * 179 clnt_sperrno(enum clnt_stat stat) 180 { 181 unsigned int errnum = stat; 182 183 if (errnum < (sizeof(rpc_errlist)/sizeof(rpc_errlist[0]))) 184 return (char *)rpc_errlist[errnum]; 185 186 return ("RPC: (unknown error code)"); 187 } 188 189 void 190 clnt_perrno(enum clnt_stat num) 191 { 192 (void) fprintf(stderr, "%s\n", clnt_sperrno(num)); 193 } 194 195 196 char * 197 clnt_spcreateerror(char *s) 198 { 199 switch (rpc_createerr.cf_stat) { 200 case RPC_PMAPFAILURE: 201 (void) snprintf(buf, CLNT_PERROR_BUFLEN, "%s: %s - %s\n", s, 202 clnt_sperrno(rpc_createerr.cf_stat), 203 clnt_sperrno(rpc_createerr.cf_error.re_status)); 204 break; 205 206 case RPC_SYSTEMERROR: 207 (void) snprintf(buf, CLNT_PERROR_BUFLEN, "%s: %s - %s\n", s, 208 clnt_sperrno(rpc_createerr.cf_stat), 209 strerror(rpc_createerr.cf_error.re_errno)); 210 break; 211 212 default: 213 (void) snprintf(buf, CLNT_PERROR_BUFLEN, "%s: %s\n", s, 214 clnt_sperrno(rpc_createerr.cf_stat)); 215 break; 216 } 217 buf[CLNT_PERROR_BUFLEN-2] = '\n'; 218 buf[CLNT_PERROR_BUFLEN-1] = '\0'; 219 return (buf); 220 } 221 222 void 223 clnt_pcreateerror(char *s) 224 { 225 fprintf(stderr, "%s", clnt_spcreateerror(s)); 226 } 227 228 static const char *const auth_errlist[] = { 229 "Authentication OK", /* 0 - AUTH_OK */ 230 "Invalid client credential", /* 1 - AUTH_BADCRED */ 231 "Server rejected credential", /* 2 - AUTH_REJECTEDCRED */ 232 "Invalid client verifier", /* 3 - AUTH_BADVERF */ 233 "Server rejected verifier", /* 4 - AUTH_REJECTEDVERF */ 234 "Client credential too weak", /* 5 - AUTH_TOOWEAK */ 235 "Invalid server verifier", /* 6 - AUTH_INVALIDRESP */ 236 "Failed (unspecified error)" /* 7 - AUTH_FAILED */ 237 }; 238 239 static char * 240 auth_errmsg(enum auth_stat stat) 241 { 242 unsigned int errnum = stat; 243 244 if (errnum < (sizeof(auth_errlist)/sizeof(auth_errlist[0]))) 245 return (char *)auth_errlist[errnum]; 246 247 return (NULL); 248 } 249