/* Import a key */ int k_import(char *cmdimport) { FILE *fp; char *user_input; char *b64_dec; char *name; char *ip; char *tmp_key; char line_read[FILE_SIZE +1]; /* Parsing user argument. */ if(cmdimport) { user_input = cmdimport; } else { printf(IMPORT_KEY); user_input = getenv("OSSEC_AGENT_KEY"); if (user_input == NULL) { user_input = read_from_user(); } } /* quit */ if(strcmp(user_input, QUIT) == 0) return(0); b64_dec = decode_base64(user_input); if(b64_dec == NULL) { printf(NO_KEY); printf(PRESS_ENTER); read_from_user(); return(0); } memset(line_read, '\0', FILE_SIZE +1); strncpy(line_read, b64_dec, FILE_SIZE); name = strchr(b64_dec, ' '); if(name && strlen(line_read) < FILE_SIZE) { *name = '\0'; name++; ip = strchr(name, ' '); if(ip) { *ip = '\0'; ip++; tmp_key = strchr(ip, ' '); if(!tmp_key) { printf(NO_KEY); return(0); } *tmp_key = '\0'; printf("\n"); printf(AGENT_INFO, b64_dec, name, ip); while(1) { printf(ADD_CONFIRM); fflush(stdout); user_input = getenv("OSSEC_ACTION_CONFIRMED"); if (user_input == NULL) { user_input = read_from_user(); } if(user_input[0] == 'y' || user_input[0] == 'Y') { fp = fopen(KEYS_FILE,"w"); if(!fp) { ErrorExit(FOPEN_ERROR, ARGV0, KEYS_FILE); } fprintf(fp,"%s\n",line_read); fclose(fp); #ifndef WIN32 chmod(KEYS_FILE, 0440); #endif /* Removing sender counter. */ OS_RemoveCounter("sender"); printf(ADDED); printf(PRESS_ENTER); read_from_user(); restart_necessary = 1; return(1); } else /* if(user_input[0] == 'n' || user_input[0] == 'N') */ { printf("%s", ADD_NOT); return(0); } } } } printf(NO_KEY); printf(PRESS_ENTER); read_from_user(); return(0); }
/* Import a key */ int k_import(const char *cmdimport) { FILE *fp; const char *user_input; char *b64_dec; char *name; char *ip; char *tmp_key; char line_read[FILE_SIZE + 1]; 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); /* Parse user argument */ if (cmdimport) { user_input = cmdimport; } else { printf(IMPORT_KEY); user_input = getenv("OSSEC_AGENT_KEY"); if (user_input == NULL) { user_input = read_from_user(); } } /* Quit */ if (strcmp(user_input, QUIT) == 0) { return (0); } b64_dec = decode_base64(user_input); if (b64_dec == NULL) { printf(NO_KEY); printf(PRESS_ENTER); read_from_user(); return (0); } memset(line_read, '\0', FILE_SIZE + 1); strncpy(line_read, b64_dec, FILE_SIZE); name = strchr(b64_dec, ' '); if (name && strlen(line_read) < FILE_SIZE) { *name = '\0'; name++; ip = strchr(name, ' '); if (ip) { *ip = '\0'; ip++; tmp_key = strchr(ip, ' '); if (!tmp_key) { printf(NO_KEY); free(b64_dec); return (0); } *tmp_key = '\0'; printf("\n"); printf(AGENT_INFO, b64_dec, name, ip); while (1) { printf(ADD_CONFIRM); fflush(stdout); user_input = getenv("OSSEC_ACTION_CONFIRMED"); if (user_input == NULL) { user_input = read_from_user(); } if (user_input[0] == 'y' || user_input[0] == 'Y') { if (mkstemp_ex(tmp_path)) { ErrorExit(MKSTEMP_ERROR, ARGV0, tmp_path, errno, strerror(errno)); } #ifndef WIN32 if (chmod(tmp_path, 0440) == -1) { if (unlink(tmp_path)) { verbose(DELETE_ERROR, ARGV0, tmp_path, errno, strerror(errno)); } ErrorExit(CHMOD_ERROR, ARGV0, tmp_path, errno, strerror(errno)); } #endif fp = fopen(tmp_path, "w"); if (!fp) { if (unlink(tmp_path)) { verbose(DELETE_ERROR, ARGV0, tmp_path, errno, strerror(errno)); } ErrorExit(FOPEN_ERROR, ARGV0, tmp_path, errno, strerror(errno)); } fprintf(fp, "%s\n", line_read); fclose(fp); if (rename_ex(tmp_path, KEYS_FILE)) { if (unlink(tmp_path)) { verbose(DELETE_ERROR, ARGV0, tmp_path, errno, strerror(errno)); } ErrorExit(RENAME_ERROR, ARGV0, tmp_path, KEYS_FILE, errno, strerror(errno)); } /* Remove sender counter */ OS_RemoveCounter("sender"); printf(ADDED); printf(PRESS_ENTER); read_from_user(); restart_necessary = 1; free(b64_dec); return (1); } else { /* if(user_input[0] == 'n' || user_input[0] == 'N') */ printf("%s", ADD_NOT); free(b64_dec); return (0); } } } } printf(NO_KEY); printf(PRESS_ENTER); read_from_user(); free(b64_dec); return (0); }
int OS_RemoveAgent(const char *u_id) { FILE *fp; int id_exist; char *full_name; long fp_seek; size_t fp_read; char *buffer; char buf_curline[OS_BUFFER_SIZE]; struct stat fp_stat; id_exist = IDExist(u_id); if (!id_exist) return 0; full_name = getFullnameById(u_id); fp = fopen(AUTH_FILE, "r"); if (!fp) return 0; chmod(AUTH_FILE, 0440); if (stat(AUTH_FILE, &fp_stat) < 0) { fclose(fp); return 0; } buffer = malloc(fp_stat.st_size + 1); if (!buffer) { fclose(fp); return 0; } fsetpos(fp, &fp_pos); fp_seek = ftell(fp); fseek(fp, 0, SEEK_SET); fp_read = fread(buffer, sizeof(char), fp_seek, fp); if (!fgets(buf_curline, OS_BUFFER_SIZE - 2, fp)) { return 0; } #ifndef REUSE_ID char *ptr_name = strchr(buf_curline, ' '); if (!ptr_name) { free(buffer); fclose(fp); return 0; } ptr_name++; memmove(ptr_name + 1, ptr_name, strlen(ptr_name) + 1); *ptr_name = '!'; size_t curline_len = strlen(buf_curline); memcpy(buffer + fp_read, buf_curline, curline_len); fp_read += curline_len; #endif if (!feof(fp)) fp_read += fread(buffer + fp_read, sizeof(char), fp_stat.st_size, fp); fclose(fp); fp = fopen(AUTH_FILE, "w"); if (!fp) { free(buffer); return 0; } fwrite(buffer, sizeof(char), fp_read, fp); fclose(fp); free(buffer); if (full_name) delete_agentinfo(full_name); /* Remove counter for ID */ OS_RemoveCounter(u_id); OS_RemoveAgentTimestamp(u_id); return 1; }
int remove_agent() { FILE *fp; char *user_input; char u_id[FILE_SIZE + 1]; int id_exist; u_id[FILE_SIZE] = '\0'; if (!print_agents(0, 0, 0)) { printf(NO_AGENT); return (0); } do { printf(REMOVE_ID); fflush(stdout); user_input = getenv("OSSEC_AGENT_ID"); if (user_input == NULL) { user_input = read_from_user(); } else { printf("%s\n", user_input); } if (strcmp(user_input, QUIT) == 0) { return (0); } strncpy(u_id, user_input, FILE_SIZE); id_exist = IDExist(user_input); if (!id_exist) { printf(NO_ID, user_input); /* Exit here if we are using environment variables * and our ID does not exist */ if (getenv("OSSEC_AGENT_ID")) { return (1); } } } while (!id_exist); do { printf(REMOVE_CONFIRM); fflush(stdout); user_input = getenv("OSSEC_ACTION_CONFIRMED"); if (user_input == NULL) { user_input = read_from_user(); } else { printf("%s\n", user_input); } /* If user confirms */ if (user_input[0] == 'y' || user_input[0] == 'Y') { /* Get full agent name */ char *full_name = getFullnameById(u_id); if (!full_name) { printf(NO_ID, u_id); return (1); } fp = fopen(AUTH_FILE, "r+"); if (!fp) { free(full_name); ErrorExit(FOPEN_ERROR, ARGV0, AUTH_FILE, errno, strerror(errno)); } #ifndef WIN32 chmod(AUTH_FILE, 0440); #endif /* Remove the agent, but keep the id */ fsetpos(fp, &fp_pos); fprintf(fp, "%s #*#*#*#*#*#*#*#*#*#*#", u_id); fclose(fp); /* Remove counter for ID */ delete_agentinfo(full_name); OS_RemoveCounter(u_id); free(full_name); full_name = NULL; printf(REMOVE_DONE, u_id); restart_necessary = 1; break; } else { /* if(user_input[0] == 'n' || user_input[0] == 'N') */ printf(REMOVE_NOT); break; } } while (1); return (0); }
/* remove an agent */ int remove_agent() { FILE *fp; char *user_input; char u_id[FILE_SIZE +1]; u_id[FILE_SIZE] = '\0'; if(!print_agents(0, 0, 0)) { printf(NO_AGENT); return(0); } do { printf(REMOVE_ID); fflush(stdout); user_input = getenv("OSPATROL_AGENT_ID"); if (user_input == NULL || !IDExist(user_input)) { user_input = read_from_user(); } if(strcmp(user_input, QUIT) == 0) return(0); strncpy(u_id, user_input, FILE_SIZE); if(!IDExist(user_input)) { printf(NO_ID, user_input); } } while(!IDExist(user_input)); do { printf(REMOVE_CONFIRM); fflush(stdout); user_input = getenv("OSPATROL_ACTION_CONFIRMED"); if (user_input == NULL) { user_input = read_from_user(); } /* If user confirm */ if(user_input[0] == 'y' || user_input[0] == 'Y') { /* Getting full agent name */ char *full_name = getFullnameById(u_id); if(!full_name) { ErrorExit(MEM_ERROR, ARGV0); } fp = fopen(AUTH_FILE, "r+"); if(!fp) { free(full_name); ErrorExit(FOPEN_ERROR, ARGV0, AUTH_FILE); } #ifndef WIN32 chmod(AUTH_FILE, 0440); #endif /* Removing the agent, but keeping the id. */ fsetpos(fp, &fp_pos); fprintf(fp, "%s #*#*#*#*#*#*#*#*#*#*#", u_id); fclose(fp); /* Remove counter for id */ delete_agentinfo(full_name); OS_RemoveCounter(u_id); free(full_name); full_name = NULL; printf(REMOVE_DONE, u_id); restart_necessary = 1; break; } else /* if(user_input[0] == 'n' || user_input[0] == 'N') */ { printf(REMOVE_NOT); break; } } while(1); return(0); }