int test4(void) { ConfFile conf_file; if (conf_file_create(&conf_file) < 0) { printf("test4: conf_file_create failed\n"); return -1; } if (conf_file_read(&conf_file, "conf_file_test4_input.conf", NULL, NULL) < 0) { printf("test4: conf_file_read failed\n"); return -1; } if (conf_file_write(&conf_file, "conf_file_test4_output.conf") < 0) { printf("test4: conf_file_write failed\n"); return -1; } if (compare_files("conf_file_test4_reference.conf", "conf_file_test4_output.conf") < 0) { printf("test4: compare_files failed\n"); return -1; } conf_file_destroy(&conf_file); return 0; }
int test1(void) { ConfFile conf_file; if (conf_file_create(&conf_file) < 0) { printf("test1: conf_file_create failed\n"); return -1; } if (conf_file_set_option_value(&conf_file, "#foobar=", "blubb") < 0) { printf("test1: conf_file_set_option_value failed\n"); return -1; } if (conf_file_set_option_value(&conf_file, "\t fo#ob=ar \r", " blubb \n dummy ") < 0) { printf("test1: conf_file_set_option_value failed\n"); return -1; } if (conf_file_write(&conf_file, "conf_file_test1_output.conf") < 0) { printf("test1: conf_file_write failed\n"); return -1; } if (compare_files("conf_file_test1_reference.conf", "conf_file_test1_output.conf") < 0) { printf("test1: compare_files failed\n"); return -1; } conf_file_destroy(&conf_file); return 0; }
static int daemonise(void) { int rc; struct passwd *pp = NULL; struct group *gp = NULL; uid_t uid = 0, euid =0; gid_t gid = 0; /* we need to do these before chroot()ing, because they need ** the /etc/ directory. */ if (conffile.chroot_user) pp = getpwnam(conffile.chroot_user); if (conffile.chroot_group) gp = getgrnam(conffile.chroot_group); if (conffile.chroot_user) { if (!pp) fprintf(stderr, "Could not find user %s\n", conffile.chroot_user ); else { uid = pp->pw_uid; gid = pp->pw_gid; } } if (conffile.chroot_group) { if(!gp) fprintf(stderr, "Could not find group %s\n", conffile.chroot_group); else gid = gp->gr_gid; } if (conffile.chroot_dir) { (void) gethostbyname("localhost") ; rc = chdir(conffile.chroot_dir); if (rc==-1) { rc=errno; fprintf(stderr, "Failed chdir(%s): %d (%s)\n" , conffile.chroot_dir, rc, strerror(rc) ); } rc = chroot(conffile.chroot_dir); if (rc==-1) { rc=errno; fprintf(stderr, "Failed chroot(%s): %d (%s)\n" , conffile.chroot_dir, rc, strerror(rc) ); } if (rc) return rc; } if (gid) rc = setgid(gid); if (rc==-1) { rc=errno; fprintf(stderr, "Failed setgid(%d:%s): %d (%s)\n" , gid, conffile.chroot_group, rc, strerror(rc) ); return rc; } if (uid) rc = setuid(uid); if (rc==-1) { rc=errno; fprintf(stderr, "Failed setuid(%d:%s): %d (%s)\n" , uid, conffile.chroot_user, rc, strerror(rc) ); return rc; } if (conffile.want_fork > 0) { if ((rc=fork())) { fprintf(stderr, "Fork1 = %d\n", rc); _exit(0); } if ((rc=fork())) { fprintf(stderr, "Fork2 = %d\n", rc); _exit(0); } } if (uid && (euid = geteuid()) != uid) { fprintf(stderr, "Failed setuid(%d:%s): euid=%d\n" , uid, conffile.chroot_user, euid ); return -1; } if (conffile.chroot_dir) { if (!uid || !(euid = geteuid()) ) { fprintf(stderr, "Refuse to run as root, uid=%d, euid=%d\n", uid, euid ); conf_file_write("refused.cnf"); return rc; } conf_file_fixup(); } return 0; }
int main(int argc, char *argv[]) { FILE *fp; #if USING_DMALLOC dmalloc_debug(1); #endif /* start the clock (which is used by the Logit fnc) */ (void) refetch_ticker(); GetArgs(argc, argv); if (conf_file_read(confname)) { Logit("Failed to read config file \"%s\"", confname); strcpy(confname, "./nngs.cnf"); conf_file_write(confname); Logit("Created \"%s\"", confname); } Logit("Starting %s (%s %s) From: %s" , conffile.version_string, conffile.compile_date, conffile.compile_time, confname); if (daemonise()) { Logit("Failed to daemonise, giving up"); main_exit(1); } conf_file_write("written.cnf"); signal(SIGTERM, TerminateServer); signal(SIGINT, TerminateServer); #if 0 signal(SIGPIPE, SIG_IGN); #else signal(SIGPIPE, BrokenPipe); #endif signal(SIGCHLD, reapchild); mink_init(); startuptime = time(NULL); srand(startuptime); read_ban_ip_list(); if (!all_the_internets() ) { fprintf(stderr, "Network initialize failed on ports %s.\n" , conffile.server_ports); main_exit(1); } player_high = 0; game_high = 0; bytes_sent = 0; bytes_received = 0; #ifdef SGI /*mallopt(100, 1);*/ /* Turn on malloc(3X) debugging (Irix only) */ #endif command_init(); EmoteInit(conffile.emotes_file); help_init(); /*Logit("commands_init()");*/ commands_init(); /*Logit("channel_init()");*/ channel_init(); /*Logit("player_array_init()");*/ player_array_init(); player_init(); ladder_init(NUM_LADDERS); Ladder9 = ladder_new(LADDERSIZE); Ladder19 = ladder_new(LADDERSIZE); completed_games = 0; num_logins = num_logouts = new_players = 0; num_9 = 0; fp = xyfopen(FILENAME_LADDER9, "r"); if (fp) { num_9 = ladder_load(fp, Ladder9); Logit("%d players loaded from file %s", num_9, filename() ); fclose(fp); } num_19 = 0; fp = xyfopen(FILENAME_LADDER19, "r"); if (fp) { num_19 = ladder_load(fp, Ladder19); Logit("%d players loaded from file %s", num_19, filename() ); fclose(fp); } /* mink_init();*/ if (conffile.admin_name) create_admin_account(conffile.admin_name ); Logit("Server up and running."); main_event_loop(); Logit("Closing down."); net_closeAll(); main_exit(0); return 0; }
int red_extension_save_ethernet_config_to_fs(ExtensionEthernetConfig *ethernet_config) { ConfFile conf_file; ConfFileLine *line; char buffer[1024]; int ret = 0; uint8_t i = 0; // Create file if (conf_file_create(&conf_file) < 0) { log_error("Could not create ethernet conf object: %s (%d)", get_errno_name(errno), errno); return -1; } // Write comment line = array_append(&conf_file.lines); if (line == NULL) { log_error("Could not add comment to ethernet conf file: %s (%d)", get_errno_name(errno), errno); ret = -1; goto cleanup; } line->raw = strdup(EXTENSION_CONFIG_COMMENT); line->name = NULL; line->value = NULL; // Write options snprintf(buffer, sizeof(buffer), "%d", ethernet_config->type); if (conf_file_set_option_value(&conf_file, "type" , buffer) < 0) { log_error("Could not set '%s' option for RS485: %s (%d)", "type", get_errno_name(errno), errno); ret = -1; goto cleanup; } snprintf(buffer, sizeof(buffer), "%02x", (unsigned char)ethernet_config->mac[0]); for (i = 1; i < EXTENSION_ETHERNET_MAC_SIZE; i++) { snprintf(buffer + strlen(buffer), sizeof(buffer)-strlen(buffer), ":%02x", (unsigned char)ethernet_config->mac[i]); } if (conf_file_set_option_value(&conf_file, "mac", buffer) < 0) { log_error("Could not set '%s' option for Ethernet: %s (%d)", "mac", get_errno_name(errno), errno); ret = -1; goto cleanup; } // Write config to filesystem snprintf(buffer, sizeof(buffer), EXTENSION_CONFIG_PATH, ethernet_config->extension); if (conf_file_write(&conf_file, buffer) < 0) { log_error("Could not write config to '%s': %s (%d)", buffer, get_errno_name(errno), errno); ret = -1; goto cleanup; } cleanup: conf_file_destroy(&conf_file); return ret; }
int red_extension_save_rs485_config_to_fs(ExtensionRS485Config *rs485_config) { ConfFile conf_file; ConfFileLine *line; char buffer[1024]; int ret = 0; uint8_t i = 0; // Create file if (conf_file_create(&conf_file) < 0) { log_error("Could not create rs485 conf object: %s (%d)", get_errno_name(errno), errno); return -1; } // Write comment line = array_append(&conf_file.lines); if (line == NULL) { log_error("Could not add comment to RS485 conf file: %s (%d)", get_errno_name(errno), errno); ret = -1; goto cleanup; } line->raw = strdup(EXTENSION_CONFIG_COMMENT); line->name = NULL; line->value = NULL; // Write options snprintf(buffer, sizeof(buffer), "%d", rs485_config->type); if (conf_file_set_option_value(&conf_file, "type" , buffer) < 0) { log_error("Could not set '%s' option for RS485: %s (%d)", "type", get_errno_name(errno), errno); ret = -1; goto cleanup; } snprintf(buffer, sizeof(buffer), "%d", rs485_config->address); if (conf_file_set_option_value(&conf_file, "address" , buffer) < 0) { log_error("Could not set '%s' option for RS485: %s (%d)", "address", get_errno_name(errno), errno); ret = -1; goto cleanup; } snprintf(buffer, sizeof(buffer), "%d", rs485_config->slave_address[0]); for (i = 1; i < rs485_config->slave_num; i++) { if (rs485_config->slave_address[i] == 0) { break; } snprintf(buffer + strlen(buffer), sizeof(buffer)-strlen(buffer), ", %d", rs485_config->slave_address[i]); } if (conf_file_set_option_value(&conf_file, "slave_address" , buffer) < 0) { log_error("Could not set '%s' option for RS485: %s (%d)", "slave_address", get_errno_name(errno), errno); ret = -1; goto cleanup; } snprintf(buffer, sizeof(buffer), "%d", rs485_config->baudrate); if (conf_file_set_option_value(&conf_file, "baudrate" , buffer) < 0) { log_error("Could not set '%s' option for RS485: %s (%d)", "baudrate", get_errno_name(errno), errno); ret = -1; goto cleanup; } snprintf(buffer, sizeof(buffer), "%c", rs485_config->parity); if (conf_file_set_option_value(&conf_file, "parity" , buffer) < 0) { log_error("Could not set '%s' option for RS485: %s (%d)", "parity", get_errno_name(errno), errno); ret = -1; goto cleanup; } snprintf(buffer, sizeof(buffer), "%d", rs485_config->slave_num); if (conf_file_set_option_value(&conf_file, "slave_num" , buffer) < 0) { log_error("Could not set '%s' option for RS485: %s (%d)", "slave_num", get_errno_name(errno), errno); ret = -1; goto cleanup; } snprintf(buffer, sizeof(buffer), "%d", rs485_config->stopbits); if (conf_file_set_option_value(&conf_file, "stopbits" , buffer) < 0) { log_error("Could not set '%s' option for RS485: %s (%d)", "stopbits", get_errno_name(errno), errno); ret = -1; goto cleanup; } // Write config to filesystem snprintf(buffer, sizeof(buffer), EXTENSION_CONFIG_PATH, rs485_config->extension); if (conf_file_write(&conf_file, buffer) < 0) { log_error("Could not write config to '%s': %s (%d)", buffer, get_errno_name(errno), errno); ret = -1; goto cleanup; } cleanup: conf_file_destroy(&conf_file); return ret; }