#include #include #include #include #include #include #include #include #include #include #include "libirc/irc.h" //epoch's libirc. should be included with segfault. //might want to change some of these. #define SERVER "127.0.0.1" #define PORT "6667" #define NICK "SegFault" //override with argv[0] #define MYUSER "segfault" #define LINES_SENT_LIMIT 1 #define LINELEN 400 #define RAWLOG "./files/rawlog" #define LOG "./files/log" #define MAXTAILS 400 //just to have it more than the system default. #define BS 502 #define TSIZE 65536 //size of hashtable. 65k isn't bad, right? // !c uses 56 for its tail. // 56 == 32 + 16 + 8 == 0x38 == 0x20+0x10+0x8 == SPAM | BEGIN | MSG #define TAILO_RAW (0x1) // r output gets sent directly to server #define TAILO_EVAL (0x2) // e interpret the lines read from the tail as if they were messages to segfault #define TAILO_CLOSE (0x4) // c close the file at EOF, default is to leave it open. #define TAILO_MSG (0x8) // m output gets sent as a PM to the place the tail was made. #define TAILO_BEGIN (0x10) //b start the tail at the beginning of the file instead of the end. #define TAILO_SPAM (0x20) //s Spam control is enabled for this stream. #define TAILO_ENDMSG (0x40) //n show a message when the tail reaches the end of a chunk #define TAILO_Q_EVAL (TAILO_EVAL|TAILO_CLOSE|TAILO_BEGIN) //0x2+0x4+0x10 = 2+4+16 = 22 #define TAILO_Q_COUT (TAILO_SPAM|TAILO_BEGIN|TAILO_MSG) //0x20+0x10+0x8 = 32+16+8 = 56 //this function isn't with the rest of them because... meh. char *tailmode_to_txt(int mode) { char *modes="recmbsn"; int i,j=0; char *m=malloc(strlen(modes)); for(i=0;inick: ((fmt[i]=='n')?user->name: ((fmt[i]=='h')?user->host: ((fmt[i]=='f')?from: ((fmt[i]=='p')?pid: ((fmt[i]=='m')?myuser->nick://and here. ((fmt[i]=='s')?arg:"%" ))))))); fmt[i-1]=0; notargs[c]=strdup(fmt+j); sz+=strlen(args[c]); sz+=strlen(notargs[c]); c++; j=i+1; } } } notargs[c]=strdup(fmt+j); sz+=strlen(notargs[c]); output=malloc(sz+1); output[0]=0; for(i=0;i 0);//this is used on named pipes usually. } } void eofp(FILE *fp) { if(fseek(fp,0,SEEK_END) == -1) { while(fgetc(fp) != -1);//this is used on named pipes usually. } clearerr(fp); } //this function got scary. basically handles all the tail magic. void extra_handler(int fd) { int tmpo,i; static int mmerp=0; char tmp[BS+1]; char *tmp2; if(oldtime == time(0) && lines_sent > LINES_SENT_LIMIT) {//if it is still the same second, skip this function. return; } else { lines_sent=0; } if(redirect_to_fd != -1) { fd=redirect_to_fd; } for(i=0;i_> tailf[i].lines++; mmerp=0; if(strchr(tmp,'\r')) *strchr(tmp,'\r')=0; if(strchr(tmp,'\n')) *strchr(tmp,'\n')=0; if(tailf[i].opt & TAILO_EVAL) {//eval if(tailf[i].args) { //only format magic evaled file lines if they have args. //why? tmp2=format_magic(fd,tailf[i].to,tailf[i].user,tmp,tailf[i].args); } else { if(!(tmp2=strdup(tmp))) exit(2); } message_handler(fd,tailf[i].to,tailf[i].user,tmp2,0); free(tmp2); } if(tailf[i].opt & TAILO_RAW) {//raw tmp2=malloc(strlen(tmp)+4); snprintf(tmp2,strlen(tmp)+3,"%s\r\n",tmp); mywrite(fd,tmp2); free(tmp2); } if(tailf[i].opt & TAILO_MSG) {//just msg the lines. privmsg(fd,tailf[i].to,tmp); } if(tailf[i].lines >= line_limit && (tailf[i].opt & TAILO_SPAM)) { tailf[i].lines=-1; //lock it. privmsg(fd,tailf[i].to,"--more--"); } } else { if(tailf[i].lines != 0 && (tailf[i].opt & TAILO_ENDMSG)) { privmsg(fd,tailf[i].to,"---------- TAILO_ENDMSG border ----------"); } tailf[i].lines=0; } } else { //don't PM in here. shows a LOT of shit. } } } } void file_tail(int fd,char *from,char *file,char *args,char opt,struct user *user) { int i,j; int fdd; char tmp[256]; struct stat st; for(i=0;inick=strdup(user->nick); tailf[i].user->name=strdup(user->name); tailf[i].user->host=strdup(user->host); if(!tailf[i].user) { mywrite(fd,"QUIT :malloc error 4.5!!! (a strdup again)\r\n"); return; } tailf[i].args=args?strdup(args):0; if(args) { if(!tailf[i].args) { mywrite(fd,"QUIT :malloc error 5!!! (well, strdup)\r\n"); return; } } tailf[i].lines=0; strcpy(tailf[i].file,file); } } void c_botup(int fd,char *from) { char tmp[256]; snprintf(tmp,sizeof(tmp)-1,"botup: %lu",(unsigned long int)time(0)-start_time); privmsg(fd,from,tmp); } void c_leettail(int fd,char *from,char *file,struct user *user) { short a=file[0]-'0'; short b=file[1]-'0'; short c=(a*10)+(b); char *args; if((args=strchr(file,' '))) { *args=0; args++; } file_tail(fd,from,file+2,args,c,user); } void c_changetail(int fd,char *from,char *line) { char *merp; int i; //if(line == 0) return mywrite(fd,"QUIT :line == 0 in changetail\r\n"); //if(from == 0) return mywrite(fd,"QUIT :from == 0 in changetail\r\n"); if((merp=strchr(line,' '))) { *merp=0; merp++; } for(i=0;inick,"22./scripts/startup",myuser); } void debug_time(int fd,char *from,char *msg) { char tmp[100]; if(debug) { snprintf(tmp,99,"%lu %s",(unsigned long int)time(0),msg?msg:"(no message)");//time() returns time_t which on BSD is a long. privmsg(fd,from,tmp); } } struct alias *leetgetalias(struct alias *start,char *msg) { struct alias *m; if(!msg) return NULL; if(!start) return NULL; for(m=start;m;m=m->next) { if(!strncmp(msg,m->original,strlen(m->original)) && (msg[strlen(m->original)]==' ' || msg[strlen(m->original)] == 0)) {//this allows !c to get called when using !c2 if !c2 is defined after !c. >_> return m; } } return NULL; } unsigned short hash(char *v) {//maybe use a seeded rand()? :) Thanks FreeArtMan unsigned short h=0; h=((*v)<<8)+(v?*(v+1):0); srand(h); return rand(); } struct hitem {//with a stick struct alias *ll; }; struct hitem **hashtable; int htkeys[TSIZE]; int htkl; void inittable() { int i; hashtable=malloc(sizeof(char *)*TSIZE); if(!hashtable) { fprintf(stderr,"malloc error 6 in hash table.\n"); return; } for(i=0;ill; } //this seems too complicated. int setkey_h(char *key,char *value) { unsigned short h=hash(key); struct alias *tmp; int i; for(i=0;ii+1?htkl:i+1; if(!hashtable[h]) { //empty bucket! //add this to the list of used buckets so we can easily //use that list later for stuff. if(!(hashtable[h]=malloc(sizeof(struct hitem)))) return 1; hashtable[h]->ll=0; //we now have a valid hashtable entry and a NULL ll in it. //don't bother with the new ll entry yet... } if((tmp=leetgetalias(hashtable[h]->ll,key)) != NULL) { //we found this alias in the ll. now to replace the value free(tmp->target); if(!(tmp->target=strdup(value))) return 2; return 0; } if(hashtable[h]->ll == NULL) { if(!(hashtable[h]->ll=malloc(sizeof(struct alias)))) return 3; hashtable[h]->ll->next=0; hashtable[h]->ll->prev=0; if(!(hashtable[h]->ll->original=strdup(key))) return 4; if(!(hashtable[h]->ll->target=strdup(value))) return 5; } else { //go to the end and add another entry to the ll. for(tmp=hashtable[h]->ll;tmp->next;tmp=tmp->next); if(!(tmp->next=malloc(sizeof(struct alias)))) return 6; tmp->next->prev=tmp; tmp=tmp->next; if(!(tmp->original=strdup(key))) return 7; if(!(tmp->target=strdup(value))) return 8; tmp->next=0; } return 0; } void c_aliases_h(int fd,char *from,char *line) { char tmp[512]; struct alias *m; int i,j=0,k=0; if(!line){ privmsg(fd,from,"usage: !aliases [search-term]"); return; } for(i=0;ill;m;m=m->next) { if(strcasestr(m->target,line) || strcasestr(m->original,line)) { snprintf(tmp,sizeof(tmp)-1,"%s -> %s",m->original,m->target); privmsg(fd,from,tmp); j++; } k++; } } snprintf(tmp,sizeof(tmp)-1,"found %d of %d aliases",j,k); privmsg(fd,from,tmp); } //CONVERT //this is the last one that needs to be converted to // be used with the hashtable /* void c_rmalias_h(int fd,char *from,char *line) { } void c_leetrmalias(int fd,char *from,char *line) { struct alias *m; for(m=a_start;m;m=m->next) { if(!strcmp(line,m->original)) { if(m->next == 0) { a_end=m->prev; } if(m->prev == 0) { a_start=m->next; free(m->target); free(m->original); free(m); } else { m->prev->next=m->next; } privmsg(fd,from,"alias deleted"); return; } } privmsg(fd,from,"alias not found"); return; } */ struct alias *getalias_h(char *msg) { return leetgetalias(getkey_h(msg),msg); } void c_alias_h(int fd,char *from,char *line) { char tmps[512]; char *derp=strchr(line,' '); struct alias *tmp; if(!derp) { if((tmp=getalias_h(line)) != NULL) { privmsg(fd,from,tmp->target); } else { snprintf(tmps,sizeof(tmps),"'%s' not an alias.",line); privmsg(fd,from,tmps); } return; } *derp=0; derp++; setkey_h(line,derp); } //hash table version void c_kill(int fd,char *from,char *line) { char *csig=line; char *cpid=strchr(line,' '); int sig,pid; if(cpid) { *cpid=0; cpid++; } else { privmsg(fd,from,"usage: !kill signum pid"); return; } sig=atoi(csig); pid=atoi(cpid); if(sig && pid) { if(kill(pid,sig)) privmsg(fd,from,"kill error"); else privmsg(fd,from,"signal sent"); } else { privmsg(fd,from,"pid or sig is 0. something is odd."); } } void c_pid(int fd,char *from) { char tmp[512]; snprintf(tmp,sizeof(tmp)-1,"pid: %d",getpid()); privmsg(fd,from,tmp); } void c_id(int fd,char *from) { char tmp[512]; snprintf(tmp,sizeof(tmp)-1,"u:%d g:%d eu:%d eg:%d",getuid(),getgid(),geteuid(),getegid()); privmsg(fd,from,tmp); } void c_leetuntail(int fd,char *from,char *line) { char *frm=line; char *file=0; int frmN=0; int i; char tmp[512]; if((file=strchr(line,' '))) { *file=0; file++; } if(file) { if(*frm == '*') { for(i=0;i IRCdestination"); for(i=0;i %s",tailf[i].file,tailf[i].inode,ftell(tailf[i].fp),tailf[i].lines,x,tailf[i].opt,tailf[i].to); free(x); privmsg(fd,from,tmp); free(tmp); } } if(!at_least_one) { privmsg(fd,from,"I don't have any tails. :("); } } char recording,recording_raw; void c_record(int fd,char *from,char *line) { if(*line == '0') { privmsg(fd,from,"no longer recording IRC."); recording=0; return; } if(*line == '1') { recording=1; unlink(LOG); privmsg(fd,from,"recording IRC."); return; } privmsg(fd,from,recording?"1":"0"); } void c_rawrecord(int fd,char *from,char *line) { if(*line == '0') { privmsg(fd,from,"no longer recording raw IRC."); recording_raw=0; return; } if(*line == '1') { recording_raw=1; unlink(RAWLOG); privmsg(fd,from,"recording raw IRC."); return; } privmsg(fd,from,recording_raw?"1":"0"); } void c_leetsetout(int fd,char *from,char *msg) { if(redirect_to_fd != -1) close(redirect_to_fd); redirect_to_fd=open(msg+3,((msg[0]-'0')*100) + ((msg[1]-'0')*10) + (msg[2]-'0'),022); if(redirect_to_fd == -1) { privmsg(fd,from,"failed to open file as redirect:"); privmsg(fd,from,msg+3); } } void c_linelimit(int fd,char *from,char *msg) { char tmp[256]; if(!msg) { snprintf(tmp,255,"current spam line limit: %d (debug: %d)",line_limit,debug); privmsg(fd,from,tmp); } else { if(msg[0] == 'a') { mode_magic^=1; privmsg(fd,from,"mode_magic flipped. happy easter!"); } if(msg[0]=='!') { if(msg[1]) { trigger_char=msg[1]; privmsg(fd,from,"trigger_char set. more easter!"); } } if(atoi(msg) > 0) { line_limit=atoi(msg); snprintf(tmp,255,"spam line limit set to: %d",line_limit); privmsg(fd,from,tmp); } else { privmsg(fd,from,"hidden feature! negative line limit flips debug bit."); debug^=1; } } } void c_resetout(int fd,char *from) { redirect_to_fd=-1; privmsg(fd,from,"output reset"); } /* maybe... that message hander is ugly. struct builtin { char *cmd; void (*func)(); }; struct builtin[] = { "resetout", c_resetout }; */ void message_handler(int fd,char *from,struct user *user,char *msg,int redones) { printf("message handler!\n"); struct alias *m; char *tmp2; char tmp[512]; int len; int sz; //debug_time(fd,from); if(user->nick) { if(strcmp(user->nick,myuser->nick)) { if(locked_down) return; for(sz=0;shitlist[sz];sz++) { if(!strcmp(shitlist[sz],user->nick)) { return; } } } } if(redirect_to_fd != -1) { fd=redirect_to_fd; } if(recording) { debug_time(fd,from,"writing to log..."); append_file(fd,"raw",LOG,msg,'\n'); debug_time(fd,from,"finished writing to log."); } len=strchr(msg,'*')?strchr(msg,'*')-msg:strlen(myuser->nick); if(!strncmp(msg,myuser->nick,len)) { if(msg[len] == '*') len++; if(msg[len] == ',' || msg[len] == ':') { if(msg[len+1] == ' ') { msg+=len+1; msg[0]=trigger_char; } } } if(*msg == trigger_char) *msg='!'; if(*msg != '!') { return; } //this section could probably be made a LOT shorter with //an array of structs that contain command and function pointer. //... meh. it'd just be a LITTLE bit shorter. // this still seems horrible though. all those constants in there that are just strlen()s... if(!strncmp(msg,"!leetsetout ",12)) { c_leetsetout(fd,from,msg+12); } else if(!strncmp(msg,"!whoareyou",10) && !msg[10]) { privmsg(fd,from,myuser->nick); } else if(!strncmp(msg,"!whoami",7) && !msg[7]) { privmsg(fd,from,user->nick); } else if(!strncmp(msg,"!whereami",9) && !msg[9]) { privmsg(fd,from,from); } else if(!strncmp(msg,"!resetout",9) && !msg[9]) { c_resetout(fd,from); } else if(!strncmp(msg,"!botup",6) && !msg[6]) { c_botup(fd,from); } else if(!strncmp(msg,"!linelimit",10) && (!msg[10] || msg[10] == ' ')) { c_linelimit(fd,from,*(msg+10)?msg+11:0); } else if(!strncmp(msg,"!nick ",6) && msg[6]) { free(myuser->nick); myuser->nick=strdup(msg+6); irc_nick(fd,myuser->nick); } else if(!strncmp(msg,"!tailunlock ",12)) { c_tailunlock(fd,from,msg+12); } else if(!strncmp(msg,"!changetail ",12)) { c_changetail(fd,from,msg+12); } else if(!strncmp(msg,"!tails",6) && !msg[6]) { c_tails(fd,from); } else if(!strncmp(msg,"!record ",8)) { c_record(fd,from,msg+8); } else if(!strncmp(msg,"!rawrecord ",11)) { c_rawrecord(fd,from,msg+11); } else if(!strncmp(msg,"!leettail ",10)) { c_leettail(fd,from,msg+10,user); } else if(!strncmp(msg,"!leetuntail ",12)) { c_leetuntail(fd,from,msg+12); } else if(!strncmp(msg,"!leetappend ",12)) { c_leetappend(fd,from,msg+12); } else if(!strncmp(msg,"!untail ",8)) { c_untail(fd,from,msg+8); } else if(!strncmp(msg,"!raw ",5)) { tmp2=malloc(strlen(msg)-5+4); snprintf(tmp2,strlen(msg)-5+3,"%s\r\n",msg+5); mywrite(fd,tmp2); free(tmp2); } else if(!strncmp(msg,"!say ",5)) { privmsg(fd,from,msg+5); } else if(!strncmp(msg,"!id",3) && !msg[3]) { c_id(fd,from); } else if(!strncmp(msg,"!pid",4) && !msg[4]) { c_pid(fd,from); } else if(!strncmp(msg,"!kill ",6)) { c_kill(fd,from,msg+6); } else if(!strncmp(msg,"!alias ",7)) { c_alias_h(fd,from,msg+7); } /* else if(!strncmp(msg,"!rmalias ",9)) { c_rmalias(fd,from,msg+9); //need to make a hashtable version }*/ else if(!strncmp(msg,"!aliases",8) && (!msg[8] || msg[8] == ' ')) { c_aliases_h(fd,from,*(msg+8)?msg+9:0); } else if(redones < 5) { debug_time(fd,from,"checking aliases..."); if((m=getalias_h(msg)) != NULL) { sz=(strlen(msg)-strlen(m->original)+strlen(m->target)+1); redo=format_magic(fd,from,user,m->target,*(msg+strlen(m->original)+1)=='\n'?"":(msg+strlen(m->original)+1)); message_handler(fd,from,user,redo,redones+1); free(redo); redo=0; return; } debug_time(fd,from,"finished checking aliases. not found."); redo=0; snprintf(tmp,sizeof(tmp),"unknown command: %s",msg); privmsg(fd,from,tmp); } if(redones >5) { privmsg(fd,from,"I don't know if I'll ever get out of this alias hole you're telling me to dig. Fuck this."); } } void line_handler(int fd,char *line) {//this should be built into the libary? printf("line: %s\n",line); char *s=line,*t=0,*u=0; struct user *user=malloc(sizeof(struct user)); user->nick=0; user->name=0; user->host=0; if(strchr(line,'\r')) *strchr(line,'\r')=0; if(strchr(line,'\n')) *strchr(line,'\n')=0; //:nick!name@host MERP DERP :message //:nick!name@host s t :u //:armitage.hacking.allowed.org MERP DERP :message //:nickhost s t :u //only sub-parse nicknamehost stuff if starts with : //strchr doesn't like null pointers. :/ why not just take them and return null? //check that I haven't gone past the end of the string? nah. it should take care of itself. if(recording_raw) { append_file(fd,"epoch",RAWLOG,line,'\n'); } // rewrite this shit. if(line[0]==':') { if((user->nick=strchr(line,':'))) { *(user->nick)=0; (user->nick)++; } } if((s=strchr((user->nick),' '))) { *s=0; s++; if((t=strchr(s,' '))) { *t=0; t++; if((u=strchr(t,' '))) {//: *u=0; u++; } } } if(((user->name)=strchr((user->nick),'!'))) { *(user->name)=0; (user->name)++; if(((user->host)=strchr((user->name),'@'))) { *(user->host)=0; (user->host)++; } } else { user->host=user->nick; } //all this shit. printf("<%s!%s@%s> '%s' '%s' '%s'\n", user->nick, user->name, user->host, s,t,u); if(!user->name && s) { //server message if(!strcmp(s,"433")) {//nick already used. srand(time(NULL)*getpid()); myuser->nick[strlen(myuser->nick)-3]=(rand()%10)+'0'; myuser->nick[strlen(myuser->nick)-2]=(rand()%10)+'0'; myuser->nick[strlen(myuser->nick)-1]=(rand()%10)+'0'; irc_nick(fd,myuser->nick); } if(!strcmp(s,"004")) {//we're connected. start trying the startup. startup_stuff(fd); } } if(s && t && u) { if(!strcmp(s,"PRIVMSG") && strcmp(user->nick,myuser->nick)) if(strcmp(user->nick,myuser->nick)) message_handler(fd,*t=='#'?t:user->nick,user,++u,0); else if(debug) privmsg(fd,*t=='#'?t:user->nick,"This server has an echo"); } if(s && user->nick && t) { if(!strcmp(s,"JOIN")) { irc_mode(fd,t+1,"+v",user->nick);//why t+1? it starts with :? } if(!strcmp(s,"MODE") && mode_magic) { if(u) { if(*u == '-') {//auto-give modes back that are removed in front of segfault. *u='+'; irc_mode(fd,t,u,"");//u contains the nick the mode is being removed from. } } } //:Ishikawa-!~epoch@localhost NICK :checking if(!strcmp(s,"NICK")) { if(!strcmp(user->nick,myuser->nick)) { free(myuser->nick); myuser->nick=strdup(t+1); } } } free(user); } int main(int argc,char *argv[]) { int fd; struct passwd *pwd; char *s,*p; int c; mode_magic=0; trigger_char='!'; redirect_to_fd=-1; debug=0; locked_down=(argc>2); lines_sent=0; line_limit=25; recording=0; recording_raw=0; start_time=time(0); htkl=0; redo=0; inittable(); myuser=malloc(sizeof(struct user)); myuser->nick=strdup(argc>1?argv[1]:NICK); myuser->name="I_dunno"; myuser->host="I_dunno"; snprintf(pid,6,"%d",getpid()); printf("starting segfault...\n"); if(!getuid() || !geteuid()) { s=getenv("seguser"); pwd=getpwnam(s?s:MYUSER); if(!pwd) { printf("I'm running with euid or uid of 0 and I can't find myself."); return 0; } setgroups(0,0); setgid(pwd->pw_gid); setuid(pwd->pw_uid); } else { pwd=getpwuid(getuid()); printf("going to run segfault as user %s\n",pwd->pw_name); if(!pwd) { printf("well, shit. I don't know who I am."); return 0; } } for(c=0;cnick, "segfault segfault segfault :segfault"); printf("cd %s\n",pwd->pw_dir); chdir(getenv("seghome")?getenv("seghome"):pwd->pw_dir); return runit(fd,line_handler,extra_handler); }