-/* @(#) $Id: main.c,v 1.15 2009/08/05 18:02:13 dcid Exp $ */
+/* @(#) $Id: ./src/addagent/main.c, 2011/09/08 dcid Exp $
+ */
/* Copyright (C) 2009 Trend Micro Inc.
* All rights reserved.
*
* This program is a free software; you can redistribute it
* and/or modify it under the terms of the GNU General Public
- * License (version 3) as published by the FSF - Free Software
+ * License (version 2) as published by the FSF - Free Software
* Foundation
*/
#include "manage_agents.h"
-
+#include <stdlib.h>
+
+#if defined(__MINGW32__)
+static int setenv(const char * name, const char * val, int overwrite) {
+ int len = strlen(name) + strlen(val) + 2;
+ char * str = (char *)malloc(len);
+ snprintf(str, len, "%s=%s", name, val);
+ putenv(str);
+ return 0;
+}
+#endif
/** help **/
void helpmsg()
printf("\t-V Display OSSEC version.\n");
printf("\t-l List available agents.\n");
printf("\t-e <id> Extracts key for an agent (Manager only).\n");
- printf("\t-i <id> Import authentication key (Agent only).\n\n");
+ printf("\t-r <id> Remove an agent. (Manager only).\n");
+ printf("\t-i <id> Import authentication key (Agent only).\n");
+ printf("\t-f <file> Bulk generate client keys from file. (Manager only).\n");
+ printf("\t <file> contains lines in IP,NAME format.\n\n");
exit(1);
}
void print_banner()
{
printf("\n");
- printf(BANNER, __name, __version);
+ printf(BANNER, __ossec_name, __version);
#ifdef CLIENT
printf(BANNER_CLIENT);
int c = 0, cmdlist = 0;
char *cmdexport = NULL;
char *cmdimport = NULL;
-
+ char *cmdbulk = NULL;
+
#ifndef WIN32
char *dir = DEFAULTDIR;
char *group = GROUPGLOBAL;
int gid;
+ #else
+ FILE *fp;
+ TCHAR path[2048];
+ DWORD last_error;
+ int ret;
#endif
-
+
/* Setting the name */
OS_SetName(ARGV0);
-
- while((c = getopt(argc, argv, "Vhle:i:")) != -1){
+
+ while((c = getopt(argc, argv, "Vhle:r:i:f:")) != -1){
switch(c){
case 'V':
print_version();
ErrorExit("%s: -e needs an argument",ARGV0);
cmdexport = optarg;
break;
+ case 'r':
+ #ifdef CLIENT
+ ErrorExit("%s: You can't remove keys on an agent", ARGV0);
+ #endif
+ if(!optarg)
+ ErrorExit("%s: -r needs an argument",ARGV0);
+
+ /* Use environment variables already available to remove_agent() */
+ setenv("OSSEC_ACTION", "r", 1);
+ setenv("OSSEC_AGENT_ID", optarg, 1);
+ setenv("OSSEC_ACTION_CONFIRMED", "y", 1);
+ break;
case 'i':
#ifndef CLIENT
ErrorExit("%s: You can't import keys on the manager.", ARGV0);
ErrorExit("%s: -i needs an argument",ARGV0);
cmdimport = optarg;
break;
+ case 'f':
+ #ifdef CLIENT
+ ErrorExit("%s: You can't bulk generate keys on an agent.", ARGV0);
+ #endif
+ if(!optarg)
+ ErrorExit("%s: -f needs an argument",ARGV0);
+ cmdbulk = optarg;
+ printf("Bulk load file: %s\n", cmdbulk);
+ break;
case 'l':
cmdlist = 1;
break;
}
}
-
-
+
+
/* Getting currently time */
time1 = time(0);
restart_necessary = 0;
-
-
- #ifndef WIN32
+
+
+ #ifndef WIN32
/* Getting the group name */
gid = Privsep_GetGroup(group);
if(gid < 0)
{
ErrorExit(USER_ERROR, ARGV0, "", group);
}
-
-
+
+
/* Setting the group */
if(Privsep_SetGroup(gid) < 0)
{
ErrorExit(SETGID_ERROR, ARGV0, group);
}
-
-
+
+
/* Chrooting to the default directory */
if(Privsep_Chroot(dir) < 0)
{
/* Starting signal handler */
StartSIG2(ARGV0, manage_shutdown);
- #endif
+ #else
+
+ /* Get full path to the directory this
+ * executable lives in
+ */
+ ret = GetModuleFileName(NULL, path, sizeof(path));
+
+ /* check for errors */
+ if(!ret)
+ {
+ ErrorExit(GMF_ERROR);
+ }
+
+ /* Get last error */
+ last_error = GetLastError();
+
+ /* Look for errors */
+ if(last_error != ERROR_SUCCESS)
+ {
+ if(last_error == ERROR_INSUFFICIENT_BUFFER)
+ {
+ ErrorExit(GMF_BUFF_ERROR, ret, sizeof(path));
+ }
+ else
+ {
+ ErrorExit(GMF_UNKN_ERROR, last_error);
+ }
+ }
+
+ /* Remove file name from path */
+ PathRemoveFileSpec(path);
+
+ /* Move to correct directory */
+ if(chdir(path))
+ {
+ ErrorExit(CHDIR_ERROR, path);
+ }
+
+ /* Check permissions */
+ fp = fopen(OSSECCONF, "r");
+ if(fp)
+ {
+ fclose(fp);
+ }
+ else
+ {
+ ErrorExit(CONF_ERROR, OSSECCONF);
+ }
+
+ #endif
if(cmdlist == 1)
{
k_extract(cmdexport);
exit(0);
}
+ else if(cmdbulk)
+ {
+ k_bulkload(cmdbulk);
+ exit(0);
+ }
{
int leave_s = 0;
print_banner();
-
- user_msg = read_from_user();
-
+
+ /* Get ACTION from the environment. If ACTION is specified,
+ * we must set leave_s = 1 to ensure that the loop will end */
+ user_msg = getenv("OSSEC_ACTION");
+ if (user_msg == NULL) {
+ user_msg = read_from_user();
+ }
+ else{
+ leave_s = 1;
+ }
+
/* All the allowed actions */
switch(user_msg[0])
{
case 'i':
case 'I':
k_import(NULL);
- break;
+ break;
case 'l':
case 'L':
list_agents(0);
- break;
+ break;
case 'r':
case 'R':
remove_agent();
leave_s = 1;
break;
case 'V':
- print_version();
+ print_version();
break;
- default:
+ default:
printf("\n ** Invalid Action ** \n\n");
- break;
+ break;
}
if(leave_s)
{
- break;
+ break;
}
-
+
continue;
-
+
}
/* Checking if restart message is necessary */
printf("\n");
}
printf(EXIT);
-
+
return(0);
}