clnt_perror.c 11 KB

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