summaryrefslogtreecommitdiff
path: root/sudo_realpath.c
blob: 40db05a92080a7a2d8033c7971dccf0f89c53254 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
/*
 *  CU sudo version 1.3.1
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 1, or (at your option)
 *  any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Please send bugs, changes, problems to sudo-bugs@cs.colorado.edu
 *
 *******************************************************************
 *
 *  This module contains sudo_realpath(3), a customized version of
 *  realpath(3).  It is necesary to do the final chdir(2) as the
 *  uid of the invoking user.
 *
 *  sudo_realpath(3) takes a path to qualify and a pointer to a string
 *  as a copyout parameter.  This string should be of size MAXPATHLEN.
 *
 *  Todd C. Miller (millert@colorado.edu) Fri Jun  3 18:32:19 MDT 1994
 */

#ifndef lint
static char rcsid[] = "$Id$";
#endif /* lint */

#include "config.h"

#include <stdio.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#ifdef HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif /* HAVE_STRINGS_H */
#include <errno.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/param.h>
#include <netinet/in.h>

#include "sudo.h"
#include "options.h"

#ifndef STDC_HEADERS
extern char *strcpy	__P((char *, const char *));
extern ssize_t readlink	__P((const char *, char *, size_t));
extern int lstat	__P((const char *, struct stat *));
#endif /* !STDC_HEADERS */


/*
 * Posix versions for those without
 */
#ifndef _S_IFMT
#define _S_IFMT		S_IFMT
#endif /* _S_IFMT */
#ifndef _S_IFLNK
#define _S_IFLNK	S_IFLNK
#endif /* _S_IFLNK */


/*
 * Prototypes
 */
static void realpath_restore	__P((char *));


/******************************************************************
 *
 *  sudo_realpath()
 *
 *  this function takes a path and makes it fully qualified and resolves
 *  all symbolic links, returning the fully qualfied path.
 */

char * sudo_realpath(old, new)
    const char * old;
          char * new;
{
    char buf[MAXPATHLEN+1];			/* generic path buffer */
    struct stat statbuf;			/* for lstat() */
    char * temp;				/* temporary ptr */
    int len;					/* length parameter */
    int err;					/* did we get an error? */

    /* check for brain damage */
    if (old == NULL || old[0] == '\0')
	return(NULL);

    new[MAXPATHLEN] = '\0';
    (void) strncpy(new, old, MAXPATHLEN);

    /* we need to be root for this section */
    set_perms(PERM_ROOT);

#ifndef USE_REALPATH
    err = stat(new, &statbuf);
    set_perms(PERM_USER);
    return((err == 0) ? new : NULL);
#endif /* USE_REALPATH */

    /*
     * Resolve the last component of the path if it is a link
     * until it is a non-link.
     */
    errno = 0;
    while (!lstat(new, &statbuf) && (statbuf.st_mode & _S_IFMT) == _S_IFLNK) {
	/* it's a link */

	if ((len = readlink(new, buf, sizeof(buf))) <= 0) {
	    realpath_restore(cwd);
	    return(NULL);
	}
	buf[len] = '\0';

	/*
	 * If the link is absolute, copy it in, else remove the last
	 * component in new and append the link unless the sum is too long.
	 */
	if (buf[0] == '/') {
	    (void) strcpy(new, buf);
	} else {
	    if ((temp = strrchr(new, '/')))
		*(++temp) = '\0';

	    if (strlen(new) + strlen(buf) >= MAXPATHLEN ) {
		errno = ENAMETOOLONG;
		realpath_restore(cwd);
		return(NULL);
	    }

	    (void) strcat(new, buf);
	}
    }

    /* did an lstat() fail? */
    if (errno) {
	realpath_restore(cwd);
	return(NULL);
    }

    /*
     * separate the last component from the rest of the path
     * so we can do a getcwd() safely.
     */
    if (!(temp = strrchr(new, '/'))) {
	/* this should not happen */
	errno = ENOTDIR;
	realpath_restore(cwd);
	return(NULL);
    }

    (void) strcpy(buf, ++temp);
    *temp = '\0';

    /*
     * chdir() to new and go a getcwd() to find real path then
     * append buf * (last component of the path) and return.
     */
    if (chdir(new)) {
	realpath_restore(cwd);
	return(NULL);
    }

    if (!(getcwd(new, MAXPATHLEN))) {
	realpath_restore(cwd);
	return(NULL);
    }

    /* append "/" and buf to new but watch for double '/' */
    len = strlen(new);
    if (len) {
	temp = new + len - 1;
	if (*temp != '/') {
	    *(++temp) = '/';
	    *(++temp) = '\0';
	}

    }
    (void) strcat(new, buf);

    realpath_restore(cwd);
    return(new);
}


/******************************************************************
 *
 *  realpath_ret()
 *
 *  this function cd's to cwd, closes it, and returns path.
 */

static void realpath_restore(cwd)
    char * cwd;
{
    /* relinquish root privs and chdir to where we started... */
    set_perms(PERM_USER);
    if (chdir(cwd)) {
	fprintf(stderr, "Error: cannot change dir back to %s, sudo aborting!\n",
			cwd);
	exit(1);
    }
}