- }
- agentname = fname;
-
-
- /* Adding the new agent. */
- if (use_ip_address)
- {
- finalkey = OS_AddNewAgent(agentname, srcip, NULL, NULL);
- }
- else
- {
- finalkey = OS_AddNewAgent(agentname, NULL, NULL, NULL);
- }
- if(!finalkey)
- {
- merror("%s: ERROR: Unable to add agent: %s (internal error)", ARGV0, agentname);
- snprintf(response, 2048, "ERROR: Internal manager error adding agent: %s\n\n", agentname);
- ret = SSL_write(ssl, response, strlen(response));
- snprintf(response, 2048, "ERROR: Unable to add agent.\n\n");
+
+ /* Check for duplicate names */
+ strncpy(fname, agentname, 2048);
+ while (NameExist(fname)) {
+ snprintf(fname, 2048, "%s%d", agentname, acount);
+ acount++;
+ if (acount > 256) {
+ merror("%s: ERROR: Invalid agent name %s (duplicated)", ARGV0, agentname);
+ snprintf(response, 2048, "ERROR: Invalid agent name: %s\n\n", agentname);
+ SSL_write(ssl, response, strlen(response));
+ snprintf(response, 2048, "ERROR: Unable to add agent.\n\n");
+ SSL_write(ssl, response, strlen(response));
+ sleep(1);
+ exit(0);
+ }
+ }
+ agentname = fname;
+
+ /* Check for duplicate IP addresses */
+ char *check_ip_address = NULL;
+ check_ip_address = IPExist(srcip);
+ if(check_ip_address) {
+ merror("%s: ERROR: Invalid IP address %s (duplicated)", ARGV0, check_ip_address);
+ snprintf(response, 2048, "ERROR: Invalid IP address: %s\n\n", check_ip_address);
+ SSL_write(ssl, response, strlen(response));
+ snprintf(response, 2048, "ERROR: Unable to add agent.\n\n");
+ SSL_write(ssl, response, strlen(response));
+ sleep(1);
+ exit(0);
+ }
+
+ /* Add the new agent */
+ if (use_ip_address) {
+ finalkey = OS_AddNewAgent(agentname, srcip, NULL);
+ } else {
+ finalkey = OS_AddNewAgent(agentname, NULL, NULL);
+ }
+ if (!finalkey) {
+ merror("%s: ERROR: Unable to add agent: %s (internal error)", ARGV0, agentname);
+ snprintf(response, 2048, "ERROR: Internal manager error adding agent: %s\n\n", agentname);
+ SSL_write(ssl, response, strlen(response));
+ snprintf(response, 2048, "ERROR: Unable to add agent.\n\n");
+ SSL_write(ssl, response, strlen(response));
+ sleep(1);
+ exit(0);
+ }
+
+ snprintf(response, 2048, "OSSEC K:'%s'\n\n", finalkey);
+ verbose("%s: INFO: Agent key generated for %s (requested by %s)", ARGV0, agentname, srcip);