Sfoglia il codice sorgente

- when starting ser in suid mode (e..g -u user), set also the supplementary
groups of the respective user.
Patch from Marcus Better <[email protected]>.

Andrei Pelinescu-Onciul 17 anni fa
parent
commit
d32f7ac428
1 ha cambiato i file con 13 aggiunte e 0 eliminazioni
  1. 13 0
      daemonize.c

+ 13 - 0
daemonize.c

@@ -57,6 +57,8 @@
 #include <sys/time.h>    
 #include <sys/time.h>    
 #include <sys/resource.h> /* setrlimit */
 #include <sys/resource.h> /* setrlimit */
 #include <unistd.h>
 #include <unistd.h>
+#include <pwd.h>
+#include <grp.h>
 
 
 #ifdef HAVE_SCHED_SETSCHEDULER
 #ifdef HAVE_SCHED_SETSCHEDULER
 #include <sched.h>
 #include <sched.h>
@@ -223,6 +225,8 @@ error:
 
 
 int do_suid()
 int do_suid()
 {
 {
+	struct passwd *pw;
+	
 	if (gid){
 	if (gid){
 		if(setgid(gid)<0){
 		if(setgid(gid)<0){
 			LOG(L_CRIT, "cannot change gid to %d: %s\n", gid, strerror(errno));
 			LOG(L_CRIT, "cannot change gid to %d: %s\n", gid, strerror(errno));
@@ -231,6 +235,15 @@ int do_suid()
 	}
 	}
 	
 	
 	if(uid){
 	if(uid){
+		if (!(pw = getpwuid(uid))){
+			LOG(L_CRIT, "user lookup failed: %s\n", strerror(errno));
+			goto error;
+		}
+		if(initgroups(pw->pw_name, pw->pw_gid)<0){
+			LOG(L_CRIT, "cannot set supplementary groups: %s\n", 
+							strerror(errno));
+			goto error;
+		}
 		if(setuid(uid)<0){
 		if(setuid(uid)<0){
 			LOG(L_CRIT, "cannot change uid to %d: %s\n", uid, strerror(errno));
 			LOG(L_CRIT, "cannot change uid to %d: %s\n", uid, strerror(errno));
 			goto error;
 			goto error;