merge soc07 r5100 - Changing fprintf(stderr, )'s to error()'s (or fatal() if followed by and exit()). Besides providing consistency, this also allows more errors to actually be logged with --log-errors.

This commit is contained in:
fyodor 2007-08-11 05:16:56 +00:00
parent 73440e4878
commit 6c06f51bd7
10 changed files with 49 additions and 60 deletions

View file

@ -433,7 +433,7 @@ void NmapOps::ValidateOptions() {
#if !defined(LINUX) && !defined(OPENBSD) && !defined(FREEBSD) && !defined(NETBSD)
if (fragscan) {
fprintf(stderr, "Warning: Packet fragmentation selected on a host other than Linux, OpenBSD, FreeBSD, or NetBSD. This may or may not work.\n");
error("Warning: Packet fragmentation selected on a host other than Linux, OpenBSD, FreeBSD, or NetBSD. This may or may not work.");
}
#endif

View file

@ -189,7 +189,7 @@ int TargetGroup::parse_expr(const char * const target_expr, int af) {
s = strtok(NULL, ""); /* find the end of the token from hostexp */
netmask = ( s ) ? atoi(s) : 32;
if ((int) netmask <= 0 || netmask > 32) {
fprintf(stderr, "Illegal netmask value (%d), must be /1 - /32 . Assuming /32 (one host)\n", netmask);
error("Illegal netmask value (%d), must be /1 - /32 . Assuming /32 (one host)", netmask);
netmask = 32;
}
for(i=0; *(hostexp + i); i++)
@ -210,7 +210,7 @@ int TargetGroup::parse_expr(const char * const target_expr, int af) {
if (count > 1)
error("Warning: Hostname %s resolves to %d IPs. Using %s.", target_net, count, inet_ntoa(*((struct in_addr *)target->h_addr_list[0])));
} else {
fprintf(stderr, "Failed to resolve given hostname/IP: %s. Note that you can't use '/mask' AND '1-4,7,100-' style IP ranges\n", target_net);
error("Failed to resolve given hostname/IP: %s. Note that you can't use '/mask' AND '1-4,7,100-' style IP ranges", target_net);
free(hostexp);
return 1;
}
@ -289,7 +289,7 @@ int TargetGroup::parse_expr(const char * const target_expr, int af) {
hints.ai_family = PF_INET6;
rc = getaddrinfo(hostexp, NULL, &hints, &result);
if (rc != 0) {
fprintf(stderr, "Failed to resolve given IPv6 hostname/IP: %s. Note that you can't use '/mask' or '[1-4,7,100-]' style ranges for IPv6. Error code %d: %s\n", hostexp, rc, gai_strerror(rc));
error("Failed to resolve given IPv6 hostname/IP: %s. Note that you can't use '/mask' or '[1-4,7,100-]' style ranges for IPv6. Error code %d: %s", hostexp, rc, gai_strerror(rc));
free(hostexp);
if (result) freeaddrinfo(result);
return 1;

39
nmap.cc
View file

@ -899,7 +899,7 @@ int nmap_main(int argc, char *argv[]) {
case 'b':
o.bouncescan++;
if (parse_bounce_argument(&ftp, optarg) < 0 ) {
fprintf(stderr, "Your argument to -b is b0rked. Use the normal url style: user:pass@server:port or just use server and use default anon login\n Use -h for help\n");
error("Your argument to -b is b0rked. Use the normal url style: user:pass@server:port or just use server and use default anon login\n Use -h for help");
}
break;
case 'D':
@ -1079,7 +1079,7 @@ int nmap_main(int argc, char *argv[]) {
break;
case 's':
if (!*optarg) {
fprintf(stderr, "An option is required for -s, most common are -sT (tcp scan), -sS (SYN scan), -sF (FIN scan), -sU (UDP scan) and -sP (Ping scan)");
error("An option is required for -s, most common are -sT (tcp scan), -sS (SYN scan), -sF (FIN scan), -sU (UDP scan) and -sP (Ping scan)");
printusage(argv[0], -1);
}
p = optarg;
@ -1368,9 +1368,8 @@ int nmap_main(int argc, char *argv[]) {
if ((target = gethostbyname(ftp.server_name)))
memcpy(&ftp.server, target->h_addr_list[0], 4);
else {
fprintf(stderr, "Failed to resolve ftp bounce proxy hostname/IP: %s\n",
fatal("Failed to resolve ftp bounce proxy hostname/IP: %s",
ftp.server_name);
exit(1);
}
} else if (o.verbose)
log_write(LOG_STDOUT, "Resolved ftp bounce attack proxy to %s (%s).\n",
@ -1439,7 +1438,7 @@ int nmap_main(int argc, char *argv[]) {
#endif
if (o.max_parallelism && (i = max_sd()) && i < o.max_parallelism) {
fprintf(stderr, "WARNING: Your specified max_parallel_sockets of %d, but your system says it might only give us %d. Trying anyway\n", o.max_parallelism, i);
error("WARNING: Your specified max_parallel_sockets of %d, but your system says it might only give us %d. Trying anyway", o.max_parallelism, i);
}
if (o.debugging > 1) log_write(LOG_STDOUT, "The max # of sockets we are using is: %d\n", o.max_parallelism);
@ -1585,7 +1584,7 @@ int nmap_main(int argc, char *argv[]) {
o.setSourceSockAddr(&ss, sslen);
currenths->setSourceSockAddr(&ss, sslen);
if (! sourceaddrwarning) {
fprintf(stderr, "WARNING: We could not determine for sure which interface to use, so we are guessing %s . If this is wrong, use -S <my_IP_address>.\n", inet_socktop(&ss));
error("WARNING: We could not determine for sure which interface to use, so we are guessing %s . If this is wrong, use -S <my_IP_address>.", inet_socktop(&ss));
sourceaddrwarning = 1;
}
}
@ -1954,7 +1953,7 @@ void init_socket(int sd) {
if (setsockopt(sd, SOL_SOCKET, SO_LINGER, (const char *) &l, sizeof(struct linger)))
{
fprintf(stderr, "Problem setting socket SO_LINGER, errno: %d\n", socket_errno());
error("Problem setting socket SO_LINGER, errno: %d", socket_errno());
perror("setsockopt");
}
if (o.spoofsource && !bind_failed)
@ -1963,7 +1962,7 @@ void init_socket(int sd) {
res=bind(sd, (struct sockaddr*)&ss, sslen);
if (res<0)
{
fprintf(stderr, "init_socket: Problem binding source address (%s), errno :%d\n", inet_socktop(&ss), socket_errno());
error("init_socket: Problem binding source address (%s), errno :%d", inet_socktop(&ss), socket_errno());
perror("bind");
bind_failed=1;
}
@ -2438,8 +2437,7 @@ int ftp_anon_connect(struct ftpinfo *ftp) {
sock.sin_port = htons(ftp->port);
res = connect(sd, (struct sockaddr *) &sock, sizeof(struct sockaddr_in));
if (res < 0 ) {
fprintf(stderr, "Your ftp bounce proxy server won't talk to us!\n");
exit(1);
fatal("Your ftp bounce proxy server won't talk to us!");
}
if (o.verbose || o.debugging) log_write(LOG_STDOUT, "Connected:");
while ((res = recvtime(sd, recvbuf, sizeof(recvbuf) - 1,7, NULL)) > 0)
@ -2463,9 +2461,7 @@ int ftp_anon_connect(struct ftpinfo *ftp) {
recvbuf[res] = '\0';
if (o.debugging) log_write(LOG_STDOUT, "sent username, received: %s", recvbuf);
if (recvbuf[0] == '5') {
fprintf(stderr, "Your ftp bounce server doesn't like the username \"%s\"\n",
ftp->user);
exit(1);
fatal("Your ftp bounce server doesn't like the username \"%s\"", ftp->user);
}
snprintf(command, 511, "PASS %s\r\n", ftp->pass);
@ -2476,14 +2472,13 @@ int ftp_anon_connect(struct ftpinfo *ftp) {
perror("recv problem from ftp bounce server\n");
exit(1);
}
if (!res) fprintf(stderr, "Timeout from bounce server ...");
if (!res) error("Timeout from bounce server ...");
else {
recvbuf[res] = '\0';
if (o.debugging) log_write(LOG_STDOUT, "sent password, received: %s", recvbuf);
if (recvbuf[0] == '5') {
fprintf(stderr, "Your ftp bounce server refused login combo (%s/%s)\n",
fatal("Your ftp bounce server refused login combo (%s/%s)",
ftp->user, ftp->pass);
exit(1);
}
}
while ((res = recvtime(sd, recvbuf, sizeof(recvbuf) - 1,2, NULL)) > 0)
@ -2522,37 +2517,37 @@ void sigdie(int signo) {
switch(signo) {
case SIGINT:
fprintf(stderr, "caught SIGINT signal, cleaning up\n");
error("caught SIGINT signal, cleaning up");
break;
#ifdef SIGTERM
case SIGTERM:
fprintf(stderr, "caught SIGTERM signal, cleaning up\n");
error("caught SIGTERM signal, cleaning up");
break;
#endif
#ifdef SIGHUP
case SIGHUP:
fprintf(stderr, "caught SIGHUP signal, cleaning up\n");
error("caught SIGHUP signal, cleaning up");
break;
#endif
#ifdef SIGSEGV
case SIGSEGV:
fprintf(stderr, "caught SIGSEGV signal, cleaning up\n");
error("caught SIGSEGV signal, cleaning up");
abt = 1;
break;
#endif
#ifdef SIGBUS
case SIGBUS:
fprintf(stderr, "caught SIGBUS signal, cleaning up\n");
error("caught SIGBUS signal, cleaning up");
abt = 1;
break;
#endif
default:
fprintf(stderr, "caught signal %d, cleaning up\n", signo);
error("caught signal %d, cleaning up", signo);
abt = 1;
break;
}

View file

@ -149,7 +149,7 @@ while(!id) id = get_random_uint();
/* check that required fields are there and not too silly */
if ( !victim || !dport || (!eth && sd < 0)) {
fprintf(stderr, "%s: One or more of your parameters suck!\n", __func__);
error("%s: One or more of your parameters suck!", __func__);
return NULL;
}
@ -824,7 +824,7 @@ static FingerPrint *get_fingerprint(Target *target, struct seq_info *si) {
if ((tcp->th_flags & TH_RST)) {
/* readtcppacket((char *) ip, ntohs(ip->ip_len));*/
if (si->responses == 0) {
fprintf(stderr, "WARNING: RST from port %lu -- is this port really open?\n", openport);
error("WARNING: RST from port %lu -- is this port really open?", openport);
/* We used to quit in this case, but left-overs from a SYN
scan or lame-ass TCP wrappers can cause this! */
}
@ -2186,7 +2186,7 @@ while(fgets(line, sizeof(line), fp)) {
if (DB->MatchPoints) fatal("Found MatchPoints directive on line %d of %s even though it has previously been seen in the file", lineno, fname);
parsingMatchPoints = true;
} else {
fprintf(stderr, "Parse error on line %d of nmap-os-fingerprints file: %s\n", lineno, line);
error("Parse error on line %d of nmap-os-fingerprints file: %s", lineno, line);
continue;
}
@ -2231,7 +2231,7 @@ while(fgets(line, sizeof(line), fp)) {
p = line;
q = strchr(line, '(');
if (!q) {
fprintf(stderr, "Parse error on line %d of nmap-os-fingerprints file: %s\n", lineno, line);
error("Parse error on line %d of nmap-os-fingerprints file: %s", lineno, line);
goto top;
}
*q = '\0';
@ -2244,7 +2244,7 @@ while(fgets(line, sizeof(line), fp)) {
*q = '(';
q = strchr(p, ')');
if (!q) {
fprintf(stderr, "Parse error on line %d of nmap-os-fingerprints file: %s\n", lineno, line);
error("Parse error on line %d of nmap-os-fingerprints file: %s", lineno, line);
goto top;
}
*q = '\0';

View file

@ -2017,7 +2017,7 @@ bool HostOsScan::processTSeqResp(HostOsScanStats *hss, struct ip *ip, int replyN
if ((tcp->th_flags & TH_RST)) {
if (hss->si.responses == 0) {
fprintf(stderr, "WARNING: RST from %s port %d -- is this port really open?\n",
error("WARNING: RST from %s port %d -- is this port really open?",
hss->target->targetipstr(), hss->openTCPPort);
}
return false;
@ -3053,7 +3053,7 @@ int send_closedudp_probe_2(struct udpprobeinfo &upi, int sd,
/* check that required fields are there and not too silly */
if ( !victim || !sport || !dport || (!eth && sd < 0)) {
fprintf(stderr, "%s: One or more of your parameters suck!\n", __func__);
error("%s: One or more of your parameters suck!", __func__);
return 1;
}

View file

@ -882,8 +882,7 @@ void log_vwrite(int logt, const char *fmt, va_list ap) {
va_end(apcopy);
return;
} else if (len < 0) {
fprintf(stderr, "vsnprintf returned %d in %s -- bizarre. Quitting.\n", len, __func__);
exit(1);
fatal("vsnprintf returned %d in %s -- bizarre. Quitting.", len, __func__);
} else if (len >= writebuflen) {
/* Didn't have enough space. Expand writebuf and try again */
free(writebuf);
@ -891,24 +890,21 @@ void log_vwrite(int logt, const char *fmt, va_list ap) {
writebuf = (char *) safe_malloc(writebuflen);
len = vsnprintf(writebuf, writebuflen, fmt, apcopy);
if (len <= 0 || len >= writebuflen) {
fprintf(stderr, "%s: vnsprintf failed. Even after increasing bufferlen to %d, vsnprintf returned %d (logt == %d). Please email this message to fyodor@insecure.org. Quitting.\n", __func__, writebuflen, len, logt);
exit(1);
fatal("%s: vnsprintf failed. Even after increasing bufferlen to %d, vsnprintf returned %d (logt == %d). Please email this message to fyodor@insecure.org. Quitting.", __func__, writebuflen, len, logt);
}
}
if (logt == LOG_SKID && !skid_noxlate)
skid_output(writebuf);
rc = fwrite(writebuf,len,1,o.logfd[fileidx]);
if (rc != 1) {
fprintf(stderr, "Failed to write %d bytes of data to (logt==%d) stream. fwrite returned %d. Quitting.\n", len, logt, rc);
exit(1);
fatal("Failed to write %d bytes of data to (logt==%d) stream. fwrite returned %d. Quitting.", len, logt, rc);
}
va_end(apcopy);
}
break;
default:
fprintf(stderr, "%s(): Passed unknown log type (%d). Note that this function, unlike log_write, can only handle one log type at a time (no bitmasks)\n", __func__, logt);
exit(1);
fatal("%s(): Passed unknown log type (%d). Note that this function, unlike log_write, can only handle one log type at a time (no bitmasks)", __func__, logt);
}
return;
@ -1783,7 +1779,7 @@ void printfinaloutput() {
&& o.scriptupdatedb == 0
#endif
)
fprintf(stderr, "WARNING: No targets were specified, so 0 hosts scanned.\n");
error("WARNING: No targets were specified, so 0 hosts scanned.");
if (o.numhosts_scanned == 1 && o.numhosts_up == 0 && !o.listscan &&
o.pingtype != PINGTYPE_NONE)
log_write(LOG_STDOUT, "Note: Host seems down. If it is really up, but blocking our ping probes, try -P0\n");

View file

@ -3572,7 +3572,7 @@ void bounce_scan(Target *target, u16 *portarray, int numports,
i--;
}
else {
fprintf(stderr, "Our socket descriptor is dead and we are out of retries. Giving up.\n");
error("Our socket descriptor is dead and we are out of retries. Giving up.");
close(sd);
ftp->sd = -1;
return;
@ -3588,15 +3588,13 @@ void bounce_scan(Target *target, u16 *portarray, int numports,
portarray[i], recvbuf);
if (recvbuf[0] == '5') {
if (portarray[i] > 1023) {
fprintf(stderr, "Your ftp bounce server sucks, it won't let us feed bogus ports!\n");
exit(1);
fatal("Your ftp bounce server sucks, it won't let us feed bogus ports!");
}
else {
fprintf(stderr, "Your ftp bounce server doesn't allow privileged ports, skipping them.\n");
error("Your ftp bounce server doesn't allow privileged ports, skipping them.");
while(i < numports && portarray[i] < 1024) i++;
if (!portarray[i]) {
fprintf(stderr, "And you didn't want to scan any unpriviliged ports. Giving up.\n");
exit(1);
fatal("And you didn't want to scan any unpriviliged ports. Giving up.");
}
}
}
@ -3617,7 +3615,7 @@ void bounce_scan(Target *target, u16 *portarray, int numports,
if (!strncmp(recvbuf, "500", 3)) {
/* fuck, we are not aligned properly */
if (o.verbose || o.debugging)
fprintf(stderr, "FTP command misalignment detected ... correcting.\n");
error("FTP command misalignment detected ... correcting.");
res = recvtime(sd, recvbuf, 2048,10, NULL);
}
if (recvbuf[0] == '1' || recvbuf[0] == '2') {

View file

@ -1095,8 +1095,8 @@ if (ptech.icmpscan) {
&& sock_err != WSAEADDRNOTAVAIL
#endif
) {
fprintf(stderr, "sendto in %s returned %d (should be 8)!\n", __func__, res);
fprintf(stderr, "sendto: %s\n", strerror(sock_err));
error("sendto in %s returned %d (should be 8)!", __func__, res);
error("sendto: %s", strerror(sock_err));
}
} else {
send_ip_raw(rawsd, ethptr,
@ -1216,7 +1216,7 @@ while(pt->block_unaccounted) {
break;
default:
snprintf (buf, sizeof(buf), "Strange read error from %s", hostbatch[hostindex]->targetipstr());
fprintf(stderr, "%s: %s\n", buf, strerror(sock_err));
error("%s: %s", buf, strerror(sock_err));
break;
}
} else {
@ -1346,7 +1346,7 @@ TargetGroup* load_exclude(FILE *fExclude, char *szExclude) {
while ((char *)0 != pc) {
if(excludelist[i].parse_expr(pc,o.af()) == 0) {
if (o.debugging > 1)
fprintf(stderr, "Loaded exclude target of: %s\n", pc);
error("Loaded exclude target of: %s", pc);
++i;
}
pc=strtok(NULL, "\t\n ");
@ -1361,7 +1361,7 @@ TargetGroup* load_exclude(FILE *fExclude, char *szExclude) {
while (NULL != pc) {
if(excludelist[i].parse_expr(pc,o.af()) == 0) {
if (o.debugging >1)
fprintf(stderr, "Loaded exclude target of: %s\n", pc);
error("Loaded exclude target of: %s", pc);
++i;
}
@ -1410,12 +1410,12 @@ int dumpExclude(TargetGroup *exclude_group) {
case TargetGroup::IPV4_NETMASK:
exclude_group[i].get_next_host(&ss, &slen);
mask = exclude_group[i].get_mask();
fprintf(stderr, "exclude host group %d is %s/%d\n", i, inet_ntoa(sin->sin_addr), mask);
error("exclude host group %d is %s/%d", i, inet_ntoa(sin->sin_addr), mask);
break;
case TargetGroup::IPV4_RANGES:
while (exclude_group[i].get_next_host(&ss, &slen) == 0)
fprintf(stderr, "exclude host group %d is %s\n", i, inet_ntoa(sin->sin_addr));
error("exclude host group %d is %s", i, inet_ntoa(sin->sin_addr));
break;
case TargetGroup::IPV6_ADDRESS:

View file

@ -1553,7 +1553,7 @@ int i;
int realfrag = 0;
if (!packet) {
fprintf(stderr, "%s: packet is NULL!\n", __func__);
error("%s: packet is NULL!", __func__);
return -1;
}
@ -1612,7 +1612,7 @@ int i;
int realfrag = 0;
if (!packet) {
fprintf(stderr, "%s: packet is NULL!\n", __func__);
error("%s: packet is NULL!", __func__);
return -1;
}
@ -3209,7 +3209,7 @@ void broadcast_socket(int sd) {
if(sd == 501) return;
#endif
if (setsockopt(sd, SOL_SOCKET, SO_BROADCAST, (const char *)&one, sizeof(int)) != 0) {
fprintf(stderr, "Failed to secure socket broadcasting permission\n");
error("Failed to secure socket broadcasting permission");
perror("setsockopt");
}
}

View file

@ -177,7 +177,7 @@ void adjust_timeouts2(const struct timeval *sent,
to->timeout = to->srtt + (to->rttvar << 2);
}
if (to->rttvar > 2300000) {
fprintf(stderr, "RTTVAR has grown to over 2.3 seconds, decreasing to 2.0\n");
error("RTTVAR has grown to over 2.3 seconds, decreasing to 2.0");
to->rttvar = 2000000;
}