Skip to content

Commit 4dec99d

Browse files
committed
Don't open output file for dry options
1 parent aeddac8 commit 4dec99d

3 files changed

Lines changed: 51 additions & 39 deletions

File tree

src/main.c

Lines changed: 49 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -71,23 +71,23 @@ int main(int argc, char *argv[])
7171
stream_targets = 0;
7272
enum operating_mode mode;
7373
uint8_t ip_type, source_mac[6], router_mac[6], source_addr[16];
74-
char *interface;
74+
char *interface, *outfile;
7575
struct ports ports;
76-
FILE *outfile, *readscan;
76+
FILE *readscan;
7777
const struct outputdef *outdef;
7878

7979
mode = M_SCAN;
8080
ip_type = IP_TYPE_TCP;
8181
interface = NULL; // automatically picked
82-
outfile = stdout;
83-
outdef = NULL;
82+
outfile = NULL; // stdout
83+
readscan = NULL;
84+
outdef = NULL; // defaults to 'list'
8485

8586
srand(time(NULL) - (getpid() * argc) + monotonic_ms());
8687
memset(source_mac, 0xff, 6);
8788
memset(router_mac, 0xff, 6);
8889
memset(source_addr, 0xff, 16);
8990
init_ports(&ports);
90-
readscan = NULL;
9191

9292
while(1) {
9393
int c = getopt_long(argc, argv, "hp:o:qbu", long_options, NULL);
@@ -120,6 +120,7 @@ int main(int argc, char *argv[])
120120
break;
121121

122122
case 2002:
123+
free(interface);
123124
interface = strdup(optarg);
124125
break;
125126
case 2003:
@@ -207,27 +208,10 @@ int main(int argc, char *argv[])
207208
return 1;
208209
}
209210
break;
210-
case 'o': {
211-
FILE *f = strcmp(optarg, "-") == 0 ? stdout : fopen(optarg, "wb");
212-
if(!f) {
213-
perror("open output file");
214-
return 1;
215-
}
216-
char *dot = find_dot(optarg);
217-
if(!outdef && dot) {
218-
const char *suggest = NULL;
219-
if(!strcmp(dot+1, "bin"))
220-
suggest = "binary";
221-
else if(!strcmp(dot+1, "json"))
222-
suggest = "json";
223-
if(suggest) {
224-
log_warning("It looks like you might want a different "
225-
"output format, try --output-format %s.", suggest);
226-
}
227-
}
228-
outfile = f;
211+
case 'o':
212+
free(outfile);
213+
outfile = strdup(optarg);
229214
break;
230-
}
231215
case 'q':
232216
quiet = 1;
233217
break;
@@ -243,9 +227,6 @@ int main(int argc, char *argv[])
243227
}
244228
}
245229

246-
if(!outdef)
247-
outdef = &output_list;
248-
249230
int max_args = 1;
250231
if(mode == M_READSCAN) {
251232
max_args = 0;
@@ -317,10 +298,38 @@ int main(int argc, char *argv[])
317298
}
318299
}
319300

301+
FILE *outfile_f = NULL;
302+
if(mode == M_READSCAN || mode == M_SCAN) {
303+
if(!outfile || !strcmp(outfile, "-"))
304+
outfile_f = stdout;
305+
else
306+
outfile_f = fopen(outfile, "wb");
307+
if(!outfile_f) {
308+
perror("open output file");
309+
return 1;
310+
}
311+
char *dot = find_dot(outfile);
312+
if(!outdef && dot) {
313+
const char *suggest = NULL;
314+
if(!strcmp(dot+1, "bin"))
315+
suggest = "binary";
316+
else if(!strcmp(dot+1, "json"))
317+
suggest = "json";
318+
if(suggest) {
319+
log_warning("It looks like you might want a different "
320+
"output format, try --output-format %s.", suggest);
321+
}
322+
}
323+
324+
// now also apply the default outdef
325+
if(!outdef)
326+
outdef = &output_list;
327+
}
328+
320329
int r;
321330
if(mode == M_READSCAN) {
322331
scan_reader_set_general(show_closed, banners);
323-
scan_reader_set_output(outfile, outdef);
332+
scan_reader_set_output(outfile_f, outdef);
324333
r = scan_reader_main(readscan) < 0 ? 1 : 0;
325334
} else if(mode == M_PRINT_HOSTS) {
326335
uint8_t addr[16];
@@ -445,35 +454,35 @@ int main(int argc, char *argv[])
445454
rawsock_ip_settings(source_addr, ttl);
446455
scan_set_general(&ports, max_rate, show_closed, banners);
447456
scan_set_network(source_addr, source_port, ip_type);
448-
scan_set_output(outfile, outdef);
457+
scan_set_output(outfile_f, outdef);
449458
r = scan_main(interface, quiet) < 0 ? 1 : 0;
450459
}
451460
}
452461

453-
if (interface)
454-
free(interface);
455462
target_gen_fini();
456-
fclose(outfile);
457-
if(mode == M_READSCAN)
463+
free(interface);
464+
free(outfile);
465+
if(outfile_f)
466+
fclose(outfile_f);
467+
if(readscan)
458468
fclose(readscan);
459469
return r;
460470
}
461471

462472
static int read_targets_from_file(const char *filename, int stream_targets)
463473
{
464-
FILE *f;
465-
char buf[256];
466-
f = fopen(filename, "r");
474+
FILE *f = fopen(filename, "r");
467475
if(!f) {
468476
perror("open target list");
469477
return -1;
470478
}
471479

472480
if(stream_targets) {
473-
target_gen_set_streaming(f);
481+
target_gen_set_streaming(f); // takes ownership
474482
return 0;
475483
}
476484

485+
char buf[256];
477486
while(fgets(buf, sizeof(buf), f) != NULL) {
478487
struct targetspec t;
479488

@@ -591,6 +600,8 @@ static inline bool is_all_ff(const uint8_t *buf, int len)
591600

592601
static inline char *find_dot(char *str)
593602
{
603+
if(!str)
604+
return NULL;
594605
char *p = str + strlen(str);
595606
do {
596607
if(*p == '/')

src/scan.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ void scan_set_network(const uint8_t *_source_addr, int _source_port, uint8_t _ip
8585
void scan_set_output(FILE *_outfile, const struct outputdef *_outdef)
8686
{
8787
outfile = _outfile;
88+
assert(_outdef);
8889
memcpy(&outdef, _outdef, sizeof(struct outputdef));
8990
}
9091

src/target-gen.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ void target_gen_fini(void)
105105
{
106106
free(cache);
107107
free(targets);
108-
if(mode_streaming)
108+
if(targets_from)
109109
fclose(targets_from);
110110
}
111111

0 commit comments

Comments
 (0)