| 1 |
michael |
5052 |
/* |
| 2 |
michael |
5351 |
* Copyright (c) 2002 Erik Fears |
| 3 |
|
|
* Copyright (c) 2014-2015 ircd-hybrid development team |
| 4 |
michael |
5052 |
* |
| 5 |
michael |
5351 |
* This program is free software; you can redistribute it and/or modify |
| 6 |
|
|
* it under the terms of the GNU General Public License as published by |
| 7 |
|
|
* the Free Software Foundation; either version 2 of the License, or |
| 8 |
|
|
* (at your option) any later version. |
| 9 |
michael |
5052 |
* |
| 10 |
michael |
5351 |
* This program is distributed in the hope that it will be useful, |
| 11 |
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 |
|
|
* GNU General Public License for more details. |
| 14 |
michael |
5052 |
* |
| 15 |
michael |
5351 |
* You should have received a copy of the GNU General Public License |
| 16 |
|
|
* along with this program; if not, write to the Free Software |
| 17 |
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 |
|
|
* USA |
| 19 |
michael |
5052 |
*/ |
| 20 |
|
|
|
| 21 |
|
|
#include "setup.h" |
| 22 |
|
|
|
| 23 |
|
|
#include <stdio.h> |
| 24 |
|
|
#include <unistd.h> |
| 25 |
|
|
#include <assert.h> |
| 26 |
michael |
5207 |
#include <stdlib.h> |
| 27 |
|
|
#include <string.h> |
| 28 |
michael |
5266 |
#include <sys/time.h> |
| 29 |
|
|
#include <time.h> |
| 30 |
michael |
5052 |
#include <errno.h> |
| 31 |
|
|
#include <fcntl.h> |
| 32 |
michael |
5311 |
#include <sys/socket.h> |
| 33 |
|
|
#include <netinet/in.h> |
| 34 |
|
|
#include <arpa/inet.h> |
| 35 |
michael |
5052 |
|
| 36 |
|
|
#include "compat.h" |
| 37 |
|
|
#include "config.h" |
| 38 |
|
|
#include "irc.h" |
| 39 |
|
|
#include "log.h" |
| 40 |
|
|
#include "stats.h" |
| 41 |
|
|
#include "dnsbl.h" |
| 42 |
|
|
#include "options.h" |
| 43 |
|
|
#include "negcache.h" |
| 44 |
michael |
5202 |
#include "main.h" |
| 45 |
michael |
5333 |
#include "memory.h" |
| 46 |
michael |
5052 |
#include "match.h" |
| 47 |
|
|
#include "scan.h" |
| 48 |
|
|
|
| 49 |
|
|
/* Libopm */ |
| 50 |
|
|
|
| 51 |
|
|
#include "libopm/src/opm.h" |
| 52 |
|
|
#include "libopm/src/opm_common.h" |
| 53 |
|
|
#include "libopm/src/opm_error.h" |
| 54 |
|
|
#include "libopm/src/opm_types.h" |
| 55 |
|
|
|
| 56 |
|
|
|
| 57 |
|
|
/* GLOBAL LISTS */ |
| 58 |
|
|
|
| 59 |
michael |
5699 |
static list_t *SCANNERS; /* List of OPM_T */ |
| 60 |
|
|
static list_t *MASKS; /* Associative list of masks->scanners */ |
| 61 |
michael |
5052 |
|
| 62 |
|
|
|
| 63 |
|
|
/* Function declarations */ |
| 64 |
michael |
5338 |
static struct scan_struct *scan_create(const char *[], const char *); |
| 65 |
michael |
5322 |
static void scan_free(struct scan_struct *); |
| 66 |
michael |
5338 |
static void scan_irckline(const struct scan_struct *, const char *, const char *); |
| 67 |
|
|
static void scan_negative(const struct scan_struct *); |
| 68 |
michael |
5052 |
static void scan_log(OPM_REMOTE_T *); |
| 69 |
|
|
|
| 70 |
|
|
/** Callbacks for LIBOPM */ |
| 71 |
michael |
5338 |
static void scan_open_proxy(OPM_T *, OPM_REMOTE_T *, int, void *); |
| 72 |
|
|
static void scan_negotiation_failed(OPM_T *, OPM_REMOTE_T *, int, void *); |
| 73 |
michael |
5052 |
static void scan_timeout(OPM_T *, OPM_REMOTE_T *, int, void *); |
| 74 |
|
|
static void scan_end(OPM_T *, OPM_REMOTE_T *, int, void *); |
| 75 |
|
|
static void scan_handle_error(OPM_T *, OPM_REMOTE_T *, int, void *); |
| 76 |
|
|
|
| 77 |
|
|
extern FILE *scanlogfile; |
| 78 |
|
|
|
| 79 |
michael |
5114 |
|
| 80 |
michael |
5052 |
/* scan_cycle |
| 81 |
|
|
* |
| 82 |
|
|
* Perform scanner tasks. |
| 83 |
|
|
*/ |
| 84 |
michael |
5114 |
void |
| 85 |
|
|
scan_cycle(void) |
| 86 |
michael |
5052 |
{ |
| 87 |
michael |
5695 |
node_t *node; |
| 88 |
michael |
5052 |
|
| 89 |
michael |
5114 |
/* Cycle through the blacklist first.. */ |
| 90 |
|
|
dnsbl_cycle(); |
| 91 |
michael |
5052 |
|
| 92 |
michael |
5114 |
/* Cycle each scanner object */ |
| 93 |
michael |
5695 |
LIST_FOREACH(node, SCANNERS->head) |
| 94 |
michael |
5114 |
{ |
| 95 |
michael |
5695 |
struct scanner_struct *scs = node->data; |
| 96 |
michael |
5114 |
opm_cycle(scs->scanner); |
| 97 |
|
|
} |
| 98 |
michael |
5052 |
} |
| 99 |
|
|
|
| 100 |
|
|
/* scan_timer |
| 101 |
michael |
5876 |
* |
| 102 |
michael |
5052 |
* Perform actions that are to be performed every ~1 second. |
| 103 |
|
|
* |
| 104 |
|
|
* Parameters: NONE |
| 105 |
|
|
* Return: NONE |
| 106 |
|
|
* |
| 107 |
|
|
*/ |
| 108 |
michael |
5114 |
void |
| 109 |
|
|
scan_timer(void) |
| 110 |
michael |
5052 |
{ |
| 111 |
michael |
5332 |
static time_t nc_counter; |
| 112 |
michael |
5052 |
|
| 113 |
michael |
5695 |
if (OptionsItem->negcache) |
| 114 |
michael |
5114 |
{ |
| 115 |
michael |
5332 |
if (nc_counter++ >= OptionsItem->negcache_rebuild) |
| 116 |
michael |
5114 |
{ |
| 117 |
|
|
/* |
| 118 |
michael |
5332 |
* Time to rebuild the negative cache. |
| 119 |
michael |
5114 |
*/ |
| 120 |
|
|
if (OPT_DEBUG) |
| 121 |
|
|
log_printf("SCAN -> Rebuilding negative cache"); |
| 122 |
michael |
5052 |
|
| 123 |
michael |
5114 |
negcache_rebuild(); |
| 124 |
|
|
nc_counter = 0; |
| 125 |
|
|
} |
| 126 |
|
|
} |
| 127 |
michael |
5052 |
} |
| 128 |
|
|
|
| 129 |
|
|
/* scan_gettype(int protocol) |
| 130 |
|
|
* |
| 131 |
|
|
* Return human readable name of OPM PROTOCOL given OPM_TYPE_PROTOCOL |
| 132 |
|
|
* |
| 133 |
|
|
* Parameters: |
| 134 |
|
|
* protocol: Protocol to return (from libopm/src/opm_types.h) |
| 135 |
|
|
* |
| 136 |
|
|
* Return: |
| 137 |
|
|
* Pointer to static string containing human readable form of protocol |
| 138 |
|
|
* name |
| 139 |
|
|
* |
| 140 |
|
|
*/ |
| 141 |
michael |
5114 |
const char * |
| 142 |
|
|
scan_gettype(int protocol) |
| 143 |
michael |
5052 |
{ |
| 144 |
michael |
5114 |
static const char *undef = "undefined"; |
| 145 |
michael |
5338 |
static const struct protocol_assoc protocols[] = |
| 146 |
michael |
5114 |
{ |
| 147 |
michael |
6222 |
{ OPM_TYPE_HTTP, "HTTP" }, |
| 148 |
|
|
{ OPM_TYPE_HTTPPOST, "HTTPPOST" }, |
| 149 |
|
|
{ OPM_TYPE_SOCKS4, "SOCKS4" }, |
| 150 |
|
|
{ OPM_TYPE_SOCKS5, "SOCKS5" }, |
| 151 |
|
|
{ OPM_TYPE_WINGATE, "WINGATE" }, |
| 152 |
|
|
{ OPM_TYPE_ROUTER, "ROUTER" }, |
| 153 |
|
|
{ OPM_TYPE_HTTPS, "HTTPS" }, |
| 154 |
|
|
{ OPM_TYPE_HTTPSPOST, "HTTPSPOST" }, |
| 155 |
|
|
{ OPM_TYPE_DREAMBOX, "DREAMBOX" } |
| 156 |
michael |
5114 |
}; |
| 157 |
michael |
5052 |
|
| 158 |
michael |
5116 |
for (unsigned int i = 0; i < (sizeof(protocols) / sizeof(struct protocol_assoc)); ++i) |
| 159 |
michael |
5114 |
if (protocol == protocols[i].type) |
| 160 |
|
|
return protocols[i].name; |
| 161 |
michael |
5052 |
|
| 162 |
michael |
5114 |
return undef; |
| 163 |
michael |
5052 |
} |
| 164 |
|
|
|
| 165 |
|
|
/* scan_init |
| 166 |
|
|
|
| 167 |
|
|
Initialize scanner and masks list based on configuration. |
| 168 |
|
|
|
| 169 |
|
|
Parameters: |
| 170 |
|
|
None |
| 171 |
michael |
5876 |
|
| 172 |
michael |
5052 |
Return: |
| 173 |
|
|
None |
| 174 |
|
|
*/ |
| 175 |
michael |
5114 |
void |
| 176 |
|
|
scan_init(void) |
| 177 |
michael |
5052 |
{ |
| 178 |
michael |
5116 |
node_t *p, *p2, *p3, *p4, *node; |
| 179 |
|
|
struct UserConf *uc; |
| 180 |
|
|
struct ScannerConf *sc; |
| 181 |
|
|
struct ProtocolConf *pc; |
| 182 |
|
|
struct scanner_struct *scs; |
| 183 |
michael |
5052 |
|
| 184 |
michael |
5116 |
SCANNERS = list_create(); |
| 185 |
|
|
MASKS = list_create(); |
| 186 |
michael |
5052 |
|
| 187 |
michael |
5116 |
/* Setup each individual scanner */ |
| 188 |
|
|
LIST_FOREACH(p, ScannerItemList->head) |
| 189 |
|
|
{ |
| 190 |
|
|
sc = p->data; |
| 191 |
michael |
6149 |
scs = xcalloc(sizeof(*scs)); |
| 192 |
michael |
5052 |
|
| 193 |
michael |
5116 |
if (OPT_DEBUG) |
| 194 |
|
|
log_printf("SCAN -> Setting up scanner [%s]", sc->name); |
| 195 |
michael |
5052 |
|
| 196 |
michael |
5116 |
/* Build the scanner */ |
| 197 |
|
|
scs->scanner = opm_create(); |
| 198 |
|
|
scs->name = xstrdup(sc->name); |
| 199 |
|
|
scs->masks = list_create(); |
| 200 |
michael |
5052 |
|
| 201 |
michael |
5116 |
/* Setup configuration */ |
| 202 |
michael |
5121 |
opm_config(scs->scanner, OPM_CONFIG_FD_LIMIT, &sc->fd); |
| 203 |
michael |
5116 |
opm_config(scs->scanner, OPM_CONFIG_SCAN_IP, sc->target_ip); |
| 204 |
michael |
5121 |
opm_config(scs->scanner, OPM_CONFIG_SCAN_PORT, &sc->target_port); |
| 205 |
|
|
opm_config(scs->scanner, OPM_CONFIG_TIMEOUT, &sc->timeout); |
| 206 |
|
|
opm_config(scs->scanner, OPM_CONFIG_MAX_READ, &sc->max_read); |
| 207 |
michael |
5116 |
opm_config(scs->scanner, OPM_CONFIG_BIND_IP, sc->vhost); |
| 208 |
michael |
5052 |
|
| 209 |
michael |
5116 |
/* add target strings */ |
| 210 |
|
|
LIST_FOREACH(p2, sc->target_string->head) |
| 211 |
michael |
5227 |
opm_config(scs->scanner, OPM_CONFIG_TARGET_STRING, p2->data); |
| 212 |
michael |
5052 |
|
| 213 |
michael |
5116 |
/* Setup callbacks */ |
| 214 |
|
|
opm_callback(scs->scanner, OPM_CALLBACK_OPENPROXY, &scan_open_proxy, scs); |
| 215 |
|
|
opm_callback(scs->scanner, OPM_CALLBACK_NEGFAIL, &scan_negotiation_failed, scs); |
| 216 |
|
|
opm_callback(scs->scanner, OPM_CALLBACK_TIMEOUT, &scan_timeout, scs); |
| 217 |
|
|
opm_callback(scs->scanner, OPM_CALLBACK_END, &scan_end, scs); |
| 218 |
|
|
opm_callback(scs->scanner, OPM_CALLBACK_ERROR, &scan_handle_error, scs); |
| 219 |
michael |
5052 |
|
| 220 |
michael |
5116 |
/* Setup the protocols */ |
| 221 |
|
|
LIST_FOREACH(p2, sc->protocols->head) |
| 222 |
|
|
{ |
| 223 |
|
|
pc = p2->data; |
| 224 |
michael |
5052 |
|
| 225 |
michael |
5116 |
if (OPT_DEBUG >= 2) |
| 226 |
|
|
log_printf("SCAN -> Adding protocol %s:%d to scanner [%s]", |
| 227 |
|
|
scan_gettype(pc->type), pc->port, scs->name); |
| 228 |
michael |
5052 |
|
| 229 |
michael |
5116 |
if (opm_addtype(scs->scanner, pc->type, pc->port) == OPM_ERR_BADPROTOCOL) |
| 230 |
|
|
log_printf("SCAN -> Error bad protocol %s:%d in scanner [%s]", |
| 231 |
|
|
scan_gettype(pc->type), pc->port, scs->name); |
| 232 |
|
|
} |
| 233 |
michael |
5052 |
|
| 234 |
michael |
5116 |
node = node_create(scs); |
| 235 |
|
|
list_add(SCANNERS, node); |
| 236 |
|
|
} |
| 237 |
michael |
5052 |
|
| 238 |
michael |
5116 |
/* Give scanners a list of masks they scan */ |
| 239 |
|
|
LIST_FOREACH(p, SCANNERS->head) |
| 240 |
|
|
{ |
| 241 |
|
|
scs = p->data; |
| 242 |
michael |
5052 |
|
| 243 |
michael |
5116 |
LIST_FOREACH(p2, UserItemList->head) |
| 244 |
|
|
{ |
| 245 |
|
|
uc = p2->data; |
| 246 |
michael |
5052 |
|
| 247 |
michael |
5116 |
LIST_FOREACH(p3, uc->scanners->head) |
| 248 |
|
|
{ |
| 249 |
michael |
5699 |
const char *scannername = p3->data; |
| 250 |
michael |
5052 |
|
| 251 |
michael |
5116 |
/* Add all these masks to scanner */ |
| 252 |
|
|
if (strcasecmp(scannername, scs->name) == 0) |
| 253 |
|
|
{ |
| 254 |
|
|
LIST_FOREACH(p4, uc->masks->head) |
| 255 |
|
|
{ |
| 256 |
michael |
5699 |
const char *mask = p4->data; |
| 257 |
michael |
5052 |
|
| 258 |
michael |
5116 |
if (OPT_DEBUG) |
| 259 |
|
|
log_printf("SCAN -> Linking the mask [%s] to scanner [%s]", mask, scannername); |
| 260 |
michael |
5052 |
|
| 261 |
michael |
5116 |
node = node_create(xstrdup(mask)); |
| 262 |
|
|
list_add(scs->masks, node); |
| 263 |
|
|
} |
| 264 |
michael |
5052 |
|
| 265 |
michael |
5116 |
break; |
| 266 |
|
|
} |
| 267 |
michael |
5052 |
} |
| 268 |
michael |
5116 |
} |
| 269 |
|
|
} |
| 270 |
michael |
5052 |
|
| 271 |
michael |
5116 |
/* Initialise negative cache */ |
| 272 |
michael |
5695 |
if (OptionsItem->negcache) |
| 273 |
michael |
5116 |
{ |
| 274 |
|
|
if (OPT_DEBUG >= 2) |
| 275 |
|
|
log_printf("SCAN -> Initializing negative cache"); |
| 276 |
|
|
|
| 277 |
|
|
nc_init(&nc_head); |
| 278 |
|
|
} |
| 279 |
michael |
5052 |
} |
| 280 |
|
|
|
| 281 |
|
|
/* scan_connect |
| 282 |
|
|
* |
| 283 |
|
|
* scan_connect is called when m_notice (irc.c) matches a connection |
| 284 |
|
|
* notice and parses the connecting user out of it. |
| 285 |
|
|
* |
| 286 |
|
|
* Parameters: |
| 287 |
|
|
* user: Parsed items from the connection notice: |
| 288 |
|
|
* user[0] = connecting users nickname |
| 289 |
|
|
* user[1] = connecting users username |
| 290 |
|
|
* user[2] = connecting users hostname |
| 291 |
|
|
* user[3] = connecting users IP |
| 292 |
|
|
* msg = Original connect notice |
| 293 |
|
|
* Return: NONE |
| 294 |
|
|
* |
| 295 |
|
|
*/ |
| 296 |
michael |
5116 |
void |
| 297 |
michael |
5338 |
scan_connect(const char *user[], const char *msg) |
| 298 |
michael |
5052 |
{ |
| 299 |
michael |
5311 |
struct sockaddr_in ip; |
| 300 |
michael |
5116 |
node_t *p, *p2; |
| 301 |
|
|
struct scan_struct *ss; |
| 302 |
|
|
struct scanner_struct *scs; |
| 303 |
|
|
int ret; |
| 304 |
michael |
5052 |
|
| 305 |
michael |
5116 |
/* |
| 306 |
|
|
* Have to use MSGLENMAX here because it is unknown what the max size of |
| 307 |
|
|
* username/hostname can be. Some ircds use really mad values for |
| 308 |
|
|
* these. |
| 309 |
|
|
*/ |
| 310 |
michael |
5279 |
char mask[MSGLENMAX]; |
| 311 |
|
|
char ipmask[MSGLENMAX]; |
| 312 |
michael |
5052 |
|
| 313 |
michael |
5116 |
/* Check negcache before anything */ |
| 314 |
michael |
5695 |
if (OptionsItem->negcache) |
| 315 |
michael |
5116 |
{ |
| 316 |
michael |
5311 |
if (inet_pton(AF_INET, user[3], &ip.sin_addr) <= 0) |
| 317 |
michael |
5284 |
{ |
| 318 |
|
|
log_printf("SCAN -> Invalid IPv4 address '%s'!", user[3]); |
| 319 |
|
|
return; |
| 320 |
|
|
} |
| 321 |
|
|
else |
| 322 |
|
|
{ |
| 323 |
michael |
5311 |
if (check_neg_cache(ip.sin_addr.s_addr)) |
| 324 |
michael |
5052 |
{ |
| 325 |
michael |
5284 |
if (OPT_DEBUG) |
| 326 |
|
|
log_printf("SCAN -> %s!%s@%s (%s) is negatively cached. " |
| 327 |
michael |
5052 |
"Skipping all tests.", user[0], user[1], user[2], |
| 328 |
|
|
user[3]); |
| 329 |
michael |
5284 |
return; |
| 330 |
michael |
5052 |
} |
| 331 |
michael |
5284 |
} |
| 332 |
|
|
} |
| 333 |
michael |
5052 |
|
| 334 |
michael |
5284 |
/* Generate user mask */ |
| 335 |
michael |
6015 |
snprintf(mask, sizeof(mask), "%s!%s@%s", user[0], user[1], user[2]); |
| 336 |
|
|
snprintf(ipmask, sizeof(ipmask), "%s!%s@%s", user[0], user[1], user[3]); |
| 337 |
michael |
5052 |
|
| 338 |
michael |
5284 |
/* Check exempt list now that we have a mask */ |
| 339 |
|
|
if (scan_checkexempt(mask, ipmask)) |
| 340 |
|
|
{ |
| 341 |
|
|
if (OPT_DEBUG) |
| 342 |
|
|
log_printf("SCAN -> %s is exempt from scanning", mask); |
| 343 |
michael |
5052 |
|
| 344 |
michael |
5284 |
return; |
| 345 |
|
|
} |
| 346 |
michael |
5052 |
|
| 347 |
michael |
5284 |
/* create scan_struct */ |
| 348 |
|
|
ss = scan_create(user, msg); |
| 349 |
michael |
5052 |
|
| 350 |
michael |
5284 |
/* Store ss in the remote struct, so that in callbacks we have ss */ |
| 351 |
|
|
ss->remote->data = ss; |
| 352 |
michael |
5052 |
|
| 353 |
michael |
5284 |
/* Start checking our DNSBLs */ |
| 354 |
|
|
if (LIST_SIZE(OpmItem->blacklists) > 0) |
| 355 |
|
|
dnsbl_add(ss); |
| 356 |
|
|
|
| 357 |
|
|
/* Add ss->remote to all matching scanners */ |
| 358 |
|
|
LIST_FOREACH(p, SCANNERS->head) |
| 359 |
|
|
{ |
| 360 |
|
|
scs = p->data; |
| 361 |
|
|
|
| 362 |
|
|
LIST_FOREACH(p2, scs->masks->head) |
| 363 |
|
|
{ |
| 364 |
michael |
5699 |
const char *scsmask = p2->data; |
| 365 |
michael |
5284 |
|
| 366 |
|
|
if (!match(scsmask, mask)) |
| 367 |
michael |
5052 |
{ |
| 368 |
michael |
5284 |
if (OPT_DEBUG) |
| 369 |
|
|
log_printf("SCAN -> Passing %s to scanner [%s]", mask, scs->name); |
| 370 |
michael |
5052 |
|
| 371 |
michael |
5284 |
if ((ret = opm_scan(scs->scanner, ss->remote)) != OPM_SUCCESS) |
| 372 |
|
|
{ |
| 373 |
|
|
switch (ret) |
| 374 |
|
|
{ |
| 375 |
|
|
case OPM_ERR_NOPROTOCOLS: |
| 376 |
|
|
continue; |
| 377 |
|
|
break; |
| 378 |
|
|
case OPM_ERR_BADADDR: |
| 379 |
|
|
log_printf("OPM -> Bad address %s [%s].", |
| 380 |
|
|
(ss->manual_target ? ss->manual_target->name : |
| 381 |
|
|
"(unknown)"), ss->ip); |
| 382 |
|
|
break; |
| 383 |
|
|
default: |
| 384 |
|
|
log_printf("OPM -> Unknown error %s [%s].", |
| 385 |
|
|
(ss->manual_target ? ss->manual_target->name : |
| 386 |
|
|
"(unknown)"), ss->ip); |
| 387 |
|
|
break; |
| 388 |
|
|
} |
| 389 |
|
|
} |
| 390 |
|
|
else |
| 391 |
|
|
++ss->scans; /* Increase scan count only if OPM_SUCCESS */ |
| 392 |
michael |
5052 |
|
| 393 |
michael |
5284 |
break; /* Continue to next scanner */ |
| 394 |
michael |
5052 |
} |
| 395 |
michael |
5284 |
} |
| 396 |
|
|
} |
| 397 |
michael |
5052 |
|
| 398 |
michael |
5116 |
/* All scanners returned !OPM_SUCCESS and there were no dnsbl checks */ |
| 399 |
|
|
if (ss->scans == 0) |
| 400 |
|
|
scan_free(ss); |
| 401 |
michael |
5052 |
} |
| 402 |
|
|
|
| 403 |
|
|
/* scan_create |
| 404 |
|
|
* |
| 405 |
michael |
5116 |
* Allocate scan struct, including user information and REMOTE |
| 406 |
michael |
5052 |
* for LIBOPM. |
| 407 |
|
|
* |
| 408 |
|
|
* Parameters: |
| 409 |
|
|
* user: Parsed items from the connection notice: |
| 410 |
|
|
* user[0] = connecting users nickname |
| 411 |
|
|
* user[1] = connecting users username |
| 412 |
|
|
* user[2] = connecting users hostname |
| 413 |
|
|
* user[3] = connecting users IP |
| 414 |
|
|
* msg = Original connect notice (used as PROOF) |
| 415 |
|
|
* |
| 416 |
|
|
* Return: Pointer to new scan_struct |
| 417 |
|
|
* |
| 418 |
|
|
*/ |
| 419 |
michael |
5322 |
static struct scan_struct * |
| 420 |
michael |
5338 |
scan_create(const char *user[], const char *msg) |
| 421 |
michael |
5052 |
{ |
| 422 |
michael |
6149 |
struct scan_struct *ss = xcalloc(sizeof(*ss)); |
| 423 |
michael |
5052 |
|
| 424 |
michael |
5116 |
ss->irc_nick = xstrdup(user[0]); |
| 425 |
|
|
ss->irc_username = xstrdup(user[1]); |
| 426 |
|
|
ss->irc_hostname = xstrdup(user[2]); |
| 427 |
|
|
ss->ip = xstrdup(user[3]); |
| 428 |
|
|
ss->proof = xstrdup(msg); |
| 429 |
|
|
ss->remote = opm_remote_create(ss->ip); |
| 430 |
michael |
5052 |
|
| 431 |
michael |
5116 |
return ss; |
| 432 |
michael |
5052 |
} |
| 433 |
|
|
|
| 434 |
|
|
/* scan_free |
| 435 |
|
|
* |
| 436 |
|
|
* Free a scan_struct. This should only be done if the scan struct has |
| 437 |
|
|
* no scans left! |
| 438 |
|
|
* |
| 439 |
|
|
* Parameters: |
| 440 |
|
|
* ss: scan_struct to free |
| 441 |
michael |
5116 |
* |
| 442 |
michael |
5052 |
* Return: NONE |
| 443 |
|
|
*/ |
| 444 |
michael |
5322 |
static void |
| 445 |
michael |
5114 |
scan_free(struct scan_struct *ss) |
| 446 |
michael |
5052 |
{ |
| 447 |
michael |
5426 |
xfree(ss->irc_nick); |
| 448 |
|
|
xfree(ss->irc_username); |
| 449 |
|
|
xfree(ss->irc_hostname); |
| 450 |
|
|
xfree(ss->ip); |
| 451 |
|
|
xfree(ss->proof); |
| 452 |
michael |
5052 |
|
| 453 |
michael |
5114 |
opm_remote_free(ss->remote); |
| 454 |
michael |
5426 |
xfree(ss); |
| 455 |
michael |
5052 |
} |
| 456 |
|
|
|
| 457 |
|
|
/* scan_checkfinished |
| 458 |
|
|
* |
| 459 |
|
|
* Check if a scan is complete (ss->scans <= 0) |
| 460 |
|
|
* and free it if need be. |
| 461 |
|
|
*/ |
| 462 |
michael |
5114 |
void |
| 463 |
|
|
scan_checkfinished(struct scan_struct *ss) |
| 464 |
michael |
5052 |
{ |
| 465 |
michael |
5116 |
if (ss->scans <= 0) |
| 466 |
|
|
{ |
| 467 |
|
|
if (ss->manual_target) |
| 468 |
|
|
irc_send("PRIVMSG %s :CHECK -> All tests on %s completed.", |
| 469 |
michael |
5052 |
ss->manual_target->name, ss->ip); |
| 470 |
michael |
5116 |
else |
| 471 |
|
|
{ |
| 472 |
|
|
if (OPT_DEBUG) |
| 473 |
|
|
/* If there was a manual_target, then irc_nick, etc is NULL. */ |
| 474 |
|
|
log_printf("SCAN -> All tests on %s!%s@%s complete.", |
| 475 |
|
|
ss->irc_nick, ss->irc_username, ss->irc_hostname); |
| 476 |
michael |
5052 |
|
| 477 |
michael |
5116 |
/* Scan was a negative */ |
| 478 |
michael |
5322 |
if (ss->positive == 0) |
| 479 |
michael |
5116 |
scan_negative(ss); |
| 480 |
|
|
} |
| 481 |
michael |
5052 |
|
| 482 |
michael |
5116 |
scan_free(ss); |
| 483 |
|
|
} |
| 484 |
michael |
5052 |
} |
| 485 |
|
|
|
| 486 |
|
|
/* scan_positive |
| 487 |
|
|
* |
| 488 |
|
|
* Remote host (defined by ss) has been found positive by one or more |
| 489 |
|
|
* tests. |
| 490 |
|
|
* |
| 491 |
|
|
* Parameters: |
| 492 |
michael |
5116 |
* ss: scan_struct containing information regarding positive host |
| 493 |
michael |
5052 |
* kline: command to send to IRC server to ban the user (see scan_irckline) |
| 494 |
|
|
* type: string of the type of proxy found to be running on the host |
| 495 |
|
|
* |
| 496 |
|
|
* Return: NONE |
| 497 |
|
|
* |
| 498 |
|
|
*/ |
| 499 |
michael |
5114 |
void |
| 500 |
|
|
scan_positive(struct scan_struct *ss, const char *kline, const char *type) |
| 501 |
michael |
5052 |
{ |
| 502 |
michael |
5114 |
node_t *node; |
| 503 |
michael |
5052 |
|
| 504 |
michael |
5114 |
/* If already a positive, don't kline/close again */ |
| 505 |
|
|
if (ss->positive) |
| 506 |
|
|
return; |
| 507 |
michael |
5052 |
|
| 508 |
michael |
5114 |
/* Format KLINE and send to IRC server */ |
| 509 |
|
|
scan_irckline(ss, kline, type); |
| 510 |
michael |
5052 |
|
| 511 |
michael |
5114 |
/* Speed up the cleanup procedure */ |
| 512 |
|
|
/* Close all scans prematurely */ |
| 513 |
|
|
LIST_FOREACH(node, SCANNERS->head) |
| 514 |
|
|
{ |
| 515 |
|
|
OPM_T *scanner = ((struct scanner_struct *)node->data)->scanner; |
| 516 |
|
|
opm_end(scanner, ss->remote); |
| 517 |
|
|
} |
| 518 |
michael |
5052 |
|
| 519 |
michael |
5114 |
/* Set it as a positive (to avoid a scan_negative call later on */ |
| 520 |
|
|
ss->positive = 1; |
| 521 |
michael |
5052 |
} |
| 522 |
|
|
|
| 523 |
|
|
/* scan_open_proxy CALLBACK |
| 524 |
|
|
* |
| 525 |
|
|
* Called by libopm when a proxy is verified open. |
| 526 |
|
|
* |
| 527 |
|
|
* Parameters: |
| 528 |
|
|
* scanner: Scanner that found the open proxy. |
| 529 |
|
|
* remote: Remote struct containing information regarding remote end |
| 530 |
|
|
* |
| 531 |
|
|
* Return: NONE |
| 532 |
|
|
*/ |
| 533 |
michael |
5338 |
static void |
| 534 |
michael |
5114 |
scan_open_proxy(OPM_T *scanner, OPM_REMOTE_T *remote, int notused, void *data) |
| 535 |
michael |
5052 |
{ |
| 536 |
michael |
5116 |
struct scan_struct *ss; |
| 537 |
|
|
struct scanner_struct *scs; |
| 538 |
michael |
5052 |
|
| 539 |
michael |
5116 |
/* Record that a scan happened */ |
| 540 |
|
|
scan_log(remote); |
| 541 |
michael |
5052 |
|
| 542 |
michael |
5116 |
scs = data; |
| 543 |
|
|
ss = remote->data; |
| 544 |
michael |
5052 |
|
| 545 |
michael |
5116 |
if (ss->manual_target == NULL) |
| 546 |
|
|
{ |
| 547 |
|
|
/* kline and close scan */ |
| 548 |
|
|
scan_positive(ss, IRCItem->kline, scan_gettype(remote->protocol)); |
| 549 |
michael |
5052 |
|
| 550 |
michael |
5116 |
/* Report to blacklist */ |
| 551 |
|
|
dnsbl_report(ss); |
| 552 |
michael |
5052 |
|
| 553 |
michael |
5116 |
irc_send_channels("OPEN PROXY -> %s!%s@%s %s:%d (%s) [%s]", |
| 554 |
|
|
ss->irc_nick, ss->irc_username, ss->irc_hostname, remote->ip, |
| 555 |
|
|
remote->port, scan_gettype(remote->protocol), scs->name); |
| 556 |
|
|
log_printf("SCAN -> OPEN PROXY %s!%s@%s %s:%d (%s) [%s]", |
| 557 |
|
|
ss->irc_nick, ss->irc_username, ss->irc_hostname, remote->ip, |
| 558 |
|
|
remote->port, scan_gettype(remote->protocol), scs->name); |
| 559 |
|
|
} |
| 560 |
|
|
else |
| 561 |
|
|
{ |
| 562 |
|
|
irc_send("PRIVMSG %s :CHECK -> OPEN PROXY %s:%d (%s) [%s]", |
| 563 |
|
|
ss->manual_target->name, remote->ip, remote->port, |
| 564 |
|
|
scan_gettype(remote->protocol), scs->name); |
| 565 |
|
|
log_printf("SCAN -> OPEN PROXY %s:%d (%s) [%s]", remote->ip, |
| 566 |
|
|
remote->port, scan_gettype(remote->protocol), scs->name); |
| 567 |
|
|
} |
| 568 |
michael |
5052 |
|
| 569 |
michael |
5116 |
/* Record the proxy for stats purposes */ |
| 570 |
|
|
stats_openproxy(remote->protocol); |
| 571 |
michael |
5052 |
} |
| 572 |
|
|
|
| 573 |
|
|
/* scan_negotiation_failed CALLBACK |
| 574 |
|
|
* |
| 575 |
|
|
* Called by libopm when negotiation of a specific protocol failed. |
| 576 |
|
|
* |
| 577 |
|
|
* Parameters: |
| 578 |
|
|
* scanner: Scanner where the negotiation failed. |
| 579 |
|
|
* remote: Remote struct containing information regarding remote end |
| 580 |
|
|
* |
| 581 |
|
|
* Return: NONE |
| 582 |
|
|
* |
| 583 |
|
|
*/ |
| 584 |
michael |
5338 |
static void |
| 585 |
michael |
5116 |
scan_negotiation_failed(OPM_T *scanner, OPM_REMOTE_T *remote, int notused, void *data) |
| 586 |
michael |
5052 |
{ |
| 587 |
michael |
6286 |
const struct scanner_struct *scs = data; |
| 588 |
michael |
5052 |
|
| 589 |
michael |
5116 |
/* Record that a scan happened */ |
| 590 |
|
|
scan_log(remote); |
| 591 |
michael |
5052 |
|
| 592 |
michael |
5116 |
if (OPT_DEBUG) |
| 593 |
|
|
log_printf("SCAN -> Negotiation failed %s:%d (%s) [%s] (%d bytes read)", |
| 594 |
michael |
6286 |
remote->ip, remote->port, scan_gettype(remote->protocol), scs->name, |
| 595 |
|
|
remote->bytes_read); |
| 596 |
michael |
5052 |
} |
| 597 |
|
|
|
| 598 |
|
|
/* scan_timeout CALLBACK |
| 599 |
|
|
* |
| 600 |
|
|
* Called by libopm when the negotiation of a specific protocol timed out. |
| 601 |
|
|
* |
| 602 |
|
|
* Parameters: |
| 603 |
|
|
* scanner: Scanner where the connection timed out. |
| 604 |
|
|
* remote: Remote struct containing information regarding remote end |
| 605 |
|
|
* |
| 606 |
|
|
* Return: NONE |
| 607 |
|
|
* |
| 608 |
|
|
*/ |
| 609 |
michael |
5116 |
static void |
| 610 |
|
|
scan_timeout(OPM_T *scanner, OPM_REMOTE_T *remote, int notused, void *data) |
| 611 |
michael |
5052 |
{ |
| 612 |
michael |
6286 |
const struct scanner_struct *scs = data; |
| 613 |
michael |
5052 |
|
| 614 |
michael |
5116 |
/* Record that a scan happened */ |
| 615 |
|
|
scan_log(remote); |
| 616 |
michael |
5052 |
|
| 617 |
michael |
5116 |
if (OPT_DEBUG) |
| 618 |
|
|
log_printf("SCAN -> Negotiation timed out %s:%d (%s) [%s] (%d bytes read)", |
| 619 |
michael |
6286 |
remote->ip, remote->port, scan_gettype(remote->protocol), scs->name, |
| 620 |
michael |
5116 |
remote->bytes_read); |
| 621 |
michael |
5052 |
} |
| 622 |
|
|
|
| 623 |
|
|
/* scan_end CALLBACK |
| 624 |
|
|
* |
| 625 |
|
|
* Called by libopm when a specific SCAN has completed (all protocols in |
| 626 |
|
|
* that scan). |
| 627 |
|
|
* |
| 628 |
|
|
* Parameters: |
| 629 |
|
|
* scanner: Scanner the scan ended on. |
| 630 |
|
|
* remote: Remote struct containing information regarding remote end |
| 631 |
|
|
* |
| 632 |
|
|
* Return: NONE |
| 633 |
|
|
*/ |
| 634 |
michael |
5116 |
static void |
| 635 |
|
|
scan_end(OPM_T *scanner, OPM_REMOTE_T *remote, int notused, void *data) |
| 636 |
michael |
5052 |
{ |
| 637 |
michael |
5116 |
struct scan_struct *ss; |
| 638 |
|
|
struct scanner_struct *scs; |
| 639 |
michael |
5052 |
|
| 640 |
michael |
5116 |
scs = data; |
| 641 |
|
|
ss = remote->data; |
| 642 |
michael |
5052 |
|
| 643 |
michael |
5116 |
if (OPT_DEBUG) |
| 644 |
|
|
log_printf("SCAN -> Scan %s [%s] completed", remote->ip, scs->name); |
| 645 |
michael |
5052 |
|
| 646 |
michael |
5116 |
--ss->scans; |
| 647 |
|
|
scan_checkfinished(ss); |
| 648 |
michael |
5052 |
} |
| 649 |
|
|
|
| 650 |
|
|
/* scan_handle_error CALLBACK |
| 651 |
|
|
* |
| 652 |
|
|
* Called by libopm when an error occurs with a specific connection. This |
| 653 |
|
|
* does not mean the entire scan has ended. |
| 654 |
|
|
* |
| 655 |
|
|
* Parameters: |
| 656 |
|
|
* scanner: Scanner where the error occured. |
| 657 |
|
|
* remote: Remote struct containing information regarding remote end |
| 658 |
|
|
* err: OPM_ERROR code describing the error. |
| 659 |
|
|
* |
| 660 |
|
|
* Return: NONE |
| 661 |
|
|
*/ |
| 662 |
michael |
5116 |
static void |
| 663 |
|
|
scan_handle_error(OPM_T *scanner, OPM_REMOTE_T *remote, int err, void *data) |
| 664 |
michael |
5052 |
{ |
| 665 |
michael |
5116 |
struct scan_struct *ss; |
| 666 |
|
|
struct scanner_struct *scs; |
| 667 |
michael |
5052 |
|
| 668 |
michael |
5116 |
scs = data; |
| 669 |
|
|
ss = remote->data; |
| 670 |
michael |
5052 |
|
| 671 |
michael |
5116 |
switch (err) |
| 672 |
|
|
{ |
| 673 |
michael |
5284 |
case OPM_ERR_MAX_READ: |
| 674 |
|
|
if (OPT_DEBUG >= 2) |
| 675 |
|
|
log_printf("SCAN -> Max read on %s:%d (%s) [%s] (%d bytes read)", |
| 676 |
|
|
remote->ip, remote->port, scan_gettype(remote->protocol), |
| 677 |
|
|
scs->name, remote->bytes_read); |
| 678 |
michael |
5114 |
|
| 679 |
michael |
5284 |
if (ss->manual_target) |
| 680 |
|
|
irc_send("PRIVMSG %s :CHECK -> Negotiation failed %s:%d (%s) " |
| 681 |
|
|
"[%s] (%d bytes read)", ss->manual_target->name, |
| 682 |
|
|
remote->ip, remote->port, scan_gettype(remote->protocol), |
| 683 |
|
|
scs->name, remote->bytes_read); |
| 684 |
|
|
break; |
| 685 |
|
|
case OPM_ERR_BIND: |
| 686 |
|
|
log_printf("SCAN -> Bind error on %s:%d (%s) [%s]", remote->ip, |
| 687 |
|
|
remote->port, scan_gettype(remote->protocol), scs->name); |
| 688 |
|
|
break; |
| 689 |
|
|
case OPM_ERR_NOFD: |
| 690 |
|
|
log_printf("SCAN -> File descriptor allocation error %s:%d (%s) " |
| 691 |
|
|
"[%s]", remote->ip, remote->port, |
| 692 |
|
|
scan_gettype(remote->protocol), scs->name); |
| 693 |
michael |
5116 |
|
| 694 |
michael |
5284 |
if (ss->manual_target) |
| 695 |
|
|
irc_send("PRIVMSG %s :CHECK -> Scan failed %s:%d (%s) [%s] " |
| 696 |
|
|
"(file descriptor allocation error)", |
| 697 |
|
|
ss->manual_target->name, remote->ip, remote->port, |
| 698 |
|
|
scan_gettype(remote->protocol), scs->name); |
| 699 |
|
|
break; |
| 700 |
|
|
default: /* Unknown Error! */ |
| 701 |
|
|
if (OPT_DEBUG) |
| 702 |
|
|
log_printf("SCAN -> Unknown error %s:%d (%s) [%s]", remote->ip, |
| 703 |
|
|
remote->port, scan_gettype(remote->protocol), scs->name); |
| 704 |
|
|
break; |
| 705 |
michael |
5116 |
} |
| 706 |
michael |
5052 |
} |
| 707 |
|
|
|
| 708 |
|
|
/* scan_negative |
| 709 |
|
|
* |
| 710 |
|
|
* Remote host (defined by ss) has passed all tests. |
| 711 |
|
|
* |
| 712 |
|
|
* Parameters: |
| 713 |
|
|
* ss: scan_struct containing information regarding negative host. |
| 714 |
|
|
* |
| 715 |
|
|
* Return: NONE |
| 716 |
|
|
* |
| 717 |
|
|
*/ |
| 718 |
michael |
5114 |
static void |
| 719 |
michael |
5338 |
scan_negative(const struct scan_struct *ss) |
| 720 |
michael |
5114 |
{ |
| 721 |
|
|
/* Insert IP in negcache */ |
| 722 |
michael |
5695 |
if (OptionsItem->negcache) |
| 723 |
michael |
5114 |
{ |
| 724 |
|
|
if (OPT_DEBUG >= 2) |
| 725 |
|
|
log_printf("SCAN -> Adding %s to negative cache", ss->ip); |
| 726 |
michael |
5052 |
|
| 727 |
michael |
5114 |
negcache_insert(ss->ip); |
| 728 |
|
|
} |
| 729 |
michael |
5052 |
} |
| 730 |
|
|
|
| 731 |
|
|
/* scan_irckline |
| 732 |
|
|
* |
| 733 |
michael |
5876 |
* ss has been found as a positive host and is to be klined. |
| 734 |
michael |
5052 |
* Format a kline message using the kline message provided |
| 735 |
|
|
* as a format, then pass it to irc_send() to be sent to the remote server. |
| 736 |
|
|
* |
| 737 |
|
|
* Parameters: |
| 738 |
|
|
* ss: scan_struct containing information regarding host to be klined |
| 739 |
|
|
* format: kline message to format |
| 740 |
|
|
* type: type of proxy found (%t format character) |
| 741 |
michael |
5876 |
* |
| 742 |
michael |
5052 |
* Return: NONE |
| 743 |
|
|
*/ |
| 744 |
michael |
5114 |
static void |
| 745 |
michael |
5338 |
scan_irckline(const struct scan_struct *ss, const char *format, const char *type) |
| 746 |
michael |
5052 |
{ |
| 747 |
michael |
6237 |
char message[MSGLENMAX] = ""; /* OUTPUT */ |
| 748 |
michael |
5052 |
|
| 749 |
michael |
5114 |
unsigned int pos = 0; /* position in format */ |
| 750 |
|
|
unsigned int len = 0; /* position in message */ |
| 751 |
|
|
unsigned int size = 0; /* temporary size buffer */ |
| 752 |
michael |
6025 |
struct kline_format_assoc |
| 753 |
michael |
5114 |
{ |
| 754 |
michael |
6025 |
const char key; |
| 755 |
michael |
6030 |
const char *data; |
| 756 |
michael |
6025 |
} table[] = |
| 757 |
|
|
{ |
| 758 |
michael |
6287 |
{ 'i', ss->ip }, |
| 759 |
|
|
{ 'h', ss->irc_hostname }, |
| 760 |
|
|
{ 'u', ss->irc_username }, |
| 761 |
|
|
{ 'n', ss->irc_nick }, |
| 762 |
|
|
{ 't', type }, |
| 763 |
|
|
{ '\0', NULL } |
| 764 |
michael |
5114 |
}; |
| 765 |
michael |
5052 |
|
| 766 |
michael |
5114 |
/* |
| 767 |
|
|
* Copy format to message character by character, inserting any matching |
| 768 |
|
|
* data after %. |
| 769 |
|
|
*/ |
| 770 |
|
|
while (format[pos] != '\0' && len < (MSGLENMAX - 2)) |
| 771 |
|
|
{ |
| 772 |
|
|
switch (format[pos]) |
| 773 |
|
|
{ |
| 774 |
michael |
5284 |
case '%': |
| 775 |
|
|
/* % is the last char in the string, move on */ |
| 776 |
|
|
if (format[pos + 1] == '\0') |
| 777 |
|
|
continue; |
| 778 |
michael |
5052 |
|
| 779 |
michael |
5284 |
/* %% escapes % and becomes % */ |
| 780 |
|
|
if (format[pos + 1] == '%') |
| 781 |
|
|
{ |
| 782 |
|
|
message[len++] = '%'; |
| 783 |
|
|
++pos; /* Skip past the escaped % */ |
| 784 |
|
|
break; |
| 785 |
|
|
} |
| 786 |
michael |
5052 |
|
| 787 |
michael |
5284 |
/* Safe to check against table now */ |
| 788 |
michael |
6029 |
for (const struct kline_format_assoc *tab = table; tab->key; ++tab) |
| 789 |
michael |
5284 |
{ |
| 790 |
michael |
6029 |
if (tab->key == format[pos + 1]) |
| 791 |
michael |
5284 |
{ |
| 792 |
michael |
6029 |
size = strlen(tab->data); |
| 793 |
michael |
5284 |
|
| 794 |
|
|
/* Check if the new string can fit! */ |
| 795 |
|
|
if ((size + len) > (MSGLENMAX - 1)) |
| 796 |
|
|
break; |
| 797 |
|
|
else |
| 798 |
michael |
5052 |
{ |
| 799 |
michael |
6029 |
strlcat(message, tab->data, sizeof(message)); |
| 800 |
michael |
5284 |
len += size; |
| 801 |
michael |
5052 |
} |
| 802 |
michael |
5284 |
} |
| 803 |
|
|
} |
| 804 |
michael |
5052 |
|
| 805 |
michael |
5284 |
/* Skip key character */ |
| 806 |
|
|
++pos; |
| 807 |
|
|
break; |
| 808 |
michael |
5052 |
|
| 809 |
michael |
5284 |
default: |
| 810 |
|
|
message[len++] = format[pos]; |
| 811 |
|
|
message[len] = '\0'; |
| 812 |
|
|
break; |
| 813 |
|
|
} |
| 814 |
michael |
5052 |
|
| 815 |
michael |
5284 |
/* Continue to next character in format */ |
| 816 |
|
|
++pos; |
| 817 |
michael |
5114 |
} |
| 818 |
|
|
|
| 819 |
|
|
irc_send("%s", message); |
| 820 |
michael |
5052 |
} |
| 821 |
|
|
|
| 822 |
|
|
/* scan_manual |
| 823 |
|
|
* |
| 824 |
|
|
* Create a manual scan. A manual scan is a scan where the |
| 825 |
|
|
* scan_struct contains a manual_target pointer. |
| 826 |
|
|
*/ |
| 827 |
michael |
5114 |
void |
| 828 |
michael |
5338 |
scan_manual(char *param, const struct ChannelConf *target) |
| 829 |
michael |
5052 |
{ |
| 830 |
michael |
5374 |
char buf[INET6_ADDRSTRLEN]; |
| 831 |
|
|
const void *addr = NULL; |
| 832 |
michael |
5114 |
struct scan_struct *ss; |
| 833 |
|
|
struct scanner_struct *scs; |
| 834 |
michael |
5374 |
const char *ip = NULL; |
| 835 |
michael |
5114 |
char *scannername; |
| 836 |
michael |
5695 |
node_t *node; |
| 837 |
michael |
5114 |
int ret; |
| 838 |
michael |
5052 |
|
| 839 |
michael |
5114 |
/* If there were no parameters sent, simply alert the user and return */ |
| 840 |
|
|
if (param == NULL) |
| 841 |
|
|
{ |
| 842 |
|
|
irc_send("PRIVMSG %s :OPM -> Invalid parameters.", target->name); |
| 843 |
|
|
return; |
| 844 |
|
|
} |
| 845 |
michael |
5052 |
|
| 846 |
michael |
5114 |
/* |
| 847 |
|
|
* Try to extract a scanner name from param, otherwise we'll be |
| 848 |
|
|
* adding to all scanners |
| 849 |
|
|
*/ |
| 850 |
|
|
ip = param; |
| 851 |
michael |
5052 |
|
| 852 |
michael |
5114 |
if ((scannername = strchr(param, ' '))) |
| 853 |
|
|
{ |
| 854 |
|
|
*scannername = '\0'; |
| 855 |
|
|
scannername++; |
| 856 |
|
|
} |
| 857 |
michael |
5052 |
|
| 858 |
michael |
5114 |
/* If IP is a hostname, resolve it using gethostbyname (which will block!) */ |
| 859 |
|
|
if ((addr = firedns_resolveip4(ip)) == NULL) |
| 860 |
|
|
{ |
| 861 |
|
|
irc_send("PRIVMSG %s :CHECK -> Error resolving host '%s': %s", |
| 862 |
michael |
5390 |
target->name, ip, firedns_strerror(firedns_errno)); |
| 863 |
michael |
5114 |
return; |
| 864 |
|
|
} |
| 865 |
michael |
5052 |
|
| 866 |
michael |
5726 |
/* IP = the resolved IP now (it was the IP or hostname before) */ |
| 867 |
michael |
5374 |
if ((ip = inet_ntop(AF_INET, addr, buf, sizeof(buf))) == NULL) |
| 868 |
|
|
{ |
| 869 |
michael |
5377 |
irc_send("PRIVMSG %s :CHECK -> invalid address: %s", |
| 870 |
|
|
target->name, strerror(errno)); |
| 871 |
michael |
5374 |
return; |
| 872 |
|
|
} |
| 873 |
michael |
5052 |
|
| 874 |
michael |
6149 |
ss = xcalloc(sizeof(*ss)); |
| 875 |
michael |
5114 |
ss->ip = xstrdup(ip); |
| 876 |
|
|
ss->remote = opm_remote_create(ss->ip); |
| 877 |
|
|
ss->remote->data = ss; |
| 878 |
|
|
ss->manual_target = target; |
| 879 |
michael |
5052 |
|
| 880 |
michael |
5114 |
if (scannername) |
| 881 |
|
|
irc_send("PRIVMSG %s :CHECK -> Checking '%s' for open proxies [%s]", |
| 882 |
|
|
target->name, ip, scannername); |
| 883 |
|
|
else |
| 884 |
|
|
irc_send("PRIVMSG %s :CHECK -> Checking '%s' for open proxies on all " |
| 885 |
|
|
"scanners", target->name, ip); |
| 886 |
michael |
5052 |
|
| 887 |
michael |
5114 |
if (LIST_SIZE(OpmItem->blacklists) > 0) |
| 888 |
|
|
dnsbl_add(ss); |
| 889 |
michael |
5052 |
|
| 890 |
michael |
5284 |
/* Add ss->remote to all scanners */ |
| 891 |
michael |
5695 |
LIST_FOREACH(node, SCANNERS->head) |
| 892 |
michael |
5284 |
{ |
| 893 |
michael |
5695 |
scs = node->data; |
| 894 |
michael |
5052 |
|
| 895 |
michael |
5284 |
/* |
| 896 |
|
|
* If we have a scannername, only allow that scanner |
| 897 |
|
|
* to be used |
| 898 |
|
|
*/ |
| 899 |
|
|
if (scannername) |
| 900 |
|
|
if (strcasecmp(scannername, scs->name)) |
| 901 |
|
|
continue; |
| 902 |
michael |
5052 |
|
| 903 |
michael |
5284 |
if (OPT_DEBUG) |
| 904 |
|
|
log_printf("SCAN -> Passing %s to scanner [%s] (MANUAL SCAN)", ip, scs->name); |
| 905 |
michael |
5052 |
|
| 906 |
michael |
5284 |
if ((ret = opm_scan(scs->scanner, ss->remote)) != OPM_SUCCESS) |
| 907 |
|
|
{ |
| 908 |
|
|
switch (ret) |
| 909 |
michael |
5052 |
{ |
| 910 |
michael |
5284 |
case OPM_ERR_NOPROTOCOLS: |
| 911 |
|
|
break; |
| 912 |
|
|
case OPM_ERR_BADADDR: |
| 913 |
|
|
irc_send("PRIVMSG %s :OPM -> Bad address %s [%s]", |
| 914 |
|
|
ss->manual_target->name, ss->ip, scs->name); |
| 915 |
|
|
break; |
| 916 |
|
|
default: |
| 917 |
|
|
irc_send("PRIVMSG %s :OPM -> Unknown error %s [%s]", |
| 918 |
|
|
ss->manual_target->name, ss->ip, scs->name); |
| 919 |
|
|
break; |
| 920 |
michael |
5052 |
} |
| 921 |
michael |
5284 |
} |
| 922 |
|
|
else |
| 923 |
|
|
++ss->scans; /* Increase scan count only if OPM_SUCCESS */ |
| 924 |
|
|
} |
| 925 |
michael |
5052 |
|
| 926 |
michael |
5114 |
/* |
| 927 |
|
|
* If all of the scanners gave !OPM_SUCCESS and there were no dnsbl checks, |
| 928 |
|
|
* cleanup here |
| 929 |
|
|
*/ |
| 930 |
|
|
if (ss->scans == 0) |
| 931 |
|
|
{ |
| 932 |
|
|
if (scannername) |
| 933 |
|
|
irc_send("PRIVMSG %s :CHECK -> No such scanner '%s', or '%s' has " |
| 934 |
michael |
5052 |
"0 protocols.", ss->manual_target->name, scannername, |
| 935 |
|
|
scannername); |
| 936 |
|
|
|
| 937 |
michael |
5114 |
irc_send("PRIVMSG %s :CHECK -> No scans active on '%s', aborting scan.", |
| 938 |
|
|
ss->manual_target->name, ss->ip); |
| 939 |
|
|
scan_free(ss); |
| 940 |
|
|
} |
| 941 |
michael |
5052 |
} |
| 942 |
|
|
|
| 943 |
|
|
/* scan_checkexempt |
| 944 |
michael |
5876 |
* |
| 945 |
michael |
5052 |
* Check mask against exempt list. |
| 946 |
michael |
5876 |
* |
| 947 |
michael |
5052 |
* Parameters: |
| 948 |
|
|
* mask: Mask to check |
| 949 |
michael |
5876 |
* |
| 950 |
michael |
5052 |
* Return: |
| 951 |
|
|
* 1 if mask is in list |
| 952 |
michael |
5876 |
* 0 if mask is not in list |
| 953 |
michael |
5052 |
*/ |
| 954 |
michael |
5114 |
int |
| 955 |
michael |
5279 |
scan_checkexempt(const char *mask, const char *ipmask) |
| 956 |
michael |
5052 |
{ |
| 957 |
michael |
5114 |
node_t *node; |
| 958 |
michael |
5052 |
|
| 959 |
michael |
5114 |
LIST_FOREACH(node, ExemptItem->masks->head) |
| 960 |
|
|
{ |
| 961 |
michael |
5279 |
const char *exempt_mask = node->data; |
| 962 |
michael |
5052 |
|
| 963 |
michael |
5114 |
if (!match(exempt_mask, mask) || !match(exempt_mask, ipmask)) |
| 964 |
|
|
return 1; |
| 965 |
|
|
} |
| 966 |
|
|
|
| 967 |
|
|
return 0; |
| 968 |
michael |
5052 |
} |
| 969 |
|
|
|
| 970 |
|
|
/* scan_log |
| 971 |
|
|
* |
| 972 |
|
|
* Log the fact that a given ip/port/protocol has just been scanned, if the |
| 973 |
|
|
* user has asked for this to be logged. |
| 974 |
|
|
* |
| 975 |
|
|
* Parameters: |
| 976 |
|
|
* remote: OPM_REMOTE_T for the remote end |
| 977 |
|
|
*/ |
| 978 |
michael |
5114 |
static void |
| 979 |
|
|
scan_log(OPM_REMOTE_T *remote) |
| 980 |
michael |
5052 |
{ |
| 981 |
michael |
5659 |
char buf_present[32]; |
| 982 |
|
|
time_t present = 0; |
| 983 |
michael |
5114 |
struct scan_struct *ss = remote->data; |
| 984 |
michael |
5052 |
|
| 985 |
michael |
5114 |
if (!(OptionsItem->scanlog && scanlogfile)) |
| 986 |
|
|
return; |
| 987 |
michael |
5052 |
|
| 988 |
michael |
5114 |
time(&present); |
| 989 |
michael |
5659 |
strftime(buf_present, sizeof(buf_present), "%FT%H:%M:%S%z", localtime(&present)); |
| 990 |
michael |
5052 |
|
| 991 |
michael |
5114 |
fprintf(scanlogfile, "[%s] %s:%d (%s) \"%s\"\n", buf_present, remote->ip, |
| 992 |
|
|
remote->port, scan_gettype(remote->protocol), ss->proof); |
| 993 |
|
|
fflush(scanlogfile); |
| 994 |
michael |
5052 |
} |