- #ifndef WIN32
- chmod(AUTH_FILE, 0440);
- #endif
-
-
- /* Random 1: Time took to write the agent information.
- * Random 2: Time took to choose the action.
- * Random 3: All of this + time + pid
- * Random 4: Md5 all of this + the name, key and ip
- * Random 5: Final key
+
+ } while (!_ip);
+
+ if (!*id) {
+ do {
+ /* Default ID */
+ i = MAX_AGENTS + 32512;
+ snprintf(id, 8, "%03d", i); //XXX
+ while (!IDExist(id)) {
+ i--;
+ snprintf(id, 8, "%03d", i);
+
+ /* No key present, use id 0 */
+ if (i <= 0) {
+ i = 0;
+ break;
+ }
+ }
+ snprintf(id, 8, "%03d", i + 1);
+
+ /* Get ID */
+ if (!json_output) {
+ printf(ADD_ID, id);
+ fflush(stdout);
+ }
+
+
+ /* Get Agent ID from environment. If 0, use default ID. If null,
+ * get from user input. If value from environment is invalid,
+ * we force user to specify an ID from the terminal. Otherwise,
+ * our program goes to infinite loop.
+ */
+ _id = getenv("OSSEC_AGENT_ID");
+ if (_id == NULL || IDExist(_id) || !OS_IsValidID(_id)) {
+ _id = read_from_user();
+ }
+
+ /* Quit */
+ if (strcmp(_id, QUIT) == 0) {
+ goto cleanup;
+ }
+
+ if (_id[0] != '\0' && strcmp(_id, "0")) {
+ strncpy(id, _id, FILE_SIZE - 1);
+ }
+
+ if (OS_IsValidID(id)) {
+ FormatID(id);
+ } else {
+ printf(INVALID_ID, id);
+ }
+
+ /* Search for ID KEY -- no duplicates */
+ if (IDExist(id)) {
+ printf(ADD_ERROR_ID, id);
+ }
+
+ } while (IDExist(id) || !OS_IsValidID(id));
+ }
+
+ if (!json_output) {
+ printf(AGENT_INFO, id, name, ip);
+ fflush(stdout);
+ }
+
+
+ do {
+ if (!json_output)
+ printf(ADD_CONFIRM);
+
+ /* Confirmation by an environment variable. The valid value is y/Y.
+ * If the user provides anything other string, it is considered as
+ * n/N; please note that the old code only accepts y/Y/n/N. So if
+ * the variable OSSEC_ACTION_CONFIRMED is 'foobar', the program will
+ * go into an infinite loop.