Annotation of ansh/src/anshd.c, revision 1.1.1.1.2.1
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.1! misho 6: * $Id: anshd.c,v 1.1.1.1 2011/10/04 22:37:46 misho Exp $
1.1 misho 7: *
8: *************************************************************************/
9: #include "global.h"
10: #include "anshd.h"
11: #include "proc.h"
12:
13:
14: intptr_t Kill;
1.1.1.1.2.1! misho 15: int bpfLEN, Verbose;
! 16: u_int Crypted = 1;
1.1 misho 17: proc_head_t pH;
18: int Daemon = 1;
19:
20: extern char compiled[], compiledby[], compilehost[];
21:
22: static void
23: Usage()
24: {
25: printf( " -= anshd =- ELWIX Layer2 remote management service\n"
26: "=== %s === %s@%s ===\n\n"
27: " Syntax: anshd [options]\n\n"
28: "\t-d <dev>\tBind to host interface, like 'em0' (default is first host interface)\n"
29: "\t-i <id>\tService ID (default is 42)\n"
30: "\t-U <user>\tRun service with other user\n"
31: "\t-C <dir>\tRun service into chroot directory\n"
32: "\t-u\t\tSwitch to unencrypted traffic between hosts\n"
33: "\t-b\t\tRun into batch mode (default is daemon mode)\n"
34: "\t-v\t\tVerbose (more -v, more verbosity ...)\n"
35: "\t-h\t\tThis help screen!\n"
36: "\n", compiled, compiledby, compilehost);
37: }
38:
39: static void
40: sig(int s)
41: {
42: int state;
43:
44: switch (s) {
45: case SIGHUP:
46: LOG("Got SIGHUP!\n");
47: break;
48: case SIGTERM:
49: LOG("Got SIGTERM!\n");
50: Kill++;
51: break;
52: case SIGPIPE:
53: LOG("Got SIGPIPE!\n");
54: break;
55: case SIGCHLD:
56: while (waitpid(-1, &state, WNOHANG) > 0);
57: break;
58: }
59: }
60:
61: int
62: main(int argc, char **argv)
63: {
64: struct passwd *pass;
65: int fd, h = 0, uid = 0, gid = 0;
66: long id = ANSH_ID;
67: char ch, szUser[STRSIZ] = "root", szChroot[STRSIZ] = "/", szDev[STRSIZ] = { 0 };
68: struct sigaction sact;
69: sched_root_task_t *root = NULL;
70: struct tagProc *proc;
71:
72: Get1stEth(szDev, STRSIZ);
73:
74: while ((ch = getopt(argc, argv, "hvulbd:U:C:")) != -1)
75: switch (ch) {
76: case 'U':
77: pass = getpwnam(optarg);
78: if (!pass) {
79: printf("Error:: User %s not found!\n", optarg);
80: return 1;
81: } else {
82: strlcpy(szUser, optarg, sizeof szUser);
83: uid = pass->pw_uid;
84: gid = pass->pw_gid;
85: }
86: endpwent();
87: break;
88: case 'C':
89: if (access(optarg, R_OK)) {
90: printf("Error:: in chroot %s #%d - %s\n", optarg, errno, strerror(errno));
91: return 1;
92: } else
93: strlcpy(szChroot, optarg, sizeof szChroot);
94: break;
95: case 'i':
96: id = strtol(optarg, NULL, 0);
97: break;
98: case 'd':
99: strlcpy(szDev, optarg, sizeof szDev);
100: break;
101: case 'u':
102: Crypted ^= Crypted;
103: break;
104: case 'b':
105: Daemon ^= Daemon;
106: break;
107: case 'v':
108: Verbose++;
109: break;
110: case 'h':
111: default:
112: Usage();
113: return 1;
114: }
115: argc -= optind;
116: argv += optind;
117:
118: /* catch signals */
119: memset(&sact, 0, sizeof sact);
120: sigemptyset(&sact.sa_mask);
121: sact.sa_handler = sig;
122: sigaction(SIGPIPE, &sact, NULL);
123: sigaction(SIGCHLD, &sact, NULL);
124: sigaction(SIGTERM, &sact, NULL);
125: sigaction(SIGHUP, &sact, NULL);
126:
127: openlog("anshd", LOG_CONS | LOG_PID, LOG_DAEMON);
128:
129: if (Daemon) {
130: switch (fork()) {
131: case -1:
132: ERR("Daemon mode #%d - %s\n", errno, strerror(errno));
133: closelog();
134: return 1;
135: case 0:
136: VERB(1) LOG("Welcome to dark ...\n");
137:
138: setsid();
139:
140: fd = open("/dev/null", O_WRONLY);
141: if (fd) {
142: dup2(fd, STDIN_FILENO);
143: dup2(fd, STDOUT_FILENO);
144: dup2(fd, STDERR_FILENO);
145: if (fd > 2)
146: close(fd);
147: }
148: break;
149: default:
150: VERB(1) LOG("Going to shadow land ...\n");
151: closelog();
152: return 0;
153: }
154: }
155:
156: h = PrepareL2(szDev, &bpfLEN);
157: if (h == -1) {
158: ERR("Error:: Descriptor not opened ... abort!\n");
159: closelog();
160: return 2;
161: }
162:
163: SLIST_INIT(&pH);
164: if (!(proc = InitProc(h, NULL, id, bpfLEN))) {
165: ERR("Error:: Not enough memory ...\n");
166: close(h);
167: closelog();
168: return 3;
169: }
170:
171: root = schedBegin();
172: if (!root) {
173: ERR("Scheduler not init #%d - %s\n", sched_GetErrno(), sched_GetError());
174: DestroyProc(id);
175: close(h);
176: closelog();
177: return 4;
178: }
179:
180: chdir("/");
181: chroot(szChroot);
182:
183: setgid(gid);
184: setuid(uid);
185:
186: if (schedRead(root, pktRx, (void*) id, h)) {
187: schedRun(root, &Kill);
188: } else
189: ERR("Failed to add reader task #%d - %s\n", sched_GetErrno(), sched_GetError());
190:
191: VERB(1) LOG("Finish process.");
192: schedEnd(&root);
193: DestroyProc(id);
194: close(h);
195: closelog();
196: return 0;
197: }
FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>