From 260b63d6c5c2b998733269499018d5e5977ec3cf Mon Sep 17 00:00:00 2001
From: obarun <eric@obarun.org>
Date: Wed, 18 Sep 2019 23:43:23 +1100
Subject: [PATCH] chown log directory with log runner account

---
 src/include/66/parser.h  | 22 +++------------
 src/lib66/parser_write.c | 58 +++++++++++++++++++---------------------
 2 files changed, 31 insertions(+), 49 deletions(-)

diff --git a/src/include/66/parser.h b/src/include/66/parser.h
index 0119b89b..698a4ed1 100644
--- a/src/include/66/parser.h
+++ b/src/include/66/parser.h
@@ -56,7 +56,7 @@ struct sv_exec_s
 {
 	/**build=45->auto,build=46->custom*/
 	int build ;
-	uid_t runas ;
+	unsigned int runas ;
 	unsigned int shebang ;
 	unsigned int exec ;
 } ;
@@ -257,14 +257,6 @@ extern int parse_service_check_enabled(ssexec_t *info, char const *svname,uint8_
 extern int parse_service_before(ssexec_t *info, stralloc *parsed_list, char const *sv,unsigned int *nbsv, stralloc *sasv,uint8_t force,uint8_t *exist) ;
 extern int parse_service_deps(ssexec_t *info,stralloc *parsed_list, sv_alltype *sv_before, char const *sv,unsigned int *nbsv,stralloc *sasv,uint8_t force) ;
 extern int parse_add_service(stralloc *parsed_list,sv_alltype *sv_before,char const *service,unsigned int *nbsv,uid_t owner) ;
-/** mill utilities 
-extern parse_mill_t MILL_FIRST_BRACKET ;
-extern parse_mill_t MILL_GET_AROBASE_KEY ;
-extern parse_mill_t MILL_GET_COMMENTED_KEY ;
-extern parse_mill_t MILL_GET_SECTION_NAME ; */
-/** utilities 
-extern int parse_line(stralloc *src,size_t *pos) ;
-extern int parse_bracket(stralloc *src,size_t *pos) ; */
 /** split */
 extern int section_get_range(section_t *sasection,stralloc *src) ;
 extern int key_get_range(genalloc *ga, section_t *sasection,int *svtype) ;
@@ -274,13 +266,7 @@ extern int nocheck_toservice(keynocheck *nocheck,int svtype, sv_alltype *service
 extern int keep_common(sv_alltype *service,keynocheck *nocheck,int svtype) ;
 extern int keep_runfinish(sv_exec *exec,keynocheck *nocheck) ;
 extern int keep_logger(sv_execlog *log,keynocheck *nocheck) ;
-/** helper 
-extern void section_setsa(int id, stralloc_ref *p,section_t *sa) ;
-extern int section_get_skip(char const *s,size_t pos,int nline) ;
-extern int section_get_id(stralloc *secname, char const *string,size_t *pos,int *id) ;
-extern int key_get_next_id(stralloc *sa, char const *string,size_t *pos) ;
-extern void parse_err(int ierr,int idsec,int idkey) ; */
-extern int read_svfile(stralloc *sasv,char const *name,char const *src) ;
+/** helper */
 extern int add_pipe(sv_alltype *sv, stralloc *sa) ;
 /** write */
 extern int write_services(ssexec_t *info,sv_alltype *sv, char const *workdir, uint8_t force,uint8_t conf) ;
@@ -289,9 +275,9 @@ extern int write_longrun(sv_alltype *sv,char const *dst, uint8_t force, uint8_t
 extern int write_oneshot(sv_alltype *sv,char const *dst, uint8_t conf) ;
 extern int write_bundle(sv_alltype *sv, char const *dst) ;
 extern int write_common(sv_alltype *sv, char const *dst,uint8_t conf) ;
-extern int write_exec(sv_alltype *sv, sv_exec *exec,char const *name,char const *dst,int mode) ;
+extern int write_exec(sv_alltype *sv, sv_exec *exec,char const *name,char const *dst,mode_t mode) ;
 extern int write_uint(char const *dst, char const *name, uint32_t ui) ;
-extern int write_logger(sv_alltype *sv, sv_execlog *log,char const *name, char const *dst, int mode, uint8_t force) ;
+extern int write_logger(sv_alltype *sv, sv_execlog *log,char const *name, char const *dst, mode_t mode, uint8_t force) ;
 extern int write_consprod(sv_alltype *sv,char const *prodname,char const *consname,char const *proddst,char const *consdst) ;
 extern int write_dependencies(unsigned int nga,unsigned int idga,char const *dst,char const *filename) ;
 extern int write_env(char const *name,stralloc *sa,char const *dst) ;
diff --git a/src/lib66/parser_write.c b/src/lib66/parser_write.c
index fe92293d..5a4227bc 100644
--- a/src/lib66/parser_write.c
+++ b/src/lib66/parser_write.c
@@ -314,15 +314,18 @@ int write_bundle(sv_alltype *sv, char const *dst)
 	return 1 ;
 }
 
-int write_logger(sv_alltype *sv, sv_execlog *log,char const *name, char const *dst, int mode, uint8_t force)
+int write_logger(sv_alltype *sv, sv_execlog *log,char const *name, char const *dst, mode_t mode, uint8_t force)
 {
 	int r ;
 	int logbuild = log->run.build ;
 	
-	char *time = NULL ;
-	char *pmax = NULL ;
-	char *pback = NULL ;
+	uid_t log_uid ;
+	gid_t log_gid ;
+	char *time = 0 ;
+	char *pmax = 0 ;
+	char *pback = 0 ;
 	char *timestamp = "t" ;
+	char *logrunner = log->run.runas ? keep.s + log->run.runas : SS_LOGGER_RUNNER ;
 	char max[UINT32_FMT] ;
 	char back[UINT32_FMT] ;
 	char const *userhome ;
@@ -416,18 +419,10 @@ int write_logger(sv_alltype *sv, sv_execlog *log,char const *name, char const *d
 			/** uid */
 			if (!stralloc_cats(&shebang, "#!" EXECLINE_SHEBANGPREFIX "execlineb -P\n")) retstralloc(0,"write_logger") ;
 			if (!stralloc_0(&shebang)) retstralloc(0,"write_logger") ;
-			if ((!MYUID))// && log->run.runas))
+			if ((!MYUID))
 			{
 				if (!stralloc_cats(&ui,S6_BINPREFIX "s6-setuidgid ")) retstralloc(0,"write_logger") ;
-				if (log->run.runas)
-				{
-					if (!get_namebyuid(log->run.runas,&ui))
-					{
-						VERBO3 strerr_warnwu1sys("set owner for the logger") ;
-						return 0 ;
-					}
-				}
-				else if (!stralloc_cats(&ui,SS_LOGGER_RUNNER)) retstralloc(0,"write_logger") ;
+				if (!stralloc_cats(&ui,logrunner)) retstralloc(0,"write_logger") ;
 			}
 			if (!stralloc_cats(&ui,"\n")) retstralloc(0,"write_logger") ;
 			if (!stralloc_0(&ui)) retstralloc(0,"write_logger") ;
@@ -541,7 +536,21 @@ int write_logger(sv_alltype *sv, sv_execlog *log,char const *name, char const *d
 			return 0 ;
 		}
 	}
-	
+	if ((!MYUID))
+	{
+		if (!youruid(&log_uid,logrunner) ||
+		!yourgid(&log_gid,log_uid))
+		{
+			VERBO3 strerr_warnwu2sys("get uid and gid of: ",logrunner) ;
+			return 0 ;
+		}
+		if (chown(destlog.s,log_uid,log_gid) == -1)
+		{
+			VERBO3 strerr_warnwu2sys("chown: ",destlog.s) ;
+			return 0 ;
+		}
+	}
+		
 	stralloc_free(&shebang) ;
 	stralloc_free(&ui) ;
 	stralloc_free(&exec) ;
@@ -760,7 +769,7 @@ int write_common(sv_alltype *sv, char const *dst,uint8_t conf)
 	return 1 ;
 }
 
-int write_exec(sv_alltype *sv, sv_exec *exec,char const *file,char const *dst,int mode)
+int write_exec(sv_alltype *sv, sv_exec *exec,char const *file,char const *dst,mode_t mode)
 {
 	
 	unsigned int type = sv->cname.itype ;
@@ -784,11 +793,7 @@ int write_exec(sv_alltype *sv, sv_exec *exec,char const *file,char const *dst,in
 			if ((!owner && exec->runas))
 			{
 				if (!stralloc_cats(&ui,S6_BINPREFIX "s6-setuidgid ")) retstralloc(0,"write_exec") ;
-				if (!get_namebyuid(exec->runas,&ui))
-				{
-					VERBO3 strerr_warnwu1sys("set owner for the execute file") ;
-					return 0 ;
-				}
+				if (!stralloc_cats(&ui,keep.s + exec->runas)) retstralloc(0,"write_exec") ;
 				if (!stralloc_cats(&ui,"\n")) retstralloc(0,"write_exec") ;
 			}
 			/** environment */
@@ -879,20 +884,13 @@ int write_exec(sv_alltype *sv, sv_exec *exec,char const *file,char const *dst,in
 int write_dependencies(unsigned int nga,unsigned int idga,char const *dst,char const *filename)
 {
 	stralloc contents = STRALLOC_ZERO ;
-	//stralloc namedeps = STRALLOC_ZERO ;
 	size_t id = idga, nid = nga ;
 	for (;nid; id += strlen(deps.s + id) + 1, nid--)
 	{
 		if (!stralloc_cats(&contents,deps.s + id) ||
 		!stralloc_cats(&contents,"\n")) retstralloc(0,"write_dependencies") ;
 	}
-	/*for (unsigned int i = 0; i < nga; i++)
-	{
-		if (!stralloc_obreplace(&namedeps,deps.s+genalloc_s(unsigned int,ga)[idga+i])) return 0 ;
-		if (!stralloc_cats(&contents,namedeps.s)) retstralloc(0,"write_dependencies") ;
-		if (!stralloc_cats(&contents,"\n")) 
-	}*/
-		
+	
 	if (contents.len)
 	{
 		if (!file_write_unsafe(dst,filename,contents.s,contents.len))
@@ -903,11 +901,9 @@ int write_dependencies(unsigned int nga,unsigned int idga,char const *dst,char c
 	}
 	
 	stralloc_free(&contents) ;
-	//stralloc_free(&namedeps) ;
 	return 1 ;
 	err:
 		stralloc_free(&contents) ;
-		//stralloc_free(&namedeps) ;
 		return 0 ;
 }
 
-- 
GitLab