lh | 9ed821d | 2023-04-07 01:36:19 -0700 | [diff] [blame^] | 1 | /* |
| 2 | * clnt_perror.c |
| 3 | * |
| 4 | * Copyright (c) 2010, 2011, Oracle America, Inc. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions are |
| 8 | * met: |
| 9 | * |
| 10 | * * Redistributions of source code must retain the above copyright |
| 11 | * notice, this list of conditions and the following disclaimer. |
| 12 | * * Redistributions in binary form must reproduce the above |
| 13 | * copyright notice, this list of conditions and the following |
| 14 | * disclaimer in the documentation and/or other materials |
| 15 | * provided with the distribution. |
| 16 | * * Neither the name of the "Oracle America, Inc." nor the names of its |
| 17 | * contributors may be used to endorse or promote products derived |
| 18 | * from this software without specific prior written permission. |
| 19 | * |
| 20 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS |
| 23 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE |
| 24 | * COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 25 | * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 26 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE |
| 27 | * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 28 | * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, |
| 29 | * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING |
| 30 | * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 31 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 32 | */ |
| 33 | #include <stdio.h> |
| 34 | #include <string.h> |
| 35 | #include <libintl.h> |
| 36 | #include <rpc/rpc.h> |
| 37 | #include <wchar.h> |
| 38 | #include <libio/iolibio.h> |
| 39 | |
| 40 | static char *auth_errmsg (enum auth_stat stat) internal_function; |
| 41 | |
| 42 | #ifdef _RPC_THREAD_SAFE_ |
| 43 | /* |
| 44 | * Making buf a preprocessor macro requires renaming the local |
| 45 | * buf variable in a few functions. Overriding a global variable |
| 46 | * with a local variable of the same name is a bad idea, anyway. |
| 47 | */ |
| 48 | #define buf RPC_THREAD_VARIABLE(clnt_perr_buf_s) |
| 49 | #else |
| 50 | static char *buf; |
| 51 | #endif |
| 52 | |
| 53 | /* |
| 54 | * Print reply error info |
| 55 | */ |
| 56 | char * |
| 57 | clnt_sperror (CLIENT * rpch, const char *msg) |
| 58 | { |
| 59 | struct rpc_err e; |
| 60 | CLNT_GETERR (rpch, &e); |
| 61 | |
| 62 | const char *errstr = clnt_sperrno (e.re_status); |
| 63 | |
| 64 | char chrbuf[1024]; |
| 65 | char *str; |
| 66 | char *tmpstr; |
| 67 | int res; |
| 68 | switch (e.re_status) |
| 69 | { |
| 70 | case RPC_SUCCESS: |
| 71 | case RPC_CANTENCODEARGS: |
| 72 | case RPC_CANTDECODERES: |
| 73 | case RPC_TIMEDOUT: |
| 74 | case RPC_PROGUNAVAIL: |
| 75 | case RPC_PROCUNAVAIL: |
| 76 | case RPC_CANTDECODEARGS: |
| 77 | case RPC_SYSTEMERROR: |
| 78 | case RPC_UNKNOWNHOST: |
| 79 | case RPC_UNKNOWNPROTO: |
| 80 | case RPC_PMAPFAILURE: |
| 81 | case RPC_PROGNOTREGISTERED: |
| 82 | case RPC_FAILED: |
| 83 | res = __asprintf (&str, "%s: %s\n", msg, errstr); |
| 84 | break; |
| 85 | |
| 86 | case RPC_CANTSEND: |
| 87 | case RPC_CANTRECV: |
| 88 | res = __asprintf (&str, "%s: %s; errno = %s\n", |
| 89 | msg, errstr, __strerror_r (e.re_errno, |
| 90 | chrbuf, sizeof chrbuf)); |
| 91 | break; |
| 92 | |
| 93 | case RPC_VERSMISMATCH: |
| 94 | res = __asprintf (&str, |
| 95 | _("%s: %s; low version = %lu, high version = %lu"), |
| 96 | msg, errstr, e.re_vers.low, e.re_vers.high); |
| 97 | break; |
| 98 | |
| 99 | case RPC_AUTHERROR: |
| 100 | tmpstr = auth_errmsg (e.re_why); |
| 101 | if (tmpstr != NULL) |
| 102 | res = __asprintf (&str, _("%s: %s; why = %s\n"), msg, errstr, tmpstr); |
| 103 | else |
| 104 | res = __asprintf (&str, _("\ |
| 105 | %s: %s; why = (unknown authentication error - %d)\n"), |
| 106 | msg, errstr, (int) e.re_why); |
| 107 | break; |
| 108 | |
| 109 | case RPC_PROGVERSMISMATCH: |
| 110 | res = __asprintf (&str, |
| 111 | _("%s: %s; low version = %lu, high version = %lu"), |
| 112 | msg, errstr, e.re_vers.low, e.re_vers.high); |
| 113 | break; |
| 114 | |
| 115 | default: /* unknown */ |
| 116 | res = __asprintf (&str, "%s: %s; s1 = %lu, s2 = %lu", |
| 117 | msg, errstr, e.re_lb.s1, e.re_lb.s2); |
| 118 | break; |
| 119 | } |
| 120 | |
| 121 | if (res < 0) |
| 122 | return NULL; |
| 123 | |
| 124 | char *oldbuf = buf; |
| 125 | buf = str; |
| 126 | free (oldbuf); |
| 127 | |
| 128 | return str; |
| 129 | } |
| 130 | libc_hidden_nolink_sunrpc (clnt_sperror, GLIBC_2_0) |
| 131 | |
| 132 | void |
| 133 | clnt_perror (CLIENT * rpch, const char *msg) |
| 134 | { |
| 135 | (void) __fxprintf (NULL, "%s", clnt_sperror (rpch, msg)); |
| 136 | } |
| 137 | #ifdef EXPORT_RPC_SYMBOLS |
| 138 | libc_hidden_def (clnt_perror) |
| 139 | #else |
| 140 | libc_hidden_nolink_sunrpc (clnt_perror, GLIBC_2_0) |
| 141 | #endif |
| 142 | |
| 143 | |
| 144 | struct rpc_errtab |
| 145 | { |
| 146 | enum clnt_stat status; |
| 147 | unsigned int message_off; |
| 148 | }; |
| 149 | |
| 150 | static const char rpc_errstr[] = |
| 151 | { |
| 152 | #define RPC_SUCCESS_IDX 0 |
| 153 | N_("RPC: Success") |
| 154 | "\0" |
| 155 | #define RPC_CANTENCODEARGS_IDX (RPC_SUCCESS_IDX + sizeof "RPC: Success") |
| 156 | N_("RPC: Can't encode arguments") |
| 157 | "\0" |
| 158 | #define RPC_CANTDECODERES_IDX (RPC_CANTENCODEARGS_IDX \ |
| 159 | + sizeof "RPC: Can't encode arguments") |
| 160 | N_("RPC: Can't decode result") |
| 161 | "\0" |
| 162 | #define RPC_CANTSEND_IDX (RPC_CANTDECODERES_IDX \ |
| 163 | + sizeof "RPC: Can't decode result") |
| 164 | N_("RPC: Unable to send") |
| 165 | "\0" |
| 166 | #define RPC_CANTRECV_IDX (RPC_CANTSEND_IDX \ |
| 167 | + sizeof "RPC: Unable to send") |
| 168 | N_("RPC: Unable to receive") |
| 169 | "\0" |
| 170 | #define RPC_TIMEDOUT_IDX (RPC_CANTRECV_IDX \ |
| 171 | + sizeof "RPC: Unable to receive") |
| 172 | N_("RPC: Timed out") |
| 173 | "\0" |
| 174 | #define RPC_VERSMISMATCH_IDX (RPC_TIMEDOUT_IDX \ |
| 175 | + sizeof "RPC: Timed out") |
| 176 | N_("RPC: Incompatible versions of RPC") |
| 177 | "\0" |
| 178 | #define RPC_AUTHERROR_IDX (RPC_VERSMISMATCH_IDX \ |
| 179 | + sizeof "RPC: Incompatible versions of RPC") |
| 180 | N_("RPC: Authentication error") |
| 181 | "\0" |
| 182 | #define RPC_PROGUNAVAIL_IDX (RPC_AUTHERROR_IDX \ |
| 183 | + sizeof "RPC: Authentication error") |
| 184 | N_("RPC: Program unavailable") |
| 185 | "\0" |
| 186 | #define RPC_PROGVERSMISMATCH_IDX (RPC_PROGUNAVAIL_IDX \ |
| 187 | + sizeof "RPC: Program unavailable") |
| 188 | N_("RPC: Program/version mismatch") |
| 189 | "\0" |
| 190 | #define RPC_PROCUNAVAIL_IDX (RPC_PROGVERSMISMATCH_IDX \ |
| 191 | + sizeof "RPC: Program/version mismatch") |
| 192 | N_("RPC: Procedure unavailable") |
| 193 | "\0" |
| 194 | #define RPC_CANTDECODEARGS_IDX (RPC_PROCUNAVAIL_IDX \ |
| 195 | + sizeof "RPC: Procedure unavailable") |
| 196 | N_("RPC: Server can't decode arguments") |
| 197 | "\0" |
| 198 | #define RPC_SYSTEMERROR_IDX (RPC_CANTDECODEARGS_IDX \ |
| 199 | + sizeof "RPC: Server can't decode arguments") |
| 200 | N_("RPC: Remote system error") |
| 201 | "\0" |
| 202 | #define RPC_UNKNOWNHOST_IDX (RPC_SYSTEMERROR_IDX \ |
| 203 | + sizeof "RPC: Remote system error") |
| 204 | N_("RPC: Unknown host") |
| 205 | "\0" |
| 206 | #define RPC_UNKNOWNPROTO_IDX (RPC_UNKNOWNHOST_IDX \ |
| 207 | + sizeof "RPC: Unknown host") |
| 208 | N_("RPC: Unknown protocol") |
| 209 | "\0" |
| 210 | #define RPC_PMAPFAILURE_IDX (RPC_UNKNOWNPROTO_IDX \ |
| 211 | + sizeof "RPC: Unknown protocol") |
| 212 | N_("RPC: Port mapper failure") |
| 213 | "\0" |
| 214 | #define RPC_PROGNOTREGISTERED_IDX (RPC_PMAPFAILURE_IDX \ |
| 215 | + sizeof "RPC: Port mapper failure") |
| 216 | N_("RPC: Program not registered") |
| 217 | "\0" |
| 218 | #define RPC_FAILED_IDX (RPC_PROGNOTREGISTERED_IDX \ |
| 219 | + sizeof "RPC: Program not registered") |
| 220 | N_("RPC: Failed (unspecified error)") |
| 221 | }; |
| 222 | |
| 223 | static const struct rpc_errtab rpc_errlist[] = |
| 224 | { |
| 225 | { RPC_SUCCESS, RPC_SUCCESS_IDX }, |
| 226 | { RPC_CANTENCODEARGS, RPC_CANTENCODEARGS_IDX }, |
| 227 | { RPC_CANTDECODERES, RPC_CANTDECODERES_IDX }, |
| 228 | { RPC_CANTSEND, RPC_CANTSEND_IDX }, |
| 229 | { RPC_CANTRECV, RPC_CANTRECV_IDX }, |
| 230 | { RPC_TIMEDOUT, RPC_TIMEDOUT_IDX }, |
| 231 | { RPC_VERSMISMATCH, RPC_VERSMISMATCH_IDX }, |
| 232 | { RPC_AUTHERROR, RPC_AUTHERROR_IDX }, |
| 233 | { RPC_PROGUNAVAIL, RPC_PROGUNAVAIL_IDX }, |
| 234 | { RPC_PROGVERSMISMATCH, RPC_PROGVERSMISMATCH_IDX }, |
| 235 | { RPC_PROCUNAVAIL, RPC_PROCUNAVAIL_IDX }, |
| 236 | { RPC_CANTDECODEARGS, RPC_CANTDECODEARGS_IDX }, |
| 237 | { RPC_SYSTEMERROR, RPC_SYSTEMERROR_IDX }, |
| 238 | { RPC_UNKNOWNHOST, RPC_UNKNOWNHOST_IDX }, |
| 239 | { RPC_UNKNOWNPROTO, RPC_UNKNOWNPROTO_IDX }, |
| 240 | { RPC_PMAPFAILURE, RPC_PMAPFAILURE_IDX }, |
| 241 | { RPC_PROGNOTREGISTERED, RPC_PROGNOTREGISTERED_IDX }, |
| 242 | { RPC_FAILED, RPC_FAILED_IDX } |
| 243 | }; |
| 244 | |
| 245 | |
| 246 | /* |
| 247 | * This interface for use by clntrpc |
| 248 | */ |
| 249 | char * |
| 250 | clnt_sperrno (enum clnt_stat stat) |
| 251 | { |
| 252 | size_t i; |
| 253 | |
| 254 | for (i = 0; i < sizeof (rpc_errlist) / sizeof (struct rpc_errtab); i++) |
| 255 | { |
| 256 | if (rpc_errlist[i].status == stat) |
| 257 | { |
| 258 | return _(rpc_errstr + rpc_errlist[i].message_off); |
| 259 | } |
| 260 | } |
| 261 | return _("RPC: (unknown error code)"); |
| 262 | } |
| 263 | libc_hidden_def (clnt_sperrno) |
| 264 | |
| 265 | void |
| 266 | clnt_perrno (enum clnt_stat num) |
| 267 | { |
| 268 | (void) __fxprintf (NULL, "%s", clnt_sperrno (num)); |
| 269 | } |
| 270 | #ifdef EXPORT_RPC_SYMBOLS |
| 271 | libc_hidden_def (clnt_perrno) |
| 272 | #else |
| 273 | libc_hidden_nolink_sunrpc (clnt_perrno, GLIBC_2_0) |
| 274 | #endif |
| 275 | |
| 276 | char * |
| 277 | clnt_spcreateerror (const char *msg) |
| 278 | { |
| 279 | struct rpc_createerr *ce = &get_rpc_createerr (); |
| 280 | |
| 281 | char chrbuf[1024]; |
| 282 | const char *connector = ""; |
| 283 | const char *errstr = ""; |
| 284 | switch (ce->cf_stat) |
| 285 | { |
| 286 | case RPC_PMAPFAILURE: |
| 287 | connector = " - "; |
| 288 | errstr = clnt_sperrno (ce->cf_error.re_status); |
| 289 | break; |
| 290 | |
| 291 | case RPC_SYSTEMERROR: |
| 292 | connector = " - "; |
| 293 | errstr = __strerror_r (ce->cf_error.re_errno, chrbuf, sizeof chrbuf); |
| 294 | break; |
| 295 | |
| 296 | default: |
| 297 | break; |
| 298 | } |
| 299 | |
| 300 | char *str; |
| 301 | if (__asprintf (&str, "%s: %s%s%s\n", |
| 302 | msg, clnt_sperrno (ce->cf_stat), connector, errstr) < 0) |
| 303 | return NULL; |
| 304 | |
| 305 | char *oldbuf = buf; |
| 306 | buf = str; |
| 307 | free (oldbuf); |
| 308 | |
| 309 | return str; |
| 310 | } |
| 311 | libc_hidden_nolink_sunrpc (clnt_spcreateerror, GLIBC_2_0) |
| 312 | |
| 313 | void |
| 314 | clnt_pcreateerror (const char *msg) |
| 315 | { |
| 316 | (void) __fxprintf (NULL, "%s", clnt_spcreateerror (msg)); |
| 317 | } |
| 318 | #ifdef EXPORT_RPC_SYMBOLS |
| 319 | libc_hidden_def (clnt_pcreateerror) |
| 320 | #else |
| 321 | libc_hidden_nolink_sunrpc (clnt_pcreateerror, GLIBC_2_0) |
| 322 | #endif |
| 323 | |
| 324 | struct auth_errtab |
| 325 | { |
| 326 | enum auth_stat status; |
| 327 | unsigned int message_off; |
| 328 | }; |
| 329 | |
| 330 | static const char auth_errstr[] = |
| 331 | { |
| 332 | #define AUTH_OK_IDX 0 |
| 333 | N_("Authentication OK") |
| 334 | "\0" |
| 335 | #define AUTH_BADCRED_IDX (AUTH_OK_IDX + sizeof "Authentication OK") |
| 336 | N_("Invalid client credential") |
| 337 | "\0" |
| 338 | #define AUTH_REJECTEDCRED_IDX (AUTH_BADCRED_IDX \ |
| 339 | + sizeof "Invalid client credential") |
| 340 | N_("Server rejected credential") |
| 341 | "\0" |
| 342 | #define AUTH_BADVERF_IDX (AUTH_REJECTEDCRED_IDX \ |
| 343 | + sizeof "Server rejected credential") |
| 344 | N_("Invalid client verifier") |
| 345 | "\0" |
| 346 | #define AUTH_REJECTEDVERF_IDX (AUTH_BADVERF_IDX \ |
| 347 | + sizeof "Invalid client verifier") |
| 348 | N_("Server rejected verifier") |
| 349 | "\0" |
| 350 | #define AUTH_TOOWEAK_IDX (AUTH_REJECTEDVERF_IDX \ |
| 351 | + sizeof "Server rejected verifier") |
| 352 | N_("Client credential too weak") |
| 353 | "\0" |
| 354 | #define AUTH_INVALIDRESP_IDX (AUTH_TOOWEAK_IDX \ |
| 355 | + sizeof "Client credential too weak") |
| 356 | N_("Invalid server verifier") |
| 357 | "\0" |
| 358 | #define AUTH_FAILED_IDX (AUTH_INVALIDRESP_IDX \ |
| 359 | + sizeof "Invalid server verifier") |
| 360 | N_("Failed (unspecified error)") |
| 361 | }; |
| 362 | |
| 363 | static const struct auth_errtab auth_errlist[] = |
| 364 | { |
| 365 | { AUTH_OK, AUTH_OK_IDX }, |
| 366 | { AUTH_BADCRED, AUTH_BADCRED_IDX }, |
| 367 | { AUTH_REJECTEDCRED, AUTH_REJECTEDCRED_IDX }, |
| 368 | { AUTH_BADVERF, AUTH_BADVERF_IDX }, |
| 369 | { AUTH_REJECTEDVERF, AUTH_REJECTEDVERF_IDX }, |
| 370 | { AUTH_TOOWEAK, AUTH_TOOWEAK_IDX }, |
| 371 | { AUTH_INVALIDRESP, AUTH_INVALIDRESP_IDX }, |
| 372 | { AUTH_FAILED, AUTH_FAILED_IDX } |
| 373 | }; |
| 374 | |
| 375 | static char * |
| 376 | internal_function |
| 377 | auth_errmsg (enum auth_stat stat) |
| 378 | { |
| 379 | size_t i; |
| 380 | |
| 381 | for (i = 0; i < sizeof (auth_errlist) / sizeof (struct auth_errtab); i++) |
| 382 | { |
| 383 | if (auth_errlist[i].status == stat) |
| 384 | { |
| 385 | return _(auth_errstr + auth_errlist[i].message_off); |
| 386 | } |
| 387 | } |
| 388 | return NULL; |
| 389 | } |
| 390 | |
| 391 | |
| 392 | libc_freeres_fn (free_mem) |
| 393 | { |
| 394 | /* Not libc_freeres_ptr, since buf is a macro. */ |
| 395 | free (buf); |
| 396 | } |