Restructure pam conversation function(Klockwork fix)

Altered return values form the function. With the earlier
implementation, the function returned PAM_AUTH_ERR on failure scenarios
which is incorrect. Replaced PAM_AUTH_ERR with PAM_CONV_ERR and
PAM_BUF_ERR at respetive places.

Added a check for number of messages received by the conversation
function capped at PAM_MAX_NUM_MSG.

Added a check for password size, which is capped at PAM_MAX_RESP_SIZE
as the bytes in the password greater than this limit would be discarded
by PAM.

Though pam_response structure and response, which are dynamically
allocated by the pam conversation function are the responsibility of the
caller to free them, with the current implemention, there is a possibility of
memory leak when numMsg would be zero or if PAM_PROMPT_ECHO_OFF
message never arrived.
This commit fixes the possible memory leak by allocating only on
receiving PAM_PROMPT_ECHO_OFF message.

Tested:
 - ipmitool tool command passed with correct credentials:
 - With Correct Creds: ipmitool -I lanplus -H x.x.x.x -C 17 -U root -P
   0penBmc raw 0x00 0x01
   Reply : 01 00 03 70
 - With Incorrect Creds: ipmitool -I lanplus -H x.x.x.x -C 17 -U root
   -P 0pen raw 0x00 0x01
   Reply : Error: Unable to establish IPMI session

Signed-off-by: P Dheeraj Srujan Kumar <p.dheeraj.srujan.kumar@intel.com>
Change-Id: I670c3316eec01993a5cd0d79d1d6be248cf64328
1 file changed