From: Pierre Habouzit Date: Sat, 1 Dec 2007 13:41:41 +0000 (+0100) Subject: listen on a port (postlicyd) X-Git-Url: http://git.madism.org/?a=commitdiff_plain;h=a73d0a8f6e71ff1637cacf7dc6cbd622aeddbec4;p=apps%2Fpfixtools.git listen on a port (postlicyd) Signed-off-by: Pierre Habouzit --- diff --git a/main-postlicyd.c b/main-postlicyd.c index 4cf059b..f9b96f5 100644 --- a/main-postlicyd.c +++ b/main-postlicyd.c @@ -41,6 +41,9 @@ #include "tokens.h" #define DAEMON_NAME "postlicyd" +#define DEFAULT_PORT 10000 +#define RUNAS_USER "nobody" +#define RUNAS_GROUP "nogroup" enum smtp_state { SMTP_UNKNOWN, @@ -247,6 +250,7 @@ void usage(void) fputs("usage: "DAEMON_NAME" [options] config\n" "\n" "Options:\n" + " -l port to listen to\n" " -p file to write our pid to\n" , stderr); } @@ -255,14 +259,22 @@ void usage(void) int main(int argc, char *argv[]) { + struct sockaddr_in addr = { + .sin_family = AF_INET, + .sin_addr = { htonl(INADDR_LOOPBACK) }, + }; const char *pidfile = NULL; + int port = DEFAULT_PORT; int sock = -1; - for (int c = 0; (c = getopt(argc, argv, "h" "p:")) >= 0; ) { + for (int c = 0; (c = getopt(argc, argv, "h" "l:p:")) >= 0; ) { switch (c) { case 'p': pidfile = optarg; break; + case 'l': + port = atoi(optarg); + break; default: usage(); return EXIT_FAILURE; @@ -279,6 +291,11 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } + if (drop_privileges(RUNAS_USER, RUNAS_GROUP) < 0) { + syslog(LOG_CRIT, "unable to drop privileges"); + return EXIT_FAILURE; + } + if (daemon_detach() < 0) { syslog(LOG_CRIT, "unable to fork"); return EXIT_FAILURE; @@ -286,6 +303,11 @@ int main(int argc, char *argv[]) pidfile_refresh(); + addr.sin_port = htons(port); + sock = tcp_listen((struct sockaddr *)&addr, sizeof(addr)); + if (sock < 0) + return EXIT_FAILURE; + while (!sigint) { int fd = accept(sock, NULL, 0); if (fd < 0) { diff --git a/main-srsd.c b/main-srsd.c index 79f9e19..0bf5bae 100644 --- a/main-srsd.c +++ b/main-srsd.c @@ -42,8 +42,8 @@ #include "buffer.h" #define DAEMON_NAME "pfix-srsd" -#define DEFAULT_ENCODER_PORT 10000 -#define DEFAULT_DECODER_PORT 10001 +#define DEFAULT_ENCODER_PORT 10001 +#define DEFAULT_DECODER_PORT 10002 #define RUNAS_USER "nobody" #define RUNAS_GROUP "nogroup"