]> arthur.barton.de Git - netatalk.git/blobdiff - etc/afpd/spotlight.c
Spotlight RPC init is working
[netatalk.git] / etc / afpd / spotlight.c
index f37e226e1038a79f82b44c067cb8da67683e0e79..176e6d6b93637e6ce254bcdac7125c23d20a6cba 100644 (file)
@@ -79,19 +79,20 @@ static int sl_pack_loop(DALLOC_CTX *query, char *buf, int offset, char *toc_buf,
 /* Helper functions and stuff */
 static const char *neststrings[] = {
     "",
-    "    ",
-    "        ",
-    "            ",
-    "                ",
-    "                    ",
-    "                        "
+    "\t",
+    "\t\t",
+    "\t\t\t",
+    "\t\t\t\t",
+    "\t\t\t\t\t",
+    "\t\t\t\t\t\t",
 };
 
 static int dd_dump(DALLOC_CTX *dd, int nestinglevel)
 {
     const char *type;
 
-    LOG(log_debug, logtype_sl, "%s1: %s(#%d): {", neststrings[nestinglevel], talloc_get_name(dd), talloc_array_length(dd->dd_talloc_array));
+    LOG(log_debug, logtype_sl, "%s%s(#%d): {",
+        neststrings[nestinglevel], talloc_get_name(dd), talloc_array_length(dd->dd_talloc_array));
 
     for (int n = 0; n < talloc_array_length(dd->dd_talloc_array); n++) {
 
@@ -104,28 +105,28 @@ static int dd_dump(DALLOC_CTX *dd, int nestinglevel)
         } else if (STRCMP(type, ==, "uint64_t")) {
             uint64_t i;
             memcpy(&i, dd->dd_talloc_array[n], sizeof(uint64_t));
-            LOG(log_debug, logtype_sl, "%s%u:\t0x%04x", neststrings[nestinglevel + 1], n + 1, i);
+            LOG(log_debug, logtype_sl, "%suint64_t: 0x%04x", neststrings[nestinglevel + 1], i);
         } else if (STRCMP(type, ==, "int64_t")) {
             int64_t i;
             memcpy(&i, dd->dd_talloc_array[n], sizeof(int64_t));
-            LOG(log_debug, logtype_sl, "%s%d:\t%" PRId64, neststrings[nestinglevel + 1], n + 1, i);
+            LOG(log_debug, logtype_sl, "%sint64_t: %" PRId64, neststrings[nestinglevel + 1], i);
         } else if (STRCMP(type, ==, "uint32_t")) {
             uint32_t i;
             memcpy(&i, dd->dd_talloc_array[n], sizeof(uint32_t));
-            LOG(log_debug, logtype_sl, "%s%d:\t%" PRIu32, neststrings[nestinglevel + 1], n + 1, i);
+            LOG(log_debug, logtype_sl, "%s%s: %" PRIu32, neststrings[nestinglevel + 1], type, i);
         } else if (STRCMP(type, ==, "char *")) {
             char *s;
             memcpy(&s, dd->dd_talloc_array[n], sizeof(char *));
-            LOG(log_debug, logtype_sl, "%s%d:\t%s", neststrings[nestinglevel + 1], n + +1, s);
+            LOG(log_debug, logtype_sl, "%sstring: %s", neststrings[nestinglevel + 1], s);
         } else if (STRCMP(type, ==, "sl_bool_t")) {
             sl_bool_t bl;
             memcpy(&bl, dd->dd_talloc_array[n], sizeof(sl_bool_t));
-            LOG(log_debug, logtype_sl, "%s%d:\t%s", neststrings[nestinglevel + 1], n + +1, bl ? "true" : "false");
+            LOG(log_debug, logtype_sl, "%sbool: %s", neststrings[nestinglevel + 1], bl ? "true" : "false");
         } else if (STRCMP(type, ==, "sl_cnids_t")) {
             sl_cnids_t cnids;
             memcpy(&cnids, dd->dd_talloc_array[n], sizeof(sl_cnids_t));
-            LOG(log_debug, logtype_sl, "%s%d:\tunkn1: %" PRIu16 ", unkn2: %" PRIu32,
-                   neststrings[nestinglevel + 1], n + 1, cnids.ca_unkn1, cnids.ca_context);
+            LOG(log_debug, logtype_sl, "%sCNIDs: unkn1: %" PRIu16 ", unkn2: %" PRIu32,
+                   neststrings[nestinglevel + 1], cnids.ca_unkn1, cnids.ca_context);
             if (cnids.ca_cnids)
                 dd_dump(cnids.ca_cnids, nestinglevel + 1);
         }
@@ -233,7 +234,7 @@ static int sl_pack_CNID(sl_cnids_t *cnids, char *buf, int offset, char *toc_buf,
     int cnid_count = talloc_array_length(cnids->ca_cnids);
 
     SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_CNIDS, (offset + SL_OFFSET_DELTA) / 8, cnid_count));
-    SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx));
+    SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
     *toc_idx += 1;
     offset += 8;
 
@@ -255,8 +256,13 @@ static int sl_pack_CNID(sl_cnids_t *cnids, char *buf, int offset, char *toc_buf,
 
 static int sl_pack_array(sl_array_t *array, char *buf, int offset, char *toc_buf, int *toc_idx)
 {
-    SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_ARRAY, (offset + SL_OFFSET_DELTA) / 8, talloc_array_length(array->dd_talloc_array)));
-    SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx));
+    int count = talloc_array_length(array->dd_talloc_array);
+    int octets = (offset + SL_OFFSET_DELTA) / 8;
+
+    LOG(log_maxdebug, logtype_sl, "sl_pack_array: count: %d, offset:%d, octets: %d", count, offset, octets);
+
+    SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_ARRAY, octets, count));
+    SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
     *toc_idx += 1;
     offset += 8;
 
@@ -268,7 +274,7 @@ static int sl_pack_array(sl_array_t *array, char *buf, int offset, char *toc_buf
 static int sl_pack_dict(sl_array_t *dict, char *buf, int offset, char *toc_buf, int *toc_idx)
 {
     SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_DICT, (offset + SL_OFFSET_DELTA) / 8, talloc_array_length(dict->dd_talloc_array)));
-    SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx));
+    SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
     *toc_idx += 1;
     offset += 8;
 
@@ -277,15 +283,19 @@ static int sl_pack_dict(sl_array_t *dict, char *buf, int offset, char *toc_buf,
     return offset;
 }
 
-static int sl_pack_string(char *s, char *buf, int offset, char *toc_buf, int *toc_idx)
+static int sl_pack_string(char **string, char *buf, int offset, char *toc_buf, int *toc_idx)
 {
     int len, octets, used_in_last_octet;
+    char *s = *string;
     len = strlen(s);
     octets = (len / 8) + (len & 7 ? 1 : 0);
     used_in_last_octet = 8 - (octets * 8 - len);
 
-    SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_DICT, (offset + SL_OFFSET_DELTA) / 8, used_in_last_octet));
-    SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx));
+    LOG(log_maxdebug, logtype_sl, "sl_pack_string(\"%s\"): len: %d, octets: %d, used_in_last_octet: %d",
+        s, len, octets, used_in_last_octet);
+
+    SLVAL(toc_buf, *toc_idx * 8, sl_pack_tag(SQ_CPX_TYPE_STRING, (offset + SL_OFFSET_DELTA) / 8, used_in_last_octet));
+    SLVAL(buf, offset, sl_pack_tag(SQ_TYPE_COMPLEX, 1, *toc_idx + 1));
     *toc_idx += 1;
     offset += 8;
 
@@ -352,9 +362,9 @@ static int sl_pack(DALLOC_CTX *query, char *buf)
     int len = 0;
 
     memcpy(buf, "432130dm", 8);
-    EC_NEG1_LOG( len = sl_pack_loop(query, buf + 16, 0, toc_buf, &toc_index) );
-    SIVAL(buf, 8, len / 8 + toc_index + 1);
-    SIVAL(buf, 12, toc_index + 1);
+    EC_NEG1_LOG( len = sl_pack_loop(query, buf + 16, 0, toc_buf + 8, &toc_index) );
+    SIVAL(buf, 8, len / 8 + 1 + toc_index + 1);
+    SIVAL(buf, 12, len / 8 + 1);
 
     SLVAL(toc_buf, 0, sl_pack_tag(SQ_TYPE_TOC, toc_index + 1, 0));
     memcpy(buf + 16 + len, toc_buf, (toc_index + 1 ) * 8);
@@ -720,6 +730,61 @@ EC_CLEANUP:
     EC_EXIT;
 }
 
+/**************************************************************************************************
+ * Spotlight RPC functions
+ **************************************************************************************************/
+
+static int sl_rpc_fetchPropertiesForContext(AFPObj *obj, const DALLOC_CTX *query, DALLOC_CTX *reply, const struct vol *v)
+{
+    EC_INIT;
+
+    char *s;
+    sl_dict_t *dict;
+    sl_array_t *array;
+    sl_uuid_t uuid;
+
+    if (!v->v_uuid)
+        EC_FAIL_LOG("sl_rpc_fetchPropertiesForContext: missing UUID for volume: %s", v->v_localname);
+
+    dict = talloc_zero(reply, sl_dict_t);
+
+    /* key/val 1 */
+    s = talloc_strdup(dict, "kMDSStoreMetaScopes");
+    dalloc_add(dict, &s, char *);
+
+    array = talloc_zero(dict, sl_array_t);
+    s = talloc_strdup(array, "kMDQueryScopeComputer");
+    dalloc_add(array, &s, char *);
+    dalloc_add(dict, array, sl_array_t);
+
+    /* key/val 2 */
+    s = talloc_strdup(dict, "kMDSStorePathScopes");
+    dalloc_add(dict, &s, char *);
+
+    array = talloc_zero(dict, sl_array_t);
+    s = talloc_strdup(array, v->v_path);
+    dalloc_add(array, &s, char *);
+    dalloc_add(dict, array, sl_array_t);
+
+    /* key/val 3 */
+    s = talloc_strdup(dict, "kMDSStoreUUID");
+    dalloc_add(dict, &s, char *);
+
+    memcpy(uuid.sl_uuid, v->v_uuid, 16);
+    dalloc_add(dict, &uuid, sl_uuid_t);
+
+    /* key/val 4 */
+    s = talloc_strdup(dict, "kMDSStoreHasPersistentUUID");
+    dalloc_add(dict, &s, char *);
+    sl_bool_t b = true;
+    dalloc_add(dict, &b, sl_bool_t);
+
+    dalloc_add(reply, dict, sl_dict_t);
+
+EC_CLEANUP:
+    EC_EXIT;
+}
+
 /**************************************************************************************************
  * AFP functions
  **************************************************************************************************/
@@ -772,8 +837,35 @@ int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_
     case SPOTLIGHT_CMD_RPC: {
         DALLOC_CTX *query;
         EC_NULL( query = talloc_zero(tmp_ctx, DALLOC_CTX) );
-        (void)dissect_spotlight(query, ibuf + 22);
+        DALLOC_CTX *reply;
+        EC_NULL( reply = talloc_zero(tmp_ctx, DALLOC_CTX) );
+
+        EC_ZERO( dissect_spotlight(query, ibuf + 22) );
         dd_dump(query, 0);
+
+        char **cmd;
+        EC_NULL_LOG( cmd = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "char *", 0) );
+
+
+        if (STRCMP(*cmd, ==, "fetchPropertiesForContext:")) {
+            EC_ZERO_LOG( sl_rpc_fetchPropertiesForContext(obj, query, reply, vol) );
+        } else if (STRCMP(*cmd, ==, "fetchQueryResultsForContext:")) {
+            uint64_t *p;
+            if ((p = dalloc_get(query, "DALLOC_CTX", 0, "DALLOC_CTX", 0, "uint64_t", 1)) != NULL) {
+                LOG(log_info, logtype_sl, "fetchQueryResultsForContext: 0x%" PRIx64, *p);
+            }
+        }
+
+        /* Spotlight RPC status code ? 0 in all traces, we use 0xffffffff for an error, never seen from Apple */
+        if (ret == 0)
+            memset(rbuf, 0, 4);
+        else
+            memset(rbuf, 0xff, 4);
+        *rbuflen += 4;
+
+        int len;
+        EC_NEG1_LOG( len = sl_pack(reply, rbuf + 4) );
+        *rbuflen += len;
         break;
     }
     }
@@ -781,6 +873,7 @@ int afp_spotlight_rpc(AFPObj *obj, char *ibuf, size_t ibuflen, char *rbuf, size_
 EC_CLEANUP:
     talloc_free(tmp_ctx);
     if (ret != AFP_OK) {
+        *rbuflen = 0;
         return AFPERR_MISC;
     }
     EC_EXIT;
@@ -840,6 +933,8 @@ int main(int argc, char **argv)
     dalloc_add(cnids->ca_cnids, &id, uint32_t);
     dalloc_add(dd, cnids, sl_cnids_t);
 
+#endif
+
     /* Now the Spotlight types */
     sl_array_t *sl_arrary = talloc_zero(dd, sl_array_t);
     i = 1234;
@@ -851,8 +946,10 @@ int main(int argc, char **argv)
     dalloc_add(sl_arrary, sl_dict, sl_dict_t);
 
     dalloc_add(dd, sl_arrary, sl_array_t);
-#endif
+    dd_dump(dd, 0);
+
 
+#if 0
     /* now parse a real spotlight packet */
     char ibuf[8192];
     char rbuf[8192];
@@ -862,7 +959,7 @@ int main(int argc, char **argv)
 
     EC_NULL( query = talloc_zero(mem_ctx, DALLOC_CTX) );
 
-    EC_NEG1_LOG( fd = open("spotlight-packet2.bin", O_RDONLY) );
+    EC_NEG1_LOG( fd = open("spotlight-packet.bin", O_RDONLY) );
     EC_NEG1_LOG( len = read(fd, ibuf, 8192) );
     close(fd);
     EC_NEG1_LOG( dissect_spotlight(query, ibuf + 24) );
@@ -874,27 +971,12 @@ int main(int argc, char **argv)
     char buf[MAX_SLQ_DAT];
     EC_NEG1_LOG( qlen = sl_pack(query, buf) );
 
-    EC_NEG1_LOG( fd = open("tmp", O_RDWR | O_CREAT, 0644) );
+    EC_NEG1_LOG( fd = open("test.bin", O_RDWR) );
+    lseek(fd, 24, SEEK_SET);
     write(fd, buf, qlen);
     close(fd);
-
-    SLVAL(buf, 0, INT64_C(0x0102030405060708U));
-
-#if 0
-    (((unsigned char *)((buf)))[(0)])         = (unsigned char)((((uint64_t)(0x0102030405060708UL))&0xFFFFFFFF&0xFFFF) &0xFF);
-    (((unsigned char *)((buf)))[(0)+1])       = (unsigned char)((((uint64_t)(0x0102030405060708UL))&0xFFFFFFFF&0xFFFF) >> 8);
-    (((unsigned char *)((buf)))[(0)+2])       = (unsigned char)((((uint64_t)(0x0102030405060708UL))&0xFFFFFFFF>>16) & 0xFF);
-    (((unsigned char *)((buf)))[(0)+2 +1])    = (unsigned char)((((uint64_t)(0x0102030405060708UL))&0xFFFFFFFF>>16)>>8);
-    (((unsigned char *)((buf)))[(0)+4])       = (unsigned char)((((uint64_t)(0x0102030405060708UL))>>32&0xFFFF)&0xFF);
-    (((unsigned char *)((buf)))[(0)+4 +1])    = (unsigned char)((((uint64_t)(0x0102030405060708UL))>>32&0xFFFF)>>8);
-    (((unsigned char *)((buf)))[(0)+4 +2])    = (unsigned char)((((uint64_t)(0x0102030405060708UL))>>32>>16)&0xFF);
-    (((unsigned char *)((buf)))[(0)+4 +2 +1]) = (unsigned char)((((uint64_t)(0x0102030405060708UL))>>32>>16)>>8);
 #endif
 
-    EC_NEG1_LOG( fd = open("tmp2", O_RDWR | O_CREAT, 0644) );
-    write(fd, buf, 8);
-    close(fd);
-
 EC_CLEANUP:
     if (mem_ctx) {
         talloc_free(mem_ctx);