Browse Source

adding login/logout command dynamically on auth init - the former approach failed when the command list was reordered - might have deleted a command when auth init was called with commands already added

stefan 9 months ago
parent
commit
f40aca95f6
3 changed files with 8 additions and 11 deletions
  1. 0 2
      src/shellmatta.c
  2. 7 6
      src/shellmatta_auth.c
  3. 1 3
      test/integrationtest_auth/test_integration_auth.cpp

+ 0 - 2
src/shellmatta.c

@@ -97,9 +97,7 @@ shellmatta_retCode_t shellmatta_doInit(
 #ifdef SHELLMATTA_AUTHENTICATION
 #ifdef SHELLMATTA_AUTHENTICATION
         /** -# copy the auth commands to the instance */
         /** -# copy the auth commands to the instance */
         memcpy(&(inst->loginCmd), &shellmatta_auth_loginCmd, sizeof(shellmatta_cmd_t));
         memcpy(&(inst->loginCmd), &shellmatta_auth_loginCmd, sizeof(shellmatta_cmd_t));
-        inst->helpCmd.next = &(inst->loginCmd);
         memcpy(&(inst->logoutCmd), &shellmatta_auth_logoutCmd, sizeof(shellmatta_cmd_t));
         memcpy(&(inst->logoutCmd), &shellmatta_auth_logoutCmd, sizeof(shellmatta_cmd_t));
-        inst->loginCmd.next = &(inst->logoutCmd);
 #endif
 #endif
 
 
         if(NULL != cmdList)
         if(NULL != cmdList)

+ 7 - 6
src/shellmatta_auth.c

@@ -213,9 +213,9 @@ static shellmatta_retCode_t loginCmdFct(const shellmatta_handle_t handle, const
                 inst->tmpUserId = getUserIdFromName(inst, username);
                 inst->tmpUserId = getUserIdFromName(inst, username);
                 checkPassword(handle, inst->tmpUserId, password);
                 checkPassword(handle, inst->tmpUserId, password);
             }
             }
+            /** -# user passed username and start interactive password input */
             else if(NULL != username)
             else if(NULL != username)
             {
             {
-                /** -# reinitialize input */
                 inst->inputCount = 0u;
                 inst->inputCount = 0u;
                 inst->cursor = 0u;
                 inst->cursor = 0u;
 
 
@@ -231,10 +231,9 @@ static shellmatta_retCode_t loginCmdFct(const shellmatta_handle_t handle, const
             }
             }
             else
             else
             {
             {
-                /** -# no credentials are passed with the command - start the input */
+                /** -# no credentials are passed with the command - start the interactive input */
                 inst->inputCount = 0u;
                 inst->inputCount = 0u;
                 inst->cursor = 0u;
                 inst->cursor = 0u;
-                /** -# store pointer to username in buffer */
                 shellmatta_write(handle, "enter username:\r\n", 17);
                 shellmatta_write(handle, "enter username:\r\n", 17);
                 inst->loginState = SHELLMATTA_AUTH_USERNAME;
                 inst->loginState = SHELLMATTA_AUTH_USERNAME;
                 ret = SHELLMATTA_CONTINUE;
                 ret = SHELLMATTA_CONTINUE;
@@ -288,7 +287,7 @@ static shellmatta_retCode_t loginCmdFct(const shellmatta_handle_t handle, const
 const shellmatta_cmd_t shellmatta_auth_loginCmd = {"login"
 const shellmatta_cmd_t shellmatta_auth_loginCmd = {"login"
                                                  , "li"
                                                  , "li"
                                                  , "Login command"
                                                  , "Login command"
-                                                 , "interactive or use -u <username> -p <password>"
+                                                 , "login interactively or use -u <username> -p <password>"
                                                  , loginCmdFct
                                                  , loginCmdFct
                                                  , NULL
                                                  , NULL
                                                  , NULL};
                                                  , NULL};
@@ -330,6 +329,7 @@ const shellmatta_cmd_t shellmatta_auth_logoutCmd = {"logout"
  * @param[in]   checkFct        pointer to custom credential check function of type shellmatta_check_t
  * @param[in]   checkFct        pointer to custom credential check function of type shellmatta_check_t
  * @param[in]   logFct          pointer to login log function of type shellmatta_auth_log_t
  * @param[in]   logFct          pointer to login log function of type shellmatta_auth_log_t
  * @return      errorcode   #SHELLMATTA_OK
  * @return      errorcode   #SHELLMATTA_OK
+ *                          #SHELLMATTA_DUPLICATE (login/logout commands already existed)
  *                          #SHELLMATTA_USE_FAULT (param err)
  *                          #SHELLMATTA_USE_FAULT (param err)
  */
  */
 shellmatta_retCode_t shellmatta_auth_init(shellmatta_handle_t       handle,
 shellmatta_retCode_t shellmatta_auth_init(shellmatta_handle_t       handle,
@@ -362,9 +362,10 @@ shellmatta_retCode_t shellmatta_auth_init(shellmatta_handle_t       handle,
         inst->userListLength = userListLength;
         inst->userListLength = userListLength;
         inst->permList = permList;
         inst->permList = permList;
         inst->permListLength = permListLength;
         inst->permListLength = permListLength;
-        if(customLogin)
+        SHELLMATTA_RET(ret, shellmatta_addCmd(handle, &inst->logoutCmd));
+        if(!customLogin)
         {
         {
-            inst->helpCmd.next = &inst->logoutCmd;
+            SHELLMATTA_RET(ret, shellmatta_addCmd(handle, &inst->loginCmd));
         }
         }
         inst->checkFct  = checkFct;
         inst->checkFct  = checkFct;
         inst->logFct    = logFct;
         inst->logFct    = logFct;

+ 1 - 3
test/integrationtest_auth/test_integration_auth.cpp

@@ -128,9 +128,7 @@ SCENARIO("Check help auth uninitialized") {
             THEN("The help command prints all commands.") {
             THEN("The help command prints all commands.") {
 
 
                 char *dummyData =   (char*) "help\r\n"
                 char *dummyData =   (char*) "help\r\n"
-                                            "help     ?   help [command] - print help or usage information\r\n"
-                                            "login    li  Login command\r\n"
-                                            "logout   lo  Logout command\r\n"
+                                            "help     ?  help [command] - print help or usage information\r\n"
                                             "private  r\r\n"
                                             "private  r\r\n"
                                             "public   p\r\n"
                                             "public   p\r\n"
                                             "\r\n"
                                             "\r\n"