From 6c06f51bd796102dbae9ac7e3c5387991388487b Mon Sep 17 00:00:00 2001 From: fyodor Date: Sat, 11 Aug 2007 05:16:56 +0000 Subject: [PATCH] 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. --- NmapOps.cc | 2 +- TargetGroup.cc | 6 +++--- nmap.cc | 39 +++++++++++++++++---------------------- osscan.cc | 10 +++++----- osscan2.cc | 4 ++-- output.cc | 14 +++++--------- scan_engine.cc | 12 +++++------- targets.cc | 14 +++++++------- tcpip.cc | 6 +++--- timing.cc | 2 +- 10 files changed, 49 insertions(+), 60 deletions(-) diff --git a/NmapOps.cc b/NmapOps.cc index 8896b85db..fb6abce3c 100644 --- a/NmapOps.cc +++ b/NmapOps.cc @@ -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 diff --git a/TargetGroup.cc b/TargetGroup.cc index 35ec69254..32a3fd429 100644 --- a/TargetGroup.cc +++ b/TargetGroup.cc @@ -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; diff --git a/nmap.cc b/nmap.cc index 4b9ece91b..88f5d428e 100644 --- a/nmap.cc +++ b/nmap.cc @@ -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 .\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 .", 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; } diff --git a/osscan.cc b/osscan.cc index 17cb71460..304ce25f0 100644 --- a/osscan.cc +++ b/osscan.cc @@ -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'; diff --git a/osscan2.cc b/osscan2.cc index ef6a6aeb7..de347f235 100644 --- a/osscan2.cc +++ b/osscan2.cc @@ -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; } diff --git a/output.cc b/output.cc index cd3130433..45deb3305 100644 --- a/output.cc +++ b/output.cc @@ -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"); diff --git a/scan_engine.cc b/scan_engine.cc index e3686b9a8..21b057f56 100644 --- a/scan_engine.cc +++ b/scan_engine.cc @@ -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') { diff --git a/targets.cc b/targets.cc index 72355323e..f81c334ee 100644 --- a/targets.cc +++ b/targets.cc @@ -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: diff --git a/tcpip.cc b/tcpip.cc index dc7a11d9d..bdda45c32 100644 --- a/tcpip.cc +++ b/tcpip.cc @@ -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"); } } diff --git a/timing.cc b/timing.cc index 6331b7c76..a3d58ff28 100644 --- a/timing.cc +++ b/timing.cc @@ -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; }