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