00001
00006 #include<stdio.h>
00007 #include<stdlib.h>
00008 #include <mysql/mysql.h>
00009 #include<string.h>
00010 #include <unistd.h>
00011 #include <stdio.h>
00012 #include <stdlib.h>
00013 #include <strings.h>
00014 #include <sys/socket.h>
00015 #include <sys/types.h>
00016 #include <netinet/in.h>
00017 #include <time.h>
00018 #include <signal.h>
00019 #include <sys/stat.h>
00020 #define SIZE_ID 20
00021 #define SIZE_PASS_WORD 10
00022 #define BUFFER_SIZE 4096*1000
00023 #define LISTEN_PORT 9999
00024 MYSQL *connection1;
00025 MYSQL *connection2;
00026 MYSQL_RES *result;
00027 MYSQL_ROW sqlrow;
00028 int num_of_res;
00029 FILE* fw=NULL,*fr=NULL;
00030
00031 int logged_in=0;
00032 char logged_user[100];
00033 char search_res[100][50],resp_users[100][50];
00034 int listen_file_descriptor;
00035
00038 void close_properly(int signal)
00039 {
00040 int return_value;
00041
00042 printf("Shutting down...\n");
00043
00044 return_value =close(listen_file_descriptor);
00045
00046 if(return_value <0)
00047 {
00048 perror("Cannot close listening socket.");
00049 exit(EXIT_FAILURE);
00050 }
00051
00052 printf("Shutdown complete.\n");
00053 exit(0);
00054 }
00058 void owner_login()
00059 {
00060 connection1=mysql_init (NULL);
00061 if (mysql_real_connect (connection1, "localhost","cs3002_user", "cs3002_password","cs3002_database", 0, NULL, 0))
00062 printf("server connected to database\n");
00063 }
00066 void drop_tables()
00067 {
00068 mysql_query (connection1, "drop table login_details");
00069 mysql_query (connection1, "drop table user_details");
00070 printf("Dropped\n");
00071 }
00074 void create_tables()
00075 {
00076 drop_tables();
00077 int result = mysql_query (connection1, "create table login_details(user_id varchar(10) PRIMARY KEY,pass_word varchar(50))");
00078 if(result!=0) {printf("Couldnot create the login table..\n");exit(0);}
00079 result= mysql_query (connection1, "create table user_details(name varchar(50),email_id varchar(50),user_id varchar(50),pass_word varchar(50),organization varchar(50))");
00080 if(result!=0) {printf("Couldnot create the login table..\n");exit(0);}
00081 mysql_query (connection1, "create table file_details(user_id varchar(10) ,file_name varchar(50))");
00082 printf("created\n");
00083
00084 }
00085
00094 int create_account(char* name,char* email_id,char* user_id,char* pass_word,char* org)
00095 {
00096 int count=0,res;
00097 char str[500];
00098 sprintf(str,"select * from login_details where user_id='%s'",user_id);
00099 res= mysql_query (connection1, str);
00100 if(res!=0) {printf("Database not found...\n");exit(0);}
00101 result = mysql_use_result (connection1);
00102 while ((sqlrow = mysql_fetch_row (result)))
00103 count++;
00104 printf("count:%d\n",count);
00105 if(count!=0)
00106 {
00107 printf("User-id not available....try another\n");
00108 return -1;
00109 }
00110 else
00111 {
00112 if(strlen(pass_word)<6) printf("Length of the password should be atleast 6...\n");
00113 else if(strlen(email_id)<6) printf("Length of the email_id should be atleast 6...\n");
00114 else
00115 {
00116 str[0]='\0';
00117 sprintf(str,"insert into login_details values('%s','%s');",user_id,pass_word);
00118 res=mysql_query (connection1, str);
00119 printf("USERID=%s Password=%s result=%d\n",user_id,pass_word,res);
00120 str[0]='\0';
00121 sprintf(str,"insert into user_details values('%s','%s','%s','%s','%s');",name,email_id,user_id,pass_word,org);
00122 mysql_query (connection1, str);
00123 mkdir(user_id,0777);
00124 }
00125 return 1;
00126 }
00127
00128 }
00129
00135 int user_check(char* user_id,char* password)
00136 {
00137 char str[500];
00138 int count=0,res;
00139 sprintf(str,"select * from login_details where user_id='%s';",user_id);
00140 res= mysql_query (connection1, str);
00141 if(res!=0)
00142 {
00143 printf("Database not found...\n");
00144 exit(0);
00145 }
00146 result = mysql_use_result(connection1);
00147 while (mysql_fetch_row (result))
00148 count++;
00149 if(count!=1)
00150 {
00151
00152 printf("The id is not found...\n");
00153 return -1;
00154 }
00155 else
00156 {
00157 sprintf(str,"select '%s' from login_details where pass_word='%s';",user_id,password);
00158 mysql_query (connection1, str);
00159 count=0;
00160 result = mysql_use_result (connection1);
00161 while ((sqlrow = mysql_fetch_row (result)))
00162 {
00163
00164 count++;
00165 }
00166 if(count!=1)
00167 {
00168 printf("count is not 1..%d\n",count);
00169 return -1;
00170 }
00171 else
00172 {
00173 printf("Successfully logged in..\n");
00174
00175 return 1;
00176 }
00177 }
00178 }
00179
00184 int search_for(char* qry)
00185 {
00186 char str[100];
00187 sprintf(str,"select * from file_details where file_name LIKE '%%%s%%';",qry);
00188 mysql_query (connection1, str);
00189 result = mysql_use_result (connection1);
00190 int i=0;
00191 while ((sqlrow = mysql_fetch_row (result)))
00192 {
00193 strcpy(search_res[i],sqlrow[1]);
00194 strcpy(resp_users[i],sqlrow[0]);
00195 printf("RESULTS:%s %s",search_res[i],resp_users[i]);
00196 i++;
00197 }
00198 return i;
00199 }
00203 int delete_from_database(char* str)
00204 {
00205 char qry[100];
00206 sprintf(qry,"delete from file_details where file_name='%s' AND user_id='%s';",str,logged_user);
00207 printf("str=%s logged_user=%s",str,logged_user);
00208 mysql_query (connection1, qry);
00209 return 0;
00210 }
00214 void delete_user()
00215 {
00216 char qry[100];
00217 sprintf(qry,"delete from file_details where user_id='%s';",logged_user);
00218 mysql_query (connection1, qry);
00219 qry[0]='\0';
00220 sprintf(qry,"delete from login_details where user_id='%s';",logged_user);
00221 mysql_query (connection1, qry);
00222 qry[0]='\0';
00223 sprintf(qry,"delete from user_details where user_id='%s';",logged_user);
00224 mysql_query (connection1, qry);
00225 }
00226
00227 char cur_file[100];
00232 int add_to_database(char* file)
00233 {
00234 char str[100];
00235 sprintf(str,"insert into file_details values('%s','%s');",logged_user,file);
00236 if(mysql_query (connection1, str)!=0) printf("NOT ADDED TO DATABASE\n");
00237 return 0;
00238 }
00242 char* handle_ip(char* input)
00243 {
00244 char ip[10][100];
00245 int i=0;
00246
00247 if(input[0]=='0')
00248 {
00249 char *result = NULL;
00250 result = strtok(input," ");
00251 printf("Sent:\n");
00252 while(result != NULL)
00253 {
00254 strcpy(ip[i],result);
00255 printf("%s\n",result);
00256 result = strtok(NULL," ");
00257 i++;
00258 }
00259 printf("value returned by user_check is:\n");
00260 printf("%d\n",user_check(ip[1],ip[2]));
00261 if(user_check(ip[1],ip[2])!=-1)
00262 {
00263 logged_in=1;
00264 strcpy(logged_user,ip[1]);
00265 return "success";
00266 }
00267 else return "fail invalid user-id or password";
00268 }
00269
00270 else if(input[0]=='1')
00271 {
00272 char *result = NULL;
00273 result = strtok(input," ");
00274 printf("Sent:\n");
00275 while(result != NULL)
00276 {
00277 strcpy(ip[i],result);
00278 printf("%s\n",ip[i]);
00279 result = strtok(NULL," ");
00280 i++;
00281 }
00282
00283 if(create_account(ip[1],ip[2],ip[3],ip[4],ip[5])!=-1) return "success";
00284 else return "fail User-id already exists";
00285 }
00286
00287 else if(input[0]=='2')
00288 {
00289 char *result = NULL;
00290 result = strtok(input," ");
00291 printf("Sent:\n");
00292 while(result != NULL)
00293 {
00294 strcpy(ip[i],result);
00295 printf("%s\n",result);
00296 result = strtok(NULL," ");
00297 i++;
00298 }
00299 num_of_res=search_for(ip[1]);
00300 return "";
00301 }
00302
00303 else if(input[0]=='3')
00304 {
00305 char *result = NULL;
00306 printf("INPUT:%s\n",input);
00307
00308 result = strtok(input," ");
00309 printf("Sent:\n");
00310 while(result != NULL)
00311 {
00312 strcpy(ip[i],result);
00313 printf("%s\n",result);
00314 result = strtok(NULL," ");
00315 i++;
00316 }
00317 add_to_database(ip[1]);
00318
00319 sprintf(cur_file,"%s/%s",logged_user,ip[1]);
00320 printf("File name...:%s\n",cur_file);
00321 fw=fopen(cur_file,"a");
00322 return "";
00323
00324 }
00325
00326 else if(input[0]=='4')
00327 {
00328 char buffer[strlen(input)-3];
00329 int j=0;
00330 int flag=0;
00331 for(i=2;i<strlen(input)-1;i++)
00332 {
00333 if(input[i]==EOF)
00334 {
00335 flag=1;
00336 break;
00337 }
00338 buffer[j++]=input[i];
00339 }
00340
00341 buffer[j]='\0';
00342 fprintf(fw,"%s",buffer);
00343
00344 fflush(fw);
00345 if(flag==1)
00346 {
00347 fclose(fw);
00348 return "success";
00349 }
00350 }
00351
00352 else if(input[0]=='5')
00353 {
00354 int j=0;
00355 char fname[100];
00356 cur_file[0]='\0';
00357 for(i=2;i<strlen(input);i++)
00358 fname[j++]=input[i];
00359
00360 fname[j]='\0';
00361 printf("File name...:%s\n%d",fname,num_of_res);
00362 for(i=0;i<num_of_res;i++)
00363 {
00364 if(strcmp(search_res[i],fname)==0)
00365 break;
00366 }
00367 if(i==num_of_res) { return "fail"; }
00368
00369 strcat(cur_file,resp_users[i]);
00370 strcat(cur_file,"/");
00371 strcat(cur_file,fname);
00372 return cur_file;
00373 }
00374 else if(input[0]=='6')
00375 {
00376
00377 return "Successfully logged out..\n";
00378 }
00379 else if(input[0]=='7')
00380 {
00381 char file_to_delete[100];
00382 int j=0;
00383 for(i=2;i<strlen(input);i++)
00384 file_to_delete[j++]=input[i];
00385 file_to_delete[j]='\0';
00386 delete_from_database(file_to_delete);
00387
00388 char str[150];
00389 str[0]='\0';
00390 strcat(str,logged_user);
00391 strcat(str,"/");
00392 strcat(str,file_to_delete);
00393 FILE *fpc=fopen(str,"r");
00394 if(fpc==NULL) return "fail No such file..\n";
00395 remove(str);
00396 return "successfully deleted..\n";
00397 }
00398 else if(input[0]=='8')
00399 {
00400 delete_user();
00401 char str[100];
00402 sprintf(str,"rm -r '%s'",logged_user);
00403 system(str);
00404 return "success";
00405 }
00406 return "";
00407 }
00408
00415 int main(int argc, char *argv[])
00416 {
00417 owner_login();
00418
00419 int connection_file_descriptor;
00420 struct sockaddr_in server_address;
00421 int return_value;
00422 struct sigaction act1;
00423 int characters_read;
00424
00425 act1.sa_handler = close_properly;
00426 sigemptyset(&act1.sa_mask);
00427 act1.sa_flags=0;
00428
00429 sigaction(SIGINT, &act1, 0);
00430
00431 listen_file_descriptor = socket(AF_INET, SOCK_STREAM,0);
00432
00433 if(listen_file_descriptor < 0)
00434 {
00435 fprintf(stderr, "%s: cannot open socket.\n", argv[0]);
00436 exit(EXIT_FAILURE);
00437 }
00438
00439 bzero(&server_address, sizeof(server_address));
00440 server_address.sin_family = AF_INET;
00441 server_address.sin_addr.s_addr = htonl(INADDR_ANY);
00442 server_address.sin_port = htons(LISTEN_PORT);
00443 return_value = bind(listen_file_descriptor, (struct sockaddr *) &server_address, sizeof(server_address));
00444 if(return_value < 0)
00445 {
00446 perror("Cannot bind");
00447 exit(EXIT_FAILURE);
00448 }
00449
00450 return_value = listen(listen_file_descriptor, 5);
00451 if(return_value < 0)
00452 {
00453 perror("Cannot listen");
00454 exit(EXIT_FAILURE);
00455 }
00456
00457 while(1)
00458 {
00459 char input_data[BUFFER_SIZE];
00460 connection_file_descriptor = accept(listen_file_descriptor, (struct sockaddr *) NULL, NULL);
00461 if(connection_file_descriptor < 0)
00462 {
00463 perror("accept() failed..");
00464 exit(EXIT_FAILURE);
00465 }
00466 input_data[0]='\0';
00467 characters_read=read(connection_file_descriptor, input_data, BUFFER_SIZE-1);
00468
00469
00470
00471
00472
00473
00474 input_data[characters_read]='\0';
00475 printf("Input:%s\n",input_data);
00476 char* output=handle_ip(input_data);
00477 printf("Output:%s\n",output);
00478
00479 if(input_data[0]=='2')
00480 {
00481 char buffer[BUFFER_SIZE];
00482 if(num_of_res==0)
00483 {
00484 strcpy(buffer,"fail no files matched the pattern\n");
00485 write(connection_file_descriptor, buffer, strlen(buffer));
00486
00487 }
00488 else
00489 {
00490 int i=0;
00491 buffer[0]='\0';
00492 for(i=0;i<num_of_res;i++)
00493 {
00494 strcat(buffer,search_res[i]);
00495 strcat(buffer," ");
00496 }
00497 printf("SENDING:%s\n",buffer);
00498 write(connection_file_descriptor, buffer, strlen(buffer));
00499 }
00500 }
00501 else if((input_data[0]=='5')&&(strcmp(output,"fail")!=0))
00502 {
00503 printf("Opening file....:%s\n",output);
00504 fr=fopen(output,"r");
00505 char ch;
00506 int i=0;
00507 char buffer[BUFFER_SIZE];
00508 while((ch=getc(fr))!=EOF)
00509 {
00510 buffer[i]=ch;
00511 i++;
00512
00513
00514
00515
00516
00517 }
00518 buffer[i]='\0';
00519 write(connection_file_descriptor, buffer, strlen(buffer));
00520
00521
00522 }
00523 else
00524 {
00525 int num=write(connection_file_descriptor, output, strlen(output));
00526 printf("written %d characters..and the string: %s\n",num,output);
00527 }
00528 if(characters_read<0)
00529 {
00530 perror("read failed");
00531 exit(EXIT_FAILURE);
00532 }
00533
00534 return_value = close(connection_file_descriptor);
00535
00536 if(return_value <0)
00537 {
00538 perror("close failed");
00539 exit(EXIT_FAILURE);
00540 }
00541 }
00542 return 0;
00543 }
00544
00545