2014-04-22 14:22:39 -04:00
|
|
|
/*
|
|
|
|
* JSON Object API
|
2019-05-04 15:55:50 -04:00
|
|
|
*
|
2025-02-14 10:24:30 -05:00
|
|
|
* Copyright (C) 2014-2025 Cisco Systems, Inc. and/or its affiliates. All rights reserved.
|
2019-05-04 15:55:50 -04:00
|
|
|
*
|
2014-04-22 14:22:39 -04:00
|
|
|
* Authors: Kevin Lin
|
2019-05-04 15:55:50 -04:00
|
|
|
*
|
2014-04-22 14:22:39 -04:00
|
|
|
* This program is free software; you can redistribute it and/or modify it under
|
|
|
|
* the terms of the GNU General Public License version 2 as published by the
|
|
|
|
* Free Software Foundation.
|
2019-05-04 15:55:50 -04:00
|
|
|
*
|
2014-04-22 14:22:39 -04:00
|
|
|
* This program is distributed in the hope that it will be useful, but WITHOUT
|
|
|
|
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
|
|
|
* more details.
|
2019-05-04 15:55:50 -04:00
|
|
|
*
|
2014-04-22 14:22:39 -04:00
|
|
|
* You should have received a copy of the GNU General Public License along with
|
|
|
|
* this program; if not, write to the Free Software Foundation, Inc., 51
|
|
|
|
* Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if HAVE_CONFIG_H
|
|
|
|
#include "clamav-config.h"
|
|
|
|
#endif
|
|
|
|
|
2014-07-01 19:38:01 -04:00
|
|
|
#include "clamav.h"
|
2014-04-22 14:22:39 -04:00
|
|
|
#include "others.h"
|
|
|
|
#include "json_api.h"
|
|
|
|
|
2019-05-04 15:55:50 -04:00
|
|
|
cl_error_t cli_json_timeout_cycle_check(cli_ctx *ctx, int *toval)
|
2014-06-25 13:08:18 -04:00
|
|
|
{
|
2018-07-20 22:28:48 -04:00
|
|
|
if (SCAN_COLLECT_METADATA) {
|
2014-06-25 15:54:57 -04:00
|
|
|
if (*toval <= 0) {
|
|
|
|
if (cli_checktimelimit(ctx) != CL_SUCCESS) {
|
libclamav: Fix scan recursion tracking
Scan recursion is the process of identifying files embedded in other
files and then scanning them, recursively.
Internally this process is more complex than it may sound because a file
may have multiple layers of types before finding a new "file".
At present we treat the recursion count in the scanning context as an
index into both our fmap list AND our container list. These two lists
are conceptually a part of the same thing and should be unified.
But what's concerning is that the "recursion level" isn't actually
incremented or decremented at the same time that we add a layer to the
fmap or container lists but instead is more touchy-feely, increasing
when we find a new "file".
To account for this shadiness, the size of the fmap and container lists
has always been a little longer than our "max scan recursion" limit so
we don't accidentally overflow the fmap or container arrays (!).
I've implemented a single recursion-stack as an array, similar to before,
which includes a pointer to each fmap at each layer, along with the size
and type. Push and pop functions add and remove layers whenever a new
fmap is added. A boolean argument when pushing indicates if the new layer
represents a new buffer or new file (descriptor). A new buffer will reset
the "nested fmap level" (described below).
This commit also provides a solution for an issue where we detect
embedded files more than once during scan recursion.
For illustration, imagine a tarball named foo.tar.gz with this structure:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.tar.gz | GZ | 0 | 0 |
| └── foo.tar | TAR | 1 | 0 |
| ├── bar.zip | ZIP | 2 | 1 |
| │  └── hola.txt | ASCII | 3 | 0 |
| └── baz.exe | PE | 2 | 1 |
But suppose baz.exe embeds a ZIP archive and a 7Z archive, like this:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| baz.exe | PE | 0 | 0 |
| ├── sfx.zip | ZIP | 1 | 1 |
| │  └── hello.txt | ASCII | 2 | 0 |
| └── sfx.7z | 7Z | 1 | 1 |
|   └── world.txt | ASCII | 2 | 0 |
(A) If we scan for embedded files at any layer, we may detect:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.tar.gz | GZ | 0 | 0 |
| ├── foo.tar | TAR | 1 | 0 |
| │ ├── bar.zip | ZIP | 2 | 1 |
| │ │  └── hola.txt | ASCII | 3 | 0 |
| │ ├── baz.exe | PE | 2 | 1 |
| │ │ ├── sfx.zip | ZIP | 3 | 1 |
| │ │ │  └── hello.txt | ASCII | 4 | 0 |
| │ │ └── sfx.7z | 7Z | 3 | 1 |
| │ │   └── world.txt | ASCII | 4 | 0 |
| │ ├── sfx.zip | ZIP | 2 | 1 |
| │ │  └── hello.txt | ASCII | 3 | 0 |
| │ └── sfx.7z | 7Z | 2 | 1 |
| │  └── world.txt | ASCII | 3 | 0 |
| ├── sfx.zip | ZIP | 1 | 1 |
| └── sfx.7z | 7Z | 1 | 1 |
(A) is bad because it scans content more than once.
Note that for the GZ layer, it may detect the ZIP and 7Z if the
signature hits on the compressed data, which it might, though
extracting the ZIP and 7Z will likely fail.
The reason the above doesn't happen now is that we restrict embedded
type scans for a bunch of archive formats to include GZ and TAR.
(B) If we scan for embedded files at the foo.tar layer, we may detect:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.tar.gz | GZ | 0 | 0 |
| └── foo.tar | TAR | 1 | 0 |
| ├── bar.zip | ZIP | 2 | 1 |
| │  └── hola.txt | ASCII | 3 | 0 |
| ├── baz.exe | PE | 2 | 1 |
| ├── sfx.zip | ZIP | 2 | 1 |
| │  └── hello.txt | ASCII | 3 | 0 |
| └── sfx.7z | 7Z | 2 | 1 |
|   └── world.txt | ASCII | 3 | 0 |
(B) is almost right. But we can achieve it easily enough only scanning for
embedded content in the current fmap when the "nested fmap level" is 0.
The upside is that it should safely detect all embedded content, even if
it may think the sfz.zip and sfx.7z are in foo.tar instead of in baz.exe.
The biggest risk I can think of affects ZIPs. SFXZIP detection
is identical to ZIP detection, which is why we don't allow SFXZIP to be
detected if insize of a ZIP. If we only allow embedded type scanning at
fmap-layer 0 in each buffer, this will fail to detect the embedded ZIP
if the bar.exe was not compressed in foo.zip and if non-compressed files
extracted from ZIPs aren't extracted as new buffers:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.zip | ZIP | 0 | 0 |
| └── bar.exe | PE | 1 | 1 |
| └── sfx.zip | ZIP | 2 | 2 |
Provided that we ensure all files extracted from zips are scanned in
new buffers, option (B) should be safe.
(C) If we scan for embedded files at the baz.exe layer, we may detect:
| description | type | rec level | nested fmap level |
| ------------------------- | ----- | --------- | ----------------- |
| foo.tar.gz | GZ | 0 | 0 |
| └── foo.tar | TAR | 1 | 0 |
| ├── bar.zip | ZIP | 2 | 1 |
| │  └── hola.txt | ASCII | 3 | 0 |
| └── baz.exe | PE | 2 | 1 |
| ├── sfx.zip | ZIP | 3 | 1 |
| │  └── hello.txt | ASCII | 4 | 0 |
| └── sfx.7z | 7Z | 3 | 1 |
|   └── world.txt | ASCII | 4 | 0 |
(C) is right. But it's harder to achieve. For this example we can get it by
restricting 7ZSFX and ZIPSFX detection only when scanning an executable.
But that may mean losing detection of archives embedded elsewhere.
And we'd have to identify allowable container types for each possible
embedded type, which would be very difficult.
So this commit aims to solve the issue the (B)-way.
Note that in all situations, we still have to scan with file typing
enabled to determine if we need to reassign the current file type, such
as re-identifying a Bzip2 archive as a DMG that happens to be Bzip2-
compressed. Detection of DMG and a handful of other types rely on
finding data partway through or near the ned of a file before
reassigning the entire file as the new type.
Other fixes and considerations in this commit:
- The utf16 HTML parser has weak error handling, particularly with respect
to creating a nested fmap for scanning the ascii decoded file.
This commit cleans up the error handling and wraps the nested scan with
the recursion-stack push()/pop() for correct recursion tracking.
Before this commit, each container layer had a flag to indicate if the
container layer is valid.
We need something similar so that the cli_recursion_stack_get_*()
functions ignore normalized layers. Details...
Imagine an LDB signature for HTML content that specifies a ZIP
container. If the signature actually alerts on the normalized HTML and
you don't ignore normalized layers for the container check, it will
appear as though the alert is in an HTML container rather than a ZIP
container.
This commit accomplishes this with a boolean you set in the scan context
before scanning a new layer. Then when the new fmap is created, it will
use that flag to set similar flag for the layer. The context flag is
reset those that anything after this doesn't have that flag.
The flag allows the new recursion_stack_get() function to ignore
normalized layers when iterating the stack to return a layer at a
requested index, negative or positive.
Scanning normalized extracted/normalized javascript and VBA should also
use the 'layer is normalized' flag.
- This commit also fixes Heuristic.Broken.Executable alert for ELF files
to make sure that:
A) these only alert if cli_append_virus() returns CL_VIRUS (aka it
respects the FP check).
B) all broken-executable alerts for ELF only happen if the
SCAN_HEURISTIC_BROKEN option is enabled.
- This commit also cleans up the error handling in cli_magic_scan_dir().
This was needed so we could correctly apply the layer-is-normalized-flag
to all VBA macros extracted to a directory when scanning the directory.
- Also fix an issue where exceeding scan maximums wouldn't cause embedded
file detection scans to abort. Granted we don't actually want to abort
if max filesize or max recursion depth are exceeded... only if max
scansize, max files, and max scantime are exceeded.
Add 'abort_scan' flag to scan context, to protect against depending on
correct error propagation for fatal conditions. Instead, setting this
flag in the scan context should guarantee that a fatal condition deep in
scan recursion isn't lost which result in more stuff being scanned
instead of aborting. This shouldn't be necessary, but some status codes
like CL_ETIMEOUT never used to be fatal and it's easier to do this than
to verify every parser only returns CL_ETIMEOUT and other "fatal
status codes" in fatal conditions.
- Remove duplicate is_tar() prototype from filestypes.c and include
is_tar.h instead.
- Presently we create the fmap hash when creating the fmap.
This wastes a bit of CPU if the hash is never needed.
Now that we're creating fmap's for all embedded files discovered with
file type recognition scans, this is a much more frequent occurence and
really slows things down.
This commit fixes the issue by only creating fmap hashes as needed.
This should not only resolve the perfomance impact of creating fmap's
for all embedded files, but also should improve performance in general.
- Add allmatch check to the zip parser after the central-header meta
match. That way we don't multiple alerts with the same match except in
allmatch mode. Clean up error handling in the zip parser a tiny bit.
- Fixes to ensure that the scan limits such as scansize, filesize,
recursion depth, # of embedded files, and scantime are always reported
if AlertExceedsMax (--alert-exceeds-max) is enabled.
- Fixed an issue where non-fatal alerts for exceeding scan maximums may
mask signature matches later on. I changed it so these alerts use the
"possibly unwanted" alert-type and thus only alert if no other alerts
were found or if all-match or heuristic-precedence are enabled.
- Added the "Heuristics.Limits.Exceeded.*" events to the JSON metadata
when the --gen-json feature is enabled. These will show up once under
"ParseErrors" the first time a limit is exceeded. In the present
implementation, only one limits-exceeded events will be added, so as to
prevent a malicious or malformed sample from filling the JSON buffer
with millions of events and using a tonne of RAM.
2021-09-11 14:15:21 -07:00
|
|
|
cli_dbgmsg("cli_json_timeout_cycle_check: timeout!\n");
|
2014-06-25 15:54:57 -04:00
|
|
|
return CL_ETIMEOUT;
|
|
|
|
}
|
|
|
|
(*toval)++;
|
|
|
|
}
|
|
|
|
if (*toval > JSON_TIMEOUT_SKIP_CYCLES) {
|
|
|
|
(*toval) = 0;
|
2014-06-25 13:08:18 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return CL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-05-04 15:55:50 -04:00
|
|
|
cl_error_t cli_json_parse_error(json_object *root, const char *errstr)
|
2015-03-13 14:18:37 -04:00
|
|
|
{
|
|
|
|
json_object *perr;
|
|
|
|
|
|
|
|
if (!root)
|
|
|
|
return CL_SUCCESS; /* CL_ENULLARG? */
|
|
|
|
|
|
|
|
perr = cli_jsonarray(root, "ParseErrors");
|
|
|
|
if (perr == NULL) {
|
|
|
|
return CL_EMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
return cli_jsonstr(perr, NULL, errstr);
|
|
|
|
}
|
|
|
|
|
2019-05-04 15:55:50 -04:00
|
|
|
cl_error_t cli_jsonnull(json_object *obj, const char *key)
|
2014-04-22 14:22:39 -04:00
|
|
|
{
|
2014-07-03 12:03:12 -04:00
|
|
|
json_type objty;
|
2014-05-06 13:49:09 -04:00
|
|
|
json_object *fpobj = NULL;
|
2014-04-22 14:58:38 -04:00
|
|
|
if (NULL == obj) {
|
2014-06-03 14:28:29 -04:00
|
|
|
cli_dbgmsg("json: null 'obj' specified to cli_jsonnull\n");
|
2014-04-22 14:58:38 -04:00
|
|
|
return CL_ENULLARG;
|
|
|
|
}
|
2014-07-03 12:03:12 -04:00
|
|
|
objty = json_object_get_type(obj);
|
|
|
|
|
|
|
|
if (objty == json_type_object) {
|
|
|
|
if (NULL == key) {
|
|
|
|
cli_dbgmsg("json: null string specified as key to cli_jsonnull\n");
|
|
|
|
return CL_ENULLARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object_object_add(obj, key, fpobj);
|
2018-12-03 12:40:13 -05:00
|
|
|
} else if (objty == json_type_array) {
|
2014-07-03 12:03:12 -04:00
|
|
|
json_object_array_add(obj, fpobj);
|
2014-04-22 15:52:31 -04:00
|
|
|
}
|
|
|
|
|
2014-04-22 14:22:39 -04:00
|
|
|
return CL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-05-04 15:55:50 -04:00
|
|
|
cl_error_t cli_jsonstr(json_object *obj, const char *key, const char *s)
|
2014-04-22 14:22:39 -04:00
|
|
|
{
|
2014-07-03 12:03:12 -04:00
|
|
|
json_type objty;
|
2014-04-22 15:52:31 -04:00
|
|
|
json_object *fpobj;
|
2014-04-22 14:58:38 -04:00
|
|
|
if (NULL == obj) {
|
2014-06-03 14:28:29 -04:00
|
|
|
cli_dbgmsg("json: null 'obj' specified to cli_jsonstr\n");
|
2014-04-22 15:52:31 -04:00
|
|
|
return CL_ENULLARG;
|
|
|
|
}
|
2014-07-03 12:03:12 -04:00
|
|
|
objty = json_object_get_type(obj);
|
|
|
|
|
|
|
|
if (objty == json_type_object) {
|
|
|
|
if (NULL == key) {
|
|
|
|
cli_dbgmsg("json: null string specified as 'key' to cli_jsonstr\n");
|
|
|
|
return CL_ENULLARG;
|
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
} else if (objty != json_type_array) {
|
2014-07-03 12:03:12 -04:00
|
|
|
return CL_EARG;
|
2014-04-22 15:52:31 -04:00
|
|
|
}
|
2014-07-03 12:03:12 -04:00
|
|
|
|
2014-04-22 15:52:31 -04:00
|
|
|
if (NULL == s) {
|
2014-06-03 14:28:29 -04:00
|
|
|
cli_dbgmsg("json: null string specified as 's' to cli_jsonstr\n");
|
2014-04-22 14:58:38 -04:00
|
|
|
return CL_ENULLARG;
|
|
|
|
}
|
2014-04-22 15:52:31 -04:00
|
|
|
|
|
|
|
fpobj = json_object_new_string(s);
|
2014-04-22 14:22:39 -04:00
|
|
|
if (NULL == fpobj) {
|
2014-04-22 15:52:31 -04:00
|
|
|
cli_errmsg("json: no memory for json string object\n");
|
2014-04-22 14:22:39 -04:00
|
|
|
return CL_EMEM;
|
|
|
|
}
|
2014-07-03 12:03:12 -04:00
|
|
|
|
|
|
|
if (objty == json_type_object)
|
|
|
|
json_object_object_add(obj, key, fpobj);
|
|
|
|
else if (objty == json_type_array)
|
|
|
|
json_object_array_add(obj, fpobj);
|
|
|
|
|
2014-04-22 14:22:39 -04:00
|
|
|
return CL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-05-04 15:55:50 -04:00
|
|
|
cl_error_t cli_jsonstrlen(json_object *obj, const char *key, const char *s, int len)
|
2014-07-03 12:37:12 -04:00
|
|
|
{
|
|
|
|
json_type objty;
|
|
|
|
json_object *fpobj;
|
|
|
|
if (NULL == obj) {
|
|
|
|
cli_dbgmsg("json: null 'obj' specified to cli_jsonstr\n");
|
|
|
|
return CL_ENULLARG;
|
|
|
|
}
|
|
|
|
objty = json_object_get_type(obj);
|
|
|
|
|
|
|
|
if (objty == json_type_object) {
|
|
|
|
if (NULL == key) {
|
|
|
|
cli_dbgmsg("json: null string specified as 'key' to cli_jsonstr\n");
|
|
|
|
return CL_ENULLARG;
|
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
} else if (objty != json_type_array) {
|
2014-07-03 12:37:12 -04:00
|
|
|
return CL_EARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (NULL == s) {
|
|
|
|
cli_dbgmsg("json: null string specified as 's' to cli_jsonstr\n");
|
|
|
|
return CL_ENULLARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
fpobj = json_object_new_string_len(s, len);
|
|
|
|
if (NULL == fpobj) {
|
|
|
|
cli_errmsg("json: no memory for json string object\n");
|
|
|
|
return CL_EMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (objty == json_type_object)
|
|
|
|
json_object_object_add(obj, key, fpobj);
|
|
|
|
else if (objty == json_type_array)
|
|
|
|
json_object_array_add(obj, fpobj);
|
|
|
|
|
|
|
|
return CL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-05-04 15:55:50 -04:00
|
|
|
cl_error_t cli_jsonint(json_object *obj, const char *key, int32_t i)
|
2014-04-22 14:22:39 -04:00
|
|
|
{
|
2014-07-03 12:03:12 -04:00
|
|
|
json_type objty;
|
2014-04-22 15:52:31 -04:00
|
|
|
json_object *fpobj;
|
2014-04-22 14:58:38 -04:00
|
|
|
if (NULL == obj) {
|
2014-06-03 14:28:29 -04:00
|
|
|
cli_dbgmsg("json: no parent object specified to cli_jsonint\n");
|
2014-04-22 14:58:38 -04:00
|
|
|
return CL_ENULLARG;
|
|
|
|
}
|
2014-07-03 12:03:12 -04:00
|
|
|
objty = json_object_get_type(obj);
|
|
|
|
|
2018-12-03 12:40:13 -05:00
|
|
|
if (objty == json_type_object) {
|
2014-07-03 12:03:12 -04:00
|
|
|
if (NULL == key) {
|
|
|
|
cli_dbgmsg("json: null string specified as key to cli_jsonint\n");
|
|
|
|
return CL_ENULLARG;
|
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
} else if (objty != json_type_array) {
|
2014-07-03 12:03:12 -04:00
|
|
|
return CL_EARG;
|
2014-04-22 15:52:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fpobj = json_object_new_int(i);
|
2014-04-22 14:22:39 -04:00
|
|
|
if (NULL == fpobj) {
|
2014-04-22 15:52:31 -04:00
|
|
|
cli_errmsg("json: no memory for json int object\n");
|
2014-04-22 14:22:39 -04:00
|
|
|
return CL_EMEM;
|
|
|
|
}
|
2014-07-03 12:03:12 -04:00
|
|
|
|
|
|
|
if (objty == json_type_object)
|
|
|
|
json_object_object_add(obj, key, fpobj);
|
|
|
|
else if (objty == json_type_array)
|
|
|
|
json_object_array_add(obj, fpobj);
|
|
|
|
|
2014-04-22 14:22:39 -04:00
|
|
|
return CL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-05-04 15:55:50 -04:00
|
|
|
cl_error_t cli_jsonint64(json_object *obj, const char *key, int64_t i)
|
2014-04-22 14:22:39 -04:00
|
|
|
{
|
2014-07-03 12:03:12 -04:00
|
|
|
json_type objty;
|
2014-04-22 15:52:31 -04:00
|
|
|
json_object *fpobj;
|
2014-04-22 14:58:38 -04:00
|
|
|
if (NULL == obj) {
|
2014-06-03 14:28:29 -04:00
|
|
|
cli_dbgmsg("json: no parent object specified to cli_jsonint64\n");
|
2014-04-22 14:58:38 -04:00
|
|
|
return CL_ENULLARG;
|
|
|
|
}
|
2014-07-03 12:03:12 -04:00
|
|
|
objty = json_object_get_type(obj);
|
|
|
|
|
|
|
|
if (objty == json_type_object) {
|
|
|
|
if (NULL == key) {
|
|
|
|
cli_dbgmsg("json: null string specified as key to cli_jsonint64\n");
|
|
|
|
return CL_ENULLARG;
|
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
} else if (objty != json_type_array) {
|
2014-07-03 12:03:12 -04:00
|
|
|
return CL_EARG;
|
2014-04-22 15:52:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fpobj = json_object_new_int64(i);
|
2014-04-22 14:22:39 -04:00
|
|
|
if (NULL == fpobj) {
|
|
|
|
cli_errmsg("json: no memory for json int object.\n");
|
|
|
|
return CL_EMEM;
|
|
|
|
}
|
2014-07-03 12:03:12 -04:00
|
|
|
|
libclamav: scan-layer callback API functions
Add the following scan callbacks:
```c
cl_engine_set_scan_callback(engine, &pre_hash_callback, CL_SCAN_CALLBACK_PRE_HASH);
cl_engine_set_scan_callback(engine, &pre_scan_callback, CL_SCAN_CALLBACK_PRE_SCAN);
cl_engine_set_scan_callback(engine, &post_scan_callback, CL_SCAN_CALLBACK_POST_SCAN);
cl_engine_set_scan_callback(engine, &alert_callback, CL_SCAN_CALLBACK_ALERT);
cl_engine_set_scan_callback(engine, &file_type_callback, CL_SCAN_CALLBACK_FILE_TYPE);
```
Each callback may alter scan behavior using the following return codes:
* CL_BREAK
Scan aborted by callback (the rest of the scan is skipped).
This does not mark the file as clean or infected, it just skips the rest of the scan.
* CL_SUCCESS / CL_CLEAN
File scan will continue.
This is different than CL_VERIFIED because it does not affect prior or future alerts.
Return CL_VERIFIED instead if you want to remove prior alerts for this layer and skip
the rest of the scan for this layer.
* CL_VIRUS
This means you don't trust the file. A new alert will be added.
For CL_SCAN_CALLBACK_ALERT: Means you agree with the alert (no extra alert needed).
* CL_VERIFIED
Layer explicitly trusted by the callback and previous alerts removed FOR THIS layer.
You might want to do this if you trust the hash or verified a digital signature.
The rest of the scan will be skipped FOR THIS layer.
For contained files, this does NOT mean that the parent or adjacent layers are trusted.
Each callback is given a pointer to the current scan layer from which
they can get previous layers, can get the the layer's fmap, and then
various attributes of the layer and of the fmap such as:
- layer recursion level
- layer object id
- layer file type
- layer attributes (was decerypted, normalized, embedded, or re-typed)
- layer last alert
- fmap name
- fmap hash (md5, sha1, or sha2-256)
- fmap data (pointer and size)
- fmap file descriptor, if any (fd, offset, size)
- fmap filepath, if any (filepath, offset, size)
To make this possible, this commits introduced a handful of new APIs to
query scan-layer details and fmap details:
- `cl_error_t cl_fmap_set_name(cl_fmap_t *map, const char *name);`
- `cl_error_t cl_fmap_get_name(cl_fmap_t *map, const char **name_out);`
- `cl_error_t cl_fmap_set_path(cl_fmap_t *map, const char *path);`
- `cl_error_t cl_fmap_get_path(cl_fmap_t *map, const char **path_out, size_t *offset_out, size_t *len_out);`
- `cl_error_t cl_fmap_get_fd(const cl_fmap_t *map, int *fd_out, size_t *offset_out, size_t *len_out);`
- `cl_error_t cl_fmap_get_size(const cl_fmap_t *map, size_t *size_out);`
- `cl_error_t cl_fmap_set_hash(const cl_fmap_t *map, const char *hash_alg, char hash);`
- `cl_error_t cl_fmap_have_hash(const cl_fmap_t *map, const char *hash_alg, bool *have_hash_out);`
- `cl_error_t cl_fmap_will_need_hash_later(const cl_fmap_t *map, const char *hash_alg);`
- `cl_error_t cl_fmap_get_hash(const cl_fmap_t *map, const char *hash_alg, const char **hash_out);`
- `cl_error_t cl_fmap_get_data(const cl_fmap_t *map, size_t offset, size_t len, const uint8_t **data_out, size_t *data_len_out);`
- `cl_error_t cl_scan_layer_get_fmap(cl_scan_layer_t *layer, cl_fmap_t **fmap_out);`
- `cl_error_t cl_scan_layer_get_parent_layer(cl_scan_layer_t *layer, cl_scan_layer_t **parent_layer_out);`
- `cl_error_t cl_scan_layer_get_type(cl_scan_layer_t *layer, const char **type_out);`
- `cl_error_t cl_scan_layer_get_recursion_level(cl_scan_layer_t *layer, uint32_t *recursion_level_out);`
- `cl_error_t cl_scan_layer_get_object_id(cl_scan_layer_t *layer, uint64_t *object_id_out);`
- `cl_error_t cl_scan_layer_get_last_alert(cl_scan_layer_t *layer, const char **alert_name_out);`
- `cl_error_t cl_scan_layer_get_attributes(cl_scan_layer_t *layer, uint32_t *attributes_out);`
This commit deprecates but does not remove the existing scan callbacks:
- `void cl_engine_set_clcb_pre_cache(struct cl_engine *engine, clcb_pre_cache callback);`
- `void cl_engine_set_clcb_file_inspection(struct cl_engine *engine, clcb_file_inspection callback);`
- `void cl_engine_set_clcb_pre_scan(struct cl_engine *engine, clcb_pre_scan callback);`
- `void cl_engine_set_clcb_post_scan(struct cl_engine *engine, clcb_post_scan callback);`
- `void cl_engine_set_clcb_virus_found(struct cl_engine *engine, clcb_virus_found callback);`
- `void cl_engine_set_clcb_hash(struct cl_engine *engine, clcb_hash callback);`
This commit also adds an interactive test program to demonstrate the callbacks.
See: `examples/ex_scan_callbacks.c`
CLAM-255
CLAM-2485
CLAM-2626
2025-06-22 14:37:03 -04:00
|
|
|
if (objty == json_type_object)
|
|
|
|
json_object_object_add(obj, key, fpobj);
|
|
|
|
else if (objty == json_type_array)
|
|
|
|
json_object_array_add(obj, fpobj);
|
|
|
|
|
|
|
|
return CL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
cl_error_t cli_jsonuint64(json_object *obj, const char *key, uint64_t i)
|
|
|
|
{
|
|
|
|
json_type objty;
|
|
|
|
json_object *fpobj;
|
|
|
|
if (NULL == obj) {
|
|
|
|
cli_dbgmsg("json: no parent object specified to cli_jsonuint64\n");
|
|
|
|
return CL_ENULLARG;
|
|
|
|
}
|
|
|
|
objty = json_object_get_type(obj);
|
|
|
|
|
|
|
|
if (objty == json_type_object) {
|
|
|
|
if (NULL == key) {
|
|
|
|
cli_dbgmsg("json: null string specified as key to cli_jsonuint64\n");
|
|
|
|
return CL_ENULLARG;
|
|
|
|
}
|
|
|
|
} else if (objty != json_type_array) {
|
|
|
|
return CL_EARG;
|
|
|
|
}
|
2025-09-26 18:26:00 -04:00
|
|
|
#if JSON_C_MINOR_VERSION >= 14
|
libclamav: scan-layer callback API functions
Add the following scan callbacks:
```c
cl_engine_set_scan_callback(engine, &pre_hash_callback, CL_SCAN_CALLBACK_PRE_HASH);
cl_engine_set_scan_callback(engine, &pre_scan_callback, CL_SCAN_CALLBACK_PRE_SCAN);
cl_engine_set_scan_callback(engine, &post_scan_callback, CL_SCAN_CALLBACK_POST_SCAN);
cl_engine_set_scan_callback(engine, &alert_callback, CL_SCAN_CALLBACK_ALERT);
cl_engine_set_scan_callback(engine, &file_type_callback, CL_SCAN_CALLBACK_FILE_TYPE);
```
Each callback may alter scan behavior using the following return codes:
* CL_BREAK
Scan aborted by callback (the rest of the scan is skipped).
This does not mark the file as clean or infected, it just skips the rest of the scan.
* CL_SUCCESS / CL_CLEAN
File scan will continue.
This is different than CL_VERIFIED because it does not affect prior or future alerts.
Return CL_VERIFIED instead if you want to remove prior alerts for this layer and skip
the rest of the scan for this layer.
* CL_VIRUS
This means you don't trust the file. A new alert will be added.
For CL_SCAN_CALLBACK_ALERT: Means you agree with the alert (no extra alert needed).
* CL_VERIFIED
Layer explicitly trusted by the callback and previous alerts removed FOR THIS layer.
You might want to do this if you trust the hash or verified a digital signature.
The rest of the scan will be skipped FOR THIS layer.
For contained files, this does NOT mean that the parent or adjacent layers are trusted.
Each callback is given a pointer to the current scan layer from which
they can get previous layers, can get the the layer's fmap, and then
various attributes of the layer and of the fmap such as:
- layer recursion level
- layer object id
- layer file type
- layer attributes (was decerypted, normalized, embedded, or re-typed)
- layer last alert
- fmap name
- fmap hash (md5, sha1, or sha2-256)
- fmap data (pointer and size)
- fmap file descriptor, if any (fd, offset, size)
- fmap filepath, if any (filepath, offset, size)
To make this possible, this commits introduced a handful of new APIs to
query scan-layer details and fmap details:
- `cl_error_t cl_fmap_set_name(cl_fmap_t *map, const char *name);`
- `cl_error_t cl_fmap_get_name(cl_fmap_t *map, const char **name_out);`
- `cl_error_t cl_fmap_set_path(cl_fmap_t *map, const char *path);`
- `cl_error_t cl_fmap_get_path(cl_fmap_t *map, const char **path_out, size_t *offset_out, size_t *len_out);`
- `cl_error_t cl_fmap_get_fd(const cl_fmap_t *map, int *fd_out, size_t *offset_out, size_t *len_out);`
- `cl_error_t cl_fmap_get_size(const cl_fmap_t *map, size_t *size_out);`
- `cl_error_t cl_fmap_set_hash(const cl_fmap_t *map, const char *hash_alg, char hash);`
- `cl_error_t cl_fmap_have_hash(const cl_fmap_t *map, const char *hash_alg, bool *have_hash_out);`
- `cl_error_t cl_fmap_will_need_hash_later(const cl_fmap_t *map, const char *hash_alg);`
- `cl_error_t cl_fmap_get_hash(const cl_fmap_t *map, const char *hash_alg, const char **hash_out);`
- `cl_error_t cl_fmap_get_data(const cl_fmap_t *map, size_t offset, size_t len, const uint8_t **data_out, size_t *data_len_out);`
- `cl_error_t cl_scan_layer_get_fmap(cl_scan_layer_t *layer, cl_fmap_t **fmap_out);`
- `cl_error_t cl_scan_layer_get_parent_layer(cl_scan_layer_t *layer, cl_scan_layer_t **parent_layer_out);`
- `cl_error_t cl_scan_layer_get_type(cl_scan_layer_t *layer, const char **type_out);`
- `cl_error_t cl_scan_layer_get_recursion_level(cl_scan_layer_t *layer, uint32_t *recursion_level_out);`
- `cl_error_t cl_scan_layer_get_object_id(cl_scan_layer_t *layer, uint64_t *object_id_out);`
- `cl_error_t cl_scan_layer_get_last_alert(cl_scan_layer_t *layer, const char **alert_name_out);`
- `cl_error_t cl_scan_layer_get_attributes(cl_scan_layer_t *layer, uint32_t *attributes_out);`
This commit deprecates but does not remove the existing scan callbacks:
- `void cl_engine_set_clcb_pre_cache(struct cl_engine *engine, clcb_pre_cache callback);`
- `void cl_engine_set_clcb_file_inspection(struct cl_engine *engine, clcb_file_inspection callback);`
- `void cl_engine_set_clcb_pre_scan(struct cl_engine *engine, clcb_pre_scan callback);`
- `void cl_engine_set_clcb_post_scan(struct cl_engine *engine, clcb_post_scan callback);`
- `void cl_engine_set_clcb_virus_found(struct cl_engine *engine, clcb_virus_found callback);`
- `void cl_engine_set_clcb_hash(struct cl_engine *engine, clcb_hash callback);`
This commit also adds an interactive test program to demonstrate the callbacks.
See: `examples/ex_scan_callbacks.c`
CLAM-255
CLAM-2485
CLAM-2626
2025-06-22 14:37:03 -04:00
|
|
|
fpobj = json_object_new_uint64(i);
|
2025-09-26 18:26:00 -04:00
|
|
|
#else
|
|
|
|
if (i > INT64_MAX) {
|
|
|
|
cli_dbgmsg("json: uint64 value too large for json int64 object\n");
|
|
|
|
return CL_EARG;
|
|
|
|
}
|
|
|
|
fpobj = json_object_new_int64(i);
|
|
|
|
#endif
|
libclamav: scan-layer callback API functions
Add the following scan callbacks:
```c
cl_engine_set_scan_callback(engine, &pre_hash_callback, CL_SCAN_CALLBACK_PRE_HASH);
cl_engine_set_scan_callback(engine, &pre_scan_callback, CL_SCAN_CALLBACK_PRE_SCAN);
cl_engine_set_scan_callback(engine, &post_scan_callback, CL_SCAN_CALLBACK_POST_SCAN);
cl_engine_set_scan_callback(engine, &alert_callback, CL_SCAN_CALLBACK_ALERT);
cl_engine_set_scan_callback(engine, &file_type_callback, CL_SCAN_CALLBACK_FILE_TYPE);
```
Each callback may alter scan behavior using the following return codes:
* CL_BREAK
Scan aborted by callback (the rest of the scan is skipped).
This does not mark the file as clean or infected, it just skips the rest of the scan.
* CL_SUCCESS / CL_CLEAN
File scan will continue.
This is different than CL_VERIFIED because it does not affect prior or future alerts.
Return CL_VERIFIED instead if you want to remove prior alerts for this layer and skip
the rest of the scan for this layer.
* CL_VIRUS
This means you don't trust the file. A new alert will be added.
For CL_SCAN_CALLBACK_ALERT: Means you agree with the alert (no extra alert needed).
* CL_VERIFIED
Layer explicitly trusted by the callback and previous alerts removed FOR THIS layer.
You might want to do this if you trust the hash or verified a digital signature.
The rest of the scan will be skipped FOR THIS layer.
For contained files, this does NOT mean that the parent or adjacent layers are trusted.
Each callback is given a pointer to the current scan layer from which
they can get previous layers, can get the the layer's fmap, and then
various attributes of the layer and of the fmap such as:
- layer recursion level
- layer object id
- layer file type
- layer attributes (was decerypted, normalized, embedded, or re-typed)
- layer last alert
- fmap name
- fmap hash (md5, sha1, or sha2-256)
- fmap data (pointer and size)
- fmap file descriptor, if any (fd, offset, size)
- fmap filepath, if any (filepath, offset, size)
To make this possible, this commits introduced a handful of new APIs to
query scan-layer details and fmap details:
- `cl_error_t cl_fmap_set_name(cl_fmap_t *map, const char *name);`
- `cl_error_t cl_fmap_get_name(cl_fmap_t *map, const char **name_out);`
- `cl_error_t cl_fmap_set_path(cl_fmap_t *map, const char *path);`
- `cl_error_t cl_fmap_get_path(cl_fmap_t *map, const char **path_out, size_t *offset_out, size_t *len_out);`
- `cl_error_t cl_fmap_get_fd(const cl_fmap_t *map, int *fd_out, size_t *offset_out, size_t *len_out);`
- `cl_error_t cl_fmap_get_size(const cl_fmap_t *map, size_t *size_out);`
- `cl_error_t cl_fmap_set_hash(const cl_fmap_t *map, const char *hash_alg, char hash);`
- `cl_error_t cl_fmap_have_hash(const cl_fmap_t *map, const char *hash_alg, bool *have_hash_out);`
- `cl_error_t cl_fmap_will_need_hash_later(const cl_fmap_t *map, const char *hash_alg);`
- `cl_error_t cl_fmap_get_hash(const cl_fmap_t *map, const char *hash_alg, const char **hash_out);`
- `cl_error_t cl_fmap_get_data(const cl_fmap_t *map, size_t offset, size_t len, const uint8_t **data_out, size_t *data_len_out);`
- `cl_error_t cl_scan_layer_get_fmap(cl_scan_layer_t *layer, cl_fmap_t **fmap_out);`
- `cl_error_t cl_scan_layer_get_parent_layer(cl_scan_layer_t *layer, cl_scan_layer_t **parent_layer_out);`
- `cl_error_t cl_scan_layer_get_type(cl_scan_layer_t *layer, const char **type_out);`
- `cl_error_t cl_scan_layer_get_recursion_level(cl_scan_layer_t *layer, uint32_t *recursion_level_out);`
- `cl_error_t cl_scan_layer_get_object_id(cl_scan_layer_t *layer, uint64_t *object_id_out);`
- `cl_error_t cl_scan_layer_get_last_alert(cl_scan_layer_t *layer, const char **alert_name_out);`
- `cl_error_t cl_scan_layer_get_attributes(cl_scan_layer_t *layer, uint32_t *attributes_out);`
This commit deprecates but does not remove the existing scan callbacks:
- `void cl_engine_set_clcb_pre_cache(struct cl_engine *engine, clcb_pre_cache callback);`
- `void cl_engine_set_clcb_file_inspection(struct cl_engine *engine, clcb_file_inspection callback);`
- `void cl_engine_set_clcb_pre_scan(struct cl_engine *engine, clcb_pre_scan callback);`
- `void cl_engine_set_clcb_post_scan(struct cl_engine *engine, clcb_post_scan callback);`
- `void cl_engine_set_clcb_virus_found(struct cl_engine *engine, clcb_virus_found callback);`
- `void cl_engine_set_clcb_hash(struct cl_engine *engine, clcb_hash callback);`
This commit also adds an interactive test program to demonstrate the callbacks.
See: `examples/ex_scan_callbacks.c`
CLAM-255
CLAM-2485
CLAM-2626
2025-06-22 14:37:03 -04:00
|
|
|
if (NULL == fpobj) {
|
|
|
|
cli_errmsg("json: no memory for json int object.\n");
|
|
|
|
return CL_EMEM;
|
|
|
|
}
|
|
|
|
|
2014-07-03 12:03:12 -04:00
|
|
|
if (objty == json_type_object)
|
|
|
|
json_object_object_add(obj, key, fpobj);
|
|
|
|
else if (objty == json_type_array)
|
|
|
|
json_object_array_add(obj, fpobj);
|
|
|
|
|
2014-04-22 14:22:39 -04:00
|
|
|
return CL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-05-04 15:55:50 -04:00
|
|
|
cl_error_t cli_jsonbool(json_object *obj, const char *key, int i)
|
2014-04-22 14:22:39 -04:00
|
|
|
{
|
2014-07-03 12:03:12 -04:00
|
|
|
json_type objty;
|
2014-04-22 15:52:31 -04:00
|
|
|
json_object *fpobj;
|
2014-04-22 14:58:38 -04:00
|
|
|
if (NULL == obj) {
|
2014-06-03 14:28:29 -04:00
|
|
|
cli_dbgmsg("json: no parent object specified to cli_jsonbool\n");
|
2014-04-22 14:58:38 -04:00
|
|
|
return CL_ENULLARG;
|
|
|
|
}
|
2014-07-03 12:03:12 -04:00
|
|
|
objty = json_object_get_type(obj);
|
|
|
|
|
|
|
|
if (objty == json_type_object) {
|
|
|
|
if (NULL == key) {
|
|
|
|
cli_dbgmsg("json: null string specified as key to cli_jsonbool\n");
|
|
|
|
return CL_ENULLARG;
|
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
} else if (objty != json_type_array) {
|
2014-07-03 12:03:12 -04:00
|
|
|
return CL_EARG;
|
2014-04-22 15:52:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fpobj = json_object_new_boolean(i);
|
2014-04-22 14:22:39 -04:00
|
|
|
if (NULL == fpobj) {
|
|
|
|
cli_errmsg("json: no memory for json boolean object.\n");
|
|
|
|
return CL_EMEM;
|
|
|
|
}
|
2014-07-03 12:03:12 -04:00
|
|
|
|
|
|
|
if (objty == json_type_object)
|
|
|
|
json_object_object_add(obj, key, fpobj);
|
|
|
|
else if (objty == json_type_array)
|
|
|
|
json_object_array_add(obj, fpobj);
|
|
|
|
|
2014-04-22 14:22:39 -04:00
|
|
|
return CL_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2019-05-04 15:55:50 -04:00
|
|
|
cl_error_t cli_jsondouble(json_object *obj, const char *key, double d)
|
2014-04-22 14:22:39 -04:00
|
|
|
{
|
2014-07-03 12:03:12 -04:00
|
|
|
json_type objty;
|
2014-04-22 15:52:31 -04:00
|
|
|
json_object *fpobj;
|
2014-04-22 14:58:38 -04:00
|
|
|
if (NULL == obj) {
|
2014-06-03 14:28:29 -04:00
|
|
|
cli_dbgmsg("json: no parent object specified to cli_jsondouble\n");
|
2014-04-22 14:58:38 -04:00
|
|
|
return CL_ENULLARG;
|
|
|
|
}
|
2014-07-03 12:03:12 -04:00
|
|
|
objty = json_object_get_type(obj);
|
|
|
|
|
|
|
|
if (objty == json_type_object) {
|
|
|
|
if (NULL == key) {
|
|
|
|
cli_dbgmsg("json: null string specified as key to cli_jsondouble\n");
|
|
|
|
return CL_ENULLARG;
|
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
} else if (objty != json_type_array) {
|
2014-07-03 12:03:12 -04:00
|
|
|
return CL_EARG;
|
2014-04-22 15:52:31 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
fpobj = json_object_new_double(d);
|
2014-04-22 14:22:39 -04:00
|
|
|
if (NULL == fpobj) {
|
2014-04-22 14:58:38 -04:00
|
|
|
cli_errmsg("json: no memory for json double object.\n");
|
2014-04-22 14:22:39 -04:00
|
|
|
return CL_EMEM;
|
|
|
|
}
|
2014-07-03 12:03:12 -04:00
|
|
|
|
|
|
|
if (objty == json_type_object)
|
|
|
|
json_object_object_add(obj, key, fpobj);
|
|
|
|
else if (objty == json_type_array)
|
|
|
|
json_object_array_add(obj, fpobj);
|
|
|
|
|
2014-04-22 14:22:39 -04:00
|
|
|
return CL_SUCCESS;
|
|
|
|
}
|
2014-04-24 14:22:00 -04:00
|
|
|
|
2014-06-13 20:39:52 -04:00
|
|
|
json_object *cli_jsonarray(json_object *obj, const char *key)
|
|
|
|
{
|
2015-12-17 16:13:20 -05:00
|
|
|
json_type objty;
|
2014-06-13 20:39:52 -04:00
|
|
|
json_object *newobj;
|
|
|
|
|
2014-06-25 15:54:57 -04:00
|
|
|
/* First check to see if this key exists */
|
2014-06-25 16:08:23 -04:00
|
|
|
if (obj && key && json_object_object_get_ex(obj, key, &newobj)) {
|
2014-06-25 15:54:57 -04:00
|
|
|
return json_object_is_type(newobj, json_type_array) ? newobj : NULL;
|
2014-06-13 20:39:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
newobj = json_object_new_array();
|
|
|
|
if (!(newobj))
|
|
|
|
return NULL;
|
|
|
|
|
2015-12-17 16:13:20 -05:00
|
|
|
if (obj) {
|
|
|
|
objty = json_object_get_type(obj);
|
|
|
|
|
|
|
|
if (key && objty == json_type_object) {
|
|
|
|
json_object_object_add(obj, key, newobj);
|
|
|
|
if (!json_object_object_get_ex(obj, key, &newobj))
|
|
|
|
return NULL;
|
|
|
|
} else if (objty == json_type_array) {
|
|
|
|
json_object_array_add(obj, newobj);
|
|
|
|
}
|
2014-06-13 20:39:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return newobj;
|
|
|
|
}
|
|
|
|
|
2019-05-04 15:55:50 -04:00
|
|
|
cl_error_t cli_jsonint_array(json_object *obj, int32_t val)
|
2014-06-13 20:39:52 -04:00
|
|
|
{
|
2014-07-03 12:03:12 -04:00
|
|
|
return cli_jsonint(obj, NULL, val);
|
2014-06-13 20:39:52 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
json_object *cli_jsonobj(json_object *obj, const char *key)
|
|
|
|
{
|
2015-12-17 16:13:20 -05:00
|
|
|
json_type objty;
|
2014-06-13 20:39:52 -04:00
|
|
|
json_object *newobj;
|
|
|
|
|
2014-06-25 15:54:57 -04:00
|
|
|
if (obj && key && json_object_object_get_ex(obj, key, &newobj))
|
|
|
|
return json_object_is_type(newobj, json_type_object) ? newobj : NULL;
|
2014-06-13 20:39:52 -04:00
|
|
|
|
|
|
|
newobj = json_object_new_object();
|
|
|
|
if (!(newobj))
|
|
|
|
return NULL;
|
|
|
|
|
2015-12-17 16:13:20 -05:00
|
|
|
if (obj) {
|
|
|
|
objty = json_object_get_type(obj);
|
|
|
|
|
|
|
|
if (key && objty == json_type_object) {
|
|
|
|
json_object_object_add(obj, key, newobj);
|
|
|
|
if (!json_object_object_get_ex(obj, key, &newobj))
|
|
|
|
return NULL;
|
|
|
|
} else if (objty == json_type_array) {
|
|
|
|
json_object_array_add(obj, newobj);
|
|
|
|
}
|
2014-06-25 13:11:00 -04:00
|
|
|
}
|
2014-06-13 20:39:52 -04:00
|
|
|
|
|
|
|
return newobj;
|
|
|
|
}
|
|
|
|
|
2016-05-10 18:43:42 -04:00
|
|
|
/* deleting an object DOES decrement reference count */
|
2019-05-04 15:55:50 -04:00
|
|
|
cl_error_t cli_json_delowner(json_object *owner, const char *key, int idx)
|
2016-05-10 18:43:42 -04:00
|
|
|
{
|
|
|
|
json_type objty;
|
|
|
|
json_object *obj;
|
|
|
|
if (NULL == owner) {
|
|
|
|
cli_dbgmsg("json: no owner object specified to cli_json_delowner\n");
|
|
|
|
return CL_ENULLARG;
|
|
|
|
}
|
|
|
|
objty = json_object_get_type(owner);
|
|
|
|
|
|
|
|
if (objty == json_type_object) {
|
|
|
|
if (NULL == key) {
|
|
|
|
cli_dbgmsg("json: null string specified as key to cli_delowner\n");
|
|
|
|
return CL_ENULLARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!json_object_object_get_ex(owner, key, &obj)) {
|
|
|
|
cli_dbgmsg("json: owner array does not have content with key %s\n", key);
|
|
|
|
return CL_EARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
json_object_object_del(owner, key);
|
2018-12-03 12:40:13 -05:00
|
|
|
} else if (objty == json_type_array) {
|
2016-05-10 18:43:42 -04:00
|
|
|
json_object *empty;
|
|
|
|
|
|
|
|
if (NULL == json_object_array_get_idx(owner, idx)) {
|
|
|
|
cli_dbgmsg("json: owner array does not have content at idx %d\n", idx);
|
|
|
|
return CL_EARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* allocate the empty object to replace target object */
|
|
|
|
empty = cli_jsonobj(NULL, NULL);
|
|
|
|
if (NULL == empty)
|
|
|
|
return CL_EMEM;
|
|
|
|
|
|
|
|
if (0 != json_object_array_put_idx(owner, idx, empty)) {
|
|
|
|
/* this shouldn't be possible */
|
|
|
|
cli_dbgmsg("json: cannot delete idx %d of owner array\n", idx);
|
|
|
|
return CL_BREAK;
|
|
|
|
}
|
2018-12-03 12:40:13 -05:00
|
|
|
} else {
|
2016-05-10 18:43:42 -04:00
|
|
|
cli_dbgmsg("json: no owner object cannot hold ownership\n");
|
|
|
|
return CL_EARG;
|
|
|
|
}
|
|
|
|
|
|
|
|
return CL_SUCCESS;
|
|
|
|
}
|