Imported Upstream version 2.5.1
[ossec-hids.git] / src / util / list_agents.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 "read-agents.h"
15
16 #undef ARGV0
17 #define ARGV0 "list_agents"
18
19 /** help **/
20 void helpmsg()
21 {
22     printf("\nOSSEC HIDS %s: List available agents.\n", ARGV0);
23     printf("Available options:\n");
24     printf("\t-h    This help message.\n");
25     printf("\t-a    List all agents.\n");
26     printf("\t-c    List the connected (active) agents.\n");
27     printf("\t-n    List the not connected (active) agents.\n");
28     exit(1);
29 }
30
31
32 /** main **/
33 int main(int argc, char **argv)
34 {
35     char *dir = DEFAULTDIR;
36     char *group = GROUPGLOBAL;
37     char *user = USER;
38     
39     char *msg;
40     char **agent_list;
41     int gid;
42     int uid;
43     int flag;
44     
45
46     /* Setting the name */
47     OS_SetName(ARGV0);
48         
49     
50     /* user arguments */
51     if(argc < 2)
52     {
53         helpmsg();
54     }
55     
56     /* Getting the group name */
57     gid = Privsep_GetGroup(group);
58     uid = Privsep_GetUser(user);
59     if(gid < 0)
60     {
61             ErrorExit(USER_ERROR, ARGV0, user, group);
62     }
63         
64     
65     /* Setting the group */
66     if(Privsep_SetGroup(gid) < 0)
67     {
68             ErrorExit(SETGID_ERROR,ARGV0, group);
69     }
70     
71     
72     /* Chrooting to the default directory */
73     if(Privsep_Chroot(dir) < 0)
74     {
75         ErrorExit(CHROOT_ERROR, ARGV0, dir);
76     }
77
78
79     /* Inside chroot now */
80     nowChroot();
81  
82
83     /* Setting the user */
84     if(Privsep_SetUser(uid) < 0)
85     {
86         ErrorExit(SETUID_ERROR, ARGV0, user);
87     }
88   
89     /* User options */
90     if(strcmp(argv[1], "-h") == 0)
91     {
92         helpmsg();
93     }
94     else if(strcmp(argv[1], "-a") == 0)
95     {
96         flag = GA_ALL;
97         msg = "is available.";
98     }
99     else if(strcmp(argv[1], "-c") == 0)
100     {
101         flag = GA_ACTIVE;
102         msg = "is active.";
103     }
104     else if(strcmp(argv[1], "-n") == 0)
105     {
106         flag = GA_NOTACTIVE;
107         msg = "is not active.";
108     }
109     else
110     {
111         printf("\n** Invalid option '%s'.\n", argv[1]);
112         helpmsg();
113     }
114
115
116     agent_list = get_agents(flag);
117     if(agent_list)
118     {
119         char **agent_list_pt = agent_list;
120         
121         while(*agent_list)
122         {
123             printf("%s %s\n", *agent_list, msg);
124             agent_list++;
125         }
126
127         free_agents(agent_list_pt);
128     }
129     else
130     {
131         printf("** No agent available.\n");
132     }
133     return(0);
134 }
135
136
137 /* EOF */