From: Nishi Date: Sat, 7 Sep 2024 12:44:18 +0000 (+0000) Subject: add uid/gid switching X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=a4f16384059aead86a08286cccf762a16fd2e1d4;p=mokou.git add uid/gid switching git-svn-id: file:///raid/svn-personal/mokou/trunk@13 35d6bad2-6c5c-c749-ada2-a2c82cb3bd79 --- diff --git a/Mokou/mk_service.h b/Mokou/mk_service.h index 23132b9..9c2acd3 100644 --- a/Mokou/mk_service.h +++ b/Mokou/mk_service.h @@ -4,6 +4,7 @@ #define __MK_SERVICE_H__ #include +#include struct mk_service { char* name; @@ -12,6 +13,8 @@ struct mk_service { char* pidfile; char* stop; bool stopped; + uid_t uid; + gid_t gid; }; void mk_service_scan(void); diff --git a/Mokou/service.c b/Mokou/service.c index fa0ac24..a9c5b67 100644 --- a/Mokou/service.c +++ b/Mokou/service.c @@ -98,6 +98,9 @@ void mk_service_scan(void){ char* exec = NULL; char* stop = NULL; char* pidfile = NULL; + uid_t uid = 0; + gid_t gid = 0; + bool bad = false; for(i = 0;; i++){ if(buffer[i] == '\n' || buffer[i] == 0){ @@ -127,6 +130,15 @@ void mk_service_scan(void){ }else if(strcmp(key, "stop") == 0){ if(stop != NULL) free(stop); stop = mk_strdup(value); + }else if(strcmp(key, "user") == 0){ + struct passwd* p = getpwnam(value); + if(p != NULL){ + uid = p->pw_uid; + gid = p->pw_gid; + }else{ + mk_log("Could not find the specified user"); + bad = true; + } } break; @@ -140,7 +152,6 @@ void mk_service_scan(void){ } fclose(f); - bool bad = false; if(exec == NULL){ char* log = mk_strcat(desc == NULL ? path : desc, ": Missing exec"); mk_log(log); @@ -174,6 +185,8 @@ void mk_service_scan(void){ serv->stop = stop != NULL ? mk_strdup(stop) : NULL; serv->exec = mk_strdup(exec); serv->pidfile = mk_strdup(pidfile); + serv->uid = uid; + serv->gid = gid; serv->stopped = false; struct mk_service** oldsrvs = services; @@ -291,6 +304,10 @@ int mk_stop_service(const char* name){ int n = open("/dev/null", O_RDWR); dup2(n, 1); dup2(n, 2); + setgid(srv->gid); + setegid(srv->gid); + setuid(srv->uid); + seteuid(srv->uid); execvp(pargv[0], pargv); _exit(-1); }else{ @@ -388,6 +405,10 @@ int mk_start_service(const char* name){ int n = open("/dev/null", O_RDWR); dup2(n, 1); dup2(n, 2); + setgid(srv->gid); + setegid(srv->gid); + setuid(srv->uid); + seteuid(srv->uid); execvp(pargv[0], pargv); _exit(-1); }else{