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