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