Subversion Repositories wimsdev

Rev

Rev 12209 | Rev 12211 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed

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