summaryrefslogtreecommitdiff
path: root/ws2a/bugcomment.c
blob: eeba8febb204fe96bc7a67ca59d2100868f443e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdint.h>

char* replaceHTML(char* str){
  //printf("Replaceing:%s\n",str);
  char* nstr = malloc((sizeof(char)*strlen(str)) + 1);
  if(nstr == NULL){
    printf("%s%c%c\n","Content-Type:text/html;charset=iso-8859-1",13,10);
    printf("Failed to allocate memory!\n");
  }

  int strp = 0;
  int nstrp = 0;
  while(str[strp] != '\0'){
    //printf("Strp=%d nstrp=%d\n",strp,nstrp);
    if(str[strp] == '+'){
      nstr[nstrp] = ' ';
      strp++;
      nstrp++;
    }else if(str[strp] == '%'){
      //printf("Hit encodeing!\n");
      int hex;
      sscanf(str+strp,"%%%x",&hex);
      //printf("Was:%c\n",(char)hex);
      nstr[nstrp] = (char)hex;
      nstrp++;
      strp+=3;
    }else if(str[strp] == '\n'){
      nstr[nstrp] = '\\';
      nstr[nstrp+1] = 'n';
      nstrp+=2;
      strp++;
    }else{
      nstr[nstrp] = str[strp];
      nstrp++;
      strp++;
    }
  }
  nstr[nstrp] = '\0';
  //printf("Done, str is now:%s\n",nstr);
  return nstr;
}

unsigned long hash(unsigned char *str){
    unsigned long hash = 5381;
    int c;

    while (c = *str++)
        hash = ((hash << 5) + hash) + c; /* hash * 33 + c */

    return hash;
}

char* useridhash(char* str){
  char* hash = malloc(sizeof(char)*20);
  int i = (int)str[0];
  int len = strlen(str);
  int tloop = i*20;
  unsigned int hashp = 0;
  unsigned int strp = 0;
  i = 0;
  while(i < tloop){
    char tchar = str[strp%len];
    hash[hashp%20] += str[strp%len];
    hashp+=(int)tchar;
    strp+=(int)(hashp*hashp);
    i++;
  }
  i = 0;
  while(i < 20){
    unsigned int hashnum = hash[i];
    unsigned int modedhashnum = hashnum % 92;
    hash[i] = modedhashnum + 32;
    i++;
  }
  return hash;
}

int main(){
  char* data = getenv("QUERY_STRING");
  //char data[20] = "?id=1";
  char* hardip = getenv("REMOTE_ADDR");

  char name[15];
  char userid[20];
  char comment[2048];
  char captcha[25];
  char bugid[5];

  char* iname = strtok(data,"&");
  char* iuserid = strtok(NULL,"&");
  char* icomment = strtok(NULL,"&");
  char* icaptcha = strtok(NULL,"&");
  char* ibugid = strtok(NULL,"&");

  sscanf(iname,"name=%s",name);
  sscanf(iuserid,"id=%s",userid);
  sscanf(icomment,"comment=%s",comment);
  sscanf(icaptcha,"captcha=%s",captcha);
  sscanf(ibugid,"bugid=%s",bugid);

  /*
  char* name = "Apickx";
  char* userid = "ThestID";
  char* comment = "This+Is+My+Comment";
  char* captcha = "teeny-tiny+currency";
  char* bugid = "1";
  */

  //printf("<p>name:%s<p>userid:%s<p>comment:%s<p>captcha:%s<p>bugid:%s",name,userid,comment,captcha,bugid);

  //Check captcha
  char captchapath[100];
  char* captchadecoded = replaceHTML(captcha);
  sprintf(captchapath,"../ws2a/captchas/%s.txt",captchadecoded);

  FILE* captchafile = fopen(captchapath,"r");
  if(captchafile == NULL){
    printf("%s%c%c\n","Content-Type:text/html;charset=iso-8859-1",13,10);
    printf("Captcha incorrect");
    return;
  }
  //Hash of "teeny-tiny currency" is 2053680550
  unsigned long inputhash = hash(captchadecoded);
  free(captchadecoded);
  unsigned long filehash = 0;
  fscanf(captchafile,"%lu",&filehash);
  if(filehash != inputhash){
    printf("%s%c%c\n","Content-Type:text/html;charset=iso-8859-1",13,10);
    printf("Captcha incorrect");
    return;
  }

  char command[100];
  sprintf(command,"rm \"%s.txt\"",captchapath);
  system(command);

  //Add comment to bug file
  char filepath[100];
  sprintf(filepath,"../ws2a/bugs/%s",bugid);
  FILE* bugfile = fopen(filepath,"a");
  if(bugfile == NULL){
    printf("%s%c%c\n","Content-Type:text/html;charset=iso-8859-1",13,10);
    printf("<p>Unable to find bug!");
    return;
  }
  char* dname = replaceHTML(name);
  char* duid = useridhash(userid);
  char* dcomment = replaceHTML(comment);
  printf("Location: ../ws2a/bugview.html?id=%s\n\n",bugid);
  //printf("<p>data:%s",data);
  //printf("Everything ok, inserting comment!");
  fprintf(bugfile,"\n%s\n%s\n%s\n",dname,duid,dcomment);
  free(dname);
  free(duid);
  free(dcomment);
  return 0;
}