print-krb.c 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  1. /*
  2. * Copyright (c) 1995, 1996, 1997
  3. * The Regents of the University of California. All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that: (1) source code distributions
  7. * retain the above copyright notice and this paragraph in its entirety, (2)
  8. * distributions including binary code include the above copyright notice and
  9. * this paragraph in its entirety in the documentation or other materials
  10. * provided with the distribution, and (3) all advertising materials mentioning
  11. * features or use of this software display the following acknowledgement:
  12. * ``This product includes software developed by the University of California,
  13. * Lawrence Berkeley Laboratory and its contributors.'' Neither the name of
  14. * the University nor the names of its contributors may be used to endorse
  15. * or promote products derived from this software without specific prior
  16. * written permission.
  17. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
  18. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
  19. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  20. *
  21. * Initial contribution from John Hawkinson (jhawk@mit.edu).
  22. */
  23. /* \summary: Kerberos printer */
  24. #ifdef HAVE_CONFIG_H
  25. #include "config.h"
  26. #endif
  27. #include <netdissect-stdinc.h>
  28. #include "netdissect.h"
  29. #include "extract.h"
  30. static const char tstr[] = " [|kerberos]";
  31. static const u_char *c_print(netdissect_options *, register const u_char *, register const u_char *);
  32. static const u_char *krb4_print_hdr(netdissect_options *, const u_char *);
  33. static void krb4_print(netdissect_options *, const u_char *);
  34. #define AUTH_MSG_KDC_REQUEST 1<<1
  35. #define AUTH_MSG_KDC_REPLY 2<<1
  36. #define AUTH_MSG_APPL_REQUEST 3<<1
  37. #define AUTH_MSG_APPL_REQUEST_MUTUAL 4<<1
  38. #define AUTH_MSG_ERR_REPLY 5<<1
  39. #define AUTH_MSG_PRIVATE 6<<1
  40. #define AUTH_MSG_SAFE 7<<1
  41. #define AUTH_MSG_APPL_ERR 8<<1
  42. #define AUTH_MSG_DIE 63<<1
  43. #define KERB_ERR_OK 0
  44. #define KERB_ERR_NAME_EXP 1
  45. #define KERB_ERR_SERVICE_EXP 2
  46. #define KERB_ERR_AUTH_EXP 3
  47. #define KERB_ERR_PKT_VER 4
  48. #define KERB_ERR_NAME_MAST_KEY_VER 5
  49. #define KERB_ERR_SERV_MAST_KEY_VER 6
  50. #define KERB_ERR_BYTE_ORDER 7
  51. #define KERB_ERR_PRINCIPAL_UNKNOWN 8
  52. #define KERB_ERR_PRINCIPAL_NOT_UNIQUE 9
  53. #define KERB_ERR_NULL_KEY 10
  54. struct krb {
  55. uint8_t pvno; /* Protocol Version */
  56. uint8_t type; /* Type+B */
  57. };
  58. static const struct tok type2str[] = {
  59. { AUTH_MSG_KDC_REQUEST, "KDC_REQUEST" },
  60. { AUTH_MSG_KDC_REPLY, "KDC_REPLY" },
  61. { AUTH_MSG_APPL_REQUEST, "APPL_REQUEST" },
  62. { AUTH_MSG_APPL_REQUEST_MUTUAL, "APPL_REQUEST_MUTUAL" },
  63. { AUTH_MSG_ERR_REPLY, "ERR_REPLY" },
  64. { AUTH_MSG_PRIVATE, "PRIVATE" },
  65. { AUTH_MSG_SAFE, "SAFE" },
  66. { AUTH_MSG_APPL_ERR, "APPL_ERR" },
  67. { AUTH_MSG_DIE, "DIE" },
  68. { 0, NULL }
  69. };
  70. static const struct tok kerr2str[] = {
  71. { KERB_ERR_OK, "OK" },
  72. { KERB_ERR_NAME_EXP, "NAME_EXP" },
  73. { KERB_ERR_SERVICE_EXP, "SERVICE_EXP" },
  74. { KERB_ERR_AUTH_EXP, "AUTH_EXP" },
  75. { KERB_ERR_PKT_VER, "PKT_VER" },
  76. { KERB_ERR_NAME_MAST_KEY_VER, "NAME_MAST_KEY_VER" },
  77. { KERB_ERR_SERV_MAST_KEY_VER, "SERV_MAST_KEY_VER" },
  78. { KERB_ERR_BYTE_ORDER, "BYTE_ORDER" },
  79. { KERB_ERR_PRINCIPAL_UNKNOWN, "PRINCIPAL_UNKNOWN" },
  80. { KERB_ERR_PRINCIPAL_NOT_UNIQUE,"PRINCIPAL_NOT_UNIQUE" },
  81. { KERB_ERR_NULL_KEY, "NULL_KEY"},
  82. { 0, NULL}
  83. };
  84. static const u_char *
  85. c_print(netdissect_options *ndo,
  86. register const u_char *s, register const u_char *ep)
  87. {
  88. register u_char c;
  89. register int flag;
  90. flag = 1;
  91. while (s < ep) {
  92. c = *s++;
  93. if (c == '\0') {
  94. flag = 0;
  95. break;
  96. }
  97. if (!ND_ISASCII(c)) {
  98. c = ND_TOASCII(c);
  99. ND_PRINT((ndo, "M-"));
  100. }
  101. if (!ND_ISPRINT(c)) {
  102. c ^= 0x40; /* DEL to ?, others to alpha */
  103. ND_PRINT((ndo, "^"));
  104. }
  105. ND_PRINT((ndo, "%c", c));
  106. }
  107. if (flag)
  108. return NULL;
  109. return (s);
  110. }
  111. static const u_char *
  112. krb4_print_hdr(netdissect_options *ndo,
  113. const u_char *cp)
  114. {
  115. cp += 2;
  116. #define PRINT if ((cp = c_print(ndo, cp, ndo->ndo_snapend)) == NULL) goto trunc
  117. PRINT;
  118. ND_PRINT((ndo, "."));
  119. PRINT;
  120. ND_PRINT((ndo, "@"));
  121. PRINT;
  122. return (cp);
  123. trunc:
  124. ND_PRINT((ndo, "%s", tstr));
  125. return (NULL);
  126. #undef PRINT
  127. }
  128. static void
  129. krb4_print(netdissect_options *ndo,
  130. const u_char *cp)
  131. {
  132. register const struct krb *kp;
  133. u_char type;
  134. u_short len;
  135. #define PRINT if ((cp = c_print(ndo, cp, ndo->ndo_snapend)) == NULL) goto trunc
  136. /* True if struct krb is little endian */
  137. #define IS_LENDIAN(kp) (((kp)->type & 0x01) != 0)
  138. #define KTOHSP(kp, cp) (IS_LENDIAN(kp) ? EXTRACT_LE_16BITS(cp) : EXTRACT_16BITS(cp))
  139. kp = (const struct krb *)cp;
  140. if ((&kp->type) >= ndo->ndo_snapend) {
  141. ND_PRINT((ndo, "%s", tstr));
  142. return;
  143. }
  144. type = kp->type & (0xFF << 1);
  145. ND_PRINT((ndo, " %s %s: ",
  146. IS_LENDIAN(kp) ? "le" : "be", tok2str(type2str, NULL, type)));
  147. switch (type) {
  148. case AUTH_MSG_KDC_REQUEST:
  149. if ((cp = krb4_print_hdr(ndo, cp)) == NULL)
  150. return;
  151. cp += 4; /* ctime */
  152. ND_TCHECK(*cp);
  153. ND_PRINT((ndo, " %dmin ", *cp++ * 5));
  154. PRINT;
  155. ND_PRINT((ndo, "."));
  156. PRINT;
  157. break;
  158. case AUTH_MSG_APPL_REQUEST:
  159. cp += 2;
  160. ND_TCHECK(*cp);
  161. ND_PRINT((ndo, "v%d ", *cp++));
  162. PRINT;
  163. ND_TCHECK(*cp);
  164. ND_PRINT((ndo, " (%d)", *cp++));
  165. ND_TCHECK(*cp);
  166. ND_PRINT((ndo, " (%d)", *cp));
  167. break;
  168. case AUTH_MSG_KDC_REPLY:
  169. if ((cp = krb4_print_hdr(ndo, cp)) == NULL)
  170. return;
  171. cp += 10; /* timestamp + n + exp + kvno */
  172. ND_TCHECK2(*cp, sizeof(short));
  173. len = KTOHSP(kp, cp);
  174. ND_PRINT((ndo, " (%d)", len));
  175. break;
  176. case AUTH_MSG_ERR_REPLY:
  177. if ((cp = krb4_print_hdr(ndo, cp)) == NULL)
  178. return;
  179. cp += 4; /* timestamp */
  180. ND_TCHECK2(*cp, sizeof(short));
  181. ND_PRINT((ndo, " %s ", tok2str(kerr2str, NULL, KTOHSP(kp, cp))));
  182. cp += 4;
  183. PRINT;
  184. break;
  185. default:
  186. ND_PRINT((ndo, "(unknown)"));
  187. break;
  188. }
  189. return;
  190. trunc:
  191. ND_PRINT((ndo, "%s", tstr));
  192. }
  193. void
  194. krb_print(netdissect_options *ndo,
  195. const u_char *dat)
  196. {
  197. register const struct krb *kp;
  198. kp = (const struct krb *)dat;
  199. if (dat >= ndo->ndo_snapend) {
  200. ND_PRINT((ndo, "%s", tstr));
  201. return;
  202. }
  203. switch (kp->pvno) {
  204. case 1:
  205. case 2:
  206. case 3:
  207. ND_PRINT((ndo, " v%d", kp->pvno));
  208. break;
  209. case 4:
  210. ND_PRINT((ndo, " v%d", kp->pvno));
  211. krb4_print(ndo, (const u_char *)kp);
  212. break;
  213. case 106:
  214. case 107:
  215. ND_PRINT((ndo, " v5"));
  216. /* Decode ASN.1 here "someday" */
  217. break;
  218. }
  219. return;
  220. }