@@ -23,18 +23,20 @@ limitations under the License.
2323#include "simpleauth.h"
2424#include <linux/limits.h>
2525
26+ const char * _mq_app_secret_file = MQ_APP_SECRET_FILE_DEFAULT ;
27+ const char * _mq_admin_secret_file = MQ_ADMIN_SECRET_FILE_DEFAULT ;
28+
2629// Check if the user is valid
27- int simpleauth_authenticate_user (char * user , char * password )
30+ int simpleauth_authenticate_user (const char * const user , const char * const password )
2831{
2932 int result = -1 ;
3033
3134 if (simpleauth_valid_user (user ))
3235 {
33- char * pwd = getSecretForUser (user );
36+ char * pwd = get_secret_for_user (user );
3437 if (pwd != NULL )
3538 {
36- int pwdCheck = strcmp (pwd , password );
37- if (pwdCheck == 0 )
39+ if (strcmp (pwd , password ) == 0 )
3840 {
3941 log_debugf ("Correct password supplied. user=%s" , user );
4042 result = SIMPLEAUTH_VALID ;
@@ -44,10 +46,12 @@ int simpleauth_authenticate_user(char *user, char *password)
4446 log_debugf ("Incorrect password supplied. user=%s" , user );
4547 result = SIMPLEAUTH_INVALID_PASSWORD ;
4648 }
49+ memset (pwd , 0 , strlen (pwd ));
4750 free (pwd );
4851 }
4952 else
5053 {
54+ log_debugf ("Failed to get secret for user '%s'" , user );
5155 result = SIMPLEAUTH_INVALID_PASSWORD ;
5256 }
5357 }
@@ -59,81 +63,88 @@ int simpleauth_authenticate_user(char *user, char *password)
5963 return result ;
6064}
6165
62- bool simpleauth_valid_user (char * user )
66+ bool simpleauth_valid_user (const char * const user )
6367{
6468 bool valid = false;
65- if ((strcmp (user , APP_USER_NAME )== 0 || strcmp (user , ADMIN_USER_NAME )== 0 ))
69+ if ((strcmp (user , APP_USER_NAME ) == 0 || strcmp (user , ADMIN_USER_NAME ) == 0 ))
6670 {
6771 valid = true;
6872 }
6973 return valid ;
7074}
7175
72- char * getSecretForUser (char * user )
76+ /**
77+ * get_secret_for_user will return a char* containing the credential for the given user
78+ * the credential is read from the filesystem if the relevant file exists and an environment
79+ * variable if not
80+ *
81+ * The caller is responsible for clearing then freeing memory
82+ */
83+ char * get_secret_for_user (const char * const user )
7384{
7485 if (0 == strcmp (user , APP_USER_NAME ))
7586 {
76- char * secret = readSecret ( MQ_APP_SECRET_FILE );
87+ char * secret = read_secret ( _mq_app_secret_file );
7788 if (secret != NULL )
7889 {
7990 return secret ;
8091 }
8192 else
8293 {
83- char * envValue = getenv ("MQ_APP_PASSWORD" );
84- if (envValue != NULL )
94+ const char * pwdFromEnv = getenv ("MQ_APP_PASSWORD" );
95+ if (pwdFromEnv != NULL )
8596 {
8697 log_infof ("Environment variable MQ_APP_PASSWORD is deprecated, use secrets to set the passwords" );
87- char * pwdFromEnv = strdup (envValue );
88- return pwdFromEnv ;
89- }
90- else
91- {
92- return NULL ;
9398 }
99+ return strdup (pwdFromEnv );
94100 }
95- } else if (0 == strcmp (user , ADMIN_USER_NAME ))
101+ }
102+ else if (0 == strcmp (user , ADMIN_USER_NAME ))
96103 {
97- char * secret = readSecret ( MQ_ADMIN_SECRET_FILE );
104+ char * secret = read_secret ( _mq_admin_secret_file );
98105 if (secret != NULL )
99106 {
100107 return secret ;
101108 }
102109 else
103110 {
104- char * envValue = getenv ("MQ_ADMIN_PASSWORD" );
105- if (envValue != NULL )
111+ const char * pwdFromEnv = getenv ("MQ_ADMIN_PASSWORD" );
112+ if (pwdFromEnv != NULL )
106113 {
107114 log_infof ("Environment variable MQ_ADMIN_PASSWORD is deprecated, use secrets to set the passwords" );
108- // Get the value of environment variable and store it as a copy to free up the memory
109- char * pwdFromEnv = strdup (envValue );
110- return pwdFromEnv ;
111115 }
112- else
113- {
114- return NULL ;
115- }
116- }
116+ return strdup (pwdFromEnv );
117+ }
117118 }
118119 else
119120 {
120121 return NULL ;
121122 }
122123}
123124
124- char * readSecret (char * secret )
125+ /**
126+ * read_secret will return a char* containing the credential read from the filesystem for the given user
127+ *
128+ * The caller is responsible for clearing then freeing memory
129+ */
130+ char * read_secret (const char * const secret )
125131{
126132 FILE * fp = fopen (secret , "r" );
127- const size_t line_size = 1024 ;
128133 if (fp )
129134 {
135+ const int line_size = MAX_PASSWORD_LENGTH + 1 ;
130136 char * pwd = malloc (line_size );
131- char * result = fgets (pwd , line_size , fp );
137+ char * result ;
138+ result = fgets (pwd , line_size , fp );
139+ fclose (fp );
132140 if (result == NULL )
141+ {
142+ memset (pwd , 0 , line_size );
143+ free (pwd );
133144 return NULL ;
134-
135- fclose ( fp ) ;
136- return pwd ;
145+ }
146+ result [ strcspn ( result , "\r\n" )] = 0 ;
147+ return result ;
137148 }
138149 else
139150 {
0 commit comments