bcb2686ebec56d1aaf03ac6f1a584a9efcc175e0
[ossec-hids.git] / src / client-agent / agentd.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 /* Part of the OSSEC HIDS
13  * Available at http://www.ossec.net/hids/
14  */
15
16
17 #include "shared.h"
18 #include "agentd.h"
19
20 #include "os_net/os_net.h"
21
22
23
24 /* AgentdStart v0.2, 2005/11/09
25  * Starts the agent daemon.
26  */
27 void AgentdStart(char *dir, int uid, int gid, char *user, char *group)
28 {
29     int rc = 0;
30     int pid = 0;
31     int maxfd = 0;   
32
33     fd_set fdset;
34     
35     struct timeval fdtimeout;
36
37     
38     /* Going daemon */
39     pid = getpid();
40     available_server = 0;
41     nowDaemon();
42     goDaemon();
43
44     
45     /* Setting group ID */
46     if(Privsep_SetGroup(gid) < 0)
47         ErrorExit(SETGID_ERROR, ARGV0, group);
48
49     
50     /* chrooting */
51     if(Privsep_Chroot(dir) < 0)
52         ErrorExit(CHROOT_ERROR, ARGV0, dir);
53
54     
55     nowChroot();
56
57
58     if(Privsep_SetUser(uid) < 0)
59         ErrorExit(SETUID_ERROR, ARGV0, user);
60
61
62     /* Create the queue. In this case we are going to create
63      * and read from it
64      * Exit if fails.
65      */
66     if((logr->m_queue = StartMQ(DEFAULTQUEUE, READ)) < 0)
67         ErrorExit(QUEUE_ERROR, ARGV0, DEFAULTQUEUE, strerror(errno));
68
69     maxfd = logr->m_queue;
70     logr->sock = -1;
71     
72
73
74     /* Creating PID file */     
75     if(CreatePID(ARGV0, getpid()) < 0)
76         merror(PID_ERROR,ARGV0);
77
78
79     /* Reading the private keys  */
80     verbose(ENC_READ, ARGV0);
81         
82     OS_ReadKeys(&keys);
83     OS_StartCounter(&keys);
84     os_write_agent_info(keys.keyentries[0]->name, NULL, keys.keyentries[0]->id);
85
86
87     /* Start up message */
88     verbose(STARTUP_MSG, ARGV0, (int)getpid());
89
90     
91     /* Initial random numbers */
92     #ifdef __OpenBSD__
93     srandomdev();
94     #else
95     srandom( time(0) + getpid()+ pid + getppid());
96     #endif
97                     
98     random();
99
100
101     /* Connecting UDP */
102     rc = 0;
103     while(rc < logr->rip_id)
104     {
105         verbose("%s: INFO: Server IP Address: %s", ARGV0, logr->rip[rc]);
106         rc++;
107     }
108
109
110     /* Trying to connect to the server */
111     if(!connect_server(0))
112     {
113         ErrorExit(UNABLE_CONN, ARGV0);
114     }
115     
116
117     /* Setting max fd for select */
118     if(logr->sock > maxfd)
119     {
120         maxfd = logr->sock;
121     }
122
123
124     /* Connecting to the execd queue */
125     if(logr->execdq == 0)
126     {
127         if((logr->execdq = StartMQ(EXECQUEUE, WRITE)) < 0)
128         {
129             merror("%s: INFO: Unable to connect to the active response "
130                    "queue (disabled).", ARGV0);
131             logr->execdq = -1;
132         }
133     }
134
135
136
137     /* Trying to connect to server */
138     os_setwait();
139
140     start_agent(1);
141     
142     os_delwait();
143
144
145     /* Sending integrity message for agent configs */
146     intcheck_file(OSSECCONF, dir);
147     intcheck_file(OSSEC_DEFINES, dir);
148
149    
150     /* Sending first notification */
151     run_notify();
152     
153      
154     /* Maxfd must be higher socket +1 */
155     maxfd++;
156     
157     
158     /* monitor loop */
159     while(1)
160     {
161         /* Monitoring all available sockets from here */
162         FD_ZERO(&fdset);
163         FD_SET(logr->sock, &fdset);
164         FD_SET(logr->m_queue, &fdset);
165
166         fdtimeout.tv_sec = 120;
167         fdtimeout.tv_usec = 0;
168
169         
170         /* Wait for 120 seconds at a maximum for any descriptor */
171         rc = select(maxfd, &fdset, NULL, NULL, &fdtimeout);
172         if(rc == -1)
173         {
174             ErrorExit(SELECT_ERROR, ARGV0);
175         }
176        
177         
178         else if(rc == 0)
179         {
180             continue;
181         }    
182
183         
184         /* For the receiver */
185         if(FD_ISSET(logr->sock, &fdset))
186         {
187             receive_msg();
188         }
189
190         
191         /* For the forwarder */
192         if(FD_ISSET(logr->m_queue, &fdset))
193         {
194             EventForward();
195         }
196     }
197 }
198
199
200
201 /* EOF */