1) Refrain from calling set_basedir() in the -t case,
and do not attempt to strip anything from the file names in that case.
Testing individual files cannot reasonably have any notion of a base dir.
2) Remove the possibility of passing NULL to set_basedir().
It was dangerous because it was not idempotent, and it served no purpose
except closing a file descriptor right before exit(), which is pointless.
Besides, the file descriptor is likely to be removed completely, soon.
3) Make sure that /foobar isn't treated as a subdirectory of /foo;
this fixes a bug reported by espie@.
-/* $Id: mandocdb.c,v 1.149 2014/06/18 19:34:04 schwarze Exp $ */
+/* $Id: mandocdb.c,v 1.150 2014/06/19 00:45:37 schwarze Exp $ */
/*
* Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2011, 2012, 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
/*
* Copyright (c) 2011, 2012 Kristaps Dzonsons <kristaps@bsd.lv>
* Copyright (c) 2011, 2012, 2013, 2014 Ingo Schwarze <schwarze@openbsd.org>
if (OP_UPDATE == op || OP_DELETE == op || OP_TEST == op) {
/*
if (OP_UPDATE == op || OP_DELETE == op || OP_TEST == op) {
/*
- * All of these deal with a specific directory.
+ * Most of these deal with a specific directory.
* Jump into that directory first.
*/
* Jump into that directory first.
*/
- if (0 == set_basedir(path_arg))
+ if (OP_TEST != op && 0 == set_basedir(path_arg))
goto out;
if (dbopen(1)) {
goto out;
if (dbopen(1)) {
manpath_free(&dirs);
mchars_free(mc);
mparse_free(mp);
manpath_free(&dirs);
mchars_free(mc);
mparse_free(mp);
- if (strstr(buf, basedir) == buf)
- start = buf + strlen(basedir) + 1;
- else if (OP_TEST == op)
+ else if (strstr(buf, basedir) == buf)
+ start = buf + strlen(basedir);
else {
exitcode = (int)MANDOCLEVEL_BADARG;
say("", "%s: outside base directory", buf);
else {
exitcode = (int)MANDOCLEVEL_BADARG;
say("", "%s: outside base directory", buf);
say(file, "Filename too long");
return;
}
say(file, "Filename too long");
return;
}
- start = strstr(buf, basedir) == buf ?
- buf + strlen(basedir) + 1 : buf;
+ start = buf;
+ if (OP_TEST != op && strstr(buf, basedir) == buf)
+ start += strlen(basedir);
}
mlink = mandoc_calloc(1, sizeof(struct mlink));
}
mlink = mandoc_calloc(1, sizeof(struct mlink));
{
static char startdir[PATH_MAX];
static int fd;
{
static char startdir[PATH_MAX];
static int fd;
/*
* Remember where we started by keeping a fd open to the origin
/*
* Remember where we started by keeping a fd open to the origin
if ('\0' == *startdir) {
if (NULL == getcwd(startdir, PATH_MAX)) {
exitcode = (int)MANDOCLEVEL_SYSERR;
if ('\0' == *startdir) {
if (NULL == getcwd(startdir, PATH_MAX)) {
exitcode = (int)MANDOCLEVEL_SYSERR;
- if (NULL != targetdir)
- say("", "&getcwd");
return(0);
}
if (-1 == (fd = open(startdir, O_RDONLY, 0))) {
return(0);
}
if (-1 == (fd = open(startdir, O_RDONLY, 0))) {
say("", "&open %s", startdir);
return(0);
}
say("", "&open %s", startdir);
return(0);
}
- if (NULL == targetdir)
- targetdir = startdir;
} else {
if (-1 == fd)
return(0);
} else {
if (-1 == fd)
return(0);
say("", "&chdir %s", startdir);
return(0);
}
say("", "&chdir %s", startdir);
return(0);
}
- if (NULL == targetdir) {
- close(fd);
- return(1);
- }
}
if (NULL == realpath(targetdir, basedir)) {
basedir[0] = '\0';
}
if (NULL == realpath(targetdir, basedir)) {
basedir[0] = '\0';
say("", "&chdir");
return(0);
}
say("", "&chdir");
return(0);
}
+ cp = strchr(basedir, '\0');
+ if ('/' != cp[-1]) {
+ if (cp - basedir >= PATH_MAX - 1) {
+ exitcode = (int)MANDOCLEVEL_SYSERR;
+ say("", "Filename too long");
+ return(0);
+ }
+ *cp++ = '/';
+ *cp = '\0';
+ }