Subversion Repositories wimsdev

Rev

Rev 12213 | Rev 12218 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. /*  Copyright (C) 1998-2003 XIAO, Gang of Universite de Nice - Sophia Antipolis
  2.  *
  3.  *  This program is free software; you can redistribute it and/or modify
  4.  *  it under the terms of the GNU General Public License as published by
  5.  *  the Free Software Foundation; either version 2 of the License, or
  6.  *  (at your option) any later version.
  7.  *
  8.  *  This program is distributed in the hope that it will be useful,
  9.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  10.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  11.  *  GNU General Public License for more details.
  12.  *
  13.  *  You should have received a copy of the GNU General Public License
  14.  *  along with this program; if not, write to the Free Software
  15.  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  16.  */
  17. /* Routines to write log files. */
  18. #include <sys/socket.h>
  19. #include <sys/un.h>
  20.  
  21. #include "wims.h"
  22.  
  23. char logbuf[4*(MAX_LINELEN+1)];
  24. char *logp=logbuf;
  25.  
  26. void write_logfile(char *fname, char *str)
  27. {
  28.   char *p;
  29.   if((p=strchr(str,'\n'))!=NULL) *p=0;
  30.   snprintf(logp,sizeof(logbuf)-(logp-logbuf),"%s %s\n",fname,str);
  31.   logp+=strlen(logp);
  32. }
  33.  
  34. void flushlog(void)
  35. {
  36.   int n, fd;
  37.   n=logp-logbuf; logp=logbuf;
  38.   if(n<=0) return;
  39.   fd=open(temp_log,O_WRONLY|O_APPEND|O_CREAT,S_IRUSR|S_IWUSR);
  40.   if(fd==-1) return;
  41.   (void)write(fd,logbuf,n); close(fd);
  42. }
  43.  
  44. /* Write module log file. */
  45. void module_log(void)
  46. {
  47.   /* log string is limited to 100 characters. */
  48.   char *logstr, *ip, *sess, lbuf[100], *p;
  49.  
  50.   if(robot_access || !modlog || strstr(session_prefix,"_check")!=NULL) return;
  51.   logstr=getvar("wims_module_log");
  52.   if(logstr==NULL || *logstr==0) return;
  53.   ip=remote_addr;
  54.   if(mode==mode_default) sess=getvar("wims_session");
  55.   else sess="popup";
  56.   if(ip==NULL || sess==NULL) return;
  57.   mystrncpy(lbuf,logstr,sizeof(lbuf));
  58.   p=strchr(sess,'_'); if(p==NULL) p=sess+strlen(sess);
  59.   if(p<sess+4) p=sess; else p=p-4;
  60.   snprintf(tmplbuf,sizeof(tmplbuf),"%s %.6s %s\11%s",
  61.    nowstr, p, ip, lbuf);
  62.   p=getvar(ro_name[ro_module]);
  63.   if(p==NULL || *p==0) return;
  64.   write_logfile(mkfname(NULL,"%s/%s",module_dir,p),tmplbuf);
  65. }
  66.  
  67. /* log http referers */
  68. void referer_log(void)
  69. {
  70.   char *c,*s,*ip,*r,*h,refstr[256];
  71.  
  72.   if(robot_access) return;
  73.   c=getvar(ro_name[ro_cmd]);
  74.   if(c==NULL) c="";
  75.   if(mode==mode_default) s=getvar("wims_session"); else s="popup";
  76.   if(s==NULL) s="----";
  77.   else if(!new_session && strcmp(c,"intro")!=0) return;
  78.   if(strlen(s)<4) s="----";
  79.   if(strchr(s,'_')!=NULL) return;
  80.   ip=remote_addr; if(*ip==0) return;
  81.   r=getenv("HTTP_REFERER");
  82.   if(r==NULL || *r==0) r="??";
  83.   /* skip some useless referers */
  84.   else {
  85.     if(strstr(r,cgi_name)!=NULL) return;
  86.     if(strstr(r,"file:")!=NULL || strchr(r,'.')==NULL) r="??";
  87.     if(strstr(r,"http")==NULL && strchr(r,'.')==NULL &&
  88.      strstr(r,"bookmark")!=NULL) r="??bookmark";
  89.   }
  90.   if(strncmp(r,"http://",strlen("http://"))==0) r+=strlen("http://");
  91.   /* Take references from the same site or not? No. */
  92.   h=getenv("HTTP_HOST");
  93.   if(h!=NULL && *h!=0 && strncmp(r,h,strlen(h))==0) return;
  94.     /* stop before '#' */
  95.   mystrncpy(refstr,r,sizeof(refstr));
  96.   r=strchr(refstr,'#'); if(r!=NULL) *r=0;
  97.   for(r=refstr;r<refstr+strlen(refstr);r++) {
  98.     if(*r=='%' && *(r+1)=='7' && *(r+2)=='E') {
  99.     *r='~'; ovlstrcpy(r+1,r+3);
  100.     }
  101.   }
  102.   snprintf(tmplbuf,sizeof(tmplbuf),"%s %s %s\11%s",
  103.      nowstr,s+strlen(s)-4,ip,refstr);
  104.   write_logfile("referer.log",tmplbuf);
  105. }
  106.  
  107. /* Log new creation of sessions. For server counting use. */
  108. void session_log(char *c)
  109. {
  110.   int i;
  111.   char *ip, *p, *agent, *s, *sess;
  112.  
  113.   ip=remote_addr; if(*ip==0) return;
  114.   if(mode==mode_default) {
  115.     sess=getvar("wims_session");
  116.     if(sess==NULL) return;
  117.     if(strchr(sess,'_')!=NULL) return;
  118.   }
  119.   else sess="popup";
  120.   p=getenv("REMOTE_HOST"); if(p==NULL) p="";
  121.   i=strlen(p); if(i>40) p+=i-40;
  122.   agent=getenv("HTTP_USER_AGENT"); if(agent==NULL) agent="";
  123.   s=strchr(sess,'_'); if(s==NULL) s=sess+strlen(sess);
  124.   if(s<sess+4) s=sess; else s=s-4;
  125.     /* limit agent name to 80 chars */
  126.   snprintf(tmplbuf,sizeof(tmplbuf),"%s %s %s\11%s\11%.80s",
  127.      nowstr, s, ip, p, agent);
  128.   write_logfile("session.log",tmplbuf);
  129. }
  130.  
  131. /* Log user information. */
  132. void user_log(char *c)
  133. {
  134.   char fname[MAX_FNAME+1], logbuf[MAX_LINELEN+1], cbuf[256], sbuf[32], shbuf[32];
  135.   char *user, *classe, *sh, *sess, *exo, *cc, *ip, *allow, *pend;
  136.   char *ex;
  137.   double sc,Sc;
  138.     int i, scorelog, testmax;
  139.  
  140.   if(robot_access || strstr("session_prefix","_check")!=NULL) return;
  141.   user=getvar("wims_user"); classe=getvar("wims_class");
  142.   sc=0;
  143.   if(isexam) {
  144.     if(user==NULL || classe==NULL || *user==0 || *classe==0) return;
  145.     sh=getvar("worksheet"); if(sh==NULL) return;
  146.     mystrncpy(shbuf,sh,sizeof(shbuf));
  147.     exo=strchr(shbuf,'.'); if(exo==NULL) return;
  148.     *exo++=0; sh=shbuf;
  149.     if(mode==mode_default) sess=getvar("wims_session");
  150.     else sess="popup";
  151.     if(sess==NULL) return;
  152.     mystrncpy(sbuf,sess,sizeof(sbuf));
  153.     sess=strchr(sbuf,'_'); if(sess==NULL) return;
  154.     *sess=0; sess=sbuf; ex="E";
  155.     accessfile(logbuf,"r","%s/.E%s",class_dir,sh);
  156.     if(strchr(logbuf,'#')!=NULL || strcmp(user,"supervisor")==0) simuxam=1;
  157.     else {
  158.       accessfile(logbuf,"r","%s/%s/examsimu.%d", session_dir,sess,sh);
  159.       if(strstr(logbuf,"yes")!=NULL) user_error("expired_exam");
  160.     }
  161.     mkfname(examlogd,"%s/examlog/%s/%s",class_dir,user,sess);
  162.     mkfname(examlogf,"%s/%s.%s",examlogd,sh,exo);
  163.   }
  164.   else {
  165.     sh=getvar("wims_sheet");
  166.     if(sh==NULL || *sh==0) return;
  167.     exo=getvar("wims_exo"); if(exo==NULL) return;
  168.     sess=getvar("wims_session");
  169.     if(sess==NULL) return;
  170.     ex="";
  171.  
  172.   }
  173.   if(strcmp(c,"new")!=0 && strcmp(c,"renew")!=0
  174.      && strcmp(c,"rafale")!=0
  175.      && strcmp(c,"hint")!=0 && parm_restore==0) {
  176.     char *s;
  177.     s=getvar("module_score");
  178.     if(s==NULL || *s==0) return;
  179.     sc=atof(s); if(!isfinite(sc)) {sc=0; return;}
  180.     snprintf(cbuf,sizeof(cbuf),"score %s",s);
  181.     cc=cbuf;
  182.   }
  183.   else cc=c;
  184.   if(classe==NULL || *classe==0) i=1;
  185.   else i=getscorestatus(classe,atoi(sh));
  186.     testmax=strcmp(c,"new")!=0 && strcmp(c,"renew")!=0 && gettrycheck(classe,user,atoi(sh),atoi(exo))==1;
  187.   pend=getvar("wims_scoring"); if(pend==NULL) pend="";
  188.     if((i==0 || !exodepOK || strcmp(pend,"pending")!=0 || testmax) && strcmp(cc,"rafale")!=0)
  189.       if(testmax) allow="maxtry noscore"; else allow="noscore";
  190.       else allow="";
  191.   ip=remote_addr; if(*ip==0) ip="-"; scorelog=0;
  192.   if(user==NULL || *user==0) {
  193.     classe="0"; allow="";
  194.     mkfname(fname,"../sessions/%s/.score",sess);
  195.   }
  196.   else {
  197.     char *pp;
  198.     if(classe==NULL || *classe==0) return;
  199.     pp=getvar("wims_scorereg");
  200.      if((allow[0]==0 || gettrycheck(classe,user,atoi(sh),atoi(exo))==1 || (pp!=NULL && strcmp(pp,"suspend")==0)) && *ex!='E')
  201.       scorelog=1;
  202.     else
  203.       mkfname(fname,"classes/%s/noscore/%s",classe,user);
  204.   }
  205.   if(isexam && user!=NULL && *user!=0) {
  206.     allow=exam_sheetexo;
  207.     snprintf(logbuf,sizeof(logbuf),":%s %2s %s  \t%s%s\n",
  208.     nowstr,exo,cc,ip,allow);
  209.     accessfile(logbuf,"a","%s/%s/examscore.%s", session_dir,sess,sh);
  210.     Sc=currexamscore(atoi(sh));
  211.     accessfile(logbuf,"r","%s/.E%s",class_dir,sh);
  212.     if(simuxam==0) {   /* not simulation */
  213.       if(sc>0) {
  214.         snprintf(logbuf,sizeof(logbuf),
  215.         "%s %.5f -1 %u %s %s\n",
  216.         sh,Sc,(unsigned int) nowtime,ip,sess);
  217.         accessfile(logbuf,"a","%s/score/%s.exam", class_dir,user);
  218.       }
  219.     }
  220.     else snprintf(exam_sheetexo+strlen(exam_sheetexo),
  221.         sizeof(exam_sheetexo)-strlen(exam_sheetexo), "\tS");
  222.   }
  223.   snprintf(logbuf,sizeof(logbuf),"%s%s %s %2s %2s %s  \t%s\t%s",
  224.       ex,nowstr,sess,sh,exo,cc,ip,allow);
  225.   if(scorelog) {
  226.     snprintf(tmplbuf,sizeof(tmplbuf),"-c%s -u%s scorelog %s",
  227.        classe,user,logbuf);
  228.     _daemoncmd(tmplbuf);
  229.   }
  230.   else write_logfile(fname,logbuf);
  231. }
  232.  
  233. /* Log class information. */
  234. void class_log(char *cl, char *l, char *ip)
  235. {
  236.   char logbuf[1024];
  237.  
  238.   if(robot_access) return;
  239.   snprintf(logbuf,sizeof(logbuf),"%s %s   \t%s",
  240.       nowstr,ip,l);
  241.   write_logfile(mkfname(NULL,"classes/%s/.log",cl),logbuf);
  242. }
  243.  
  244. /* Log accesses to modules. For server counting use. */
  245. void access_log(char *c)
  246. {
  247.   int i;
  248.   char *ip, *p, *sess, *s, *agent, *u, *cl;
  249.   time_t logtime;
  250.   char ag[128], tm[64];
  251.  
  252.   ip=remote_addr;
  253.   if(*ip==0) ip="????????";
  254.   if(mode==mode_default) sess=getvar("wims_session");
  255.   else sess="popup";
  256.   if(sess==NULL) sess="----------";
  257.   p=getvar(ro_name[ro_module]);
  258.   if(p==NULL || *p==0) p="-";
  259. /* limit module name to 40 chars */
  260.   i=strlen(p); if(i>40) p+=i-40;
  261.   if(robot_access) {
  262.    agent=getenv("HTTP_USER_AGENT"); if(agent==NULL) agent="-";
  263.    snprintf(ag,sizeof(ag)," %s",agent);
  264.   }
  265.   else {
  266.    u=getvar("wims_user");
  267.    if(u!=NULL && *u!=0) snprintf(ag,sizeof(ag)," %s,%s",u,getvar("wims_class"));
  268.    else ag[0]=0;
  269.   }
  270.   s=strchr(sess,'_'); if(s==NULL) s=sess+strlen(sess);
  271.   if(s<sess+4) s=sess; else s=s-4;
  272.   tm[0]=0; logtime=time(0); if(logtime>nowtime+2) {
  273.    snprintf(tm,sizeof(tm)," (%lus)", logtime-nowtime);
  274.   }
  275.   snprintf(tmplbuf,sizeof(tmplbuf),"%s %.6s %s\11%s\11%s%s%s",
  276.       nowstr, s, ip, c, p, tm, ag);
  277.   write_logfile("access.log",tmplbuf);
  278.   user_log(c);
  279.   cl=getvar("wims_class");
  280.   if(cl!=NULL && *cl!=0) {
  281.    char *l;
  282.    l=getvar("wims_class_log");
  283.    if(l!=NULL && *l!=0) class_log(cl, l, ip);
  284.   }
  285. }
  286.  
  287. /* Log of mails. */
  288. void mail_log(char *c)
  289. {
  290.   int i;
  291.   char *ip, *p, *sess, *s, *cl;
  292.  
  293.   ip=remote_addr;
  294.   if(*ip==0) ip="????????";
  295.   if(mode==mode_default) sess=getvar("wims_session");
  296.   else sess="popup";
  297.   if(sess==NULL) sess="----------";
  298.   p=getvar(ro_name[ro_module]);
  299.   if(p==NULL || *p==0) p="-";
  300. /* limit module name to 40 chars */
  301.   i=strlen(p); if(i>40) p+=i-40;
  302.   s=strchr(sess,'_'); if(s==NULL) s=sess+strlen(sess);
  303.   if(s<sess+4) s=sess; else s=s-4;
  304.   cl=getvar("wims_class"); if(cl==NULL) cl="";
  305.   snprintf(tmplbuf,sizeof(tmplbuf),"%s %.6s %s\11%s\11%s\11%s",
  306.       nowstr, s, ip, c, cl, p);
  307.   write_logfile("mail.log",tmplbuf);
  308. }
  309.  
  310. /* log posted data */
  311. void post_log(void)
  312. {
  313.   char *h, *l, logstr[2*MAX_LINELEN+2];
  314.   char *authpwd, *p, *ll, *l1 ;
  315.  
  316.   h=remote_addr;
  317.   if(mpboundary[0]!=0) l="multipart/form-data"; else l=stdinbuf;
  318.  
  319.   ll=strdup(l);
  320.   authpwd="auth_password=";
  321.   if((p=strstr(l,authpwd))!=NULL ) {
  322.     l1=strdup(l);
  323.     mystrncpy(ll,l,p-l+strlen(authpwd)+1);
  324.     strcat(ll,"xxxx");
  325.     mystrncpy(l1,p+strlen(authpwd),strlen(l));
  326.     if((p=strstr(l1,"&"))!=NULL) strcat(ll,p);
  327.   }
  328.  
  329.   snprintf(logstr,sizeof(logstr),"%s %s\t%s",
  330.      nowstr, h, ll);
  331.   write_logfile("post.log",logstr);
  332. }
  333.  
  334. /* It is this routine which is called by main(). */
  335. void write_logs(void)
  336. {
  337.   char *p;
  338.   p=getvar(ro_name[ro_cmd]); if(p==NULL || *p==0) p="no_cmd";
  339.   access_log(p); if(strstr(session_prefix,"_check")!=NULL) return;
  340.   module_log(); referer_log();
  341.   if(new_session) session_log(p);
  342. }
  343.  
  344. void user_error_log(char msg[])
  345. {
  346.   char *s, *m, *c, *h, *q, *r, *sess, logstr[512];
  347.   if(mode==mode_default) sess=getvar("wims_session");
  348.   else sess="popup";
  349.   if(sess==NULL) sess="----------";
  350.   m=getvar(ro_name[ro_module]);if(m==NULL) m="";
  351.   c=getvar(ro_name[ro_cmd]);if(c==NULL) c="";
  352.   h=remote_addr;
  353.   q=getenv("QUERY_STRING");if(q==NULL) q="";
  354.   r=getenv("HTTP_REFERER");
  355.   if(r==NULL || strstr(r,cgi_name)!=NULL) r="";
  356.   s=strchr(sess,'_'); if(s==NULL) s=sess+strlen(sess);
  357.   if(s<sess+4) s=sess; else s=s-4;
  358.   snprintf(logstr,sizeof(logstr),"%s %.5s %s %s, module=%s cmd=%s: %s %s",
  359.      nowstr, s, h, msg, m, c, r, q);
  360.   write_logfile("user_error.log",logstr);
  361.   if(user_error_nolog) return;
  362.   access_log("user_error");referer_log();
  363. }
  364.  
  365. void module_error_log(char msg[])
  366. {
  367.   char *s, *m, *c, logstr[256];
  368.   if(strstr(msg,"debug")!=NULL || strstr(msg,"timeup")!=NULL) return;
  369.   if(strstr(m_file.name,"sessions/")!=NULL) return;
  370.   s=getvar(ro_name[ro_module]);
  371.   if(s!=NULL) {
  372.    if(strncmp(s,"classes/",strlen("classes/"))==0 ||
  373.     strncmp(s,"devel/",strlen("devel/"))==0) return;
  374.   }
  375.   s=getvar("wims_session"); if(s==NULL) s="  ";
  376.   m=getvar(ro_name[ro_module]);if(m==NULL) m="";
  377.   c=getvar(ro_name[ro_cmd]);if(c==NULL) c="";
  378.   snprintf(logstr,sizeof(logstr),"%s %.10s %s in %s/%s, line %d",
  379.      nowstr, s+2, msg, m, m_file.name, m_file.l+1);
  380.   write_logfile("module_error.log",logstr);
  381.   access_log("module_error");
  382. }
  383.  
  384. /* Refused users due to threshold excess */
  385. void refuse_log(int th)
  386. {
  387.   char *load, *h;
  388.  
  389.   load=getvar("wims_server_load"); if(load==NULL) load="??";
  390.   h=remote_addr;
  391.   snprintf(tmplbuf,sizeof(tmplbuf),"%s %s\11%d:%s",
  392.       nowstr, h, th, load);
  393.   write_logfile("refuse.log",tmplbuf);
  394. }
  395.  
  396. #define logdpid "../tmp/log/wimslogd.pid"
  397. #define newlogd "../tmp/log/wimslogd.new"
  398.  
  399. void bringuplogd(void)
  400. {
  401.   char *arg[]={"../bin/wimslogd",NULL};
  402.   struct stat st;
  403.   pid_t pid;
  404.  
  405. /* need to update wimslogd? */
  406.   if(stat(newlogd,&st)==0) {
  407.    if((S_IXUSR&st.st_mode)!=0 && st.st_size>40000 && st.st_size<200000)
  408.      call_ssh("mv %s %s",newlogd,arg[0]);
  409.    else call_ssh("rm -f %s",newlogd);
  410.   }
  411.   pid=fork(); if(pid) return;   /* parent */
  412. /* double fork to escape sysmask orphan. */
  413.   pid=fork(); if(pid) {   /* secondary parent */
  414.    snprintf(tmplbuf,sizeof(tmplbuf),"%u",pid);
  415.    mkdirs("../tmp/log");
  416.    chmod("../tmp/log",S_IRUSR|S_IWUSR|S_IXUSR);
  417.    accessfile(tmplbuf,"w",logdpid);
  418.    exit(0);
  419.   }
  420.   setreuid(geteuid(),geteuid());setregid(getegid(),getegid());
  421.   snprintf(tmplbuf,sizeof(tmplbuf),"%u %u %u %u %u %u %d %d %d",
  422.       idle_time,idle_time2,idle_time3,
  423.       OLD_LOG_FILES,GEN_LOG_LIMIT,
  424.       MODULE_LOG_LIMIT,backup_hour,site_accounting,
  425.       examlog_limit);
  426.  
  427.   setenv("wimslogd",tmplbuf,1);
  428.   execve(arg[0],arg,environ);
  429.   fprintf(stderr,"Unable to execute wimslogd: %s\n",strerror(errno));
  430.   exit(1);
  431. }
  432.  
  433. void checklogd(void)
  434. {
  435.   int i,t;
  436.   char *p1, *p2, *p, buf[MAX_LINELEN+1];
  437.   sun.sun_family=PF_UNIX;
  438.   snprintf(sun.sun_path,sizeof(sun.sun_path),"%s",ksockfile);
  439.   p=getenv("REMOTE_ADDR"); if(p==NULL) p="";
  440.   snprintf(buf+sizeof(int),sizeof(buf)-sizeof(int),"ping %s",p);
  441.   t=kerneld(buf,sizeof(buf)); if(t<0) {
  442.     bringuplogd();
  443.     for(i=0; i<10 && t<0; i++) {
  444.       msleep(100);
  445.         snprintf(buf+sizeof(int),sizeof(buf)-sizeof(int),"ping %s",p);
  446.      t=kerneld(buf,sizeof(buf));
  447.     }
  448.   }
  449.   if(t<0) internal_error("Unable to bring up wimslogd.");
  450.   p1=find_word_start(buf+sizeof(int)); p2=find_word_end(p1);
  451.   if(*p2) *p2++=0;
  452.   if(strcmp(p1,"OK")!=0) internal_error("wimslogd error.");
  453.   if(*p2=='1') hostcquota=1; else hostcquota=0;
  454.   p1=find_word_start(find_word_end(p2));
  455.   p2=strchr(p1,'\n'); if(p2) *p2++=0; else p2=p1+strlen(p1);
  456.   mystrncpy(loadavg,p1,sizeof(loadavg));
  457.   p2=find_word_start(p2);
  458.   p1=find_word_end(p2); if(*p1) *p1++=0;
  459.  
  460.   p=getenv("SERVER_ADDR");
  461.   if(*p2!=0 && memcmp(p,"10.",3)==0) p=p2;
  462.   if(p!=NULL) {
  463.     i=strlen(cookieheader);
  464.     snprintf(cookieheader+i, sizeof(cookieheader)-i,"%s/",p);
  465.   }
  466.   p=getenv("HTTP_COOKIE"); cookiegot[0]=0;
  467.   if(p!=NULL && (p2=strstr(p,cookieheader))!=NULL) {
  468.     mystrncpy(cookiegot,find_word_start(p+strlen(cookieheader)),sizeof(cookiegot));
  469.     *find_word_end(cookiegot)=0;
  470.   }
  471. }
  472.