clnt_perror.c 10 KB

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