Imported Upstream version 2.7
[ossec-hids.git] / src / remoted / sendmsg.c
1 /* @(#) $Id: ./src/remoted/sendmsg.c, 2011/09/08 dcid Exp $
2  */
3
4 /* Copyright (C) 2009 Trend Micro Inc.
5  * All right reserved.
6  *
7  * This program is a free software; you can redistribute it
8  * and/or modify it under the terms of the GNU General Public
9  * License (version 2) as published by the FSF - Free Software
10  * Foundation
11  */
12
13
14 #include "shared.h"
15 #include <pthread.h>
16
17 #include "remoted.h"
18 #include "os_net/os_net.h"
19
20
21 /* pthread send_msg mutex */
22 pthread_mutex_t sendmsg_mutex;
23
24 /* pthread key update mutex */
25 pthread_mutex_t keyupdate_mutex;
26
27
28 /* void keyupdate_init()
29  * Initializes mutex.
30  */
31 void keyupdate_init()
32 {
33     /* Initializing mutex */
34     pthread_mutex_init(&keyupdate_mutex, NULL);
35 }
36
37
38 /* void void key_lock()
39  * void key_unlock()
40  * Locks/unlocks the update mutex.
41  */
42 void key_lock()
43 {
44     if(pthread_mutex_lock(&keyupdate_mutex) != 0)
45     {
46         merror(MUTEX_ERROR, ARGV0);
47     }
48 }
49 void key_unlock()
50 {
51     if(pthread_mutex_unlock(&keyupdate_mutex) != 0)
52     {
53         merror(MUTEX_ERROR, ARGV0);
54     }
55 }
56
57
58 /* check_keyupdate()
59  * Check for key updates.
60  */
61 int check_keyupdate()
62 {
63     /* Checking key for updates. */
64     if(!OS_CheckUpdateKeys(&keys))
65     {
66         return(0);
67     }
68
69     key_lock();
70
71     /* Locking before using */
72     if(pthread_mutex_lock(&sendmsg_mutex) != 0)
73     {
74         key_unlock();
75         merror(MUTEX_ERROR, ARGV0);
76         return(0);
77     }
78
79     if(OS_UpdateKeys(&keys))
80     {
81         if(pthread_mutex_unlock(&sendmsg_mutex) != 0)
82         {
83             merror(MUTEX_ERROR, ARGV0);
84         }
85         key_unlock();
86         return(1);
87     }
88
89     if(pthread_mutex_unlock(&sendmsg_mutex) != 0)
90     {
91         merror(MUTEX_ERROR, ARGV0);
92     }
93     key_unlock();
94
95     return(0);
96 }
97
98
99 /* send_msg_init():
100  * Initializes send_msg.
101  */
102 void send_msg_init()
103 {
104     /* Initializing mutex */
105     pthread_mutex_init(&sendmsg_mutex, NULL);
106 }
107
108
109 /* send_msg()
110  * Send message to an agent.
111  * Returns -1 on error
112  */
113 int send_msg(int agentid, char *msg)
114 {
115     int msg_size;
116     char crypt_msg[OS_MAXSTR +1];
117
118
119     /* If we don't have the agent id, ignore it */
120     if(keys.keyentries[agentid]->rcvd < (time(0) - (2*NOTIFY_TIME)))
121     {
122         return(-1);
123     }
124
125
126     msg_size = CreateSecMSG(&keys, msg, crypt_msg, agentid);
127     if(msg_size == 0)
128     {
129         merror(SEC_ERROR,ARGV0);
130         return(-1);
131     }
132
133
134     /* Locking before using */
135     if(pthread_mutex_lock(&sendmsg_mutex) != 0)
136     {
137         merror(MUTEX_ERROR, ARGV0);
138         return(-1);
139     }
140
141
142     /* Sending initial message */
143     if(sendto(logr.sock, crypt_msg, msg_size, 0,
144                        (struct sockaddr *)&keys.keyentries[agentid]->peer_info,
145                        logr.peer_size) < 0)
146     {
147         merror(SEND_ERROR,ARGV0, keys.keyentries[agentid]->id);
148     }
149
150
151     /* Unlocking mutex */
152     if(pthread_mutex_unlock(&sendmsg_mutex) != 0)
153     {
154         merror(MUTEX_ERROR, ARGV0);
155         return(-1);
156     }
157
158
159     return(0);
160 }
161
162
163
164 /* EOF */