@@ -216,7 +216,7 @@ MI_Result _AddProtocolSocket_Handler(
216216
217217static void _ProtocolSocket_Cleanup (ProtocolSocket * handler )
218218{
219- ProtocolBase * protocolBase ;
219+ ProtocolBase * protocolBase = ( ProtocolBase * ) handler -> base . data ;
220220
221221 if (handler -> closeOtherScheduled )
222222 return ;
@@ -230,6 +230,8 @@ static void _ProtocolSocket_Cleanup(ProtocolSocket* handler)
230230 Batch_Destroy ( handler -> receivingBatch );
231231 if (handler -> engineBatch )
232232 {
233+ // Remove engine's handler from selector list just in case it has not been removed yet.
234+ Selector_RemoveHandler (protocolBase -> selector , handler -> engineHandler );
233235 Batch_Destroy ( handler -> engineBatch );
234236 handler -> engineBatch = NULL ;
235237 }
@@ -258,7 +260,6 @@ static void _ProtocolSocket_Cleanup(ProtocolSocket* handler)
258260 }
259261
260262 // skip for engine communicating with server
261- protocolBase = (ProtocolBase * )handler -> base .data ;
262263 if (!protocolBase -> forwardRequests || protocolBase -> type == PRT_TYPE_LISTENER )
263264 Strand_ScheduleClose ( & handler -> strand );
264265}
@@ -673,6 +674,7 @@ static MI_Boolean _SendAuthResponse(
673674 gid_t gid
674675 )
675676{
677+ ProtocolBase * protocolBase = (ProtocolBase * )h -> base .data ;
676678 BinProtocolNotification * req ;
677679 MI_Boolean retVal = MI_TRUE ;
678680
@@ -694,6 +696,16 @@ static MI_Boolean _SendAuthResponse(
694696 }
695697 }
696698
699+ if (protocolBase -> expectedSecretString && * protocolBase -> expectedSecretString )
700+ {
701+ req -> message = Batch_Strdup (req -> base .batch , protocolBase -> expectedSecretString );
702+ if (!req -> message )
703+ {
704+ BinProtocolNotification_Release (req );
705+ return MI_FALSE ;
706+ }
707+ }
708+
697709 req -> uid = uid ;
698710 req -> gid = gid ;
699711
@@ -1112,9 +1124,17 @@ static MI_Boolean _ProcessEngineAuthMessage(
11121124 /* engine waiting for server's response */
11131125 if (PostSocketFileResponse == sockMsg -> type )
11141126 {
1127+ // secret string is mandatory and can be set only during engine start-up
1128+ if ( (sockMsg -> secretString == NULL ) ||
1129+ (* s_secretString && Strncmp (sockMsg -> secretString , s_secretString , S_SECRET_STRING_LENGTH ) != 0 ) )
1130+ {
1131+ trace_AttemptToResetSecretString ();
1132+ return MI_FALSE ;
1133+ }
1134+
11151135 DEBUG_ASSERT (sockMsg -> sockFilePath );
11161136 DEBUG_ASSERT (sockMsg -> secretString );
1117-
1137+
11181138 Strlcpy (s_socketFile , sockMsg -> sockFilePath , PAL_MAX_PATH_SIZE );
11191139 Strlcpy (s_secretString , sockMsg -> secretString , S_SECRET_STRING_LENGTH );
11201140 trace_ServerInfoReceived ();
@@ -1404,6 +1424,7 @@ static MI_Boolean _SendPamCheckUserResp(
14041424 MI_Boolean result
14051425 )
14061426{
1427+ ProtocolBase * protocolBase = (ProtocolBase * )h -> base .data ;
14071428 PamCheckUserResp * req = NULL ;
14081429 MI_Boolean retVal = MI_TRUE ;
14091430
@@ -1416,6 +1437,16 @@ static MI_Boolean _SendPamCheckUserResp(
14161437 req -> handle = handle ;
14171438 req -> result = result ;
14181439
1440+ if (protocolBase -> expectedSecretString && * protocolBase -> expectedSecretString )
1441+ {
1442+ req -> message = Batch_Strdup (req -> base .batch , protocolBase -> expectedSecretString );
1443+ if (!req -> message )
1444+ {
1445+ PamCheckUserResp_Release (req );
1446+ return MI_FALSE ;
1447+ }
1448+ }
1449+
14191450 /* send message */
14201451 {
14211452 DEBUG_ASSERT (h -> message == NULL );
@@ -1471,6 +1502,19 @@ static MI_Boolean _ProcessPamCheckUserResp(
14711502
14721503 pamMsg = (PamCheckUserResp * ) msg ;
14731504
1505+ // server authentication check
1506+ if ( (pamMsg -> message != NULL ) && (* s_secretString ) && (Strncmp (pamMsg -> message , s_secretString , S_SECRET_STRING_LENGTH ) == 0 ) )
1507+ {
1508+ trace_ServerCredentialsVerified (handler );
1509+ }
1510+ else
1511+ {
1512+ trace_InvalidServerCredentials ();
1513+ return MI_FALSE ;
1514+ }
1515+
1516+ pamMsg -> message = NULL ;
1517+
14741518 /* engine waiting server's response */
14751519
14761520 result = authenticateCallback (pamMsg );
@@ -1950,8 +1994,8 @@ static Protocol_CallbackResult _ProcessReceivedMessage(
19501994 return PRT_RETURN_FALSE ;
19511995 }
19521996
1953- DEBUG_ASSERT (s_socketFile != NULL );
1954- DEBUG_ASSERT (s_secretString != NULL );
1997+ DEBUG_ASSERT (* s_socketFile );
1998+ DEBUG_ASSERT (* s_secretString );
19551999
19562000 /* If system supports connection-based auth, use it for
19572001 implicit auth */
@@ -1986,6 +2030,7 @@ static Protocol_CallbackResult _ProcessReceivedMessage(
19862030 return PRT_RETURN_FALSE ;
19872031 }
19882032
2033+ handler -> engineHandler = & newSocketAndBase -> protocolSocket .base ;
19892034 handler -> clientAuthState = PRT_AUTH_WAIT_CONNECTION_RESPONSE ;
19902035 handler = & newSocketAndBase -> protocolSocket ;
19912036 newSocketAndBase -> internalProtocolBase .forwardRequests = MI_TRUE ;
@@ -2009,6 +2054,18 @@ static Protocol_CallbackResult _ProcessReceivedMessage(
20092054 }
20102055 else if (binMsg -> type == BinNotificationConnectResponse )
20112056 {
2057+ // server authentication check
2058+ if ( (binMsg -> message != NULL ) && (* s_secretString ) && (Strncmp (binMsg -> message , s_secretString , S_SECRET_STRING_LENGTH ) == 0 ) )
2059+ {
2060+ trace_ServerCredentialsVerified (handler );
2061+ }
2062+ else
2063+ {
2064+ trace_InvalidServerCredentials ();
2065+ return PRT_RETURN_FALSE ;
2066+ }
2067+ binMsg -> message = NULL ;
2068+
20122069 // forward to client
20132070
20142071 Sock s = binMsg -> forwardSock ;
@@ -2672,7 +2729,8 @@ MI_Result _ProtocolSocket_New(
26722729 self -> closeOtherScheduled = MI_FALSE ;
26732730
26742731 self -> base .callback = _RequestCallback ;
2675-
2732+ self -> authInfo .uid = INVALID_ID ;
2733+ self -> authInfo .gid = INVALID_ID ;
26762734 /* Set output parameter */
26772735 * selfOut = self ;
26782736 return MI_RESULT_OK ;
@@ -3042,6 +3100,8 @@ static MI_Result _ProtocolSocketAndBase_New_Server_Connection(
30423100 protocolSocketAndBase -> protocolSocket .refCount = 1 ; //ref associated with Strand. Released on Strand_Finish
30433101 protocolSocketAndBase -> protocolSocket .closeOtherScheduled = MI_FALSE ;
30443102 protocolSocketAndBase -> protocolSocket .base .callback = NULL ;
3103+ protocolSocketAndBase -> protocolSocket .authInfo .uid = INVALID_ID ;
3104+ protocolSocketAndBase -> protocolSocket .authInfo .gid = INVALID_ID ;
30453105
30463106 r = _ProtocolBase_Init (& protocolSocketAndBase -> internalProtocolBase , selector , NULL , NULL , PRT_TYPE_FROM_SOCKET );
30473107 if ( r != MI_RESULT_OK )
0 commit comments