Ejemplo n.º 1
0
/* Execute /etc/rmt as user USER on remote system HOST using rexec.
   Return a file descriptor of a bidirectional socket for stdin and
   stdout.  If USER is zero, use the current username.

   By default, this code is not used, since it requires that the user
   have a .netrc file in his/her home directory, or that the
   application designer be willing to have rexec prompt for login and
   password info.  This may be unacceptable, and .rhosts files for use
   with rsh are much more common on BSD systems.  */
static int
_rmt_rexec (char *host, char *user)
{
  int saved_stdin = dup (STDIN_FILENO);
  int saved_stdout = dup (STDOUT_FILENO);
  struct servent *rexecserv;
  int result;

  /* When using cpio -o < filename, stdin is no longer the tty.  But the
     rexec subroutine reads the login and the passwd on stdin, to allow
     remote execution of the command.  So, reopen stdin and stdout on
     /dev/tty before the rexec and give them back their original value
     after.  */

  if (! freopen ("/dev/tty", "r", stdin))
    freopen ("/dev/null", "r", stdin);
  if (! freopen ("/dev/tty", "w", stdout))
    freopen ("/dev/null", "w", stdout);

  if (rexecserv = getservbyname ("exec", "tcp"), !rexecserv)
    error (EXIT_ON_EXEC_ERROR, 0, _("exec/tcp: Service not available"));

  result = rexec (&host, rexecserv->s_port, user, 0, rmt_command, 0);
  if (fclose (stdin) == EOF)
    error (0, errno, _("stdin"));
  fdopen (saved_stdin, "r");
  if (fclose (stdout) == EOF)
    error (0, errno, _("stdout"));
  fdopen (saved_stdout, "w");

  return result;
}
Ejemplo n.º 2
0
static int
_rmt_rexec (char *host, char *user)
{
  struct servent *rexecserv;
  int save_stdin = dup (fileno (stdin));
  int save_stdout = dup (fileno (stdout));
  int tape_fd;			/* return value */

  /* When using cpio -o < filename, stdin is no longer the tty.  But the
     rexec subroutine reads the login and the passwd on stdin, to allow
     remote execution of the command.  So, reopen stdin and stdout on
     /dev/tty before the rexec and give them back their original value
     after.  */

  if (freopen ("/dev/tty", "r", stdin) == NULL)
    freopen ("/dev/null", "r", stdin);
  if (freopen ("/dev/tty", "w", stdout) == NULL)
    freopen ("/dev/null", "w", stdout);

  if (rexecserv = getservbyname ("exec", "tcp"), !rexecserv)
    error (EXIT_ON_EXEC_ERROR, 0, _("exec/tcp: Service not available"));

  tape_fd = rexec (&host, rexecserv->s_port, user, NULL,
		   "/etc/rmt", (int *) NULL);
  fclose (stdin);
  fdopen (save_stdin, "r");
  fclose (stdout);
  fdopen (save_stdout, "w");

  return tape_fd;
}
Ejemplo n.º 3
0
int main_loop() 
{	
	if( arm_main_timers() == -1) {
		wrn_print("failed to arm ring timers");
		terminate();
	}

	if(arm_24h_sig() == -1) {                                  
		wrn_print("failed to arm 24h \"killer timer\"");
		terminate();
	}	
	sigset_t set;
	
	if(sigfillset(&set) == -1 ) {
		err_print ("sigwaitinfo");
		return -1;
	}
	siginfo_t info;
	char *custom_dur;

	for( ;; ) {	
		int sig = sigwaitinfo(&set, &info);

		dbg_print("FETCHED %s SIGNAL(%ld)",strsignal(sig),(long)sig);

		if(IS_EXIT_SIG(sig)) {
			dbg_print("%s SIGNAL(%ld) - exit sig; exiting", strsignal(sig),(long)sig);
			exit(EXIT_SUCCESS);
		}
		
		if(sig == SIG_RING_CUSTOM) {
			custom_dur = get_duration(&info);
			ring(custom_dur);
			continue;
		}
		switch(sig) {
		case -1:
			if(errno != EAGAIN) {
				err_print("sigwaitinfo");		
				break;
			}
		case SIG_REXEC:
			rexec();
			break;	

		case SIG_RING_SHORT:
			ring(cmd.short_ring);
			break;
		case SIG_RING_LONG:
			ring(cmd.long_ring);
			break;


		case SIGCHLD:
			zombie_assasin();
			break;
		}
	}	
}
Ejemplo n.º 4
0
int
build(void)
{
	CF cf;
	int afd, tfd;
	int current_mid;
	off_t size;

	current_mid = -1;
	afd = open_archive(O_RDWR);
	fp = fdopen(afd, "r+");
	tfd = tmp();

	SETCF(afd, archive, tfd, tname, RPAD|WPAD);

	/* Read through the archive, creating list of symbols. */
	symcnt = tsymlen = 0;
	pnext = &rhead;
	while(get_arobj(afd)) {
		int new_mid;

		if (!strcmp(chdr.name, RANLIBMAG)) {
			skip_arobj(afd);
			continue;
		}
		new_mid = rexec(afd, tfd);
		if (new_mid != -1) {
			if (current_mid == -1)
				current_mid = new_mid;
			else if (new_mid != current_mid)
				errx(1, "Mixed object format archive: %d / %d", 
					new_mid, current_mid);
		}
		put_arobj(&cf, (struct stat *)NULL);
	}
	*pnext = NULL;

	/* Create the symbol table.  Endianess the same as last mid seen */
	symobj(current_mid);

	/* Copy the saved objects into the archive. */
	size = lseek(tfd, (off_t)0, SEEK_CUR);
	(void)lseek(tfd, (off_t)0, SEEK_SET);
	SETCF(tfd, tname, afd, archive, NOPAD);
	copy_ar(&cf, size);
	(void)ftruncate(afd, lseek(afd, (off_t)0, SEEK_CUR));
	(void)close(tfd);

	/* Set the time. */
	settime(afd);
	close_archive(afd);
	return(0);
}
Ejemplo n.º 5
0
main(int argc,char *argv[])
{
        struct servent *ser;
        int  i,fd2p=0,s;
        char com[128],*rhost[]={"                       "},user[50],password[50];
        char rtnStr[255]="\0";
        FILE *fp;


        if(argc<4)
        {
                printf("LocalHost Message:\n	Usage:./irexec hostname user password command\n");
                exit(1);
        }

        umask(000);

        memset(user,0,sizeof(user));
        memset(password,0,sizeof(password));

        strcpy(*rhost,argv[1]);
        strcpy(user,argv[2]);
        strcpy(password,argv[3]);

        if(argc>199) argc=199;
        for(i=4;i<argc;i++)
        {
                strcat(com,argv[i]);
                strcat(com," ");
        }

        ser=getservbyname("exec","tcp");
        s=rexec(rhost,ser->s_port,user,password,com,&fd2p);
        if(s<0) 
        {
                printf("rexec excute failed!\n");
                exit(1);
        }

        fp=fdopen(s,"r");
        while(!feof(fp))
        {
                while(fgets(rtnStr,sizeof(rtnStr),fp) != NULL)
                {
                        printf("%s",rtnStr);
                }
        }
        fclose(fp);
}
Ejemplo n.º 6
0
static int
_rmt_rexec(const char *host, const char *user)
{
	struct servent *rexecserv;

	_DIAGASSERT(host != NULL);
	/* user may be NULL */

	rexecserv = getservbyname("exec", "tcp");
	if (rexecserv == NULL)
		errx(1, "exec/tcp: service not available.");
	if ((user != NULL) && *user == '\0')
		user = NULL;
	return rexec(&host, rexecserv->s_port, user, NULL,
	    "/etc/rmt", NULL);
}
Ejemplo n.º 7
0
build()
{
	CF cf;
	int afd, tfd;
	off_t size;

	afd = open_archive(O_RDWR);
	fp = fdopen(afd, "r+");
	tfd = tmp();

	SETCF(afd, archive, tfd, tname, RPAD|WPAD);

	/* Read through the archive, creating list of symbols. */
	pnext = &rhead;
	while(get_arobj(afd)) {
		if (!strcmp(chdr.name, RANLIBMAG)) {
			skip_arobj(afd);
			continue;
		}
		rexec(afd, tfd);
		put_arobj(&cf, (struct stat *)NULL);
	}
	*pnext = NULL;

	/* Create the symbol table. */
	symobj();

	/* Copy the saved objects into the archive. */
	size = lseek(tfd, (off_t)0, SEEK_CUR);
	(void)lseek(tfd, (off_t)0, SEEK_SET);
	SETCF(tfd, tname, afd, archive, RPAD|WPAD);
	copy_ar(&cf, size);
	(void)ftruncate(afd, lseek(afd, (off_t)0, SEEK_CUR));
	(void)close(tfd);

	/* Set the time. */
	settime(afd);
	close_archive(afd);
	return(0);
}
Ejemplo n.º 8
0
int P_rexec(char **ahost, u_short inport, char *user,
	    char *passwd, char *cmd, int *fd2p){
  return (rexec(ahost, inport, user, passwd, cmd, fd2p));}
Ejemplo n.º 9
0
Archivo: cron.c Proyecto: 99years/plan9
static int
mklock(char *file)
{
	int fd, try;
	Dir *dir;

	fd = openlock(file);
	if (fd >= 0) {
		/* make it a lock file if it wasn't */
		dir = dirfstat(fd);
		if (dir == nil)
			error("%s vanished: %r", file);
		dir->mode |= DMEXCL;
		dir->qid.type |= QTEXCL;
		dirfwstat(fd, dir);
		free(dir);

		/* reopen in case it wasn't a lock file at last open */
		close(fd);
	}
	for (try = 0; try < 65 && (fd = openlock(file)) < 0; try++)
		sleep(10*1000);
	return fd;
}

void
main(int argc, char *argv[])
{
	Job *j;
	Tm tm;
	Time t;
	ulong now, last;		/* in seconds */
	int i, lock;

	debug = 0;
	ARGBEGIN{
	case 'c':
		createuser();
		exits(0);
	case 'd':
		debug = 1;
		break;
	default:
		usage();
	}ARGEND

	if(debug){
		readalljobs();
		printjobs();
		exits(0);
	}

	initcap();		/* do this early, before cpurc removes it */

	switch(fork()){
	case -1:
		fatal("can't fork: %r");
	case 0:
		break;
	default:
		exits(0);
	}

	/*
	 * it can take a few minutes before the file server notices that
	 * we've rebooted and gives up the lock.
	 */
	lock = mklock("/cron/lock");
	if (lock < 0)
		fatal("cron already running: %r");

	argv0 = "cron";
	srand(getpid()*time(0));
	last = time(0);
	for(;;){
		readalljobs();
		/*
		 * the system's notion of time may have jumped forward or
		 * backward an arbitrary amount since the last call to time().
		 */
		now = time(0);
		/*
		 * if time has jumped backward, just note it and adapt.
		 * if time has jumped forward more than a day,
		 * just execute one day's jobs.
		 */
		if (now < last) {
			clog("time went backward");
			last = now;
		} else if (now - last > Day) {
			clog("time advanced more than a day");
			last = now - Day;
		}
		now = minute(now);
		for(last = minute(last); last <= now; last += Minute){
			tm = *localtime(last);
			t.min = 1ULL << tm.min;
			t.hour = 1 << tm.hour;
			t.wday = 1 << tm.wday;
			t.mday = 1 << tm.mday;
			t.mon =  1 << (tm.mon + 1);
			for(i = 0; i < nuser; i++)
				for(j = users[i].jobs; j; j = j->next)
					if(j->time.min & t.min
					&& j->time.hour & t.hour
					&& j->time.wday & t.wday
					&& j->time.mday & t.mday
					&& j->time.mon & t.mon)
						rexec(&users[i], j);
		}
		seek(lock, 0, 0);
		write(lock, "x", 1);	/* keep the lock alive */
		/*
		 * if we're not at next minute yet, sleep until a second past
		 * (to allow for sleep intervals being approximate),
		 * which synchronises with minute roll-over as a side-effect.
		 */
		sleepuntil(now + Minute + 1);
	}
	/* not reached */
}
Ejemplo n.º 10
0
int
_dorexec(
	const char *host,
	const char *user,
	const char *passwd,
	const char *cmd,
	const char *locale) {

	int ret = 0;
	int fd = 0;
	int fd2 = 1;

	FILE *fderr;
	char *ferr;

	(void) memset(glob_stdout, 0, BUFSIZ);
	(void) memset(glob_stderr, 0, BUFSIZ);

	/*
	 * Re-direct stderr to a file
	 */
	ferr = tempnam(NULL, NULL);
	if (ferr != NULL) {
		fderr = freopen(ferr, "w+", stderr);
	}

	fd = rexec((char **)&host, htons(512), user,
	    passwd, cmd, &fd2);

	if (fd > -1) {
		/*
		 * rexec worked. Clean up stderr file.
		 */
		f_cleanup(fderr, ferr);

		ret = read(fd, glob_stdout, BUFSIZ - 1);
		if (ret < 0) {
			(void) strncpy(glob_stderr, strerror(errno),
			    (BUFSIZ - 1));
			fd_cleanup(fd, fd2);
			return (errno);
		}

		ret = read(fd2, glob_stderr, BUFSIZ - 1);
		if (ret < 0) {
			(void) strncpy(glob_stderr, strerror(errno),
			    (BUFSIZ - 1));
			fd_cleanup(fd, fd2);
			return (errno);
		}
	} else {
		/*
		 * rexec failed. Read from the stderr file.
		 */
		if (fderr != NULL) {
			char tmpbuf[BUFSIZ];

			(void) rewind(fderr);
			strcpy(glob_stderr, "");
			while (fgets(tmpbuf, BUFSIZ - 1,
			    fderr) != NULL) {
				if ((strlen(glob_stderr) +
				    strlen(tmpbuf)) > BUFSIZ - 1) {
					break;
				} else {
					(void) strcat(glob_stderr, tmpbuf);
				}
			}
		}
		f_cleanup(fderr, ferr);
		fd_cleanup(fd, fd2);
		return (1);
	}
	fd_cleanup(fd, fd2);
	return (0);
}