00001
00002
00003
00004
00005
00006 #include "action_alert_email.h"
00007 #include <stdio.h>
00008 #include "../engine/message.h"
00009 #include <sys/socket.h>
00010 #include <netinet/in.h>
00011 #include <arpa/inet.h>
00012 #include <stdlib.h>
00013 #include <string.h>
00014 #include <unistd.h>
00015 #include <netdb.h>
00016
00017
00018
00019 extern GlobalVars Globals;
00020
00021 typedef struct alert_email_data{
00022 char Host[MAX_EMAIL_ARG_LEN];
00023 char From[MAX_EMAIL_ARG_LEN];
00024 char To[MAX_EMAIL_ARG_LEN];
00025 char Subject[MAX_EMAIL_ARG_LEN];
00026 char* Message;
00027 } EMailData;
00028
00029
00030 pthread_mutex_t EMailMutex=PTHREAD_MUTEX_INITIALIZER;
00031
00032
00033
00034
00035 void* EMailMessageReal(void* data){
00036 EMailData* Data;
00037 struct hostent* he;
00038 struct sockaddr_in host_addr;
00039 int sockfd;
00040 char Buff[MAX_EMAIL_ARG_LEN+128];
00041
00042 #ifdef DEBUGPATH
00043 printf("In EMailMessageReal\n");
00044 #endif
00045
00046 if (!data) return NULL;
00047 Data=(EMailData*)data;
00048
00049 if ( (he=gethostbyname(Data->Host)) ==NULL){
00050 printf("Couldn't resolve \"%s\"\n",Data->Host);
00051 goto FreeMe;
00052 }
00053
00054 if ( (sockfd=socket(AF_INET, SOCK_STREAM,0)) ==-1){
00055 printf("Couldn't create socket\n");
00056 goto FreeMe;
00057 }
00058
00059 bzero(&host_addr, sizeof(struct sockaddr_in));
00060 host_addr.sin_family=AF_INET;
00061 host_addr.sin_port=htons(25);
00062 host_addr.sin_addr=*((struct in_addr*)he->h_addr);
00063
00064 if (connect(sockfd, (struct sockaddr*)&host_addr, sizeof(struct sockaddr))==-1){
00065 printf("Counldn't connect to %s port 25\n", Data->Host);
00066 goto FreeMe;
00067 }
00068
00069 snprintf(Buff, MAX_EMAIL_ARG_LEN+128, "Mail From: %s\n", Data->From);
00070 if (!send(sockfd, Buff, strlen(Buff), 0)==-1){
00071 printf("Failed to send Mail From\n");
00072 goto FreeMe;
00073 }
00074 sleep(1);
00075 snprintf(Buff, MAX_EMAIL_ARG_LEN+128, "Rcpt To: %s\n", Data->To);
00076 if (!send(sockfd, Buff, strlen(Buff), 0)==-1){
00077 printf("Failed to send Rcpt To\n");
00078 goto FreeMe;
00079 }
00080 sleep(1);
00081 snprintf(Buff, MAX_EMAIL_ARG_LEN+128, "data\n");
00082 if (!send(sockfd, Buff, strlen(Buff), 0)==-1){
00083 printf("Failed to send data\n");
00084 goto FreeMe;
00085 }
00086 sleep(1);
00087 snprintf(Buff, MAX_EMAIL_ARG_LEN+128, "Subject: %s\n\n", Data->Subject);
00088 if (!send(sockfd, Buff, strlen(Buff), 0)==-1){
00089 printf("Failed to send Subject\n");
00090 goto FreeMe;
00091 }
00092 sleep(1);
00093 snprintf(Buff, MAX_EMAIL_ARG_LEN+128, "%s\n", Data->Message);
00094 if (!send(sockfd, Buff, strlen(Buff), 0)==-1){
00095 printf("Failed to send Message\n");
00096 goto FreeMe;
00097 }
00098 sleep(1);
00099 snprintf(Buff, MAX_EMAIL_ARG_LEN+128, "\n.\n");
00100 if (!send(sockfd, Buff, strlen(Buff), 0)==-1){
00101 printf("Failed to send Terminator\n");
00102 goto FreeMe;
00103 }
00104 sleep(1);
00105 snprintf(Buff, MAX_EMAIL_ARG_LEN+128, "quit\n");
00106 if (!send(sockfd, Buff, strlen(Buff), 0)==-1){
00107 printf("Failed to send quit\n");
00108 goto FreeMe;
00109 }
00110 sleep(1);
00111
00112
00113 close(sockfd);
00114
00115 FreeMe:
00116 if (Data->Message) free(Data->Message);
00117 Data->Message=NULL;
00118
00119 return (void*)TRUE;
00120 }
00121
00122
00123
00124
00125
00126
00127 void EMailMessage(EMailData* data, char* Message){
00128 pthread_t email_thread;
00129
00130 #ifdef DEBUGPATH
00131 printf("In EMailMessage\n");
00132 #endif
00133
00134
00135 data->Message=malloc(1024);
00136 snprintf(data->Message, 1024, "%s", Message);
00137
00138 if (!Globals.UseThreads){
00139 if (!fork()){
00140 EMailMessageReal(data);
00141 exit(0);
00142 }
00143 }else{
00144 pthread_create(&email_thread, NULL, EMailMessageReal, data);
00145 pthread_detach(email_thread);
00146 }
00147
00148 }
00149
00150
00151
00152
00153
00154
00155 void* AlertEMailParseArgs(char* Args){
00156 EMailData* data;
00157 char* c1;
00158 char* c2;
00159
00160 #ifdef DEBUGPATH
00161 printf("In AlertEMailParseArgs\n");
00162 #endif
00163
00164 #ifdef DEBUG
00165 printf("Parsing args for action_alert_email\n");
00166 #endif
00167
00168 data=(EMailData*)calloc(sizeof(EMailData),1);
00169
00170 c1=Args;
00171 while ( (*c1) && (*c1==' ') ) c1++;
00172 if (!*c1){
00173 printf("Expected (Host, From, To, Subject)\n");
00174 free(data);
00175 return NULL;
00176 }
00177
00178 c2=strchr(c1, ',');
00179 if (!c2){
00180 printf("Expected \",\"\n");
00181 free(data);
00182 return NULL;
00183 }
00184 *c2=0x00;
00185
00186 snprintf(data->Host, MAX_EMAIL_ARG_LEN, "%s", c1);
00187 #ifdef DEBUG
00188 printf("Setting Host to \"%s\"\n",data->Host);
00189 #endif
00190
00191 c1=c2+1;
00192 while ( (*c1) && (*c1==' ') ) c1++;
00193 if (!*c1){
00194 printf("Expected (Host, From, To, Subject)\n");
00195 free(data);
00196 return NULL;
00197 }
00198
00199 c2=strchr(c1, ',');
00200 if (!c2){
00201 printf("Expected \",\"\n");
00202 free(data);
00203 return NULL;
00204 }
00205 *c2=0x00;
00206
00207 snprintf(data->From, MAX_EMAIL_ARG_LEN, "%s", c1);
00208 #ifdef DEBUG
00209 printf("Setting From to \"%s\"\n",data->From);
00210 #endif
00211
00212 c1=c2+1;
00213 while ( (*c1) && (*c1==' ') ) c1++;
00214 if (!*c1){
00215 printf("Expected (Host, From, To, Subject)\n");
00216 free(data);
00217 return NULL;
00218 }
00219
00220 c2=strchr(c1, ',');
00221 if (!c2){
00222 printf("Expected \",\"\n");
00223 free(data);
00224 return NULL;
00225 }
00226 *c2=0x00;
00227
00228 snprintf(data->To, MAX_EMAIL_ARG_LEN, "%s", c1);
00229 #ifdef DEBUG
00230 printf("Setting To to \"%s\"\n",data->To);
00231 #endif
00232
00233 c1=c2+1;
00234 while ( (*c1) && (*c1==' ') ) c1++;
00235 if (!*c1){
00236 printf("Expected (Host, From, To, Subject)\n");
00237 free(data);
00238 return NULL;
00239 }
00240
00241 snprintf(data->Subject, MAX_EMAIL_ARG_LEN, "%s", c1);
00242 #ifdef DEBUG
00243 printf("Setting Subject to \"%s\"\n",data->Subject);
00244 #endif
00245
00246 return data;
00247 }
00248
00249
00250
00251
00252 int AlertEMailMessage(char* Message, void* Data){
00253 #ifdef DEBUGPATH
00254 printf("In AlertEMailMessage\n");
00255 #endif
00256
00257 #ifdef DEBUG
00258 printf("Emailing %s\n",Message);
00259 #endif
00260
00261 EMailMessage(Data, Message);
00262
00263 return TRUE;
00264 }
00265
00266
00267
00268
00269 int AlertEMailAction(int RuleNum, int PacketSlot, void* Data){
00270 char Buff[1024];
00271 PacketRec* p;
00272 EMailData* data;
00273
00274 #ifdef DEBUGPATH
00275 printf("In AlsertEMailAction\n");
00276 #endif
00277
00278 #ifdef DEBUG
00279 printf("Writing to email\n");
00280 #endif
00281
00282 if (!Data) return FALSE;
00283 data=(EMailData*)Data;
00284
00285 p=&Globals.Packets[PacketSlot];
00286
00287 if (!ApplyMessage(Globals.Rules[RuleNum].MessageFormat, PacketSlot, Buff, 1024)){
00288 printf("Couldn't apply message to packet\n");
00289 return FALSE;
00290 }
00291
00292 #ifdef DEBUG
00293 printf("Sending email with message %s\n",Buff);
00294 #endif
00295
00296 EMailMessage(data, Buff);
00297
00298 return TRUE;
00299 }
00300
00301
00302
00303
00304 int InitActionAlertEMail(){
00305 int ActionID;
00306
00307 #ifdef DEBUGPATH
00308 printf("In InitActionAlertEMail\n");
00309 #endif
00310
00311 ActionID=CreateAction("email");
00312 if (ActionID==ACTION_NONE){
00313 #ifdef DEBUG
00314 printf("Couldn't allocation action alert email\n");
00315 #endif
00316 return FALSE;
00317 }
00318
00319 Globals.ActionItems[ActionID].ActionFunc=AlertEMailAction;
00320 Globals.ActionItems[ActionID].MessageFunc=AlertEMailMessage;
00321 Globals.ActionItems[ActionID].ParseArgs=AlertEMailParseArgs;
00322
00323 return TRUE;
00324 }