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