/*Spiros Ioannou 1998-1999*/ /*Original perl idea by Vassilis Papadimos*/ /*C Version by Spiros Ioannou*/ #include #include #include #include #include /*bzero*/ #include #include #include #include #include #include #include #include #include #include #include #define PASSFILE "/var/www/mp3/passwd" #define IPZDIR "/var/www/mp3/ipz/" #define DBFILE "/var/www/mp3/mp3.db" #define WHATSNEW "/var/www/mp3/mp3.whatsnew" #define STATISTIK "/var/www/mp3/statistik.out" #ifdef linux #define _XOPEN_SOURCE #endif #define IDLE 1800 #include "mp3info.h" int getline(char *, int , FILE *) ; void unescape_url(char *) ; void plustospace(char *) ; void spacetoplus(char *) ; void cutlast(char*); void tobasename(char*); void escape_url(char*); char *crypt(const char *key, const char *salt); int my_strcmp(const char *s1, const char *s2); void qsort2(register int ,register int,char *[],off_t *,MPEG_HEAD *); void qsort1(l,r,a) register int l,r; char *a[]; { register int i,j; char *w; char x[256]; i=l; j=r; strncpy(x,a[(l+r)/2],255); while(i<=j) { while(strcmp(a[i],x)<0) i++; while(strcmp(a[j],x)>0) j--; if(i<=j) { w=a[i]; a[i]=a[j]; a[j]=w; i++; j--; } } if(l0) j--; if(i<=j) { w=a[i]; a[i]=a[j]; a[j]=w; s=siz[i]; siz[i]=siz[j]; siz[j]=s; if (inf) { mh=inf[i]; inf[i]=inf[j]; inf[j]=mh; } i++; j--; } } if(l\n

Go away lamer from...%s\n",remote_addr); printf(" this page isn't for yer eiz

\n"); printf(""); exit(0); } if (!strcmp((char *)crypt(passwd,pass/*"pw"*/),pass)) { /*it's him*/ authorized=1; sprintf(tmp,"%s%s",IPZDIR,remote_addr); i=open(tmp,O_WRONLY|O_CREAT,00644); if (i<0) { sprintf(loginfo,"User %s denied listening from %s [%s] (%s)", username,remote_addr,remote_host,http_x_forwarded_for); syslog(LOG_LOCAL2|LOG_INFO,"%s",loginfo); printf("Content-type: text/html\n\n"); printf("\n\n"); printf(""); printf(""); printf("\n

Ούστ Κακόγουστε

\n"); printf(""); exit(0); } utime(tmp,0x0); } else { /*Wrong password*/ authorized=0; sprintf(loginfo,"User %s failed authentication from %s [%s] (%s)", username,remote_addr,remote_host,http_x_forwarded_for); syslog(LOG_LOCAL2|LOG_INFO,"%s",loginfo); printf("Content-type: text/html\n\n"); printf("\n

No way dude...

\n"); printf(""); exit(0); } if (strlen(acl)) { /*check acl*/ regcomp(prbuf,acl,REG_EXTENDED|REG_ICASE); r=regexec(prbuf,remote_addr,0,0,0); if (r) { authorized=0; printf("Content-type: text/html\n\n"); printf("\n

Your Ip (%s) is Banned

\n", remote_addr); /* printf("Your acl:%s
\n",acl);*/ printf(""); sprintf(loginfo,"User %s failed acl from %s [%s] (%s)", username,remote_addr,remote_host,http_x_forwarded_for); syslog(LOG_LOCAL2|LOG_INFO,"%s",loginfo); exit(0); } } /*check acl*/ sprintf(tmp,"User %s authorized from %s [%s] (%s)", username,remote_addr,remote_host,http_x_forwarded_for); syslog(LOG_LOCAL2|LOG_INFO,"%s",tmp); } closelog(); /*Check if hosts exists && if it's expired*/ sprintf(tmp,"%s/%s",IPZDIR,remote_addr); r=stat(tmp,&st_s); if (r!=0 || tim-st_s.st_mtime>IDLE) { /*ip does not exist or is too old*/ expired=1; } else expired=0; if (strlen(logout)) { struct utimbuf tb; tb.actime=tim-IDLE-10; tb.modtime=tim-IDLE-10; sprintf(tmp,"%s%s",IPZDIR,remote_addr); utime(tmp,&tb); expired=1; } if (expired) { /*host not authorized*/ if (strstr(my_url,"mp3file=")) { strcpy(tmp,""); strcpy(path,"/"); } else if (strstr(my_url,"?path=")){ strcpy(tmp,""); } else strcpy(tmp,"?path="); printf("Content-type: text/html\n\n"); printf("\n\nThe Kill-9 MP3z Page\n\n\n"); printf("\n"); printf("


\n"); printf("

\n",http_host,script_name,path); printf("USERNAME
\n"); printf("

\n"); printf("PASSWORD
\n"); printf("

\n"); printf("\n"); printf("\n",tmp,path); printf("

\n\n"); exit(0); } else if (strlen(searchitem)) { FILE *ptr; char buf[1024]; char *restab[10240]; int rsize; int totalfiles=0; float totalsize=0,totalfree=0; FILE *fp; rsize=0; printf("Content-type: text/html\n\n"); printf("\n\nMP3 Search Results\n"); printf("\n\n\n",IDLE/2); printf("\n"); /*call egrep here*/ unescape_url(searchitem); plustospace(searchitem); if (!strlen (searchitem) || strstr(searchitem,";") || searchitem[0]=='\'') { strcpy(searchitem,"FUGELAMERA"); } sprintf(tmp,"/bin/egrep -i '%s' %s",searchitem,DBFILE); if ((ptr = popen(tmp, "r")) != NULL) while (fgets(buf,1024, ptr) != NULL){ buf[strlen(buf)-1]=0; restab[rsize]=(char*)malloc(strlen(buf)+2); strcpy(restab[rsize],buf); rsize++; } printf("

%d Matches " "Found for \"%s\"

\n", rsize,searchitem); printf("
\n");
  for (i=0;i%s ",tmp4,tmp3);
    printf("");
    printf("%s\n",tmp,(restab[i]+strlen(tmp3)));
  }

  printf("
\n"); printf("\n\n" "" "
\n" ""); printf("regular expression search:"); printf("\n" "\n" "" "
\n" "
\n"); if ((fp=fopen(STATISTIK,"r"))==NULL) { printf("Content-Type: "); printf("text/plain\n\n"); printf("%s open:%s",STATISTIK,strerror(errno)); exit(-1); } else { fgets(tmp,256,fp); sscanf(tmp,"%d",&totalfiles); fgets(tmp,256,fp); sscanf(tmp,"%f",&totalsize); fgets(tmp,256,fp); sscanf(tmp,"%f",&totalfree); fclose(fp); } printf ("
\n"); printf("\n",totalfiles); printf("\n",totalsize); printf("\n",totalfree); printf("
Total Songs: %d
Used Megabytes: %.1f
Free Megabytes: %.1f
\n"); printf("

\n"); printf("[Go Back!]\n"); printf("\n\n"); exit(0); } else if (strlen (whatsnew)) { FILE *fp; char buf[1024]; int totalfiles=0; float totalsize=0,totalfree=0; printf("Content-type: text/html\n\n"); printf("\n\nMP3 Search Results\n"); printf("\n\n\n",IDLE/2); printf("\n"); if ((fp=fopen(WHATSNEW,"r"))==NULL) { printf("Content-Type: "); printf("text/plain\n\n"); printf("%s open:%s",WHATSNEW,strerror(errno)); exit(-1); } printf("

What's New on Kill-9 " "Site

The 250 newest files

\n
\n");
  while (fgets(buf,1024, fp) != NULL){
    buf[strlen(buf)-1]=0; /*chop*/
    strcpy(tmp2,&buf[7]);
    strcpy(tmp3,&buf[7]);
    cutlast(tmp3);
    escape_url(tmp2); /*Song*/
    escape_url(tmp3); /*Directory*/
    
    sprintf(tmp4,"http://%s%s?path=%s",http_host,script_name,tmp3);

    if (!strcmp(encoding,"streaming")) {
      sprintf(tmp,
      "http://%s%s?path=%s&mp3file=1",http_host,script_name,&tmp2[0]); 
    }
    else  { /*not streaming*/
      sprintf(tmp,
      "http://%s%s?path=%s&mp3data=1",http_host,script_name,&tmp2[0]); 
    }
    unescape_url(tmp3);
    strcpy(tmp2,buf);
    tmp2[6]=0;
    printf("%s %s ",
	tmp2,tmp4,tmp3);
    printf("");
    printf("%s\n",tmp,&buf[7+strlen(tmp3)]);

  }
  fclose(fp);

  if ((fp=fopen(STATISTIK,"r"))==NULL) {
    printf("Content-Type: ");
    printf("text/plain\n\n");
    printf("%s open:%s",STATISTIK,strerror(errno));
    exit(-1);
  }
  else {
    fgets(tmp,256,fp);
    sscanf(tmp,"%d",&totalfiles);
    fgets(tmp,256,fp);
    sscanf(tmp,"%f",&totalsize);
    fgets(tmp,256,fp);
    sscanf(tmp,"%f",&totalfree);
    fclose(fp);
  }

  printf ("
\n"); printf("" "\n",totalfiles); printf("\n",totalsize); printf("\n",totalfree); printf("
Total Songs: %d
Used Megabytes: %.1f
Free Megabytes: %.1f
\n"); printf("

\n"); printf("\n\n"); exit(0); } else if (strlen(mp3data) && isanmp3) { /* give us the actual data*/ if (path[strlen(path)-1]=='/') path[strlen(path)-1]=0; if (!strcmp(encoding,"download")) { strcpy(tmp,path); tobasename(tmp); printf("Content-disposition: attachment; filename=%s\n",tmp); printf("Content-type: application/octet-stream\n\n"); } else printf("Content-type: audio/mpeg\n\n"); sprintf(tmp,"/kill-9/sorted/%s",path); fd=open(tmp,O_RDONLY); if (fd<0) { printf("open %s: %s\n",tmp,strerror(errno)); exit(-4); } sprintf(tmp,"%s/%s",IPZDIR,remote_addr); utime(tmp,0x0); while((r=read(fd,buffer,8196))>0 && write(1,buffer,r)>0); /*Refresh time file*/ exit(0); } else if (strlen(mp3file) && isanmp3) { /*we want a reference to the file*/ if (!strcmp(encoding,"streaming")) { printf("Content-Type: "); printf("audio/mpeg-url\n\n"); } else { printf("Content-Type: "); printf("audio/mpeg\n\n"); } printf("http://%s%s&mp3data=1\n",http_host,req_uri); exit(0); } else if (strlen(mp3file) && !isanmp3) { /*handle different file types*/ if (path[strlen(path)-1]=='/') path[strlen(path)-1]=0; sprintf(tmp,"/kill-9/sorted/%s",path); fd=open(tmp,O_RDONLY); if (fd<0) { printf("Content-Type: text/plain\n\n"); printf("open %s: %s\n",tmp,strerror(errno)); exit(-4); } printf("Content-Type: "); if (strstr(path,".jpg")) printf("image/jpeg\n\n"); else if (strstr(path,".gif")) printf("image/gif\n\n"); else if (strstr(path,".html")) printf("text/html\n\n"); else printf("text/plain\n\n"); while((r=read(fd,buffer,8196))>0 && (write(1,buffer,r))>0); exit(0); } else if (strlen(playlist)) { /*we want a reference to all files in dir*/ struct dirent * dp; struct stat stp; DIR *dirp; char dir[256]; char *files[2048]; int ffsize,i; off_t fsize[2048]; strcpy(dir,"/kill-9/sorted/"); strcat(dir,path); dirp = opendir(dir); if (dirp==0) { printf("Content-Type: text/plain\n\n"); printf("opendir %s: %s\n",dir,strerror(errno)); exit(-2); } ffsize=0; while ((dp = readdir(dirp))!=NULL) { strcpy(dir,"/kill-9/sorted/"); strcat(dir,path); strcat(dir,dp->d_name); stat(dir,&stp); if (!S_ISDIR(stp.st_mode) ){ /*is a file*/ files[ffsize]=(char*) realloc((char*)files[ffsize],strlen(dp->d_name)+2); strcpy(files[ffsize],dp->d_name); fsize[ffsize]=stp.st_size; ffsize++; } } closedir(dirp); /*Sort files */ if (ffsize-1>0) qsort2(0,ffsize-1,files,fsize,0); escape_url(path); printf("Content-Type: audio/mpeg-url\n\n"); for (i=0;id_name); if (strstr(dir,".mp3")) ismpg=1; else ismpg=0; lstat(dir,&stp); finfo[ffsize].fmode=stp.st_mode; if (S_ISLNK(finfo[ffsize].fmode)) { stat(dir,&stp); if (S_ISDIR(stp.st_mode)) finfo[ffsize].fmode=stp.st_mode; } if (!S_ISDIR(stp.st_mode) ){ files[ffsize]=(char*)realloc((char*)files[ffsize],strlen(dp->d_name)+2); strcpy(files[ffsize],dp->d_name); fsize[ffsize]=stp.st_size; if (ismpg) { if (!S_ISREG(stp.st_mode)) { finfo[ffsize].valid=0; fd=-1; } else fd=open(dir,O_RDONLY); if (fd<0) { finfo[ffsize].valid=0; } else if (S_ISREG(stp.st_mode)) { get_mp3header(&finfo[ffsize],stp.st_size,fd); close(fd); } } else finfo[ffsize].valid=0; ffsize++; } else { dirs[ddsize]=(char*) realloc((char*)dirs[ddsize],strlen(dp->d_name)+2); strcpy(dirs[ddsize],dp->d_name); ddsize++; } } closedir(dirp); /*Sort dirs */ if (ddsize-1>2) qsort1(2,ddsize-1,dirs); /*Sort files */ if (ffsize-1>0) qsort2(0,ffsize-1,files,fsize,finfo); printf("Content-type: text/html\n\n"); printf("\n\nMP3 %s\n",path); printf("\n\n\n",IDLE/2); printf("\n"); /* printf("background=\"http://www.softlab.ece.ntua.gr/~sivann/images/cel5d.gif\">\n"); */ printf("\n"); printf("\n\n"); printf( "\n"); printf("\n"); /*Directories*/ for (i = 0;i < ddsize;i++) { if (!strcmp(dirs[i],".")) continue; if (!strcmp(dirs[i],"..")) { printf("\n" "\n\n"); continue; } printf("\n\n\n",dirs[i]); printf("\n"); }//Directories /*Files*/ for (i = 0; i < ffsize; i++) { if (S_ISLNK(finfo[i].fmode) && !S_ISDIR(finfo[i].fmode)) { strcpy(tmp2,"/kill-9/sorted/"); strcat(tmp2,path); strcat(tmp2,files[i]); r=readlink(tmp2,tmp3,256); tmp3[r]=0; /* printf("READLINK[%s]:#%s#(%d)
\n",tmp2,tmp3,r); */ cutlast(tmp3); sprintf(tmp,"http://%s%s?path=%s",http_host,script_name,&tmp3[14]); printf("",&tmp3[14]); printf("\n"); } else { /*Not streaming, valid*/ printf("&mp3data=1\">%s\n"); } } else { /*sizes*/ if (!strcmp(encoding,"streaming")) { printf("\"> %s \n"); } else { /*Not streaming not valid*/ printf("&mp3data=1\">%s\n"); } } totfsize+=fsize[i]; }//Files printf("
" /*"Song"*/ "SONG" "" /*"Length / Info"*/ "LENGTH / " "INFO" "



\"[DIR]\".." /*"Go Up"*/ "GO " "UP" "
\"[DIR]\"" "%s"); printf("DIRECTORY"); printf("
",tmp); printf("\"%s\"" " %s ", files[i]); if (!finfo[i].vbr) printf("[%02d:%02d] %03dKbps", finfo[i].mins,finfo[i].secs,finfo[i].bitrate); else printf("(%ld) VBR", fsize[i]); printf("
",files[i]); if (!finfo[i].vbr) printf("[%02d:%02d] %03dKbps", finfo[i].mins,finfo[i].secs,finfo[i].bitrate); else printf("(%ld) VBR", fsize[i]); printf("
%ld Bytes",files[i],fsize[i]); printf("
%ld Bytes",files[i], fsize[i]); printf("
\n"); printf("


\n"); printf("\n
"); printf("Total: %d file(s) (%.2f MB)",ffsize,(float)(totfsize/1000000.0)); printf("
\n"); printf("
\n"); printf("\n\n" "" "
\n" ""); printf("Regular Expression search:"); printf("\n" "\n" "" "
\n" "
\n"); /* printf("

user:%s
pass:%s
gecos:%s
maker:%s
acl:%s

\n", user,pass,gecos,maker,acl); */ /*Footer*/ printf("

"); printf("",http_host); printf("[Upload!] \n"); if (!strcmp(encoding,"streaming")) { printf("[What's new!]\n"); printf("
\n"); printf("[Streaming] "); strcpy(tmp,path); escape_url(tmp); printf("[Not Streaming] \n"); printf("[Save mode] \n",tmp); printf("",tmp); printf(" [Play all of them]\n"); } else if (!strcmp(encoding,"nostreaming")) { /*Not streaming*/ printf("[What's new!]\n"); printf("\n"); strcpy(tmp,path); escape_url(tmp); printf("
[Streaming] [Not Streaming]\n"); printf("[Save mode] \n",tmp); } else { /*Not streaming*/ printf("[What's new!]\n"); printf("\n"); strcpy(tmp,path); escape_url(tmp); printf("
[Streaming] \n"); printf("[Not Streaming] \n"); printf(" [Save mode]\n"); } printf("
"); printf("[Home] \n",http_host,script_name); printf("[Logout]\n", http_host,script_name); printf("
"); printf("\n"); if (strlen(http_x_forwarded_for)) { printf("Bgale ton proxy lamera"); } /* printf("

\n"); printf ("
Username:#%s#, Pass:#%s#
\n",username,passwd); printf ("Path:#%s#
\n",path); printf ("Searchitem:#%s#
\n",searchitem); printf ("params:%d
\n",params); printf ("method:%s
\n",method); printf ("tmp:%s
\n",tmp); printf ("my_url:%s
\n",my_url); printf ("req_uri:%s
\n",req_uri); printf ("script_name:%s
\n",script_name); printf ("http_host:%s
\n",http_host); printf ("remote_addr:%s
\n",remote_addr); printf ("http_x_forwarded_for:%s
\n",http_x_forwarded_for); printf ("logout:%s
\n",logout); printf("

\n"); */ printf("\n"); printf("\n"); /*Refresh time file*/ sprintf(tmp,"%s/%s",IPZDIR,remote_addr); utime(tmp,0x0); } //authorized return errno; }//main