From: Ralph Boehme Date: Thu, 4 Sep 2025 15:41:44 +0000 (+0200) Subject: mdssvc: fix filtering by share path prefix X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=594eb4eb0eb4a6c87c5b136106afd05357b80e91;p=thirdparty%2Fsamba.git mdssvc: fix filtering by share path prefix To correctly filter by share path, use a filter with a prefix match. BUG: https://bugzilla.samba.org/show_bug.cgi?id=15927 Signed-off-by: Ralph Boehme Reviewed-by: Volker Lendecke --- diff --git a/python/samba/tests/blackbox/mdsearch.py b/python/samba/tests/blackbox/mdsearch.py index 8d67090e182..7709ae6b032 100644 --- a/python/samba/tests/blackbox/mdsearch.py +++ b/python/samba/tests/blackbox/mdsearch.py @@ -102,8 +102,21 @@ class MdfindBlackboxTests(BlackboxTestCase): json_in = r'''{ "from": 0, "size": 50, "_source": ["path.real"], "query": { - "query_string": { - "query": "(samba*) AND path.real.fulltext:\"%BASEPATH%\"" + "bool": { + "filter": [ + { + "prefix": { + "path.real": "%BASEPATH%/" + } + } + ], + "must": [ + { + "query_string": { + "query": "samba*" + } + } + ] } } }''' diff --git a/python/samba/tests/dcerpc/mdssvc.py b/python/samba/tests/dcerpc/mdssvc.py index cd256548b91..a6e69f15474 100644 --- a/python/samba/tests/dcerpc/mdssvc.py +++ b/python/samba/tests/dcerpc/mdssvc.py @@ -133,8 +133,21 @@ class MdssvcTests(RpcInterfaceTestCase): exp_json_query = r'''{ "from": 0, "size": 50, "_source": ["path.real"], "query": { - "query_string": { - "query": "(samba*) AND path.real.fulltext:\"%BASEPATH%\"" + "bool": { + "filter": [ + { + "prefix": { + "path.real": "%BASEPATH%/" + } + } + ], + "must": [ + { + "query_string": { + "query": "samba*" + } + } + ] } } }''' @@ -165,8 +178,21 @@ class MdssvcTests(RpcInterfaceTestCase): exp_json_query = r'''{ "from": 0, "size": 50, "_source": ["path.real"], "query": { - "query_string": { - "query": "(file.filename:x\\+x OR file.filename:x\\*x OR file.filename:x=x OR file.filename:x'x OR file.filename:x\\?x OR file.filename:x\\ x OR file.filename:x\\(x OR file.filename:x\\\"x OR file.filename:x\\\\x) AND path.real.fulltext:\"%BASEPATH%\"" + "bool": { + "filter": [ + { + "prefix": { + "path.real": "%BASEPATH%/" + } + } + ], + "must": [ + { + "query_string": { + "query": "file.filename:x\\+x OR file.filename:x\\*x OR file.filename:x=x OR file.filename:x'x OR file.filename:x\\?x OR file.filename:x\\ x OR file.filename:x\\(x OR file.filename:x\\\"x OR file.filename:x\\\\x" + } + } + ] } } }''' @@ -207,8 +233,21 @@ class MdssvcTests(RpcInterfaceTestCase): exp_json_query = r'''{ "from": 0, "size": 50, "_source": ["path.real"], "query": { - "query_string": { - "query": "(*samba*) AND path.real.fulltext:\"%BASEPATH%\"" + "bool": { + "filter": [ + { + "prefix": { + "path.real": "%BASEPATH%/" + } + } + ], + "must": [ + { + "query_string": { + "query": "*samba*" + } + } + ] } } }''' diff --git a/source3/rpc_server/mdssvc/es_parser.y b/source3/rpc_server/mdssvc/es_parser.y index 7f3275d3d92..62ca63d90f2 100644 --- a/source3/rpc_server/mdssvc/es_parser.y +++ b/source3/rpc_server/mdssvc/es_parser.y @@ -85,7 +85,6 @@ int mdsyylwrap(void); bool map_spotlight_to_es_query(TALLOC_CTX *mem_ctx, json_t *mappings, - const char *path_scope, const char *query_string, char **_es_query); } @@ -640,7 +639,6 @@ int mdsyylwrap(void) **/ bool map_spotlight_to_es_query(TALLOC_CTX *mem_ctx, json_t *mappings, - const char *path_scope, const char *query_string, char **_es_query) { @@ -691,13 +689,11 @@ bool map_spotlight_to_es_query(TALLOC_CTX *mem_ctx, return false; } - es_query = talloc_asprintf(mem_ctx, - "(%s) AND path.real.fulltext:\\\"%s\\\"", - s.result, path_scope); - TALLOC_FREE(s.frame); + es_query = talloc_strdup(mem_ctx, s.result); if (es_query == NULL) { return false; } + TALLOC_FREE(s.frame); *_es_query = es_query; return true; diff --git a/source3/rpc_server/mdssvc/es_parser_test.c b/source3/rpc_server/mdssvc/es_parser_test.c index 7d88c67abff..dfce8601911 100644 --- a/source3/rpc_server/mdssvc/es_parser_test.c +++ b/source3/rpc_server/mdssvc/es_parser_test.c @@ -41,7 +41,6 @@ int main(int argc, char **argv) char *default_path = NULL; const char *path = NULL; const char *query_string = NULL; - const char *path_scope = NULL; char *es_query = NULL; bool ok; @@ -50,7 +49,6 @@ int main(int argc, char **argv) return 1; } query_string = argv[1]; - path_scope = "/foo/bar"; lp_load_global(get_dyn_CONFIGFILE()); @@ -86,7 +84,6 @@ int main(int argc, char **argv) ok = map_spotlight_to_es_query(mem_ctx, mappings, - path_scope, query_string, &es_query); printf("%s\n", ok ? es_query : "*mapping failed*"); diff --git a/source3/rpc_server/mdssvc/mdssvc_es.c b/source3/rpc_server/mdssvc/mdssvc_es.c index d51441092b4..83550bcdc23 100644 --- a/source3/rpc_server/mdssvc/mdssvc_es.c +++ b/source3/rpc_server/mdssvc/mdssvc_es.c @@ -36,17 +36,26 @@ #undef DBGC_CLASS #define DBGC_CLASS DBGC_RPC_SRV -#define MDSSVC_ELASTIC_QUERY_TEMPLATE \ - "{" \ - " \"from\": %zu," \ - " \"size\": %zu," \ - " \"_source\": [%s]," \ - " \"query\": {" \ - " \"query_string\": {" \ - " \"query\": \"%s\"" \ - " }" \ - " }" \ - "}" +#define MDSSVC_ELASTIC_QUERY_TEMPLATE \ + "{\n" \ + " \"from\": %zu,\n" \ + " \"size\": %zu,\n" \ + " \"_source\": [%s],\n" \ + " \"query\": {\n" \ + " \"bool\": {\n" \ + " \"filter\": [ {\n" \ + " \"prefix\": {\n" \ + " \"path.real\": \"%s/\"\n" \ + " }\n" \ + " } ],\n" \ + " \"must\": [ {\n" \ + " \"query_string\": {\n" \ + " \"query\": \"%s\"\n" \ + " }\n" \ + " } ]\n" \ + " }\n" \ + " }\n" \ + "}\n" #define MDSSVC_ELASTIC_SOURCES \ "\"path.real\"" @@ -413,7 +422,6 @@ static bool mds_es_search(struct sl_query *slq) ok = map_spotlight_to_es_query( s, mds_es_ctx->mdssvc_es_ctx->mappings, - slq->path_scope, slq->query_string, &s->es_query); if (!ok) { @@ -621,6 +629,7 @@ static struct tevent_req *mds_es_search_send(TALLOC_CTX *mem_ctx, s->from, s->size, MDSSVC_ELASTIC_SOURCES, + s->slq->path_scope, s->es_query); if (tevent_req_nomem(elastic_query, req)) { return tevent_req_post(req, ev); diff --git a/source3/rpc_server/mdssvc/test_mdsparser_es.c b/source3/rpc_server/mdssvc/test_mdsparser_es.c index 8fc011220a4..a61f84ceaff 100644 --- a/source3/rpc_server/mdssvc/test_mdsparser_es.c +++ b/source3/rpc_server/mdssvc/test_mdsparser_es.c @@ -28,136 +28,133 @@ #include "lib/param/param.h" #include "rpc_server/mdssvc/es_parser.tab.h" -#define PATH_QUERY_SUBEXPR \ - " AND path.real.fulltext:\\\"/foo/bar\\\"" - static struct { const char *mds; const char *es; } map[] = { { "*==\"samba\"", - "(samba)" PATH_QUERY_SUBEXPR + "samba" }, { "kMDItemTextContent==\"samba\"", - "(content:samba)" PATH_QUERY_SUBEXPR + "content:samba" }, { "_kMDItemGroupId==\"11\"", - "(file.content_type:(application\\\\/pdf))" PATH_QUERY_SUBEXPR + "file.content_type:(application\\\\/pdf)" }, { "kMDItemContentType==\"1\"", - "(file.content_type:(message\\\\/rfc822))" PATH_QUERY_SUBEXPR + "file.content_type:(message\\\\/rfc822)" }, { "kMDItemContentType==\"public.content\"", - "(file.content_type:(message\\\\/rfc822 application\\\\/pdf application\\\\/vnd.oasis.opendocument.presentation image\\\\/* text\\\\/*))" PATH_QUERY_SUBEXPR + "file.content_type:(message\\\\/rfc822 application\\\\/pdf application\\\\/vnd.oasis.opendocument.presentation image\\\\/* text\\\\/*)" }, { "kMDItemContentTypeTree==\"1\"", - "(file.content_type:(message\\\\/rfc822))" PATH_QUERY_SUBEXPR + "file.content_type:(message\\\\/rfc822)" }, { "kMDItemFSContentChangeDate==$time.iso(2018-10-01T10:00:00Z)", - "(file.last_modified:2018\\\\-10\\\\-01T10\\\\:00\\\\:00Z)" PATH_QUERY_SUBEXPR + "file.last_modified:2018\\\\-10\\\\-01T10\\\\:00\\\\:00Z" }, { "kMDItemFSContentChangeDate==\"1\"", - "(file.last_modified:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z)" PATH_QUERY_SUBEXPR + "file.last_modified:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z" }, { "kMDItemFSCreationDate==\"1\"", - "(file.created:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z)" PATH_QUERY_SUBEXPR + "file.created:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z" }, { "kMDItemFSName==\"samba*\"", - "(file.filename:samba*)" PATH_QUERY_SUBEXPR + "file.filename:samba*" }, { "kMDItemFSOwnerGroupID==\"0\"", - "(attributes.owner:0)" PATH_QUERY_SUBEXPR + "attributes.owner:0" }, { "kMDItemFSOwnerUserID==\"0\"", - "(attributes.group:0)" PATH_QUERY_SUBEXPR + "attributes.group:0" }, { "kMDItemFSSize==\"1\"", - "(file.filesize:1)" PATH_QUERY_SUBEXPR + "file.filesize:1" }, { "kMDItemPath==\"/foo/bar\"", - "(path.real:\\\\/foo\\\\/bar)" PATH_QUERY_SUBEXPR + "path.real:\\\\/foo\\\\/bar" }, { "kMDItemAttributeChangeDate==\"1\"", - "(file.last_modified:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z)" PATH_QUERY_SUBEXPR + "file.last_modified:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z" }, { "kMDItemAuthors==\"Chouka\"", - "(meta.author:Chouka)" PATH_QUERY_SUBEXPR + "meta.author:Chouka" }, { "kMDItemContentCreationDate==\"1\"", - "(file.created:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z)" PATH_QUERY_SUBEXPR + "file.created:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z" }, { "kMDItemContentModificationDate==\"1\"", - "(file.last_modified:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z)" PATH_QUERY_SUBEXPR + "file.last_modified:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z" }, { "kMDItemCreator==\"Chouka\"", - "(meta.raw.creator:Chouka)" PATH_QUERY_SUBEXPR + "meta.raw.creator:Chouka" }, { "kMDItemDescription==\"Dog\"", - "(meta.raw.description:Dog)" PATH_QUERY_SUBEXPR + "meta.raw.description:Dog" }, { "kMDItemDisplayName==\"Samba\"", - "(file.filename:Samba)" PATH_QUERY_SUBEXPR + "file.filename:Samba" }, { "kMDItemDurationSeconds==\"1\"", - "(meta.raw.xmpDM\\\\:duration:1)" PATH_QUERY_SUBEXPR + "meta.raw.xmpDM\\\\:duration:1" }, { "kMDItemNumberOfPages==\"1\"", - "(meta.raw.xmpTPg\\\\:NPages:1)" PATH_QUERY_SUBEXPR + "meta.raw.xmpTPg\\\\:NPages:1" }, { "kMDItemTitle==\"Samba\"", - "(meta.title:Samba)" PATH_QUERY_SUBEXPR + "meta.title:Samba" }, { "kMDItemAlbum==\"Red Roses for Me\"", - "(meta.raw.xmpDM\\\\:album:Red\\\\ Roses\\\\ for\\\\ Me)" PATH_QUERY_SUBEXPR + "meta.raw.xmpDM\\\\:album:Red\\\\ Roses\\\\ for\\\\ Me" }, { "kMDItemBitsPerSample==\"1\"", - "(meta.raw.tiff\\\\:BitsPerSample:1)" PATH_QUERY_SUBEXPR + "meta.raw.tiff\\\\:BitsPerSample:1" }, { "kMDItemPixelHeight==\"1\"", - "(meta.raw.Image\\\\ Height:1)" PATH_QUERY_SUBEXPR + "meta.raw.Image\\\\ Height:1" }, { "kMDItemPixelWidth==\"1\"", - "(meta.raw.Image\\\\ Width:1)" PATH_QUERY_SUBEXPR + "meta.raw.Image\\\\ Width:1" }, { "kMDItemResolutionHeightDPI==\"72\"", - "(meta.raw.Y\\\\ Resolution:72)" PATH_QUERY_SUBEXPR + "meta.raw.Y\\\\ Resolution:72" }, { "kMDItemResolutionWidthDPI==\"72\"", - "(meta.raw.X\\\\ Resolution:72)" PATH_QUERY_SUBEXPR + "meta.raw.X\\\\ Resolution:72" },{ "*!=\"samba\"", - "((NOT samba))" PATH_QUERY_SUBEXPR + "(NOT samba)" }, { "kMDItemFSSize!=\"1\"", - "((NOT file.filesize:1))" PATH_QUERY_SUBEXPR + "(NOT file.filesize:1)" }, { "kMDItemFSSize>\"1\"", - "(file.filesize:{1 TO *})" PATH_QUERY_SUBEXPR + "file.filesize:{1 TO *}" }, { "kMDItemFSSize<\"1\"", - "(file.filesize:{* TO 1})" PATH_QUERY_SUBEXPR + "file.filesize:{* TO 1}" }, { "kMDItemFSCreationDate!=\"1\"", - "((NOT file.created:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z))" PATH_QUERY_SUBEXPR + "(NOT file.created:2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z)" }, { "kMDItemFSCreationDate>\"1\"", - "(file.created:{2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z TO *})" PATH_QUERY_SUBEXPR + "file.created:{2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z TO *}" }, { "kMDItemFSCreationDate<\"1\"", - "(file.created:{* TO 2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z})" PATH_QUERY_SUBEXPR + "file.created:{* TO 2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z}" }, { "kMDItemFSName==\"Samba\"||kMDItemTextContent==\"Samba\"", - "(file.filename:Samba OR content:Samba)" PATH_QUERY_SUBEXPR + "file.filename:Samba OR content:Samba" }, { "kMDItemFSName==\"Samba\"&&kMDItemTextContent==\"Samba\"", - "((file.filename:Samba) AND (content:Samba))" PATH_QUERY_SUBEXPR + "(file.filename:Samba) AND (content:Samba)" }, { "InRange(kMDItemFSCreationDate,1,2)", - "(file.created:[2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z TO 2001\\\\-01\\\\-01T00\\\\:00\\\\:02Z])" PATH_QUERY_SUBEXPR + "file.created:[2001\\\\-01\\\\-01T00\\\\:00\\\\:01Z TO 2001\\\\-01\\\\-01T00\\\\:00\\\\:02Z]" }, { "InRange(kMDItemFSSize,1,2)", - "(file.filesize:[1 TO 2])" PATH_QUERY_SUBEXPR + "file.filesize:[1 TO 2]" } }; @@ -167,38 +164,37 @@ static struct { } map_ignore_failures[] = { { "*==\"Samba\"||foo==\"bar\"", - "(Samba)" PATH_QUERY_SUBEXPR + "Samba" }, { "*==\"Samba\"&&foo==\"bar\"", - "(Samba)" PATH_QUERY_SUBEXPR + "Samba" }, { "*==\"Samba\"||kMDItemContentType==\"666\"", - "(Samba)" PATH_QUERY_SUBEXPR + "Samba" }, { "*==\"Samba\"&&kMDItemContentType==\"666\"", - "(Samba)" PATH_QUERY_SUBEXPR + "Samba" }, { "*==\"Samba\"||foo==\"bar\"||kMDItemContentType==\"666\"", - "(Samba)" PATH_QUERY_SUBEXPR + "Samba" }, { "*==\"Samba\"&&foo==\"bar\"&&kMDItemContentType==\"666\"", - "(Samba)" PATH_QUERY_SUBEXPR + "Samba" }, { "foo==\"bar\"||kMDItemContentType==\"666\"||*==\"Samba\"||x!=\"6\"", - "(Samba)" PATH_QUERY_SUBEXPR + "Samba" }, { "*==\"Samba\"||InRange(foo,1,2)", - "(Samba)" PATH_QUERY_SUBEXPR + "Samba" }, { "*==\"Samba\"||foo==$time.iso(2018-10-01T10:00:00Z)", - "(Samba)" PATH_QUERY_SUBEXPR + "Samba" } }; static void test_mdsparser_es(void **state) { TALLOC_CTX *frame = talloc_stackframe(); - const char *path_scope = "/foo/bar"; char *es_query = NULL; char *default_path = NULL; const char *path = NULL; @@ -226,7 +222,6 @@ static void test_mdsparser_es(void **state) DBG_DEBUG("Mapping: %s\n", map[i].mds); ok = map_spotlight_to_es_query(frame, mappings, - path_scope, map[i].mds, &es_query); assert_true(ok); @@ -245,7 +240,6 @@ static void test_mdsparser_es(void **state) DBG_DEBUG("Mapping: %s\n", map_ignore_failures[i].mds); ok = map_spotlight_to_es_query(frame, mappings, - path_scope, map_ignore_failures[i].mds, &es_query); assert_true(ok);