Mahmoud Ahmadinejad, pictured right, is at risk of falling out with Iran’s clergy because of the rise of the controversial confidant who stands behind him

IN THE summer of 2009 Iran's divided conservatives came together to save the presidency of Mahmoud Ahmadinejad, after his disputed re-election provoked huge street protests by the reformist Green Movement. To have lost Mr Ahmadinejad to a liberal “plot” would, they judged, have imperilled the Islamic Republic which succours them all.

All the same, many conservatives are far from enamoured of Iran's president. Challenging him, however, is turning out to be a different matter. Barely a year into his second and constitutionally final term, his future is again the object of dark speculation, only this time by people who once professed to be his friends. His immediate entourage, in particular, is being castigated and none more so than the man whom, it is thought, Mr Ahmadinejad would like to succeed him: his old friend and relation by marriage, Esfandiar Rahim Mashai.

As the president's closest adviser, the slim, handsome, self-confident Mr Mashai has come to represent all that traditionalists in Shia Iran find odious about Mr Ahmadinejad's presidency. The Islamic Republic was founded on the idea that the Muslim community awaits the reappearance of the hidden “12th imam”, a messianic leader who was “occulted”—hidden by God—in the ninth century; in the meantime it is up to the clergy to run human affairs, under an arrangement known as the Guardianship of the Jurist. Mr Mashai, it is strongly rumoured, believes himself to have a direct link to the hidden imam, and hence regards the intercession of Iran's clergy as superfluous. He is also said to have encouraged the president's well-known millenarian tendencies.

For long, Mr Mashai's critics have expressed their fears sotto voce. Over the course of the summer, however, several conservatives openly raised fears of a campaign among Mr Ahmadinejad's closest allies to drive the clergy from public life. Last month, a conservative parliamentarian, Hamid Rasai, revealed that the country's current “guardian-jurist”, the supreme leader, Ayatollah Ali Khamenei, had spoken to him and a few other deputies about a “new plot” carrying even greater danger than last year's protests. Mr Rasai hinted that Mr Mashai and the Green Movement, albeit now much diminished, may be working in sinister concert; after all, he pointed out, both are “uncommitted to the Guardianship of the Jurist”.

Reverence for the hidden imam has long been an accepted part of Shia Islam, but millenarian zeal has produced schismatics in the past—the Bahais, for instance, who are now banned and persecuted. From a position of ostentatious piety, Mr Mashai clearly feels he has the licence to behave provocatively. He has renounced hostility for the people of Israel (for which he received a dressing down from Mr Khamenei), suggested that Iran is incapable of dealing with modern challenges, and flirted impiously with a famous actress. Were he a reformist, it is likely that he would have been silenced months ago.

In August Mr Mashai caused perhaps his biggest rumpus to date, when he urged hundreds of expatriate Iranians, who had been invited to Tehran at government expense, to act as propagandists for a national ideology, as opposed to an Islamic one. Lacing his address with references to Iran's pre-Islamic history and claiming that Iran had saved Islam from Arab parochialism, Mr Mashai's patriotic theme provoked a storm of recrimination from members of the religious establishment. “If someone turns away from Islam,” warned Ayatollah Muhammad Taqi Mesbah Yazdi, a longtime leading government supporter, “we warn him, and then, if that does not work, we beat him.”

In the eyes of his enemies, Mr Mashai's position at the heart of the government, and his repeated protestations of loyalty to the Guardianship of the Jurist, make him all the more threatening. Last summer Mr Khamenei stripped Mr Mashai of the vice-presidency he had just been awarded, only for the unrepentant Mr Ahmadinejad to appoint him his chief of staff. Nowadays Mr Mashai is more likely to be seen hobnobbing with foreign heads of state in his role as the president's representative on Middle Eastern affairs.

Mr Mashai is a member of a new diplomatic team that Mr Ahmadinejad has set up independently of the foreign ministry, which is controlled by the supreme leader. The president's “experts” are not known for their subtlety; his senior vice-president recently called the British “a bunch of imbeciles” and the Australians “cowherds.” The foreign minister, Manouchehr Mottaki, is at odds with some of these presidential experts. In any event, they have a serious intent: to exploit what they believe to be Iran's enhanced position in the world and to use it to their advantage back home.

In Mr Ahmadinejad's view, Iran's refusal to buckle under increasing international sanctions aimed at halting its progress towards becoming a nuclear power qualifies it as a world player on a level with the old enemy, the United States. Last month Iran passed its latest milestone with the fuelling of its first power-generating reactor, set up long ago by the Russians at Bushehr. Iran's president has challenged Barack Obama to join him before the media for a “man to man” debate on “world issues” when the two attend the UN's General Assembly in New York later this month.

Mr Obama is unlikely to give him satisfaction but Mr Ahmadinejad's opponents fear any sign that the Americans regard him as a possible interlocutor, thereby raising his prestige at home. A senior ayatollah recently denounced those who are “trying to beat a path to negotiations with America”. Mr Mashai, who usually accompanies the president on his trips to New York, has also been accused of meeting a former American ambassador to Israel.

Whatever his ambitions abroad, Mr Ahmadinejad is playing a high-risk game at home. He has offended conservatives by appearing to condone less-than-Islamic dress for women, and has presided over a breakdown in co-operation between the government and parliament. Sanctions are starting to hurt, with investment dropping in key sectors, including oil and gas. The most painful of the president's cuts in subsidies has yet to come into effect.

This dispute at the heart of Iran's ruling establishment may seem arcane. After all, both Mr Ahmadinejad and his traditionalist opponents agree on the need to repress the Green Movement and to press on towards nuclear self-sufficiency. But the president fits uncomfortably into the country's power structure, which rewards collegiate effort under the supreme leader's benevolent tutelage. Although the president professes his undying loyalty to Mr Khamenei, his own patent ambition and his friend's theology have led him perilously close to open defiance.

From the American and Western point of view, the very opacity of Iran's leadership structure—and the continuing feuds within it—have made diplomacy harder. Indeed, it is unclear who indisputably runs the show, though the supreme leader still has the final say. It is plainly more complex than a struggle between conservatives and reformers.

Ayatollah Khamenei has tried his best to end the infighting, but his authority is limited by his record of support for Mr Ahmadinejad, which may be all that stops parliament from impeaching him. However according to Mr Mashai, it is only a matter of time before “certain people are calling Ahmadinejad an apostate.”