Beispiel #1
0
/**
 *  Check the server for greeting "@RSYNCD: XX, then send this greeting back
 *  to server, send command '#list' to get a listing of modules.
 *
 *  @author Igor Homyakov <*****@*****.**>
 *
 *  @file
 */
int check_rsync(Socket_T s) {
  char  buf[64];
  char  header[10];
  int   rc, version_major, version_minor;
  char  *rsyncd = "@RSYNCD:";
  char  *rsyncd_exit = "@RSYNCD: EXIT";

  ASSERT(s);

  /* Read and check the greeting */
  if (!socket_readln(s, buf, sizeof(buf))) {
    LogError("RSYNC: did not see server greeting  -- %s\n", STRERROR);
    return FALSE;
  }
  Util_chomp(buf);
  rc = sscanf(buf, "%10s %d.%d", header, &version_major, &version_minor);
  if ((rc == EOF) || (rc != 3)) {
    LogError("RSYNC: server greeting parse error %s\n", buf);
    return FALSE;
  }
  if (strncasecmp(header, rsyncd, strlen(rsyncd)) != 0) {
    LogError("RSYNC: server sent unexpected greeting -- %s\n", buf);
    return FALSE;
  }

  /* Send back the greeting */
  if (socket_print(s, "%s\n", buf) <= 0) {
    LogError("RSYNC: identification string send failed -- %s\n", STRERROR);
    return FALSE;
  }

  /* Send #list command */
  if (socket_print(s, "#list\n") < 0) {
    LogError("RSYNC: #list command failed -- %s\n", STRERROR);
    return FALSE;
  }

  /* Read response: discard list output and check that we've received successful exit */
  do {
    if (! socket_readln(s, buf, sizeof(buf))) {
      LogError("RSYNC: error receiving data -- %s\n", STRERROR);
      return FALSE;
    }
    Util_chomp(buf);
  } while (strncasecmp(buf, rsyncd, strlen(rsyncd)));
  if (strncasecmp(buf, rsyncd_exit, strlen(rsyncd_exit)) != 0) {
    LogError("RSYNC: server sent unexpected response -- %s\n", buf);
    return FALSE;
  }

  return TRUE;

}
Beispiel #2
0
/**
 *  Send PING and check for PONG.
 *  If alive return TRUE, else, return FALSE.
 *
 *  @author Debrard Sébastien <*****@*****.**>
 *
 *  @file
 */
int check_clamav(Socket_T s) {

  char buf[STRLEN];
  const char *ok= "PONG";

  ASSERT(s);
  
  if(socket_print(s, "PING\r\n") < 0) {
    LogError("CLAMAV: error sending data -- %s\n", STRERROR);
    return FALSE;
  }

  if(!socket_readln(s, buf, sizeof(buf))) {
    LogError("CLAMAV: error receiving data -- %s\n", STRERROR);
    return FALSE;
  }

  Util_chomp(buf);
  
  if(strncasecmp(buf, ok, strlen(ok)) != 0) {
    LogError("CLAMAV error: %s\n", buf);
    return FALSE;
  }

  return TRUE;
  
}
Beispiel #3
0
/**
 *  Check the server for greeting code '* OK' and then send LOGOUT and
 *  check for code '* BYE'. If alive return TRUE, else, return FALSE.
 *
 *  @author Jan-Henrik Haukeland, <*****@*****.**>
 *
 *  @file
 */
int check_imap(Socket_T s) {

  char buf[STRLEN];
  const char *ok= "* OK";
  const char *bye= "* BYE";

  
  ASSERT(s);
  
  if(!socket_readln(s, buf, sizeof(buf))) {
    LogError("IMAP: error receiving data -- %s\n", STRERROR);
    return FALSE;
  }

  Util_chomp(buf);
  
  if(strncasecmp(buf, ok, strlen(ok)) != 0) {
    LogError("IMAP error: %s\n", buf);
    return FALSE;
  }
  
  if(socket_print(s, "001 LOGOUT\r\n") < 0) {
    LogError("IMAP: error sending data -- %s\n", STRERROR);
    return FALSE;
  }

  if(!socket_readln(s, buf, sizeof(buf))) {
    LogError("IMAP: error receiving data -- %s\n", STRERROR);
    return FALSE;
  }

  Util_chomp(buf);
  
  if(strncasecmp(buf, bye, strlen(bye)) != 0) {
    LogError("IMAP error: %s\n", buf);
    return FALSE;
  }

  return TRUE;
  
}
Beispiel #4
0
static int expect(Socket_T s, int expect, int log) {
  
  int status;
  char buf[STRLEN];

  if(!socket_readln(s, buf, STRLEN)) {
    LogError("LMTP: error receiving data -- %s\n", STRERROR);
    return FALSE;
  }
  
  Util_chomp(buf);
  
  sscanf(buf, "%d%*s", &status);
  if(status != expect) {
    if(log) 
      LogError("LMTP error: %s\n", buf);
    return FALSE;
  }
  
  return TRUE;
  
}
Beispiel #5
0
int CFG_LoadConfig(const char *alternate_config_filename)
{
	FILE *fp;
	const char *fname = rtconfig_filename;
	char string[256];
#ifndef BASIC
	int was_obsolete_dir = FALSE;
#endif

#ifdef SUPPORTS_PLATFORM_CONFIGINIT
	PLATFORM_ConfigInit();
#endif

	/* if alternate config filename is passed then use it */
	if (alternate_config_filename != NULL && *alternate_config_filename > 0) {
		Util_strlcpy(rtconfig_filename, alternate_config_filename, FILENAME_MAX);
	}
	/* else use the default config name under the HOME folder */
	else {
		char *home = getenv("HOME");
		if (home != NULL)
			Util_catpath(rtconfig_filename, home, DEFAULT_CFG_NAME);
		else
			strcpy(rtconfig_filename, DEFAULT_CFG_NAME);
	}

	fp = fopen(fname, "r");
	if (fp == NULL) {
		Log_print("User config file '%s' not found.", rtconfig_filename);

#ifdef SYSTEM_WIDE_CFG_FILE
		/* try system wide config file */
		fname = SYSTEM_WIDE_CFG_FILE;
		Log_print("Trying system wide config file: %s", fname);
		fp = fopen(fname, "r");
#endif
		if (fp == NULL) {
			Log_print("No configuration file found, will create fresh one from scratch:");
			return FALSE;
		}
	}

	if (fgets(string, sizeof(string), fp) != NULL) {
		Log_print("Using Atari800 config file: %s\nCreated by %s", fname, string);
	}

	while (fgets(string, sizeof(string), fp)) {
		char *ptr;
		Util_chomp(string);
		ptr = strchr(string, '=');
		if (ptr != NULL) {
			*ptr++ = '\0';
			Util_trim(string);
			Util_trim(ptr);

			if (SYSROM_ReadConfig(string, ptr)) {
			}
#ifdef BASIC
			else if (strcmp(string, "ATARI_FILES_DIR") == 0
				  || strcmp(string, "SAVED_FILES_DIR") == 0
				  || strcmp(string, "DISK_DIR") == 0 || strcmp(string, "ROM_DIR") == 0
				  || strcmp(string, "EXE_DIR") == 0 || strcmp(string, "STATE_DIR") == 0)
				/* do nothing */;
#else
			else if (strcmp(string, "ATARI_FILES_DIR") == 0) {
				if (UI_n_atari_files_dir >= UI_MAX_DIRECTORIES)
					Log_print("All ATARI_FILES_DIR slots used!");
				else
					Util_strlcpy(UI_atari_files_dir[UI_n_atari_files_dir++], ptr, FILENAME_MAX);
			}
			else if (strcmp(string, "SAVED_FILES_DIR") == 0) {
				if (UI_n_saved_files_dir >= UI_MAX_DIRECTORIES)
					Log_print("All SAVED_FILES_DIR slots used!");
				else
					Util_strlcpy(UI_saved_files_dir[UI_n_saved_files_dir++], ptr, FILENAME_MAX);
			}
			else if (strcmp(string, "DISK_DIR") == 0 || strcmp(string, "ROM_DIR") == 0
				  || strcmp(string, "EXE_DIR") == 0 || strcmp(string, "STATE_DIR") == 0) {
				/* ignore blank and "." values */
				if (ptr[0] != '\0' && (ptr[0] != '.' || ptr[1] != '\0'))
					was_obsolete_dir = TRUE;
			}
#endif
			else if (strcmp(string, "H1_DIR") == 0)
				Util_strlcpy(Devices_atari_h_dir[0], ptr, FILENAME_MAX);
			else if (strcmp(string, "H2_DIR") == 0)
				Util_strlcpy(Devices_atari_h_dir[1], ptr, FILENAME_MAX);
			else if (strcmp(string, "H3_DIR") == 0)
				Util_strlcpy(Devices_atari_h_dir[2], ptr, FILENAME_MAX);
			else if (strcmp(string, "H4_DIR") == 0)
				Util_strlcpy(Devices_atari_h_dir[3], ptr, FILENAME_MAX);
			else if (strcmp(string, "HD_READ_ONLY") == 0)
				Devices_h_read_only = Util_sscandec(ptr);

			else if (strcmp(string, "PRINT_COMMAND") == 0) {
				if (!Devices_SetPrintCommand(ptr))
					Log_print("Unsafe PRINT_COMMAND ignored");
			}

			else if (strcmp(string, "SCREEN_REFRESH_RATIO") == 0)
				Atari800_refresh_rate = Util_sscandec(ptr);
			else if (strcmp(string, "DISABLE_BASIC") == 0)
				Atari800_disable_basic = Util_sscanbool(ptr);

			else if (strcmp(string, "ENABLE_SIO_PATCH") == 0) {
				ESC_enable_sio_patch = Util_sscanbool(ptr);
			}
			else if (strcmp(string, "ENABLE_H_PATCH") == 0) {
				Devices_enable_h_patch = Util_sscanbool(ptr);
			}
			else if (strcmp(string, "ENABLE_P_PATCH") == 0) {
				Devices_enable_p_patch = Util_sscanbool(ptr);
			}
			else if (strcmp(string, "ENABLE_R_PATCH") == 0) {
				Devices_enable_r_patch = Util_sscanbool(ptr);
			}

			else if (strcmp(string, "ENABLE_NEW_POKEY") == 0) {
#ifdef SOUND
				POKEYSND_enable_new_pokey = Util_sscanbool(ptr);
#endif /* SOUND */
			}
			else if (strcmp(string, "STEREO_POKEY") == 0) {
#ifdef STEREO_SOUND
				POKEYSND_stereo_enabled = Util_sscanbool(ptr);
#ifdef SOUND_THIN_API
				Sound_desired.channels = POKEYSND_stereo_enabled ? 2 : 1;
#endif /* SOUND_THIN_API */
#endif /* STEREO_SOUND */
			}
			else if (strcmp(string, "SPEAKER_SOUND") == 0) {
#ifdef CONSOLE_SOUND
				POKEYSND_console_sound_enabled = Util_sscanbool(ptr);
#endif
			}
			else if (strcmp(string, "SERIO_SOUND") == 0) {
#ifdef SERIO_SOUND
				POKEYSND_serio_sound_enabled = Util_sscanbool(ptr);
#endif
			}
			else if (strcmp(string, "MACHINE_TYPE") == 0) {
				if (strcmp(ptr, "Atari 400/800") == 0 ||
				    /* Also recognise legacy values of this parameter */
				    strcmp(ptr, "Atari OS/A") == 0 ||
				    strcmp(ptr, "Atari OS/B") == 0)
					Atari800_machine_type = Atari800_MACHINE_800;
				else if (strcmp(ptr, "Atari XL/XE") == 0)
					Atari800_machine_type = Atari800_MACHINE_XLXE;
				else if (strcmp(ptr, "Atari 5200") == 0)
					Atari800_machine_type = Atari800_MACHINE_5200;
				else
					Log_print("Invalid machine type: %s", ptr);
			}
			else if (strcmp(string, "RAM_SIZE") == 0) {
				if (strcmp(ptr, "320 (RAMBO)") == 0)
					MEMORY_ram_size = MEMORY_RAM_320_RAMBO;
				else if (strcmp(ptr, "320 (COMPY SHOP)") == 0)
					MEMORY_ram_size = MEMORY_RAM_320_COMPY_SHOP;
				else {
					int size = Util_sscandec(ptr);
					if (MEMORY_SizeValid(size))
						MEMORY_ram_size = size;
					else
						Log_print("Invalid RAM size: %s", ptr);
				}
			}
			else if (strcmp(string, "DEFAULT_TV_MODE") == 0) {
				if (strcmp(ptr, "PAL") == 0)
					Atari800_tv_mode = Atari800_TV_PAL;
				else if (strcmp(ptr, "NTSC") == 0)
					Atari800_tv_mode = Atari800_TV_NTSC;
				else
					Log_print("Invalid TV Mode: %s", ptr);
			}
			else if (strcmp(string, "MOSAIC_RAM_NUM_BANKS") == 0) {
				int num = Util_sscandec(ptr);
				if (num >= 0 && num <= 64)
					MEMORY_mosaic_num_banks = num;
				else
					Log_print("Invalid Mosaic RAM number of banks: %s", ptr);
			}
			else if (strcmp(string, "AXLON_RAM_NUM_BANKS") == 0) {
				int num = Util_sscandec(ptr);
				if (num == 0 || num == 8 || num == 16 || num == 32 || num == 64 || num == 128 || num == 256)
					MEMORY_axlon_num_banks = num;
				else
					Log_print("Invalid Mosaic RAM number of banks: %s", ptr);
			}
			else if (strcmp(string, "ENABLE_MAPRAM") == 0)
				MEMORY_enable_mapram = Util_sscanbool(ptr);
			else if (strcmp(string, "BUILTIN_BASIC") == 0)
				Atari800_builtin_basic = Util_sscanbool(ptr);
			else if (strcmp(string, "KEYBOARD_LEDS") == 0)
				Atari800_keyboard_leds = Util_sscanbool(ptr);
			else if (strcmp(string, "F_KEYS") == 0)
				Atari800_f_keys = Util_sscanbool(ptr);
			else if (strcmp(string, "BUILTIN_GAME") == 0)
				Atari800_builtin_game = Util_sscanbool(ptr);
			else if (strcmp(string, "KEYBOARD_DETACHED") == 0)
				Atari800_keyboard_detached = Util_sscanbool(ptr);
			else if (strcmp(string, "1200XL_JUMPER") == 0)
				Atari800_jumper = Util_sscanbool(ptr);
			else if (strcmp(string, "CFG_SAVE_ON_EXIT") == 0) {
				CFG_save_on_exit = Util_sscanbool(ptr);
			}
			/* Add module-specific configurations here */
			else if (PBI_ReadConfig(string,ptr)) {
			}
			else if (CARTRIDGE_ReadConfig(string, ptr)) {
			}
			else if (CASSETTE_ReadConfig(string, ptr)) {
			}
			else if (RTIME_ReadConfig(string, ptr)) {
			}
#ifdef XEP80_EMULATION
			else if (XEP80_ReadConfig(string, ptr)) {
			}
#endif
#ifdef AF80
			else if (AF80_ReadConfig(string,ptr)) {
			}
#endif
#if !defined(BASIC) && !defined(CURSES_BASIC)
			else if (Colours_ReadConfig(string, ptr)) {
			}
			else if (ARTIFACT_ReadConfig(string, ptr)) {
			}
			else if (Screen_ReadConfig(string, ptr)) {
			}
#endif
#ifdef NTSC_FILTER
			else if (FILTER_NTSC_ReadConfig(string, ptr)) {
			}
#endif
#if SUPPORTS_CHANGE_VIDEOMODE
			else if (VIDEOMODE_ReadConfig(string, ptr)) {
			}
#endif
#if defined(SOUND) && defined(SOUND_THIN_API)
			else if (Sound_ReadConfig(string, ptr)) {
			}
#endif /* defined(SOUND) && defined(SOUND_THIN_API) */
			else {
#ifdef SUPPORTS_PLATFORM_CONFIGURE
				if (!PLATFORM_Configure(string, ptr)) {
					Log_print("Unrecognized variable or bad parameters: '%s=%s'", string, ptr);
				}
#else
				Log_print("Unrecognized variable: %s", string);
#endif
			}
		}
		else {
			Log_print("Ignored config line: %s", string);
		}
	}

	fclose(fp);
#ifndef BASIC
	if (was_obsolete_dir) {
		Log_print(
			"DISK_DIR, ROM_DIR, EXE_DIR and STATE_DIR configuration options\n"
			"are no longer supported. Please use ATARI_FILES_DIR\n"
			"and SAVED_FILES_DIR in your Atari800 configuration file.");
	}
#endif
	return TRUE;
}
Beispiel #6
0
/**
 * Pass on to methods in http/cervlet.c to start/stop services
 * @param S A service name as stated in the config file
 * @param action A string describing the action to execute
 * @return FALSE for error, otherwise TRUE
 */
int control_service_daemon(const char *S, const char *action) {
  int rv = FALSE;
  int status, content_length = 0;
  Socket_T s;
  char *auth;
  char buf[STRLEN];
  
  ASSERT(S);
  ASSERT(action);
  
  if (Util_getAction(action) == ACTION_IGNORE) {
    LogError("%s: Cannot %s service '%s' -- invalid action %s\n", prog, action, S, action);
    return FALSE;
  }
  
  s = socket_new(Run.bind_addr ? Run.bind_addr : "localhost", Run.httpdport, SOCKET_TCP, Run.httpdssl, NET_TIMEOUT);
  if (!s) {
    LogError("%s: Cannot connect to the monit daemon. Did you start it with http support?\n", prog);
    return FALSE;
  }

  /* Send request */
  auth = Util_getBasicAuthHeaderMonit();
  if (socket_print(s,
        "POST /%s HTTP/1.0\r\n"
        "Content-Type: application/x-www-form-urlencoded\r\n"
        "Content-Length: %d\r\n"
        "%s"
        "\r\n"
        "action=%s",
        S,
        strlen("action=") + strlen(action),
        auth ? auth : "",
        action) < 0) {
    LogError("%s: Cannot send the command '%s' to the monit daemon -- %s", prog, action ? action : "null", STRERROR);
    goto err1;
  }

  /* Process response */
  if (! socket_readln(s, buf, STRLEN)) {
    LogError("%s: error receiving data -- %s\n", prog, STRERROR);
    goto err1;
  }
  Util_chomp(buf);
  if (! sscanf(buf, "%*s %d", &status)) {
    LogError("%s: cannot parse status in response: %s\n", prog, buf);
    goto err1;
  }
  if (status >= 300) {
    char *message = NULL;

    /* Skip headers */
    while (socket_readln(s, buf, STRLEN)) {
      if (! strncmp(buf, "\r\n", sizeof(buf)))
        break;
      if(Util_startsWith(buf, "Content-Length") && ! sscanf(buf, "%*s%*[: ]%d", &content_length))
        goto err1;
    }
    if (content_length > 0 && content_length < 1024 && socket_readln(s, buf, STRLEN)) {
      char token[] = "</h2>";
      char *p = strstr(buf, token);

      if (strlen(p) <= strlen(token))
        goto err2;
      p += strlen(token);
      message = xcalloc(sizeof(unsigned char), content_length + 1);
      snprintf(message, content_length + 1, "%s", p);
      p = strstr(message, "<p>");
      if (p)
        *p = 0;
    }
err2:
    LogError("%s: action failed -- %s\n", prog, message ? message : "unable to parse response");
    FREE(message);
  } else
    rv = TRUE;
err1:
  FREE(auth);
  socket_free(&s);

  return rv;
}
Beispiel #7
0
/**
 * Pass on to methods in http/cervlet.c to start/stop services
 * @param S A service name as stated in the config file
 * @param action A string describing the action to execute
 * @param attempts Number of attemps (in case of "already action already in progres...")
 * @return a string starting with "ERR" in for error, otherwise return "OK"
 */
char *control_service_daemon_message(const char *S, const char *action, int attempts, int check_exit_status /* ignored */) {
  char *rv = NULL;
  int status, content_length = 0;
  Socket_T s;
  char *auth;
  char buf[STRLEN];
  
  ASSERT(S);
  ASSERT(action);
  
  if (Util_getAction(action) == ACTION_IGNORE) {
    return xstrdup("ERR1: invalid action");
  }

  auth = Util_getBasicAuthHeaderMonit();

retry:
  /* Try with the socket first */
  if (Run.bind_path) {
    s = socket_new(Run.bind_path, 0, SOCKET_TCP, Run.httpdssl, NET_TIMEOUT);
  }

  /* Try network connection */
  if (!s) {
    if(!(s = socket_new(Run.bind_addr ? Run.bind_addr : "localhost", Run.httpdport,
                            SOCKET_TCP, Run.httpdssl, NET_TIMEOUT))) {
        FREE(auth);
        return xstrdup("ERR2: cannot connect to the monit daemon");
    }
  }

  /* Send request */
  if (socket_print(s,
        "POST /%s HTTP/1.0\r\n"
        "Content-Type: application/x-www-form-urlencoded\r\n"
        "Content-Length: %d\r\n"
        "%s"
        "\r\n"
        "action=%s",
        S,
        strlen("action=") + strlen(action),
        auth ? auth : "",
        action) < 0) {
    return xstrdup("ERR3: cannot send the command");
    goto err1;
  }

  /* Process response */
  if (! socket_readln(s, buf, STRLEN)) {
    rv = xstrdup("ERR4: error receiving data");
    goto err1;
  }
  Util_chomp(buf);
  if (! sscanf(buf, "%*s %d", &status)) {
    rv = xstrdup("ERR5: cannot parse status in response");
    goto err1;
  }
  if (status >= 300) {
    /* Skip headers */
    while (socket_readln(s, buf, STRLEN)) {
      if (! strncmp(buf, "\r\n", sizeof(buf)))
        break;
      if(Util_startsWith(buf, "Content-Length") && ! sscanf(buf, "%*s%*[: ]%d", &content_length)) {
        rv = xstrdup("ERR6: error parsing headers");
        goto err1;
      }
    }
    if (content_length > 0 && content_length < 1024 && socket_readln(s, buf, STRLEN)) {
      char token[] = "</h2>";
      char *p = strstr(buf, token);

      if (strlen(p) <= strlen(token)) {
        rv = xstrdup("ERR7: error parsing body");
        goto err1;
      }
      p += strlen(token);
      rv = xcalloc(sizeof(unsigned char), content_length + 1 + 6);
      snprintf(rv, content_length + 1 + 6, "ERR8: %s", p);
      p = strstr(rv, "<p>");
      if (p)
        *p = 0;
      attempts--;
      if ((attempts > 0) && (strstr(rv, "please try again later") != NULL)) {
          socket_free(&s);
          /*LogInfo("%s: retrying %s %s (%s)\n", prog, action, S, rv);*/
          LogInfo("%s: retrying %s %s\n", prog, action, S);
          FREE(rv);
          sleep(1);
          goto retry;
      }
    }
  } else
    rv = xstrdup("OK");
err1:
  FREE(auth);
  socket_free(&s);

  return rv;
}