Exemplo n.º 1
0
//
// queryMacroFileProvider - load and save macros from a XML file in the users
//                              home directory
//
queryMacroList *queryMacroFileProvider::LoadMacros(bool emptyOnFailure)
{
	xmlTextReaderPtr reader;
	int ret;

	if (!wxFile::Access(settings->GetMacrosFile(), wxFile::read))
		return emptyOnFailure ? (new queryMacroList()) : NULL;

	reader = xmlReaderForFile((const char *)settings->GetMacrosFile().mb_str(wxConvUTF8), NULL, 0);
	if (!reader)
	{
		wxMessageBox(_("Failed to load macros file!"));
		return emptyOnFailure ? (new queryMacroList()) : NULL;
	}

	ret = xmlTextReaderRead(reader);
	if (ret != 1)
	{
		wxMessageBox(_("Failed to read macros file!"));
		return emptyOnFailure ? (new queryMacroList()) : NULL;
	}

	queryMacroList *f = (queryMacroList *)(new queryMacroList(reader));

	xmlTextReaderClose(reader);
	xmlFreeTextReader(reader);
	xmlCleanupParser();

	return f;
}
Exemplo n.º 2
0
//
// queryFavouriteFileProvider - load and save favourites from a XML file in the users
//                              home directory
//
queryFavouriteFolder *queryFavouriteFileProvider::LoadFavourites(bool emptyonfailure)
{
	xmlTextReaderPtr reader;
	int ret;

	if (!wxFile::Access(settings->GetFavouritesFile(), wxFile::read))
		return emptyonfailure ? (new queryFavouriteFolder()) : NULL;

	reader = xmlReaderForFile((const char *)settings->GetFavouritesFile().mb_str(wxConvUTF8), NULL, 0);
	if (!reader)
	{
		wxMessageBox(_("Failed to load favourites file!"));
		return emptyonfailure ? (new queryFavouriteFolder()) : NULL;
	}

	ret = xmlTextReaderRead(reader);
	if (ret != 1)
	{
		wxMessageBox(_("Failed to read favourites file!"));
		return emptyonfailure ? (new queryFavouriteFolder()) : NULL;
	}

	queryFavouriteFolder *f = (queryFavouriteFolder *)(new queryFavouriteFolder(reader, wxT("")));

	xmlTextReaderClose(reader);
	xmlFreeTextReader(reader);
	xmlCleanupParser();

	return f;
}
Exemplo n.º 3
0
static void
on_downloaded (SummerWebBackend *web, gchar *save_path, gchar *save_data, GError *error, gpointer user_data)
{
	g_return_if_fail (SUMMER_IS_FEED (user_data));
	SummerFeed *self = SUMMER_FEED (user_data);
	SummerFeedPrivate *priv = self->priv;
	if (error == NULL) {
		SummerFeedParser *parsers[] = {
			SUMMER_FEED_PARSER (summer_atom_parser_new ()),
			SUMMER_FEED_PARSER (summer_rss2_parser_new ())};
		xmlParserInputBufferPtr buffer = xmlParserInputBufferCreateMem (save_data, strlen (save_data), 0);
		xmlTextReaderPtr reader = xmlNewTextReader (buffer, priv->url);
		priv->feed_data = summer_feed_parser_parse (parsers, sizeof (parsers) / sizeof (*parsers), reader);
		priv->feed_data->url = priv->url;
		
		unsigned int i;
		for (i = 0; i < sizeof (parsers) / sizeof (*parsers); i++) {
			g_object_unref (parsers[i]);
		}
		xmlTextReaderClose (reader);
		xmlFreeTextReader (reader);
		xmlFreeParserInputBuffer (buffer);
	
		g_signal_emit_by_name (self, "new-entries");
	}

	g_object_unref (web);

	if (priv->frequency <= 0)
		g_object_unref (self);
}
Exemplo n.º 4
0
static int ooxml_parse_document(int fd, cli_ctx *ctx)
{
    int ret = CL_SUCCESS;
    xmlTextReaderPtr reader = NULL;

    cli_dbgmsg("in ooxml_parse_document\n");

    /* perform engine limit checks in temporary tracking session */
    ret = ooxml_updatelimits(fd, ctx);
    if (ret != CL_CLEAN)
        return ret;

    reader = xmlReaderForFd(fd, "properties.xml", NULL, CLAMAV_MIN_XMLREADER_FLAGS);
    if (reader == NULL) {
        cli_dbgmsg("ooxml_parse_document: xmlReaderForFd error\n");
        return CL_SUCCESS; // internal error from libxml2
    }

    ret = cli_msxml_parse_document(ctx, reader, ooxml_keys, num_ooxml_keys, 1);

    if (ret != CL_SUCCESS && ret != CL_ETIMEOUT && ret != CL_BREAK)
        cli_warnmsg("ooxml_parse_document: encountered issue in parsing properties document\n");

    xmlTextReaderClose(reader);
    xmlFreeTextReader(reader);
    return ret;
}
Exemplo n.º 5
0
//## @Native void XmlReader.close();
static KMETHOD XmlReader_close(KonohaContext *kctx, KonohaStack *sfp)
{
	xmlTextReaderPtr reader = getRawXmlReader(sfp[0]);
	if(reader != NULL) {
		xmlTextReaderClose(reader);
	}
	KReturnVoid();
}
Exemplo n.º 6
0
static int _exml_read(EXML *xml, xmlTextReaderPtr reader)
{
	int empty;
	xmlChar *name, *value;

	if (!reader)
		return -1;

	exml_clear( xml );

	while( xmlTextReaderRead( reader ) == 1 ) {
		name = xmlTextReaderName(reader);
		value = xmlTextReaderValue(reader);
		empty = xmlTextReaderIsEmptyElement(reader);

		switch( xmlTextReaderNodeType(reader) ) {
			case XML_READER_TYPE_ELEMENT:
				exml_start(xml);
				exml_tag_set(xml, (char *) name);
		
				if (xmlTextReaderHasAttributes(reader)) {
					xmlTextReaderMoveToFirstAttribute(reader);
					do {
						xmlChar *attr_name, *attr_value;

						attr_name = xmlTextReaderName(reader);
						attr_value = xmlTextReaderValue(reader);

						exml_attribute_set(xml, (char *) attr_name, (char *) attr_value);

						xmlFree(attr_name);
						xmlFree(attr_value);
					} while( xmlTextReaderMoveToNextAttribute(reader) == 1 );
				}

				if (!empty)
					break;
			case XML_READER_TYPE_END_ELEMENT:
				exml_end(xml);
				break;
			case XML_READER_TYPE_WHITESPACE:
				break;
			case XML_READER_TYPE_TEXT:
				exml_value_set(xml, (char *) value);
				break;
		}
		xmlFree(name);
		xmlFree(value);
	}

	xmlTextReaderClose(reader);
	xmlFreeTextReader(reader);

	exml_goto_top( xml );

	return TRUE;
}
Exemplo n.º 7
0
/*
 * mgmt_convert_param() converts legacy params file of each LUN
 * to scf data. It will convert LUNs under one target each time.
 * Args:
 *   dir - string of directory where param file is stored
 *   tnode - node tree which contains to a target
 */
Boolean_t
mgmt_convert_param(char *dir, tgt_node_t *tnode)
{
	Boolean_t	ret = False;
	char		path[MAXPATHLEN];
	int		xml_fd = -1;
	int		n;
	int		lun_num;
	tgt_node_t	*lun = NULL;
	tgt_node_t	*params = NULL;
	xmlTextReaderPtr	r;

	while ((lun = tgt_node_next(tnode, XML_ELEMENT_LUN, lun)) != NULL) {
		if ((tgt_find_value_int(lun, XML_ELEMENT_LUN, &lun_num)) ==
		    False)
			continue;
		(void) snprintf(path, sizeof (path), "%s/%s%d",
		    dir, PARAMBASE, lun_num);
		if ((xml_fd = open(path, O_RDONLY)) < 0)
			continue;
		if ((r = (xmlTextReaderPtr)xmlReaderForFd(xml_fd,
		    NULL, NULL, 0)) == NULL)
			continue;

		n = xmlTextReaderRead(r);
		while (n == 1) {
			if (tgt_node_process(r, &params) == False) {
				break;
			}
			n = xmlTextReaderRead(r);
		}
		if (n < 0) {
			ret = False;
			break;
		}

		if (mgmt_param_save2scf(params, tnode->x_value, lun_num)
		    != True) {
			ret = False;
			break;
		} else {
			backup(path, tnode->x_value);
			ret = True;
		}
		params = NULL;
		(void) close(xml_fd);
		xmlTextReaderClose(r);
		xmlFreeTextReader(r);
	}

	if (ret == False)
		syslog(LOG_ERR, "Converting target %s params failed", dir);
	return (ret);
}
Exemplo n.º 8
0
void Free_Reader(CXMLREADER *test)
{
	if (test->buffer)GB.Free(POINTER(&test->buffer));
	if (test->reader)
	{
		xmlTextReaderClose(test->reader);
		xmlFreeTextReader(test->reader);
		test->reader=NULL;
	}
	test->eof=0;
}
Exemplo n.º 9
0
/* reader:close() */
static int xmlreader_close(lua_State *L) {
  xmlreader xr = check_xmlreader(L, 1);

  int ret = xmlTextReaderClose(xr);
  if (ret == 0) {
    lua_pushboolean(L, 1);
    return 1;
  } else {
    lua_pushnil(L);
    xmlreader_pusherror(L);
    return 2;
  }
}
Exemplo n.º 10
0
static void
xml_reader_clear (XmlReader *reader)
{
   g_return_if_fail(XML_IS_READER(reader));

   g_free (reader->cur_name);
   reader->cur_name = NULL;

   if (reader->xml) {
      xmlTextReaderClose(reader->xml);
      xmlFreeTextReader(reader->xml);
      reader->xml = NULL;
   }

   if (reader->stream) {
      g_object_unref(reader->stream);
      reader->stream = NULL;
   }
}
Exemplo n.º 11
0
static int ooxml_parse_document(int fd, cli_ctx *ctx)
{
    int ret = CL_SUCCESS;
    xmlTextReaderPtr reader = NULL;

    cli_dbgmsg("in ooxml_parse_document\n");

    reader = xmlReaderForFd(fd, "properties.xml", NULL, 0);
    if (reader == NULL) {
        cli_dbgmsg("ooxml_parse_document: xmlReaderForFd error\n");
        return CL_SUCCESS; // internal error from libxml2
    }

    /* move reader to first element */
    if (xmlTextReaderRead(reader) != 1) {
        return CL_SUCCESS; /* libxml2 failed */
    }

    ret = ooxml_parse_element(reader, ctx->wrkproperty, 0, 0);

    xmlTextReaderClose(reader);
    xmlFreeTextReader(reader);
    return ret;
}
Exemplo n.º 12
0
static int ooxml_basic_json(int fd, cli_ctx *ctx, const char *key)
{
    int ret = CL_SUCCESS;
    const xmlChar *stack[OOXML_JSON_RECLEVEL];
    json_object *summary, *wrkptr;
    int type, rlvl = 0;
    int32_t val2;
    const xmlChar *name, *value;
    xmlTextReaderPtr reader = NULL;

    cli_dbgmsg("in ooxml_basic_json\n");

    reader = xmlReaderForFd(fd, "properties.xml", NULL, 0);
    if (reader == NULL) {
        cli_dbgmsg("ooxml_basic_json: xmlReaderForFd error for %s\n", key);
        return CL_SUCCESS; // libxml2 failed!
    }

    summary = json_object_new_object();
    if (NULL == summary) {
        cli_errmsg("ooxml_basic_json: no memory for json object.\n");
        ret = CL_EFORMAT;
        goto ooxml_basic_exit;
    }

    while (xmlTextReaderRead(reader) == 1) {
        name = xmlTextReaderConstLocalName(reader);
        value = xmlTextReaderConstValue(reader);
        type = xmlTextReaderNodeType(reader);

        cli_dbgmsg("%s [%i]: %s\n", name, type, value);

        switch (type) {
        case XML_READER_TYPE_ELEMENT:
            stack[rlvl] = name;
            rlvl++;
            break;
        case XML_READER_TYPE_TEXT:
            {
                wrkptr = summary;
                if (rlvl > 2) { /* 0 is root xml object */
                    int i;
                    for (i = 1; i < rlvl-1; ++i) {
                        json_object *newptr = json_object_object_get(wrkptr, stack[i]);
                        if (!newptr) {
                            newptr = json_object_new_object();
                            if (NULL == newptr) {
                                cli_errmsg("ooxml_basic_json: no memory for json object.\n");
                                ret = CL_EMEM;
                                goto ooxml_basic_exit;
                            }
                            json_object_object_add(wrkptr, stack[i], newptr);
                        }
                        else {
                            /* object already exists */
                            if (!json_object_is_type(newptr, json_type_object)) {
                                cli_warnmsg("ooxml_content_cb: json object already exists as not an object\n");
                                ret = CL_EFORMAT;
                                goto ooxml_basic_exit;
                            } 
                        }
                        wrkptr = newptr;
                        cli_dbgmsg("stack %d: %s\n", i, stack[i]);
                    }
                }
                
                if (ooxml_is_int(value, xmlStrlen(value), &val2)) {
                    ret = cli_jsonint(wrkptr, stack[rlvl-1], val2);
                }
                else if (!xmlStrcmp(value, "true")) {
                    ret = cli_jsonbool(wrkptr, stack[rlvl-1], 1);
                }
                else if (!xmlStrcmp(value, "false")) {
                    ret = cli_jsonbool(wrkptr, stack[rlvl-1], 0);
                }
                else {
                    ret = cli_jsonstr(wrkptr, stack[rlvl-1], value);
                }

                if (ret != CL_SUCCESS)
                    goto ooxml_basic_exit;
            }
            break;
        case XML_READER_TYPE_END_ELEMENT:
            rlvl--;
            break;
        default:
            cli_dbgmsg("ooxml_content_cb: unhandled xml node %s [%i]: %s\n", name, type, value);
            ret = CL_EFORMAT;
            goto ooxml_basic_exit;
        }
    }

    json_object_object_add(ctx->wrkproperty, key, summary);

    if (rlvl != 0) {
        cli_warnmsg("ooxml_basic_json: office property file has unbalanced tags\n");
        /* FAIL */
    }

 ooxml_basic_exit:
    xmlTextReaderClose(reader);
    xmlFreeTextReader(reader);
    return ret;
}
Exemplo n.º 13
0
static Eina_Bool _lng_read(Language_XML *xml, xmlTextReaderPtr reader)
{
    Eina_Bool empty;
    xmlChar *name, *value;

    if (!reader) return EINA_FALSE;
    language_xml_clear(xml);

    while (xmlTextReaderRead(reader) == 1)
    {
        name = xmlTextReaderName(reader);
        value = xmlTextReaderValue(reader);
        empty = xmlTextReaderIsEmptyElement(reader);

        switch (xmlTextReaderNodeType(reader))
        {
        case XML_READER_TYPE_ELEMENT:
        {
            _lng_start(xml);
            _lng_set(xml, (char *) name, NULL, "tag");

            if (xmlTextReaderHasAttributes(reader))
            {
                xmlTextReaderMoveToFirstAttribute(reader);
                do
                {
                    xmlChar *attr_name, *attr_value;

                    attr_name = xmlTextReaderName(reader);
                    attr_value = xmlTextReaderValue(reader);

                    _lng_set(xml, (char *) attr_name, (char *) attr_value, "atr");

                    xmlFree(attr_name);
                    xmlFree(attr_value);
                } while (xmlTextReaderMoveToNextAttribute(reader) == 1);
            }

            if (!empty) break;
        }
        case XML_READER_TYPE_END_ELEMENT:
        {
            if (!xml) return EINA_FALSE;
            if (!xml->current)
                xml->current = xml->top;
            else
                xml->current = xml->current->parent;
            break;
        }
        case XML_READER_TYPE_WHITESPACE:
            break;
        case XML_READER_TYPE_TEXT:
        {
            _lng_set(xml, (char *) value, NULL, "val");
            break;
        }
        }
        xmlFree(name);
        xmlFree(value);
    }

    xmlTextReaderClose(reader);
    xmlFreeTextReader(reader);

    xml->current = xml->top;

    return EINA_TRUE;
}
Exemplo n.º 14
0
void XmlTextReader::Close() const {
	if (m_p)
		XmlCheck(xmlTextReaderClose(ToReader(exchange(m_p, nullptr))));	
//!!!R	ReadState = Ext::ReadState::Closed;
	
}
Exemplo n.º 15
0
static GstylePalette *
gstyle_palette_new_from_xml (GFile         *file,
                             GCancellable  *cancellable,
                             GError       **error)
{
  g_autoptr(GInputStream) stream = NULL;
  g_autofree gchar *uri = NULL;
  GstylePalette *palette = NULL;
  xmlTextReaderPtr reader;
  GError *tmp_error = NULL;
  gboolean has_colors = FALSE;
  gint ret = -1;

  g_assert (G_IS_FILE (file));

  uri = g_file_get_uri (file);

  if (!(stream = G_INPUT_STREAM (g_file_read (file, cancellable, &tmp_error))))
    goto finish;

  reader = xmlReaderForIO (gstyle_palette_io_read_cb,
                           gstyle_palette_io_close_cb,
                           stream,
                           uri,
                           NULL,
                           XML_PARSE_RECOVER | XML_PARSE_NOBLANKS | XML_PARSE_COMPACT);

  if (reader != NULL)
    {
      GstyleColor *color;
      g_autofree gchar *id = NULL;
      g_autofree gchar *name = NULL;
      g_autofree gchar *domain = NULL;

      xmlTextReaderSetErrorHandler (reader, gstyle_palette_error_cb, NULL);

      if (xmlTextReaderRead(reader) &&
          gstyle_palette_xml_get_header (reader, &id, &name, &domain))
        {
          palette = g_object_new (GSTYLE_TYPE_PALETTE,
                                  "id", id,
                                  "domain", domain,
                                  "name", name,
                                  "file", file,
                                  NULL);

          ret = xmlTextReaderRead(reader);
          while (ret == 1)
            {
              if (xmlTextReaderNodeType (reader) == XML_READER_TYPE_END_ELEMENT)
                {
                  ret = 0;
                  break;
                }

              /* TODO: better naming */
              color = gstyle_palette_xml_get_color (reader);
              if (color == NULL)
                {
                  ret = -1;
                  break;
                }

              gstyle_palette_add (palette, color, &tmp_error);
              g_object_unref (color);
              has_colors = TRUE;

              ret = xmlTextReaderRead(reader);
            }
        }

      if (ret != 0 || !has_colors)
        {
          g_clear_object (&palette);
          g_set_error (&tmp_error, GSTYLE_PALETTE_ERROR, GSTYLE_PALETTE_ERROR_PARSE,
                       _("%s: failed to parse\n"), uri);
        }

      xmlTextReaderClose(reader);
      xmlFreeTextReader(reader);
    }
  else
    g_set_error (&tmp_error, GSTYLE_PALETTE_ERROR, GSTYLE_PALETTE_ERROR_FILE,
                 _("Unable to open %s\n"), uri);

finish:

  if (tmp_error)
    g_propagate_error (error, tmp_error);

  return palette;
}
Exemplo n.º 16
0
/* @method void XmlReader.close() */
METHOD XmlReader_close(Ctx *ctx, knh_sfp_t *sfp)
{
    xmlTextReaderPtr reader = (xmlTextReaderPtr) p_cptr(sfp[0]);
    xmlTextReaderClose(reader);
    KNH_RETURN_void(ctx,sfp);
}
Exemplo n.º 17
0
/*
 * call-seq:
 *    reader.close -> code
 *
 * This method releases any resources allocated by the current instance
 * changes the state to Closed and close any underlying input.
 */
static VALUE rxml_reader_close(VALUE self)
{
  return INT2FIX(xmlTextReaderClose(rxml_text_reader_get(self)));
}
Exemplo n.º 18
0
static int ooxml_content_cb(int fd, cli_ctx *ctx)
{
    int ret = CL_SUCCESS, tmp, toval = 0, state;
    int core=0, extn=0, cust=0, dsig=0;
    int mcore=0, mextn=0, mcust=0;
    const xmlChar *name, *value, *CT, *PN;
    xmlTextReaderPtr reader = NULL;
    uint32_t loff;

    unsigned long sav_scansize = ctx->scansize;
    unsigned int sav_scannedfiles = ctx->scannedfiles;

    cli_dbgmsg("in ooxml_content_cb\n");

    /* perform engine limit checks in temporary tracking session */
    ret = ooxml_updatelimits(fd, ctx);
    if (ret != CL_CLEAN)
        return ret;

    /* apply a reader to the document */
    reader = xmlReaderForFd(fd, "[Content_Types].xml", NULL, CLAMAV_MIN_XMLREADER_FLAGS);
    if (reader == NULL) {
        cli_dbgmsg("ooxml_content_cb: xmlReaderForFd error for ""[Content_Types].xml""\n");
        cli_json_parse_error(ctx->wrkproperty, "OOXML_ERROR_XML_READER_FD");

        ctx->scansize = sav_scansize;
        ctx->scannedfiles = sav_scannedfiles;
        return CL_SUCCESS; // libxml2 failed!
    }

    /* locate core-properties, extended-properties, and custom-properties (optional) */
    while ((state = xmlTextReaderRead(reader)) == 1) {
        if (cli_json_timeout_cycle_check(ctx, &toval) != CL_SUCCESS) {
            ret = CL_ETIMEOUT;
            goto ooxml_content_exit;
        }

        name = xmlTextReaderConstLocalName(reader);
        if (name == NULL) continue;

        if (strcmp((const char *)name, "Override")) continue;

        if (xmlTextReaderHasAttributes(reader) != 1) continue;

        CT = PN = NULL;
        while (xmlTextReaderMoveToNextAttribute(reader) == 1) {
            name = xmlTextReaderConstLocalName(reader);
            value = xmlTextReaderConstValue(reader);
            if (name == NULL || value == NULL) continue;

            if (!xmlStrcmp(name, (const xmlChar *)"ContentType")) {
                CT = value;
            }
            else if (!xmlStrcmp(name, (const xmlChar *)"PartName")) {
                PN = value;
            }

            cli_dbgmsg("%s: %s\n", name, value);
        }

        if (!CT && !PN) continue;

        if (!xmlStrcmp(CT, (const xmlChar *)"application/vnd.openxmlformats-package.core-properties+xml")) {
            /* default: /docProps/core.xml*/
            tmp = unzip_search_single(ctx, (const char *)(PN+1), xmlStrlen(PN)-1, &loff);
            if (tmp == CL_ETIMEOUT) {
                ret = tmp;
            }
            else if (tmp != CL_VIRUS) {
                cli_dbgmsg("cli_process_ooxml: failed to find core properties file \"%s\"!\n", PN);
                mcore++;
            }
            else {
                cli_dbgmsg("ooxml_content_cb: found core properties file \"%s\" @ %x\n", PN, loff);
                if (!core) {
                    tmp = unzip_single_internal(ctx, loff, ooxml_core_cb);
                    if (tmp == CL_ETIMEOUT || tmp == CL_EMEM) {
                        ret = tmp;
                    }
                }
                core++;
            }
        }
        else if (!xmlStrcmp(CT, (const xmlChar *)"application/vnd.openxmlformats-officedocument.extended-properties+xml")) {
            /* default: /docProps/app.xml */
            tmp = unzip_search_single(ctx, (const char *)(PN+1), xmlStrlen(PN)-1, &loff);
            if (tmp == CL_ETIMEOUT) {
                ret = tmp;
            }
            else if (tmp != CL_VIRUS) {
                cli_dbgmsg("cli_process_ooxml: failed to find extended properties file \"%s\"!\n", PN);
                mextn++;
            }
            else {
                cli_dbgmsg("ooxml_content_cb: found extended properties file \"%s\" @ %x\n", PN, loff);
                if (!extn) {
                    tmp = unzip_single_internal(ctx, loff, ooxml_extn_cb);
                    if (tmp == CL_ETIMEOUT || tmp == CL_EMEM) {
                        ret = tmp;
                    }
                }
                extn++;
            }
        }
        else if (!xmlStrcmp(CT, (const xmlChar *)"application/vnd.openxmlformats-officedocument.custom-properties+xml")) {
            /* default: /docProps/custom.xml */
            tmp = unzip_search_single(ctx, (const char *)(PN+1), xmlStrlen(PN)-1, &loff);
            if (tmp == CL_ETIMEOUT) {
                ret = tmp;
            }
            else if (tmp != CL_VIRUS) {
                cli_dbgmsg("cli_process_ooxml: failed to find custom properties file \"%s\"!\n", PN);
                mcust++;
            }
            else {
                cli_dbgmsg("ooxml_content_cb: found custom properties file \"%s\" @ %x\n", PN, loff);
                /* custom properties are not parsed */
                cust++;
            }
        }
        else if (!xmlStrcmp(CT, (const xmlChar *)"application/vnd.openxmlformats-package.digital-signature-xmlsignature+xml")) {
            dsig++;
        }

        if (ret != CL_SUCCESS)
            goto ooxml_content_exit;
    }

 ooxml_content_exit:
    if (core) {
        cli_jsonint(ctx->wrkproperty, "CorePropertiesFileCount", core);
        if (core > 1)
            cli_json_parse_error(ctx->wrkproperty, "OOXML_ERROR_MULTIPLE_CORE_PROPFILES");
    }
    else if (!mcore)
        cli_dbgmsg("cli_process_ooxml: file does not contain core properties file\n");
    if (mcore) {
        cli_jsonint(ctx->wrkproperty, "CorePropertiesMissingFileCount", mcore);
        cli_json_parse_error(ctx->wrkproperty, "OOXML_ERROR_MISSING_CORE_PROPFILES");
    }

    if (extn) {
        cli_jsonint(ctx->wrkproperty, "ExtendedPropertiesFileCount", extn);
        if (extn > 1)
            cli_json_parse_error(ctx->wrkproperty, "OOXML_ERROR_MULTIPLE_EXTN_PROPFILES");
    }
    else if (!mextn)
        cli_dbgmsg("cli_process_ooxml: file does not contain extended properties file\n");
    if (mextn) {
        cli_jsonint(ctx->wrkproperty, "ExtendedPropertiesMissingFileCount", mextn);
        cli_json_parse_error(ctx->wrkproperty, "OOXML_ERROR_MISSING_EXTN_PROPFILES");
    }

    if (cust) {
        cli_jsonint(ctx->wrkproperty, "CustomPropertiesFileCount", cust);
        if (cust > 1)
            cli_json_parse_error(ctx->wrkproperty, "OOXML_ERROR_MULTIPLE_CUSTOM_PROPFILES");
    }
    else if (!mcust)
        cli_dbgmsg("cli_process_ooxml: file does not contain custom properties file\n");
    if (mcust) {
        cli_jsonint(ctx->wrkproperty, "CustomPropertiesMissingFileCount", mcust);
        cli_json_parse_error(ctx->wrkproperty, "OOXML_ERROR_MISSING_CUST_PROPFILES");
    }

    if (dsig) {
        cli_jsonint(ctx->wrkproperty, "DigitalSignaturesCount", dsig);
    }

    /* restore the engine tracking limits; resets session limit tracking */
    ctx->scansize = sav_scansize;
    ctx->scannedfiles = sav_scannedfiles;

    xmlTextReaderClose(reader);
    xmlFreeTextReader(reader);
    return ret;
}
Exemplo n.º 19
0
static int ooxml_content_cb(int fd, cli_ctx *ctx)
{
    int ret = CL_SUCCESS;
    int core=0, extn=0, cust=0, dsig=0;
    const xmlChar *name, *value, *CT, *PN;
    xmlTextReaderPtr reader = NULL;
    uint32_t loff;

    cli_dbgmsg("in ooxml_content_cb\n");

    reader = xmlReaderForFd(fd, "[Content_Types].xml", NULL, 0);
    if (reader == NULL) {
        cli_dbgmsg("ooxml_content_cb: xmlReaderForFd error for ""[Content_Types].xml""\n");
        return CL_SUCCESS; // libxml2 failed!
    }

    /* locate core-properties, extended-properties, and custom-properties (optional)  */
    while (xmlTextReaderRead(reader) == 1) {
        name = xmlTextReaderConstLocalName(reader);
        if (name == NULL) continue;

        if (strcmp(name, "Override")) continue;

        if (!xmlTextReaderHasAttributes(reader)) continue;

        CT = NULL; PN = NULL;
        while (xmlTextReaderMoveToNextAttribute(reader) == 1) {
            name = xmlTextReaderConstLocalName(reader);
            value = xmlTextReaderConstValue(reader);
            if (name == NULL || value == NULL) continue;

            if (!xmlStrcmp(name, "ContentType")) {
                CT = value;
            }
            else if (!xmlStrcmp(name, "PartName")) {
                PN = value;
            }

            cli_dbgmsg("%s: %s\n", name, value);
        }

        if (!CT && !PN) continue;

        if (!core && !xmlStrcmp(CT, "application/vnd.openxmlformats-package.core-properties+xml")) {
            /* default: /docProps/core.xml*/
            if (unzip_search(ctx, PN+1, xmlStrlen(PN)-1, &loff) != CL_VIRUS) {
                cli_dbgmsg("cli_process_ooxml: failed to find core properties file \"%s\"!\n", PN);
            }
            else {
                cli_dbgmsg("ooxml_content_cb: found core properties file \"%s\" @ %x\n", PN, loff);
                ret = unzip_single_internal(ctx, loff, ooxml_core_cb);
            }
            core = 1;
        }
        else if (!extn && !xmlStrcmp(CT, "application/vnd.openxmlformats-officedocument.extended-properties+xml")) {
            /* default: /docProps/app.xml */
            if (unzip_search(ctx, PN+1, xmlStrlen(PN)-1, &loff) != CL_VIRUS) {
                cli_dbgmsg("cli_process_ooxml: failed to find extended properties file \"%s\"!\n", PN);
            }
            else {
                cli_dbgmsg("ooxml_content_cb: found extended properties file \"%s\" @ %x\n", PN, loff);
                ret = unzip_single_internal(ctx, loff, ooxml_extn_cb);
            }
            extn = 1;
        }
        else if (!cust && !xmlStrcmp(CT, "application/vnd.openxmlformats-officedocument.custom-properties+xml")) {
            /* default: /docProps/custom.xml */
            if (unzip_search(ctx, PN+1, xmlStrlen(PN)-1, &loff) != CL_VIRUS) {
                cli_dbgmsg("cli_process_ooxml: failed to find custom properties file \"%s\"!\n", PN);
            }
            else {
                cli_dbgmsg("ooxml_content_cb: found custom properties file \"%s\" @ %x\n", PN, loff);
                /* custom properties ignored for now */
                cli_jsonbool(ctx->wrkproperty, "CustomProperties", 1);
                //ret = unzip_single_internal(ctx, loff, ooxml_cust_cb);
            }
            cust = 1;
        }
        else if (!dsig && !xmlStrcmp(CT, "application/vnd.openxmlformats-package.digital-signature-xmlsignature+xml")) {
            if (unzip_search(ctx, PN+1, xmlStrlen(PN)-1, &loff) != CL_VIRUS) {
                cli_dbgmsg("cli_process_ooxml: failed to find digital signature file \"%s\"!\n", PN);
            }
            else {
                cli_dbgmsg("ooxml_content_cb: found digital signature file \"%s\" @ %x\n", PN, loff);
                /* digital signatures ignored for now */
                cli_jsonbool(ctx->wrkproperty, "DigitalSignatures", 1);
                //ret = unzip_single_internal(ctx, loff, ooxml_dsig_cb);
            }
            dsig = 1;
        }

        if (ret != CL_SUCCESS)
            goto ooxml_content_exit;
    }

    if (!core) {
        cli_dbgmsg("cli_process_ooxml: file does not contain core properties file\n");
    }
    if (!extn) {
        cli_dbgmsg("cli_process_ooxml: file does not contain extended properties file\n");
    }
    if (!cust) {
        cli_dbgmsg("cli_process_ooxml: file does not contain custom properties file\n");
    }

 ooxml_content_exit:
    xmlTextReaderClose(reader);
    xmlFreeTextReader(reader);
    return ret;
}
Exemplo n.º 20
0
void readConfig( const char* filename ) {
    /* States:
     * 0 : reading new entities ( to: 1, 3, 4, 5 )
     * 1 : reading machine
     * 2 : reading role
     * 3 : reading core
     * 4 : reading file
     * 5 : reading test ( to: 2 )
     */
    int state = 0;

    struct machine m;
    struct role r;
    struct file f;
    struct core c;
    struct test t;

    xmlTextReaderPtr xmlRead = xmlReaderForFile( filename, NULL, XML_PARSE_NONET | XML_PARSE_NOENT | XML_PARSE_NOCDATA | XML_PARSE_NOXINCNODE | XML_PARSE_COMPACT );
    if( !xmlRead )
        quit( "Could not open %s for reading as XML config\n", filename );

    xmlTextReaderSetErrorHandler( xmlRead, xmlErrorHandler, 0 );

    int nodeType, ret;
    xmlChar* nodeName;
    while( 1 ) {
        if( ( ret = xmlTextReaderRead( xmlRead ) ) != 1 ) {
            if( ret < 0 )
                quit( "Error occurred\n" );
            if( state )
                quit( "No more nodes to read, but state is not 0. Incomplete elements.\n" );
            xmlTextReaderClose( xmlRead );
            return;
        }
        nodeType = xmlTextReaderNodeType( xmlRead );
        
        nodeName = NULL;
        switch( nodeType ) {
            case XmlNodeType.Element :
                nodeName = xmlTextReaderLocalName( xmlRead );
                switch( state ) {
                    case 0 :
                        if( !strcmp( nodeName, "machine" ) ) {
                            state = 1;
                            bzero( m, sizeof( struct machine ) );
                            readValidRequiredAttribute( m.name, "machine", "name" );
                            readValidRequiredAttribute( m.address, "machine", "address" );
                            if( !strcmp( m.address, "DAS4" ) ) {
                                readValidRequiredAttribute( m.count, "machine", "DAS4 node count" );
                                char* end = NULL;
                                m.icount = strtol( m.count, &end, 10 );
                                if( end == m.count || *end || m.icount < 1 )
                                    quit( "Invalid DAS4 node count for machine %s\n", m.name );
                            }
                            break;
                        }
                        if( !strcmp( nodeName, "core" ) ) {
                            state = 3;
                            bzero( c, sizeof( struct core ) );
                            readValidRequiredAttribute( c.name, "core", "name" );
                            break;
                        }
                        if( !strcmp( nodeName, "file" ) ) {
                            state = 4;
                            bzero( f, sizeof( struct file ) );
                            readValidRequiredAttribute( f.name, "file", "name" );
                            readValidRequiredAttribute( f.size, "file", "size" );
                            char* end = NULL;
                            f.isize = strtol( f.size, &end, 10 );
                            if( end == f.size || f.isize < 1 )
                                quit( "Invalid size specifier for file %s\n", f.name );
                            if( *end && *(end+1) )
                                quit( "Invalid size specifier for file %s\n", f.name );
                            if( *end ) {
                                switch( *end ) {
                                    case 'b' :
                                    case 'B' :
                                        break;
                                    case 'k' :
                                    case 'K' :
                                        f.isize *= 1024;
                                        break;
                                    case 'm' :
                                    case 'M' :
                                        f.isize *= 1024 * 1024;
                                        break;
                                    case 'g' :
                                    case 'G' :
                                        f.isize *= 1024 * 1024 * 1024;
                                        break;
                                    case 't' :
                                    case 'T' :
                                        f.isize *= 1024 * 1024 * 1024 * 1024;
                                        break;
                                    default :
                                        quit( "Invalid size specifier for file %s\n", f.name );
                                }
                            }
                            if( f.isize > 512 * 1024 * 1024 ) {
                                int p = 512*1024*1024;
                                while( p < f.isize )
                                    p <<= 1;
                                if( p != f.isize )
                                    quit( "Invalid size specifier for file %s: sizes above 512M should be powers of 2\n", f.name );
                            }
                            if( f.isize & 0x3 )
                                quit( "Invalid size specifier for file %s: sizes should be a multiple of 4\n", f.name );
                            break;
                        }
                        if( !strcmp( nodeName, "test" ) ) {
                            state = 5;
                            bzero( t, sizeof( struct test ) );
                            readValidRequiredAttribute( t.name, "test", "name" );
                            break;
                        }
                        break;
                    case 1 :
                        if( !strcmp( nodeName, "tmpDir" ) ) {
                            onlyOne( m.tmpdir, "machine", "tmpDir" );
                            readValidString( m.tmpdir, "temporary directory location" );
                            skipToEndElement( "tmpDir" );
                            break;
                        }
                        if( !strcmp( nodeName, "params" ) ) {
                            onlyOne( m.params, "machine", "params" );
                            readValidString( m.params, "params" );
                            skipToEndElement( "params" );
                            break;
                        }
                        printf( "Unexpected element %s in machine %s\n", nodeName, m.name );
                        break;
                    case 2 :
                        if( !strcmp( nodeName, "machine" ) ) {
                            if( r.machineCount > 255 )
                                quit( "Maximum of 256 machines per role passed on line %d\n", xmlTextReaderGetParserLineNumber( xmlRead ) );
                            readValid( r.machine[r.machineCount], "machine name" );
                            r.machineCount++;
                            skipToEndElement( "machine" );
                            break;
                        }
                        if( !strcmp( nodeName, "user" ) ) {
                            onlyOne( r.user, "role", "user" );
                            readValidString( r.user, "user" );
                            skipToEndElement( "user" );
                            break;
                        }
                        if( !strcmp( nodeName, "core" ) ) {
                            onlyOne( r.core, "role", "core" );
                            readValidString( r.core, "core name" );
                            skipToEndElement( "core" );
                            break;
                        }
                        if( !strcmp( nodeName, "file" ) ) {
                            onlyOne( r.file, "role", "file" );
                            readValidString( r.file, "file name" );
                            skipToEndElement( "file" );
                            break;
                        }
                        if( !strcmp( nodeName, "params" ) ) {
                            onlyOne( r.params, "role", "params" );
                            readValidString( r.params, "params" );
                            skipToEndElement( "params" );
                            break;
                        }
                        printf( "Unexpected element %s in role on line %d\n", nodeName, xmlTextReaderGetParserLineNumber( xmlRead ) );
                        break;
                    case 3 :
                        if( !strcmp( nodeName, "params" ) ) {
                            onlyOne( c.params, "core", "params" );
                            readValidString( c.params, "params" );
                            skipToEndElement( "params" );
                            break;
                        }
                        if( !strcmp( nodeName, "localDir" ) ) {
                            onlyOne( c.localdir, "core", "localDir" );
                            readValidString( c.localdir, "local core directory" );
                            skipToEndElement( "localDir" );
                            break;
                        }
                        if( !strcmp( nodeName, "compilationDir" ) ) {
                            onlyOne( c.compdir, "core", "compilationDir" );
                            readValidString( c.compdir, "relative compilation directory" );
                            skipToEndElement( "compilationDir" );
                            break;
                        }
                        if( !strcmp( nodeName, "program" ) ) {
                            onlyOne( c.program, "core", "program" );
                            readValidString( c.program, "program name" );
                            skipToEndElement( "program" );
                            break;
                        }
                        printf( "Unexpected element %s in core %s\n", nodeName, c.name );
                        skipToEndElement( nodeName );
                        break;
                    case 4 :
                        if( !strcmp( nodeName, "offset" ) ) {
                            onlyOne( c.offset, "file", "offset" );
                            readValidString( c.offset, "offset" );
                            char* end = NULL;
                            f.ioffset = strtol( f.offset, &end, 10 );
                            if( end == f.offset || *end || f.ioffset < 0 || f >= f.isize )
                                quit( "Invalid file offset for file %s\n", f.name );
                            skipToEndElement( "offset" );
                            break;
                        }
                        printf( "Unexpected element %s in file %s\n", nodeName, f.name );
                        skipToEndElement( nodeName );
                        break;
                    case 5 :
                        if( !strcmp( nodeName, "role" ) ) {
                            state = 2;
                            bzero( r, sizeof( struct role ) );
                            readValidRequiredAttribute( r.type, "role", "type" );
                            if( strcmp( r.type, "seed" ) && strcmp( r.type, "leech" ) )
                                quit( "Invalid value for attribute 'type' for role on line %d, expected 'seed' or 'leech'\n", xmlTextReaderGetParserLineNumber( xmlRead ) );
                            break;
                        }
                        quit( "Unexpected element %s in test on line %d\n", nodeName, xmlTextReaderGetParserLineNumber( xmlRead ) );
                    default :
                        quit( "State sanity\n", nodeName );
                }
                break;
            case XmlNodeType.EndElement :
                nodeName = xmlTextReaderLocalName( xmlRead );
                switch( state ) {
                    case 0 :
                        quit( "End element %s found while not in entity\n", nodeName );
                    case 1 :
                        if( !strcmp( nodeName, "machine" ) ) {
                            memcpy( machines + machineCount, &m, sizeof( struct machine ) );
                            machineCount++;
                            state = 0;
                        }
                        break;
                    case 2 :
                        if( !strcmp( nodeName, "role" ) ) {
                            mempcy( t.roles + r.roleCount, &r, sizeof( struct role ) );
                            t.roleCount++;
                            state = 5;
                        }
                        break;
                    case 3 :
                        if( !strcmp( nodeName, "core" ) ) {
                            memcpy( cores + coreCount, &c, sizeof( struct core ) );
                            coreCount++;
                            state = 0;
                        }
                        break;
                    case 4 :
                        if( !strcmp( nodeName, "file" ) ) {
                            memcpy( files + fileCount, &f, sizeof( struct file ) );
                            fileCount++;
                            state = 0;
                        }
                        break;
                    case 5 :
                        if( !strcmp( nodeName, "test" ) ) {
                            memcpy( tests + testCount, &t, sizeof( struct test ) );
                            testCount++;
                            state = 0;
                        }
                        break;
                    default :
                        quit( "State sanity\n" );
                }
            default :
        }
        if( nodeName )
            free( nodeName );
    }
}

void validateConfigAndGenerateScripts( ) {
    int i, j, k, l;
    bool found;

    // == Validate test-role references to machines, cores and files
    // == Also register which machine uses which cores and which files
    for( i = 0; i < testCount; i++ ) {
        for( j = 0; j < tests[i].roleCount; j++ ) {
            // Check for validity of each role's core
            found = false;
            for( k = 0; k < coreCount; k++ ) {
                if( !strcmp( cores[k].name, tests[i].roles[j].core ) ) {
                    found = true;
                    break;
                }
            }
            if( !found )
                quit( "Test %s role %i refers to core %s which does not exist\n", tests[i].name, j, tests[i].roles[j].core );
            // Check for validity of each role's file
            found = false;
            for( k = 0; k < fileCount; k++ ) {
                if( !strcmp( files[k].name, tests[i].roles[j].file ) ) {
                    found = true;
                    break;
                }
            }
            if( !found )
                quit( "Test %s role %i refers to file %s which does not exist\n", tests[i].name, j, tests[i].roles[j].file );
            // Check for validity of each role's machines
            for( l = 0; l < tests[i].roles[j].machineCount; l++ ) {
                found = false;
                for( k = 0; k < machineCount; k++ ) {
                    if( !strcmp( machines[k].name, tests[i].roles[j].machines[l] ) ) {
                        // Machine found: register used core with the machine if needed
                        for( m = 0; m < machines[k].coreCount; m++ ) {
                            if( !strcmp( machines[k].cores[m], tests[i].roles[j].core ) ) {
                                found = true;
                                break;
                            }
                        }
                        if( !found )
                            machines[k].cores[machines[k].coreCount++] = tests[i].roles[j].core;
                        // Machine found: register used file with the machine if needed, only if seeding
                        if( !strcmp( tests[i].roles[j].type, "seed" ) ) {
                            found = false;
                            for( m = 0; m < machines[k].fileCount; m++ ) {
                                if( !strcmp( machines[k].files[m], tests[i].roles[j].file ) ) {
                                    found = true;
                                    break;
                                }
                            }
                            if( !found )
                                machines[k].files[machines[k].fileCount++] = tests[i].roles[j].file;
                        }
                        // Machine found
                        found = true;
                        break;
                    }
                }
                if( !found )
                    quit( "Test %s role %i refers to machine %s which does not exist\n", tests[i].name, j, tests[i].roles[j].machines[l] );
            }
        }
    }

    // Create temporary script
    FILE* script = tmpfile();
    if( !script )
        quit( "Can't create temporary script\n" );
    fprintf( script, "#!/bin/bash\n" );

    // == Check validity of machines and users: can each machine be accessed?
    for( i = 0; i < machineCount; i++ ) {
        // Creates a function in the script for sending command to the machine using ssh
        // Call using
        //    ssh_machine_%i "commands" || cleanup -1
        // where %i is the index of the machine in machines. Also be sure to return non-zero from your commands on error.
        fprintf( script, "function ssh_machine_%i {\n", i );
        if( strcmp( machines[i].address, "DAS4" ) )
            fprintf( script, "    ssh -T -n -o BatchMode=yes -h \"%s\"", machines[i].address );
        else
            fprintf( script, "    ssh -T -n -o BatchMode=yes -h fs3.das4.tudelft.nl" );
        if( machines[i].user )
            fprintf( script, " -l \"%s\"", machines[i].user );
        if( machines[i].params )
            fprintf( script, " %s", machines[i].params );
        fprintf( script, " $1 || return -1;\n" );
        fprintf( script, "}\n" );
        // Creates a function in the script for sending files to the machine using scp
        // Call using
        //    scp_to_machine_%i localfile remotefile
        fprintf( script, "function scp_to_machine_%i {\n", i );
        fprintf( script, "scp -o BatchMode=yes " );
        if( machines[i].params )
            fprintf( script, "%s ", machines[i].params );
        fprintf( script, "$1 ", l );
        if( machines[i].user )
            fprintf( script, "\"%s\"@", machines[i].user );
        if( strcmp( machines[i].address, "DAS4" ) )
            fprintf( script, "\"%s\"", machines[i].address );
        else
            fprintf( script, "fs3.das4.tudelft.nl" );
        fprintf( script, ":$2 || cleanup -1\n", i, l )
        fprintf( script, "}\n" );
        // Creates a function in the script for retrieving files from the machine using scp
        // Call using
        //    scp_from_machine_%i remotefile localfile
        fprintf( script, "function scp_from_machine_%i {\n", i );
        fprintf( script, "scp -o BatchMode=yes " );
        if( machines[i].params )
            fprintf( script, "%s ", machines[i].params );
        if( machines[i].user )
            fprintf( script, "\"%s\"@", machines[i].user );
        if( strcmp( machines[i].address, "DAS4" ) )
            fprintf( script, "\"%s\"", machines[i].address );
        else
            fprintf( script, "fs3.das4.tudelft.nl" );
        fprintf( script, ":$1 $2 || cleanup -1\n", i, l )
        fprintf( script, "}\n" );
        // Checks reachability of machine
        fprintf( script, "ssh_machine_%i || exit -1\n", i );
    }

    // == Check validity of each core: can each core be packaged? Can it be compiled locally and does the program then exist?
    // Create a cleanup file. This file should have code appended to cleanup things when errors occur or testing has finished. See existing code for examples of concatenating to it.
    // The cleanup function is available after this as well. Call it with an exit argument to end the script cleanly.
    fprintf( script, "CLEANUPFILE=`mktemp`\n" );
    fprintf( script, "chmod +x CLEANUPFILE\n" );
    fprintf( script, "[ -x CLEANUPFILE ] || exit -1\n" );
    fprintf( script, "function cleanup {\n" );
    fprintf( script, "    (\ncat <<EOL\nrm $CLEANUPFILE\nEOL\n) >> $CLEANUPFILE\n" );
    fprintf( script, "    . $CLEANUPFILE\n" );
    fprintf( script  "    exit $1\n" );
    fprintf( script, "}\n" );
    // Create a local temporary directory for storage
    fprintf( script, "TARDIR=`mktemp -d`\n[ \"X${TARDIR}X\" == \"XX\" ] && exit -1\n" );
    fprintf( script, "(\ncat <<EOL\n#!/bin/bash\nrm -rf $TARDIR\nEOL\n) >> $CLEANUPFILE\n" );
    fprintf( script, "CURDIR=`pwd`\n" );
    // Create a tarball for the testenvironment in the temporary local storage
    fprintf( script, "make clean || cleanup -1\n" );
    fprintf( script, "tar cf ${TARDIR}/testenvironment.tar . || cleanup -1\n" );
    fprintf( script, "bzip2 ${TARDIR}/testenvironment.tar || cleanup -1\n" );
    // Check whether the needed tools of the testenvironment compile locally
    fprintf( script, "make genfakedata || cleanup -1\n" );
    for( i = 0; i < coreCount; i++ ) {
        if( !cores[i].localdir )
            cores[i].localdir = strdup( "../" );
        // Create a tarball for the core in the temporary local storage
        fprintf( script, "cd %s || cleanup -1\n", cores[i].localdir );
        fprintf( script, "make clean\n" ); // Not checked: SHOULD be available, but...
        fprintf( script, "tar cf ${TARDIR}/core_%i.tar . || cleanup -1\n", i );
        fprintf( script, "bzip2 ${TARDIR}/core_%i.tar || cleanup -1\n", i );
        if( !cores[i].compdir )
            cores[i].compdir = strdup( "testenvironment/" );
        // Check whether the core compiles locally and the program exists after
        fprintf( script, "cd %s || cleanup -1\n", cores[i].compdir );
        fprintf( script, "make || cleanup -1\n" );
        if( !cores[i].program )
            cores[i].program = strdup( "swift" );
        fprintf( script, "[ -x %s ] || cleanup -1\n", cores[i].program );
        fprintf( script, "cd ${CURDIR}\n" );
    }

    // For each file, precalculate the hash
    for( i = 0; i < fileCount; i++ ) {
        // Create some temporary file to write fake data to. These fake data files will be regenerated at each machine since generating is faster than copying.
        size_t size = files[i].isize;
        FILE* data = tmpfile();
        if( !data )
            quit( "can't create temporary data file\n" );
        int datan = fileno( data );
        int filesize;
        if( size > 512*1024*1024 )
            filesize = 512*1024*1024;
        else
            filesize = size;
        if( generateFakeData( datan, filesize ) )
            quit( "could not write fake data\n" );
        
        MemoryHashStorage mhs;
        FileOffsetDataStorage fods( datan, files[i].ioffset );
        if( !fods.valid() )
            quit( "can't read back from temporary data file\n" );
        HashTree ht( fods, Sha1Hash::ZERO, mhs );
        Sha1Hash hash = ht.root_hash();
        if( size > filesize ) {
            MemoryHashStorage mhs2;
            int max = size/filesize;
            for( j = 0; j < max; j++ )
                mhs2.setHash( bin64(0,j), hash );
            int lvl = 0;
            do {
                max >>= 1;
                lvl++;
                for( j = 0; j < max; j++ )
                    mhs2.hashLeftRight( bin64_t(lvl, j) );
            } while( max > 1 );
            files[i].hash = mhs.getHash( bin64_t(lvl, 0) );
        }
        else
Exemplo n.º 21
0
bool IWORKParser::parse()
{
  const shared_ptr<xmlTextReader> sharedReader(xmlReaderForIO(readFromStream, closeStream, m_input.get(), "", nullptr, 0), xmlFreeTextReader);
  if (!sharedReader)
    return false;

  xmlTextReaderPtr reader = sharedReader.get();
  assert(reader);

  const IWORKTokenizer &tokenizer = getTokenizer();
  stack<IWORKXMLContextPtr_t> contextStack;

  int ret = xmlTextReaderRead(reader);
  contextStack.push(createDocumentContext());

  bool keynoteDocTypeChecked=false;
  char const *defaultNS=nullptr;
  while ((1 == ret))
  {
    switch (xmlTextReaderNodeType(reader))
    {
    case XML_READER_TYPE_ELEMENT:
    {
      if (!keynoteDocTypeChecked)
      {
        // check for keynote 1 file with doctype node and not a namespace in first node
        keynoteDocTypeChecked=true;
        if (xmlTextReaderNodeType(reader)==XML_READER_TYPE_ELEMENT &&
            xmlTextReaderConstNamespaceUri(reader)==nullptr)
          defaultNS="http://developer.apple.com/schemas/APXL";
      }
      const int id = tokenizer.getQualifiedId(char_cast(xmlTextReaderConstLocalName(reader)),
                                              defaultNS ? defaultNS : char_cast(xmlTextReaderConstNamespaceUri(reader)));

      IWORKXMLContextPtr_t newContext = contextStack.top()->element(id);

      if (!newContext)
        newContext = createDiscardContext();

      const bool isEmpty = xmlTextReaderIsEmptyElement(reader);

      newContext->startOfElement();
      if (xmlTextReaderHasAttributes(reader))
      {
        ret = xmlTextReaderMoveToFirstAttribute(reader);
        while (1 == ret)
        {
          processAttribute(reader, newContext, tokenizer);
          ret = xmlTextReaderMoveToNextAttribute(reader);
        }
      }

      if (isEmpty)
        newContext->endOfElement();
      else
        contextStack.push(newContext);

      break;
    }
    case XML_READER_TYPE_END_ELEMENT:
    {
      contextStack.top()->endOfElement();
      contextStack.pop();
      break;
    }
    case XML_READER_TYPE_CDATA :
    {
      const xmlChar *text = xmlTextReaderConstValue(reader);
      if (text) contextStack.top()->CDATA(char_cast(text));
      break;
    }
    case XML_READER_TYPE_TEXT :
    {
      xmlChar *const text = xmlTextReaderReadString(reader);
      contextStack.top()->text(char_cast(text));
      xmlFree(text);
      break;
    }
    default:
      break;
    }

    ret = xmlTextReaderRead(reader);

  }

  while (!contextStack.empty()) // finish parsing in case of broken XML
  {
    contextStack.top()->endOfElement();
    contextStack.pop();
  }
  xmlTextReaderClose(reader);

  return true;
}
Exemplo n.º 22
0
/*
 * this function tries to convert configuration in files into scf
 * it loads xml conf into node tree then dump them to scf with
 * mgmt_config_save2scf()
 * this function has 3 return values:
 * CONVERT_OK: successfully converted
 * CONVERT_INIT_NEW: configuration files dont exist, created a new scf entry
 * CONVERT_FAIL: some error occurred in conversion and no scf entry created.
 *               In this case, user have to check files manually and try
 *               conversion again.
 */
convert_ret_t
mgmt_convert_conf()
{
	targ_scf_t		*h = NULL;
	xmlTextReaderPtr	r;
	convert_ret_t		ret = CONVERT_FAIL;
	int			xml_fd = -1;
	int			n;
	tgt_node_t		*node = NULL;
	tgt_node_t		*next = NULL;
	char			path[MAXPATHLEN];
	char			*target = NULL;

	h = mgmt_handle_init();
	if (h == NULL)
		return (CONVERT_FAIL);

	/* check main config in pgroup iscsitgt */
	if (scf_service_get_pg(h->t_service, "iscsitgt", h->t_pg) == 0) {
		ret = CONVERT_OK;
		goto done;
	}

	/* check the conf files */
	if (access(config_file, R_OK) != 0) {
		/*
		 * if there is no configuration file, initialize
		 * an empty scf entry
		 */
		if (mgmt_transaction_start(h, "iscsitgt", "basic") == True) {
			ret = CONVERT_INIT_NEW;

			node = tgt_node_alloc(XML_ELEMENT_VERS, String, "1.0");
			new_property(h, node);
			tgt_node_free(node);
			/* "daemonize" is set to true by default */
			node = tgt_node_alloc(XML_ELEMENT_DBGDAEMON, String,
			    "true");
			new_property(h, node);
			tgt_node_free(node);
			node = NULL;
			node = tgt_node_alloc(ISCSI_MODIFY_AUTHNAME, String,
			    ISCSI_AUTH_MODIFY);
			new_property(h, node);
			tgt_node_free(node);
			node = tgt_node_alloc(ISCSI_VALUE_AUTHNAME, String,
			    ISCSI_AUTH_VALUE);
			new_property(h, node);
			tgt_node_free(node);
			mgmt_transaction_end(h);
		} else {
			syslog(LOG_ERR, "Creating empty entry failed");
			ret = CONVERT_FAIL;
			goto done;
		}
		if (mgmt_transaction_start(h, "passwords", "application") ==
		    True) {
			node = tgt_node_alloc(ISCSI_READ_AUTHNAME, String,
			    ISCSI_AUTH_READ);
			new_property(h, node);
			tgt_node_free(node);
			node = tgt_node_alloc(ISCSI_MODIFY_AUTHNAME, String,
			    ISCSI_AUTH_MODIFY);
			new_property(h, node);
			tgt_node_free(node);
			node = tgt_node_alloc(ISCSI_VALUE_AUTHNAME, String,
			    ISCSI_AUTH_VALUE);
			new_property(h, node);
			tgt_node_free(node);
			mgmt_transaction_end(h);
		} else {
			syslog(LOG_ERR, "Creating empty entry failed");
			ret = CONVERT_FAIL;
		}
		goto done;
	}

	if ((xml_fd = open(config_file, O_RDONLY)) >= 0)
		r = (xmlTextReaderPtr)xmlReaderForFd(xml_fd, NULL, NULL, 0);

	if (r != NULL) {
		n = xmlTextReaderRead(r);
		while (n == 1) {
			if (tgt_node_process(r, &node) == False) {
				break;
			}
			n = xmlTextReaderRead(r);
		}
		if (n < 0) {
			syslog(LOG_ERR, "Parsing main config failed");
			ret = CONVERT_FAIL;
			goto done;
		}

		main_config = node;

		(void) tgt_find_value_str(node, XML_ELEMENT_BASEDIR,
		    &target_basedir);

		if (target_basedir == NULL)
			target_basedir = strdup(DEFAULT_TARGET_BASEDIR);

		/* Now convert targets' config if possible */
		if (xml_fd != -1)
			(void) close(xml_fd);
		xmlTextReaderClose(r);
		xmlFreeTextReader(r);
		xmlCleanupParser();
		r = NULL;
		xml_fd = -1;
		node = NULL;

		(void) snprintf(path, MAXPATHLEN, "%s/%s",
		    target_basedir, "config.xml");

		if ((xml_fd = open(path, O_RDONLY)) >= 0)
			r = (xmlTextReaderPtr)xmlReaderForFd(xml_fd,
			    NULL, NULL, 0);

		if (r != NULL) {
			n = xmlTextReaderRead(r);
			while (n == 1) {
				if (tgt_node_process(r, &node) == False) {
					break;
				}
				n = xmlTextReaderRead(r);
			}
			if (n < 0) {
				syslog(LOG_ERR, "Parsing target conf failed");
				ret = CONVERT_FAIL;
				goto done;
			}

			/* now combine main_config and node */
			if (node) {
				next = NULL;
				while ((next = tgt_node_next(node,
				    XML_ELEMENT_TARG, next)) != NULL) {
					tgt_node_add(main_config,
					    tgt_node_dup(next));
				}
				tgt_node_free(node);
			}

			if (mgmt_config_save2scf() != True) {
				syslog(LOG_ERR, "Converting config failed");
				if (xml_fd != -1)
					(void) close(xml_fd);
				xmlTextReaderClose(r);
				xmlFreeTextReader(r);
				xmlCleanupParser();
				ret = CONVERT_FAIL;
				goto done;
			}

			/* Copy files into backup dir */
			(void) snprintf(path, sizeof (path), "%s/backup",
			    target_basedir);
			if ((mkdir(path, 0755) == -1) && (errno != EEXIST)) {
				syslog(LOG_ERR, "Creating backup dir failed");
				ret = CONVERT_FAIL;
				goto done;
			}
			backup(config_file, NULL);
			(void) snprintf(path, MAXPATHLEN, "%s/%s",
			    target_basedir, "config.xml");
			backup(path, NULL);


			while ((next = tgt_node_next(main_config,
			    XML_ELEMENT_TARG, next)) != NULL) {
				if (tgt_find_value_str(next, XML_ELEMENT_INAME,
				    &target) == False) {
					continue;
				}
				(void) snprintf(path, MAXPATHLEN, "%s/%s",
				    target_basedir, target);
				if (mgmt_convert_param(path, next)
				    != True) {
					ret = CONVERT_FAIL;
					goto done;
				}
				free(target);
			}

			ret = CONVERT_OK;
			syslog(LOG_NOTICE, "Conversion succeeded");

			xmlTextReaderClose(r);
			xmlFreeTextReader(r);
			xmlCleanupParser();
		} else {
			syslog(LOG_ERR, "Reading targets config failed");
			ret = CONVERT_FAIL;
			goto done;
		}
	} else {
		syslog(LOG_ERR, "Reading main config failed");
		ret = CONVERT_FAIL;
		goto done;
	}

done:
	if (xml_fd != -1)
		(void) close(xml_fd);
	mgmt_handle_fini(h);
	return (ret);
}
Exemplo n.º 23
0
/*
 * call-seq:
 *    reader.close -> code
 *
 * This method releases any resources allocated by the current instance
 * changes the state to Closed and close any underlying input.
 */
static VALUE rxml_reader_close(VALUE self)
{
  xmlTextReaderPtr xreader = rxml_text_reader_get(self);
  return INT2FIX(xmlTextReaderClose(xreader));
}
Exemplo n.º 24
0
int cli_scanxar(cli_ctx *ctx)
{
    int rc = CL_SUCCESS;
    unsigned int cksum_fails = 0;
    unsigned int extract_errors = 0;
#if HAVE_LIBXML2
    int fd = -1;
    struct xar_header hdr;
    fmap_t *map = *ctx->fmap;
    long length, offset, size, at;
    int encoding;
    z_stream strm;
    char *toc, *tmpname;
    xmlTextReaderPtr reader = NULL;
    int a_hash, e_hash;
    unsigned char *a_cksum = NULL, *e_cksum = NULL;

    memset(&strm, 0x00, sizeof(z_stream));

    /* retrieve xar header */
    if (fmap_readn(*ctx->fmap, &hdr, 0, sizeof(hdr)) != sizeof(hdr)) {
        cli_dbgmsg("cli_scanxar: Invalid header, too short.\n");
        return CL_EFORMAT;
    }
    hdr.magic = be32_to_host(hdr.magic);

    if (hdr.magic == XAR_HEADER_MAGIC) {
        cli_dbgmsg("cli_scanxar: Matched magic\n");
    }
    else {
        cli_dbgmsg("cli_scanxar: Invalid magic\n");
        return CL_EFORMAT;
    }
    hdr.size = be16_to_host(hdr.size);
    hdr.version = be16_to_host(hdr.version);
    hdr.toc_length_compressed = be64_to_host(hdr.toc_length_compressed);
    hdr.toc_length_decompressed = be64_to_host(hdr.toc_length_decompressed);
    hdr.chksum_alg = be32_to_host(hdr.chksum_alg);

    /* cli_dbgmsg("hdr.magic %x\n", hdr.magic); */
    /* cli_dbgmsg("hdr.size %i\n", hdr.size); */
    /* cli_dbgmsg("hdr.version %i\n", hdr.version); */
    /* cli_dbgmsg("hdr.toc_length_compressed %lu\n", hdr.toc_length_compressed); */
    /* cli_dbgmsg("hdr.toc_length_decompressed %lu\n", hdr.toc_length_decompressed); */
    /* cli_dbgmsg("hdr.chksum_alg %i\n", hdr.chksum_alg); */

    /* Uncompress TOC */
    strm.next_in = (unsigned char *)fmap_need_off_once(*ctx->fmap, hdr.size, hdr.toc_length_compressed);
    if (strm.next_in == NULL) {
        cli_dbgmsg("cli_scanxar: fmap_need_off_once fails on TOC.\n");
        return CL_EREAD;
    }
    strm.avail_in = hdr.toc_length_compressed;
    toc = cli_malloc(hdr.toc_length_decompressed+1);
    if (toc == NULL) {
        cli_dbgmsg("cli_scanxar: cli_malloc fails on TOC decompress buffer.\n");
        return CL_EMEM;
    }
    toc[hdr.toc_length_decompressed] = '\0';
    strm.avail_out = hdr.toc_length_decompressed;
    strm.next_out = (unsigned char *)toc;
    rc = inflateInit(&strm);
    if (rc != Z_OK) {
        cli_dbgmsg("cli_scanxar:inflateInit error %i \n", rc);
        rc = CL_EFORMAT;
        goto exit_toc;
    }
    rc = inflate(&strm, Z_SYNC_FLUSH);
    if (rc != Z_OK && rc != Z_STREAM_END) {
        cli_dbgmsg("cli_scanxar:inflate error %i \n", rc);
        rc = CL_EFORMAT;
        goto exit_toc;
    }
    rc = inflateEnd(&strm);
    if (rc != Z_OK) {
        cli_dbgmsg("cli_scanxar:inflateEnd error %i \n", rc);
        rc = CL_EFORMAT;
        goto exit_toc;
    }

    /* cli_dbgmsg("cli_scanxar: TOC xml:\n%s\n", toc); */
    /* printf("cli_scanxar: TOC xml:\n%s\n", toc); */
    /* cli_dbgmsg("cli_scanxar: TOC end:\n"); */
    /* printf("cli_scanxar: TOC end:\n"); */

    /* scan the xml */
    cli_dbgmsg("cli_scanxar: scanning xar TOC xml in memory.\n");
    rc = cli_mem_scandesc(toc, hdr.toc_length_decompressed, ctx);
    if (rc != CL_SUCCESS) {
        if (rc != CL_VIRUS || !SCAN_ALL)
            goto exit_toc;
    }

    /* make a file to leave if --leave-temps in effect */
    if(ctx->engine->keeptmp) {
        if ((rc = cli_gentempfd(ctx->engine->tmpdir, &tmpname, &fd)) != CL_SUCCESS) {
            cli_dbgmsg("cli_scanxar: Can't create temporary file for TOC.\n");
            goto exit_toc;
        }
        if (cli_writen(fd, toc, hdr.toc_length_decompressed) < 0) {
            cli_dbgmsg("cli_scanxar: cli_writen error writing TOC.\n");
            rc = CL_EWRITE;
            xar_cleanup_temp_file(ctx, fd, tmpname);
            goto exit_toc;
        }
        rc = xar_cleanup_temp_file(ctx, fd, tmpname);
        if (rc != CL_SUCCESS)
            goto exit_toc;
    }

    reader = xmlReaderForMemory(toc, hdr.toc_length_decompressed, "noname.xml", NULL, 0);
    if (reader == NULL) {
        cli_dbgmsg("cli_scanxar: xmlReaderForMemory error for TOC\n");
        goto exit_toc;
    }

    rc = xar_scan_subdocuments(reader, ctx);
    if (rc != CL_SUCCESS) {
        cli_dbgmsg("xar_scan_subdocuments returns %i.\n", rc);
        goto exit_reader;
    }

    /* Walk the TOC XML and extract files */
    fd = -1;
    tmpname = NULL;
    while (CL_SUCCESS == (rc = xar_get_toc_data_values(reader, &length, &offset, &size, &encoding,
                               &a_cksum, &a_hash, &e_cksum, &e_hash))) {
        int do_extract_cksum = 1;
        unsigned char * blockp;
        void *a_sc, *e_sc;
        void *a_mc, *e_mc;
        void *a_hash_ctx, *e_hash_ctx;
        char result[SHA1_HASH_SIZE];
        char * expected;

        /* clean up temp file from previous loop iteration */
        if (fd > -1 && tmpname) {
            rc = xar_cleanup_temp_file(ctx, fd, tmpname);
            if (rc != CL_SUCCESS)
                goto exit_reader;
        }

        at = offset + hdr.toc_length_compressed + hdr.size;

        if ((rc = cli_gentempfd(ctx->engine->tmpdir, &tmpname, &fd)) != CL_SUCCESS) {
            cli_dbgmsg("cli_scanxar: Can't generate temporary file.\n");
            goto exit_reader;
        }

        cli_dbgmsg("cli_scanxar: decompress into temp file:\n%s, size %li,\n"
                   "from xar heap offset %li length %li\n",
                   tmpname, size, offset, length);


        a_hash_ctx = xar_hash_init(a_hash, &a_sc, &a_mc);
        e_hash_ctx = xar_hash_init(e_hash, &e_sc, &e_mc);

        switch (encoding) {
        case CL_TYPE_GZ:
            /* inflate gzip directly because file segments do not contain magic */
            memset(&strm, 0, sizeof(strm));
            if ((rc = inflateInit(&strm)) != Z_OK) {
                cli_dbgmsg("cli_scanxar: InflateInit failed: %d\n", rc);
                rc = CL_EFORMAT;
                extract_errors++;
                break;
            }

            while ((size_t)at < map->len && (unsigned long)at < offset+hdr.toc_length_compressed+hdr.size+length) {
                unsigned long avail_in;
                void * next_in;
                unsigned int bytes = MIN(map->len - at, map->pgsz);
                bytes = MIN(length, bytes);
                if(!(strm.next_in = next_in = (void*)fmap_need_off_once(map, at, bytes))) {
                    cli_dbgmsg("cli_scanxar: Can't read %u bytes @ %lu.\n", bytes, (long unsigned)at);
                    inflateEnd(&strm);
                    rc = CL_EREAD;
                    goto exit_tmpfile;
                }
                at += bytes;
                strm.avail_in = avail_in = bytes;
                do {
                    int inf, outsize = 0;
                    unsigned char buff[FILEBUFF];
                    strm.avail_out = sizeof(buff);
                    strm.next_out = buff;
                    inf = inflate(&strm, Z_SYNC_FLUSH);
                    if (inf != Z_OK && inf != Z_STREAM_END && inf != Z_BUF_ERROR) {
                        cli_dbgmsg("cli_scanxar: inflate error %i %s.\n", inf, strm.msg?strm.msg:"");
                        rc = CL_EFORMAT;
                        extract_errors++;
                        break;
                    }

                    bytes = sizeof(buff) - strm.avail_out;

                    xar_hash_update(e_hash_ctx, buff, bytes, e_hash);

                    if (cli_writen(fd, buff, bytes) < 0) {
                        cli_dbgmsg("cli_scanxar: cli_writen error file %s.\n", tmpname);
                        inflateEnd(&strm);
                        rc = CL_EWRITE;
                        goto exit_tmpfile;
                    }
                    outsize += sizeof(buff) - strm.avail_out;
                    if (cli_checklimits("cli_scanxar", ctx, outsize, 0, 0) != CL_CLEAN) {
                        break;
                    }
                    if (inf == Z_STREAM_END) {
                        break;
                    }
                } while (strm.avail_out == 0);

                if (rc != CL_SUCCESS)
                    break;

                avail_in -= strm.avail_in;
                xar_hash_update(a_hash_ctx, next_in, avail_in, a_hash);
            }

            inflateEnd(&strm);
            break;
        case CL_TYPE_7Z:
#define CLI_LZMA_OBUF_SIZE 1024*1024
#define CLI_LZMA_HDR_SIZE LZMA_PROPS_SIZE+8
#define CLI_LZMA_IBUF_SIZE CLI_LZMA_OBUF_SIZE>>2 /* estimated compression ratio 25% */
        {
            struct CLI_LZMA lz;
            unsigned long in_remaining = length;
            unsigned long out_size = 0;
            unsigned char * buff = __lzma_wrap_alloc(NULL, CLI_LZMA_OBUF_SIZE);
            int lret;

            memset(&lz, 0, sizeof(lz));
            if (buff == NULL) {
                cli_dbgmsg("cli_scanxar: memory request for lzma decompression buffer fails.\n");
                rc = CL_EMEM;
                goto exit_tmpfile;

            }

            blockp = (void*)fmap_need_off_once(map, at, CLI_LZMA_HDR_SIZE);
            if (blockp == NULL) {
                char errbuff[128];
                cli_strerror(errno, errbuff, sizeof(errbuff));
                cli_dbgmsg("cli_scanxar: Can't read %li bytes @ %li, errno:%s.\n",
                           length, at, errbuff);
                rc = CL_EREAD;
                __lzma_wrap_free(NULL, buff);
                goto exit_tmpfile;
            }

            lz.next_in = blockp;
            lz.avail_in = CLI_LZMA_HDR_SIZE;

            xar_hash_update(a_hash_ctx, blockp, CLI_LZMA_HDR_SIZE, a_hash);

            lret = cli_LzmaInit(&lz, 0);
            if (lret != LZMA_RESULT_OK) {
                cli_dbgmsg("cli_scanxar: cli_LzmaInit() fails: %i.\n", lret);
                rc = CL_EFORMAT;
                __lzma_wrap_free(NULL, buff);
                extract_errors++;
                break;
            }

            at += CLI_LZMA_HDR_SIZE;
            in_remaining -= CLI_LZMA_HDR_SIZE;
            while ((size_t)at < map->len && (unsigned long)at < offset+hdr.toc_length_compressed+hdr.size+length) {
                SizeT avail_in;
                SizeT avail_out;
                void * next_in;
                unsigned long in_consumed;

                lz.next_out = buff;
                lz.avail_out = CLI_LZMA_OBUF_SIZE;
                lz.avail_in = avail_in = MIN(CLI_LZMA_IBUF_SIZE, in_remaining);
                lz.next_in = next_in = (void*)fmap_need_off_once(map, at, lz.avail_in);
                if (lz.next_in == NULL) {
                    char errbuff[128];
                    cli_strerror(errno, errbuff, sizeof(errbuff));
                    cli_dbgmsg("cli_scanxar: Can't read %li bytes @ %li, errno: %s.\n",
                               length, at, errbuff);
                    rc = CL_EREAD;
                    __lzma_wrap_free(NULL, buff);
                    cli_LzmaShutdown(&lz);
                    goto exit_tmpfile;
                }

                lret = cli_LzmaDecode(&lz);
                if (lret != LZMA_RESULT_OK && lret != LZMA_STREAM_END) {
                    cli_dbgmsg("cli_scanxar: cli_LzmaDecode() fails: %i.\n", lret);
                    rc = CL_EFORMAT;
                    extract_errors++;
                    break;
                }

                in_consumed = avail_in - lz.avail_in;
                in_remaining -= in_consumed;
                at += in_consumed;
                avail_out = CLI_LZMA_OBUF_SIZE - lz.avail_out;

                if (avail_out == 0)
                    cli_dbgmsg("cli_scanxar: cli_LzmaDecode() produces no output for "
                               "avail_in %lu, avail_out %lu.\n", avail_in, avail_out);

                xar_hash_update(a_hash_ctx, next_in, in_consumed, a_hash);
                xar_hash_update(e_hash_ctx, buff, avail_out, e_hash);

                /* Write a decompressed block. */
                /* cli_dbgmsg("Writing %li bytes to LZMA decompress temp file, " */
                /*            "consumed %li of %li available compressed bytes.\n", */
                /*            avail_out, in_consumed, avail_in); */

                if (cli_writen(fd, buff, avail_out) < 0) {
                    cli_dbgmsg("cli_scanxar: cli_writen error writing lzma temp file for %li bytes.\n",
                               avail_out);
                    __lzma_wrap_free(NULL, buff);
                    cli_LzmaShutdown(&lz);
                    rc = CL_EWRITE;
                    goto exit_tmpfile;
                }

                /* Check file size limitation. */
                out_size += avail_out;
                if (cli_checklimits("cli_scanxar", ctx, out_size, 0, 0) != CL_CLEAN) {
                    break;
                }

                if (lret == LZMA_STREAM_END)
                    break;
            }

            cli_LzmaShutdown(&lz);
            __lzma_wrap_free(NULL, buff);
        }
        break;
        case CL_TYPE_ANY:
        default:
        case CL_TYPE_BZ:
        case CL_TYPE_XZ:
            /* for uncompressed, bzip2, xz, and unknown, just pull the file, cli_magic_scandesc does the rest */
            do_extract_cksum = 0;
            {
                unsigned long write_len;

                if (ctx->engine->maxfilesize)
                    write_len = MIN((size_t)(ctx->engine->maxfilesize), (size_t)length);
                else
                    write_len = length;

                if (!(blockp = (void*)fmap_need_off_once(map, at, length))) {
                    char errbuff[128];
                    cli_strerror(errno, errbuff, sizeof(errbuff));
                    cli_dbgmsg("cli_scanxar: Can't read %li bytes @ %li, errno:%s.\n",
                               length, at, errbuff);
                    rc = CL_EREAD;
                    goto exit_tmpfile;
                }

                xar_hash_update(a_hash_ctx, blockp, length, a_hash);

                if (cli_writen(fd, blockp, write_len) < 0) {
                    cli_dbgmsg("cli_scanxar: cli_writen error %li bytes @ %li.\n", length, at);
                    rc = CL_EWRITE;
                    goto exit_tmpfile;
                }
                /*break;*/
            }
        }

        if (rc == CL_SUCCESS) {
            xar_hash_final(a_hash_ctx, result, a_hash);
            if (a_cksum != NULL) {
                expected = cli_hex2str((char *)a_cksum);
                if (xar_hash_check(a_hash, result, expected) != 0) {
                    cli_dbgmsg("cli_scanxar: archived-checksum missing or mismatch.\n");
                    cksum_fails++;
                } else {
                    cli_dbgmsg("cli_scanxar: archived-checksum matched.\n");
                }
                free(expected);
            }
            if (e_cksum != NULL) {
                if (do_extract_cksum) {
                    xar_hash_final(e_hash_ctx, result, e_hash);
                    expected = cli_hex2str((char *)e_cksum);
                    if (xar_hash_check(e_hash, result, expected) != 0) {
                        cli_dbgmsg("cli_scanxar: extracted-checksum missing or mismatch.\n");
                        cksum_fails++;
                    } else {
                        cli_dbgmsg("cli_scanxar: extracted-checksum matched.\n");
                    }
                    free(expected);
                }
            }

            rc = cli_magic_scandesc(fd, ctx);
            if (rc != CL_SUCCESS) {
                if (rc == CL_VIRUS) {
                    cli_dbgmsg("cli_scanxar: Infected with %s\n", cli_get_last_virus(ctx));
                    if (!SCAN_ALL)
                        goto exit_tmpfile;
                } else if (rc != CL_BREAK) {
                    cli_dbgmsg("cli_scanxar: cli_magic_scandesc error %i\n", rc);
                    goto exit_tmpfile;
                }
            }
        }

        if (a_cksum != NULL) {
            xmlFree(a_cksum);
            a_cksum = NULL;
        }
        if (e_cksum != NULL) {
            xmlFree(e_cksum);
            e_cksum = NULL;
        }
    }

exit_tmpfile:
    xar_cleanup_temp_file(ctx, fd, tmpname);

exit_reader:
    if (a_cksum != NULL)
        xmlFree(a_cksum);
    if (e_cksum != NULL)
        xmlFree(e_cksum);
    xmlTextReaderClose(reader);
    xmlFreeTextReader(reader);

exit_toc:
    free(toc);
    if (rc == CL_BREAK)
        rc = CL_SUCCESS;
#else
    cli_dbgmsg("cli_scanxar: can't scan xar files, need libxml2.\n");
#endif
    if (cksum_fails + extract_errors != 0) {
        cli_warnmsg("cli_scanxar: %u checksum errors and %u extraction errors, use --debug for more info.\n",
                    cksum_fails, extract_errors);
    }

    return rc;
}
Exemplo n.º 25
0
bool IWORKParser::parse()
{
  const shared_ptr<xmlTextReader> sharedReader(xmlReaderForIO(readFromStream, closeStream, m_input.get(), "", 0, 0), xmlFreeTextReader);
  if (!sharedReader)
    return false;

  xmlTextReaderPtr reader = sharedReader.get();
  assert(reader);

  const IWORKTokenizer &tokenizer = getTokenizer();
  stack<IWORKXMLContextPtr_t> contextStack;

  int ret = xmlTextReaderRead(reader);
  contextStack.push(createDocumentContext());

  while ((1 == ret))
  {
    switch (xmlTextReaderNodeType(reader))
    {
    case XML_READER_TYPE_ELEMENT:
    {
      const int id = tokenizer.getQualifiedId(char_cast(xmlTextReaderConstLocalName(reader)), char_cast(xmlTextReaderConstNamespaceUri(reader)));

      IWORKXMLContextPtr_t newContext = contextStack.top()->element(id);

      if (!newContext)
        newContext = createDiscardContext();

      const bool isEmpty = xmlTextReaderIsEmptyElement(reader);

      newContext->startOfElement();
      if (xmlTextReaderHasAttributes(reader))
      {
        ret = xmlTextReaderMoveToFirstAttribute(reader);
        while (1 == ret)
        {
          processAttribute(reader, newContext, tokenizer);
          ret = xmlTextReaderMoveToNextAttribute(reader);
        }
      }

      if (isEmpty)
        newContext->endOfElement();
      else
        contextStack.push(newContext);

      break;
    }
    case XML_READER_TYPE_END_ELEMENT:
    {
      contextStack.top()->endOfElement();
      contextStack.pop();
      break;
    }
    case XML_READER_TYPE_TEXT :
    {
      xmlChar *const text = xmlTextReaderReadString(reader);
      contextStack.top()->text(char_cast(text));
      xmlFree(text);
      break;
    }
    default:
      break;
    }

    ret = xmlTextReaderRead(reader);

  }

  while (!contextStack.empty()) // finish parsing in case of broken XML
  {
    contextStack.top()->endOfElement();
    contextStack.pop();
  }
  xmlTextReaderClose(reader);

  return true;
}
Exemplo n.º 26
0
int openioc_parse(const char * fname, int fd, struct cl_engine *engine, unsigned int options)
{
    int rc;
    xmlTextReaderPtr reader = NULL;
    const xmlChar * name;
    struct openioc_hash * elems = NULL, * elem = NULL;
    const char * iocp = NULL;
    uint16_t ioclen;
    char * virusname;
    int hash_count = 0;
    
    if (fname == NULL)
        return CL_ENULLARG;

    if (fd < 0)
        return CL_EARG;

    cli_dbgmsg("openioc_parse: XML parsing file %s\n", fname);

    reader = xmlReaderForFd(fd, NULL, NULL, CLAMAV_MIN_XMLREADER_FLAGS);
    if (reader == NULL) {
        cli_dbgmsg("openioc_parse: xmlReaderForFd error\n");
        return CL_EOPEN;
    }
    rc = xmlTextReaderRead(reader);
    while (rc == 1) {
        name = xmlTextReaderConstLocalName(reader);
        cli_dbgmsg("openioc_parse: xmlTextReaderRead read %s\n", name);
        if (xmlStrEqual(name, (const xmlChar *)"Indicator") && 
            xmlTextReaderNodeType(reader) == XML_READER_TYPE_ELEMENT) {
            rc = openioc_parse_indicator(reader, &elems);
            if (rc != CL_SUCCESS) {
                xmlTextReaderClose(reader);
                xmlFreeTextReader(reader);
                return rc;
            }
        }
        if (xmlStrEqual(name, (const xmlChar *)"ioc") &&
            xmlTextReaderNodeType(reader) == XML_READER_TYPE_END_ELEMENT) {
            break;
        }
        rc = xmlTextReaderRead(reader);
    }

    iocp = strrchr(fname, *PATHSEP);

    if (NULL == iocp)
        iocp = fname;
    else
        iocp++;

    ioclen = strlen(fname);

    if (elems != NULL) {
        if (NULL == engine->hm_hdb) {
            engine->hm_hdb = mpool_calloc(engine->mempool, 1, sizeof(struct cli_matcher));
            if (NULL == engine->hm_hdb) {            
                xmlTextReaderClose(reader);
                xmlFreeTextReader(reader);
                return CL_EMEM;
            }
#ifdef USE_MPOOL
            engine->hm_hdb->mempool = engine->mempool;
#endif
        }
    }

    while (elems != NULL) {
        const char * sp;
        char * hash, * vp;
        int i, hashlen;

        elem = elems;
        elems = elems->next;
        hash = (char *)(elem->hash);
        while (isspace(*hash))
            hash++;
        hashlen = strlen(hash);
        if (hashlen == 0) {
            xmlFree(elem->hash);
            free(elem);
            continue;
        }
        vp = hash+hashlen-1;
        while (isspace(*vp) && vp > hash) {
            *vp-- = '\0';
            hashlen--;
        }
        virusname = calloc(1, ioclen+hashlen+2);
        if (NULL == virusname) {
            cli_dbgmsg("openioc_parse: mpool_malloc for virname memory failed.\n");
            xmlTextReaderClose(reader);
            xmlFreeTextReader(reader);
            return CL_EMEM;
        }
        sp = fname;
        vp = virusname;
        for (i=0; i<ioclen; i++, sp++, vp++) {
            switch (*sp) {
            case '\\':
            case '/':
            case '?':
            case '%':
            case '*':
            case ':':
            case '|':
            case '"':
            case '<':
            case '>':
                *vp = '_';
                break;
            default:
                if (isspace(*sp))
                    *vp = '_';
                else
                    *vp = *sp;
            }
        }
        *vp++ = '.';
        sp = hash;
        for (i=0; i<hashlen; i++, sp++) {
            if (isxdigit(*sp)) {
                *vp++ = *sp;
            }
        }

        vp = virusname;
        virusname = cli_mpool_virname(engine->mempool, virusname, options & CL_DB_OFFICIAL);
        if (!(virusname)) {
            cli_dbgmsg("openioc_parse: mpool_malloc for virname memory failed.\n");
            xmlTextReaderClose(reader);
            xmlFreeTextReader(reader);
            free(vp);
            return CL_EMEM;
        }

        free(vp);

        rc = hm_addhash_str(engine->hm_hdb, hash, 0, virusname);
        if (rc != CL_SUCCESS)
            cli_dbgmsg("openioc_parse: hm_addhash_str failed with %i hash len %i for %s.\n",
                       rc, hashlen, virusname);
        else
            hash_count++;

        xmlFree(elem->hash);
        free(elem);
    }

    if (hash_count == 0)
        cli_warnmsg("openioc_parse: No hash signatures extracted from %s.\n", fname);
    else
        cli_dbgmsg("openioc_parse: %i hash signature%s extracted from %s.\n",
                   hash_count, hash_count==1?"":"s", fname);

    xmlTextReaderClose(reader);
    xmlFreeTextReader(reader);

    return CL_SUCCESS;
}
Exemplo n.º 27
0
static void
target_info(char **msg, char *targ_name, xml_node_t *tnode)
{
	char			path[MAXPATHLEN],
				lun_buf[16],
				*prop;
	xmlTextReaderPtr	r;
	xml_node_t		*lnode,	/* list node */
				*lnp, /* list node pointer */
				*lun,
				*params;
	int			xml_fd,
				lun_num;

	if ((lnode = xml_node_next(tnode, XML_ELEMENT_ACLLIST, NULL)) !=
	    NULL) {
		lnp = NULL;
		buf_add_tag(msg, XML_ELEMENT_ACLLIST, Tag_Start);
		while ((lnp = xml_node_next(lnode, XML_ELEMENT_INIT, lnp)) !=
		    NULL)
			xml_add_tag(msg, XML_ELEMENT_INIT, lnp->x_value);
		buf_add_tag(msg, XML_ELEMENT_ACLLIST, Tag_End);
	}

	if ((lnode = xml_node_next(tnode, XML_ELEMENT_TPGTLIST, NULL)) !=
	    NULL) {
		lnp = NULL;
		buf_add_tag(msg, XML_ELEMENT_TPGTLIST, Tag_Start);
		while ((lnp = xml_node_next(lnode, XML_ELEMENT_TPGT, lnp)) !=
		    NULL)
			xml_add_tag(msg, XML_ELEMENT_TPGT, lnp->x_value);
		buf_add_tag(msg, XML_ELEMENT_TPGTLIST, Tag_End);
	}

	if ((lnode = xml_node_next(tnode, XML_ELEMENT_ALIAS, NULL)) != NULL)
		xml_add_tag(msg, XML_ELEMENT_ALIAS, lnode->x_value);

	if ((lnode = xml_node_next(tnode, XML_ELEMENT_MAXRECV, NULL)) != NULL)
		xml_add_tag(msg, XML_ELEMENT_MAXRECV, lnode->x_value);

	if ((lnode = xml_node_next(tnode, XML_ELEMENT_LUNLIST, NULL)) == NULL)
		return;

	buf_add_tag(msg, XML_ELEMENT_LUNINFO, Tag_Start);
	lun = NULL;
	while ((lun = xml_node_next(lnode, XML_ELEMENT_LUN, lun)) != NULL) {
		if ((xml_find_value_int(lun, XML_ELEMENT_LUN, &lun_num)) ==
		    False)
			continue;
		(void) snprintf(path, sizeof (path), "%s/%s/%s%d",
		    target_basedir, targ_name, PARAMBASE, lun_num);
		if ((xml_fd = open(path, O_RDONLY)) < 0)
			continue;
		if ((r = (xmlTextReaderPtr)xmlReaderForFd(xml_fd,
		    NULL, NULL, 0)) == NULL)
			continue;

		params = NULL;
		while (xmlTextReaderRead(r) == 1) {
			if (xml_process_node(r, &params) == False)
				break;
		}
		(void) close(xml_fd);
		xmlTextReaderClose(r);
		xmlFreeTextReader(r);

		buf_add_tag(msg, XML_ELEMENT_LUN, Tag_Start);
		snprintf(lun_buf, sizeof (lun_buf), "%d", lun_num);
		buf_add_tag(msg, lun_buf, Tag_String);

		if (xml_find_value_str(params, XML_ELEMENT_GUID, &prop) ==
		    True) {
			xml_add_tag(msg, XML_ELEMENT_GUID, prop);
			free(prop);
		}
		if (xml_find_value_str(params, XML_ELEMENT_VID, &prop) ==
		    True) {
			xml_add_tag(msg, XML_ELEMENT_VID, prop);
			free(prop);
		}
		if (xml_find_value_str(params, XML_ELEMENT_PID, &prop) ==
		    True) {
			xml_add_tag(msg, XML_ELEMENT_PID, prop);
			free(prop);
		}
		if (xml_find_value_str(params, XML_ELEMENT_DTYPE, &prop) ==
		    True) {
			xml_add_tag(msg, XML_ELEMENT_DTYPE, prop);
			free(prop);
		}
		if (xml_find_value_str(params, XML_ELEMENT_SIZE, &prop) ==
		    True) {
			xml_add_tag(msg, XML_ELEMENT_SIZE, prop);
			free(prop);
		}
		if (xml_find_value_str(params, XML_ELEMENT_STATUS, &prop) ==
		    True) {
			xml_add_tag(msg, XML_ELEMENT_STATUS, prop);
			free(prop);
		}
		if (xml_find_value_str(params, XML_ELEMENT_BACK, &prop) ==
		    True) {
			xml_add_tag(msg, XML_ELEMENT_BACK, prop);
			free(prop);
		}
		buf_add_tag(msg, XML_ELEMENT_LUN, Tag_End);

		xml_tree_free(params);
	}
	buf_add_tag(msg, XML_ELEMENT_LUNINFO, Tag_End);
}
Exemplo n.º 28
0
Arquivo: mgmt_scf.c Projeto: imp/slist
/*
 * Convert legacy (XML) configuration files into an equivalent SCF
 * representation.
 *
 * Read the XML from disk, translate the XML into a tree of nodes of
 * type tgt_node_t, and write the in-memory tree to SCF's persistent
 * data-store using mgmt_config_save2scf().
 *
 * Return Values:
 * CONVERT_OK:	     successfully converted
 * CONVERT_INIT_NEW: configuration files don't exist; created an SCF entry
 * CONVERT_FAIL: some conversion error occurred; no SCF entry created.
 *		 In this case, user has to manually check files and try
 *		 conversion again.
 */
convert_ret_t
mgmt_convert_conf()
{
	targ_scf_t		*h = NULL;
	xmlTextReaderPtr	r;
	convert_ret_t		ret = CONVERT_FAIL;
	int			xml_fd = -1;
	int			n;
	tgt_node_t		*node = NULL;
	tgt_node_t		*next = NULL;
	char			path[MAXPATHLEN];
	char			*target = NULL;

	h = mgmt_handle_init();
	if (h == NULL)
		return (CONVERT_FAIL);

	/*
	 * Check if the "iscsitgt" PropertyGroup has already been added
	 * to the "iscsitgt" SMF Service.  If so, then we have already
	 * converted the legacy configuration files (and there is no work
	 * to do).
	 */
	if (scf_service_get_pg(h->t_service, "iscsitgt", h->t_pg) == 0) {
		ret = CONVERT_OK;
		goto done;
	}

	if (access(config_file, R_OK) != 0) {
		/*
		 * then the Main Config file is not present; initialize
		 * SCF Properties to default values.
		 */
		if (mgmt_transaction_start(h, "iscsitgt", "basic") == True) {
			ret = CONVERT_INIT_NEW;

			node = tgt_node_alloc(XML_ELEMENT_VERS, String, "1.0");
			new_property(h, node);
			tgt_node_free(node);
			/* "daemonize" is set to true by default */
			node = tgt_node_alloc(XML_ELEMENT_DBGDAEMON, String,
			    "true");
			new_property(h, node);
			tgt_node_free(node);
			node = NULL;
			node = tgt_node_alloc(ISCSI_MODIFY_AUTHNAME, String,
			    ISCSI_AUTH_MODIFY);
			new_property(h, node);
			tgt_node_free(node);
			node = tgt_node_alloc(ISCSI_VALUE_AUTHNAME, String,
			    ISCSI_AUTH_VALUE);
			new_property(h, node);
			tgt_node_free(node);
			(void) mgmt_transaction_end(h);
		} else {
			syslog(LOG_ERR, "Creating empty entry failed");
			ret = CONVERT_FAIL;
			goto done;
		}
		if (mgmt_transaction_start(h, "passwords", "application") ==
		    True) {
			node = tgt_node_alloc(ISCSI_READ_AUTHNAME, String,
			    ISCSI_AUTH_READ);
			new_property(h, node);
			tgt_node_free(node);
			node = tgt_node_alloc(ISCSI_MODIFY_AUTHNAME, String,
			    ISCSI_AUTH_MODIFY);
			new_property(h, node);
			tgt_node_free(node);
			node = tgt_node_alloc(ISCSI_VALUE_AUTHNAME, String,
			    ISCSI_AUTH_VALUE);
			new_property(h, node);
			tgt_node_free(node);
			(void) mgmt_transaction_end(h);
		} else {
			syslog(LOG_ERR, "Creating empty entry failed");
			ret = CONVERT_FAIL;
		}
		goto done;
	}

	if ((xml_fd = open(config_file, O_RDONLY)) >= 0)
		r = (xmlTextReaderPtr)xmlReaderForFd(xml_fd, NULL, NULL, 0);

	if (r != NULL) {
		int is_target_config;

		n = xmlTextReaderRead(r);
		while (n == 1) {
			if (tgt_node_process(r, &node) == False) {
				break;
			}
			n = xmlTextReaderRead(r);
		}
		if (n < 0) {
			syslog(LOG_ERR, "Parsing main config failed");
			ret = CONVERT_FAIL;
			goto done;
		}

		main_config = node;

		/*
		 * Initialize the Base Directory (global) variable by
		 * using the value specified in the XML_ELEMENT_BASEDIR
		 * XML tag.  If a tag is not specified, use a default.
		 */
		(void) tgt_find_value_str(node, XML_ELEMENT_BASEDIR,
		    &target_basedir);

		if (target_basedir == NULL)
			target_basedir = strdup(DEFAULT_TARGET_BASEDIR);

		if (xml_fd != -1) {
			(void) close(xml_fd);
			xml_fd = -1;
		}
		(void) xmlTextReaderClose(r);
		xmlFreeTextReader(r);
		xmlCleanupParser();

		/*
		 * If a Target Config file is present, read and translate
		 * its XML representation into a tree of tgt_node_t.
		 * Merge that tree with the tree of tgt_node_t rooted at
		 * 'main_config'.  The merged tree will then be archived
		 * using an SCF representation.
		 */
		(void) snprintf(path, MAXPATHLEN, "%s/%s",
		    target_basedir, "config.xml");

		if ((xml_fd = open(path, O_RDONLY)) >= 0) {
			is_target_config = 1;
			r = (xmlTextReaderPtr)xmlReaderForFd(xml_fd,
			    NULL, NULL, 0);
		} else {
			is_target_config = 0;
			r = NULL;
		}

		if (r != NULL) {
			/* then the Target Config file is available. */

			node = NULL;

			/*
			 * Create a tree of tgt_node_t rooted at 'node' by
			 * processing each XML Tag in the file.
			 */
			n = xmlTextReaderRead(r);
			while (n == 1) {
				if (tgt_node_process(r, &node) == False) {
					break;
				}
				n = xmlTextReaderRead(r);
			}
			if (n < 0) {
				syslog(LOG_ERR, "Parsing target conf failed");
				ret = CONVERT_FAIL;
				goto done;
			}

			/*
			 * Merge the tree at 'node' into the tree rooted at
			 * 'main_config'.
			 */
			if (node != NULL) {
				next = NULL;
				while ((next = tgt_node_next(node,
				    XML_ELEMENT_TARG, next)) != NULL) {
					tgt_node_add(main_config,
					    tgt_node_dup(next));
				}
				tgt_node_free(node);
			}
		}

		/*
		 * Iterate over the in-memory tree rooted at 'main_config'
		 * and write a representation of the appropriate nodes to
		 * SCF's persistent data-store.
		 */
		if (mgmt_config_save2scf() != True) {
			syslog(LOG_ERR, "Converting config failed");
			if (xml_fd != -1) {
				(void) close(xml_fd);
				xml_fd = -1;
			}
			(void) xmlTextReaderClose(r);
			xmlFreeTextReader(r);
			xmlCleanupParser();
			ret = CONVERT_FAIL;
			goto done;
		}

		/*
		 * Move the configuration files into a well-known backup
		 * directory.  This allows a user to restore their
		 * configuration, if they choose.
		 */
		(void) snprintf(path, sizeof (path), "%s/backup",
		    target_basedir);
		if ((mkdir(path, 0755) == -1) && (errno != EEXIST)) {
			syslog(LOG_ERR, "Creating backup dir failed");
			ret = CONVERT_FAIL;
			goto done;
		}
		/* Save the Main Config file. */
		backup(config_file, NULL);

		/* Save the Target Config file, if it was present. */
		if (is_target_config != 0) {
			(void) snprintf(path, MAXPATHLEN, "%s/%s",
			    target_basedir, "config.xml");
			backup(path, NULL);
		}

		/*
		 * For each tgt_node_t node in 'main_config' whose value is
		 * an iSCSI Name as defined in the RFC (3720) standard (eg,
		 * "iqn.1986..."), read its XML-encoded attributes from a
		 * flat-file and write an equivalent representation to SCF's
		 * data-store.
		 */
		while ((next = tgt_node_next(main_config,
		    XML_ELEMENT_TARG, next)) != NULL) {
			if (tgt_find_value_str(next, XML_ELEMENT_INAME,
			    &target) == False) {
				continue;
			}
			(void) snprintf(path, MAXPATHLEN, "%s/%s",
			    target_basedir, target);
			if (mgmt_convert_param(path, next)
			    != True) {
				ret = CONVERT_FAIL;
				goto done;
			}
			free(target);
		}

		ret = CONVERT_OK;
		syslog(LOG_NOTICE, "Conversion succeeded");

		(void) xmlTextReaderClose(r);
		xmlFreeTextReader(r);
		xmlCleanupParser();
	} else {
		syslog(LOG_ERR, "Reading main config failed");
		ret = CONVERT_FAIL;
		goto done;
	}

done:
	if (xml_fd != -1)
		(void) close(xml_fd);
	mgmt_handle_fini(h);
	return (ret);
}