int main(int argc, char **argv) { int dev_cnt; if(argc>1 && (strcmp(argv[1], "-d")==0) ) demonize(argv[0]); while(1) { db_open(); dev_cnt = 0; receive_history = true; frame_id = 0; printf("Wait for cm160 device to be connected\n"); while((dev_cnt = scan_usb()) == 0) sleep(2); printf("Found %d compatible device%s\n", dev_cnt, dev_cnt>1?"s":""); // Only 1 device supported if(!(g_devices[0].hdev = usb_open(g_devices[0].usb_dev))) { fprintf(stderr, "failed to open device\n"); db_close(); break; } handle_device(0); usb_close(g_devices[0].hdev); db_close(); } return 0; }
int main (int argc, char **argv) { int i, c; int pid_flags = 0; char *user = NULL; char *local_port = NULL; char *local_addr = NULL; char *password = NULL; char *timeout = NULL; char *method = NULL; char *pid_path = NULL; char *conf_path = NULL; char *iface = NULL; int remote_num = 0; ss_addr_t remote_addr[MAX_REMOTE_NUM]; char *remote_port = NULL; opterr = 0; while ((c = getopt (argc, argv, "f:s:p:l:k:t:m:i:c:b:a:uv")) != -1) { switch (c) { case 's': remote_addr[remote_num].host = optarg; remote_addr[remote_num++].port = NULL; break; case 'p': remote_port = optarg; break; case 'l': local_port = optarg; break; case 'k': password = optarg; break; case 'f': pid_flags = 1; pid_path = optarg; break; case 't': timeout = optarg; break; case 'm': method = optarg; break; case 'c': conf_path = optarg; break; case 'i': iface = optarg; break; case 'b': local_addr = optarg; break; case 'a': user = optarg; break; case 'u': udprelay = 1; break; case 'v': verbose = 1; break; } } if (opterr) { usage(); exit(EXIT_FAILURE); } if (conf_path != NULL) { jconf_t *conf = read_jconf(conf_path); if (remote_num == 0) { remote_num = conf->remote_num; for (i = 0; i < remote_num; i++) { remote_addr[i] = conf->remote_addr[i]; } } if (remote_port == NULL) remote_port = conf->remote_port; if (local_addr == NULL) local_addr = conf->local_addr; if (local_port == NULL) local_port = conf->local_port; if (password == NULL) password = conf->password; if (method == NULL) method = conf->method; if (timeout == NULL) timeout = conf->timeout; } if (remote_num == 0 || remote_port == NULL || local_port == NULL || password == NULL) { usage(); exit(EXIT_FAILURE); } if (timeout == NULL) timeout = "10"; if (local_addr == NULL) local_addr = "0.0.0.0"; if (pid_flags) { USE_SYSLOG(argv[0]); demonize(pid_path); } #ifdef __MINGW32__ winsock_init(); #else // ignore SIGPIPE signal(SIGPIPE, SIG_IGN); signal(SIGABRT, SIG_IGN); #endif // Setup keys LOGD("initialize ciphers... %s", method); int m = enc_init(password, method); // Setup socket int listenfd; listenfd = create_and_bind(local_addr, local_port); if (listenfd < 0) { FATAL("bind() error.."); } if (listen(listenfd, SOMAXCONN) == -1) { FATAL("listen() error."); } setnonblocking(listenfd); LOGD("server listening at port %s.", local_port); // Setup proxy context struct listen_ctx listen_ctx; listen_ctx.remote_num = remote_num; listen_ctx.remote_addr = malloc(sizeof(ss_addr_t) * remote_num); while (remote_num > 0) { int index = --remote_num; if (remote_addr[index].port == NULL) remote_addr[index].port = remote_port; listen_ctx.remote_addr[index] = remote_addr[index]; } listen_ctx.timeout = atoi(timeout); listen_ctx.fd = listenfd; listen_ctx.iface = iface; listen_ctx.method = m; struct ev_loop *loop = ev_default_loop(0); if (!loop) { FATAL("ev_loop error."); } ev_io_init (&listen_ctx.io, accept_cb, listenfd, EV_READ); ev_io_start (loop, &listen_ctx.io); // Setup UDP if (udprelay) { LOGD("udprelay enabled."); udprelay_init(local_addr, local_port, remote_addr[0].host, remote_addr[0].port, m, listen_ctx.timeout, iface); } // setuid if (user != NULL) run_as(user); ev_run (loop, 0); #ifdef __MINGW32__ winsock_cleanup(); #endif return 0; }
void ParseCommandLine(int argc, char *argv[], ListNode *DL_List, int *OverrideType) { int i,j, DebugLevel=0; char *ptr; ProgName=CopyStr(ProgName,argv[0]); CmdLine=argv[0]; for (i=1; i < argc; i++) { if (strcmp(argv[i],"-p")==0) Proxy=CopyStr(Proxy,argv[++i]); else if (strcmp(argv[i],"-proxy")==0) Proxy=CopyStr(Proxy,argv[++i]); else if (strcmp(argv[i],"-a")==0) { ptr=GetToken(argv[++i],":",&Username,0); ptr=GetToken(ptr,":",&Password,0); } else if (strcmp(argv[i],"-v")==0) { DebugLevel++; } else if (strcmp(argv[i],"-o")==0) { i++; AddOutputFile(argv[i], TRUE); if (strcmp(argv[i],"-")==0) Flags |= FLAG_STDOUT; } else if (strcmp(argv[i],"+o")==0) { i++; AddOutputFile(argv[i], FALSE); if (strcmp(argv[i],"-")==0) Flags |= FLAG_STDOUT; } else if (strcmp(argv[i],"-n")==0) { ItemSelectionArg=CopyStr(ItemSelectionArg,argv[++i]); } else if (strcmp(argv[i],"-t")==0) *OverrideType=ParseType(argv[++i]); else if (strcmp(argv[i],"-f")==0) FormatPreference=CopyStr(FormatPreference,argv[++i]); else if (strcmp(argv[i],"-q")==0) Flags |= FLAG_QUIET; else if (strcmp(argv[i],"-b")==0) Flags |= FLAG_BACKGROUND; else if (strcmp(argv[i],"-r")==0) Flags |= FLAG_RESUME; else if (strcmp(argv[i],"-x")==0) Flags |= FLAG_PORN; else if (strcmp(argv[i],"-T")==0) Flags |= FLAG_TEST; else if (strcmp(argv[i],"-w")==0) Flags |= FLAG_STDIN; else if (strcmp(argv[i],"-dt")==0) DisplayTitleWidth=atoi(argv[++i]); else if (strcmp(argv[i],"-st")==0) STREAMTimeout=atoi(argv[++i]); else if (strcmp(argv[i],"-P")==0) Player=CopyStr(Player,argv[++i]); else if (strcmp(argv[i],"-Pp")==0) PlayerLaunchPercent=atoi(argv[++i]); else if (strcmp(argv[i],"-?")==0) Flags |= FLAG_PRINT_USAGE; else if (strcmp(argv[i],"-h")==0) Flags |= FLAG_PRINT_USAGE; else if (strcmp(argv[i],"-help")==0) Flags |= FLAG_PRINT_USAGE; else if (strcmp(argv[i],"--help")==0) Flags |= FLAG_PRINT_USAGE; else if (strcmp(argv[i],"-version")==0) Flags |= FLAG_PRINT_VERSION; else if (strcmp(argv[i],"--version")==0) Flags |= FLAG_PRINT_VERSION; else if (strcmp(argv[i],"-test-sites")==0) { Flags |= FLAG_TEST_SITES | FLAG_QUIET; for (j=1; TestLinks[j] !=NULL; j++) { if (StrLen(TestLinks[j])) ListAddNamedItem(DL_List,DownloadTypes[j],CopyStr(NULL,TestLinks[j])); } ItemSelectionArg=CopyStr(ItemSelectionArg,"0"); } else { ListAddItem(DL_List,CopyStr(NULL,argv[i])); } } if (Flags & FLAG_BACKGROUND) { Flags |= FLAG_QUIET; demonize(); } if (Flags & FLAG_QUIET) DebugLevel=0; if (DebugLevel==1) Flags |= FLAG_DEBUG1; if (DebugLevel==2) Flags |= FLAG_DEBUG2; if (DebugLevel > 2) Flags |= FLAG_DEBUG3; }
void SpawnApplyConfig(const char *Config, int Flags) { char *User=NULL, *Group=NULL, *Dir=NULL; char *Name=NULL, *Value=NULL; const char *ptr; struct rlimit limit; rlim_t val; int i; //set all signal handlers to default if (Flags & SPAWN_SIGDEF) { for (i =0; i < _NSIG; i++) signal(i,SIG_DFL); } //Set controlling tty to be stdin. This means that CTRL-C, SIGWINCH etc is handled for the //stdin file descriptor, not for any oher if (Flags & SPAWN_DAEMON) demonize(); else { if (Flags & SPAWN_SETSID) setsid(); if (Flags & SPAWN_CTRL_TTY) tcsetpgrp(0, getpgrp()); } User=CopyStr(User,""); Group=CopyStr(Group,""); ptr=GetNameValuePair(Config,"\\S","=",&Name,&Value); while (ptr) { if (strcasecmp(Name,"User")==0) User=CopyStr(User, Value); else if (strcasecmp(Name,"Group")==0) Group=CopyStr(Group, Value); else if (strcasecmp(Name,"Dir")==0) Dir=CopyStr(Dir, Value); else if (strcasecmp(Name,"PidFile")==0) WritePidFile(Value); else if (strcasecmp(Name,"prio")==0) setpriority(PRIO_PROCESS, 0, atoi(Value)); else if (strcasecmp(Name,"nice")==0) setpriority(PRIO_PROCESS, 0, atoi(Value)); else if (strcasecmp(Name,"priority")==0) setpriority(PRIO_PROCESS, 0, atoi(Value)); else if (strcasecmp(Name,"mem")==0) { val=(rlim_t) ParseHumanReadableDataQty(Value, 0); limit.rlim_cur=val; limit.rlim_max=val; setrlimit(RLIMIT_DATA, &limit); } else if (strcasecmp(Name,"fsize")==0) { val=(rlim_t) ParseHumanReadableDataQty(Value, 0); limit.rlim_cur=val; limit.rlim_max=val; setrlimit(RLIMIT_FSIZE, &limit); } else if (strcasecmp(Name,"files")==0) { val=(rlim_t) ParseHumanReadableDataQty(Value, 0); limit.rlim_cur=val; limit.rlim_max=val; setrlimit(RLIMIT_NOFILE, &limit); } else if (strcasecmp(Name,"coredumps")==0) { val=(rlim_t) ParseHumanReadableDataQty(Value, 0); limit.rlim_cur=val; limit.rlim_max=val; setrlimit(RLIMIT_CORE, &limit); } else if ( (strcasecmp(Name,"procs")==0) || (strcasecmp(Name,"nproc")==0) ) { val=(rlim_t) ParseHumanReadableDataQty(Value, 0); limit.rlim_cur=val; limit.rlim_max=val; setrlimit(RLIMIT_NPROC, &limit); } ptr=GetNameValuePair(ptr,"\\S","=",&Name,&Value); } // This allows us to chroot into a whole different unix directory tree, with its own // password file etc if (Flags & SPAWN_CHROOT) chroot("."); if (StrLen(Dir)) chdir(Dir); //Always do group first, otherwise we'll lose ability to switch user/group if (StrLen(Group)) SwitchGroup(Group); if (StrLen(User)) SwitchUser(User); //Must do this last! After parsing Config, and also after functions like //SwitchUser that will need access to /etc/passwd if (Flags & SPAWN_JAIL) chroot("."); DestroyString(Name); DestroyString(Value); DestroyString(User); DestroyString(Group); DestroyString(Dir); }
void PTelnetDServerMode() { int listensock, fd, i; struct sigaction sigact; char *Tempstr=NULL, *IPStr=NULL; listensock=InitServerSock(Settings.Interface,Settings.Port); if (listensock==-1) { printf("ERROR: Cannot bind to port %d on interface %s\n",Settings.Port,Settings.Interface); exit(3); } if (! (Settings.Flags & FLAG_NODEMON)) demonize(); SetupPidFile(); if (Settings.Flags & FLAG_HONEYPOT) JailAndSwitchUser(FLAG_CHROOT, Settings.RealUser, Settings.ChDir); while (1) { /*Set up a signal handler for SIGCHLD so that our 'select' gets interrupted when something exits*/ sigact.sa_handler = default_signal_handler; sigemptyset(&sigact.sa_mask); sigact.sa_flags = 0; sigaction(SIGCHLD, &sigact, NULL); if (FDSelect(listensock, SELECT_READ, NULL)) { fd=TCPServerSockAccept(listensock, &IPStr); if (fork()==0) { //Sub processes shouldn't keep the pid file open, only the parent server //should close(PidFile); //if we've been passed a socket, then make it into stdin/stdout/stderr //but don't do this is fd==0, because then this has already been done by inetd close(0); close(1); close(2); dup(fd); dup(fd); dup(fd); //Having dupped it we no longer need to keep this copy open close(fd); Tempstr=MCopyStr(Tempstr, g_argv[0]," ",IPStr,NULL); for (i=0; i <g_argc; i++) memset(g_argv[i],0,StrLen(g_argv[i])); strcpy(g_argv[0],Tempstr); //In case logging demon was restarted, ensure we have connection before we chroot //Eric Wedaa modified the following line to log to the LongTail honeypot consolidation server openlog("ptelnetd",LOG_PID|LOG_NDELAY,LOG_AUTH); HandleClient(); //Should be redundant, but if something goes wrong in HandleClient, we might want this //exit call _exit(0); } close(fd); } waitpid(-1,NULL,WNOHANG); } }
int main (int argc, char **argv) { int i, c; int pid_flags = 0; char *password = NULL; char *timeout = NULL; char *method = NULL; char *pid_path = NULL; char *conf_path = NULL; char *iface = NULL; int server_num = 0; char *server_host[MAX_REMOTE_NUM]; char *server_port = NULL; int dns_thread_num = DNS_THREAD_NUM; opterr = 0; while ((c = getopt (argc, argv, "f:s:p:l:k:t:m:c:i:d:v")) != -1) { switch (c) { case 's': server_host[server_num++] = optarg; break; case 'p': server_port = optarg; break; case 'k': password = optarg; break; case 'f': pid_flags = 1; pid_path = optarg; break; case 't': timeout = optarg; break; case 'm': method = optarg; break; case 'c': conf_path = optarg; break; case 'i': iface = optarg; break; case 'd': dns_thread_num = atoi(optarg); if (!dns_thread_num) FATAL("Invalid DNS thread number"); break; case 'v': verbose = 1; break; } } if (opterr) { usage(); exit(EXIT_FAILURE); } if (conf_path != NULL) { jconf_t *conf = read_jconf(conf_path); if (server_num == 0) { server_num = conf->remote_num; for (i = 0; i < server_num; i++) { server_host[i] = conf->remote_host[i]; } } if (server_port == NULL) server_port = conf->remote_port; if (password == NULL) password = conf->password; if (method == NULL) method = conf->method; if (timeout == NULL) timeout = conf->timeout; } if (server_num == 0 || server_port == NULL || password == NULL) { usage(); exit(EXIT_FAILURE); } if (timeout == NULL) timeout = "60"; if (pid_flags) { demonize(pid_path); } // ignore SIGPIPE signal(SIGPIPE, SIG_IGN); signal(SIGCHLD, SIG_IGN); signal(SIGABRT, SIG_IGN); // setup asyncns asyncns_t *asyncns; if (!(asyncns = asyncns_new(dns_thread_num))) { FATAL("asyncns failed"); } // setup keys LOGD("initialize cihpers... %s", method); int m = enc_init(password, method); // inilitialize ev loop struct ev_loop *loop = EV_DEFAULT; // bind to each interface while (server_num > 0) { int index = --server_num; const char* host = server_host[index]; // Bind to port int listenfd; listenfd = create_and_bind(host, server_port); if (listenfd < 0) { FATAL("bind() error.."); } if (listen(listenfd, SOMAXCONN) == -1) { FATAL("listen() error."); } setnonblocking(listenfd); LOGD("server listening at port %s.", server_port); // Setup proxy context struct listen_ctx *listen_ctx = malloc(sizeof(struct listen_ctx)); listen_ctx->timeout = atoi(timeout); listen_ctx->asyncns = asyncns; listen_ctx->fd = listenfd; listen_ctx->method = m; listen_ctx->iface = iface; ev_io_init (&listen_ctx->io, accept_cb, listenfd, EV_READ); ev_io_start (loop, &listen_ctx->io); } // start ev loop ev_run (loop, 0); return 0; }
int main (int argc, char **argv) { int i, c; int pid_flags = 0; char *local_port = NULL; char *local_addr = NULL; char *password = NULL; char *timeout = NULL; char *method = NULL; char *pid_path = NULL; char *conf_path = NULL; int remote_num = 0; char *remote_host[MAX_REMOTE_NUM]; char *remote_port = NULL; opterr = 0; while ((c = getopt (argc, argv, "f:s:p:l:k:t:m:c:b:")) != -1) { switch (c) { case 's': remote_host[remote_num++] = optarg; break; case 'p': remote_port = optarg; break; case 'l': local_port = optarg; break; case 'k': password = optarg; break; case 'f': pid_flags = 1; pid_path = optarg; break; case 't': timeout = optarg; break; case 'm': method = optarg; break; case 'c': conf_path = optarg; break; case 'b': local_addr = optarg; break; } } if (opterr) { usage(); exit(EXIT_FAILURE); } if (conf_path != NULL) { jconf_t *conf = read_jconf(conf_path); if (remote_num == 0) { remote_num = conf->remote_num; for (i = 0; i < remote_num; i++) { remote_host[i] = conf->remote_host[i]; } } if (remote_port == NULL) remote_port = conf->remote_port; if (local_port == NULL) local_port = conf->local_port; if (password == NULL) password = conf->password; if (method == NULL) method = conf->method; if (timeout == NULL) timeout = conf->timeout; } if (remote_num == 0 || remote_port == NULL || local_port == NULL || password == NULL) { usage(); exit(EXIT_FAILURE); } if (timeout == NULL) timeout = "10"; if (local_addr == NULL) local_addr = "0.0.0.0"; if (pid_flags) { demonize(pid_path); } // ignore SIGPIPE signal(SIGPIPE, SIG_IGN); signal(SIGABRT, SIG_IGN); // Setup keys LOGD("calculating ciphers..."); int m = enc_init(password, method); // Setup socket int listenfd; listenfd = create_and_bind(local_addr, local_port); if (listenfd < 0) { FATAL("bind() error.."); } if (listen(listenfd, SOMAXCONN) == -1) { FATAL("listen() error."); } setnonblocking(listenfd); LOGD("server listening at port %s.", local_port); // Setup proxy context struct listen_ctx listen_ctx; listen_ctx.remote_num = remote_num; listen_ctx.remote_host = malloc(sizeof(char *) * remote_num); while (remote_num > 0) { int index = --remote_num; listen_ctx.remote_host[index] = remote_host[index]; } listen_ctx.remote_port = remote_port; listen_ctx.timeout = atoi(timeout); listen_ctx.fd = listenfd; listen_ctx.method = m; struct ev_loop *loop = ev_default_loop(0); if (!loop) { FATAL("ev_loop error."); } ev_io_init (&listen_ctx.io, accept_cb, listenfd, EV_READ); ev_io_start (loop, &listen_ctx.io); ev_run (loop, 0); return 0; }
void create_daemon() { demonize(); openlog("serwerTCP", LOG_CONS | LOG_PID | LOG_NDELAY, LOG_LOCAL1); syslog(LOG_NOTICE, "Successfully started daemon"); }