File:  [ELWIX - Embedded LightWeight unIX -] / ansh / src / anshd.c
Revision 1.3: download - view: text, annotated - select for diffs - revision graph
Sun Jul 22 22:41:33 2012 UTC (11 years, 11 months ago) by misho
Branches: MAIN
CVS tags: ansh2_1, ansh2_0, ansh1_3, HEAD, ANSH2_0, ANSH1_3, ANSH1_2
version 1.2

    1: /*************************************************************************
    2:  * (C) 2011 AITNET - Sofia/Bulgaria - <office@aitnet.org>
    3:  *  by Michael Pounov <misho@elwix.org>
    4:  *
    5:  * $Author: misho $
    6:  * $Id: anshd.c,v 1.3 2012/07/22 22:41:33 misho Exp $
    7:  *
    8:  *************************************************************************
    9: The ELWIX and AITNET software is distributed under the following
   10: terms:
   11: 
   12: All of the documentation and software included in the ELWIX and AITNET
   13: Releases is copyrighted by ELWIX - Sofia/Bulgaria <info@elwix.org>
   14: 
   15: Copyright 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011, 2012
   16: 	by Michael Pounov <misho@elwix.org>.  All rights reserved.
   17: 
   18: Redistribution and use in source and binary forms, with or without
   19: modification, are permitted provided that the following conditions
   20: are met:
   21: 1. Redistributions of source code must retain the above copyright
   22:    notice, this list of conditions and the following disclaimer.
   23: 2. Redistributions in binary form must reproduce the above copyright
   24:    notice, this list of conditions and the following disclaimer in the
   25:    documentation and/or other materials provided with the distribution.
   26: 3. All advertising materials mentioning features or use of this software
   27:    must display the following acknowledgement:
   28: This product includes software developed by Michael Pounov <misho@elwix.org>
   29: ELWIX - Embedded LightWeight unIX and its contributors.
   30: 4. Neither the name of AITNET nor the names of its contributors
   31:    may be used to endorse or promote products derived from this software
   32:    without specific prior written permission.
   33: 
   34: THIS SOFTWARE IS PROVIDED BY AITNET AND CONTRIBUTORS ``AS IS'' AND
   35: ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
   36: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
   37: ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
   38: FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
   39: DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
   40: OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
   41: HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
   42: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
   43: OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
   44: SUCH DAMAGE.
   45: */
   46: #include "global.h"
   47: #include "anshd.h"
   48: #include "proc.h"
   49: 
   50: 
   51: intptr_t Kill;
   52: int Verbose;
   53: u_int Crypted = 1;
   54: proc_head_t pH;
   55: int bpfLEN, Timeout = 300, Daemon = 1;
   56: char Key[STRSIZ];
   57: 
   58: static sched_root_task_t *root = NULL;
   59: static struct tagProc *proc;
   60: 
   61: extern char compiled[], compiledby[], compilehost[];
   62: 
   63: static void
   64: Usage()
   65: {
   66: 	printf(	" -= anshd =- ELWIX Layer2 remote management service\n"
   67: 		"=== %s === %s@%s ===\n\n"
   68: 		" Syntax: anshd [options]\n\n"
   69: 		"\t-d <dev>\tBind to host interface, like 'em0' (default is first host interface)\n"
   70: 		"\t-U <user>\tRun service with other user\n"
   71: 		"\t-C <dir>\tRun service into chroot directory\n"
   72: 		"\t-k <key>\tService cipher key\n"
   73: 		"\t-t <timeout>\tTimeout of login if no activity (default is 300 sec)\n"
   74: 		"\t-u\t\tSwitch to unencrypted traffic between hosts\n"
   75: 		"\t-b\t\tRun into batch mode (default is daemon mode)\n"
   76: 		"\t-v\t\tVerbose (more -v, more verbosity ...)\n"
   77: 		"\t-h\t\tThis help screen!\n"
   78: 		"\n", compiled, compiledby, compilehost);
   79: }
   80: 
   81: static void
   82: sig(int s)
   83: {
   84: 	int state;
   85: 	pid_t pid;
   86: 
   87: 	switch (s) {
   88: 		case SIGHUP:
   89: 			VERB(1) LOG("Got SIGHUP!\n");
   90: 			break;
   91: 		case SIGTERM:
   92: 			Kill++;
   93: 			VERB(1) LOG("Got SIGTERM!\n");
   94: 			break;
   95: 		case SIGPIPE:
   96: 			VERB(1) LOG("Got SIGPIPE!\n");
   97: 			break;
   98: 		case SIGCHLD:
   99: 			VERB(1) LOG("Got SIGCHLD!\n");
  100: 			while ((pid = waitpid(-1, &state, WNOHANG)) > 0)
  101: 				stopProcess(root, &pH, pid, pktTx);
  102: 			break;
  103: 	}
  104: }
  105: 
  106: static void *
  107: hook_error(void *root, void *arg)
  108: {
  109: /*	sched_root_task_t *r = root; */
  110: 
  111: 	if (!root)
  112: 		return (void*) -1;
  113: 
  114: 	if (arg == (void*) EINTR)
  115: 		return (void*) -1;
  116: 
  117: 	return NULL;
  118: }
  119: 
  120: int
  121: main(int argc, char **argv)
  122: {
  123: 	struct passwd *pass;
  124: 	int fd, h = 0, uid = 0, gid = 0;
  125: 	char ch, szUser[STRSIZ] = "root", szChroot[STRSIZ] = "/", szDev[STRSIZ] = { 0 };
  126: 	struct sigaction sact;
  127: 
  128: 	Get1stEth(szDev, STRSIZ);
  129: 
  130: 	strlcpy(Key, DEFAULT_KEY, sizeof Key);
  131: 
  132: 	while ((ch = getopt(argc, argv, "hvubt:d:U:C:k:")) != -1)
  133: 		switch (ch) {
  134: 			case 'U':
  135: 				pass = getpwnam(optarg);
  136: 				if (!pass) {
  137: 					printf("Error:: User %s not found!\n", optarg);
  138: 					return 1;
  139: 				} else {
  140: 					strlcpy(szUser, optarg, sizeof szUser);
  141: 					uid = pass->pw_uid;
  142: 					gid = pass->pw_gid;
  143: 				}
  144: 				endpwent();
  145: 				break;
  146: 			case 'C':
  147: 				if (access(optarg, R_OK)) {
  148: 					printf("Error:: in chroot %s #%d - %s\n", optarg, errno, strerror(errno));
  149: 					return 1;
  150: 				} else
  151: 					strlcpy(szChroot, optarg, sizeof szChroot);
  152: 				break;
  153: 			case 't':
  154: 				Timeout = abs(strtol(optarg, NULL, 0));
  155: 				break;
  156: 			case 'd':
  157: 				strlcpy(szDev, optarg, sizeof szDev);
  158: 				break;
  159: 			case 'k':
  160: 				strlcpy(Key, optarg, sizeof Key);
  161: 				break;
  162: 			case 'u':
  163: 				Crypted ^= Crypted;
  164: 				break;
  165: 			case 'b':
  166: 				Daemon ^= Daemon;
  167: 				break;
  168: 			case 'v':
  169: 				Verbose++;
  170: 				break;
  171: 			case 'h':
  172: 			default:
  173: 				Usage();
  174: 				return 1;
  175: 		}
  176: 	argc -= optind;
  177: 	argv += optind;
  178: 
  179: 	/* catch signals */
  180: 	memset(&sact, 0, sizeof sact);
  181: 	sigemptyset(&sact.sa_mask);
  182: 	sact.sa_handler = sig;
  183: 	sigaction(SIGPIPE, &sact, NULL);
  184: 	sigaction(SIGCHLD, &sact, NULL);
  185: 	sigaction(SIGTERM, &sact, NULL);
  186: 	sigaction(SIGHUP, &sact, NULL);
  187: 
  188: 	openlog("anshd", LOG_CONS | LOG_PID, LOG_DAEMON);
  189: 
  190: 	if (Daemon) {
  191: 		switch (fork()) {
  192: 			case -1:
  193: 				ERR("Daemon mode #%d - %s\n", errno, strerror(errno));
  194: 				closelog();
  195: 				return 1;
  196: 			case 0:
  197: 				VERB(1) LOG("Welcome to dark ...\n");
  198: 
  199: 				setsid();
  200: 
  201: 				fd = open("/dev/null", O_WRONLY);
  202: 				if (fd) {
  203: 					dup2(fd, STDIN_FILENO);
  204: 					dup2(fd, STDOUT_FILENO);
  205: 					dup2(fd, STDERR_FILENO);
  206: 					if (fd > 2)
  207: 						close(fd);
  208: 				}
  209: 				break;
  210: 			default:
  211: 				VERB(1) LOG("Going to shadow land ...\n");
  212: 				closelog();
  213: 				return 0;
  214: 		}
  215: 	}
  216: 
  217: 	if (ioCreatePIDFile(PIDFILE_ANSHD, 42)) {
  218: 		ERR("Error:: already started anshd service ...\n");
  219: 		closelog();
  220: 		return 1;
  221: 	}
  222: 
  223: 	h = PrepareL2(szDev, &bpfLEN);
  224: 	if (h == -1) {
  225: 		ERR("Error:: Descriptor not opened ... abort!\n");
  226: 		unlink(PIDFILE_ANSHD);
  227: 		closelog();
  228: 		return 2;
  229: 	}
  230: 
  231: 	SLIST_INIT(&pH);
  232: 	if (!(proc = InitProc(h, NULL, ANSH_ID, bpfLEN))) {
  233: 		ERR("Error:: Not enough memory ...\n");
  234: 		close(h);
  235: 		unlink(PIDFILE_ANSHD);
  236: 		closelog();
  237: 		return 3;
  238: 	}
  239: 
  240: 	root = schedBegin();
  241: 	if (!root) {
  242: 		ERR("Scheduler not init #%d - %s\n", sched_GetErrno(), sched_GetError());
  243: 		DestroyProc(ANSH_ID);
  244: 		close(h);
  245: 		unlink(PIDFILE_ANSHD);
  246: 		closelog();
  247: 		return 4;
  248: 	} else
  249: 		root->root_hooks.hook_root.error = hook_error;
  250: 
  251: 	chdir("/");
  252: 	chroot(szChroot);
  253: 
  254: 	setgid(gid);
  255: 	setuid(uid);
  256: 
  257: 	if (schedRead(root, pktRx, (void*) ANSH_ID, h, NULL, 0)) {
  258: 		schedRun(root, &Kill);
  259: 	} else
  260: 		ERR("Failed to add reader task #%d - %s\n", sched_GetErrno(), sched_GetError());
  261: 
  262: 	VERB(1) LOG("Finish process.");
  263: 	schedEnd(&root);
  264: 	DestroyProc(ANSH_ID);
  265: 	close(h);
  266: 	unlink(PIDFILE_ANSHD);
  267: 	closelog();
  268: 	return 0;
  269: }

FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>