src: switch from fnmatch to g_pattern_match_simple

The g_pattern_match function_simple is an acceptably close
approximation of fnmatch for libvirt's needs.

In contrast to fnmatch(), the '/' character can be matched
by the wildcards, there are no '[...]' character ranges and
'*' and '?' can not be escaped to include them literally in
a pattern.

Reviewed-by: Fabiano Fidêncio <fidencio@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2019-12-20 16:02:49 +00:00
parent d0312c584f
commit f7df985684
8 changed files with 25 additions and 34 deletions

View File

@ -20,8 +20,6 @@
#include <config.h> #include <config.h>
#include <fnmatch.h>
#include "qemu_firmware.h" #include "qemu_firmware.h"
#include "qemu_interop_config.h" #include "qemu_interop_config.h"
#include "configmake.h" #include "configmake.h"
@ -921,7 +919,7 @@ qemuFirmwareMatchesMachineArch(const qemuFirmware *fw,
continue; continue;
for (j = 0; j < fw->targets[i]->nmachines; j++) { for (j = 0; j < fw->targets[i]->nmachines; j++) {
if (fnmatch(fw->targets[i]->machines[j], machine, 0) == 0) if (g_pattern_match_simple(fw->targets[i]->machines[j], machine))
return true; return true;
} }
} }

View File

@ -262,7 +262,9 @@
# #
# "C=GB,ST=London,L=London,O=Red Hat,CN=*" # "C=GB,ST=London,L=London,O=Red Hat,CN=*"
# #
# See the POSIX fnmatch function for the format of the wildcards. # See the g_pattern_match function for the format of the wildcards:
#
# https://developer.gnome.org/glib/stable/glib-Glob-style-pattern-matching.html
# #
# NB If this is an empty list, no client can connect, so comment out # NB If this is an empty list, no client can connect, so comment out
# entirely rather than using empty list to disable these checks # entirely rather than using empty list to disable these checks
@ -288,7 +290,9 @@
# #
# "*@EXAMPLE.COM" # "*@EXAMPLE.COM"
# #
# See the POSIX fnmatch function for the format of the wildcards. # See the g_pattern_match function for the format of the wildcards.
#
# https://developer.gnome.org/glib/stable/glib-Glob-style-pattern-matching.html
# #
# NB If this is an empty list, no client can connect, so comment out # NB If this is an empty list, no client can connect, so comment out
# entirely rather than using empty list to disable these checks # entirely rather than using empty list to disable these checks

View File

@ -20,8 +20,6 @@
#include <config.h> #include <config.h>
#include <fnmatch.h>
#include "virnetsaslcontext.h" #include "virnetsaslcontext.h"
#include "virnetmessage.h" #include "virnetmessage.h"
@ -155,17 +153,10 @@ int virNetSASLContextCheckIdentity(virNetSASLContextPtr ctxt,
} }
while (*wildcards) { while (*wildcards) {
int rv = fnmatch(*wildcards, identity, 0); if (g_pattern_match_simple(*wildcards, identity)) {
if (rv == 0) {
ret = 1; ret = 1;
goto cleanup; /* Successful match */ goto cleanup; /* Successful match */
} }
if (rv != FNM_NOMATCH) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Malformed TLS whitelist regular expression '%s'"),
*wildcards);
goto cleanup;
}
wildcards++; wildcards++;
} }

View File

@ -21,7 +21,6 @@
#include <config.h> #include <config.h>
#include <unistd.h> #include <unistd.h>
#include <fnmatch.h>
#include <gnutls/gnutls.h> #include <gnutls/gnutls.h>
#include <gnutls/crypto.h> #include <gnutls/crypto.h>
@ -361,15 +360,8 @@ virNetTLSContextCheckCertDNWhitelist(const char *dname,
const char *const*wildcards) const char *const*wildcards)
{ {
while (*wildcards) { while (*wildcards) {
int ret = fnmatch(*wildcards, dname, 0); if (g_pattern_match_simple(*wildcards, dname))
if (ret == 0) /* Successful match */
return 1; return 1;
if (ret != FNM_NOMATCH) {
virReportError(VIR_ERR_INTERNAL_ERROR,
_("Malformed TLS whitelist regular expression '%s'"),
*wildcards);
return -1;
}
wildcards++; wildcards++;
} }

View File

@ -36,7 +36,6 @@
#if HAVE_SYS_UN_H #if HAVE_SYS_UN_H
# include <sys/un.h> # include <sys/un.h>
#endif #endif
#include <fnmatch.h>
#include "virerror.h" #include "virerror.h"
#include "virlog.h" #include "virlog.h"
@ -488,7 +487,7 @@ virLogSourceUpdate(virLogSourcePtr source)
size_t i; size_t i;
for (i = 0; i < virLogNbFilters; i++) { for (i = 0; i < virLogNbFilters; i++) {
if (fnmatch(virLogFilters[i]->match, source->name, 0) == 0) { if (g_pattern_match_simple(virLogFilters[i]->match, source->name)) {
priority = virLogFilters[i]->priority; priority = virLogFilters[i]->priority;
break; break;
} }
@ -1338,7 +1337,7 @@ virLogFilterNew(const char *match,
return NULL; return NULL;
} }
/* We must treat 'foo' as equiv to '*foo*' for fnmatch /* We must treat 'foo' as equiv to '*foo*' for g_pattern_match
* todo substring matches, so add 2 extra bytes * todo substring matches, so add 2 extra bytes
*/ */
if (VIR_ALLOC_N_QUIET(mdup, mlen + 3) < 0) if (VIR_ALLOC_N_QUIET(mdup, mlen + 3) < 0)

View File

@ -183,7 +183,9 @@ tls_no_verify_certificate = 1
# #
# "C=GB,ST=London,L=London,O=Red Hat,CN=*" # "C=GB,ST=London,L=London,O=Red Hat,CN=*"
# #
# See the POSIX fnmatch function for the format of the wildcards. # See the g_pattern_match function for the format of the wildcards.
#
# https://developer.gnome.org/glib/stable/glib-Glob-style-pattern-matching.html
# #
# NB If this is an empty list, no client can connect, so comment out # NB If this is an empty list, no client can connect, so comment out
# entirely rather than using empty list to disable these checks # entirely rather than using empty list to disable these checks
@ -200,7 +202,9 @@ tls_allowed_dn_list = ["DN1", "DN2"]
# #
# "*@EXAMPLE.COM" # "*@EXAMPLE.COM"
# #
# See the POSIX fnmatch function for the format of the wildcards. # See the g_pattern_match function for the format of the wildcards.
#
# https://developer.gnome.org/glib/stable/glib-Glob-style-pattern-matching.html
# #
# NB If this is an empty list, no client can connect, so comment out # NB If this is an empty list, no client can connect, so comment out
# entirely rather than using empty list to disable these checks # entirely rather than using empty list to disable these checks

View File

@ -147,7 +147,9 @@ tls_no_verify_certificate = 1
# #
# "C=GB,ST=London,L=London,O=Red Hat,CN=*" # "C=GB,ST=London,L=London,O=Red Hat,CN=*"
# #
# See the POSIX fnmatch function for the format of the wildcards. # See the g_pattern_match function for the format of the wildcards.
#
# https://developer.gnome.org/glib/stable/glib-Glob-style-pattern-matching.html
# #
# NB If this is an empty list, no client can connect, so comment out # NB If this is an empty list, no client can connect, so comment out
# entirely rather than using empty list to disable these checks # entirely rather than using empty list to disable these checks
@ -162,7 +164,9 @@ tls_allowed_dn_list = [ "DN1", "DN2" ]
# #
# "*@EXAMPLE.COM" # "*@EXAMPLE.COM"
# #
# See the POSIX fnmatch function for the format of the wildcards. # See the g_pattern_match function for the format of the wildcards.
#
# https://developer.gnome.org/glib/stable/glib-Glob-style-pattern-matching.html
# #
# NB If this is an empty list, no client can connect, so comment out # NB If this is an empty list, no client can connect, so comment out
# entirely rather than using empty list to disable these checks # entirely rather than using empty list to disable these checks

View File

@ -19,7 +19,6 @@
*/ */
#include <config.h> #include <config.h>
#include <fnmatch.h>
#include <getopt.h> #include <getopt.h>
#include <signal.h> #include <signal.h>
#include <stdarg.h> #include <stdarg.h>
@ -67,14 +66,14 @@ static int virLoginShellAllowedUser(virConfPtr conf,
for (i = 0; i < ngroups; i++) { for (i = 0; i < ngroups; i++) {
if (!(gname = virGetGroupName(groups[i]))) if (!(gname = virGetGroupName(groups[i])))
continue; continue;
if (fnmatch(entry, gname, 0) == 0) { if (g_pattern_match_simple(entry, gname)) {
ret = 0; ret = 0;
goto cleanup; goto cleanup;
} }
VIR_FREE(gname); VIR_FREE(gname);
} }
} else { } else {
if (fnmatch(entry, name, 0) == 0) { if (g_pattern_match_simple(entry, name)) {
ret = 0; ret = 0;
goto cleanup; goto cleanup;
} }