2
0
Эх сурвалжийг харах

*** empty log message ***

Andrei Pelinescu-Onciul 24 жил өмнө
parent
commit
c9ca45b380
2 өөрчлөгдсөн 54 нэмэгдсэн , 4 устгасан
  1. 3 0
      TODO
  2. 51 4
      main.c

+ 3 - 0
TODO

@@ -2,6 +2,7 @@ $Id$
 
 ( - todo, x - done)
 
+
 - fix parse_cseq!!! (it doesnt parse 1234\n INVITE a.s.o)
 x fix 0 parameter module f. call
 x better Via parsing (handle ' ' in uri, eg: foo.bar : 1234 ; received=) and
@@ -21,6 +22,8 @@ x plugin interface
 - add User-Agent (for the replies)
 
 Low priority:
+- add support for -u user and -g group (not only -u uid, -g uid)
+- change uid/gid after opening the sockets
 - exec improvments (add format strings to it)
 - command line switch for checking the config file syntax
 - config file version (a la sendmail)

+ 51 - 4
main.c

@@ -110,7 +110,11 @@ Options:\n\
     -V           Version number\n\
     -h           This help message\n\
     -b nr        Maximum receive buffer size which will not be exceeded by\n\
-                 auto-probing procedure even if  OS allows\n"
+                 auto-probing procedure even if  OS allows\n\
+    -w  dir      change the working directory to \"dir\" (default \"/\")\n\
+    -t  dir      chroot to \"dir\"\n\
+    -u uid       change uid \n\
+    -g gid       change gid \n"
 #ifdef STATS
 "    -s file	 File to which statistics is dumped (disabled otherwise)\n"
 #endif
@@ -159,6 +163,10 @@ int check_via =  0;        /* check if reply first via host==us */
 int loop_checks = 0;	/* calculate branches and check for loops/spirals */
 int received_dns = 0;      /* use dns and/or rdns or to see if we need to 
                               add a ;received=x.x.x.x to via: */
+char* working_dir = 0;
+char* chroot_dir = 0;
+int uid = 0;
+int gid = 0;
 
 char* names[MAX_LISTEN];               /* our names */
 int names_len[MAX_LISTEN];    /* lengths of the names*/
@@ -194,11 +202,27 @@ int daemonize(char*  name)
 		openlog(name, LOG_PID|LOG_CONS, LOG_LOCAL1 /*LOG_DAEMON*/);
 		/* LOG_CONS, LOG_PERRROR ? */
 
-	if (chdir("/")<0){
-		LOG(L_CRIT,"cannot chroot:%s\n", strerror(errno));
+
+	if (chroot_dir&&(chroot(chroot_dir)<0)){
+		LOG(L_CRIT, "Cannot chroot to %s: %s\n", chroot_dir, strerror(errno));
 		goto error;
 	}
 	
+	if (chdir(working_dir)<0){
+		LOG(L_CRIT,"cannot chdir to %s: %s\n", working_dir, strerror(errno));
+		goto error;
+	}
+
+	if (gid&&(setgid(gid)<0)){
+		LOG(L_CRIT, "cannot change gid to %d: %s\n", gid, strerror(errno));
+		goto error;
+	}
+	
+	if(uid&&(setuid(uid)<0)){
+		LOG(L_CRIT, "cannot change uid to %d: %s\n", uid, strerror(errno));
+		goto error;
+	}
+
 	/* fork to become!= group leader*/
 	if ((pid=fork())<0){
 		LOG(L_CRIT, "Cannot fork:%s\n", strerror(errno));
@@ -437,7 +461,7 @@ int main(int argc, char** argv)
 #ifdef STATS
 	"s:"
 #endif
-	"f:p:b:l:n:rRvcdDEVh";
+	"f:p:b:l:n:rRvcdDEVhw:t:u:g:";
 	
 	while((c=getopt(argc,argv,options))!=-1){
 		switch(c){
@@ -521,6 +545,27 @@ int main(int argc, char** argv)
 					printf("%s",help_msg);
 					exit(0);
 					break;
+			case 'w':
+					working_dir=optarg;
+					break;
+			case 't':
+					chroot_dir=optarg;
+					break;
+			case 'u':
+					uid=strtol(optarg, &tmp, 10);
+					if ((tmp==0) ||(*tmp)){
+						fprintf(stderr, "bad uid number: -u %s\n", optarg);
+						goto error;
+					}
+					/* test if string?*/
+					break;
+			case 'g':
+					gid=strtol(optarg, &tmp, 10);
+					if ((tmp==0) ||(*tmp)){
+						fprintf(stderr, "bad gid number: -g %s\n", optarg);
+						goto error;
+					}
+					break;
 			case '?':
 					if (isprint(optopt))
 						fprintf(stderr, "Unknown option `-%c´.\n", optopt);
@@ -598,6 +643,8 @@ int main(int argc, char** argv)
 			MAX_PROCESSES-1 );
 		goto error;
 	}
+	
+	if (working_dir==0) working_dir="/";
 	/*alloc pids*/
 #ifdef SHM_MEM
 	pids=shm_malloc(sizeof(int)*children_no);