+ /* Zero strings */
+ memset(str1, '\0', STR_SIZE + 1);
+ memset(str2, '\0', STR_SIZE + 1);
+
+ /* Check the name */
+ if (!OS_IsValidName(name)) {
+ printf(INVALID_NAME, name);
+ continue;
+ }
+
+ /* Search for name -- no duplicates */
+ if (NameExist(name)) {
+ printf(ADD_ERROR_NAME, name);
+ continue;
+ }
+
+ if (!OS_IsValidIP(ip, &c_ip)) {
+ printf(IP_ERROR, ip);
+ continue;
+ }
+
+ /* Default ID */
+ i = MAX_AGENTS + 32512;
+ snprintf(id, 8, "%03d", i);
+ 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);
+
+ if (!OS_IsValidID(id)) {
+ printf(INVALID_ID, id);
+ goto cleanup;
+ }
+
+ /* Search for ID KEY -- no duplicates */
+ if (IDExist(id)) {
+ printf(NO_DEFAULT, i + 1);
+ goto cleanup;
+ }
+
+ printf(AGENT_INFO, id, name, ip);
+ fflush(stdout);
+
+ time3 = time(0);
+ rand2 = random();
+
+ fp = fopen(authfile, "a");
+ if (!fp) {
+ ErrorExit(FOPEN_ERROR, ARGV0, KEYS_FILE, errno, strerror(errno));
+ }
+#ifndef WIN32
+ if (chmod(authfile, 0440) == -1) {
+ ErrorExit(CHMOD_ERROR, ARGV0, authfile, errno, strerror(errno));
+ }
+#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
+ */
+
+ snprintf(str1, STR_SIZE, "%d%s%d", (int)(time3 - time2), name, (int)rand1);
+ snprintf(str2, STR_SIZE, "%d%s%s%d", (int)(time2 - time1), ip, id, (int)rand2);
+
+ OS_MD5_Str(str1, md1);
+ OS_MD5_Str(str2, md2);
+
+ snprintf(str1, STR_SIZE, "%s%d%d%d", md1, (int)getpid(), (int)random(),
+ (int)time3);
+ OS_MD5_Str(str1, md1);
+
+ fprintf(fp, "%s %s %s %s%s\n", id, name, c_ip.ip, md1, md2);
+ fclose(fp);
+
+ printf(AGENT_ADD, id);
+ restart_necessary = 1;
+
+cleanup:
+ free(c_ip.ip);
+ };
+
+ fclose(infp);
+ return (0);
+}