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