Commit fffe6137 fffe6137e227c5af157b7520fb4aa73aad00dd8e by Sam Roberts

Implemented symlink unrolling before locking.

1 parent 7eb2aa3d
......@@ -84,6 +84,7 @@ typedef void *(*mu_retrieve_fp) __P((void *));
extern void mu_register_retriever __P((list_t *pflist, mu_retrieve_fp fun));
extern void * mu_retrieve __P((list_t flist, void *data));
extern int mu_unroll_symlink __P((char *out, size_t outsz, const char *in));
#ifdef __cplusplus
}
......
......@@ -100,21 +100,26 @@ int locker_set_default_flags(int flags)
}
int
locker_create (locker_t *plocker, const char *filename, int flags)
locker_create (locker_t *plocker, const char *filename_, int flags)
{
locker_t l;
char filename[_POSIX_PATH_MAX];
int err = 0;
if (plocker == NULL)
return MU_ERR_OUT_PTR_NULL;
if (filename == NULL)
if (filename_ == NULL)
return EINVAL;
if((err = mu_unroll_symlink(filename, sizeof(filename), filename_)))
return err;
l = calloc (1, sizeof (*l));
if (l == NULL)
return ENOMEM;
/* Should make l->file be the result of following the symlinks. */
l->file = strdup(filename);
if (l->file == NULL)
......
......@@ -21,6 +21,7 @@
#endif
#include <errno.h>
#include <limits.h>
#include <netdb.h>
#include <pwd.h>
#include <signal.h>
......@@ -776,3 +777,74 @@ int mu_spawnvp (const char* prog, const char* const av_[], int* stat)
return err;
}
/* The result of readlink() may be a path relative to that link,
* qualify it if necessary.
*/
static void
mu_qualify_link (const char *path, const char *link, char *qualified)
{
const char *lb = NULL;
size_t len;
/* link is full path */
if (*link == '/')
{
mu_cpystr (qualified, link, _POSIX_PATH_MAX);
return;
}
if ((lb = strrchr (path, '/')) == NULL)
{
/* no path in link */
mu_cpystr (qualified, link, _POSIX_PATH_MAX);
return;
}
len = lb - path + 1;
memcpy (qualified, path, len);
mu_cpystr (qualified + len, link, _POSIX_PATH_MAX - len);
}
#ifndef _POSIX_SYMLOOP_MAX
# define _POSIX_SYMLOOP_MAX 255
#endif
int
mu_unroll_symlink (char *out, size_t outsz, const char *in)
{
char path[_POSIX_PATH_MAX];
int symloops = 0;
while (symloops++ < _POSIX_SYMLOOP_MAX)
{
struct stat s;
char link[_POSIX_PATH_MAX];
char qualified[_POSIX_PATH_MAX];
int len;
if (lstat (in, &s) == -1)
return errno;
if (!S_ISLNK (s.st_mode))
{
mu_cpystr (path, in, sizeof (path));
break;
}
if ((len = readlink (in, link, sizeof (link))) == -1)
return errno;
link[(len >= sizeof (link)) ? (sizeof (link) - 1) : len] = '\0';
mu_qualify_link (in, link, qualified);
mu_cpystr (path, qualified, sizeof (path));
in = path;
}
mu_cpystr (out, path, outsz);
return 0;
}
......