int CreatePID(char *name, int pid) { char file[256]; FILE *fp; if(isChroot()) { snprintf(file,255,"%s/%s-%d.pid",OS_PIDFILE,name,pid); } else { snprintf(file,255,"%s%s/%s-%d.pid",DEFAULTDIR, OS_PIDFILE,name,pid); } fp = fopen(file,"a"); if(!fp) return(-1); fprintf(fp,"%d\n",pid); fclose(fp); return(0); }
/* Is_Name (is valid name) */ int NameExist(char *u_name) { FILE *fp; char line_read[FILE_SIZE +1]; line_read[FILE_SIZE] = '\0'; if((!u_name)|| (*u_name == '\0')|| (*u_name == '\r')|| (*u_name == '\n')) return(0); if(isChroot()) fp = fopen(AUTH_FILE, "r"); else fp = fopen(KEYSFILE_PATH, "r"); if(!fp) return(0); fseek(fp, 0, SEEK_SET); fgetpos(fp, &fp_pos); while(fgets(line_read, FILE_SIZE-1, fp) != NULL) { char *name; if(line_read[0] == '#') continue; name = strchr(line_read, ' '); if(name) { char *ip; name++; if(*name == '#') { continue; } ip = strchr(name, ' '); if(ip) { *ip = '\0'; if(strcmp(u_name, name) == 0) { fclose(fp); return(1); } } } fgetpos(fp, &fp_pos); } fclose(fp); return(0); }
int DeletePID(const char *name) { char file[256]; if (isChroot()) { snprintf(file, 255, "%s/%s-%d.pid", OS_PIDFILE, name, (int)getpid()); } else { snprintf(file, 255, "%s%s/%s-%d.pid", DEFAULTDIR, OS_PIDFILE, name, (int)getpid()); } if (File_DateofChange(file) < 0) { return (-1); } if (unlink(file)) { log2file( DELETE_ERROR, __local_name, file, errno, strerror(errno) ); } return (0); }
/* Returns the ID of an agent, or NULL if not found */ char *IPExist(const char *u_ip) { FILE *fp; char *name, *ip, *pass; char line_read[FILE_SIZE + 1]; line_read[FILE_SIZE] = '\0'; if (!(u_ip && strncmp(u_ip, "any", 3)) || strchr(u_ip, '/')) return NULL; if (isChroot()) fp = fopen(AUTH_FILE, "r"); else fp = fopen(KEYSFILE_PATH, "r"); if (!fp) return NULL; fseek(fp, 0, SEEK_SET); fgetpos(fp, &fp_pos); while (fgets(line_read, FILE_SIZE - 1, fp) != NULL) { if (line_read[0] == '#') { continue; } name = strchr(line_read, ' '); if (name) { name++; if (*name == '#' || *name == '!') { continue; } ip = strchr(name, ' '); if (ip) { ip++; pass = strchr(ip, ' '); if (pass) { *pass = '******'; if (strcmp(u_ip, ip) == 0) { fclose(fp); name[-1] = '\0'; return strdup(line_read); } } } } fgetpos(fp, &fp_pos); } fclose(fp); return NULL; }
/* ID Search (is valid ID) */ int IDExist(char *id) { FILE *fp; char line_read[FILE_SIZE +1]; line_read[FILE_SIZE] = '\0'; /* ID must not be null */ if(!id) return(0); if(isChroot()) fp = fopen(AUTH_FILE, "r"); else fp = fopen(KEYSFILE_PATH, "r"); if(!fp) return(0); fseek(fp, 0, SEEK_SET); fgetpos(fp, &fp_pos); while(fgets(line_read,FILE_SIZE -1, fp) != NULL) { char *name; if(line_read[0] == '#') { fgetpos(fp, &fp_pos); continue; } name = strchr(line_read, ' '); if(name) { *name = '\0'; name++; if(strcmp(line_read,id) == 0) { fclose(fp); return (1); /*(fp_pos);*/ } } fgetpos(fp, &fp_pos); } fclose(fp); return(0); }
/* Create database for agent from profile. Returns 0 on success or -1 on error. */ int wdb_remove_agent_db(int id) { char path[OS_FLSIZE + 1]; char path_aux[OS_FLSIZE + 1]; char *name = wdb_agent_name(id); if (!name) return -1; snprintf(path, OS_FLSIZE, "%s%s/agents/%03d-%s.db", isChroot() ? "/" : "", WDB_DIR, id, name); free(name); if (!remove(path)) { snprintf(path_aux, OS_FLSIZE, "%s-shm", path); remove(path_aux); snprintf(path_aux, OS_FLSIZE, "%s-wal", path); remove(path_aux); return 0; } else return -1; }
int DeletePID(char *name) { char file[256]; if(isChroot()) { snprintf(file,255,"%s/%s-%d.pid",OS_PIDFILE,name,(int)getpid()); } else { snprintf(file,255,"%s%s/%s-%d.pid",DEFAULTDIR, OS_PIDFILE,name,(int)getpid()); } if(File_DateofChange(file) < 0) return(-1); unlink(file); return(0); }
/* Create database for agent from profile. Returns 0 on success or -1 on error. */ int wdb_create_agent_db(int id, const char *name) { char path[OS_FLSIZE + 1]; char buffer[4096]; FILE *source; FILE *dest; size_t nbytes; int retval = 0; if (!name) return -1; snprintf(path, OS_FLSIZE, "%s/%s", WDB_DIR, WDB_PROF_NAME); if (!(source = fopen(path, "r"))) { debug1("%s: Couldn't open profile '%s'.", ARGV0, path); return -1; } snprintf(path, OS_FLSIZE, "%s%s/agents/%03d-%s.db", isChroot() ? "/" : "", WDB_DIR, id, name); if (!(dest = fopen(path, "w"))) { debug1("%s: Couldn't create database '%s'.", ARGV0, path); return -1; } while (nbytes = fread(buffer, 1, 4096, source), nbytes) { if (fwrite(buffer, 1, nbytes, dest) != nbytes) { retval = -1; break; } } fclose(source); fclose(dest); return retval == 0 ? chmod(path, 0660) : retval; }
/* Read the file and return a string the matches the following * format: high_name.low_name. * If return is not null, value must be freed */ static char *_read_file(const char *high_name, const char *low_name, const char *defines_file) { FILE *fp; char def_file[OS_FLSIZE + 1]; char buf[OS_SIZE_1024 + 1]; char *buf_pt; char *tmp_buffer; char *ret; #ifndef WIN32 if (isChroot()) { snprintf(def_file, OS_FLSIZE, "%s", defines_file); } else { snprintf(def_file, OS_FLSIZE, "%s%s", DEFAULTDIR, defines_file); } #else snprintf(def_file, OS_FLSIZE, "%s", defines_file); #endif fp = fopen(def_file, "r"); if (!fp) { if (strcmp(defines_file, OSSEC_LDEFINES) != 0) { merror(FOPEN_ERROR, __local_name, def_file, errno, strerror(errno)); } return (NULL); } /* Invalid call */ if (!high_name || !low_name) { merror(NULL_ERROR, __local_name); fclose(fp); return (NULL); } /* Read it */ buf[OS_SIZE_1024] = '\0'; while (fgets(buf, OS_SIZE_1024 , fp) != NULL) { /* Commented or blank lines */ if (buf[0] == '#' || buf[0] == ' ' || buf[0] == '\n') { continue; } /* Messages not formatted correctly */ buf_pt = strchr(buf, '.'); if (!buf_pt) { merror(FGETS_ERROR, __local_name, def_file, buf); continue; } /* Check for the high name */ *buf_pt = '\0'; buf_pt++; if (strcmp(buf, high_name) != 0) { continue; } tmp_buffer = buf_pt; /* Get the equal */ buf_pt = strchr(buf_pt, '='); if (!buf_pt) { merror(FGETS_ERROR, __local_name, def_file, buf); continue; } /* Check for the low name */ *buf_pt = '\0'; buf_pt++; if (strcmp(tmp_buffer, low_name) != 0) { continue; } /* Remove newlines or anything that will cause errors */ tmp_buffer = strrchr(buf_pt, '\n'); if (tmp_buffer) { *tmp_buffer = '\0'; } tmp_buffer = strrchr(buf_pt, '\r'); if (tmp_buffer) { *tmp_buffer = '\0'; } os_strdup(buf_pt, ret); fclose(fp); return (ret); } fclose(fp); return (NULL); }