xref: /netbsd-src/libexec/rexecd/unit-tests/rexec.c (revision ce099b40997c43048fb78bd578195f81d2456523)
1*ce099b40Smartin /*	$NetBSD: rexec.c,v 1.4 2008/04/28 20:23:04 martin Exp $	*/
281e0d2b0Schristos 
381e0d2b0Schristos /*-
481e0d2b0Schristos  * Copyright (c) 2005 The NetBSD Foundation, Inc.
581e0d2b0Schristos  * All rights reserved.
681e0d2b0Schristos  *
781e0d2b0Schristos  * This code is derived from software contributed to The NetBSD Foundation
881e0d2b0Schristos  * by Christos Zoulas.
981e0d2b0Schristos  *
1081e0d2b0Schristos  * Redistribution and use in source and binary forms, with or without
1181e0d2b0Schristos  * modification, are permitted provided that the following conditions
1281e0d2b0Schristos  * are met:
1381e0d2b0Schristos  * 1. Redistributions of source code must retain the above copyright
1481e0d2b0Schristos  *    notice, this list of conditions and the following disclaimer.
1581e0d2b0Schristos  * 2. Redistributions in binary form must reproduce the above copyright
1681e0d2b0Schristos  *    notice, this list of conditions and the following disclaimer in the
1781e0d2b0Schristos  *    documentation and/or other materials provided with the distribution.
1881e0d2b0Schristos  *
1981e0d2b0Schristos  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2081e0d2b0Schristos  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2181e0d2b0Schristos  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2281e0d2b0Schristos  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2381e0d2b0Schristos  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2481e0d2b0Schristos  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2581e0d2b0Schristos  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2681e0d2b0Schristos  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2781e0d2b0Schristos  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2881e0d2b0Schristos  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2981e0d2b0Schristos  * POSSIBILITY OF SUCH DAMAGE.
3081e0d2b0Schristos  */
3181e0d2b0Schristos #include <sys/cdefs.h>
32*ce099b40Smartin __RCSID("$NetBSD: rexec.c,v 1.4 2008/04/28 20:23:04 martin Exp $");
3381e0d2b0Schristos 
3481e0d2b0Schristos #include <stdio.h>
3581e0d2b0Schristos #include <netdb.h>
3681e0d2b0Schristos #include <stdlib.h>
3781e0d2b0Schristos #include <unistd.h>
3881e0d2b0Schristos #include <err.h>
3981e0d2b0Schristos 
408b0f9554Sperry static void usage(void) __dead;
4181e0d2b0Schristos /*XXX*/
4281e0d2b0Schristos int rexec(char **, int, char *, char *, char *, int *);
4381e0d2b0Schristos 
4481e0d2b0Schristos int
main(int argc,char ** argv)4581e0d2b0Schristos main(int argc, char **argv)
4681e0d2b0Schristos {
47aa73cae1Schristos 	int c, s;
48aa73cae1Schristos 	ssize_t nr;
4981e0d2b0Schristos 	struct servent *sv;
5081e0d2b0Schristos 	char *host = __UNCONST("localhost");
5181e0d2b0Schristos 	char *user = __UNCONST("root");
5281e0d2b0Schristos 	char *pass = __UNCONST("h@x0R");
5381e0d2b0Schristos 	char *cmd  = __UNCONST("ls");
54aa73cae1Schristos 	char line[BUFSIZ];
5581e0d2b0Schristos 
5681e0d2b0Schristos 	while ((c = getopt(argc, argv, "h:u:p:c:")) != -1)
5781e0d2b0Schristos 		switch (c) {
5881e0d2b0Schristos 		case 'h':
5981e0d2b0Schristos 			host = optarg;
6081e0d2b0Schristos 			break;
6181e0d2b0Schristos 		case 'u':
6281e0d2b0Schristos 			user = optarg;
6381e0d2b0Schristos 			break;
6481e0d2b0Schristos 		case 'p':
6581e0d2b0Schristos 			pass = optarg;
6681e0d2b0Schristos 			break;
6781e0d2b0Schristos 		case 'c':
6881e0d2b0Schristos 			cmd = optarg;
6981e0d2b0Schristos 			break;
7081e0d2b0Schristos 		default:
7181e0d2b0Schristos 			usage();
7281e0d2b0Schristos 		}
7381e0d2b0Schristos 
7481e0d2b0Schristos 	if ((sv = getservbyname("exec", "tcp")) == NULL)
7581e0d2b0Schristos 		errx(1, "Cannot find service exec/tcp");
7681e0d2b0Schristos 
77aa73cae1Schristos 	if ((s = rexec(&host, sv->s_port, user, pass, cmd, NULL)) == -1)
7881e0d2b0Schristos 		return 1;
79aa73cae1Schristos 	while ((nr = read(s, line, sizeof(line))) > 0)
80aa73cae1Schristos 		(void)write(STDOUT_FILENO, line, nr);
81aa73cae1Schristos 	if (nr == -1)
82aa73cae1Schristos 		err(1, "read failed");
8381e0d2b0Schristos 	return 0;
8481e0d2b0Schristos }
8581e0d2b0Schristos 
8681e0d2b0Schristos static void
usage(void)8781e0d2b0Schristos usage(void)
8881e0d2b0Schristos {
8981e0d2b0Schristos 	(void)fprintf(stderr, "Usage: %s [-u <user>] [-p <password>]"
9081e0d2b0Schristos 	    "[-h <hostname>] [-c <cmd>]\n", getprogname());
9181e0d2b0Schristos 	exit(1);
9281e0d2b0Schristos }
93