X-Git-Url: http://ftp.carnet.hr/carnet-debian/scm?a=blobdiff_plain;f=src%2Fremoted%2Fsendmsg.c;h=b8c645a6c10dc3024ebdd1ac96effa38e1a36d9f;hb=3f728675941dc69d4e544d3a880a56240a6e394a;hp=12556dbab72888889fdd7555f130dd15eaf67a8b;hpb=301048b51990573e58a30dc4a5bb4ec285cad554;p=ossec-hids.git diff --git a/src/remoted/sendmsg.c b/src/remoted/sendmsg.c old mode 100755 new mode 100644 index 12556db..b8c645a --- a/src/remoted/sendmsg.c +++ b/src/remoted/sendmsg.c @@ -1,5 +1,3 @@ -/* @(#) $Id$ */ - /* Copyright (C) 2009 Trend Micro Inc. * All right reserved. * @@ -9,155 +7,152 @@ * Foundation */ - -#include "shared.h" #include +#include "shared.h" #include "remoted.h" #include "os_net/os_net.h" - /* pthread send_msg mutex */ -pthread_mutex_t sendmsg_mutex; +static pthread_mutex_t sendmsg_mutex; /* pthread key update mutex */ -pthread_mutex_t keyupdate_mutex; +static pthread_mutex_t keyupdate_mutex; -/* void keyupdate_init() - * Initializes mutex. - */ +/* Initializes mutex */ void keyupdate_init() { - /* Initializing mutex */ + /* Initialize mutex */ pthread_mutex_init(&keyupdate_mutex, NULL); } - -/* void void key_lock() - * void key_unlock() - * Locks/unlocks the update mutex. - */ void key_lock() { - if(pthread_mutex_lock(&keyupdate_mutex) != 0) - { + if (pthread_mutex_lock(&keyupdate_mutex) != 0) { merror(MUTEX_ERROR, ARGV0); } } + void key_unlock() { - if(pthread_mutex_unlock(&keyupdate_mutex) != 0) - { + if (pthread_mutex_unlock(&keyupdate_mutex) != 0) { merror(MUTEX_ERROR, ARGV0); } } - -/* check_keyupdate() - * Check for key updates. - */ +/* Check for key updates */ int check_keyupdate() { - /* Checking key for updates. */ - if(!OS_CheckUpdateKeys(&keys)) - { - return(0); + /* Check key for updates */ + if (!OS_CheckUpdateKeys(&keys)) { + return (0); } - + key_lock(); - - /* Locking before using */ - if(pthread_mutex_lock(&sendmsg_mutex) != 0) - { + + /* Lock before using */ + if (pthread_mutex_lock(&sendmsg_mutex) != 0) { key_unlock(); merror(MUTEX_ERROR, ARGV0); - return(0); + return (0); } - - if(OS_UpdateKeys(&keys)) - { - if(pthread_mutex_unlock(&sendmsg_mutex) != 0) - { + + if (OS_UpdateKeys(&keys)) { + if (pthread_mutex_unlock(&sendmsg_mutex) != 0) { merror(MUTEX_ERROR, ARGV0); } key_unlock(); - return(1); + return (1); } - if(pthread_mutex_unlock(&sendmsg_mutex) != 0) - { + if (pthread_mutex_unlock(&sendmsg_mutex) != 0) { merror(MUTEX_ERROR, ARGV0); } key_unlock(); - - return(0); -} + return (0); +} -/* send_msg_init(): - * Initializes send_msg. - */ +/* Initialize send_msg */ void send_msg_init() { - /* Initializing mutex */ + /* Initialize mutex */ pthread_mutex_init(&sendmsg_mutex, NULL); } -/* send_msg() - * Send message to an agent. +/* + * Send message to an agent * Returns -1 on error */ -int send_msg(int agentid, char *msg) -{ - int msg_size; - char crypt_msg[OS_MAXSTR +1]; +int send_msg(unsigned int agentid, const char *msg) +{ + size_t msg_size, sa_size; + char crypt_msg[OS_MAXSTR + 1]; + struct sockaddr * dest_sa; /* If we don't have the agent id, ignore it */ - if(keys.keyentries[agentid]->rcvd < (time(0) - (2*NOTIFY_TIME))) - { - return(-1); + if (keys.keyentries[agentid]->rcvd < (time(0) - (2 * NOTIFY_TIME))) { + return (-1); } - msg_size = CreateSecMSG(&keys, msg, crypt_msg, agentid); - if(msg_size == 0) - { - merror(SEC_ERROR,ARGV0); - return(-1); + if (msg_size == 0) { + merror(SEC_ERROR, ARGV0); + return (-1); } - - /* Locking before using */ - if(pthread_mutex_lock(&sendmsg_mutex) != 0) - { + /* Lock before using */ + if (pthread_mutex_lock(&sendmsg_mutex) != 0) { merror(MUTEX_ERROR, ARGV0); - return(-1); + return (-1); } + /* Send initial message */ + dest_sa = (struct sockaddr *)&keys.keyentries[agentid]->peer_info; + sa_size = (dest_sa->sa_family == AF_INET) ? + sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6); + + /* + * Because we handle multiple IP addresses, we won't know what interfaces + * are active for network communication until we receive something on one + * of them. This is a work around in the event we need to send before + * we have identified the working interface in secure.c. (dgs - 2/26/18) + */ + + if (logr.sock == 0) { + int i, ok = 0; + + /* socket not established - try current sockets */ + for (i = 0; i < logr.netinfo->fdcnt; i++) { + if (sendto(logr.netinfo->fds[i], crypt_msg, msg_size, 0, + dest_sa, sa_size) < 0) { + continue; + } + + ok = 1; + break; + } - /* Sending initial message */ - if(sendto(logr.sock, crypt_msg, msg_size, 0, - (struct sockaddr *)&keys.keyentries[agentid]->peer_info, - logr.peer_size) < 0) - { - merror(SEND_ERROR,ARGV0, keys.keyentries[agentid]->id); + /* if we tried all the sockets and noe of them worked, send an error */ + if (ok == 0) { + merror(SEND_ERROR, ARGV0, keys.keyentries[agentid]->id); + } + } else { + /* working socket identified in secure.c */ + if (sendto(logr.sock, crypt_msg, msg_size, 0, dest_sa, sa_size) < 0) { + merror(SEND_ERROR, ARGV0, keys.keyentries[agentid]->id); + } } - - - /* Unlocking mutex */ - if(pthread_mutex_unlock(&sendmsg_mutex) != 0) - { + + /* Unlock mutex */ + if (pthread_mutex_unlock(&sendmsg_mutex) != 0) { merror(MUTEX_ERROR, ARGV0); - return(-1); + return (-1); } - - return(0); + return (0); } - - -/* EOF */