2 * User FTP Server, Share folders over FTP without being root.
3 * Copyright (C) 2008 Isaac Jurado
5 * This program is free software; you can redistribute it and/or modify it under
6 * the terms of the GNU General Public License as published by the Free Software
7 * Foundation; either version 2 of the License, or (at your option) any later
10 * This program is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
12 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
15 * You should have received a copy of the GNU General Public License along with
16 * this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 * - Check every component to see if it is a symbolic link in order to
25 * protect the shared hierarchy even more. This may come at a cost in
33 * Apply new path components to an existing working directory. The working
34 * directory is contained (full path) in wd, which is a buffer with a capacity
35 * of size bytes. This working directory path length is len bytes (counting the
38 * The function returns the length of the resulting path after walking the path
39 * argument from the working directory. In case the working directory would
40 * exceed size bytes (at any stage), -1 is returning.
42 static int apply_path (const char *path
, char *wd
, int len
)
48 /* Absolute path, truncate wd */
56 /* Combine delimiters */
61 /* No more components to apply */
64 /* Isolate next component */
66 while (path
[i
] != '/' && path
[i
] != '\0')
69 if (i
== 2 && path
[0] == '.' && path
[1] == '.')
71 /* Return to parent directory, found ".." */
72 while (wd
[len
] != '/')
75 len
++; /* Root reached, fix */
79 else if (i
!= 1 || path
[0] != '.')
81 if (len
+ i
>= LINE_SIZE
)
84 /* Apply component, because it is different than "." */
88 len
--; /* Skip delimiter at root */
90 memcpy(wd
+ len
, path
, i
);
95 /* Now skip to the next component */
103 void change_dir (void)
109 reply_c("501 Argument required.\r\n");
113 len
= apply_path(SS
.arg
, SS
.cwd
, SS
.cwd_len
);
116 reply_c("552 Path overflow.\r\n");
117 fatal("Path overflow in CWD");
121 reply_c("250 Directory changed.\r\n");
122 debug("Directory changed to '%s'", SS
.cwd
);
126 int expand_arg (void)
128 int len
= SS
.cwd_len
;
130 strncpy(SS
.aux
, SS
.cwd
, len
);
134 len
= apply_path(SS
.arg
, SS
.aux
, SS
.cwd_len
);
137 reply_c("552 Path overflow.\r\n");
138 fatal("Path overflow");
143 debug("Argument expanded to '%s'", SS
.arg
);