Exemple #1
0
int main(int argc, char *argv[])
{
	const char *root, *cwd, *env, *exe;
	exit_if(argc < 5,
		"Usage: %s /path/to/root /work/directory /env/file /to/exec [args ...]", argv[0]);

	root = argv[1];
	cwd = argv[2];
	env = argv[3];
	exe = argv[4];

	load_env(env);

	pexit_if(chroot(root) == -1, "Chroot \"%s\" failed", root);
	pexit_if(chdir(cwd) == -1, "Chdir \"%s\" failed", cwd);

	/* XXX(vc): note that since execvp() is happening post-chroot, the
	 * app's environment settings correctly affect the PATH search.
	 * This is why execvpe() isn't being used, we manipulate the environment
	 * manually then let it potentially affect execvp().  execvpe() simply
	 * passes the environment to execve() _after_ performing the search, not
	 * what we want here. */
	pexit_if(execvp(exe, &argv[4]) == -1 &&
		 errno != ENOENT && errno != EACCES,
		 "Exec of \"%s\" failed", exe);
	diag(exe);

	return EXIT_FAILURE;
}
Exemple #2
0
static void stat_ip(const char* ip)
{
  time_t now;
  struct stat s;
  int fd;
  if ((fd = open(ip, O_RDONLY)) == -1) {
    if (errno == ENOENT) {
      if (log_ips)
	msg3("IP ", ip, " not found");
    }
    else
      warn3sys("Could not open IP file '", ip, "'");
  }
  else {
    if (fstat(fd, &s) == -1)
      warn1sys("Could not fstat opened IP file?!?");
    else {
      now = time(0);
      if (s.st_mtime + expiry >= now) {
	if (log_ips)
	  msg3("IP ", ip, " found, setting $RELAYCLIENT");
	setenv("RELAYCLIENT", rc, 1);
	load_env(fd);
	close(fd);
      }
      else {
	if (log_ips)
	  msg3("Expired IP ", ip, " found, removing");
	unlink(ip);
      }
    }
  }
}
Exemple #3
0
int mshell_init(struct mshell *mshell, int argc, char **argv, char **env)
{
	memset(mshell, 0, sizeof(struct mshell));
	mshell->user = getpwuid(getuid());
	mshell->main_loop = 1;
	mshell->is_show_cmdline = 1;
	getcwd(mshell->cur_dir, PATH_MAX);
	mshell->fd = 0;
	mshell->pipe = -1;
	mshell->wait_for_childs = 0;

	/*需要执行脚本*/
	if(argc >= 2) {
		mshell->fd = open(argv[1], O_RDONLY);
		if(mshell->fd < 0) {
			mshell->main_loop = 0;
			perror("open");
			return 0;
		}
		mshell->is_show_cmdline = 0;
	}

	signal_init();
	load_env(mshell, env);
	return 0;
}
Exemple #4
0
int main(int argc, char *argv[])
{
	/* We need to keep these env variables since systemd uses them for socket
	 * activation
	 */
	static const char *keep_env[] = {
		"LISTEN_FDS",
		"LISTEN_PID",
		NULL
	};

	const char	*root, *cwd, *env, *uid_str, *gid_str, *exe;
	char		**args;
	uid_t		uid;
	gid_t		*gids;
	size_t		n_gids;

	exit_if(argc < 7,
		"Usage: %s /path/to/root /work/directory /env/file uid gid[,gid...] /to/exec [args ...]", argv[0]);

	root = argv[1];
	cwd = argv[2];
	env = argv[3];
	uid_str = argv[4];
	uid = atoi(uid_str);
	gid_str = argv[5];
	args = &argv[6];
	exe = args[0];

	parse_gids(gid_str, &n_gids, &gids);
	load_env(env, keep_env);

	pexit_if(chroot(root) == -1, "Chroot \"%s\" failed", root);
	pexit_if(chdir(cwd) == -1, "Chdir \"%s\" failed", cwd);
	pexit_if(gids[0] > 0 && setresgid(gids[0], gids[0], gids[0]) == -1,
		"Setresgid \"%s\" failed", gid_str);
	pexit_if(n_gids > 1 && setgroups(n_gids - 1, &gids[1]) == -1,
		"Setgroups \"%s\" failed", gid_str);
	pexit_if(uid > 0 && setresuid(uid, uid, uid) == -1,
		"Setresuid \"%s\" failed", uid_str);

	/* XXX(vc): note that since execvp() is happening post-chroot, the
	 * app's environment settings correctly affect the PATH search.
	 * This is why execvpe() isn't being used, we manipulate the environment
	 * manually then let it potentially affect execvp().  execvpe() simply
	 * passes the environment to execve() _after_ performing the search, not
	 * what we want here. */
	pexit_if(execvp(exe, args) == -1 &&
		 errno != ENOENT && errno != EACCES,
		 "Exec of \"%s\" failed", exe);
	diag(exe);

	return EXIT_FAILURE;
}
Exemple #5
0
Fichier : eo.c Projet : linehan/eo
/* MAIN ***********************************************************************/
int main(int argc, char *argv[]) 
{
        load_env(&ENV);

        if (!ARG(1))
                usage();

        eo_prep(argc, argv);

        return 0;
}
Exemple #6
0
/**
   The runtime entrypoint. The (C ABI) main function generated by rustc calls
   `rust_start`, providing the address of the Rust ABI main function, the
   platform argument vector, and a `crate_map` the provides some logging
   metadata.
*/
extern "C" CDECL int
rust_start(uintptr_t main_fn, int argc, char **argv, void* crate_map) {

#ifndef _WIN32
    pthread_key_create(&sched_key, NULL);
#endif

    // Load runtime configuration options from the environment.
    // FIXME #1497: Should provide a way to get these from the command
    // line as well.
    rust_env *env = load_env(argc, argv);

    global_crate_map = crate_map;

    update_gc_metadata(crate_map);

    update_log_settings(crate_map, env->logspec);

    rust_kernel *kernel = new rust_kernel(env);

    // Create the main task
    rust_sched_id sched_id = kernel->main_sched_id();
    rust_scheduler *sched = kernel->get_scheduler_by_id(sched_id);
    assert(sched != NULL);
    rust_task *root_task = sched->create_task(NULL, "main");

    // Schedule the main Rust task
    root_task->start((spawn_fn)main_fn, NULL, NULL);

    // At this point the task lifecycle is responsible for it
    // and our pointer may not be valid
    root_task = NULL;

    // Run the kernel until all schedulers exit
    int ret = kernel->run();

    delete kernel;
    free_env(env);

    return ret;
}
Exemple #7
0
extern "C" CDECL int
rust_start(uintptr_t main_fn, int argc, char **argv, void* crate_map) {

    rust_env *env = load_env();

    update_log_settings(crate_map, env->logspec);
    check_claims = env->check_claims;

    rust_srv *srv = new rust_srv(env);
    rust_kernel *kernel = new rust_kernel(srv, env->num_sched_threads);
    rust_task *root_task = kernel->create_task(NULL, "main");
    rust_scheduler *sched = root_task->sched;
    command_line_args *args
        = new (kernel, "main command line args")
        command_line_args(root_task, argc, argv);

    DLOG(sched, dom, "startup: %d args in 0x%" PRIxPTR,
             args->argc, (uintptr_t)args->args);
    for (int i = 0; i < args->argc; i++) {
        DLOG(sched, dom, "startup: arg[%d] = '%s'", i, args->argv[i]);
    }

    root_task->start(main_fn, (uintptr_t)args->args);

    int ret = kernel->start_task_threads();
    delete args;
    delete kernel;
    delete srv;

    free_env(env);

#if !defined(__WIN32__)
    // Don't take down the process if the main thread exits without an
    // error.
    if (!ret)
        pthread_exit(NULL);
#endif
    return ret;
}
Exemple #8
0
extern "C" CDECL int
rust_start(uintptr_t main_fn, int argc, char **argv, void* crate_map) {

    rust_env *env = load_env();

    update_log_settings(crate_map, env->logspec);
    check_claims = env->check_claims;

    rust_srv *srv = new rust_srv(env);
    rust_kernel *kernel = new rust_kernel(srv, env->num_sched_threads);
    rust_scheduler *sched = kernel->get_default_scheduler();
    rust_task_id root_id = sched->create_task(NULL, "main", MAIN_STACK_SIZE);
    rust_task *root_task = kernel->get_task_by_id(root_id);
    I(kernel, root_task != NULL);
    rust_task_thread *thread = root_task->thread;
    command_line_args *args
        = new (kernel, "main command line args")
        command_line_args(root_task, argc, argv);

    DLOG(thread, dom, "startup: %d args in 0x%" PRIxPTR,
             args->argc, (uintptr_t)args->args);
    for (int i = 0; i < args->argc; i++) {
        DLOG(thread, dom, "startup: arg[%d] = '%s'", i, args->argv[i]);
    }

    root_task->start((spawn_fn)main_fn, NULL, args->args);
    root_task->deref();
    root_task = NULL;

    int ret = kernel->start_schedulers();
    delete args;
    delete kernel;
    delete srv;

    free_env(env);

    return ret;
}
Exemple #9
0
/* returns	0	on success
 *		-1	on syntax error
 *		-2	on install error
 */
static int
replace_cmd (void)
{
  char n[MAX_FNAME], envstr[MAX_ENVSTR], tn[MAX_FNAME];
  FILE *tmp;
  int ch, eof;
  entry *e;
  char **envp = env_init ();
  int n_header_lines;		/* Number of #CRONTAB header lines we write */

  (void) snprintf (n, MAX_FNAME, "tmp.%d", Pid);
  (void) snprintf (tn, MAX_FNAME, CRON_TAB (n));
  if (!(tmp = fopen (tn, "w+")))
    {
      fprintf (stderr, "Unable to create new crontab file '%s'.\n"
	       "fopen() failed with errno=%s (%d)\n",
	       tn, strerror (errno), errno);
      if (errno == EACCES)
	fprintf (stderr, "In order to have permission to create such files,\n"
		 "Crontab normally is installed setuid either superuser or \n"
		 "a user or group that owns the crontab files and "
		 "directories.\n");
      return (-2);
    }

  write_header_if_wanted (tmp, &n_header_lines);

  /* copy the crontab to the tmp
   */
  rewind (NewCrontab);
  Set_LineNum (1);
  while (EOF != (ch = get_char (NewCrontab)))
    putc (ch, tmp);
  ftruncate (fileno (tmp), ftell (tmp));
  fflush (tmp);
  rewind (tmp);

  if (ferror (tmp))
    {
      fprintf (stderr, "%s: error while writing new crontab to %s\n",
	       ProgramName, tn);
      fclose (tmp);
      unlink (tn);
      return (-2);
    }

  /* check the syntax of the file being installed.
   */

  /* BUG: was reporting errors after the EOF if there were any errors
   * in the file proper -- kludged it by stopping after first error.
   *              vix 31mar87
   */
  Set_LineNum (1 - n_header_lines);
  CheckErrorCount = 0;
  eof = FALSE;
  while (!CheckErrorCount && !eof)
    {
      switch (load_env (envstr, tmp))
	{
	case ERR:
	  eof = TRUE;
	  break;
	case FALSE:
	  e = load_entry (tmp, check_error, pw, envp);
	  if (e)
	    free (e);
	  break;
	case TRUE:
	  break;
	}
    }

  if (CheckErrorCount != 0)
    {
      fprintf (stderr, "errors in crontab file, can't install.\n");
      fclose (tmp);
      unlink (tn);
      return (-1);
    }

/* Why are we doing this?  We just created this file, so if we're privileged
   to do a chown(), the file's owner is already root.  (Maybe on some 
   systems the file doesn't automatically get the creator's effective uid?)
*/
#ifdef HAVE_FCHOWN
  if (fchown (fileno (tmp), ROOT_UID, -1) < OK)
#else
  if (chown (tn, ROOT_UID, -1) < OK)
#endif
    {
      if (errno == EPERM)
	{
	  /* This just means we aren't running as superuser.  We already
	     passed the test of having enough privilege to create the 
	     crontab file in the crontab directory, so the fact that we
	     aren't superuser just means the system administrator wants
	     the crontabs owned by the owner of Crontab, not root.  So
	     let it be.
	   */
	}
      else
	{
	  perror ("chown");
	  fclose (tmp);
	  unlink (tn);
	  return (-2);
	}
    }

#ifdef HAVE_FCHMOD
  if (fchmod (fileno (tmp), 0600) < OK)
#else
  if (chmod (tn, 0600) < OK)
#endif
    {
      perror ("chmod");
      fclose (tmp);
      unlink (tn);
      return (-2);
    }

  if (fclose (tmp) == EOF)
    {
      perror ("fclose");
      unlink (tn);
      return (-2);
    }

  (void) snprintf (n, sizeof (n), CRON_TAB (User));
  if (rename (tn, n))
    {
      fprintf (stderr, "%s: error renaming %s to %s\n", ProgramName, tn, n);
      perror ("rename");
      unlink (tn);
      return (-2);
    }
  log_it (RealUser, Pid, "REPLACE", User);

  poke_daemon ();

  return (0);
}
Exemple #10
0
user *
load_user(int crontab_fd, struct passwd	*pw, const char *name) {
	char envstr[MAX_ENVSTR];
	FILE *file;
	user *u;
	entry *e;
	int status, save_errno;
	char **envp, **tenvp;

	if (!(file = fdopen(crontab_fd, "r"))) {
		perror("fdopen on crontab_fd in load_user");
		return (NULL);
	}

	Debug(DPARS, ("load_user()\n"))

	/* file is open.  build user entry, then read the crontab file.
	 */
	if ((u = (user *) malloc(sizeof(user))) == NULL)
		return (NULL);
	if ((u->name = strdup(name)) == NULL) {
		save_errno = errno;
		free(u);
		errno = save_errno;
		return (NULL);
	}
	u->crontab = NULL;

	/* init environment.  this will be copied/augmented for each entry.
	 */
	if ((envp = env_init()) == NULL) {
		save_errno = errno;
		free(u->name);
		free(u);
		errno = save_errno;
		return (NULL);
	}

	/* load the crontab
	 */
	while ((status = load_env(envstr, file)) >= OK) {
		switch (status) {
		case ERR:
			free_user(u);
			u = NULL;
			goto done;
		case FALSE:
			e = load_entry(file, NULL, pw, envp);
			if (e) {
				e->next = u->crontab;
				u->crontab = e;
			}
			break;
		case TRUE:
			if ((tenvp = env_set(envp, envstr)) == NULL) {
				save_errno = errno;
				free_user(u);
				u = NULL;
				errno = save_errno;
				goto done;
			}
			envp = tenvp;
			break;
		}
	}

 done:
	env_free(envp);
	fclose(file);
	Debug(DPARS, ("...load_user() done\n"))
	return (u);
}
Exemple #11
0
/* returns	0	on success
 *		-1	on syntax error
 *		-2	on install error
 */
static int
replace_cmd(void) {
	char n[MAX_FNAME], n2[MAX_FNAME], envstr[MAX_ENVSTR], lastch;
	FILE *tmp, *fmaxtabsize;
	int ch, eof, fd;
	int error = 0;
	entry *e;
	sig_t oint, oabrt, oquit, ohup;
	uid_t file_owner;
	time_t now = time(NULL);
	char **envp = env_init();
	size_t	maxtabsize;
	struct	stat statbuf;

	if (envp == NULL) {
		warn("Cannot allocate memory.");
		return (-2);
	}

	if (!glue_strings(TempFilename, sizeof TempFilename, SPOOL_DIR,
	    "tmp.XXXXXXXXXX", '/')) {
		TempFilename[0] = '\0';
		warnx("path too long");
		return (-2);
	}
	if ((fd = mkstemp(TempFilename)) == -1 || !(tmp = fdopen(fd, "w+"))) {
		warn("cannot create `%s'", TempFilename);
		if (fd != -1) {
			(void)close(fd);
			(void)unlink(TempFilename);
		}
		TempFilename[0] = '\0';
		return (-2);
	}

	ohup = signal(SIGHUP, SIG_IGN);
	oint = signal(SIGINT, SIG_IGN);
	oquit = signal(SIGQUIT, SIG_IGN);
	oabrt = signal(SIGABRT, SIG_IGN);

	/* Make sure that the crontab is not an unreasonable size.
	 *
	 * XXX This is subject to a race condition--the user could
	 * add stuff to the file after we've checked the size but
	 * before we slurp it in and write it out. We can't just move
	 * the test to test the temp file we later create, because by
	 * that time we've already filled up the crontab disk. Probably
	 * the right thing to do is to do a bytecount in the copy loop
	 * rather than stating the file we're about to read.
	 */
	(void)snprintf(n2, sizeof(n2), "%s/%s", CRONDIR, MAXTABSIZE_FILE);
	if ((fmaxtabsize = fopen(n2, "r")) != NULL)  {
	    if (fgets(n2, (int)sizeof(n2), fmaxtabsize) == NULL)  {
		maxtabsize = 0;
	    } else {
		maxtabsize = atoi(n2);
	    }
	    (void)fclose(fmaxtabsize);
	} else {
	    maxtabsize = MAXTABSIZE_DEFAULT;
	}

	if (fstat(fileno(NewCrontab), &statbuf))  {
	    warn("error stat'ing crontab input");
	    error = -2;
	    goto done;
	}
	if ((uintmax_t)statbuf.st_size > (uintmax_t)maxtabsize)  {
	    warnx("%ld bytes is larger than the maximum size of %ld bytes",
		(long) statbuf.st_size, (long) maxtabsize);
	    error = -2;
	    goto done;
	}

	/* write a signature at the top of the file.
	 *
	 * VERY IMPORTANT: make sure NHEADER_LINES agrees with this code.
	 */
	(void)fprintf(tmp, "# DO NOT EDIT THIS FILE - edit the master and reinstall.\n");
	(void)fprintf(tmp, "# (%s installed on %-24.24s)\n", Filename, ctime(&now));
	(void)fprintf(tmp, "# (Cron version %s -- %s)\n", CRON_VERSION, "$NetBSD: crontab.c,v 1.3.8.1 2012/03/07 23:41:17 riz Exp $");

	/* copy the crontab to the tmp
	 */
	(void)rewind(NewCrontab);
	Set_LineNum(1);
	lastch = EOF;
	while (EOF != (ch = get_char(NewCrontab)))
		(void)putc(lastch = ch, tmp);

	if (lastch != (char)EOF && lastch != '\n') {
		warnx("missing trailing newline in `%s'", Filename);
		error = -1;
		goto done;
	}

	if (ferror(NewCrontab)) {
		warn("error while reading `%s'", Filename);
		error = -2;
		goto done;
	}

	(void)ftruncate(fileno(tmp), ftell(tmp));
	/* XXX this should be a NOOP - is */
	(void)fflush(tmp);

	if (ferror(tmp)) {
		(void)fclose(tmp);
		warn("error while writing new crontab to `%s'", TempFilename);
		error = -2;
		goto done;
	}

	/* check the syntax of the file being installed.
	 */

	/* BUG: was reporting errors after the EOF if there were any errors
	 * in the file proper -- kludged it by stopping after first error.
	 *		vix 31mar87
	 */
	Set_LineNum(1 - NHEADER_LINES);
	CheckErrorCount = 0;  eof = FALSE;
	while (!CheckErrorCount && !eof) {
		switch (load_env(envstr, tmp)) {
		case ERR:
			/* check for data before the EOF */
			if (envstr[0] != '\0') {
				Set_LineNum(LineNumber + 1);
				check_error("premature EOF");
			}
			eof = TRUE;
			break;
		case FALSE:
			e = load_entry(tmp, check_error, pw, envp);
			if (e)
				free(e);
			break;
		case TRUE:
			break;
		}
	}

	if (CheckErrorCount != 0) {
		warnx("errors in crontab file, can't install.");
		(void)fclose(tmp);
		error = -1;
		goto done;
	}

	file_owner = (getgid() == getegid()) ? ROOT_UID : pw->pw_uid;

#ifdef HAVE_FCHOWN
	error = fchown(fileno(tmp), file_owner, (uid_t)-1);
#else
	error = chown(TempFilename, file_owner, (gid_t)-1);
#endif
	if (error < OK) {
		warn("cannot chown `%s'", TempFilename);
		(void)fclose(tmp);
		error = -2;
		goto done;
	}

	if (fclose(tmp) == EOF) {
		warn("error closing file");
		error = -2;
		goto done;
	}

	if (!glue_strings(n, sizeof n, SPOOL_DIR, User, '/')) {
		warnx("path too long");
		error = -2;
		goto done;
	}
	if (rename(TempFilename, n)) {
		warn("error renaming `%s' to `%s'", TempFilename, n);
		error = -2;
		goto done;
	}
	TempFilename[0] = '\0';
	log_it(RealUser, Pid, "REPLACE", User);

	poke_daemon();

done:
	(void)signal(SIGHUP, ohup);
	(void)signal(SIGINT, oint);
	(void)signal(SIGQUIT, oquit);
	(void)signal(SIGABRT, oabrt);
	if (TempFilename[0]) {
		(void) unlink(TempFilename);
		TempFilename[0] = '\0';
	}
	return (error);
}