forked from shammamah-zz/PluggedIn
/
login.c
executable file
·192 lines (137 loc) · 3.69 KB
/
login.c
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int i= 0;
char * entries[2]; // 1st entry is user name 2nd is password
int loginStatus=0;
char command[50];
//prepare the commandline call line which in "./scriptName UserName" format(note the space inbetween)
char *commandParser()
{
//prepare the commandline call line which in "./scriptName UserName" format(note the space inbetween)
char argument[50];
char *nameWithSpace =(char *)malloc( strlen(entries[0]) + 2 );
*(nameWithSpace)=' ';
char * counterPointer= nameWithSpace;
counterPointer++;
//we are going to insert a space before the username
int i=0;
for(i; i < strlen(entries[0]); i++ )
{
*(counterPointer++)=*(entries[0]+i);
}
*(counterPointer)='\0';
strcpy(argument, nameWithSpace);
strcpy(command, "./dashboard.py");
strcat(command,argument);
//command line command now in corect
return command;
}
//takes input an already parsed section, eg username=bob
int parser( char *s )
/* Nested strtoks are a problem so this is our own parser*/
{
char *vname;
char *val;
//this part gets rid of the CGI vatiabble name tag
if(s == '\0') {
printf("X_ERR='Null phrase'\n");
i++;
return 0;
}
vname = s;//i we ll need the vname later on we can get it form here
while( (*s != '\0') && (*s != '=')) s++;
if(*s == '\0' )
{
printf("X_ERR='Null assignment'\n");
i++;
return 0;
}
*(s++) = '\0';
//now the pointer points to the beggining of the username/password
val =s;
while( (*s != '\0') && (*s != '=')) s++;
*(s++) = '\0';
entries[i]=val;
i++;
return 1;
}
int main(void)
{
char *query_string=getenv("QUERY_STRING");
char buffer[ strlen(query_string) +1];
strcpy(buffer, query_string);
char* token;
token = strtok(buffer,"&");
while(i<2)
{
parser(token);
token= strtok(NULL,"&");
}
//validate password
FILE *validate= fopen("users.txt", "rt"); //file pointer to database
if(validate == NULL) //give Error if problem reading database
{
printf("%s%c%c\n","Content-Type:text/html",13,10);
printf("<html>");
printf("<head><title>ERROR 01001</title></head>");
printf("<body><p>DATABASE NOT FOUND</p></body>");
return 1;
}
char field[5000];
fgets( field, 5000, validate);
field[strlen(field) - 1] = '\0';
char *usr;
usr=field;
strtok(usr," ");
while(!feof(validate) )
{
if(strcmp(entries[0], usr) == 0)//if user name is valid
{
fgets( field, 5000, validate);
field[strlen(field) - 1] = '\0';
if( strcmp(entries[1], field) == 0)//if password is valid
{
loginStatus=1; //login is valid now
break;
}
else
{
break;
}
}
//skip 4 lines to next user name
int c1=0;
for(c1; c1<4; c1++)
{
fgets( field, 5000, validate);
field[strlen(field) - 1] = '\0';
usr=field;
strtok(usr," ");
}
}
fclose(validate); //validation done
FILE *in=fopen("index.html", "rt"); //File pointer to the dashboard
//publish dashboard to STDOUT character by character if valid login
if(loginStatus)
{
char *parsedCommand = commandParser(); //fromat commandline input
system(parsedCommand); //we are calling dashboard.py here
return 0;
}
else
{
//if failure
FILE *error=fopen("loginError.html", "rt");
int c;
c=fgetc(error);
printf("%s%c%c\n","Content-Type:text/html",13,10);
while(!feof(error) )
{
fputc(c, stdout);
c=fgetc(error);
}
fclose(error);
return 1;
}
}