Exemplo n.º 1
0
static bool
get_console_input_systemd(const char *prompt, const bool echo, char *input, const int capacity)
{
    int std_out;
    bool ret = false;
    struct argv argv = argv_new();

    argv_printf(&argv, SYSTEMD_ASK_PASSWORD_PATH);
#ifdef SYSTEMD_NEWER_THAN_216
    /* the --echo support arrived in upstream systemd 217 */
    if (echo)
    {
        argv_printf_cat(&argv, "--echo");
    }
#endif
    argv_printf_cat(&argv, "--icon network-vpn");
    argv_printf_cat(&argv, "%s", prompt);

    if ((std_out = openvpn_popen(&argv, NULL)) < 0)
    {
        return false;
    }
    memset(input, 0, capacity);
    if (read(std_out, input, capacity-1) != 0)
    {
        chomp(input);
        ret = true;
    }
    close(std_out);

    argv_reset(&argv);

    return ret;
}
static bool
get_console_input_systemd (const char *prompt, const bool echo, char *input, const int capacity)
{
  int std_out;
  bool ret = false;
  struct argv argv;

  argv_init (&argv);
  argv_printf (&argv, SYSTEMD_ASK_PASSWORD_PATH);
  argv_printf_cat (&argv, "%s", prompt);

  if ((std_out = openvpn_popen (&argv, NULL)) < 0) {
	  return false;
  }

  memset (input, 0, capacity);
  if (read (std_out, input, capacity-1) > 0)
    {
       chomp (input);
       ret = true;
    }
  close (std_out);

  argv_reset (&argv);

  return ret;
}
Exemplo n.º 3
0
static bool
tls_crypt_v2_verify_metadata(const struct tls_wrap_ctx *ctx,
                             const struct tls_options *opt)
{
    bool ret = false;
    struct gc_arena gc = gc_new();
    const char *tmp_file = NULL;
    struct buffer metadata = ctx->tls_crypt_v2_metadata;
    int metadata_type = buf_read_u8(&metadata);
    if (metadata_type < 0)
    {
        msg(M_WARN, "ERROR: no metadata type");
        goto cleanup;
    }

    tmp_file = platform_create_temp_file(opt->tmp_dir, "tls_crypt_v2_metadata_",
                                         &gc);
    if (!tmp_file || !buffer_write_file(tmp_file, &metadata))
    {
        msg(M_WARN, "ERROR: could not write metadata to file");
        goto cleanup;
    }

    char metadata_type_str[4] = { 0 }; /* Max value: 255 */
    openvpn_snprintf(metadata_type_str, sizeof(metadata_type_str),
                     "%i", metadata_type);
    struct env_set *es = env_set_create(NULL);
    setenv_str(es, "script_type", "tls-crypt-v2-verify");
    setenv_str(es, "metadata_type", metadata_type_str);
    setenv_str(es, "metadata_file", tmp_file);

    struct argv argv = argv_new();
    argv_parse_cmd(&argv, opt->tls_crypt_v2_verify_script);
    argv_msg_prefix(D_TLS_DEBUG, &argv, "Executing tls-crypt-v2-verify");

    ret = openvpn_run_script(&argv, es, 0, "--tls-crypt-v2-verify");

    argv_reset(&argv);
    env_set_destroy(es);

    if (!platform_unlink(tmp_file))
    {
        msg(M_WARN, "WARNING: failed to remove temp file '%s", tmp_file);
    }

    if (ret)
    {
        msg(D_HANDSHAKE, "TLS CRYPT V2 VERIFY SCRIPT OK");
    }
    else
    {
        msg(D_HANDSHAKE, "TLS CRYPT V2 VERIFY SCRIPT ERROR");
    }

cleanup:
    gc_free(&gc);
    return ret;
}
Exemplo n.º 4
0
int
set_lladdr(const char *ifname, const char *lladdr,
           const struct env_set *es)
{
    struct argv argv = argv_new();
    int r;

    if (!ifname || !lladdr)
    {
        return -1;
    }

#if defined(TARGET_LINUX)
#ifdef ENABLE_IPROUTE
    argv_printf(&argv,
                "%s link set addr %s dev %s",
                iproute_path, lladdr, ifname);
#else
    argv_printf(&argv,
                "%s %s hw ether %s",
                IFCONFIG_PATH,
                ifname, lladdr);
#endif
#elif defined(TARGET_SOLARIS)
    argv_printf(&argv,
                "%s %s ether %s",
                IFCONFIG_PATH,
                ifname, lladdr);
#elif defined(TARGET_OPENBSD)
    argv_printf(&argv,
                "%s %s lladdr %s",
                IFCONFIG_PATH,
                ifname, lladdr);
#elif defined(TARGET_DARWIN)
    argv_printf(&argv,
                "%s %s lladdr %s",
                IFCONFIG_PATH,
                ifname, lladdr);
#elif defined(TARGET_FREEBSD)
    argv_printf(&argv,
                "%s %s ether %s",
                IFCONFIG_PATH,
                ifname, lladdr);
#else  /* if defined(TARGET_LINUX) */
    msg(M_WARN, "Sorry, but I don't know how to configure link layer addresses on this operating system.");
    return -1;
#endif /* if defined(TARGET_LINUX) */

    argv_msg(M_INFO, &argv);
    r = openvpn_execve_check(&argv, es, M_WARN, "ERROR: Unable to set link layer address.");
    if (r)
    {
        msg(M_INFO, "TUN/TAP link layer address set to %s", lladdr);
    }

    argv_reset(&argv);
    return r;
}
Exemplo n.º 5
0
void
argv_printf_arglist (struct argv *a, const char *format, const unsigned int flags, va_list arglist)
{
  struct gc_arena gc = gc_new ();
  char *term;
  const char *f = format;

  if (!(flags & APA_CAT))
    argv_reset (a);
  argv_extend (a, 1); /* ensure trailing NULL */

  while ((term = argv_term (&f)) != NULL) 
    {
      if (term[0] == '%')
	{
	  if (!strcmp (term, "%s"))
	    {
	      char *s = va_arg (arglist, char *);
	      if (!s)
		s = "";
	      argv_append (a, string_alloc (s, NULL));
	      argv_system_str_append (a, s, true);
	    }
	  else if (!strcmp (term, "%sc"))
	    {
	      char *s = va_arg (arglist, char *);
	      if (s)
		{
		  int nparms;
		  char *parms[MAX_PARMS+1];
		  int i;

		  nparms = parse_line (s, parms, MAX_PARMS, "SCRIPT-ARGV", 0, D_ARGV_PARSE_CMD, &gc);
		  if (nparms)
		    {
		      for (i = 0; i < nparms; ++i)
			argv_append (a, string_alloc (parms[i], NULL));
		    }
		  else
		    argv_append (a, string_alloc (s, NULL));

		  argv_system_str_append (a, s, false);
		}
	      else
		{
		  argv_append (a, string_alloc ("", NULL));
		  argv_system_str_append (a, "echo", false);
		}
	    }
Exemplo n.º 6
0
static int
plugin_call_item (const struct plugin *p,
		  void *per_client_context,
		  const int type,
		  const struct argv *av,
		  struct openvpn_plugin_string_list **retlist,
		  const char **envp)
{
  int status = OPENVPN_PLUGIN_FUNC_SUCCESS;

  /* clear return list */
  if (retlist)
    *retlist = NULL;

  if (p->plugin_handle && (p->plugin_type_mask & OPENVPN_PLUGIN_MASK (type)))
    {
      struct gc_arena gc = gc_new ();
      struct argv a = argv_insert_head (av, p->so_pathname);

      dmsg (D_PLUGIN_DEBUG, "PLUGIN_CALL: PRE type=%s", plugin_type_name (type));
      plugin_show_args_env (D_PLUGIN_DEBUG, (const char **)a.argv, envp);

      /*
       * Call the plugin work function
       */
      if (p->func2)
	status = (*p->func2)(p->plugin_handle, type, (const char **)a.argv, envp, per_client_context, retlist);
      else if (p->func1)
	status = (*p->func1)(p->plugin_handle, type, (const char **)a.argv, envp);
      else
	ASSERT (0);

      msg (D_PLUGIN, "PLUGIN_CALL: POST %s/%s status=%d",
	   p->so_pathname,
	   plugin_type_name (type),
	   status);

      if (status == OPENVPN_PLUGIN_FUNC_ERROR)
	msg (M_WARN, "PLUGIN_CALL: plugin function %s failed with status %d: %s",
	     plugin_type_name (type),
	     status,
	     p->so_pathname);

      argv_reset (&a);
      gc_free (&gc);
    }
  return status;
}
Exemplo n.º 7
0
static int
plugin_call_item (const struct plugin *p,
		  void *per_client_context,
		  const int type,
		  const struct argv *av,
		  struct openvpn_plugin_string_list **retlist,
		  const char **envp
#ifdef USE_SSL
		  , int certdepth,
		  x509_cert_t *current_cert
#endif
		 )
{
  int status = OPENVPN_PLUGIN_FUNC_SUCCESS;

  /* clear return list */
  if (retlist)
    *retlist = NULL;

  if (p->plugin_handle && (p->plugin_type_mask & OPENVPN_PLUGIN_MASK (type)))
    {
      struct gc_arena gc = gc_new ();
      struct argv a = argv_insert_head (av, p->so_pathname);

      dmsg (D_PLUGIN_DEBUG, "PLUGIN_CALL: PRE type=%s", plugin_type_name (type));
      plugin_show_args_env (D_PLUGIN_DEBUG, (const char **)a.argv, envp);

      /*
       * Call the plugin work function
       */
      if (p->func3) {
        struct openvpn_plugin_args_func_in args = { type,
                                                    (const char ** const) a.argv,
                                                    (const char ** const) envp,
                                                    p->plugin_handle,
                                                    per_client_context,
#ifdef USE_SSL
						    (current_cert ? certdepth : -1),
						    current_cert
#else
						    -1,
						    NULL
#endif
	  };

        struct openvpn_plugin_args_func_return retargs;

        CLEAR(retargs);
        status = (*p->func3)(OPENVPN_PLUGINv3_STRUCTVER, &args, &retargs);
        retlist = retargs.return_list;
      } else if (p->func2)
	status = (*p->func2)(p->plugin_handle, type, (const char **)a.argv, envp, per_client_context, retlist);
      else if (p->func1)
	status = (*p->func1)(p->plugin_handle, type, (const char **)a.argv, envp);
      else
	ASSERT (0);

      msg (D_PLUGIN, "PLUGIN_CALL: POST %s/%s status=%d",
	   p->so_pathname,
	   plugin_type_name (type),
	   status);

      if (status == OPENVPN_PLUGIN_FUNC_ERROR)
	msg (M_WARN, "PLUGIN_CALL: plugin function %s failed with status %d: %s",
	     plugin_type_name (type),
	     status,
	     p->so_pathname);

      argv_reset (&a);
      gc_free (&gc);
    }
  return status;
}
Exemplo n.º 8
0
/*
 * GuizmOVPN_updown (const char *command, const struct plugin_list *plugins, int plugin_type, const char *arg, const char *dev_type, int tun_mtu, int link_mtu, const char *ifconfig_local, const char* ifconfig_remote, const char *context, const char *signal_text, const char *script_type, struct env_set *es) :
 *      Run external script
 */
void GuizmOVPN_updown (const char *command,
                       const struct plugin_list *plugins,
                       int plugin_type,
                       const char *arg,
                       const char *dev_type,
                       int tun_mtu,
                       int link_mtu,
                       const char *ifconfig_local,
                       const char* ifconfig_remote,
                       const char *context,
                       const char *signal_text,
                       const char *script_type,
                       struct env_set *es)
{
    if (signal_text)
        setenv_str (es, "signal", signal_text);
    setenv_str (es, "script_context", context);
    setenv_int (es, "tun_mtu", tun_mtu);
    setenv_int (es, "link_mtu", link_mtu);
    setenv_str (es, "dev", arg);
    
    if (!ifconfig_local)
        ifconfig_local = "";
    if (!ifconfig_remote)
    {
        ifconfig_remote = "";
    }
    
    setenv_str (es, "InfosGateway", tapemu_ip_to_string(tapemu_get_remote_ip()));
    
    if (!context)
        context = "";
    
    static struct argv guizmovpn_argv;
    argv_reset(&guizmovpn_argv);
    guizmovpn_argv = argv_new ();
    
    ASSERT (arg);
    setenv_str (es, "script_type", script_type);
    
    char szTemp[32];
    szTemp[0]='\0';
    GuizmOVPN_ReadPrefs("DNSPush",szTemp);
    if(strcmp(szTemp,"NO") != 0)
    {
        setenv_str (es, "DNSPush", "Y");
    }
    
    GuizmOVPN_ReadPrefs("DNSKeep",szTemp);
    if(strcmp(szTemp,"NO") != 0)
    {
        setenv_str (es, "DNSKeep", "Y");
    }
    
    szTemp[0]='\0';
    GuizmOVPN_ReadPrefs("Multicast",szTemp);
    
    if(dev_type!=NULL && !strcmp(dev_type,"tap") && strcmp(szTemp,"NO") != 0)
    {
        setenv_str (es, "Multicast", "Y");
    }
    
/*    if(client_proxy_infos.active)
    {
        setenv_str (es, "ClientProxyIP", client_proxy_infos.server_ip);
        setenv_int (es, "ClientProxyPort", client_proxy_infos.port);
    }*/
    
    argv_printf (&guizmovpn_argv,
                 "%sc %s %d %d %s %s %s",
                 GUIZMOVPN_COMMAND,
                 arg,
                 tun_mtu, link_mtu,
                 ifconfig_local, ifconfig_remote,
                 context);
    
    openvpn_execve (&guizmovpn_argv, es, 0);
}
Exemplo n.º 9
0
/*
 * Pass tunnel endpoint and MTU parms to a user-supplied script.
 * Used to execute the up/down script/plugins.
 */
void
run_up_down (const char *command,
	     const struct plugin_list *plugins,
	     int plugin_type,
	     const char *arg,
	     const char *dev_type,
	     int tun_mtu,
	     int link_mtu,
	     const char *ifconfig_local,
	     const char* ifconfig_remote,
	     const char *context,
	     const char *signal_text,
	     const char *script_type,
	     struct env_set *es)
{
  struct gc_arena gc = gc_new ();

  if (signal_text)
    setenv_str (es, "signal", signal_text);
  setenv_str (es, "script_context", context);
  setenv_int (es, "tun_mtu", tun_mtu);
  setenv_int (es, "link_mtu", link_mtu);
  setenv_str (es, "dev", arg);
  if (dev_type)
    setenv_str (es, "dev_type", dev_type);

  if (!ifconfig_local)
    ifconfig_local = "";
  if (!ifconfig_remote)
    ifconfig_remote = "";
  if (!context)
    context = "";

  if (plugin_defined (plugins, plugin_type))
    {
      struct argv argv = argv_new ();
      ASSERT (arg);
      argv_printf (&argv,
		   "%s %d %d %s %s %s",
		   arg,
		   tun_mtu, link_mtu,
		   ifconfig_local, ifconfig_remote,
		   context);

      if (plugin_call (plugins, plugin_type, &argv, NULL, es) != OPENVPN_PLUGIN_FUNC_SUCCESS)
	msg (M_FATAL, "ERROR: up/down plugin call failed");

      argv_reset (&argv);
    }

  if (command)
    {
      struct argv argv = argv_new ();
      ASSERT (arg);
      setenv_str (es, "script_type", script_type);
      argv_printf (&argv,
		  "%sc %s %d %d %s %s %s",
		  command,
		  arg,
		  tun_mtu, link_mtu,
		  ifconfig_local, ifconfig_remote,
		  context);
      argv_msg (M_INFO, &argv);
      openvpn_run_script (&argv, es, S_FATAL, "--up/--down");
      argv_reset (&argv);
    }

  gc_free (&gc);
}
Exemplo n.º 10
0
int main(int argc, char *argv[], char *envp[]) 
{
	int fd, i;
	char c;
    char *inputs = calloc(BUFFERSIZE, sizeof(char));
    char *cmd = calloc(BUFFERSIZE, sizeof(char));
    char *path_str = calloc(BUFFERSIZE, sizeof(char));

    // use ctrl+c to interrupt whatever the shell is doing
    signal(SIGINT, SIG_IGN);
	signal(SIGINT, sig_hdlr);

	get_paths(envp);

	if(fork() == 0) {
		execve("/usr/bin/clear", argv, envp);
		exit(1);
	} else {
		wait(NULL);
	}

	// Print the prompt string for the first time
    printf("$ ");
	fflush(stdout);

	// Main loop
    while(c != EOF) {
		c = getchar();
		switch(c) {
			case '\n':
				if(inputs[0] == NULL_SYMBOL) {
					printf("$ ");
				} else {
					// Parse the command line
					argv_parser(inputs);
					strncpy(cmd, parsed_argv[0], strlen(parsed_argv[0]));
					strncat(cmd, "\0", 1);
					if(strchr(cmd, '/') == NULL) {
						// Find the full pathname for the file
						if(attach_path(cmd) == 0) {
							// Create a process to execute the command
							run_cmd(cmd, envp);
						} else {
							printf("%s: command not found\n", cmd);
						}
					} else {
						if((fd = open(cmd, O_RDONLY)) == -1) {
							close(fd);
							run_cmd(cmd, envp);
						} else {
							printf("%s: command not found\n", cmd);
						}
					}
					// Parent waits until child finishes executing command
					// ????????????

					argv_reset();
					// Print the prompt string
					printf("$ ");
					memset(cmd, 0, BUFFERSIZE);
				}
				memset(inputs, 0, BUFFERSIZE);
				break;
			default:
				// Read the command line
				strncat(inputs, &c, 1);
				// printf("%s\n", inputs);
				break;
		}
	}
	free(inputs);
	free(path_str);

	for(i=0; i < BUFFERSIZE; i++)
		free(paths[i]);

	printf("\n");
	return 0;
}