+ if (slash != NULL) {
+ *slash = '/';
+ }
+ return 0;
+}
+
+static int nfs_normalize_path(struct nfs_context *nfs, char *path)
+{
+ char *str;
+ int len;
+
+ /* // -> / */
+ while (str = strstr(path, "//")) {
+ while(*str) {
+ *str = *(str + 1);
+ str++;
+ }
+ }
+
+ /* /./ -> / */
+ while (str = strstr(path, "/./")) {
+ while(*(str + 1)) {
+ *str = *(str + 2);
+ str++;
+ }
+ }
+
+ /* ^/../ -> error */
+ if (!strncmp(path, "/../", 4)) {
+ rpc_set_error(nfs->rpc,
+ "Absolute path starts with '/../' "
+ "during normalization");
+ return -1;
+ }
+
+ /* ^[^/] -> error */
+ if (path[0] != '/') {
+ rpc_set_error(nfs->rpc,
+ "Absolute path does not start with '/'");
+ return -1;
+ }
+
+ /* /string/../ -> / */
+ while (str = strstr(path, "/../")) {
+ char *tmp;
+
+ if (!strncmp(path, "/../", 4)) {
+ rpc_set_error(nfs->rpc,
+ "Absolute path starts with '/../' "
+ "during normalization");
+ return -1;
+ }
+
+ tmp = str - 1;
+ while (*tmp != '/') {
+ tmp--;
+ }
+ str += 3;
+ while((*(tmp++) = *(str++)) != '\0')
+ ;
+ }
+
+ /* /$ -> \0 */
+ len = strlen(path);
+ if (len >= 1) {
+ if (path[len - 1] == '/') {
+ path[len - 1] = '\0';
+ len--;
+ }
+ }
+ if (path[0] == '\0') {
+ rpc_set_error(nfs->rpc,
+ "Absolute path became '' "
+ "during normalization");
+ return -1;
+ }
+
+ /* /.$ -> \0 */
+ if (len >= 2) {
+ if (!strcmp(&path[len - 2], "/.")) {
+ path[len - 2] = '\0';
+ len -= 2;
+ }
+ }
+
+ /* ^/..$ -> error */
+ if (!strcmp(path, "/..")) {
+ rpc_set_error(nfs->rpc,
+ "Absolute path is '/..' "
+ "during normalization");
+ return -1;
+ }
+
+ /* /string/..$ -> / */
+ if (len >= 3) {
+ if (!strcmp(&path[len - 3], "/..")) {
+ char *tmp = &path[len - 3];
+ while (*--tmp != '/')
+ ;
+ *tmp = '\0';
+ }
+ }
+