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