| 1 |
/*---------------------------------------------------------------------------------------------- |
| 2 |
Copyright (c) 2004, Noboru Iwata |
| 3 |
All rights reserved. |
| 4 |
|
| 5 |
Redistribution and use in source and binary forms, with or without modification, |
| 6 |
are permitted provided that the following conditions are met: |
| 7 |
|
| 8 |
* Redistributions of source code must retain the above copyright notice, |
| 9 |
this list of conditions and the following disclaimer. |
| 10 |
* Redistributions in binary form must reproduce the above copyright notice, |
| 11 |
this list of conditions and the following disclaimer in the documentation |
| 12 |
and/or other materials provided with the distribution. |
| 13 |
* Neither the name of the <ORGANIZATION> nor the names of its contributors |
| 14 |
may be used to endorse or promote products derived from this software without |
| 15 |
specific prior written permission. |
| 16 |
|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 18 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, |
| 19 |
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 20 |
IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, |
| 21 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, |
| 22 |
BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 23 |
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 24 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 25 |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, |
| 26 |
EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 |
|
| 28 |
program : pmxfilter |
| 29 |
|
| 30 |
---------------------------------------------------------------------------------------------*/ |
| 31 |
#include <stdio.h> |
| 32 |
#include <stdlib.h> |
| 33 |
#include <memory.h> |
| 34 |
#include <time.h> |
| 35 |
#include <syslog.h> |
| 36 |
#include <unistd.h> |
| 37 |
#include "pmxfilter.h" |
| 38 |
|
| 39 |
#define DEBUG |
| 40 |
|
| 41 |
#define BUF_SIZE 256 |
| 42 |
|
| 43 |
char SMTP_HOST[BUF_SIZE]; |
| 44 |
char MAIL_ADDR[BUF_SIZE]; |
| 45 |
char MAIL_FROM[BUF_SIZE]; |
| 46 |
char MAIL_SUBJ[BUF_SIZE]; |
| 47 |
char SEND_FLAG[BUF_SIZE]; |
| 48 |
|
| 49 |
BOOL IpSend(void); |
| 50 |
void DateGet(void); |
| 51 |
void IniRead(char *fname); |
| 52 |
void IniDefaultSet(void); |
| 53 |
BOOL StdOutPut(void); |
| 54 |
BOOL StdOutPut2(void); |
| 55 |
|
| 56 |
char LineBuf[1024]; |
| 57 |
char TimeBuf[1024]; |
| 58 |
char ReadBuf[1024]; |
| 59 |
|
| 60 |
int main(int argc,char *argv[]) |
| 61 |
{ |
| 62 |
memset(SMTP_HOST,0,BUF_SIZE); |
| 63 |
memset(MAIL_ADDR,0,BUF_SIZE); |
| 64 |
memset(MAIL_FROM,0,BUF_SIZE); |
| 65 |
memset(MAIL_SUBJ,0,BUF_SIZE); |
| 66 |
memset(SEND_FLAG,0,BUF_SIZE); |
| 67 |
|
| 68 |
IniDefaultSet(); // DEFAULT |
| 69 |
if (argc == 2){ |
| 70 |
IniRead(argv[1]); |
| 71 |
} |
| 72 |
if (strlen(SMTP_HOST) > 0 && strlen(MAIL_ADDR) > 0 && strlen(MAIL_FROM) > 0 && strlen(MAIL_SUBJ)){ |
| 73 |
switch(SEND_FLAG[0]){ |
| 74 |
case '0': // SMTP SEND |
| 75 |
IpSend(); |
| 76 |
break; |
| 77 |
case '1': // STDOUT OUTPUT |
| 78 |
StdOutPut(); |
| 79 |
break; |
| 80 |
case '2': // STDOUT OUTPUT |
| 81 |
StdOutPut2(); |
| 82 |
break; |
| 83 |
} |
| 84 |
} |
| 85 |
else{ |
| 86 |
syslog(LOG_NOTICE,"Ini File Read Fail\n"); |
| 87 |
} |
| 88 |
return(0); |
| 89 |
} |
| 90 |
|
| 91 |
int IpSend(void) |
| 92 |
{ |
| 93 |
BOOL rc; |
| 94 |
int rcvLen; |
| 95 |
int scnt; |
| 96 |
int l; |
| 97 |
int i; |
| 98 |
|
| 99 |
#ifdef DEBUG |
| 100 |
syslog(LOG_DEBUG,"IpSend Start\n"); |
| 101 |
#endif |
| 102 |
rc = sub_SocInit(SMTP_HOST,25); |
| 103 |
if (rc == FALSE) |
| 104 |
return(rc); |
| 105 |
scnt = 0; |
| 106 |
while(1){ |
| 107 |
memset(rcvMsg,0,MSGLEN); |
| 108 |
rcvLen = sub_cRecv(60); |
| 109 |
if (rcvLen > 0){ |
| 110 |
if (memcmp(rcvMsg,"220",3) == 0){ |
| 111 |
l = sub_cSend("HELO IWATA\r\n",12); |
| 112 |
scnt = 1; |
| 113 |
} |
| 114 |
else if (memcmp(rcvMsg,"250",3) == 0){ |
| 115 |
switch(scnt){ |
| 116 |
case 1: |
| 117 |
l = sub_cSend("MAIL FROM:",10); |
| 118 |
l = sub_cSend(MAIL_FROM,strlen(MAIL_FROM)); |
| 119 |
l = sub_cSend("\r\n",2); |
| 120 |
scnt = 2; |
| 121 |
break; |
| 122 |
case 2: |
| 123 |
l = sub_cSend("RCPT TO:",8); |
| 124 |
l = sub_cSend(MAIL_ADDR,strlen(MAIL_ADDR)); |
| 125 |
l = sub_cSend("\r\n",2); |
| 126 |
scnt = 3; |
| 127 |
break; |
| 128 |
case 3: |
| 129 |
l = sub_cSend("DATA\r\n",6); |
| 130 |
scnt = 4; |
| 131 |
break; |
| 132 |
case 4: |
| 133 |
l = sub_cSend("QUIT\r\n",6); |
| 134 |
scnt = 9; |
| 135 |
break; |
| 136 |
} |
| 137 |
} |
| 138 |
else if (memcmp(rcvMsg,"354",3) == 0){ |
| 139 |
l = sub_cSend("Date: ",6); |
| 140 |
DateGet(); |
| 141 |
l = sub_cSend(TimeBuf,strlen(TimeBuf)); |
| 142 |
l = sub_cSend("\r\n",2); |
| 143 |
l = sub_cSend("From: ",6); |
| 144 |
l = sub_cSend(MAIL_FROM,strlen(MAIL_FROM)); |
| 145 |
l = sub_cSend("\r\n",2); |
| 146 |
l = sub_cSend("Subject: ",9); |
| 147 |
l = sub_cSend(MAIL_SUBJ,strlen(MAIL_SUBJ)); |
| 148 |
l = sub_cSend("\r\n",2); |
| 149 |
l = sub_cSend("To: ",4); |
| 150 |
l = sub_cSend(MAIL_ADDR,strlen(MAIL_ADDR)); |
| 151 |
l = sub_cSend("\r\n",2); |
| 152 |
l = sub_cSend("\r\n",2); |
| 153 |
while(1){ |
| 154 |
memset(LineBuf,0,sizeof(LineBuf)); |
| 155 |
if (fgets(LineBuf,sizeof(LineBuf),stdin) == NULL){ |
| 156 |
break; |
| 157 |
} |
| 158 |
for (i = 0;i < strlen(LineBuf);i++){ |
| 159 |
if (LineBuf[i] == 0x0d || LineBuf[i] == 0x0a){ |
| 160 |
LineBuf[i] = 0; |
| 161 |
break; |
| 162 |
} |
| 163 |
} |
| 164 |
if (strlen(LineBuf) < 1) |
| 165 |
break; |
| 166 |
l = sub_cSend(LineBuf,strlen(LineBuf)); |
| 167 |
l = sub_cSend("\r\n",2); |
| 168 |
} |
| 169 |
l = sub_cSend("\r\n.\r\n",5); |
| 170 |
} |
| 171 |
} |
| 172 |
if (scnt == 9) |
| 173 |
break; |
| 174 |
} |
| 175 |
sub_SocEnd(); |
| 176 |
return(TRUE); |
| 177 |
} |
| 178 |
|
| 179 |
int StdOutPut(void) |
| 180 |
{ |
| 181 |
BOOL rc; |
| 182 |
int rcvLen; |
| 183 |
int scnt; |
| 184 |
int l; |
| 185 |
int i; |
| 186 |
|
| 187 |
#ifdef DEBUG |
| 188 |
syslog(LOG_DEBUG,"StdOutPut Start\n"); |
| 189 |
#endif |
| 190 |
scnt = 0; |
| 191 |
memset(LineBuf,0,sizeof(LineBuf)); |
| 192 |
DateGet(); |
| 193 |
sprintf(LineBuf,"Date: %s\r\n",TimeBuf); |
| 194 |
fputs(LineBuf,stdout); |
| 195 |
|
| 196 |
memset(LineBuf,0,sizeof(LineBuf)); |
| 197 |
sprintf(LineBuf,"From: <%s>\r\n",MAIL_FROM); |
| 198 |
fputs(LineBuf,stdout); |
| 199 |
|
| 200 |
memset(LineBuf,0,sizeof(LineBuf)); |
| 201 |
sprintf(LineBuf,"To: %s\r\n",MAIL_ADDR); |
| 202 |
fputs(LineBuf,stdout); |
| 203 |
|
| 204 |
memset(LineBuf,0,sizeof(LineBuf)); |
| 205 |
sprintf(LineBuf,"Subject: %s\r\n",MAIL_SUBJ); |
| 206 |
fputs(LineBuf,stdout); |
| 207 |
|
| 208 |
fputs("Content-Type: text/plain; charset=\"ISO-2022-JP\"\r\n",stdout); |
| 209 |
fputs("Content-Transfer-Encoding: 7bit\r\n",stdout); |
| 210 |
fputs("\r\n",stdout); |
| 211 |
while(1){ |
| 212 |
memset(LineBuf,0,sizeof(LineBuf)); |
| 213 |
if (fgets(LineBuf,sizeof(LineBuf),stdin) == NULL){ |
| 214 |
break; |
| 215 |
} |
| 216 |
if (strlen(LineBuf) < 3) |
| 217 |
break; |
| 218 |
fputs(LineBuf,stdout); |
| 219 |
} |
| 220 |
return(TRUE); |
| 221 |
} |
| 222 |
|
| 223 |
int StdOutPut2(void) |
| 224 |
{ |
| 225 |
BOOL rc; |
| 226 |
BOOL flg; |
| 227 |
BOOL flg2; |
| 228 |
int bflg; |
| 229 |
int cflg; |
| 230 |
char *p; |
| 231 |
char *p1; |
| 232 |
char *p2; |
| 233 |
char b[128]; |
| 234 |
|
| 235 |
#ifdef DEBUG |
| 236 |
syslog(LOG_DEBUG,"StdOutPut2 Start\n"); |
| 237 |
#endif |
| 238 |
rc = FALSE; |
| 239 |
flg = TRUE; |
| 240 |
flg2 = TRUE; |
| 241 |
bflg = 0; |
| 242 |
cflg = 0; |
| 243 |
while(1){ |
| 244 |
memset(LineBuf,0,sizeof(LineBuf)); |
| 245 |
if (fgets(LineBuf,sizeof(LineBuf),stdin) == NULL){ |
| 246 |
break; |
| 247 |
} |
| 248 |
if (rc == FALSE){ |
| 249 |
switch(bflg){ |
| 250 |
case 0: |
| 251 |
p = strstr(LineBuf,"Content-Type: multipart/"); |
| 252 |
if (p != NULL){ |
| 253 |
p1 = strstr(p,"boundary"); |
| 254 |
if (p1 != NULL){ |
| 255 |
p1 = strstr(p1,"\""); |
| 256 |
p2 = strstr(p1+1,"\""); |
| 257 |
if (p1 != NULL && p2 != NULL){ |
| 258 |
memset(b,0,sizeof(b)); |
| 259 |
memcpy(b,p1+1,p2-p1-1); |
| 260 |
rc = TRUE; |
| 261 |
} |
| 262 |
} |
| 263 |
else{ |
| 264 |
bflg = 1; |
| 265 |
} |
| 266 |
} |
| 267 |
break; |
| 268 |
case 1: |
| 269 |
p = strstr(LineBuf,"boundary"); |
| 270 |
if (p != NULL){ |
| 271 |
p1 = strstr(p,"\""); |
| 272 |
p2 = strstr(p1+1,"\""); |
| 273 |
if (p1 != NULL && p2 != NULL){ |
| 274 |
memset(b,0,sizeof(b)); |
| 275 |
memcpy(b,p1+1,p2-p1-1); |
| 276 |
rc = TRUE; |
| 277 |
bflg = 0; |
| 278 |
} |
| 279 |
} |
| 280 |
break; |
| 281 |
} |
| 282 |
} |
| 283 |
else{ |
| 284 |
switch(cflg){ |
| 285 |
case 0: |
| 286 |
p = strstr(LineBuf,b); |
| 287 |
if (p != NULL){ |
| 288 |
flg = TRUE; |
| 289 |
cflg = 1; |
| 290 |
} |
| 291 |
break; |
| 292 |
case 1: |
| 293 |
p = strstr(LineBuf,"Content-Type:"); |
| 294 |
if (p != NULL){ |
| 295 |
cflg = 0; |
| 296 |
p1 = strstr(p,"text/plain"); |
| 297 |
if (p1 == NULL){ |
| 298 |
flg = FALSE; |
| 299 |
flg2 = TRUE; |
| 300 |
} |
| 301 |
else{ |
| 302 |
flg = TRUE; |
| 303 |
} |
| 304 |
} |
| 305 |
break; |
| 306 |
} |
| 307 |
} |
| 308 |
if (flg == TRUE){ |
| 309 |
fputs(LineBuf,stdout); |
| 310 |
} |
| 311 |
else{ |
| 312 |
if (flg2 == TRUE){ |
| 313 |
if (strlen(LineBuf) <= 2){ |
| 314 |
flg2 = FALSE; |
| 315 |
fputs(LineBuf,stdout); |
| 316 |
} |
| 317 |
else{ |
| 318 |
fputs(LineBuf,stdout); |
| 319 |
} |
| 320 |
} |
| 321 |
} |
| 322 |
} |
| 323 |
return(TRUE); |
| 324 |
} |
| 325 |
|
| 326 |
void DateGet(void) |
| 327 |
{ |
| 328 |
int i; |
| 329 |
int j; |
| 330 |
int k; |
| 331 |
time_t t; |
| 332 |
char *c; |
| 333 |
struct tm *tm; |
| 334 |
|
| 335 |
|
| 336 |
t = time(&t); |
| 337 |
tm = localtime(&t); |
| 338 |
c = asctime(tm); |
| 339 |
memset(TimeBuf,0,sizeof(TimeBuf)); |
| 340 |
sprintf(TimeBuf,"XXX, %2.2d XXX %4.4d %2.2d:%2.2d:%2.2d +0900", |
| 341 |
tm->tm_mday,tm->tm_year+1900,tm->tm_hour,tm->tm_min,tm->tm_sec); |
| 342 |
memcpy(TimeBuf,c,3); |
| 343 |
memcpy(&TimeBuf[8],(c+4),3); |
| 344 |
return; |
| 345 |
} |
| 346 |
|
| 347 |
void IniRead(char *fname) |
| 348 |
{ |
| 349 |
FILE *f; |
| 350 |
char *c; |
| 351 |
int i,j; |
| 352 |
|
| 353 |
f = fopen(fname,"r"); |
| 354 |
if (f != NULL){ |
| 355 |
while(1){ |
| 356 |
memset(ReadBuf,0,sizeof(ReadBuf)); |
| 357 |
c = fgets(ReadBuf,1024,f); |
| 358 |
if (c == NULL){ |
| 359 |
break; |
| 360 |
} |
| 361 |
if (ReadBuf[0] == ';' || ReadBuf[0] == '#'){ |
| 362 |
continue; |
| 363 |
} |
| 364 |
else if (memcmp(ReadBuf,"SMTP_HOST=",10) == 0){ |
| 365 |
i = 10; |
| 366 |
j = 0; |
| 367 |
while(1){ |
| 368 |
if (ReadBuf[i] <= 0x20) |
| 369 |
break; |
| 370 |
SMTP_HOST[j] = ReadBuf[i]; |
| 371 |
i++; |
| 372 |
j++; |
| 373 |
if (j == BUF_SIZE - 1) |
| 374 |
break; |
| 375 |
} |
| 376 |
SMTP_HOST[j] = 0; |
| 377 |
} |
| 378 |
else if (memcmp(ReadBuf,"MAIL_ADDR=",10) == 0){ |
| 379 |
i = 10; |
| 380 |
j = 0; |
| 381 |
while(1){ |
| 382 |
if (ReadBuf[i] <= 0x20) |
| 383 |
break; |
| 384 |
MAIL_ADDR[j] = ReadBuf[i]; |
| 385 |
i++; |
| 386 |
j++; |
| 387 |
if (j == BUF_SIZE - 1) |
| 388 |
break; |
| 389 |
} |
| 390 |
MAIL_ADDR[j] = 0; |
| 391 |
} |
| 392 |
else if (memcmp(ReadBuf,"MAIL_FROM=",10) == 0){ |
| 393 |
i = 10; |
| 394 |
j = 0; |
| 395 |
while(1){ |
| 396 |
if (ReadBuf[i] <= 0x20) |
| 397 |
break; |
| 398 |
MAIL_FROM[j] = ReadBuf[i]; |
| 399 |
i++; |
| 400 |
j++; |
| 401 |
if (j == BUF_SIZE - 1) |
| 402 |
break; |
| 403 |
} |
| 404 |
MAIL_FROM[j] = 0; |
| 405 |
} |
| 406 |
else if (memcmp(ReadBuf,"MAIL_SUBJ=",10) == 0){ |
| 407 |
i = 10; |
| 408 |
j = 0; |
| 409 |
while(1){ |
| 410 |
if (ReadBuf[i] < 0x20) |
| 411 |
break; |
| 412 |
MAIL_SUBJ[j] = ReadBuf[i]; |
| 413 |
i++; |
| 414 |
j++; |
| 415 |
if (j == BUF_SIZE - 1) |
| 416 |
break; |
| 417 |
} |
| 418 |
MAIL_SUBJ[j] = 0; |
| 419 |
} |
| 420 |
else if (memcmp(ReadBuf,"SEND_FLAG=",10) == 0){ |
| 421 |
i = 10; |
| 422 |
j = 0; |
| 423 |
while(1){ |
| 424 |
if (ReadBuf[i] < 0x20) |
| 425 |
break; |
| 426 |
SEND_FLAG[j] = ReadBuf[i]; |
| 427 |
i++; |
| 428 |
j++; |
| 429 |
if (j == BUF_SIZE - 1) |
| 430 |
break; |
| 431 |
} |
| 432 |
SEND_FLAG[j] = 0; |
| 433 |
} |
| 434 |
} |
| 435 |
fclose(f); |
| 436 |
} |
| 437 |
else{ |
| 438 |
syslog(LOG_NOTICE,"Ini File Not Found\n"); |
| 439 |
} |
| 440 |
return; |
| 441 |
} |
| 442 |
|
| 443 |
void IniDefaultSet(void) |
| 444 |
{ |
| 445 |
gethostname(SMTP_HOST,BUF_SIZE); |
| 446 |
memcpy(MAIL_ADDR,"pmxfilter",9); |
| 447 |
memcpy(MAIL_FROM,"pmxfilter",9); |
| 448 |
memcpy(MAIL_SUBJ,"title:pmx",9); |
| 449 |
SEND_FLAG[0] = '1'; |
| 450 |
return; |
| 451 |
} |
| 452 |
|