Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions cf-serverd/cf-serverd-enterprise-stubs.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,11 @@ ENTERPRISE_VOID_FUNC_0ARG_DEFINE_STUB(void, CollectCallMarkProcessed)
{
}

ENTERPRISE_VOID_FUNC_1ARG_DEFINE_STUB(void, NotifyNewHostSeen,
ARG_UNUSED const char *, hostkey)
{
}

ENTERPRISE_VOID_FUNC_1ARG_DEFINE_STUB(void, FprintAvahiCfengineTag, FILE *, fp)
{
fprintf(fp,"<name replace-wildcards=\"yes\" >CFEngine Community %s Policy Server on %s </name>\n", Version(), "%h");
Expand Down
2 changes: 2 additions & 0 deletions cf-serverd/cf-serverd-enterprise-stubs.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ ENTERPRISE_VOID_FUNC_0ARG_DECLARE(void, CleanReportBookFilterSet);

ENTERPRISE_VOID_FUNC_1ARG_DECLARE(void, FprintAvahiCfengineTag, FILE *, fp);

ENTERPRISE_VOID_FUNC_1ARG_DECLARE(void, NotifyNewHostSeen, const char *, hostkey);

ENTERPRISE_VOID_FUNC_1ARG_DECLARE(void, CollectCallStart, ARG_UNUSED int, interval);
ENTERPRISE_VOID_FUNC_0ARG_DECLARE(void, CollectCallStop);
ENTERPRISE_FUNC_0ARG_DECLARE(bool, CollectCallHasPending);
Expand Down
13 changes: 11 additions & 2 deletions cf-serverd/server_tls.c
Original file line number Diff line number Diff line change
Expand Up @@ -608,8 +608,17 @@
conn->user_data_set = true;
conn->rsa_auth = true;

LastSaw1(conn->ipaddr, KeyPrintableHash(ConnectionInfoKey(conn->conn_info)),
LAST_SEEN_ROLE_ACCEPT);
bool is_new_host = LastSaw1(
conn->ipaddr,

Check notice

Code scanning / CodeQL

Pointer argument is dereferenced without checking for NULL Note

Parameter conn in ServerTLSSessionEstablish() is dereferenced without an explicit null-check
KeyPrintableHash(ConnectionInfoKey(conn->conn_info)),

Check notice

Code scanning / CodeQL

Pointer argument is dereferenced without checking for NULL Note

Parameter conn in ServerTLSSessionEstablish() is dereferenced without an explicit null-check
LAST_SEEN_ROLE_ACCEPT);

if (is_new_host)
{
NotifyNewHostSeen(
KeyPrintableHash(
ConnectionInfoKey(conn->conn_info)));

Check notice

Code scanning / CodeQL

Pointer argument is dereferenced without checking for NULL Note

Parameter conn in ServerTLSSessionEstablish() is dereferenced without an explicit null-check
}

ServerSendWelcome(conn);
return true;
Expand Down
20 changes: 11 additions & 9 deletions libpromises/lastseen.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
#include <lmdb.h>
#endif

void UpdateLastSawHost(const char *hostkey, const char *address,
bool UpdateLastSawHost(const char *hostkey, const char *address,
bool incoming, time_t timestamp);

/*
Expand Down Expand Up @@ -80,40 +80,40 @@ void UpdateLastSawHost(const char *hostkey, const char *address,
* @brief Same as LastSaw() but the digest parameter is the hash as a
* "SHA=..." string, to avoid converting twice.
*/
void LastSaw1(const char *ipaddress, const char *hashstr,
bool LastSaw1(const char *ipaddress, const char *hashstr,
LastSeenRole role)
{
const char *mapip = MapAddress(ipaddress);
UpdateLastSawHost(hashstr, mapip, role == LAST_SEEN_ROLE_ACCEPT, time(NULL));
return UpdateLastSawHost(hashstr, mapip, role == LAST_SEEN_ROLE_ACCEPT, time(NULL));
}

void LastSaw(const char *ipaddress, const unsigned char *digest, LastSeenRole role)
bool LastSaw(const char *ipaddress, const unsigned char *digest, LastSeenRole role)
{
char databuf[CF_HOSTKEY_STRING_SIZE];

if (strlen(ipaddress) == 0)
{
Log(LOG_LEVEL_INFO, "LastSeen registry for empty IP with role %d", role);
return;
return false;
}

HashPrintSafe(databuf, sizeof(databuf), digest, CF_DEFAULT_DIGEST, true);

const char *mapip = MapAddress(ipaddress);

UpdateLastSawHost(databuf, mapip, role == LAST_SEEN_ROLE_ACCEPT, time(NULL));
return UpdateLastSawHost(databuf, mapip, role == LAST_SEEN_ROLE_ACCEPT, time(NULL));
}

/*****************************************************************************/

void UpdateLastSawHost(const char *hostkey, const char *address,
bool UpdateLastSawHost(const char *hostkey, const char *address,
bool incoming, time_t timestamp)
{
DBHandle *db = NULL;
if (!OpenDB(&db, dbid_lastseen))
{
Log(LOG_LEVEL_ERR, "Unable to open last seen db");
return;
return false;
}

/* Update quality-of-connection entry */
Expand All @@ -127,7 +127,8 @@ void UpdateLastSawHost(const char *hostkey, const char *address,
};

KeyHostSeen q;
if (ReadDB(db, quality_key, &q, sizeof(q)))
bool host_existed = ReadDB(db, quality_key, &q, sizeof(q));
if (host_existed)
{
newq.Q = QAverage(q.Q, newq.lastseen - q.lastseen, 0.4);
}
Expand All @@ -153,6 +154,7 @@ void UpdateLastSawHost(const char *hostkey, const char *address,
WriteDB(db, address_key, hostkey, strlen(hostkey) + 1);

CloseDB(db);
return !host_existed;
}
/*****************************************************************************/

Expand Down
9 changes: 7 additions & 2 deletions libpromises/lastseen.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,13 @@ typedef enum
bool Address2Hostkey(char *dst, size_t dst_size, const char *address);
char *HostkeyToAddress(const char *hostkey);

void LastSaw1(const char *ipaddress, const char *hashstr, LastSeenRole role);
void LastSaw(const char *ipaddress, const unsigned char *digest, LastSeenRole role);
/**
* @brief Record a host connection in the lastseen database.
* @return true if this is the first time the host has been seen (new host),
* false if the host was already known.
*/
bool LastSaw1(const char *ipaddress, const char *hashstr, LastSeenRole role);
bool LastSaw(const char *ipaddress, const unsigned char *digest, LastSeenRole role);

bool DeleteIpFromLastSeen(const char *ip, char *digest, size_t digest_size);
bool DeleteDigestFromLastSeen(const char *key, char *ip, size_t ip_size, bool a_entry_required);
Expand Down
2 changes: 1 addition & 1 deletion tests/load/lastseen_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ static void tests_setup(void)
mkdir(GetStateDir(), (S_IRWXU | S_IRWXG | S_IRWXO));
}

void UpdateLastSawHost(const char *hostkey, const char *address,
bool UpdateLastSawHost(const char *hostkey, const char *address,
bool incoming, time_t timestamp);

int main()
Expand Down
2 changes: 1 addition & 1 deletion tests/load/lastseen_threaded_load.c
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pthread_mutex_t end_mtx = PTHREAD_ERRORCHECK_MUTEX_INITIALIZER_NP;
pthread_cond_t end_cond = PTHREAD_COND_INITIALIZER;


void UpdateLastSawHost(const char *hostkey, const char *address,
bool UpdateLastSawHost(const char *hostkey, const char *address,
bool incoming, time_t timestamp);


Expand Down
2 changes: 1 addition & 1 deletion tests/unit/lastseen_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

char CFWORKDIR[CF_BUFSIZE];

void UpdateLastSawHost(const char *hostkey, const char *address,
bool UpdateLastSawHost(const char *hostkey, const char *address,
bool incoming, time_t timestamp);

/* For abbreviation of tests. */
Expand Down
Loading