new upstream release (3.3.0); modify package compatibility for Stretch
[ossec-hids.git] / src / addagent / manage_agents.h
1 /* Copyright (C) 2009 Trend Micro Inc.
2  * All rights 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 "sec.h"
12 #include "external/cJSON/cJSON.h"
13
14 /** Prototypes **/
15
16 /* b64 function prototypes */
17 char *decode_base64(const char *src);
18 char *encode_base64(int size, char *src);
19
20 /* Read any input from the user (stdin) */
21 char *read_from_user(void);
22
23 /* Add or remove an agent */
24 int add_agent(int json_output);
25 int remove_agent(int json_output);
26
27
28 /* Extract or import a key */
29 int k_extract(const char *cmdextract, int json_output);
30 int k_import(const char *cmdimport);
31 int k_bulkload(const char *cmdbulk);
32
33 /* Validation functions */
34 int OS_IsValidName(const char *u_name);
35 int OS_IsValidID(const char *id);
36 int IDExist(const char *id);
37 int NameExist(const char *u_name);
38 char *IPExist(const char *u_name);
39 char *getFullnameById(const char *id);
40 char *OS_AddNewAgent(const char *name, const char *ip, const char *id);
41 int  OS_RemoveAgent(const char *id);
42 double OS_AgentAntiquity(const char *id);
43 void FormatID(char *id);
44
45 /* Print available agents */
46 int print_agents(int print_status, int active_only, int csv_output, cJSON *json_output);
47 int list_agents(int cmdlist);
48
49 /* Clear a line */
50 char *chomp(char *str);
51
52 /* Shared variables */
53 extern int restart_necessary;
54 extern time_t time1;
55 extern time_t time2;
56 extern time_t time3;
57 extern long int rand1;
58 extern long int rand2;
59 extern fpos_t fp_pos;
60
61 /* Internal defines */
62 #define USER_SIZE       514
63 #define FILE_SIZE       257
64 #define STR_SIZE        66
65
66 /* Internal strings */
67 #define QUIT                "\\q"
68
69 /* Print agents */
70 #define PRINT_AVAILABLE     "\nAvailable agents: \n"
71 #define PRINT_AGENT         "   ID: %s, Name: %s, IP: %s\n"
72 #define PRINT_AGENT_STATUS  "   ID: %s, Name: %s, IP: %s, %s\n"
73
74 /* Add new agent */
75 #define ADD_NEW         "\n- Adding a new agent"\
76                         " (use '\\q' to return to the main menu).\n"\
77                         "  Please provide the following:\n"
78 #define ADD_NAME        "   * A name for the new agent: "
79 #define ADD_IP          "   * The IP Address of the new agent: "
80 #define ADD_ID          "   * An ID for the new agent[%s]: "
81 #define AGENT_INFO      "Agent information:\n   ID:%s\n   Name:%s\n   " \
82                         "IP Address:%s\n\n"
83 #define ADD_CONFIRM     "Confirm adding it?(y/n): "
84 #define AGENT_ADD       "Agent added with ID %s.\n"
85 #define ADDED           "Added.\n"
86 #define ADD_NOT         "Not Adding.\n"
87 #define PRESS_ENTER     "** Press ENTER to return to the main menu.\n"
88 #define MUST_RESTART    "\n** You must restart OSSEC for your changes" \
89                         " to take effect.\n\n"
90
91 /* Add errors */
92 #define ADD_ERROR_ID    "\n** ID '%s' already present. They must be unique.\n\n"
93 #define ADD_ERROR_NAME  "\n** Name '%s' already present. Please enter a new name.\n\n"
94 #define IP_ERROR        "\n** Invalid IP '%s'. Please enter a valid IP Address.\n\n"
95 #define IP_DUP_ERROR    "\n** Duplicated IP '%s'. Please enter an unique IP Address.\n\n"
96 #define NO_AGENT        "\n** No agent available. You need to add one first.\n"
97 #define NO_ID           "\n** Invalid ID '%s' given. ID is not present.\n"
98 #define NO_KEY          "\n** Invalid authentication key. Starting over again.\n"
99 #define INVALID_ID      "\n** Invalid ID '%s' given. ID must be numeric (max 8 digits).\n\n"
100 #define INVALID_NAME    "\n** Invalid name '%s' given. Name must contain only alphanumeric characters (min=2, max=32).\n\n"
101 #define NO_DEFAULT      "\n** Could not get default ID. Ran out of IDs to try with a max of '%d'. Either need to raise max agents or clean out client.keys.\n\n"
102
103 /* Remove agent */
104 #define REMOVE_ID       "Provide the ID of the agent to be removed (or '\\q' to quit): "
105 #define REMOVE_CONFIRM  "Confirm deleting it?(y/n): "
106 #define REMOVE_DONE     "Agent '%s' removed.\n"
107 #define REMOVE_NOT      "Not removing.\n"
108
109 /* Import agent */
110 #define IMPORT_KEY      "\n* Provide the Key generated by the server.\n" \
111                         "* The best approach is to cut and paste it.\n" \
112                         "*** OBS: Do not include spaces or new lines.\n\n" \
113                         "Paste it here (or '\\q' to quit): "
114
115 /* Extract key */
116 #define EXTRACT_KEY     "Provide the ID of the agent to extract " \
117                         "the key (or '\\q' to quit): "
118 #define EXTRACT_MSG     "\nAgent key information for '%s' is: \n%s\n"
119
120
121 /* Common errors */
122 #define ERROR_KEYS      "Unable to handle keys file. Exiting.\n"
123 #define EXTRACT_ERROR   "Unable to extract agent key.\n"
124 #define INPUT_LARGE     ARGV0 ": Input too large. Not adding it.\n"
125 #define EXIT            ARGV0 ": Exiting.\n"
126
127 #define BANNER          "\n****************************************" \
128                         "\n* %s %s Agent manager.     *" \
129                         "\n* The following options are available: *" \
130                         "\n****************************************\n"
131
132 #define BANNER_OPT      "   (A)dd an agent (A).\n" \
133                         "   (E)xtract key for an agent (E).\n" \
134                         "   (L)ist already added agents (L).\n" \
135                         "   (R)emove an agent (R).\n" \
136                         "   (Q)uit.\n" \
137                         "Choose your action: A,E,L,R or Q: "
138
139 #define BANNER_CLIENT   "   (I)mport key from the server (I).\n" \
140                         "   (Q)uit.\n" \
141                         "Choose your action: I or Q: "
142
143 /* WIN32 errors */
144 #define CONF_ERROR      ARGV0 ": Could not read (%s) (Make sure config exists and executable is running with Administrative privileges).\n"
145 #define GMF_ERROR       ARGV0 ": Could not run GetModuleFileName.\n"
146 #define GMF_BUFF_ERROR  ARGV0 ": Could not get path because it is too long and was shrunk by (%d) characters with a max of (%d).\n"
147 #define GMF_UNKN_ERROR  ARGV0 ": Could not run GetModuleFileName which returned (%ld).\n"
148
149
150 int willchroot;