What was the attitude toward homosexuality of the Mughals? Was it different between the ruling class and the common people? Was homosexuality, officially or unofficially, permissible during the reign of any Mughal ruler, or was it illegal throughout the period?
Homosexuality or at least homoeroticism was actually quite common in Mughal court life.
None other than Barbur, the first Mughal Emperor himself, was known to have had a crush on a boy he saw in Kabul. He even recorded it in his own memoirs, the Barburnama. By all appearances, homosexuality was not particularly frowned upon among the Muslim ruling elite. For homosexual men, at least.
In Islamic sufi literature homosexual eroticism was used as a metaoporical expression of the sppiritual relationship between god and man, and much Persian poetry and fiction used omosexual relationships as examples of moral love. Although the Qur'an and early religious writings display mildly negative attitudes towards homosexuality, Muslim cultures seem to treat homosexuality with indifference, if not admiration.
De Sondy, Amanullah. The Crisis of Islamic Masculinities. Bloomsbury Publishing, 2015.
Other examples of prominent Mughal man engaging in homosexuality includes Ali Quli Khan, a Mughal commander of the Second Battle of Panipat. The poet Sharmad Kashani had such a crush on a Hindu boy, Abhai Chand, that he went to his home naked. Jean-Baptiste Tavernier reports that a governor of Surat once provoked an uprising of dervishes and fakirs, by attempting to force himself onto a beautiful son of a fakir.
Which is not to say homosxuality was legal per se. Islamic law generally forbade sodomy, with various punishments proscribed by different jurists. On the harshest end of the spectrum execution was prescribed, including by stoning, burning, or being thrown from a minaret. The dominant school of thought in the Mughal Empire, Hanafi, was much more lenient and did not demand a death penalty. Punishments could be a fine or left up to the judge's discretion.
In practice, however, even this milder stipulation was largely ignored.
[T]hese legal provisions were rarely implemented since guilty was very difficult to establish. The Shariah demands incontrovertible evidence, such as confessions or four faithful eyewitnesses confirming that they saw penetration occur . . . The difficulty of finding eyewitnesses to confirm instances of penetration in effect removes private acts between consenting individuals from the realm of punishment.
Kidwai, Saleem. "Introduction: Medieval materials in the Perso-Urdu tradition." Vanita, Ruth, and Saleem Kidwai, eds. Same-Sex Love in India: Readings in Indian Literature. Springer, 2000.
In general, among the elites at least, as long as men fulfilled their other duties at home, they were free to engage in whatever other dalliances they may desire.
Curiously, in contrast, homosexuality was reportedly regarded as a major taboo among the common, mostly native Indian, people of the Mughal Empire.
Homosexuality, though prevalent among the Mughal amirs, seems to have been rare among the common people in India. Hindus . . . according to Albiruni, considered [homosexuality] as revolting as eating beef.
Eraly, Abraham. Last Spring: The Lives and Times of Great Mughals. Penguin UK, 2000.