* Ahmadinejad alienates allies in spat with Khamenei
* Rift with conservatives may complicate cabinet formation
* Iran power struggle hampers decision on nuclear diplomacy
By Alistair Lyon, Special Correspondent
BEIRUT, July 28 Iran's hardline President Mahmoud Ahmadinejad has chosen a strange moment to cross swords with his chief patron, Supreme Leader Ayatollah Ali Khamenei.
As if widespread popular unrest and the wrath of reformists over a disputed election were not enough, Ahmadinejad has alienated some of his own allies and lost two hardline cabinet members by defying Khamenei over his choice of vice president. The disarray in the hardline camp is likely to complicate Ahmadinejad's job of forming a new cabinet, risking prolonged paralysis in decision-making even as a Western deadline looms for Iran to enter substantive talks on its nuclear programme.
Ahmadinejad, due to be sworn in by parliament next week, is already under fire from his moderate rivals, Mirhossein Mousavi and Mehdi Karoubi, who say any new cabinet will be illegitimate as the June 12 poll was rigged -- a charge the authorities deny.
Part of Iran's influential Shi'ite clerical establishment based in the shrine city of Qom has also signalled misgivings over the aftermath of the poll, which has plunged Iran into its worst internal upheaval since the 1979 Islamic revolution.
"Given the crisis of legitimacy Ahmadinejad faces, not just from the apparatchiks in Tehran, but increasingly from Qom, he will face difficulty in composing a credible cabinet," said Anoush Ehteshami, an Iran expert at Britain's Durham University.
"For the moment the strategy has to be to make the glue stick, but there will come a moment when Ahmadinejad is untenable," he said, adding this could be just months away.
Khamenei, who endorsed the election result and sided openly with Ahmadinejad, can hardly ditch his protege now. But he reacted firmly when the president named as his deputy Esfandiar Rahim Mashaie, who had previously angered hardliners by saying Iran had no quarrel with Israelis, only their government.
In a rare move, Khamenei ordered Ahmadinejad in an open letter to dismiss Mashaie. Instead, the president publicly defended his nominee, who is related to him by marriage, and presented him last week at a stormy cabinet meeting.
Ahmadinejad finally dismissed Mashaie at the weekend, only to reappoint him as head of his own office. He also sacked his intelligence minister, Gholamhossein Mohseni-Ejei, apparently for insisting Mashaie must go to conform with Khamenei's orders.
Culture Minister Mohammad Hossein Saffar-Harandi, seen as another hardliner close to Khamenei, quit over the same issue, although Ahmadinejad has not accepted his resignation.
The supreme leader's approval is normally required for appointments to head sensitive ministries such as intelligence and culture, along with the defence and interior portfolios.
Ahmadinejad's handling of his outgoing cabinet can only make it harder to compose a new one amidst Iran's wider crisis.
"Clearly he has weakened his position, even with the conservatives in parliament," said Shaul Bakhash, history professor at George Mason University in Virginia.
"He has always had this in-your-face streak toward others outside the ruling group. Now he has taken this independence a step further by almost defying the authority of the leader."
The assembly is dominated by conservatives, but it has in the past rejected some of Ahmadinejad's cabinet picks. Without Khamenei's support, any new list could get a very rough ride.
Baqer Moin, a London-based Iran analyst, said conservatives felt the supreme leader's election backing for Ahmadinejad had cost them dear. Now they wanted to control the president.
"It boils down to Ahmadinejad's singleminded and volatile behaviour," he said. "If they can't control him, they will get rid of him, but not now. There would be too great an upheaval."
Moin argued that Ahmadinejad would have to name a broad-based cabinet from beyond his own circle to gain the acceptance of conservatives, or risk a parliamentary revolt.
Iran's ruling elite, already split and bruised by the tumult since the election, would have little appetite for a new crisis that would erupt if Ahmadinejad resigned or were forced out.
"Even his critics within the leadership and among the conservatives would not want to generate a crisis of such proportions," Bakhash said. "It would require a new election. I imagine Khamenei would want to avoid that, almost at any cost."
The supreme leader's own authority has already been sapped, partly by his own departure from his usual role of arbiter above the political fray. The opposition has defied his ruling that the election was valid. And now Ahmadinejad has challenged him.
The opposition has found new energy in the last few weeks, winning support from Akbar Hashemi Rafsanjani and other senior clerics, after authorities quelled last month's street protests.
The power struggle can only hamper the leadership's ability to tackle the Islamic Republic's economic problems, as well as the struggle over its nuclear programme, which Iran says is only peaceful, but which the West suspects is aimed at bomb-making.
"I sense Washington is slowly but surely increasing the pressure. From the autumn it will become relentless," Durham University's Ehteshami said of President Barack Obama's offer to engage with Iran in return for nuclear concessions.
"They will demand a position from Tehran by the end of September and will then give it till the end of the year to implement it. This is a very small window."