new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / util / list_agents.c
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All right reserved.
3  *
4  * This program is a free software; you can redistribute it
5  * and/or modify it under the terms of the GNU General Public
6  * License (version 2) as published by the FSF - Free Software
7  * Foundation
8  */
9
10 #include "shared.h"
11 #include "read-agents.h"
12
13 #undef ARGV0
14 #define ARGV0 "list_agents"
15
16 /* Prototypes */
17 static void helpmsg(void) __attribute__((noreturn));
18
19
20 static 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 int main(int argc, char **argv)
32 {
33     const char *dir = DEFAULTDIR;
34     const char *group = GROUPGLOBAL;
35     const char *user = USER;
36
37     const char *msg;
38     char **agent_list;
39     gid_t gid;
40     uid_t uid;
41     int flag = 0;
42
43     /* Set the name */
44     OS_SetName(ARGV0);
45
46     /* User arguments */
47     if (argc < 2) {
48         helpmsg();
49     }
50
51     /* Get the group name */
52     gid = Privsep_GetGroup(group);
53     uid = Privsep_GetUser(user);
54     if (uid == (uid_t) - 1 || gid == (gid_t) - 1) {
55         ErrorExit(USER_ERROR, ARGV0, user, group);
56     }
57
58     /* Set the group */
59     if (Privsep_SetGroup(gid) < 0) {
60         ErrorExit(SETGID_ERROR, ARGV0, group, errno, strerror(errno));
61     }
62
63     /* Chroot to the default directory */
64     if (Privsep_Chroot(dir) < 0) {
65         ErrorExit(CHROOT_ERROR, ARGV0, dir, errno, strerror(errno));
66     }
67
68     /* Inside chroot now */
69     nowChroot();
70
71     /* Set the user */
72     if (Privsep_SetUser(uid) < 0) {
73         ErrorExit(SETUID_ERROR, ARGV0, user, errno, strerror(errno));
74     }
75
76     /* User options */
77     if (strcmp(argv[1], "-h") == 0) {
78         helpmsg();
79     } else if (strcmp(argv[1], "-a") == 0) {
80         flag = GA_ALL;
81         msg = "is available.";
82     } else if (strcmp(argv[1], "-c") == 0) {
83         flag = GA_ACTIVE;
84         msg = "is active.";
85     } else if (strcmp(argv[1], "-n") == 0) {
86         flag = GA_NOTACTIVE;
87         msg = "is not active.";
88     } else {
89         printf("\n** Invalid option '%s'.\n", argv[1]);
90         helpmsg();
91     }
92
93     agent_list = get_agents(flag);
94     if (agent_list) {
95         char **agent_list_pt = agent_list;
96
97         while (*agent_list) {
98             printf("%s %s\n", *agent_list, msg);
99             agent_list++;
100         }
101
102         free_agents(agent_list_pt);
103     } else {
104         printf("** No agent available.\n");
105     }
106     return (0);
107 }
108