1#include <stdio.h> 2#include <string.h> 3#include <unistd.h> 4#include <errno.h> 5#include <stdlib.h> 6#include <pwd.h> 7#include <sys/types.h> 8 9#include "commands.h" 10#include "cwd.h" 11#include "logging.h" 12#include "mystring.h" 13 14char *cwd = NULL; 15 16extern int readyshareCloud_conn; /* pling added 10/08/2012 */ 17 18int bftpd_cwd_chdir(char *dir) 19{ 20 /* Foxconn added start pling 10/08/2012 */ 21 char root_dir[] = "/shares/%s"; 22 char convert_dir[1024]; 23 /* Foxconn added end pling 10/08/2012 */ 24 25 if (strncmp(dir,"/shares/shares",strlen("/shares/shares"))==0) 26 {//jenny 27 if (chdir(dir)) 28 { 29 dir = dir+strlen("/shares"); 30 } 31 } 32 char *tmp = bftpd_cwd_mappath(dir); 33 34 /* Foxconn added start pling 10/08/2012 */ 35 /* Handle ReadyShare Cloud connections specially */ 36 if (readyshareCloud_conn) 37 { 38 if (strncmp(tmp, "/shares", 7)) 39 { 40 free(tmp); 41 sprintf(convert_dir, root_dir, dir); 42 tmp = bftpd_cwd_mappath(convert_dir); 43 } 44 } 45 /* Foxconn added end pling 10/08/2012 */ 46 47 /* Foxconn added start pling 05/14/2009 */ 48 /* Sanity check, don't allow user to get outside of the /shares */ 49 /* Foxconn modified start pling 09/17/2013 */ 50 /* IE will have problem when sees 'access denied', so allow chdir to '/' */ 51 if (strlen(tmp) == 0 /*|| strcmp(tmp, "/") == 0*/) { 52 /* Foxconn modified end pling 09/17/2013 */ 53 free(tmp); 54 errno = EACCES; 55 return -1; 56 } 57 /* Foxconn added end pling 05/14/2009 */ 58 59 /* Foxconn added start pling 09/02/2012 */ 60 /* WNDR4500v2 IR45/46/47, don't allow user to access outside /shares */ 61 /* Foxconn modified start pling 09/17/2013 */ 62 /* IE will have problem when sees 'access denied', so allow chdir to '/' */ 63 if (strncmp(tmp, "/shares", 7) != 0 && strcmp(tmp, "/") != 0) { 64 free(tmp); 65 bftpd_log("Block cwd to '%s'\n", tmp); 66 errno = ENOENT; //EACCES; 67 return -1; 68 } 69 /* Foxconn modified end pling 09/17/2013 */ 70 /* Foxconn added end pling 09/02/2012 */ 71 72 if (chdir(tmp)) { 73 free(tmp); 74 return errno; 75 } 76 cwd = realloc(cwd, strlen(tmp) + 1); 77 strcpy(cwd, tmp); 78 new_umask(); 79 free(tmp); 80 return 0; 81} 82 83char *bftpd_cwd_getcwd() 84{ 85 return strdup(cwd); 86} 87 88void appendpath(char *result, char *tmp) 89{ 90 if (!strcmp(tmp, ".")) 91 return; 92 if (!strcmp(tmp, "..")) { 93 if (strcmp(result, "/")) { 94 if (result[strlen(result) - 1] == '/') 95 result[strlen(result) - 1] = '\0'; 96 tmp = result; 97 while (strchr(tmp, '/')) 98 tmp = strchr(tmp, '/') + 1; 99 *tmp = '\0'; 100 if ((result[strlen(result) - 1] == '/') && (strlen(result) > 1)) 101 result[strlen(result) - 1] = '\0'; 102 } 103 } else { 104 if (result[strlen(result) - 1] != '/') 105 strcat(result, "/"); 106 strcat(result, tmp); 107 } 108} 109 110 111 112char *bftpd_cwd_mappath(char *path) 113{ 114 char *result = malloc(strlen(path) + strlen(cwd) + 16); 115 char *path2; 116 char *tmp; 117 118 if (! result) 119 return NULL; 120 path2 = strdup(path); 121 if (! path2) 122 { 123 free(result); 124 return NULL; 125 } 126 127 if (path[0] == '/') 128 strcpy(result, "/"); 129 else 130 strcpy(result, cwd); 131 132 while (strchr(path2, '/')) { 133 tmp = strdup(path2); 134 *strchr(tmp, '/') = '\0'; 135 cutto(path2, strlen(tmp) + 1); 136 appendpath(result, tmp); 137 free(tmp); 138 } 139 appendpath(result, path2); 140 free(path2); 141 return result; 142} 143 144void bftpd_cwd_init() 145{ 146 cwd = malloc(256); 147 getcwd(cwd, 255); 148} 149 150void bftpd_cwd_end() 151{ 152 if (cwd) { 153 free(cwd); 154 cwd = NULL; 155 } 156} 157