1*e9e8876aSEd Maste /* $OpenBSD: sftp-realpath.c,v 1.2 2021/09/02 21:03:54 deraadt Exp $ */
219261079SEd Maste /*
319261079SEd Maste * Copyright (c) 2003 Constantin S. Svintsoff <kostik@iclub.nsu.ru>
419261079SEd Maste *
519261079SEd Maste * Redistribution and use in source and binary forms, with or without
619261079SEd Maste * modification, are permitted provided that the following conditions
719261079SEd Maste * are met:
819261079SEd Maste * 1. Redistributions of source code must retain the above copyright
919261079SEd Maste * notice, this list of conditions and the following disclaimer.
1019261079SEd Maste * 2. Redistributions in binary form must reproduce the above copyright
1119261079SEd Maste * notice, this list of conditions and the following disclaimer in the
1219261079SEd Maste * documentation and/or other materials provided with the distribution.
1319261079SEd Maste * 3. The names of the authors may not be used to endorse or promote
1419261079SEd Maste * products derived from this software without specific prior written
1519261079SEd Maste * permission.
1619261079SEd Maste *
1719261079SEd Maste * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
1819261079SEd Maste * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
1919261079SEd Maste * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
2019261079SEd Maste * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
2119261079SEd Maste * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
2219261079SEd Maste * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
2319261079SEd Maste * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
2419261079SEd Maste * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
2519261079SEd Maste * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
2619261079SEd Maste * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2719261079SEd Maste * SUCH DAMAGE.
2819261079SEd Maste */
2919261079SEd Maste
3019261079SEd Maste #include "includes.h"
3119261079SEd Maste
3219261079SEd Maste #include <sys/types.h>
3319261079SEd Maste #include <sys/stat.h>
3419261079SEd Maste
3519261079SEd Maste #include <errno.h>
3619261079SEd Maste #include <stdlib.h>
3719261079SEd Maste #include <stddef.h>
3819261079SEd Maste #include <string.h>
3919261079SEd Maste #include <unistd.h>
4019261079SEd Maste #include <limits.h>
4119261079SEd Maste
4219261079SEd Maste #ifndef SYMLOOP_MAX
4319261079SEd Maste # define SYMLOOP_MAX 32
4419261079SEd Maste #endif
4519261079SEd Maste
4619261079SEd Maste /* XXX rewrite sftp-server to use POSIX realpath and remove this hack */
4719261079SEd Maste
4819261079SEd Maste char *sftp_realpath(const char *path, char *resolved);
4919261079SEd Maste
5019261079SEd Maste /*
5119261079SEd Maste * char *realpath(const char *path, char resolved[PATH_MAX]);
5219261079SEd Maste *
5319261079SEd Maste * Find the real name of path, by removing all ".", ".." and symlink
5419261079SEd Maste * components. Returns (resolved) on success, or (NULL) on failure,
5519261079SEd Maste * in which case the path which caused trouble is left in (resolved).
5619261079SEd Maste */
5719261079SEd Maste char *
sftp_realpath(const char * path,char * resolved)5819261079SEd Maste sftp_realpath(const char *path, char *resolved)
5919261079SEd Maste {
6019261079SEd Maste struct stat sb;
6119261079SEd Maste char *p, *q, *s;
6219261079SEd Maste size_t left_len, resolved_len;
6319261079SEd Maste unsigned symlinks;
6419261079SEd Maste int serrno, slen, mem_allocated;
6519261079SEd Maste char left[PATH_MAX], next_token[PATH_MAX], symlink[PATH_MAX];
6619261079SEd Maste
6719261079SEd Maste if (path[0] == '\0') {
6819261079SEd Maste errno = ENOENT;
6919261079SEd Maste return (NULL);
7019261079SEd Maste }
7119261079SEd Maste
7219261079SEd Maste serrno = errno;
7319261079SEd Maste
7419261079SEd Maste if (resolved == NULL) {
7519261079SEd Maste resolved = malloc(PATH_MAX);
7619261079SEd Maste if (resolved == NULL)
7719261079SEd Maste return (NULL);
7819261079SEd Maste mem_allocated = 1;
7919261079SEd Maste } else
8019261079SEd Maste mem_allocated = 0;
8119261079SEd Maste
8219261079SEd Maste symlinks = 0;
8319261079SEd Maste if (path[0] == '/') {
8419261079SEd Maste resolved[0] = '/';
8519261079SEd Maste resolved[1] = '\0';
8619261079SEd Maste if (path[1] == '\0')
8719261079SEd Maste return (resolved);
8819261079SEd Maste resolved_len = 1;
8919261079SEd Maste left_len = strlcpy(left, path + 1, sizeof(left));
9019261079SEd Maste } else {
9119261079SEd Maste if (getcwd(resolved, PATH_MAX) == NULL) {
9219261079SEd Maste if (mem_allocated)
9319261079SEd Maste free(resolved);
9419261079SEd Maste else
9519261079SEd Maste strlcpy(resolved, ".", PATH_MAX);
9619261079SEd Maste return (NULL);
9719261079SEd Maste }
9819261079SEd Maste resolved_len = strlen(resolved);
9919261079SEd Maste left_len = strlcpy(left, path, sizeof(left));
10019261079SEd Maste }
10119261079SEd Maste if (left_len >= sizeof(left) || resolved_len >= PATH_MAX) {
10219261079SEd Maste errno = ENAMETOOLONG;
10319261079SEd Maste goto err;
10419261079SEd Maste }
10519261079SEd Maste
10619261079SEd Maste /*
10719261079SEd Maste * Iterate over path components in `left'.
10819261079SEd Maste */
10919261079SEd Maste while (left_len != 0) {
11019261079SEd Maste /*
11119261079SEd Maste * Extract the next path component and adjust `left'
11219261079SEd Maste * and its length.
11319261079SEd Maste */
11419261079SEd Maste p = strchr(left, '/');
11519261079SEd Maste s = p ? p : left + left_len;
11619261079SEd Maste if (s - left >= (ptrdiff_t)sizeof(next_token)) {
11719261079SEd Maste errno = ENAMETOOLONG;
11819261079SEd Maste goto err;
11919261079SEd Maste }
12019261079SEd Maste memcpy(next_token, left, s - left);
12119261079SEd Maste next_token[s - left] = '\0';
12219261079SEd Maste left_len -= s - left;
12319261079SEd Maste if (p != NULL)
12419261079SEd Maste memmove(left, s + 1, left_len + 1);
12519261079SEd Maste if (resolved[resolved_len - 1] != '/') {
12619261079SEd Maste if (resolved_len + 1 >= PATH_MAX) {
12719261079SEd Maste errno = ENAMETOOLONG;
12819261079SEd Maste goto err;
12919261079SEd Maste }
13019261079SEd Maste resolved[resolved_len++] = '/';
13119261079SEd Maste resolved[resolved_len] = '\0';
13219261079SEd Maste }
13319261079SEd Maste if (next_token[0] == '\0')
13419261079SEd Maste continue;
13519261079SEd Maste else if (strcmp(next_token, ".") == 0)
13619261079SEd Maste continue;
13719261079SEd Maste else if (strcmp(next_token, "..") == 0) {
13819261079SEd Maste /*
13919261079SEd Maste * Strip the last path component except when we have
14019261079SEd Maste * single "/"
14119261079SEd Maste */
14219261079SEd Maste if (resolved_len > 1) {
14319261079SEd Maste resolved[resolved_len - 1] = '\0';
14419261079SEd Maste q = strrchr(resolved, '/') + 1;
14519261079SEd Maste *q = '\0';
14619261079SEd Maste resolved_len = q - resolved;
14719261079SEd Maste }
14819261079SEd Maste continue;
14919261079SEd Maste }
15019261079SEd Maste
15119261079SEd Maste /*
15219261079SEd Maste * Append the next path component and lstat() it. If
15319261079SEd Maste * lstat() fails we still can return successfully if
15419261079SEd Maste * there are no more path components left.
15519261079SEd Maste */
15619261079SEd Maste resolved_len = strlcat(resolved, next_token, PATH_MAX);
15719261079SEd Maste if (resolved_len >= PATH_MAX) {
15819261079SEd Maste errno = ENAMETOOLONG;
15919261079SEd Maste goto err;
16019261079SEd Maste }
16119261079SEd Maste if (lstat(resolved, &sb) != 0) {
16219261079SEd Maste if (errno == ENOENT && p == NULL) {
16319261079SEd Maste errno = serrno;
16419261079SEd Maste return (resolved);
16519261079SEd Maste }
16619261079SEd Maste goto err;
16719261079SEd Maste }
16819261079SEd Maste if (S_ISLNK(sb.st_mode)) {
16919261079SEd Maste if (symlinks++ > SYMLOOP_MAX) {
17019261079SEd Maste errno = ELOOP;
17119261079SEd Maste goto err;
17219261079SEd Maste }
17319261079SEd Maste slen = readlink(resolved, symlink, sizeof(symlink) - 1);
17419261079SEd Maste if (slen < 0)
17519261079SEd Maste goto err;
17619261079SEd Maste symlink[slen] = '\0';
17719261079SEd Maste if (symlink[0] == '/') {
17819261079SEd Maste resolved[1] = 0;
17919261079SEd Maste resolved_len = 1;
18019261079SEd Maste } else if (resolved_len > 1) {
18119261079SEd Maste /* Strip the last path component. */
18219261079SEd Maste resolved[resolved_len - 1] = '\0';
18319261079SEd Maste q = strrchr(resolved, '/') + 1;
18419261079SEd Maste *q = '\0';
18519261079SEd Maste resolved_len = q - resolved;
18619261079SEd Maste }
18719261079SEd Maste
18819261079SEd Maste /*
18919261079SEd Maste * If there are any path components left, then
19019261079SEd Maste * append them to symlink. The result is placed
19119261079SEd Maste * in `left'.
19219261079SEd Maste */
19319261079SEd Maste if (p != NULL) {
19419261079SEd Maste if (symlink[slen - 1] != '/') {
19519261079SEd Maste if (slen + 1 >=
19619261079SEd Maste (ptrdiff_t)sizeof(symlink)) {
19719261079SEd Maste errno = ENAMETOOLONG;
19819261079SEd Maste goto err;
19919261079SEd Maste }
20019261079SEd Maste symlink[slen] = '/';
20119261079SEd Maste symlink[slen + 1] = 0;
20219261079SEd Maste }
20319261079SEd Maste left_len = strlcat(symlink, left, sizeof(symlink));
20419261079SEd Maste if (left_len >= sizeof(symlink)) {
20519261079SEd Maste errno = ENAMETOOLONG;
20619261079SEd Maste goto err;
20719261079SEd Maste }
20819261079SEd Maste }
20919261079SEd Maste left_len = strlcpy(left, symlink, sizeof(left));
21019261079SEd Maste }
21119261079SEd Maste }
21219261079SEd Maste
21319261079SEd Maste /*
21419261079SEd Maste * Remove trailing slash except when the resolved pathname
21519261079SEd Maste * is a single "/".
21619261079SEd Maste */
21719261079SEd Maste if (resolved_len > 1 && resolved[resolved_len - 1] == '/')
21819261079SEd Maste resolved[resolved_len - 1] = '\0';
21919261079SEd Maste return (resolved);
22019261079SEd Maste
22119261079SEd Maste err:
22219261079SEd Maste if (mem_allocated)
22319261079SEd Maste free(resolved);
22419261079SEd Maste return (NULL);
22519261079SEd Maste }
226