Annotation of ansh/src/ansh3d.c, revision 1.1.1.1.2.4
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.4! misho 6: * $Id: ansh3d.c,v 1.1.1.1.2.3 2011/10/14 09:04:04 misho Exp $
1.1 misho 7: *
1.1.1.1.2.2 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.1 misho 52: int Verbose;
53: u_int Crypted = 1;
1.1 misho 54: proc_head_t pH;
55: int bpfLEN, Timeout, Daemon = 1;
1.1.1.1.2.3 misho 56: char Key[STRSIZ];
1.1 misho 57:
1.1.1.1.2.4! misho 58: static struct tagProc *proc;
! 59: static sched_root_task_t *root;
! 60:
1.1 misho 61: extern char compiled[], compiledby[], compilehost[];
62:
63: static void
64: Usage()
65: {
66: printf( " -= anshd =- ELWIX Layer3 remote management service over ICMP\n"
67: "=== %s === %s@%s ===\n\n"
68: " Syntax: ansh3d [options]\n\n"
69: "\t-a <host>\tBind to host address (default is *any*)\n"
70: "\t-i <id>\tService ID (default is 42)\n"
71: "\t-U <user>\tRun service with other user\n"
72: "\t-C <dir>\tRun service into chroot directory\n"
73: "\t-t <timeout>\tTimeout of login if no activity (default is 0 sec)\n"
1.1.1.1.2.3 misho 74: "\t-k <key>\tService cipher key\n"
1.1 misho 75: "\t-u\t\tSwitch to unencrypted traffic between hosts\n"
76: "\t-b\t\tRun into batch mode (default is daemon mode)\n"
77: "\t-v\t\tVerbose (more -v, more verbosity ...)\n"
78: "\t-h\t\tThis help screen!\n"
79: "\n", compiled, compiledby, compilehost);
80: }
81:
82: static void
83: sig(int s)
84: {
85: int state;
1.1.1.1.2.4! misho 86: pid_t pid;
1.1 misho 87:
88: switch (s) {
89: case SIGHUP:
90: VERB(1) LOG("Got SIGHUP!\n");
91: break;
92: case SIGTERM:
93: Kill++;
94: VERB(1) LOG("Got SIGTERM!\n");
95: break;
96: case SIGPIPE:
97: VERB(1) LOG("Got SIGPIPE!\n");
98: break;
99: case SIGCHLD:
100: VERB(1) LOG("Got SIGCHLD!\n");
1.1.1.1.2.4! misho 101: while ((pid = waitpid(-1, &state, WNOHANG)) > 0)
! 102: stopProcess(root, &pH, pid, icmpTx);
1.1 misho 103: break;
104: }
105: }
106:
107: static void *
108: hook_error(void *root, void *arg)
109: {
110: /* sched_root_task_t *r = root; */
111:
112: if (!root)
113: return (void*) -1;
114:
115: if (arg == (void*) EINTR)
116: return (void*) -1;
117:
118: return NULL;
119: }
120:
121: int
122: main(int argc, char **argv)
123: {
124: struct sockaddr sa = { 0 };
125: struct sockaddr_in *sin4 = (struct sockaddr_in*) &sa;
126: struct sockaddr_in6 *sin6 = (struct sockaddr_in6*) &sa;
127: struct hostent *host;
128: struct passwd *pass;
129: int fd, h = 0, uid = 0, gid = 0;
130: long id = ANSH_ID;
131: char ch, szUser[STRSIZ] = "root", szChroot[STRSIZ] = "/";
132: struct sigaction sact;
133:
1.1.1.1.2.3 misho 134: strlcpy(Key, DEFAULT_KEY, sizeof Key);
135:
136: while ((ch = getopt(argc, argv, "hvubt:a:i:U:C:k:")) != -1)
1.1 misho 137: switch (ch) {
138: case 'U':
139: pass = getpwnam(optarg);
140: if (!pass) {
141: printf("Error:: User %s not found!\n", optarg);
142: return 1;
143: } else {
144: strlcpy(szUser, optarg, sizeof szUser);
145: uid = pass->pw_uid;
146: gid = pass->pw_gid;
147: }
148: endpwent();
149: break;
150: case 'C':
151: if (access(optarg, R_OK)) {
152: printf("Error:: in chroot %s #%d - %s\n", optarg, errno, strerror(errno));
153: return 1;
154: } else
155: strlcpy(szChroot, optarg, sizeof szChroot);
156: break;
157: case 'i':
158: id = strtol(optarg, NULL, 0);
159: break;
160: case 't':
161: Timeout = abs(strtol(optarg, NULL, 0));
162: break;
1.1.1.1.2.3 misho 163: case 'k':
164: strlcpy(Key, optarg, sizeof Key);
165: break;
1.1 misho 166: case 'a':
167: host = gethostbyname(optarg);
168: if (!host) {
169: printf("Error:: in bind address '%s' #%d - %s\n",
170: optarg, h_errno, hstrerror(h_errno));
171: return 1;
172: }
173: switch (host->h_addrtype) {
174: case AF_INET:
175: sin4->sin_len = sizeof(struct sockaddr_in);
176: sin4->sin_family = AF_INET;
177: memcpy(&sin4->sin_addr.s_addr, host->h_addr, host->h_length);
178: break;
179: case AF_INET6:
180: sin6->sin6_len = sizeof(struct sockaddr_in6);
181: sin6->sin6_family = AF_INET6;
182: memcpy(&sin6->sin6_addr.s6_addr, host->h_addr, host->h_length);
183: break;
184: default:
185: printf("Error:: Unknown address type %d !!!\n", host->h_addrtype);
186: return 1;
187: }
188: break;
189: case 'u':
190: Crypted ^= Crypted;
191: break;
192: case 'b':
193: Daemon ^= Daemon;
194: break;
195: case 'v':
196: Verbose++;
197: break;
198: case 'h':
199: default:
200: Usage();
201: return 1;
202: }
203: argc -= optind;
204: argv += optind;
205:
206: /* sanity check for openned descriptor */
207: if (!sa.sa_family) {
208: sin4->sin_len = sizeof(struct sockaddr_in);
209: sin4->sin_family = AF_INET;
210: }
211:
212: /* catch signals */
213: memset(&sact, 0, sizeof sact);
214: sigemptyset(&sact.sa_mask);
215: sact.sa_handler = sig;
216: sigaction(SIGPIPE, &sact, NULL);
217: sigaction(SIGCHLD, &sact, NULL);
218: sigaction(SIGTERM, &sact, NULL);
219: sigaction(SIGHUP, &sact, NULL);
220:
221: openlog("ansh3d", LOG_CONS | LOG_PID, LOG_DAEMON);
222:
223: if (Daemon) {
224: switch (fork()) {
225: case -1:
226: ERR("Daemon mode #%d - %s\n", errno, strerror(errno));
227: closelog();
228: return 1;
229: case 0:
230: VERB(1) LOG("Welcome to dark ...\n");
231:
232: setsid();
233:
234: fd = open("/dev/null", O_WRONLY);
235: if (fd) {
236: dup2(fd, STDIN_FILENO);
237: dup2(fd, STDOUT_FILENO);
238: dup2(fd, STDERR_FILENO);
239: if (fd > 2)
240: close(fd);
241: }
242: break;
243: default:
244: VERB(1) LOG("Going to shadow land ...\n");
245: closelog();
246: return 0;
247: }
248: }
249:
250: if (ioCreatePIDFile(PIDFILE_ANSH3D, 42)) {
251: ERR("Error:: already started ansh3d service ...\n");
252: closelog();
253: return 1;
254: }
255:
256: h = PrepareL3(&sa, &bpfLEN);
257: if (h == -1) {
258: ERR("Error:: Descriptor not opened ... abort!\n");
259: unlink(PIDFILE_ANSH3D);
260: closelog();
261: return 2;
262: }
263:
264: SLIST_INIT(&pH);
265: if (!(proc = InitProc(h, NULL, id, bpfLEN))) {
266: ERR("Error:: Not enough memory ...\n");
267: close(h);
268: unlink(PIDFILE_ANSH3D);
269: closelog();
270: return 3;
271: }
272:
273: root = schedBegin();
274: if (!root) {
275: ERR("Scheduler not init #%d - %s\n", sched_GetErrno(), sched_GetError());
276: DestroyProc(id);
277: close(h);
278: unlink(PIDFILE_ANSH3D);
279: closelog();
280: return 4;
281: } else
282: root->root_hooks.hook_root.error = hook_error;
283:
284: chdir("/");
285: chroot(szChroot);
286:
287: setgid(gid);
288: setuid(uid);
289:
290: if (schedRead(root, icmpRx, (void*) id, h)) {
291: schedRun(root, &Kill);
292: } else
293: ERR("Failed to add reader task #%d - %s\n", sched_GetErrno(), sched_GetError());
294:
295: VERB(1) LOG("Finish process.");
296: schedEnd(&root);
297: DestroyProc(id);
298: close(h);
299: unlink(PIDFILE_ANSH3D);
300: closelog();
301: return 0;
302: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>