TODO ... pkinit win2k
[metze/wireshark/wip.git] / wiretap / vms.c
1 /* vms.c
2  *
3  * Wiretap Library
4  * Copyright (c) 2001 by Marc Milgram <ethereal@mmilgram.NOSPAMmail.net>
5  *
6  * SPDX-License-Identifier: GPL-2.0-or-later
7  */
8
9 /* Notes:
10  *   TCPIPtrace TCP fragments don't have the header line.  So, we are never
11  *   to look for that line for the first line of a packet except the first
12  *   packet.  This allows us to read fragmented packets.  Define
13  *   TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE to expect the first line to be
14  *   at the start of every packet.
15  */
16 #include "config.h"
17 #include "wtap-int.h"
18 #include "vms.h"
19 #include "file_wrappers.h"
20
21 #include <wsutil/strtoi.h>
22
23 #include <stdlib.h>
24 #include <string.h>
25
26 /* This module reads the output of the various VMS TCPIP trace utilities
27  * such as TCPIPTRACE, TCPTRACE and UCX$TRACE
28  *
29  * It was initially based on toshiba.c and refined with code from cosine.c
30
31 --------------------------------------------------------------------------------
32    Example TCPIPTRACE TCPTRACE output data:
33
34    TCPIPtrace full display RCV packet 8 at 10-JUL-2001 14:54:19.56
35
36    IP Version = 4,  IHL = 5,  TOS = 00,   Total Length = 84 = ^x0054
37    IP Identifier  = ^x178F,  Flags (0=0,DF=0,MF=0),
38          Fragment Offset = 0 = ^x0000,   Calculated Offset = 0 = ^x0000
39    IP TTL = 64 = ^x40,  Protocol = 17 = ^x11,  Header Checksum = ^x4C71
40    IP Source Address      = 10.12.1.80
41    IP Destination Address = 10.12.1.50
42
43    UDP Source Port = 731,   UDP Destination Port = 111
44    UDP Header and Datagram Length = 64 = ^x0040,   Checksum = ^xB6C0
45
46    50010C0A   714C1140   00008F17   54000045    0000    E..T....@.Lq...P
47    27E54C3C | C0B64000   6F00DB02 | 32010C0A    0010    ...2...o.@..<L.'
48    02000000   A0860100   02000000   00000000    0020    ................
49    00000000   00000000   00000000   03000000    0030    ................
50    06000000   01000000   A5860100   00000000    0040    ................
51                                     00000000    0050    ....
52 --------------------------------------------------------------------------------
53
54    Example UCX$TRACE output data:
55
56     UCX INTERnet trace RCV packet seq # = 1 at 14-MAY-2003 11:32:10.93
57
58    IP Version = 4,  IHL = 5,  TOS = 00,   Total Length = 583 = ^x0247
59    IP Identifier  = ^x702E,  Flags (0=0,DF=0,MF=0),
60          Fragment Offset = 0 = ^x0000,   Calculated Offset = 0 = ^x0000
61    IP TTL = 128 = ^x80,  Protocol = 17 = ^x11,  Header Checksum = ^x70EC
62    IP Source Address      = 10.20.4.159
63    IP Destination Address = 10.20.4.255
64
65    UDP Source Port = 138,   UDP Destination Port = 138
66    UDP Header and Datagram Length = 563 = ^x0233,   Checksum = ^xB913
67
68    9F04140A   70EC1180   0000702E   47020045    0000    E..G.p.....p....
69    B1B80E11 | B9133302   8A008A00 | FF04140A    0010    .........3......
70    46484648   45200000   1D028A00   9F04140A    0020    ...........EHFHF
71    43414341   4341434D   454D4546   45454550    0030    PEEEFEMEMCACACAC
72
73 --------------------------------------------------------------------------------
74
75    Alternate UCX$TRACE type output data:
76
77    TCPIP INTERnet trace RCV packet seq # = 1 at 23-OCT-1998 15:19:33.29
78
79    IP Version = 4,  IHL = 5,  TOS = 00,   Total Length = 217 = ^x00D9
80    IP Identifier  = ^x0065,  Flags (0=0,DF=0,MF=0),
81          Fragment Offset = 0 = ^x0000,   Calculated Offset = 0 = ^x0000
82    IP TTL = 32 = ^x20,  Protocol = 17 = ^x11,  Header Checksum = ^x8F6C
83    IP Source Address      = 16.20.168.93
84    IP Destination Address = 16.20.255.255
85
86    UDP Source Port = 138,   UDP Destination Port = 138
87    UDP Header and Datagram Length = 197 = ^x00C5,   Checksum = ^x0E77
88
89    5DA81410   8F6C1120   00000065   D9000045    0000    E...awe.....l....]
90             | 0E77C500   8A008A00 | FFFF1410    0010    ..........w.
91
92 --------------------------------------------------------------------------------
93
94 The only difference between the utilities is the Packet header line, primarily
95 the utility identifier and the packet sequence formats.
96
97 There appear to be 2 formats for packet seqencing
98
99 Format 1:
100
101  ... packet nn at DD-MMM-YYYY hh:mm:ss.ss
102
103 Format 2:
104
105  ... packet seq # = nn at DD-MMM-YYYY hh:mm:ss.ss
106
107 If there are other formats then code will have to be written in parse_vms_packet()
108 to handle them.
109
110 --------------------------------------------------------------------------------
111
112  */
113
114 /* Magic text to check for VMS-ness of file using possible utility names
115  *
116  */
117 #define VMS_HDR_MAGIC_STR1      "TCPIPtrace"
118 #define VMS_HDR_MAGIC_STR2      "TCPtrace"
119 #define VMS_HDR_MAGIC_STR3      "INTERnet trace"
120
121 /* Magic text for start of packet */
122 #define VMS_REC_MAGIC_STR1      VMS_HDR_MAGIC_STR1
123 #define VMS_REC_MAGIC_STR2      VMS_HDR_MAGIC_STR2
124 #define VMS_REC_MAGIC_STR3      VMS_HDR_MAGIC_STR3
125
126 #define VMS_HEADER_LINES_TO_CHECK    200
127 #define VMS_LINE_LENGTH              240
128
129 static gboolean vms_read(wtap *wth, int *err, gchar **err_info,
130     gint64 *data_offset);
131 static gboolean vms_seek_read(wtap *wth, gint64 seek_off,
132     wtap_rec *rec, Buffer *buf, int *err, gchar **err_info);
133 static gboolean parse_single_hex_dump_line(char* rec, guint8 *buf,
134     long byte_offset, int in_off, int remaining_bytes);
135 static gboolean parse_vms_packet(FILE_T fh, wtap_rec *rec,
136     Buffer *buf, int *err, gchar **err_info);
137
138 #ifdef TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE
139 /* Seeks to the beginning of the next packet, and returns the
140    byte offset.  Returns -1 on failure, and sets "*err" to the error
141    and sets "*err_info" to null or an additional error string. */
142 static long vms_seek_next_packet(wtap *wth, int *err, gchar **err_info)
143 {
144     long cur_off;
145     char buf[VMS_LINE_LENGTH];
146
147     while (1) {
148         cur_off = file_tell(wth->fh);
149         if (cur_off == -1) {
150             /* Error */
151             *err = file_error(wth->fh, err_info);
152             return -1;
153         }
154         if (file_gets(buf, sizeof(buf), wth->fh) == NULL) {
155             /* EOF or error. */
156             *err = file_error(wth->fh, err_info);
157             break;
158         }
159         if (strstr(buf, VMS_REC_MAGIC_STR1) ||
160             strstr(buf, VMS_REC_MAGIC_STR2) ||
161             strstr(buf, VMS_REC_MAGIC_STR2)) {
162             g_strlcpy(hdr, buf,VMS_LINE_LENGTH);
163             return cur_off;
164         }
165     }
166     return -1;
167 }
168 #endif /* TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE */
169
170 /* Look through the first part of a file to see if this is
171  * a VMS trace file.
172  *
173  * Returns TRUE if it is, FALSE if it isn't or if we get an I/O error;
174  * if we get an I/O error, "*err" will be set to a non-zero value and
175  * "*err_info will be set to null or an additional error string.
176  *
177  * Leaves file handle at beginning of line that contains the VMS Magic
178  * identifier.
179  */
180 static gboolean vms_check_file_type(wtap *wth, int *err, gchar **err_info)
181 {
182     char buf[VMS_LINE_LENGTH];
183     guint reclen, line;
184     gint64 mpos;
185
186     buf[VMS_LINE_LENGTH-1] = '\0';
187
188     for (line = 0; line < VMS_HEADER_LINES_TO_CHECK; line++) {
189         mpos = file_tell(wth->fh);
190         if (mpos == -1) {
191             /* Error. */
192             *err = file_error(wth->fh, err_info);
193             return FALSE;
194         }
195         if (file_gets(buf, VMS_LINE_LENGTH, wth->fh) == NULL) {
196             /* EOF or error. */
197             *err = file_error(wth->fh, err_info);
198             return FALSE;
199         }
200
201         reclen = (guint) strlen(buf);
202         if (reclen < strlen(VMS_HDR_MAGIC_STR1) ||
203             reclen < strlen(VMS_HDR_MAGIC_STR2) ||
204             reclen < strlen(VMS_HDR_MAGIC_STR3)) {
205             continue;
206         }
207
208         if (strstr(buf, VMS_HDR_MAGIC_STR1) ||
209             strstr(buf, VMS_HDR_MAGIC_STR2) ||
210             strstr(buf, VMS_HDR_MAGIC_STR3)) {
211             /* Go back to the beginning of this line, so we will
212              * re-read it. */
213             if (file_seek(wth->fh, mpos, SEEK_SET, err) == -1) {
214                 /* Error. */
215                 return FALSE;
216             }
217             return TRUE;
218         }
219     }
220     *err = 0;
221     return FALSE;
222 }
223
224
225 wtap_open_return_val vms_open(wtap *wth, int *err, gchar **err_info)
226 {
227     /* Look for VMS header */
228     if (!vms_check_file_type(wth, err, err_info)) {
229         if (*err != 0 && *err != WTAP_ERR_SHORT_READ)
230             return WTAP_OPEN_ERROR;
231         return WTAP_OPEN_NOT_MINE;
232     }
233
234     wth->file_encap = WTAP_ENCAP_RAW_IP;
235     wth->file_type_subtype = WTAP_FILE_TYPE_SUBTYPE_VMS;
236     wth->snapshot_length = 0; /* not known */
237     wth->subtype_read = vms_read;
238     wth->subtype_seek_read = vms_seek_read;
239     wth->file_tsprec = WTAP_TSPREC_CSEC;
240
241     return WTAP_OPEN_MINE;
242 }
243
244 /* Find the next packet and parse it; called from wtap_read(). */
245 static gboolean vms_read(wtap *wth, int *err, gchar **err_info,
246     gint64 *data_offset)
247 {
248     gint64   offset = 0;
249
250     /* Find the next packet */
251 #ifdef TCPIPTRACE_FRAGMENTS_HAVE_HEADER_LINE
252     offset = vms_seek_next_packet(wth, err, err_info);
253 #else
254     offset = file_tell(wth->fh);
255 #endif
256     if (offset < 1) {
257         *err = file_error(wth->fh, err_info);
258         return FALSE;
259     }
260     *data_offset = offset;
261
262     /* Parse the packet */
263     return parse_vms_packet(wth->fh, &wth->rec, wth->rec_data, err, err_info);
264 }
265
266 /* Used to read packets in random-access fashion */
267 static gboolean
268 vms_seek_read(wtap *wth, gint64 seek_off, wtap_rec *rec,
269     Buffer *buf, int *err, gchar **err_info)
270 {
271     if (file_seek(wth->random_fh, seek_off - 1, SEEK_SET, err) == -1)
272         return FALSE;
273
274     if (!parse_vms_packet(wth->random_fh, rec, buf, err, err_info)) {
275         if (*err == 0)
276             *err = WTAP_ERR_SHORT_READ;
277         return FALSE;
278     }
279     return TRUE;
280 }
281
282 /* isdumpline assumes that dump lines start with some non-alphanumerics
283  * followed by 4 hex numbers - each 8 digits long, each hex number followed
284  * by 3 spaces.
285  */
286 static int
287 isdumpline( gchar *line )
288 {
289     int i, j;
290
291     while (*line && !g_ascii_isalnum(*line))
292         line++;
293
294     for (j=0; j<4; j++) {
295         for (i=0; i<8; i++, line++)
296             if (! g_ascii_isxdigit(*line))
297                 return FALSE;
298
299         for (i=0; i<3; i++, line++)
300             if (*line != ' ')
301                 return FALSE;
302     }
303
304     return g_ascii_isspace(*line);
305 }
306
307 /* Parses a packet record. */
308 static gboolean
309 parse_vms_packet(FILE_T fh, wtap_rec *rec, Buffer *buf, int *err, gchar **err_info)
310 {
311     char    line[VMS_LINE_LENGTH + 1];
312     int     num_items_scanned;
313     guint32 pkt_len = 0;
314     int     pktnum;
315     int     csec = 101;
316     struct tm tm;
317     char mon[4] = {'J', 'A', 'N', 0};
318     gchar  *p;
319     const gchar *endp;
320     static const gchar months[] = "JANFEBMARAPRMAYJUNJULAUGSEPOCTNOVDEC";
321     guint32 i;
322     int     offset = 0;
323     guint8 *pd;
324
325     tm.tm_year = 1970;
326     tm.tm_mon = 0;
327     tm.tm_mday = 1;
328     tm.tm_hour = 1;
329     tm.tm_min = 1;
330     tm.tm_sec = 1;
331
332     /* Skip lines until one starts with a hex number */
333     do {
334         if (file_gets(line, VMS_LINE_LENGTH, fh) == NULL) {
335             *err = file_error(fh, err_info);
336             if ((*err == 0) && (csec != 101)) {
337                 *err = WTAP_ERR_SHORT_READ;
338             }
339             return FALSE;
340         }
341         line[VMS_LINE_LENGTH] = '\0';
342
343         if ((csec == 101) && (p = strstr(line, "packet ")) != NULL
344             && (! strstr(line, "could not save "))) {
345             /* Find text in line starting with "packet ". */
346
347             /* First look for the Format 1 type sequencing */
348             num_items_scanned = sscanf(p,
349                                        "packet %9d at %2d-%3s-%4d %2d:%2d:%2d.%9d",
350                                        &pktnum, &tm.tm_mday, mon,
351                                        &tm.tm_year, &tm.tm_hour,
352                                        &tm.tm_min, &tm.tm_sec, &csec);
353             /* Next look for the Format 2 type sequencing */
354             if (num_items_scanned != 8) {
355               num_items_scanned = sscanf(p,
356                                          "packet seq # = %9d at %2d-%3s-%4d %2d:%2d:%2d.%9d",
357                                          &pktnum, &tm.tm_mday, mon,
358                                          &tm.tm_year, &tm.tm_hour,
359                                          &tm.tm_min, &tm.tm_sec, &csec);
360             }
361             /* if unknown format then exit with error        */
362             /* We will need to add code to handle new format */
363             if (num_items_scanned != 8) {
364                 *err = WTAP_ERR_BAD_FILE;
365                 *err_info = g_strdup("vms: header line not valid");
366                 return FALSE;
367             }
368         }
369         if ( (! pkt_len) && (p = strstr(line, "Length"))) {
370             p += sizeof("Length ");
371             while (*p && ! g_ascii_isdigit(*p))
372                 p++;
373
374             if ( !*p ) {
375                 *err = WTAP_ERR_BAD_FILE;
376                 *err_info = g_strdup("vms: Length field not valid");
377                 return FALSE;
378             }
379
380             if (!ws_strtou32(p, &endp, &pkt_len) || (*endp != '\0' && !g_ascii_isspace(*endp))) {
381                 *err = WTAP_ERR_BAD_FILE;
382                 *err_info = g_strdup_printf("vms: Length field '%s' not valid", p);
383                 return FALSE;
384             }
385             break;
386         }
387     } while (! isdumpline(line));
388     if (pkt_len > WTAP_MAX_PACKET_SIZE_STANDARD) {
389         /*
390          * Probably a corrupt capture file; return an error,
391          * so that our caller doesn't blow up trying to allocate
392          * space for an immensely-large packet.
393          */
394         *err = WTAP_ERR_BAD_FILE;
395         *err_info = g_strdup_printf("vms: File has %u-byte packet, bigger than maximum of %u",
396                                     pkt_len, WTAP_MAX_PACKET_SIZE_STANDARD);
397         return FALSE;
398     }
399
400     p = strstr(months, mon);
401     if (p)
402         tm.tm_mon = (int) (p - months) / 3;
403     tm.tm_year -= 1900;
404     tm.tm_isdst = -1;
405
406     rec->rec_type = REC_TYPE_PACKET;
407     rec->presence_flags = WTAP_HAS_TS;
408     rec->ts.secs = mktime(&tm);
409     rec->ts.nsecs = csec * 10000000;
410     rec->rec_header.packet_header.caplen = pkt_len;
411     rec->rec_header.packet_header.len = pkt_len;
412
413     /* Make sure we have enough room for the packet */
414     ws_buffer_assure_space(buf, pkt_len);
415     pd = ws_buffer_start_ptr(buf);
416
417     /* Convert the ASCII hex dump to binary data */
418     for (i = 0; i < pkt_len; i += 16) {
419         if (file_gets(line, VMS_LINE_LENGTH, fh) == NULL) {
420             *err = file_error(fh, err_info);
421             if (*err == 0) {
422                 *err = WTAP_ERR_SHORT_READ;
423             }
424             return FALSE;
425         }
426         line[VMS_LINE_LENGTH] = '\0';
427         if (i == 0) {
428             while (! isdumpline(line)) { /* advance to start of hex data */
429                 if (file_gets(line, VMS_LINE_LENGTH, fh) == NULL) {
430                     *err = file_error(fh, err_info);
431                     if (*err == 0) {
432                         *err = WTAP_ERR_SHORT_READ;
433                     }
434                     return FALSE;
435                 }
436                 line[VMS_LINE_LENGTH] = '\0';
437             }
438             while (line[offset] && !g_ascii_isxdigit(line[offset]))
439                 offset++;
440         }
441         if (!parse_single_hex_dump_line(line, pd, i,
442                                         offset, pkt_len - i)) {
443             *err = WTAP_ERR_BAD_FILE;
444             *err_info = g_strdup("vms: hex dump not valid");
445             return FALSE;
446         }
447     }
448     /* Avoid TCPIPTRACE-W-BUFFERSFUL, TCPIPtrace could not save n packets.
449      * errors.
450      *
451      * XXX - when we support packet drop report information in the
452      * Wiretap API, we should parse those lines and return "n" as
453      * a packet drop count. */
454     if (!file_gets(line, VMS_LINE_LENGTH, fh)) {
455         *err = file_error(fh, err_info);
456         if (*err == 0) {
457             /* There is no next line, so there's no "TCPIPtrace could not
458              * save n packets" line; not an error. */
459             return TRUE;
460         }
461         return FALSE;
462     }
463     return TRUE;
464 }
465
466 /*
467           1         2         3         4
468 0123456789012345678901234567890123456789012345
469    50010C0A   A34C0640   00009017   2C000045    0000    E..,....@.L....P
470    00000000   14945E52   0A00DC02 | 32010C0A    0010    ...2....R^......
471        0000 | B4050402   00003496   00020260    0020    `....4........
472 */
473
474 #define START_POS    7
475 #define HEX_LENGTH    ((8 * 4) + 7) /* eight clumps of 4 bytes with 7 inner spaces */
476 /* Take a string representing one line from a hex dump and converts the
477  * text to binary data. We check the printed offset with the offset
478  * we are passed to validate the record. We place the bytes in the buffer
479  * at the specified offset.
480  *
481  * Returns TRUE if good hex dump, FALSE if bad.
482  */
483 static gboolean
484 parse_single_hex_dump_line(char* rec, guint8 *buf, long byte_offset,
485                int in_off, int remaining_bytes) {
486
487     int        i;
488     char        *s;
489     int        value;
490     static const int offsets[16] = {39,37,35,33,28,26,24,22,17,15,13,11,6,4,2,0};
491     char lbuf[3] = {0,0,0};
492
493
494     /* Get the byte_offset directly from the record */
495     s = rec;
496     value = (int)strtoul(s + 45 + in_off, NULL, 16);    /* XXX - error check? */
497
498     if (value != byte_offset) {
499         return FALSE;
500     }
501
502     if (remaining_bytes > 16)
503         remaining_bytes = 16;
504
505     /* Read the octets right to left, as that is how they are displayed
506      * in VMS.
507      */
508
509     for (i = 0; i < remaining_bytes; i++) {
510         lbuf[0] = rec[offsets[i] + in_off];
511         lbuf[1] = rec[offsets[i] + 1 + in_off];
512
513         buf[byte_offset + i] = (guint8) strtoul(lbuf, NULL, 16);
514     }
515
516     return TRUE;
517 }
518
519 /*
520  * Editor modelines  -  http://www.wireshark.org/tools/modelines.html
521  *
522  * Local variables:
523  * c-basic-offset: 4
524  * tab-width: 8
525  * indent-tabs-mode: nil
526  * End:
527  *
528  * vi: set shiftwidth=4 tabstop=8 expandtab:
529  * :indentSize=4:tabSize=8:noTabs=true:
530  */