From 25d54f68d62eb4d63bee5019803f0c6eca752b8e Mon Sep 17 00:00:00 2001 From: Andrew Karpow Date: Wed, 23 Apr 2014 12:17:55 +0200 Subject: [PATCH] drop privliges after port binding, fixes #12 --- src/ympd.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/ympd.c b/src/ympd.c index 52bd364..53ad470 100644 --- a/src/ympd.c +++ b/src/ympd.c @@ -56,6 +56,7 @@ int main(int argc, char **argv) int n, option_index = 0; struct mg_server *server = mg_create_server(NULL); unsigned int current_timer = 0, last_timer = 0; + char *run_as_user = NULL; atexit(bye); mg_set_option(server, "listening_port", "8080"); @@ -84,7 +85,7 @@ int main(int argc, char **argv) mg_set_option(server, "listening_port", optarg); break; case 'u': - mg_set_option(server, "run_as_user", optarg); + run_as_user = strdup(optarg); break; case 'v': fprintf(stdout, "ympd %d.%d.%d\n" @@ -106,6 +107,13 @@ int main(int argc, char **argv) } } + /* drop privilges at last to ensure proper port binding */ + if(run_as_user != NULL) + { + mg_set_option(server, "run_as_user", run_as_user); + free(run_as_user); + } + mg_set_http_close_handler(server, mpd_close_handler); mg_set_request_handler(server, server_callback); while (!force_exit) { -- 2.20.1