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