int HandleQueryPulse(SSL *ssl, cJSON *attr) { if(!attr) return -1; cJSON *child = attr->child; int sid=-1; while(child) { if(0==strcmp(child->string, "sid")) sid = child->valueint; child = child->next; } if(sid==-1) { HandleError(ssl, "Who are you?"); return -1; } SESS_DATA *sess = Session_Find(sid); if(!sess) { HandleError(ssl, "Please login first!"); return -1; } char *username = sess->username; FILE_REQUEST *fr = File_Request_To_Whom(username); if(fr) { char *resp = GenerateFileSendingResp(fr->sid, fr->from, fr->fileName, fr->q, fr->xa_en); SSL_send(ssl, resp, strlen(resp)); free(resp); /*Next is to waiting for the client's confirm*/ return; } //search if there is files need to be delete int fsid = 0; char from[256]= {0}, fileName[256]= {0}; if(DB_FileToDelete_ToWhom(username, &fsid, from, fileName)>0) { char *resp = GenerateFileDeleteResp2(from, fileName); SSL_send(ssl, resp, strlen(resp)); free(resp); //delete file record from database DB_Delete_File_Record(fsid, from, username, fileName); return; } char *resp = "{\n\"cmd\": \"none\"\n}"; SSL_send(ssl, resp, strlen(resp)); }
int HandleLogin(SSL *ssl, cJSON *attr) { if(!ssl || !attr) return -1; char *username=NULL, *password=NULL; cJSON *child = attr->child; while(child) { if(0==strcmp(child->string, "username")) username = child->valuestring; else if(0==strcmp(child->string, "password")) password = child->valuestring; child = child->next; } if(!username || !password) return -1; int status = DB_Login(username, password); int sid=-1; cJSON *respJson; char *respStr=NULL; switch(status) { case -1: /*Unknown Error*/ HandleError(ssl, "Unknown error!"); break; case -2: /*user not exists*/ HandleError(ssl, "User not exists."); break; case -3: /*password wrong*/ HandleError(ssl, "Password is not right."); break; case -4: /*cert status not ready*/ HandleError(ssl, "Certificate is not ready?"); break; case 0: /*correct*/ sid = SSL_get_fd(ssl); /*Add the session into the list*/ sid = Session_Add(sid, username); /*Print all the session data for debug purpose*/ Session_Print_All(); respJson = cJSON_CreateObject(); cJSON_AddStringToObject(respJson, "cmd", "success"); cJSON *attr = cJSON_CreateObject(); cJSON_AddItemToObject(respJson, "attr", attr); cJSON_AddNumberToObject(attr, "sid", sid); respStr = cJSON_Print(respJson); cJSON_Delete(respJson); SSL_send(ssl, respStr, strlen(respStr)); free(respStr); break; default: break; } return 0; }
void HandleRegister(SSL *ssl, cJSON *attr) { printf("Customer want to register.\n"); cJSON *child = attr->child; char *username=NULL, *password=NULL, *email=NULL; while(child) { if(strcmp(child->string, "username")==0) { username = child->valuestring; } else if(strcmp(child->string, "password")==0) { password = child->valuestring; } else if(strcmp(child->string, "email")==0) { email = child->valuestring; } child = child->next; } int status = DB_Check_User(username); char *resp = NULL; if(status < 0) /*Database error*/ { resp = GenerateErrorResp("Database error!"); } else if(status > 0) /*user exists in the database*/ { resp = GenerateErrorResp("Username already taken!"); } else { /*Insert user into database*/ if(DB_Insert_User(username, password, email)) { resp = GenerateErrorResp("Database update error!"); } else { cJSON *cmd = cJSON_CreateObject(); cJSON_AddStringToObject(cmd, "cmd", "get_cert"); resp = cJSON_Print(cmd); cJSON_Delete(cmd); } } SSL_send(ssl, resp, strlen(resp)); free(resp); return; }
/* Write data. Return the number of bytes written or -1 on errors. */ PUBLIC ssize sslWrite(Webs *wp, void *buf, ssize len) { Nano *np; WebsSocket *sp; ssize totalWritten; int rc, count, sent; np = (Nano*) wp->ssl; if (len <= 0) { assert(0); return -1; } if (!np->connected && (rc = nanoHandshake(wp)) <= 0) { return rc; } totalWritten = 0; do { rc = sent = SSL_send(np->handle, (sbyte*) buf, (int) len); logmsg(7, "NanoSSL: written %d, requested len %d", sent, len); if (rc <= 0) { logmsg(0, "NanoSSL: SSL_send failed sent %d", rc); sp = socketPtr(wp->sid); sp->flags |= SOCKET_EOF; return -1; } totalWritten += sent; buf = (void*) ((char*) buf + sent); len -= sent; logmsg(7, "NanoSSL: write: len %d, written %d, total %d", len, sent, totalWritten); } while (len > 0); SSL_sendPending(np->handle, &count); if (count > 0) { socketReservice(wp->sid); } return totalWritten; }
int HandleRevokeFile(SSL *ssl, cJSON *attr) { if(!ssl) return -1; int sid; char *from=NULL, *to=NULL, *filename=NULL; cJSON *child = attr->child; while(child) { if(strcmp(child->string, "sid")==0) { sid = child->valueint; } else if(0==strcmp(child->string, "to")) { to = child->valuestring; } else if(0==strcmp(child->string, "filename")) { filename = child->valuestring; } child = child->next; } SESS_DATA *sess = Session_Find(sid); if(!sess) { HandleError(ssl, "Session not found. Please login first"); return -1; } from = sess->username; int status = DB_RevokeFile(from, to, filename); if(status==0) { printf("Revoke ok!\n"); char *resp = "{\n\"cmd\":\"success\"\n}"; SSL_send(ssl, resp, strlen(resp)); return 0; } else if(status==-1) { printf("There is no file result.\n"); HandleError(ssl, "File records not found."); return -1; } else if(status==-2) { printf("File has been revoked before\n"); HandleError(ssl, "File has been revoked before."); return -1; } else if(status==-3) { printf("File has been deleted before\n"); HandleError(ssl, "File has been deleted before."); return -1; } else if(status==-4) { printf("Database error!\n"); HandleError(ssl, "Server database error."); return -1; } }
int HandleOpenFile(SSL *ssl, cJSON *attr) { if(!ssl) return -1; int sid, fsid; char *from=NULL, *to=NULL, *filename=NULL; cJSON *child = attr->child; while(child) { if(strcmp(child->string, "sid")==0) { sid = child->valueint; } else if(0==strcmp(child->string, "fsid")) { fsid = child->valueint; } else if(0==strcmp(child->string, "from")) { from = child->valuestring; } else if(0==strcmp(child->string, "filename")) { filename = child->valuestring; } child = child->next; } SESS_DATA *sess = Session_Find(sid); if(!sess) { HandleError(ssl, "Session not found. Please login first"); return -1; } to = sess->username; int y = DB_Get_YB(fsid, from, to, filename); char *resp = NULL; if(-1==y) /*File not exists*/ { //HandleError(ssl, "Can't find file records"); resp = GenerateFileDeleteResp(); SSL_send(ssl, resp, strlen(resp)); free(resp); return -1; } else if(-2==y) /*File revoked*/ { //HandleError(ssl, "You can not open this file any more!"); resp = GenerateFileDeleteResp(); SSL_send(ssl, resp, strlen(resp)); free(resp); DB_Delete_File_Record(fsid, from, to, filename); return 0; } else if(-3==y) { HandleError(ssl, "Your file is supposed to be deleted already."); return -1; } else if(-4==y) { HandleError(ssl, "Database error!"); return -1; } else if(y>0) { resp = GenerateFileOpenResp(y); SSL_send(ssl, resp, strlen(resp)); free(resp); return 0; } }
int HandleReceivingFile(SSL *ssl, cJSON *attr) { if(!ssl) return -1; int sid; cJSON *child = attr->child; while(child) { if(strcmp(child->string, "sid")==0) { sid = child->valueint; } child = child->next; } FILE_REQUEST *fr = File_Request_Find(sid); char *from = fr->from; char *filename = fr->fileName; char filepath[512] = {0}; snprintf(filepath, 511,"receiveFiles/%s/%s", from, filename); /*Begin to send file to server*/ printf("Sending file....\n"); char buffer[512]= {0}; FILE *file = fopen(filepath, "r"); if(!file) { printf("Can't open file [%s].\n", filepath); return -1; } int sendLen=0, len; while(!feof(file)) { len = fread(buffer, sizeof(char), 511, file); if(len<=0) break; buffer[len] = '\0'; sendLen = SSL_send(ssl, buffer, len); if(sendLen<len) break; } SSL_send(ssl, "!@done*#", 8); printf("Done.\n"); fclose(file); /*Record the key information into the database*/ if(0!=DB_Record_File_Info(fr)) { printf("Database error!\n"); return -1; } /*Delete file request */ if(0!=File_Request_Delete(fr->sid)) { printf("File request delete failed!\n"); return -1; } File_Request_Print_All(); /*Delete file from server end*/ remove(filepath); return 0; }
int HandleSendingFile(SSL *ssl, cJSON *attr) { if(!ssl) return -1; int sid; char *xa_en=NULL; cJSON *child = attr->child; while(child) { if(strcmp(child->string, "sid")==0) { sid = child->valueint; } else if(strcmp(child->string, "x_en")==0) { xa_en = child->valuestring; } child = child->next; } printf("sid:%d; xa_en length:%d;\n", sid, (int)strlen(xa_en)); FILE_REQUEST *fr = File_Request_Find(sid); if(!fr) { HandleError(ssl, "Can not find file request session!"); return -1; } fr->xa_en = (char*)malloc(strlen(xa_en)+1); bzero(fr->xa_en, strlen(xa_en)+1); memcpy(fr->xa_en, xa_en, strlen(xa_en)); char *resp = "{\n\"cmd\": \"waiting_to_receive\"\n}"; SSL_send(ssl, resp, strlen(resp)); char filePath[512]= {0}; char dir[256]= {0}; snprintf(dir, 255, "receiveFiles/%s", fr->from); if(0>access(dir, F_OK)) mkdir(dir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); snprintf(filePath,255, "%s/%s", dir, fr->fileName); FILE *f = fopen(filePath, "w"); if(!f) printf("File [%s] can not open!\n", fr->fileName); char buffer[512]; int len = 0; printf("Receive file from client [%s]...", fr->from); while(1) { len = SSL_recv(ssl,buffer, 511); if(len <=0) break; buffer[len] = '\0'; if(0==strcmp("!@done*#",buffer )) break; else { if(f) fwrite(buffer, sizeof(char), len, f); } } if(!f) return -1; fclose(f); printf("done.\n"); fr->ready = 1; return 0; }
void HandleError(SSL *ssl, char *errStr) { char *resp = GenerateErrorResp(errStr); SSL_send(ssl, resp, strlen(resp)); free(resp); }
int HandleFileQuery(SSL *ssl, cJSON *attr) { cJSON *child = attr->child; char *to=NULL, *filename=NULL; int q, a, sid; while(child) { if(strcmp(child->string, "sid")==0) { sid = child->valueint; } else if(strcmp(child->string, "to")==0) { to = child->valuestring; } else if(strcmp(child->string, "q")==0) { q = child->valueint; } else if(strcmp(child->string, "a")==0) { a = child->valueint; } else if(strcmp(child->string, "filename")==0) { filename = child->valuestring; } child = child->next; } /*Check whether the to user exists.*/ int status = DB_Check_User(to); if(status < 0) { HandleError(ssl, "Unknown error from server database."); return -1; } else if(status==0) { HandleError(ssl, "User not exists!"); return -1; } /*Check if the user has logined in*/ const SESS_DATA *sess = Session_Find(sid); if(sess==NULL) { HandleError(ssl, "Please login first!"); return -1; } const char *from = sess->username; FILE_REQUEST *fr = File_Request_Add((char*)from, to, filename, q, a); if(!fr) { HandleError(ssl, "File request can not be handled."); return -1; } File_Request_Print_All(); fflush(NULL); char *resp = GenerateFileRequestResp(fr->sid, fr->y); SSL_send(ssl, resp, strlen(resp)); free(resp); return 0; }
void HandleClientMsg(SSL_CLIENT_DATA* ssl_data, int epollfd) { if(!ssl_data) return; SSL *ssl = ssl_data->ssl; char buffer[BUFF_LEN]; int recvLen; bzero(buffer, BUFF_LEN); recvLen = SSL_recv(ssl, buffer, BUFF_LEN); if(recvLen <= 0 || strncmp(buffer, "quit", 4)==0) { printf("client quit!\n"); SSL_Client_Leave(ssl_data, epollfd); return; } /*Parse the user cert request*/ cJSON *root = cJSON_Parse(buffer); if(!root) { HandleError(ssl, "JSON parse error!"); goto end; } cJSON *child = root->child, *cmd=NULL, *attr=NULL; while(child) { if(0==strcmp(child->string, "cmd")) cmd = child; else if(0==strcmp(child->string, "attr")) attr = child; child = child->next; } printf("Receive from client %d: %s\n", SSL_get_fd(ssl_data->ssl), buffer); if(0==strcmp(cmd->valuestring, "get_cert")) { char *certFile = ProduceNewCertForUser(attr); printf("certfile:%s;\n", certFile); fflush(NULL); /*Generate the response text*/ char *resp = NULL; FILE *file = fopen(certFile, "r"); if(!file) { HandleError(ssl, "Certificate generate failed with unknown error!\n"); }else{ fseek(file, 0L, SEEK_END); if(0==ftell(file)) { HandleError(ssl, "Certificate generate failed with unknown error!\n"); fclose(file); remove(certFile); }else{ /*Calculate. Remove all the path prefix, leave the pure file name*/ char *fileName = certFile, *ret=NULL; ret = strtok(certFile, "/"); while(ret) { fileName = ret; ret = strtok(NULL, "/"); } cJSON *resp_json = cJSON_CreateObject(); cJSON_AddStringToObject(resp_json, "cmd", "sending_cert_next"); cJSON *attr = cJSON_CreateObject(); cJSON_AddItemToObject(resp_json, "attr", attr); cJSON_AddStringToObject(attr, "filename", fileName); char *resp = cJSON_Print(resp_json); cJSON_Delete(resp_json); SSL_send(ssl, resp, strlen(resp)); free(resp); /*Next we must send the cert file to the client*/ rewind(file); char buffer[512]={0}; int len = 0, sendLen=0; while(!feof(file)) { printf("Sending cert....\n"); len = fread(buffer, sizeof(char), 511, file); if(len<=0) break; buffer[len] = '\0'; sendLen = SSL_send(ssl, buffer, len); if(sendLen<len) break; } SSL_send(ssl, "!@done*#", 8); fclose(file); } } free(certFile); }else if(0==strcmp(cmd->valuestring, "pubkey_query")) { /*get the username*/ char *username = NULL; if(0==strcmp(attr->child->string, "username")) username = attr->child->valuestring; if(!username) { HandleError(ssl, "You must specify the user name!"); return; } /*Generate the response text*/ char certFile[512]; strcpy(certFile, "certs/client/"); strcat(certFile, username); strcat(certFile, "Pub.pem"); char *resp = NULL; FILE *file = fopen(certFile, "r"); if(!file) { HandleError(ssl, "your requested file not exists!\n"); }else{ fseek(file, 0L, SEEK_END); if(0==ftell(file)) { HandleError(ssl, "your requested file not exists!\n"); fclose(file); remove(certFile); }else{ /*Calculate. Remove all the path prefix, leave the pure file name*/ char *fileName = basename(certFile), *ret=NULL; cJSON *resp_json = cJSON_CreateObject(); cJSON_AddStringToObject(resp_json, "cmd", "sending_pubkey_next"); cJSON *attr = cJSON_CreateObject(); cJSON_AddItemToObject(resp_json, "attr", attr); cJSON_AddStringToObject(attr, "filename", fileName); char *resp = cJSON_Print(resp_json); cJSON_Delete(resp_json); SSL_send(ssl, resp, strlen(resp)); free(resp); /*Next we must send the cert file to the client*/ rewind(file); char buffer[512]={0}; int len = 0, sendLen=0; while(!feof(file)) { printf("Sending public key....\n"); len = fread(buffer, sizeof(char), 511, file); if(len<=0) break; buffer[len] = '\0'; sendLen = SSL_send(ssl, buffer, len); if(sendLen<len) break; } SSL_send(ssl, "!@done*#", 8); printf("Done.\n"); fclose(file); } } } end: cJSON_Delete(root); }