Annotation of ansh/src/anshd.c, revision 1.1.1.1.2.6
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.6! misho 6: * $Id: anshd.c,v 1.1.1.1.2.5 2011/10/14 09:04:04 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:
1.1.1.1.2.6! misho 58: static sched_root_task_t *root = NULL;
! 59: static struct tagProc *proc;
! 60:
1.1 misho 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"
1.1.1.1.2.5 misho 72: "\t-k <key>\tService cipher key\n"
1.1.1.1.2.2 misho 73: "\t-t <timeout>\tTimeout of login if no activity (default is 0 sec)\n"
1.1 misho 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;
1.1.1.1.2.6! misho 85: pid_t pid;
1.1 misho 86:
87: switch (s) {
88: case SIGHUP:
1.1.1.1.2.2 misho 89: VERB(1) LOG("Got SIGHUP!\n");
1.1 misho 90: break;
91: case SIGTERM:
92: Kill++;
1.1.1.1.2.2 misho 93: VERB(1) LOG("Got SIGTERM!\n");
1.1 misho 94: break;
95: case SIGPIPE:
1.1.1.1.2.2 misho 96: VERB(1) LOG("Got SIGPIPE!\n");
1.1 misho 97: break;
98: case SIGCHLD:
1.1.1.1.2.2 misho 99: VERB(1) LOG("Got SIGCHLD!\n");
1.1.1.1.2.6! misho 100: while ((pid = waitpid(-1, &state, WNOHANG)) > 0)
! 101: stopProcess(root, &pH, pid, pktTx);
1.1 misho 102: break;
103: }
104: }
105:
1.1.1.1.2.2 misho 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:
1.1 misho 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:
1.1.1.1.2.5 misho 128: strlcpy(Key, DEFAULT_KEY, sizeof Key);
129:
1.1 misho 130: Get1stEth(szDev, STRSIZ);
131:
1.1.1.1.2.5 misho 132: while ((ch = getopt(argc, argv, "hvubt:d:U:C:k:")) != -1)
1.1 misho 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;
1.1.1.1.2.2 misho 153: case 't':
154: Timeout = abs(strtol(optarg, NULL, 0));
155: break;
1.1 misho 156: case 'd':
157: strlcpy(szDev, optarg, sizeof szDev);
158: break;
1.1.1.1.2.5 misho 159: case 'k':
160: strlcpy(Key, optarg, sizeof Key);
161: break;
1.1 misho 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:
1.1.1.1.2.2 misho 217: if (ioCreatePIDFile(PIDFILE_ANSHD, 42)) {
218: ERR("Error:: already started anshd service ...\n");
219: closelog();
220: return 1;
221: }
222:
1.1 misho 223: h = PrepareL2(szDev, &bpfLEN);
224: if (h == -1) {
225: ERR("Error:: Descriptor not opened ... abort!\n");
1.1.1.1.2.2 misho 226: unlink(PIDFILE_ANSHD);
1.1 misho 227: closelog();
228: return 2;
229: }
230:
231: SLIST_INIT(&pH);
1.1.1.1.2.3 misho 232: if (!(proc = InitProc(h, NULL, ANSH_ID, bpfLEN))) {
1.1 misho 233: ERR("Error:: Not enough memory ...\n");
234: close(h);
1.1.1.1.2.2 misho 235: unlink(PIDFILE_ANSHD);
1.1 misho 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());
1.1.1.1.2.3 misho 243: DestroyProc(ANSH_ID);
1.1 misho 244: close(h);
1.1.1.1.2.2 misho 245: unlink(PIDFILE_ANSHD);
1.1 misho 246: closelog();
247: return 4;
1.1.1.1.2.2 misho 248: } else
249: root->root_hooks.hook_root.error = hook_error;
1.1 misho 250:
251: chdir("/");
252: chroot(szChroot);
253:
254: setgid(gid);
255: setuid(uid);
256:
1.1.1.1.2.3 misho 257: if (schedRead(root, pktRx, (void*) ANSH_ID, h)) {
1.1 misho 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);
1.1.1.1.2.3 misho 264: DestroyProc(ANSH_ID);
1.1 misho 265: close(h);
1.1.1.1.2.2 misho 266: unlink(PIDFILE_ANSHD);
1.1 misho 267: closelog();
268: return 0;
269: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>