Fix some compiler warnings.
[ira/wip.git] / source3 / utils / log2pcaphex.c
1 /* 
2    Unix SMB/CIFS implementation.
3    Utility to extract pcap files from samba (log level 10) log files
4
5    Copyright (C) Jelmer Vernooij 2003
6    Thanks to Tim Potter for the genial idea
7
8    Portions (from capconvert.c) (C) Andrew Tridgell 1997
9    Portions (from text2pcap.c) (C) Ashok Narayanan 2001
10
11    Example use with -h parameter: 
12         log2pcaphex < samba-log-file | text2pcap -T 139,139 - foo.pcap
13
14    TODO: Have correct IP and TCP checksums.
15
16    This program is free software; you can redistribute it and/or modify
17    it under the terms of the GNU General Public License as published by
18    the Free Software Foundation; either version 2 of the License, or
19    (at your option) any later version.
20
21    This program is distributed in the hope that it will be useful,
22    but WITHOUT ANY WARRANTY; without even the implied warranty of
23    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
24    GNU General Public License for more details.
25
26    You should have received a copy of the GNU General Public License
27    along with this program; if not, write to the Free Software
28    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
29 */
30
31 #include "includes.h"
32 #include <assert.h>
33
34 int quiet = 0;
35 int hexformat = 0;
36
37 #define itoa(a) ((a) < 0xa?'0'+(a):'A' + (a-0xa))
38
39 #include <stdlib.h>
40 #include <unistd.h>
41 #include <memory.h>
42 #include <sys/time.h>
43 #include <stdio.h>
44 #include <fcntl.h>
45
46 #define TCPDUMP_MAGIC 0xa1b2c3d4
47
48 /* tcpdump file format */
49 struct tcpdump_file_header {
50         uint32 magic;
51         uint16 major;
52         uint16 minor;
53         int32 zone;
54         uint32 sigfigs;
55         uint32 snaplen;
56         uint32 linktype;
57 };
58
59 struct tcpdump_packet {
60         struct timeval ts;
61         uint32 caplen;
62         uint32 len;
63 };
64
65 typedef struct {
66     uint8  ver_hdrlen;
67     uint8  dscp;
68     uint16 packet_length;
69     uint16 identification;
70     uint8  flags;
71     uint8  fragment;
72     uint8  ttl;
73     uint8  protocol;
74     uint16 hdr_checksum;
75     uint32 src_addr;
76     uint32 dest_addr;
77 } hdr_ip_t;
78
79 static hdr_ip_t HDR_IP = {0x45, 0, 0, 0x3412, 0, 0, 0xff, 6, 0, 0x01010101, 0x02020202};
80
81 typedef struct {
82     uint16 source_port;
83     uint16 dest_port;
84     uint32 seq_num;
85     uint32 ack_num;
86     uint8  hdr_length;
87     uint8  flags;
88     uint16 window;
89     uint16 checksum;
90     uint16 urg;
91 } hdr_tcp_t;
92
93 static hdr_tcp_t HDR_TCP = {139, 139, 0, 0, 0x50, 0, 0, 0, 0};
94
95 void print_pcap_header(FILE *out)
96 {
97         struct tcpdump_file_header h;
98         h.magic = TCPDUMP_MAGIC;
99         h.major = 2;
100         h.minor = 4;
101         h.zone = 0;
102         h.sigfigs = 0;
103         h.snaplen = 102400; /* As long packets as possible */
104         h.linktype = 101; /* Raw IP */
105         fwrite(&h, sizeof(struct tcpdump_file_header), 1, out);
106 }
107
108 void print_pcap_packet(FILE *out, unsigned char *data, long length, long caplen)
109 {
110         static int i = 0;
111         struct tcpdump_packet p;
112         i++;
113         p.ts.tv_usec = 0;
114         p.ts.tv_sec = 0;
115         p.caplen = caplen;
116         p.len = length;
117         fwrite(&p, sizeof(struct tcpdump_packet), 1, out);
118         fwrite(data, sizeof(unsigned char), caplen, out);
119 }
120
121 void print_hex_packet(FILE *out, unsigned char *data, long length)
122 {
123         long i,cur = 0;
124         while(cur < length) {
125                 fprintf(out, "%06lX ", cur);
126                 for(i = cur; i < length && i < cur + 16; i++) {
127                         fprintf(out, "%02x ", data[i]);
128                 }
129         
130                 cur = i;
131                 fprintf(out, "\n");
132         }
133 }
134
135 void print_netbios_packet(FILE *out, unsigned char *data, long length, long actual_length)
136 {       
137         unsigned char *newdata; long offset = 0;
138         long newlen;
139         
140         newlen = length+sizeof(HDR_IP)+sizeof(HDR_TCP);
141         newdata = malloc(newlen);
142
143         HDR_IP.packet_length = htons(newlen);
144         HDR_TCP.window = htons(0x2000);
145         HDR_TCP.source_port = HDR_TCP.dest_port = htons(139);
146
147         memcpy(newdata+offset, &HDR_IP, sizeof(HDR_IP));offset+=sizeof(HDR_IP);
148         memcpy(newdata+offset, &HDR_TCP, sizeof(HDR_TCP));offset+=sizeof(HDR_TCP);
149         memcpy(newdata+offset,data,length);
150         
151         print_pcap_packet(out, newdata, newlen, actual_length+offset);
152         free(newdata);
153 }
154
155 unsigned char *curpacket = NULL;
156 long curpacket_len = 0;
157
158 void read_log_msg(FILE *in, unsigned char **_buffer, long *buffersize, long *data_offset, long *data_length)
159 {
160         unsigned char *buffer;
161         int tmp; long i;
162         assert(fscanf(in, " size=%ld\n", buffersize));
163         *buffersize+=4; /* for netbios */
164         buffer = malloc(*buffersize);
165         memset(buffer, 0, *buffersize);
166         /* NetBIOS */
167         buffer[0] = 0x00;
168         buffer[1] = 0x00;
169         memcpy(buffer+2, &buffersize, 2);
170         buffer[4] = 0xFF;
171         buffer[5] = 'S';
172         buffer[6] = 'M';
173         buffer[7] = 'B';
174         assert(fscanf(in, "  smb_com=0x%x\n", &tmp)); buffer[smb_com] = tmp;
175         assert(fscanf(in, "  smb_rcls=%d\n", &tmp)); buffer[smb_rcls] = tmp;
176         assert(fscanf(in, "  smb_reh=%d\n", &tmp)); buffer[smb_reh] = tmp;
177         assert(fscanf(in, "  smb_err=%d\n", &tmp)); memcpy(buffer+smb_err, &tmp, 2);
178         assert(fscanf(in, "  smb_flg=%d\n", &tmp)); buffer[smb_flg] = tmp;
179         assert(fscanf(in, "  smb_flg2=%d\n", &tmp)); memcpy(buffer+smb_flg2, &tmp, 2);
180         assert(fscanf(in, "  smb_tid=%d\n", &tmp)); memcpy(buffer+smb_tid, &tmp, 2);
181         assert(fscanf(in, "  smb_pid=%d\n", &tmp)); memcpy(buffer+smb_pid, &tmp, 2);
182         assert(fscanf(in, "  smb_uid=%d\n", &tmp)); memcpy(buffer+smb_uid, &tmp, 2);
183         assert(fscanf(in, "  smb_mid=%d\n", &tmp)); memcpy(buffer+smb_mid, &tmp, 2);
184         assert(fscanf(in, "  smt_wct=%d\n", &tmp)); buffer[smb_wct] = tmp;
185         for(i = 0; i < buffer[smb_wct]; i++) {
186                 assert(fscanf(in, "  smb_vwv[%*2d]=%*5d (0x%X)\n", &tmp));
187                 memcpy(buffer+smb_vwv+i*2, &tmp, 2);
188         }
189
190         *data_offset = smb_vwv+buffer[smb_wct]*2;
191         assert(fscanf(in, "  smb_bcc=%ld\n", data_length)); buffer[(*data_offset)] = *data_length;
192         (*data_offset)+=2;
193         *_buffer = buffer;
194 }
195
196 long read_log_data(FILE *in, unsigned char *buffer, long data_length)
197 {
198         long i, addr; char real[2][16]; int ret;
199         unsigned char tmp;
200         for(i = 0; i < data_length; i++) {
201                 if(i % 16 == 0){
202                         if(i != 0) { /* Read data after each line */
203                                 assert(fscanf(in, "%8s %8s", real[0], real[1]) == 2);
204                         }
205                         ret = fscanf(in, "  [%03lX]", &addr);
206                         if(!ret) {
207                                 if(!quiet)fprintf(stderr, "Only first %ld bytes are logged, packet trace will be incomplete\nTry a higher log level\n", i);
208                                 return i-1;
209                         }
210                         assert(addr == i);
211                 }
212                 if(!fscanf(in, "%02lX", &tmp)) {
213                         if(!quiet)fprintf(stderr, "Only first %ld bytes are logged, packet trace will be incomplete\nTry a higher log level\n", i-1);
214                         return i-1;
215                 }
216                 buffer[i] = tmp;
217         }
218         return data_length;
219 }
220
221 int main (int argc, char **argv)
222 {
223         const char *infile, *outfile;
224         FILE *out, *in;
225         int opt;
226         poptContext pc;
227         char buffer[4096];
228         long data_offset, data_length;
229         long data_bytes_read;
230         int in_packet = 0;
231         struct poptOption long_options[] = {
232                 POPT_AUTOHELP
233                 { "quiet", 'q', POPT_ARG_NONE, &quiet, 0, "Be quiet, don't output warnings" },
234                 { "hex", 'h', POPT_ARG_NONE, &hexformat, 0, "Output format readable by text2pcap" },
235                 POPT_TABLEEND
236         };
237         
238         pc = poptGetContext(NULL, argc, (const char **) argv, long_options,
239                             POPT_CONTEXT_KEEP_FIRST);
240         poptSetOtherOptionHelp(pc, "[<infile> [<outfile>]]");
241         
242         
243         while((opt = poptGetNextOpt(pc)) != -1) {
244                 switch (opt) {
245                 }
246         }
247
248         poptGetArg(pc); /* Drop argv[0], the program name */
249
250         infile = poptGetArg(pc);
251
252         if(infile) {
253                 in  = fopen(infile, "r");
254                 if(!in) {
255                         perror("fopen");
256                         return 1;
257                 }
258         } else in = stdin;
259         
260         outfile = poptGetArg(pc);
261
262         if(outfile) {
263                 out = fopen(outfile, "w+");
264                 if(!out) { 
265                         perror("fopen"); 
266                         fprintf(stderr, "Can't find %s, using stdout...\n", outfile);
267                 }
268         }
269
270         if(!outfile) out = stdout;
271
272         if(!hexformat)print_pcap_header(out);
273
274         while(!feof(in)) {
275                 fgets(buffer, sizeof(buffer), in);
276                 if(buffer[0] == '[') { /* Header */
277                         if(strstr(buffer, "show_msg")) {
278                                 in_packet++;
279                                 if(in_packet == 1)continue;
280                                 read_log_msg(in, &curpacket, &curpacket_len, &data_offset, &data_length);
281                         } else if(in_packet && strstr(buffer, "dump_data")) {
282                                 data_bytes_read = read_log_data(in, curpacket+data_offset, data_length);
283                         }  else { 
284                                 if(in_packet){ 
285                                         if(hexformat) print_hex_packet(out, curpacket, curpacket_len); 
286                                         else print_netbios_packet(out, curpacket, curpacket_len, data_bytes_read+data_offset);
287                                         free(curpacket); 
288                                 }
289                                 in_packet = 0;
290                         }
291                 } 
292         }
293
294         return 0;
295 }