This is a auto logger for Amuser-net BBS which is used in the many Japanese underground sites
80653f6e1487e011985dfd86c164d0cf36943b4d7308752dc4124f262cb28c83
/*--------------------------------------------------------------------
amnuser-net ·Ç¼¨ÈÄLogger Ver2.1 Programmed by ¤¦¤Ë¤å¤ó
[¤Ï¤¸¤á¤Ë]
¤³¤Î¥×¥í¥°¥é¥à¤Ï¡¢amuser-net¤Î·Ç¼¨ÈĤò¼«Æ°¥í¥®¥ó¥°¤¹¤ë¤â¤Î
¤Ç¤¹¡£UNIXÍѤǤ¹¡£
[»ÈÍÑÊýË¡]
(1) cc amloger.c -o amloger
¤Ç¥³¥ó¥Ñ¥¤¥ë¤·¤Æ¤¯¤À¤µ¤¤(gcc¤Ç¤âOK).
(2) amloger [SJIS Conversion] [-t,h] [URL] &
¤Ç¾ïÃ󤵤»¤Þ¤¹¡£
[SJIS Conversion]
UNIX¤Ë¤è¤Ã¤Æ¤³¤È¤Ê¤ê¤Þ¤¹¡£
0 = sjistoeuc ¥³¥Þ¥ó¥É¤ò¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤ëUNIX
(Îã : IRIX )
1 = sjtoeuc ¥³¥Þ¥ó¥É¤ò¥µ¥Ý¡¼¥È¤·¤Æ¤¤¤ëUNIX
(Îã : SunOS4, Solaris2.x)
sjistoeuc¤ª¤è¤Ósjtoeuc¤ò¼Â¹Ô¤µ¤»¤Æ¤ß¤ë¤³¤È¤Ç¡¢¤É¤Á¤é¤ò¥µ¥Ý¡¼¥È
¤·¤Æ¤¤¤ë¤«³Îǧ¤Ç¤¤Þ¤¹¡£
[-t,h]
-t¤«-h¤Î¤É¤Á¤é¤«¤Î¥í¥®¥ó¥°¥¿¥¤¥×¤ò»ØÄꤷ¤Þ¤¹¡£
-t : EUC-¥Æ¥¥¹¥È·Á¼°¤ÇÀ°·Á¤·¤Æ¥í¥®¥ó¥°¤·¤Þ¤¹¡£
-h : EUC-HTML·Á¼°¤Ç¥í¥®¥ó¥°¤·¤Þ¤¹¡£
[URL]
·Ç¼¨ÈĤÎURL¤òµ½Ò¤·¤Æ¤¯¤À¤µ¤¤¡£
(Îã) ShadowPenguin¤ÎPenguinBBS¤òSolaris2.5¤ÇHTML·Á¼°¤Çlogging¤¹¤ë¾ì¹ç
amloger 1 -h hyper2.amuser-net.ne.jp/~auto/b28/usr/551/brd1/bbs.cgi
¤¢¤È¤Ï¡¢1»þ´Ö¤ª¤¤Ë¼«Æ°¥í¥®¥ó¥°¤·¡¢¥Õ¥¡¥¤¥ë̾"log"(-h»ØÄê»þ¤Ïlog.html)¤Ë
¼«Æ°Åª¤ËÄɲ䵤ì¤Þ¤¹¡£
-h»ØÄê»þ(HTML·Á¼°)¤Ç¥í¥®¥ó¥°¤·¤¿¾ì¹ç¤Ï¡¢"top.html"¤Ë¥«¥é¡¼¥¿¥°¤Ê¤É¤¬´Þ¤Þ
¤ì¤Þ¤¹¤Î¤Ç¡¢
cat top.html log.html > aaa.html
¤È¤·¡¢aaa.html¤ò³«¤¯¤³¤È¤Ë¤è¤ê¥Ö¥é¥¦¥¶¤Ç¸«¤ë¤³¤È¤¬¤Ç¤¤Þ¤¹¡£
[Ãí°Õ»ö¹à]
(1) ¥²¥¹¥È¥Ö¥Ã¥¯·¿¤Î¤ß¥í¥®¥ó¥°²Äǽ¤Ç¤¹¡£¥¿¥¤¥È¥ë°ìÍ÷¥â¡¼¥É¤Ï¥µ¥Ý¡¼¥È
¤·¤Æ¤¤¤Þ¤»¤ó¡£
(2) 1¹Ô¥ì¥¹¤Ë¤ÏÂбþ¤·¤Æ¤¤¤Þ¤»¤ó¡£
(3) ¥í¥°¤ÏEUC¥³¡¼¥É¤ÇÊݸ¤µ¤ì¤Þ¤¹¤Î¤Ç¡¢WindowsÅù¤ÇÆɤà¾ì¹ç¤Ï¡¢
´Á»ú¥³¡¼¥É¤òÊÑ´¹¤·¤Æ¤¯¤À¤µ¤¤¡£
(¡ö¤³¤Î¥×¥í¥°¥é¥à¤ÏžºÜOK¤Ç¤¹)
[Æ°ºî³Îǧ]
Solaris2.6, SunOS4, IRIX6.3
--------------------------------------------------------------------
*/
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define LOGFILE "log" /* ¥í¥°¥Õ¥¡¥¤¥ë̾ */
#define INTERVAL_TIME 3600 /* ¹¹¿·»þ´Ö(ÉÃ) */
#define SEPARATER '-' /* ¥í¥°¡¦¥»¥Ñ¥ì¡¼¥¿Ê¸»ú */
#define SEPARATER_NUM 80 /* ¥»¥Ñ¥ì¡¼¥¿Ê¸»ú¿ô */
#define PORT_HTTP 80 /* Request Port */
#define RECVBUF_SIZE 5000 /* ¼õ¿®¥Ð¥Ã¥Õ¥¡¥µ¥¤¥º */
#define LINEBUF_SIZE 10000 /* ¥Æ¥ó¥Ý¥é¥ê¥Ð¥Ã¥Õ¥¡¥µ¥¤¥º */
#define GETFILE ".amlog" /* ¥Æ¥ó¥Ý¥é¥ê¥í¥°¥Õ¥¡¥¤¥ë̾ */
#define FILE_HEADER "top.html" /* Header */
int SJISTOEUC=0;
int HTML_FLAG=0;
int main(argc,argv)
int argc;
char *argv[];
{
int n;
char logf[100],buf[100],host[100],request[100];
char *p;
int save_log();
int separation();
int diff_file();
void addnewlog();
int copyfile();
if (argc<4){
printf("usage : %s [switch] [-t,h] [url]\n",argv[0]);
printf("[SJIS Conversion]\n");
printf(" 0 : sjistoeuc (IRIX)\n");
printf(" 1 : sjtoeuc (SunOS4, Solaris2.x)\n");
printf("[Log Type]\n");
printf(" -t : EUC Text\n");
printf(" -h : EUC HTML\n");
exit(1);
}
SJISTOEUC=atoi(argv[1]);
if (strcmp(argv[2],"-t")==0 || strcmp(argv[2],"-T")==0)
HTML_FLAG=0;
else if (strcmp(argv[2],"-h")==0 || strcmp(argv[2],"-H")==0)
HTML_FLAG=1;
strcpy(host,argv[3]);
if ((p=strchr(host,'/'))!=NULL) *p=0;
else{
printf("Invalid URL.\n");
exit(1);
}
strcpy(request,strchr(argv[3],'/'));
if (strlen(host)==0 || strlen(request)==0){
printf("Invalid URL.\n");
exit(1);
}
if (HTML_FLAG==1) sprintf(logf,"%s.html",LOGFILE);
else strcpy(logf,LOGFILE);
for (;;){
printf("Checking...\n");
sprintf(buf,"tmp0");
copyfile(buf,"tmp0.old");
save_log(host,request,GETFILE);
separation(GETFILE,"tmp");
if (diff_file("tmp0","tmp0.old")==0
&& existfile(logf)==1) {
printf("¿·¤·¤¤½ñ¤¹þ¤ß¤Ï¤¢¤ê¤Þ¤»¤ó\n");
}else
addnewlog(logf,"tmp");
remove("amlog");
sleep(INTERVAL_TIME);
}
}
/*=======================================================
¥Õ¥¡¥¤¥ë¤Î¸ºß¥Á¥§¥Ã¥¯
[Input]
0 = ¤Ê¤·
1 = ¤¢¤ê
=======================================================
*/
int existfile(file)
char *file;
{
FILE *fp;
if ((fp=fopen(file,"rb"))==NULL) return(0);
fclose(fp);
return(1);
}
/*=======================================================
¿·¤¿¤Ê¥«¥¥³¤Îʬ³ä¥Õ¥¡¥¤¥ëÈÖ¹æ¤Î½ªÃ¼¤òµá¤á¡¢
¥í¥°¤ËÄɲ乤ë
[Input]
logfile : ¥í¥°¥Õ¥¡¥¤¥ë
sepf : ʬ³ä¥Õ¥¡¥¤¥ëindex
=======================================================
*/
void addnewlog(logfile,sepf)
char *logfile,*sepf;
{
FILE *fps,*fpl,*fpt,*fpcol;
static char buf[LINEBUF_SIZE];
int i,j,r,maxi,n;
/* ¥í¥°¤ËÄɲ乤٤ʬ³ä¥Õ¥¡¥¤¥ë¤Î½ªÃ¼¤òÁªÂò */
if (existfile(logfile)==1){
for (i=0,maxi=0;;i++){
sprintf(buf,"%s%d",sepf,i);
if ((r=diff_file(buf,logfile))==1)
maxi=i;
else break;
}
if (r!=-1) printf("%d·ï¤Î½ñ¤¹þ¤ß¤¬¤¢¤ê¤Þ¤¹.\n",maxi+1);
}else{
printf("¿·µ¬Logging³«»Ï\n");
maxi=-1;
}
/* ¿·¥í¥°¥Õ¥¡¥¤¥ëtemp¤ò½àÈ÷ */
if ((fpt=fopen(".temp","wb"))==NULL){
printf("Can not write temp file.\n");
return;
}
/* ʬ³ä¥Õ¥¡¥¤¥ë¤òtemp¥Õ¥¡¥¤¥ë¤Ë½ñ¤¹þ¤à */
for (i=0;;i++){
if (maxi!=-1 && i==maxi+1) break;
sprintf(buf,"%s%d",sepf,i);
if ((fps=fopen(buf,"rb"))==NULL) break;
for (;;){
if (feof(fps)) break;
if ((n=fread(buf,1,LINEBUF_SIZE,fps))==0) break;
fwrite(buf,1,n,fpt);
}
fclose(fps);
if (HTML_FLAG==1)
fprintf(fpt,"<hr>\n");
else{
for (j=0;j<SEPARATER_NUM;j++) fprintf(fpt,"%c",SEPARATER);
fprintf(fpt,"\n");
}
}
if (r==-1){
char br[10];
if (HTML_FLAG==1) strcpy(br,"<br>");
else strcpy(br,"");
fprintf(fpt,"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%s\n",br);
fprintf(fpt,"¤µ¤¤Û¤ÉºÇ¿·¤Î½ñ¤¹þ¤ß¤¬ºï½ü¤µ¤ì¤¿¤¿¤á¡¢½ñ¤¹þ¤ß¿ô¤Î%s\n",br);
fprintf(fpt,"»»½Ð¤¬¤Ç¤¤Þ¤»¤ó¡£1¥Ú¡¼¥¸Á´¤Æ¤ò¥í¥®¥ó¥°¤·¤Þ¤·¤¿ %s\n",br);
fprintf(fpt,"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx%s\n",br);
if (HTML_FLAG==1)
fprintf(fpt,"<hr>\n");
else{
for (j=0;j<SEPARATER_NUM;j++) fprintf(fpt,"%c",SEPARATER);
fprintf(fpt,"\n");
}
}
/* ¥í¥°¤òtemp¥Õ¥¡¥¤¥ë¤Ë½ñ¤¹þ¤à */
if ((fpl=fopen(logfile,"rb"))!=NULL){
for (;;){
if (feof(fpl)) break;
if ((n=fread(buf,1,LINEBUF_SIZE,fpl))==0) break;
fwrite(buf,1,n,fpt);
}
fclose(fpl);
}
fclose(fpt);
copyfile(".temp",logfile);
remove(".temp");
}
/*=======================================================
¥Õ¥¡¥¤¥ë¥³¥Ô¡¼
[Input]
f1 : ¥³¥Ô¡¼¸µ
f2 : ¥³¥Ô¡¼Àè
[Output]
-1 = Error
0 = OK
=======================================================
*/
int copyfile(f1,f2)
char *f1,*f2;
{
FILE *fpl,*fpt;
int c;
if ((fpt=fopen(f1,"rb"))==NULL){
return (-1);
}
if ((fpl=fopen(f2,"wb"))==NULL){
fclose(fpt);
return (-2);
}
while((c=getc(fpt))!=EOF) putc(c,fpl);
fclose(fpl);
fclose(fpt);
return(0);
}
/*=======================================================
¥Õ¥¡¥¤¥ëÈæ³Ó
[Input]
f1 : file1
f2 : file2
[Output]
-1 = Error
0 = file1¤Èfile2¤ÎÆâÍƤ¬Æ±¤¸
1 = file1¤Èfile2¤ÎÆâÍƤϰۤʤë
2 = file1¤Èfile2¤Î¥µ¥¤¥º¤Ï°Û¤Ê¤ë¤¬¡¢file1¤ÎEOF¤Þ¤Ç
¤Ïfile2¤ÈƱ¤¸
=======================================================
*/
int diff_file(f1,f2)
char *f1,*f2;
{
FILE *fp1,*fp2;
int c;
if ((fp1=fopen(f1,"rb"))==NULL
|| (fp2=fopen(f2,"rb"))==NULL) return (-1);
while((c=getc(fp1))!=EOF)
if (c!=getc(fp2)) return(1);
getc(fp2);
if (!feof(fp2)) return(2);
return(0);
}
/*=======================================================
¥Õ¥¡¥¤¥ë¤«¤é£±¹Ô¤Å¤Äbuf¤ËÆɤà
[Input]
fp : Âоݥե¡¥¤¥ë¥Ý¥¤¥ó¥¿
buf : ¹Ô¥Ð¥Ã¥Õ¥¡
maxbuf : buf¤ÎºÇÂç¥Ð¥¤¥È¿ô
[Output]
buf¤Îʸ»ú¿ô
=======================================================
*/
int fgetline(fp,buf,maxbuf)
FILE *fp;
char *buf;
int maxbuf;
{
int i;
strcpy(buf,"");
for (i=0;i<maxbuf-1;i++){
if (feof(fp)) break;
buf[i]=fgetc(fp);
if (buf[i]==(char)255) break;
if (buf[i]==10) break;
}
buf[i]=0;
return (i);
}
/*=======================================================
¥«¥¥³¤òʬ³ä¤·¤Æ¥Õ¥¡¥¤¥ë¤ËÊݸ
[Input]
logf : EUC¤Î·Ç¼¨ÈÄ¥¤¥á¡¼¥¸
sepf : ʬ³ä¥Õ¥¡¥¤¥ëindex
[Output]
-1 = Error
0 = Àµ¾ï½ªÎ»
=======================================================
*/
int separation(logf,sepf)
char *logf,*sepf;
{
FILE *fp,*fpo,*fpcol;
static char buf[LINEBUF_SIZE];
int flag=0,msgflag=0,name_count=-1;
int i,kn;
char *p,fn[100];
static int first_colortag;
first_colortag=0;
if ((fp=fopen(logf,"r"))==NULL){
printf("Can not open logfile.\n");
return(-1);
}
sprintf(buf,"%s0",sepf);
if ((fpo=fopen(buf,"w"))==NULL){
printf("File write error.\n");
return(-1);
}
for(kn=0;;){
if (feof(fp)) break;
fgetline(fp,buf,LINEBUF_SIZE);
/* bgcolor, text, link,Åù¤Î¥«¥é¡¼¤òÆÀ¤ë */
/* (HTML¥â¡¼¥ÉÀìÍÑ) */
if (HTML_FLAG==1
&& strstr(buf,"<body bgcolor=")!=NULL
&& strstr(buf,"text=")!=NULL
&& strstr(buf,"link=")!=NULL
&& strstr(buf,"vlink=")!=NULL
&& strstr(buf,"alink=")!=NULL){
if (first_colortag==0){
if ((fpcol=fopen(FILE_HEADER,"w"))!=NULL){
fprintf(fpcol,"<html>\n");
fprintf(fpcol,"%s\n",buf);
fclose(fpcol);
}
first_colortag=1;
}
}
/* 1¥«¥¥³¤Î½ªÃ¼¤ò¥»¥Ã¥È */
if (strcmp(buf,"</td></table>")==0){
if (HTML_FLAG==1)
fprintf(fpo,"%s\n",buf);
flag=msgflag=0;
name_count=-1;
fclose(fpo);
kn++;
sprintf(fn,"%s%d",sepf,kn);
if ((fpo=fopen(fn,"w"))==NULL){
printf("File write error.\n");
return(-1);
}
continue;
}
/* 1¥«¥¥³¤Î³«»Ï¤ò¥»¥Ã¥È */
if (strcmp(buf,"<table width=\"99%\">")==0){
if (HTML_FLAG==1)
fprintf(fpo,"%s\n",buf);
flag=1;
}
if (HTML_FLAG==1 && flag==1)
fprintf(fpo,"%s\n",buf);
if (HTML_FLAG==0 && flag==1){
if (msgflag==1){
/* ¥á¥Ã¥»¡¼¥¸ */
fprintf(fpo,"\n");
for (i=0;i<strlen(buf);i++)
if (strncmp(buf+i,"<br>",4)==0){
buf[i+3]=10;
buf[i]=buf[i+1]=buf[i+2]=' ';
}
fprintf(fpo,"%s\n",buf);
}
if (name_count==1){
/* ÆüÉÕ */
buf[strlen(buf)-4]=0;
for (i=0;i<strlen(buf);i++)
if (buf[i]>='0' && buf[i]<='9') break;
p=buf+i;
fprintf(fpo,"¡ÚÆü¡¡¡¡ÉÕ¡Û%s\n",p);
name_count=-1;
}
if (name_count==0){
/* ¥¿¥¤¥È¥ë */
buf[strlen(buf)-4]=0;
fprintf(fpo,"%s\n",buf);
name_count++;
}
/* ¥á¥Ã¥»¡¼¥¸³«»Ï¹Ô¤Î¥»¥Ã¥È */
if (strncmp(buf,"<table width=\"99%\"><td bgcolor=#",32)==0
&& strstr(buf,"width=")!=NULL
&& strstr(buf,"font color=#")!=NULL
&& strstr(buf,"¡Ú")!=NULL){
if (HTML_FLAG==1)
fprintf(fpo,"%s\n",buf);
msgflag=1;
}
/* ̾Á° */
if (strncmp(buf,"<td bgcolor=#",13)==0
&& strstr(buf,"width=")!=NULL
&& (p=strstr(buf,"¡Ú"))!=NULL){
p[strlen(p)-4]=0;
fprintf(fpo,"%s\n",p);
name_count=0;
}
/* MAIL */
if (strncmp(buf,"<img src=\"../../mail.gif",24)==0
&& (p=strstr(buf,"mailto:"))!=NULL){
p+=7;
for (i=0;i<strlen(p);i++)
if (p[i]=='\"') break;
p[i]=0;
if (strlen(p)!=0)
fprintf(fpo,"¡Ú E-Mail ¡Û%s\n",p);
}
/* URL */
if (strncmp(buf,"<img src=\"../../home.gif",24)==0
&& (p=strstr(buf,"<a href=\""))!=NULL){
p+=9;
for (i=0;i<strlen(p);i++)
if (p[i]=='\"') break;
p[i]=0;
if (strlen(p)!=0)
fprintf(fpo,"¡Ú URL ¡Û%s\n",p);
}
}
}
fclose(fp);
fclose(fpo);
sprintf(fn,"%s%d",sepf,kn);
remove(fn);
return(0);
}
/*=======================================================
»ØÄꤷ¤¿IP¤È¥ê¥¯¥¨¥¹¥È¤Ë¤è¤ëHTTP¥¤¥á¡¼¥¸¤ò
EUC¤ËÊÑ´¹¤·¤Æ¥Õ¥¡¥¤¥ë¤ËÊݸ
[Input]
ip : ÂоÝIP
request : HTTP¥ê¥¯¥¨¥¹¥È
logf : Êݸ¥Õ¥¡¥¤¥ë̾
[Output]
-1 = Error
0 = Àµ¾ï½ªÎ»
=======================================================
*/
int save_log(ip,request,logf)
char *ip,*request,*logf;
{
FILE *fp;
char buf[100];
void dump_http();
static char *sj[]={"sjistoeuc","sjtoeuc"};
remove(logf);
if ((fp=fopen(".temp","w"))==NULL){
printf("Can not write temp file.\n");
return(-1);
}
dump_http(fp,ip,request);
fclose(fp);
sprintf(buf,"%s .temp > %s",sj[SJISTOEUC],logf);
system(buf);
remove(".temp");
return(0);
}
/*=======================================================
»ØÄꤷ¤¿IP¤È¥ê¥¯¥¨¥¹¥È¤Ë¤è¤ëHTTP¥¤¥á¡¼¥¸¤ò
»ØÄê¥Õ¥¡¥¤¥ë¥Ý¥¤¥ó¥¿¤ËÂФ·¤ÆÊݸ
[Input]
fp : ÊݸÂоݥե¡¥¤¥ë¥Ý¥¤¥ó¥¿
ip : ÂоÝIP
request : HTTP¥ê¥¯¥¨¥¹¥È
[Output]
-1 = Error
0 = Àµ¾ï½ªÎ»
=======================================================
*/
void dump_http(fp,ip,command)
FILE *fp;
char *ip,*command;
{
int make_connection();
int s,i,r;
char buf[1000];
static char recvbuf[RECVBUF_SIZE];
if ((s=make_connection(PORT_HTTP,ip))<0) return;
sprintf(buf,"GET %s\n",command);
send(s,buf,strlen(buf),0);
for (;;){
memset(recvbuf,0,RECVBUF_SIZE);
if ((r=recv(s,recvbuf,RECVBUF_SIZE-1,0))<=0) break;
recvbuf[RECVBUF_SIZE-1]=0;
fprintf(fp,"%s",recvbuf);
}
fprintf(fp,"\n\n");
close(s);
}
/*=======================================================
TCP¥³¥Í¥¯¥·¥ç¥ó¤òÄ¥¤ë
[Input]
port : Âоݥݡ¼¥È
ipaddr : ÂоÝIP
[Output]
-1 = ¥½¥±¥Ã¥ÈºîÀ®¼ºÇÔ
-2 = BIND¼ºÇÔ
-3 = CONNECT¼ºÇÔ
-4 = UnknowHost
¤½¤Î¾ = socket
=======================================================
*/
int make_connection(port,ipaddr)
int port;
char *ipaddr;
{
struct sockaddr_in addr, server;
struct hostent *host;
void timeoutfunc();
int sock;
sock = socket(AF_INET, SOCK_STREAM, 0);
if (sock < 0){
printf("Socket creation error");
return(-1);
}
memset((char *) &server, 0, sizeof(server));
server.sin_family = AF_INET;
server.sin_port = 0;
server.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock, (struct sockaddr *) &server, sizeof(server)) < 0) {
close(sock);
printf("Bind error ");
return (-2);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(port);
if ((addr.sin_addr.s_addr=inet_addr(ipaddr))==-1){
if ((host=gethostbyname(ipaddr))==NULL){
printf( "Unknown host.\n");
return(-4);
}
addr.sin_family = host->h_addrtype;
memcpy( ( caddr_t )&addr.sin_addr, host->h_addr, host->h_length );
}
if (connect(sock,(struct sockaddr *)&addr,sizeof(addr))!=0){
close(sock);
return (-3);
}
return(sock);
}