From f5865886b2c6ec98e5a3f4359070c8ca46d20bd3 Mon Sep 17 00:00:00 2001 From: Sergey Lyubka <valenok@gmail.com> Date: Thu, 12 Dec 2013 20:31:43 +0000 Subject: [PATCH] Respecting -run_as_user --- mongoose.c | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/mongoose.c b/mongoose.c index b5078b251..a96c38fb7 100644 --- a/mongoose.c +++ b/mongoose.c @@ -84,11 +84,12 @@ typedef struct _stati64 file_stat_t; #define __func__ __FILE__ ":" STR(__LINE__) #endif #else -#include <inttypes.h> #include <dirent.h> -#include <unistd.h> +#include <inttypes.h> #include <pthread.h> +#include <pwd.h> #include <signal.h> +#include <unistd.h> #include <arpa/inet.h> // For inet_pton() when USE_IPV6 is defined #include <netinet/in.h> #include <sys/socket.h> @@ -3298,6 +3299,17 @@ const char *mg_set_option(struct mg_server *server, const char *name, } else { set_non_blocking_mode(server->listening_sock); } + } else if (ind == RUN_AS_USER) { +#ifndef _WIN32 + struct passwd *pw; + if ((pw = getpwnam(value)) == NULL) { + error_msg = "Unknown user"; + } else if (setgid(pw->pw_gid) != 0) { + error_msg = "setgid() failed"; + } else if (setuid(pw->pw_uid) != 0) { + error_msg = "setuid() failed"; + } +#endif } } -- GitLab