PHB p123: "Lidda suffers a -4 penalty because this orc is in melee with Jozan. Jozan also provides it with a +4 cover bonus to AC and if Lidda misses by 1 to 4 points, she might hit him instead of the orc."
Also, same page: "Lidda can shoot this orc but suffers a -4 penalty because it is in melee with Jozan."
Therefore, it follows that if a target is engaged in melee with an ally, the attacker suffers a -4 penalty to hit, and if the ally is in the way, the target also receives cover.
QED
What is unknown is the penalty for firing indiscriminately into a melee during a multi-way battle in which all combatants are enemies, and the attacker is mostly indifferent to the possibility of hitting the wrong target. Would that mean that if the attacker misses by 4 or less, he hits a random opponent instead?