[PATCH] Spotlight

Ralph Böhme rb at sernet.de
Fri Jul 3 17:09:22 CEST 2015


Hi,

attached are the latest Spotlight patches. They can also be found in
my spotlight branch.

All reviewed by metze and ready for master. :)

For those who dare to give it a whirl, instructions can be found in
the Samba wiki:

<https://wiki.samba.org/index.php/Spotlight>

-Ralph

-- 
SerNet GmbH, Bahnhofsallee 1b, 37081 Göttingen
phone: +49-551-370000-0, fax: +49-551-370000-9
AG Göttingen, HRB 2816, GF: Dr. Johannes Loxen
http://www.sernet.de,mailto:kontakt@sernet.de
-------------- next part --------------
From 3a67230003e32d84a3483f77b39777f624347f82 Mon Sep 17 00:00:00 2001
From: Ralph Boehme <rb at sernet.de>
Date: Wed, 23 Jul 2014 07:15:50 +0200
Subject: [PATCH 01/12] s3-mdssvc: add configure option --enable-spotlight

configure check with pkg-config for libtracker-sparql, default is
disabled.

Signed-off-by: Ralph Boehme <slow at samba.org>
Reviewed-by: Stefan Metzmacher <metze at samba.org>
---
 source3/wscript | 20 +++++++++++++++++++-
 1 file changed, 19 insertions(+), 1 deletion(-)

diff --git a/source3/wscript b/source3/wscript
index c9f3a37..a9a7c14 100644
--- a/source3/wscript
+++ b/source3/wscript
@@ -62,6 +62,7 @@ def set_options(opt):
                   help=("enable support for VxFS (default=no)"),
                   action="store_true", dest='enable_vxfs', default=False)
 
+    opt.SAMBA3_ADD_OPTION('spotlight', with_name="enable", without_name="disable", default=False)
 
 def configure(conf):
     from samba_utils import TO_LIST
@@ -1552,7 +1553,24 @@ main() {
         else:
             conf.fatal('AFS headers not available, but --with-fake-kaserver was specified')
 
-
+    conf.env['libtracker']=''
+    conf.env.with_spotlight = False
+    if Options.options.with_spotlight:
+        versions = ['1.0', '0.16', '0.14']
+        for version in versions:
+            testlib = 'tracker-sparql-' + version
+            if conf.check_cfg(package=testlib,
+                              args='--cflags --libs',
+                              mandatory=False):
+                conf.SET_TARGET_TYPE(testlib, 'SYSLIB')
+                conf.env['libtracker'] = testlib
+                conf.env.with_spotlight = True
+                conf.DEFINE('WITH_SPOTLIGHT', '1')
+                break
+
+        if not conf.env.with_spotlight:
+            conf.fatal("Spotlight support requested but tracker-sparql library missing")
+        Logs.info("building with Spotlight support")
 
     default_static_modules.extend(TO_LIST('''pdb_smbpasswd pdb_tdbsam pdb_wbc_sam
                                       auth_sam auth_unix auth_winbind auth_wbc
-- 
2.1.0


From 68f8955f717c9bc12171dc168fe612790a45b5dd Mon Sep 17 00:00:00 2001
From: Ralph Boehme <rb at sernet.de>
Date: Wed, 23 Jul 2014 09:58:45 +0200
Subject: [PATCH 02/12] mdssvc: IDL file for new RPC service

'mdssvc' aka 'Metadata Search Service' is an RPC service used by Apple
for passing marshalled Spotlight search queries and results between
client to server.

Signed-off-by: Ralph Boehme <slow at samba.org>
Reviewed-by: Stefan Metzmacher <metze at samba.org>
---
 librpc/idl/mdssvc.idl    | 68 ++++++++++++++++++++++++++++++++++++++++++++++++
 librpc/idl/wscript_build |  3 ++-
 librpc/wscript_build     | 12 ++++++++-
 3 files changed, 81 insertions(+), 2 deletions(-)
 create mode 100644 librpc/idl/mdssvc.idl

diff --git a/librpc/idl/mdssvc.idl b/librpc/idl/mdssvc.idl
new file mode 100644
index 0000000..bfb27c4
--- /dev/null
+++ b/librpc/idl/mdssvc.idl
@@ -0,0 +1,68 @@
+import "misc.idl";
+[
+	uuid("885d85fb-c754-4062-a0e7-6872ce0064f4"),
+	endpoint("ncacn_np:[\\pipe\\mdssvc]", "ncalrpc:"),
+	version(2.0),
+	helpstring("Spotlight metadata search service")
+]
+interface mdssvc
+{
+	void mdssvc_open(
+		[in,out,ref]                             uint32         *device_id,
+		[in,out,ref]                             uint32         *unkn2, /* always 0x17 ? */
+		[in,out,ref]                             uint32         *unkn3, /* always 0 ? */
+		[in][string,charset(UTF8),size_is(1025)] uint8           share_mount_path[],
+		[in][string,charset(UTF8),size_is(1025)] uint8           share_name[],
+		[out,string,charset(UTF8),size_is(1025)] uint8           share_path[],
+		[out,ref]                                policy_handle  *handle
+	);
+
+	void mdssvc_unknown1(
+		[in]        policy_handle   handle,
+		[in]        uint32          unkn1, /* always 0, some status ? */
+		[in]        uint32          device_id,
+		[in]        uint32          unkn3, /* = mdssvc_open.unkn2 ? */
+		[in]        uint32          unkn4, /* always 0, some status ? */
+		[in]        uint32          uid,
+		[in]        uint32          gid,
+		[out,ref]   uint32         *status,
+		[out,ref]   uint32         *flags, /* always 0x6b000001 ? */
+		[out,ref]   uint32         *unkn7  /* always 0 ? */
+	);
+
+	typedef [public] struct {
+		uint32 length;
+		uint32 size;
+		[size_is(size),length_is(length)] uint8 *spotlight_blob;
+	} mdssvc_blob;
+
+	void mdssvc_cmd(
+		[in]        policy_handle   handle,
+		[in]        uint32          unkn1, /* always 0, status ? */
+		[in]        uint32          device_id,
+		[in]        uint32          unkn3, /* = mdssvc_open.unkn2 ? */
+		[in]        uint32          unkn4, /* always 0 ? */
+		[in]        uint32          flags, /* always 0x6b000001 ? */
+		[in]        mdssvc_blob     request_blob,
+		[in]        uint32          unkn5, /* always 0 ? */
+		[in]        uint32          max_fragment_size1,
+		[in]        uint32          unkn6, /* always 1 ? */
+		/* always max_fragment_size1 = max_fragment_size2 ? */
+		[in]        uint32          max_fragment_size2,
+		[in]        uint32          unkn7, /* always 0 ? */
+		[in]        uint32          unkn8, /* always 0 ? */
+		[out,ref]   uint32         *status,
+		[out,ref]   mdssvc_blob    *response_blob,
+		[out,ref]   uint32         *unkn9  /* always 0 ? */
+	);
+
+	void mdssvc_close(
+		[in]        policy_handle   in_handle,
+		[in]        uint32          unkn1, /* always 0, some status ? */
+		[in]        uint32          device_id,
+		[in]        uint32          unkn2, /* = mdssvc_open.unkn2 ? */
+		[in]        uint32          unkn3, /* always 0, some status ? */
+		[out,ref]   policy_handle  *out_handle,
+		[out,ref]   uint32         *status
+	);
+}
diff --git a/librpc/idl/wscript_build b/librpc/idl/wscript_build
index 4b56af6..6316edd 100644
--- a/librpc/idl/wscript_build
+++ b/librpc/idl/wscript_build
@@ -13,7 +13,8 @@ bld.SAMBA_PIDL_LIST('PIDL',
                        notify.idl
                        smb2_lease_struct.idl
                        policyagent.idl scerpc.idl svcctl.idl wkssvc.idl eventlog6.idl backupkey.idl
-                       fsrvp.idl bkupblobs.idl fscc.idl frsblobs.idl witness.idl clusapi.idl''',
+                       fsrvp.idl bkupblobs.idl fscc.idl frsblobs.idl witness.idl clusapi.idl
+                       mdssvc.idl''',
                     options='--header --ndr-parser --samba3-ndr-server --server --client --python',
                     output_dir='../gen_ndr')
 
diff --git a/librpc/wscript_build b/librpc/wscript_build
index 212733a..1594e72 100644
--- a/librpc/wscript_build
+++ b/librpc/wscript_build
@@ -330,6 +330,11 @@ bld.SAMBA_SUBSYSTEM('NDR_CLUSAPI',
     public_deps='ndr'
     )
 
+bld.SAMBA_SUBSYSTEM('NDR_MDSSVC',
+    source='gen_ndr/ndr_mdssvc.c',
+    public_deps='ndr'
+    )
+
 bld.SAMBA_SUBSYSTEM('NDR_DCERPC',
     source='gen_ndr/ndr_dcerpc.c ndr/ndr_dcerpc.c',
     public_deps='ndr',
@@ -665,12 +670,17 @@ bld.SAMBA_SUBSYSTEM('RPC_NDR_CLUSAPI',
     public_deps='dcerpc-binding NDR_CLUSAPI'
     )
 
+bld.SAMBA_SUBSYSTEM('RPC_NDR_MDSSVC',
+    source='gen_ndr/ndr_mdssvc_c.c',
+    public_deps='dcerpc-binding NDR_MDSSVC'
+    )
+
 # a grouping library for NDR subsystems that may be used by more than one target
 bld.SAMBA_LIBRARY('ndr-samba',
     source=[],
     deps='''NDR_DRSBLOBS NDR_DRSUAPI NDR_IDMAP NDR_NTLMSSP NDR_SCHANNEL NDR_MGMT
     NDR_DNSP NDR_EPMAPPER NDR_XATTR NDR_UNIXINFO NDR_NAMED_PIPE_AUTH NDR_DCOM
-    NDR_NTPRINTING NDR_FSRVP NDR_WITNESS NDR_OPEN_FILES NDR_SMBXSRV''',
+    NDR_NTPRINTING NDR_FSRVP NDR_WITNESS NDR_MDSSVC NDR_OPEN_FILES NDR_SMBXSRV''',
     private_library=True,
     grouping_library=True
     )
-- 
2.1.0


From b27d3bb0473ae2755b76e3e52ab3fee2d3b241ac Mon Sep 17 00:00:00 2001
From: Ralph Boehme <rb at sernet.de>
Date: Wed, 23 Jul 2014 09:58:45 +0200
Subject: [PATCH 03/12] s3-mdssvc: add Spotlight RPC stubs

'mdssvc' aka 'Metadata Search Service' is an RPC service used by Apple
for passing marshalled Spotlight search queries and results between
client to server.

Signed-off-by: Ralph Boehme <slow at samba.org>
Reviewed-by: Stefan Metzmacher <metze at samba.org>
---
 source3/rpc_server/mdssvc/mdssvc.c        | 31 +++++++++++++++++
 source3/rpc_server/mdssvc/mdssvc.h        | 21 ++++++++++++
 source3/rpc_server/mdssvc/srv_mdssvc_nt.c | 57 +++++++++++++++++++++++++++++++
 source3/rpc_server/rpc_config.c           |  1 +
 source3/rpc_server/rpc_service_setup.c    | 29 ++++++++++++++++
 source3/rpc_server/wscript_build          |  8 +++++
 6 files changed, 147 insertions(+)
 create mode 100644 source3/rpc_server/mdssvc/mdssvc.c
 create mode 100644 source3/rpc_server/mdssvc/mdssvc.h
 create mode 100644 source3/rpc_server/mdssvc/srv_mdssvc_nt.c

diff --git a/source3/rpc_server/mdssvc/mdssvc.c b/source3/rpc_server/mdssvc/mdssvc.c
new file mode 100644
index 0000000..20e45f1
--- /dev/null
+++ b/source3/rpc_server/mdssvc/mdssvc.c
@@ -0,0 +1,31 @@
+/*
+   Unix SMB/CIFS implementation.
+   Main metadata server / Spotlight routines
+
+   Copyright (C) Ralph Boehme 2012-2014
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   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, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "includes.h"
+#include "mdssvc.h"
+
+#undef DBGC_CLASS
+#define DBGC_CLASS DBGC_RPC_SRV
+
+int mds_dispatch(void)
+{
+	DEBUG(10, ("mds_dispatch\n"));
+	return 0;
+}
diff --git a/source3/rpc_server/mdssvc/mdssvc.h b/source3/rpc_server/mdssvc/mdssvc.h
new file mode 100644
index 0000000..f016e96
--- /dev/null
+++ b/source3/rpc_server/mdssvc/mdssvc.h
@@ -0,0 +1,21 @@
+/*
+   Unix SMB/CIFS implementation.
+   Main metadata server / Spotlight routines
+
+   Copyright (C) Ralph Boehme			2012-2014
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   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, see <http://www.gnu.org/licenses/>.
+*/
+
+int mds_dispatch(void);
diff --git a/source3/rpc_server/mdssvc/srv_mdssvc_nt.c b/source3/rpc_server/mdssvc/srv_mdssvc_nt.c
new file mode 100644
index 0000000..3779e45
--- /dev/null
+++ b/source3/rpc_server/mdssvc/srv_mdssvc_nt.c
@@ -0,0 +1,57 @@
+/*
+ *  Unix SMB/CIFS implementation.
+ *  RPC Pipe client / server routines for mdssvc
+ *  Copyright (C) Ralph Boehme 2014
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "includes.h"
+#include "mdssvc.h"
+#include "ntdomain.h"
+#include "../librpc/gen_ndr/srv_mdssvc.h"
+
+#undef DBGC_CLASS
+#define DBGC_CLASS DBGC_RPC_SRV
+
+void _mdssvc_open(struct pipes_struct *p, struct mdssvc_open *r)
+{
+	DEBUG(10, ("%s\n", __func__));
+	p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
+	return;
+}
+
+void _mdssvc_unknown1(struct pipes_struct *p, struct mdssvc_unknown1 *r)
+{
+	DEBUG(10, ("%s\n", __func__));
+	p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
+	return;
+}
+
+void _mdssvc_cmd(struct pipes_struct *p, struct mdssvc_cmd *r)
+{
+	DEBUG(10, ("%s\n", __func__));
+	p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
+
+	mds_dispatch();
+
+	return;
+}
+
+void _mdssvc_close(struct pipes_struct *p, struct mdssvc_close *r)
+{
+	DEBUG(10, ("%s\n", __func__));
+	p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
+	return;
+}
diff --git a/source3/rpc_server/rpc_config.c b/source3/rpc_server/rpc_config.c
index efc6668..23c6f88 100644
--- a/source3/rpc_server/rpc_config.c
+++ b/source3/rpc_server/rpc_config.c
@@ -31,6 +31,7 @@ struct rpc_service_defaults {
 	const char *def_mode;
 } rpc_service_defaults[] = {
 	{ "epmapper", "disabled" },
+	{ "mdssvc", "disabled" },
 	/* { "spoolss", "embedded" }, */
 	/* { "lsarpc", "embedded" }, */
 	/* { "samr", "embedded" }, */
diff --git a/source3/rpc_server/rpc_service_setup.c b/source3/rpc_server/rpc_service_setup.c
index b8bb8ae..7395caa 100644
--- a/source3/rpc_server/rpc_service_setup.c
+++ b/source3/rpc_server/rpc_service_setup.c
@@ -38,6 +38,7 @@
 #include "../librpc/gen_ndr/srv_spoolss.h"
 #include "../librpc/gen_ndr/srv_svcctl.h"
 #include "../librpc/gen_ndr/srv_wkssvc.h"
+#include "../librpc/gen_ndr/srv_mdssvc.h"
 
 #include "printing/nt_printing_migrate_internal.h"
 #include "rpc_server/eventlog/srv_eventlog_reg.h"
@@ -443,6 +444,27 @@ static bool rpc_setup_initshutdown(struct tevent_context *ev_ctx,
 	return rpc_setup_embedded(ev_ctx, msg_ctx, t, NULL);
 }
 
+#ifdef WITH_SPOTLIGHT
+static bool rpc_setup_mdssvc(struct tevent_context *ev_ctx,
+			     struct messaging_context *msg_ctx)
+{
+	const struct ndr_interface_table *t = &ndr_table_mdssvc;
+	const char *pipe_name = "mdssvc";
+	NTSTATUS status;
+	enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
+	if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
+		return true;
+	}
+
+	status = rpc_mdssvc_init(NULL);
+	if (!NT_STATUS_IS_OK(status)) {
+		return false;
+	}
+
+	return rpc_setup_embedded(ev_ctx, msg_ctx, t, pipe_name);
+}
+#endif
+
 bool dcesrv_ep_setup(struct tevent_context *ev_ctx,
 		     struct messaging_context *msg_ctx)
 {
@@ -526,6 +548,13 @@ bool dcesrv_ep_setup(struct tevent_context *ev_ctx,
 		goto done;
 	}
 
+#ifdef WITH_SPOTLIGHT
+	ok = rpc_setup_mdssvc(ev_ctx, msg_ctx);
+	if (!ok) {
+		goto done;
+	}
+#endif
+
 done:
 	talloc_free(tmp_ctx);
 	return ok;
diff --git a/source3/rpc_server/wscript_build b/source3/rpc_server/wscript_build
index 25c923d..25d85cb 100755
--- a/source3/rpc_server/wscript_build
+++ b/source3/rpc_server/wscript_build
@@ -128,6 +128,13 @@ bld.SAMBA3_SUBSYSTEM('RPC_WKSSVC',
                     ../../librpc/gen_ndr/srv_wkssvc.c''',
                     deps='LIBNET')
 
+bld.SAMBA3_SUBSYSTEM('RPC_MDSSVC',
+                    source='''mdssvc/mdssvc.c
+                    mdssvc/srv_mdssvc_nt.c
+                    ../../librpc/gen_ndr/srv_mdssvc.c''',
+                    deps='samba-util ' + bld.env['libtracker'],
+                    enabled=bld.env.with_spotlight)
+
 # RPC_SERVICE
 bld.SAMBA3_SUBSYSTEM('RPC_SERVER_REGISTER',
                     source='rpc_ep_register.c ../librpc/rpc/dcerpc_ep.c',
@@ -155,6 +162,7 @@ bld.SAMBA3_SUBSYSTEM('RPC_SERVICE',
                     RPC_SERVER
                     RPC_EPMAPPER
 		    RPC_FSS_AGENT
+                    RPC_MDSSVC
                     ''')
 
 # RPC_DAEMONS
-- 
2.1.0


From 1126acee0166f68a8740925aabb327e1f0b3e0df Mon Sep 17 00:00:00 2001
From: Ralph Boehme <rb at sernet.de>
Date: Thu, 31 Jul 2014 13:49:49 +0200
Subject: [PATCH 04/12] s3-mdssvc: add new option 'spotlight'

Per share option: it reflects whether a share is indexed by Tracker or
not. The global switch that controls whether Spotlight is enabled or
not, are the mdsvc RPC switches.

Signed-off-by: Ralph Boehme <slow at samba.org>
Reviewed-by: Stefan Metzmacher <metze at samba.org>
---
 docs-xml/smbdotconf/misc/spotlight.xml | 45 ++++++++++++++++++++++++++++++++++
 lib/param/param_table.c                |  8 ++++++
 source3/param/loadparm.c               |  1 +
 3 files changed, 54 insertions(+)
 create mode 100644 docs-xml/smbdotconf/misc/spotlight.xml

diff --git a/docs-xml/smbdotconf/misc/spotlight.xml b/docs-xml/smbdotconf/misc/spotlight.xml
new file mode 100644
index 0000000..d872bb6
--- /dev/null
+++ b/docs-xml/smbdotconf/misc/spotlight.xml
@@ -0,0 +1,45 @@
+<samba:parameter name="spotlight"
+		 context="S"
+		 type="boolean"
+		 xmlns:samba="http://www.samba.org/samba/DTD/samba-doc">
+<description>
+	<para>
+	  This parameter controls whether Samba allows Spotlight
+	  queries on a share. For controlling indexing of filesystems
+	  you also have to use Tracker's own configuration system.
+	</para>
+
+	<para>
+	  Spotlight has several prerequisites:
+	</para>
+
+	<itemizedlist>
+	  <listitem><para>
+	    Samba must be configured and built with Spotlight support.
+	  </para></listitem>
+
+	  <listitem><para>
+	    The <emphasis>mdssvc</emphasis> RPC service must be
+	    enabled, see below.
+	  </para></listitem>
+
+	  <listitem><para> Tracker intergration must be setup and the
+	  share must be indexed by Tracker.</para></listitem>
+	</itemizedlist>
+
+	<para>For a detailed set of instructions please see <ulink
+	url="https://wiki.samba.org/index.php/Spotlight">https://wiki.samba.org/index.php/Spotlight</ulink>.
+	</para>
+
+	<para>
+      To enable the Spotlight RPC service:
+	</para>
+
+<programlisting>
+<smbconfsection name="[Global]"/>
+<smbconfoption name="rpc_server:mdsvc">embedded</smbconfoption>
+</programlisting>
+
+</description>
+<value type="default">no</value>
+</samba:parameter>
diff --git a/lib/param/param_table.c b/lib/param/param_table.c
index 287839f..57110e2 100644
--- a/lib/param/param_table.c
+++ b/lib/param/param_table.c
@@ -807,6 +807,14 @@ struct parm_struct parm_table[] = {
 		.enum_list	= NULL,
 	},
 	{
+		.label		= "spotlight",
+		.type		= P_BOOL,
+		.p_class	= P_LOCAL,
+		.offset		= LOCAL_VAR(spotlight),
+		.special	= NULL,
+		.enum_list	= NULL,
+	},
+	{
 		.label		= "write ok",
 		.type		= P_BOOLREV,
 		.p_class	= P_LOCAL,
diff --git a/source3/param/loadparm.c b/source3/param/loadparm.c
index cedaf1a..af74d68 100644
--- a/source3/param/loadparm.c
+++ b/source3/param/loadparm.c
@@ -185,6 +185,7 @@ static struct loadparm_service sDefault =
 	.access_based_share_enum = false,
 	.bAvailable = true,
 	.read_only = true,
+	.spotlight = false,
 	.guest_only = false,
 	.administrative_share = false,
 	.guest_ok = false,
-- 
2.1.0


From ded23a4660f118192c2b6ab35cd1d59b172ab24c Mon Sep 17 00:00:00 2001
From: Ralph Boehme <rb at sernet.de>
Date: Thu, 31 Jul 2014 16:27:36 +0200
Subject: [PATCH 05/12] s3-mdssvc: dalloc: dynamic object store based on talloc

dalloc is a hack with a bizarre API, but it does its job: it's a
simple object store that allows for storing simple and complex data
types.

We'll use it for storing Spotlight query data.

Signed-off-by: Ralph Boehme <slow at samba.org>
Reviewed-by: Stefan Metzmacher <metze at samba.org>
---
 source3/rpc_server/mdssvc/dalloc.c | 230 +++++++++++++++++++++++++++++++++++++
 source3/rpc_server/mdssvc/dalloc.h | 163 ++++++++++++++++++++++++++
 source3/rpc_server/wscript_build   |   1 +
 3 files changed, 394 insertions(+)
 create mode 100644 source3/rpc_server/mdssvc/dalloc.c
 create mode 100644 source3/rpc_server/mdssvc/dalloc.h

diff --git a/source3/rpc_server/mdssvc/dalloc.c b/source3/rpc_server/mdssvc/dalloc.c
new file mode 100644
index 0000000..7a4a86c
--- /dev/null
+++ b/source3/rpc_server/mdssvc/dalloc.c
@@ -0,0 +1,230 @@
+/*
+  Copyright (c) Ralph Boehme			2012-2014
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 3 of the License, or
+  (at your option) any later version.
+
+  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, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "replace.h"
+#include <talloc.h>
+#include "dalloc.h"
+
+/**
+ * Dynamic Datastore
+ **/
+struct dalloc_ctx {
+	void **dd_talloc_array;
+};
+
+void *_dalloc_new(TALLOC_CTX *mem_ctx, const char *type)
+{
+	void *p;
+
+	p = talloc_zero(mem_ctx, DALLOC_CTX);
+	if (p == NULL) {
+		return NULL;
+	}
+	talloc_set_name_const(p, type);
+
+	return p;
+}
+
+int _dalloc_add_talloc_chunk(DALLOC_CTX *dd, void *obj, const char *type, size_t size)
+{
+	size_t array_len = talloc_array_length(dd->dd_talloc_array);
+
+	dd->dd_talloc_array = talloc_realloc(dd,
+					     dd->dd_talloc_array,
+					     void *,
+					     array_len + 1);
+	if (dd->dd_talloc_array == NULL) {
+		return -1;
+	}
+
+	if (size != 0) {
+		void *p;
+
+		p = talloc_named_const(dd->dd_talloc_array, size, type);
+		if (p == NULL) {
+			return -1;
+		}
+		memcpy(p, obj, size);
+		obj = p;
+	} else {
+		_talloc_get_type_abort(obj, type, __location__);
+	}
+
+	dd->dd_talloc_array[array_len] = obj;
+
+	return 0;
+}
+
+/* Get number of elements, returns 0 if the structure is empty or not initialized */
+size_t dalloc_size(const DALLOC_CTX *d)
+{
+	if (d == NULL) {
+		return 0;
+	}
+
+	if (d->dd_talloc_array == NULL) {
+		return 0;
+	}
+
+	return talloc_array_length(d->dd_talloc_array);
+}
+
+/* Return element at position */
+void *dalloc_get_object(const DALLOC_CTX *d, int i)
+{
+	size_t size = dalloc_size(d);
+
+	if (i >= size) {
+		return NULL;
+	}
+
+	return d->dd_talloc_array[i];
+}
+
+/* Return typename of element at position */
+const char *dalloc_get_name(const DALLOC_CTX *d, int i)
+{
+	void *o = dalloc_get_object(d, i);
+
+	if (o == NULL) {
+		return NULL;
+	}
+
+	return talloc_get_name(o);
+}
+
+/*
+ * Get pointer to value from a DALLOC object
+ *
+ * Returns pointer to object from a DALLOC object. Nested object interation
+ * is supported by using the type string "DALLOC_CTX". Any other type string
+ * designates the requested objects type.
+ */
+void *dalloc_get(const DALLOC_CTX *d, ...)
+{
+	int result = 0;
+	void *p = NULL;
+	va_list args;
+	const char *type;
+	int elem;
+
+	va_start(args, d);
+	type = va_arg(args, const char *);
+
+	while (strcmp(type, "DALLOC_CTX") == 0) {
+		elem = va_arg(args, int);
+		if (elem >= talloc_array_length(d->dd_talloc_array)) {
+			result = -1;
+			goto done;
+		}
+		d = d->dd_talloc_array[elem];
+		type = va_arg(args, const char *);
+	}
+
+	elem = va_arg(args, int);
+	if (elem >= talloc_array_length(d->dd_talloc_array)) {
+		result = -1;
+		goto done;
+	}
+
+	p = talloc_check_name(d->dd_talloc_array[elem], type);
+	if (p == NULL) {
+		result = -1;
+		goto done;
+	}
+
+done:
+	va_end(args);
+	if (result != 0) {
+		p = NULL;
+	}
+	return p;
+}
+
+void *dalloc_value_for_key(const DALLOC_CTX *d, ...)
+{
+	int result = 0;
+	void *p = NULL;
+	va_list args;
+	const char *type;
+	int elem;
+	size_t array_len;
+
+	va_start(args, d);
+	type = va_arg(args, const char *);
+
+	while (strcmp(type, "DALLOC_CTX") == 0) {
+		array_len = talloc_array_length(d->dd_talloc_array);
+		elem = va_arg(args, int);
+		if (elem >= array_len) {
+			result = -1;
+			goto done;
+		}
+		d = d->dd_talloc_array[elem];
+		type = va_arg(args, const char *);
+	}
+
+	array_len = talloc_array_length(d->dd_talloc_array);
+
+	for (elem = 0; elem + 1 < array_len; elem += 2) {
+		if (strcmp(talloc_get_name(d->dd_talloc_array[elem]), "char *") != 0) {
+			result = -1;
+			goto done;
+		}
+		if (strcmp((char *)d->dd_talloc_array[elem],type) == 0) {
+			p = d->dd_talloc_array[elem + 1];
+			break;
+		}
+	}
+	va_end(args);
+
+done:
+	if (result != 0) {
+		p = NULL;
+	}
+	return p;
+}
+
+static char *dalloc_strdup(TALLOC_CTX *mem_ctx, const char *string)
+{
+	char *p;
+
+	p = talloc_strdup(mem_ctx, string);
+	if (p == NULL) {
+		return NULL;
+	}
+	talloc_set_name_const(p, "char *");
+	return p;
+}
+
+int dalloc_stradd(DALLOC_CTX *d, const char *string)
+{
+	int result;
+	char *p;
+
+	p = dalloc_strdup(d, string);
+	if (p == NULL) {
+		return -1;
+	}
+
+	result = dalloc_add(d, p, char *);
+	if (result != 0) {
+		return -1;
+	}
+
+	return 0;
+}
diff --git a/source3/rpc_server/mdssvc/dalloc.h b/source3/rpc_server/mdssvc/dalloc.h
new file mode 100644
index 0000000..7a0aecc
--- /dev/null
+++ b/source3/rpc_server/mdssvc/dalloc.h
@@ -0,0 +1,163 @@
+/*
+   Copyright (c) Ralph Boehme			2012-2014
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   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, see <http://www.gnu.org/licenses/>.
+*/
+
+/*
+  Typesafe, dynamic object store based on talloc
+
+  Usage
+  =====
+
+  Define some types:
+
+  A key/value store aka dictionary that supports retrieving elements
+  by key:
+
+    typedef dict_t DALLOC_CTX;
+
+  An ordered set that can store different objects which can be
+  retrieved by number:
+
+    typedef set_t DALLOC_CTX;
+
+  Create an dalloc object and add elementes of different type:
+
+    TALLOC_CTX *mem_ctx = talloc_new(NULL);
+    DALLOC_CTX *d = dalloc_new(mem_ctx);
+
+  Store an int value in the object:
+
+    uint64_t i = 1;
+    dalloc_add_copy(d, &i, uint64_t);
+
+  Store a string:
+
+    dalloc_stradd(d, "hello world");
+
+  Add a nested object:
+
+    DALLOC_CTX *nested = dalloc_new(d);
+    dalloc_add(d, nested, DALLOC_CTX);
+
+  Add an int value to the nested object, this can be fetched:
+
+    i = 2;
+    dalloc_add_copy(nested, &i, uint64_t);
+
+  Add a nested set:
+
+    set_t *set = dalloc_zero(nested, set_t);
+    dalloc_add(nested, set, set_t);
+
+  Add an int value to the set:
+
+    i = 3;
+    dalloc_add_copy(set, &i, uint64_t);
+
+  Add a dictionary (key/value store):
+
+    dict_t *dict = dalloc_zero(nested, dict_t);
+    dalloc_add(nested, dict, dict_t);
+
+  Store a string as key in the dict:
+
+    dalloc_stradd(dict, "key");
+
+  Add a value for the key:
+
+    i = 4;
+    dalloc_add_copy(dict, &i, uint64_t);
+
+  Fetching value references
+  =========================
+
+  You can fetch anything that is not a DALLOC_CTXs, because passing
+  "DALLOC_CTXs" as type to the functions dalloc_get() and
+  dalloc_value_for_key() tells the function to step into that object
+  and expect more arguments that specify which element to fetch.
+
+  Get reference to an objects element by position:
+
+    uint64_t *p = dalloc_get(d, "uint64_t", 0);
+
+  p now points to the first int with a value of 1.
+
+  Get reference to the "hello world" string:
+
+    str = dalloc_get(d, "char *", 1);
+
+  You can't fetch a DALLOC_CTX itself:
+
+    nested = dalloc_get(d, "DALLOC_CTX", 2);
+
+  But you can fetch elements from the neseted DALLOC_CTX:
+
+    p = dalloc_get(d, "DALLOC_CTX", 2, "uint64_t", 0);
+
+  p now points to the value 2.
+
+  You can fetch types that are typedefd DALLOC_CTXs:
+
+    set = dalloc_get(d, "DALLOC_CTX", 2, "set_t", 1);
+
+  Fetch int from set, must use DALLOC_CTX as type for the set:
+
+    p = dalloc_get(d, "DALLOC_CTX", 2, "DALLOC_CTX", 1, "uint64_t", 0);
+
+  p points to 3.
+
+  Fetch value by key from dictionary:
+
+    p = dalloc_value_for_key(d, "DALLOC_CTX", 2, "DALLOC_CTX", 2, "key");
+
+  p now points to 4.
+*/
+
+#ifndef DALLOC_H
+#define DALLOC_H
+
+#include <talloc.h>
+
+struct dalloc_ctx;
+typedef struct dalloc_ctx DALLOC_CTX;
+
+#define dalloc_new(mem_ctx) (DALLOC_CTX *)_dalloc_new((mem_ctx), "DALLOC_CTX")
+#define dalloc_zero(mem_ctx, type) (type *)_dalloc_new((mem_ctx), #type)
+
+/**
+ * talloc a chunk for obj of required size, copy the obj into the
+ * chunk and add the chunk to the dalloc ctx
+ **/
+#define dalloc_add_copy(d, obj, type) _dalloc_add_talloc_chunk((d), (obj), #type, sizeof(type))
+
+/**
+ * Add a pointer to a talloced object to the dalloc ctx. The object
+ * must be a talloc child of the dalloc ctx.
+ **/
+#define dalloc_add(d, obj, type) _dalloc_add_talloc_chunk((d), (obj), #type, 0)
+
+
+extern void *dalloc_get(const DALLOC_CTX *d, ...);
+extern void *dalloc_value_for_key(const DALLOC_CTX *d, ...);
+extern size_t dalloc_size(const DALLOC_CTX *d);
+extern void *dalloc_get_object(const DALLOC_CTX *d, int i);
+extern const char *dalloc_get_name(const DALLOC_CTX *d, int i);
+extern int dalloc_stradd(DALLOC_CTX *d, const char *string);
+
+extern void *_dalloc_new(TALLOC_CTX *mem_ctx, const char *type);
+extern int _dalloc_add_talloc_chunk(DALLOC_CTX *d, void *obj, const char *type, size_t size);
+
+#endif  /* DALLOC_H */
diff --git a/source3/rpc_server/wscript_build b/source3/rpc_server/wscript_build
index 25d85cb..03dc63e 100755
--- a/source3/rpc_server/wscript_build
+++ b/source3/rpc_server/wscript_build
@@ -130,6 +130,7 @@ bld.SAMBA3_SUBSYSTEM('RPC_WKSSVC',
 
 bld.SAMBA3_SUBSYSTEM('RPC_MDSSVC',
                     source='''mdssvc/mdssvc.c
+                    mdssvc/dalloc.c
                     mdssvc/srv_mdssvc_nt.c
                     ../../librpc/gen_ndr/srv_mdssvc.c''',
                     deps='samba-util ' + bld.env['libtracker'],
-- 
2.1.0


From 51b427fb8016dc66f52c43bffd8cef4a28bb7c05 Mon Sep 17 00:00:00 2001
From: Ralph Boehme <rb at sernet.de>
Date: Thu, 31 Jul 2014 17:07:28 +0200
Subject: [PATCH 06/12] s3-mdssvc: (un)marshalling Spotlight RPC blob

Add code for marshalling and unmarshalling Spotlight RPC blobs
from/into a dalloc object store.

Signed-off-by: Ralph Boehme <slow at samba.org>
Reviewed-by: Stefan Metzmacher <metze at samba.org>
---
 source3/rpc_server/mdssvc/marshalling.c | 1362 +++++++++++++++++++++++++++++++
 source3/rpc_server/mdssvc/marshalling.h |   55 ++
 source3/rpc_server/mdssvc/mdssvc.h      |   15 +
 source3/rpc_server/wscript_build        |    1 +
 4 files changed, 1433 insertions(+)
 create mode 100644 source3/rpc_server/mdssvc/marshalling.c
 create mode 100644 source3/rpc_server/mdssvc/marshalling.h

diff --git a/source3/rpc_server/mdssvc/marshalling.c b/source3/rpc_server/mdssvc/marshalling.c
new file mode 100644
index 0000000..a16966f
--- /dev/null
+++ b/source3/rpc_server/mdssvc/marshalling.c
@@ -0,0 +1,1362 @@
+/*
+   Unix SMB/CIFS implementation.
+   Main metadata server / Spotlight routines
+
+   Copyright (C) Ralph Boehme			2012-2014
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   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, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "includes.h"
+#include "dalloc.h"
+#include "marshalling.h"
+
+#undef DBGC_CLASS
+#define DBGC_CLASS DBGC_RPC_SRV
+
+/*
+ * This is used to talloc an array that will hold the table of
+ * contents of a marshalled Spotlight RPC (S-RPC) reply. Each ToC
+ * entry is 8 bytes, so we allocate space for 1024 entries which
+ * should be sufficient for even the largest S-RPC replies.
+ *
+ * The total buffersize for S-RPC packets is typically limited to 64k,
+ * so we can only store so many elements there anyway.
+ */
+#define MAX_SLQ_TOC 1024*8
+#define MAX_SLQ_TOCIDX 1024
+#define MAX_SLQ_COUNT 4096
+#define MAX_SL_STRLEN 1024
+
+/******************************************************************************
+ * RPC data marshalling and unmarshalling
+ ******************************************************************************/
+
+/* Spotlight epoch is UNIX epoch minus SPOTLIGHT_TIME_DELTA */
+#define SPOTLIGHT_TIME_DELTA 280878921600ULL
+
+#define SQ_TYPE_NULL    0x0000
+#define SQ_TYPE_COMPLEX 0x0200
+#define SQ_TYPE_INT64   0x8400
+#define SQ_TYPE_BOOL    0x0100
+#define SQ_TYPE_FLOAT   0x8500
+#define SQ_TYPE_DATA    0x0700
+#define SQ_TYPE_CNIDS   0x8700
+#define SQ_TYPE_UUID    0x0e00
+#define SQ_TYPE_DATE    0x8600
+#define SQ_TYPE_TOC     0x8800
+
+#define SQ_CPX_TYPE_ARRAY           0x0a00
+#define SQ_CPX_TYPE_STRING          0x0c00
+#define SQ_CPX_TYPE_UTF16_STRING    0x1c00
+#define SQ_CPX_TYPE_DICT            0x0d00
+#define SQ_CPX_TYPE_CNIDS           0x1a00
+#define SQ_CPX_TYPE_FILEMETA        0x1b00
+
+struct sl_tag  {
+	int type;
+	int count;
+	size_t length;
+	size_t size;
+};
+
+static ssize_t sl_pack_loop(DALLOC_CTX *query, char *buf,
+			    ssize_t offset, size_t bufsize,
+			    char *toc_buf, int *toc_idx, int *count);
+static ssize_t sl_unpack_loop(DALLOC_CTX *query, const char *buf,
+			      ssize_t offset, size_t bufsize,
+			      int count, ssize_t toc_offset,
+			      int encoding);
+
+/******************************************************************************
+ * Wrapper functions for the *VAL macros with bound checking
+ ******************************************************************************/
+
+static ssize_t sl_push_uint64_val(char *buf,
+				  ssize_t offset,
+				  size_t max_offset,
+				  uint64_t val)
+{
+	if (offset + 8 > max_offset) {
+		DEBUG(1, ("%s: offset: %zd, max_offset: %zu",
+			  __func__, offset, max_offset));
+		return -1;
+	}
+
+	SBVAL(buf, offset, val);
+	return offset + 8;
+}
+
+static ssize_t sl_pull_uint64_val(const char *buf,
+				  ssize_t offset,
+				  size_t bufsize,
+				  uint encoding,
+				  uint64_t *presult)
+{
+	uint64_t val;
+
+	if (offset + 8 > bufsize) {
+		DEBUG(1,("%s: buffer overflow\n", __func__));
+		return -1;
+	}
+
+	if (encoding == SL_ENC_LITTLE_ENDIAN) {
+		val = BVAL(buf, offset);
+	} else {
+		val = RBVAL(buf, offset);
+	}
+
+	*presult = val;
+
+	return offset + 8;
+}
+
+/*
+ * Returns the UTF-16 string encoding, by checking the 2-byte byte order mark.
+ * If there is no byte order mark, -1 is returned.
+ */
+static int spotlight_get_utf16_string_encoding(const char *buf, ssize_t offset,
+					       size_t query_length, int encoding)
+{
+	int utf16_encoding;
+
+	/* Assumed encoding in absence of a bom is little endian */
+	utf16_encoding = SL_ENC_LITTLE_ENDIAN;
+
+	if (query_length >= 2) {
+		uint8_t le_bom[] = {0xff, 0xfe};
+		uint8_t be_bom[] = {0xfe, 0xff};
+		if (memcmp(le_bom, buf + offset, sizeof(uint16_t)) == 0) {
+			utf16_encoding = SL_ENC_LITTLE_ENDIAN | SL_ENC_UTF_16;
+		} else if (memcmp(be_bom, buf + offset, sizeof(uint16_t)) == 0) {
+			utf16_encoding = SL_ENC_BIG_ENDIAN | SL_ENC_UTF_16;
+		}
+	}
+
+	return utf16_encoding;
+}
+
+/******************************************************************************
+ * marshalling functions
+ ******************************************************************************/
+
+static inline uint64_t sl_pack_tag(uint16_t type, uint16_t size_or_count, uint32_t val)
+{
+	uint64_t tag = ((uint64_t)val << 32) | ((uint64_t)type << 16) | size_or_count;
+	return tag;
+}
+
+static ssize_t sl_pack_float(double d, char *buf, ssize_t offset, size_t bufsize)
+{
+	union {
+		double d;
+		uint64_t w;
+	} ieee_fp_union;
+
+	offset = sl_push_uint64_val(buf, offset, bufsize, sl_pack_tag(SQ_TYPE_FLOAT, 2, 1));
+	if (offset == -1) {
+		return -1;
+	}
+	offset = sl_push_uint64_val(buf, offset, bufsize, ieee_fp_union.w);
+	if (offset == -1) {
+		return -1;
+	}
+
+	return offset;
+}
+
+static ssize_t sl_pack_uint64(uint64_t u, char *buf, ssize_t offset, size_t bufsize)
+{
+	uint64_t tag;
+
+	tag = sl_pack_tag(SQ_TYPE_INT64, 2, 1);
+	offset = sl_push_uint64_val(buf, offset, bufsize, tag);
+	if (offset == -1) {
+		return -1;
+	}
+	offset = sl_push_uint64_val(buf, offset, bufsize, u);
+	if (offset == -1) {
+		return -1;
+	}
+
+	return offset;
+}
+
+static ssize_t sl_pack_uint64_array(uint64_t *u, char *buf, ssize_t offset, size_t bufsize, int *toc_count)
+{
+	int count, i;
+	uint64_t tag;
+
+	count = talloc_array_length(u);
+
+	tag = sl_pack_tag(SQ_TYPE_INT64, count + 1, count);
+	offset = sl_push_uint64_val(buf, offset, bufsize, tag);
+	if (offset == -1) {
+		return -1;
+	}
+
+	for (i = 0; i < count; i++) {
+		offset = sl_push_uint64_val(buf, offset, bufsize, u[i]);
+		if (offset == -1) {
+			return -1;
+		}
+	}
+
+	if (count > 1) {
+		*toc_count += (count - 1);
+	}
+
+	return offset;
+}
+
+static ssize_t sl_pack_bool(sl_bool_t val, char *buf, ssize_t offset, size_t bufsize)
+{
+	uint64_t tag;
+
+	tag = sl_pack_tag(SQ_TYPE_BOOL, 1, val ? 1 : 0);
+	offset = sl_push_uint64_val(buf, offset, bufsize, tag);
+	if (offset == -1) {
+		return -1;
+	}
+
+	return offset;
+}
+
+static ssize_t sl_pack_nil(char *buf, ssize_t offset, size_t bufsize)
+{
+	uint64_t tag;
+
+	tag = sl_pack_tag(SQ_TYPE_NULL, 1, 1);
+	offset = sl_push_uint64_val(buf, offset, bufsize, tag);
+	if (offset == -1) {
+		return -1;
+	}
+
+	return offset;
+}
+
+static ssize_t sl_pack_date(sl_time_t t, char *buf, ssize_t offset, size_t bufsize)
+{
+	uint64_t data;
+	uint64_t tag;
+
+	tag = sl_pack_tag(SQ_TYPE_DATE, 2, 1);
+	offset = sl_push_uint64_val(buf, offset, bufsize, tag);
+	if (offset == -1) {
+		return -1;
+	}
+
+	data = (t.tv_sec + SPOTLIGHT_TIME_DELTA) << 24;
+	offset = sl_push_uint64_val(buf, offset, bufsize, data);
+	if (offset == -1) {
+		return -1;
+	}
+
+	return offset;
+}
+
+static ssize_t sl_pack_uuid(sl_uuid_t *uuid, char *buf, ssize_t offset, size_t bufsize)
+{
+	uint64_t tag;
+
+	tag = sl_pack_tag(SQ_TYPE_UUID, 3, 1);
+	offset = sl_push_uint64_val(buf, offset, bufsize, tag);
+	if (offset == -1) {
+		return -1;
+	}
+
+	if (offset + 16 > bufsize) {
+		return -1;
+	}
+	memcpy(buf + offset, uuid, 16);
+
+	return offset + 16;
+}
+
+static ssize_t sl_pack_CNID(sl_cnids_t *cnids, char *buf, ssize_t offset,
+			    size_t bufsize, char *toc_buf, int *toc_idx)
+{
+	ssize_t result;
+	int len, i;
+	int cnid_count = dalloc_size(cnids->ca_cnids);
+	uint64_t tag;
+	uint64_t id;
+	void *p;
+
+	tag = sl_pack_tag(SQ_CPX_TYPE_CNIDS, offset / 8, 0);
+	result = sl_push_uint64_val(toc_buf, *toc_idx * 8, MAX_SLQ_TOC, tag);
+	if (result == -1) {
+		return -1;
+	}
+
+	tag = sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1);
+	offset = sl_push_uint64_val(buf, offset, bufsize, tag);
+	if (offset == -1) {
+		return -1;
+	}
+
+	*toc_idx += 1;
+
+	len = cnid_count + 1;
+	if (cnid_count > 0) {
+		len ++;
+	}
+
+	/* unknown meaning, but always 8 */
+	tag = sl_pack_tag(SQ_TYPE_CNIDS, len, 8 );
+	offset = sl_push_uint64_val(buf, offset, bufsize, tag);
+	if (offset == -1) {
+		return -1;
+	}
+
+	if (cnid_count > 0) {
+		tag = sl_pack_tag(cnids->ca_unkn1, cnid_count, cnids->ca_context);
+		offset = sl_push_uint64_val(buf, offset, bufsize, tag);
+		if (offset == -1) {
+			return -1;
+		}
+
+		for (i = 0; i < cnid_count; i++) {
+			p = dalloc_get_object(cnids->ca_cnids, i);
+			if (p == NULL) {
+				return -1;
+			}
+			memcpy(&id, p, sizeof(uint64_t));
+			offset = sl_push_uint64_val(buf, offset, bufsize, id);
+			if (offset == -1) {
+				return -1;
+			}
+		}
+	}
+
+	return offset;
+}
+
+static ssize_t sl_pack_array(sl_array_t *array, char *buf, ssize_t offset,
+			     size_t bufsize, char *toc_buf, int *toc_idx)
+{
+	ssize_t result;
+	int count = dalloc_size(array);
+	int octets = offset / 8;
+	uint64_t tag;
+	int toc_idx_save = *toc_idx;
+
+	tag = sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1);
+	offset = sl_push_uint64_val(buf, offset, bufsize, tag);
+	if (offset == -1) {
+		return -1;
+	}
+
+	*toc_idx += 1;
+
+	offset = sl_pack_loop(array, buf, offset, bufsize - offset, toc_buf, toc_idx, &count);
+
+	tag = sl_pack_tag(SQ_CPX_TYPE_ARRAY, octets, count);
+	result = sl_push_uint64_val(toc_buf, toc_idx_save * 8, MAX_SLQ_TOC, tag);
+	if (result == -1) {
+		return -1;
+	}
+
+	return offset;
+}
+
+static ssize_t sl_pack_dict(sl_array_t *dict, char *buf, ssize_t offset,
+			    size_t bufsize, char *toc_buf, int *toc_idx, int *count)
+{
+	ssize_t result;
+	uint64_t tag;
+
+	tag = sl_pack_tag(SQ_CPX_TYPE_DICT, offset / 8,
+			  dalloc_size(dict));
+	result = sl_push_uint64_val(toc_buf, *toc_idx * 8, MAX_SLQ_TOC, tag);
+	if (result == -1) {
+		return -1;
+	}
+
+	tag = sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1);
+	offset = sl_push_uint64_val(buf, offset, bufsize, tag);
+	if (offset == -1) {
+		return -1;
+	}
+
+	*toc_idx += 1;
+
+	offset = sl_pack_loop(dict, buf, offset, bufsize - offset, toc_buf, toc_idx, count);
+
+	return offset;
+}
+
+static ssize_t sl_pack_filemeta(sl_filemeta_t *fm, char *buf, ssize_t offset,
+				size_t bufsize, char *toc_buf, int *toc_idx)
+{
+	ssize_t result;
+	ssize_t fmlen;
+	ssize_t saveoff = offset;
+	uint64_t tag;
+
+	tag = sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1);
+	offset = sl_push_uint64_val(buf, offset, bufsize, tag);
+	if (offset == -1) {
+		return -1;
+	}
+
+	offset += 8;
+
+	fmlen = sl_pack(fm, buf + offset, bufsize - offset);
+	if (fmlen == -1) {
+		return -1;
+	}
+
+	/*
+	 * Check for empty filemeta array, if it's only 40 bytes, it's
+	 * only the header but no content
+	 */
+	if (fmlen > 40) {
+		offset += fmlen;
+	} else {
+		fmlen = 0;
+	}
+
+	/* unknown meaning, but always 8 */
+	tag = sl_pack_tag(SQ_TYPE_DATA, (fmlen / 8) + 1, 8);
+	result = sl_push_uint64_val(buf, saveoff + 8, bufsize, tag);
+	if (result == -1) {
+		return -1;
+	}
+
+	tag = sl_pack_tag(SQ_CPX_TYPE_FILEMETA, saveoff / 8, fmlen / 8);
+	result = sl_push_uint64_val(toc_buf, *toc_idx * 8, MAX_SLQ_TOC, tag);
+	if (result == -1) {
+		return -1;
+	}
+
+	*toc_idx += 1;
+
+	return offset;
+}
+
+static ssize_t sl_pack_string(char *s, char *buf, ssize_t offset, size_t bufsize,
+			      char *toc_buf, int *toc_idx)
+{
+	ssize_t result;
+	size_t len, octets, used_in_last_octet;
+	uint64_t tag;
+
+	len = strlen(s);
+	if (len > MAX_SL_STRLEN) {
+		return -1;
+	}
+	octets = (len + 7) / 8;
+	used_in_last_octet = len % 8;
+	if (used_in_last_octet == 0) {
+		used_in_last_octet = 8;
+	}
+
+	tag = sl_pack_tag(SQ_CPX_TYPE_STRING, offset / 8, used_in_last_octet);
+	result = sl_push_uint64_val(toc_buf, *toc_idx * 8, MAX_SLQ_TOC, tag);
+	if (result == -1) {
+		return -1;
+	}
+
+	tag = sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1);
+	offset = sl_push_uint64_val(buf, offset, bufsize, tag);
+	if (offset == -1) {
+		return -1;
+	}
+
+	*toc_idx += 1;
+
+	tag = sl_pack_tag(SQ_TYPE_DATA, octets + 1, used_in_last_octet);
+	offset = sl_push_uint64_val(buf, offset, bufsize, tag);
+	if (offset == -1) {
+		return -1;
+	}
+
+	if (offset + (octets * 8) > bufsize) {
+		return -1;
+	}
+
+	memset(buf + offset, 0, octets * 8);
+	strncpy(buf + offset, s, len);
+	offset += octets * 8;
+
+	return offset;
+}
+
+static ssize_t sl_pack_string_as_utf16(char *s, char *buf, ssize_t offset,
+				       size_t bufsize, char *toc_buf, int *toc_idx)
+{
+	ssize_t result;
+	int utf16_plus_bom_len, octets, used_in_last_octet;
+	char *utf16string = NULL;
+	char bom[] = { 0xff, 0xfe };
+	size_t slen, utf16len;
+	uint64_t tag;
+	bool ok;
+
+	slen = strlen(s);
+	if (slen > MAX_SL_STRLEN) {
+		return -1;
+	}
+
+	ok = convert_string_talloc(talloc_tos(),
+				   CH_UTF8,
+				   CH_UTF16LE,
+				   s,
+				   slen,
+				   &utf16string,
+				   &utf16len);
+	if (!ok) {
+		return -1;
+	}
+
+	utf16_plus_bom_len = utf16len + 2;
+	octets = (utf16_plus_bom_len + 7) / 8;
+	used_in_last_octet = utf16_plus_bom_len % 8;
+	if (used_in_last_octet == 0) {
+		used_in_last_octet = 8;
+	}
+
+	tag = sl_pack_tag(SQ_CPX_TYPE_UTF16_STRING, offset / 8, used_in_last_octet);
+	result = sl_push_uint64_val(toc_buf, *toc_idx * 8, MAX_SLQ_TOC, tag);
+	if (result == -1) {
+		offset = -1;
+		goto done;
+	}
+
+	tag = sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1);
+	offset = sl_push_uint64_val(buf, offset, bufsize, tag);
+	if (offset == -1) {
+		goto done;
+	}
+
+	*toc_idx += 1;
+
+	tag = sl_pack_tag(SQ_TYPE_DATA, octets + 1, used_in_last_octet);
+	offset = sl_push_uint64_val(buf, offset, bufsize, tag);
+	if (offset == -1) {
+		goto done;
+	}
+
+	if (offset + (octets * 8) > bufsize) {
+		offset = -1;
+		goto done;
+	}
+
+	memset(buf + offset, 0, octets * 8);
+	memcpy(buf + offset, &bom, sizeof(bom));
+	memcpy(buf + offset + 2, utf16string, utf16len);
+	offset += octets * 8;
+
+done:
+	TALLOC_FREE(utf16string);
+	return offset;
+}
+
+static ssize_t sl_pack_loop(DALLOC_CTX *query, char *buf, ssize_t offset,
+			    size_t bufsize, char *toc_buf, int *toc_idx, int *count)
+{
+	const char *type;
+	int n;
+	uint64_t i;
+	sl_bool_t bl;
+	double d;
+	sl_time_t t;
+	void *p;
+
+	for (n = 0; n < dalloc_size(query); n++) {
+
+		type = dalloc_get_name(query, n);
+		if (type == NULL) {
+			return -1;
+		}
+		p = dalloc_get_object(query, n);
+		if (p == NULL) {
+			return -1;
+		}
+
+		if (strcmp(type, "sl_array_t") == 0) {
+			offset = sl_pack_array(p, buf, offset, bufsize,
+					       toc_buf, toc_idx);
+		} else if (strcmp(type, "sl_dict_t") == 0) {
+			offset = sl_pack_dict(p, buf, offset, bufsize,
+					      toc_buf, toc_idx, count);
+		} else if (strcmp(type, "sl_filemeta_t") == 0) {
+			offset = sl_pack_filemeta(p, buf, offset, bufsize,
+						  toc_buf, toc_idx);
+		} else if (strcmp(type, "uint64_t") == 0) {
+			memcpy(&i, p, sizeof(uint64_t));
+			offset = sl_pack_uint64(i, buf, offset, bufsize);
+		} else if (strcmp(type, "uint64_t *") == 0) {
+			offset = sl_pack_uint64_array(p, buf, offset,
+						      bufsize, count);
+		} else if (strcmp(type, "char *") == 0) {
+			offset = sl_pack_string(p, buf, offset, bufsize,
+						toc_buf, toc_idx);						
+		} else if (strcmp(type, "smb_ucs2_t *") == 0) {
+			offset = sl_pack_string_as_utf16(p, buf, offset, bufsize,
+							 toc_buf, toc_idx);
+		} else if (strcmp(type, "sl_bool_t") == 0) {
+			memcpy(&bl, p, sizeof(sl_bool_t));
+			offset = sl_pack_bool(bl, buf, offset, bufsize);
+		} else if (strcmp(type, "double") == 0) {
+			memcpy(&d, p, sizeof(double));
+			offset = sl_pack_float(d, buf, offset, bufsize);
+		} else if (strcmp(type, "sl_nil_t") == 0) {
+			offset = sl_pack_nil(buf, offset, bufsize);
+		} else if (strcmp(type, "sl_time_t") == 0) {
+			memcpy(&t, p, sizeof(sl_time_t));
+			offset = sl_pack_date(t, buf, offset, bufsize);
+		} else if (strcmp(type, "sl_uuid_t") == 0) {
+			offset = sl_pack_uuid(p, buf, offset, bufsize);
+		} else if (strcmp(type, "sl_cnids_t") == 0) {
+			offset = sl_pack_CNID(p, buf, offset,
+					      bufsize, toc_buf, toc_idx);
+		} else {
+			DEBUG(1, ("unknown type: %s", type));
+			return -1;
+		}
+		if (offset == -1) {
+			DEBUG(1, ("error packing type: %s\n", type));
+			return -1;
+		}
+	}
+
+	return offset;
+}
+
+/******************************************************************************
+ * unmarshalling functions
+ ******************************************************************************/
+
+static ssize_t sl_unpack_tag(const char *buf,
+			     ssize_t offset,
+			     size_t bufsize,
+			     uint encoding,
+			     struct sl_tag *tag)
+{
+	uint64_t val;
+
+	if (offset + 8 > bufsize) {
+		DEBUG(1,("%s: buffer overflow\n", __func__));
+		return -1;
+	}
+
+	if (encoding == SL_ENC_LITTLE_ENDIAN) {
+		val = BVAL(buf, offset);
+	} else {
+		val = RBVAL(buf, offset);
+	}
+
+	tag->size = (val & 0xffff) * 8;
+	tag->type = (val & 0xffff0000) >> 16;
+	tag->count = val >> 32;
+	tag->length = tag->count * 8;
+
+	if (tag->size > MAX_SL_FRAGMENT_SIZE) {
+		DEBUG(1,("%s: size limit %zu\n", __func__, tag->size));
+		return -1;
+	}
+
+	if (tag->length > MAX_SL_FRAGMENT_SIZE) {
+		DEBUG(1,("%s: length limit %zu\n", __func__, tag->length));
+		return -1;
+	}
+
+	if (tag->count > MAX_SLQ_COUNT) {
+		DEBUG(1,("%s: count limit %d\n", __func__, tag->count));
+		return -1;
+	}
+
+	return offset + 8;
+}
+
+static int sl_unpack_ints(DALLOC_CTX *query,
+			  const char *buf,
+			  ssize_t offset,
+			  size_t bufsize,
+			  int encoding)
+{
+	int i, result;
+	struct sl_tag tag;
+	uint64_t query_data64;
+
+	offset = sl_unpack_tag(buf, offset, bufsize, encoding, &tag);
+	if (offset == -1) {
+		return -1;
+	}
+
+	for (i = 0; i < tag.count; i++) {
+		offset = sl_pull_uint64_val(buf, offset, bufsize, encoding, &query_data64);
+		if (offset == -1) {
+			return -1;
+		}
+		result = dalloc_add_copy(query, &query_data64, uint64_t);
+		if (result != 0) {
+			return -1;
+		}
+	}
+
+	return tag.count;
+}
+
+static int sl_unpack_date(DALLOC_CTX *query,
+			  const char *buf,
+			  ssize_t offset,
+			  size_t bufsize,
+			  int encoding)
+{
+	int i, result;
+	struct sl_tag tag;
+	uint64_t query_data64;
+	sl_time_t t;
+
+	offset = sl_unpack_tag(buf, offset, bufsize, encoding, &tag);
+	if (offset == -1) {
+		return -1;
+	}
+
+	for (i = 0; i < tag.count; i++) {
+		offset = sl_pull_uint64_val(buf, offset, bufsize, encoding, &query_data64);
+		if (offset == -1) {
+			return -1;
+		}
+		query_data64 = query_data64 >> 24;
+		t.tv_sec = query_data64 - SPOTLIGHT_TIME_DELTA;
+		t.tv_usec = 0;
+		result = dalloc_add_copy(query, &t, sl_time_t);
+		if (result != 0) {
+			return -1;
+		}
+	}
+
+	return tag.count;
+}
+
+static int sl_unpack_uuid(DALLOC_CTX *query,
+			  const char *buf,
+			  ssize_t offset,
+			  size_t bufsize,
+			  int encoding)
+{
+	int i, result;
+	sl_uuid_t uuid;
+	struct sl_tag tag;
+
+	offset = sl_unpack_tag(buf, offset, bufsize, encoding, &tag);
+	if (offset == -1) {
+		return -1;
+	}
+
+	for (i = 0; i < tag.count; i++) {
+		if (offset + 16 > bufsize) {
+			DEBUG(1,("%s: buffer overflow\n", __func__));
+			return -1;
+		}
+		memcpy(uuid.sl_uuid, buf + offset, 16);
+		result = dalloc_add_copy(query, &uuid, sl_uuid_t);
+		if (result != 0) {
+			return -1;
+		}
+		offset += 16;
+	}
+
+	return tag.count;
+}
+
+static int sl_unpack_floats(DALLOC_CTX *query,
+			    const char *buf,
+			    ssize_t offset,
+			    size_t bufsize,
+			    int encoding)
+{
+	int i, result;
+	union {
+		double d;
+		uint32_t w[2];
+	} ieee_fp_union;
+	struct sl_tag tag;
+
+	offset = sl_unpack_tag(buf, offset, bufsize, encoding, &tag);
+	if (offset == -1) {
+		return -1;
+	}
+
+	for (i = 0; i < tag.count; i++) {
+		if (offset + 8 > bufsize) {
+			DEBUG(1,("%s: buffer overflow\n", __func__));
+			return -1;
+		}
+		if (encoding == SL_ENC_LITTLE_ENDIAN) {
+#ifdef WORDS_BIGENDIAN
+			ieee_fp_union.w[0] = IVAL(buf, offset + 4);
+			ieee_fp_union.w[1] = IVAL(buf, offset);
+#else
+			ieee_fp_union.w[0] = IVAL(buf, offset);
+			ieee_fp_union.w[1] = IVAL(buf, offset + 4);
+#endif
+		} else {
+#ifdef WORDS_BIGENDIAN
+			ieee_fp_union.w[0] = RIVAL(buf, offset);
+			ieee_fp_union.w[1] = RIVAL(buf, offset + 4);
+#else
+			ieee_fp_union.w[0] = RIVAL(buf, offset + 4);
+			ieee_fp_union.w[1] = RIVAL(buf, offset);
+#endif
+		}
+		result = dalloc_add_copy(query, &ieee_fp_union.d, double);
+		if (result != 0) {
+			return -1;
+		}
+		offset += 8;
+	}
+
+	return tag.count;
+}
+
+static int sl_unpack_CNID(DALLOC_CTX *query,
+			  const char *buf,
+			  ssize_t offset,
+			  size_t bufsize,
+			  int length,
+			  int encoding)
+{
+	int i, count, result;
+	uint64_t query_data64;
+	sl_cnids_t *cnids;
+
+	cnids = talloc_zero(query, sl_cnids_t);
+	if (cnids == NULL) {
+		return -1;
+	}
+	cnids->ca_cnids = dalloc_new(cnids);
+	if (cnids->ca_cnids == NULL) {
+		return -1;
+	}
+
+	if (length <= 16) {
+		/*
+		 * That's permitted, iirc length = 16 is an empty
+		 * array, so anything lesser then 16 should probably
+		 * be treated as an error, but I'm not quite sure.
+		 */
+		return 0;
+	}
+
+	offset = sl_pull_uint64_val(buf, offset, bufsize, encoding, &query_data64);
+	if (offset == -1) {
+		return -1;
+	}
+
+	/*
+	 * Note: ca_unkn1 and ca_context could be taken from the tag
+	 * type and count members, but the fields are packed
+	 * differently in this context, so we can't use
+	 * sl_unpack_tag().
+	 */
+	count = query_data64 & 0xffff;;
+	cnids->ca_unkn1 = (query_data64 & 0xffff0000) >> 16;
+	cnids->ca_context = query_data64 >> 32;
+
+	for (i = 0; i < count; i++) {
+		offset = sl_pull_uint64_val(buf, offset, bufsize, encoding, &query_data64);
+		if (offset == -1) {
+			return -1;
+		}
+
+		result = dalloc_add_copy(cnids->ca_cnids, &query_data64, uint64_t);
+		if (result != 0) {
+			return -1;
+		}
+	}
+
+	result = dalloc_add(query, cnids, sl_cnids_t);
+	if (result != 0) {
+		return -1;
+	}
+
+	return 0;
+}
+
+static ssize_t sl_unpack_cpx(DALLOC_CTX *query,
+			     const char *buf,
+			     ssize_t offset,
+			     size_t bufsize,
+			     int cpx_query_type,
+			     int cpx_query_count,
+			     ssize_t toc_offset,
+			     int encoding)
+{
+	int result;
+	ssize_t roffset = offset;
+	int unicode_encoding;
+	bool mark_exists;
+	char *p;
+	size_t slen, tmp_len;
+	sl_array_t *sl_array;
+	sl_dict_t *sl_dict;
+	sl_filemeta_t *sl_fm;
+	bool ok;
+	struct sl_tag tag;
+
+	switch (cpx_query_type) {
+	case SQ_CPX_TYPE_ARRAY:
+		sl_array = dalloc_zero(query, sl_array_t);
+		if (sl_array == NULL) {
+			return -1;
+		}
+		roffset = sl_unpack_loop(sl_array, buf, offset, bufsize,
+					 cpx_query_count, toc_offset, encoding);
+		if (roffset == -1) {
+			return -1;
+		}
+		result = dalloc_add(query, sl_array, sl_array_t);
+		if (result != 0) {
+			return -1;
+		}
+		break;
+
+	case SQ_CPX_TYPE_DICT:
+		sl_dict = dalloc_zero(query, sl_dict_t);
+		if (sl_dict == NULL) {
+			return -1;
+		}
+		roffset = sl_unpack_loop(sl_dict, buf, offset, bufsize,
+					 cpx_query_count, toc_offset, encoding);
+		if (roffset == -1) {
+			return -1;
+		}
+		result = dalloc_add(query, sl_dict, sl_dict_t);
+		if (result != 0) {
+			return -1;
+		}
+		break;
+
+	case SQ_CPX_TYPE_STRING:
+	case SQ_CPX_TYPE_UTF16_STRING:
+		offset = sl_unpack_tag(buf, offset, bufsize, encoding, &tag);
+		if (offset == -1) {
+			return -1;
+		}
+
+		if (tag.size < 16) {
+			DEBUG(1,("%s: string buffer too small\n", __func__));
+			return -1;
+		}
+		slen = tag.size - 16 + tag.count;
+		if (slen > MAX_SL_FRAGMENT_SIZE) {
+			return -1;
+		}
+
+		if (offset + slen > bufsize) {
+			DEBUG(1,("%s: buffer overflow\n", __func__));
+			return -1;
+		}
+
+		if (cpx_query_type == SQ_CPX_TYPE_STRING) {
+			p = talloc_strndup(query, buf + offset, slen);
+			if (p == NULL) {
+				return -1;
+			}
+		} else {
+			unicode_encoding = spotlight_get_utf16_string_encoding(
+				buf, offset, slen, encoding);
+			mark_exists = (unicode_encoding & SL_ENC_UTF_16) ? true : false;
+			if (unicode_encoding & SL_ENC_BIG_ENDIAN) {
+				DEBUG(1, ("Unsupported big endian UTF16 string"));
+				return -1;
+			}
+			slen -= mark_exists ? 2 : 0;
+			ok = convert_string_talloc(
+				query,
+				CH_UTF16LE,
+				CH_UTF8,
+				buf + offset + (mark_exists ? 2 : 0),
+				slen,
+				&p,
+				&tmp_len);
+			if (!ok) {
+				return -1;
+			}
+		}
+
+		result = dalloc_stradd(query, p);
+		if (result != 0) {
+			return -1;
+		}
+		roffset += tag.size;
+		break;
+
+	case SQ_CPX_TYPE_FILEMETA:
+		offset = sl_unpack_tag(buf, offset, bufsize, encoding, &tag);
+		if (offset == -1) {
+			return -1;
+		}
+		if (tag.size < 16) {
+			DEBUG(1, ("%s: size too mall: %zu", __func__, tag.size));
+			return -1;
+		}
+
+		sl_fm = dalloc_zero(query, sl_filemeta_t);
+		if (sl_fm == NULL) {
+			return -1;
+		}
+		result = sl_unpack(sl_fm, buf + offset, bufsize - offset );
+		if (result == -1) {
+			return -1;
+		}
+		result = dalloc_add(query, sl_fm, sl_filemeta_t);
+		if (result != 0) {
+			return -1;
+		}
+		roffset += tag.size;
+		break;
+
+	case SQ_CPX_TYPE_CNIDS:
+		offset = sl_unpack_tag(buf, offset, bufsize, encoding, &tag);
+		if (offset == -1) {
+			return -1;
+		}
+
+		result = sl_unpack_CNID(query, buf, offset, bufsize,
+					tag.length, encoding);
+		if (result == -1) {
+			return -1;
+		}
+		roffset += tag.size;
+		break;
+
+	default:
+		DEBUG(1, ("unkown complex query type: %u", cpx_query_type));
+		return -1;
+	}
+
+	return roffset;
+}
+
+static ssize_t sl_unpack_loop(DALLOC_CTX *query,
+			      const char *buf,
+			      ssize_t offset,
+			      size_t bufsize,
+			      int count,
+			      ssize_t toc_offset,
+			      int encoding)
+{
+	int i, toc_index, subcount;
+	uint64_t result;
+	sl_nil_t nil;
+	sl_bool_t b;
+	struct sl_tag tag, cpx_tag;
+
+	while (count > 0) {
+		if (offset >= toc_offset) {
+			return -1;
+		}
+
+		result = sl_unpack_tag(buf, offset, bufsize, encoding, &tag);
+		if (result == -1) {
+			return -1;
+		}
+
+		switch (tag.type) {
+		case SQ_TYPE_COMPLEX:
+			if (tag.count < 1) {
+				DEBUG(1,("%s: invalid tag.count: %d\n",
+					 __func__, tag.count));
+				return -1;
+			}
+			toc_index = tag.count - 1;
+			if (toc_index > MAX_SLQ_TOCIDX) {
+				DEBUG(1,("%s: toc_index too large: %d\n",
+					 __func__, toc_index));
+				return -1;
+			}
+			result = sl_unpack_tag(buf, toc_offset + (toc_index * 8),
+					       bufsize, encoding, &cpx_tag);
+			if (result == -1) {
+				return -1;
+			}
+
+			offset = sl_unpack_cpx(query, buf, offset + 8, bufsize, cpx_tag.type,
+					       cpx_tag.count, toc_offset, encoding);
+			if (offset == -1) {
+				return -1;
+			}
+			/*
+			 * tag.size is not the size here, so we need
+			 * to use the offset returned from sl_unpack_cpx()
+			 * instead of offset += tag.size;
+			 */
+			count--;
+			break;
+
+		case SQ_TYPE_NULL:
+			subcount = tag.count;
+			if (subcount > count) {
+				return -1;
+			}
+			nil = 0;
+			for (i = 0; i < subcount; i++) {
+				result = dalloc_add_copy(query, &nil, sl_nil_t);
+				if (result != 0) {
+					return -1;
+				}
+			}
+			offset += tag.size;
+			count -= subcount;
+			break;
+
+		case SQ_TYPE_BOOL:
+			b = tag.count != 0 ? true : false;
+			result = dalloc_add_copy(query, &b, sl_bool_t);
+			if (result != 0) {
+				return -1;
+			}
+			offset += tag.size;
+			count--;
+			break;
+
+		case SQ_TYPE_INT64:
+			subcount = sl_unpack_ints(query, buf, offset, bufsize, encoding);
+			if (subcount == -1 || subcount > count) {
+				return -1;
+			}
+			offset += tag.size;
+			count -= subcount;
+			break;
+
+		case SQ_TYPE_UUID:
+			subcount = sl_unpack_uuid(query, buf, offset, bufsize, encoding);
+			if (subcount == -1 || subcount > count) {
+				return -1;
+			}
+			offset += tag.size;
+			count -= subcount;
+			break;
+
+		case SQ_TYPE_FLOAT:
+			subcount = sl_unpack_floats(query, buf, offset, bufsize, encoding);
+			if (subcount == -1 || subcount > count) {
+				return -1;
+			}
+			offset += tag.size;
+			count -= subcount;
+			break;
+
+		case SQ_TYPE_DATE:
+			subcount = sl_unpack_date(query, buf, offset, bufsize, encoding);
+			if (subcount == -1 || subcount > count) {
+				return -1;
+			}
+			offset += tag.size;
+			count -= subcount;
+			break;
+
+		default:
+			DEBUG(1, ("unknown query type: %d\n", tag.type));
+			return -1;
+		}
+	}
+
+	return offset;
+}
+
+/******************************************************************************
+ * Global functions for packing und unpacking
+ ******************************************************************************/
+
+ssize_t sl_pack(DALLOC_CTX *query, char *buf, size_t bufsize)
+{
+	ssize_t result;
+	char *toc_buf;
+	int toc_index = 0;
+	int toc_count = 0;
+	ssize_t offset, len;
+	uint64_t hdr;
+	uint32_t total_octets;
+	uint32_t data_octets;
+	uint64_t tag;
+
+	memset(buf, 0, bufsize);
+
+	toc_buf = talloc_zero_size(query, MAX_SLQ_TOC + 8);
+	if (toc_buf == NULL) {
+		return -1;
+	}
+
+	offset = sl_pack_loop(query, buf, 16, bufsize, toc_buf + 8, &toc_index, &toc_count);
+	if (offset == -1 || offset < 16) {
+		DEBUG(10,("%s: sl_pack_loop error\n", __func__));
+		return -1;
+	}
+	len = offset - 16;
+
+	/*
+	 * Marshalling overview:
+	 *
+	 * 16 bytes at the start of buf:
+	 *
+	 * 8 bytes byte order mark
+	 * 4 bytes total octets
+	 * 4 bytes table of content octets
+	 *
+	 * x bytes total octets * 8 from sl_pack_loop
+	 * x bytes ToC octets * 8 from toc_buf
+	 */
+
+	/* Byte-order mark - we are using little endian only for now */
+	memcpy(buf, "432130dm", strlen("432130dm"));
+
+	/*
+	 * The data buffer and ToC buffer sizes are enocoded in number
+	 * of octets (size / 8), plus one, because the octet encoding
+	 * the sizes is included.
+	 */
+	data_octets = (len / 8) + 1;
+	total_octets = data_octets + toc_index + 1;
+
+	hdr = total_octets;
+	hdr |= ((uint64_t)data_octets << 32);
+
+	/* HDR */
+	result = sl_push_uint64_val(buf, 8, bufsize, hdr);
+	if (result == -1) {
+		return -1;
+	}
+
+	/*
+	 * ToC tag with number of ToC entries plus one, the ToC tag
+	 * header.
+	 */
+	tag = sl_pack_tag(SQ_TYPE_TOC, toc_index + 1, 0);
+	result = sl_push_uint64_val(toc_buf, 0, MAX_SLQ_TOC, tag);
+	if (result == -1) {
+		return -1;
+	}
+
+	if ((16 + len + ((toc_index + 1 ) * 8)) > bufsize) {
+		DEBUG(1, ("%s: exceeding size limit %zu", __func__, bufsize));
+		return -1;
+	}
+
+	memcpy(buf + 16 + len, toc_buf, (toc_index + 1 ) * 8);
+	len += 16 + (toc_index + 1 ) * 8;
+
+	return len;
+}
+
+bool sl_unpack(DALLOC_CTX *query, const char *buf, size_t bufsize)
+{
+	ssize_t result;
+	ssize_t offset = 0;
+	int encoding;
+	uint64_t hdr;
+	uint32_t total_octets;
+	uint64_t total_bytes;
+	uint32_t data_octets;
+	uint64_t data_bytes;
+	uint64_t toc_offset;
+	struct sl_tag toc_tag;
+
+	if (bufsize > MAX_SL_FRAGMENT_SIZE) {
+		return false;
+	}
+
+	if (bufsize < 8) {
+		return false;
+	}
+	if (strncmp(buf + offset, "md031234", 8) == 0) {
+		encoding = SL_ENC_BIG_ENDIAN;
+	} else {
+		encoding = SL_ENC_LITTLE_ENDIAN;
+	}
+	offset += 8;
+
+	offset = sl_pull_uint64_val(buf, offset, bufsize, encoding, &hdr);
+	if (offset == -1) {
+		return false;
+	}
+
+	total_octets = hdr & UINT32_MAX;
+	data_octets = hdr >> 32;
+
+	/*
+	 * Both fields contain the number of octets of the
+	 * corresponding buffer plus the tag octet. We adjust the
+	 * values to match just the number of octets in the buffers.
+	 */
+	if (total_octets < 1) {
+		return false;
+	}
+	if (data_octets < 1) {
+		return false;
+	}
+	total_octets--;
+	data_octets--;
+	data_bytes = data_octets * 8;
+	total_bytes = total_octets * 8;
+
+	if (data_bytes >= total_bytes) {
+		DEBUG(1,("%s: data_bytes: %" PRIu64 ", total_bytes: %" PRIu64 "\n",
+			 __func__, data_bytes, total_bytes));
+		return false;
+	}
+
+	if (total_bytes > (bufsize - offset)) {
+		return false;
+	}
+
+	toc_offset = data_bytes;
+
+	toc_offset = sl_unpack_tag(buf + offset, toc_offset,
+				   bufsize - offset, encoding, &toc_tag);
+	if (toc_offset == -1) {
+		return false;
+	}
+
+	if (toc_tag.type != SQ_TYPE_TOC) {
+		DEBUG(1,("%s: unknown tag type %d\n", __func__, toc_tag.type));
+		return false;
+	}
+
+	/*
+	 * Check toc_tag.size even though we don't use it when unmarshalling
+	 */
+	if (toc_tag.size > MAX_SLQ_TOC) {
+		DEBUG(1,("%s: bad size %zu\n", __func__, toc_tag.size));
+		return false;
+	}
+	if (toc_tag.size > (total_bytes - data_bytes)) {
+		DEBUG(1,("%s: bad size %zu\n", __func__, toc_tag.size));
+		return false;
+	}
+
+	if (toc_tag.count != 0) {
+		DEBUG(1,("%s: bad count %u\n", __func__, toc_tag.count));
+		return false;
+	}
+
+	/*
+	 * We already consumed 16 bytes from the buffer (BOM and size
+	 * tag), so we start at buf + offset.
+	 */
+	result = sl_unpack_loop(query, buf + offset, 0, bufsize - offset,
+				1, toc_offset, encoding);
+	if (result == -1) {
+		DEBUG(1,("%s: sl_unpack_loop failed\n", __func__));
+		return false;
+	}
+
+	return true;
+}
diff --git a/source3/rpc_server/mdssvc/marshalling.h b/source3/rpc_server/mdssvc/marshalling.h
new file mode 100644
index 0000000..086ca74
--- /dev/null
+++ b/source3/rpc_server/mdssvc/marshalling.h
@@ -0,0 +1,55 @@
+/*
+   Unix SMB/CIFS implementation.
+   Main metadata server / Spotlight routines
+
+   Copyright (C) Ralph Boehme 2015
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   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, see <http://www.gnu.org/licenses/>.
+*/
+
+#ifndef _MDSSVC_MARSHALLING_H
+#define _MDSSVC_MARSHALLING_H
+
+#include "dalloc.h"
+
+#define MAX_SL_FRAGMENT_SIZE 0xFFFFF
+
+/* Can be ored and used as flags */
+#define SL_ENC_LITTLE_ENDIAN 1
+#define SL_ENC_BIG_ENDIAN    2
+#define SL_ENC_UTF_16        4
+
+typedef DALLOC_CTX     sl_array_t;    /* an array of elements */
+typedef DALLOC_CTX     sl_dict_t;     /* an array of key/value elements */
+typedef DALLOC_CTX     sl_filemeta_t; /* contains one sl_array_t */
+typedef int            sl_nil_t;      /* a nil element */
+typedef bool           sl_bool_t;
+typedef struct timeval sl_time_t;
+typedef struct {
+	char sl_uuid[16];
+} sl_uuid_t;
+typedef struct {
+	uint16_t   ca_unkn1;
+	uint32_t   ca_context;
+	DALLOC_CTX *ca_cnids;
+} sl_cnids_t; /* an array of CNIDs */
+
+/******************************************************************************
+ * Function declarations
+ ******************************************************************************/
+
+extern ssize_t sl_pack(DALLOC_CTX *query, char *buf, size_t bufsize);
+extern bool sl_unpack(DALLOC_CTX *query, const char *buf, size_t bufsize);
+
+#endif
diff --git a/source3/rpc_server/mdssvc/mdssvc.h b/source3/rpc_server/mdssvc/mdssvc.h
index f016e96..db17b3c 100644
--- a/source3/rpc_server/mdssvc/mdssvc.h
+++ b/source3/rpc_server/mdssvc/mdssvc.h
@@ -18,4 +18,19 @@
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
+#ifndef _MDSSVC_H
+#define _MDSSVC_H
+
+#include "dalloc.h"
+#include "marshalling.h"
+
+/******************************************************************************
+ * Function declarations
+ ******************************************************************************/
+
+/*
+ * mdssvc.c
+ */
 int mds_dispatch(void);
+
+#endif /* _MDSSVC_H */
diff --git a/source3/rpc_server/wscript_build b/source3/rpc_server/wscript_build
index 03dc63e..d448e8b 100755
--- a/source3/rpc_server/wscript_build
+++ b/source3/rpc_server/wscript_build
@@ -131,6 +131,7 @@ bld.SAMBA3_SUBSYSTEM('RPC_WKSSVC',
 bld.SAMBA3_SUBSYSTEM('RPC_MDSSVC',
                     source='''mdssvc/mdssvc.c
                     mdssvc/dalloc.c
+                    mdssvc/marshalling.c
                     mdssvc/srv_mdssvc_nt.c
                     ../../librpc/gen_ndr/srv_mdssvc.c''',
                     deps='samba-util ' + bld.env['libtracker'],
-- 
2.1.0


From 03b749f7fd3c6bd8985a98de1e34914c0260030c Mon Sep 17 00:00:00 2001
From: Ralph Boehme <rb at sernet.de>
Date: Thu, 31 Jul 2014 17:56:44 +0200
Subject: [PATCH 07/12] s3-mdssvc: Spotlight attribute mappings

Add mappings for metadata attribute between Spotlight and NEPOMUK for
use with Tracker.

Signed-off-by: Ralph Boehme <slow at samba.org>
Reviewed-by: Stefan Metzmacher <metze at samba.org>
---
 source3/rpc_server/mdssvc/sparql_mapping.c | 373 +++++++++++++++++++++++++++++
 source3/rpc_server/mdssvc/sparql_mapping.h |  58 +++++
 source3/rpc_server/wscript_build           |   1 +
 3 files changed, 432 insertions(+)
 create mode 100644 source3/rpc_server/mdssvc/sparql_mapping.c
 create mode 100644 source3/rpc_server/mdssvc/sparql_mapping.h

diff --git a/source3/rpc_server/mdssvc/sparql_mapping.c b/source3/rpc_server/mdssvc/sparql_mapping.c
new file mode 100644
index 0000000..a3baf09
--- /dev/null
+++ b/source3/rpc_server/mdssvc/sparql_mapping.c
@@ -0,0 +1,373 @@
+/*
+   Unix SMB/CIFS implementation.
+   Main metadata server / Spotlight routines
+
+   Copyright (C) Ralph Boehme			2012-2014
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   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, see <http://www.gnu.org/licenses/>.
+*/
+
+#include "replace.h"
+#include "sparql_mapping.h"
+
+const struct sl_attr_map *sl_attr_map_by_spotlight(const char *sl_attr)
+{
+	static const struct sl_attr_map spotlight_sparql_attr_map[] = {
+		{
+			.spotlight_attr = "*",
+			.type = ssmt_fts,
+			.sparql_attr = "fts:match",
+		},
+
+		/* Filesystem metadata */
+		{
+			.spotlight_attr = "kMDItemFSLabel",
+			.type = ssmt_num,
+			.sparql_attr = NULL,
+		},
+		{
+			.spotlight_attr = "kMDItemDisplayName",
+			.type = ssmt_str,
+			.sparql_attr = "nfo:fileName",
+		},
+		{
+			.spotlight_attr = "kMDItemFSName",
+			.type = ssmt_str,
+			.sparql_attr = "nfo:fileName",
+		},
+		{
+			.spotlight_attr = "kMDItemFSContentChangeDate",
+			.type = ssmt_date,
+			.sparql_attr = "nfo:fileLastModified",
+		},
+		{
+			.spotlight_attr = "kMDItemLastUsedDate",
+			.type = ssmt_date,
+			.sparql_attr = "nfo:fileLastAccessed",
+		},
+
+		/* Common metadata */
+		{
+			.spotlight_attr = "kMDItemTextContent",
+			.type = ssmt_fts,
+			.sparql_attr = "fts:match",
+		},
+		{
+			.spotlight_attr = "kMDItemContentCreationDate",
+			.type = ssmt_date,
+			.sparql_attr = "nie:contentCreated",
+		},
+		{
+			.spotlight_attr = "kMDItemContentModificationDate",
+			.type = ssmt_date,
+			.sparql_attr = "nfo:fileLastModified",
+		},
+		{
+			.spotlight_attr = "kMDItemAttributeChangeDate",
+			.type = ssmt_date,
+			.sparql_attr = "nfo:fileLastModified",
+		},
+		{
+			.spotlight_attr = "kMDItemAuthors",
+			.type = ssmt_str,
+			.sparql_attr = "dc:creator",
+		},
+		{
+			.spotlight_attr = "kMDItemCopyright",
+			.type = ssmt_str,
+			.sparql_attr = "nie:copyright",
+		},
+		{
+			.spotlight_attr = "kMDItemCountry",
+			.type = ssmt_str,
+			.sparql_attr = "nco:country",
+		},
+		{
+			.spotlight_attr = "kMDItemCreator",
+			.type = ssmt_str,
+			.sparql_attr = "dc:creator",
+		},
+		{
+			.spotlight_attr = "kMDItemDurationSeconds",
+			.type = ssmt_num,
+			.sparql_attr = "nfo:duration",
+		},
+		{
+			.spotlight_attr = "kMDItemNumberOfPages",
+			.type = ssmt_num,
+			.sparql_attr = "nfo:pageCount",
+		},
+		{
+			.spotlight_attr = "kMDItemTitle",
+			.type = ssmt_str,
+			.sparql_attr = "nie:title",
+		},
+		{
+			.spotlight_attr = "kMDItemCity",
+			.type = ssmt_str,
+			.sparql_attr = "nco:locality",
+		},
+		{
+			.spotlight_attr = "kMDItemCoverage",
+			.type = ssmt_str,
+			.sparql_attr = "nco:locality",
+		},
+		{
+			.spotlight_attr = "_kMDItemGroupId",
+			.type = ssmt_type,
+			.sparql_attr = NULL,
+		},
+		{
+			.spotlight_attr = "kMDItemContentTypeTree",
+			.type = ssmt_type,
+			.sparql_attr = NULL,
+		},
+
+		/* Image metadata */
+		{
+			.spotlight_attr = "kMDItemPixelWidth",
+			.type = ssmt_num,
+			.sparql_attr = "nfo:width",
+		},
+		{
+			.spotlight_attr = "kMDItemPixelHeight",
+			.type = ssmt_num,
+			.sparql_attr = "nfo:height",
+		},
+		{
+			.spotlight_attr = "kMDItemColorSpace",
+			.type = ssmt_str,
+			.sparql_attr = "nexif:colorSpace",
+		},
+		{
+			.spotlight_attr = "kMDItemBitsPerSample",
+			.type = ssmt_num,
+			.sparql_attr = "nfo:colorDepth",
+		},
+		{
+			.spotlight_attr = "kMDItemFocalLength",
+			.type = ssmt_num,
+			.sparql_attr = "nmm:focalLength",
+		},
+		{
+			.spotlight_attr = "kMDItemISOSpeed",
+			.type = ssmt_num,
+			.sparql_attr = "nmm:isoSpeed",
+		},
+		{
+			.spotlight_attr = "kMDItemOrientation",
+			.type = ssmt_bool,
+			.sparql_attr = "nfo:orientation",
+		},
+		{
+			.spotlight_attr = "kMDItemResolutionWidthDPI",
+			.type = ssmt_num,
+			.sparql_attr = "nfo:horizontalResolution",
+		},
+		{
+			.spotlight_attr = "kMDItemResolutionHeightDPI",
+			.type = ssmt_num,
+			.sparql_attr = "nfo:verticalResolution",
+		},
+		{
+			.spotlight_attr = "kMDItemExposureTimeSeconds",
+			.type = ssmt_num,
+			.sparql_attr = "nmm:exposureTime",
+		},
+
+		/* Audio metadata */
+		{
+			.spotlight_attr = "kMDItemComposer",
+			.type = ssmt_str,
+			.sparql_attr = "nmm:composer",
+		},
+		{
+			.spotlight_attr = "kMDItemMusicalGenre",
+			.type = ssmt_str,
+			.sparql_attr = "nfo:genre",
+		},
+	};
+	size_t i;
+
+	for (i = 0; i < ARRAY_SIZE(spotlight_sparql_attr_map); i++) {
+		const struct sl_attr_map *m = &spotlight_sparql_attr_map[i];
+		int cmp;
+
+		cmp = strcmp(m->spotlight_attr, sl_attr);
+		if (cmp == 0) {
+			return m;
+		}
+	}
+
+	return NULL;
+}
+
+const struct sl_type_map *sl_type_map_by_spotlight(const char *sl_type)
+{
+	static const struct sl_type_map spotlight_sparql_type_map[] = {
+		{
+			.spotlight_type = "1",
+			.type = kMDTypeMapRDF,
+			.sparql_type = "http://www.semanticdesktop.org/ontologies/2007/03/22/nmo#Email",
+		},
+		{
+			.spotlight_type = "2",
+			.type = kMDTypeMapRDF,
+			.sparql_type = "http://www.semanticdesktop.org/ontologies/2007/03/22/nco#Contact",
+		},
+		{
+			.spotlight_type = "3",
+			.type = kMDTypeMapNotSup,
+			.sparql_type = NULL, /*PrefPane*/
+		},
+		{
+			.spotlight_type = "4",
+			.type = kMDTypeMapRDF,
+			.sparql_type = "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Font",
+		},
+		{
+			.spotlight_type = "5",
+			.type = kMDTypeMapRDF,
+			.sparql_type = "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Bookmark",
+		},
+		{
+			.spotlight_type = "6",
+			.type = kMDTypeMapRDF,
+			.sparql_type = "http://www.semanticdesktop.org/ontologies/2007/03/22/nco#Contact",
+		},
+		{
+			.spotlight_type = "7",
+			.type = kMDTypeMapRDF,
+			.sparql_type = "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Video",
+		},
+		{
+			.spotlight_type = "8",
+			.type = kMDTypeMapRDF,
+			.sparql_type = "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Executable",
+		},
+		{
+			.spotlight_type = "9",
+			.type = kMDTypeMapRDF,
+			.sparql_type = "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Folder",
+		},
+		{
+			.spotlight_type = "10",
+			.type = kMDTypeMapRDF,
+			.sparql_type = "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Audio",
+		},
+		{
+			.spotlight_type = "11",
+			.type = kMDTypeMapMime,
+			.sparql_type = "application/pdf",
+		},
+		{
+			.spotlight_type = "12",
+			.type = kMDTypeMapRDF,
+			.sparql_type = "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Presentation",
+		},
+		{
+			.spotlight_type = "13",
+			.type = kMDTypeMapRDF,
+			.sparql_type = "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Image",
+		},
+		{
+			.spotlight_type = "public.jpeg",
+			.type = kMDTypeMapMime,
+			.sparql_type = "image/jpeg",
+		},
+		{
+			.spotlight_type = "public.tiff",
+			.type = kMDTypeMapMime,
+			.sparql_type = "image/tiff",
+		},
+		{
+			.spotlight_type = "com.compuserve.gif",
+			.type = kMDTypeMapMime,
+			.sparql_type = "image/gif",
+		},
+		{
+			.spotlight_type = "public.png",
+			.type = kMDTypeMapMime,
+			.sparql_type = "image/png",
+		},
+		{
+			.spotlight_type = "com.microsoft.bmp",
+			.type = kMDTypeMapMime,
+			.sparql_type = "image/bmp",
+		},
+		{
+			.spotlight_type = "public.content",
+			.type = kMDTypeMapRDF,
+			.sparql_type = "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Document",
+		},
+		{
+			.spotlight_type = "public.mp3",
+			.type = kMDTypeMapMime,
+			.sparql_type = "audio/mpeg",
+		},
+		{
+			.spotlight_type = "public.mpeg-4-audio",
+			.type = kMDTypeMapMime,
+			.sparql_type = "audio/x-aac",
+		},
+		{
+			.spotlight_type = "com.apple.application",
+			.type = kMDTypeMapRDF,
+			.sparql_type = "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#Software",
+		},
+		{
+			.spotlight_type = "public.text",
+			.type = kMDTypeMapRDF,
+			.sparql_type = "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#TextDocument",
+		},
+		{
+			.spotlight_type = "public.plain-text",
+			.type = kMDTypeMapMime,
+			.sparql_type = "text/plain",
+		},
+		{
+			.spotlight_type = "public.rtf",
+			.type = kMDTypeMapMime,
+			.sparql_type = "text/rtf",
+		},
+		{
+			.spotlight_type = "public.html",
+			.type = kMDTypeMapMime,
+			.sparql_type = "text/html",
+		},
+		{
+			.spotlight_type = "public.xml",
+			.type = kMDTypeMapMime,
+			.sparql_type = "text/xml",
+		},
+		{
+			.spotlight_type = "public.source-code",
+			.type = kMDTypeMapRDF,
+			.sparql_type = "http://www.semanticdesktop.org/ontologies/2007/03/22/nfo#SourceCode",
+		},
+	};
+	size_t i;
+
+	for (i = 0; i < ARRAY_SIZE(spotlight_sparql_type_map); i++) {
+		const struct sl_type_map *m = &spotlight_sparql_type_map[i];
+		int cmp;
+
+		cmp = strcmp(m->spotlight_type, sl_type);
+		if (cmp == 0) {
+			return m;
+		}
+	}
+
+	return NULL;
+}
diff --git a/source3/rpc_server/mdssvc/sparql_mapping.h b/source3/rpc_server/mdssvc/sparql_mapping.h
new file mode 100644
index 0000000..496e19c
--- /dev/null
+++ b/source3/rpc_server/mdssvc/sparql_mapping.h
@@ -0,0 +1,58 @@
+/*
+  Copyright (c) 2012 Ralph Boehme
+
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 2 of the License, or
+  (at your option) any later version.
+
+  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.
+*/
+
+#ifndef SPOTLIGHT_SPARQL_MAP_H
+#define SPOTLIGHT_SPARQL_MAP_H
+
+enum ssm_type {
+	ssmt_bool,   /* a boolean value that doesn't requires a SPARQL FILTER */
+	ssmt_num,    /* a numeric value that requires a SPARQL FILTER */
+	ssmt_str,    /* a string value that requieres a SPARQL FILTER */
+	ssmt_fts,    /* a string value that will be queried with SPARQL 'fts:match' */
+	ssmt_date,   /* date values are handled in a special map function map_daterange() */
+	ssmt_type    /* kMDItemContentType, requires special mapping */
+};
+
+struct sl_attr_map {
+	const char *spotlight_attr;
+	enum ssm_type type;
+	const char *sparql_attr;
+};
+
+enum kMDTypeMap {
+	kMDTypeMapNotSup, /* not supported */
+	kMDTypeMapRDF,    /* query with rdf:type */
+	kMDTypeMapMime    /* query with nie:mimeType */
+};
+
+struct sl_type_map {
+	/*
+	 * MD query value of attributes '_kMDItemGroupId' and
+	 * 'kMDItemContentTypeTree
+	 */
+	const char *spotlight_type;
+
+	/*
+	 * Whether SPARQL query must search attribute rdf:type or
+	 * nie:mime_Type
+	 */
+	enum kMDTypeMap type;
+
+	/* the SPARQL query match string */
+	const char *sparql_type;
+};
+
+const struct sl_attr_map *sl_attr_map_by_spotlight(const char *sl_attr);
+const struct sl_type_map *sl_type_map_by_spotlight(const char *sl_type);
+#endif
diff --git a/source3/rpc_server/wscript_build b/source3/rpc_server/wscript_build
index d448e8b..1da41f2 100755
--- a/source3/rpc_server/wscript_build
+++ b/source3/rpc_server/wscript_build
@@ -132,6 +132,7 @@ bld.SAMBA3_SUBSYSTEM('RPC_MDSSVC',
                     source='''mdssvc/mdssvc.c
                     mdssvc/dalloc.c
                     mdssvc/marshalling.c
+                    mdssvc/sparql_mapping.c
                     mdssvc/srv_mdssvc_nt.c
                     ../../librpc/gen_ndr/srv_mdssvc.c''',
                     deps='samba-util ' + bld.env['libtracker'],
-- 
2.1.0


From abc244c82af4841ea458f947e6c3c7d9dca59984 Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Thu, 26 Mar 2015 22:39:21 +0100
Subject: [PATCH 08/12] s3-mdssvc: main Spotlight code

Implement all Spotlight RPC commands with the Tracker SPARQL async query
API.

Tracker uses glib for implemeting async tasks, we thus have to use a
glib mainloop for processing sheduled tasks in threads.

Signed-off-by: Ralph Boehme <slow at samba.org>
Reviewed-by: Stefan Metzmacher <metze at samba.org>
---
 source3/rpc_server/mdssvc/mdssvc.c        | 2043 ++++++++++++++++++++++++++++-
 source3/rpc_server/mdssvc/mdssvc.h        |   90 +-
 source3/rpc_server/mdssvc/srv_mdssvc_nt.c |  176 ++-
 source3/rpc_server/mdssvc/srv_mdssvc_nt.h |   27 +
 source3/rpc_server/rpc_service_setup.c    |   29 +-
 5 files changed, 2351 insertions(+), 14 deletions(-)
 create mode 100644 source3/rpc_server/mdssvc/srv_mdssvc_nt.h

diff --git a/source3/rpc_server/mdssvc/mdssvc.c b/source3/rpc_server/mdssvc/mdssvc.c
index 20e45f1..ba39e25 100644
--- a/source3/rpc_server/mdssvc/mdssvc.c
+++ b/source3/rpc_server/mdssvc/mdssvc.c
@@ -19,13 +19,2052 @@
 */
 
 #include "includes.h"
+#include "librpc/gen_ndr/auth.h"
+#include "dbwrap/dbwrap.h"
+#include "lib/util/dlinklist.h"
+#include "lib/util/util_tdb.h"
+#include "lib/util/time_basic.h"
+#include "lib/dbwrap/dbwrap_rbt.h"
+#include "libcli/security/dom_sid.h"
 #include "mdssvc.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_RPC_SRV
 
-int mds_dispatch(void)
+#define SLQ_DEBUG(lvl, _slq, state) do { if (CHECK_DEBUGLVL(lvl)) {	\
+	const struct sl_query *__slq = _slq;				\
+	struct timeval_buf start_buf;					\
+	const char *start;						\
+	struct timeval_buf last_used_buf;				\
+	const char *last_used;						\
+	struct timeval_buf expire_buf;					\
+	const char *expire;						\
+	start = timeval_str_buf(&__slq->start_time, false,		\
+				true, &start_buf);			\
+	last_used = timeval_str_buf(&__slq->last_used, false,		\
+				    true, &last_used_buf);		\
+	expire = timeval_str_buf(&__slq->expire_time, false,		\
+				 true, &expire_buf);			\
+	DEBUG(lvl,("%s slq[0x%jx,0x%jx], start: %s, last_used: %s, "	\
+		   "expires: %s, query: '%s'\n", state,			\
+		   (uintmax_t)__slq->ctx1, (uintmax_t)__slq->ctx2,	\
+		   start, last_used, expire, __slq->query_string));	\
+}} while(0)
+
+struct slrpc_cmd {
+	const char *name;
+	bool (*function)(struct mds_ctx *mds_ctx,
+			 const DALLOC_CTX *query,
+			 DALLOC_CTX *reply);
+};
+
+struct slq_destroy_state {
+	struct tevent_context *ev;
+	struct sl_query *slq;
+};
+
+/*
+ * If these functions return an error, they hit something like a non
+ * recoverable talloc error. Most errors are dealt with by returning
+ * an errror code in the Spotlight RPC reply.
+ */
+static bool slrpc_fetch_properties(struct mds_ctx *mds_ctx,
+				   const DALLOC_CTX *query, DALLOC_CTX *reply);
+static bool slrpc_open_query(struct mds_ctx *mds_ctx,
+			     const DALLOC_CTX *query, DALLOC_CTX *reply);
+static bool slrpc_fetch_query_results(struct mds_ctx *mds_ctx,
+				      const DALLOC_CTX *query, DALLOC_CTX *reply);
+static bool slrpc_store_attributes(struct mds_ctx *mds_ctx,
+				   const DALLOC_CTX *query, DALLOC_CTX *reply);
+static bool slrpc_fetch_attributenames(struct mds_ctx *mds_ctx,
+				       const DALLOC_CTX *query, DALLOC_CTX *reply);
+static bool slrpc_fetch_attributes(struct mds_ctx *mds_ctx,
+				   const DALLOC_CTX *query, DALLOC_CTX *reply);
+static bool slrpc_close_query(struct mds_ctx *mds_ctx,
+			      const DALLOC_CTX *query, DALLOC_CTX *reply);
+
+static struct tevent_req *slq_destroy_send(TALLOC_CTX *mem_ctx,
+					   struct tevent_context *ev,
+					   struct sl_query **slq)
+{
+	struct tevent_req *req;
+	struct slq_destroy_state *state;
+
+	req = tevent_req_create(mem_ctx, &state,
+				struct slq_destroy_state);
+	if (req == NULL) {
+		return NULL;
+	}
+	state->slq = talloc_move(state, slq);
+	tevent_req_done(req);
+
+	return tevent_req_post(req, ev);
+}
+
+static void slq_destroy_recv(struct tevent_req *req)
+{
+	tevent_req_received(req);
+}
+
+/************************************************
+ * Misc utility functions
+ ************************************************/
+
+static char *tab_level(TALLOC_CTX *mem_ctx, int level)
+{
+	int i;
+	char *string = talloc_array(mem_ctx, char, level + 1);
+
+	for (i = 0; i < level; i++) {
+		string[i] = '\t';
+	}
+
+	string[i] = '\0';
+	return string;
+}
+
+char *mds_dalloc_dump(DALLOC_CTX *dd, int nestinglevel)
+{
+	const char *type;
+	int n, result;
+	uint64_t i;
+	sl_bool_t bl;
+	sl_time_t t;
+	struct tm *tm;
+	char datestring[256];
+	sl_cnids_t cnids;
+	char *logstring, *nested_logstring;
+	char *tab_string1, *tab_string2;
+	void *p;
+	bool ok;
+	char *utf8string;
+	size_t utf8len;
+
+	tab_string1 = tab_level(dd, nestinglevel);
+	if (tab_string1 == NULL) {
+		return NULL;
+	}
+	tab_string2 = tab_level(dd, nestinglevel + 1);
+	if (tab_string2 == NULL) {
+		return NULL;
+	}
+
+	logstring = talloc_asprintf(dd,
+				    "%s%s(#%lu): {\n",
+				    tab_string1,
+				    talloc_get_name(dd),
+				    dalloc_size(dd));
+	if (logstring == NULL) {
+		return NULL;
+	}
+
+	for (n = 0; n < dalloc_size(dd); n++) {
+		type = dalloc_get_name(dd, n);
+		if (type == NULL) {
+			return NULL;
+		}
+		p = dalloc_get_object(dd, n);
+		if (p == NULL) {
+			return NULL;
+		}
+		if (strcmp(type, "DALLOC_CTX") == 0
+		    || strcmp(type, "sl_array_t") == 0
+		    || strcmp(type, "sl_filemeta_t") == 0
+		    || strcmp(type, "sl_dict_t") == 0) {
+			nested_logstring = mds_dalloc_dump(p, nestinglevel + 1);
+			if (nested_logstring == NULL) {
+				return NULL;
+			}
+			logstring = talloc_strdup_append(logstring,
+							 nested_logstring);
+		} else if (strcmp(type, "uint64_t") == 0) {
+			memcpy(&i, p, sizeof(uint64_t));
+			logstring = talloc_asprintf_append(
+				logstring,
+				"%suint64_t: 0x%04jx\n",
+				tab_string2, (uintmax_t)i);
+		} else if (strcmp(type, "char *") == 0) {
+			logstring = talloc_asprintf_append(
+				logstring,
+				"%sstring: %s\n",
+				tab_string2,
+				(char *)p);
+		} else if (strcmp(type, "smb_ucs2_t *") == 0) {
+			ok = convert_string_talloc(talloc_tos(),
+						   CH_UTF16LE,
+						   CH_UTF8,
+						   p,
+						   talloc_get_size(p),
+						   &utf8string,
+						   &utf8len);
+			if (!ok) {
+				return NULL;
+			}
+			logstring = talloc_asprintf_append(
+				logstring,
+				"%sUTF16-string: %s\n",
+				tab_string2,
+				utf8string);
+			TALLOC_FREE(utf8string);
+		} else if (strcmp(type, "sl_bool_t") == 0) {
+			memcpy(&bl, p, sizeof(sl_bool_t));
+			logstring = talloc_asprintf_append(
+				logstring,
+				"%sbool: %s\n",
+				tab_string2,
+				bl ? "true" : "false");
+		} else if (strcmp(type, "sl_nil_t") == 0) {
+			logstring = talloc_asprintf_append(
+				logstring,
+				"%snil\n",
+				tab_string2);
+		} else if (strcmp(type, "sl_time_t") == 0) {
+			memcpy(&t, p, sizeof(sl_time_t));
+			tm = localtime(&t.tv_sec);
+			if (tm == NULL) {
+				return NULL;
+			}
+			result = strftime(datestring,
+					 sizeof(datestring),
+					 "%Y-%m-%d %H:%M:%S", tm);
+			if (result == 0) {
+				return NULL;
+			}
+			logstring = talloc_asprintf_append(
+				logstring,
+				"%ssl_time_t: %s.%06lu\n",
+				tab_string2,
+				datestring,
+				(unsigned long)t.tv_usec);
+		} else if (strcmp(type, "sl_cnids_t") == 0) {
+			memcpy(&cnids, p, sizeof(sl_cnids_t));
+			logstring = talloc_asprintf_append(
+				logstring,
+				"%sCNIDs: unkn1: 0x%" PRIx16 ", unkn2: 0x%" PRIx32 "\n",
+				tab_string2,
+				cnids.ca_unkn1,
+				cnids.ca_context);
+			if (logstring == NULL) {
+				return NULL;
+			}
+			if (cnids.ca_cnids) {
+				nested_logstring = mds_dalloc_dump(
+					cnids.ca_cnids,
+					nestinglevel + 2);
+				if (!nested_logstring) {
+					return NULL;
+				}
+				logstring = talloc_strdup_append(logstring,
+								 nested_logstring);
+			}
+		} else {
+			logstring = talloc_asprintf_append(
+				logstring,
+				"%stype: %s\n",
+				tab_string2,
+				type);
+		}
+		if (logstring == NULL) {
+			return NULL;
+		}
+	}
+	logstring = talloc_asprintf_append(logstring,
+					   "%s}\n",
+					   tab_string1);
+	if (logstring == NULL) {
+		return NULL;
+	}
+	return logstring;
+}
+
+static char *tracker_to_unix_path(TALLOC_CTX *mem_ctx, const char *uri)
+{
+	GFile *f;
+	char *path;
+	char *talloc_path;
+
+	f = g_file_new_for_uri(uri);
+	if (f == NULL) {
+		return NULL;
+	}
+
+	path = g_file_get_path(f);
+	g_object_unref(f);
+
+	if (path == NULL) {
+		return NULL;
+	}
+
+	talloc_path = talloc_strdup(mem_ctx, path);
+	g_free(path);
+	if (talloc_path == NULL) {
+		return NULL;
+	}
+
+	return talloc_path;
+}
+
+/**
+ * Add requested metadata for a query result element
+ *
+ * This could be rewritten to something more sophisticated like
+ * querying metadata from Tracker.
+ *
+ * If path or sp is NULL, simply add nil values for all attributes.
+ **/
+static bool add_filemeta(sl_array_t *reqinfo,
+			 sl_array_t *fm_array,
+			 const char *path,
+			 const struct stat_ex *sp)
+{
+	sl_array_t *meta;
+	sl_nil_t nil;
+	int i, metacount, result;
+	uint64_t uint64var;
+	sl_time_t sl_time;
+	char *p;
+	const char *attribute;
+
+	metacount = dalloc_size(reqinfo);
+	if (metacount == 0 || path == NULL || sp == NULL) {
+		result = dalloc_add_copy(fm_array, &nil, sl_nil_t);
+		if (result != 0) {
+			return false;
+		}
+		return true;
+	}
+
+	meta = dalloc_zero(fm_array, sl_array_t);
+	if (meta == NULL) {
+		return false;
+	}
+
+	for (i = 0; i < metacount; i++) {
+		attribute = dalloc_get_object(reqinfo, i);
+		if (strcmp(attribute, "kMDItemDisplayName") == 0
+		    || strcmp(attribute, "kMDItemFSName") == 0) {
+			p = strrchr(path, '/');
+			if (p) {
+				result = dalloc_stradd(meta, p + 1);
+				if (result != 0) {
+					return false;
+				}
+			}
+		} else if (strcmp(attribute, "kMDItemPath") == 0) {
+			result = dalloc_stradd(meta, path);
+			if (result != 0) {
+				return false;
+			}
+		} else if (strcmp(attribute, "kMDItemFSSize") == 0) {
+			uint64var = sp->st_ex_size;
+			result = dalloc_add_copy(meta, &uint64var, uint64_t);
+			if (result != 0) {
+				return false;
+			}
+		} else if (strcmp(attribute, "kMDItemFSOwnerUserID") == 0) {
+			uint64var = sp->st_ex_uid;
+			result = dalloc_add_copy(meta, &uint64var, uint64_t);
+			if (result != 0) {
+				return false;
+			}
+		} else if (strcmp(attribute, "kMDItemFSOwnerGroupID") == 0) {
+			uint64var = sp->st_ex_gid;
+			result = dalloc_add_copy(meta, &uint64var, uint64_t);
+			if (result != 0) {
+				return false;
+			}
+		} else if (strcmp(attribute, "kMDItemFSContentChangeDate") == 0) {
+			sl_time.tv_sec = sp->st_ex_mtime.tv_sec;
+			result = dalloc_add_copy(meta, &sl_time, sl_time_t);
+			if (result != 0) {
+				return false;
+			}
+		} else {
+			result = dalloc_add_copy(meta, &nil, sl_nil_t);
+			if (result != 0) {
+				return false;
+			}
+		}
+	}
+
+	result = dalloc_add(fm_array, meta, sl_array_t);
+	if (result != 0) {
+		return false;
+	}
+	return true;
+}
+
+static int cnid_comp_fn(const void *p1, const void *p2)
+{
+	const uint64_t *cnid1 = p1, *cnid2 = p2;
+	if (*cnid1 == *cnid2) {
+		return 0;
+	}
+	if (*cnid1 < *cnid2) {
+		return -1;
+	}
+	return 1;
+}
+
+/**
+ * Create a sorted copy of a CNID array
+ **/
+static bool sort_cnids(struct sl_query *slq, const DALLOC_CTX *d)
+{
+	uint64_t *cnids = NULL;
+	int i;
+	const void *p;
+
+	cnids = talloc_array(slq, uint64_t, dalloc_size(d));
+	if (cnids == NULL) {
+		return false;
+	}
+
+	for (i = 0; i < dalloc_size(d); i++) {
+		p = dalloc_get_object(d, i);
+		if (p == NULL) {
+			return NULL;
+		}
+		memcpy(&cnids[i], p, sizeof(uint64_t));
+	}
+	qsort(cnids, dalloc_size(d), sizeof(uint64_t), cnid_comp_fn);
+
+	slq->cnids = cnids;
+	slq->cnids_num = dalloc_size(d);
+
+	return true;
+}
+
+/**
+ * Allocate result handle used in the async Tracker cursor result
+ * handler for storing results
+ **/
+static bool create_result_handle(struct sl_query *slq)
+{
+	sl_nil_t nil = 0;
+	struct sl_rslts *query_results;
+	int result;
+
+	if (slq->query_results) {
+		DEBUG(1, ("unexpected existing result handle\n"));
+		return false;
+	}
+
+	query_results = talloc_zero(slq, struct sl_rslts);
+	if (query_results == NULL) {
+		return false;
+	}
+
+	/* CNIDs */
+	query_results->cnids = talloc_zero(query_results, sl_cnids_t);
+	if (query_results->cnids == NULL) {
+		return false;
+	}
+	query_results->cnids->ca_cnids = dalloc_new(query_results->cnids);
+	if (query_results->cnids->ca_cnids == NULL) {
+		return false;
+	}
+
+	query_results->cnids->ca_unkn1 = 0xadd;
+	if (slq->ctx2 > UINT32_MAX) {
+		DEBUG(1,("64bit ctx2 id too large: 0x%jx", (uintmax_t)slq->ctx2));
+		return false;
+	}
+	query_results->cnids->ca_context = (uint32_t)slq->ctx2;
+
+	/* FileMeta */
+	query_results->fm_array = dalloc_zero(query_results, sl_array_t);
+	if (query_results->fm_array == NULL) {
+		return false;
+	}
+
+	/* For some reason the list of results always starts with a nil entry */
+	result = dalloc_add_copy(query_results->fm_array, &nil, sl_nil_t);
+	if (result != 0) {
+		return false;
+	}
+
+	slq->query_results = query_results;
+	return true;
+}
+
+static bool add_results(sl_array_t *array, struct sl_query *slq)
+{
+	sl_filemeta_t *fm;
+	uint64_t status = 0;
+	int result;
+	bool ok;
+
+	/* FileMeta */
+	fm = dalloc_zero(array, sl_filemeta_t);
+	if (fm == NULL) {
+		return false;
+	}
+
+	result = dalloc_add_copy(array, &status, uint64_t);
+	if (result != 0) {
+		return false;
+	}
+	result = dalloc_add(array, slq->query_results->cnids, sl_cnids_t);
+	if (result != 0) {
+		return false;
+	}
+	if (slq->query_results->num_results > 0) {
+		result = dalloc_add(fm, slq->query_results->fm_array, sl_array_t);
+		if (result != 0) {
+			return false;
+		}
+	}
+	result = dalloc_add(array, fm, sl_filemeta_t);
+	if (result != 0) {
+		return false;
+	}
+
+	/* This ensure the results get clean up after been sent to the client */
+	talloc_move(array, &slq->query_results);
+
+	ok = create_result_handle(slq);
+	if (!ok) {
+		DEBUG(1, ("couldn't add result handle\n"));
+		slq->state = SLQ_STATE_ERROR;
+		return false;
+	}
+
+	return true;
+}
+
+static const struct slrpc_cmd *slrpc_cmd_by_name(const char *rpccmd)
+{
+	size_t i;
+	static const struct slrpc_cmd cmds[] = {
+		{ "fetchPropertiesForContext:", slrpc_fetch_properties},
+		{ "openQueryWithParams:forContext:", slrpc_open_query},
+		{ "fetchQueryResultsForContext:", slrpc_fetch_query_results},
+		{ "storeAttributes:forOIDArray:context:", slrpc_store_attributes},
+		{ "fetchAttributeNamesForOIDArray:context:", slrpc_fetch_attributenames},
+		{ "fetchAttributes:forOIDArray:context:", slrpc_fetch_attributes},
+		{ "fetchAllAttributes:forOIDArray:context:", slrpc_fetch_attributes},
+		{ "closeQueryForContext:", slrpc_close_query},
+	};
+
+	for (i = 0; i < ARRAY_SIZE(cmds); i++) {
+		int cmp;
+
+		cmp = strcmp(cmds[i].name, rpccmd);
+		if (cmp == 0) {
+			return &cmds[i];
+		}
+	}
+
+	return NULL;
+}
+
+/**
+ * Search the list of active queries given their context ids
+ **/
+static struct sl_query *slq_for_ctx(struct mds_ctx *mds_ctx,
+				    uint64_t ctx1, uint64_t ctx2)
+{
+	struct sl_query *q;
+
+	for (q = mds_ctx->query_list; q; q = q->next) {
+		if ((q->ctx1 == ctx1) && (q->ctx2 == ctx2)) {
+			return q;
+		}
+	}
+
+	return NULL;
+}
+
+static int slq_destructor_cb(struct sl_query *slq)
+{
+	SLQ_DEBUG(10, slq, "destroying");
+
+	/* Free all entries before freeing the slq handle! */
+	TALLOC_FREE(slq->entries_ctx);
+	TALLOC_FREE(slq->te);
+
+	if (slq->mds_ctx != NULL) {
+		DLIST_REMOVE(slq->mds_ctx->query_list, slq);
+		slq->mds_ctx = NULL;
+	}
+
+	if (slq->tracker_cursor != NULL) {
+		g_object_unref(slq->tracker_cursor);
+		slq->tracker_cursor = NULL;
+	}
+
+	if (slq->gcancellable != NULL) {
+		g_cancellable_cancel(slq->gcancellable);
+		g_object_unref(slq->gcancellable);
+		slq->gcancellable = NULL;
+	}
+
+	return 0;
+}
+
+/**
+ * Remove talloc_refcounted entry from mapping db
+ *
+ * Multiple queries (via the slq handle) may reference a
+ * sl_inode_path_map entry, when the last reference goes away as the
+ * queries are closed and this gets called to remove the entry from
+ * the db.
+ **/
+static int ino_path_map_destr_cb(struct sl_inode_path_map *entry)
 {
-	DEBUG(10, ("mds_dispatch\n"));
+	NTSTATUS status;
+	TDB_DATA key;
+
+	key = make_tdb_data((uint8_t *)&entry->ino, sizeof(entry->ino));
+
+	status = dbwrap_delete(entry->mds_ctx->ino_path_map, key);
+	if (!NT_STATUS_IS_OK(status)) {
+		DEBUG(1, ("Failed to delete record: %s\n", nt_errstr(status)));
+		return -1;
+	}
+
+	DEBUG(10,("deleted: %s\n", entry->path));
 	return 0;
 }
+
+/**
+ * Add result to inode->path mapping dbwrap rbt db
+ *
+ * This is necessary as a CNID db substitute, ie we need a way to
+ * simulate unique, constant numerical identifiers for paths with an
+ * API that supports mapping from id to path.
+ *
+ * Entries are talloc'ed of the query, using talloc_reference() if
+ * multiple queries returned the same result. That way we can cleanup
+ * entries by calling talloc_free() on the query slq handles.
+ **/
+
+static bool inode_map_add(struct sl_query *slq, uint64_t ino, const char *path)
+{
+	NTSTATUS status;
+	struct sl_inode_path_map *entry;
+	TDB_DATA key, value;
+	void *p;
+
+	key = make_tdb_data((uint8_t *)&ino, sizeof(ino));
+	status = dbwrap_fetch(slq->mds_ctx->ino_path_map, slq, key, &value);
+
+	if (NT_STATUS_IS_OK(status)) {
+		/*
+		 * We have one db, so when different parallel queries
+		 * return the same file, we have to refcount entries
+		 * in the db.
+		 */
+
+		if (value.dsize != sizeof(void *)) {
+			DEBUG(1, ("invalide dsize\n"));
+			return false;
+		}
+		memcpy(&p, value.dptr, sizeof(p));
+		entry = talloc_get_type_abort(p, struct sl_inode_path_map);
+
+		DEBUG(10, ("map: %s\n", entry->path));
+
+		entry = talloc_reference(slq->entries_ctx, entry);
+		if (entry == NULL) {
+			DEBUG(1, ("talloc_reference failed\n"));
+			return false;
+		}
+		return true;
+	}
+
+	if (!NT_STATUS_EQUAL(status, NT_STATUS_NOT_FOUND)) {
+		DEBUG(1, ("dbwrap_fetch failed %s\n", nt_errstr(status)));
+		return false;
+	}
+
+	entry = talloc_zero(slq->entries_ctx, struct sl_inode_path_map);
+	if (entry == NULL) {
+		DEBUG(1, ("talloc failed\n"));
+		return false;
+	}
+
+	entry->ino = ino;
+	entry->mds_ctx = slq->mds_ctx;
+	entry->path = talloc_strdup(entry, path);
+	if (entry->path == NULL) {
+		DEBUG(1, ("talloc failed\n"));
+		TALLOC_FREE(entry);
+		return false;
+	}
+
+	status = dbwrap_store(slq->mds_ctx->ino_path_map, key,
+			      make_tdb_data((void *)&entry, sizeof(void *)), 0);
+	if (!NT_STATUS_IS_OK(status)) {
+		DEBUG(1, ("Failed to store record: %s\n", nt_errstr(status)));
+		TALLOC_FREE(entry);
+		return false;
+	}
+
+	talloc_set_destructor(entry, ino_path_map_destr_cb);
+
+	return true;
+}
+
+/************************************************
+ * Tracker async callbacks
+ ************************************************/
+
+static void tracker_con_cb(GObject *object,
+			   GAsyncResult *res,
+			   gpointer user_data)
+{
+	struct mds_ctx *mds_ctx = talloc_get_type_abort(user_data, struct mds_ctx);
+	GError *error = NULL;
+
+	mds_ctx->tracker_con = tracker_sparql_connection_get_finish(res,
+								    &error);
+	if (error) {
+		DEBUG(1, ("Could not connect to Tracker: %s\n",
+			  error->message));
+		g_error_free(error);
+	}
+
+	DEBUG(10, ("connected to Tracker\n"));
+	g_main_loop_quit(mds_ctx->gmainloop);
+}
+
+static void tracker_cursor_cb_destroy_done(struct tevent_req *subreq);
+
+static void tracker_cursor_cb(GObject *object,
+			      GAsyncResult *res,
+			      gpointer user_data)
+{
+	GError *error = NULL;
+	struct sl_query *slq = talloc_get_type_abort(user_data, struct sl_query);
+	gboolean more_results;
+	const gchar *uri;
+	char *path;
+	int result;
+	struct stat_ex sb;
+	uint64_t ino64;
+	bool ok;
+	struct tevent_req *req;
+
+	SLQ_DEBUG(10, slq, "tracker_cursor_cb");
+
+	more_results = tracker_sparql_cursor_next_finish(slq->tracker_cursor,
+							 res,
+							 &error);
+
+	if (slq->state == SLQ_STATE_DONE) {
+		/*
+		 * The query was closed in slrpc_close_query(), so we
+		 * don't care for results or errors from
+		 * tracker_sparql_cursor_next_finish(), we just go
+		 * ahead and schedule deallocation of the slq handle.
+		 *
+		 * We have to shedule the deallocation via tevent,
+		 * because we have to unref the cursor glib object and
+		 * we can't do it here, because it's still used after
+		 * we return.
+		 */
+		SLQ_DEBUG(10, slq, "closed");
+		g_main_loop_quit(slq->mds_ctx->gmainloop);
+
+		req = slq_destroy_send(slq, server_event_context(), &slq);
+		if (req == NULL) {
+			slq->state = SLQ_STATE_ERROR;
+			return;
+		}
+		tevent_req_set_callback(req, tracker_cursor_cb_destroy_done, NULL);
+		return;
+	}
+
+	if (error) {
+		DEBUG(1, ("Tracker cursor: %s\n", error->message));
+		g_error_free(error);
+		slq->state = SLQ_STATE_ERROR;
+		g_main_loop_quit(slq->mds_ctx->gmainloop);
+		return;
+	}
+
+	if (!more_results) {
+		slq->state = SLQ_STATE_DONE;
+		g_main_loop_quit(slq->mds_ctx->gmainloop);
+		return;
+	}
+
+	uri = tracker_sparql_cursor_get_string(slq->tracker_cursor, 0, NULL);
+	if (uri == NULL) {
+		DEBUG(1, ("error fetching Tracker URI\n"));
+		slq->state = SLQ_STATE_ERROR;
+		g_main_loop_quit(slq->mds_ctx->gmainloop);
+		return;
+	}
+	path = tracker_to_unix_path(slq->query_results, uri);
+	if (path == NULL) {
+		DEBUG(1, ("error converting Tracker URI to path: %s\n", uri));
+		slq->state = SLQ_STATE_ERROR;
+		g_main_loop_quit(slq->mds_ctx->gmainloop);
+		return;
+	}
+
+	if (geteuid() != slq->mds_ctx->uid) {
+		DEBUG(0, ("uid mismatch: %d/%d\n", geteuid(), slq->mds_ctx->uid));
+		smb_panic("uid mismatch");
+	}
+
+	result = sys_stat(path, &sb, false);
+	if (result != 0) {
+		goto done;
+	}
+	result = access(path, R_OK);
+	if (result != 0) {
+		goto done;
+	}
+
+	ino64 = sb.st_ex_ino;
+	if (slq->cnids) {
+		/*
+		 * Check whether the found element is in the requested
+		 * set of IDs. Note that we're faking CNIDs by using
+		 * filesystem inode numbers here
+		 */
+		ok = bsearch(&ino64, slq->cnids, slq->cnids_num,
+			     sizeof(uint64_t), cnid_comp_fn);
+		if (!ok) {
+			goto done;
+		}
+	}
+
+	/*
+	 * Add inode number and filemeta to result set, this is what
+	 * we return as part of the result set of a query
+	 */
+	result = dalloc_add_copy(slq->query_results->cnids->ca_cnids,
+				 &ino64, uint64_t);
+	if (result != 0) {
+		DEBUG(1, ("dalloc error\n"));
+		slq->state = SLQ_STATE_ERROR;
+		g_main_loop_quit(slq->mds_ctx->gmainloop);
+		return;
+	}
+	ok = add_filemeta(slq->reqinfo, slq->query_results->fm_array,
+			  path, &sb);
+	if (!ok) {
+		DEBUG(1, ("add_filemeta error\n"));
+		slq->state = SLQ_STATE_ERROR;
+		g_main_loop_quit(slq->mds_ctx->gmainloop);
+		return;
+	}
+
+	ok = inode_map_add(slq, ino64, path);
+	if (!ok) {
+		DEBUG(1, ("inode_map_add error\n"));
+		slq->state = SLQ_STATE_ERROR;
+		g_main_loop_quit(slq->mds_ctx->gmainloop);
+		return;
+	}
+
+	slq->query_results->num_results++;
+
+done:
+	if (slq->query_results->num_results >= MAX_SL_RESULTS) {
+		slq->state = SLQ_STATE_FULL;
+		SLQ_DEBUG(10, slq, "full");
+		g_main_loop_quit(slq->mds_ctx->gmainloop);
+		return;
+	}
+
+	slq->state = SLQ_STATE_RESULTS;
+	SLQ_DEBUG(10, slq, "cursor next");
+	tracker_sparql_cursor_next_async(slq->tracker_cursor,
+					 slq->gcancellable,
+					 tracker_cursor_cb,
+					 slq);
+}
+
+static void tracker_cursor_cb_destroy_done(struct tevent_req *req)
+{
+	slq_destroy_recv(req);
+	TALLOC_FREE(req);
+
+	DEBUG(10, ("%s\n", __func__));
+}
+
+static void tracker_query_cb(GObject *object,
+			     GAsyncResult *res,
+			     gpointer user_data)
+{
+	GError *error = NULL;
+	struct sl_query *slq = talloc_get_type_abort(user_data, struct sl_query);
+
+	SLQ_DEBUG(10, slq, "tracker_query_cb");
+
+	slq->tracker_cursor = tracker_sparql_connection_query_finish(
+		TRACKER_SPARQL_CONNECTION(object),
+		res,
+		&error);
+	if (error) {
+		slq->state = SLQ_STATE_ERROR;
+		DEBUG(1, ("Tracker query error: %s\n", error->message));
+		g_error_free(error);
+		g_main_loop_quit(slq->mds_ctx->gmainloop);
+		return;
+	}
+
+	if (slq->state == SLQ_STATE_DONE) {
+		SLQ_DEBUG(10, slq, "done");
+		g_main_loop_quit(slq->mds_ctx->gmainloop);
+		talloc_free(slq);
+		return;
+	}
+
+	slq->state = SLQ_STATE_RESULTS;
+
+	tracker_sparql_cursor_next_async(slq->tracker_cursor,
+					 slq->gcancellable,
+					 tracker_cursor_cb,
+					 slq);
+}
+
+/***********************************************************
+ * Spotlight RPC functions
+ ***********************************************************/
+
+static bool slrpc_fetch_properties(struct mds_ctx *mds_ctx,
+				   const DALLOC_CTX *query, DALLOC_CTX *reply)
+{
+	sl_dict_t *dict;
+	sl_array_t *array;
+	char *s;
+	uint64_t u;
+	sl_bool_t b;
+	sl_uuid_t uuid;
+	int result;
+
+	dict = dalloc_zero(reply, sl_dict_t);
+	if (dict == NULL) {
+		return false;
+	}
+
+	/* kMDSStoreHasPersistentUUID = false */
+	result = dalloc_stradd(dict, "kMDSStoreHasPersistentUUID");
+	if (result != 0) {
+		return false;
+	}
+	b = false;
+	result = dalloc_add_copy(dict, &b, sl_bool_t);
+	if (result != 0) {
+		return false;
+	}
+
+	/* kMDSStoreIsBackup = false */
+	result = dalloc_stradd(dict, "kMDSStoreIsBackup");
+	if (result != 0) {
+		return false;
+	}
+	b = false;
+	result = dalloc_add_copy(dict, &b, sl_bool_t);
+	if (result != 0) {
+		return false;
+	}
+
+	/* kMDSStoreUUID = uuid */
+	result = dalloc_stradd(dict, "kMDSStoreUUID");
+	if (result != 0) {
+		return false;
+	}
+	memcpy(uuid.sl_uuid, "fakeuuidfakeuuid", sizeof(uuid.sl_uuid));
+	result = dalloc_add_copy(dict, &uuid, sl_uuid_t);
+	if (result != 0) {
+		return false;
+	}
+
+	/* kMDSStoreSupportsVolFS = true */
+	result = dalloc_stradd(dict, "kMDSStoreSupportsVolFS");
+	if (result != 0) {
+		return false;
+	}
+	b = true;
+	result = dalloc_add_copy(dict, &b, sl_bool_t);
+	if (result != 0) {
+		return false;
+	}
+
+	/* kMDSVolumeUUID = uuid */
+	result = dalloc_stradd(dict, "kMDSVolumeUUID");
+	if (result != 0) {
+		return false;
+	}
+	memcpy(uuid.sl_uuid, "fakeuuidfakeuuid", sizeof(uuid.sl_uuid));
+	result = dalloc_add_copy(dict, &uuid, sl_uuid_t);
+	if (result != 0) {
+		return false;
+	}
+
+	/* kMDSDiskStoreSpindleNumber = 1 (fake) */
+	result = dalloc_stradd(dict, "kMDSDiskStoreSpindleNumber");
+	if (result != 0) {
+		return false;
+	}
+	u = 1;
+	result = dalloc_add_copy(dict, &u, uint64_t);
+	if (result != 0) {
+		return false;
+	}
+
+	/* kMDSDiskStorePolicy = 3 (whatever that means, taken from OS X) */
+	result = dalloc_stradd(dict, "kMDSDiskStorePolicy");
+	if (result != 0) {
+		return false;
+	}
+	u = 3;
+	result = dalloc_add_copy(dict, &u, uint64_t);
+	if (result != 0) {
+		return false;
+	}
+
+	/* kMDSStoreMetaScopes array */
+	array = dalloc_zero(dict, sl_array_t);
+	if (array == NULL) {
+		return NULL;
+	}
+	result = dalloc_stradd(array, "kMDQueryScopeComputer");
+	if (result != 0) {
+		return false;
+	}
+	result = dalloc_stradd(array, "kMDQueryScopeAllIndexed");
+	if (result != 0) {
+		return false;
+	}
+	result = dalloc_stradd(array, "kMDQueryScopeComputerIndexed");
+	if (result != 0) {
+		return false;
+	}
+	result = dalloc_add(dict, array, sl_array_t);
+	if (result != 0) {
+		return false;
+	}
+
+	/* kMDSStoreDevice = 0x1000003 (whatever that means, taken from OS X) */
+	result = dalloc_stradd(dict, "kMDSStoreDevice");
+	if (result != 0) {
+		return false;
+	}
+	u = 0x1000003;
+	result = dalloc_add_copy(dict, &u, uint64_t);
+	if (result != 0) {
+		return false;
+	}
+
+	/* kMDSStoreSupportsTCC = true (whatever that means, taken from OS X) */
+	result = dalloc_stradd(dict, "kMDSStoreSupportsTCC");
+	if (result != 0) {
+		return false;
+	}
+	b = true;
+	result = dalloc_add_copy(dict, &b, sl_bool_t);
+	if (result != 0) {
+		return false;
+	}
+
+	/* kMDSStorePathScopes = ["/"] (whatever that means, taken from OS X) */
+	result = dalloc_stradd(dict, "kMDSStorePathScopes");
+	if (result != 0) {
+		return false;
+	}
+	array = dalloc_zero(dict, sl_array_t);
+	if (array == NULL) {
+		return false;
+	}
+	s = talloc_strdup(dict, "/");
+	if (s == NULL) {
+		return false;
+	}
+	talloc_set_name(s, "smb_ucs2_t *");
+	result = dalloc_add(array, s, smb_ucs2_t *);
+	if (result != 0) {
+		return false;
+	}
+	result = dalloc_add(dict, array, sl_array_t);
+	if (result != 0) {
+		return false;
+	}
+
+	result = dalloc_add(reply, dict, sl_dict_t);
+	if (result != 0) {
+		return false;
+	}
+
+	return true;
+}
+
+static void slq_close_timer(struct tevent_context *ev,
+			    struct tevent_timer *te,
+			    struct timeval current_time,
+			    void *private_data)
+{
+	struct sl_query *slq = talloc_get_type_abort(
+		private_data, struct sl_query);
+	struct mds_ctx *mds_ctx = slq->mds_ctx;
+
+	SLQ_DEBUG(10, slq, "expired");
+
+	TALLOC_FREE(slq);
+
+	if (CHECK_DEBUGLVL(10)) {
+		for (slq = mds_ctx->query_list; slq != NULL; slq = slq->next) {
+			SLQ_DEBUG(10, slq, "pending");
+		}
+	}
+}
+
+/**
+ * Begin a search query
+ **/
+static bool slrpc_open_query(struct mds_ctx *mds_ctx,
+			     const DALLOC_CTX *query, DALLOC_CTX *reply)
+{
+	bool ok;
+	uint64_t sl_result;
+	uint64_t *uint64p;
+	DALLOC_CTX *reqinfo;
+	sl_array_t *array, *path_scope;
+	sl_cnids_t *cnids;
+	struct sl_query *slq = NULL;
+	int result;
+	char *querystring;
+
+	array = dalloc_zero(reply, sl_array_t);
+	if (array == NULL) {
+		return false;
+	}
+
+	if (mds_ctx->tracker_con == NULL) {
+		DEBUG(1, ("no connection to Tracker\n"));
+		goto error;
+	}
+
+	/* Allocate and initialize query object */
+	slq = talloc_zero(mds_ctx, struct sl_query);
+	if (slq == NULL) {
+		return false;
+	}
+	slq->entries_ctx = talloc_named_const(slq, 0, "struct sl_query.entries_ctx");
+	if (slq->entries_ctx == NULL) {
+		TALLOC_FREE(slq);
+		return false;
+	}
+	talloc_set_destructor(slq, slq_destructor_cb);
+	slq->state = SLQ_STATE_NEW;
+	slq->mds_ctx = mds_ctx;
+
+	slq->last_used = timeval_current();
+	slq->start_time = slq->last_used;
+	slq->expire_time = timeval_add(&slq->last_used, MAX_SL_RUNTIME, 0);
+	slq->te = tevent_add_timer(server_event_context(), slq,
+				   slq->expire_time, slq_close_timer, slq);
+	if (slq->te == NULL) {
+		DEBUG(1, ("tevent_add_timer failed\n"));
+		goto error;
+	}
+
+	slq->gcancellable = g_cancellable_new();
+	if (slq->gcancellable == NULL) {
+		DEBUG(1,("error from g_cancellable_new\n"));
+		goto error;
+	}
+
+	querystring = dalloc_value_for_key(query, "DALLOC_CTX", 0,
+					   "DALLOC_CTX", 1,
+					   "kMDQueryString");
+	if (querystring == NULL) {
+		DEBUG(1, ("missing kMDQueryString\n"));
+		goto error;
+	}
+	slq->query_string = talloc_strdup(slq, querystring);
+	if (slq->query_string == NULL) {
+		DEBUG(1, ("out of memory\n"));
+		goto error;
+	}
+
+	/*
+	 * FIXME: convert spotlight query charset from decomposed UTF8
+	 * to host charset precomposed UTF8.
+	 */
+
+	uint64p = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0,
+			     "uint64_t", 1);
+	if (uint64p == NULL) {
+		goto error;
+	}
+	slq->ctx1 = *uint64p;
+	uint64p = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0,
+			     "uint64_t", 2);
+	if (uint64p == NULL) {
+		goto error;
+	}
+	slq->ctx2 = *uint64p;
+
+	path_scope = dalloc_value_for_key(query, "DALLOC_CTX", 0,
+					  "DALLOC_CTX", 1, "kMDScopeArray");
+	if (path_scope == NULL) {
+		goto error;
+	}
+
+	slq->path_scope = dalloc_get(path_scope, "char *", 0);
+	if (slq->path_scope == NULL) {
+		goto error;
+	}
+
+	slq->path_scope = talloc_strdup(slq, slq->path_scope);
+	if (slq->path_scope == NULL) {
+		goto error;
+	}
+
+
+	reqinfo = dalloc_value_for_key(query, "DALLOC_CTX", 0,
+				       "DALLOC_CTX", 1, "kMDAttributeArray");
+	if (reqinfo == NULL) {
+		goto error;
+	}
+
+	slq->reqinfo = talloc_steal(slq, reqinfo);
+	DEBUG(10, ("requested attributes: %s", mds_dalloc_dump(reqinfo, 0)));
+
+	cnids = dalloc_value_for_key(query, "DALLOC_CTX", 0,
+				     "DALLOC_CTX", 1, "kMDQueryItemArray");
+	if (cnids) {
+		ok = sort_cnids(slq, cnids->ca_cnids);
+		if (!ok) {
+			goto error;
+		}
+	}
+
+	ok = create_result_handle(slq);
+	if (!ok) {
+		DEBUG(1, ("create_result_handle error\n"));
+		slq->state = SLQ_STATE_ERROR;
+		goto error;
+	}
+
+	SLQ_DEBUG(10, slq, "new");
+
+	DLIST_ADD(mds_ctx->query_list, slq);
+
+	/*
+	 * TODO: call the SPARQL translator here...
+	 */
+	goto error;
+
+	DEBUG(10, ("SPARQL query: \"%s\"\n", sparql_query));
+
+	g_main_context_push_thread_default(mds_ctx->gcontext);
+	tracker_sparql_connection_query_async(mds_ctx->tracker_con,
+					      sparql_query,
+					      slq->gcancellable,
+					      tracker_query_cb,
+					      slq);
+	g_main_context_pop_thread_default(mds_ctx->gcontext);
+	slq->state = SLQ_STATE_RUNNING;
+
+	sl_result = 0;
+	result = dalloc_add_copy(array, &sl_result, uint64_t);
+	if (result != 0) {
+		goto error;
+	}
+	result = dalloc_add(reply, array, sl_array_t);
+	if (result != 0) {
+		goto error;
+	}
+	return true;
+
+error:
+	sl_result = UINT64_MAX;
+	TALLOC_FREE(slq);
+	result = dalloc_add_copy(array, &sl_result, uint64_t);
+	if (result != 0) {
+		return false;
+	}
+	result = dalloc_add(reply, array, sl_array_t);
+	if (result != 0) {
+		return false;
+	}
+	return true;
+}
+
+/**
+ * Fetch results of a query
+ **/
+static bool slrpc_fetch_query_results(struct mds_ctx *mds_ctx,
+				      const DALLOC_CTX *query,
+				      DALLOC_CTX *reply)
+{
+	bool ok;
+	struct sl_query *slq = NULL;
+	uint64_t *uint64p, ctx1, ctx2;
+	uint64_t status;
+	sl_array_t *array;
+	int result;
+
+	array = dalloc_zero(reply, sl_array_t);
+	if (array == NULL) {
+		return false;
+	}
+
+	/* Get query for context */
+	uint64p = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0,
+			     "uint64_t", 1);
+	if (uint64p == NULL) {
+		goto error;
+	}
+	ctx1 = *uint64p;
+
+	uint64p = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0,
+			     "uint64_t", 2);
+	if (uint64p == NULL) {
+		goto error;
+	}
+	ctx2 = *uint64p;
+
+	slq = slq_for_ctx(mds_ctx, ctx1, ctx2);
+	if (slq == NULL) {
+		DEBUG(1, ("bad context: [0x%jx,0x%jx]\n",
+			  (uintmax_t)ctx1, (uintmax_t)ctx2));
+		goto error;
+	}
+
+	TALLOC_FREE(slq->te);
+	slq->last_used = timeval_current();
+	slq->expire_time = timeval_add(&slq->last_used, MAX_SL_RUNTIME, 0);
+	slq->te = tevent_add_timer(server_event_context(), slq,
+				   slq->expire_time, slq_close_timer, slq);
+	if (slq->te == NULL) {
+		DEBUG(1, ("tevent_add_timer failed\n"));
+		goto error;
+	}
+
+	SLQ_DEBUG(10, slq, "fetch");
+
+	switch (slq->state) {
+	case SLQ_STATE_RUNNING:
+	case SLQ_STATE_RESULTS:
+	case SLQ_STATE_FULL:
+	case SLQ_STATE_DONE:
+		ok = add_results(array, slq);
+		if (!ok) {
+			DEBUG(1, ("error adding results\n"));
+			goto error;
+		}
+		if (slq->state == SLQ_STATE_FULL) {
+			slq->state = SLQ_STATE_RESULTS;
+			g_main_context_push_thread_default(mds_ctx->gcontext);
+			tracker_sparql_cursor_next_async(
+				slq->tracker_cursor,
+				slq->gcancellable,
+				tracker_cursor_cb,
+				slq);
+			g_main_context_pop_thread_default(mds_ctx->gcontext);
+		}
+		break;
+
+	case SLQ_STATE_ERROR:
+		DEBUG(1, ("query in error state\n"));
+		goto error;
+
+	default:
+		DEBUG(1, ("unexpected query state %d\n", slq->state));
+		goto error;
+	}
+
+	result = dalloc_add(reply, array, sl_array_t);
+	if (result != 0) {
+		goto error;
+	}
+	return true;
+
+error:
+	status = UINT64_MAX;
+	TALLOC_FREE(slq);
+	result = dalloc_add_copy(array, &status, uint64_t);
+	if (result != 0) {
+		return false;
+	}
+	result = dalloc_add(reply, array, sl_array_t);
+	if (result != 0) {
+		return false;
+	}
+	return true;
+}
+
+/**
+ * Store metadata attributes for a CNID
+ **/
+static bool slrpc_store_attributes(struct mds_ctx *mds_ctx,
+				   const DALLOC_CTX *query, DALLOC_CTX *reply)
+{
+	uint64_t sl_result;
+	sl_array_t *array;
+	int result;
+
+	array = dalloc_zero(reply, sl_array_t);
+	if (array == NULL) {
+		return false;
+	}
+
+	/*
+	 * FIXME: not implemented. Used by the client for eg setting
+	 * the modification date of the shared directory which clients
+	 * poll indicating changes on the share and cause the client
+	 * to refresh view.
+	 */
+
+	sl_result = 0;
+	result = dalloc_add_copy(array, &sl_result, uint64_t);
+	if (result != 0) {
+		return false;
+	}
+	result = dalloc_add(reply, array, sl_array_t);
+	if (result != 0) {
+		return false;
+	}
+
+	return true;
+}
+
+/**
+ * Fetch supported metadata attributes for a CNID
+ **/
+static bool slrpc_fetch_attributenames(struct mds_ctx *mds_ctx,
+				       const DALLOC_CTX *query,
+				       DALLOC_CTX *reply)
+{
+	uint64_t id;
+	sl_cnids_t *cnids;
+	sl_array_t *array;
+	uint64_t sl_result;
+	sl_cnids_t *replycnids;
+	sl_array_t *mdattrs;
+	sl_filemeta_t *fmeta;
+	int result;
+	void *p;
+
+	cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 1);
+	if (cnids == NULL) {
+		return false;
+	}
+
+	p = dalloc_get_object(cnids->ca_cnids, 0);
+	if (p == NULL) {
+		return NULL;
+	}
+	memcpy(&id, p, sizeof(uint64_t));
+
+	/* Result array */
+	array = dalloc_zero(reply, sl_array_t);
+	if (array == NULL) {
+		return false;
+	}
+
+	result = dalloc_add(reply, array, sl_array_t);
+	if (result != 0) {
+		return false;
+	}
+
+	/* Return result value 0 */
+	sl_result = 0;
+	result = dalloc_add_copy(array, &sl_result, uint64_t);
+	if (result != 0) {
+		return false;
+	}
+
+	/* Return CNID array */
+	replycnids = talloc_zero(reply, sl_cnids_t);
+	if (replycnids == NULL) {
+		return false;
+	}
+
+	replycnids->ca_cnids = dalloc_new(cnids);
+	if (replycnids->ca_cnids == NULL) {
+		return false;
+	}
+
+	replycnids->ca_unkn1 = 0xfec;
+	replycnids->ca_context = cnids->ca_context;
+	result = dalloc_add_copy(replycnids->ca_cnids, &id, uint64_t);
+	if (result != 0) {
+		return false;
+	}
+	result = dalloc_add(array, replycnids, sl_cnids_t);
+	if (result != 0) {
+		return false;
+	}
+
+	/*
+	 * FIXME: this should return the real attributes from all
+	 * known metadata sources (Tracker and filesystem)
+	 */
+	mdattrs = dalloc_zero(reply, sl_array_t);
+	if (mdattrs == NULL) {
+		return false;
+	}
+
+	result = dalloc_stradd(mdattrs, "kMDItemFSName");
+	if (result != 0) {
+		return false;
+	}
+	result = dalloc_stradd(mdattrs, "kMDItemDisplayName");
+	if (result != 0) {
+		return false;
+	}
+	result = dalloc_stradd(mdattrs, "kMDItemFSSize");
+	if (result != 0) {
+		return false;
+	}
+	result = dalloc_stradd(mdattrs, "kMDItemFSOwnerUserID");
+	if (result != 0) {
+		return false;
+	}
+	result = dalloc_stradd(mdattrs, "kMDItemFSOwnerGroupID");
+	if (result != 0) {
+		return false;
+	}
+	result = dalloc_stradd(mdattrs, "kMDItemFSContentChangeDate");
+	if (result != 0) {
+		return false;
+	}
+
+	fmeta = dalloc_zero(reply, sl_filemeta_t);
+	if (fmeta == NULL) {
+		return false;
+	}
+	result = dalloc_add(fmeta, mdattrs, sl_array_t);
+	if (result != 0) {
+		return false;
+	}
+	result = dalloc_add(array, fmeta, sl_filemeta_t);
+	if (result != 0) {
+		return false;
+	}
+
+	return true;
+}
+
+/**
+ * Fetch metadata attribute values for a CNID
+ **/
+static bool slrpc_fetch_attributes(struct mds_ctx *mds_ctx,
+				   const DALLOC_CTX *query, DALLOC_CTX *reply)
+{
+	int result;
+	bool ok;
+	sl_array_t *array;
+	sl_cnids_t *cnids;
+	sl_cnids_t *replycnids;
+	sl_array_t *reqinfo;
+	uint64_t ino;
+	uint64_t sl_result;
+	sl_filemeta_t *fm;
+	sl_array_t *fm_array;
+	sl_nil_t nil;
+	struct stat_ex sb;
+	struct sl_inode_path_map *elem = NULL;
+	void *p;
+	TDB_DATA val = tdb_null;
+	NTSTATUS status;
+
+	array = dalloc_zero(reply, sl_array_t);
+	if (array == NULL) {
+		return false;
+	}
+	replycnids = talloc_zero(reply, sl_cnids_t);
+	if (replycnids == NULL) {
+		goto error;
+	}
+	replycnids->ca_cnids = dalloc_new(replycnids);
+	if (replycnids->ca_cnids == NULL) {
+		goto error;
+	}
+	fm = dalloc_zero(array, sl_filemeta_t);
+	if (fm == NULL) {
+		goto error;
+	}
+	fm_array = dalloc_zero(fm, sl_array_t);
+	if (fm_array == NULL) {
+		goto error;
+	}
+	/* For some reason the list of results always starts with a nil entry */
+	dalloc_add_copy(fm_array, &nil, sl_nil_t);
+
+	reqinfo = dalloc_get(query, "DALLOC_CTX", 0, "sl_array_t", 1);
+	if (reqinfo == NULL) {
+		goto error;
+	}
+
+	cnids = dalloc_get(query, "DALLOC_CTX", 0, "sl_cnids_t", 2);
+	if (cnids == NULL) {
+		goto error;
+	}
+	p = dalloc_get_object(cnids->ca_cnids, 0);
+	if (p == NULL) {
+		goto error;
+	}
+	memcpy(&ino, p, sizeof(uint64_t));
+
+	replycnids->ca_unkn1 = 0xfec;
+	replycnids->ca_context = cnids->ca_context;
+	result = dalloc_add_copy(replycnids->ca_cnids, &ino, uint64_t);
+	if (result != 0) {
+		goto error;
+	}
+
+	status = dbwrap_fetch(mds_ctx->ino_path_map, reply,
+			      make_tdb_data((void*)&ino, sizeof(uint64_t)),
+			      &val);
+	if (!NT_STATUS_IS_OK(status)) {
+		DEBUG(1, ("Failed to fetch inode: %s\n", nt_errstr(status)));
+		goto error;
+	}
+	if (val.dsize != sizeof(p)) {
+		DEBUG(1, ("invalid record pointer size: %zd\n", val.dsize));
+		TALLOC_FREE(val.dptr);
+		goto error;
+	}
+
+	memcpy(&p, val.dptr, sizeof(p));
+	elem = talloc_get_type_abort(p, struct sl_inode_path_map);
+
+	result = sys_stat(elem->path, &sb, false);
+	if (result != 0) {
+		goto error;
+	}
+
+	ok = add_filemeta(reqinfo, fm_array,
+			  elem ? elem->path : NULL,
+			  elem ? &sb : NULL);
+	if (!ok) {
+		goto error;
+	}
+
+	sl_result = 0;
+	result = dalloc_add_copy(array, &sl_result, uint64_t);
+	if (result != 0) {
+		goto error;
+	}
+	result = dalloc_add(array, replycnids, sl_cnids_t);
+	if (result != 0) {
+		goto error;
+	}
+	result = dalloc_add(fm, fm_array, sl_array_t);
+	if (result != 0) {
+		goto error;
+	}
+	result = dalloc_add(array, fm, sl_filemeta_t);
+	if (result != 0) {
+		goto error;
+	}
+	result = dalloc_add(reply, array, sl_array_t);
+	if (result != 0) {
+		goto error;
+	}
+
+	return true;
+
+error:
+	sl_result = UINT64_MAX;
+	result = dalloc_add_copy(array, &sl_result, uint64_t);
+	if (result != 0) {
+		return false;
+	}
+	result = dalloc_add(reply, array, sl_array_t);
+	if (result != 0) {
+		return false;
+	}
+
+	return true;
+}
+
+/**
+ * Close a query
+ **/
+static bool slrpc_close_query(struct mds_ctx *mds_ctx,
+			      const DALLOC_CTX *query, DALLOC_CTX *reply)
+{
+	struct sl_query *slq = NULL;
+	uint64_t *uint64p, ctx1, ctx2;
+	sl_array_t *array;
+	uint64_t sl_res;
+	int result;
+
+	array = dalloc_zero(reply, sl_array_t);
+	if (array == NULL) {
+		return false;
+	}
+
+	/* Context */
+	uint64p = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0,
+			     "uint64_t", 1);
+	if (uint64p == NULL) {
+		goto done;
+	}
+	ctx1 = *uint64p;
+
+	uint64p = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0,
+			     "uint64_t", 2);
+	if (uint64p == NULL) {
+		goto done;
+	}
+	ctx2 = *uint64p;
+
+	/* Get query for context and free it */
+	slq = slq_for_ctx(mds_ctx, ctx1, ctx2);
+	if (slq == NULL) {
+		DEBUG(1, ("bad context: [0x%jx,0x%jx]\n",
+			  (uintmax_t)ctx1, (uintmax_t)ctx2));
+		goto done;
+	}
+
+	switch (slq->state) {
+	case SLQ_STATE_RUNNING:
+	case SLQ_STATE_RESULTS:
+		DEBUG(10, ("close: requesting query close\n"));
+		/*
+		 * Mark the query is done so the cursor callback can
+		 * act accordingly by stopping to request more results
+		 * and sheduling query resource deallocation via
+		 * tevent.
+		 */
+		slq->state = SLQ_STATE_DONE;
+		break;
+
+	case SLQ_STATE_FULL:
+	case SLQ_STATE_DONE:
+		DEBUG(10, ("close: query was done or result queue was full\n"));
+		/*
+		 * We can directly deallocate the query because there
+		 * are no pending Tracker async calls in flight in
+		 * these query states.
+		 */
+		TALLOC_FREE(slq);
+		break;
+
+	default:
+		DEBUG(1, ("close: unexpected state: %d\n", slq->state));
+		break;
+	}
+
+
+done:
+	sl_res = 0;
+	result = dalloc_add_copy(array, &sl_res, uint64_t);
+	if (result != 0) {
+		return false;
+	}
+	result = dalloc_add(reply, array, sl_array_t);
+	if (result != 0) {
+		return false;
+	}
+	return true;
+}
+
+/**
+ * Init callbacks at startup, nothing to do here really
+ **/
+bool mds_init(struct messaging_context *msg_ctx)
+{
+	return true;
+}
+
+bool mds_shutdown(void)
+{
+	return true;
+}
+
+static gboolean gmainloop_timer(gpointer user_data)
+{
+	struct mds_ctx *ctx = talloc_get_type_abort(user_data, struct mds_ctx);
+
+	DEBUG(10,("%s\n", __func__));
+	g_main_loop_quit(ctx->gmainloop);
+
+	return G_SOURCE_CONTINUE;
+}
+
+/**
+ * Initialise a context per share handle
+ **/
+struct mds_ctx *mds_init_ctx(TALLOC_CTX *mem_ctx,
+			     const struct auth_session_info *session_info,
+			     const char *path)
+{
+	struct mds_ctx *mds_ctx;
+
+	mds_ctx = talloc_zero(mem_ctx, struct mds_ctx);
+	if (mds_ctx == NULL) {
+		return NULL;
+	}
+	talloc_set_destructor(mds_ctx, mds_ctx_destructor_cb);
+
+	mds_ctx->spath = talloc_strdup(mds_ctx, path);
+	if (mds_ctx->spath == NULL) {
+		goto error;
+	}
+
+	if (session_info->security_token->num_sids < 1) {
+		goto error;
+	}
+	sid_copy(&mds_ctx->sid, &session_info->security_token->sids[0]);
+	mds_ctx->uid = session_info->unix_token->uid;
+
+	mds_ctx->ino_path_map = db_open_rbt(mds_ctx);
+	if (mds_ctx->ino_path_map == NULL) {
+		DEBUG(1,("open inode map db failed\n"));
+		goto error;
+	}
+
+	mds_ctx->gcontext = g_main_context_new();
+	if (mds_ctx->gcontext == NULL) {
+		DEBUG(1,("error from g_main_context_new\n"));
+		goto error;
+	}
+
+	mds_ctx->gmainloop = g_main_loop_new(mds_ctx->gcontext, false);
+	if (mds_ctx->gmainloop == NULL) {
+		DEBUG(1,("error from g_main_loop_new\n"));
+		goto error;
+	}
+
+	g_main_context_push_thread_default(mds_ctx->gcontext);
+	tracker_sparql_connection_get_async(mds_ctx->gcancellable,
+					    tracker_con_cb, mds_ctx);
+	g_main_context_pop_thread_default(mds_ctx->gcontext);
+
+	return mds_ctx;
+
+error:
+	TALLOC_FREE(mds_ctx);
+	return NULL;
+}
+
+/**
+ * Tear down connections and free all resources
+ **/
+int mds_ctx_destructor_cb(struct mds_ctx *mds_ctx)
+{
+	/*
+	 * We need to free query_list before ino_path_map
+	 */
+	while (mds_ctx->query_list != NULL) {
+		/*
+		 * slq destructor removes element from list.
+		 * Don't use TALLOC_FREE()!
+		 */
+		talloc_free(mds_ctx->query_list);
+	}
+	TALLOC_FREE(mds_ctx->ino_path_map);
+
+	if (mds_ctx->tracker_con != NULL) {
+		g_object_unref(mds_ctx->tracker_con);
+	}
+	if (mds_ctx->gcancellable != NULL) {
+		g_cancellable_cancel(mds_ctx->gcancellable);
+		g_object_unref(mds_ctx->gcancellable);
+	}
+	if (mds_ctx->gmainloop != NULL) {
+		g_main_loop_unref(mds_ctx->gmainloop);
+	}
+	if (mds_ctx->gcontext != NULL) {
+		g_main_context_unref(mds_ctx->gcontext);
+	}
+
+	ZERO_STRUCTP(mds_ctx);
+
+	return 0;
+}
+
+static bool mds_run_gmainloop(struct mds_ctx *mds_ctx, guint timeout)
+{
+	guint timer_id;
+	GSource *timer;
+
+	/*
+	 * It seems the event processing of the libtracker-sparql
+	 * async subsystem defers callbacks until *all* events are
+	 * processes by the async subsystem main processing loop.
+	 *
+	 * g_main_context_iteration(may_block=FALSE) can't be used,
+	 * because a search that produces a few thousand matches
+	 * generates as many events that must be processed in either
+	 * g_main_context_iteration() or g_main_loop_run() before
+	 * callbacks are called.
+	 *
+	 * Unfortunately g_main_context_iteration() only processes a
+	 * small subset of these event (1-30) at a time when run in
+	 * mds_dispatch(), which happens once a second while the
+	 * client polls for results.
+	 *
+	 * Carefully using the blocking g_main_loop_run() fixes
+	 * this. It processes events until we exit from the loop at
+	 * defined exit points. By adding a 1 ms timeout we at least
+	 * try to get as close as possible to non-blocking behaviour.
+	 */
+
+	if (!g_main_context_pending(mds_ctx->gcontext)) {
+		return true;
+	}
+
+	g_main_context_push_thread_default(mds_ctx->gcontext);
+
+	timer = g_timeout_source_new(timeout);
+	if (timer == NULL) {
+		DEBUG(1,("g_timeout_source_new_seconds\n"));
+		g_main_context_pop_thread_default(mds_ctx->gcontext);
+		return false;
+	}
+
+	timer_id = g_source_attach(timer, mds_ctx->gcontext);
+	if (timer_id == 0) {
+		DEBUG(1,("g_timeout_add failed\n"));
+		g_source_destroy(timer);
+		g_main_context_pop_thread_default(mds_ctx->gcontext);
+		return false;
+	}
+
+	g_source_set_callback(timer, gmainloop_timer, mds_ctx, NULL);
+
+	g_main_loop_run(mds_ctx->gmainloop);
+
+	g_source_destroy(timer);
+
+	g_main_context_pop_thread_default(mds_ctx->gcontext);
+	return true;
+}
+
+/**
+ * Dispatch a Spotlight RPC command
+ **/
+bool mds_dispatch(struct mds_ctx *mds_ctx,
+		  struct mdssvc_blob *request_blob,
+		  struct mdssvc_blob *response_blob)
+{
+	bool ok;
+	ssize_t len;
+	DALLOC_CTX *query = NULL;
+	DALLOC_CTX *reply = NULL;
+	char *rpccmd;
+	const struct slrpc_cmd *slcmd;
+
+	if (CHECK_DEBUGLVL(10)) {
+		const struct sl_query *slq;
+
+		for (slq = mds_ctx->query_list; slq != NULL; slq = slq->next) {
+			SLQ_DEBUG(10, slq, "pending");
+		}
+	}
+
+	response_blob->length = 0;
+
+	/*
+	 * Process finished glib events.
+	 *
+	 * FIXME: integrate with tevent instead of piggy packing it
+	 * onto the processing of new requests.
+	 *
+	 * mds_dispatch() is called by the client a few times in a row:
+	 *
+	 * - first in order to open/start a search query
+	 *
+	 * - later in order to fetch results asynchronously, typically
+	 *   once a second. If no results have been retrieved from the
+	 *   search store (Tracker) yet, we return no results.
+	 *   The client asks for more results every second as long
+	 *   as the "Search Window" in the client gui is open.
+	 *
+	 * - at some point the query is closed
+	 *
+	 * This means we try to iterate through the glib event loop
+	 * before processing the request in order to get result
+	 * from tracker which can be returned to the client.
+	 */
+
+	ok = mds_run_gmainloop(mds_ctx, MDS_TRACKER_ASYNC_TIMEOUT_MS);
+	if (!ok) {
+		goto cleanup;
+	}
+
+	DEBUG(10, ("share path: %s\n", mds_ctx->spath));
+
+	query = dalloc_new(mds_ctx);
+	if (query == NULL) {
+		ok = false;
+		goto cleanup;
+	}
+	reply = dalloc_new(mds_ctx);
+	if (reply == NULL) {
+		ok = false;
+		goto cleanup;
+	}
+
+	ok = sl_unpack(query, (char *)request_blob->spotlight_blob,
+		       request_blob->length);
+	if (!ok) {
+		DEBUG(1, ("error unpacking Spotlight RPC blob\n"));
+		goto cleanup;
+	}
+
+	DEBUG(5, ("%s", mds_dalloc_dump(query, 0)));
+
+	rpccmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0,
+			    "char *", 0);
+	if (rpccmd == NULL) {
+		DEBUG(1, ("missing primary Spotlight RPC command\n"));
+		ok = false;
+		goto cleanup;
+	}
+
+	DEBUG(10, ("Spotlight RPC cmd: %s\n", rpccmd));
+
+	slcmd = slrpc_cmd_by_name(rpccmd);
+	if (slcmd == NULL) {
+		DEBUG(1, ("unsupported primary Spotlight RPC command %s\n",
+			  rpccmd));
+		ok = false;
+		goto cleanup;
+	}
+
+	/*
+	 * If these functions return an error, they hit something like
+	 * a non recoverable talloc error
+	 */
+	ok = slcmd->function(mds_ctx, query, reply);
+	if (!ok) {
+		DEBUG(1, ("error in Spotlight RPC handler\n"));
+		goto cleanup;
+	}
+
+	DEBUG(5, ("%s", mds_dalloc_dump(reply, 0)));
+
+	len = sl_pack(reply, (char *)response_blob->spotlight_blob,
+		      response_blob->size);
+	if (len == -1) {
+		DEBUG(1, ("error packing Spotlight RPC reply\n"));
+		ok = false;
+		goto cleanup;
+	}
+
+	/*
+	 * Run g_main_loop a second time in order to dispatch events
+	 * that may have been queued at the libtracker-sparql level.
+	 * As we only want to dispatch (write out requests) but not
+	 * wait for anything, we use a much shorter timeout here.
+	 */
+	ok = mds_run_gmainloop(mds_ctx, MDS_TRACKER_ASYNC_TIMEOUT_MS / 10);
+	if (!ok) {
+		goto cleanup;
+	}
+
+	response_blob->length = len;
+
+cleanup:
+	talloc_free(query);
+	talloc_free(reply);
+	return ok;
+}
diff --git a/source3/rpc_server/mdssvc/mdssvc.h b/source3/rpc_server/mdssvc/mdssvc.h
index db17b3c..0ad1e48 100644
--- a/source3/rpc_server/mdssvc/mdssvc.h
+++ b/source3/rpc_server/mdssvc/mdssvc.h
@@ -23,6 +23,85 @@
 
 #include "dalloc.h"
 #include "marshalling.h"
+#include "lib/util/dlinklist.h"
+#include "librpc/gen_ndr/mdssvc.h"
+
+/*
+ * glib uses TRUE and FALSE which was redefined by "includes.h" to be
+ * unusable, undefine so glib can establish its own working
+ * replacement.
+ */
+#undef TRUE
+#undef FALSE
+#include <gio/gio.h>
+#include <tracker-sparql.h>
+
+#define MAX_SL_FRAGMENT_SIZE 0xFFFFF
+#define MAX_SL_RESULTS 100
+#define MAX_SL_RUNTIME 30
+#define MDS_TRACKER_ASYNC_TIMEOUT_MS 250
+
+/******************************************************************************
+ * Some helper stuff dealing with queries
+ ******************************************************************************/
+
+/* query state */
+typedef enum {
+	SLQ_STATE_NEW,       /* Query received from client         */
+	SLQ_STATE_RUNNING,   /* Query dispatched to Tracker        */
+	SLQ_STATE_RESULTS,   /* Async Tracker query read           */
+	SLQ_STATE_FULL,	     /* the max amount of result has beed queued */
+	SLQ_STATE_DONE,      /* Got all results from Tracker       */
+	SLQ_STATE_END,       /* Query results returned to client   */
+	SLQ_STATE_ERROR	     /* an error happended somewhere       */
+} slq_state_t;
+
+/* query structure */
+struct sl_query {
+	struct sl_query *prev, *next;	 /* list pointers */
+	struct mds_ctx  *mds_ctx;        /* context handle */
+	slq_state_t      state;          /* query state */
+	struct timeval   start_time;	 /* Query start time */
+	struct timeval   last_used;	 /* Time of last result fetch */
+	struct timeval   expire_time;	 /* Query expiration time */
+	struct tevent_timer *te;	 /* query timeout */
+	int              snum;           /* share snum  */
+	uint64_t         ctx1;           /* client context 1 */
+	uint64_t         ctx2;           /* client context 2 */
+	sl_array_t      *reqinfo;        /* array with requested metadata */
+	const char      *query_string;   /* the Spotlight query string */
+	uint64_t        *cnids;          /* restrict query to these CNIDs */
+	size_t           cnids_num;      /* Size of slq_cnids array */
+	const char      *path_scope;	 /* path to directory to search */
+	GCancellable    *gcancellable;
+	TrackerSparqlCursor *tracker_cursor; /* Tracker SPARQL query result cursor */
+	struct sl_rslts *query_results;  /* query results */
+	TALLOC_CTX      *entries_ctx;    /* talloc parent of the search results */
+};
+
+struct sl_rslts {
+	int                num_results;
+	sl_cnids_t        *cnids;
+	sl_array_t        *fm_array;
+};
+
+struct sl_inode_path_map {
+	struct mds_ctx    *mds_ctx;
+	uint64_t           ino;
+	char              *path;
+};
+
+struct mds_ctx {
+	struct dom_sid sid;
+	uid_t uid;
+	const char *spath;
+	GCancellable *gcancellable;
+	TrackerSparqlConnection *tracker_con;
+	GMainContext *gcontext;
+	GMainLoop *gmainloop;
+	struct sl_query *query_list;     /* list of active queries */
+	struct db_context *ino_path_map; /* dbwrap rbt for storing inode->path mappings */
+};
 
 /******************************************************************************
  * Function declarations
@@ -31,6 +110,15 @@
 /*
  * mdssvc.c
  */
-int mds_dispatch(void);
+extern bool mds_init(struct messaging_context *msg_ctx);
+extern bool mds_shutdown(void);
+extern struct mds_ctx *mds_init_ctx(TALLOC_CTX *mem_ctx,
+				    const struct auth_session_info *session_info,
+				    const char *path);
+extern int mds_ctx_destructor_cb(struct mds_ctx *mds_ctx);
+extern bool mds_dispatch(struct mds_ctx *query_ctx,
+			 struct mdssvc_blob *request_blob,
+			 struct mdssvc_blob *response_blob);
+extern char *mds_dalloc_dump(DALLOC_CTX *dd, int nestinglevel);
 
 #endif /* _MDSSVC_H */
diff --git a/source3/rpc_server/mdssvc/srv_mdssvc_nt.c b/source3/rpc_server/mdssvc/srv_mdssvc_nt.c
index 3779e45..cb0d759 100644
--- a/source3/rpc_server/mdssvc/srv_mdssvc_nt.c
+++ b/source3/rpc_server/mdssvc/srv_mdssvc_nt.c
@@ -18,40 +18,196 @@
  */
 
 #include "includes.h"
-#include "mdssvc.h"
 #include "ntdomain.h"
+#include "rpc_server/mdssvc/srv_mdssvc_nt.h"
 #include "../librpc/gen_ndr/srv_mdssvc.h"
+#include "libcli/security/security_token.h"
+#include "gen_ndr/auth.h"
+#include "mdssvc.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_RPC_SRV
 
+bool init_service_mdssvc(struct messaging_context *msg_ctx)
+{
+	return mds_init(msg_ctx);
+}
+
+bool shutdown_service_mdssvc(void)
+{
+	return mds_shutdown();
+}
+
+static NTSTATUS create_mdssvc_policy_handle(TALLOC_CTX *mem_ctx,
+					    struct pipes_struct *p,
+					    const char *path,
+					    struct policy_handle *handle)
+{
+	struct mds_ctx *mds_ctx;
+
+	ZERO_STRUCTP(handle);
+
+	mds_ctx = mds_init_ctx(mem_ctx, p->session_info, path);
+	if (mds_ctx == NULL) {
+		DEBUG(1, ("error in mds_init_ctx for: %s\n", path));
+		return NT_STATUS_UNSUCCESSFUL;
+	}
+
+	if (!create_policy_hnd(p, handle, mds_ctx)) {
+		talloc_free(mds_ctx);
+		ZERO_STRUCTP(handle);
+		return NT_STATUS_NO_MEMORY;
+	}
+
+	return NT_STATUS_OK;
+}
+
 void _mdssvc_open(struct pipes_struct *p, struct mdssvc_open *r)
 {
-	DEBUG(10, ("%s\n", __func__));
-	p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
+	int snum;
+	char *path;
+	NTSTATUS status;
+
+	DEBUG(10, ("%s: [%s]\n", __func__, r->in.share_name));
+
+	snum = lp_servicenumber(r->in.share_name);
+	if (!VALID_SNUM(snum)) {
+		p->fault_state = DCERPC_FAULT_CANT_PERFORM;
+		return;
+	}
+
+	if (lp_spotlight(snum)) {
+		DEBUG(10, ("Spotlight enabled: %s\n", r->in.share_name));
+
+		path = lp_path(talloc_tos(), snum);
+		if (path == NULL) {
+			DEBUG(1, ("Couldn't create policy handle for %s\n",
+				  r->in.share_name));
+			p->fault_state = DCERPC_FAULT_CANT_PERFORM;
+			return;
+		}
+
+		status = create_mdssvc_policy_handle(p->mem_ctx, p, path,
+						     r->out.handle);
+		if (!NT_STATUS_IS_OK(status)) {
+			DEBUG(1, ("Couldn't create policy handle for %s\n",
+				  r->in.share_name));
+			talloc_free(path);
+			p->fault_state = DCERPC_FAULT_CANT_PERFORM;
+			return;
+		}
+
+		strlcpy(discard_const_p(char, r->out.share_path), path, 1024);
+		talloc_free(path);
+		*r->out.device_id = *r->in.device_id;
+	}
+
+	*r->out.unkn2 = 0x17;
+	*r->out.unkn3 = 0;
+
 	return;
 }
 
 void _mdssvc_unknown1(struct pipes_struct *p, struct mdssvc_unknown1 *r)
 {
-	DEBUG(10, ("%s\n", __func__));
-	p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
+	struct mds_ctx *mds_ctx;
+
+	if (!find_policy_by_hnd(p, &r->in.handle, (void **)(void *)&mds_ctx)) {
+		DEBUG(1, ("%s: invalid handle\n", __func__));
+		return;
+	}
+
+	DEBUG(10, ("%s: path: %s\n", __func__, mds_ctx->spath));
+
+	*r->out.status = 0;
+	*r->out.flags = 0x6b000001;
+	*r->out.unkn7 = 0;
+
 	return;
 }
 
 void _mdssvc_cmd(struct pipes_struct *p, struct mdssvc_cmd *r)
 {
-	DEBUG(10, ("%s\n", __func__));
-	p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
+	bool ok;
+	char *rbuf;
+	struct mds_ctx *mds_ctx;
+
+	if (!find_policy_by_hnd(p, &r->in.handle, (void **)(void *)&mds_ctx)) {
+		DEBUG(1, ("%s: invalid handle\n", __func__));
+		return;
+	}
+
+	DEBUG(10, ("%s: path: %s\n", __func__, mds_ctx->spath));
+
+	ok = security_token_is_sid(p->session_info->security_token,
+				   &mds_ctx->sid);
+	if (!ok) {
+		DEBUG(1,("%s: not the same sid: %s\n", __func__,
+			 sid_string_tos(&mds_ctx->sid)));
+		p->fault_state = DCERPC_FAULT_ACCESS_DENIED;
+		return;
+	}
 
-	mds_dispatch();
+	if (geteuid() != mds_ctx->uid) {
+		DEBUG(0, ("uid mismatch: %d/%d\n", geteuid(), mds_ctx->uid));
+		smb_panic("uid mismatch");
+	}
+
+	if (r->in.request_blob.size > MAX_SL_FRAGMENT_SIZE) {
+		DEBUG(1, ("%s: request size too large\n", __func__));
+		p->fault_state = DCERPC_FAULT_CANT_PERFORM;
+		return;
+	}
+
+	if (r->in.request_blob.length > MAX_SL_FRAGMENT_SIZE) {
+		DEBUG(1, ("%s: request length too large\n", __func__));
+		p->fault_state = DCERPC_FAULT_CANT_PERFORM;
+		return;
+	}
+
+	if (r->in.max_fragment_size1 > MAX_SL_FRAGMENT_SIZE) {
+		DEBUG(1, ("%s: request fragment size too large: %u\n",
+			  __func__, (unsigned)r->in.max_fragment_size1));
+		p->fault_state = DCERPC_FAULT_CANT_PERFORM;
+		return;
+	}
+
+	rbuf = talloc_zero_array(p->mem_ctx, char, r->in.max_fragment_size1);
+	if (rbuf == NULL) {
+		p->fault_state = DCERPC_FAULT_CANT_PERFORM;
+		return;
+	}
+	r->out.response_blob->spotlight_blob = (uint8_t *)rbuf;
+	r->out.response_blob->size = r->in.max_fragment_size1;
+
+	ok = mds_dispatch(mds_ctx, &r->in.request_blob, r->out.response_blob);
+	if (ok) {
+		*r->out.status = 0;
+		*r->out.unkn9 = 0;
+	} else {
+		/* FIXME: just interpolating from AFP, needs verification */
+		*r->out.status = UINT32_MAX;
+		*r->out.unkn9 = UINT32_MAX;
+	}
 
 	return;
 }
 
 void _mdssvc_close(struct pipes_struct *p, struct mdssvc_close *r)
 {
-	DEBUG(10, ("%s\n", __func__));
-	p->fault_state = DCERPC_FAULT_OP_RNG_ERROR;
+	struct mds_ctx *mds_ctx;
+
+	if (!find_policy_by_hnd(p, &r->in.in_handle, (void **)(void *)&mds_ctx)) {
+		DEBUG(1, ("%s: invalid handle\n", __func__));
+		return;
+	}
+
+	DEBUG(10, ("%s: path: %s\n", __func__, mds_ctx->spath));
+
+	close_policy_hnd(p, &r->in.in_handle);
+
+	ZERO_STRUCTP(r->out.out_handle);
+	*r->out.status = 0;
+
 	return;
 }
diff --git a/source3/rpc_server/mdssvc/srv_mdssvc_nt.h b/source3/rpc_server/mdssvc/srv_mdssvc_nt.h
new file mode 100644
index 0000000..8b78f5e
--- /dev/null
+++ b/source3/rpc_server/mdssvc/srv_mdssvc_nt.h
@@ -0,0 +1,27 @@
+/*
+ *  Unix SMB/CIFS implementation.
+ *  MDSSVC RPC pipe initialisation routines
+ *
+ *  Copyright (C) Ralph Boehme                 2014
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  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, see <http://www.gnu.org/licenses/>.
+ */
+
+#ifndef _SRV_MDSSVC_NT_H
+#define _SRV_MDSSVC_NT_H
+
+bool init_service_mdssvc(struct messaging_context *msg_ctx);
+bool shutdown_service_mdssvc(void);
+
+#endif /* _SRV_MDSSVC_NT_H */
diff --git a/source3/rpc_server/rpc_service_setup.c b/source3/rpc_server/rpc_service_setup.c
index 7395caa..f062497 100644
--- a/source3/rpc_server/rpc_service_setup.c
+++ b/source3/rpc_server/rpc_service_setup.c
@@ -45,6 +45,7 @@
 #include "rpc_server/svcctl/srv_svcctl_reg.h"
 #include "rpc_server/spoolss/srv_spoolss_nt.h"
 #include "rpc_server/svcctl/srv_svcctl_nt.h"
+#include "rpc_server/mdssvc/srv_mdssvc_nt.h"
 
 #include "librpc/rpc/dcerpc_ep.h"
 #include "rpc_server/rpc_sock_helper.h"
@@ -445,18 +446,44 @@ static bool rpc_setup_initshutdown(struct tevent_context *ev_ctx,
 }
 
 #ifdef WITH_SPOTLIGHT
+static bool mdssvc_init_cb(void *ptr)
+{
+	struct messaging_context *msg_ctx =
+		talloc_get_type_abort(ptr, struct messaging_context);
+	bool ok;
+
+	ok = init_service_mdssvc(msg_ctx);
+	if (!ok) {
+		return false;
+	}
+
+	return true;
+}
+
+static bool mdssvc_shutdown_cb(void *ptr)
+{
+	shutdown_service_mdssvc();
+
+	return true;
+}
+
 static bool rpc_setup_mdssvc(struct tevent_context *ev_ctx,
 			     struct messaging_context *msg_ctx)
 {
 	const struct ndr_interface_table *t = &ndr_table_mdssvc;
 	const char *pipe_name = "mdssvc";
+	struct rpc_srv_callbacks mdssvc_cb;
 	NTSTATUS status;
 	enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
 	if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
 		return true;
 	}
 
-	status = rpc_mdssvc_init(NULL);
+	mdssvc_cb.init         = mdssvc_init_cb;
+	mdssvc_cb.shutdown     = mdssvc_shutdown_cb;
+	mdssvc_cb.private_data = msg_ctx;
+
+	status = rpc_mdssvc_init(&mdssvc_cb);
 	if (!NT_STATUS_IS_OK(status)) {
 		return false;
 	}
-- 
2.1.0


From 876e3271ce3858db30fd69501cd42802d437da69 Mon Sep 17 00:00:00 2001
From: Ralph Boehme <rb at sernet.de>
Date: Thu, 31 Jul 2014 18:01:34 +0200
Subject: [PATCH 09/12] s3-mdssvc: lexer and parser for Spotlight queries

Add a lexer and parser for translating Spotlight query strings to
SPARQL.

Signed-off-by: Ralph Boehme <slow at samba.org>
Reviewed-by: Stefan Metzmacher <metze at samba.org>
---
 source3/rpc_server/mdssvc/Makefile             |   16 +
 source3/rpc_server/mdssvc/README               |   14 +
 source3/rpc_server/mdssvc/mdssvc.c             |   25 +-
 source3/rpc_server/mdssvc/mdssvc.h             |    1 +
 source3/rpc_server/mdssvc/sparql_lexer.c       | 1834 ++++++++++++++++++++++
 source3/rpc_server/mdssvc/sparql_lexer.l       |   57 +
 source3/rpc_server/mdssvc/sparql_parser.c      | 1973 ++++++++++++++++++++++++
 source3/rpc_server/mdssvc/sparql_parser.h      |   98 ++
 source3/rpc_server/mdssvc/sparql_parser.y      |  479 ++++++
 source3/rpc_server/mdssvc/sparql_parser_test.c |   38 +
 source3/rpc_server/wscript_build               |    2 +
 source3/wscript_build                          |   10 +
 12 files changed, 4541 insertions(+), 6 deletions(-)
 create mode 100644 source3/rpc_server/mdssvc/Makefile
 create mode 100644 source3/rpc_server/mdssvc/README
 create mode 100644 source3/rpc_server/mdssvc/sparql_lexer.c
 create mode 100644 source3/rpc_server/mdssvc/sparql_lexer.l
 create mode 100644 source3/rpc_server/mdssvc/sparql_parser.c
 create mode 100644 source3/rpc_server/mdssvc/sparql_parser.h
 create mode 100644 source3/rpc_server/mdssvc/sparql_parser.y
 create mode 100644 source3/rpc_server/mdssvc/sparql_parser_test.c

diff --git a/source3/rpc_server/mdssvc/Makefile b/source3/rpc_server/mdssvc/Makefile
new file mode 100644
index 0000000..3b0983f
--- /dev/null
+++ b/source3/rpc_server/mdssvc/Makefile
@@ -0,0 +1,16 @@
+BISON=bison
+FLEX=flex
+SED=sed
+
+PARSER=sparql_parser
+LEXER=sparql_lexer
+
+all: $(PARSER).c $(LEXER).c
+
+$(PARSER).c: $(PARSER).y
+	$(BISON) -d -o $@ $<
+
+$(LEXER).c: $(LEXER).l
+	$(FLEX) -o $@ $<
+	$(SED) -i s/malloc/SMB_MALLOC/g $@
+	$(SED) -i s/realloc/SMB_REALLOC/g $@
diff --git a/source3/rpc_server/mdssvc/README b/source3/rpc_server/mdssvc/README
new file mode 100644
index 0000000..7dff83e
--- /dev/null
+++ b/source3/rpc_server/mdssvc/README
@@ -0,0 +1,14 @@
+Introduction:
+=============
+This directory contains source code for the metadata search service
+aka Spotlight.
+
+Bison and flex:
+===============
+Not yet integrated into the waf buildsystem, run these by hand:
+
+$ bison -d -o sparql_parser.c sparql_parser.y
+$ flex -o sparql_lexer.c sparql_lexer.l
+
+or use the bundled Makefile.
+
diff --git a/source3/rpc_server/mdssvc/mdssvc.c b/source3/rpc_server/mdssvc/mdssvc.c
index ba39e25..1143b45 100644
--- a/source3/rpc_server/mdssvc/mdssvc.c
+++ b/source3/rpc_server/mdssvc/mdssvc.c
@@ -27,6 +27,7 @@
 #include "lib/dbwrap/dbwrap_rbt.h"
 #include "libcli/security/dom_sid.h"
 #include "mdssvc.h"
+#include "sparql_parser.h"
 
 #undef DBGC_CLASS
 #define DBGC_CLASS DBGC_RPC_SRV
@@ -1250,16 +1251,28 @@ static bool slrpc_open_query(struct mds_ctx *mds_ctx,
 
 	DLIST_ADD(mds_ctx->query_list, slq);
 
-	/*
-	 * TODO: call the SPARQL translator here...
-	 */
-	goto error;
+	ok = map_spotlight_to_sparql_query(slq);
+	if (!ok) {
+		/*
+		 * Two cases:
+		 *
+		 * 1) the query string is "false", the parser returns
+		 * an error for that. We're supposed to return -1
+		 * here.
+		 *
+		 * 2) the parsing really failed, in that case we're
+		 * probably supposed to return -1 too, this needs
+		 * verification though
+		 */
+		SLQ_DEBUG(10, slq, "map failed");
+		goto error;
+	}
 
-	DEBUG(10, ("SPARQL query: \"%s\"\n", sparql_query));
+	DEBUG(10, ("SPARQL query: \"%s\"\n", slq->sparql_query));
 
 	g_main_context_push_thread_default(mds_ctx->gcontext);
 	tracker_sparql_connection_query_async(mds_ctx->tracker_con,
-					      sparql_query,
+					      slq->sparql_query,
 					      slq->gcancellable,
 					      tracker_query_cb,
 					      slq);
diff --git a/source3/rpc_server/mdssvc/mdssvc.h b/source3/rpc_server/mdssvc/mdssvc.h
index 0ad1e48..2c9dc83 100644
--- a/source3/rpc_server/mdssvc/mdssvc.h
+++ b/source3/rpc_server/mdssvc/mdssvc.h
@@ -70,6 +70,7 @@ struct sl_query {
 	uint64_t         ctx2;           /* client context 2 */
 	sl_array_t      *reqinfo;        /* array with requested metadata */
 	const char      *query_string;   /* the Spotlight query string */
+	const char      *sparql_query;   /* the SPARQL query string */
 	uint64_t        *cnids;          /* restrict query to these CNIDs */
 	size_t           cnids_num;      /* Size of slq_cnids array */
 	const char      *path_scope;	 /* path to directory to search */
diff --git a/source3/rpc_server/mdssvc/sparql_lexer.c b/source3/rpc_server/mdssvc/sparql_lexer.c
new file mode 100644
index 0000000..34bc0ca
--- /dev/null
+++ b/source3/rpc_server/mdssvc/sparql_lexer.c
@@ -0,0 +1,1834 @@
+#line 2 "sparql_lexer.c"
+
+#line 4 "sparql_lexer.c"
+
+#define  YY_INT_ALIGNED short int
+
+/* A lexical scanner generated by flex */
+
+#define FLEX_SCANNER
+#define YY_FLEX_MAJOR_VERSION 2
+#define YY_FLEX_MINOR_VERSION 5
+#define YY_FLEX_SUBMINOR_VERSION 37
+#if YY_FLEX_SUBMINOR_VERSION > 0
+#define FLEX_BETA
+#endif
+
+/* First, we deal with  platform-specific or compiler-specific issues. */
+
+/* begin standard C headers. */
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <stdlib.h>
+
+/* end standard C headers. */
+
+/* flex integer type definitions */
+
+#ifndef FLEXINT_H
+#define FLEXINT_H
+
+/* C99 systems have <inttypes.h>. Non-C99 systems may or may not. */
+
+#if defined (__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
+
+/* C99 says to define __STDC_LIMIT_MACROS before including stdint.h,
+ * if you want the limit (max/min) macros for int types. 
+ */
+#ifndef __STDC_LIMIT_MACROS
+#define __STDC_LIMIT_MACROS 1
+#endif
+
+#include <inttypes.h>
+typedef int8_t flex_int8_t;
+typedef uint8_t flex_uint8_t;
+typedef int16_t flex_int16_t;
+typedef uint16_t flex_uint16_t;
+typedef int32_t flex_int32_t;
+typedef uint32_t flex_uint32_t;
+#else
+typedef signed char flex_int8_t;
+typedef short int flex_int16_t;
+typedef int flex_int32_t;
+typedef unsigned char flex_uint8_t; 
+typedef unsigned short int flex_uint16_t;
+typedef unsigned int flex_uint32_t;
+
+/* Limits of integral types. */
+#ifndef INT8_MIN
+#define INT8_MIN               (-128)
+#endif
+#ifndef INT16_MIN
+#define INT16_MIN              (-32767-1)
+#endif
+#ifndef INT32_MIN
+#define INT32_MIN              (-2147483647-1)
+#endif
+#ifndef INT8_MAX
+#define INT8_MAX               (127)
+#endif
+#ifndef INT16_MAX
+#define INT16_MAX              (32767)
+#endif
+#ifndef INT32_MAX
+#define INT32_MAX              (2147483647)
+#endif
+#ifndef UINT8_MAX
+#define UINT8_MAX              (255U)
+#endif
+#ifndef UINT16_MAX
+#define UINT16_MAX             (65535U)
+#endif
+#ifndef UINT32_MAX
+#define UINT32_MAX             (4294967295U)
+#endif
+
+#endif /* ! C99 */
+
+#endif /* ! FLEXINT_H */
+
+#ifdef __cplusplus
+
+/* The "const" storage-class-modifier is valid. */
+#define YY_USE_CONST
+
+#else	/* ! __cplusplus */
+
+/* C99 requires __STDC__ to be defined as 1. */
+#if defined (__STDC__)
+
+#define YY_USE_CONST
+
+#endif	/* defined (__STDC__) */
+#endif	/* ! __cplusplus */
+
+#ifdef YY_USE_CONST
+#define yyconst const
+#else
+#define yyconst
+#endif
+
+/* Returned upon end-of-file. */
+#define YY_NULL 0
+
+/* Promotes a possibly negative, possibly signed char to an unsigned
+ * integer for use as an array index.  If the signed char is negative,
+ * we want to instead treat it as an 8-bit unsigned char, hence the
+ * double cast.
+ */
+#define YY_SC_TO_UI(c) ((unsigned int) (unsigned char) c)
+
+/* Enter a start condition.  This macro really ought to take a parameter,
+ * but we do it the disgusting crufty way forced on us by the ()-less
+ * definition of BEGIN.
+ */
+#define BEGIN (yy_start) = 1 + 2 *
+
+/* Translate the current start state into a value that can be later handed
+ * to BEGIN to return to the state.  The YYSTATE alias is for lex
+ * compatibility.
+ */
+#define YY_START (((yy_start) - 1) / 2)
+#define YYSTATE YY_START
+
+/* Action number for EOF rule of a given start state. */
+#define YY_STATE_EOF(state) (YY_END_OF_BUFFER + state + 1)
+
+/* Special action meaning "start processing a new file". */
+#define YY_NEW_FILE yyrestart(yyin  )
+
+#define YY_END_OF_BUFFER_CHAR 0
+
+/* Size of default input buffer. */
+#ifndef YY_BUF_SIZE
+#define YY_BUF_SIZE 16384
+#endif
+
+/* The state buf must be large enough to hold one state per character in the main buffer.
+ */
+#define YY_STATE_BUF_SIZE   ((YY_BUF_SIZE + 2) * sizeof(yy_state_type))
+
+#ifndef YY_TYPEDEF_YY_BUFFER_STATE
+#define YY_TYPEDEF_YY_BUFFER_STATE
+typedef struct yy_buffer_state *YY_BUFFER_STATE;
+#endif
+
+#ifndef YY_TYPEDEF_YY_SIZE_T
+#define YY_TYPEDEF_YY_SIZE_T
+typedef size_t yy_size_t;
+#endif
+
+extern yy_size_t yyleng;
+
+extern FILE *yyin, *yyout;
+
+#define EOB_ACT_CONTINUE_SCAN 0
+#define EOB_ACT_END_OF_FILE 1
+#define EOB_ACT_LAST_MATCH 2
+
+    #define YY_LESS_LINENO(n)
+    
+/* Return all but the first "n" matched characters back to the input stream. */
+#define yyless(n) \
+	do \
+		{ \
+		/* Undo effects of setting up yytext. */ \
+        int yyless_macro_arg = (n); \
+        YY_LESS_LINENO(yyless_macro_arg);\
+		*yy_cp = (yy_hold_char); \
+		YY_RESTORE_YY_MORE_OFFSET \
+		(yy_c_buf_p) = yy_cp = yy_bp + yyless_macro_arg - YY_MORE_ADJ; \
+		YY_DO_BEFORE_ACTION; /* set up yytext again */ \
+		} \
+	while ( 0 )
+
+#define unput(c) yyunput( c, (yytext_ptr)  )
+
+#ifndef YY_STRUCT_YY_BUFFER_STATE
+#define YY_STRUCT_YY_BUFFER_STATE
+struct yy_buffer_state
+	{
+	FILE *yy_input_file;
+
+	char *yy_ch_buf;		/* input buffer */
+	char *yy_buf_pos;		/* current position in input buffer */
+
+	/* Size of input buffer in bytes, not including room for EOB
+	 * characters.
+	 */
+	yy_size_t yy_buf_size;
+
+	/* Number of characters read into yy_ch_buf, not including EOB
+	 * characters.
+	 */
+	yy_size_t yy_n_chars;
+
+	/* Whether we "own" the buffer - i.e., we know we created it,
+	 * and can SMB_REALLOC() it to grow it, and should free() it to
+	 * delete it.
+	 */
+	int yy_is_our_buffer;
+
+	/* Whether this is an "interactive" input source; if so, and
+	 * if we're using stdio for input, then we want to use getc()
+	 * instead of fread(), to make sure we stop fetching input after
+	 * each newline.
+	 */
+	int yy_is_interactive;
+
+	/* Whether we're considered to be at the beginning of a line.
+	 * If so, '^' rules will be active on the next match, otherwise
+	 * not.
+	 */
+	int yy_at_bol;
+
+    int yy_bs_lineno; /**< The line count. */
+    int yy_bs_column; /**< The column count. */
+    
+	/* Whether to try to fill the input buffer when we reach the
+	 * end of it.
+	 */
+	int yy_fill_buffer;
+
+	int yy_buffer_status;
+
+#define YY_BUFFER_NEW 0
+#define YY_BUFFER_NORMAL 1
+	/* When an EOF's been seen but there's still some text to process
+	 * then we mark the buffer as YY_EOF_PENDING, to indicate that we
+	 * shouldn't try reading from the input source any more.  We might
+	 * still have a bunch of tokens to match, though, because of
+	 * possible backing-up.
+	 *
+	 * When we actually see the EOF, we change the status to "new"
+	 * (via yyrestart()), so that the user can continue scanning by
+	 * just pointing yyin at a new input file.
+	 */
+#define YY_BUFFER_EOF_PENDING 2
+
+	};
+#endif /* !YY_STRUCT_YY_BUFFER_STATE */
+
+/* Stack of input buffers. */
+static size_t yy_buffer_stack_top = 0; /**< index of top of stack. */
+static size_t yy_buffer_stack_max = 0; /**< capacity of stack. */
+static YY_BUFFER_STATE * yy_buffer_stack = 0; /**< Stack as an array. */
+
+/* We provide macros for accessing buffer states in case in the
+ * future we want to put the buffer states in a more general
+ * "scanner state".
+ *
+ * Returns the top of the stack, or NULL.
+ */
+#define YY_CURRENT_BUFFER ( (yy_buffer_stack) \
+                          ? (yy_buffer_stack)[(yy_buffer_stack_top)] \
+                          : NULL)
+
+/* Same as previous macro, but useful when we know that the buffer stack is not
+ * NULL or when we need an lvalue. For internal use only.
+ */
+#define YY_CURRENT_BUFFER_LVALUE (yy_buffer_stack)[(yy_buffer_stack_top)]
+
+/* yy_hold_char holds the character lost when yytext is formed. */
+static char yy_hold_char;
+static yy_size_t yy_n_chars;		/* number of characters read into yy_ch_buf */
+yy_size_t yyleng;
+
+/* Points to current character in buffer. */
+static char *yy_c_buf_p = (char *) 0;
+static int yy_init = 0;		/* whether we need to initialize */
+static int yy_start = 0;	/* start state number */
+
+/* Flag which is used to allow yywrap()'s to do buffer switches
+ * instead of setting up a fresh yyin.  A bit of a hack ...
+ */
+static int yy_did_buffer_switch_on_eof;
+
+void yyrestart (FILE *input_file  );
+void yy_switch_to_buffer (YY_BUFFER_STATE new_buffer  );
+YY_BUFFER_STATE yy_create_buffer (FILE *file,int size  );
+void yy_delete_buffer (YY_BUFFER_STATE b  );
+void yy_flush_buffer (YY_BUFFER_STATE b  );
+void yypush_buffer_state (YY_BUFFER_STATE new_buffer  );
+void yypop_buffer_state (void );
+
+static void yyensure_buffer_stack (void );
+static void yy_load_buffer_state (void );
+static void yy_init_buffer (YY_BUFFER_STATE b,FILE *file  );
+
+#define YY_FLUSH_BUFFER yy_flush_buffer(YY_CURRENT_BUFFER )
+
+YY_BUFFER_STATE yy_scan_buffer (char *base,yy_size_t size  );
+YY_BUFFER_STATE yy_scan_string (yyconst char *yy_str  );
+YY_BUFFER_STATE yy_scan_bytes (yyconst char *bytes,yy_size_t len  );
+
+void *yyalloc (yy_size_t  );
+void *yySMB_REALLOC (void *,yy_size_t  );
+void yyfree (void *  );
+
+#define yy_new_buffer yy_create_buffer
+
+#define yy_set_interactive(is_interactive) \
+	{ \
+	if ( ! YY_CURRENT_BUFFER ){ \
+        yyensure_buffer_stack (); \
+		YY_CURRENT_BUFFER_LVALUE =    \
+            yy_create_buffer(yyin,YY_BUF_SIZE ); \
+	} \
+	YY_CURRENT_BUFFER_LVALUE->yy_is_interactive = is_interactive; \
+	}
+
+#define yy_set_bol(at_bol) \
+	{ \
+	if ( ! YY_CURRENT_BUFFER ){\
+        yyensure_buffer_stack (); \
+		YY_CURRENT_BUFFER_LVALUE =    \
+            yy_create_buffer(yyin,YY_BUF_SIZE ); \
+	} \
+	YY_CURRENT_BUFFER_LVALUE->yy_at_bol = at_bol; \
+	}
+
+#define YY_AT_BOL() (YY_CURRENT_BUFFER_LVALUE->yy_at_bol)
+
+/* Begin user sect3 */
+
+typedef unsigned char YY_CHAR;
+
+FILE *yyin = (FILE *) 0, *yyout = (FILE *) 0;
+
+typedef int yy_state_type;
+
+extern int yylineno;
+
+int yylineno = 1;
+
+extern char *yytext;
+#define yytext_ptr yytext
+
+static yy_state_type yy_get_previous_state (void );
+static yy_state_type yy_try_NUL_trans (yy_state_type current_state  );
+static int yy_get_next_buffer (void );
+static void yy_fatal_error (yyconst char msg[]  );
+
+/* Done after the current pattern has been matched and before the
+ * corresponding action - sets up yytext.
+ */
+#define YY_DO_BEFORE_ACTION \
+	(yytext_ptr) = yy_bp; \
+	yyleng = (size_t) (yy_cp - yy_bp); \
+	(yy_hold_char) = *yy_cp; \
+	*yy_cp = '\0'; \
+	(yy_c_buf_p) = yy_cp;
+
+#define YY_NUM_RULES 18
+#define YY_END_OF_BUFFER 19
+/* This struct is not used in this scanner,
+   but its presence is necessary. */
+struct yy_trans_info
+	{
+	flex_int32_t yy_verify;
+	flex_int32_t yy_nxt;
+	};
+static yyconst flex_int16_t yy_accept[57] =
+    {   0,
+        0,    0,   19,   18,   17,   18,    5,   18,   18,    6,
+        7,   16,   15,   13,   12,   14,   16,   16,   16,   18,
+       18,   18,   18,   11,    0,    8,   16,    0,    0,    0,
+       10,   16,   16,   16,    9,    0,    0,    0,   16,   16,
+       16,    0,    0,   16,   16,    4,    0,   16,    3,    0,
+       16,    0,    1,    0,    2,    0
+    } ;
+
+static yyconst flex_int32_t yy_ec[256] =
+    {   0,
+        1,    1,    1,    1,    1,    1,    1,    1,    2,    2,
+        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
+        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
+        1,    2,    3,    4,    1,    5,    1,    6,    1,    7,
+        8,    9,    1,   10,    9,   11,    1,    9,    9,    9,
+        9,    9,    9,    9,    9,    9,    9,    9,    1,   12,
+       13,   14,    1,    1,    9,    9,    9,    9,    9,    9,
+        9,    9,   15,    9,    9,    9,    9,    9,    9,    9,
+        9,   16,    9,    9,    9,    9,    9,    9,    9,    9,
+        1,    1,    1,    1,    9,    1,   17,    9,    9,    9,
+
+       18,   19,   20,    9,   21,    9,    9,   22,   23,   24,
+       25,    9,    9,   26,   27,   28,   29,    9,    9,    9,
+        9,    9,    1,   30,    1,    1,    1,   31,   31,   31,
+       31,   31,   31,   31,   31,   31,   31,   31,   31,   31,
+       31,   31,   31,   31,   31,   31,   31,   31,   31,   31,
+       31,   31,   31,   31,   31,   31,   31,   31,   31,   31,
+       31,   31,   31,   31,   31,   31,   31,   31,   31,   31,
+       31,   31,   31,   31,   31,   31,   31,   31,   31,   31,
+       31,   31,   31,   31,   31,   31,   31,   31,   31,   31,
+       31,    1,    1,   32,   32,   32,   32,   32,   32,   32,
+
+       32,   32,   32,   32,   32,   32,   32,   32,   32,   32,
+       32,   32,   32,   32,   32,   32,   32,   32,   32,   32,
+       32,   32,   32,   33,   33,   33,   33,   33,   33,   33,
+       33,   33,   33,   33,   33,   33,   33,   33,   33,   34,
+       34,   34,   34,   34,    1,    1,    1,    1,    1,    1,
+        1,    1,    1,    1,    1
+    } ;
+
+static yyconst flex_int32_t yy_meta[35] =
+    {   0,
+        1,    1,    1,    1,    1,    1,    1,    1,    2,    1,
+        2,    1,    1,    1,    2,    2,    2,    2,    2,    2,
+        2,    2,    2,    2,    2,    2,    2,    2,    2,    1,
+        1,    2,    2,    2
+    } ;
+
+static yyconst flex_int16_t yy_base[58] =
+    {   0,
+        0,    0,   91,   92,   92,   77,   92,   61,   82,   92,
+       92,    3,   92,   92,   74,   92,   14,   25,   15,   56,
+       54,   53,   52,   92,   61,   92,   57,   49,   48,   47,
+       92,   27,   28,   16,   92,   46,   45,   52,   36,   30,
+       37,   43,   55,   48,   38,   47,   59,   39,   45,   47,
+       40,   40,   42,   40,   92,   92,   42
+    } ;
+
+static yyconst flex_int16_t yy_def[58] =
+    {   0,
+       56,    1,   56,   56,   56,   56,   56,   56,   56,   56,
+       56,   57,   56,   56,   56,   56,   57,   17,   17,   56,
+       56,   56,   56,   56,   56,   56,   17,   56,   56,   56,
+       56,   17,   17,   17,   56,   56,   56,   56,   17,   17,
+       17,   56,   56,   17,   17,   17,   56,   17,   17,   56,
+       17,   56,   17,   56,   56,    0,   56
+    } ;
+
+static yyconst flex_int16_t yy_nxt[127] =
+    {   0,
+        4,    5,    6,    7,    8,    9,   10,   11,   12,   13,
+       12,   14,   15,   16,   17,   12,   12,   12,   18,   12,
+       12,   12,   12,   12,   12,   12,   12,   19,   12,   20,
+        4,   21,   22,   23,   28,   29,   30,   32,   27,   27,
+       34,   33,   39,   27,   41,   28,   29,   30,   27,   40,
+       27,   27,   44,   27,   46,   49,   45,   53,   51,   27,
+       27,   27,   27,   27,   55,   27,   54,   52,   27,   50,
+       27,   48,   47,   27,   43,   42,   27,   37,   36,   27,
+       27,   38,   37,   36,   27,   35,   31,   26,   25,   24,
+       56,    3,   56,   56,   56,   56,   56,   56,   56,   56,
+
+       56,   56,   56,   56,   56,   56,   56,   56,   56,   56,
+       56,   56,   56,   56,   56,   56,   56,   56,   56,   56,
+       56,   56,   56,   56,   56,   56
+    } ;
+
+static yyconst flex_int16_t yy_chk[127] =
+    {   0,
+        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
+        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
+        1,    1,    1,    1,    1,    1,    1,    1,    1,    1,
+        1,    1,    1,    1,   12,   12,   12,   17,   19,   34,
+       19,   18,   32,   57,   34,   17,   17,   17,   18,   33,
+       32,   33,   39,   40,   41,   45,   40,   51,   48,   39,
+       41,   45,   48,   51,   54,   53,   52,   50,   49,   47,
+       46,   44,   43,   42,   38,   37,   36,   30,   29,   28,
+       27,   25,   23,   22,   21,   20,   15,    9,    8,    6,
+        3,   56,   56,   56,   56,   56,   56,   56,   56,   56,
+
+       56,   56,   56,   56,   56,   56,   56,   56,   56,   56,
+       56,   56,   56,   56,   56,   56,   56,   56,   56,   56,
+       56,   56,   56,   56,   56,   56
+    } ;
+
+static yy_state_type yy_last_accepting_state;
+static char *yy_last_accepting_cpos;
+
+extern int yy_flex_debug;
+int yy_flex_debug = 0;
+
+/* The intent behind this definition is that it'll catch
+ * any uses of REJECT which flex missed.
+ */
+#define REJECT reject_used_but_not_detected
+#define yymore() yymore_used_but_not_detected
+#define YY_MORE_ADJ 0
+#define YY_RESTORE_YY_MORE_OFFSET
+char *yytext;
+#line 1 "sparql_lexer.l"
+/*
+   Unix SMB/CIFS implementation.
+   Main metadata server / Spotlight routines
+
+   Copyright (C) Ralph Boehme			2012-2014
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   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, see <http://www.gnu.org/licenses/>.
+*/
+#line 22 "sparql_lexer.l"
+#include "includes.h"
+#include "sparql_parser.h"
+
+#define YY_NO_INPUT
+#line 519 "sparql_lexer.c"
+
+#define INITIAL 0
+
+#ifndef YY_NO_UNISTD_H
+/* Special case for "unistd.h", since it is non-ANSI. We include it way
+ * down here because we want the user's section 1 to have been scanned first.
+ * The user has a chance to override it with an option.
+ */
+#include <unistd.h>
+#endif
+
+#ifndef YY_EXTRA_TYPE
+#define YY_EXTRA_TYPE void *
+#endif
+
+static int yy_init_globals (void );
+
+/* Accessor methods to globals.
+   These are made visible to non-reentrant scanners for convenience. */
+
+int yylex_destroy (void );
+
+int yyget_debug (void );
+
+void yyset_debug (int debug_flag  );
+
+YY_EXTRA_TYPE yyget_extra (void );
+
+void yyset_extra (YY_EXTRA_TYPE user_defined  );
+
+FILE *yyget_in (void );
+
+void yyset_in  (FILE * in_str  );
+
+FILE *yyget_out (void );
+
+void yyset_out  (FILE * out_str  );
+
+yy_size_t yyget_leng (void );
+
+char *yyget_text (void );
+
+int yyget_lineno (void );
+
+void yyset_lineno (int line_number  );
+
+/* Macros after this point can all be overridden by user definitions in
+ * section 1.
+ */
+
+#ifndef YY_SKIP_YYWRAP
+#ifdef __cplusplus
+extern "C" int yywrap (void );
+#else
+extern int yywrap (void );
+#endif
+#endif
+
+#ifndef yytext_ptr
+static void yy_flex_strncpy (char *,yyconst char *,int );
+#endif
+
+#ifdef YY_NEED_STRLEN
+static int yy_flex_strlen (yyconst char * );
+#endif
+
+#ifndef YY_NO_INPUT
+
+#ifdef __cplusplus
+static int yyinput (void );
+#else
+static int input (void );
+#endif
+
+#endif
+
+/* Amount of stuff to slurp up with each read. */
+#ifndef YY_READ_BUF_SIZE
+#define YY_READ_BUF_SIZE 8192
+#endif
+
+/* Copy whatever the last rule matched to the standard output. */
+#ifndef ECHO
+/* This used to be an fputs(), but since the string might contain NUL's,
+ * we now use fwrite().
+ */
+#define ECHO do { if (fwrite( yytext, yyleng, 1, yyout )) {} } while (0)
+#endif
+
+/* Gets input and stuffs it into "buf".  number of characters read, or YY_NULL,
+ * is returned in "result".
+ */
+#ifndef YY_INPUT
+#define YY_INPUT(buf,result,max_size) \
+	if ( YY_CURRENT_BUFFER_LVALUE->yy_is_interactive ) \
+		{ \
+		int c = '*'; \
+		size_t n; \
+		for ( n = 0; n < max_size && \
+			     (c = getc( yyin )) != EOF && c != '\n'; ++n ) \
+			buf[n] = (char) c; \
+		if ( c == '\n' ) \
+			buf[n++] = (char) c; \
+		if ( c == EOF && ferror( yyin ) ) \
+			YY_FATAL_ERROR( "input in flex scanner failed" ); \
+		result = n; \
+		} \
+	else \
+		{ \
+		errno=0; \
+		while ( (result = fread(buf, 1, max_size, yyin))==0 && ferror(yyin)) \
+			{ \
+			if( errno != EINTR) \
+				{ \
+				YY_FATAL_ERROR( "input in flex scanner failed" ); \
+				break; \
+				} \
+			errno=0; \
+			clearerr(yyin); \
+			} \
+		}\
+\
+
+#endif
+
+/* No semi-colon after return; correct usage is to write "yyterminate();" -
+ * we don't want an extra ';' after the "return" because that will cause
+ * some compilers to complain about unreachable statements.
+ */
+#ifndef yyterminate
+#define yyterminate() return YY_NULL
+#endif
+
+/* Number of entries by which start-condition stack grows. */
+#ifndef YY_START_STACK_INCR
+#define YY_START_STACK_INCR 25
+#endif
+
+/* Report a fatal error. */
+#ifndef YY_FATAL_ERROR
+#define YY_FATAL_ERROR(msg) yy_fatal_error( msg )
+#endif
+
+/* end tables serialization structures and prototypes */
+
+/* Default declaration of generated scanner - a define so the user can
+ * easily add parameters.
+ */
+#ifndef YY_DECL
+#define YY_DECL_IS_OURS 1
+
+extern int yylex (void);
+
+#define YY_DECL int yylex (void)
+#endif /* !YY_DECL */
+
+/* Code executed at the beginning of each rule, after yytext and yyleng
+ * have been set up.
+ */
+#ifndef YY_USER_ACTION
+#define YY_USER_ACTION
+#endif
+
+/* Code executed at the end of each rule. */
+#ifndef YY_BREAK
+#define YY_BREAK break;
+#endif
+
+#define YY_RULE_SETUP \
+	YY_USER_ACTION
+
+/** The main scanner function which does all the work.
+ */
+YY_DECL
+{
+	register yy_state_type yy_current_state;
+	register char *yy_cp, *yy_bp;
+	register int yy_act;
+    
+#line 39 "sparql_lexer.l"
+
+#line 701 "sparql_lexer.c"
+
+	if ( !(yy_init) )
+		{
+		(yy_init) = 1;
+
+#ifdef YY_USER_INIT
+		YY_USER_INIT;
+#endif
+
+		if ( ! (yy_start) )
+			(yy_start) = 1;	/* first start state */
+
+		if ( ! yyin )
+			yyin = stdin;
+
+		if ( ! yyout )
+			yyout = stdout;
+
+		if ( ! YY_CURRENT_BUFFER ) {
+			yyensure_buffer_stack ();
+			YY_CURRENT_BUFFER_LVALUE =
+				yy_create_buffer(yyin,YY_BUF_SIZE );
+		}
+
+		yy_load_buffer_state( );
+		}
+
+	while ( 1 )		/* loops until end-of-file is reached */
+		{
+		yy_cp = (yy_c_buf_p);
+
+		/* Support of yytext. */
+		*yy_cp = (yy_hold_char);
+
+		/* yy_bp points to the position in yy_ch_buf of the start of
+		 * the current run.
+		 */
+		yy_bp = yy_cp;
+
+		yy_current_state = (yy_start);
+yy_match:
+		do
+			{
+			register YY_CHAR yy_c = yy_ec[YY_SC_TO_UI(*yy_cp)];
+			if ( yy_accept[yy_current_state] )
+				{
+				(yy_last_accepting_state) = yy_current_state;
+				(yy_last_accepting_cpos) = yy_cp;
+				}
+			while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
+				{
+				yy_current_state = (int) yy_def[yy_current_state];
+				if ( yy_current_state >= 57 )
+					yy_c = yy_meta[(unsigned int) yy_c];
+				}
+			yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
+			++yy_cp;
+			}
+		while ( yy_base[yy_current_state] != 92 );
+
+yy_find_action:
+		yy_act = yy_accept[yy_current_state];
+		if ( yy_act == 0 )
+			{ /* have to back up */
+			yy_cp = (yy_last_accepting_cpos);
+			yy_current_state = (yy_last_accepting_state);
+			yy_act = yy_accept[yy_current_state];
+			}
+
+		YY_DO_BEFORE_ACTION;
+
+do_action:	/* This label is used only to access EOF actions. */
+
+		switch ( yy_act )
+	{ /* beginning of action switch */
+			case 0: /* must back up */
+			/* undo the effects of YY_DO_BEFORE_ACTION */
+			*yy_cp = (yy_hold_char);
+			yy_cp = (yy_last_accepting_cpos);
+			yy_current_state = (yy_last_accepting_state);
+			goto yy_find_action;
+
+case 1:
+YY_RULE_SETUP
+#line 40 "sparql_lexer.l"
+return FUNC_INRANGE;
+	YY_BREAK
+case 2:
+YY_RULE_SETUP
+#line 41 "sparql_lexer.l"
+return DATE_ISO;
+	YY_BREAK
+case 3:
+YY_RULE_SETUP
+#line 42 "sparql_lexer.l"
+{yylval.bval = false; return BOOL;}
+	YY_BREAK
+case 4:
+YY_RULE_SETUP
+#line 43 "sparql_lexer.l"
+{yylval.bval = true; return BOOL;}
+	YY_BREAK
+case 5:
+YY_RULE_SETUP
+#line 44 "sparql_lexer.l"
+return QUOTE;
+	YY_BREAK
+case 6:
+YY_RULE_SETUP
+#line 45 "sparql_lexer.l"
+return OBRACE;
+	YY_BREAK
+case 7:
+YY_RULE_SETUP
+#line 46 "sparql_lexer.l"
+return CBRACE;
+	YY_BREAK
+case 8:
+YY_RULE_SETUP
+#line 47 "sparql_lexer.l"
+return AND;
+	YY_BREAK
+case 9:
+YY_RULE_SETUP
+#line 48 "sparql_lexer.l"
+return OR;
+	YY_BREAK
+case 10:
+YY_RULE_SETUP
+#line 49 "sparql_lexer.l"
+return EQUAL;
+	YY_BREAK
+case 11:
+YY_RULE_SETUP
+#line 50 "sparql_lexer.l"
+return UNEQUAL;
+	YY_BREAK
+case 12:
+YY_RULE_SETUP
+#line 51 "sparql_lexer.l"
+return EQUAL;
+	YY_BREAK
+case 13:
+YY_RULE_SETUP
+#line 52 "sparql_lexer.l"
+return LT;
+	YY_BREAK
+case 14:
+YY_RULE_SETUP
+#line 53 "sparql_lexer.l"
+return GT;
+	YY_BREAK
+case 15:
+YY_RULE_SETUP
+#line 54 "sparql_lexer.l"
+return COMMA;
+	YY_BREAK
+case 16:
+YY_RULE_SETUP
+#line 55 "sparql_lexer.l"
+{yylval.sval = talloc_strdup(talloc_tos(), yytext); return WORD;}
+	YY_BREAK
+case 17:
+/* rule 17 can match eol */
+YY_RULE_SETUP
+#line 56 "sparql_lexer.l"
+/* ignore */
+	YY_BREAK
+case 18:
+YY_RULE_SETUP
+#line 57 "sparql_lexer.l"
+ECHO;
+	YY_BREAK
+#line 875 "sparql_lexer.c"
+case YY_STATE_EOF(INITIAL):
+	yyterminate();
+
+	case YY_END_OF_BUFFER:
+		{
+		/* Amount of text matched not including the EOB char. */
+		int yy_amount_of_matched_text = (int) (yy_cp - (yytext_ptr)) - 1;
+
+		/* Undo the effects of YY_DO_BEFORE_ACTION. */
+		*yy_cp = (yy_hold_char);
+		YY_RESTORE_YY_MORE_OFFSET
+
+		if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_NEW )
+			{
+			/* We're scanning a new file or input source.  It's
+			 * possible that this happened because the user
+			 * just pointed yyin at a new source and called
+			 * yylex().  If so, then we have to assure
+			 * consistency between YY_CURRENT_BUFFER and our
+			 * globals.  Here is the right place to do so, because
+			 * this is the first action (other than possibly a
+			 * back-up) that will match for the new input source.
+			 */
+			(yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars;
+			YY_CURRENT_BUFFER_LVALUE->yy_input_file = yyin;
+			YY_CURRENT_BUFFER_LVALUE->yy_buffer_status = YY_BUFFER_NORMAL;
+			}
+
+		/* Note that here we test for yy_c_buf_p "<=" to the position
+		 * of the first EOB in the buffer, since yy_c_buf_p will
+		 * already have been incremented past the NUL character
+		 * (since all states make transitions on EOB to the
+		 * end-of-buffer state).  Contrast this with the test
+		 * in input().
+		 */
+		if ( (yy_c_buf_p) <= &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] )
+			{ /* This was really a NUL. */
+			yy_state_type yy_next_state;
+
+			(yy_c_buf_p) = (yytext_ptr) + yy_amount_of_matched_text;
+
+			yy_current_state = yy_get_previous_state(  );
+
+			/* Okay, we're now positioned to make the NUL
+			 * transition.  We couldn't have
+			 * yy_get_previous_state() go ahead and do it
+			 * for us because it doesn't know how to deal
+			 * with the possibility of jamming (and we don't
+			 * want to build jamming into it because then it
+			 * will run more slowly).
+			 */
+
+			yy_next_state = yy_try_NUL_trans( yy_current_state );
+
+			yy_bp = (yytext_ptr) + YY_MORE_ADJ;
+
+			if ( yy_next_state )
+				{
+				/* Consume the NUL. */
+				yy_cp = ++(yy_c_buf_p);
+				yy_current_state = yy_next_state;
+				goto yy_match;
+				}
+
+			else
+				{
+				yy_cp = (yy_c_buf_p);
+				goto yy_find_action;
+				}
+			}
+
+		else switch ( yy_get_next_buffer(  ) )
+			{
+			case EOB_ACT_END_OF_FILE:
+				{
+				(yy_did_buffer_switch_on_eof) = 0;
+
+				if ( yywrap( ) )
+					{
+					/* Note: because we've taken care in
+					 * yy_get_next_buffer() to have set up
+					 * yytext, we can now set up
+					 * yy_c_buf_p so that if some total
+					 * hoser (like flex itself) wants to
+					 * call the scanner after we return the
+					 * YY_NULL, it'll still work - another
+					 * YY_NULL will get returned.
+					 */
+					(yy_c_buf_p) = (yytext_ptr) + YY_MORE_ADJ;
+
+					yy_act = YY_STATE_EOF(YY_START);
+					goto do_action;
+					}
+
+				else
+					{
+					if ( ! (yy_did_buffer_switch_on_eof) )
+						YY_NEW_FILE;
+					}
+				break;
+				}
+
+			case EOB_ACT_CONTINUE_SCAN:
+				(yy_c_buf_p) =
+					(yytext_ptr) + yy_amount_of_matched_text;
+
+				yy_current_state = yy_get_previous_state(  );
+
+				yy_cp = (yy_c_buf_p);
+				yy_bp = (yytext_ptr) + YY_MORE_ADJ;
+				goto yy_match;
+
+			case EOB_ACT_LAST_MATCH:
+				(yy_c_buf_p) =
+				&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)];
+
+				yy_current_state = yy_get_previous_state(  );
+
+				yy_cp = (yy_c_buf_p);
+				yy_bp = (yytext_ptr) + YY_MORE_ADJ;
+				goto yy_find_action;
+			}
+		break;
+		}
+
+	default:
+		YY_FATAL_ERROR(
+			"fatal flex scanner internal error--no action found" );
+	} /* end of action switch */
+		} /* end of scanning one token */
+} /* end of yylex */
+
+/* yy_get_next_buffer - try to read in a new buffer
+ *
+ * Returns a code representing an action:
+ *	EOB_ACT_LAST_MATCH -
+ *	EOB_ACT_CONTINUE_SCAN - continue scanning from current position
+ *	EOB_ACT_END_OF_FILE - end of file
+ */
+static int yy_get_next_buffer (void)
+{
+    	register char *dest = YY_CURRENT_BUFFER_LVALUE->yy_ch_buf;
+	register char *source = (yytext_ptr);
+	register int number_to_move, i;
+	int ret_val;
+
+	if ( (yy_c_buf_p) > &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] )
+		YY_FATAL_ERROR(
+		"fatal flex scanner internal error--end of buffer missed" );
+
+	if ( YY_CURRENT_BUFFER_LVALUE->yy_fill_buffer == 0 )
+		{ /* Don't try to fill the buffer, so this is an EOF. */
+		if ( (yy_c_buf_p) - (yytext_ptr) - YY_MORE_ADJ == 1 )
+			{
+			/* We matched a single character, the EOB, so
+			 * treat this as a final EOF.
+			 */
+			return EOB_ACT_END_OF_FILE;
+			}
+
+		else
+			{
+			/* We matched some text prior to the EOB, first
+			 * process it.
+			 */
+			return EOB_ACT_LAST_MATCH;
+			}
+		}
+
+	/* Try to read more data. */
+
+	/* First move last chars to start of buffer. */
+	number_to_move = (int) ((yy_c_buf_p) - (yytext_ptr)) - 1;
+
+	for ( i = 0; i < number_to_move; ++i )
+		*(dest++) = *(source++);
+
+	if ( YY_CURRENT_BUFFER_LVALUE->yy_buffer_status == YY_BUFFER_EOF_PENDING )
+		/* don't do the read, it's not guaranteed to return an EOF,
+		 * just force an EOF
+		 */
+		YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars) = 0;
+
+	else
+		{
+			yy_size_t num_to_read =
+			YY_CURRENT_BUFFER_LVALUE->yy_buf_size - number_to_move - 1;
+
+		while ( num_to_read <= 0 )
+			{ /* Not enough room in the buffer - grow it. */
+
+			/* just a shorter name for the current buffer */
+			YY_BUFFER_STATE b = YY_CURRENT_BUFFER_LVALUE;
+
+			int yy_c_buf_p_offset =
+				(int) ((yy_c_buf_p) - b->yy_ch_buf);
+
+			if ( b->yy_is_our_buffer )
+				{
+				yy_size_t new_size = b->yy_buf_size * 2;
+
+				if ( new_size <= 0 )
+					b->yy_buf_size += b->yy_buf_size / 8;
+				else
+					b->yy_buf_size *= 2;
+
+				b->yy_ch_buf = (char *)
+					/* Include room in for 2 EOB chars. */
+					yySMB_REALLOC((void *) b->yy_ch_buf,b->yy_buf_size + 2  );
+				}
+			else
+				/* Can't grow it, we don't own it. */
+				b->yy_ch_buf = 0;
+
+			if ( ! b->yy_ch_buf )
+				YY_FATAL_ERROR(
+				"fatal error - scanner input buffer overflow" );
+
+			(yy_c_buf_p) = &b->yy_ch_buf[yy_c_buf_p_offset];
+
+			num_to_read = YY_CURRENT_BUFFER_LVALUE->yy_buf_size -
+						number_to_move - 1;
+
+			}
+
+		if ( num_to_read > YY_READ_BUF_SIZE )
+			num_to_read = YY_READ_BUF_SIZE;
+
+		/* Read in more data. */
+		YY_INPUT( (&YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[number_to_move]),
+			(yy_n_chars), num_to_read );
+
+		YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars);
+		}
+
+	if ( (yy_n_chars) == 0 )
+		{
+		if ( number_to_move == YY_MORE_ADJ )
+			{
+			ret_val = EOB_ACT_END_OF_FILE;
+			yyrestart(yyin  );
+			}
+
+		else
+			{
+			ret_val = EOB_ACT_LAST_MATCH;
+			YY_CURRENT_BUFFER_LVALUE->yy_buffer_status =
+				YY_BUFFER_EOF_PENDING;
+			}
+		}
+
+	else
+		ret_val = EOB_ACT_CONTINUE_SCAN;
+
+	if ((yy_size_t) ((yy_n_chars) + number_to_move) > YY_CURRENT_BUFFER_LVALUE->yy_buf_size) {
+		/* Extend the array by 50%, plus the number we really need. */
+		yy_size_t new_size = (yy_n_chars) + number_to_move + ((yy_n_chars) >> 1);
+		YY_CURRENT_BUFFER_LVALUE->yy_ch_buf = (char *) yySMB_REALLOC((void *) YY_CURRENT_BUFFER_LVALUE->yy_ch_buf,new_size  );
+		if ( ! YY_CURRENT_BUFFER_LVALUE->yy_ch_buf )
+			YY_FATAL_ERROR( "out of dynamic memory in yy_get_next_buffer()" );
+	}
+
+	(yy_n_chars) += number_to_move;
+	YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] = YY_END_OF_BUFFER_CHAR;
+	YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars) + 1] = YY_END_OF_BUFFER_CHAR;
+
+	(yytext_ptr) = &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[0];
+
+	return ret_val;
+}
+
+/* yy_get_previous_state - get the state just before the EOB char was reached */
+
+    static yy_state_type yy_get_previous_state (void)
+{
+	register yy_state_type yy_current_state;
+	register char *yy_cp;
+    
+	yy_current_state = (yy_start);
+
+	for ( yy_cp = (yytext_ptr) + YY_MORE_ADJ; yy_cp < (yy_c_buf_p); ++yy_cp )
+		{
+		register YY_CHAR yy_c = (*yy_cp ? yy_ec[YY_SC_TO_UI(*yy_cp)] : 1);
+		if ( yy_accept[yy_current_state] )
+			{
+			(yy_last_accepting_state) = yy_current_state;
+			(yy_last_accepting_cpos) = yy_cp;
+			}
+		while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
+			{
+			yy_current_state = (int) yy_def[yy_current_state];
+			if ( yy_current_state >= 57 )
+				yy_c = yy_meta[(unsigned int) yy_c];
+			}
+		yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
+		}
+
+	return yy_current_state;
+}
+
+/* yy_try_NUL_trans - try to make a transition on the NUL character
+ *
+ * synopsis
+ *	next_state = yy_try_NUL_trans( current_state );
+ */
+    static yy_state_type yy_try_NUL_trans  (yy_state_type yy_current_state )
+{
+	register int yy_is_jam;
+    	register char *yy_cp = (yy_c_buf_p);
+
+	register YY_CHAR yy_c = 1;
+	if ( yy_accept[yy_current_state] )
+		{
+		(yy_last_accepting_state) = yy_current_state;
+		(yy_last_accepting_cpos) = yy_cp;
+		}
+	while ( yy_chk[yy_base[yy_current_state] + yy_c] != yy_current_state )
+		{
+		yy_current_state = (int) yy_def[yy_current_state];
+		if ( yy_current_state >= 57 )
+			yy_c = yy_meta[(unsigned int) yy_c];
+		}
+	yy_current_state = yy_nxt[yy_base[yy_current_state] + (unsigned int) yy_c];
+	yy_is_jam = (yy_current_state == 56);
+
+		return yy_is_jam ? 0 : yy_current_state;
+}
+
+#ifndef YY_NO_INPUT
+#ifdef __cplusplus
+    static int yyinput (void)
+#else
+    static int input  (void)
+#endif
+
+{
+	int c;
+    
+	*(yy_c_buf_p) = (yy_hold_char);
+
+	if ( *(yy_c_buf_p) == YY_END_OF_BUFFER_CHAR )
+		{
+		/* yy_c_buf_p now points to the character we want to return.
+		 * If this occurs *before* the EOB characters, then it's a
+		 * valid NUL; if not, then we've hit the end of the buffer.
+		 */
+		if ( (yy_c_buf_p) < &YY_CURRENT_BUFFER_LVALUE->yy_ch_buf[(yy_n_chars)] )
+			/* This was really a NUL. */
+			*(yy_c_buf_p) = '\0';
+
+		else
+			{ /* need more input */
+			yy_size_t offset = (yy_c_buf_p) - (yytext_ptr);
+			++(yy_c_buf_p);
+
+			switch ( yy_get_next_buffer(  ) )
+				{
+				case EOB_ACT_LAST_MATCH:
+					/* This happens because yy_g_n_b()
+					 * sees that we've accumulated a
+					 * token and flags that we need to
+					 * try matching the token before
+					 * proceeding.  But for input(),
+					 * there's no matching to consider.
+					 * So convert the EOB_ACT_LAST_MATCH
+					 * to EOB_ACT_END_OF_FILE.
+					 */
+
+					/* Reset buffer status. */
+					yyrestart(yyin );
+
+					/*FALLTHROUGH*/
+
+				case EOB_ACT_END_OF_FILE:
+					{
+					if ( yywrap( ) )
+						return EOF;
+
+					if ( ! (yy_did_buffer_switch_on_eof) )
+						YY_NEW_FILE;
+#ifdef __cplusplus
+					return yyinput();
+#else
+					return input();
+#endif
+					}
+
+				case EOB_ACT_CONTINUE_SCAN:
+					(yy_c_buf_p) = (yytext_ptr) + offset;
+					break;
+				}
+			}
+		}
+
+	c = *(unsigned char *) (yy_c_buf_p);	/* cast for 8-bit char's */
+	*(yy_c_buf_p) = '\0';	/* preserve yytext */
+	(yy_hold_char) = *++(yy_c_buf_p);
+
+	return c;
+}
+#endif	/* ifndef YY_NO_INPUT */
+
+/** Immediately switch to a different input stream.
+ * @param input_file A readable stream.
+ * 
+ * @note This function does not reset the start condition to @c INITIAL .
+ */
+    void yyrestart  (FILE * input_file )
+{
+    
+	if ( ! YY_CURRENT_BUFFER ){
+        yyensure_buffer_stack ();
+		YY_CURRENT_BUFFER_LVALUE =
+            yy_create_buffer(yyin,YY_BUF_SIZE );
+	}
+
+	yy_init_buffer(YY_CURRENT_BUFFER,input_file );
+	yy_load_buffer_state( );
+}
+
+/** Switch to a different input buffer.
+ * @param new_buffer The new input buffer.
+ * 
+ */
+    void yy_switch_to_buffer  (YY_BUFFER_STATE  new_buffer )
+{
+    
+	/* TODO. We should be able to replace this entire function body
+	 * with
+	 *		yypop_buffer_state();
+	 *		yypush_buffer_state(new_buffer);
+     */
+	yyensure_buffer_stack ();
+	if ( YY_CURRENT_BUFFER == new_buffer )
+		return;
+
+	if ( YY_CURRENT_BUFFER )
+		{
+		/* Flush out information for old buffer. */
+		*(yy_c_buf_p) = (yy_hold_char);
+		YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p);
+		YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars);
+		}
+
+	YY_CURRENT_BUFFER_LVALUE = new_buffer;
+	yy_load_buffer_state( );
+
+	/* We don't actually know whether we did this switch during
+	 * EOF (yywrap()) processing, but the only time this flag
+	 * is looked at is after yywrap() is called, so it's safe
+	 * to go ahead and always set it.
+	 */
+	(yy_did_buffer_switch_on_eof) = 1;
+}
+
+static void yy_load_buffer_state  (void)
+{
+    	(yy_n_chars) = YY_CURRENT_BUFFER_LVALUE->yy_n_chars;
+	(yytext_ptr) = (yy_c_buf_p) = YY_CURRENT_BUFFER_LVALUE->yy_buf_pos;
+	yyin = YY_CURRENT_BUFFER_LVALUE->yy_input_file;
+	(yy_hold_char) = *(yy_c_buf_p);
+}
+
+/** Allocate and initialize an input buffer state.
+ * @param file A readable stream.
+ * @param size The character buffer size in bytes. When in doubt, use @c YY_BUF_SIZE.
+ * 
+ * @return the allocated buffer state.
+ */
+    YY_BUFFER_STATE yy_create_buffer  (FILE * file, int  size )
+{
+	YY_BUFFER_STATE b;
+    
+	b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state )  );
+	if ( ! b )
+		YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
+
+	b->yy_buf_size = size;
+
+	/* yy_ch_buf has to be 2 characters longer than the size given because
+	 * we need to put in 2 end-of-buffer characters.
+	 */
+	b->yy_ch_buf = (char *) yyalloc(b->yy_buf_size + 2  );
+	if ( ! b->yy_ch_buf )
+		YY_FATAL_ERROR( "out of dynamic memory in yy_create_buffer()" );
+
+	b->yy_is_our_buffer = 1;
+
+	yy_init_buffer(b,file );
+
+	return b;
+}
+
+/** Destroy the buffer.
+ * @param b a buffer created with yy_create_buffer()
+ * 
+ */
+    void yy_delete_buffer (YY_BUFFER_STATE  b )
+{
+    
+	if ( ! b )
+		return;
+
+	if ( b == YY_CURRENT_BUFFER ) /* Not sure if we should pop here. */
+		YY_CURRENT_BUFFER_LVALUE = (YY_BUFFER_STATE) 0;
+
+	if ( b->yy_is_our_buffer )
+		yyfree((void *) b->yy_ch_buf  );
+
+	yyfree((void *) b  );
+}
+
+/* Initializes or reinitializes a buffer.
+ * This function is sometimes called more than once on the same buffer,
+ * such as during a yyrestart() or at EOF.
+ */
+    static void yy_init_buffer  (YY_BUFFER_STATE  b, FILE * file )
+
+{
+	int oerrno = errno;
+    
+	yy_flush_buffer(b );
+
+	b->yy_input_file = file;
+	b->yy_fill_buffer = 1;
+
+    /* If b is the current buffer, then yy_init_buffer was _probably_
+     * called from yyrestart() or through yy_get_next_buffer.
+     * In that case, we don't want to reset the lineno or column.
+     */
+    if (b != YY_CURRENT_BUFFER){
+        b->yy_bs_lineno = 1;
+        b->yy_bs_column = 0;
+    }
+
+        b->yy_is_interactive = file ? (isatty( fileno(file) ) > 0) : 0;
+    
+	errno = oerrno;
+}
+
+/** Discard all buffered characters. On the next scan, YY_INPUT will be called.
+ * @param b the buffer state to be flushed, usually @c YY_CURRENT_BUFFER.
+ * 
+ */
+    void yy_flush_buffer (YY_BUFFER_STATE  b )
+{
+    	if ( ! b )
+		return;
+
+	b->yy_n_chars = 0;
+
+	/* We always need two end-of-buffer characters.  The first causes
+	 * a transition to the end-of-buffer state.  The second causes
+	 * a jam in that state.
+	 */
+	b->yy_ch_buf[0] = YY_END_OF_BUFFER_CHAR;
+	b->yy_ch_buf[1] = YY_END_OF_BUFFER_CHAR;
+
+	b->yy_buf_pos = &b->yy_ch_buf[0];
+
+	b->yy_at_bol = 1;
+	b->yy_buffer_status = YY_BUFFER_NEW;
+
+	if ( b == YY_CURRENT_BUFFER )
+		yy_load_buffer_state( );
+}
+
+/** Pushes the new state onto the stack. The new state becomes
+ *  the current state. This function will allocate the stack
+ *  if necessary.
+ *  @param new_buffer The new state.
+ *  
+ */
+void yypush_buffer_state (YY_BUFFER_STATE new_buffer )
+{
+    	if (new_buffer == NULL)
+		return;
+
+	yyensure_buffer_stack();
+
+	/* This block is copied from yy_switch_to_buffer. */
+	if ( YY_CURRENT_BUFFER )
+		{
+		/* Flush out information for old buffer. */
+		*(yy_c_buf_p) = (yy_hold_char);
+		YY_CURRENT_BUFFER_LVALUE->yy_buf_pos = (yy_c_buf_p);
+		YY_CURRENT_BUFFER_LVALUE->yy_n_chars = (yy_n_chars);
+		}
+
+	/* Only push if top exists. Otherwise, replace top. */
+	if (YY_CURRENT_BUFFER)
+		(yy_buffer_stack_top)++;
+	YY_CURRENT_BUFFER_LVALUE = new_buffer;
+
+	/* copied from yy_switch_to_buffer. */
+	yy_load_buffer_state( );
+	(yy_did_buffer_switch_on_eof) = 1;
+}
+
+/** Removes and deletes the top of the stack, if present.
+ *  The next element becomes the new top.
+ *  
+ */
+void yypop_buffer_state (void)
+{
+    	if (!YY_CURRENT_BUFFER)
+		return;
+
+	yy_delete_buffer(YY_CURRENT_BUFFER );
+	YY_CURRENT_BUFFER_LVALUE = NULL;
+	if ((yy_buffer_stack_top) > 0)
+		--(yy_buffer_stack_top);
+
+	if (YY_CURRENT_BUFFER) {
+		yy_load_buffer_state( );
+		(yy_did_buffer_switch_on_eof) = 1;
+	}
+}
+
+/* Allocates the stack if it does not exist.
+ *  Guarantees space for at least one push.
+ */
+static void yyensure_buffer_stack (void)
+{
+	yy_size_t num_to_alloc;
+    
+	if (!(yy_buffer_stack)) {
+
+		/* First allocation is just for 2 elements, since we don't know if this
+		 * scanner will even need a stack. We use 2 instead of 1 to avoid an
+		 * immediate SMB_REALLOC on the next call.
+         */
+		num_to_alloc = 1;
+		(yy_buffer_stack) = (struct yy_buffer_state**)yyalloc
+								(num_to_alloc * sizeof(struct yy_buffer_state*)
+								);
+		if ( ! (yy_buffer_stack) )
+			YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" );
+								  
+		memset((yy_buffer_stack), 0, num_to_alloc * sizeof(struct yy_buffer_state*));
+				
+		(yy_buffer_stack_max) = num_to_alloc;
+		(yy_buffer_stack_top) = 0;
+		return;
+	}
+
+	if ((yy_buffer_stack_top) >= ((yy_buffer_stack_max)) - 1){
+
+		/* Increase the buffer to prepare for a possible push. */
+		int grow_size = 8 /* arbitrary grow size */;
+
+		num_to_alloc = (yy_buffer_stack_max) + grow_size;
+		(yy_buffer_stack) = (struct yy_buffer_state**)yySMB_REALLOC
+								((yy_buffer_stack),
+								num_to_alloc * sizeof(struct yy_buffer_state*)
+								);
+		if ( ! (yy_buffer_stack) )
+			YY_FATAL_ERROR( "out of dynamic memory in yyensure_buffer_stack()" );
+
+		/* zero only the new slots.*/
+		memset((yy_buffer_stack) + (yy_buffer_stack_max), 0, grow_size * sizeof(struct yy_buffer_state*));
+		(yy_buffer_stack_max) = num_to_alloc;
+	}
+}
+
+/** Setup the input buffer state to scan directly from a user-specified character buffer.
+ * @param base the character buffer
+ * @param size the size in bytes of the character buffer
+ * 
+ * @return the newly allocated buffer state object. 
+ */
+YY_BUFFER_STATE yy_scan_buffer  (char * base, yy_size_t  size )
+{
+	YY_BUFFER_STATE b;
+    
+	if ( size < 2 ||
+	     base[size-2] != YY_END_OF_BUFFER_CHAR ||
+	     base[size-1] != YY_END_OF_BUFFER_CHAR )
+		/* They forgot to leave room for the EOB's. */
+		return 0;
+
+	b = (YY_BUFFER_STATE) yyalloc(sizeof( struct yy_buffer_state )  );
+	if ( ! b )
+		YY_FATAL_ERROR( "out of dynamic memory in yy_scan_buffer()" );
+
+	b->yy_buf_size = size - 2;	/* "- 2" to take care of EOB's */
+	b->yy_buf_pos = b->yy_ch_buf = base;
+	b->yy_is_our_buffer = 0;
+	b->yy_input_file = 0;
+	b->yy_n_chars = b->yy_buf_size;
+	b->yy_is_interactive = 0;
+	b->yy_at_bol = 1;
+	b->yy_fill_buffer = 0;
+	b->yy_buffer_status = YY_BUFFER_NEW;
+
+	yy_switch_to_buffer(b  );
+
+	return b;
+}
+
+/** Setup the input buffer state to scan a string. The next call to yylex() will
+ * scan from a @e copy of @a str.
+ * @param yystr a NUL-terminated string to scan
+ * 
+ * @return the newly allocated buffer state object.
+ * @note If you want to scan bytes that may contain NUL values, then use
+ *       yy_scan_bytes() instead.
+ */
+YY_BUFFER_STATE yy_scan_string (yyconst char * yystr )
+{
+    
+	return yy_scan_bytes(yystr,strlen(yystr) );
+}
+
+/** Setup the input buffer state to scan the given bytes. The next call to yylex() will
+ * scan from a @e copy of @a bytes.
+ * @param yybytes the byte buffer to scan
+ * @param _yybytes_len the number of bytes in the buffer pointed to by @a bytes.
+ * 
+ * @return the newly allocated buffer state object.
+ */
+YY_BUFFER_STATE yy_scan_bytes  (yyconst char * yybytes, yy_size_t  _yybytes_len )
+{
+	YY_BUFFER_STATE b;
+	char *buf;
+	yy_size_t n;
+	yy_size_t i;
+    
+	/* Get memory for full buffer, including space for trailing EOB's. */
+	n = _yybytes_len + 2;
+	buf = (char *) yyalloc(n  );
+	if ( ! buf )
+		YY_FATAL_ERROR( "out of dynamic memory in yy_scan_bytes()" );
+
+	for ( i = 0; i < _yybytes_len; ++i )
+		buf[i] = yybytes[i];
+
+	buf[_yybytes_len] = buf[_yybytes_len+1] = YY_END_OF_BUFFER_CHAR;
+
+	b = yy_scan_buffer(buf,n );
+	if ( ! b )
+		YY_FATAL_ERROR( "bad buffer in yy_scan_bytes()" );
+
+	/* It's okay to grow etc. this buffer, and we should throw it
+	 * away when we're done.
+	 */
+	b->yy_is_our_buffer = 1;
+
+	return b;
+}
+
+#ifndef YY_EXIT_FAILURE
+#define YY_EXIT_FAILURE 2
+#endif
+
+static void yy_fatal_error (yyconst char* msg )
+{
+    	(void) fprintf( stderr, "%s\n", msg );
+	exit( YY_EXIT_FAILURE );
+}
+
+/* Redefine yyless() so it works in section 3 code. */
+
+#undef yyless
+#define yyless(n) \
+	do \
+		{ \
+		/* Undo effects of setting up yytext. */ \
+        int yyless_macro_arg = (n); \
+        YY_LESS_LINENO(yyless_macro_arg);\
+		yytext[yyleng] = (yy_hold_char); \
+		(yy_c_buf_p) = yytext + yyless_macro_arg; \
+		(yy_hold_char) = *(yy_c_buf_p); \
+		*(yy_c_buf_p) = '\0'; \
+		yyleng = yyless_macro_arg; \
+		} \
+	while ( 0 )
+
+/* Accessor  methods (get/set functions) to struct members. */
+
+/** Get the current line number.
+ * 
+ */
+int yyget_lineno  (void)
+{
+        
+    return yylineno;
+}
+
+/** Get the input stream.
+ * 
+ */
+FILE *yyget_in  (void)
+{
+        return yyin;
+}
+
+/** Get the output stream.
+ * 
+ */
+FILE *yyget_out  (void)
+{
+        return yyout;
+}
+
+/** Get the length of the current token.
+ * 
+ */
+yy_size_t yyget_leng  (void)
+{
+        return yyleng;
+}
+
+/** Get the current token.
+ * 
+ */
+
+char *yyget_text  (void)
+{
+        return yytext;
+}
+
+/** Set the current line number.
+ * @param line_number
+ * 
+ */
+void yyset_lineno (int  line_number )
+{
+    
+    yylineno = line_number;
+}
+
+/** Set the input stream. This does not discard the current
+ * input buffer.
+ * @param in_str A readable stream.
+ * 
+ * @see yy_switch_to_buffer
+ */
+void yyset_in (FILE *  in_str )
+{
+        yyin = in_str ;
+}
+
+void yyset_out (FILE *  out_str )
+{
+        yyout = out_str ;
+}
+
+int yyget_debug  (void)
+{
+        return yy_flex_debug;
+}
+
+void yyset_debug (int  bdebug )
+{
+        yy_flex_debug = bdebug ;
+}
+
+static int yy_init_globals (void)
+{
+        /* Initialization is the same as for the non-reentrant scanner.
+     * This function is called from yylex_destroy(), so don't allocate here.
+     */
+
+    (yy_buffer_stack) = 0;
+    (yy_buffer_stack_top) = 0;
+    (yy_buffer_stack_max) = 0;
+    (yy_c_buf_p) = (char *) 0;
+    (yy_init) = 0;
+    (yy_start) = 0;
+
+/* Defined in main.c */
+#ifdef YY_STDINIT
+    yyin = stdin;
+    yyout = stdout;
+#else
+    yyin = (FILE *) 0;
+    yyout = (FILE *) 0;
+#endif
+
+    /* For future reference: Set errno on error, since we are called by
+     * yylex_init()
+     */
+    return 0;
+}
+
+/* yylex_destroy is for both reentrant and non-reentrant scanners. */
+int yylex_destroy  (void)
+{
+    
+    /* Pop the buffer stack, destroying each element. */
+	while(YY_CURRENT_BUFFER){
+		yy_delete_buffer(YY_CURRENT_BUFFER  );
+		YY_CURRENT_BUFFER_LVALUE = NULL;
+		yypop_buffer_state();
+	}
+
+	/* Destroy the stack itself. */
+	yyfree((yy_buffer_stack) );
+	(yy_buffer_stack) = NULL;
+
+    /* Reset the globals. This is important in a non-reentrant scanner so the next time
+     * yylex() is called, initialization will occur. */
+    yy_init_globals( );
+
+    return 0;
+}
+
+/*
+ * Internal utility routines.
+ */
+
+#ifndef yytext_ptr
+static void yy_flex_strncpy (char* s1, yyconst char * s2, int n )
+{
+	register int i;
+	for ( i = 0; i < n; ++i )
+		s1[i] = s2[i];
+}
+#endif
+
+#ifdef YY_NEED_STRLEN
+static int yy_flex_strlen (yyconst char * s )
+{
+	register int n;
+	for ( n = 0; s[n]; ++n )
+		;
+
+	return n;
+}
+#endif
+
+void *yyalloc (yy_size_t  size )
+{
+	return (void *) SMB_MALLOC( size );
+}
+
+void *yySMB_REALLOC  (void * ptr, yy_size_t  size )
+{
+	/* The cast to (char *) in the following accommodates both
+	 * implementations that use char* generic pointers, and those
+	 * that use void* generic pointers.  It works with the latter
+	 * because both ANSI C and C++ allow castless assignment from
+	 * any pointer type to void*, and deal with argument conversions
+	 * as though doing an assignment.
+	 */
+	return (void *) SMB_REALLOC( (char *) ptr, size );
+}
+
+void yyfree (void * ptr )
+{
+	free( (char *) ptr );	/* see yySMB_REALLOC() for (char *) cast */
+}
+
+#define YYTABLES_NAME "yytables"
+
+#line 57 "sparql_lexer.l"
+
+
+
diff --git a/source3/rpc_server/mdssvc/sparql_lexer.l b/source3/rpc_server/mdssvc/sparql_lexer.l
new file mode 100644
index 0000000..cbcd358
--- /dev/null
+++ b/source3/rpc_server/mdssvc/sparql_lexer.l
@@ -0,0 +1,57 @@
+/*
+   Unix SMB/CIFS implementation.
+   Main metadata server / Spotlight routines
+
+   Copyright (C) Ralph Boehme			2012-2014
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   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, see <http://www.gnu.org/licenses/>.
+*/
+
+%{
+#include "includes.h"
+#include "sparql_parser.h"
+
+#define YY_NO_INPUT
+%}
+
+%option nounput
+
+ASC     [a-zA-Z0-9_\*\:\-\.]
+U       [\x80-\xbf]
+U2      [\xc2-\xdf]
+U3      [\xe0-\xef]
+U4      [\xf0-\xf4]
+
+UANY    {ASC}|{U2}{U}|{U3}{U}{U}|{U4}{U}{U}{U}
+UONLY   {U2}{U}|{U3}{U}{U}|{U4}{U}{U}{U}
+
+%%
+InRange           return FUNC_INRANGE;
+\$time\.iso       return DATE_ISO;
+false             {yylval.bval = false; return BOOL;}
+true              {yylval.bval = true; return BOOL;}
+\"                return QUOTE;
+\(                return OBRACE;
+\)                return CBRACE;
+\&\&              return AND;
+\|\|              return OR;
+\=\=              return EQUAL;
+\!\=              return UNEQUAL;
+\=                return EQUAL;
+\<                return LT;
+\>                return GT;
+\,                return COMMA;
+{UANY}+           {yylval.sval = talloc_strdup(talloc_tos(), yytext); return WORD;}
+[ \t\n]           /* ignore */
+%%
diff --git a/source3/rpc_server/mdssvc/sparql_parser.c b/source3/rpc_server/mdssvc/sparql_parser.c
new file mode 100644
index 0000000..8170290
--- /dev/null
+++ b/source3/rpc_server/mdssvc/sparql_parser.c
@@ -0,0 +1,1973 @@
+/* A Bison parser, made by GNU Bison 3.0.2.  */
+
+/* Bison implementation for Yacc-like parsers in C
+
+   Copyright (C) 1984, 1989-1990, 2000-2013 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   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, see <http://www.gnu.org/licenses/>.  */
+
+/* As a special exception, you may create a larger work that contains
+   part or all of the Bison parser skeleton and distribute that work
+   under terms of your choice, so long as that work isn't itself a
+   parser generator using the skeleton or a modified version thereof
+   as a parser skeleton.  Alternatively, if you modify or redistribute
+   the parser skeleton itself, you may (at your option) remove this
+   special exception, which will cause the skeleton and the resulting
+   Bison output files to be licensed under the GNU General Public
+   License without this special exception.
+
+   This special exception was added by the Free Software Foundation in
+   version 2.2 of Bison.  */
+
+/* C LALR(1) parser skeleton written by Richard Stallman, by
+   simplifying the original so-called "semantic" parser.  */
+
+/* All symbols defined below should begin with yy or YY, to avoid
+   infringing on user name space.  This should be done even for local
+   variables, as they might otherwise be expanded by user macros.
+   There are some unavoidable exceptions within include files to
+   define necessary library symbols; they are noted "INFRINGES ON
+   USER NAME SPACE" below.  */
+
+/* Identify Bison output.  */
+#define YYBISON 1
+
+/* Bison version.  */
+#define YYBISON_VERSION "3.0.2"
+
+/* Skeleton name.  */
+#define YYSKELETON_NAME "yacc.c"
+
+/* Pure parsers.  */
+#define YYPURE 0
+
+/* Push parsers.  */
+#define YYPUSH 0
+
+/* Pull parsers.  */
+#define YYPULL 1
+
+
+
+
+/* Copy the first part of user declarations.  */
+#line 21 "sparql_parser.y" /* yacc.c:339  */
+
+	#include "includes.h"
+	#include "mdssvc.h"
+	#include "sparql_parser.h"
+	#include "sparql_mapping.h"
+
+	#define YYMALLOC SMB_MALLOC
+	#define YYREALLOC SMB_REALLOC
+
+	struct yy_buffer_state;
+	typedef struct yy_buffer_state *YY_BUFFER_STATE;
+	extern int yylex (void);
+	extern void yyerror (char const *);
+	extern void *yyterminate(void);
+	extern YY_BUFFER_STATE yy_scan_string( const char *str);
+	extern void yy_delete_buffer ( YY_BUFFER_STATE buffer );
+
+	/* forward declarations */
+	static const char *map_expr(const char *attr, char op, const char *val);
+	static const char *map_daterange(const char *dateattr,
+					 time_t date1, time_t date2);
+	static time_t isodate2unix(const char *s);
+
+	/* global vars, eg needed by the lexer */
+	struct sparql_parser_state {
+		TALLOC_CTX *frame;
+		YY_BUFFER_STATE s;
+		char var;
+		const char *result;
+	} *global_sparql_parser_state;
+
+#line 98 "sparql_parser.c" /* yacc.c:339  */
+
+# ifndef YY_NULLPTR
+#  if defined __cplusplus && 201103L <= __cplusplus
+#   define YY_NULLPTR nullptr
+#  else
+#   define YY_NULLPTR 0
+#  endif
+# endif
+
+/* Enabling verbose error messages.  */
+#ifdef YYERROR_VERBOSE
+# undef YYERROR_VERBOSE
+# define YYERROR_VERBOSE 1
+#else
+# define YYERROR_VERBOSE 1
+#endif
+
+/* In a future release of Bison, this section will be replaced
+   by #include "sparql_parser.h".  */
+#ifndef YY_YY_SPARQL_PARSER_H_INCLUDED
+# define YY_YY_SPARQL_PARSER_H_INCLUDED
+/* Debug traces.  */
+#ifndef YYDEBUG
+# define YYDEBUG 0
+#endif
+#if YYDEBUG
+extern int yydebug;
+#endif
+
+/* Token type.  */
+#ifndef YYTOKENTYPE
+# define YYTOKENTYPE
+  enum yytokentype
+  {
+    WORD = 258,
+    BOOL = 259,
+    FUNC_INRANGE = 260,
+    DATE_ISO = 261,
+    OBRACE = 262,
+    CBRACE = 263,
+    EQUAL = 264,
+    UNEQUAL = 265,
+    GT = 266,
+    LT = 267,
+    COMMA = 268,
+    QUOTE = 269,
+    AND = 270,
+    OR = 271
+  };
+#endif
+
+/* Value type.  */
+#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
+typedef union YYSTYPE YYSTYPE;
+union YYSTYPE
+{
+#line 61 "sparql_parser.y" /* yacc.c:355  */
+
+	int ival;
+	const char *sval;
+	bool bval;
+	time_t tval;
+
+#line 162 "sparql_parser.c" /* yacc.c:355  */
+};
+# define YYSTYPE_IS_TRIVIAL 1
+# define YYSTYPE_IS_DECLARED 1
+#endif
+
+
+extern YYSTYPE yylval;
+
+int yyparse (void);
+/* "%code provides" blocks.  */
+#line 53 "sparql_parser.y" /* yacc.c:355  */
+
+	#include <stdbool.h>
+	#include "mdssvc.h"
+	#define SPRAW_TIME_OFFSET 978307200
+	extern int yywrap(void);
+	extern bool map_spotlight_to_sparql_query(struct sl_query *slq);
+
+#line 181 "sparql_parser.c" /* yacc.c:355  */
+
+#endif /* !YY_YY_SPARQL_PARSER_H_INCLUDED  */
+
+/* Copy the second part of user declarations.  */
+
+#line 187 "sparql_parser.c" /* yacc.c:358  */
+
+#ifdef short
+# undef short
+#endif
+
+#ifdef YYTYPE_UINT8
+typedef YYTYPE_UINT8 yytype_uint8;
+#else
+typedef unsigned char yytype_uint8;
+#endif
+
+#ifdef YYTYPE_INT8
+typedef YYTYPE_INT8 yytype_int8;
+#else
+typedef signed char yytype_int8;
+#endif
+
+#ifdef YYTYPE_UINT16
+typedef YYTYPE_UINT16 yytype_uint16;
+#else
+typedef unsigned short int yytype_uint16;
+#endif
+
+#ifdef YYTYPE_INT16
+typedef YYTYPE_INT16 yytype_int16;
+#else
+typedef short int yytype_int16;
+#endif
+
+#ifndef YYSIZE_T
+# ifdef __SIZE_TYPE__
+#  define YYSIZE_T __SIZE_TYPE__
+# elif defined size_t
+#  define YYSIZE_T size_t
+# elif ! defined YYSIZE_T
+#  include <stddef.h> /* INFRINGES ON USER NAME SPACE */
+#  define YYSIZE_T size_t
+# else
+#  define YYSIZE_T unsigned int
+# endif
+#endif
+
+#define YYSIZE_MAXIMUM ((YYSIZE_T) -1)
+
+#ifndef YY_
+# if defined YYENABLE_NLS && YYENABLE_NLS
+#  if ENABLE_NLS
+#   include <libintl.h> /* INFRINGES ON USER NAME SPACE */
+#   define YY_(Msgid) dgettext ("bison-runtime", Msgid)
+#  endif
+# endif
+# ifndef YY_
+#  define YY_(Msgid) Msgid
+# endif
+#endif
+
+#ifndef YY_ATTRIBUTE
+# if (defined __GNUC__                                               \
+      && (2 < __GNUC__ || (__GNUC__ == 2 && 96 <= __GNUC_MINOR__)))  \
+     || defined __SUNPRO_C && 0x5110 <= __SUNPRO_C
+#  define YY_ATTRIBUTE(Spec) __attribute__(Spec)
+# else
+#  define YY_ATTRIBUTE(Spec) /* empty */
+# endif
+#endif
+
+#ifndef YY_ATTRIBUTE_PURE
+# define YY_ATTRIBUTE_PURE   YY_ATTRIBUTE ((__pure__))
+#endif
+
+#ifndef YY_ATTRIBUTE_UNUSED
+# define YY_ATTRIBUTE_UNUSED YY_ATTRIBUTE ((__unused__))
+#endif
+
+#if !defined _Noreturn \
+     && (!defined __STDC_VERSION__ || __STDC_VERSION__ < 201112)
+# if defined _MSC_VER && 1200 <= _MSC_VER
+#  define _Noreturn __declspec (noreturn)
+# else
+#  define _Noreturn YY_ATTRIBUTE ((__noreturn__))
+# endif
+#endif
+
+/* Suppress unused-variable warnings by "using" E.  */
+#if ! defined lint || defined __GNUC__
+# define YYUSE(E) ((void) (E))
+#else
+# define YYUSE(E) /* empty */
+#endif
+
+#if defined __GNUC__ && 407 <= __GNUC__ * 100 + __GNUC_MINOR__
+/* Suppress an incorrect diagnostic about yylval being uninitialized.  */
+# define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \
+    _Pragma ("GCC diagnostic push") \
+    _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"")\
+    _Pragma ("GCC diagnostic ignored \"-Wmaybe-uninitialized\"")
+# define YY_IGNORE_MAYBE_UNINITIALIZED_END \
+    _Pragma ("GCC diagnostic pop")
+#else
+# define YY_INITIAL_VALUE(Value) Value
+#endif
+#ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
+# define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
+# define YY_IGNORE_MAYBE_UNINITIALIZED_END
+#endif
+#ifndef YY_INITIAL_VALUE
+# define YY_INITIAL_VALUE(Value) /* Nothing. */
+#endif
+
+
+#if ! defined yyoverflow || YYERROR_VERBOSE
+
+/* The parser invokes alloca or malloc; define the necessary symbols.  */
+
+# ifdef YYSTACK_USE_ALLOCA
+#  if YYSTACK_USE_ALLOCA
+#   ifdef __GNUC__
+#    define YYSTACK_ALLOC __builtin_alloca
+#   elif defined __BUILTIN_VA_ARG_INCR
+#    include <alloca.h> /* INFRINGES ON USER NAME SPACE */
+#   elif defined _AIX
+#    define YYSTACK_ALLOC __alloca
+#   elif defined _MSC_VER
+#    include <malloc.h> /* INFRINGES ON USER NAME SPACE */
+#    define alloca _alloca
+#   else
+#    define YYSTACK_ALLOC alloca
+#    if ! defined _ALLOCA_H && ! defined EXIT_SUCCESS
+#     include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
+      /* Use EXIT_SUCCESS as a witness for stdlib.h.  */
+#     ifndef EXIT_SUCCESS
+#      define EXIT_SUCCESS 0
+#     endif
+#    endif
+#   endif
+#  endif
+# endif
+
+# ifdef YYSTACK_ALLOC
+   /* Pacify GCC's 'empty if-body' warning.  */
+#  define YYSTACK_FREE(Ptr) do { /* empty */; } while (0)
+#  ifndef YYSTACK_ALLOC_MAXIMUM
+    /* The OS might guarantee only one guard page at the bottom of the stack,
+       and a page size can be as small as 4096 bytes.  So we cannot safely
+       invoke alloca (N) if N exceeds 4096.  Use a slightly smaller number
+       to allow for a few compiler-allocated temporary stack slots.  */
+#   define YYSTACK_ALLOC_MAXIMUM 4032 /* reasonable circa 2006 */
+#  endif
+# else
+#  define YYSTACK_ALLOC YYMALLOC
+#  define YYSTACK_FREE YYFREE
+#  ifndef YYSTACK_ALLOC_MAXIMUM
+#   define YYSTACK_ALLOC_MAXIMUM YYSIZE_MAXIMUM
+#  endif
+#  if (defined __cplusplus && ! defined EXIT_SUCCESS \
+       && ! ((defined YYMALLOC || defined malloc) \
+             && (defined YYFREE || defined free)))
+#   include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
+#   ifndef EXIT_SUCCESS
+#    define EXIT_SUCCESS 0
+#   endif
+#  endif
+#  ifndef YYMALLOC
+#   define YYMALLOC malloc
+#   if ! defined malloc && ! defined EXIT_SUCCESS
+void *malloc (YYSIZE_T); /* INFRINGES ON USER NAME SPACE */
+#   endif
+#  endif
+#  ifndef YYFREE
+#   define YYFREE free
+#   if ! defined free && ! defined EXIT_SUCCESS
+void free (void *); /* INFRINGES ON USER NAME SPACE */
+#   endif
+#  endif
+# endif
+#endif /* ! defined yyoverflow || YYERROR_VERBOSE */
+
+
+#if (! defined yyoverflow \
+     && (! defined __cplusplus \
+         || (defined YYSTYPE_IS_TRIVIAL && YYSTYPE_IS_TRIVIAL)))
+
+/* A type that is properly aligned for any stack member.  */
+union yyalloc
+{
+  yytype_int16 yyss_alloc;
+  YYSTYPE yyvs_alloc;
+};
+
+/* The size of the maximum gap between one aligned stack and the next.  */
+# define YYSTACK_GAP_MAXIMUM (sizeof (union yyalloc) - 1)
+
+/* The size of an array large to enough to hold all stacks, each with
+   N elements.  */
+# define YYSTACK_BYTES(N) \
+     ((N) * (sizeof (yytype_int16) + sizeof (YYSTYPE)) \
+      + YYSTACK_GAP_MAXIMUM)
+
+# define YYCOPY_NEEDED 1
+
+/* Relocate STACK from its old location to the new one.  The
+   local variables YYSIZE and YYSTACKSIZE give the old and new number of
+   elements in the stack, and YYPTR gives the new location of the
+   stack.  Advance YYPTR to a properly aligned location for the next
+   stack.  */
+# define YYSTACK_RELOCATE(Stack_alloc, Stack)                           \
+    do                                                                  \
+      {                                                                 \
+        YYSIZE_T yynewbytes;                                            \
+        YYCOPY (&yyptr->Stack_alloc, Stack, yysize);                    \
+        Stack = &yyptr->Stack_alloc;                                    \
+        yynewbytes = yystacksize * sizeof (*Stack) + YYSTACK_GAP_MAXIMUM; \
+        yyptr += yynewbytes / sizeof (*yyptr);                          \
+      }                                                                 \
+    while (0)
+
+#endif
+
+#if defined YYCOPY_NEEDED && YYCOPY_NEEDED
+/* Copy COUNT objects from SRC to DST.  The source and destination do
+   not overlap.  */
+# ifndef YYCOPY
+#  if defined __GNUC__ && 1 < __GNUC__
+#   define YYCOPY(Dst, Src, Count) \
+      __builtin_memcpy (Dst, Src, (Count) * sizeof (*(Src)))
+#  else
+#   define YYCOPY(Dst, Src, Count)              \
+      do                                        \
+        {                                       \
+          YYSIZE_T yyi;                         \
+          for (yyi = 0; yyi < (Count); yyi++)   \
+            (Dst)[yyi] = (Src)[yyi];            \
+        }                                       \
+      while (0)
+#  endif
+# endif
+#endif /* !YYCOPY_NEEDED */
+
+/* YYFINAL -- State number of the termination state.  */
+#define YYFINAL  2
+/* YYLAST -- Last index in YYTABLE.  */
+#define YYLAST   52
+
+/* YYNTOKENS -- Number of terminals.  */
+#define YYNTOKENS  17
+/* YYNNTS -- Number of nonterminals.  */
+#define YYNNTS  7
+/* YYNRULES -- Number of rules.  */
+#define YYNRULES  22
+/* YYNSTATES -- Number of states.  */
+#define YYNSTATES  51
+
+/* YYTRANSLATE[YYX] -- Symbol number corresponding to YYX as returned
+   by yylex, with out-of-bounds checking.  */
+#define YYUNDEFTOK  2
+#define YYMAXUTOK   271
+
+#define YYTRANSLATE(YYX)                                                \
+  ((unsigned int) (YYX) <= YYMAXUTOK ? yytranslate[YYX] : YYUNDEFTOK)
+
+/* YYTRANSLATE[TOKEN-NUM] -- Symbol number corresponding to TOKEN-NUM
+   as returned by yylex, without out-of-bounds checking.  */
+static const yytype_uint8 yytranslate[] =
+{
+       0,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     2,     2,     2,     2,
+       2,     2,     2,     2,     2,     2,     1,     2,     3,     4,
+       5,     6,     7,     8,     9,    10,    11,    12,    13,    14,
+      15,    16
+};
+
+#if YYDEBUG
+  /* YYRLINE[YYN] -- Source line where rule number YYN was defined.  */
+static const yytype_uint8 yyrline[] =
+{
+       0,    83,    83,    85,    89,    95,   118,   125,   128,   131,
+     134,   137,   147,   151,   155,   159,   163,   167,   171,   175,
+     182,   189,   190
+};
+#endif
+
+#if YYDEBUG || YYERROR_VERBOSE || 1
+/* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
+   First, the terminals, then, starting at YYNTOKENS, nonterminals.  */
+static const char *const yytname[] =
+{
+  "$end", "error", "$undefined", "WORD", "BOOL", "FUNC_INRANGE",
+  "DATE_ISO", "OBRACE", "CBRACE", "EQUAL", "UNEQUAL", "GT", "LT", "COMMA",
+  "QUOTE", "AND", "OR", "$accept", "input", "line", "expr", "match",
+  "function", "date", YY_NULLPTR
+};
+#endif
+
+# ifdef YYPRINT
+/* YYTOKNUM[NUM] -- (External) token number corresponding to the
+   (internal) symbol number NUM (which must be that of a token).  */
+static const yytype_uint16 yytoknum[] =
+{
+       0,   256,   257,   258,   259,   260,   261,   262,   263,   264,
+     265,   266,   267,   268,   269,   270,   271
+};
+# endif
+
+#define YYPACT_NINF -10
+
+#define yypact_value_is_default(Yystate) \
+  (!!((Yystate) == (-10)))
+
+#define YYTABLE_NINF -1
+
+#define yytable_value_is_error(Yytable_value) \
+  0
+
+  /* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
+     STATE-NUM.  */
+static const yytype_int8 yypact[] =
+{
+     -10,    10,   -10,     9,   -10,    -2,    -1,   -10,     8,    -9,
+     -10,     2,    12,    13,    14,    26,    -7,    -1,    -1,    27,
+      28,    29,    30,    31,    22,   -10,    20,   -10,   -10,    23,
+      24,    25,    32,    19,    37,    38,    39,    40,   -10,    41,
+      34,   -10,   -10,   -10,   -10,    42,    19,    36,    43,   -10,
+     -10
+};
+
+  /* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM.
+     Performed when YYTABLE does not specify something else to do.  Zero
+     means the default is an error.  */
+static const yytype_uint8 yydefact[] =
+{
+       2,     0,     1,     0,     5,     0,     0,     3,     4,     7,
+       8,     0,     0,     0,     0,     0,     0,     0,     0,     0,
+       0,     0,     0,     0,     0,     9,    10,    11,     6,     0,
+       0,     0,     0,     0,    12,    13,    15,    14,    22,     0,
+       0,    16,    17,    19,    18,     0,     0,     0,     0,    21,
+      20
+};
+
+  /* YYPGOTO[NTERM-NUM].  */
+static const yytype_int8 yypgoto[] =
+{
+     -10,   -10,   -10,    -6,    33,   -10,     3
+};
+
+  /* YYDEFGOTO[NTERM-NUM].  */
+static const yytype_int8 yydefgoto[] =
+{
+      -1,     1,     7,     8,     9,    10,    40
+};
+
+  /* YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM.  If
+     positive, shift that token.  If negative, reduce the rule whose
+     number is the opposite.  If YYTABLE_NINF, syntax error.  */
+static const yytype_uint8 yytable[] =
+{
+      16,    25,     3,     4,     5,    15,     6,    19,    17,    18,
+       2,    26,    27,     3,     4,     5,    20,     6,    11,    12,
+      13,    14,    38,    17,    18,    39,    21,    22,    23,    24,
+       3,    29,    30,    31,    32,    33,    18,    34,    35,    36,
+      41,    42,    43,    44,    49,    47,    37,    46,    45,    48,
+       0,    50,    28
+};
+
+static const yytype_int8 yycheck[] =
+{
+       6,     8,     3,     4,     5,     7,     7,    16,    15,    16,
+       0,    17,    18,     3,     4,     5,    14,     7,     9,    10,
+      11,    12,     3,    15,    16,     6,    14,    14,    14,     3,
+       3,     3,     3,     3,     3,    13,    16,    14,    14,    14,
+       3,     3,     3,     3,     8,     3,    14,    13,     7,    46,
+      -1,     8,    19
+};
+
+  /* YYSTOS[STATE-NUM] -- The (internal number of the) accessing
+     symbol of state STATE-NUM.  */
+static const yytype_uint8 yystos[] =
+{
+       0,    18,     0,     3,     4,     5,     7,    19,    20,    21,
+      22,     9,    10,    11,    12,     7,    20,    15,    16,    16,
+      14,    14,    14,    14,     3,     8,    20,    20,    21,     3,
+       3,     3,     3,    13,    14,    14,    14,    14,     3,     6,
+      23,     3,     3,     3,     3,     7,    13,     3,    23,     8,
+       8
+};
+
+  /* YYR1[YYN] -- Symbol number of symbol that rule YYN derives.  */
+static const yytype_uint8 yyr1[] =
+{
+       0,    17,    18,    18,    19,    20,    20,    20,    20,    20,
+      20,    20,    21,    21,    21,    21,    21,    21,    21,    21,
+      22,    23,    23
+};
+
+  /* YYR2[YYN] -- Number of symbols on the right hand side of rule YYN.  */
+static const yytype_uint8 yyr2[] =
+{
+       0,     2,     0,     2,     1,     1,     3,     1,     1,     3,
+       3,     3,     5,     5,     5,     5,     6,     6,     6,     6,
+       8,     4,     1
+};
+
+
+#define yyerrok         (yyerrstatus = 0)
+#define yyclearin       (yychar = YYEMPTY)
+#define YYEMPTY         (-2)
+#define YYEOF           0
+
+#define YYACCEPT        goto yyacceptlab
+#define YYABORT         goto yyabortlab
+#define YYERROR         goto yyerrorlab
+
+
+#define YYRECOVERING()  (!!yyerrstatus)
+
+#define YYBACKUP(Token, Value)                                  \
+do                                                              \
+  if (yychar == YYEMPTY)                                        \
+    {                                                           \
+      yychar = (Token);                                         \
+      yylval = (Value);                                         \
+      YYPOPSTACK (yylen);                                       \
+      yystate = *yyssp;                                         \
+      goto yybackup;                                            \
+    }                                                           \
+  else                                                          \
+    {                                                           \
+      yyerror (YY_("syntax error: cannot back up")); \
+      YYERROR;                                                  \
+    }                                                           \
+while (0)
+
+/* Error token number */
+#define YYTERROR        1
+#define YYERRCODE       256
+
+
+
+/* Enable debugging if requested.  */
+#if YYDEBUG
+
+# ifndef YYFPRINTF
+#  include <stdio.h> /* INFRINGES ON USER NAME SPACE */
+#  define YYFPRINTF fprintf
+# endif
+
+# define YYDPRINTF(Args)                        \
+do {                                            \
+  if (yydebug)                                  \
+    YYFPRINTF Args;                             \
+} while (0)
+
+/* This macro is provided for backward compatibility. */
+#ifndef YY_LOCATION_PRINT
+# define YY_LOCATION_PRINT(File, Loc) ((void) 0)
+#endif
+
+
+# define YY_SYMBOL_PRINT(Title, Type, Value, Location)                    \
+do {                                                                      \
+  if (yydebug)                                                            \
+    {                                                                     \
+      YYFPRINTF (stderr, "%s ", Title);                                   \
+      yy_symbol_print (stderr,                                            \
+                  Type, Value); \
+      YYFPRINTF (stderr, "\n");                                           \
+    }                                                                     \
+} while (0)
+
+
+/*----------------------------------------.
+| Print this symbol's value on YYOUTPUT.  |
+`----------------------------------------*/
+
+static void
+yy_symbol_value_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep)
+{
+  FILE *yyo = yyoutput;
+  YYUSE (yyo);
+  if (!yyvaluep)
+    return;
+# ifdef YYPRINT
+  if (yytype < YYNTOKENS)
+    YYPRINT (yyoutput, yytoknum[yytype], *yyvaluep);
+# endif
+  YYUSE (yytype);
+}
+
+
+/*--------------------------------.
+| Print this symbol on YYOUTPUT.  |
+`--------------------------------*/
+
+static void
+yy_symbol_print (FILE *yyoutput, int yytype, YYSTYPE const * const yyvaluep)
+{
+  YYFPRINTF (yyoutput, "%s %s (",
+             yytype < YYNTOKENS ? "token" : "nterm", yytname[yytype]);
+
+  yy_symbol_value_print (yyoutput, yytype, yyvaluep);
+  YYFPRINTF (yyoutput, ")");
+}
+
+/*------------------------------------------------------------------.
+| yy_stack_print -- Print the state stack from its BOTTOM up to its |
+| TOP (included).                                                   |
+`------------------------------------------------------------------*/
+
+static void
+yy_stack_print (yytype_int16 *yybottom, yytype_int16 *yytop)
+{
+  YYFPRINTF (stderr, "Stack now");
+  for (; yybottom <= yytop; yybottom++)
+    {
+      int yybot = *yybottom;
+      YYFPRINTF (stderr, " %d", yybot);
+    }
+  YYFPRINTF (stderr, "\n");
+}
+
+# define YY_STACK_PRINT(Bottom, Top)                            \
+do {                                                            \
+  if (yydebug)                                                  \
+    yy_stack_print ((Bottom), (Top));                           \
+} while (0)
+
+
+/*------------------------------------------------.
+| Report that the YYRULE is going to be reduced.  |
+`------------------------------------------------*/
+
+static void
+yy_reduce_print (yytype_int16 *yyssp, YYSTYPE *yyvsp, int yyrule)
+{
+  unsigned long int yylno = yyrline[yyrule];
+  int yynrhs = yyr2[yyrule];
+  int yyi;
+  YYFPRINTF (stderr, "Reducing stack by rule %d (line %lu):\n",
+             yyrule - 1, yylno);
+  /* The symbols being reduced.  */
+  for (yyi = 0; yyi < yynrhs; yyi++)
+    {
+      YYFPRINTF (stderr, "   $%d = ", yyi + 1);
+      yy_symbol_print (stderr,
+                       yystos[yyssp[yyi + 1 - yynrhs]],
+                       &(yyvsp[(yyi + 1) - (yynrhs)])
+                                              );
+      YYFPRINTF (stderr, "\n");
+    }
+}
+
+# define YY_REDUCE_PRINT(Rule)          \
+do {                                    \
+  if (yydebug)                          \
+    yy_reduce_print (yyssp, yyvsp, Rule); \
+} while (0)
+
+/* Nonzero means print parse trace.  It is left uninitialized so that
+   multiple parsers can coexist.  */
+int yydebug;
+#else /* !YYDEBUG */
+# define YYDPRINTF(Args)
+# define YY_SYMBOL_PRINT(Title, Type, Value, Location)
+# define YY_STACK_PRINT(Bottom, Top)
+# define YY_REDUCE_PRINT(Rule)
+#endif /* !YYDEBUG */
+
+
+/* YYINITDEPTH -- initial size of the parser's stacks.  */
+#ifndef YYINITDEPTH
+# define YYINITDEPTH 200
+#endif
+
+/* YYMAXDEPTH -- maximum size the stacks can grow to (effective only
+   if the built-in stack extension method is used).
+
+   Do not make this value too large; the results are undefined if
+   YYSTACK_ALLOC_MAXIMUM < YYSTACK_BYTES (YYMAXDEPTH)
+   evaluated with infinite-precision integer arithmetic.  */
+
+#ifndef YYMAXDEPTH
+# define YYMAXDEPTH 10000
+#endif
+
+
+#if YYERROR_VERBOSE
+
+# ifndef yystrlen
+#  if defined __GLIBC__ && defined _STRING_H
+#   define yystrlen strlen
+#  else
+/* Return the length of YYSTR.  */
+static YYSIZE_T
+yystrlen (const char *yystr)
+{
+  YYSIZE_T yylen;
+  for (yylen = 0; yystr[yylen]; yylen++)
+    continue;
+  return yylen;
+}
+#  endif
+# endif
+
+# ifndef yystpcpy
+#  if defined __GLIBC__ && defined _STRING_H && defined _GNU_SOURCE
+#   define yystpcpy stpcpy
+#  else
+/* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in
+   YYDEST.  */
+static char *
+yystpcpy (char *yydest, const char *yysrc)
+{
+  char *yyd = yydest;
+  const char *yys = yysrc;
+
+  while ((*yyd++ = *yys++) != '\0')
+    continue;
+
+  return yyd - 1;
+}
+#  endif
+# endif
+
+# ifndef yytnamerr
+/* Copy to YYRES the contents of YYSTR after stripping away unnecessary
+   quotes and backslashes, so that it's suitable for yyerror.  The
+   heuristic is that double-quoting is unnecessary unless the string
+   contains an apostrophe, a comma, or backslash (other than
+   backslash-backslash).  YYSTR is taken from yytname.  If YYRES is
+   null, do not copy; instead, return the length of what the result
+   would have been.  */
+static YYSIZE_T
+yytnamerr (char *yyres, const char *yystr)
+{
+  if (*yystr == '"')
+    {
+      YYSIZE_T yyn = 0;
+      char const *yyp = yystr;
+
+      for (;;)
+        switch (*++yyp)
+          {
+          case '\'':
+          case ',':
+            goto do_not_strip_quotes;
+
+          case '\\':
+            if (*++yyp != '\\')
+              goto do_not_strip_quotes;
+            /* Fall through.  */
+          default:
+            if (yyres)
+              yyres[yyn] = *yyp;
+            yyn++;
+            break;
+
+          case '"':
+            if (yyres)
+              yyres[yyn] = '\0';
+            return yyn;
+          }
+    do_not_strip_quotes: ;
+    }
+
+  if (! yyres)
+    return yystrlen (yystr);
+
+  return yystpcpy (yyres, yystr) - yyres;
+}
+# endif
+
+/* Copy into *YYMSG, which is of size *YYMSG_ALLOC, an error message
+   about the unexpected token YYTOKEN for the state stack whose top is
+   YYSSP.
+
+   Return 0 if *YYMSG was successfully written.  Return 1 if *YYMSG is
+   not large enough to hold the message.  In that case, also set
+   *YYMSG_ALLOC to the required number of bytes.  Return 2 if the
+   required number of bytes is too large to store.  */
+static int
+yysyntax_error (YYSIZE_T *yymsg_alloc, char **yymsg,
+                yytype_int16 *yyssp, int yytoken)
+{
+  YYSIZE_T yysize0 = yytnamerr (YY_NULLPTR, yytname[yytoken]);
+  YYSIZE_T yysize = yysize0;
+  enum { YYERROR_VERBOSE_ARGS_MAXIMUM = 5 };
+  /* Internationalized format string. */
+  const char *yyformat = YY_NULLPTR;
+  /* Arguments of yyformat. */
+  char const *yyarg[YYERROR_VERBOSE_ARGS_MAXIMUM];
+  /* Number of reported tokens (one for the "unexpected", one per
+     "expected"). */
+  int yycount = 0;
+
+  /* There are many possibilities here to consider:
+     - If this state is a consistent state with a default action, then
+       the only way this function was invoked is if the default action
+       is an error action.  In that case, don't check for expected
+       tokens because there are none.
+     - The only way there can be no lookahead present (in yychar) is if
+       this state is a consistent state with a default action.  Thus,
+       detecting the absence of a lookahead is sufficient to determine
+       that there is no unexpected or expected token to report.  In that
+       case, just report a simple "syntax error".
+     - Don't assume there isn't a lookahead just because this state is a
+       consistent state with a default action.  There might have been a
+       previous inconsistent state, consistent state with a non-default
+       action, or user semantic action that manipulated yychar.
+     - Of course, the expected token list depends on states to have
+       correct lookahead information, and it depends on the parser not
+       to perform extra reductions after fetching a lookahead from the
+       scanner and before detecting a syntax error.  Thus, state merging
+       (from LALR or IELR) and default reductions corrupt the expected
+       token list.  However, the list is correct for canonical LR with
+       one exception: it will still contain any token that will not be
+       accepted due to an error action in a later state.
+  */
+  if (yytoken != YYEMPTY)
+    {
+      int yyn = yypact[*yyssp];
+      yyarg[yycount++] = yytname[yytoken];
+      if (!yypact_value_is_default (yyn))
+        {
+          /* Start YYX at -YYN if negative to avoid negative indexes in
+             YYCHECK.  In other words, skip the first -YYN actions for
+             this state because they are default actions.  */
+          int yyxbegin = yyn < 0 ? -yyn : 0;
+          /* Stay within bounds of both yycheck and yytname.  */
+          int yychecklim = YYLAST - yyn + 1;
+          int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS;
+          int yyx;
+
+          for (yyx = yyxbegin; yyx < yyxend; ++yyx)
+            if (yycheck[yyx + yyn] == yyx && yyx != YYTERROR
+                && !yytable_value_is_error (yytable[yyx + yyn]))
+              {
+                if (yycount == YYERROR_VERBOSE_ARGS_MAXIMUM)
+                  {
+                    yycount = 1;
+                    yysize = yysize0;
+                    break;
+                  }
+                yyarg[yycount++] = yytname[yyx];
+                {
+                  YYSIZE_T yysize1 = yysize + yytnamerr (YY_NULLPTR, yytname[yyx]);
+                  if (! (yysize <= yysize1
+                         && yysize1 <= YYSTACK_ALLOC_MAXIMUM))
+                    return 2;
+                  yysize = yysize1;
+                }
+              }
+        }
+    }
+
+  switch (yycount)
+    {
+# define YYCASE_(N, S)                      \
+      case N:                               \
+        yyformat = S;                       \
+      break
+      YYCASE_(0, YY_("syntax error"));
+      YYCASE_(1, YY_("syntax error, unexpected %s"));
+      YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s"));
+      YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s"));
+      YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s"));
+      YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s"));
+# undef YYCASE_
+    }
+
+  {
+    YYSIZE_T yysize1 = yysize + yystrlen (yyformat);
+    if (! (yysize <= yysize1 && yysize1 <= YYSTACK_ALLOC_MAXIMUM))
+      return 2;
+    yysize = yysize1;
+  }
+
+  if (*yymsg_alloc < yysize)
+    {
+      *yymsg_alloc = 2 * yysize;
+      if (! (yysize <= *yymsg_alloc
+             && *yymsg_alloc <= YYSTACK_ALLOC_MAXIMUM))
+        *yymsg_alloc = YYSTACK_ALLOC_MAXIMUM;
+      return 1;
+    }
+
+  /* Avoid sprintf, as that infringes on the user's name space.
+     Don't have undefined behavior even if the translation
+     produced a string with the wrong number of "%s"s.  */
+  {
+    char *yyp = *yymsg;
+    int yyi = 0;
+    while ((*yyp = *yyformat) != '\0')
+      if (*yyp == '%' && yyformat[1] == 's' && yyi < yycount)
+        {
+          yyp += yytnamerr (yyp, yyarg[yyi++]);
+          yyformat += 2;
+        }
+      else
+        {
+          yyp++;
+          yyformat++;
+        }
+  }
+  return 0;
+}
+#endif /* YYERROR_VERBOSE */
+
+/*-----------------------------------------------.
+| Release the memory associated to this symbol.  |
+`-----------------------------------------------*/
+
+static void
+yydestruct (const char *yymsg, int yytype, YYSTYPE *yyvaluep)
+{
+  YYUSE (yyvaluep);
+  if (!yymsg)
+    yymsg = "Deleting";
+  YY_SYMBOL_PRINT (yymsg, yytype, yyvaluep, yylocationp);
+
+  YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
+  YYUSE (yytype);
+  YY_IGNORE_MAYBE_UNINITIALIZED_END
+}
+
+
+
+
+/* The lookahead symbol.  */
+int yychar;
+
+/* The semantic value of the lookahead symbol.  */
+YYSTYPE yylval;
+/* Number of syntax errors so far.  */
+int yynerrs;
+
+
+/*----------.
+| yyparse.  |
+`----------*/
+
+int
+yyparse (void)
+{
+    int yystate;
+    /* Number of tokens to shift before error messages enabled.  */
+    int yyerrstatus;
+
+    /* The stacks and their tools:
+       'yyss': related to states.
+       'yyvs': related to semantic values.
+
+       Refer to the stacks through separate pointers, to allow yyoverflow
+       to reallocate them elsewhere.  */
+
+    /* The state stack.  */
+    yytype_int16 yyssa[YYINITDEPTH];
+    yytype_int16 *yyss;
+    yytype_int16 *yyssp;
+
+    /* The semantic value stack.  */
+    YYSTYPE yyvsa[YYINITDEPTH];
+    YYSTYPE *yyvs;
+    YYSTYPE *yyvsp;
+
+    YYSIZE_T yystacksize;
+
+  int yyn;
+  int yyresult;
+  /* Lookahead token as an internal (translated) token number.  */
+  int yytoken = 0;
+  /* The variables used to return semantic value and location from the
+     action routines.  */
+  YYSTYPE yyval;
+
+#if YYERROR_VERBOSE
+  /* Buffer for error messages, and its allocated size.  */
+  char yymsgbuf[128];
+  char *yymsg = yymsgbuf;
+  YYSIZE_T yymsg_alloc = sizeof yymsgbuf;
+#endif
+
+#define YYPOPSTACK(N)   (yyvsp -= (N), yyssp -= (N))
+
+  /* The number of symbols on the RHS of the reduced rule.
+     Keep to zero when no symbol should be popped.  */
+  int yylen = 0;
+
+  yyssp = yyss = yyssa;
+  yyvsp = yyvs = yyvsa;
+  yystacksize = YYINITDEPTH;
+
+  YYDPRINTF ((stderr, "Starting parse\n"));
+
+  yystate = 0;
+  yyerrstatus = 0;
+  yynerrs = 0;
+  yychar = YYEMPTY; /* Cause a token to be read.  */
+  goto yysetstate;
+
+/*------------------------------------------------------------.
+| yynewstate -- Push a new state, which is found in yystate.  |
+`------------------------------------------------------------*/
+ yynewstate:
+  /* In all cases, when you get here, the value and location stacks
+     have just been pushed.  So pushing a state here evens the stacks.  */
+  yyssp++;
+
+ yysetstate:
+  *yyssp = yystate;
+
+  if (yyss + yystacksize - 1 <= yyssp)
+    {
+      /* Get the current used size of the three stacks, in elements.  */
+      YYSIZE_T yysize = yyssp - yyss + 1;
+
+#ifdef yyoverflow
+      {
+        /* Give user a chance to reallocate the stack.  Use copies of
+           these so that the &'s don't force the real ones into
+           memory.  */
+        YYSTYPE *yyvs1 = yyvs;
+        yytype_int16 *yyss1 = yyss;
+
+        /* Each stack pointer address is followed by the size of the
+           data in use in that stack, in bytes.  This used to be a
+           conditional around just the two extra args, but that might
+           be undefined if yyoverflow is a macro.  */
+        yyoverflow (YY_("memory exhausted"),
+                    &yyss1, yysize * sizeof (*yyssp),
+                    &yyvs1, yysize * sizeof (*yyvsp),
+                    &yystacksize);
+
+        yyss = yyss1;
+        yyvs = yyvs1;
+      }
+#else /* no yyoverflow */
+# ifndef YYSTACK_RELOCATE
+      goto yyexhaustedlab;
+# else
+      /* Extend the stack our own way.  */
+      if (YYMAXDEPTH <= yystacksize)
+        goto yyexhaustedlab;
+      yystacksize *= 2;
+      if (YYMAXDEPTH < yystacksize)
+        yystacksize = YYMAXDEPTH;
+
+      {
+        yytype_int16 *yyss1 = yyss;
+        union yyalloc *yyptr =
+          (union yyalloc *) YYSTACK_ALLOC (YYSTACK_BYTES (yystacksize));
+        if (! yyptr)
+          goto yyexhaustedlab;
+        YYSTACK_RELOCATE (yyss_alloc, yyss);
+        YYSTACK_RELOCATE (yyvs_alloc, yyvs);
+#  undef YYSTACK_RELOCATE
+        if (yyss1 != yyssa)
+          YYSTACK_FREE (yyss1);
+      }
+# endif
+#endif /* no yyoverflow */
+
+      yyssp = yyss + yysize - 1;
+      yyvsp = yyvs + yysize - 1;
+
+      YYDPRINTF ((stderr, "Stack size increased to %lu\n",
+                  (unsigned long int) yystacksize));
+
+      if (yyss + yystacksize - 1 <= yyssp)
+        YYABORT;
+    }
+
+  YYDPRINTF ((stderr, "Entering state %d\n", yystate));
+
+  if (yystate == YYFINAL)
+    YYACCEPT;
+
+  goto yybackup;
+
+/*-----------.
+| yybackup.  |
+`-----------*/
+yybackup:
+
+  /* Do appropriate processing given the current state.  Read a
+     lookahead token if we need one and don't already have one.  */
+
+  /* First try to decide what to do without reference to lookahead token.  */
+  yyn = yypact[yystate];
+  if (yypact_value_is_default (yyn))
+    goto yydefault;
+
+  /* Not known => get a lookahead token if don't already have one.  */
+
+  /* YYCHAR is either YYEMPTY or YYEOF or a valid lookahead symbol.  */
+  if (yychar == YYEMPTY)
+    {
+      YYDPRINTF ((stderr, "Reading a token: "));
+      yychar = yylex ();
+    }
+
+  if (yychar <= YYEOF)
+    {
+      yychar = yytoken = YYEOF;
+      YYDPRINTF ((stderr, "Now at end of input.\n"));
+    }
+  else
+    {
+      yytoken = YYTRANSLATE (yychar);
+      YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc);
+    }
+
+  /* If the proper action on seeing token YYTOKEN is to reduce or to
+     detect an error, take that action.  */
+  yyn += yytoken;
+  if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken)
+    goto yydefault;
+  yyn = yytable[yyn];
+  if (yyn <= 0)
+    {
+      if (yytable_value_is_error (yyn))
+        goto yyerrlab;
+      yyn = -yyn;
+      goto yyreduce;
+    }
+
+  /* Count tokens shifted since error; after three, turn off error
+     status.  */
+  if (yyerrstatus)
+    yyerrstatus--;
+
+  /* Shift the lookahead token.  */
+  YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc);
+
+  /* Discard the shifted token.  */
+  yychar = YYEMPTY;
+
+  yystate = yyn;
+  YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
+  *++yyvsp = yylval;
+  YY_IGNORE_MAYBE_UNINITIALIZED_END
+
+  goto yynewstate;
+
+
+/*-----------------------------------------------------------.
+| yydefault -- do the default action for the current state.  |
+`-----------------------------------------------------------*/
+yydefault:
+  yyn = yydefact[yystate];
+  if (yyn == 0)
+    goto yyerrlab;
+  goto yyreduce;
+
+
+/*-----------------------------.
+| yyreduce -- Do a reduction.  |
+`-----------------------------*/
+yyreduce:
+  /* yyn is the number of a rule to reduce with.  */
+  yylen = yyr2[yyn];
+
+  /* If YYLEN is nonzero, implement the default value of the action:
+     '$$ = $1'.
+
+     Otherwise, the following line sets YYVAL to garbage.
+     This behavior is undocumented and Bison
+     users should not rely upon it.  Assigning to YYVAL
+     unconditionally makes the parser a bit smaller, and it avoids a
+     GCC warning that YYVAL may be used uninitialized.  */
+  yyval = yyvsp[1-yylen];
+
+
+  YY_REDUCE_PRINT (yyn);
+  switch (yyn)
+    {
+        case 4:
+#line 89 "sparql_parser.y" /* yacc.c:1646  */
+    {
+	global_sparql_parser_state->result = (yyvsp[0].sval);
+}
+#line 1290 "sparql_parser.c" /* yacc.c:1646  */
+    break;
+
+  case 5:
+#line 95 "sparql_parser.y" /* yacc.c:1646  */
+    {
+	/*
+	 * We can't properly handle these in expressions, fortunately this
+	 * is probably only ever used by OS X as sole element in an
+	 * expression ie "False" (when Finder window selected our share
+	 * but no search string entered yet). Packet traces showed that OS
+	 * X Spotlight server then returns a failure (ie -1) which is what
+	 * we do here too by calling YYABORT.
+	 */
+	YYABORT;
+}
+#line 1306 "sparql_parser.c" /* yacc.c:1646  */
+    break;
+
+  case 6:
+#line 118 "sparql_parser.y" /* yacc.c:1646  */
+    {
+	if (strcmp((yyvsp[-2].sval), (yyvsp[0].sval)) != 0) {
+		(yyval.sval) = talloc_asprintf(talloc_tos(), "{ %s } UNION { %s }", (yyvsp[-2].sval), (yyvsp[0].sval));
+	} else {
+		(yyval.sval) = talloc_asprintf(talloc_tos(), "%s", (yyvsp[-2].sval));
+	}
+}
+#line 1318 "sparql_parser.c" /* yacc.c:1646  */
+    break;
+
+  case 7:
+#line 125 "sparql_parser.y" /* yacc.c:1646  */
+    {
+	(yyval.sval) = (yyvsp[0].sval);
+}
+#line 1326 "sparql_parser.c" /* yacc.c:1646  */
+    break;
+
+  case 8:
+#line 128 "sparql_parser.y" /* yacc.c:1646  */
+    {
+	(yyval.sval) = (yyvsp[0].sval);
+}
+#line 1334 "sparql_parser.c" /* yacc.c:1646  */
+    break;
+
+  case 9:
+#line 131 "sparql_parser.y" /* yacc.c:1646  */
+    {
+	(yyval.sval) = talloc_asprintf(talloc_tos(), "%s", (yyvsp[-1].sval));
+}
+#line 1342 "sparql_parser.c" /* yacc.c:1646  */
+    break;
+
+  case 10:
+#line 134 "sparql_parser.y" /* yacc.c:1646  */
+    {
+	(yyval.sval) = talloc_asprintf(talloc_tos(), "%s . %s", (yyvsp[-2].sval), (yyvsp[0].sval));
+}
+#line 1350 "sparql_parser.c" /* yacc.c:1646  */
+    break;
+
+  case 11:
+#line 137 "sparql_parser.y" /* yacc.c:1646  */
+    {
+	if (strcmp((yyvsp[-2].sval), (yyvsp[0].sval)) != 0) {
+		(yyval.sval) = talloc_asprintf(talloc_tos(), "{ %s } UNION { %s }", (yyvsp[-2].sval), (yyvsp[0].sval));
+	} else {
+		(yyval.sval) = talloc_asprintf(talloc_tos(), "%s", (yyvsp[-2].sval));
+	}
+}
+#line 1362 "sparql_parser.c" /* yacc.c:1646  */
+    break;
+
+  case 12:
+#line 147 "sparql_parser.y" /* yacc.c:1646  */
+    {
+	(yyval.sval) = map_expr((yyvsp[-4].sval), '=', (yyvsp[-1].sval));
+	if ((yyval.sval) == NULL) YYABORT;
+}
+#line 1371 "sparql_parser.c" /* yacc.c:1646  */
+    break;
+
+  case 13:
+#line 151 "sparql_parser.y" /* yacc.c:1646  */
+    {
+	(yyval.sval) = map_expr((yyvsp[-4].sval), '!', (yyvsp[-1].sval));
+	if ((yyval.sval) == NULL) YYABORT;
+}
+#line 1380 "sparql_parser.c" /* yacc.c:1646  */
+    break;
+
+  case 14:
+#line 155 "sparql_parser.y" /* yacc.c:1646  */
+    {
+	(yyval.sval) = map_expr((yyvsp[-4].sval), '<', (yyvsp[-1].sval));
+	if ((yyval.sval) == NULL) YYABORT;
+}
+#line 1389 "sparql_parser.c" /* yacc.c:1646  */
+    break;
+
+  case 15:
+#line 159 "sparql_parser.y" /* yacc.c:1646  */
+    {
+	(yyval.sval) = map_expr((yyvsp[-4].sval), '>', (yyvsp[-1].sval));
+	if ((yyval.sval) == NULL) YYABORT;
+}
+#line 1398 "sparql_parser.c" /* yacc.c:1646  */
+    break;
+
+  case 16:
+#line 163 "sparql_parser.y" /* yacc.c:1646  */
+    {
+	(yyval.sval) = map_expr((yyvsp[-5].sval), '=', (yyvsp[-2].sval));
+	if ((yyval.sval) == NULL) YYABORT;
+}
+#line 1407 "sparql_parser.c" /* yacc.c:1646  */
+    break;
+
+  case 17:
+#line 167 "sparql_parser.y" /* yacc.c:1646  */
+    {
+	(yyval.sval) = map_expr((yyvsp[-5].sval), '!', (yyvsp[-2].sval));
+	if ((yyval.sval) == NULL) YYABORT;
+}
+#line 1416 "sparql_parser.c" /* yacc.c:1646  */
+    break;
+
+  case 18:
+#line 171 "sparql_parser.y" /* yacc.c:1646  */
+    {
+	(yyval.sval) = map_expr((yyvsp[-5].sval), '<', (yyvsp[-2].sval));
+	if ((yyval.sval) == NULL) YYABORT;
+}
+#line 1425 "sparql_parser.c" /* yacc.c:1646  */
+    break;
+
+  case 19:
+#line 175 "sparql_parser.y" /* yacc.c:1646  */
+    {
+	(yyval.sval) = map_expr((yyvsp[-5].sval), '>', (yyvsp[-2].sval));
+	if ((yyval.sval) == NULL) YYABORT;
+}
+#line 1434 "sparql_parser.c" /* yacc.c:1646  */
+    break;
+
+  case 20:
+#line 182 "sparql_parser.y" /* yacc.c:1646  */
+    {
+	(yyval.sval) = map_daterange((yyvsp[-5].sval), (yyvsp[-3].tval), (yyvsp[-1].tval));
+	if ((yyval.sval) == NULL) YYABORT;
+}
+#line 1443 "sparql_parser.c" /* yacc.c:1646  */
+    break;
+
+  case 21:
+#line 189 "sparql_parser.y" /* yacc.c:1646  */
+    {(yyval.tval) = isodate2unix((yyvsp[-1].sval));}
+#line 1449 "sparql_parser.c" /* yacc.c:1646  */
+    break;
+
+  case 22:
+#line 190 "sparql_parser.y" /* yacc.c:1646  */
+    {(yyval.tval) = atoi((yyvsp[0].sval)) + SPRAW_TIME_OFFSET;}
+#line 1455 "sparql_parser.c" /* yacc.c:1646  */
+    break;
+
+
+#line 1459 "sparql_parser.c" /* yacc.c:1646  */
+      default: break;
+    }
+  /* User semantic actions sometimes alter yychar, and that requires
+     that yytoken be updated with the new translation.  We take the
+     approach of translating immediately before every use of yytoken.
+     One alternative is translating here after every semantic action,
+     but that translation would be missed if the semantic action invokes
+     YYABORT, YYACCEPT, or YYERROR immediately after altering yychar or
+     if it invokes YYBACKUP.  In the case of YYABORT or YYACCEPT, an
+     incorrect destructor might then be invoked immediately.  In the
+     case of YYERROR or YYBACKUP, subsequent parser actions might lead
+     to an incorrect destructor call or verbose syntax error message
+     before the lookahead is translated.  */
+  YY_SYMBOL_PRINT ("-> $$ =", yyr1[yyn], &yyval, &yyloc);
+
+  YYPOPSTACK (yylen);
+  yylen = 0;
+  YY_STACK_PRINT (yyss, yyssp);
+
+  *++yyvsp = yyval;
+
+  /* Now 'shift' the result of the reduction.  Determine what state
+     that goes to, based on the state we popped back to and the rule
+     number reduced by.  */
+
+  yyn = yyr1[yyn];
+
+  yystate = yypgoto[yyn - YYNTOKENS] + *yyssp;
+  if (0 <= yystate && yystate <= YYLAST && yycheck[yystate] == *yyssp)
+    yystate = yytable[yystate];
+  else
+    yystate = yydefgoto[yyn - YYNTOKENS];
+
+  goto yynewstate;
+
+
+/*--------------------------------------.
+| yyerrlab -- here on detecting error.  |
+`--------------------------------------*/
+yyerrlab:
+  /* Make sure we have latest lookahead translation.  See comments at
+     user semantic actions for why this is necessary.  */
+  yytoken = yychar == YYEMPTY ? YYEMPTY : YYTRANSLATE (yychar);
+
+  /* If not already recovering from an error, report this error.  */
+  if (!yyerrstatus)
+    {
+      ++yynerrs;
+#if ! YYERROR_VERBOSE
+      yyerror (YY_("syntax error"));
+#else
+# define YYSYNTAX_ERROR yysyntax_error (&yymsg_alloc, &yymsg, \
+                                        yyssp, yytoken)
+      {
+        char const *yymsgp = YY_("syntax error");
+        int yysyntax_error_status;
+        yysyntax_error_status = YYSYNTAX_ERROR;
+        if (yysyntax_error_status == 0)
+          yymsgp = yymsg;
+        else if (yysyntax_error_status == 1)
+          {
+            if (yymsg != yymsgbuf)
+              YYSTACK_FREE (yymsg);
+            yymsg = (char *) YYSTACK_ALLOC (yymsg_alloc);
+            if (!yymsg)
+              {
+                yymsg = yymsgbuf;
+                yymsg_alloc = sizeof yymsgbuf;
+                yysyntax_error_status = 2;
+              }
+            else
+              {
+                yysyntax_error_status = YYSYNTAX_ERROR;
+                yymsgp = yymsg;
+              }
+          }
+        yyerror (yymsgp);
+        if (yysyntax_error_status == 2)
+          goto yyexhaustedlab;
+      }
+# undef YYSYNTAX_ERROR
+#endif
+    }
+
+
+
+  if (yyerrstatus == 3)
+    {
+      /* If just tried and failed to reuse lookahead token after an
+         error, discard it.  */
+
+      if (yychar <= YYEOF)
+        {
+          /* Return failure if at end of input.  */
+          if (yychar == YYEOF)
+            YYABORT;
+        }
+      else
+        {
+          yydestruct ("Error: discarding",
+                      yytoken, &yylval);
+          yychar = YYEMPTY;
+        }
+    }
+
+  /* Else will try to reuse lookahead token after shifting the error
+     token.  */
+  goto yyerrlab1;
+
+
+/*---------------------------------------------------.
+| yyerrorlab -- error raised explicitly by YYERROR.  |
+`---------------------------------------------------*/
+yyerrorlab:
+
+  /* Pacify compilers like GCC when the user code never invokes
+     YYERROR and the label yyerrorlab therefore never appears in user
+     code.  */
+  if (/*CONSTCOND*/ 0)
+     goto yyerrorlab;
+
+  /* Do not reclaim the symbols of the rule whose action triggered
+     this YYERROR.  */
+  YYPOPSTACK (yylen);
+  yylen = 0;
+  YY_STACK_PRINT (yyss, yyssp);
+  yystate = *yyssp;
+  goto yyerrlab1;
+
+
+/*-------------------------------------------------------------.
+| yyerrlab1 -- common code for both syntax error and YYERROR.  |
+`-------------------------------------------------------------*/
+yyerrlab1:
+  yyerrstatus = 3;      /* Each real token shifted decrements this.  */
+
+  for (;;)
+    {
+      yyn = yypact[yystate];
+      if (!yypact_value_is_default (yyn))
+        {
+          yyn += YYTERROR;
+          if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYTERROR)
+            {
+              yyn = yytable[yyn];
+              if (0 < yyn)
+                break;
+            }
+        }
+
+      /* Pop the current state because it cannot handle the error token.  */
+      if (yyssp == yyss)
+        YYABORT;
+
+
+      yydestruct ("Error: popping",
+                  yystos[yystate], yyvsp);
+      YYPOPSTACK (1);
+      yystate = *yyssp;
+      YY_STACK_PRINT (yyss, yyssp);
+    }
+
+  YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
+  *++yyvsp = yylval;
+  YY_IGNORE_MAYBE_UNINITIALIZED_END
+
+
+  /* Shift the error token.  */
+  YY_SYMBOL_PRINT ("Shifting", yystos[yyn], yyvsp, yylsp);
+
+  yystate = yyn;
+  goto yynewstate;
+
+
+/*-------------------------------------.
+| yyacceptlab -- YYACCEPT comes here.  |
+`-------------------------------------*/
+yyacceptlab:
+  yyresult = 0;
+  goto yyreturn;
+
+/*-----------------------------------.
+| yyabortlab -- YYABORT comes here.  |
+`-----------------------------------*/
+yyabortlab:
+  yyresult = 1;
+  goto yyreturn;
+
+#if !defined yyoverflow || YYERROR_VERBOSE
+/*-------------------------------------------------.
+| yyexhaustedlab -- memory exhaustion comes here.  |
+`-------------------------------------------------*/
+yyexhaustedlab:
+  yyerror (YY_("memory exhausted"));
+  yyresult = 2;
+  /* Fall through.  */
+#endif
+
+yyreturn:
+  if (yychar != YYEMPTY)
+    {
+      /* Make sure we have latest lookahead translation.  See comments at
+         user semantic actions for why this is necessary.  */
+      yytoken = YYTRANSLATE (yychar);
+      yydestruct ("Cleanup: discarding lookahead",
+                  yytoken, &yylval);
+    }
+  /* Do not reclaim the symbols of the rule whose action triggered
+     this YYABORT or YYACCEPT.  */
+  YYPOPSTACK (yylen);
+  YY_STACK_PRINT (yyss, yyssp);
+  while (yyssp != yyss)
+    {
+      yydestruct ("Cleanup: popping",
+                  yystos[*yyssp], yyvsp);
+      YYPOPSTACK (1);
+    }
+#ifndef yyoverflow
+  if (yyss != yyssa)
+    YYSTACK_FREE (yyss);
+#endif
+#if YYERROR_VERBOSE
+  if (yymsg != yymsgbuf)
+    YYSTACK_FREE (yymsg);
+#endif
+  return yyresult;
+}
+#line 193 "sparql_parser.y" /* yacc.c:1906  */
+
+
+static time_t isodate2unix(const char *s)
+{
+	struct tm tm;
+	const char *p;
+
+	p = strptime(s, "%Y-%m-%dT%H:%M:%SZ", &tm);
+	if (p == NULL) {
+		return (time_t)-1;
+	}
+	return mktime(&tm);
+}
+
+static const char *map_daterange(const char *dateattr,
+				 time_t date1, time_t date2)
+{
+	struct sparql_parser_state *s = global_sparql_parser_state;
+	int result = 0;
+	char *sparql = NULL;
+	const struct sl_attr_map *p;
+	struct tm *tmp;
+	char buf1[64], buf2[64];
+
+	if (s->var == 'z') {
+		return NULL;
+	}
+
+	tmp = localtime(&date1);
+	if (tmp == NULL) {
+		return NULL;
+	}
+	result = strftime(buf1, sizeof(buf1), "%Y-%m-%dT%H:%M:%SZ", tmp);
+	if (result == 0) {
+		return NULL;
+	}
+
+	tmp = localtime(&date2);
+	if (tmp == NULL) {
+		return NULL;
+	}
+	result = strftime(buf2, sizeof(buf2), "%Y-%m-%dT%H:%M:%SZ", tmp);
+	if (result == 0) {
+		return NULL;
+	}
+
+	p = sl_attr_map_by_spotlight(dateattr);
+	if (p == NULL) {
+		return NULL;
+	}
+
+	sparql = talloc_asprintf(talloc_tos(),
+				 "?obj %s ?%c FILTER (?%c > '%s' && ?%c < '%s')",
+				 p->sparql_attr,
+				 s->var,
+				 s->var,
+				 buf1,
+				 s->var,
+				 buf2);
+	if (sparql == NULL) {
+		return NULL;
+	}
+
+	s->var++;
+	return sparql;
+}
+
+static char *map_type_search(const char *attr, char op, const char *val)
+{
+	char *result = NULL;
+	const char *sparqlAttr;
+	const struct sl_type_map *p;
+
+	p = sl_type_map_by_spotlight(val);
+	if (p == NULL) {
+		return NULL;
+	}
+
+	switch (p->type) {
+	case kMDTypeMapRDF:
+		sparqlAttr = "rdf:type";
+		break;
+	case kMDTypeMapMime:
+		sparqlAttr = "nie:mimeType";
+		break;
+	default:
+		return NULL;
+	}
+
+	result = talloc_asprintf(talloc_tos(), "?obj %s '%s'",
+				 sparqlAttr,
+				 p->sparql_type);
+	if (result == NULL) {
+		return NULL;
+	}
+
+	return result;
+}
+
+static const char *map_expr(const char *attr, char op, const char *val)
+{
+	struct sparql_parser_state *s = global_sparql_parser_state;
+	int result = 0;
+	char *sparql = NULL;
+	const struct sl_attr_map *p;
+	time_t t;
+	struct tm *tmp;
+	char buf1[64];
+	char *q;
+	const char *start;
+
+	if (s->var == 'z') {
+		return NULL;
+	}
+
+	p = sl_attr_map_by_spotlight(attr);
+	if (p == NULL) {
+		return NULL;
+	}
+
+	if ((p->type != ssmt_type) && (p->sparql_attr == NULL)) {
+		yyerror("unsupported Spotlight attribute");
+		return NULL;
+	}
+
+	switch (p->type) {
+	case ssmt_bool:
+		sparql = talloc_asprintf(talloc_tos(), "?obj %s '%s'",
+					 p->sparql_attr, val);
+		if (sparql == NULL) {
+			return NULL;
+		}
+		break;
+
+	case ssmt_num:
+		sparql = talloc_asprintf(talloc_tos(),
+					 "?obj %s ?%c FILTER(?%c %c%c '%s')",
+					 p->sparql_attr,
+					 s->var,
+					 s->var,
+					 op,
+					 /* append '=' to '!' */
+					 op == '!' ? '=' : ' ',
+					 val);
+		if (sparql == NULL) {
+			return NULL;
+		}
+		s->var++;
+		break;
+
+	case ssmt_str:
+		q = talloc_strdup(talloc_tos(), "");
+		if (q == NULL) {
+			return NULL;
+		}
+		start = val;
+		while (*val) {
+			if (*val != '*') {
+				val++;
+				continue;
+			}
+			if (val > start) {
+				q = talloc_strndup_append(q, start, val - start);
+				if (q == NULL) {
+					return NULL;
+				}
+			}
+			q = talloc_strdup_append(q, ".*");
+			if (q == NULL) {
+				return NULL;
+			}
+			val++;
+			start = val;
+		}
+		if (val > start) {
+			q = talloc_strndup_append(q, start, val - start);
+			if (q == NULL) {
+				return NULL;
+			}
+		}
+		sparql = talloc_asprintf(talloc_tos(),
+					 "?obj %s ?%c "
+					 "FILTER(regex(?%c, '^%s$', 'i'))",
+					 p->sparql_attr,
+					 s->var,
+					 s->var,
+					 q);
+		TALLOC_FREE(q);
+		if (sparql == NULL) {
+			return NULL;
+		}
+		s->var++;
+		break;
+
+	case ssmt_fts:
+		sparql = talloc_asprintf(talloc_tos(), "?obj %s '%s'",
+					 p->sparql_attr, val);
+		if (sparql == NULL) {
+			return NULL;
+		}
+		break;
+
+	case ssmt_date:
+		t = atoi(val) + SPRAW_TIME_OFFSET;
+		tmp = localtime(&t);
+		if (tmp == NULL) {
+			return NULL;
+		}
+		result = strftime(buf1, sizeof(buf1),
+				  "%Y-%m-%dT%H:%M:%SZ", tmp);
+		if (result == 0) {
+			return NULL;
+		}
+		sparql = talloc_asprintf(talloc_tos(),
+					 "?obj %s ?%c FILTER(?%c %c '%s')",
+					 p->sparql_attr,
+					 s->var,
+					 s->var,
+					 op,
+					 buf1);
+		if (sparql == NULL) {
+			return NULL;
+		}
+		s->var++;
+		break;
+
+	case ssmt_type:
+		sparql = map_type_search(attr, op, val);
+		if (sparql == NULL) {
+			return NULL;
+		}
+		break;
+
+	default:
+		return NULL;
+	}
+
+	return sparql;
+}
+
+void yyerror(const char *str)
+{
+	DEBUG(1, ("yyerror: %s\n", str));
+}
+
+int yywrap(void)
+{
+	return 1;
+}
+
+/**
+ * Map a Spotlight RAW query string to a SPARQL query string
+ **/
+bool map_spotlight_to_sparql_query(struct sl_query *slq)
+{
+	struct sparql_parser_state s = {
+		.frame = talloc_stackframe(),
+		.var = 'a',
+	};
+	int result;
+
+	s.s = yy_scan_string(slq->query_string);
+	if (s.s == NULL) {
+		TALLOC_FREE(s.frame);
+		return false;
+	}
+	global_sparql_parser_state = &s;
+	result = yyparse();
+	global_sparql_parser_state = NULL;
+	yy_delete_buffer(s.s);
+
+	if (result != 0) {
+		TALLOC_FREE(s.frame);
+		return false;
+	}
+
+	slq->sparql_query = talloc_asprintf(slq,
+		"SELECT ?url WHERE { %s . ?obj nie:url ?url . "
+		"FILTER(tracker:uri-is-descendant('file://%s/', ?url)) }",
+		s.result, slq->path_scope);
+	TALLOC_FREE(s.frame);
+	if (slq->sparql_query == NULL) {
+		return false;
+	}
+
+	return true;
+}
diff --git a/source3/rpc_server/mdssvc/sparql_parser.h b/source3/rpc_server/mdssvc/sparql_parser.h
new file mode 100644
index 0000000..943ad9a
--- /dev/null
+++ b/source3/rpc_server/mdssvc/sparql_parser.h
@@ -0,0 +1,98 @@
+/* A Bison parser, made by GNU Bison 3.0.2.  */
+
+/* Bison interface for Yacc-like parsers in C
+
+   Copyright (C) 1984, 1989-1990, 2000-2013 Free Software Foundation, Inc.
+
+   This program is free software: you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation, either version 3 of the License, or
+   (at your option) any later version.
+
+   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, see <http://www.gnu.org/licenses/>.  */
+
+/* As a special exception, you may create a larger work that contains
+   part or all of the Bison parser skeleton and distribute that work
+   under terms of your choice, so long as that work isn't itself a
+   parser generator using the skeleton or a modified version thereof
+   as a parser skeleton.  Alternatively, if you modify or redistribute
+   the parser skeleton itself, you may (at your option) remove this
+   special exception, which will cause the skeleton and the resulting
+   Bison output files to be licensed under the GNU General Public
+   License without this special exception.
+
+   This special exception was added by the Free Software Foundation in
+   version 2.2 of Bison.  */
+
+#ifndef YY_YY_SPARQL_PARSER_H_INCLUDED
+# define YY_YY_SPARQL_PARSER_H_INCLUDED
+/* Debug traces.  */
+#ifndef YYDEBUG
+# define YYDEBUG 0
+#endif
+#if YYDEBUG
+extern int yydebug;
+#endif
+
+/* Token type.  */
+#ifndef YYTOKENTYPE
+# define YYTOKENTYPE
+  enum yytokentype
+  {
+    WORD = 258,
+    BOOL = 259,
+    FUNC_INRANGE = 260,
+    DATE_ISO = 261,
+    OBRACE = 262,
+    CBRACE = 263,
+    EQUAL = 264,
+    UNEQUAL = 265,
+    GT = 266,
+    LT = 267,
+    COMMA = 268,
+    QUOTE = 269,
+    AND = 270,
+    OR = 271
+  };
+#endif
+
+/* Value type.  */
+#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
+typedef union YYSTYPE YYSTYPE;
+union YYSTYPE
+{
+#line 61 "sparql_parser.y" /* yacc.c:1909  */
+
+	int ival;
+	const char *sval;
+	bool bval;
+	time_t tval;
+
+#line 78 "sparql_parser.h" /* yacc.c:1909  */
+};
+# define YYSTYPE_IS_TRIVIAL 1
+# define YYSTYPE_IS_DECLARED 1
+#endif
+
+
+extern YYSTYPE yylval;
+
+int yyparse (void);
+/* "%code provides" blocks.  */
+#line 53 "sparql_parser.y" /* yacc.c:1909  */
+
+	#include <stdbool.h>
+	#include "mdssvc.h"
+	#define SPRAW_TIME_OFFSET 978307200
+	extern int yywrap(void);
+	extern bool map_spotlight_to_sparql_query(struct sl_query *slq);
+
+#line 97 "sparql_parser.h" /* yacc.c:1909  */
+
+#endif /* !YY_YY_SPARQL_PARSER_H_INCLUDED  */
diff --git a/source3/rpc_server/mdssvc/sparql_parser.y b/source3/rpc_server/mdssvc/sparql_parser.y
new file mode 100644
index 0000000..7a151dc
--- /dev/null
+++ b/source3/rpc_server/mdssvc/sparql_parser.y
@@ -0,0 +1,479 @@
+/*
+   Unix SMB/CIFS implementation.
+   Main metadata server / Spotlight routines
+
+   Copyright (C) Ralph Boehme 2012-2014
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   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, see <http://www.gnu.org/licenses/>.
+*/
+
+%{
+	#include "includes.h"
+	#include "mdssvc.h"
+	#include "sparql_parser.h"
+	#include "sparql_mapping.h"
+
+	#define YYMALLOC SMB_MALLOC
+	#define YYREALLOC SMB_REALLOC
+
+	struct yy_buffer_state;
+	typedef struct yy_buffer_state *YY_BUFFER_STATE;
+	extern int yylex (void);
+	extern void yyerror (char const *);
+	extern void *yyterminate(void);
+	extern YY_BUFFER_STATE yy_scan_string( const char *str);
+	extern void yy_delete_buffer ( YY_BUFFER_STATE buffer );
+
+	/* forward declarations */
+	static const char *map_expr(const char *attr, char op, const char *val);
+	static const char *map_daterange(const char *dateattr,
+					 time_t date1, time_t date2);
+	static time_t isodate2unix(const char *s);
+
+	/* global vars, eg needed by the lexer */
+	struct sparql_parser_state {
+		TALLOC_CTX *frame;
+		YY_BUFFER_STATE s;
+		char var;
+		const char *result;
+	} *global_sparql_parser_state;
+%}
+
+%code provides {
+	#include <stdbool.h>
+	#include "mdssvc.h"
+	#define SPRAW_TIME_OFFSET 978307200
+	extern int yywrap(void);
+	extern bool map_spotlight_to_sparql_query(struct sl_query *slq);
+}
+
+%union {
+	int ival;
+	const char *sval;
+	bool bval;
+	time_t tval;
+}
+
+%expect 5
+%error-verbose
+
+%type <sval> match expr line function
+%type <tval> date
+
+%token <sval> WORD
+%token <bval> BOOL
+%token FUNC_INRANGE
+%token DATE_ISO
+%token OBRACE CBRACE EQUAL UNEQUAL GT LT COMMA QUOTE
+%left AND
+%left OR
+%%
+
+input:
+/* empty */
+| input line
+;
+
+line:
+expr {
+	global_sparql_parser_state->result = $1;
+}
+;
+
+expr:
+BOOL {
+	/*
+	 * We can't properly handle these in expressions, fortunately this
+	 * is probably only ever used by OS X as sole element in an
+	 * expression ie "False" (when Finder window selected our share
+	 * but no search string entered yet). Packet traces showed that OS
+	 * X Spotlight server then returns a failure (ie -1) which is what
+	 * we do here too by calling YYABORT.
+	 */
+	YYABORT;
+}
+/*
+ * We have "match OR match" and "expr OR expr", because the former is
+ * supposed to catch and coalesque expressions of the form
+ *
+ *   MDSattribute1="hello"||MDSattribute2="hello"
+ *
+ * into a single SPARQL expression for the case where both
+ * MDSattribute1 and MDSattribute2 map to the same SPARQL attibute,
+ * which is eg the case for "*" and "kMDItemTextContent" which both
+ * map to SPARQL "fts:match".
+ */
+
+| match OR match {
+	if (strcmp($1, $3) != 0) {
+		$$ = talloc_asprintf(talloc_tos(), "{ %s } UNION { %s }", $1, $3);
+	} else {
+		$$ = talloc_asprintf(talloc_tos(), "%s", $1);
+	}
+}
+| match {
+	$$ = $1;
+}
+| function {
+	$$ = $1;
+}
+| OBRACE expr CBRACE {
+	$$ = talloc_asprintf(talloc_tos(), "%s", $2);
+}
+| expr AND expr {
+	$$ = talloc_asprintf(talloc_tos(), "%s . %s", $1, $3);
+}
+| expr OR expr {
+	if (strcmp($1, $3) != 0) {
+		$$ = talloc_asprintf(talloc_tos(), "{ %s } UNION { %s }", $1, $3);
+	} else {
+		$$ = talloc_asprintf(talloc_tos(), "%s", $1);
+	}
+}
+;
+
+match:
+WORD EQUAL QUOTE WORD QUOTE {
+	$$ = map_expr($1, '=', $4);
+	if ($$ == NULL) YYABORT;
+}
+| WORD UNEQUAL QUOTE WORD QUOTE {
+	$$ = map_expr($1, '!', $4);
+	if ($$ == NULL) YYABORT;
+}
+| WORD LT QUOTE WORD QUOTE {
+	$$ = map_expr($1, '<', $4);
+	if ($$ == NULL) YYABORT;
+}
+| WORD GT QUOTE WORD QUOTE {
+	$$ = map_expr($1, '>', $4);
+	if ($$ == NULL) YYABORT;
+}
+| WORD EQUAL QUOTE WORD QUOTE WORD {
+	$$ = map_expr($1, '=', $4);
+	if ($$ == NULL) YYABORT;
+}
+| WORD UNEQUAL QUOTE WORD QUOTE WORD {
+	$$ = map_expr($1, '!', $4);
+	if ($$ == NULL) YYABORT;
+}
+| WORD LT QUOTE WORD QUOTE WORD {
+	$$ = map_expr($1, '<', $4);
+	if ($$ == NULL) YYABORT;
+}
+| WORD GT QUOTE WORD QUOTE WORD {
+	$$ = map_expr($1, '>', $4);
+	if ($$ == NULL) YYABORT;
+}
+;
+
+function:
+FUNC_INRANGE OBRACE WORD COMMA date COMMA date CBRACE {
+	$$ = map_daterange($3, $5, $7);
+	if ($$ == NULL) YYABORT;
+}
+;
+
+date:
+DATE_ISO OBRACE WORD CBRACE    {$$ = isodate2unix($3);}
+| WORD                         {$$ = atoi($1) + SPRAW_TIME_OFFSET;}
+;
+
+%%
+
+static time_t isodate2unix(const char *s)
+{
+	struct tm tm;
+	const char *p;
+
+	p = strptime(s, "%Y-%m-%dT%H:%M:%SZ", &tm);
+	if (p == NULL) {
+		return (time_t)-1;
+	}
+	return mktime(&tm);
+}
+
+static const char *map_daterange(const char *dateattr,
+				 time_t date1, time_t date2)
+{
+	struct sparql_parser_state *s = global_sparql_parser_state;
+	int result = 0;
+	char *sparql = NULL;
+	const struct sl_attr_map *p;
+	struct tm *tmp;
+	char buf1[64], buf2[64];
+
+	if (s->var == 'z') {
+		return NULL;
+	}
+
+	tmp = localtime(&date1);
+	if (tmp == NULL) {
+		return NULL;
+	}
+	result = strftime(buf1, sizeof(buf1), "%Y-%m-%dT%H:%M:%SZ", tmp);
+	if (result == 0) {
+		return NULL;
+	}
+
+	tmp = localtime(&date2);
+	if (tmp == NULL) {
+		return NULL;
+	}
+	result = strftime(buf2, sizeof(buf2), "%Y-%m-%dT%H:%M:%SZ", tmp);
+	if (result == 0) {
+		return NULL;
+	}
+
+	p = sl_attr_map_by_spotlight(dateattr);
+	if (p == NULL) {
+		return NULL;
+	}
+
+	sparql = talloc_asprintf(talloc_tos(),
+				 "?obj %s ?%c FILTER (?%c > '%s' && ?%c < '%s')",
+				 p->sparql_attr,
+				 s->var,
+				 s->var,
+				 buf1,
+				 s->var,
+				 buf2);
+	if (sparql == NULL) {
+		return NULL;
+	}
+
+	s->var++;
+	return sparql;
+}
+
+static char *map_type_search(const char *attr, char op, const char *val)
+{
+	char *result = NULL;
+	const char *sparqlAttr;
+	const struct sl_type_map *p;
+
+	p = sl_type_map_by_spotlight(val);
+	if (p == NULL) {
+		return NULL;
+	}
+
+	switch (p->type) {
+	case kMDTypeMapRDF:
+		sparqlAttr = "rdf:type";
+		break;
+	case kMDTypeMapMime:
+		sparqlAttr = "nie:mimeType";
+		break;
+	default:
+		return NULL;
+	}
+
+	result = talloc_asprintf(talloc_tos(), "?obj %s '%s'",
+				 sparqlAttr,
+				 p->sparql_type);
+	if (result == NULL) {
+		return NULL;
+	}
+
+	return result;
+}
+
+static const char *map_expr(const char *attr, char op, const char *val)
+{
+	struct sparql_parser_state *s = global_sparql_parser_state;
+	int result = 0;
+	char *sparql = NULL;
+	const struct sl_attr_map *p;
+	time_t t;
+	struct tm *tmp;
+	char buf1[64];
+	char *q;
+	const char *start;
+
+	if (s->var == 'z') {
+		return NULL;
+	}
+
+	p = sl_attr_map_by_spotlight(attr);
+	if (p == NULL) {
+		return NULL;
+	}
+
+	if ((p->type != ssmt_type) && (p->sparql_attr == NULL)) {
+		yyerror("unsupported Spotlight attribute");
+		return NULL;
+	}
+
+	switch (p->type) {
+	case ssmt_bool:
+		sparql = talloc_asprintf(talloc_tos(), "?obj %s '%s'",
+					 p->sparql_attr, val);
+		if (sparql == NULL) {
+			return NULL;
+		}
+		break;
+
+	case ssmt_num:
+		sparql = talloc_asprintf(talloc_tos(),
+					 "?obj %s ?%c FILTER(?%c %c%c '%s')",
+					 p->sparql_attr,
+					 s->var,
+					 s->var,
+					 op,
+					 /* append '=' to '!' */
+					 op == '!' ? '=' : ' ',
+					 val);
+		if (sparql == NULL) {
+			return NULL;
+		}
+		s->var++;
+		break;
+
+	case ssmt_str:
+		q = talloc_strdup(talloc_tos(), "");
+		if (q == NULL) {
+			return NULL;
+		}
+		start = val;
+		while (*val) {
+			if (*val != '*') {
+				val++;
+				continue;
+			}
+			if (val > start) {
+				q = talloc_strndup_append(q, start, val - start);
+				if (q == NULL) {
+					return NULL;
+				}
+			}
+			q = talloc_strdup_append(q, ".*");
+			if (q == NULL) {
+				return NULL;
+			}
+			val++;
+			start = val;
+		}
+		if (val > start) {
+			q = talloc_strndup_append(q, start, val - start);
+			if (q == NULL) {
+				return NULL;
+			}
+		}
+		sparql = talloc_asprintf(talloc_tos(),
+					 "?obj %s ?%c "
+					 "FILTER(regex(?%c, '^%s$', 'i'))",
+					 p->sparql_attr,
+					 s->var,
+					 s->var,
+					 q);
+		TALLOC_FREE(q);
+		if (sparql == NULL) {
+			return NULL;
+		}
+		s->var++;
+		break;
+
+	case ssmt_fts:
+		sparql = talloc_asprintf(talloc_tos(), "?obj %s '%s'",
+					 p->sparql_attr, val);
+		if (sparql == NULL) {
+			return NULL;
+		}
+		break;
+
+	case ssmt_date:
+		t = atoi(val) + SPRAW_TIME_OFFSET;
+		tmp = localtime(&t);
+		if (tmp == NULL) {
+			return NULL;
+		}
+		result = strftime(buf1, sizeof(buf1),
+				  "%Y-%m-%dT%H:%M:%SZ", tmp);
+		if (result == 0) {
+			return NULL;
+		}
+		sparql = talloc_asprintf(talloc_tos(),
+					 "?obj %s ?%c FILTER(?%c %c '%s')",
+					 p->sparql_attr,
+					 s->var,
+					 s->var,
+					 op,
+					 buf1);
+		if (sparql == NULL) {
+			return NULL;
+		}
+		s->var++;
+		break;
+
+	case ssmt_type:
+		sparql = map_type_search(attr, op, val);
+		if (sparql == NULL) {
+			return NULL;
+		}
+		break;
+
+	default:
+		return NULL;
+	}
+
+	return sparql;
+}
+
+void yyerror(const char *str)
+{
+	DEBUG(1, ("yyerror: %s\n", str));
+}
+
+int yywrap(void)
+{
+	return 1;
+}
+
+/**
+ * Map a Spotlight RAW query string to a SPARQL query string
+ **/
+bool map_spotlight_to_sparql_query(struct sl_query *slq)
+{
+	struct sparql_parser_state s = {
+		.frame = talloc_stackframe(),
+		.var = 'a',
+	};
+	int result;
+
+	s.s = yy_scan_string(slq->query_string);
+	if (s.s == NULL) {
+		TALLOC_FREE(s.frame);
+		return false;
+	}
+	global_sparql_parser_state = &s;
+	result = yyparse();
+	global_sparql_parser_state = NULL;
+	yy_delete_buffer(s.s);
+
+	if (result != 0) {
+		TALLOC_FREE(s.frame);
+		return false;
+	}
+
+	slq->sparql_query = talloc_asprintf(slq,
+		"SELECT ?url WHERE { %s . ?obj nie:url ?url . "
+		"FILTER(tracker:uri-is-descendant('file://%s/', ?url)) }",
+		s.result, slq->path_scope);
+	TALLOC_FREE(s.frame);
+	if (slq->sparql_query == NULL) {
+		return false;
+	}
+
+	return true;
+}
diff --git a/source3/rpc_server/mdssvc/sparql_parser_test.c b/source3/rpc_server/mdssvc/sparql_parser_test.c
new file mode 100644
index 0000000..8e4789d
--- /dev/null
+++ b/source3/rpc_server/mdssvc/sparql_parser_test.c
@@ -0,0 +1,38 @@
+#include "includes.h"
+#include "mdssvc.h"
+#include "sparql_parser.h"
+
+/*
+ * Examples:
+ *
+ * $ ./spotlight2sparql '_kMDItemGroupId=="11"'
+ * ...
+ * $ ./spotlight2sparql '*=="test*"cwd||kMDItemTextContent=="test*"cwd'
+ * ...
+ */
+
+int main(int argc, char **argv)
+{
+	bool ok;
+	struct sl_query *slq;
+
+	if (argc != 2) {
+		printf("usage: %s QUERY\n", argv[0]);
+		return 1;
+	}
+
+	slq = talloc_zero(NULL, struct sl_query);
+	if (slq == NULL) {
+		printf("talloc error\n");
+		return 1;
+	}
+
+	slq->query_string = argv[1];
+	slq->path_scope = "/foo/bar";
+
+	ok = map_spotlight_to_sparql_query(slq);
+	printf("%s\n", ok ? slq->sparql_query : "*mapping failed*");
+
+	talloc_free(slq);
+	return ok ? 0 : 1;
+}
diff --git a/source3/rpc_server/wscript_build b/source3/rpc_server/wscript_build
index 1da41f2..99e2b06 100755
--- a/source3/rpc_server/wscript_build
+++ b/source3/rpc_server/wscript_build
@@ -133,6 +133,8 @@ bld.SAMBA3_SUBSYSTEM('RPC_MDSSVC',
                     mdssvc/dalloc.c
                     mdssvc/marshalling.c
                     mdssvc/sparql_mapping.c
+                    mdssvc/sparql_parser.c
+                    mdssvc/sparql_lexer.c
                     mdssvc/srv_mdssvc_nt.c
                     ../../librpc/gen_ndr/srv_mdssvc.c''',
                     deps='samba-util ' + bld.env['libtracker'],
diff --git a/source3/wscript_build b/source3/wscript_build
index 231f1c0..79a0158 100755
--- a/source3/wscript_build
+++ b/source3/wscript_build
@@ -1496,6 +1496,16 @@ bld.SAMBA3_BINARY('samba-regedit',
                   deps='ncurses menu panel form registry param popt_samba3 smbregistry',
                   enabled=bld.env.build_regedit)
 
+bld.SAMBA3_BINARY('spotlight2sparql',
+                 source='''
+                 rpc_server/mdssvc/sparql_parser_test.c
+                 rpc_server/mdssvc/sparql_parser.c
+                 rpc_server/mdssvc/sparql_lexer.c
+                 rpc_server/mdssvc/sparql_mapping.c''',
+                 deps='samba3-util talloc ' + bld.env['libtracker'],
+                 enabled=bld.env.with_spotlight,
+                 install=False)
+
 ########################## INCLUDES #################################
 
 bld.RECURSE('auth')
-- 
2.1.0


From 7ca80eb0bebe211c2ecde6efcbc481c9712ab49b Mon Sep 17 00:00:00 2001
From: Ralph Boehme <rb at sernet.de>
Date: Mon, 11 Aug 2014 22:29:09 +0200
Subject: [PATCH 10/12] s3-mdssvc: add mdssd RPC service daemon for mdssvc

Signed-off-by: Ralph Boehme <slow at samba.org>
Reviewed-by: Stefan Metzmacher <metze at samba.org>
---
 source3/rpc_server/mdssd.c             | 753 +++++++++++++++++++++++++++++++++
 source3/rpc_server/rpc_config.h        |   2 +
 source3/rpc_server/rpc_service_setup.c |   5 +-
 source3/rpc_server/wscript_build       |   5 +
 source3/smbd/server.c                  |  10 +
 source3/wscript_build                  |   2 +-
 6 files changed, 775 insertions(+), 2 deletions(-)
 create mode 100644 source3/rpc_server/mdssd.c

diff --git a/source3/rpc_server/mdssd.c b/source3/rpc_server/mdssd.c
new file mode 100644
index 0000000..fac386b
--- /dev/null
+++ b/source3/rpc_server/mdssd.c
@@ -0,0 +1,753 @@
+/*
+ *  Unix SMB/CIFS implementation.
+ *
+ *  mds service daemon
+ *
+ *  Copyright (c) 2014      Ralph Boehme <rb at sernet.de>
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 3 of the License, or
+ *  (at your option) any later version.
+ *
+ *  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, see <http://www.gnu.org/licenses/>.
+ */
+
+#include "includes.h"
+#include "serverid.h"
+#include "messages.h"
+#include "ntdomain.h"
+
+#include "lib/util/util_process.h"
+
+#include "lib/id_cache.h"
+
+#include "../lib/tsocket/tsocket.h"
+#include "lib/server_prefork.h"
+#include "lib/server_prefork_util.h"
+#include "librpc/rpc/dcerpc_ep.h"
+
+#include "rpc_server/rpc_server.h"
+#include "rpc_server/rpc_ep_register.h"
+#include "rpc_server/rpc_sock_helper.h"
+
+#include "librpc/gen_ndr/srv_mdssvc.h"
+#include "rpc_server/mdssvc/srv_mdssvc_nt.h"
+
+#undef DBGC_CLASS
+#define DBGC_CLASS DBGC_RPC_SRV
+
+#define DAEMON_NAME "mdssd"
+#define MDSSD_MAX_SOCKETS 64
+
+static struct server_id parent_id;
+static struct prefork_pool *mdssd_pool = NULL;
+static int mdssd_child_id = 0;
+
+static struct pf_daemon_config default_pf_mdssd_cfg = {
+	.prefork_status = PFH_INIT,
+	.min_children = 5,
+	.max_children = 25,
+	.spawn_rate = 5,
+	.max_allowed_clients = 1000,
+	.child_min_life = 60 /* 1 minute minimum life time */
+};
+static struct pf_daemon_config pf_mdssd_cfg = { 0 };
+
+void start_mdssd(struct tevent_context *ev_ctx,
+		 struct messaging_context *msg_ctx);
+
+static void mdssd_smb_conf_updated(struct messaging_context *msg,
+				   void *private_data,
+				   uint32_t msg_type,
+				   struct server_id server_id,
+				   DATA_BLOB *data)
+{
+	struct tevent_context *ev_ctx;
+
+	DEBUG(10, ("Got message saying smb.conf was updated. Reloading.\n"));
+	ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
+
+	change_to_root_user();
+	lp_load_global(get_dyn_CONFIGFILE());
+
+	reopen_logs();
+	if (mdssd_child_id == 0) {
+		pfh_daemon_config(DAEMON_NAME,
+				  &pf_mdssd_cfg,
+				  &default_pf_mdssd_cfg);
+		pfh_manage_pool(ev_ctx, msg, &pf_mdssd_cfg, mdssd_pool);
+	}
+}
+
+static void mdssd_sig_term_handler(struct tevent_context *ev,
+				   struct tevent_signal *se,
+				   int signum,
+				   int count,
+				   void *siginfo,
+				   void *private_data)
+{
+	rpc_mdssvc_shutdown();
+
+	DEBUG(0, ("termination signal\n"));
+	exit(0);
+}
+
+static void mdssd_setup_sig_term_handler(struct tevent_context *ev_ctx)
+{
+	struct tevent_signal *se;
+
+	se = tevent_add_signal(ev_ctx,
+			       ev_ctx,
+			       SIGTERM, 0,
+			       mdssd_sig_term_handler,
+			       NULL);
+	if (!se) {
+		DEBUG(0, ("failed to setup SIGTERM handler\n"));
+		exit(1);
+	}
+}
+
+static void mdssd_sig_hup_handler(struct tevent_context *ev,
+				  struct tevent_signal *se,
+				  int signum,
+				  int count,
+				  void *siginfo,
+				  void *pvt)
+{
+
+	change_to_root_user();
+	lp_load_global(get_dyn_CONFIGFILE());
+
+	reopen_logs();
+	pfh_daemon_config(DAEMON_NAME,
+			  &pf_mdssd_cfg,
+			  &default_pf_mdssd_cfg);
+
+	/* relay to all children */
+	prefork_send_signal_to_all(mdssd_pool, SIGHUP);
+}
+
+static void mdssd_setup_sig_hup_handler(struct tevent_context *ev_ctx)
+{
+	struct tevent_signal *se;
+
+	se = tevent_add_signal(ev_ctx,
+			       ev_ctx,
+			       SIGHUP, 0,
+			       mdssd_sig_hup_handler,
+			       NULL);
+	if (!se) {
+		DEBUG(0, ("failed to setup SIGHUP handler\n"));
+		exit(1);
+	}
+}
+
+/**********************************************************
+ * Children
+ **********************************************************/
+
+static void mdssd_chld_sig_hup_handler(struct tevent_context *ev,
+				       struct tevent_signal *se,
+				       int signum,
+				       int count,
+				       void *siginfo,
+				       void *pvt)
+{
+	change_to_root_user();
+	reopen_logs();
+}
+
+static bool mdssd_setup_chld_hup_handler(struct tevent_context *ev_ctx)
+{
+	struct tevent_signal *se;
+
+	se = tevent_add_signal(ev_ctx,
+			       ev_ctx,
+			       SIGHUP, 0,
+			       mdssd_chld_sig_hup_handler,
+			       NULL);
+	if (!se) {
+		DEBUG(1, ("failed to setup SIGHUP handler"));
+		return false;
+	}
+
+	return true;
+}
+
+static void parent_ping(struct messaging_context *msg_ctx,
+			void *private_data,
+			uint32_t msg_type,
+			struct server_id server_id,
+			DATA_BLOB *data)
+{
+	/*
+	 * The fact we received this message is enough to let make the
+	 * event loop if it was idle. mdssd_children_main will cycle
+	 * through mdssd_next_client at least once. That function will
+	 * take whatever action is necessary
+	 */
+	DEBUG(10, ("Got message that the parent changed status.\n"));
+	return;
+}
+
+static bool mdssd_child_init(struct tevent_context *ev_ctx,
+			     int child_id,
+			     struct pf_worker_data *pf)
+{
+	NTSTATUS status;
+	struct messaging_context *msg_ctx = server_messaging_context();
+	bool ok;
+
+	status = reinit_after_fork(msg_ctx, ev_ctx,
+				   true);
+	if (!NT_STATUS_IS_OK(status)) {
+		DEBUG(0,("reinit_after_fork() failed\n"));
+		smb_panic("reinit_after_fork() failed");
+	}
+
+	prctl_set_comment("mdssd-child");
+
+	mdssd_child_id = child_id;
+	reopen_logs();
+
+	ok = mdssd_setup_chld_hup_handler(ev_ctx);
+	if (!ok) {
+		return false;
+	}
+
+	if (!serverid_register(messaging_server_id(msg_ctx),
+			       FLAG_MSG_GENERAL)) {
+		return false;
+	}
+
+	messaging_register(msg_ctx, ev_ctx,
+			   MSG_SMB_CONF_UPDATED, mdssd_smb_conf_updated);
+	messaging_register(msg_ctx, ev_ctx,
+			   MSG_PREFORK_PARENT_EVENT, parent_ping);
+
+	status = rpc_mdssvc_init(NULL);
+	if (!NT_STATUS_IS_OK(status)) {
+		DEBUG(0, ("Failed to intialize RPC: %s\n",
+			  nt_errstr(status)));
+		return false;
+	}
+
+	return true;
+}
+
+struct mdssd_children_data {
+	struct tevent_context *ev_ctx;
+	struct messaging_context *msg_ctx;
+	struct pf_worker_data *pf;
+	int listen_fd_size;
+	int *listen_fds;
+};
+
+static void mdssd_next_client(void *pvt);
+
+static int mdssd_children_main(struct tevent_context *ev_ctx,
+			       struct messaging_context *msg_ctx,
+			       struct pf_worker_data *pf,
+			       int child_id,
+			       int listen_fd_size,
+			       int *listen_fds,
+			       void *private_data)
+{
+	struct mdssd_children_data *data;
+	bool ok;
+	int ret = 0;
+
+	ok = mdssd_child_init(ev_ctx, child_id, pf);
+	if (!ok) {
+		return 1;
+	}
+
+	data = talloc(ev_ctx, struct mdssd_children_data);
+	if (!data) {
+		return 1;
+	}
+	data->pf = pf;
+	data->ev_ctx = ev_ctx;
+	data->msg_ctx = msg_ctx;
+	data->listen_fd_size = listen_fd_size;
+	data->listen_fds = listen_fds;
+
+	/* loop until it is time to exit */
+	while (pf->status != PF_WORKER_EXITING) {
+		/* try to see if it is time to schedule the next client */
+		mdssd_next_client(data);
+
+		ret = tevent_loop_once(ev_ctx);
+		if (ret != 0) {
+			DEBUG(0, ("tevent_loop_once() exited with %d: %s\n",
+				  ret, strerror(errno)));
+			pf->status = PF_WORKER_EXITING;
+		}
+	}
+
+	return ret;
+}
+
+static void mdssd_client_terminated(void *pvt)
+{
+	struct mdssd_children_data *data;
+
+	data = talloc_get_type_abort(pvt, struct mdssd_children_data);
+
+	pfh_client_terminated(data->pf);
+
+	mdssd_next_client(pvt);
+}
+
+struct mdssd_new_client {
+	struct mdssd_children_data *data;
+};
+
+static void mdssd_handle_client(struct tevent_req *req);
+
+static void mdssd_next_client(void *pvt)
+{
+	struct tevent_req *req;
+	struct mdssd_children_data *data;
+	struct mdssd_new_client *next;
+
+	data = talloc_get_type_abort(pvt, struct mdssd_children_data);
+
+	if (!pfh_child_allowed_to_accept(data->pf)) {
+		/* nothing to do for now we are already listening
+		 * or we are not allowed to listen further */
+		return;
+	}
+
+	next = talloc_zero(data, struct mdssd_new_client);
+	if (!next) {
+		DEBUG(1, ("Out of memory!?\n"));
+		return;
+	}
+	next->data = data;
+
+	req = prefork_listen_send(next,
+				  data->ev_ctx,
+				  data->pf,
+				  data->listen_fd_size,
+				  data->listen_fds);
+	if (!req) {
+		DEBUG(1, ("Failed to make listening request!?\n"));
+		talloc_free(next);
+		return;
+	}
+	tevent_req_set_callback(req, mdssd_handle_client, next);
+}
+
+static void mdssd_handle_client(struct tevent_req *req)
+{
+	struct mdssd_children_data *data;
+	struct mdssd_new_client *client;
+	const DATA_BLOB ping = data_blob_null;
+	int rc;
+	int sd;
+	TALLOC_CTX *tmp_ctx;
+	struct tsocket_address *srv_addr;
+	struct tsocket_address *cli_addr;
+
+	client = tevent_req_callback_data(req, struct mdssd_new_client);
+	data = client->data;
+
+	tmp_ctx = talloc_stackframe();
+	if (tmp_ctx == NULL) {
+		DEBUG(1, ("Failed to allocate stackframe!\n"));
+		return;
+	}
+
+	rc = prefork_listen_recv(req,
+				 tmp_ctx,
+				 &sd,
+				 &srv_addr,
+				 &cli_addr);
+
+	/* this will free the request too */
+	talloc_free(client);
+
+	if (rc != 0) {
+		DEBUG(6, ("No client connection was available after all!\n"));
+		goto done;
+	}
+
+	/* Warn parent that our status changed */
+	messaging_send(data->msg_ctx, parent_id,
+			MSG_PREFORK_CHILD_EVENT, &ping);
+
+	DEBUG(2, ("mdssd preforked child %d got client connection!\n",
+		  (int)(data->pf->pid)));
+
+	if (tsocket_address_is_inet(srv_addr, "ip")) {
+		DEBUG(3, ("Got a tcpip client connection from %s on inteface %s\n",
+			   tsocket_address_string(cli_addr, tmp_ctx),
+			   tsocket_address_string(srv_addr, tmp_ctx)));
+
+		dcerpc_ncacn_accept(data->ev_ctx,
+				    data->msg_ctx,
+				    NCACN_IP_TCP,
+				    "IP",
+				    cli_addr,
+				    srv_addr,
+				    sd,
+				    NULL);
+	} else if (tsocket_address_is_unix(srv_addr)) {
+		const char *p;
+		const char *b;
+
+		p = tsocket_address_unix_path(srv_addr, tmp_ctx);
+		if (p == NULL) {
+			talloc_free(tmp_ctx);
+			return;
+		}
+
+		b = strrchr(p, '/');
+		if (b != NULL) {
+			b++;
+		} else {
+			b = p;
+		}
+
+		if (strstr(p, "/np/")) {
+			named_pipe_accept_function(data->ev_ctx,
+						   data->msg_ctx,
+						   b,
+						   sd,
+						   mdssd_client_terminated,
+						   data);
+		} else {
+			dcerpc_ncacn_accept(data->ev_ctx,
+					    data->msg_ctx,
+					    NCALRPC,
+					    b,
+					    cli_addr,
+					    srv_addr,
+					    sd,
+					    NULL);
+		}
+	} else {
+		DEBUG(0, ("ERROR: Unsupported socket!\n"));
+	}
+
+done:
+	talloc_free(tmp_ctx);
+}
+
+/*
+ * MAIN
+ */
+
+static void child_ping(struct messaging_context *msg_ctx,
+			void *private_data,
+			uint32_t msg_type,
+			struct server_id server_id,
+			DATA_BLOB *data)
+{
+	struct tevent_context *ev_ctx;
+
+	ev_ctx = talloc_get_type_abort(private_data, struct tevent_context);
+
+	DEBUG(10, ("Got message that a child changed status.\n"));
+	pfh_manage_pool(ev_ctx, msg_ctx, &pf_mdssd_cfg, mdssd_pool);
+}
+
+static bool mdssd_schedule_check(struct tevent_context *ev_ctx,
+				 struct messaging_context *msg_ctx,
+				 struct timeval current_time);
+
+static void mdssd_check_children(struct tevent_context *ev_ctx,
+				    struct tevent_timer *te,
+				    struct timeval current_time,
+				    void *pvt);
+
+static void mdssd_sigchld_handler(struct tevent_context *ev_ctx,
+				  struct prefork_pool *pfp,
+				  void *pvt)
+{
+	struct messaging_context *msg_ctx;
+
+	msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
+
+	/* run pool management so we can fork/retire or increase
+	 * the allowed connections per child based on load */
+	pfh_manage_pool(ev_ctx, msg_ctx, &pf_mdssd_cfg, mdssd_pool);
+}
+
+static bool mdssd_setup_children_monitor(struct tevent_context *ev_ctx,
+					 struct messaging_context *msg_ctx)
+{
+	bool ok;
+
+	/* add our oun sigchld callback */
+	prefork_set_sigchld_callback(mdssd_pool, mdssd_sigchld_handler, msg_ctx);
+
+	ok = mdssd_schedule_check(ev_ctx, msg_ctx, tevent_timeval_current());
+
+	return ok;
+}
+
+static bool mdssd_schedule_check(struct tevent_context *ev_ctx,
+				 struct messaging_context *msg_ctx,
+				 struct timeval current_time)
+{
+	struct tevent_timer *te;
+	struct timeval next_event;
+
+	/* check situation again in 10 seconds */
+	next_event = tevent_timeval_current_ofs(10, 0);
+
+	/* TODO: check when the socket becomes readable, so that children
+	 * are checked only when there is some activity ? */
+	te = tevent_add_timer(ev_ctx, mdssd_pool, next_event,
+			      mdssd_check_children, msg_ctx);
+	if (!te) {
+		DEBUG(2, ("Failed to set up children monitoring!\n"));
+		return false;
+	}
+
+	return true;
+}
+
+static void mdssd_check_children(struct tevent_context *ev_ctx,
+				 struct tevent_timer *te,
+				 struct timeval current_time,
+				 void *pvt)
+{
+	struct messaging_context *msg_ctx;
+
+	msg_ctx = talloc_get_type_abort(pvt, struct messaging_context);
+
+	pfh_manage_pool(ev_ctx, msg_ctx, &pf_mdssd_cfg, mdssd_pool);
+
+	mdssd_schedule_check(ev_ctx, msg_ctx, current_time);
+}
+
+/*
+ * start it up
+ */
+
+static bool mdssd_create_sockets(struct tevent_context *ev_ctx,
+				 struct messaging_context *msg_ctx,
+				 int *listen_fd,
+				 int *listen_fd_size)
+{
+	struct dcerpc_binding_vector *v, *v_orig;
+	TALLOC_CTX *tmp_ctx;
+	NTSTATUS status;
+	int fd = -1;
+	int rc;
+	bool ok = false;
+
+	tmp_ctx = talloc_stackframe();
+	if (tmp_ctx == NULL) {
+		return false;
+	}
+
+	status = dcerpc_binding_vector_new(tmp_ctx, &v_orig);
+	if (!NT_STATUS_IS_OK(status)) {
+		goto done;
+	}
+
+	/* mdssvc */
+	fd = create_named_pipe_socket("mdssvc");
+	if (fd < 0) {
+		goto done;
+	}
+
+	rc = listen(fd, pf_mdssd_cfg.max_allowed_clients);
+	if (rc == -1) {
+		goto done;
+	}
+	listen_fd[*listen_fd_size] = fd;
+	(*listen_fd_size)++;
+
+	fd = create_dcerpc_ncalrpc_socket("mdssvc");
+	if (fd < 0) {
+		goto done;
+	}
+
+	rc = listen(fd, pf_mdssd_cfg.max_allowed_clients);
+	if (rc == -1) {
+		goto done;
+	}
+	listen_fd[*listen_fd_size] = fd;
+	(*listen_fd_size)++;
+	fd = -1;
+
+	v = dcerpc_binding_vector_dup(tmp_ctx, v_orig);
+	if (v == NULL) {
+		goto done;
+	}
+
+	status = dcerpc_binding_vector_replace_iface(&ndr_table_mdssvc, v);
+	if (!NT_STATUS_IS_OK(status)) {
+		goto done;
+	}
+
+	status = dcerpc_binding_vector_add_np_default(&ndr_table_mdssvc, v);
+	if (!NT_STATUS_IS_OK(status)) {
+		goto done;
+	}
+
+	status = dcerpc_binding_vector_add_unix(&ndr_table_mdssvc, v, "mdssvc");
+	if (!NT_STATUS_IS_OK(status)) {
+		goto done;
+	}
+
+	ok = true;
+done:
+	if (fd != -1) {
+		close(fd);
+	}
+	talloc_free(tmp_ctx);
+	return ok;
+}
+
+static bool mdssvc_init_cb(void *ptr)
+{
+	struct messaging_context *msg_ctx =
+		talloc_get_type_abort(ptr, struct messaging_context);
+	bool ok;
+
+	ok = init_service_mdssvc(msg_ctx);
+	if (!ok) {
+		return false;
+	}
+
+	return true;
+}
+
+static bool mdssvc_shutdown_cb(void *ptr)
+{
+	shutdown_service_mdssvc();
+
+	return true;
+}
+
+void start_mdssd(struct tevent_context *ev_ctx,
+		 struct messaging_context *msg_ctx)
+{
+	NTSTATUS status;
+	int listen_fd[MDSSD_MAX_SOCKETS];
+	int listen_fd_size = 0;
+	pid_t pid;
+	int rc;
+	bool ok;
+	struct rpc_srv_callbacks mdssvc_cb;
+
+	DEBUG(1, ("Forking Metadata Service Daemon\n"));
+
+	/*
+	 * Block signals before forking child as it will have to
+	 * set its own handlers. Child will re-enable SIGHUP as
+	 * soon as the handlers are set up.
+	 */
+	BlockSignals(true, SIGTERM);
+	BlockSignals(true, SIGHUP);
+
+	pid = fork();
+	if (pid == -1) {
+		DEBUG(0, ("Failed to fork mdssd [%s], aborting ...\n",
+			   strerror(errno)));
+		exit(1);
+	}
+
+	/* parent or error */
+	if (pid != 0) {
+
+		/* Re-enable SIGHUP before returnig */
+		BlockSignals(false, SIGTERM);
+		BlockSignals(false, SIGHUP);
+
+		return;
+	}
+
+	status = reinit_after_fork(msg_ctx,
+				   ev_ctx,
+				   true);
+	if (!NT_STATUS_IS_OK(status)) {
+		DEBUG(0,("reinit_after_fork() failed\n"));
+		smb_panic("reinit_after_fork() failed");
+	}
+
+	prctl_set_comment("mdssd-master");
+	reopen_logs();
+
+	/* save the parent process id so the children can use it later */
+	parent_id = messaging_server_id(msg_ctx);
+
+	pfh_daemon_config(DAEMON_NAME,
+			  &pf_mdssd_cfg,
+			  &default_pf_mdssd_cfg);
+
+	mdssd_setup_sig_term_handler(ev_ctx);
+	mdssd_setup_sig_hup_handler(ev_ctx);
+
+	BlockSignals(false, SIGTERM);
+	BlockSignals(false, SIGHUP);
+
+	ok = mdssd_create_sockets(ev_ctx, msg_ctx, listen_fd, &listen_fd_size);
+	if (!ok) {
+		exit(1);
+	}
+
+	/* start children before any more initialization is done */
+	ok = prefork_create_pool(ev_ctx, /* mem_ctx */
+				 ev_ctx,
+				 msg_ctx,
+				 listen_fd_size,
+				 listen_fd,
+				 pf_mdssd_cfg.min_children,
+				 pf_mdssd_cfg.max_children,
+				 &mdssd_children_main,
+				 NULL,
+				 &mdssd_pool);
+	if (!ok) {
+		exit(1);
+	}
+
+	if (!serverid_register(messaging_server_id(msg_ctx),
+			       FLAG_MSG_GENERAL)) {
+		exit(1);
+	}
+
+	messaging_register(msg_ctx,
+			   ev_ctx,
+			   MSG_SMB_CONF_UPDATED,
+			   mdssd_smb_conf_updated);
+	messaging_register(msg_ctx, ev_ctx,
+			   MSG_PREFORK_CHILD_EVENT, child_ping);
+
+	mdssvc_cb.init         = mdssvc_init_cb;
+	mdssvc_cb.shutdown     = mdssvc_shutdown_cb;
+	mdssvc_cb.private_data = msg_ctx;
+
+	status = rpc_mdssvc_init(&mdssvc_cb);
+	if (!NT_STATUS_IS_OK(status)) {
+		exit(1);
+	}
+
+	ok = mdssd_setup_children_monitor(ev_ctx, msg_ctx);
+	if (!ok) {
+		exit(1);
+	}
+
+	DEBUG(1, ("mdssd Daemon Started (%u)\n", (unsigned int)getpid()));
+
+	/* loop forever */
+	rc = tevent_loop_wait(ev_ctx);
+
+	/* should not be reached */
+	DEBUG(0,("mdssd: tevent_loop_wait() exited with %d - %s\n",
+		 rc, (rc == 0) ? "out of events" : strerror(errno)));
+	exit(1);
+}
diff --git a/source3/rpc_server/rpc_config.h b/source3/rpc_server/rpc_config.h
index ab5e3a4..5091704 100644
--- a/source3/rpc_server/rpc_config.h
+++ b/source3/rpc_server/rpc_config.h
@@ -44,6 +44,7 @@ enum rpc_service_mode_e rpc_service_mode(const char *name);
 #define rpc_samr_mode() rpc_service_mode("samr")
 #define rpc_netlogon_mode() rpc_service_mode("netlogon")
 #define rpc_fssagentrpc_mode() rpc_service_mode("fssagentrpc")
+#define rpc_mdssvc_mode() rpc_service_mode("mdssvc")
 
 
 
@@ -66,5 +67,6 @@ enum rpc_daemon_type_e rpc_daemon_type(const char *name);
 #define rpc_spoolss_daemon() rpc_daemon_type("spoolssd")
 #define rpc_lsasd_daemon() rpc_daemon_type("lsasd")
 #define rpc_fss_daemon() rpc_daemon_type("fssd")
+#define rpc_mdssd_daemon() rpc_daemon_type("mdssd")
 
 #endif /* _RPC_CONFIG_H */
diff --git a/source3/rpc_server/rpc_service_setup.c b/source3/rpc_server/rpc_service_setup.c
index f062497..ee995a8 100644
--- a/source3/rpc_server/rpc_service_setup.c
+++ b/source3/rpc_server/rpc_service_setup.c
@@ -475,7 +475,10 @@ static bool rpc_setup_mdssvc(struct tevent_context *ev_ctx,
 	struct rpc_srv_callbacks mdssvc_cb;
 	NTSTATUS status;
 	enum rpc_service_mode_e service_mode = rpc_service_mode(t->name);
-	if (service_mode != RPC_SERVICE_MODE_EMBEDDED) {
+	enum rpc_daemon_type_e mdssvc_type = rpc_mdssd_daemon();
+
+	if (service_mode != RPC_SERVICE_MODE_EMBEDDED
+	    || mdssvc_type != RPC_DAEMON_EMBEDDED) {
 		return true;
 	}
 
diff --git a/source3/rpc_server/wscript_build b/source3/rpc_server/wscript_build
index 99e2b06..278e0bd 100755
--- a/source3/rpc_server/wscript_build
+++ b/source3/rpc_server/wscript_build
@@ -186,3 +186,8 @@ bld.SAMBA3_SUBSYSTEM('LSASD',
 bld.SAMBA3_SUBSYSTEM('FSSD',
                     source='fssd.c',
                     deps='samba-util')
+
+bld.SAMBA3_SUBSYSTEM('MDSSD',
+                    source='mdssd.c',
+                    deps='RPC_SOCK_HELPER samba-util',
+                    enabled=bld.env.with_spotlight)
diff --git a/source3/smbd/server.c b/source3/smbd/server.c
index 20aed16..0ebaf85 100644
--- a/source3/smbd/server.c
+++ b/source3/smbd/server.c
@@ -90,6 +90,9 @@ extern void start_lsasd(struct tevent_context *ev_ctx,
 extern void start_fssd(struct tevent_context *ev_ctx,
 		       struct messaging_context *msg_ctx);
 
+extern void start_mdssd(struct tevent_context *ev_ctx,
+			struct messaging_context *msg_ctx);
+
 #ifdef WITH_DFS
 extern int dcelogin_atmost_once;
 #endif /* WITH_DFS */
@@ -1586,6 +1589,13 @@ extern void build_options(bool screen);
 				exit_daemon("Samba failed to init printing subsystem", EACCES);
 			}
 		}
+
+#ifdef WITH_SPOTLIGHT
+		if ((rpc_mdssvc_mode() == RPC_SERVICE_MODE_EXTERNAL) &&
+		    (rpc_mdssd_daemon() == RPC_DAEMON_FORK)) {
+			start_mdssd(ev_ctx, msg_ctx);
+		}
+#endif
 	} else if (!lp__disable_spoolss() &&
 		   (rpc_spoolss_daemon() != RPC_DAEMON_DISABLED)) {
 		if (!printing_subsystem_init(ev_ctx, msg_ctx, false, false)) {
diff --git a/source3/wscript_build b/source3/wscript_build
index 79a0158..67b6c8d 100755
--- a/source3/wscript_build
+++ b/source3/wscript_build
@@ -857,7 +857,7 @@ bld.SAMBA3_SUBSYSTEM('LIBLSA',
 
 bld.SAMBA3_BINARY('smbd/smbd',
                  source='smbd/server.c',
-                 deps='smbd_base EPMD LSASD FSSD',
+                 deps='smbd_base EPMD LSASD FSSD MDSSD',
                  install_path='${SBINDIR}')
 
 bld.SAMBA3_BINARY('nmbd/nmbd',
-- 
2.1.0


From aa373e54fe29bd78c959a4ecc8f3dec9841d9d48 Mon Sep 17 00:00:00 2001
From: Ralph Boehme <rb at sernet.de>
Date: Mon, 11 Aug 2014 22:30:53 +0200
Subject: [PATCH 11/12] s3-mdssvc: add documentation for mdssvc and mdssd

Signed-off-by: Ralph Boehme <slow at samba.org>
Reviewed-by: Stefan Metzmacher <metze at samba.org>
---
 docs-xml/smbdotconf/misc/rpcdaemon.xml |  7 ++++---
 docs-xml/smbdotconf/misc/rpcserver.xml |  8 ++++++--
 docs-xml/smbdotconf/misc/spotlight.xml | 13 ++++++++++++-
 3 files changed, 22 insertions(+), 6 deletions(-)

diff --git a/docs-xml/smbdotconf/misc/rpcdaemon.xml b/docs-xml/smbdotconf/misc/rpcdaemon.xml
index e9654a0..72f057d 100644
--- a/docs-xml/smbdotconf/misc/rpcdaemon.xml
+++ b/docs-xml/smbdotconf/misc/rpcdaemon.xml
@@ -55,14 +55,15 @@
 	</para>
 
 	<para>
-	Samba includes separate daemons for spoolss, lsarpc/lsass, netlogon,
-	samr, and FSRVP. Currently four daemons are available and they are
-	called:
+	Samba includes separate daemons for spoolss, lsarpc/lsass,
+	netlogon, samr, FSRVP and mdssvc(Spotlight). Currently five
+	daemons are available and they are called:
 	<programlisting>
 		epmd
 		lsasd
 		spoolssd
 		fssd
+		mdssd
 	</programlisting>
 	Example:
 	<programlisting>
diff --git a/docs-xml/smbdotconf/misc/rpcserver.xml b/docs-xml/smbdotconf/misc/rpcserver.xml
index d285872..fafedaf 100644
--- a/docs-xml/smbdotconf/misc/rpcserver.xml
+++ b/docs-xml/smbdotconf/misc/rpcserver.xml
@@ -32,6 +32,7 @@
 		<listitem><para>ntsvcs       - Plug and Play Services</para></listitem>
 		<listitem><para>eventlog     - Event Logger</para></listitem>
 		<listitem><para>initshutdown - Init Shutdown Service</para></listitem>
+		<listitem><para>mdssvc       - Spotlight</para></listitem>
 	</itemizedlist>
 
 	<para>
@@ -55,8 +56,8 @@
 	</para>
 
 	<para>
-		Currently in Samba3 we support three daemons, spoolssd, epmd and
-		lsasd. These daemons can be enabled using the
+		Currently in Samba3 we support four daemons, spoolssd, epmd,
+		lsasd and mdssd. These daemons can be enabled using the
 		<emphasis>rpc_daemon</emphasis> option. For spoolssd you have
 		to enable the daemon and proxy the named pipe with:
 	</para>
@@ -71,6 +72,9 @@
 
 			rpc_server:spoolss = external
 			rpc_server:epmapper = disabled
+
+			rpc_daemon:mdssd = fork
+			rpc_server:mdssvc = external
 		</programlisting>
 	</para>
 
diff --git a/docs-xml/smbdotconf/misc/spotlight.xml b/docs-xml/smbdotconf/misc/spotlight.xml
index d872bb6..fa2001d 100644
--- a/docs-xml/smbdotconf/misc/spotlight.xml
+++ b/docs-xml/smbdotconf/misc/spotlight.xml
@@ -32,7 +32,8 @@
 	</para>
 
 	<para>
-      To enable the Spotlight RPC service:
+	  The Spotlight RPC service can either be enabled as embedded
+	  RPC service:
 	</para>
 
 <programlisting>
@@ -40,6 +41,16 @@
 <smbconfoption name="rpc_server:mdsvc">embedded</smbconfoption>
 </programlisting>
 
+	<para>
+	  Or it can be run in a seperate RPC service daemon:
+	</para>
+
+<programlisting>
+<smbconfsection name="[Global]"/>
+<smbconfoption name="rpc_server:mdssd">fork</smbconfoption>
+<smbconfoption name="rpc_server:mdsvc">external</smbconfoption>
+</programlisting>
+
 </description>
 <value type="default">no</value>
 </samba:parameter>
-- 
2.1.0


From 0667d15a9aad5c2fd3fed16bbf663ff2aac35b6e Mon Sep 17 00:00:00 2001
From: Ralph Boehme <slow at samba.org>
Date: Fri, 27 Mar 2015 17:39:43 +0100
Subject: [PATCH 12/12] WHATSNEW: Spotlight

Signed-off-by: Ralph Boehme <slow at samba.org>
Reviewed-by: Stefan Metzmacher <metze at samba.org>
---
 WHATSNEW.txt | 8 ++++++++
 1 file changed, 8 insertions(+)

diff --git a/WHATSNEW.txt b/WHATSNEW.txt
index ac2e40d..cbf73b9 100644
--- a/WHATSNEW.txt
+++ b/WHATSNEW.txt
@@ -22,6 +22,14 @@ backends for logging to the systemd-journal, lttng and gpfs have been
 added. Please consult the section for the 'logging' parameter in the
 smb.conf manpage for details.
 
+Spotlight
+=========
+
+Support for Apple's Spotlight has been added by integrating with Gnome
+Tracker.
+
+For detailed instructions how to build and setup Samba for Spotlight,
+please see the Samba wiki: <https://wiki.samba.org/index.php/Spotlight>
 
 ######################################################################
 Changes
-- 
2.1.0



More information about the samba-technical mailing list