novi upstream verzije 2.8.3
[ossec-hids.git] / src / addagent / manage_keys.c
index a46379d..f18f32b 100755 (executable)
@@ -53,6 +53,17 @@ int k_import(char *cmdimport)
 
     char line_read[FILE_SIZE +1];
 
+    #ifdef WIN32
+    int result;
+    int cmdlen;
+    int caclslen;
+    char *comspec;
+    char *cacls;
+    STARTUPINFO si;
+    PROCESS_INFORMATION pi;
+    DWORD exit_code;
+    #endif
+
 
     /* Parsing user argument. */
     if(cmdimport)
@@ -129,8 +140,76 @@ int k_import(char *cmdimport)
                     }
                     fprintf(fp,"%s\n",line_read);
                     fclose(fp);
+
                     #ifndef WIN32
                     chmod(KEYS_FILE, 0440);
+                    #else
+                    /* Get cmd location from environment */
+                    comspec = getenv("COMSPEC");
+                    if (comspec == NULL || strncmp(comspec, "", strlen(comspec) == 0))
+                    {
+                        if(unlink(KEYS_FILE))
+                        {
+                            verbose(AGENT_DELETE_ERROR, KEYS_FILE);
+                        }
+                        ErrorExit(COMPSEC_ERROR);
+                    }
+
+                    /* Build cacls command */
+                    cacls = "echo y|cacls \"%s\" /T /G Administrators:f";
+                    caclslen = strlen(cacls) + strlen(KEYS_FILE);
+                    char caclscmd[caclslen];
+                    snprintf(caclscmd, caclslen, cacls, KEYS_FILE);
+
+                    /* Build final command */
+                    cmdlen = strlen(comspec) + 5 + caclslen;
+                    char cmd[cmdlen];
+                    snprintf(cmd, cmdlen, "%s /c %s", comspec, caclscmd);
+                    
+                     /* Log command being run */
+                     log2file("%s: INFO: Running the following command (%s)", ARGV0, cmd);
+
+                    ZeroMemory(&si, sizeof(si));
+                    si.cb = sizeof(si);
+                    ZeroMemory(&pi, sizeof(pi));
+
+                    if(!CreateProcess(NULL, cmd, NULL, NULL, FALSE, CREATE_NO_WINDOW, NULL, NULL,
+                                      &si, &pi))
+                    {
+                        if(unlink(KEYS_FILE))
+                        {
+                            verbose(AGENT_DELETE_ERROR, KEYS_FILE);
+                        }
+                        ErrorExit(PROC_ERROR, cmd);
+                    }
+
+                    /* Wait until process exits */
+                    WaitForSingleObject(pi.hProcess, INFINITE);
+
+                    /* Get exit code from command */
+                    result = GetExitCodeProcess(pi.hProcess, &exit_code);
+
+                    /* Close process and thread */
+                    CloseHandle(pi.hProcess);
+                    CloseHandle(pi.hThread);
+
+                    if (!result)
+                    {
+                        if(unlink(KEYS_FILE))
+                        {
+                            verbose(AGENT_DELETE_ERROR, KEYS_FILE);
+                        }
+                        ErrorExit(RESULT_ERROR, cmd, GetLastError());
+                    }
+
+                    if (exit_code)
+                    {
+                        if(unlink(KEYS_FILE))
+                        {
+                            verbose(AGENT_DELETE_ERROR, KEYS_FILE);
+                        }
+                        ErrorExit(CACLS_ERROR, cmd, exit_code);
+                    }
                     #endif
 
                     /* Removing sender counter. */
@@ -350,33 +429,35 @@ int k_bulkload(char *cmdbulk)
                        continue;
                }
 
-               do
-               {
-                       /* Default ID */
-                       i = 1024;
-                       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);
-
-                       /* Search for ID KEY  -- no duplicates */
-                       if(IDExist(id))
-                       printf(ADD_ERROR_ID, id);
-
-               } while(IDExist(id) || !OS_IsValidID(id));
+               /* 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);
+               continue;
+               }
+
+               /* Search for ID KEY  -- no duplicates */
+               if(IDExist(id))
+               {
+               printf(NO_DEFAULT, i+1);
+               continue;
+               }
 
                printf(AGENT_INFO, id, name, ip);
                fflush(stdout);