clnt_perror.c 11 KB

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