From: <svn...@op...> - 2009-04-11 18:29:42
|
Author: scriptor Date: Sat Apr 11 20:29:35 2009 New Revision: 5579 URL: http://www.opensync.org/changeset/5579 Log: Fixed bug in ldap_format_print_format1(). Fixed some error messages and other issues. Modified: plugins/ldap-sync/src/ldap_format.c plugins/ldap-sync/src/ldap_format.h Modified: plugins/ldap-sync/src/ldap_format.c ============================================================================== --- plugins/ldap-sync/src/ldap_format.c Sat Apr 11 20:29:13 2009 (r5578) +++ plugins/ldap-sync/src/ldap_format.c Sat Apr 11 20:29:35 2009 (r5579) @@ -1965,12 +1965,12 @@ if (!strcmp((char *)root_element->name, "contact")) { if (strcmp(stylesheet_file, STYLESHEET_XMLFORMAT_CONTACT2LDAP_EVOLUTIONPERSON) && (strcmp(stylesheet_file, STYLESHEET_XMLFORMAT_CONTACT2LDAP_INETORGPERSON))) { - osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: For this root_element \"%s\" is \"%s\" the wrong style sheet file. %s() is not prepared to convert this.\n", __FILE__, __LINE__, root_element->name, stylesheet_file, __FILE__); + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: For this root_element \"%s\" is \"%s\" the wrong style sheet file. %s is not prepared to convert this.\n", __FILE__, __LINE__, root_element->name, stylesheet_file, __FILE__); #ifdef CALL_ABORT - osync_trace(TRACE_ERROR, "%s:%i: ERROR: For this root_element \"%s\" is \"%s\" the wrong style sheet file. %s() is not prepared to convert this. Calling abort().\n", __FILE__, __LINE__, root_element->name, stylesheet_file, __FILE__); - ldap_plugin_printf("%s:%i: ERROR: For this root_element \"%s\" is \"%s\" the wrong style sheet file. %s() is not prepared to convert this. Calling abort().\n", __FILE__, __LINE__, root_element->name, stylesheet_file, __FILE__); - fprintf(stderr, "%s:%i: ERROR: For this root_element \"%s\" is \"%s\" the wrong style sheet file. %s() is not prepared to convert this. Calling abort().\n", __FILE__, __LINE__, root_element->name, stylesheet_file, __FILE__); + osync_trace(TRACE_ERROR, "%s:%i: ERROR: For this root_element \"%s\" is \"%s\" the wrong style sheet file. %s is not prepared to convert this. Calling abort().\n", __FILE__, __LINE__, root_element->name, stylesheet_file, __FILE__); + ldap_plugin_printf("%s:%i: ERROR: For this root_element \"%s\" is \"%s\" the wrong style sheet file. %s is not prepared to convert this. Calling abort().\n", __FILE__, __LINE__, root_element->name, stylesheet_file, __FILE__); + fprintf(stderr, "%s:%i: ERROR: For this root_element \"%s\" is \"%s\" the wrong style sheet file. %s is not prepared to convert this. Calling abort().\n", __FILE__, __LINE__, root_element->name, stylesheet_file, __FILE__); fflush(stderr); abort(); @@ -2230,10 +2230,13 @@ xmlSubstituteEntitiesDefault(1); xmlLoadExtDtdDefaultValue = 1; + + // First off, try and find out what type of data has been casted to char *: if (inpsize >= 6 && !strncmp(input, "<?xml ", 6)) { // Convert an xmlChar string of the input into an xmlDoc struct: input_doc = xmlParseMemory(input, inpsize); + } else { osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: input does not seem to be a human readable xml string.\n", __FILE__, __LINE__); @@ -2241,12 +2244,16 @@ if (inpsize == osync_xmlformat_size()) { osync_trace(TRACE_ERROR, "%s:%i: ERROR: Looks like opensync specific xmlformat.\n", __FILE__, __LINE__); + } else if (inpsize == 0) { osync_trace(TRACE_ERROR, "%s:%i: ERROR: inpsize = 0.\n", __FILE__, __LINE__); - } else if (inpsize == sizeof(GList)) { - osync_trace(TRACE_ERROR, "%s:%i: ERROR: Looks like a GList.", __FILE__, __LINE__); + } else if (inpsize == sizeof(glist_container)) { osync_trace(TRACE_ERROR, "%s:%i: ERROR: Looks like a glist_container.", __FILE__, __LINE__); + + } else if (inpsize == sizeof(GList)) { + osync_trace(TRACE_ERROR, "%s:%i: ERROR: Looks like a GList.", __FILE__, __LINE__); + } else { osync_trace(TRACE_ERROR, "%s:%i: ERROR: Could be xmlinternal?\n", __FILE__, __LINE__); } @@ -4718,19 +4725,20 @@ } + // First off, try and find out what type of data has been casted to char *: if (inpsize != sizeof(glist_container)) { - // osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: input is NOT a glist_container.\n", __FILE__, __LINE__); - osync_trace(TRACE_ERROR, "%s:%i: What is input? inpsize = %i", __FILE__, __LINE__, inpsize); + osync_trace(TRACE_INTERNAL, "%s:%i: What is input? inpsize = %i", __FILE__, __LINE__, inpsize); if (inpsize == sizeof(GList)) { - osync_trace(TRACE_ERROR, "%s:%i: input looks like a GList.", __FILE__, __LINE__); + osync_trace(TRACE_INTERNAL, "%s:%i: input looks like a GList.", __FILE__, __LINE__); } else if (inpsize == sizeof(ldap_entry)) { - osync_trace(TRACE_ERROR, "%s:%i: input looks like an ldap_entry.", __FILE__, __LINE__); + osync_trace(TRACE_INTERNAL, "%s:%i: input looks like an ldap_entry.", __FILE__, __LINE__); + } else if (inpsize == osync_xmlformat_size()) { - osync_trace(TRACE_ERROR, "%s:%i: input looks like an OsyncXMLFormat.\n", __FILE__, __LINE__); + osync_trace(TRACE_INTERNAL, "%s:%i: input looks like an OsyncXMLFormat.\n", __FILE__, __LINE__); #ifdef CALL_ABORT @@ -4748,20 +4756,20 @@ if (!osync_xmlformat_assemble((OSyncXMLFormat *)input, (char **) &xmlbuff, (unsigned int *)&size)) { - osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: input is NOT a glist_container. Looks like an OSyncXMLFormat.\n", __FILE__, __LINE__); + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: input is NOT a glist_container. Looks like an OSyncXMLFormat. But osync_xmlformat_assemble() has failed for an unknown reason. Giving up.", __FILE__, __LINE__); osync_trace(TRACE_ERROR, "%s:%i: ERROR: osync_xmlformat_assemble() has failed for an unknown reason. Giving up.\n", __FILE__, __LINE__); goto error; } if (xmlbuff == NULL) { - osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: input is NOT a glist_container. Looks like an OSyncXMLFormat.\n", __FILE__, __LINE__); + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: input is NOT a glist_container. Looks like an OSyncXMLFormat. But xmlbuff = NULL. osync_xmlformat_assemble() must have failed for an unknown reason. Giving up.\n", __FILE__, __LINE__); osync_trace(TRACE_ERROR, "%s:%i: ERROR: xmlbuff = NULL. osync_xmlformat_assemble() must have failed for an unknown reason. Giving up.\n", __FILE__, __LINE__); goto error; } if (size < 6) { - osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: input is NOT a glist_container. Looks like an OSyncXMLFormat.\n", __FILE__, __LINE__); + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: input is NOT a glist_container. Looks like an OSyncXMLFormat. But size = %i. osync_xmlformat_assemble() must have failed for an unknown reason. Giving up.\n", __FILE__, __LINE__, size); osync_trace(TRACE_ERROR, "%s:%i: ERROR: size = %i. osync_xmlformat_assemble() must have failed for an unknown reason. Giving up.\n", __FILE__, __LINE__, size); goto error; @@ -4774,6 +4782,13 @@ #endif + } else if (inpsize > 6 && ldap_format_check_xml((char *)input, (int)inpsize, error)) { + osync_trace(TRACE_INTERNAL, "%s:%i: This is a string with a human readable XML document.", __FILE__, __LINE__); + xmlbuff = (xmlChar *)g_strdup(input); + size = inpsize; + goto applying_stylesheet; + + } else { osync_trace(TRACE_ERROR, "%s:%i: Don't know. inpsize = %u\n", __FILE__, __LINE__, inpsize); } @@ -4782,7 +4797,8 @@ } - // Check the magic string + // Assuming it is a glist_container struct: + // Check the magic string and extract the included GList. if (!ldap_plugin_get_glist(input, &list, error)) { osync_trace(TRACE_ERROR, "%s:%i: ERROR: ldap_plugin_get_glist() has failed.\n", __FILE__, __LINE__); @@ -4818,9 +4834,7 @@ } -#ifndef CALL_ABORT applying_stylesheet: -#endif // Apply stylesheet @@ -5005,6 +5019,10 @@ * 2. From this plugin-internal XML format into ldap_entry structs * which can libldap be fed with. * + * This is one of two locations in the LDAP plugin, where a + * glist_container is generated. + * See also ldap_plugin_report_change() in ldap_plugin.c. + * * @param input The input: The XML document in a format as used by libopensync. * @param inpsize: Its size. * @param output The resulting list of LDAP entries. @@ -5040,7 +5058,7 @@ char *xmlformat = NULL; unsigned int xmlformat_size = 0; GList *ldap_entries = NULL; - glist_container *container = NULL; + glist_container *container = NULL; osync_trace(TRACE_ENTRY, "%s(%p, %u, %p, %p, %p, %p, %p, %p, %p, %p, %p, %p)", __func__, input, inpsize, output, outpsize, free_input, xmlformat_name, ldap_format_name, stylesheet_name, objtype, config, userdata, error); @@ -5142,6 +5160,7 @@ container = (glist_container *)g_malloc0(sizeof(glist_container) + 1); osync_assert(container); container->magic = "glist_container"; + container->format_name = ldap_format_name; container->list = ldap_entries; osync_assert(output); @@ -5719,6 +5738,7 @@ osync_trace(TRACE_INTERNAL, "%s:%i:%s():\n\n\ndestroy function has been called for input = %p\n\n\n", __FILE__, __LINE__, __func__, input); + // First off, try and find out what type of data has been casted to char * osync_trace(TRACE_INTERNAL, "%s:%i: What is input? inpsize = %i", __FILE__, __LINE__, inpsize); @@ -5742,21 +5762,24 @@ if (inpsize == osync_xmlformat_size()) { osync_trace(TRACE_INTERNAL, "Looks like the opensync specific xmlformat.\n"); + } else if (inpsize == sizeof(ldap_entry)) { osync_trace(TRACE_INTERNAL, "Looks like an ldap_entry struct."); + } else if (inpsize == sizeof(xmlDoc)) { osync_trace(TRACE_INTERNAL, "Looks like an xmlDoc."); + } else if (inpsize == sizeof(glist_container)) { osync_trace(TRACE_INTERNAL, "Looks like a glist_container."); + } else if (inpsize == sizeof(GList)) { unsigned int length = 0; GList *list = (GList *)input; osync_trace(TRACE_INTERNAL, "Looks like a GList."); - length = g_list_length(list); - osync_trace(TRACE_INTERNAL, "With %u elements.", length); + } else { osync_trace(TRACE_INTERNAL, "Don't know."); } @@ -5834,41 +5857,138 @@ unsigned int i = 0, j = 0; ldap_entry *entry = NULL; GString *str = NULL; + xmlChar *xmlbuff = NULL; + unsigned int xmlbuff_size = 0; + OSyncError *error = NULL; + + osync_trace(TRACE_ENTRY, "%s(%p, %d, %p) ", __func__, data, size, user_data); - osync_trace(TRACE_ENTRY, "%s(%p, %d, %p) ", __func__, data, size, user_data); if (data == NULL) { osync_trace(TRACE_ERROR, "%s:%i: ERROR: data = NULL. Returning NULL.\n", __FILE__, __LINE__); goto error; } - if (size != sizeof(GList)) { - osync_trace(TRACE_ERROR, "%s:%i:%s(): WARNING: size = %u bytes. Ignoring.\n", __FILE__, __LINE__, __func__, size); + + + if (size < sizeof(glist_container)) { + +#ifdef CALL_ABORT + osync_trace(TRACE_ERROR, "%s:%i: ERROR: size = %i. Calling abort()\n", __FILE__, __LINE__, size); + ldap_plugin_printf("%s:%i: ERROR: size = %i. Calling abort()\n", __FILE__, __LINE__, size); + abort(); +#else + osync_trace(TRACE_ERROR, "%s:%i: ERROR: size = %i.\n", __FILE__, __LINE__, size); + goto error; +#endif + + } + + + // First off, try and find out what type of data has been casted to char *: + if (size == sizeof(glist_container) && ((glist_container *)data)->magic && !strcmp(((glist_container *)data)->magic, "glist_container")) { + if (!ldap_plugin_get_glist(data, &list, &error)) { + osync_trace(TRACE_ERROR, "%s:%i: ERROR: ldap_plugin_get_glist() has failed.\n", __FILE__, __LINE__); + + if (!osync_error_is_set(&error)) + osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: ldap_plugin_get_glist() has failed.\n", __FILE__, __LINE__); + + goto error; + } + } else { + osync_trace(TRACE_INTERNAL, "%s:%i: What is data? size = %i", __FILE__, __LINE__, size); + + + if (size == sizeof(GList)) { + osync_trace(TRACE_ERROR, "%s:%i: data looks like a GList.", __FILE__, __LINE__); + list = (GList *)data; + + } else if (size == sizeof(ldap_entry)) { + osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: data looks like an ldap_entry. This is completely wrong.", __FILE__, __LINE__); + goto error; + + } else if (size == osync_xmlformat_size()) { + osync_trace(TRACE_ERROR, "%s:%i: data looks like an OsyncXMLFormat.\n", __FILE__, __LINE__); + + +#ifdef CALL_ABORT + osync_trace(TRACE_ERROR, "%s:%i: ERROR: data is NOT a glist_container. Looks like an OSyncXMLFormat.\n", __FILE__, __LINE__); + osync_trace(TRACE_ERROR, "%s:%i: Where does that come from? Calling abort().", __FILE__, __LINE__); + ldap_plugin_printf("%s:%i: Where does that come from? Calling abort().", __FILE__, __LINE__); + fprintf(stderr, "%s:%i: Where does that come from? Calling abort().\n\n", __FILE__, __LINE__); + fflush(stderr); + abort(); + +#else + osync_assert(data); + osync_trace(TRACE_INTERNAL, "%s:%i: We can handle this. If you prefer getting a core dump, define CALL_ABORT in ldap_plugin.h and recompile the LDAP format plugin.", __FILE__, __LINE__); + + + + if (!osync_xmlformat_assemble((OSyncXMLFormat *)data, (char **) &xmlbuff, &xmlbuff_size)) { + osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: data is NOT a glist_container. Looks like an OSyncXMLFormat. But osync_xmlformat_assemble() has failed for an unknown reason. Giving up.\n", __FILE__, __LINE__); + goto error; + } + + if (xmlbuff == NULL) { + osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: data is NOT a glist_container. Looks like an OSyncXMLFormat. But xmlbuff = NULL. osync_xmlformat_assemble() must have failed for an unknown reason. Giving up.\n", __FILE__, __LINE__); + goto error; + } + + + if (xmlbuff_size < 6) { + osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: data is NOT a glist_container. Looks like an OSyncXMLFormat. But xmlbuff_size = %i. sync_xmlformat_assemble() must have failed for an unknown reason. Giving up. size = %i.", __FILE__, __LINE__, xmlbuff_size, size); + goto error; + } + + + osync_trace(TRACE_INTERNAL, "%s:%i: This is the content of the OSyncXMLFormat:\n\"%.*s\"", __FILE__, __LINE__, xmlbuff_size, xmlbuff); + + osync_trace(TRACE_EXIT, "%s(): str = %p", __func__, xmlbuff); + return (char *)xmlbuff; + +#endif + + + } else { + osync_trace(TRACE_INTERNAL, "%s:%i: Don't know what that is: size = %u. We print it literally no matter what...\n", __FILE__, __LINE__, size); + osync_trace(TRACE_EXIT, "%s(): Returning data = %p", __func__, data); + return (char *)data; + } + + + // unreachable + osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: Don't know what that is. Bailing out."); + goto error; } - list = (GList *)data; + if (list == NULL) { + osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: list = NULL.", __FILE__, __LINE__); + goto error; + } + if (g_list_length(list) < 1) { - osync_trace(TRACE_ERROR, "%s:%i: ERROR: list has %u elements. Returning NULL.", __FILE__, __LINE__, g_list_length(list)); + osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: list has %u elements. Returning NULL.", __FILE__, __LINE__, g_list_length(list)); goto error; } str = g_string_new((const gchar *)"----- <list> -----\n"); if (str == NULL) { - osync_trace(TRACE_ERROR, "%s:%i: ERROR: str = NULL. Returning NULL.\n", __FILE__, __LINE__); + osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: str = NULL. Returning NULL.\n", __FILE__, __LINE__); goto error; } for (i = 0; i < g_list_length(list); i++) { - g_string_append_printf(str, "---------\n"); + g_string_append_printf(str, "\n"); entry = (ldap_entry *)g_list_nth_data(list, i); if (entry == NULL) { osync_trace(TRACE_ERROR, "%s:%i: ERROR: entry = NULL with i = %u. Ignoring this LDAP entry.\n", __FILE__, __LINE__, i); - g_string_append_printf(str, "---------\n"); + g_string_append_printf(str, "\n"); continue; } @@ -5884,7 +6004,7 @@ if (entry->mods == NULL) { osync_trace(TRACE_ERROR, "%s:%i: ERROR: entry->mods = NULL with i = %u\n", __FILE__, __LINE__, i); g_string_append_printf(str, "NULL.\n"); - g_string_append_printf(str, "---------\n"); + g_string_append_printf(str, "\n"); continue; } @@ -5893,7 +6013,7 @@ LDAPMod *mod = entry->mods[j]; if (mod) { if (mod->mod_type) { - g_string_append_printf(str, "%s: \t", mod->mod_type); + g_string_append_printf(str, "%s: ", mod->mod_type); if (mod->mod_bvalues) { if (mod->mod_bvalues[0]) { if (mod->mod_bvalues[0]->bv_val) { @@ -5916,21 +6036,25 @@ } } // for (j = 0; entry->mods[j]; j++) - g_string_append_printf(str, "---------\n"); + g_string_append_printf(str, "\n"); } // for (i = 0; i < g_list_length(list); i++) g_string_append_printf(str, "----- </list> -----\n"); - if (str && str->len) { - osync_trace(TRACE_INTERNAL, "%s:%i:%s():\nstr = \"%s\"\n", __FILE__, __LINE__, __func__, str); + if (str && str->len && str->str) { + osync_trace(TRACE_INTERNAL, "%s:%i:%s():\nstr->str = \"%s\"\n", __FILE__, __LINE__, __func__, str->str); } - osync_trace(TRACE_EXIT, "%s(): str = %p", __func__, str); - return (char *)str; + osync_trace(TRACE_EXIT, "%s(): str = %p, str->str = %p", __func__, str, str->str); + return (char *)str->str; error: - osync_trace(TRACE_EXIT_ERROR, "%s: Returning NULL.", __func__); + if (!osync_error_is_set(&error)) + osync_error_set(&error, OSYNC_ERROR_GENERIC, "Unknown reason.\n"); + + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(&error)); + osync_error_unref(&error); return NULL; } @@ -5961,111 +6085,80 @@ - - - /** - * @brief Main function to set up the different functions required - * for converting from and to the ldap related formats - * offered by this plugin. - * @param env The format environment. - * @param error libopensync error pointer. - * @returns TRUE on success, FALSE on error + * @brief This function performs some rudimentary check whether the byte + * sequence pointed to by data is a human readable XML document. + * This is a helper function for the detector functions and for the + * validator function. + * + * @param data This is the byte sequence to be checked. + * @param size This is the length of the byte sequence. + * @param error The libopensync error pointer. + * + * @returns TRUE if it IS an XML doc, FALSE, if not and in case of any error. + * */ -osync_bool get_format_info(OSyncFormatEnv *env, OSyncError **error) -{ - osync_trace(TRACE_ENTRY, "%s(%p, %p)", __func__, env, error); - +osync_bool ldap_format_check_xml(const char *data, int size, OSyncError **error) +{ + GRegex* regex = NULL; + osync_bool rv = FALSE; + GError *gerror = NULL; - OSyncObjFormat *contact1_format = osync_objformat_new(FORMAT_LDAP_EVOLUTIONPERSON, OBJECTTYPE_CONTACT, error); - OSyncObjFormat *contact2_format = osync_objformat_new(FORMAT_LDAP_INETORGPERSON, OBJECTTYPE_CONTACT, error); + osync_trace(TRACE_ENTRY, "%s(%p, %p, %p)", __func__, data, size, error); - OSyncObjFormat *event_format = osync_objformat_new(FORMAT_LDAP_EVENT, OBJECTTYPE_EVENT, error); - - OSyncObjFormat *todo_format = osync_objformat_new(FORMAT_LDAP_TODO, OBJECTTYPE_TODO, error); - - OSyncObjFormat *note_format = osync_objformat_new(FORMAT_LDAP_NOTE, OBJECTTYPE_NOTE, error); - - if (!contact1_format) { - osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: contact_format = NULL.\n", __FILE__, __LINE__); + if (data == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: data = NULL."); goto error; - } + } - if (!event_format) { - osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: event_format = NULL.\n", __FILE__, __LINE__); - goto error; - } - if (!todo_format) { - osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: todo_format = NULL.\n", __FILE__, __LINE__); + // Note: ? is a quantifier rather than a question mark. + // ? after another quantifier means that the preceding quantifier + // is "ungreedy". + regex = g_regex_new("\\s*?<\?\\s*?xml\\sversion\\s*=\\s*\"1\\.0\"\\s*[^?]*?\\?\\s*?>.?", 0, 0, &gerror); + + if (gerror) { + if (gerror->message) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: g_regex_new() has failed. The pattern must be wrong: %s", __FILE__, __LINE__, gerror->message); + } else { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: g_regex_new() has failed. The pattern must be wrong.", __FILE__, __LINE__); + } + goto error; } - if (!note_format) { - osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: note_format = NULL.\n", __FILE__, __LINE__); + if (regex == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: regex = NULL. g_regex_new() must have failed.", __FILE__, __LINE__); goto error; } + + if (g_regex_match(regex, data, 0, NULL)) { +#ifdef DEBUG_detection + osync_trace(TRACE_INTERNAL, "%s:%i: It IS an XML document.\n", __FILE__, __LINE__); +#endif - osync_objformat_set_compare_func(contact1_format, ldap_format_compare_format1); - osync_objformat_set_destroy_func(contact1_format, ldap_format_destroy_format1); - osync_objformat_set_duplicate_func(contact1_format, ldap_format_duplicate_format1); - osync_objformat_set_print_func(contact1_format, ldap_format_print_format1); - osync_objformat_set_validate_func(contact1_format, ldap_format_validate); - - - - osync_objformat_set_compare_func(contact2_format, ldap_format_compare_format1); - osync_objformat_set_destroy_func(contact2_format, ldap_format_destroy_format1); - osync_objformat_set_duplicate_func(contact2_format, ldap_format_duplicate_format1); - osync_objformat_set_print_func(contact2_format, ldap_format_print_format1); - osync_objformat_set_validate_func(contact2_format, ldap_format_validate); - - - osync_objformat_set_compare_func(event_format, ldap_format_compare_format1); - osync_objformat_set_destroy_func(event_format, ldap_format_destroy_format1); - osync_objformat_set_duplicate_func(event_format, ldap_format_duplicate_format1); - osync_objformat_set_print_func(event_format, ldap_format_print_format1); - osync_objformat_set_validate_func(event_format, ldap_format_validate); - - - osync_objformat_set_compare_func(todo_format, ldap_format_compare_format1); - osync_objformat_set_destroy_func(todo_format, ldap_format_destroy_format1); - osync_objformat_set_duplicate_func(todo_format, ldap_format_duplicate_format1); - osync_objformat_set_print_func(todo_format, ldap_format_print_format1); - osync_objformat_set_validate_func(todo_format, ldap_format_validate); - - - osync_objformat_set_compare_func(note_format, ldap_format_compare_format1); - osync_objformat_set_destroy_func(note_format, ldap_format_destroy_format1); - osync_objformat_set_duplicate_func(note_format, ldap_format_duplicate_format1); - osync_objformat_set_print_func(note_format, ldap_format_print_format1); - osync_objformat_set_validate_func(note_format, ldap_format_validate); - - + rv = TRUE; - - osync_format_env_register_objformat(env, contact1_format); - osync_format_env_register_objformat(env, contact2_format); - osync_format_env_register_objformat(env, event_format); - osync_format_env_register_objformat(env, todo_format); - osync_format_env_register_objformat(env, note_format); + } else { +#ifdef DEBUG_detection + osync_trace(TRACE_INTERNAL, "%s(): It is NOT an XML document.", __func__); +#endif + rv = FALSE; + } - osync_objformat_unref(contact1_format); - osync_objformat_unref(contact2_format); - osync_objformat_unref(event_format); - osync_objformat_unref(todo_format); - osync_objformat_unref(note_format); + if (regex) + g_regex_unref(regex); - osync_trace(TRACE_EXIT, "%s", __func__); - return TRUE; + osync_trace(TRACE_EXIT, "%s(): rv = %s", __func__, rv ? "TRUE" : "FALSE"); + return rv; error: if (!osync_error_is_set(error)) @@ -6076,105 +6169,846 @@ } -/** - * \typedef (* fn_ptr) - * @brief typedef for a function pointer to be used as parameter in - * ldap_format_do_get_conversion_info() - * @param input The data you need to convert - * @param inpsize The size of the input data - * - * @param output This will contain the result of the conversion - * @param outpsize The will contain the size of the output - * - * @param free_input - * "You need to set this to TRUE if opensync - * can free the input after the conversion (so you don't - * use any reference from or to the input). An example where - * *free_input = FALSE; needs to be done would be an encapsulator - * that stores the input reference somewhere in its struct." - * - * @param config ??? - * @param userdata ??? - * @param error if something bad happens and you cannot convert, set the error! - * - * @returns TRUE on success, FALSE on error - */ -typedef osync_bool (* fn_ptr) (char *input, unsigned int inpsize, char **output, unsigned int *outpsize, osync_bool *free_input, const char *config, void *userdata, OSyncError **error); - - - - - - /** - * @brief Helper function for setting up the converter functions. - * - * @param env Format specific environment. - * @param format_name Name of the first format. - * @param other_format_name of the second format. - * @param objtype Object type, such as "contact", "event", "todo", "note". - * @param converter1 Function pointer to a converter function. - * @param converter2 Function pointer to a converter function for the opposite direction. - * @param error libopensync error pointer. + * @brief This function performs the check of a byte sequence whether + * or not it is of the type of format the calling function + * expects. The caller tells this function about its expectation + * by stating the format_name, and the opening and closing tag + * of a potential XML document. + * + * Problem: + * + * At least three different kinds of data are washed ashore, here: + * + * - Real strings, char *, i.e. human readable forms of XML documents. + * - OSyncXMLFormat structs, + * - glist_container structs. This is the only type that is caused + * by the LDAP plugin. The LDAP plugin does not proliferate other + * kinds of data. + * + * Similar problems does ldap_format_do_apply_stylesheet() + * face from time to time. + * + * + * @param data The byte sequence to be checked. + * @param size The length of the byte sequence. + * @param format_name The name of the format to be checked. + * @param opening_tag How would the opening tag of an XML document look like, + * if it were of the expected format. + * @param closing_tag How would the closing tag of an XML document look like, + * if it were of the expected format. + * @param userdata ??????? + * @param error The libopensync error pointer. * - * @returns TRUE on success, FALSE on failure. + * @returns TRUE if it IS the type of format, the caller want to check by + * telling the format_name, the opening_tag and the closing_tag. + * FALSE if it is NOT the type of format the caller would have + * expected, and also in case of any error. + * */ -static osync_bool ldap_format_do_get_conversion_info(OSyncFormatEnv *env, const char *format_name, const char *other_format_name, const char *objtype, fn_ptr converter1, fn_ptr converter2, OSyncError **error) +osync_bool ldap_format_do_detect_plain_as_ldap_format(const char *data, const int size, const char *format_name, const char *opening_tag, const char *closing_tag, const void *userdata, OSyncError **error) { - osync_trace(TRACE_ENTRY, "%s(%p, %p, %p, %p, %p, %p, %p)", __func__, env, format_name, other_format_name, objtype, converter1, converter2, error); + osync_trace(TRACE_ENTRY, "%s(%p, %i, %p, %p, %p, %p, %p)", __func__, data, size, format_name, opening_tag, closing_tag, userdata, error); - if (env == NULL) { - osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: env = NULL.\n", __FILE__, __LINE__); + if (data == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: data = NULL."); goto error; } - + if (format_name == NULL) { - osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: format_name = NULL.\n", __FILE__, __LINE__); + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: format_name = NULL."); goto error; } - if (other_format_name == NULL) { - osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: other_format_name = NULL.\n", __FILE__, __LINE__); + if (format_name[0] == 0) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: format_name[0] = 0."); goto error; } - if (format_name == NULL) { - osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: format_name = NULL.\n", __FILE__, __LINE__); + if (opening_tag == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: opening_tag = NULL."); goto error; } - if (objtype == NULL) { - osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: objtype = NULL.\n", __FILE__, __LINE__); + if (opening_tag[0] == 0) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: opening_tag[0] = 0."); goto error; } - OSyncObjFormat *format1 = osync_format_env_find_objformat(env, format_name); - if (!format1) { - osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to find \"%s\" format", format_name); + + if (closing_tag == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: closing_tag = NULL."); goto error; } + if (closing_tag[0] == 0) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: closing_tag[0] = 0."); + goto error; + } + + + + +#ifdef DEBUG_detection + fprintf(stderr, "%s:%i: Input:\n", __FILE__, __LINE__); + GString *buffer = NULL; + + ldap_plugin_dump_bytes((unsigned char *)data, size < 64 ? size : 64, &buffer, error); + + if (buffer && buffer->str) { + fprintf(stderr, "%s\n\n", buffer->str); + } +#endif + + + // First off, try and find out what type of data has been casted to + // char *. The following possibilities have already been seen here: + // + // 1. A real string of a human readable XML document. I.e. a char *, + // that has NOT been casted, at all. An easy case. + // + // 2. A glist_container struct: This is the type of data used by the + // LDAP format plugin itself. It can clearly be identified by means + // of a magic string. Also an easy and, moreover, an unambigous case. + // + // 3. An OSyncXMLFormat. Who knows, where that comes from. The pointers + // in it seem all to be invalid. Any try to call osync_xmlformat_assemble() + // lead to SIGSEGV. Therefore this function call can be commented out + // by undefining HANDLE_OSYNC_XMLFORMAT in ldap_plugin.h. + // + if (size == sizeof(glist_container)) { + glist_container *container = (glist_container *)data; + if (container->magic) { + if (!strcmp(container->magic, "glist_container")) { + + osync_trace(TRACE_INTERNAL, "%s:%i: data looks like a glist_container.", __FILE__, __LINE__); + + if (container->format_name == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: container->format_name = NULL.", __FILE__, __LINE__); + goto error; + } + + if (container->format_name[0] == 0) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: container->format_name[0] = 0.", __FILE__, __LINE__); + goto error; + } + + +#ifdef DEBUG_detection + ldap_plugin_printf("%s:%i: \n" + "container->format_name = \"%s\", \n" + "format_name = \"%s\".", + __FILE__, __LINE__, container->format_name, format_name); +#endif + + if (!strcmp(container->format_name, format_name)) { + + osync_trace(TRACE_EXIT, "%s(): Returning TRUE.", __func__); + return TRUE; + } else { + osync_trace(TRACE_EXIT, "%s(): Returning FALSE.", __func__); + return FALSE; + } + + + osync_trace(TRACE_EXIT, "%s(): Unreachable Code! Returning FALSE.", __func__); + return FALSE; + + + } else { + osync_trace(TRACE_ERROR, "%s:%i: ERROR: data happened to have the same size as a glist_container struct. But its magic is different. So this is NOT a glist_container.", __FILE__, __LINE__); + goto regular_way; + } + } else { + osync_trace(TRACE_ERROR, "%s:%i: ERROR: data happened to have the same size as a glist_container struct. But its magic is NULL. So this is NOT a glist_container.", __FILE__, __LINE__); + goto regular_way; + } + + } else if ((unsigned int)size == osync_xmlformat_size()) { +#ifdef HANDLE_OSYNC_XMLFORMAT + char *xmlbuff = NULL; + unsigned int xmlbuff_size = 0; +#endif + + osync_trace(TRACE_INTERNAL, "%s:%i: data looks like an OSyncXMLFormat. This is most probably NOT an LDAP specific format.", __FILE__, __LINE__); + + + +#ifdef CALL_ABORT + osync_trace(TRACE_ERROR, "%s:%i: Where does that come from? Calling abort().", __FILE__, __LINE__); + ldap_plugin_printf("%s:%i: Where does that come from? Calling abort().", __FILE__, __LINE__); + printf(stderr, "%s:%i: Where does that come from? Calling abort().\n\n", __FILE__, __LINE__); + fflush(stderr); + abort(); +#endif + + + + +#ifdef HANDLE_OSYNC_XMLFORMAT + osync_assert(data); + if (!osync_xmlformat_assemble((OSyncXMLFormat *)data, &xmlbuff, &xmlbuff_size)) { + osync_trace(TRACE_ERROR, "%s:%i: ERROR: osync_xmlformat_assemble() has failed for an unknown reason. Giving up.\n", __FILE__, __LINE__); + goto error; + } + + if (xmlbuff == NULL) { + osync_trace(TRACE_ERROR, "%s:%i: ERROR: xmlbuff = NULL. osync_xmlformat_assemble() must have failed for an unknown reason. Giving up.\n", __FILE__, __LINE__); + goto error; + } + + + if (xmlbuff_size < 6) { + osync_trace(TRACE_ERROR, "%s:%i: ERROR: xmlbuff_size = %i. osync_xmlformat_assemble() must have failed for an unknown reason. Giving up.\n", __FILE__, __LINE__, xmlbuff_size); + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: xmlbuff_size = %i. osync_xmlformat_assemble() must have failed for an unknown reason. Giving up.\n", __FILE__, __LINE__, xmlbuff_size); + GString *str = NULL; + ldap_plugin_dump_bytes((unsigned char *)xmlbuff, (int)xmlbuff_size, &str, error); + if (str && str->str) { + ldap_plugin_printf("%s", str->str); + } + + goto error; + } + + + osync_trace(TRACE_INTERNAL, "%s:%i: This is the content of the OSyncXMLFormat:\n\n\n\"%.*s\"\n\n\n", __FILE__, __LINE__, xmlbuff_size, xmlbuff); + +#endif + + + osync_trace(TRACE_EXIT, "%s(): Returning FALSE.", __func__); + return FALSE; + + } else if (size == sizeof(ldap_entry)) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: data looks like an ldap_entry struct. This is totally wrong. How could that even happen?", __FILE__, __LINE__); + goto error; + + } else if (size == sizeof(xmlDoc)) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: data looks like an xmlDoc.", __FILE__, __LINE__); + goto error; + + } else if (size == sizeof(GList)) { + unsigned int i = 0; + + + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: data looks like a GList.", __FILE__, __LINE__); + + osync_trace(TRACE_ERROR, "ERROR: data looks like a GList with %u elements.", g_list_length((GList *)data)); + + for (i = 0; i < g_list_length((GList *)data); i++) { + ldap_entry *entry = g_list_nth_data((GList *)data, i); + if (entry == NULL) { + osync_trace(TRACE_ERROR, "%s:%i: ERROR: entry = NULL with i = %u.\n", __FILE__, __LINE__, i); + continue; + } + + if (entry->dn) { + ldap_plugin_printf("entry->dn = \"%s\"", entry->dn); + } + } + + + goto error; + } + + + + +regular_way: + + if (!ldap_format_check_xml(data, size, error)) { + osync_trace(TRACE_INTERNAL, "%s:%i: ldap_format_check_xml() has NOT recognized input as an XML document.", __FILE__, __LINE__); + osync_trace(TRACE_EXIT, "%s(): Returning FALSE.", __func__); + return FALSE; + } + + + if (g_strstr_len(data, size, opening_tag) && g_strstr_len(data, size, closing_tag)) { + osync_trace(TRACE_EXIT, "%s(): Returning TRUE.", __func__); + return TRUE; + } else { + osync_trace(TRACE_EXIT, "%s(): Returning FALSE.", __func__); + return FALSE; + } + + + osync_trace(TRACE_EXIT, "%s(): Unreachable code! Returning FALSE.", __func__); + return FALSE; + + +error: + if (!osync_error_is_set(error)) + osync_error_set(error, OSYNC_ERROR_GENERIC, "Unknown reason.\n"); + + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); + return FALSE; +} + + + + +/** + * @brief This function checks whether a byte sequence contains an + * ldap-evolutionperson format. + * + * @param data The byte sequence to be checked. + * @param size The length of the byte sequence. + * @param userdata ??????????????? + * + * @returns TRUE if the byte sequence is of the ldap-evolution format. + * FALSE if not, and in case of any errors. + * + */ + +osync_bool ldap_format_detect_plain_as_ldap_evolutionperson(const char *data, const int size, void *userdata) +{ + OSyncError *error = NULL; + + osync_trace(TRACE_ENTRY, "%s(%p, %i, %p)", __func__, data, size, userdata); + + + if (data == NULL) { + osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: data = NULL."); + goto error; + } + + + if (ldap_format_do_detect_plain_as_ldap_format(data, size, "ldap-evolutionperson", "<ldap-evolutionperson>", "</ldap-evolutionperson>", userdata, &error)) { +#ifdef DEBUG_detection + osync_trace(TRACE_INTERNAL, "%s:%i: It IS an ldap-evolutionperson format.\n", __FILE__, __LINE__); +#endif + + if (osync_error_is_set(&error)) { + goto error; + } + + osync_trace(TRACE_EXIT, "%s", __func__); + return TRUE; + + } else { +#ifdef DEBUG_detection + osync_trace(TRACE_INTERNAL, "%s:%i: It is NOT an ldap-evolutionperson format.\n", __FILE__, __LINE__); +#endif + + if (osync_error_is_set(&error)) { + goto error; + } + + osync_trace(TRACE_EXIT, "%s", __func__); + return FALSE; + } + + + osync_trace(TRACE_EXIT, "%s", __func__); + return FALSE; + +error: + if (!osync_error_is_set(&error)) + osync_error_set(&error, OSYNC_ERROR_GENERIC, "Unknown reason.\n"); + + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(&error)); + return FALSE; +} + + + + + + +/** + * @brief This function checks whether a byte sequence contains an + * ldap-inetorgperson format. + * + * @param data The byte sequence to be checked. + * @param size The length of the byte sequence. + * @param userdata ??????????????? + * + * @returns TRUE if the byte sequence is of the ldap-inetorgperson format. + * FALSE if not, and in case of any errors. + * + */ + +osync_bool ldap_format_detect_plain_as_ldap_inetorgperson(const char *data, int size, void *userdata) +{ + OSyncError *error = NULL; + + + osync_trace(TRACE_ENTRY, "%s(%p, %i, %p)", __func__, data, size, userdata); + + + if (data == NULL) { + osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: data = NULL."); + goto error; + } + + + if (ldap_format_do_detect_plain_as_ldap_format(data, size, "ldap-inetorgperson", "<ldap-inetorgperson>", "</ldap-inetorgperson>", userdata, &error)) { +#ifdef DEBUG_detection + ldap_plugin_printf("%s:%i: It IS an ldap-inetorgperson format.\n", __FILE__, __LINE__); +#endif + + if (osync_error_is_set(&error)) { + goto error; + } + + osync_trace(TRACE_EXIT, "%s", __func__); + return TRUE; + + } else { +#ifdef DEBUG_detection + osync_trace(TRACE_INTERNAL, "%s:%i: It is NOT an ldap-inetorgperson format.\n", __FILE__, __LINE__); +#endif + + if (osync_error_is_set(&error)) { + goto error; + } + + osync_trace(TRACE_EXIT, "%s", __func__); + return FALSE; + } + + + osync_trace(TRACE_EXIT, "%s", __func__); + return FALSE; + +error: + if (!osync_error_is_set(&error)) + osync_error_set(&error, OSYNC_ERROR_GENERIC, "Unknown reason.\n"); + + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(&error)); + return FALSE; +} + + + + +/** + * @brief This function checks whether a byte sequence contains an + * ldap-event format. + * + * @param data The byte sequence to be checked. + * @param size The length of the byte sequence. + * @param userdata ??????????????? + * + * @returns TRUE if the byte sequence is of the ldap-event format. + * FALSE if not, and in case of any errors. + * + */ + +osync_bool ldap_format_detect_plain_as_ldap_event(const char *data, int size, void *userdata) +{ + OSyncError *error = NULL; + + + osync_trace(TRACE_ENTRY, "%s(%p, %i, %p)", __func__, data, size, userdata); + + + if (data == NULL) { + osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: data = NULL."); + goto error; + } + + + if (ldap_format_do_detect_plain_as_ldap_format(data, size, "ldap-event", "<ldap-event>", "</ldap-event>", userdata, &error)) { +#ifdef DEBUG_detection + ldap_plugin_printf("%s:%i: It IS an ldap-event format.\n", __FILE__, __LINE__); +#endif + + if (osync_error_is_set(&error)) { + goto error; + } + + osync_trace(TRACE_EXIT, "%s", __func__); + return TRUE; + + } else { +#ifdef DEBUG_detection + osync_trace(TRACE_INTERNAL, "%s:%i: It is NOT an ldap-event format.\n", __FILE__, __LINE__); +#endif + + if (osync_error_is_set(&error)) { + goto error; + } + + osync_trace(TRACE_EXIT, "%s", __func__); + return FALSE; + } + + + osync_trace(TRACE_EXIT, "%s", __func__); + return FALSE; + +error: + if (!osync_error_is_set(&error)) + osync_error_set(&error, OSYNC_ERROR_GENERIC, "Unknown reason.\n"); + + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(&error)); + return FALSE; +} + + + +/** + * @brief This function checks whether a byte sequence contains an + * ldap-todo format. + * + * @param data The byte sequence to be checked. + * @param size The length of the byte sequence. + * @param userdata ??????????????? + * + * @returns TRUE if the byte sequence is of the ldap-todo format. + * FALSE if not, and in case of any errors. + * + */ + +osync_bool ldap_format_detect_plain_as_ldap_todo(const char *data, int size, void *userdata) +{ + OSyncError *error = NULL; + + + + osync_trace(TRACE_ENTRY, "%s(%p, %i, %p)", __func__, data, size, userdata); + + + if (data == NULL) { + osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: data = NULL."); + goto error; + } + + + + if (ldap_format_do_detect_plain_as_ldap_format(data, size, "ldap-todo", "<ldap-todo>", "</ldap-todo>", userdata, &error)) { +#ifdef DEBUG_detection + ldap_plugin_printf("%s:%i: It IS an ldap-todo format.\n", __FILE__, __LINE__); +#endif + + if (osync_error_is_set(&error)) { + goto error; + } + + osync_trace(TRACE_EXIT, "%s", __func__); + return TRUE; + + } else { +#ifdef DEBUG_detection + osync_trace(TRACE_INTERNAL, "%s:%i: It is NOT an ldap-todo.\n", __FILE__, __LINE__); +#endif + + if (osync_error_is_set(&error)) { + goto error; + } + + osync_trace(TRACE_EXIT, "%s", __func__); + return FALSE; + } + + + + osync_trace(TRACE_EXIT, "%s", __func__); + return FALSE; + +error: + if (!osync_error_is_set(&error)) + osync_error_set(&error, OSYNC_ERROR_GENERIC, "Unknown reason.\n"); + + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(&error)); + return FALSE; +} + + + + + + +/** + * @brief This function checks whether a byte sequence contains an + * ldap-note format. + * + * @param data The byte sequence to be checked. + * @param size The length of the byte sequence. + * @param userdata ??????????????? + * + * @returns TRUE if the byte sequence is of the ldap-note format. + * FALSE if not, and in case of any errors. + * + */ + +osync_bool ldap_format_detect_plain_as_ldap_note(const char *data, int size, void *userdata) +{ + OSyncError *error = NULL; + + + + osync_trace(TRACE_ENTRY, "%s(%p, %i, %p)", __func__, data, size, userdata); + + if (data == NULL) { + osync_error_set(&error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: data = NULL."); + goto error; + } + + + if (ldap_format_do_detect_plain_as_ldap_format(data, size, "ldap-note", "<ldap-note>", "</ldap-note>", userdata, &error)) { +#ifdef DEBUG_detection + ldap_plugin_printf("%s:%i: It IS an ldap-note format.\n", __FILE__, __LINE__); +#endif + + if (osync_error_is_set(&error)) { + goto error; + } + + osync_trace(TRACE_EXIT, "%s", __func__); + return TRUE; + + } else { +#ifdef DEBUG_detection + osync_trace(TRACE_INTERNAL, "%s:%i: It is NOT an ldap-note format.\n", __FILE__, __LINE__); +#endif + + if (osync_error_is_set(&error)) { + goto error; + } + + osync_trace(TRACE_EXIT, "%s", __func__); + return FALSE; + } + + + osync_trace(TRACE_EXIT, "%s", __func__); + return FALSE; + +error: + if (!osync_error_is_set(&error)) + osync_error_set(&error, OSYNC_ERROR_GENERIC, "Unknown reason.\n"); + + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(&error)); + return FALSE; +} + + + + + + +/** + * @brief Main function to set up the different functions required + * for converting from and to the ldap related formats + * offered by this plugin. + * @param env The format environment. + * @param error libopensync error pointer. + * @returns TRUE on success, FALSE on error + */ +osync_bool get_format_info(OSyncFormatEnv *env, OSyncError **error) +{ + osync_trace(TRACE_ENTRY, "%s(%p, %p)", __func__, env, error); + + + + OSyncObjFormat *contact1_format = osync_objformat_new(FORMAT_LDAP_EVOLUTIONPERSON, OBJECTTYPE_CONTACT, error); + + OSyncObjFormat *contact2_format = osync_objformat_new(FORMAT_LDAP_INETORGPERSON, OBJECTTYPE_CONTACT, error); + + OSyncObjFormat *event_format = osync_objformat_new(FORMAT_LDAP_EVENT, OBJECTTYPE_EVENT, error); + + OSyncObjFormat *todo_format = osync_objformat_new(FORMAT_LDAP_TODO, OBJECTTYPE_TODO, error); + + OSyncObjFormat *note_format = osync_objformat_new(FORMAT_LDAP_NOTE, OBJECTTYPE_NOTE, error); + + + + if (!contact1_format) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: contact_format = NULL.\n", __FILE__, __LINE__); + goto error; + } + + if (!event_format) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: event_format = NULL.\n", __FILE__, __LINE__); + goto error; + } + + if (!todo_format) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: todo_format = NULL.\n", __FILE__, __LINE__); + goto error; + } + + if (!note_format) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: note_format = NULL.\n", __FILE__, __LINE__); + goto error; + } + + + + osync_objformat_set_compare_func(contact1_format, ldap_format_compare_format1); + osync_objformat_set_destroy_func(contact1_format, ldap_format_destroy_format1); + osync_objformat_set_duplicate_func(contact1_format, ldap_format_duplicate_format1); + osync_objformat_set_print_func(contact1_format, ldap_format_print_format1); + osync_objformat_set_validate_func(contact1_format, ldap_format_validate); + + + + osync_objformat_set_compare_func(contact2_format, ldap_format_compare_format1); + osync_objformat_set_destroy_func(contact2_format, ldap_format_destroy_format1); + osync_objformat_set_duplicate_func(contact2_format, ldap_format_duplicate_format1); + osync_objformat_set_print_func(contact2_format, ldap_format_print_format1); + osync_objformat_set_validate_func(contact2_format, ldap_format_validate); + + + osync_objformat_set_compare_func(event_format, ldap_format_compare_format1); + osync_objformat_set_destroy_func(event_format, ldap_format_destroy_format1); + osync_objformat_set_duplicate_func(event_format, ldap_format_duplicate_format1); + osync_objformat_set_print_func(event_format, ldap_format_print_format1); + osync_objformat_set_validate_func(event_format, ldap_format_validate); + + + osync_objformat_set_compare_func(todo_format, ldap_format_compare_format1); + osync_objformat_set_destroy_func(todo_format, ldap_format_destroy_format1); + osync_objformat_set_duplicate_func(todo_format, ldap_format_duplicate_format1); + osync_objformat_set_print_func(todo_format, ldap_format_print_format1); + osync_objformat_set_validate_func(todo_format, ldap_format_validate); + + + osync_objformat_set_compare_func(note_format, ldap_format_compare_format1); + osync_objformat_set_destroy_func(note_format, ldap_format_destroy_format1); + osync_objformat_set_duplicate_func(note_format, ldap_format_duplicate_format1); + osync_objformat_set_print_func(note_format, ldap_format_print_format1); + osync_objformat_set_validate_func(note_format, ldap_format_validate); + + + + + osync_format_env_register_objformat(env, contact1_format); + osync_format_env_register_objformat(env, contact2_format); + osync_format_env_register_objformat(env, event_format); + osync_format_env_register_objformat(env, todo_format); + osync_format_env_register_objformat(env, note_format); + + + osync_objformat_unref(contact1_format); + osync_objformat_unref(contact2_format); + osync_objformat_unref(event_format); + osync_objformat_unref(todo_format); + osync_objformat_unref(note_format); + + + osync_trace(TRACE_EXIT, "%s", __func__); + return TRUE; + +error: + if (!osync_error_is_set(error)) + osync_error_set(error, OSYNC_ERROR_GENERIC, "Unknown reason.\n"); + + osync_trace(TRACE_EXIT_ERROR, "%s: %s", __func__, osync_error_print(error)); + return FALSE; +} + + + + + +/** + * @brief Helper function for setting up the converter functions. + * + * @param env Format specific environment. + * @param format_name Name of the first format. + * @param other_format_name of the second format. + * @param objtype Object type, such as "contact", "event", "todo", "note". + * @param converter1 Function pointer to a converter function. + * @param converter2 Function pointer to a converter function for the opposite direction. + * @param detector_function Function pointer to a detector function. + * @param error libopensync error pointer. + * + * @returns TRUE on success, FALSE on failure. + */ + +static osync_bool ldap_format_do_get_conversion_info(OSyncFormatEnv *env, const char *format_name, const char *other_format_name, const char *objtype, OSyncFormatConvertFunc converter1, OSyncFormatConvertFunc converter2, OSyncFormatDetectFunc detector_function, OSyncError **error) +{ + OSyncFormatConverter *conv = NULL; + OSyncObjFormat *plain = NULL; + OSyncObjFormat *format1 = NULL; + + + + + osync_trace(TRACE_ENTRY, "%s(%p, %p, %p, %p, %p, %p, %p, %p)", __func__, env, format_name, other_format_name, objtype, converter1, converter2, detector_function, error); + + + if (env == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: env = NULL.\n", __FILE__, __LINE__); + goto error; + } + + if (format_name == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: format_name = NULL.\n", __FILE__, __LINE__); + goto error; + } + + if (other_format_name == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: other_format_name = NULL.\n", __FILE__, __LINE__); + goto error; + } + + if (format_name == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: format_name = NULL.\n", __FILE__, __LINE__); + goto error; + } + + if (objtype == NULL) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: objtype = NULL.\n", __FILE__, __LINE__); + goto error; + } + + + + format1 = osync_format_env_find_objformat(env, format_name); + if (!format1) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: Unable to find \"%s\" format", __FILE__, __LINE__, format_name); + goto error; + } + + + + if (detector_function) { + osync_trace(TRACE_INTERNAL, "%s:%i: Setting detector_function for format \"%s\" against \"plain\". detector_function = %p.", __FILE__, __LINE__, format_name, detector_function); + + plain = osync_format_env_find_objformat(env, "plain"); + if (!plain) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: Unable to find \"plain\" format", __FILE__, __LINE__); + goto error; + } + + conv = osync_converter_new_detector(plain, format1, detector_function, error); + + if (!conv) { + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: Could not set detector_function for the \"%s\" format", __FILE__, __LINE__, format_name); + goto error; + } + +#ifdef ENABLE_DETECTOR_FUNCTIONS + osync_format_env_register_converter(env, conv); +#endif + + osync_converter_unref(conv); + } OSyncObjFormat *format2 = osync_format_env_find_objformat(env, other_format_name); if (!format2) { - osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to find \"%s\" format", other_format_name); + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: Unable to find \"%s\" format", __FILE__, __LINE__, other_format_name); goto error; } if (converter1 == NULL) { - osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to find converter1 function."); + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: Unable to find converter1 function.", __FILE__, __LINE__); goto error; } if (converter2 == NULL) { - osync_error_set(error, OSYNC_ERROR_GENERIC, "Unable to find converter2 function."); + osync_error_set(error, OSYNC_ERROR_GENERIC, "%s:%i: ERROR: Unable to find converter2 function.", __FILE__, __LINE__); goto error; } @@ -6183,7 +7017,7 @@ // Converter functions: - OSyncFormatConverter *conv = osync_converter_new(OSYNC_CONVERTER_CONV, format1, format2, converter1, error); + conv = osync_converter_new(OSYNC_CONVERTER_CONV, format1, format2, converter1, error); if (!conv) { if (!osync_error_is_set(error)) @@ -6268,6 +7102,7 @@ OBJECTTYPE_CONTACT, ldap_format_conv_ldap_evolutionperson_to_xmlformat_contact, ldap_format_conv_xmlformat_contact_to_ldap_evolution_person, + ldap_format_detect_plain_as_ldap_evolutionperson, error) ) { @@ -6296,6 +7131,7 @@ OBJECTTYPE_CONTACT, ldap_format_conv_ldap_inetorgperson_to_xmlformat_contact, ldap_format_conv_xmlformat_contact_to_ldap_inetorg_person, + ldap_format_detect_plain_as_ldap_inetorgperson, error) ) { @@ -6330,6 +7166,7 @@ OBJECTTYPE_EVENT, ldap_format_conv_ldap_event_to_xmlformat_event, ldap_format_conv_xmlformat_event_to_ldap_event, + ldap_format_detect_plain_as_ldap_event, error) ) { @@ -6356,6 +7193,7 @@ OBJECTTYPE_TODO, ldap_format_conv_ldap_todo_to_xmlformat_todo, ldap_format_conv_xmlformat_todo_to_ldap_todo, + ldap_format_detect_plain_as_ldap_todo, error) ) { @@ -6384,6 +7222,7 @@ OBJECTTYPE_NOTE, ldap_format_conv_ldap_note_to_xmlformat_note, ldap_format_conv_xmlformat_note_to_ldap_note, + ldap_format_detect_plain_as_ldap_note, error) ) { Modified: plugins/ldap-sync/src/ldap_format.h ============================================================================== --- plugins/ldap-sync/src/ldap_format.h Sat Apr 11 20:29:13 2009 (r5578) +++ plugins/ldap-sync/src/ldap_format.h Sat Apr 11 20:29:35 2009 (r5579) @@ -51,6 +51,7 @@ static int ldap_format_avoid_duplicate_attribute(GList *ldapmods, const char *name); static osync_bool ldap_format_check_adequate_stylesheet(xmlNode *root_element, const char *stylesheet_file, OSyncError **error); static osync_bool ldap_format_check_root_element(const xmlDoc *xmldata, const char *objtype, const char *ldap_format_name, const gchar *dn, xmlNode **root, OSyncError **error); +osync_bool ldap_format_check_xml(const char *data, int size, OSyncError **error); static OSyncConvCmpResult ldap_format_compare_format1(const char *leftdata, unsigned int leftsize, const char *rightdata, unsigned int rightsize, void *user_data); static osync_bool ldap_format_conv_ldap_event_to_xmlformat_event(char *input, unsigned int inpsize, char **output, unsigned int *outpsize, osync_bool *free_input, const char *config, void *userdata, OSyncError **error); static osync_bool ldap_format_conv_ldap_evolutionperson_to_xmlformat_contact(char *input, unsigned int inpsize, char **output, unsigned int *outpsize, osync_bool *free_input, const char *config, void *userdata, OSyncError **error); @@ -63,9 +64,16 @@ static osync_bool ldap_format_convert_xmlinternal2ldap (const xmlDoc *xmldata, const char *objtype, const char *ldap_format_name, const gchar *dn, GList **potential_subentries, GList **ldap_entries, OSyncError **error); static osync_bool ldap_format_create_ldap_entries_list_from_ldapmods(const char *dn, GList *ldapmods, GList **potential_subentries, GList **ldap_entries, OSyncError **error); static ldap_entry *ldap_format_create_ldap_entry_from_ldapmods(GList *ldapmods); + +osync_bool ldap_format_detect_plain_as_ldap_evolutionperson(const char *data, int size, void *userdata); +osync_bool ldap_format_detect_plain_as_ldap_inetorgperson(const char *data, int size, void *userdata); +osync_bool ldap_format_detect_plain_as_ldap_event(const char *data, int size, void *userdata); +osync_bool ldap_format_detect_plain_as_ldap_todo(const char *data, int size, void *userdata); +osync_bool ldap_format_detect_plain_as_ldap_note(const char *data, int size, void *userdata); static osync_bool ldap_format_do_apply_stylesheet(char *input, unsigned int inpsize, char **output, unsigned int *outpsize, const char *config, void *userdata, const char *stylesheet_file, OSyncError **error); static osync_bool ldap_format_do_conv_ldap_to_xmlformat(char *input, unsigned int inpsize, char **output, unsigned int *outpsize, osync_bool *free_input, const char *ldap_format_name, const char *xmlformat_name, const char *stylesheet_name, const char *objtype, const char *config, void *userdata, OSyncError **error); static osync_bool ldap_format_do_conv_xmlformat_to_ldap(char *input, unsigned int inpsize, char **output, unsigned int *outpsize, osync_bool *free_input, const char *xmlformat_name, const char *ldap_format_name, const char *stylesheet_name, const char *objtype, const char *config, void *userdata, OSyncError **error); +osync_bool ldap_format_do_detect_plain_as_ldap_format(const char *data, const int size, const char *format_name, const char *opening_tag, const char *closing_tag, const void *userdata, OSyncError **error); void ldap_format_free_complex_element_lists(GList **list); static void ldap_format_free_ldap_entry (ldap_entry *entry); static void ldap_format_free_ldap_entries (GList **entrylist); |