Subversion Repositories wimsdev

Rev

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

Rev 10 Rev 7673
Line 15... Line 15...
15
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
15
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
16
 */
16
 */
17
 
17
 
18
void secure_exec(void);
18
void secure_exec(void);
19
 
19
 
20
int fwrite_calls=0;     /* number of files */
20
int fwrite_calls=0; /* number of files */
21
int fwrite_sizes=0;     /* total size */
21
int fwrite_sizes=0; /* total size */
22
 
22
 
23
        /* write to a writable file. */
23
/* write to a writable file. */
24
void _filewrite(char *prefix, char *fname, char *content, char *type)
24
void _filewrite(char *prefix, char *fname, char *content, char *type)
25
{
25
{
26
    char *m, buf[MAX_LINELEN+1];
26
    char *m, buf[MAX_LINELEN+1];
27
    FILE *f;
27
    FILE *f;
28
    int n,checklink;
28
    int n,checklink;
29
    struct stat stl;
29
    struct stat stl;
30
    checklink=0;
30
    checklink=0;
31
    if(strstr(fname,parent_dir_string)!=NULL){
31
    if(strstr(fname,parent_dir_string)!=NULL){
32
        setvar(error_data_string,fname);
32
     setvar(error_data_string,fname);
33
        module_error("illegal_fname"); return;
33
     module_error("illegal_fname"); return;
34
    }
34
    }
35
    m=getvar(ro_name[ro_module]);
35
    m=getvar(ro_name[ro_module]);
36
    if(m==NULL || *m==0) return;
36
    if(m==NULL || *m==0) return;
37
    if(strncmp(fname,"TEMP_",5)==0 && strchr(fname,'/')==NULL &&
37
    if(strncmp(fname,"TEMP_",5)==0 && strchr(fname,'/')==NULL &&
38
       strstr(session_prefix,"robot")==NULL) {
38
       strstr(session_prefix,"robot")==NULL) {
39
        mystrncpy(buf,tmp_dir,sizeof(buf));
39
     mystrncpy(buf,tmp_dir,sizeof(buf));
40
        goto add;
40
     goto add;
41
    }
41
    }
42
    if(strncmp(fname,"getfile/",strlen("getfile/"))==0) {
42
    if(strncmp(fname,"getfile/",strlen("getfile/"))==0) {
43
        if(strchr(fname+strlen("getfile/"),'/')!=NULL) {
43
     if(strchr(fname+strlen("getfile/"),'/')!=NULL) {
44
denied:
44
denied:
45
            setvar(error_data_string,fname);
45
         setvar(error_data_string,fname);
46
            module_error("file_access_denied"); return;
46
         module_error("file_access_denied"); return;
47
        }
47
     }
48
        fname+=strlen("getfile/");
48
     fname+=strlen("getfile/");
49
        snprintf(buf,sizeof(buf),"%s/getfile",session_prefix);
49
     snprintf(buf,sizeof(buf),"%s/getfile",session_prefix);
50
        mkdirs(buf);
50
     mkdirs(buf);
51
        checklink=1; goto add;
51
     checklink=1; goto add;
52
    }
52
    }
53
    if(trusted_module() && !is_class_module && strncmp(fname,"wimshome/",9)==0) {
53
    if(trusted_module() && !is_class_module && strncmp(fname,"wimshome/",9)==0) {
54
        mystrncpy(buf,getvar("wims_home"),sizeof(buf));
54
     mystrncpy(buf,getvar("wims_home"),sizeof(buf));
55
        fname+=9; goto add;
55
     fname+=9; goto add;
56
    }
56
    }
57
    if(strncmp(m,"adm/",4)==0 || strcmp(m,home_module)==0) {
57
    if(strncmp(m,"adm/",4)==0 || strcmp(m,home_module)==0) {
58
        mystrncpy(buf,prefix,sizeof(buf));
58
     mystrncpy(buf,prefix,sizeof(buf));
59
    }
59
    }
60
    else {
60
    else {
61
        if(!trusted_module() && strchr(fname,'/')!=NULL) return; /* silent */
61
     if(!trusted_module() && strchr(fname,'/')!=NULL) return; /* silent */
62
        snprintf(buf,sizeof(buf),"w/%s",prefix);
62
     snprintf(buf,sizeof(buf),"w/%s",prefix);
63
        mkdirs(buf);
63
     mkdirs(buf);
64
    }
64
    }
65
    add: snprintf(buf+strlen(buf),sizeof(buf)-strlen(buf),"/%s",fname);
65
    add: snprintf(buf+strlen(buf),sizeof(buf)-strlen(buf),"/%s",fname);
66
    if(!trusted_module() || is_class_module) {
66
    if(!trusted_module() || is_class_module) {
67
        if(fwrite_calls>=MAX_FWRITE) goto denied;
67
     if(fwrite_calls>=MAX_FWRITE) goto denied;
68
        fwrite_calls++;
68
     fwrite_calls++;
69
        n=strlen(content)+1;
69
     n=strlen(content)+1;
70
        if(fwrite_sizes+n>MAX_FWRITE_SIZE) goto denied;
70
     if(fwrite_sizes+n>MAX_FWRITE_SIZE) goto denied;
71
        fwrite_sizes+=n;
71
     fwrite_sizes+=n;
72
    }
72
    }
73
    if(checklink && lstat(buf,&stl)==0 && S_ISLNK(stl.st_mode))
73
    if(checklink && lstat(buf,&stl)==0 && S_ISLNK(stl.st_mode))
74
      goto denied;
74
      goto denied;
75
    lastdatafile[0]=lastftest[0]=0;
75
    lastdatafile[0]=lastftest[0]=0;
76
    f=fopen(buf,type); if(f==NULL) return;
76
    f=fopen(buf,type); if(f==NULL) return;
77
    fprintf(f,"%s\n",content);
77
    fprintf(f,"%s\n",content);
78
    fclose(f);
78
    fclose(f);
79
}
79
}
80
 
80
 
81
        /* write to a file in module */
81
/* write to a file in module */
82
void filewrite(char *p)
82
void filewrite(char *p)
83
{
83
{
84
    char *p1, *p2;
84
    char *p1, *p2;
85
    secure_exec();
85
    secure_exec();
86
    p1=find_word_start(p);
86
    p1=find_word_start(p);
Line 89... Line 89...
89
    if(*p2!=0) *p2++=0;
89
    if(*p2!=0) *p2++=0;
90
    _filewrite(module_prefix,p1,p2,"w");
90
    _filewrite(module_prefix,p1,p2,"w");
91
    *p=0;
91
    *p=0;
92
}
92
}
93
 
93
 
94
        /* append to a file in module */
94
/* append to a file in module */
95
void fileappend(char *p)
95
void fileappend(char *p)
96
{
96
{
97
    char *p1, *p2;
97
    char *p1, *p2;
98
    secure_exec();
98
    secure_exec();
99
    p1=find_word_start(p);
99
    p1=find_word_start(p);