+/* Set OSSEC Authentication Key */
+int set_ossec_key(char *key, HWND hwnd)
+{
+ FILE *fp;
+
+ char auth_file_tmp[] = AUTH_FILE;
+ char *keys_file = basename_ex(auth_file_tmp);
+
+ char tmp_path[strlen(TMP_DIR) + 1 + strlen(keys_file) + 6 + 1];
+
+ snprintf(tmp_path, sizeof(tmp_path), "%s/%sXXXXXX", TMP_DIR, keys_file);
+
+ /* Create temporary file */
+ if (mkstemp_ex(tmp_path) == -1) {
+ MessageBox(hwnd, "Could not create temporary file.",
+ "Error -- Failure Setting IP", MB_OK);
+ return (0);
+ }
+
+ fp = fopen(tmp_path, "w");
+ if (fp) {
+ fprintf(fp, "%s", key);
+ fclose(fp);
+ } else {
+ MessageBox(hwnd, "Could not open temporary file for write.",
+ "Error -- Failure Importing Key", MB_OK);
+
+ if (unlink(tmp_path)) {
+ MessageBox(hwnd, "Could not delete temporary file.",
+ "Error -- Failure Deleting Temporary File", MB_OK);
+ }
+
+ return (0);
+ }
+
+ if (rename_ex(tmp_path, AUTH_FILE)) {
+ MessageBox(hwnd, "Unable to rename temporary file.",
+ "Error -- Failure Renaming Temporary File", MB_OK);
+
+ if (unlink(tmp_path)) {
+ MessageBox(hwnd, "Could not delete temporary file.",
+ "Error -- Failure Deleting Temporary File", MB_OK);
+ }
+
+ return (0);
+ }