From: Thomas Walker Lynch Date: Wed, 6 Mar 2019 13:48:09 +0000 (+0100) Subject: some refactoring X-Git-Url: https://git.reasoningtechnology.com/style/static/git-logo.png?a=commitdiff_plain;h=ffc5ccb2449b892ea959273b37dcd24c1d08f576;p=subu some refactoring --- diff --git a/src/5_scratch/common.lib.h b/src/5_scratch/common.lib.h index 217c0f7..52dc587 100644 --- a/src/5_scratch/common.lib.h +++ b/src/5_scratch/common.lib.h @@ -1,6 +1,7 @@ /* This file was automatically generated. Do not edit! */ #undef INTERFACE typedef unsigned int uint; +extern uint first_max_subu_number; extern uint subuhome_perms; extern char config_file[]; #define BUG_SSS_CACHE_RUID 1 diff --git a/src/5_scratch/subu-config.lib.h b/src/5_scratch/subu-config.lib.h index a692fbd..3a5e858 100644 --- a/src/5_scratch/subu-config.lib.h +++ b/src/5_scratch/subu-config.lib.h @@ -5,5 +5,5 @@ int subu_put_masteru_subu(sqlite3 *db,char *masteru_name,char *subuname,char *su int subu_number_get(sqlite3 *db,char **nsp,char **errmsg); typedef unsigned int uint; int schema(sqlite3 *db,uint max_subu_number); -#define ERR_CONFIG_FILE -1 +#define ERR_CONFIG_FILE 1 #define INTERFACE 0 diff --git a/src/5_scratch/subu-init.cli.h b/src/5_scratch/subu-init.cli.h index d0dcddd..b64efc7 100644 --- a/src/5_scratch/subu-init.cli.h +++ b/src/5_scratch/subu-init.cli.h @@ -1,7 +1,8 @@ /* This file was automatically generated. Do not edit! */ #undef INTERFACE -#include -#define ERR_CONFIG_FILE -1 typedef unsigned int uint; +extern uint first_max_subu_number; +#include int schema(sqlite3 *db,uint max_subu_number); +#define ERR_CONFIG_FILE 1 extern char config_file[]; diff --git a/src/5_scratch/subu-mk-0.lib.h b/src/5_scratch/subu-mk-0.lib.h index 27ffcce..0e6876d 100644 --- a/src/5_scratch/subu-mk-0.lib.h +++ b/src/5_scratch/subu-mk-0.lib.h @@ -35,13 +35,14 @@ struct subu_mk_0_ctx { char *aux; uint err; }; -#define ERR_SUBU_MK_0_FAILED_USERADD 9 -#define ERR_SUBU_MK_0_BUG_SSS 8 -#define ERR_SUBU_MK_0_SUBUHOME_EXISTS 7 -#define ERR_SUBU_MK_0_CONFIG_FILE 6 -#define ERR_SUBU_MK_0_MALLOC 5 -#define ERR_SUBU_MK_0_MASTERU_HOMELESS 4 -#define ERR_SUBU_MK_0_SETUID_ROOT 3 -#define ERR_SUBU_MK_0_SUBUNAME_MALFORMED 2 +#define ERR_SUBU_MK_0_FAILED_USERADD 10 +#define ERR_SUBU_MK_0_BUG_SSS 9 +#define ERR_SUBU_MK_0_SUBUHOME_EXISTS 8 +#define ERR_SUBU_MK_0_CONFIG_FILE 7 +#define ERR_SUBU_MK_0_MALLOC 6 +#define ERR_SUBU_MK_0_MASTERU_HOMELESS 5 +#define ERR_SUBU_MK_0_SETUID_ROOT 4 +#define ERR_SUBU_MK_0_SUBUNAME_MALFORMED 3 +#define ERR_SUBU_MK_0_RMDIR_SUBUHOME 2 #define ERR_SUBU_MK_0_MKDIR_SUBUHOME 1 #define INTERFACE 0 diff --git a/src/5_scratch/subu-number.cli.h b/src/5_scratch/subu-number.cli.h index 35b0da0..f9633fa 100644 --- a/src/5_scratch/subu-number.cli.h +++ b/src/5_scratch/subu-number.cli.h @@ -2,5 +2,5 @@ #undef INTERFACE #include int subu_number_get(sqlite3 *db,char **nsp,char **errmsg); -#define ERR_CONFIG_FILE -1 +#define ERR_CONFIG_FILE 1 extern char config_file[]; diff --git a/src/5_scratch/subu-put.cli.h b/src/5_scratch/subu-put.cli.h index ee08783..782010d 100644 --- a/src/5_scratch/subu-put.cli.h +++ b/src/5_scratch/subu-put.cli.h @@ -2,5 +2,5 @@ #undef INTERFACE #include int subu_put_masteru_subu(sqlite3 *db,char *masteru_name,char *subuname,char *subu_username); -#define ERR_CONFIG_FILE -1 +#define ERR_CONFIG_FILE 1 extern char config_file[]; diff --git a/src/common.lib.c b/src/common.lib.c index 6da0ca2..0087698 100644 --- a/src/common.lib.c +++ b/src/common.lib.c @@ -16,3 +16,4 @@ typedef unsigned int uint; // char *config_file = "/etc/subu.db"; char config_file[] = "subu.db"; uint subuhome_perms = 0700; +uint first_max_subu_number = 114; diff --git a/src/subu-config.lib.c b/src/subu-config.lib.c index d10ad96..4d14df7 100644 --- a/src/subu-config.lib.c +++ b/src/subu-config.lib.c @@ -14,7 +14,7 @@ currently a unit converted to base 10 will always fit in a 21 bit buffer. #if INTERFACE #include -#define ERR_CONFIG_FILE -1 +#define ERR_CONFIG_FILE 1 #endif #include diff --git a/src/subu-init.cli.c b/src/subu-init.cli.c index 4ef7527..cf74a06 100644 --- a/src/subu-init.cli.c +++ b/src/subu-init.cli.c @@ -7,16 +7,16 @@ This command initializes the configuration file. int main(){ sqlite3 *db; - if( - sqlite3_open(config_file, &db) - || - schema(db, 10) - ){ - fprintf(stderr, "error exit, could not build schema\n"); + if( sqlite3_open_v2(config_file, &db, SQLITE_OPEN_READWRITE, NULL) != SQLITE_OK ){ + fprintf(stderr, "error exit, could not open configuration file\n"); return ERR_CONFIG_FILE; } - if( sqlite3_close(db) ){ - fprintf(stderr, "error exit, strange, we could not close the db\n"); + if( schema(db, first_max_subu_number) != SQLITE_OK ){ + fprintf(stderr, "error exit, opened config file but could not build scheme\n"); + return ERR_CONFIG_FILE; + } + if( sqlite3_close(db) != SQLITE_OK ){ + fprintf(stderr, "error exit, could not close the db\n"); return ERR_CONFIG_FILE; } return 0; diff --git a/src/subu-mk-0.lib.c b/src/subu-mk-0.lib.c index aa7add3..775a95b 100644 --- a/src/subu-mk-0.lib.c +++ b/src/subu-mk-0.lib.c @@ -47,14 +47,15 @@ // #if INTERFACE #define ERR_SUBU_MK_0_MKDIR_SUBUHOME 1 -#define ERR_SUBU_MK_0_SUBUNAME_MALFORMED 2 -#define ERR_SUBU_MK_0_SETUID_ROOT 3 -#define ERR_SUBU_MK_0_MASTERU_HOMELESS 4 -#define ERR_SUBU_MK_0_MALLOC 5 -#define ERR_SUBU_MK_0_CONFIG_FILE 6 -#define ERR_SUBU_MK_0_SUBUHOME_EXISTS 7 -#define ERR_SUBU_MK_0_BUG_SSS 8 -#define ERR_SUBU_MK_0_FAILED_USERADD 9 +#define ERR_SUBU_MK_0_RMDIR_SUBUHOME 2 +#define ERR_SUBU_MK_0_SUBUNAME_MALFORMED 3 +#define ERR_SUBU_MK_0_SETUID_ROOT 4 +#define ERR_SUBU_MK_0_MASTERU_HOMELESS 5 +#define ERR_SUBU_MK_0_MALLOC 6 +#define ERR_SUBU_MK_0_CONFIG_FILE 7 +#define ERR_SUBU_MK_0_SUBUHOME_EXISTS 8 +#define ERR_SUBU_MK_0_BUG_SSS 9 +#define ERR_SUBU_MK_0_FAILED_USERADD 10 struct subu_mk_0_ctx{ char *name; @@ -159,14 +160,22 @@ static int allowed_subuname(char *subuname, size_t *subuname_len){ // dispatched functions // // the making of subuhome is dispatched to its own process so as to give it its own uid/gid -static int masteru_makes_subuhome(void *arg){ +static int masteru_mkdir_subuhome(void *arg){ char *subuhome = (char *) arg; if( mkdir( subuhome, subuhome_perms ) == -1 ){ // find subuhome perms in common - perror("masteru_makes_subuhome"); + perror("masteru_mkdir_subuhome"); return ERR_SUBU_MK_0_MKDIR_SUBUHOME; } return 0; } +static int masteru_rmdir_subuhome(void *arg){ + char *subuhome = (char *) arg; + if( rmdir( subuhome ) == -1 ){ // find subuhome perms in common + perror("masteru_rmdir_subuhome"); + return ERR_SUBU_MK_0_RMDIR_SUBUHOME; + } + return 0; +} //-------------------------------------------------------------------------------- // the public call point @@ -307,8 +316,8 @@ struct subu_mk_0_ctx *subu_mk_0(sqlite3 *db, char *subuname){ } dispatch_ctx *dfr = dispatch_f_euid_egid ( - "masteru_makes_subuhome", - masteru_makes_subuhome, + "masteru_mkdir_subuhome", + masteru_mkdir_subuhome, (void *)ctxp->subuhome, masteru_uid, masteru_gid @@ -352,12 +361,28 @@ struct subu_mk_0_ctx *subu_mk_0(sqlite3 *db, char *subuname){ envp[0] = (char *) NULL; dispatch_ctx *dfr = dispatch_exec(argv, envp); if( dfr->err != 0 ){ - #ifdef DEBUG + #ifdef DEBUG if( dfr->err <= ERR_DISPATCH ) dispatch_f_mess(dfr); else perror("useradd"); #endif + // go back and remove the directory we made in subuland + dispatch_ctx *dfr = dispatch_f_euid_egid + ( + "masteru_rmdir_subuhome", + masteru_rmdir_subuhome, + (void *)ctxp->subuhome, + masteru_uid, + masteru_gid + ); + #ifdef DEBUG + if( dfr->err <= ERR_DISPATCH || dfr->err == ERR_SUBU_MK_0_RMDIR_SUBUHOME ) + if( dfr->err == ERR_SUBU_MK_0_RMDIR_SUBUHOME ) + perror("rmdir"); + else + dispatch_f_mess(dfr); + #endif ctxp->err = ERR_SUBU_MK_0_FAILED_USERADD; return ctxp; }