]> The Tcpdump Group git mirrors - tcpdump/blob - print-zephyr.c
CVE-2017-12900/Properly terminate all struct tok arrays.
[tcpdump] / print-zephyr.c
1 /*
2 * Decode and print Zephyr packets.
3 *
4 * http://web.mit.edu/zephyr/doc/protocol
5 *
6 * Copyright (c) 2001 Nickolai Zeldovich <kolya@MIT.EDU>
7 * All rights reserved.
8 *
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that: (1) source code
11 * distributions retain the above copyright notice and this paragraph
12 * in its entirety, and (2) distributions including binary code include
13 * the above copyright notice and this paragraph in its entirety in
14 * the documentation or other materials provided with the distribution.
15 * The name of the author(s) may not be used to endorse or promote
16 * products derived from this software without specific prior written
17 * permission. THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY
18 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20 * PURPOSE.
21 */
22
23 /* \summary: Zephyr printer */
24
25 #ifdef HAVE_CONFIG_H
26 #include "config.h"
27 #endif
28
29 #include <netdissect-stdinc.h>
30
31 #include <stdio.h>
32 #include <string.h>
33 #include <stdlib.h>
34
35 #include "netdissect.h"
36
37 struct z_packet {
38 const char *version;
39 int numfields;
40 int kind;
41 const char *uid;
42 int port;
43 int auth;
44 int authlen;
45 const char *authdata;
46 const char *class;
47 const char *inst;
48 const char *opcode;
49 const char *sender;
50 const char *recipient;
51 const char *format;
52 int cksum;
53 int multi;
54 const char *multi_uid;
55 /* Other fields follow here.. */
56 };
57
58 enum z_packet_type {
59 Z_PACKET_UNSAFE = 0,
60 Z_PACKET_UNACKED,
61 Z_PACKET_ACKED,
62 Z_PACKET_HMACK,
63 Z_PACKET_HMCTL,
64 Z_PACKET_SERVACK,
65 Z_PACKET_SERVNAK,
66 Z_PACKET_CLIENTACK,
67 Z_PACKET_STAT
68 };
69
70 static const struct tok z_types[] = {
71 { Z_PACKET_UNSAFE, "unsafe" },
72 { Z_PACKET_UNACKED, "unacked" },
73 { Z_PACKET_ACKED, "acked" },
74 { Z_PACKET_HMACK, "hm-ack" },
75 { Z_PACKET_HMCTL, "hm-ctl" },
76 { Z_PACKET_SERVACK, "serv-ack" },
77 { Z_PACKET_SERVNAK, "serv-nak" },
78 { Z_PACKET_CLIENTACK, "client-ack" },
79 { Z_PACKET_STAT, "stat" },
80 { 0, NULL }
81 };
82
83 static char z_buf[256];
84
85 static const char *
86 parse_field(netdissect_options *ndo, const char **pptr, int *len)
87 {
88 const char *s;
89
90 if (*len <= 0 || !pptr || !*pptr)
91 return NULL;
92 if (*pptr > (const char *) ndo->ndo_snapend)
93 return NULL;
94
95 s = *pptr;
96 while (*pptr <= (const char *) ndo->ndo_snapend && *len >= 0 && **pptr) {
97 (*pptr)++;
98 (*len)--;
99 }
100 (*pptr)++;
101 (*len)--;
102 if (*len < 0 || *pptr > (const char *) ndo->ndo_snapend)
103 return NULL;
104 return s;
105 }
106
107 static const char *
108 z_triple(const char *class, const char *inst, const char *recipient)
109 {
110 if (!*recipient)
111 recipient = "*";
112 snprintf(z_buf, sizeof(z_buf), "<%s,%s,%s>", class, inst, recipient);
113 z_buf[sizeof(z_buf)-1] = '\0';
114 return z_buf;
115 }
116
117 static const char *
118 str_to_lower(const char *string)
119 {
120 char *zb_string;
121
122 strncpy(z_buf, string, sizeof(z_buf));
123 z_buf[sizeof(z_buf)-1] = '\0';
124
125 zb_string = z_buf;
126 while (*zb_string) {
127 *zb_string = tolower((unsigned char)(*zb_string));
128 zb_string++;
129 }
130
131 return z_buf;
132 }
133
134 void
135 zephyr_print(netdissect_options *ndo, const u_char *cp, int length)
136 {
137 struct z_packet z;
138 const char *parse = (const char *) cp;
139 int parselen = length;
140 const char *s;
141 int lose = 0;
142
143 /* squelch compiler warnings */
144
145 z.kind = 0;
146 z.class = 0;
147 z.inst = 0;
148 z.opcode = 0;
149 z.sender = 0;
150 z.recipient = 0;
151
152 #define PARSE_STRING \
153 s = parse_field(ndo, &parse, &parselen); \
154 if (!s) lose = 1;
155
156 #define PARSE_FIELD_INT(field) \
157 PARSE_STRING \
158 if (!lose) field = strtol(s, 0, 16);
159
160 #define PARSE_FIELD_STR(field) \
161 PARSE_STRING \
162 if (!lose) field = s;
163
164 PARSE_FIELD_STR(z.version);
165 if (lose) return;
166 if (strncmp(z.version, "ZEPH", 4))
167 return;
168
169 PARSE_FIELD_INT(z.numfields);
170 PARSE_FIELD_INT(z.kind);
171 PARSE_FIELD_STR(z.uid);
172 PARSE_FIELD_INT(z.port);
173 PARSE_FIELD_INT(z.auth);
174 PARSE_FIELD_INT(z.authlen);
175 PARSE_FIELD_STR(z.authdata);
176 PARSE_FIELD_STR(z.class);
177 PARSE_FIELD_STR(z.inst);
178 PARSE_FIELD_STR(z.opcode);
179 PARSE_FIELD_STR(z.sender);
180 PARSE_FIELD_STR(z.recipient);
181 PARSE_FIELD_STR(z.format);
182 PARSE_FIELD_INT(z.cksum);
183 PARSE_FIELD_INT(z.multi);
184 PARSE_FIELD_STR(z.multi_uid);
185
186 if (lose) {
187 ND_PRINT((ndo, " [|zephyr] (%d)", length));
188 return;
189 }
190
191 ND_PRINT((ndo, " zephyr"));
192 if (strncmp(z.version+4, "0.2", 3)) {
193 ND_PRINT((ndo, " v%s", z.version+4));
194 return;
195 }
196
197 ND_PRINT((ndo, " %s", tok2str(z_types, "type %d", z.kind)));
198 if (z.kind == Z_PACKET_SERVACK) {
199 /* Initialization to silence warnings */
200 const char *ackdata = NULL;
201 PARSE_FIELD_STR(ackdata);
202 if (!lose && strcmp(ackdata, "SENT"))
203 ND_PRINT((ndo, "/%s", str_to_lower(ackdata)));
204 }
205 if (*z.sender) ND_PRINT((ndo, " %s", z.sender));
206
207 if (!strcmp(z.class, "USER_LOCATE")) {
208 if (!strcmp(z.opcode, "USER_HIDE"))
209 ND_PRINT((ndo, " hide"));
210 else if (!strcmp(z.opcode, "USER_UNHIDE"))
211 ND_PRINT((ndo, " unhide"));
212 else
213 ND_PRINT((ndo, " locate %s", z.inst));
214 return;
215 }
216
217 if (!strcmp(z.class, "ZEPHYR_ADMIN")) {
218 ND_PRINT((ndo, " zephyr-admin %s", str_to_lower(z.opcode)));
219 return;
220 }
221
222 if (!strcmp(z.class, "ZEPHYR_CTL")) {
223 if (!strcmp(z.inst, "CLIENT")) {
224 if (!strcmp(z.opcode, "SUBSCRIBE") ||
225 !strcmp(z.opcode, "SUBSCRIBE_NODEFS") ||
226 !strcmp(z.opcode, "UNSUBSCRIBE")) {
227
228 ND_PRINT((ndo, " %ssub%s", strcmp(z.opcode, "SUBSCRIBE") ? "un" : "",
229 strcmp(z.opcode, "SUBSCRIBE_NODEFS") ? "" :
230 "-nodefs"));
231 if (z.kind != Z_PACKET_SERVACK) {
232 /* Initialization to silence warnings */
233 const char *c = NULL, *i = NULL, *r = NULL;
234 PARSE_FIELD_STR(c);
235 PARSE_FIELD_STR(i);
236 PARSE_FIELD_STR(r);
237 if (!lose) ND_PRINT((ndo, " %s", z_triple(c, i, r)));
238 }
239 return;
240 }
241
242 if (!strcmp(z.opcode, "GIMME")) {
243 ND_PRINT((ndo, " ret"));
244 return;
245 }
246
247 if (!strcmp(z.opcode, "GIMMEDEFS")) {
248 ND_PRINT((ndo, " gimme-defs"));
249 return;
250 }
251
252 if (!strcmp(z.opcode, "CLEARSUB")) {
253 ND_PRINT((ndo, " clear-subs"));
254 return;
255 }
256
257 ND_PRINT((ndo, " %s", str_to_lower(z.opcode)));
258 return;
259 }
260
261 if (!strcmp(z.inst, "HM")) {
262 ND_PRINT((ndo, " %s", str_to_lower(z.opcode)));
263 return;
264 }
265
266 if (!strcmp(z.inst, "REALM")) {
267 if (!strcmp(z.opcode, "ADD_SUBSCRIBE"))
268 ND_PRINT((ndo, " realm add-subs"));
269 if (!strcmp(z.opcode, "REQ_SUBSCRIBE"))
270 ND_PRINT((ndo, " realm req-subs"));
271 if (!strcmp(z.opcode, "RLM_SUBSCRIBE"))
272 ND_PRINT((ndo, " realm rlm-sub"));
273 if (!strcmp(z.opcode, "RLM_UNSUBSCRIBE"))
274 ND_PRINT((ndo, " realm rlm-unsub"));
275 return;
276 }
277 }
278
279 if (!strcmp(z.class, "HM_CTL")) {
280 ND_PRINT((ndo, " hm_ctl %s", str_to_lower(z.inst)));
281 ND_PRINT((ndo, " %s", str_to_lower(z.opcode)));
282 return;
283 }
284
285 if (!strcmp(z.class, "HM_STAT")) {
286 if (!strcmp(z.inst, "HMST_CLIENT") && !strcmp(z.opcode, "GIMMESTATS")) {
287 ND_PRINT((ndo, " get-client-stats"));
288 return;
289 }
290 }
291
292 if (!strcmp(z.class, "WG_CTL")) {
293 ND_PRINT((ndo, " wg_ctl %s", str_to_lower(z.inst)));
294 ND_PRINT((ndo, " %s", str_to_lower(z.opcode)));
295 return;
296 }
297
298 if (!strcmp(z.class, "LOGIN")) {
299 if (!strcmp(z.opcode, "USER_FLUSH")) {
300 ND_PRINT((ndo, " flush_locs"));
301 return;
302 }
303
304 if (!strcmp(z.opcode, "NONE") ||
305 !strcmp(z.opcode, "OPSTAFF") ||
306 !strcmp(z.opcode, "REALM-VISIBLE") ||
307 !strcmp(z.opcode, "REALM-ANNOUNCED") ||
308 !strcmp(z.opcode, "NET-VISIBLE") ||
309 !strcmp(z.opcode, "NET-ANNOUNCED")) {
310 ND_PRINT((ndo, " set-exposure %s", str_to_lower(z.opcode)));
311 return;
312 }
313 }
314
315 if (!*z.recipient)
316 z.recipient = "*";
317
318 ND_PRINT((ndo, " to %s", z_triple(z.class, z.inst, z.recipient)));
319 if (*z.opcode)
320 ND_PRINT((ndo, " op %s", z.opcode));
321 }