4 * Trojanscan copyright (C) Chris Porter 2002-2005
5 * Newserv bits copyright (C) David Mansell 2002-2003
8 * - Poke splidge about +r'ing bots, potential problems:
9 * - users might whine about T clone stealing account
10 * - would have to steal one already in use, so if trojans start using /msg q whois they'll see
11 * (though they have to be authed for this, they could use a clone of their own however)
14 #include "trojanscan.h"
15 #include "../lib/strlfunc.h"
16 #include "../lib/version.h"
18 MODULE_VERSION(TROJANSCAN_VERSION
" / $Id")
21 trojanscan_cmds
= newcommandtree();
23 addcommandtotree(trojanscan_cmds
, "showcommands", TROJANSCAN_ACL_UNAUTHED
, 0, &trojanscan_showcommands
);
24 addcommandtotree(trojanscan_cmds
, "help", TROJANSCAN_ACL_UNAUTHED
, 1, &trojanscan_help
);
25 addcommandtotree(trojanscan_cmds
, "hello", TROJANSCAN_ACL_UNAUTHED
| TROJANSCAN_ACL_OPER
, 1, &trojanscan_hello
);
27 addcommandtotree(trojanscan_cmds
, "join", TROJANSCAN_ACL_STAFF
, 1, &trojanscan_userjoin
);
28 addcommandtotree(trojanscan_cmds
, "chanlist", TROJANSCAN_ACL_STAFF
, 0, &trojanscan_chanlist
);
29 addcommandtotree(trojanscan_cmds
, "whois", TROJANSCAN_ACL_STAFF
, 1, &trojanscan_whois
);
31 addcommandtotree(trojanscan_cmds
, "changelev", TROJANSCAN_ACL_STAFF
| TROJANSCAN_ACL_OPER
, 2, &trojanscan_changelev
);
32 addcommandtotree(trojanscan_cmds
, "deluser", TROJANSCAN_ACL_TEAMLEADER
| TROJANSCAN_ACL_OPER
, 2, &trojanscan_deluser
);
33 addcommandtotree(trojanscan_cmds
, "mew", TROJANSCAN_ACL_STAFF
| TROJANSCAN_ACL_OPER
, 2, &trojanscan_mew
);
34 addcommandtotree(trojanscan_cmds
, "status", TROJANSCAN_ACL_STAFF
| TROJANSCAN_ACL_OPER
, 0, &trojanscan_status
);
35 addcommandtotree(trojanscan_cmds
, "listusers", TROJANSCAN_ACL_TEAMLEADER
, 0, &trojanscan_listusers
);
37 addcommandtotree(trojanscan_cmds
, "rehash", TROJANSCAN_ACL_WEBSITE
, 0, &trojanscan_rehash
);
39 addcommandtotree(trojanscan_cmds
, "cat", TROJANSCAN_ACL_OPER
, 1, &trojanscan_cat
);
41 addcommandtotree(trojanscan_cmds
, "reschedule", TROJANSCAN_ACL_DEVELOPER
| TROJANSCAN_ACL_OPER
, 0, &trojanscan_reschedule
);
43 srand((int)time(NULL
));
45 trojanscan_connect_schedule
= scheduleoneshot(time(NULL
) + 1, &trojanscan_connect
, NULL
);
50 struct trojanscan_realchannels
*rp
= trojanscan_realchanlist
, *oldrp
;
51 struct trojanscan_rejoinlist
*rj
= trojanscan_schedulerejoins
, *oldrj
;
54 deregisterlocaluser(trojanscan_nick
, NULL
);
56 if (trojanscan_connect_schedule
)
57 deleteschedule(trojanscan_connect_schedule
, &trojanscan_connect
, NULL
);
59 if(trojanscan_schedule
)
60 deleteschedule(trojanscan_schedule
, &trojanscan_dojoin
, NULL
);
62 if(trojanscan_poolschedule
)
63 deleteschedule(trojanscan_poolschedule
, &trojanscan_repool
, NULL
);
65 if(trojanscan_cloneschedule
)
66 deleteschedule(trojanscan_poolschedule
, &trojanscan_registerclones
, NULL
);
69 deleteschedule(rp
->schedule
, &trojanscan_dopart
, (void *)rp
);
76 deleteschedule(rj
->schedule
, &trojanscan_rejoin_channel
, (void *)rj
);
77 freesstring(rj
->channel
);
83 if(trojanscan_initialschedule
)
84 deleteschedule(trojanscan_initialschedule
, &trojanscan_fill_channels
, NULL
);
86 deleteschedule(trojanscan_rehashschedule
, &trojanscan_rehash_schedule
, NULL
);
88 for (i
=0;i
<TROJANSCAN_CLONE_TOTAL
;i
++)
89 if(trojanscan_swarm
[i
].clone
) {
90 deregisterlocaluser(trojanscan_swarm
[i
].clone
, NULL
);
91 trojanscan_swarm
[i
].clone
= NULL
;
93 trojanscan_free_database();
94 trojanscan_free_channels();
96 for (i
=0;i
<trojanscan_hostpoolsize
;i
++)
97 freesstring(trojanscan_hostpool
[i
]);
99 for (i
=0;i
<trojanscan_tailpoolsize
;i
++)
100 freesstring(trojanscan_tailpool
[i
]);
101 trojanscan_database_close();
104 void trojanscan_connect(void *arg
) {
105 sstring
*mnick
, *myident
, *myhost
, *myrealname
, *myauthname
;
106 sstring
*dbhost
, *dbuser
, *dbpass
, *db
, *dbport
, *temp
;
111 trojanscan_connect_schedule
= NULL
;
113 for(i
=0;i
<TROJANSCAN_CLONE_TOTAL
;i
++)
114 trojanscan_swarm
[i
].index
= i
; /* sure this could be done with pointer arithmetic... */
116 trojanscan_hostpoolsize
= 0;
117 trojanscan_tailpoolsize
= 0;
118 trojanscan_hostmode
= 0;
119 trojanscan_poolschedule
= NULL
;
120 trojanscan_cloneschedule
= NULL
;
121 trojanscan_realchanlist
= NULL
;
122 trojanscan_database
.glines
= 0;
123 trojanscan_database
.detections
= 0;
125 mnick
= getcopyconfigitem("trojanscan", "nick", "T", NICKLEN
);
126 myident
= getcopyconfigitem("trojanscan", "ident", "trojanscan", NICKLEN
);
127 myhost
= getcopyconfigitem("trojanscan", "hostname", "trojanscan.slug.netsplit.net", HOSTLEN
);
128 myrealname
= getcopyconfigitem("trojanscan", "realname", "Trojanscan v" TROJANSCAN_VERSION
, REALLEN
);
129 myauthname
= getcopyconfigitem("trojanscan", "authname", "T", ACCOUNTLEN
);
131 dbhost
= getcopyconfigitem("trojanscan", "dbhost", "localhost", HOSTLEN
);
132 dbuser
= getcopyconfigitem("trojanscan", "dbuser", "", NICKLEN
);
133 dbpass
= getcopyconfigitem("trojanscan", "dbpass", "", REALLEN
);
134 db
= getcopyconfigitem("trojanscan", "db", "", NICKLEN
);
136 dbport
= getcopyconfigitem("trojanscan", "dbport", "3306", ACCOUNTLEN
);
138 length
= snprintf(buf
, sizeof(buf
) - 1, "%d", TROJANSCAN_DEFAULT_MAXCHANS
);
139 temp
= getcopyconfigitem("trojanscan", "maxchans", buf
, length
);
141 trojanscan_maxchans
= atoi(temp
->content
);
144 length
= snprintf(buf
, sizeof(buf
) - 1, "%d", TROJANSCAN_DEFAULT_CYCLETIME
);
145 temp
= getcopyconfigitem("trojanscan", "cycletime", buf
, length
);
147 trojanscan_cycletime
= atoi(temp
->content
);
150 length
= snprintf(buf
, sizeof(buf
) - 1, "%d", TROJANSCAN_DEFAULT_PARTTIME
);
151 temp
= getcopyconfigitem("trojanscan", "parttime", buf
, length
);
152 trojanscan_part_time
= atoi(temp
->content
);
155 length
= snprintf(buf
, sizeof(buf
) - 1, "%d", TROJANSCAN_DEFAULT_MAXUSERS
);
156 temp
= getcopyconfigitem("trojanscan", "maxusers", buf
, length
);
157 trojanscan_maxusers
= atoi(temp
->content
);
160 length
= snprintf(buf
, sizeof(buf
) - 1, "%d", TROJANSCAN_MINIMUM_HOSTS_BEFORE_POOL
);
161 temp
= getcopyconfigitem("trojanscan", "minpoolhosts", buf
, length
);
162 trojanscan_min_hosts
= atoi(temp
->content
);
165 if ((trojanscan_cycletime
/ trojanscan_maxchans
) < 1) {
166 Error("trojanscan", ERR_FATAL
, "Cycletime / maxchans < 1, increase cycletime or decrease maxchans else cycling breaks.");
167 return; /* PPA: module failed to load */
170 length
= snprintf(buf
, sizeof(buf
) - 1, "%d", TROJANSCAN_DEFAULT_MINIMUM_CHANNEL_SIZE
);
171 temp
= getcopyconfigitem("trojanscan", "minchansize", buf
, length
);
172 trojanscan_minchansize
= atoi(temp
->content
);
175 trojanscan_nick
= registerlocaluser(mnick
->content
, myident
->content
, myhost
->content
, myrealname
->content
, myauthname
->content
, UMODE_SERVICE
| UMODE_DEAF
|
176 UMODE_OPER
| UMODE_INV
|
178 &trojanscan_handlemessages
);
180 if (trojanscan_database_connect(dbhost
->content
, dbuser
->content
, dbpass
->content
, db
->content
, atoi(dbport
->content
)) < 0) {
181 Error("trojanscan", ERR_FATAL
, "Cannot connect to database host!");
182 return; /* PPA: module failed to load */
185 trojanscan_database_query("CREATE TABLE phrases (id INT(10) PRIMARY KEY AUTO_INCREMENT, wormid INT(10) NOT NULL, phrase TEXT NOT NULL, priority INT(10) DEFAULT 0 NOT NULL, dateadded int(10))");
186 trojanscan_database_query("CREATE TABLE worms (id INT(10) PRIMARY KEY AUTO_INCREMENT, wormname TEXT NOT NULL, glinetype INT DEFAULT 0, data text, hitmsgs BOOL DEFAULT 1, hitchans BOOL DEFAULT 0, epidemic BOOL DEFAULT 0, privinfo text)");
187 trojanscan_database_query("CREATE TABLE logs (id INT(10) PRIMARY KEY AUTO_INCREMENT, userid INT(10) NOT NULL, act TEXT NOT NULL, description TEXT NOT NULL, ts TIMESTAMP)");
188 trojanscan_database_query("CREATE TABLE channels (id INT(10) PRIMARY KEY AUTO_INCREMENT, channel VARCHAR(%d) NOT NULL, exempt BOOL DEFAULT 0)", CHANNELLEN
);
189 trojanscan_database_query("CREATE TABLE users (id INT(10) PRIMARY KEY AUTO_INCREMENT, authname VARCHAR(%d) NOT NULL, authlevel TINYINT(4) NOT NULL)", ACCOUNTLEN
);
190 trojanscan_database_query("CREATE TABLE hits (id INT(10) PRIMARY KEY AUTO_INCREMENT, nickname VARCHAR(%d) NOT NULL, ident VARCHAR(%d) NOT NULL, host VARCHAR(%d) NOT NULL, phrase INT(10) NOT NULL, ts TIMESTAMP, messagetype VARCHAR(1) NOT NULL DEFAULT 'm', glined BOOL DEFAULT 1)", NICKLEN
, USERLEN
, HOSTLEN
);
191 trojanscan_database_query("CREATE TABLE settings (id INT(10) PRIMARY KEY AUTO_INCREMENT, setting VARCHAR(15) NOT NULL, value VARCHAR(15) NOT NULL)");
192 trojanscan_database_query("CREATE TABLE wwwlogs (id INT(10) PRIMARY KEY AUTO_INCREMENT, authid INT(10) NOT NULL, ip VARCHAR(15), action TEXT, ts TIMESTAMP)");
193 trojanscan_database_query("CREATE TABLE unknownlog (id INT(10) PRIMARY KEY AUTO_INCREMENT, data TEXT, user VARCHAR(%d) NOT NULL, ts TIMESTAMP)", NICKLEN
+USERLEN
+HOSTLEN
+3);
195 trojanscan_database_query("DELETE FROM settings");
196 trojanscan_database_query("INSERT INTO settings (setting, value) VALUES ('rehash','0')");
197 trojanscan_database_query("INSERT INTO settings (setting, value) VALUES ('changed','0')");
199 trojanscan_read_database(1);
201 cp
= findchannel(TROJANSCAN_OPERCHANNEL
);
203 localcreatechannel(trojanscan_nick
, TROJANSCAN_OPERCHANNEL
);
205 if(!localjoinchannel(trojanscan_nick
, cp
))
206 localgetops(trojanscan_nick
, cp
);
209 cp
= findchannel(TROJANSCAN_CHANNEL
);
211 localcreatechannel(trojanscan_nick
, TROJANSCAN_CHANNEL
);
213 if(!localjoinchannel(trojanscan_nick
, cp
))
214 localgetops(trojanscan_nick
, cp
);
217 cp
= findchannel(TROJANSCAN_PEONCHANNEL
);
219 localcreatechannel(trojanscan_nick
, TROJANSCAN_PEONCHANNEL
);
221 if(!localjoinchannel(trojanscan_nick
, cp
))
222 localgetops(trojanscan_nick
, cp
);
226 freesstring(myident
);
228 freesstring(myrealname
);
229 freesstring(myauthname
);
235 trojanscan_registerclones(NULL
);
237 trojanscan_rehashschedule
= scheduleoneshot(time(NULL
) + 60, &trojanscan_rehash_schedule
, NULL
);
241 void trojanscan_rehash_schedule(void *arg
) {
242 trojanscan_rehashschedule
= scheduleoneshot(time(NULL
) + 60, &trojanscan_rehash_schedule
, NULL
);
243 if (!(trojanscan_database_query("SELECT value FROM settings WHERE setting = 'rehash'"))) {
244 trojanscan_database_res
*res
;
245 if ((res
= trojanscan_database_store_result(&trojanscan_sql
))) {
246 if (trojanscan_database_num_rows(res
) > 0) {
247 trojanscan_database_row sqlrow
= trojanscan_database_fetch_row(res
);
248 if (sqlrow
&& (sqlrow
[0][0] == '1')) {
249 trojanscan_mainchanmsg("n: rehash initiated by website. . .");
250 trojanscan_read_database(0);
253 trojanscan_database_free_result(res
);
259 void trojanscan_free_database(void) {
261 for(i
=0;i
<trojanscan_database
.total_channels
;i
++)
262 freesstring(trojanscan_database
.channels
[i
].name
);
263 free(trojanscan_database
.channels
);
264 for(i
=0;i
<trojanscan_database
.total_phrases
;i
++) {
265 if (trojanscan_database
.phrases
[i
].phrase
)
266 pcre_free(trojanscan_database
.phrases
[i
].phrase
);
267 if (trojanscan_database
.phrases
[i
].hint
)
268 pcre_free(trojanscan_database
.phrases
[i
].hint
);
270 free(trojanscan_database
.phrases
);
271 for(i
=0;i
<trojanscan_database
.total_worms
;i
++)
272 freesstring(trojanscan_database
.worms
[i
].name
);
273 free(trojanscan_database
.worms
);
274 trojanscan_database
.total_channels
= 0;
275 trojanscan_database
.total_phrases
= 0;
276 trojanscan_database
.total_worms
= 0;
280 sstring
*trojanscan_getsstring(char *string
, int length
) {
283 for(i
=0;i
<length
;i
++) {
284 if ((string
[i
] == '\r') || (string
[i
] == '\n')) {
285 Error("trojanscan", ERR_WARNING
, "Error reading %s at position %d, set to ERROR!", string
, i
+1);
286 return getsstring("ERROR", sizeof("ERROR"));
290 return getsstring(string
, length
);
293 int trojanscan_strip_codes(char *buf
, size_t max
, char *original
) {
294 int i
, j
, length
= TROJANSCAN_MMIN(strlen(original
), max
-1);
295 char *p2
= original
, *p3
, flag
= 0;
297 for(i
=0;i
<length
+1;i
++) {
306 if ((i
+ 1) > length
)
308 if ((j
== 4) && flag
)
312 if ((j
== 0) && (!((*p2
>= '0') && (*p2
<= '9'))))
317 if ((i
+ 1) > length
)
319 if (!((*(p2
+ 1) >= '0') && (*(p2
+ 1) <= '9')))
322 } else if ((*p2
>= '0') && (*p2
<= '9')) {
330 if (!((*p2
>= '0') && (*p2
<= '9')))
336 if ((i
+ 1) > length
)
338 if (!((*(p2
+ 1) >= '0') && (*(p2
+ 1) <= '9')))
343 if ((j
== 3) && (!((*p2
>= '0') && (*p2
<= '9'))))
345 if ((j
== 4) && (!((*p2
>= '0') && (*p2
<= '9'))))
362 struct trojanscan_worms
*trojanscan_find_worm_by_id(int id
) {
364 for(i
=0;i
<trojanscan_database
.total_worms
;i
++)
365 if ((trojanscan_database
.worms
[i
].id
== id
))
366 return &trojanscan_database
.worms
[i
];
370 void trojanscan_read_database(int first_time
) {
372 int erroroffset
, i
, tempresult
;
374 trojanscan_database_res
*res
;
375 trojanscan_database_row sqlrow
;
378 trojanscan_free_database();
380 trojanscan_database
.total_channels
= 0;
381 trojanscan_database
.total_phrases
= 0;
382 trojanscan_database
.total_worms
= 0;
385 if (!(trojanscan_database_query("SELECT channel, exempt FROM channels"))) {
386 if ((res
= trojanscan_database_store_result(&trojanscan_sql
))) {
387 trojanscan_database
.total_channels
= trojanscan_database_num_rows(res
);
388 if (trojanscan_database
.total_channels
> 0) {
389 if ((trojanscan_database
.channels
= (trojanscan_channels
*)malloc(sizeof(trojanscan_channels
) * trojanscan_database
.total_channels
))) {
390 if ((trojanscan_database
.total_channels
>0) && trojanscan_database
.channels
) {
392 while((sqlrow
= trojanscan_database_fetch_row(res
))) {
393 trojanscan_database
.channels
[i
].name
= trojanscan_getsstring(sqlrow
[0], strlen(sqlrow
[0]));
394 trojanscan_database
.channels
[i
].exempt
= (sqlrow
[1][0] == '1');
400 trojanscan_database_free_result(res
);
404 if (!(trojanscan_database_query("SELECT id, wormname, glinetype, length(data), hitmsgs, hitchans, epidemic FROM worms"))) {
405 if ((res
= trojanscan_database_store_result(&trojanscan_sql
))) {
406 trojanscan_database
.total_worms
= trojanscan_database_num_rows(res
);
407 if (trojanscan_database
.total_worms
> 0) {
408 if ((trojanscan_database
.worms
= (trojanscan_worms
*)malloc(sizeof(trojanscan_worms
) * trojanscan_database
.total_worms
))) {
410 while((sqlrow
= trojanscan_database_fetch_row(res
))) {
411 trojanscan_database
.worms
[i
].id
= atoi(sqlrow
[0]);
412 trojanscan_database
.worms
[i
].name
= trojanscan_getsstring(sqlrow
[1], strlen(sqlrow
[1]));
413 tempresult
= atoi(sqlrow
[2]);
414 trojanscan_database
.worms
[i
].glineuser
= (tempresult
== 0);
415 trojanscan_database
.worms
[i
].glinehost
= (tempresult
== 1);
416 trojanscan_database
.worms
[i
].monitor
= (tempresult
== 2);
418 trojanscan_database
.worms
[i
].datalen
= ((atoi(sqlrow
[3]) == 0) ? 0 : 1);
420 trojanscan_database
.worms
[i
].datalen
= 0;
423 trojanscan_database
.worms
[i
].hitpriv
= (atoi(sqlrow
[4]) == 1);
424 trojanscan_database
.worms
[i
].hitchans
= (atoi(sqlrow
[5]) == 1);
425 trojanscan_database
.worms
[i
].epidemic
= (atoi(sqlrow
[6]) == 1);
431 trojanscan_database_free_result(res
);
435 if (!(trojanscan_database_query("SELECT id, phrase, wormid FROM phrases ORDER BY priority DESC"))) {
436 if ((res
= trojanscan_database_store_result(&trojanscan_sql
))) {
437 trojanscan_database
.total_phrases
= trojanscan_database_num_rows(res
);
438 if (trojanscan_database
.total_phrases
> 0) {
439 if ((trojanscan_database
.phrases
= (trojanscan_phrases
*)malloc(sizeof(trojanscan_phrases
) * trojanscan_database
.total_phrases
))) {
441 while((sqlrow
= trojanscan_database_fetch_row(res
))) {
442 trojanscan_database
.phrases
[i
].id
= atoi(sqlrow
[0]);
443 trojanscan_database
.phrases
[i
].worm
= trojanscan_find_worm_by_id(atoi(sqlrow
[2]));
444 if (!(trojanscan_database
.phrases
[i
].phrase
= pcre_compile(sqlrow
[1], PCRE_CASELESS
, &error
, &erroroffset
, NULL
))) {
445 Error("trojanscan", ERR_WARNING
, "Error compiling expression %s at offset %d: %s", sqlrow
[1], erroroffset
, error
);
447 trojanscan_database
.phrases
[i
].hint
= pcre_study(trojanscan_database
.phrases
[i
].phrase
, 0, &error
);
449 Error("trojanscan", ERR_WARNING
, "Error studying expression %s: %s", sqlrow
[1], error
);
450 pcre_free(trojanscan_database
.phrases
[i
].phrase
);
451 trojanscan_database
.phrases
[i
].phrase
= NULL
;
458 trojanscan_database_free_result(res
);
462 trojanscan_database_query("UPDATE settings SET value = '0' where setting = 'rehash'");
466 void trojanscan_log(nick
*np
, char *event
, char *details
, ...) {
468 char eevent
[TROJANSCAN_QUERY_TEMP_BUF_SIZE
], edetails
[TROJANSCAN_QUERY_TEMP_BUF_SIZE
], buf
[513];
471 va_start(va
, details
);
472 vsnprintf(buf
, sizeof(buf
) - 1, details
, va
);
477 nickid
= trojanscan_user_id_by_authname(np
->authname
);
479 trojanscan_database_escape_string(eevent
, event
, strlen(event
));
480 trojanscan_database_escape_string(edetails
, buf
, strlen(buf
));
481 trojanscan_database_query("INSERT INTO logs (userid, act, description) VALUES ('%d', '%s', '%s')", nickid
, eevent
, edetails
);
484 void trojanscan_generateclone(void *arg
) {
485 int i
, loops
= 0, modes
= UMODE_XOPER
| UMODE_INV
;
486 char c_nick
[NICKLEN
+1], c_ident
[USERLEN
+1], c_host
[HOSTLEN
+1], c_real
[REALLEN
+1];
491 /* PPA: unlikely to be infinite */
494 if (!loops
&& trojanscan_hostmode
) /* only have one go at this */
495 trojanscan_generatenick(c_nick
, NICKLEN
);
497 trojanscan_gennick(c_nick
, trojanscan_minmaxrand(7, TROJANSCAN_MMIN(13, NICKLEN
)));
499 } while ((getnickbynick(c_nick
) != NULL
));
501 trojanscan_generateident(c_ident
, USERLEN
);
503 trojanscan_genident(c_ident
, trojanscan_minmaxrand(4, TROJANSCAN_MMIN(8, USERLEN
)));
505 if(trojanscan_hostmode
) {
506 trojanscan_generatehost(c_host
, HOSTLEN
, &fakeip
);
508 trojanscan_genhost(c_host
, HOSTLEN
, &fakeip
);
510 trojanscan_genhost(c_host
, HOSTLEN
, &fakeip
);
513 trojanscan_generaterealname(c_real
, REALLEN
);
515 trojanscan_genreal(c_real
, trojanscan_minmaxrand(15, TROJANSCAN_MMIN(50, REALLEN
)));
517 trojanscan_swarm
[i
].clone
= registerlocaluser(c_nick
, c_ident
, c_host
, c_real
, NULL
, modes
, &trojanscan_clonehandlemessages
);
518 trojanscan_swarm
[i
].fakeip
= fakeip
;
520 if(trojanscan_swarm
[i
].clone
&& !trojanscan_swarm_created
) {
521 nick
*np
= trojanscan_selectuser();
522 if(np
) /* select a 'random' sign on time for whois generation */
523 trojanscan_swarm
[i
].clone
->timestamp
= np
->timestamp
;
525 trojanscan_swarm
[i
].remaining
= trojanscan_minmaxrand(5, 100);
527 trojanscan_swarm
[i
].sitting
= 0;
531 void trojanscan_free_channels(void) {
533 if(trojanscan_chans
) {
534 for(i
=0;i
<trojanscan_activechans
;i
++)
535 freesstring(trojanscan_chans
[i
].channel
);
536 free(trojanscan_chans
);
537 trojanscan_chans
= NULL
;
538 trojanscan_activechans
= 0;
542 void trojanscan_repool(void *arg
) {
543 if (trojanscan_generatepool() < TROJANSCAN_MINPOOLSIZE
) {
544 trojanscan_hostmode
= 0;
547 trojanscan_hostmode
= 1;
548 trojanscan_poolschedule
= scheduleoneshot(time(NULL
) + TROJANSCAN_POOL_REGENERATION
, &trojanscan_repool
, NULL
);
552 void trojanscan_registerclones(void *arg
) {
555 if (trojanscan_generatepool() < TROJANSCAN_MINPOOLSIZE
) {
556 trojanscan_hostmode
= 0;
557 trojanscan_cloneschedule
= scheduleoneshot(time(NULL
) + 10, &trojanscan_registerclones
, NULL
);
560 trojanscan_hostmode
= 1;
561 trojanscan_poolschedule
= scheduleoneshot(time(NULL
) + TROJANSCAN_POOL_REGENERATION
, &trojanscan_repool
, NULL
);
562 trojanscan_cloneschedule
= NULL
;
565 for (i
=0;i
<TROJANSCAN_CLONE_TOTAL
;i
++)
566 trojanscan_generateclone((void *)i
);
567 trojanscan_mainchanmsg("n: swarm (%d clones) created.", TROJANSCAN_CLONE_TOTAL
);
568 trojanscan_swarm_created
= 1;
570 trojanscan_initialschedule
= scheduleoneshot(time(NULL
) + 5, &trojanscan_fill_channels
, NULL
);
573 int trojanscan_status(void *sender
, int cargc
, char **cargv
) {
574 nick
*np
= (nick
*)sender
;
575 trojanscan_log(np
, "status", "");
576 trojanscan_reply(np
, "Channels in schedule: %d", trojanscan_activechans
);
577 trojanscan_reply(np
, "Channels in database: %d", trojanscan_database
.total_channels
);
578 trojanscan_reply(np
, "Phrases: %d", trojanscan_database
.total_phrases
);
579 trojanscan_reply(np
, "Worms: %d", trojanscan_database
.total_worms
);
580 trojanscan_reply(np
, "Detections: %d", trojanscan_database
.detections
);
581 trojanscan_reply(np
, "Glines: %d", trojanscan_database
.glines
);
582 trojanscan_reply(np
, "Host/tail pool size: %d", TROJANSCAN_POOLSIZE
);
583 trojanscan_reply(np
, "Cycletime: %d", trojanscan_cycletime
);
584 trojanscan_reply(np
, "Clones: %d", TROJANSCAN_CLONE_TOTAL
);
588 int trojanscan_chanlist(void *sender
, int cargc
, char **cargv
) {
590 nick
*np
= (nick
*)sender
;
591 char buf
[CHANNELLEN
* 2 + 20];
592 trojanscan_reply(np
, "Channel list (%d total):", trojanscan_activechans
);
595 for(i
=0;i
<trojanscan_activechans
;i
++) {
596 if(trojanscan_chans
[i
].channel
->length
+ 3 > sizeof(buf
) - strlen(buf
)) {
597 trojanscan_reply(np
, "%s", buf
);
601 /* if splidge sees this I'm going to die */
602 strlcat(buf
, trojanscan_chans
[i
].channel
->content
, sizeof(buf
));
603 strlcat(buf
, " ", sizeof(buf
));
606 trojanscan_reply(np
, "%s", buf
);
608 trojanscan_reply(np
, "Done.");
612 int trojanscan_whois(void *sender
, int cargc
, char **cargv
) {
614 nick
*np
= (nick
*)sender
, *np2
;
618 trojanscan_reply(np
, "Not enough parameters.");
622 if (cargv
[0][0] == '#') {
623 tochange
= cargv
[0] + 1;
626 np2
= getnickbynick(cargv
[0]);
628 trojanscan_reply(np
, "That nickname is not on the network.");
631 for(i
=0;i
<TROJANSCAN_CLONE_TOTAL
;i
++) {
632 if(trojanscan_swarm
[i
].clone
->nick
&& !ircd_strcmp(trojanscan_swarm
[i
].clone
->nick
, np2
->nick
)) {
633 trojanscan_reply(np
, "Nickname : %s", np2
->nick
);
634 trojanscan_reply(np
, "Swarm : yes", trojanscan_swarm
[i
].clone
->nick
);
638 if (!IsAccount(np2
)) {
639 trojanscan_reply(np
, "User is not authed.");
642 tochange
= np2
->authname
;
645 templevel
= trojanscan_user_level_by_authname(tochange
);
646 if (templevel
== -1) {
647 trojanscan_reply(np
, "User does not exist.");
649 union trojanscan_userlevel flags
;
650 flags
.number
= templevel
;
651 trojanscan_reply(np
, "Authname : %s", tochange
);
652 trojanscan_reply(np
, "Flags : +" TROJANSCAN_FLAG_MASK
, TrojanscanFlagsInfo(flags
));
658 void trojanscan_privmsg_chan_or_nick(channel
*cp
, nick
*np
, char *message
, ...) {
662 if (!trojanscan_nick
)
665 va_start(va
, message
);
666 vsnprintf(buf
, sizeof(buf
) - 1, message
, va
);
670 sendmessagetochannel(trojanscan_nick
, cp
, buf
);
672 sendmessagetouser(trojanscan_nick
, np
, buf
);
677 int trojanscan_mew(void *sender
, int cargc
, char **cargv
) {
678 nick
*np
= (nick
*)sender
, *np2
= NULL
;
682 trojanscan_reply(np
, "Not enough paramaters.");
686 if(cargv
[0][0] == '#') {
687 if (!(cp
= findchannel(cargv
[0]))) {
688 trojanscan_reply(np
, "Channel not found.");
691 trojanscan_log(np
, "mew", "%s %s", cp
->index
->name
->content
, cargv
[1]);
693 if (!(np2
= getnickbynick(cargv
[0]))) {
694 trojanscan_reply(np
, "Nickname is not present on the network.");
697 trojanscan_log(np
, "mew", "%s %s", np2
->nick
, cargv
[1]);
700 trojanscan_privmsg_chan_or_nick(cp
, np2
, "\001ACTION mews hopefully at %s\001", cargv
[1]);
703 trojanscan_reply(np
, "Mewed at %s in %s.", cargv
[1], cp
->index
->name
->content
);
705 trojanscan_reply(np
, "Mewed at %s at %s.", cargv
[1], np2
->nick
);
710 int trojanscan_cat(void *sender
, int cargc
, char **cargv
) {
711 nick
*np
= (nick
*)sender
, *np2
= NULL
;
717 trojanscan_reply(np
, "Not enough paramaters.");
721 if(cargv
[0][0] == '#') {
722 if (!(cp
= findchannel(cargv
[0]))) {
723 trojanscan_reply(np
, "Channel not found.");
726 trojanscan_log(np
, "cat", cp
->index
->name
->content
);
728 if (!(np2
= getnickbynick(cargv
[0]))) {
729 trojanscan_reply(np
, "Nickname is not present on the network.");
732 trojanscan_log(np
, "cat", np2
->nick
);
735 if ((!(cat
= fopen(TROJANSCAN_CAT
, "r")))) {
736 trojanscan_reply(np
, "Unable to open cat!");
740 while (fgets(buf
, sizeof(buf
) - 1, cat
)) {
741 if ((p
= strchr(buf
, '\n'))) {
743 trojanscan_privmsg_chan_or_nick(cp
, np2
, "%s", buf
);
744 } else if (feof(cat
)) {
745 trojanscan_privmsg_chan_or_nick(cp
, np2
, "%s", buf
);
752 trojanscan_reply(np
, "Spammed cat in %s.", cp
->index
->name
->content
);
754 trojanscan_reply(np
, "Spammed cat at %s.", np2
->nick
);
760 int trojanscan_reschedule(void *sender
, int cargc
, char **cargv
) {
761 nick
*np
= (nick
*)sender
;
762 trojanscan_log(np
, "reschedule", "");
763 trojanscan_fill_channels(NULL
);
765 trojanscan_reply(np
, "Rescheduled.");
769 int trojanscan_listusers(void *sender
, int cargc
, char **cargv
) {
770 nick
*np
= (nick
*)sender
;
772 trojanscan_log(np
, "listusers", "");
774 trojanscan_reply(np
, "User list:");
776 if (!(trojanscan_database_query("SELECT authname, authlevel FROM users ORDER BY authlevel DESC, authname"))) {
777 trojanscan_database_res
*res
;
778 if ((res
= trojanscan_database_store_result(&trojanscan_sql
))) {
779 if (trojanscan_database_num_rows(res
)) {
780 trojanscan_database_row sqlrow
;
781 union trojanscan_userlevel flags
;
782 while((sqlrow
= trojanscan_database_fetch_row(res
))) {
783 flags
.number
= atoi(sqlrow
[1]);
784 trojanscan_reply(np
, "%s +" TROJANSCAN_FLAG_MASK
, sqlrow
[0], TrojanscanIsDeveloper(flags
) ? "d" : "", TrojanscanIsTeamLeader(flags
) ? "t" : "", TrojanscanIsStaff(flags
) ? "s" : "", TrojanscanIsWebsite(flags
) ? "w" : "", TrojanscanIsCat(flags
) ? "c" : "");
787 trojanscan_database_free_result(res
);
791 trojanscan_reply(np
, "Done.");
795 int trojanscan_help(void *sender
, int cargc
, char **cargv
) {
796 nick
*np
= (nick
*)sender
;
799 trojanscan_reply(np
, "Not enough parameters.");
803 if (!strcasecmp("help", cargv
[0])) {
804 trojanscan_reply(np
, "Syntax: help <command name>");
805 trojanscan_reply(np
, "Gives help on commands.");
806 } else if (!strcasecmp("status", cargv
[0])) {
807 trojanscan_reply(np
, "Syntax: status");
808 trojanscan_reply(np
, "Gives statistical information about the bot.");
809 } else if (!strcasecmp("join", cargv
[0])) {
810 trojanscan_reply(np
, "Syntax: join <#channel>");
811 trojanscan_reply(np
, "Orders a clone to join supplied channel.");
812 } else if (!strcasecmp("showcommands", cargv
[0])) {
813 trojanscan_reply(np
, "Syntax: showcommands");
814 trojanscan_reply(np
, "Pretty obvious.");
815 } else if (!strcasecmp("hello", cargv
[0])) {
816 trojanscan_reply(np
, "Syntax: hello ?nickname?");
817 trojanscan_reply(np
, "Creates a new user.");
818 } else if (!strcasecmp("rehash", cargv
[0])) {
819 trojanscan_reply(np
, "Syntax: rehash");
820 trojanscan_reply(np
, "Reloads bot database.");
821 } else if (!strcasecmp("changelev", cargv
[0])) {
822 trojanscan_reply(np
, "Syntax: changelev <nickname or #authname> <flags>");
823 trojanscan_reply(np
, "Changes access flags of selected user to supplied input.");
824 trojanscan_reply(np
, "+d -> developer");
825 trojanscan_reply(np
, "+t -> team leader");
826 trojanscan_reply(np
, "+s -> staff");
827 trojanscan_reply(np
, "+w -> web management");
828 trojanscan_reply(np
, "+c -> cat access");
829 } else if (!strcasecmp("deluser", cargv
[0])) {
830 trojanscan_reply(np
, "Syntax: deluser <nickname or #authname>");
831 trojanscan_reply(np
, "Deletes selected user from my database.");
832 } else if (!strcasecmp("mew", cargv
[0])) {
833 trojanscan_reply(np
, "Syntax: mew <#channel or nickname> <nickname>");
834 trojanscan_reply(np
, "Gracefully mews at target in selected channel or query.");
835 } else if (!strcasecmp("cat", cargv
[0])) {
836 trojanscan_reply(np
, "Syntax: cat <#channel or nickname>");
837 trojanscan_reply(np
, "Shows the almightly cat.");
838 } else if (!strcasecmp("reschedule", cargv
[0])) {
839 trojanscan_reply(np
, "Syntax: reschedule");
840 trojanscan_reply(np
, "Recalculates bots schedule.");
841 } else if (!strcasecmp("chanlist", cargv
[0])) {
842 trojanscan_reply(np
, "Syntax: chanlist");
843 trojanscan_reply(np
, "Displays bots current channel list.");
844 } else if (!strcasecmp("whois", cargv
[0])) {
845 trojanscan_reply(np
, "Syntax: whois <nickname or #authname>");
846 trojanscan_reply(np
, "Displays information on given user.");
847 } else if (!strcasecmp("whois", cargv
[0])) {
848 trojanscan_reply(np
, "Syntax: listusers <flags>");
849 trojanscan_reply(np
, "Displays users with listusersing flags.");
851 trojanscan_reply(np
, "Command not found.");
858 int trojanscan_hello(void *sender
, int cargc
, char **cargv
) {
859 nick
*np
= (nick
*)sender
, *toadd
;
860 char eaccount
[TROJANSCAN_QUERY_TEMP_BUF_SIZE
];
864 toadd
= getnickbynick(cargv
[0]);
866 trojanscan_reply(np
, "That nickname is not on the network.");
869 if (!IsAccount(toadd
)) {
870 trojanscan_reply(np
, "That user is not authed with the network.");
874 if (!IsAccount(np
)) {
875 trojanscan_reply(np
, "You are not authed with the network, auth before creating your user.");
881 if (trojanscan_user_level_by_authname(toadd
->authname
)!=-1) {
882 trojanscan_reply(np
, "Authname (%s) is already on file.", toadd
->authname
);
886 trojanscan_log(np
, "hello", toadd
->authname
);
888 if (!(trojanscan_database_query("SELECT id FROM users LIMIT 1"))) {
889 trojanscan_database_res
*res
;
890 if ((res
= trojanscan_database_store_result(&trojanscan_sql
))) {
891 if (trojanscan_database_num_rows(res
) == 0)
892 level
= TROJANSCAN_ACL_DEVELOPER
| TROJANSCAN_ACL_STAFF
| TROJANSCAN_ACL_WEBSITE
| TROJANSCAN_ACL_CAT
;
893 trojanscan_database_free_result(res
);
897 trojanscan_database_escape_string(eaccount
, toadd
->authname
, strlen(toadd
->authname
));
898 trojanscan_database_query("INSERT INTO users (authname, authlevel) VALUES ('%s', %d)", eaccount
, level
);
899 trojanscan_reply(np
, "Account added to database, account %s%s.", toadd
->authname
, level
>0?" (first user so developer access)":"");
904 int trojanscan_user_level_by_authname(char *authname
) {
905 int result
= -1, sl
= strlen(authname
);
906 char eaccount
[TROJANSCAN_QUERY_TEMP_BUF_SIZE
];
908 trojanscan_database_escape_string(eaccount
, authname
, sl
);
909 if (!(trojanscan_database_query("SELECT authlevel, authname FROM users WHERE authname = '%s'", eaccount
))) {
910 trojanscan_database_res
*res
;
911 if ((res
= trojanscan_database_store_result(&trojanscan_sql
))) {
912 if (trojanscan_database_num_rows(res
) > 0) {
913 trojanscan_database_row sqlrow
= trojanscan_database_fetch_row(res
);
914 result
= atoi(sqlrow
[0]);
915 strlcpy(authname
, sqlrow
[1], sl
+ 1);
917 trojanscan_database_free_result(res
);
923 int trojanscan_user_id_by_authname(char *authname
) {
925 char eaccount
[TROJANSCAN_QUERY_TEMP_BUF_SIZE
];
927 trojanscan_database_escape_string(eaccount
, authname
, strlen(authname
));
928 if (!(trojanscan_database_query("SELECT id FROM users WHERE authname = '%s'", eaccount
))) {
929 trojanscan_database_res
*res
;
930 if ((res
= trojanscan_database_store_result(&trojanscan_sql
))) {
931 if (trojanscan_database_num_rows(res
) > 0) {
932 trojanscan_database_row sqlrow
= trojanscan_database_fetch_row(res
);
933 result
= atoi(sqlrow
[0]);
935 trojanscan_database_free_result(res
);
941 struct trojanscan_clones
*trojanscan_selectclone(char type
) {
942 struct trojanscan_clones
*rc
;
943 int randomclone
, hits
= 0, minlimit
, maxlimit
;
945 if(type
== TROJANSCAN_WATCH_CLONES
) {
946 minlimit
= TROJANSCAN_CLONE_MAX
;
947 maxlimit
= minlimit
+ TROJANSCAN_WATCHCLONE_MAX
- 1;
950 maxlimit
= TROJANSCAN_CLONE_MAX
- 1;
954 randomclone
= trojanscan_minmaxrand(minlimit
, maxlimit
);
957 rc
= &trojanscan_swarm
[randomclone
];
958 if ((type
== TROJANSCAN_NORMAL_CLONES
) && (rc
->sitting
== 0) && (rc
->remaining
== 0))
961 } while (rc
->remaining
== 0);
963 if(type
== TROJANSCAN_NORMAL_CLONES
) {
964 if ((rc
->sitting
== 0) && (rc
->remaining
== 0)) {
965 if ((!rc
->remaining
) && (!rc
->sitting
)) {
967 deregisterlocaluser(rc
->clone
, NULL
);
970 trojanscan_generateclone((void *)rc
->index
);
980 int trojanscan_nickbanned(trojanscan_clones
*np
, channel
*cp
) {
982 long realip
= np
->clone
->ipaddress
;
984 np
->clone
->ipaddress
= np
->fakeip
;
986 ret
= nickbanned(np
->clone
, cp
);
988 np
->clone
->ipaddress
= realip
;
993 struct trojanscan_realchannels
*trojanscan_allocaterc(char *chan
) {
994 struct trojanscan_realchannels
*rc
;
995 struct trojanscan_clones
*clonep
;
997 int attempts_left
= 10;
1000 trojanscan_errorcode
= 1; /* sorry splidge ;( */
1004 if(chan
[0] != '#') {
1005 trojanscan_errorcode
= 2;
1009 if (strlen(chan
) > 1) {
1010 if(strrchr(chan
, ',')) {
1011 trojanscan_errorcode
= 3;
1015 if(strrchr(chan
, ' ')) {
1016 trojanscan_errorcode
= 4;
1021 cp
= findchannel(chan
);
1023 trojanscan_errorcode
= 5;
1028 clonep
= trojanscan_selectclone(TROJANSCAN_NORMAL_CLONES
);
1030 trojanscan_errorcode
= 6;
1033 if(!trojanscan_nickbanned(clonep
, cp
))
1035 } while (--attempts_left
> 0);
1037 if (!attempts_left
) {
1038 trojanscan_errorcode
= 7;
1042 rc
= (struct trojanscan_realchannels
*)malloc(sizeof(struct trojanscan_realchannels
));
1052 void trojanscan_join(struct trojanscan_realchannels
*rc
) {
1053 struct trojanscan_realchannels
*rp
= trojanscan_realchanlist
;
1055 if (rc
->clone
&& rc
->clone
->clone
) {
1056 if (!localjoinchannel(rc
->clone
->clone
, rc
->chan
)) {
1057 rc
->clone
->remaining
--;
1058 rc
->clone
->sitting
++;
1059 if (trojanscan_minmaxrand(1, TROJANSCAN_NICKCHANGE_ODDS
)%TROJANSCAN_NICKCHANGE_ODDS
== 0)
1060 trojanscan_donickchange((void *)rc
->clone
);
1062 rc
->schedule
= scheduleoneshot(time(NULL
)+trojanscan_part_time
, &trojanscan_dopart
, (void *)rc
);
1065 for(;rp
->next
;rp
=rp
->next
);
1068 trojanscan_realchanlist
= rc
;
1075 int trojanscan_userjoin(void *sender
, int cargc
, char **cargv
) {
1076 nick
*np
= (nick
*)sender
;
1077 struct trojanscan_realchannels
*rc
;
1080 trojanscan_reply(np
, "Not enough paramaters");
1084 if (!trojanscan_swarm_created
) {
1085 trojanscan_reply(np
, "My swarm is currently empty.");
1089 if((rc
= trojanscan_allocaterc(cargv
[0]))) {
1090 trojanscan_log(np
, "join", cargv
[0]);
1091 trojanscan_join(rc
);
1092 trojanscan_reply(np
, "Clone has joined channel.");
1094 trojanscan_mainchanmsg("n: join: %s (%s/%s)", cargv
[0], np
->nick
, np
->authname
);
1096 if (trojanscan_errorcode
== 5) {
1097 trojanscan_reply(np
, "Not joining empty channel, check you entered the correct channel name.");
1099 trojanscan_reply(np
, "Clone could not join channel (error code %d)!", trojanscan_errorcode
);
1105 int trojanscan_rehash(void *sender
, int cargc
, char **cargv
) {
1106 nick
*np
= (void *)sender
;
1107 trojanscan_read_database(0);
1108 trojanscan_log(np
, "rehash", "");
1109 trojanscan_reply(np
, "Done.");
1113 int trojanscan_changelev(void *sender
, int cargc
, char **cargv
) {
1114 nick
*np
= (nick
*)sender
, *np2
;
1116 char eaccount
[TROJANSCAN_QUERY_TEMP_BUF_SIZE
], *tochange
, *p
, mode
= 1, error
= 0, clast
= 0, specialcase
;
1117 union trojanscan_userlevel flags1
, flags2
;
1120 trojanscan_reply(np
, "Not enough parameters.");
1124 templevel
= trojanscan_user_level_by_authname(np
->authname
);
1126 if (templevel
== -1) {
1127 trojanscan_reply(np
, "You do not have an account.");
1131 flags1
.number
= templevel
;
1133 if (cargv
[0][0] == '#') {
1134 tochange
= cargv
[0] + 1;
1136 np2
= getnickbynick(cargv
[0]);
1138 trojanscan_reply(np
, "That nickname is not on the network.");
1141 if (!IsAccount(np2
)) {
1142 trojanscan_reply(np
, "That user is not authed with the network.");
1145 tochange
= np2
->authname
;
1148 templevel
= trojanscan_user_level_by_authname(tochange
);
1150 if (templevel
== -1) {
1151 trojanscan_reply(np
, "User does not exist.");
1155 flags2
.number
= templevel
;
1157 if (!ircd_strcmp(np
->authname
, tochange
)) {
1163 for (p
=cargv
[1];*p
;p
++) {
1170 if (!TrojanscanIsDeveloper(flags1
))
1172 flags2
.values
.developer
= mode
;
1175 if (!TrojanscanIsDeveloper(flags1
))
1177 flags2
.values
.teamleader
= mode
;
1180 if (!TrojanscanIsLeastTeamLeader(flags1
))
1182 flags2
.values
.staff
= mode
;
1185 if (!TrojanscanIsDeveloper(flags1
))
1187 flags2
.values
.website
= mode
;
1190 if (!TrojanscanIsDeveloper(flags1
))
1192 flags2
.values
.cat
= mode
;
1200 if (specialcase
&& !mode
) { /* allow user to remove their own flags */
1211 trojanscan_reply(np
, "Unknown mode: %c%c.", mode
?'+':'-', *p
);
1213 trojanscan_reply(np
, "You have insufficient privilidges to add/remove one or more flags specified.");
1218 trojanscan_log(np
, "changelev", "%s %s", tochange
, cargv
[1]);
1219 trojanscan_database_escape_string(eaccount
, tochange
, strlen(tochange
));
1220 trojanscan_database_query("UPDATE users SET authlevel = %d WHERE authname = '%s'", flags2
.number
, eaccount
);
1222 trojanscan_reply(np
, "Flags changed.");
1227 int trojanscan_deluser(void *sender
, int cargc
, char **cargv
) {
1228 nick
*np
= (nick
*)sender
, *to
;
1230 char eaccount
[TROJANSCAN_QUERY_TEMP_BUF_SIZE
], *account
;
1231 union trojanscan_userlevel flags1
, flags2
;
1234 trojanscan_reply(np
, "Not enough parameters.");
1238 if (cargv
[0][0] == '#') {
1239 account
= cargv
[0] + 1;
1241 to
= getnickbynick(cargv
[0]);
1243 trojanscan_reply(np
, "That nickname is not on the network.");
1246 if (!IsAccount(to
)) {
1247 trojanscan_reply(np
, "That user is not authed with the network.");
1250 account
= to
->authname
;
1253 flags1
.number
= trojanscan_user_level_by_authname(np
->authname
);
1254 templevel
= trojanscan_user_level_by_authname(account
);
1256 if (templevel
== -1) {
1257 trojanscan_reply(np
, "Auth %s does not exist.", account
);
1261 flags2
.number
= templevel
;
1263 if (!TrojanscanIsDeveloper(flags1
) && TrojanscanIsLeastTeamLeader(flags2
)) {
1264 trojanscan_reply(np
, "Your cannot delete %s as his/her flags equal or surpass your own.", account
);
1268 trojanscan_log(np
, "deluser", account
);
1269 trojanscan_database_escape_string(eaccount
, account
, strlen(account
));
1270 trojanscan_database_query("DELETE FROM users WHERE authname = '%s'", eaccount
);
1271 trojanscan_reply(np
, "User deleted.");
1276 int trojanscan_add_ll(struct trojanscan_prechannels
**head
, struct trojanscan_prechannels
*newitem
) {
1277 struct trojanscan_prechannels
*position
, *lastitem
= NULL
, *location
= NULL
;
1280 newitem
->next
= NULL
;
1281 if (newitem
->exempt
) {
1287 /* if its exempt, we don't give a monkeys where it is... */
1288 if (newitem
->exempt
) {
1289 newitem
->next
= *head
;
1294 for(position
=*head
;position
;lastitem
=position
,position
=position
->next
) {
1295 if (!ircd_strcmp(position
->name
->content
, newitem
->name
->content
)) {
1299 if (!location
&& (position
->size
< newitem
->size
)) {
1303 location
= lastitem
;
1308 newitem
->next
= NULL
;
1309 lastitem
->next
= newitem
;
1311 newitem
->next
= location
->next
;
1312 location
->next
= newitem
;
1314 if(newitem
->exempt
) {
1321 void trojanscan_watch_clone_update(struct trojanscan_prechannels
*hp
, int count
) {
1323 struct trojanscan_prechannels
*lp
;
1324 struct trojanscan_templist
*markedlist
= NULL
;
1327 markedlist
= (struct trojanscan_templist
*)calloc(count
, sizeof(struct trojanscan_templist
));
1332 for(i
=0;i
<trojanscan_activechans
;i
++) {
1335 for(lp
=hp
,j
=0;j
<count
&&lp
;j
++,lp
=lp
->next
) {
1336 if(!markedlist
[j
].active
&& !lp
->exempt
&& !ircd_strcmp(lp
->name
->content
, trojanscan_chans
[i
].channel
->content
)) { /* we're already on the channel */
1337 if(trojanscan_chans
[i
].watch_clone
) {
1338 markedlist
[j
].active
= 1;
1339 markedlist
[j
].watch_clone
= trojanscan_chans
[i
].watch_clone
;
1340 lp
->watch_clone
= trojanscan_chans
[i
].watch_clone
;
1347 if(!marked
&& trojanscan_chans
[i
].watch_clone
) {
1348 channel
*cp
= findchannel(trojanscan_chans
[i
].channel
->content
);
1350 localpartchannel(trojanscan_chans
[i
].watch_clone
->clone
, cp
);
1357 for(j
=0,lp
=hp
;j
<count
&&lp
;j
++,lp
=lp
->next
) {
1358 if((!markedlist
[j
].active
|| !markedlist
[j
].watch_clone
) && !lp
->exempt
) {
1359 channel
*cp
= findchannel(lp
->name
->content
);
1363 lp
->watch_clone
= trojanscan_selectclone(TROJANSCAN_WATCH_CLONES
);
1364 if(!lp
->watch_clone
)
1366 if(!trojanscan_nickbanned(lp
->watch_clone
, cp
)) {
1367 if(localjoinchannel(lp
->watch_clone
->clone
, cp
))
1368 lp
->watch_clone
= NULL
;
1371 } while(--attempts
> 0);
1373 lp
->watch_clone
= NULL
;
1382 void trojanscan_fill_channels(void *arg
) {
1383 struct trojanscan_prechannels
*head
= NULL
, *lp
, *last
= NULL
;
1384 int i
, count
, tempctime
= 0;
1388 for (count
=i
=0;i
<trojanscan_database
.total_channels
;i
++) {
1389 lp
= (trojanscan_prechannels
*)malloc(sizeof(trojanscan_prechannels
));
1390 lp
->name
= trojanscan_database
.channels
[i
].name
;
1392 lp
->exempt
= trojanscan_database
.channels
[i
].exempt
;
1393 lp
->watch_clone
= NULL
;
1394 if (trojanscan_add_ll(&head
, lp
))
1398 for (i
=0;i
<CHANNELHASHSIZE
;i
++) {
1399 for(chn
=chantable
[i
];chn
;chn
=chn
->next
) {
1400 if (chn
->channel
&& !IsKey(chn
->channel
) && !IsInviteOnly(chn
->channel
) && !IsRegOnly(chn
->channel
) && (chn
->channel
->users
->totalusers
>= trojanscan_minchansize
)) {
1401 lp
= (trojanscan_prechannels
*)malloc(sizeof(trojanscan_prechannels
));
1402 lp
->name
= chn
->name
;
1403 lp
->size
= chn
->channel
->users
->totalusers
;
1405 lp
->watch_clone
= NULL
;
1406 if (trojanscan_add_ll(&head
, lp
))
1412 count
= TROJANSCAN_MMIN(count
, trojanscan_maxchans
);
1414 trojanscan_watch_clone_update(head
, count
);
1416 trojanscan_free_channels();
1417 trojanscan_chans
= (struct trojanscan_inchannel
*)calloc(count
, sizeof(struct trojanscan_inchannel
));
1418 trojanscan_activechans
= count
;
1421 for(lp
=head
;lp
;last
=lp
,lp
=lp
->next
) {
1422 if (!(lp
->exempt
) && (i
< count
)) {
1423 trojanscan_chans
[i
].channel
= getsstring(lp
->name
->content
, lp
->name
->length
);
1424 trojanscan_chans
[i
++].watch_clone
= lp
->watch_clone
;
1433 if (trojanscan_activechans
> 0) {
1434 tempctime
= trojanscan_cycletime
/ trojanscan_activechans
;
1437 trojanscan_mainchanmsg("d: just escaped a divide by zero error (no activechans!), rescheduling in 60 seconds");
1440 if(trojanscan_schedule
)
1441 deleteschedule(trojanscan_schedule
, &trojanscan_dojoin
, NULL
);
1443 trojanscan_channumber
= 0;
1445 trojanscan_schedule
= schedulerecurring(time(NULL
) + tempctime
, trojanscan_activechans
+ 1, tempctime
, trojanscan_dojoin
, NULL
);
1449 void trojanscan_dojoin(void *arg
) {
1450 struct trojanscan_realchannels
*rc
;
1451 if (trojanscan_channumber
>= trojanscan_activechans
) {
1452 trojanscan_schedule
= NULL
;
1453 trojanscan_fill_channels(NULL
);
1455 if ((rc
= trojanscan_allocaterc(trojanscan_chans
[trojanscan_channumber
++].channel
->content
)))
1456 trojanscan_join(rc
);
1461 void trojanscan_dopart(void *arg
) {
1462 struct trojanscan_realchannels
*rc
= (struct trojanscan_realchannels
*)arg
, *rp
, *past
= NULL
;
1464 if (rc
->kickedout
) { /* there's a join scheduled, wait for it (reschedule) */
1465 rc
->schedule
= scheduleoneshot(time(NULL
)+5, &trojanscan_dopart
, (void *)rc
);
1469 if (rc
->clone
->clone
&& (!(rc
->donotpart
)))
1470 localpartchannel(rc
->clone
->clone
, rc
->chan
);
1472 rc
->clone
->sitting
--;
1474 for(rp
=trojanscan_realchanlist
;rp
;rp
=rp
->next
) {
1477 trojanscan_realchanlist
= rp
->next
;
1479 past
->next
= rp
->next
;
1489 void trojanscan_donickchange(void *arg
) { /* just incase I choose to make this schedule at some point */
1490 struct trojanscan_clones
*clone
= (trojanscan_clones
*)arg
;
1491 if (clone
&& clone
->clone
) {
1492 char c_nick
[NICKLEN
+1];
1494 /* PPA: unlikely to be infinite */
1496 if ((loops
++ < 10) && trojanscan_hostmode
) {
1497 trojanscan_generatenick(c_nick
, NICKLEN
);
1499 trojanscan_gennick(c_nick
, trojanscan_minmaxrand(7, TROJANSCAN_MMIN(13, NICKLEN
)));
1501 } while (c_nick
&& (getnickbynick(c_nick
) != NULL
));
1503 renamelocaluser(clone
->clone
, c_nick
);
1508 int trojanscan_keysort(const void *v1
, const void *v2
) {
1509 return ((*(trojanscan_prechannels
**)v2
)->size
- (*(trojanscan_prechannels
**)v1
)->size
);
1512 int trojanscan_showcommands(void *sender
, int cargc
, char **cargv
) {
1513 nick
*np
= (nick
*)sender
;
1514 Command
*cmdlist
[100];
1518 n
= getcommandlist(trojanscan_cmds
, cmdlist
, 100);
1520 trojanscan_reply(np
, "The following commands are registered at present:");
1523 if (cmdlist
[i
]->level
& TROJANSCAN_ACL_STAFF
) {
1525 } else if (cmdlist
[i
]->level
& TROJANSCAN_ACL_DEVELOPER
) {
1527 } else if (cmdlist
[i
]->level
& TROJANSCAN_ACL_TEAMLEADER
) {
1529 } else if (cmdlist
[i
]->level
& TROJANSCAN_ACL_CAT
) {
1531 } else if (cmdlist
[i
]->level
& TROJANSCAN_ACL_WEBSITE
) {
1533 } else if (cmdlist
[i
]->level
& TROJANSCAN_ACL_UNAUTHED
) {
1537 trojanscan_reply(np
, "%s (+%c)", cmdlist
[i
]->command
->content
, level
);
1539 trojanscan_reply(np
, "%s", cmdlist
[i
]->command
->content
);
1542 trojanscan_reply(np
, "End of list.");
1546 void trojanscan_handlemessages(nick
*target
, int messagetype
, void **args
) {
1549 int cargc
, templevel
;
1551 union trojanscan_userlevel level
;
1553 switch(messagetype
) {
1556 /* If it's a message, first arg is nick and second is message */
1557 sender
= (nick
*)args
[0];
1559 if(strncmp(TROJANSCAN_VERSION_DETECT
, args
[1], sizeof(TROJANSCAN_VERSION_DETECT
)-1)==0) {
1560 char p
= ((char *)args
[1])[sizeof(TROJANSCAN_VERSION_DETECT
)-1];
1561 if((p
== ' ') || (p
== '\0') || (p
== 1)) {
1562 trojanscan_reply(sender
, "\001VERSION Trojanscan (or Tigger) Newserv module version %s by Chris Porter (slug), Newserv by David Mansell (splidge). Compiled " __DATE__
" " __TIME__
".\001", TROJANSCAN_VERSION
);
1567 /* Split the line into params */
1568 cargc
= splitline((char *)args
[1], cargv
, 50, 0);
1570 cmd
=findcommandintree(trojanscan_cmds
, cargv
[0], 1);
1572 trojanscan_reply(sender
, "Unknown command.");
1576 if ((cmd
->level
& TROJANSCAN_ACL_OPER
) && !IsOper(sender
)) {
1577 trojanscan_reply(sender
, "You need to be opered to use this command.");
1581 /* bit grim code... */
1583 if (!(cmd
->level
& TROJANSCAN_ACL_UNAUTHED
)) {
1584 if (!IsAccount(sender
)) {
1585 trojanscan_reply(sender
, "You must be authed with the network to access this command!");
1588 templevel
= trojanscan_user_level_by_authname(sender
->authname
);
1590 if (templevel
== -1) {
1591 trojanscan_reply(sender
, "You do not have an account.");
1595 level
.number
= templevel
;
1597 if ((cmd
->level
& TROJANSCAN_ACL_DEVELOPER
) && !TrojanscanIsDeveloper(level
)) {
1598 trojanscan_reply(sender
, "Access denied.");
1601 if ((cmd
->level
& TROJANSCAN_ACL_TEAMLEADER
) && !TrojanscanIsLeastTeamLeader(level
)) {
1602 trojanscan_reply(sender
, "Access denied.");
1605 if ((cmd
->level
& TROJANSCAN_ACL_STAFF
) && !TrojanscanIsLeastStaff(level
)) {
1606 trojanscan_reply(sender
, "Access denied.");
1609 if ((cmd
->level
& TROJANSCAN_ACL_CAT
) && !TrojanscanIsCat(level
)) {
1610 trojanscan_reply(sender
, "Access denied.");
1613 if ((cmd
->level
& TROJANSCAN_ACL_WEBSITE
) && !TrojanscanIsLeastWebsite(level
)) {
1614 trojanscan_reply(sender
, "Access denied.");
1619 /* Check the maxargs */
1620 if (cmd
->maxparams
<(cargc
-1)) {
1621 /* We need to do some rejoining */
1622 rejoinline(cargv
[cmd
->maxparams
], cargc
-(cmd
->maxparams
));
1623 cargc
= (cmd
->maxparams
) + 1;
1626 (cmd
->handler
)((void *)sender
, cargc
- 1, &(cargv
[1]));
1630 /* someone killed me? Bastards */
1631 trojanscan_connect_schedule
= scheduleoneshot(time(NULL
) + 1, &trojanscan_connect
, NULL
);
1632 trojanscan_nick
= NULL
;
1640 char trojanscan_getmtfrommessagetype(int input
) {
1642 case LU_PRIVMSG
: return 'm';
1643 case LU_PRIVNOTICE
: return 'n';
1644 case LU_SECUREMSG
: return 's';
1645 case LU_CHANMSG
: return 'M';
1646 case LU_CHANNOTICE
: return 'N';
1647 default: return '?';
1651 void trojanscan_clonehandlemessages(nick
*target
, int messagetype
, void **args
) {
1652 char *pretext
= NULL
, etext
[TROJANSCAN_QUERY_TEMP_BUF_SIZE
], enick
[TROJANSCAN_QUERY_TEMP_BUF_SIZE
], eident
[TROJANSCAN_QUERY_TEMP_BUF_SIZE
], ehost
[TROJANSCAN_QUERY_TEMP_BUF_SIZE
], text
[513], detected
= 0;
1654 struct trojanscan_realchannels
*rp
;
1655 struct trojanscan_rejoinlist
*rj
;
1656 unsigned int i
, len
;
1657 struct trojanscan_worms
*worm
;
1659 char mt
= trojanscan_getmtfrommessagetype(messagetype
);
1662 switch(messagetype
) {
1667 pretext
= (char *)args
[1];
1672 sender
= (nick
*)args
[0];
1673 if (strlen(sender
->nick
) < 2)
1677 pretext
= (char *)args
[2];
1679 if(strncmp(TROJANSCAN_VERSION_DETECT
, pretext
, sizeof(TROJANSCAN_VERSION_DETECT
)-1)==0) {
1680 char p
= pretext
[sizeof(TROJANSCAN_VERSION_DETECT
)-1];
1681 if((p
== ' ') || (p
== '\0') || (p
== 1)) {
1683 if (IsOper(sender
)) {
1686 if (IsAccount(sender
)) {
1687 int templevel
= trojanscan_user_level_by_authname(sender
->authname
);
1688 if (templevel
!= -1) {
1689 union trojanscan_userlevel level
;
1690 level
.number
= templevel
;
1691 if (TrojanscanIsLeastStaff(level
))
1697 if(trojanscan_nick
) {
1698 sendnoticetouser(target
, sender
, "\001VERSION T clone, check T for confirmation.\001");
1699 sendnoticetouser(trojanscan_nick
, sender
, "\001VERSION %s is part of my swarm.\001", target
->nick
);
1701 sendnoticetouser(target
, sender
, "\001VERSION T clone, though since T is currently gone you'll have to version me again in a minute for confirmation.\001");
1704 sendnoticetouser(target
, sender
, "\001VERSION " TROJANSCAN_CLONE_VERSION_REPLY
"\001");
1711 trojanscan_strip_codes(text
, sizeof(text
) - 1, pretext
);
1715 for(i
=0;i
<trojanscan_database
.total_phrases
;i
++) {
1718 (worm
= trojanscan_database
.phrases
[i
].worm
)
1723 (messagetype
== LU_PRIVMSG
) || (messagetype
== LU_SECUREMSG
) || (messagetype
== LU_PRIVNOTICE
)
1726 (trojanscan_database
.phrases
[i
].worm
->hitpriv
)
1731 (messagetype
== LU_CHANMSG
) || (messagetype
== LU_CHANNOTICE
)
1734 (trojanscan_database
.phrases
[i
].worm
->hitchans
)
1738 (trojanscan_database
.phrases
[i
].phrase
)
1740 int pre
= pcre_exec(trojanscan_database
.phrases
[i
].phrase
, trojanscan_database
.phrases
[i
].hint
, text
, len
, 0, 0, vector
, 30);
1742 char glinemask
[HOSTLEN
+ USERLEN
+ NICKLEN
+ 4];
1745 unsigned int j
, usercount
, frequency
;
1746 char ip
[TROJANSCAN_IPLEN
];
1748 channel
*chp
= (channel
*)args
[1];
1750 nick
*np
= NULL
; /* sigh at warnings */
1754 trojanscan_database
.detections
++;
1756 if (!(hp
=findhost(sender
->host
->name
->content
))) {
1757 trojanscan_mainchanmsg("w: user %s!%s@%s triggered infection monitor, yet no hosts found at stage 1 -- worm: %s", sender
->nick
, sender
->ident
, sender
->host
->name
->content
, worm
->name
->content
);
1761 usercount
= 0; /* stupid warnings */
1762 if (worm
->monitor
) {
1765 } else if (worm
->glinehost
&& (hp
->clonecount
<= TROJANSCAN_MAX_HOST_GLINE
)) {
1766 snprintf(glinemask
, sizeof(glinemask
) - 1, "*@%s", trojanscan_iptostr(ip
, sizeof(ip
) - 1, sender
->ipaddress
));
1767 usercount
= hp
->clonecount
;
1769 else if (worm
->glineuser
|| (worm
->glinehost
&& hp
->clonecount
> TROJANSCAN_MAX_HOST_GLINE
)) {
1770 userbit
= sender
->ident
;
1771 if(userbit
[0] == '~')
1773 snprintf(glinemask
, sizeof(glinemask
) - 1, "*%s@%s", userbit
, trojanscan_iptostr(ip
, sizeof(ip
) - 1, sender
->ipaddress
));
1774 for (j
=0;j
<NICKHASHSIZE
;j
++) {
1775 for (np
=nicktable
[j
];np
;np
=np
->next
) {
1776 if ((np
->host
==hp
) && (!ircd_strcmp(np
->ident
,sender
->ident
)))
1783 trojanscan_mainchanmsg("w: user %s!%s@%s triggered infection monitor, yet no hosts found at stage 2 -- worm: %s", sender
->nick
, sender
->ident
, sender
->host
->name
->content
, worm
->name
->content
);
1787 if (glining
&& (usercount
> trojanscan_maxusers
)) {
1788 trojanscan_mainchanmsg("w: not glining %s!%s@%s due to too many users (%d) with mask: *!%s -- worm: %s)", sender
->nick
, sender
->ident
, sender
->host
->name
->content
, usercount
, glinemask
, worm
->name
->content
);
1792 if (glining
&& !worm
->datalen
) {
1793 trojanscan_mainchanmsg("w: not glining %s!%s@%s due to too lack of removal data with mask: *!%s (%d users) -- worm: %s)", sender
->nick
, sender
->ident
, sender
->host
->name
->content
, glinemask
, usercount
, worm
->name
->content
);
1797 trojanscan_database_escape_string(enick
, sender
->nick
, strlen(sender
->nick
));
1798 trojanscan_database_escape_string(eident
, sender
->ident
, strlen(sender
->ident
));
1799 trojanscan_database_escape_string(ehost
, sender
->host
->name
->content
, sender
->host
->name
->length
);
1803 if (!(trojanscan_database_query("SELECT COUNT(*) FROM hits WHERE glined = %d AND host = '%s'", glining
, ehost
))) {
1804 trojanscan_database_res
*res
;
1805 if ((res
= trojanscan_database_store_result(&trojanscan_sql
))) {
1806 trojanscan_database_row sqlrow
;
1807 if ((trojanscan_database_num_rows(res
) > 0) && (sqlrow
= trojanscan_database_fetch_row(res
)))
1808 frequency
= atoi(sqlrow
[0]) + 1;
1809 trojanscan_database_free_result(res
);
1816 matchbuf
[512] = 0; /* hmm */
1819 if (pcre_copy_substring(text
, vector
, pre
, 1, matchbuf
, sizeof(matchbuf
) - 1) <= 0)
1822 trojanscan_mainchanmsg("m: t: %c u: %s!%s@%s%s%s w: %s %s%s", mt
, sender
->nick
, sender
->ident
, sender
->host
->name
->content
, mt
=='N'||mt
=='M'?" #: ":"", mt
=='N'||mt
=='M'?chp
->index
->name
->content
:"", worm
->name
->content
, matchbuf
[0]?" --: ":"", matchbuf
[0]?matchbuf
:"");
1823 /* trojanscan_peonchanmsg("m: t: %c u: %s!%s@%s%s%s%s w: %s %s%s", mt, sender->nick, sender->ident, (IsHideHost(sender)&&IsAccount(sender))?sender->authname:sender->host->name->content, (IsHideHost(sender)&&IsAccount(sender))?"."HIS_HIDDENHOST:"", mt=='N'||mt=='M'?" #: ":"", mt=='N'||mt=='M'?chp->index->name->content:"", worm->name->content, matchbuf[0]?" --: ":"", matchbuf[0]?matchbuf:""); */
1825 int glinetime
= TROJANSCAN_FIRST_OFFENSE
* frequency
* (worm
->epidemic
?TROJANSCAN_EPIDEMIC_MULTIPLIER
:1);
1826 if(glinetime
> 7 * 24)
1827 glinetime
= 7 * 24; /* can't set glines over 7 days with normal non U:lined glines */
1829 trojanscan_database_query("INSERT INTO hits (nickname, ident, host, phrase, messagetype, glined) VALUES ('%s', '%s', '%s', %d, '%c', %d)", enick
, eident
, ehost
, trojanscan_database
.phrases
[i
].id
, mt
, glining
);
1830 trojanscan_database
.glines
++;
1832 irc_send("%s GL * +%s %d :You (%s!%s@%s) are infected with a trojan (%s), see %s%d for details - banned for %d hours\r\n", mynumeric
->content
, glinemask
, glinetime
* 3600, sender
->nick
, sender
->ident
, sender
->host
->name
->content
, worm
->name
->content
, TROJANSCAN_URL_PREFIX
, worm
->id
, glinetime
);
1834 trojanscan_mainchanmsg("g: *!%s t: %c u: %s!%s@%s%s%s c: %d w: %s%s f: %d", glinemask
, mt
, sender
->nick
, sender
->ident
, sender
->host
->name
->content
, mt
=='N'||mt
=='M'?" #: ":"", mt
=='N'||mt
=='M'?chp
->index
->name
->content
:"", usercount
, worm
->name
->content
, worm
->epidemic
?"(E)":"", frequency
);
1841 if (!detected
&& (mt
!= 'N') && (mt
!= 'M')) {
1842 trojanscan_database_escape_string(etext
, text
, len
);
1843 trojanscan_database_escape_string(enick
, sender
->nick
, strlen(sender
->nick
));
1844 trojanscan_database_escape_string(eident
, sender
->ident
, strlen(sender
->ident
));
1845 trojanscan_database_escape_string(ehost
, sender
->host
->name
->content
, sender
->host
->name
->length
);
1846 trojanscan_database_query("INSERT INTO unknownlog (data, user) VALUES ('%s','%s!%s@%s')", etext
, enick
, eident
, ehost
);
1850 /* someone killed me? Bastards */
1852 /* PPA: we do NOT rejoin channels at this moment in time, it is possible to do this though */
1853 for (i
=0;i
<TROJANSCAN_CLONE_TOTAL
;i
++) {
1854 if (trojanscan_swarm
[i
].clone
== target
) {
1856 scheduleoneshot(time(NULL
)+1, &trojanscan_generateclone
, (void *)i
);
1857 if(i
>= TROJANSCAN_CLONE_MAX
) {
1859 for(j
=0;j
<trojanscan_activechans
;j
++)
1860 if(trojanscan_chans
[j
].watch_clone
== &trojanscan_swarm
[i
])
1861 trojanscan_chans
[j
].watch_clone
= NULL
;
1863 for(rp
=trojanscan_realchanlist
;rp
;rp
=rp
->next
)
1864 if ((rp
->clone
== &(trojanscan_swarm
[i
])))
1867 trojanscan_swarm
[i
].clone
= NULL
;
1868 trojanscan_swarm
[i
].remaining
= 0; /* bah */
1875 channel_name
= ((channel
*)args
[1])->index
->name
->content
;
1876 for (i
=0;i
<trojanscan_activechans
;i
++) {
1877 if (!trojanscan_chans
[i
].watch_clone
)
1879 if ((trojanscan_chans
[i
].watch_clone
->clone
== target
) && (!strcmp(trojanscan_chans
[i
].channel
->content
, channel_name
)))
1882 if(i
!= trojanscan_activechans
) {
1884 for(j
=0;j
<TROJANSCAN_CLONE_TOTAL
;j
++) {
1885 if(&trojanscan_swarm
[j
] == trojanscan_chans
[i
].watch_clone
) {
1886 trojanscan_chans
[i
].watch_clone
= NULL
;
1892 trojanscan_mainchanmsg("k: %s on %s by %s", target->nick, ((channel *)args[1])->index->name->content, (((nick *)args[0])->nick)?(((nick *)args[0])->nick):"(server)");
1894 rj
= (struct trojanscan_rejoinlist
*)malloc(sizeof(struct trojanscan_rejoinlist
));
1897 for(rp
=trojanscan_realchanlist
;rp
;rp
=rp
->next
)
1898 if ((rp
->clone
->clone
== target
) && (rp
->chan
== args
[1])) {
1908 rj
->channel
= getsstring(((channel
*)args
[1])->index
->name
->content
, ((channel
*)args
[1])->index
->name
->length
);
1910 trojanscan_mainchanmsg("d: unable to allocate memory for channel: %s upon rejoin", ((channel
*)args
[1])->index
->name
->content
);
1915 rj
->clone
= rp
->clone
;
1916 rj
->next
= trojanscan_schedulerejoins
;
1917 trojanscan_schedulerejoins
= rj
;
1919 rj
->schedule
= scheduleoneshot(time(NULL
)+1, &trojanscan_rejoin_channel
, (void *)rj
);
1928 void trojanscan_rejoin_channel(void *arg
) {
1929 struct trojanscan_rejoinlist
*rj2
, *lrj
, *rj
= (struct trojanscan_rejoinlist
*)arg
;
1931 channel
*cp
= findchannel(rj
->channel
->content
);
1932 freesstring(rj
->channel
);
1935 rj
->rp
->kickedout
--;
1937 rj
->rp
->donotpart
= 1; /* we were the last user on the channel, so we need to be VERY careful freeing it */
1939 if(!rj
->rp
->donotpart
&& !rj
->rp
->kickedout
) { /* check we're allowed to join channels (not killed), and we're the last one to join */
1940 if (trojanscan_nickbanned(rj
->clone
, cp
)) {
1941 rj
->rp
->donotpart
= 1;
1943 localjoinchannel(rj
->clone
->clone
, cp
);
1949 rj2
= trojanscan_schedulerejoins
;
1953 trojanscan_schedulerejoins
= rj
->next
;
1956 for(rj2
=trojanscan_schedulerejoins
;rj2
;lrj
=rj2
,rj2
=rj2
->next
) {
1958 lrj
->next
= rj2
->next
;
1967 void trojanscan_reply(nick
*target
, char *message
, ... ) {
1971 if (!trojanscan_nick
)
1974 va_start(va
, message
);
1975 vsnprintf(buf
, sizeof(buf
) - 1, message
, va
);
1978 sendnoticetouser(trojanscan_nick
, target
, "%s", buf
);
1982 void trojanscan_mainchanmsg(char *message
, ...) {
1987 if (!trojanscan_nick
)
1989 if (!(cp
= findchannel(TROJANSCAN_CHANNEL
)))
1992 va_start(va
, message
);
1993 vsnprintf(buf
, sizeof(buf
) - 1, message
, va
);
1996 sendmessagetochannel(trojanscan_nick
, cp
, "%s", buf
);
1999 void trojanscan_peonchanmsg(char *message
, ...) {
2004 if (!trojanscan_nick
)
2006 if (!(cp
= findchannel(TROJANSCAN_PEONCHANNEL
)))
2009 va_start(va
, message
);
2010 vsnprintf(buf
, sizeof(buf
) - 1, message
, va
);
2013 sendmessagetochannel(trojanscan_nick
, cp
, "%s", buf
);
2016 int trojanscan_minmaxrand(float min
, float max
) {
2017 return (int)((max
-min
+1)*rand()/(RAND_MAX
+min
))+min
;
2020 char *trojanscan_iptostr(char *buf
, int buflen
, unsigned int ip
) {
2021 snprintf(buf
, buflen
, "%d.%d.%d.%d", ip
>> 24, (ip
>> 16) & 255, (ip
>> 8) & 255, ip
& 255);
2025 char trojanscan_genchar(int ty
) {
2026 /* hostname and realname characters*/
2028 if (!(trojanscan_minmaxrand(0, 40) % 10)) {
2029 return trojanscan_minmaxrand(48, 57);
2031 return trojanscan_minmaxrand(97, 122);
2033 /* ident characters - without numbers*/
2034 } else if (ty
== 1) {
2035 return trojanscan_minmaxrand(97, 122);
2036 /* ident characters - with numbers*/
2037 } else if (ty
== 2) {
2038 ty
= trojanscan_minmaxrand(97, 125);
2039 if (ty
> 122) return trojanscan_minmaxrand(48, 57);
2041 /* nick characters - with and without numbers*/
2042 } else if (ty
== 3 || ty
== 4) {
2043 if (!(trojanscan_minmaxrand(0, 59) % 16)) {
2044 char weirdos
[6] = { '\\', '|', '[', '{', ']', '}' };
2045 return weirdos
[trojanscan_minmaxrand(0, 5)];
2048 ty
= trojanscan_minmaxrand(65, 93);
2049 if (ty
> 90) return trojanscan_minmaxrand(48, 57);
2051 ty
= trojanscan_minmaxrand(65, 90);
2053 if (!(trojanscan_minmaxrand(0, 40) % 8)) return ty
;
2061 void trojanscan_gennick(char *ptc
, char size
) {
2063 for (i
=0;i
<size
;i
++) {
2065 ptc
[i
] = trojanscan_genchar(3);
2067 ptc
[i
] = trojanscan_genchar(4);
2073 void trojanscan_genident(char *ptc
, char size
) {
2075 for (i
=0;i
<size
;i
++) {
2077 ptc
[i
] = trojanscan_genchar(1);
2079 ptc
[i
] = trojanscan_genchar(2);
2085 void trojanscan_genhost(char *ptc
, char size
, long *fakeip
) {
2086 int dots
= trojanscan_minmaxrand(2, 5), i
, dotexist
= 0, cur
;
2089 for (i
=0;i
<size
;i
++) {
2090 ptc
[i
] = trojanscan_genchar(0);
2091 if ((i
> 5) && (i
< (size
-4))) {
2092 if ((ptc
[i
-1] != '.') && (ptc
[i
-1] != '-')) {
2093 cur
= trojanscan_minmaxrand(1,size
/ dots
);
2108 *fakeip
= (trojanscan_minmaxrand(0, 65535) << 16) | trojanscan_minmaxrand(0, 65535);
2111 void trojanscan_genreal(char *ptc
, char size
) {
2112 int spaces
= trojanscan_minmaxrand(2, 4), i
;
2113 for (i
=0;i
<size
;i
++) {
2114 ptc
[i
] = trojanscan_genchar(0);
2115 if ((i
> 5) && (i
< (size
-4))) {
2116 if (ptc
[i
-1] != ' ') {
2117 if (trojanscan_minmaxrand(1,size
/ spaces
) == 1) ptc
[i
] = ' ';
2124 int trojanscan_is_not_octet(char *begin
, int length
) {
2128 for(i
=0;i
<length
;i
++) {
2129 if (!((*begin
>= '0') && (*begin
<= '9')))
2136 int trojanscan_generatepool(void) {
2137 int i
, k
= 0, j
= 0, loops
= 0;
2141 for (i
=0;i
<NICKHASHSIZE
;i
++)
2142 for (np
=nicktable
[i
];np
;np
=np
->next
)
2145 if(j
< trojanscan_min_hosts
)
2148 if(TROJANSCAN_HOST_MODE
== TROJANSCAN_STEAL_HOST
)
2149 return TROJANSCAN_MINPOOLSIZE
;
2153 for (j
=trojanscan_minmaxrand(0, NICKHASHSIZE
-1);j
<NICKHASHSIZE
;j
++) {
2155 for(p
=nicktable
[j
]->host
->name
->content
, pp
=p
;*p
;) {
2157 if (!trojanscan_is_not_octet(pp
, p
-pp
)) {
2158 if (i
< TROJANSCAN_POOLSIZE
) {
2159 if (i
< trojanscan_hostpoolsize
)
2160 freesstring(trojanscan_hostpool
[i
]);
2161 trojanscan_hostpool
[i
] = getsstring(pp
, p
-pp
);
2164 if (k
>= TROJANSCAN_POOLSIZE
)
2171 if (!trojanscan_is_not_octet(pp
, p
-pp
)) {
2172 if (k
< TROJANSCAN_POOLSIZE
) {
2173 if (k
< trojanscan_tailpoolsize
)
2174 freesstring(trojanscan_tailpool
[k
]);
2175 trojanscan_tailpool
[k
] = getsstring(pp
, p
-pp
);
2178 if (i
>= TROJANSCAN_POOLSIZE
)
2185 } while ((loops
< 5) && ((i
< TROJANSCAN_POOLSIZE
) || (k
< TROJANSCAN_POOLSIZE
)));
2187 trojanscan_hostpoolsize
= i
;
2188 trojanscan_tailpoolsize
= k
;
2192 nick
*trojanscan_selectuser(void) {
2193 int target
= trojanscan_minmaxrand(0, 500), loops
= 150, j
;
2196 for (j
=trojanscan_minmaxrand(0, NICKHASHSIZE
-1);j
<NICKHASHSIZE
;j
++)
2197 for(np
=nicktable
[j
];np
;np
=np
->next
)
2200 } while(--loops
> 0);
2204 host
*trojanscan_selecthost(void) {
2205 int target
= trojanscan_minmaxrand(0, 500), loops
= 150, j
;
2208 for (j
=trojanscan_minmaxrand(0, HOSTHASHSIZE
-1);j
<HOSTHASHSIZE
;j
++)
2209 for(hp
=hosttable
[j
];hp
;hp
=hp
->next
)
2212 } while(--loops
> 0);
2217 void trojanscan_generatehost(char *buf
, int maxsize
, long *fakeip
) {
2218 if(TROJANSCAN_HOST_MODE
== TROJANSCAN_STEAL_HOST
) {
2225 hp
= trojanscan_selecthost();
2226 if(hp
&& (hp
->clonecount
<= TROJANSCAN_MAX_CLONE_COUNT
) && !trojanscan_isip(hp
->name
->content
)) {
2227 strlcpy(buf
, hp
->name
->content
, maxsize
+ 1);
2229 *fakeip
= hp
->nicks
->ipaddress
;
2231 *fakeip
= (trojanscan_minmaxrand(0, 65535) << 16) | trojanscan_minmaxrand(0, 65535);
2235 } while(--loops
> 0);
2238 int pieces
= trojanscan_minmaxrand(2, 4), totallen
= 0, a
= 0, i
;
2239 int *choices
= malloc(sizeof(int) * (pieces
+ 1));
2240 int *lengths
= malloc(sizeof(int) * (pieces
+ 1));
2242 choices
[pieces
] = trojanscan_minmaxrand(0, trojanscan_tailpoolsize
-1);
2243 lengths
[pieces
] = strlen(trojanscan_tailpool
[choices
[pieces
]]->content
) + 1;
2244 totallen
+= lengths
[pieces
];
2246 for (i
=0;i
<pieces
;i
++) {
2247 choices
[i
] = trojanscan_minmaxrand(0, trojanscan_hostpoolsize
-1);
2248 lengths
[i
] = strlen(trojanscan_hostpool
[choices
[i
]]->content
) + 1;
2249 if (totallen
+lengths
[i
] > maxsize
) {
2250 choices
[i
] = choices
[pieces
];
2251 lengths
[i
] = lengths
[pieces
];
2255 totallen
+= lengths
[i
];
2258 for (i
=0;i
<pieces
;i
++) {
2259 for (cpos
=trojanscan_hostpool
[choices
[i
]]->content
; *cpos
;)
2264 for (cpos
=trojanscan_tailpool
[choices
[i
]]->content
; *cpos
;) {
2272 *fakeip
= (trojanscan_minmaxrand(0, 65535) << 16) | trojanscan_minmaxrand(0, 65535);
2276 void trojanscan_generatenick(char *buf
, int maxsize
) {
2277 int bits
= trojanscan_minmaxrand(2, 3), loops
= 0, wanttocopy
, len
= 0, i
, d
= 0, newmaxsize
= maxsize
- trojanscan_minmaxrand(0, 7);
2281 maxsize
= newmaxsize
;
2284 np
= trojanscan_selectuser();
2286 wanttocopy
= trojanscan_minmaxrand(1, (strlen(np
->nick
) / 2) + 3);
2287 for(i
=0;((i
<wanttocopy
) && (len
<maxsize
));i
++)
2288 buf
[len
++] = np
->nick
[i
];
2294 } while (++loops
< 10);
2298 void trojanscan_generateident(char *buf
, int maxsize
) {
2299 nick
*np
= trojanscan_selectuser();
2302 strlcpy(buf
, np
->ident
, maxsize
+ 1);
2305 void trojanscan_generaterealname(char *buf
, int maxsize
) {
2306 nick
*np
= trojanscan_selectuser();
2309 strlcpy(buf
, np
->realname
->name
->content
, maxsize
+ 1);
2312 void trojanscan_database_close(void) {
2313 mysql_close(&trojanscan_sql
);
2316 int trojanscan_database_connect(char *dbhost
, char *dbuser
, char *dbpass
, char *db
, unsigned int port
) {
2317 mysql_init(&trojanscan_sql
);
2318 if (!mysql_real_connect(&trojanscan_sql
, dbhost
, dbuser
, dbpass
, db
, port
, NULL
, 0))
2323 void trojanscan_database_escape_string(char *dest
, char *source
, size_t length
) {
2324 mysql_escape_string(dest
, source
, length
);
2327 int trojanscan_database_query(char *format
, ...) {
2328 char trojanscan_sqlquery
[TROJANSCAN_QUERY_BUF_SIZE
];
2331 va_start(va
, format
);
2332 vsnprintf(trojanscan_sqlquery
, sizeof(trojanscan_sqlquery
) - 1, format
, va
);
2334 return mysql_query(&trojanscan_sql
, trojanscan_sqlquery
);
2337 int trojanscan_database_num_rows(trojanscan_database_res
*res
) {
2338 return mysql_num_rows(res
);
2341 trojanscan_database_res
*trojanscan_database_store_result() {
2342 return mysql_store_result(&trojanscan_sql
);
2345 trojanscan_database_row
trojanscan_database_fetch_row(trojanscan_database_res
*res
) {
2346 return mysql_fetch_row(res
);
2349 void trojanscan_database_free_result(trojanscan_database_res
*res
) {
2350 mysql_free_result(res
);
2353 int trojanscan_isip(char *host
) {
2354 char *p
= host
, components
= 0, length
= 0;
2358 if(((!length
) || (length
= 0)) || (++components
> 3))
2361 if ((++length
> 3) || !isdigit(*p
))
2365 return components
== 3;