1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406
|
#include "gld.h"
#include "sockets.h"
TcpServer srv;
config conf;
int main(int argc,char **argv)
{
int s;
int cid;
int c;
char clean=0;
char query[QLEN];
int status;
struct group *grp;
struct passwd *user;
if(argc==2 && strcmp(argv[1],"-v")==0)
{
printf("gld %s <salim@gasmi.net> <http://www.gasmi.net>\n",VERSION);
exit(0);
}
if(argc==3 && strcmp(argv[1],"-c")==0) clean=1;
if(argc==3 && strcmp(argv[1],"-C")==0) clean=2;
if(argc==3 && strcmp(argv[1],"-k")==0) clean=3;
if(argc==3 && strcmp(argv[1],"-K")==0) clean=4;
if(argc==2 && strcmp(argv[1],"-i")==0) clean=5;
if(argc!=1 && clean==0 && strcmp(argv[1],"-d")!=0)
{
printf("Usage: gld [-c <n>|-C <n>|-k <n>|-K <n>|-h|-v]\n");
printf(" gld -c <n> : clean the database for ALL entries not updated since <n> days\n");
printf(" gld -C <n> : show what the -c option would do, without doing it\n");
printf(" gld -k <n> : clean the database for entries not updated since <n> days with only one hit \n");
printf(" gld -K <n> : show what the -k option would do, without doing it\n");
printf(" gld -i : show some database informations\n");
printf(" gld -d : enable debug mode\n");
printf(" gld -v : display version\n");
printf(" gld -h : display Usage\n");
exit(1);
}
if(ReadConfig(CONF,&conf)!=0)
{
printf("Invalid config file %s\n",CONF);
exit(2);
}
if(argc==2 && strcmp(argv[1],"-d")==0) conf.debug=1;
signal(SIGTERM,TheEnd);
signal(SIGHUP,Reload);
signal(SIGCHLD,NoZombies);
//
// Here we drop privileges and setuid/setgid if needed
//
if(conf.grp[0]!=0)
{
grp=getgrnam(conf.grp);
if(grp==(struct group *)NULL)
{
printf("Group %s not found, please check the GROUP variable in gld.conf\n",conf.grp);
exit(10);
}
if(setgid(grp->gr_gid)!=0)
{
printf("Unable to setgid to %s\n",conf.grp);
exit(11);
}
if(conf.debug==1) printf("setgid to %s OK\n",conf.grp);
}
if(conf.user[0]!=0)
{
user=getpwnam(conf.user);
if(user==(struct passwd *)NULL)
{
printf("User %s not found, please check the USER variable in gld.conf\n",conf.user);
exit(10);
}
if(setuid(user->pw_uid)!=0)
{
printf("Unable to setuid to %s\n",conf.user);
exit(12);
}
if(conf.debug==1) printf("setuid to %s OK\n",conf.user);
}
//
// Now we do what we have to do
//
if(clean!=0)
{
if(SQLConnect(conf.sqlhost,conf.sqluser,conf.sqlpasswd,conf.sqldb)<0)
{
printf("Unable to connect to MYSQL\n");
exit(1);
}
if(clean==5)
{
ShowBaseInfo();
SQLClose();
exit(0);
}
if(clean==1 || clean==2)
snprintf(query,sizeof(query)-1,"select count(last) from greylist where last < UNIX_TIMESTAMP()-86400*%d",atoi(argv[2]));
if(clean==3 || clean==4)
snprintf(query,sizeof(query)-1,"select count(last) from greylist where last < UNIX_TIMESTAMP()-86400*%d AND n=1",atoi(argv[2]));
c=SQLQuery(query);
if(clean==2 || clean==4)
{
printf("I would clean %d entries older than %d days\n",c,atoi(argv[2]));
SQLClose();
exit(0);
}
if(clean==1) snprintf(query,sizeof(query)-1,"delete from greylist where last < UNIX_TIMESTAMP()-86400*%d",atoi(argv[2]));
if(clean==3) snprintf(query,sizeof(query)-1,"delete from greylist where last < UNIX_TIMESTAMP()-86400*%d and n=1",atoi(argv[2]));
SQLQuery(query);
SQLClose();
printf("Cleaned %d entries older than %d days\n",c,atoi(argv[2]));
exit(0);
}
//
// Ok, here we start the server
//
srv=OpenTcpServer(conf.port,conf.maxcon,conf.loopback);
if(srv.sd==-1)
{
printf("Unable to bind to port %d\n",conf.port);
perror("Error was: ");
exit(1);
}
if(conf.debug==1) printf("bind to port %d succesful\n",conf.port);
if(conf.syslog==1) ErrorLog(&conf,"gld started, up and running");
if(conf.debug==0) MyDaemon(0,0);
if(conf.debug==1) printf("Waiting for incoming connexions\n");
//
// The main loop
//
while(1==1)
{
s=WaitTcpServer(srv);
if(s>=0)
{
cid=fork();
if(cid < 0 && conf.syslog==1) ErrorLog(&conf,"Fork returned error code, no child");
if(cid==0)
{
c=HandleChild(s,&conf);
if(c!=0 && conf.accept==1)
WriteSocket(s,"action=dunno\n\n",14,TOUT);
close(s);
waitpid(-1, &status, WNOHANG);
exit(0);
}
close(s);
}
}
CloseTcpServer(srv);
exit(0);
}
int HandleChild(int s,config *cnf)
{
char buff[BLEN];
char request[BLEN];
char sender[BLEN];
char recipient[BLEN];
char ip[BLEN];
char *plus;
char *at;
int n;
long ts;
int pid;
pid=getpid();
if(SQLConnect(cnf->sqlhost,cnf->sqluser,cnf->sqlpasswd,cnf->sqldb)<0)
{
if(cnf->debug==1) printf("%d: Unable to connect to MYSQL\n",pid);
if(cnf->syslog==1)
{
snprintf(buff,sizeof(buff)-1,"Unable to connect to MYSQL\n");
ErrorLog(cnf,buff);
}
return(-1);
}
GetPeerIp(s,ip,buff);
//
// We check if this IP is authorized to connect to us
//
if(CheckIP(cnf,ip)!=1)
{
if(cnf->debug==1) printf("%d: Rejected New incoming connexion from %s (%s)\n",pid,buff,ip);
if(cnf->syslog==1)
{
snprintf(buff,sizeof(buff)-1,"Rejected New incoming connexion from %s (%s)\n",buff,ip);
ErrorLog(cnf,buff);
}
SQLClose();
return(0);
}
//
// Ok, The IP is accepted
//
if(cnf->debug==1) printf("%d: New incoming connexion from %s (%s)\n",pid,buff,ip);
ts=time(0);
request[0]=sender[0]=recipient[0]=ip[0]=0;
CloseTcpServer(srv);
while(1==1)
{
//
// This functions does not read more than BLEN-1 bytes
// from the network and thus no buffer overflow is possible
//
if(ReadLSocket(s,buff,BLEN-1,TOUT)<0)
{
if(cnf->syslog==1) ErrorLog(cnf,"Read Network error");
if(cnf->debug==1) printf("%d: Read Network error\n",pid);
SQLClose();
return(-1);
}
//
// To be sure that our buffer is null terminated to avoid
// a buffer overflow, we manually set a null to the end of the buffer.
//
buff[BLEN-1]=0;
//
// Now, we are sure our buffer string length is no more than BLEN
// as all parameters are defined also as buffers with a BLEN size
// no buffer overflow is possible using strcpy .
//
if(strcmp(buff,"")==0) break;
if(strncmp(buff,"request=",8)==0)
strcpy(request,buff+8);
if(strncmp(buff,"sender=",7)==0)
strcpy(sender,buff+7);
if(strncmp(buff,"recipient=",10)==0)
strcpy(recipient,buff+10);
if(strncmp(buff,"client_address=",15)==0)
strcpy(ip,buff+15);
}
//
// To be sure that our parameters are null terminated to avoid
// a buffer overflow, we manually set a null to the end of the parameters.
//
ip[BLEN-1]=0;
recipient[BLEN-1]=0;
sender[BLEN-1]=0;
//
// Then we remove nasty chars to avoid a possible SQL injection
//
Quote(ip);
Quote(recipient);
Quote(sender);
//
// Now, we can safely use, str** functions
//
if(sender[0]==0) strcpy(sender,"void@void");
if((plus=strchr(sender, '+')) && (at=strchr(sender, '@')) && at-plus > 0)
while (*plus++ = *at++);
if(strcmp(request,REQ)!=0 || recipient[0]==0 || ip[0]==0)
{
snprintf(buff,sizeof(buff)-1,"Received invalid data req=(%s) sender=(%s) recipient=(%s) ip=(%s)",request,sender,recipient,ip);
if(cnf->syslog==1) ErrorLog(cnf,buff);
if(cnf->debug==1) printf("%d: %s\n",pid,buff);
SQLClose();
return(-2);
}
if(cnf->debug==1)
printf("%d: Got the following valid data req=(%s) sender=(%s) recipient=(%s) ip=(%s)\n",pid,request,sender,recipient,ip);
n=GreyList(ip,sender,recipient,cnf);
if(cnf->debug==1) printf("%d: End of the greylist algo\n",pid);
if(n<0)
{
if(cnf->syslog==1) ErrorLog(cnf,"MySQL error");
if(cnf->debug==1) printf("%d: MySQL error\n",pid);
SQLClose();
return(-3);
}
if(n==0)
{
if(cnf->syslog==1) Log(cnf,recipient,sender,ip,MSGGREYLIST);
if(cnf->debug==1) printf("%d: Decision is to greylist\n",pid);
if(cnf->training==0)
{
if(atoi(cnf->message)<400 || atoi(cnf->message)>499)
snprintf(buff,sizeof(buff)-1,"action=defer_if_permit %s\n\n",cnf->message);
else snprintf(buff,sizeof(buff)-1,"action=%s\n\n",cnf->message);
}
else
{
if(cnf->debug==1) printf("%d: Training mode, sending dunno\n",pid);
strcpy(buff,"action=dunno\n\n");
}
WriteSocket(s,buff,strlen(buff),TOUT);
}
else
{
WriteSocket(s,"action=dunno\n\n",14,TOUT);
if(cnf->debug==1) printf("%d: Decision is to not greylist\n",pid);
}
SQLClose();
return(0);
}
void TheEnd(int s)
{
int status;
while(wait(&status) > 0);
shutdown(srv.sd,2);
CloseTcpServer(srv);
exit(0);
}
void Reload(int s)
{
ReadConfig(CONF,&conf);
}
int MyDaemon(int nochdir, int noclose)
{
int fd;
switch (fork()) {
case -1:
return(-1);
case 0:
break;
default:
_exit(0);
}
if(setsid() == -1) return(-1);
if(!nochdir) (void)chdir("/");
if(!noclose && (fd = open("/dev/null", O_RDWR, 0)) != -1)
{
(void)dup2(fd, STDIN_FILENO);
(void)dup2(fd, STDOUT_FILENO);
(void)dup2(fd, STDERR_FILENO);
if(fd>2) (void)close(fd);
}
return(0);
}
|