clamav/win32/compat/glob.c
Micah Snyder (micasnyd) 9e20cdf6ea Add CMake build tooling
This patch adds experimental-quality CMake build tooling.

The libmspack build required a modification to use "" instead of <> for
header #includes. This will hopefully be included in the libmspack
upstream project when adding CMake build tooling to libmspack.

Removed use of libltdl when using CMake.

Flex & Bison are now required to build.

If -DMAINTAINER_MODE, then GPERF is also required, though it currently
doesn't actually do anything.  TODO!

I found that the autotools build system was generating the lexer output
but not actually compiling it, instead using previously generated (and
manually renamed) lexer c source. As a consequence, changes to the .l
and .y files weren't making it into the build. To resolve this, I
removed generated flex/bison files and fixed the tooling to use the
freshly generated files. Flex and bison are now required build tools.
On Windows, this adds a dependency on the winflexbison package,
which can be obtained using Chocolatey or may be manually installed.

CMake tooling only has partial support for building with external LLVM
library, and no support for the internal LLVM (to be removed in the
future). I.e. The CMake build currently only supports the bytecode
interpreter.

Many files used include paths relative to the top source directory or
relative to the current project, rather than relative to each build
target. Modern CMake support requires including internal dependency
headers the same way you would external dependency headers (albeit
with "" instead of <>). This meant correcting all header includes to
be relative to the build targets and not relative to the workspace.

For example, ...

```c
include "../libclamav/clamav.h"
include "clamd/clamd_others.h"
```

... becomes:

```c
// libclamav
include "clamav.h"

// clamd
include "clamd_others.h"
```

Fixes header name conflicts by renaming a few of the files.

Converted the "shared" code into a static library, which depends on
libclamav. The ironically named "shared" static library provides
features common to the ClamAV apps which are not required in
libclamav itself and are not intended for use by downstream projects.
This change was required for correct modern CMake practices but was
also required to use the automake "subdir-objects" option.
This eliminates warnings when running autoreconf which, in the next
version of autoconf & automake are likely to break the build.

libclamav used to build in multiple stages where an earlier stage is
a static library containing utils required by the "shared" code.
Linking clamdscan and clamdtop with this libclamav utils static lib
allowed these two apps to function without libclamav. While this is
nice in theory, the practical gains are minimal and it complicates
the build system. As such, the autotools and CMake tooling was
simplified for improved maintainability and this feature was thrown
out. clamdtop and clamdscan now require libclamav to function.

Removed the nopthreads version of the autotools
libclamav_internal_utils static library and added pthread linking to
a couple apps that may have issues building on some platforms without
it, with the intention of removing needless complexity from the
source. Kept the regular version of libclamav_internal_utils.la
though it is no longer used anywhere but in libclamav.

Added an experimental doxygen build option which attempts to build
clamav.h and libfreshclam doxygen html docs.

The CMake build tooling also may build the example program(s), which
isn't a feature in the Autotools build system.

Changed C standard to C90+ due to inline linking issues with socket.h
when linking libfreshclam.so on Linux.

Generate common.rc for win32.

Fix tabs/spaces in shared Makefile.am, and remove vestigial ifndef
from misc.c.

Add CMake files to the automake dist, so users can try the new
CMake tooling w/out having to build from a git clone.

clamonacc changes:
- Renamed FANOTIFY macro to HAVE_SYS_FANOTIFY_H to better match other
  similar macros.
- Added a new clamav-clamonacc.service systemd unit file, based on
  the work of ChadDevOps & Aaron Brighton.
- Added missing clamonacc man page.

Updates to clamdscan man page, add missing options.

Remove vestigial CL_NOLIBCLAMAV definitions (all apps now use
libclamav).

Rename Windows mspack.dll to libmspack.dll so all ClamAV-built
libraries have the lib-prefix with Visual Studio as with CMake.
2020-08-13 00:25:34 -07:00

321 lines
12 KiB
C

/*
* Copyright (C) 2013-2020 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
* Copyright (C) 2010-2013 Sourcefire, Inc.
*
* Authors: aCaB <acab@clamav.net>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* 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., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/
#include "dirent.h"
#include "libgen.h"
#include <stdio.h>
#include <malloc.h>
#include <WinNls.h>
/*
I GIVE UP! The CRT is b0rked and cannot be helped.
The documentation suggests to handle globbing automagically via linking in
the msvc-provided setargv.obj. Unfortunately that thing has got any sort of bugs
and perverts filenames rather than expanding them.
The other suggested approach is to override the crt-builtin "_setargv" with a
custom routine to manually process the command line args before they are fed to main.
Now this is even funnier: the hook is indeed called bedore main(), but then its work
is discarded and replaced with that of the default parser... how useful!
After some debugging the problem turned out to be in the design. The flow is like:
pre_c_init -> _setargv
then
pre_cpp_init -> _setargv
Looking at the code in there, it clearly shows that both _init functions are 99% the
same. In case you are wondering... yes, everything is done twice! Including the
command line parsing...
There is however a small difference: while pre_c_init correctly calls the custom
_setargv if present, pre_cpp_init always calls the crt-builtin!
If you want to double check this link the msvc-provided noarg.obj in, then break in main
and see how argv and argc are actually set... If you try with setargv.obj, instead, you
will see that it apparently works, but that's just a hack for which pre_cpp_init ends
up calling __setargv instead of _setargv based on the _dowildcard flag.
So the way to FIX this mess involves a small trick: in the _setargv override I
don't just parse the command line properly, but I also turn my arguments into a new
command line, which I use to replace the existing one. The replaced line will then be
processed by pre_cpp_init and everything is fine.
To replace the original line with the fixed one it's sufficient to replace the pointer
returned by the __p__acmdln() function. The proto it "extern char **__p__acmdln(void)".
Of course the trick only works if the line is crafted in a way that can be understood
and parsed by the _setargv builtin.
Apparently, however, the authors of this pile of crap which goes under the name of CRT,
are not even able to keep their bugs consistent. So, while in MSVC 2008 it was enough to
put each arg in "'s, in MSVC 2008 SP 1 you additionally need to take care of "escaped"
quotes. I.e.: \".
Whatever...
I've given up trying to fit globbing below main. It's now hooked into main via a
#define wrapper.
*/
static int glob_add(char *path, int *argc, char ***argv)
{
char *tail = strchr(path, '*'), *tailqmark;
char *dup1, *dup2, *dir, *base, *taildirsep, *tailwldsep;
struct dirent *de;
int baselen, taillen, dirlen, mergedir = 0, outlen = 0;
int qmarklen = 0;
DIR *d;
void *p;
if (strlen(path) > 4 && !memcmp(path, "\\\\?\\", 4))
tailqmark = strchr(&path[4], '?');
else
tailqmark = strchr(path, '?');
if (tailqmark && (!tail || tailqmark < tail))
tail = tailqmark;
if (!tail) {
p = realloc(*argv, sizeof(**argv) * (*argc + 1));
if (p == NULL) {
/* realloc() failed, print warning */
fprintf(stderr, "warning: realloc() for '*argv' failed\n");
return -1;
}
*argv = (char **)p;
(*argv)[*argc] = path;
(*argc)++;
return strlen(path);
}
if (tail != path && tail[-1] == '\\') {
tail[-1] = '\0';
mergedir = 1;
}
while (*tail) {
if (*tail == '?') {
if (tail == tailqmark || qmarklen)
qmarklen++;
*tail = 0;
} else if (*tail == '*') {
*tail = '\0';
qmarklen = 0;
} else
break;
tail++;
}
taillen = strlen(tail);
taildirsep = strchr(tail, '\\');
if (taildirsep && taildirsep - tail == taillen - 1) {
*taildirsep = '\0';
taildirsep = NULL;
taillen--;
}
if (!taildirsep)
taildirsep = tail + taillen;
tailwldsep = strchr(tail, '*');
tailqmark = strchr(tail, '?');
if (tailqmark && (!tailwldsep || tailqmark < tailwldsep))
tailwldsep = tailqmark;
if (!tailwldsep)
tailwldsep = tail + taillen;
baselen = strlen(path) + 1;
dup1 = (char *)_alloca(baselen * 2);
memcpy(dup1, path, baselen);
dup2 = dup1 + baselen;
memcpy(dup2, path, baselen);
if (!mergedir) {
dir = dirname(dup1);
base = basename(dup2);
} else {
dir = dup1;
base = dup2;
*dup2 = '\0';
}
dirlen = strlen(dir);
baselen = strlen(base);
d = opendir(dir);
while (d && (de = readdir(d))) {
int namelen = strlen(de->d_name);
char *newpath;
if (!strcmp(de->d_name, ".") || !strcmp(de->d_name, "..")) continue;
if (namelen < baselen) continue;
if (strncasecmp(base, de->d_name, baselen)) continue;
if (de->d_type == DT_DIR && taildirsep < tailwldsep) {
int d_taillen = taildirsep - tail;
if (namelen < baselen + d_taillen) continue;
if (strncasecmp(tail, &de->d_name[namelen - d_taillen], d_taillen)) continue;
newpath = (char *)malloc(dirlen + namelen + taillen - d_taillen + 3);
if (newpath == NULL) { /* oops, malloc() has failed */
fprintf(stderr, "warning: malloc() failed in function 'globadd'...\n");
return -1;
}
sprintf(newpath, "%s\\%s\\%s", dir, de->d_name, &tail[d_taillen + 1]);
outlen += glob_add(newpath, argc, argv);
} else {
int d_taillen = tailwldsep - tail;
char *start;
if (namelen < baselen + d_taillen) continue;
if (qmarklen && baselen + qmarklen + d_taillen != namelen) continue;
if (d_taillen == taillen) {
start = &de->d_name[namelen - d_taillen];
namelen = d_taillen;
} else {
start = &de->d_name[baselen];
namelen -= baselen;
}
for (; namelen >= d_taillen; start++, namelen--) {
if (strncasecmp(start, tail, d_taillen)) continue;
newpath = (char *)malloc(dirlen + (start - de->d_name) + taillen + 2);
if (newpath == NULL) { /* oops, malloc() has failed */
fprintf(stderr, "warning: malloc() failed in function 'globadd'...\n");
return -1;
}
sprintf(newpath, "%s\\", dir);
memcpy(&newpath[dirlen + 1], de->d_name, start - de->d_name);
strcpy(&newpath[dirlen + 1 + start - de->d_name], tail);
outlen += glob_add(newpath, argc, argv);
}
}
}
if (d) closedir(d);
_freea(dup1);
free(path);
return outlen;
}
void w32_glob(int *argc_ptr, char ***argv_ptr)
{
wchar_t *wtmp = GetCommandLineW();
char *cur, *begparm = NULL, *endparm = NULL;
char **argv = NULL, c;
int argc = 0, in_sq = 0, in_dq = 0, need_glob = 0, allarglen = 0, linelen;
void *p;
linelen = wcslen(wtmp);
cur = (char *)_alloca(linelen * 6 + 1);
if (!WideCharToMultiByte(CP_UTF8, 0, wtmp, -1, cur, linelen * 6 + 1, NULL, NULL))
cur = GetCommandLineA();
do {
c = *cur;
switch (c) {
case '\0':
endparm = cur;
break;
case ' ':
if (begparm && !(in_sq | in_dq))
endparm = cur;
break;
case '\'':
if (!in_dq) {
in_sq = !in_sq;
if (!in_sq)
endparm = cur;
}
break;
case '"':
if (!in_sq) {
in_dq = !in_dq;
if (!in_dq)
endparm = cur;
}
break;
case '*':
case '?':
if (!in_sq)
need_glob = 1;
default:
if (!begparm) {
begparm = cur;
endparm = NULL;
}
}
if (begparm && endparm) {
if (begparm < endparm) {
int arglen = 0;
char *path = (char *)malloc(endparm - begparm + 1), *quotes;
if (path == NULL) { /* oops, malloc() failed */
fprintf(stderr, "warning: malloc() failed for '*path'...\n");
return;
}
memcpy(path, begparm, endparm - begparm);
path[endparm - begparm] = '\0';
quotes = path;
while ((quotes = strchr(quotes, '"')))
memmove(quotes, quotes + 1, (endparm - begparm) - (quotes - path));
if (argc && need_glob) {
arglen = glob_add(path, &argc, &argv);
if (!arglen) {
path = (char *)malloc(endparm - begparm + 1);
if (path == NULL) { /* oops, malloc() failed */
fprintf(stderr, "warning: malloc failed for 'path'...\n");
return;
}
memcpy(path, begparm, endparm - begparm);
path[endparm - begparm] = '\0';
}
}
if (!arglen) {
p = realloc(argv, sizeof(*argv) * (argc + 1));
if (p == NULL) { /* realloc() failed */
fprintf(stderr, "warning: realloc() for 'argv' failed, original value unchanged...\n");
return;
}
argv = (char **)p;
argv[argc] = path;
argc++;
arglen = endparm - begparm;
}
allarglen += arglen;
}
need_glob = 0;
in_sq = 0;
in_dq = 0;
begparm = NULL;
endparm = NULL;
}
cur++;
} while (c);
if (argc) {
int i, argvlen = sizeof(*argv) * (argc + 1), argclen = 0;
p = realloc(argv, argvlen + allarglen + argc);
if (p == NULL) { /* oops, realloc() failed */
fprintf(stderr, "warning: realloc() for 'argv' failed, original value unchanged...\n");
return;
}
argv = (char **)p;
argv[argc] = NULL;
for (i = 0; i < argc; i++) {
int curlen = strlen(argv[i]) + 1;
char *curarg = (char *)argv + argvlen + argclen;
memcpy(curarg, argv[i], curlen);
argclen += curlen;
free(argv[i]);
argv[i] = curarg;
}
}
*argc_ptr = argc;
*argv_ptr = argv;
}