1 /* @(#) $Id: ./src/addagent/main.c, 2011/09/08 dcid Exp $
4 /* Copyright (C) 2009 Trend Micro Inc.
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
15 #include "manage_agents.h"
18 #if defined(__MINGW32__)
19 static int setenv(const char * name, const char * val, int overwrite) {
20 int len = strlen(name) + strlen(val) + 2;
21 char * str = (char *)malloc(len);
22 snprintf(str, len, "%s=%s", name, val);
31 printf("\nOSSEC HIDS %s: Manage agents.\n", ARGV0);
32 printf("Available options:\n");
33 printf("\t-h This help message.\n");
34 printf("\t-V Display OSSEC version.\n");
35 printf("\t-l List available agents.\n");
36 printf("\t-e <id> Extracts key for an agent (Manager only).\n");
37 printf("\t-r <id> Remove an agent. (Manager only).\n");
38 printf("\t-i <id> Import authentication key (Agent only).\n");
39 printf("\t-f <file> Bulk generate client keys from file. (Manager only).\n");
40 printf("\t <file> contains lines in IP,NAME format.\n\n");
49 printf(BANNER, __ossec_name, __version);
52 printf(BANNER_CLIENT);
61 /* Clean shutdown on kill */
62 void manage_shutdown()
64 /* Checking if restart message is necessary */
80 int main(int argc, char **argv)
84 int c = 0, cmdlist = 0;
85 char *cmdexport = NULL;
86 char *cmdimport = NULL;
90 char *dir = DEFAULTDIR;
91 char *group = GROUPGLOBAL;
101 /* Setting the name */
105 while((c = getopt(argc, argv, "Vhle:r:i:f:")) != -1){
118 ErrorExit("%s: You can't export keys on an agent", ARGV0);
121 ErrorExit("%s: -e needs an argument",ARGV0);
126 ErrorExit("%s: You can't remove keys on an agent", ARGV0);
129 ErrorExit("%s: -r needs an argument",ARGV0);
131 /* Use environment variables already available to remove_agent() */
132 setenv("OSSEC_ACTION", "r", 1);
133 setenv("OSSEC_AGENT_ID", optarg, 1);
134 setenv("OSSEC_ACTION_CONFIRMED", "y", 1);
138 ErrorExit("%s: You can't import keys on the manager.", ARGV0);
141 ErrorExit("%s: -i needs an argument",ARGV0);
146 ErrorExit("%s: You can't bulk generate keys on an agent.", ARGV0);
149 ErrorExit("%s: -f needs an argument",ARGV0);
151 printf("Bulk load file: %s\n", cmdbulk);
165 /* Getting currently time */
167 restart_necessary = 0;
171 /* Getting the group name */
172 gid = Privsep_GetGroup(group);
175 ErrorExit(USER_ERROR, ARGV0, "", group);
179 /* Setting the group */
180 if(Privsep_SetGroup(gid) < 0)
182 ErrorExit(SETGID_ERROR, ARGV0, group);
186 /* Chrooting to the default directory */
187 if(Privsep_Chroot(dir) < 0)
189 ErrorExit(CHROOT_ERROR, ARGV0, dir);
193 /* Inside chroot now */
197 /* Starting signal handler */
198 StartSIG2(ARGV0, manage_shutdown);
202 /* Get full path to the directory this
203 * executable lives in
205 ret = GetModuleFileName(NULL, path, sizeof(path));
207 /* check for errors */
210 ErrorExit(GMF_ERROR);
214 last_error = GetLastError();
216 /* Look for errors */
217 if(last_error != ERROR_SUCCESS)
219 if(last_error == ERROR_INSUFFICIENT_BUFFER)
221 ErrorExit(GMF_BUFF_ERROR, ret, sizeof(path));
225 ErrorExit(GMF_UNKN_ERROR, last_error);
229 /* Remove file name from path */
230 PathRemoveFileSpec(path);
232 /* Move to correct directory */
235 ErrorExit(CHDIR_ERROR, path);
238 /* Check permissions */
239 fp = fopen(OSSECCONF, "r");
246 ErrorExit(CONF_ERROR, OSSECCONF);
253 list_agents(cmdlist);
263 k_extract(cmdexport);
280 /* Get ACTION from the environment. If ACTION is specified,
281 * we must set leave_s = 1 to ensure that the loop will end */
282 user_msg = getenv("OSSEC_ACTION");
283 if (user_msg == NULL) {
284 user_msg = read_from_user();
290 /* All the allowed actions */
321 printf("\n ** Invalid Action ** \n\n");
334 /* Checking if restart message is necessary */
335 if(restart_necessary)
337 printf(MUST_RESTART);