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