changeset 1860:0d09c435ad0f

Make it possible to set UID and GID for privilege dropping.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 02 Nov 2017 15:05:34 +0200
parents e52b2ce7c287
children f865ee78711e
files mapsearch.c
diffstat 1 files changed, 41 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/mapsearch.c	Thu Nov 02 15:04:57 2017 +0200
+++ b/mapsearch.c	Thu Nov 02 15:05:34 2017 +0200
@@ -8,6 +8,10 @@
 #include "liblocfile.h"
 #include "libmaputils.h"
 #include <libwebsockets.h>
+#include <sys/types.h>
+#include <pwd.h>
+#include <grp.h>
+
 
 
 /* Default settings etc. constants
@@ -88,6 +92,7 @@
 int     optWorldXC = 0, optWorldYC = 0;
 char    *optTest = NULL;
 unsigned char *setLWSBuffer = NULL;
+int     optUID = -1, optGID = -1;
 
 
 /* Arguments
@@ -101,6 +106,8 @@
     { 4,   0, "clean-chars",   "String of characters to 'clean up' from map blocks.", OPT_ARGREQ },
     { 5, 'w', "world-origin",  "Specify the world origin <x:y> which map offsets relate to.", OPT_ARGREQ },
     { 6, 'T', "test",          "Test search with given file input", OPT_ARGREQ },
+    { 7, 'U', "uid",           "Run as UID", OPT_ARGREQ },
+    { 8, 'G', "gid",           "Run as GID", OPT_ARGREQ },
 };
 
 static const int optListN = sizeof(optList) / sizeof(optList[0]);
@@ -414,6 +421,34 @@
         optTest = optArg;
         break;
 
+    case 7:
+        if (sscanf(optArg, "%d", &optUID) != 1)
+        {
+            struct passwd *info = getpwnam(optArg);
+            if (info != NULL)
+                optUID = info->pw_uid;
+            else
+            {
+                THERR("Invalid UID '%s'.\n", optArg);
+                return FALSE;
+            }
+        }
+        break;
+
+    case 8:
+        if (sscanf(optArg, "%d", &optGID) != 1)
+        {
+            struct group *info = getgrnam(optArg);
+            if (info != NULL)
+                optUID = info->gr_gid;
+            else
+            {
+                THERR("Invalid GID '%s'.\n", optArg);
+                return FALSE;
+            }
+        }
+        break;
+
     default:
         THERR("Unknown option '%s'.\n", currArg);
         return FALSE;
@@ -1133,12 +1168,16 @@
         goto exit;
     }
 
+    // Setup log handler
     lws_set_log_level(LLL_ERR | LLL_WARN | LLL_NOTICE, mapLogStr);
 
+    // Create the LWS context(s)
     MAPListenerCtx *ctx = optListenTo[0];
     struct lws_context_creation_info info;
     memset(&info, 0, sizeof(info));
 
+    info.gid = optGID;
+    info.uid = optUID;
     info.port = ctx->port;
     info.iface = ctx->interface;
     info.max_http_header_pool = 16;
@@ -1197,6 +1236,8 @@
         }
     }
 
+    lws_finalize_startup(setLWSContext);
+
     // Set up signal handlers
     THMSG(1, "Setting up signal handlers.\n");
     lws_uv_sigint_cfg(setLWSContext, 1, mapSigHandler);