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	if (strlen(tmp) == 0 || strcmp(tmp, "/") == 0) {
50		free(tmp);
51		errno = EACCES;
52		return -1;
53	}
54	/* Foxconn added end pling 05/14/2009 */
55
56	/* Foxconn added start pling 09/02/2012 */
57	/* WNDR4500v2 IR45/46/47, don't allow user to access outside /shares */
58	if (strncmp(tmp, "/shares", 7) != 0) {
59		free(tmp);
60		bftpd_log("Block cwd to '%s'\n", tmp);
61		errno = EACCES;
62		return -1;
63	}
64	/* Foxconn added end pling 09/02/2012 */
65
66	if (chdir(tmp)) {
67		free(tmp);
68		return errno;
69	}
70	cwd = realloc(cwd, strlen(tmp) + 1);
71	strcpy(cwd, tmp);
72	new_umask();
73	free(tmp);
74	return 0;
75}
76
77char *bftpd_cwd_getcwd()
78{
79	return strdup(cwd);
80}
81
82void appendpath(char *result, char *tmp)
83{
84	if (!strcmp(tmp, "."))
85		return;
86	if (!strcmp(tmp, "..")) {
87        if (strcmp(result, "/")) {
88            if (result[strlen(result) - 1] == '/')
89                result[strlen(result) - 1] = '\0';
90            tmp = result;
91            while (strchr(tmp, '/'))
92                tmp = strchr(tmp, '/') + 1;
93            *tmp = '\0';
94            if ((result[strlen(result) - 1] == '/') && (strlen(result) > 1))
95                result[strlen(result) - 1] = '\0';
96        }
97	} else {
98		if (result[strlen(result) - 1] != '/')
99			strcat(result, "/");
100		strcat(result, tmp);
101	}
102}
103
104
105
106char *bftpd_cwd_mappath(char *path)
107{
108	char *result = malloc(strlen(path) + strlen(cwd) + 16);
109	char *path2;
110	char *tmp;
111
112        if (! result)
113           return NULL;
114        path2 = strdup(path);
115        if (! path2)
116        {
117           free(result);
118           return NULL;
119        }
120
121	if (path[0] == '/')
122		strcpy(result, "/");
123	else
124		strcpy(result, cwd);
125
126	while (strchr(path2, '/')) {
127		tmp = strdup(path2);
128		*strchr(tmp, '/') = '\0';
129		cutto(path2, strlen(tmp) + 1);
130		appendpath(result, tmp);
131		free(tmp);
132	}
133	appendpath(result, path2);
134	free(path2);
135	return result;
136}
137
138void bftpd_cwd_init()
139{
140	cwd = malloc(256);
141	getcwd(cwd, 255);
142}
143
144void bftpd_cwd_end()
145{
146	if (cwd) {
147		free(cwd);
148		cwd = NULL;
149	}
150}
151