mirror of
https://github.com/sheumann/hush.git
synced 2024-12-21 23:29:34 +00:00
xmalloc_follow_symlinks() -- fix ELOOP issue with absolute paths,
return full path in cases where path doesn't resolve to a link. change name to better differentiate from xmalloc_readlink().
This commit is contained in:
parent
abbd363261
commit
599bbfbd9b
@ -260,10 +260,10 @@ DIR *warn_opendir(const char *path);
|
|||||||
|
|
||||||
/* UNUSED: char *xmalloc_realpath(const char *path); */
|
/* UNUSED: char *xmalloc_realpath(const char *path); */
|
||||||
char *xmalloc_readlink(const char *path);
|
char *xmalloc_readlink(const char *path);
|
||||||
char *xmalloc_readlink_follow(const char *path);
|
|
||||||
char *xmalloc_readlink_or_warn(const char *path);
|
char *xmalloc_readlink_or_warn(const char *path);
|
||||||
char *xrealloc_getcwd_or_warn(char *cwd);
|
char *xrealloc_getcwd_or_warn(char *cwd);
|
||||||
|
|
||||||
|
char *xmalloc_follow_symlinks(const char *path);
|
||||||
|
|
||||||
//TODO: signal(sid, f) is the same? then why?
|
//TODO: signal(sid, f) is the same? then why?
|
||||||
extern void sig_catch(int,void (*)(int));
|
extern void sig_catch(int,void (*)(int));
|
||||||
|
@ -52,7 +52,7 @@ int update_passwd(const char *filename, const char *username,
|
|||||||
int cnt = 0;
|
int cnt = 0;
|
||||||
int ret = -1; /* failure */
|
int ret = -1; /* failure */
|
||||||
|
|
||||||
filename = xmalloc_readlink_follow(filename);
|
filename = xmalloc_follow_symlinks(filename);
|
||||||
if (filename == NULL)
|
if (filename == NULL)
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
@ -33,13 +33,16 @@ char *xmalloc_readlink(const char *path)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* this routine is not the same as realpath(), which canonicalizes
|
* this routine is not the same as realpath(), which
|
||||||
* the given path completely. this routine only follows trailing
|
* canonicalizes the given path completely. this routine only
|
||||||
* symlinks until a real file is reached, and returns its name.
|
* follows trailing symlinks until a real file is reached, and
|
||||||
* intermediate symlinks are not expanded. as above, a malloced
|
* returns its name. if the path ends in a dangling link, or if
|
||||||
* char* is returned, which must be freed.
|
* the target doesn't exist, the path is returned in any case.
|
||||||
|
* intermediate symlinks in the path are not expanded -- only
|
||||||
|
* those at the tail.
|
||||||
|
* a malloced char* is returned, which must be freed by the caller.
|
||||||
*/
|
*/
|
||||||
char *xmalloc_readlink_follow(const char *path)
|
char *xmalloc_follow_symlinks(const char *path)
|
||||||
{
|
{
|
||||||
char *buf;
|
char *buf;
|
||||||
char *lpc;
|
char *lpc;
|
||||||
@ -47,23 +50,27 @@ char *xmalloc_readlink_follow(const char *path)
|
|||||||
int bufsize;
|
int bufsize;
|
||||||
int looping = MAXSYMLINKS + 1;
|
int looping = MAXSYMLINKS + 1;
|
||||||
|
|
||||||
linkpath = xstrdup(path);
|
buf = xstrdup(path);
|
||||||
goto jump_in;
|
goto jump_in;
|
||||||
|
|
||||||
while (1) {
|
while (1) {
|
||||||
|
|
||||||
|
linkpath = xmalloc_readlink(buf);
|
||||||
|
if (!linkpath) {
|
||||||
|
/* not a symlink, or doesn't exist */
|
||||||
|
if (errno == EINVAL || errno == ENOENT)
|
||||||
|
return buf;
|
||||||
|
free(buf);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
if (!--looping) {
|
if (!--looping) {
|
||||||
free(linkpath);
|
free(linkpath);
|
||||||
free(buf);
|
free(buf);
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
linkpath = xmalloc_readlink(buf);
|
|
||||||
if (!linkpath) {
|
if (*linkpath != '/') {
|
||||||
if (errno == EINVAL) /* not a symlink */
|
|
||||||
return buf;
|
|
||||||
free(buf);
|
|
||||||
return NULL;
|
|
||||||
}
|
|
||||||
if (linkpath[0] != '/') {
|
|
||||||
bufsize += strlen(linkpath);
|
bufsize += strlen(linkpath);
|
||||||
buf = xrealloc(buf, bufsize);
|
buf = xrealloc(buf, bufsize);
|
||||||
lpc = bb_get_last_path_component_strip(buf);
|
lpc = bb_get_last_path_component_strip(buf);
|
||||||
@ -71,8 +78,8 @@ char *xmalloc_readlink_follow(const char *path)
|
|||||||
free(linkpath);
|
free(linkpath);
|
||||||
} else {
|
} else {
|
||||||
free(buf);
|
free(buf);
|
||||||
jump_in:
|
|
||||||
buf = linkpath;
|
buf = linkpath;
|
||||||
|
jump_in:
|
||||||
bufsize = strlen(buf) + 1;
|
bufsize = strlen(buf) + 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user