fixes server user init

Sat, 14 Jan 2017 11:27:55 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 14 Jan 2017 11:27:55 +0100
changeset 146
a9591a91c004
parent 145
1c93281ca4bf
child 147
d050449c3b9e

fixes server user init

make/clang.mk file | annotate | diff | comparison | revisions
make/gcc.mk file | annotate | diff | comparison | revisions
src/server/daemon/main.c file | annotate | diff | comparison | revisions
src/server/daemon/webserver.c file | annotate | diff | comparison | revisions
--- a/make/clang.mk	Fri Jan 13 09:53:55 2017 +0100
+++ b/make/clang.mk	Sat Jan 14 11:27:55 2017 +0100
@@ -27,7 +27,7 @@
 #
 
 CFLAGS = -g
-LDFLAGS = -Wl,-R,'$$ORIGIN/../lib'
+LDFLAGS = -rdynamic -Wl,-R,'$$ORIGIN/../lib'
 
 CC  = clang
 CXX = clang++
--- a/make/gcc.mk	Fri Jan 13 09:53:55 2017 +0100
+++ b/make/gcc.mk	Sat Jan 14 11:27:55 2017 +0100
@@ -27,7 +27,7 @@
 #
 
 CFLAGS = -std=gnu99 -g
-LDFLAGS = -Wl,-R,'$$ORIGIN/../lib'
+LDFLAGS = -rdynamic -Wl,-R,'$$ORIGIN/../lib'
 
 CC  = gcc
 CXX = g++
--- a/src/server/daemon/main.c	Fri Jan 13 09:53:55 2017 +0100
+++ b/src/server/daemon/main.c	Sat Jan 14 11:27:55 2017 +0100
@@ -154,6 +154,7 @@
     signal(SIGINT, sig_term);
     
     struct sigaction act;
+    ZERO(&act, sizeof(struct sigaction));
     act.sa_handler = SIG_IGN;
     sigaction(SIGPIPE, &act, NULL);
 
--- a/src/server/daemon/webserver.c	Fri Jan 13 09:53:55 2017 +0100
+++ b/src/server/daemon/webserver.c	Sat Jan 14 11:27:55 2017 +0100
@@ -97,7 +97,7 @@
 
     char *pid_file_path = NULL;
     asprintf(&pid_file_path, "%s/pid", cfg->tmp.ptr);
-    FILE *pidfile = fopen(pid_file_path, "w");
+    FILE *pidfile = fopen(pid_file_path, "w"); // TODO: check error
     pid_t pid = getpid();
     fprintf(pidfile, "%d", pid);
     fclose(pidfile);
@@ -106,6 +106,7 @@
     // set global vars
     conf_global_vars_s *vars = conf_getglobals();
     
+    WSBool changeuid = FALSE;
     uid_t ws_uid = geteuid();
     setpwent();
     char *pwbuf = malloc(DEF_PWBUF);
@@ -118,6 +119,8 @@
                     cfg->user.ptr);
             free(vars->Vuserpw);
             vars->Vuserpw = NULL;
+        } else {
+            changeuid = TRUE;
         }
     } else {
         if(!util_getpwuid(ws_uid, vars->Vuserpw, pwbuf, DEF_PWBUF)) {
@@ -129,25 +132,9 @@
     if(!vars->Vuserpw) {
         log_ereport(LOG_WARN, "globalvars->Vuserpw is null");
     }
-    
-    if(cfg->user.ptr) {
-        vars->Vuserpw = malloc(sizeof(struct passwd));
-        // open user database
-        //setpwent();
-        if(!util_getpwnam(cfg->user.ptr, vars->Vuserpw, pwbuf, DEF_PWBUF)) {
-            log_ereport(
-                    LOG_MISCONFIG,
-                    "user %s does not exist!",
-                    cfg->user.ptr);
-            free(vars->Vuserpw);
-            vars->Vuserpw = NULL;
-        }
-        //endpwent(); // TODO: close or not?
-    }
-    free(pwbuf);
 
     // change uid
-    if(vars->Vuserpw && ws_uid == 0) {
+    if(changeuid && ws_uid == 0) {
         // a webserver user is set and we are root
         
         if(setgid(vars->Vuserpw->pw_gid) != 0) {
@@ -176,6 +163,9 @@
                     "server must be started as root to change uid");
     }
     
+    //endpwent(); // TODO: close or not?
+    //free(pwbuf); // TODO: ?
+    
     return 0;
 }
 

mercurial