int setup_tcp_server_socket(const char ip[4], const char ip6[16], u16 port, int backlog) { char sip[FMT_IP6]; int fd; fd = socket_tcp6(); if(fd >= 0) { if(socket_bind6_reuse(fd, ip6, port, 0) == -1) { close(fd); fd = socket_tcp(); if(fd >= 0) { if(-1 == socket_bind4_reuse(fd, ip, port)) ERROR("unable to create server socket: %s", strerror(errno)); else { fmt_ip4(sip, ip); DEBUG("bound TCP socket on %s:%hu", sip, port); } } else { ERROR("unable to create TCP socket: %s", strerror(errno)); } } else { fmt_ip6(sip, ip6); DEBUG("bound TCP socket on [%s]:%hu", sip, port); } if(socket_listen(fd, backlog) == -1) ERROR("listen() failed: %s", strerror(errno)); } else { ERROR("unable to create TCP socket: %s", strerror(errno)); } return fd; }
static int try_connect(const ipv4addr* ip, ipv4port port, unsigned long timeout) { int sock; if ((sock = socket_tcp()) >= 0) { if (socket_connect4_timeout(sock, ip, port, timeout)) return sock; close(sock); } return -1; }
void socket_call_handler(unsigned long long uniqueSockID, int threads, unsigned char *buf, ssize_t len) { int domain; unsigned int type; int protocol; u_char *pt; PRINT_DEBUG("socket call handler1"); PRINT_DEBUG("%llu", uniqueSockID); pt = buf; domain = *(int *) pt; pt += sizeof(int); type = *(unsigned int *) pt; pt += sizeof(unsigned int); protocol = *(int *) pt; pt += sizeof(int); if (pt - buf != len) { PRINT_DEBUG("READING ERROR! CRASH, diff=%d len=%d", pt - buf, len); nack_send(uniqueSockID, socket_call); return; } PRINT_DEBUG("socket call handler2"); PRINT_DEBUG("%d,%d,%d", domain, protocol, type); if (domain != AF_INET) { PRINT_DEBUG("Wrong domain, only AF_INET us supported"); nack_send(uniqueSockID, socket_call); return; } if (type == SOCK_DGRAM) { socket_udp(domain, type, protocol, uniqueSockID); return; } else if (type == SOCK_STREAM) { socket_tcp(domain, type, protocol, uniqueSockID); return; } else if (type == SOCK_RAW && (protocol == IPPROTO_ICMP)) { socket_icmp(domain, type, protocol, uniqueSockID); return; } else { PRINT_DEBUG("non supported socket type"); return; } return; }
static int thistcp (struct dns_transmit *d) { struct taia now; const char *ip = NULL; socketfree (d); packetfree (d); for (; d->curserver < 16; ++d->curserver) { ip = d->servers + 4 * d->curserver; if (byte_diff (ip, 4, "\0\0\0\0")) { d->query[2] = dns_random (256); d->query[3] = dns_random (256); d->s1 = 1 + socket_tcp (); if (!d->s1) { dns_transmit_free (d); return -1; } if (randombind (d) == -1) { dns_transmit_free (d); return -1; } taia_now (&now); taia_uint (&d->deadline, 10); taia_add (&d->deadline, &d->deadline, &now); if (socket_connect4 (d->s1 - 1, ip, 53) == 0) { d->pos = 0; d->tcpstate = 2; return 0; } if (errno == error_inprogress || errno == error_wouldblock) { d->tcpstate = 1; return 0; } socketfree(d); } } dns_transmit_free(d); return -1; }
int remoteinfo(stralloc *out,char ipremote[4],uint16 portremote,char iplocal[4],uint16 portlocal,unsigned int timeout) { int s; int r; if (!stralloc_copys(out,"")) return -1; taia_now(&now); taia_uint(&deadline,timeout); taia_add(&deadline,&now,&deadline); s = socket_tcp(); if (s == -1) return -1; r = doit(out,s,ipremote,portremote,iplocal,portlocal,timeout); close(s); return r; }
static socket_t Listen(int port) { int r; socket_t server; server = socket_tcp(); if(socket_error == server) return socket_invalid; // reuse addr r = socket_setreuseaddr(server, 1); // bind r = socket_bind_any(server, (unsigned short)port); // listen r = socket_listen(server, 64); return server; }
void mainloop(int sock) { char code; int newsock; iopoll_fd fds; fds.fd = sock; fds.events = IOPOLL_READ; for (;;) { switch (iopoll_restart(&fds, 1, timeout*1000)) { case -1: return; case 0: /* Timed out */ if (kill(ppid, 0) == -1) return; continue; default: if (read(sock, &code, 1) != 1) return; code = 0; if ((newsock = socket_tcp()) == -1) { error1sys("Creating socket failed"); code = 1; } else if (!socket_reuse(newsock)) { error1sys("Setting flags on socket failed"); code = 2; } else if (!socket_bind4(newsock, &ip, port)) { error1sys("Binding socket failed"); code = 3; } if (write(sock, &code, 1) != 1) error1sys("Sending the result code failed"); if (!code) if (!socket_sendfd(sock, newsock)) error1sys("Sending the bound socket failed"); close(newsock); } } }
int main (int argc, char *argv[]) { int i = 0; time_t t = 0; struct sigaction sa; unsigned long cachesize = 0; char *x = NULL, char_seed[128]; sa.sa_handler = handle_term; sigaction (SIGINT, &sa, NULL); sigaction (SIGTERM, &sa, NULL); sa.sa_handler = SIG_IGN; sigaction (SIGPIPE, &sa, NULL); seed_addtime (); seed_adduint32 (getpid ()); seed_adduint32 (getppid ()); seed_adduint32 (getuid ()); seed_adduint32 (getgid ()); seed_addtime (); prog = strdup ((x = strrchr (argv[0], '/')) != NULL ? x + 1 : argv[0]); i = check_option (argc, argv); argc -= i; argv += i; if (mode & DAEMON) { i = fork (); if (i == -1) err (-1, "could not fork a daemon process"); if (i > 0) return 0; } time (&t); strftime (char_seed, sizeof (char_seed), "%b-%d %Y %T %Z", localtime (&t)); warnx ("version %s: starting: %s\n", VERSION, char_seed); set_timezone (); if (debug_level) warnx ("TIMEZONE: %s", env_get ("TZ")); read_conf (CFGFILE); if (!debug_level) if ((x = env_get ("DEBUG_LEVEL"))) debug_level = atol (x); warnx ("DEBUG_LEVEL set to `%d'", debug_level); if ((x = env_get ("DATALIMIT"))) { struct rlimit r; unsigned long dlimit = atol (x); if (getrlimit (RLIMIT_DATA, &r) != 0) err (-1, "could not get resource RLIMIT_DATA"); r.rlim_cur = (dlimit <= r.rlim_max) ? dlimit : r.rlim_max; if (setrlimit (RLIMIT_DATA, &r) != 0) err (-1, "could not set resource RLIMIT_DATA"); if (debug_level) warnx ("DATALIMIT set to `%ld' bytes", r.rlim_cur); } if (!(x = env_get ("IP"))) err (-1, "$IP not set"); if (!ip4_scan (x, myipincoming)) err (-1, "could not parse IP address `%s'", x); seed_addtime (); udp53 = socket_udp (); if (udp53 == -1) err (-1, "could not open UDP socket"); if (socket_bind4_reuse (udp53, myipincoming, 53) == -1) err (-1, "could not bind UDP socket"); seed_addtime (); tcp53 = socket_tcp (); if (tcp53 == -1) err (-1, "could not open TCP socket"); if (socket_bind4_reuse (tcp53, myipincoming, 53) == -1) err (-1, "could not bind TCP socket"); if (mode & DAEMON) { /* redirect stdout & stderr to a log file */ redirect_to_log (LOGFILE, STDOUT_FILENO | STDERR_FILENO); write_pid (PIDFILE); } seed_addtime (); droproot (); if (mode & DAEMON) /* crerate a new session & detach from controlling tty */ if (setsid () < 0) err (-1, "could not start a new session for the daemon"); seed_addtime (); socket_tryreservein (udp53, 131072); memset (char_seed, 0, sizeof (char_seed)); for (i = 0, x = (char *)seed; (unsigned)i < sizeof (char_seed); i++, x++) char_seed[i] = *x; dns_random_init (char_seed); if (!(x = env_get ("IPSEND"))) err (-1, "$IPSEND not set"); if (!ip4_scan (x, myipoutgoing)) err (-1, "could not parse IP address `%s'", x); if (!(x = env_get ("CACHESIZE"))) err (-1, "$CACHESIZE not set"); scan_ulong (x, &cachesize); if (!cache_init (cachesize)) err (-1, "could not allocate `%ld' bytes for cache", cachesize); if (env_get ("HIDETTL")) response_hidettl (); if (env_get ("FORWARDONLY")) query_forwardonly (); if (env_get ("MERGEQUERIES")) dns_enable_merge (log_merge); if (!roots_init ()) err (-1, "could not read servers"); if (debug_level > 3) roots_display(); if (socket_listen (tcp53, 20) == -1) err (-1, "could not listen on TCP socket"); if (!dbl_init() && debug_level > 1) warnx ("could not read dnsbl.cdb"); doit (); return 0; }
int main(int argc,char * const *argv) { const char *hostname; int opt; char *x; unsigned long u; int s; int t; int flagv4 = 1, flagv6 = 1, rc; struct addrinfo *localai = NULL, hints = {0}, *ai; while ((opt = getopt(argc,argv,"dDvqQhHrR1UXx:t:T:u:g:l:b:B:c:pPoO3IiEeSsw:nN46")) != opteof) switch(opt) { case 'b': scan_ulong(optarg,&backlog); break; case 'c': scan_ulong(optarg,&limit); break; case 'X': flagallownorules = 1; break; case 'x': fnrules = optarg; break; case 'B': banner = optarg; break; case 'd': flagdelay = 1; break; case 'D': flagdelay = 0; break; case 'v': verbosity = 2; break; case 'q': verbosity = 0; break; case 'Q': verbosity = 1; break; case 'P': flagparanoid = 0; break; case 'p': flagparanoid = 1; break; case 'O': flagkillopts = 1; break; case 'o': flagkillopts = 0; break; case 'H': flagremotehost = 0; break; case 'h': flagremotehost = 1; break; case 'R': flagremoteinfo = 0; break; case 'r': flagremoteinfo = 1; break; case 't': scan_ulong(optarg,&timeout); break; case 'T': scan_ulong(optarg,&ssltimeout); break; case 'w': scan_uint(optarg,&progtimeout); break; case 'U': x = env_get("UID"); if (x) scan_ulong(x,&uid); x = env_get("GID"); if (x) scan_ulong(x,&gid); break; case 'u': scan_ulong(optarg,&uid); break; case 'g': scan_ulong(optarg,&gid); break; case '1': flag1 = 1; break; case 'l': localhost = optarg; break; case '3': flag3 = 1; break; case 'I': flagclientcert = 0; break; case 'i': flagclientcert = 1; break; case 'S': flagsslenv = 0; break; case 's': flagsslenv = 1; break; case 'E': flagtcpenv = 0; break; case 'e': flagtcpenv = 1; break; case 'n': flagsslwait = 1; break; case 'N': flagsslwait = 0; break; case '4': flagv6 = 0; break; case '6': flagv4 = 0; break; default: usage(); } if (flagv4 == flagv6) { flagv4 = flagv6 = 1; } argc -= optind; argv += optind; if (!verbosity) buffer_2->fd = -1; hostname = *argv++; if (!hostname) usage(); if (str_equal(hostname,"")) hostname = NULL; if (str_equal(hostname,"0")) hostname = NULL; x = *argv++; if (!x) usage(); prog = argv; if (!*argv) usage(); hints.ai_family = flagv4 == flagv6 ? AF_UNSPEC : flagv4 ? AF_INET : AF_INET6; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; if ((rc = getaddrinfo(hostname, x, &hints, &localai))) { strerr_die(111,FATAL "unable to figure out address for ", hostname ? hostname : "0", " ",x,": ",gai_strerror(rc),0); } if (!localai) { strerr_die2x(111,FATAL,"address not found"); } if (x = env_get("VERIFYDEPTH")) { scan_ulong(x,&u); verifydepth = u; } if (x = env_get("CAFILE")) cafile = x; if (cafile && str_equal(cafile,"")) cafile = 0; if (x = env_get("CCAFILE")) ccafile = x; if (ccafile && str_equal(ccafile,"")) ccafile = 0; if (!flagclientcert) ccafile = 0; if (x = env_get("CADIR")) cadir = x; if (cadir && str_equal(cadir,"")) cadir= 0; if (x = env_get("CERTCHAINFILE")) certchainfile = x; if (certchainfile && str_equal(certchainfile,"")) certchainfile = 0; if (x = env_get("CERTFILE")) certfile = x; if (certfile && str_equal(certfile,"")) certfile = 0; if (x = env_get("KEYFILE")) keyfile = x; if (keyfile && str_equal(keyfile,"")) keyfile = 0; if (x = env_get("DHFILE")) dhfile = x; if (dhfile && str_equal(dhfile,"")) dhfile = 0; if (x = env_get("CIPHERS")) ciphers = x; if (ciphers && str_equal(ciphers,"")) ciphers = 0; sig_block(sig_child); sig_catch(sig_child,sigchld); sig_catch(sig_term,sigterm); sig_ignore(sig_pipe); for (ai = localai; ai; ai = ai->ai_next) { s = socket_tcp(ai->ai_family, ai->ai_protocol); if (s == -1) strerr_die2sys(111,FATAL,"unable to create socket: "); if (socket_bind_reuse(s,ai) == -1) strerr_die2sys(111,FATAL,"unable to bind: "); if (socket_local(s,&localaddr,&localport) == -1) strerr_die2sys(111,FATAL,"unable to get local address: "); if (socket_listen(s,backlog) == -1) strerr_die2sys(111,FATAL,"unable to listen: "); break; } freeaddrinfo(localai); localai = NULL; ndelay_off(s); localportstr[fmt_ulong(localportstr,localport)] = 0; if (flag1) { buffer_init(&b,buffer_unixwrite,1,bspace,sizeof bspace); buffer_puts(&b,localportstr); buffer_puts(&b,"\n"); buffer_flush(&b); } if (flag3) read_passwd(); ctx = ssl_server(); ssl_errstr(); if (!ctx) strerr_die2x(111,FATAL,"unable to create SSL context"); if (certchainfile) { switch (ssl_chainfile(ctx,certchainfile,keyfile,passwd_cb)) { case -1: strerr_die2x(111,FATAL,"unable to load certificate chain file"); case -2: strerr_die2x(111,FATAL,"unable to load key"); case -3: strerr_die2x(111,FATAL,"key does not match certificate"); default: break; } } else { switch (ssl_certkey(ctx,certfile,keyfile,passwd_cb)) { case -1: strerr_die2x(111,FATAL,"unable to load certificate"); case -2: strerr_die2x(111,FATAL,"unable to load key"); case -3: strerr_die2x(111,FATAL,"key does not match certificate"); default: break; } } if (!ssl_ca(ctx,cafile,cadir,verifydepth)) strerr_die2x(111,FATAL,"unable to load CA list"); if (!ssl_cca(ctx,ccafile)) strerr_die2x(111,FATAL,"unable to load client CA list"); if (!ssl_params(ctx,dhfile,rsalen)) strerr_die2x(111,FATAL,"unable to set DH/RSA parameters"); if (!ssl_ciphers(ctx,ciphers)) strerr_die2x(111,FATAL,"unable to set cipher list"); if (verbosity >= 2) { strnum[fmt_ulong(strnum,getpid())] = 0; strnum2[fmt_ulong(strnum2,rsalen)] = 0; strerr_warn4("sslserver: cafile ",strnum," ",cafile,0); strerr_warn4("sslserver: ccafile ",strnum," ",ccafile,0); strerr_warn4("sslserver: cadir ",strnum," ",cadir,0); strerr_warn4("sslserver: chainfile ",strnum," ",certchainfile,0); strerr_warn4("sslserver: cert ",strnum," ",certfile,0); strerr_warn4("sslserver: key ",strnum," ",keyfile,0); strerr_warn6("sslserver: param ",strnum," ",dhfile," ",strnum2,0); } close(0); open_read("/dev/null"); close(1); open_append("/dev/null"); printstatus(); for (;;) { while (numchildren >= limit) sig_pause(); sig_unblock(sig_child); t = socket_accept(s,&remoteaddr,&remoteport); sig_block(sig_child); if (t == -1) continue; ++numchildren; printstatus(); switch(fork()) { case 0: close(s); doit(t); strerr_die4sys(111,DROP,"unable to run ",*argv,": "); case -1: strerr_warn2(DROP,"unable to fork: ",&strerr_sys); --numchildren; printstatus(); } close(t); } }
int main(int argc,char **argv) { char *hostname, *x; int c, s, t; unsigned int u; unsigned int cpid = 0; opterr = 0; while ((c = getopt(argc, argv, "dDoOC:k:c:")) != -1) switch (c) { case 'c': limit = atoi(optarg); if (limit == 0) usage(); break; case 'd': flagdelay = 1; break; case 'D': flagdelay = 0; break; case 'O': flagkillopts = 1; break; case 'o': flagkillopts = 0; break; case 'C': cacheprogram = 1; break; case 'k': autokill = atoi(optarg); if (autokill == 0) usage(); break; default: abort(); } argc -= optind; argv += optind; hostname = *argv++; if (!hostname) usage(); x = *argv++; if (!x) usage(); u = 0; u = atoi(x); if (u != 0) localport = u; else usage(); if (!*argv) usage(); sig_block(sig_child); sig_catch(sig_child,sigchld); sig_catch(sig_term,sigterm); sig_catch(sig_int,sigint); sig_ignore(sig_pipe); inet_aton(hostname, (struct in_addr *) &localip); if (autokill != 0) pt = ptable_init(limit); s = socket_tcp(); if (s == -1) die(111, "unable to create socket"); if (socket_bind4_reuse(s,localip,localport) == -1) die(111, "unable to bind"); if (socket_local4(s,localip,&localport) == -1) die(111, "unable to get local address"); if (socket_listen(s,20) == -1) die(111, "unable to listen"); ndelay_off(s); fprintf(stderr, "bind: %s:%d\n", hostname, localport); close(0); close(1); printstatus(); if (cacheprogram) { FILE *fp1; int fp2; char path[1024]; ssize_t n; fp1 = popen(*argv, "r"); if (fp1 == NULL) { fprintf(stderr, "Failed to run command\n"); exit(1); } fp2 = open("/var/tmp/tcpd.cache", O_RDWR | O_CREAT, S_IRUSR | S_IWUSR); if (fp2 == -1) { fprintf(stderr, "Can't open cache file\n"); exit(1); } while ((n = fgets(path, sizeof(path)-1, fp1)) != NULL) { if (write(fp2, path, n) == n) { fprintf(stderr, "Error occured while creating cache\n"); exit(1); } } /* close */ pclose(fp1); close(fp2); // read cache file into memory FILE *f = fopen("/var/tmp/tcpd.cache", "rb"); fseek(f, 0, SEEK_END); cachesize = ftell(f); fseek(f, 0, SEEK_SET); //same as rewind(f); cache = malloc(cachesize + 1); n = fread(cache, cachesize, 1, f); fclose(f); cache[cachesize] = 0; } for (;;) { while (numchildren >= limit) { if (autokill != 0) ptable_autokill(pt, limit, autokill); sig_pause(); } sig_unblock(sig_child); t = socket_accept4(s,remoteip,&remoteport); sig_block(sig_child); if (t == -1) continue; ++numchildren; printstatus(); fprintf(stderr, "inbound connection from %d.%d.%d.%d:%d\n", (unsigned char) remoteip[0], (unsigned char) remoteip[1], (unsigned char) remoteip[2], (unsigned char) remoteip[3], remoteport); if (autokill != 0) ptable_autokill(pt,limit,autokill); cpid = fork(); switch(cpid) { case 0: close(s); if(flagkillopts) socket_ipoptionskill(t); if(!flagdelay) socket_tcpnodelay(t); if((fd_move(0,t) == -1) || (fd_copy(1,0) == -1)) die(111,"unable to setup descriptors"); sig_uncatch(sig_child); sig_unblock(sig_child); sig_uncatch(sig_term); sig_uncatch(sig_int); sig_uncatch(sig_pipe); if (cacheprogram) { printf("%s", cache); close(t); exit(0); } else { if(execve(*argv,argv,NULL) == 0) { close(t); exit(0); } else { die(111, "unable to run argv"); } } break; case -1: // unable to fork eprint(P_WARN,"unable to fork"); --numchildren; printstatus(); break; default: fprintf(stderr, "fork: child pid %d\n", cpid); if (autokill != 0) ptable_set(pt, limit, cpid, time(NULL)); break; } close(t); } }
int main(int argc, char **argv) { int opt; char *user =0; char *host; unsigned long port; int pid; int s; int conn; int delim; progname =*argv; phccmax =0; #ifdef SSLSVD while ((opt =getopt(argc, (const char **)argv, "c:C:i:x:u:l:Eb:hpt:vVU:/:Z:K:")) != opteof) { #else while ((opt =getopt(argc, (const char **)argv, "c:C:i:x:u:l:Eb:hpt:vV")) != opteof) { #endif switch(opt) { case 'c': scan_ulong(optarg, &cmax); if (cmax < 1) usage(); break; case 'C': delim =scan_ulong(optarg, &phccmax); if (phccmax < 1) usage(); if (optarg[delim] == ':') { if (ipsvd_fmt_msg(&msg, optarg +delim +1) == -1) die_nomem(); if (! stralloc_0(&msg)) die_nomem(); phccmsg =msg.s; } break; case 'i': if (instructs) usage(); instructs =optarg; break; case 'x': if (instructs) usage(); instructs =optarg; iscdb =1; break; case 'u': user =(char*)optarg; break; case 'l': if (! stralloc_copys(&local_hostname, optarg)) die_nomem(); if (! stralloc_0(&local_hostname)) die_nomem(); break; case 'E': ucspi =0; break; case 'b': scan_ulong(optarg, &backlog); break; case 'h': lookuphost =1; break; case 'p': lookuphost =1; paranoid =1; break; case 't': scan_ulong(optarg, &timeout); break; case 'v': ++verbose; break; #ifdef SSLSVD case 'U': ssluser =(char*)optarg; break; case '/': root =(char*)optarg; break; case 'Z': cert =(char*)optarg; break; case 'K': key =(char*)optarg; break; #endif case 'V': strerr_warn1(VERSION, 0); case '?': usage(); } } argv +=optind; if (! argv || ! *argv) usage(); host =*argv++; if (! argv || ! *argv) usage(); local_port =*argv++; if (! argv || ! *argv) usage(); prog =(const char **)argv; if (phccmax > cmax) phccmax =cmax; if (user) if (! uidgids_get(&ugid, user)) { if (errno) strerr_die4sys(111, FATAL, "unable to get user/group: ", user, ": "); strerr_die3x(100, FATAL, "unknown user/group: ", user); } #ifdef SSLSVD svuser =user; client =0; if ((getuid() == 0) && (! ssluser)) strerr_die2x(100, FATAL, "-U ssluser must be set when running as root"); if (ssluser) if (! uidgids_get(&sslugid, ssluser)) { if (errno) strerr_die4sys(111, FATAL, "unable to get user/group: ", ssluser, ": "); strerr_die3x(100, FATAL, "unknown user/group: ", ssluser); } if (! cert) cert ="./cert.pem"; if (! key) key =cert; if (matrixSslOpen() < 0) fatal("unable to initialize ssl"); if (matrixSslReadKeys(&keys, cert, key, 0, ca) < 0) { if (client) fatal("unable to read cert, key, or ca file"); fatal("unable to read cert or key file"); } if (matrixSslNewSession(&ssl, keys, 0, SSL_FLAGS_SERVER) < 0) strerr_die2x(111, FATAL, "unable to create ssl session"); #endif dns_random_init(seed); sig_block(sig_child); sig_catch(sig_child, sig_child_handler); sig_catch(sig_term, sig_term_handler); sig_ignore(sig_pipe); if (phccmax) if (ipsvd_phcc_init(cmax) == -1) die_nomem(); if (str_equal(host, "")) host ="0.0.0.0"; if (str_equal(host, "0")) host ="0.0.0.0"; if (! ipsvd_scan_port(local_port, "tcp", &port)) strerr_die3x(100, FATAL, "unknown port number or name: ", local_port); if (! stralloc_copys(&sa, host)) die_nomem(); if ((dns_ip4(&ips, &sa) == -1) || (ips.len < 4)) if (dns_ip4_qualify(&ips, &fqdn, &sa) == -1) fatal2("unable to look up ip address", host); if (ips.len < 4) strerr_die3x(100, FATAL, "unable to look up ip address: ", host); ips.len =4; if (! stralloc_0(&ips)) die_nomem(); local_ip[ipsvd_fmt_ip(local_ip, ips.s)] =0; if (! lookuphost) { if (! stralloc_copys(&remote_hostname, "")) die_nomem(); if (! stralloc_0(&remote_hostname)) die_nomem(); } if ((s =socket_tcp()) == -1) fatal("unable to create socket"); if (socket_bind4_reuse(s, ips.s, port) == -1) fatal("unable to bind socket"); if (listen(s, backlog) == -1) fatal("unable to listen"); ndelay_off(s); #ifdef SSLSVD #else if (user) { /* drop permissions */ if (setgroups(ugid.gids, ugid.gid) == -1) fatal("unable to set groups"); if (setgid(*ugid.gid) == -1) fatal("unable to set gid"); if (prot_uid(ugid.uid) == -1) fatal("unable to set uid"); } #endif close(0); if (verbose) { out(INFO); out("listening on "); outfix(local_ip); out(":"); outfix(local_port); #ifdef SSLSVD #else if (user) { bufnum[fmt_ulong(bufnum, (unsigned long)ugid.uid)] =0; out(", uid "); out(bufnum); bufnum[fmt_ulong(bufnum, (unsigned long)ugid.gid)] =0; out(", gid "); out(bufnum); } #endif flush(", starting.\n"); } for (;;) { while (cnum >= cmax) sig_pause(); socka_size =sizeof(socka); sig_unblock(sig_child); conn =accept(s, (struct sockaddr *)&socka, &socka_size); sig_block(sig_child); if (conn == -1) { if (errno != error_intr) warn("unable to accept connection"); continue; } cnum++; if (verbose) connection_status(); if (phccmax) phcc =ipsvd_phcc_add((char*)&socka.sin_addr); if ((pid =fork()) == -1) { warn2("drop connection", "unable to fork"); close(conn); continue; } if (pid == 0) { /* child */ close(s); #ifdef SSLSVD if (*progname) *progname ='\\'; #endif connection_accept(conn); } if (phccmax) ipsvd_phcc_setpid(pid); close(conn); } _exit(0); }
int main(int argc,char **argv) { char *hostname; // char *portname; int opt; struct servent *se; char *x; unsigned long u; int s; int t; while ((opt = getopt(argc,argv,"4dDvqQhHrR1UXx:t:u:g:l:b:B:c:I:pPoO")) != opteof) switch(opt) { case 'b': scan_ulong(optarg,&backlog); break; case 'c': scan_ulong(optarg,&limit); break; case 'X': flagallownorules = 1; break; case 'x': fnrules = optarg; break; case 'B': banner = optarg; break; case 'd': flagdelay = 1; break; case 'D': flagdelay = 0; break; case 'v': verbosity = 2; break; case 'q': verbosity = 0; break; case 'Q': verbosity = 1; break; case 'P': flagparanoid = 0; break; case 'p': flagparanoid = 1; break; case 'O': flagkillopts = 1; break; case 'o': flagkillopts = 0; break; case 'H': flagremotehost = 0; break; case 'h': flagremotehost = 1; break; // case 'R': flagremoteinfo = 0; break; case 'r': flagremoteinfo = 1; break; case 't': scan_ulong(optarg,&timeout); break; case 'U': x = env_get("UID"); if (x) scan_ulong(x,&uid); x = env_get("GID"); if (x) scan_ulong(x,&gid); break; case 'u': scan_ulong(optarg,&uid); break; case 'g': scan_ulong(optarg,&gid); break; case 'I': netif=socket_getifidx(optarg); break; case '1': flag1 = 1; break; // case '4': noipv6 = 1; break; case '4': ipv4socket = 1; break; // case '6': forcev6 = 1; break; case 'l': localhost = optarg; break; default: usage(); } argc -= optind; argv += optind; if (!verbosity) buffer_2->fd = -1; hostname = *argv++; if (!hostname) usage(); if (str_equal(hostname,"")) hostname = "0"; x = *argv++; if (!x) usage(); if (!x[scan_ulong(x,&u)]) localport = u; else { se = getservbyname(x,"tcp"); if (!se) errint(EHARD,B("unable to figure out port number for ",x)); uint16_unpack_big((char*)&se->s_port,&localport); } if (!*argv) usage(); sig_block(sig_child); sig_catch(sig_child,sigchld); sig_catch(sig_term,sigterm); sig_ignore(sig_pipe); if (str_equal(hostname,"0")) { byte_zero(localip,sizeof localip); } else { if (!stralloc_copys(&tmp,hostname)) errmem; if (dns_ip6_qualify(&addresses,&fqdn,&tmp) == -1) errint(EHARD,B("temporarily unable to figure out IP address for ",hostname,": ")); if (addresses.len < 16) errint(EHARD,B("no IP address for ",hostname)); byte_copy(localip,16,addresses.s); if (ip6_isv4mapped(localip)) ipv4socket = 1; } s = socket_tcp(); if (s == -1) errint(EHARD,"unable to create socket: "); if (socket_bind_reuse(s,localip,localport,netif) == -1) errint(EHARD,"unable to bind: "); if (!ipv4socket) ipv4socket = ip6_isv4mapped(localip); if (socket_local(s,localip,&localport,&netif) == -1) errint(EHARD,"unable to get local address: "); if (socket_listen(s,backlog) == -1) errint(EHARD,"unable to listen: "); ndelay_off(s); if (gid) if (prot_gid(gid) == -1) errint(EHARD,"unable to set gid: "); if (uid) if (prot_uid(uid) == -1) errint(EHARD,"unable to set uid: "); localportstr[fmt_ulong(localportstr,localport)] = 0; if (flag1) { buffer_init(&b,write,1,bspace,sizeof bspace); buffer_puts(&b,localportstr); buffer_puts(&b,"\n"); buffer_flush(&b); } close(0); close(1); printstatus(); for (;;) { while (numchildren >= limit) sig_pause(); sig_unblock(sig_child); t = socket_accept(s,remoteip,&remoteport,&netif); sig_block(sig_child); if (t == -1) continue; ++numchildren; printstatus(); switch(fork()) { case 0: close(s); doit(t); if ((fd_move(0,t) == -1) || (fd_copy(1,0) == -1)) errint(EHARD,"unable to set up descriptors: "); sig_uncatch(sig_child); sig_unblock(sig_child); sig_uncatch(sig_term); sig_uncatch(sig_pipe); pathexec(argv); errint(EHARD,B("unable to run ",*argv,": ")); case -1: errlog(ESOFT,NOTICE,"unable to fork: "); --numchildren; printstatus(); } close(t); } }