]>
jfr.im git - irc/quakenet/newserv.git/blob - proxyscan/proxyscandb.c
4 * Handles the database interface routines for proxyscan.
9 #include <mysql/mysql.h>
10 #include "../core/config.h"
11 #include "../lib/sstring.h"
12 #include "../irc/irc_config.h"
13 #include "../lib/irc_string.h"
14 #include "../core/error.h"
15 #include "../nick/nick.h"
16 #include "../localuser/localuser.h"
26 * Connects to the database and recovers the last gline ID
29 int proxyscandbinit () {
30 sstring
* dbhost
,* dbusername
,* dbpassword
,* dbdatabase
,* dbport
;
36 dbhost
= getcopyconfigitem ( "proxyscan" , "dbhost" , "localhost" , HOSTLEN
);
37 dbusername
= getcopyconfigitem ( "proxyscan" , "dbusername" , "proxyscan" , 20 );
38 dbpassword
= getcopyconfigitem ( "proxyscan" , "dbpassword" , "moo" , 20 );
39 dbdatabase
= getcopyconfigitem ( "proxyscan" , "dbdatabase" , "proxyscan" , 20 );
40 dbport
= getcopyconfigitem ( "proxyscan" , "dbport" , "3306" , 8 );
42 mysql_init (& proxyscansql
);
43 if (! mysql_real_connect (& proxyscansql
, dbhost
-> content
, dbusername
-> content
,
44 dbpassword
-> content
, dbdatabase
-> content
,
45 strtol ( dbport
-> content
, NULL
, 10 ), NULL
, 0 )) {
46 Error ( "proxyscan" , ERR_ERROR
, "Unable to connect to database" );
51 freesstring ( dbusername
);
52 freesstring ( dbpassword
);
53 freesstring ( dbdatabase
);
58 /* Set up the table */
59 mysql_query (& proxyscansql
, "CREATE TABLE openproxies (ID BIGINT not null, IP VARCHAR (20) not null, PM INT not null, TS DATETIME not null, RH TEXT not null, PRIMARY KEY (ID), INDEX (ID), UNIQUE (ID))" );
62 if (( mysql_query (& proxyscansql
, "SELECT max(ID) FROM openproxies" ))!= 0 ) {
63 Error ( "proxyscan" , ERR_ERROR
, "Unable to retrieve max ID from database" );
67 myres
= mysql_store_result (& proxyscansql
);
68 if ( mysql_num_fields ( myres
)!= 1 ) {
69 Error ( "proxyscan" , ERR_ERROR
, "Weird format retrieving max ID" );
70 mysql_free_result ( myres
);
75 if (( myrow
= mysql_fetch_row ( myres
))) {
79 lastid
= strtol ( myrow
[ 0 ], NULL
, 10 );
81 Error ( "proxyscan" , ERR_INFO
, "Retrieved lastid %d from database." , lastid
);
84 mysql_free_result ( myres
);
90 * Given a scan number, returns the string associated.
93 const char * scantostr ( int type
) {
94 char * reason
= "UNKNOWN" ;
127 * Given a scan number, returns the bitmask associated (as used in the
128 * previous version of P)
131 int scantodm ( int scannum
) {
137 * This function takes the given scanhost (which is assumed to have
138 * at least one "OPEN" scan) and logs it to the database. It returns
139 * the unique ID assigned to this gline (for the gline message itself).
142 void loggline ( cachehost
* chp
) {
143 char reasonlist
[ 100 ];
157 for ( fpp
= chp
-> proxies
; fpp
; fpp
= fpp
-> next
) {
158 reasonpos
+= sprintf ( reasonlist
+ reasonpos
, " %s : %d " , scantostr ( fpp
-> type
), fpp
-> port
);
161 if ( chp
-> glineid
== 0 ) {
162 chp
-> glineid
=++ lastid
;
164 mysql_escape_string ( reasonesc
, reasonlist
, strlen ( reasonlist
));
165 sprintf ( sqlquery
, "INSERT INTO openproxies VALUES( %u ,' %s ', %d ,NOW(),' %s ')" , chp
-> glineid
,
166 IPtostr ( chp
-> IP
), reasonmask
, reasonesc
);
167 mysql_query (& proxyscansql
, sqlquery
);
169 mysql_escape_string ( reasonesc
, reasonlist
, strlen ( reasonlist
));
170 sprintf ( sqlquery
, "UPDATE openproxies SET PM= %d ,RH=' %s ' where ID= %u " ,
171 reasonmask
, reasonesc
, chp
-> glineid
);
172 mysql_query (& proxyscansql
, sqlquery
);
178 * Closes the db socket when proxyscan is unloaded
181 void proxyscandbclose () {
182 if ( sqlconnected
== 1 ) {
183 mysql_close (& proxyscansql
);
188 * proxyscandolistopen:
189 * Lists all the open proxies found since <since> to user usernick.
192 void proxyscandolistopen ( nick
* mynick
, nick
* usernick
, time_t snce
) {
193 char mysqlquery
[ 2000 ];
198 strftime ( timestmp
, 30 , " %Y - %m - %d %H : %M : %S " , localtime (& snce
));
199 sprintf ( mysqlquery
, "SELECT IP,TS,RH FROM openproxies WHERE TS>' %s ' ORDER BY TS" , timestmp
);
201 if (( mysql_query (& proxyscansql
, mysqlquery
))!= 0 ) {
202 sendnoticetouser ( mynick
, usernick
, "Error performing database query!" );
203 Error ( "proxyscan" , ERR_ERROR
, "Error performing listopen query" );
207 myres
= mysql_use_result (& proxyscansql
);
208 if ( mysql_num_fields ( myres
)!= 3 ) {
209 sendnoticetouser ( mynick
, usernick
, "Error performing database query!" );
210 Error ( "proxyscan" , ERR_ERROR
, "Error performing listopen query" );
214 sendnoticetouser ( mynick
, usernick
, "%-20s %-22s %s " , "IP" , "Found at" , "What was open" );
215 while (( myrow
= mysql_fetch_row ( myres
))) {
216 sendnoticetouser ( mynick
, usernick
, "%-20s %-22s %s " , myrow
[ 0 ], myrow
[ 1 ], myrow
[ 2 ]);
218 sendnoticetouser ( mynick
, usernick
, "--- End of list ---" );
219 mysql_free_result ( myres
);