clnt_perror.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448
  1. /* @(#)clnt_perror.c 2.1 88/07/29 4.0 RPCSRC */
  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. #if 0
  31. static char sccsid[] = "@(#)clnt_perror.c 1.15 87/10/07 Copyr 1984 Sun Micro";
  32. #endif
  33. /*
  34. * clnt_perror.c
  35. *
  36. * Copyright (C) 1984, Sun Microsystems, Inc.
  37. *
  38. */
  39. #define __FORCE_GLIBC
  40. #include <features.h>
  41. #include <stdio.h>
  42. #include <string.h>
  43. #include "rpc_private.h"
  44. #ifdef USE_IN_LIBIO
  45. # include <wchar.h>
  46. # include <libio/iolibio.h>
  47. # define fputs(s, f) _IO_fputs (s, f)
  48. #endif
  49. /* Experimentally off - libc_hidden_proto(strcpy) */
  50. /* Experimentally off - libc_hidden_proto(strlen) */
  51. libc_hidden_proto(sprintf)
  52. libc_hidden_proto(__glibc_strerror_r)
  53. libc_hidden_proto(fputs)
  54. #ifdef USE_IN_LIBIO
  55. libc_hidden_proto(fwprintf)
  56. #endif
  57. static char *auth_errmsg (enum auth_stat stat) internal_function;
  58. #ifdef __UCLIBC_HAS_THREADS__
  59. /*
  60. * Making buf a preprocessor macro requires renaming the local
  61. * buf variable in a few functions. Overriding a global variable
  62. * with a local variable of the same name is a bad idea, anyway.
  63. */
  64. #define buf (*(char **)&RPC_THREAD_VARIABLE(clnt_perr_buf_s))
  65. #else
  66. static char *buf;
  67. #endif
  68. static char *
  69. _buf (void)
  70. {
  71. if (buf == NULL)
  72. buf = (char *) malloc (256);
  73. return buf;
  74. }
  75. struct rpc_errtab
  76. {
  77. enum clnt_stat status;
  78. unsigned int message_off;
  79. };
  80. static const char rpc_errstr[] =
  81. {
  82. #define RPC_SUCCESS_IDX 0
  83. _("RPC: Success")
  84. "\0"
  85. #define RPC_CANTENCODEARGS_IDX (RPC_SUCCESS_IDX + sizeof "RPC: Success")
  86. _("RPC: Can't encode arguments")
  87. "\0"
  88. #define RPC_CANTDECODERES_IDX (RPC_CANTENCODEARGS_IDX \
  89. + sizeof "RPC: Can't encode arguments")
  90. _("RPC: Can't decode result")
  91. "\0"
  92. #define RPC_CANTSEND_IDX (RPC_CANTDECODERES_IDX \
  93. + sizeof "RPC: Can't decode result")
  94. _("RPC: Unable to send")
  95. "\0"
  96. #define RPC_CANTRECV_IDX (RPC_CANTSEND_IDX \
  97. + sizeof "RPC: Unable to send")
  98. _("RPC: Unable to receive")
  99. "\0"
  100. #define RPC_TIMEDOUT_IDX (RPC_CANTRECV_IDX \
  101. + sizeof "RPC: Unable to receive")
  102. _("RPC: Timed out")
  103. "\0"
  104. #define RPC_VERSMISMATCH_IDX (RPC_TIMEDOUT_IDX \
  105. + sizeof "RPC: Timed out")
  106. _("RPC: Incompatible versions of RPC")
  107. "\0"
  108. #define RPC_AUTHERROR_IDX (RPC_VERSMISMATCH_IDX \
  109. + sizeof "RPC: Incompatible versions of RPC")
  110. _("RPC: Authentication error")
  111. "\0"
  112. #define RPC_PROGUNAVAIL_IDX (RPC_AUTHERROR_IDX \
  113. + sizeof "RPC: Authentication error")
  114. _("RPC: Program unavailable")
  115. "\0"
  116. #define RPC_PROGVERSMISMATCH_IDX (RPC_PROGUNAVAIL_IDX \
  117. + sizeof "RPC: Program unavailable")
  118. _("RPC: Program/version mismatch")
  119. "\0"
  120. #define RPC_PROCUNAVAIL_IDX (RPC_PROGVERSMISMATCH_IDX \
  121. + sizeof "RPC: Program/version mismatch")
  122. _("RPC: Procedure unavailable")
  123. "\0"
  124. #define RPC_CANTDECODEARGS_IDX (RPC_PROCUNAVAIL_IDX \
  125. + sizeof "RPC: Procedure unavailable")
  126. _("RPC: Server can't decode arguments")
  127. "\0"
  128. #define RPC_SYSTEMERROR_IDX (RPC_CANTDECODEARGS_IDX \
  129. + sizeof "RPC: Server can't decode arguments")
  130. _("RPC: Remote system error")
  131. "\0"
  132. #define RPC_UNKNOWNHOST_IDX (RPC_SYSTEMERROR_IDX \
  133. + sizeof "RPC: Remote system error")
  134. _("RPC: Unknown host")
  135. "\0"
  136. #define RPC_UNKNOWNPROTO_IDX (RPC_UNKNOWNHOST_IDX \
  137. + sizeof "RPC: Unknown host")
  138. _("RPC: Unknown protocol")
  139. "\0"
  140. #define RPC_PMAPFAILURE_IDX (RPC_UNKNOWNPROTO_IDX \
  141. + sizeof "RPC: Unknown protocol")
  142. _("RPC: Port mapper failure")
  143. "\0"
  144. #define RPC_PROGNOTREGISTERED_IDX (RPC_PMAPFAILURE_IDX \
  145. + sizeof "RPC: Port mapper failure")
  146. _("RPC: Program not registered")
  147. "\0"
  148. #define RPC_FAILED_IDX (RPC_PROGNOTREGISTERED_IDX \
  149. + sizeof "RPC: Program not registered")
  150. _("RPC: Failed (unspecified error)")
  151. };
  152. static const struct rpc_errtab rpc_errlist[] =
  153. {
  154. { RPC_SUCCESS, RPC_SUCCESS_IDX },
  155. { RPC_CANTENCODEARGS, RPC_CANTENCODEARGS_IDX },
  156. { RPC_CANTDECODERES, RPC_CANTDECODERES_IDX },
  157. { RPC_CANTSEND, RPC_CANTSEND_IDX },
  158. { RPC_CANTRECV, RPC_CANTRECV_IDX },
  159. { RPC_TIMEDOUT, RPC_TIMEDOUT_IDX },
  160. { RPC_VERSMISMATCH, RPC_VERSMISMATCH_IDX },
  161. { RPC_AUTHERROR, RPC_AUTHERROR_IDX },
  162. { RPC_PROGUNAVAIL, RPC_PROGUNAVAIL_IDX },
  163. { RPC_PROGVERSMISMATCH, RPC_PROGVERSMISMATCH_IDX },
  164. { RPC_PROCUNAVAIL, RPC_PROCUNAVAIL_IDX },
  165. { RPC_CANTDECODEARGS, RPC_CANTDECODEARGS_IDX },
  166. { RPC_SYSTEMERROR, RPC_SYSTEMERROR_IDX },
  167. { RPC_UNKNOWNHOST, RPC_UNKNOWNHOST_IDX },
  168. { RPC_UNKNOWNPROTO, RPC_UNKNOWNPROTO_IDX },
  169. { RPC_PMAPFAILURE, RPC_PMAPFAILURE_IDX },
  170. { RPC_PROGNOTREGISTERED, RPC_PROGNOTREGISTERED_IDX },
  171. { RPC_FAILED, RPC_FAILED_IDX }
  172. };
  173. /*
  174. * This interface for use by clntrpc
  175. */
  176. libc_hidden_proto(clnt_sperrno)
  177. char *
  178. clnt_sperrno (enum clnt_stat stat)
  179. {
  180. size_t i;
  181. for (i = 0; i < sizeof (rpc_errlist) / sizeof (struct rpc_errtab); i++)
  182. {
  183. if (rpc_errlist[i].status == stat)
  184. {
  185. return (char*)_(rpc_errstr + rpc_errlist[i].message_off);
  186. }
  187. }
  188. return _("RPC: (unknown error code)");
  189. }
  190. libc_hidden_def(clnt_sperrno)
  191. void
  192. clnt_perrno (enum clnt_stat num)
  193. {
  194. #ifdef USE_IN_LIBIO
  195. if (_IO_fwide (stderr, 0) > 0)
  196. (void) fwprintf (stderr, L"%s", clnt_sperrno (num));
  197. else
  198. #endif
  199. (void) fputs (clnt_sperrno (num), stderr);
  200. }
  201. /*
  202. * Print reply error info
  203. */
  204. libc_hidden_proto(clnt_sperror)
  205. char *
  206. clnt_sperror (CLIENT * rpch, const char *msg)
  207. {
  208. char chrbuf[1024];
  209. struct rpc_err e;
  210. char *err;
  211. char *str = _buf ();
  212. char *strstart = str;
  213. int len;
  214. if (str == NULL)
  215. return NULL;
  216. CLNT_GETERR (rpch, &e);
  217. len = sprintf (str, "%s: ", msg);
  218. str += len;
  219. (void) strcpy(str, clnt_sperrno(e.re_status));
  220. str += strlen(str);
  221. switch (e.re_status)
  222. {
  223. case RPC_SUCCESS:
  224. case RPC_CANTENCODEARGS:
  225. case RPC_CANTDECODERES:
  226. case RPC_TIMEDOUT:
  227. case RPC_PROGUNAVAIL:
  228. case RPC_PROCUNAVAIL:
  229. case RPC_CANTDECODEARGS:
  230. case RPC_SYSTEMERROR:
  231. case RPC_UNKNOWNHOST:
  232. case RPC_UNKNOWNPROTO:
  233. case RPC_PMAPFAILURE:
  234. case RPC_PROGNOTREGISTERED:
  235. case RPC_FAILED:
  236. break;
  237. case RPC_CANTSEND:
  238. case RPC_CANTRECV:
  239. __glibc_strerror_r (e.re_errno, chrbuf, sizeof chrbuf);
  240. len = sprintf (str, "; errno = %s", chrbuf);
  241. str += len;
  242. break;
  243. case RPC_VERSMISMATCH:
  244. len= sprintf (str, _("; low version = %lu, high version = %lu"),
  245. e.re_vers.low, e.re_vers.high);
  246. str += len;
  247. break;
  248. case RPC_AUTHERROR:
  249. err = auth_errmsg (e.re_why);
  250. (void) strcpy(str, _("; why = "));
  251. str += strlen(str);
  252. if (err != NULL)
  253. {
  254. (void) strcpy(str, err);
  255. str += strlen(str);
  256. }
  257. else
  258. {
  259. len = sprintf (str, _("(unknown authentication error - %d)"),
  260. (int) e.re_why);
  261. str += len;
  262. }
  263. break;
  264. case RPC_PROGVERSMISMATCH:
  265. len = sprintf (str, _("; low version = %lu, high version = %lu"),
  266. e.re_vers.low, e.re_vers.high);
  267. str += len;
  268. break;
  269. default: /* unknown */
  270. len = sprintf (str, "; s1 = %lu, s2 = %lu", e.re_lb.s1, e.re_lb.s2);
  271. str += len;
  272. break;
  273. }
  274. *str = '\n';
  275. *++str = '\0';
  276. return (strstart);
  277. }
  278. libc_hidden_def(clnt_sperror)
  279. libc_hidden_proto(clnt_perror)
  280. void
  281. clnt_perror (CLIENT * rpch, const char *msg)
  282. {
  283. #ifdef USE_IN_LIBIO
  284. if (_IO_fwide (stderr, 0) > 0)
  285. (void) fwprintf (stderr, L"%s", clnt_sperror (rpch, msg));
  286. else
  287. #endif
  288. (void) fputs (clnt_sperror (rpch, msg), stderr);
  289. }
  290. libc_hidden_def(clnt_perror)
  291. libc_hidden_proto(clnt_spcreateerror)
  292. char *
  293. clnt_spcreateerror (const char *msg)
  294. {
  295. char chrbuf[1024];
  296. char *str = _buf ();
  297. char *cp;
  298. int len;
  299. struct rpc_createerr *ce;
  300. if (str == NULL)
  301. return NULL;
  302. ce = &get_rpc_createerr ();
  303. len = sprintf (str, "%s: ", msg);
  304. cp = str + len;
  305. (void) strcpy(cp, clnt_sperrno (ce->cf_stat));
  306. cp += strlen(cp);
  307. switch (ce->cf_stat)
  308. {
  309. case RPC_PMAPFAILURE:
  310. (void) strcpy(cp, " - ");
  311. cp += strlen(cp);
  312. (void) strcpy(cp, clnt_sperrno (ce->cf_error.re_status));
  313. cp += strlen(cp);
  314. break;
  315. case RPC_SYSTEMERROR:
  316. (void) strcpy(cp, " - ");
  317. cp += strlen(cp);
  318. __glibc_strerror_r (ce->cf_error.re_errno, chrbuf, sizeof chrbuf);
  319. (void) strcpy(cp, chrbuf);
  320. cp += strlen(cp);
  321. break;
  322. default:
  323. break;
  324. }
  325. *cp = '\n';
  326. *++cp = '\0';
  327. return str;
  328. }
  329. libc_hidden_def(clnt_spcreateerror)
  330. void
  331. clnt_pcreateerror (const char *msg)
  332. {
  333. #ifdef USE_IN_LIBIO
  334. if (_IO_fwide (stderr, 0) > 0)
  335. (void) fwprintf (stderr, L"%s", clnt_spcreateerror (msg));
  336. else
  337. #endif
  338. (void) fputs (clnt_spcreateerror (msg), stderr);
  339. }
  340. struct auth_errtab
  341. {
  342. enum auth_stat status;
  343. unsigned int message_off;
  344. };
  345. static const char auth_errstr[] =
  346. {
  347. #define AUTH_OK_IDX 0
  348. _("Authentication OK")
  349. "\0"
  350. #define AUTH_BADCRED_IDX (AUTH_OK_IDX + sizeof "Authentication OK")
  351. _("Invalid client credential")
  352. "\0"
  353. #define AUTH_REJECTEDCRED_IDX (AUTH_BADCRED_IDX \
  354. + sizeof "Invalid client credential")
  355. _("Server rejected credential")
  356. "\0"
  357. #define AUTH_BADVERF_IDX (AUTH_REJECTEDCRED_IDX \
  358. + sizeof "Server rejected credential")
  359. _("Invalid client verifier")
  360. "\0"
  361. #define AUTH_REJECTEDVERF_IDX (AUTH_BADVERF_IDX \
  362. + sizeof "Invalid client verifier")
  363. _("Server rejected verifier")
  364. "\0"
  365. #define AUTH_TOOWEAK_IDX (AUTH_REJECTEDVERF_IDX \
  366. + sizeof "Server rejected verifier")
  367. _("Client credential too weak")
  368. "\0"
  369. #define AUTH_INVALIDRESP_IDX (AUTH_TOOWEAK_IDX \
  370. + sizeof "Client credential too weak")
  371. _("Invalid server verifier")
  372. "\0"
  373. #define AUTH_FAILED_IDX (AUTH_INVALIDRESP_IDX \
  374. + sizeof "Invalid server verifier")
  375. _("Failed (unspecified error)")
  376. };
  377. static const struct auth_errtab auth_errlist[] =
  378. {
  379. { AUTH_OK, AUTH_OK_IDX },
  380. { AUTH_BADCRED, AUTH_BADCRED_IDX },
  381. { AUTH_REJECTEDCRED, AUTH_REJECTEDCRED_IDX },
  382. { AUTH_BADVERF, AUTH_BADVERF_IDX },
  383. { AUTH_REJECTEDVERF, AUTH_REJECTEDVERF_IDX },
  384. { AUTH_TOOWEAK, AUTH_TOOWEAK_IDX },
  385. { AUTH_INVALIDRESP, AUTH_INVALIDRESP_IDX },
  386. { AUTH_FAILED, AUTH_FAILED_IDX }
  387. };
  388. static char *
  389. internal_function
  390. auth_errmsg (enum auth_stat stat)
  391. {
  392. size_t i;
  393. for (i = 0; i < sizeof (auth_errlist) / sizeof (struct auth_errtab); i++)
  394. {
  395. if (auth_errlist[i].status == stat)
  396. {
  397. return (char*)_(auth_errstr + auth_errlist[i].message_off);
  398. }
  399. }
  400. return NULL;
  401. }
  402. static void __attribute_used__
  403. free_mem (void)
  404. {
  405. free (buf);
  406. }