some refactoring
authorThomas Walker Lynch <thomas.lynch@reasoningtechnology.com>
Wed, 6 Mar 2019 13:48:09 +0000 (14:48 +0100)
committerThomas Walker Lynch <thomas.lynch@reasoningtechnology.com>
Wed, 6 Mar 2019 13:48:09 +0000 (14:48 +0100)
src/5_scratch/common.lib.h
src/5_scratch/subu-config.lib.h
src/5_scratch/subu-init.cli.h
src/5_scratch/subu-mk-0.lib.h
src/5_scratch/subu-number.cli.h
src/5_scratch/subu-put.cli.h
src/common.lib.c
src/subu-config.lib.c
src/subu-init.cli.c
src/subu-mk-0.lib.c

index 217c0f7..52dc587 100644 (file)
@@ -1,6 +1,7 @@
 /* \aThis 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
index a692fbd..3a5e858 100644 (file)
@@ -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
index d0dcddd..b64efc7 100644 (file)
@@ -1,7 +1,8 @@
 /* \aThis file was automatically generated.  Do not edit! */
 #undef INTERFACE
-#include <sqlite3.h>
-#define ERR_CONFIG_FILE -1
 typedef unsigned int uint;
+extern uint first_max_subu_number;
+#include <sqlite3.h>
 int schema(sqlite3 *db,uint max_subu_number);
+#define ERR_CONFIG_FILE 1
 extern char config_file[];
index 27ffcce..0e6876d 100644 (file)
@@ -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
index 35b0da0..f9633fa 100644 (file)
@@ -2,5 +2,5 @@
 #undef INTERFACE
 #include <sqlite3.h>
 int subu_number_get(sqlite3 *db,char **nsp,char **errmsg);
-#define ERR_CONFIG_FILE -1
+#define ERR_CONFIG_FILE 1
 extern char config_file[];
index ee08783..782010d 100644 (file)
@@ -2,5 +2,5 @@
 #undef INTERFACE
 #include <sqlite3.h>
 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[];
index 6da0ca2..0087698 100644 (file)
@@ -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;
index d10ad96..4d14df7 100644 (file)
@@ -14,7 +14,7 @@ currently a unit converted to base 10 will always fit in a 21 bit buffer.
 
 #if INTERFACE
 #include <sqlite3.h>
-#define ERR_CONFIG_FILE -1
+#define ERR_CONFIG_FILE 1
 #endif
 
 #include <stdio.h>
index 4ef7527..cf74a06 100644 (file)
@@ -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;
index aa7add3..775a95b 100644 (file)
 //
 #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;
     }