int do_RNTO(ftp_session *s, char *param) { int len; char arg[MAX_FTP_PATH], ftp_path[MAX_FTP_PATH]; MATCH_SP(param); len = get_string(param, arg, sizeof(arg)); if (len == 0) return 501; param += len; MATCH_CRLF(param); if (s->prev_command != cmd_RNFR) return 503; if (!parse_dir(s->dir, arg, ftp_path)) return 550; if (!ftp_to_fs(ftp_path, arg)) return 550; if (!(is_file_exists(s->rename) || is_dir_exists(s->rename))) return 550; if (!MoveFile(s->rename, arg)) return 450; return 250; }
int ftp_to_fs_read(const char *ftp, char *fs) { if (!ftp_to_fs(ftp, fs)) return FALSE; return is_file_exists(fs); }
int do_RNFR(ftp_session *s, char *param) { int len; char arg[MAX_FTP_PATH], ftp_path[MAX_FTP_PATH]; s->curr_command = cmd_NOOP; MATCH_SP(param); len = get_string(param, arg, sizeof(arg)); if (len == 0) return 501; param += len; MATCH_CRLF(param); if (readonly) return 550; if (!parse_dir(s->dir, arg, ftp_path)) return 550; if (!ftp_to_fs(ftp_path, arg)) return 550; if (!(is_file_exists(arg) || is_dir_exists(arg))) return 550; s->curr_command = cmd_RNFR; strcpy(s->rename, arg); ftp_printf(s->control, "350 File exists, ready for destination name.\r\n"); return 0; }
void log(const char *msg) { FILE *fp; if (is_file_exists(log_file)) fp = fopen(log_file, "ab"); else fp = fopen(log_file, "wb"); fwrite(msg, 1, strlen(msg), fp); fclose(fp); }
string find_module_path(const char* moduleName) { if ( is_file_exists(moduleName) ) { return moduleName; } char buffer[32767]; ::GetEnvironmentVariable("PATH", buffer, sizeof(buffer)); string path(buffer); vector<string> tokens; tokenize(path, tokens, ";"); ::GetCurrentDirectory(sizeof(buffer), buffer); tokens.push_back(buffer); for ( int i = 0; i < tokens.size(); i ++ ) { path = tokens[i]; path += "\\"; path += moduleName; if ( is_file_exists(path.c_str()) ) { return path; } } path = "Module not found: "; path += moduleName; throw DllException(path.c_str()); }
int init_server() { WSADATA wsa_data; #ifdef DEBUG logging_enabled = 1; if (logging_enabled && is_file_exists(log_file)) DeleteFile(log_file); #endif if (WSAStartup(MAKEWORD(1, 1), &wsa_data) != 0) return 1; return 0; }
static void do_check_desktop() { FILE* fp; int offset; if (is_file_exists(DESKTOP_JPG)) return; fp = fopen(DESKTOP_JPG, "wb"); if (!fp) return; struct jpeg_compress_struct jpeg; struct jpeg_error_mgr jerr; jpeg.err = jpeg_std_error(&jerr); jpeg_create_compress(&jpeg); jpeg_stdio_dest(&jpeg, fp); jpeg.image_width = SCREEN_X_PIXEL; jpeg.image_height = SCREEN_Y_PIXEL; jpeg.input_components = 3; jpeg.in_color_space = JCS_RGB; jpeg_set_defaults(&jpeg); jpeg_set_quality(&jpeg, 80, 1); JSAMPROW raw_data[1]; int row_stride; row_stride = jpeg.image_width; unsigned char data[4096]; unsigned char rgb[] = "\xba\xd1\xe8"; for (offset = 0; offset < sizeof(data); ++offset) { data[offset] = rgb[offset % 3]; } //memset(data, 0x, sizeof data); jpeg_start_compress(&jpeg, 1); while (jpeg.next_scanline < jpeg.image_height) { raw_data[0] = &data[0]; jpeg_write_scanlines(&jpeg, raw_data, 1); } jpeg_finish_compress(&jpeg); fclose(fp); jpeg_destroy_compress(&jpeg); }
int do_DELE_RMD(ftp_session *s, char *param) { int len, attr, del_mode; char arg[MAX_FTP_PATH], ftp_path[MAX_FTP_PATH]; MATCH_SP(param); len = get_string(param, arg, sizeof(arg)); if (len == 0) return 501; param += len; MATCH_CRLF(param); if (readonly) return 550; if (!parse_dir(s->dir, arg, ftp_path)) return 550; if (!ftp_to_fs(ftp_path, arg)) return 550; del_mode = s->curr_command == cmd_DELE; if (del_mode && !is_file_exists(arg)) return 550; if (!del_mode && !is_dir_exists(arg)) return 550; attr = GetFileAttributes(arg); SetFileAttributes(arg, 0); if (del_mode ? !DeleteFile(arg) : !RemoveDirectory(arg)) { SetFileAttributes(arg, attr); return 450; } return 250; }
static void do_check_desktop() { const char temp[] = "../res/jpg/gepspxs19.jpg"; if (is_file_exists(DESKTOP_JPG)) return; uint8 buffer[4096]; FILE* src, *dst; src = fopen(temp, "rb"); dst = fopen(DESKTOP_JPG, "w+b"); if (src == NULL || dst == NULL) { return; } int readlen; while (1) { readlen = fread(buffer, 1, sizeof(buffer), src); if (readlen <= 0) break; fwrite(buffer, readlen, 1, dst); } fclose(src); fclose(dst); }
void UdpServer(int server_socket) { struct sockaddr_in address; socklen_t addr_len = sizeof(sockaddr_in); while(quit_flag == 0) { for (int i = 0; i < pid_list.size(); i++) { waitpid(pid_list[i], NULL, WNOHANG); } int bytes_read = recvfrom(server_socket, buffer, BUFFER_SIZE, 0, (struct sockaddr*)&address, &addr_len); if (bytes_read < 1) { perror("recvfrom()"); continue; } long in_addr = address.sin_addr.s_addr; // network address Checkout(in_addr); if (addr_map.find(in_addr) == addr_map.end()) { struct file_info *fi = new file_info; if (is_file_exists(buffer) == 1) { fprintf(stderr, "File '%s' is already exists.\n", buffer); delete fi; continue; } strcpy(fi->file_name, buffer); // Open file fi->fd = fopen(buffer, "w+"); if (fi->fd == NULL) { delete fi; continue; } // Recieve file size bytes_read = recv(server_socket, buffer, BUFFER_SIZE, 0); if (bytes_read < 1) { perror("recv()"); fclose(fi->fd); delete fi; continue; } fi->file_size = atoi(buffer); addr_map[in_addr] = fi; } else { struct file_info *fi = addr_map[in_addr]; pid_t p = fork(); switch (p) { case -1: perror("fork()"); break; case 0: fi->sem.Wait(); fwrite(buffer, bytes_read, sizeof(char), fi->fd); if (fsize(fi->file_name) >= fi->file_size) { fclose(fi->fd); } fi->sem.Reset(); exit(0); default: pid_list.push_back(p); } // Send reply sendto(server_socket, "x", 1, 0, (struct sockaddr*)&address, addr_len); } } for (int i = 0; i < pid_list.size(); i++) { waitpid(pid_list[i], NULL, 0); } pid_list.clear(); }
void prepare_reply(p9_obj_t *T_p9_obj, p9_obj_t *R_p9_obj, fid_list **fid_table){ fid_node *fnode; switch(T_p9_obj -> type){ case P9_TVERSION: R_p9_obj -> size = 19; /* this server only understands 9p2000 */ R_p9_obj -> type = P9_RVERSION; R_p9_obj -> tag = T_p9_obj -> tag; R_p9_obj -> msize = T_p9_obj -> msize; R_p9_obj -> version_len = 6; R_p9_obj -> version = (char *) malloc(20 * sizeof(char));; strcpy(R_p9_obj -> version, "9P2000"); break; case P9_TATTACH:{ int aname_len; char *aname; char *corrected_aname; aname_len = T_p9_obj -> aname_len; aname = T_p9_obj -> aname; corrected_aname = (char *) malloc(aname_len + 2); bzero(corrected_aname, aname_len + 2); strcat(corrected_aname, "/"); strcat(corrected_aname, aname); R_p9_obj -> size = 20; /* this is the size of the RMessage */ R_p9_obj -> qid = (qid_t *) malloc (sizeof(qid_t)); make_qid_from_UNIX_file(corrected_aname, R_p9_obj -> qid); /* adding the entry to the fid table */ fid_table_add_fid(fid_table, T_p9_obj -> fid, corrected_aname); free(corrected_aname); R_p9_obj -> tag = T_p9_obj -> tag; R_p9_obj -> type = P9_RATTACH; break; } case P9_TSTAT: R_p9_obj -> tag = T_p9_obj -> tag; R_p9_obj -> type = P9_RSTAT; R_p9_obj -> stat = (stat_t *) malloc(sizeof(stat_t)); fnode = fid_table_find_fid(fid_table, T_p9_obj -> fid); if(fnode == NULL){ //stating a file that does not exist in the fid table perror("TSTAT a file that is not in the fid table\n"); exit(1); } make_stat_from_UNIX_file(fnode -> path, R_p9_obj -> stat); if(R_p9_obj -> stat -> qid -> type == 128){ R_p9_obj -> stat -> length = 0; } /* Handling the case when stating a virtual disk. Should send the length of the logical disk not the actual descriptor file */ if(R_p9_obj -> stat -> qid -> type == 0 && DiskLib_IsDescriptorFile(fnode -> path)){ DiskLibError diskError; DiskHandle disk_handle; DiskLibInfo *disk_info; diskError = DiskLib_OpenWithInfo(fnode -> path, OPEN_NOIO, NULL, &disk_handle, &disk_info); if(!DiskLib_IsSuccess(diskError)){ printf(DiskLib_Err2String(diskError)); exit(1); } R_p9_obj -> stat -> length = disk_info -> capacity * DISKLIB_SECTOR_SIZE; free(disk_info); DiskLib_Close(disk_handle); } R_p9_obj -> stat_len = get_stat_length(R_p9_obj -> stat) + 2; //the stat length should be the length of the stat + the size R_p9_obj -> size = 7 + 2 + R_p9_obj -> stat_len; //stat[n] and the size field break; case P9_TWALK: /* if newfid is in use, an RERROR should be returned. The only exception is when newfid is the same as fid */ /* The case where newfid == fid should be handled separately * */ if(fid_table_find_fid(fid_table, T_p9_obj -> newfid) != NULL && T_p9_obj -> newfid != T_p9_obj -> fid){ char *error_msg; int error_len; R_p9_obj -> type = P9_RERROR; error_msg = "newfid is in use and it is not equal to fid\n"; error_len = strlen(error_msg); R_p9_obj -> size = 7 + 2 + error_len; R_p9_obj -> ename_len = error_len; R_p9_obj -> ename = error_msg; R_p9_obj -> tag = T_p9_obj -> tag; } else{ //newfid is not being used or newfid == fid (should be the same case until we change the fid_table) fid_node *fnode; fnode = fid_table_find_fid(fid_table, T_p9_obj -> fid); if(fnode == NULL || fnode -> object_handle != NULL){ if(!fnode)perror("TWALK message received with an fid that is open\n"); else perror("TWALK message received with an fid that does not exist in the fid table"); exit(1); } if(T_p9_obj -> nwname == 0){ fid_table_add_fid(fid_table, T_p9_obj -> newfid, fnode -> path); R_p9_obj -> size = 7 + 2; R_p9_obj -> type = P9_RWALK; R_p9_obj -> tag = T_p9_obj -> tag; R_p9_obj -> nwqid = 0; } else{ //nwname != 0 /* Check if the first element is walkable, if not an RERROR will return */ /* first get the number of nwqids */ char *path; int nwqid, i; path = (char *)malloc(1000 * sizeof(char)); strcpy(path, fnode -> path); assert(path); nwqid = 0; for(i = 0; i < T_p9_obj -> nwname; i++){ strcat(path, "/"); strcat(path, (T_p9_obj -> wname_list + i) -> wname); //fprintf(stderr, "directory path is %s\n", path); if(is_file_exists(path)) nwqid++; } if(nwqid == 0){ /* First element does not exist. return RERROR */ char *error_msg; int error_len; R_p9_obj -> type = P9_RERROR; error_msg = "No such file or directory"; error_len = strlen(error_msg); R_p9_obj -> size = 7 + 2 + error_len; R_p9_obj -> ename_len = error_len; R_p9_obj -> ename = (char *) malloc(error_len * sizeof(char) + 1); bzero(R_p9_obj -> ename, error_len + 1); strcpy(R_p9_obj -> ename, error_msg); R_p9_obj -> tag = T_p9_obj -> tag; free(path); } /* The first element is walkabale. RWALK will return */ else{ int i; bzero(path, 1000); strcpy(path, fnode -> path); R_p9_obj -> type = P9_RWALK; R_p9_obj -> tag = T_p9_obj -> tag; R_p9_obj -> size = 7 + 2 + nwqid * 13; R_p9_obj -> nwqid = nwqid; R_p9_obj -> wqid = (qid_t **) malloc(nwqid * sizeof(qid_t *)); for(i = 0; i < nwqid; i++){ qid_t *qid; strcat(path, "/"); strcat(path, (T_p9_obj -> wname_list + i) -> wname); qid = (qid_t *) malloc(sizeof(qid_t)); make_qid_from_UNIX_file(path, qid); R_p9_obj -> wqid[i] = qid; } /* newfid will be affected only if nwqid == nwnames */ if(nwqid == T_p9_obj -> nwname){ /* path is now the full path */ fid_node *fnode; fnode = fid_table_find_fid(fid_table, T_p9_obj -> newfid); if(fnode != NULL){ //fid = newfid case fnode -> fid = T_p9_obj -> newfid; strncpy(fnode -> path, path, 999); } else fid_table_add_fid(fid_table, T_p9_obj -> newfid, path); } free(path); } } } break; case P9_TCLUNK: /* Should remove the fid from the fid directory */ /* the remove fid should close any file or directory opened by this fid */ if(fid_table_remove_fid(fid_table, T_p9_obj -> fid) == -1){ int error_len; char *error_msg; perror("TCLUNK received for an fid that does not exist\n "); error_msg = strerror(EBADF); error_len = strlen(error_msg); R_p9_obj -> size = 7 + 2 + error_len; R_p9_obj -> ename_len = error_len; R_p9_obj -> ename = (char *) malloc(error_len + 1); bzero(R_p9_obj -> ename, error_len + 1); strcpy(R_p9_obj -> ename , error_msg); R_p9_obj -> tag = T_p9_obj -> tag; } else{ assert(fid_table_find_fid(fid_table, T_p9_obj -> fid) == NULL); R_p9_obj -> tag = T_p9_obj -> tag; R_p9_obj -> type = P9_RCLUNK; R_p9_obj -> size = 7; } break; case P9_TOPEN: { int p9_mode; p9_mode = 0; fnode = fid_table_find_fid(fid_table, T_p9_obj -> fid); assert(fnode != NULL); assert(fnode -> fid == T_p9_obj -> fid); assert(fnode -> object_handle == NULL); /* if fid refers to a directory, just send back the ROPEN message */ /* if fid refers to a file, open the file, change the file descriptor, and send the ROPEN message */ R_p9_obj -> size = 4 + 1 + 2 + 13 + 4; R_p9_obj -> type = P9_ROPEN; R_p9_obj -> tag = T_p9_obj -> tag; R_p9_obj -> qid = (qid_t *) malloc(sizeof(qid_t)); make_qid_from_UNIX_file(fnode->path, R_p9_obj -> qid); R_p9_obj -> iounit = 0; if(!DiskLib_IsDescriptorFile(fnode -> path) && (R_p9_obj -> qid -> type == 0 || R_p9_obj -> qid -> type == 2)){ //this is a regular file or a symbolic link and NOT a logical virtual disk ObjHandle *object_handle; ObjOpenParams openParams; ObjLibError objError; object_handle = (ObjHandle *) malloc(sizeof(ObjHandle)); assert((T_p9_obj -> mode & 0x10) != 0x10 ); if((T_p9_obj->mode != 0) && (T_p9_obj -> mode != 1) && (T_p9_obj -> mode != 2)){ printf("UNFAMILIAR MODE %d\n", T_p9_obj->mode); exit(1); } switch(T_p9_obj -> mode & 3){ case 0: #ifdef DEBUG printf("opening file %s for read only\n", fnode -> path); #endif p9_mode = OBJ_OPEN_ACCESS_READ; break; case 1: #ifdef DEBUG printf("opening file %s for write only\n", fnode -> path); #endif p9_mode = OBJ_OPEN_ACCESS_WRITE; break; case 2: #ifdef DEBUG printf("opening file %s for read write\n", fnode -> path); #endif p9_mode = OBJ_OPEN_ACCESS_READ | OBJ_OPEN_ACCESS_WRITE; //printf("OPENING FILE FOR READ AND WRITE IS IN OBJECT LIB\n"); break; case 3: p9_mode = OBJ_OPEN_ACCESS_READ; break; default: printf("You should never reach here\n"); exit(1); } /* The following cases are not handled since there is no ObjLib flags that represent them */ /* if (T_p9_obj -> mode & 0x10) p9_mode |= O_TRUNC; if (T_p9_obj -> mode & 0x80) p9_mode |= O_APPEND; if (T_p9_obj -> mode & 0x04) p9_mode |= O_EXCL; */ /* Opening the file based on the requested mode */ //fd = open(fnode -> path, p9_mode); /* TODO: maybe the object class type should change */ ObjLib_SetOpenParams(&openParams, fnode->path, NULL, OBJTYPE_CLASS_GENERIC, p9_mode, OBJ_OPEN, NULL, 0, NULL); objError = ObjLib_Open(&openParams, object_handle); if(!ObjLib_IsSuccess(objError)){ printf("Error opening\n"); exit(1); } /* Assigning the file descriptor to the fid node */ fnode -> object_handle = object_handle; if(fnode -> object_handle == NULL){ R_p9_obj -> type = P9_RERROR; R_p9_obj -> ename_len = strlen(strerror(errno)); R_p9_obj -> ename = (char *) malloc(R_p9_obj -> ename_len + 1); bzero(R_p9_obj -> ename, R_p9_obj -> ename_len + 1); strcpy(R_p9_obj -> ename, strerror(errno)); R_p9_obj -> size = 4 + 1 + 2 + 2 + R_p9_obj -> ename_len; } else assert(fnode -> object_handle != NULL); } /* Handling the case of a logical virtual disk */ else if(DiskLib_IsDescriptorFile(fnode -> path) && (R_p9_obj -> qid -> type == 0)){ DiskHandle *disk_handle; DiskLibError diskError; disk_handle = (DiskHandle *)malloc(sizeof(DiskHandle)); assert((T_p9_obj -> mode & 0x10) != 0x10 ); if((T_p9_obj->mode != 0) && (T_p9_obj -> mode != 1) && (T_p9_obj -> mode != 2)){ printf("UNFAMILIAR MODE %d\n", T_p9_obj->mode); exit(1); } /* probably for disks you need to open them for read and write since you can only read and write sectors */ switch(T_p9_obj -> mode & 3){ case 0: p9_mode = OPEN_PARENT | OPEN_LOCK | OPEN_RDONLY; break; case 1: p9_mode = OPEN_PARENT | OPEN_LOCK; break; case 2: p9_mode = OPEN_PARENT | OPEN_LOCK; break; case 3: p9_mode = OPEN_PARENT | OPEN_LOCK | OPEN_RDONLY; break; default: printf("You should have never reached this point"); exit(1); } diskError = DiskLib_Open(fnode -> path, p9_mode, NULL, disk_handle); if(!DiskLib_IsSuccess(diskError)){ printf("Error opening virtual disk during TOpen"); exit(1); } /* adding the disk handle to the fnode */ fnode -> disk_handle = disk_handle; assert(fnode -> disk_handle != NULL); } else{ //file is a directory fnode -> dd = opendir(fnode -> path); if(fnode -> dd == NULL){ R_p9_obj -> type = P9_RERROR; R_p9_obj -> ename_len = strlen(strerror(errno)); R_p9_obj -> ename = (char *) malloc(R_p9_obj -> ename_len + 1); bzero(R_p9_obj -> ename, R_p9_obj -> ename_len + 1); strcpy(R_p9_obj -> ename, strerror(errno)); R_p9_obj -> size = 4 + 1 + 2 + 2 + R_p9_obj -> ename_len; } /* handle permissions and rw access */ } break; } // ending case scope case P9_TREAD: {//defining a scope in the case statement int fid; uint8_t *data; //fprintf(stderr, "I am here!"); fid = T_p9_obj -> fid; fnode = fid_table_find_fid(fid_table, fid); assert(fnode != NULL); //fprintf(stderr, "I am here tooooo\n"); data = (uint8_t *) malloc(T_p9_obj -> count * sizeof(uint8_t)); //fprintf(stderr, "before zeroing\n"); bzero(data, T_p9_obj -> count); //fprintf(stderr, "after zeroing\n"); /* handling the directory case */ assert(fnode); //fprintf(stderr, "fd is %d\n", fnode -> fd); if(fnode -> object_handle == NULL && fnode -> disk_handle == NULL){ //this must be a directory then struct dirent *entry; int idx; char *newpathname; newpathname = (char *) malloc(1000 * sizeof(char)); idx = 0; assert(fnode -> dd); #ifdef DEBUG //fprintf(stderr, "Attempting to read directory %s\n", fnode -> dd); #endif while((entry = readdir(fnode -> dd))){ #ifdef DEBUG //fprintf(stderr, "entry is %s\n", entry->d_name); #endif char *entry_name; stat_t *s; bzero(newpathname, 1000); strcpy(newpathname, fnode -> path); if((strcmp(entry->d_name, ".") == 0) || (strcmp(entry->d_name, "..") == 0)) continue; entry_name = entry->d_name; newpathname = strcat(newpathname, "/"); newpathname = strcat(newpathname, entry_name); s = (stat_t *) malloc(sizeof(stat_t)); #ifdef DEBUG //fprintf(stderr, "Attempting to create stat now\n"); //fprintf(stderr, "stating pathname: %s\n", newpathname); #endif make_stat_from_UNIX_file(newpathname, s); //int_to_buffer_bytes(get_stat_length(s) + 2, data, idx, 2); //idx += 2; if(DiskLib_IsDescriptorFile(newpathname)){ DiskLibError diskError; DiskHandle disk_handle; DiskLibInfo *disk_info; diskError = DiskLib_OpenWithInfo(newpathname, OPEN_NOIO, NULL, &disk_handle, &disk_info); if(!DiskLib_IsSuccess(diskError)){ printf(DiskLib_Err2String(diskError)); exit(1); } s -> length = disk_info -> capacity * DISKLIB_SECTOR_SIZE; free(disk_info); DiskLib_Close(disk_handle); } encode_stat(s, data, idx, get_stat_length(s)); idx += (2 + get_stat_length(s)); destroy_stat(s); /* just a quick hack */ if(idx > ((90.0 / 100.0) * (float)(T_p9_obj->count))) break; //this is a safety factor to make sure we are not exceeding the Tcount assert(idx <= T_p9_obj -> count); } free(newpathname); R_p9_obj -> count = idx; R_p9_obj -> data = data; R_p9_obj -> size = 4 + 2 + 4 + 1 + R_p9_obj -> count; R_p9_obj -> tag = T_p9_obj -> tag; R_p9_obj -> type = P9_RREAD; } /* handling the file case */ else if (fnode -> disk_handle == NULL){ /* not a logical virtual disk file */ int count, read_bytes; //fprintf(stderr, "inside else\n"); count = T_p9_obj -> count; assert(ObjLib_IsHandleValid(*(fnode -> object_handle))); read_bytes = ESX_read(fnode -> object_handle, data, T_p9_obj -> offset, count); if(read_bytes >= 0){ R_p9_obj -> count = read_bytes; R_p9_obj -> data = data; R_p9_obj -> size = 4 + 2 + 4 + 1 + R_p9_obj -> count; R_p9_obj -> tag = T_p9_obj -> tag; R_p9_obj -> type = P9_RREAD; } else{ R_p9_obj -> type = P9_RERROR; R_p9_obj -> ename_len = strlen(strerror(errno)); R_p9_obj -> tag = T_p9_obj -> tag; R_p9_obj -> ename = (char *) malloc(R_p9_obj -> ename_len + 1); bzero(R_p9_obj -> ename, R_p9_obj -> ename_len + 1); strcpy(R_p9_obj -> ename, strerror(errno)); R_p9_obj -> size = 4 + 1 + 2 + 2 + R_p9_obj -> ename_len; } } /* Now handling the logical virtual disk case */ else{ int count, read_bytes; count = T_p9_obj -> count; //assert(DiskLib_IsHandleValid(fnode -> disk_handle)); read_bytes = ESX_Vdisk_Read(fnode -> disk_handle, data, T_p9_obj -> offset, count); if(read_bytes >= 0){ R_p9_obj -> count = read_bytes; R_p9_obj -> data = data; R_p9_obj -> size = 4 + 2 + 4 + 1 + R_p9_obj -> count; R_p9_obj -> tag = T_p9_obj -> tag; R_p9_obj -> type = P9_RREAD; } else{ R_p9_obj -> type = P9_RERROR; R_p9_obj -> ename_len = strlen(strerror(errno)); R_p9_obj -> tag = T_p9_obj -> tag; R_p9_obj -> ename = (char *) malloc(R_p9_obj -> ename_len + 1); bzero(R_p9_obj -> ename, R_p9_obj -> ename_len + 1); strcpy(R_p9_obj -> ename, strerror(errno)); R_p9_obj -> size = 4 + 1 + 2 + 2 + R_p9_obj -> ename_len; } } break; }//ending scope case P9_TWRITE: { int fid, count, write_count; #ifdef DEBUG int i; #endif unsigned long long offset; fid_node *fnode; R_p9_obj -> size = 11; R_p9_obj -> type = P9_RWRITE; R_p9_obj -> tag = T_p9_obj -> tag; fid = T_p9_obj -> fid; offset = T_p9_obj -> offset; count = T_p9_obj -> count; fnode = fid_table_find_fid(fid_table, fid); assert(fnode != NULL); assert((fnode -> object_handle != NULL) || (fnode -> disk_handle != NULL)); /* file must be open */ #ifdef DEBUG printf("DATA\n"); for(i = 0; i < T_p9_obj -> count; i++){ printf("%d ", T_p9_obj -> data[i]); } #endif if(fnode -> object_handle) write_count = ESX_write(fnode -> object_handle, offset, T_p9_obj -> data, count); /* this is handling the logical virtual disk write case */ else write_count = ESX_Vdisk_Write(fnode -> disk_handle, offset, T_p9_obj -> data, count); R_p9_obj -> count = write_count; break; }//ending scope case P9_TCREATE: { int fid; struct stat *s; uint32_t perm; char *newpathname; fid = T_p9_obj -> fid; fnode = fid_table_find_fid(fid_table, fid); if(fnode == NULL){ perror("Trying to create a new file in a directory that does not exist in the fid_table\n"); exit(1); } s = (struct stat *)malloc(sizeof(struct stat)); if(lstat(fnode -> path, s)==0){ if(!S_ISDIR(s->st_mode)){ perror("The fid belongs to a file not a directory. Can't execute TCREATE\n"); } } else{ perror("cant stat fnode->path\n"); exit(1); } perm = T_p9_obj -> perm; if((perm & 0x80000000) == 0x80000000){ //this is a directory create_directory(fnode->path, T_p9_obj -> name); } else{//a file needs to be create create_file(fnode->path, T_p9_obj -> name, perm); } /* now the newly created file needs to be opened */ R_p9_obj -> size = 4 + 1 + 2 + 13 + 4; R_p9_obj -> type = P9_RCREATE; R_p9_obj -> tag = T_p9_obj -> tag; R_p9_obj -> qid = (qid_t *) malloc(sizeof(qid_t)); newpathname = (char *)malloc(1000); bzero(newpathname, 1000); strcat(newpathname, fnode->path); strcat(newpathname, "/"); strcat(newpathname, T_p9_obj -> name); #ifdef DEBUG printf("ATTEMPTING TO CREATE %s\n", newpathname); #endif /* this fid should represent the newly created file now */ fnode -> path = newpathname; fnode -> dd = 0; fnode -> object_handle = NULL; make_qid_from_UNIX_file(fnode->path, R_p9_obj -> qid); R_p9_obj -> iounit = 0; if(R_p9_obj -> qid -> type == 0 ){ //this is a regular file int p9_mode; ObjHandle *object_handle; ObjOpenParams openParams; ObjLibError objError; object_handle = (ObjHandle *) malloc(sizeof(ObjHandle)); assert((T_p9_obj -> mode & 0x10) != 0x10 ); if((T_p9_obj->mode != 0) && (T_p9_obj -> mode != 1) && (T_p9_obj -> mode != 2)){ printf("UNFAMILIAR MODE %d\n", T_p9_obj->mode); exit(1); } p9_mode = 0; switch(T_p9_obj -> mode & 3){ case 0: #ifdef DEBUG printf("opening file %s for read only\n", fnode -> path); #endif p9_mode = OBJ_OPEN_ACCESS_READ; break; case 1: #ifdef DEBUG printf("opening file %s for write only\n", fnode -> path); #endif p9_mode = OBJ_OPEN_ACCESS_WRITE; break; case 2: #ifdef DEBUG printf("opening file %s for read write\n", fnode -> path); #endif p9_mode = OBJ_OPEN_ACCESS_READ | OBJ_OPEN_ACCESS_WRITE; //printf("OPENING FILE FOR READ AND WRITE IS NOT SUPPORTED IN OBJECT LIB\n"); break; case 3: p9_mode = OBJ_OPEN_ACCESS_READ; break; default: printf("You should never reach here\n"); exit(1); break; } ObjLib_SetOpenParams(&openParams, fnode->path, NULL, OBJTYPE_CLASS_GENERIC, p9_mode, OBJ_OPEN, NULL, 0, NULL); objError = ObjLib_Open(&openParams, object_handle); if(!ObjLib_IsSuccess(objError)){ printf("Error opening\n"); exit(1); } /* Assigning the file descriptor to the fid node */ fnode -> object_handle = object_handle; if(fnode -> object_handle == NULL){ R_p9_obj -> type = P9_RERROR; R_p9_obj -> ename_len = strlen(strerror(errno)); R_p9_obj -> ename = (char *) malloc(R_p9_obj -> ename_len + 1); bzero(R_p9_obj -> ename, R_p9_obj -> ename_len + 1); strcpy(R_p9_obj -> ename, strerror(errno)); R_p9_obj -> size = 4 + 1 + 2 + 2 + R_p9_obj -> ename_len; } else{ assert(fnode -> object_handle != NULL); } } else{ //file is a directory fnode -> dd = opendir(fnode -> path); /* handle permissions and rw access */ } free(s); //free the temporary allocated stat data structure break; }//end scope case P9_TREMOVE:{ int fid; R_p9_obj -> size = 7; R_p9_obj -> type = P9_RREMOVE; R_p9_obj -> tag = T_p9_obj -> tag; fid = T_p9_obj -> fid; fnode = fid_table_find_fid(fid_table, fid); assert(fnode != NULL); assert(fnode->path != NULL); if(UNIX_remove(fnode->path) != 0){ perror("failed to remove\n"); exit(1); } if(fid_table_remove_fid(fid_table, fid) == -1){ perror("TRREMOVE"); exit(1); } assert(fid_table_find_fid(fid_table, T_p9_obj -> fid) == NULL); break; }//end scope case P9_TWSTAT: { int fid; fid_node *fnode; stat_t *s_new; stat_t *s_old; fid = T_p9_obj -> fid; s_new = T_p9_obj -> stat; fnode = fid_table_find_fid(fid_table, fid); if(fnode == NULL){ perror("writing stat to non existing file\n"); exit(1); } s_old = (stat_t *)malloc(sizeof(stat_t)); make_stat_from_UNIX_file(fnode->path, s_old); /* now you have s_new and s_old. Check differences and call the appropriate UNIX api */ /* it doesn't make any sense to change the type, dev, qid, atime, mtime, muid */ /* only name, uid, gid, permission part of the mode can be changed * */ /* it does not make any sense to change the length */ if((strcmp(s_new -> name, "")!= 0) && (strcmp(s_old -> name, s_new -> name) != 0)){ #ifdef DEBUG printf("RENAMING: %s to %s\n", s_old -> name, s_new -> name); #endif if(T_p9_obj -> stat -> qid -> type == 128){ /* TODO: check permissions */ UNIX_rename_directory(fnode -> path, s_new -> name); } else{ /* TODO: check permission */ UNIX_rename_file(fnode->path, s_new->name); } } if(s_new -> mode != 0xffffffff && s_old -> mode != s_new -> mode){ /* only change the permissions */ #ifdef DEBUG printf("MODE required to change from %d to %d!\n", s_old -> mode, s_new -> mode); #endif UNIX_change_permissions(fnode->path, s_new -> mode); } R_p9_obj -> size = 7; R_p9_obj -> type = P9_RWSTAT; R_p9_obj -> tag = T_p9_obj -> tag; /* also gid can be changed but that should be taken care of later */ destroy_stat(s_old); break; }//end of scope case P9_TFLUSH: R_p9_obj -> size = 7; R_p9_obj -> tag = T_p9_obj -> tag; R_p9_obj -> type = P9_RFLUSH; break; default: break; }; }
static int read_write_file(struct filename *f1name, struct filename *f2name, unsigned char *key, int flag) { struct file *fp1 =NULL; struct file *fp2 =NULL; struct file *fp_temp = NULL; int rbytes, wbytes; char *buf = NULL; int keylen =16; int rc; int flag_outfile, flag_delete_temp; mm_segment_t fs; flag_outfile =0; flag_delete_temp =0; fp1 = filp_open(f1name->name,O_RDONLY,0); putname(f1name); if(!fp1) { printk("Error opening input file \n"); rc = -ENOENT; return rc; } if(IS_ERR(fp1)) { printk("Error opening input file \n"); rc = -ENOENT; return rc; } rc = is_regular_file(fp1); if(rc){ printk("Input file is not regular \n"); rc = -EISDIR; goto out; } if(!fp1->f_op){ printk("Permission Denied \n"); rc = -EPERM; goto out; } if(!fp1->f_op->read){ printk("Read operation not permitted \n"); rc = -EPERM; goto out; } flag_outfile = is_file_exists(f2name->name); if(flag_outfile) fp2 = filp_open(f2name->name,O_WRONLY ,fp1->f_mode); else fp2 = filp_open(f2name->name,O_CREAT | O_WRONLY ,fp1->f_mode); fp_temp = filp_open(strcat((char *)f2name->name,".tmp"),O_CREAT | O_WRONLY,fp1->f_mode); putname(f2name); if(!fp2 || !fp_temp){ printk("Error opening write file\n"); rc= -ENOENT; return rc; } if(IS_ERR(fp2) || IS_ERR(fp_temp)){ printk("Error opening write file\n"); rc= -ENOENT; return rc; } if(!fp2->f_op || !fp2->f_op){ printk("Permission Denied \n"); rc = -EPERM; goto out; } if(!fp2->f_op->write || !fp2->f_op->write){ printk("Write operation not permitted \n"); rc = -EPERM; goto out; } rc = is_same_file(fp1,fp2); if(rc){ printk("Input and output files are same \n"); rc = -EINVAL; goto out; } buf = kmalloc(PAGE_SIZE, GFP_KERNEL); if(IS_ERR(buf)){ printk("Error while allocationg temporary buffer for reading and writing \n"); rc = -PTR_ERR(key); goto out; } /* Add info about key in preamble during encryption */ if(flag == 1) { rc=add_preamble(fp_temp,key,keylen); if(rc< 0){ printk("Error adding preamble to the outfile\n"); goto out; } } /* Verify hashed key stored in preamble during decryption */ if(flag == 0) { rc=verify_preamble(fp1,key,keylen); if(rc<0){ printk("Error verifying preamble in the encrypted file\n"); goto out; } } /* read and write actual data */ while(1){ fs=get_fs(); set_fs(get_ds()); rbytes = fp1->f_op->read(fp1,buf,PAGE_SIZE,&fp1->f_pos); if(rbytes < 0){ printk("Failed reading input file\n"); set_fs(fs); rc= -EIO; goto out; } if (rbytes == 0){ printk("Reached end of file while reading \n"); rc= 0; goto out_rename; } /* If flag is set as 1, input file is encrypted */ if(flag == 1){ rc=encrypt_decrypt_file(buf,key,rbytes,flag); if(rc < 0){ printk("Encrypt failure\n"); goto out; } } /* If flag is set as 0, input file is decrypted */ else if(flag == 0){ rc=encrypt_decrypt_file(buf,key,rbytes,flag); if(rc < 0){ printk("Decrypt failure \n"); goto out; } } wbytes = fp_temp->f_op->write(fp_temp,buf,rbytes,&fp_temp->f_pos); if(wbytes < 0){ printk("Failed writing output file\n"); set_fs(fs); rc= -EIO; goto out; } set_fs(fs); } out_rename: flag_delete_temp=1; rc = rename_temp_file(fp_temp,fp2); if(rc) printk("Rename operation failed \n"); out: if(flag_delete_temp==0){ if(rc<0){ if(delete_partial_file(fp_temp)) printk("Deleting partial temp file failed \n"); } } printk("flag_outfile = %d \n",flag_outfile); if(flag_outfile==0){ if(rc < 0){ if(delete_partial_file(fp2)) printk("Deleting out file failed\n"); } } if(fp_temp) filp_close(fp_temp,NULL); if(buf) kfree(buf); if(fp2) filp_close(fp2,NULL); if(fp1) filp_close(fp1,NULL); if(IS_ERR(f2name)) putname(f2name); if(IS_ERR(f1name)) putname(f1name); return rc; }
int do_STOR(ftp_session *s, char *param) { int len; char arg[MAX_FTP_PATH], ftp_dir[MAX_FTP_PATH], buf[MAX_BUFFER]; FILE *fp; SOCKET sockfd; MATCH_SP(param); len = get_string(param, arg, sizeof(arg)); if (len == 0) return 501; param += len; MATCH_CRLF(param); if (!parse_dir(s->dir, arg, ftp_dir)) return 553; if (!ftp_to_fs_write(ftp_dir, arg)) return 553; if (s->prev_command == cmd_REST) { fp = fopen(arg, is_file_exists(arg) ? "r+b" : "wb"); fseek(fp, s->restart, SEEK_SET); } else fp = fopen(arg, "wb"); if (fp == NULL) return 450; ftp_printf(s->control, reply_150); sockfd = ftp_connect(s); if (sockfd == -1) { fclose(fp); return 425; } for (;;) { s->tick = GetTickCount(); len = recv(sockfd, buf, sizeof(buf), 0); if (len == 0) break; if (len == SOCKET_ERROR) { closesocket(sockfd); fclose(fp); return 426; } if (len != fwrite(buf, 1, len, fp)) { closesocket(sockfd); fclose(fp); return 452; } } closesocket(sockfd); fclose(fp); return 226; }
int proc_sign(const ts::wstrings_c & pars) { if (pars.size() < 3) return 0; ts::wstr_c arch = pars.get(1); ts::fix_path( arch, FNO_SIMPLIFY ); ts::wstr_c proc = pars.get(2); ts::fix_path( proc, FNO_SIMPLIFY ); if (!is_file_exists(arch.as_sptr())) { Print(FOREGROUND_RED, "arch file not found: %s\n", to_str(arch).cstr()); return 0; } if (!is_file_exists(proc.as_sptr())) { Print(FOREGROUND_RED, "proc file not found: %s\n", to_str(proc).cstr()); return 0; } ts::buf_c b; b.load_from_disk_file(arch); int archlen = b.size(); ts::md5_c md5; md5.update(b.data(), b.size()); md5.done(); ts::abp_c bp; b.load_from_disk_file(proc); bp.load(b.cstr()); ts::wstr_c procpath = ts::fn_get_path(proc); auto pa = [&]( ts::asptr p ) ->ts::wstr_c { return ts::fn_join(procpath, to_wstr(bp.get_string(p))); }; b.load_from_disk_file( pa(CONSTASTR("ver")) ); ts::str_c ver = b.cstr(); ver.replace_all('/','.').trim(); ts::str_c ss(CONSTASTR("ver=")); ss.append( ver ); ss.append(CONSTASTR("\r\nurl=")); ts::str_c path = bp.get_string(CONSTASTR("path")); path.replace_all(CONSTASTR("%ver%"), ver); path.replace_all(CONSTASTR("%https%"), CONSTASTR("https://")); path.replace_all(CONSTASTR("%http%"), CONSTASTR("http://")); path.appendcvt(ts::fn_get_name_with_ext(arch)); ss.append(path); ss.append(CONSTASTR("\r\nsize=")); ss.append_as_uint(archlen); ss.append(CONSTASTR("\r\nmd5=")); ss.append_as_hex(md5.result(), 16); unsigned char pk[crypto_sign_PUBLICKEYBYTES]; unsigned char sk[crypto_sign_SECRETKEYBYTES]; b.clear(); b.load_from_disk_file( pa(CONSTASTR("sk")) ); if (b.size() != crypto_sign_SECRETKEYBYTES) { rebuild: crypto_sign_keypair(pk, sk); FILE *f = _wfopen(pa(CONSTASTR("sk")), L"wb"); fwrite(sk, 1, sizeof(sk), f); fclose(f); ts::str_c spk; for(int i=0;i<crypto_sign_PUBLICKEYBYTES;++i) spk.append(CONSTASTR("0x")).append_as_hex(pk[i]).append(CONSTASTR(", ")); spk.trunc_length(2); f = _wfopen(pa(CONSTASTR("pk")), L"wb"); fwrite(spk.cstr(), 1, spk.get_length(), f); fclose(f); } else { memcpy(sk, b.data(), crypto_sign_SECRETKEYBYTES); crypto_sign_ed25519_sk_to_pk(pk, sk); b.load_from_disk_file( pa(CONSTASTR("pk")) ); ts::token<char> t(b.cstr(), ','); int n = 0; for(;t; ++t, ++n) { if (n >= sizeof(pk)) goto rebuild; ts::str_c nx(*t); nx.trim(); if (pk[n] != (byte)nx.as_uint()) goto rebuild; } if (n < sizeof(pk)) goto rebuild; } unsigned char sig[crypto_sign_BYTES]; unsigned long long siglen; crypto_sign_detached(sig,&siglen, (const byte *)ss.cstr(), ss.get_length(), sk); ss.append(CONSTASTR("\r\nsign=")); ss.append_as_hex(sig, (int)siglen); if (CONSTASTR("github") == bp.get_string(CONSTASTR("wiki"))) { ss.insert(0,CONSTASTR("[begin]\r\n")); ss.replace_all(CONSTASTR("\r\n"), CONSTASTR("`<br>\r\n`")); ss.replace_all(CONSTASTR("[begin]`"), CONSTASTR("[begin]")); ss.append(CONSTASTR("`<br>\r\n[end]<br>\r\n")); } FILE *f = _wfopen(pa(CONSTASTR("result")), L"wb"); fwrite(ss.cstr(), 1, ss.get_length(), f); fclose(f); return 0; }