File:  [ELWIX - Embedded LightWeight unIX -] / ansh / src / anshd.c
Revision 1.1.1.1.2.4: download - view: text, annotated - select for diffs - revision graph
Thu Oct 13 16:08:52 2011 UTC (12 years, 11 months ago) by misho
Branches: ansh1_0
Diff to: branchpoint 1.1.1.1: preferred, unified
added BSD license

    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.1.1.1.2.4 2011/10/13 16:08:52 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
   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, Daemon = 1;
   56: 
   57: extern char compiled[], compiledby[], compilehost[];
   58: 
   59: static void
   60: Usage()
   61: {
   62: 	printf(	" -= anshd =- ELWIX Layer2 remote management service\n"
   63: 		"=== %s === %s@%s ===\n\n"
   64: 		" Syntax: anshd [options]\n\n"
   65: 		"\t-d <dev>\tBind to host interface, like 'em0' (default is first host interface)\n"
   66: 		"\t-U <user>\tRun service with other user\n"
   67: 		"\t-C <dir>\tRun service into chroot directory\n"
   68: 		"\t-t <timeout>\tTimeout of login if no activity (default is 0 sec)\n"
   69: 		"\t-u\t\tSwitch to unencrypted traffic between hosts\n"
   70: 		"\t-b\t\tRun into batch mode (default is daemon mode)\n"
   71: 		"\t-v\t\tVerbose (more -v, more verbosity ...)\n"
   72: 		"\t-h\t\tThis help screen!\n"
   73: 		"\n", compiled, compiledby, compilehost);
   74: }
   75: 
   76: static void
   77: sig(int s)
   78: {
   79: 	int state;
   80: 
   81: 	switch (s) {
   82: 		case SIGHUP:
   83: 			VERB(1) LOG("Got SIGHUP!\n");
   84: 			break;
   85: 		case SIGTERM:
   86: 			Kill++;
   87: 			VERB(1) LOG("Got SIGTERM!\n");
   88: 			break;
   89: 		case SIGPIPE:
   90: 			VERB(1) LOG("Got SIGPIPE!\n");
   91: 			break;
   92: 		case SIGCHLD:
   93: 			VERB(1) LOG("Got SIGCHLD!\n");
   94: 			while (waitpid(-1, &state, WNOHANG) > 0);
   95: 			break;
   96: 	}
   97: }
   98: 
   99: static void *
  100: hook_error(void *root, void *arg)
  101: {
  102: /*	sched_root_task_t *r = root; */
  103: 
  104: 	if (!root)
  105: 		return (void*) -1;
  106: 
  107: 	if (arg == (void*) EINTR)
  108: 		return (void*) -1;
  109: 
  110: 	return NULL;
  111: }
  112: 
  113: int
  114: main(int argc, char **argv)
  115: {
  116: 	struct passwd *pass;
  117: 	int fd, h = 0, uid = 0, gid = 0;
  118: 	char ch, szUser[STRSIZ] = "root", szChroot[STRSIZ] = "/", szDev[STRSIZ] = { 0 };
  119: 	struct sigaction sact;
  120: 	sched_root_task_t *root = NULL;
  121: 	struct tagProc *proc;
  122: 
  123: 	Get1stEth(szDev, STRSIZ);
  124: 
  125: 	while ((ch = getopt(argc, argv, "hvubt:d:U:C:")) != -1)
  126: 		switch (ch) {
  127: 			case 'U':
  128: 				pass = getpwnam(optarg);
  129: 				if (!pass) {
  130: 					printf("Error:: User %s not found!\n", optarg);
  131: 					return 1;
  132: 				} else {
  133: 					strlcpy(szUser, optarg, sizeof szUser);
  134: 					uid = pass->pw_uid;
  135: 					gid = pass->pw_gid;
  136: 				}
  137: 				endpwent();
  138: 				break;
  139: 			case 'C':
  140: 				if (access(optarg, R_OK)) {
  141: 					printf("Error:: in chroot %s #%d - %s\n", optarg, errno, strerror(errno));
  142: 					return 1;
  143: 				} else
  144: 					strlcpy(szChroot, optarg, sizeof szChroot);
  145: 				break;
  146: 			case 't':
  147: 				Timeout = abs(strtol(optarg, NULL, 0));
  148: 				break;
  149: 			case 'd':
  150: 				strlcpy(szDev, optarg, sizeof szDev);
  151: 				break;
  152: 			case 'u':
  153: 				Crypted ^= Crypted;
  154: 				break;
  155: 			case 'b':
  156: 				Daemon ^= Daemon;
  157: 				break;
  158: 			case 'v':
  159: 				Verbose++;
  160: 				break;
  161: 			case 'h':
  162: 			default:
  163: 				Usage();
  164: 				return 1;
  165: 		}
  166: 	argc -= optind;
  167: 	argv += optind;
  168: 
  169: 	/* catch signals */
  170: 	memset(&sact, 0, sizeof sact);
  171: 	sigemptyset(&sact.sa_mask);
  172: 	sact.sa_handler = sig;
  173: 	sigaction(SIGPIPE, &sact, NULL);
  174: 	sigaction(SIGCHLD, &sact, NULL);
  175: 	sigaction(SIGTERM, &sact, NULL);
  176: 	sigaction(SIGHUP, &sact, NULL);
  177: 
  178: 	openlog("anshd", LOG_CONS | LOG_PID, LOG_DAEMON);
  179: 
  180: 	if (Daemon) {
  181: 		switch (fork()) {
  182: 			case -1:
  183: 				ERR("Daemon mode #%d - %s\n", errno, strerror(errno));
  184: 				closelog();
  185: 				return 1;
  186: 			case 0:
  187: 				VERB(1) LOG("Welcome to dark ...\n");
  188: 
  189: 				setsid();
  190: 
  191: 				fd = open("/dev/null", O_WRONLY);
  192: 				if (fd) {
  193: 					dup2(fd, STDIN_FILENO);
  194: 					dup2(fd, STDOUT_FILENO);
  195: 					dup2(fd, STDERR_FILENO);
  196: 					if (fd > 2)
  197: 						close(fd);
  198: 				}
  199: 				break;
  200: 			default:
  201: 				VERB(1) LOG("Going to shadow land ...\n");
  202: 				closelog();
  203: 				return 0;
  204: 		}
  205: 	}
  206: 
  207: 	if (ioCreatePIDFile(PIDFILE_ANSHD, 42)) {
  208: 		ERR("Error:: already started anshd service ...\n");
  209: 		closelog();
  210: 		return 1;
  211: 	}
  212: 
  213: 	h = PrepareL2(szDev, &bpfLEN);
  214: 	if (h == -1) {
  215: 		ERR("Error:: Descriptor not opened ... abort!\n");
  216: 		unlink(PIDFILE_ANSHD);
  217: 		closelog();
  218: 		return 2;
  219: 	}
  220: 
  221: 	SLIST_INIT(&pH);
  222: 	if (!(proc = InitProc(h, NULL, ANSH_ID, bpfLEN))) {
  223: 		ERR("Error:: Not enough memory ...\n");
  224: 		close(h);
  225: 		unlink(PIDFILE_ANSHD);
  226: 		closelog();
  227: 		return 3;
  228: 	}
  229: 
  230: 	root = schedBegin();
  231: 	if (!root) {
  232: 		ERR("Scheduler not init #%d - %s\n", sched_GetErrno(), sched_GetError());
  233: 		DestroyProc(ANSH_ID);
  234: 		close(h);
  235: 		unlink(PIDFILE_ANSHD);
  236: 		closelog();
  237: 		return 4;
  238: 	} else
  239: 		root->root_hooks.hook_root.error = hook_error;
  240: 
  241: 	chdir("/");
  242: 	chroot(szChroot);
  243: 
  244: 	setgid(gid);
  245: 	setuid(uid);
  246: 
  247: 	if (schedRead(root, pktRx, (void*) ANSH_ID, h)) {
  248: 		schedRun(root, &Kill);
  249: 	} else
  250: 		ERR("Failed to add reader task #%d - %s\n", sched_GetErrno(), sched_GetError());
  251: 
  252: 	VERB(1) LOG("Finish process.");
  253: 	schedEnd(&root);
  254: 	DestroyProc(ANSH_ID);
  255: 	close(h);
  256: 	unlink(PIDFILE_ANSHD);
  257: 	closelog();
  258: 	return 0;
  259: }

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