WHAT does Ayatollah Ali Khamenei, Iran's supreme leader, really think of Mahmoud Ahmadinejad, his bellicose, populist president? After Mr Ahmadinejad was elected in 2005, to the surprise of almost all the pundits, it was widely assumed he would be a meek figurehead. Yet he has been given much leeway, and his reckless economic and risky foreign policies have dragged Iran into a state of near-constant crisis. But even Iranians close to the government find it hard to tell whether the president is the supreme leader's trusted lieutenant or whether he is an out-of-control maverick with grander ambitions that may be giving Mr Khamenei sleepless nights.

In terms of constitutional authority, the ascetic Mr Khamenei is plainly the most powerful man in the Islamic Republic; no big decision can be taken without his consent. Some Western experts think he is more powerful now than at any time in his 19 years as leader. The most influential institutions in Iran's elaborate power-structure, including the Revolutionary Guards, the Guardian Council, the presidency and parliament, are all still run by direct appointees of the supreme leader or by people unfailingly obsequious to him.

Yet Mr Khamenei wields his power lightly, to the extent that he often seems aloof. He is thought not to have left Iran since 1989. He rarely meets journalists or visiting Western officials. Whether intentionally or not, he has been overshadowed by Iran's presidents, even before Mr Ahmadinejad. The reform-minded Muhammad Khatami, who presided from 1997-2005, upstaged him from the left with hopeful calls for a “dialogue of civilisations”, while Mr Ahmadinejad seems to outflank him from the right with diatribes against Israel and denials of the Holocaust. Moreover, Mr Ahmadinejad gives the impression across the world that he is Iran's main man. For instance, the Republican presidential candidate, John McCain, has argued that Mr Ahmadinejad, not Mr Khamenei, has ultimate authority in Iran.

But Mr Ahmadinejad may have overreached himself. Last month, weeks after he publicly threatened to expose officials involved in corruption, one of his ideological allies, a former staff member of Iran's parliament called Abbas Palizdar, said that dozens of top clerical leaders, including several close to Mr Khamenei, had used their connections to swindle hundreds of millions of dollars from the state and even to kill their opponents.

Iranians have long accepted that the leading clergy have deep pockets, but it is rare for insiders such as Mr Palizdar to name names. Some Western experts on Iran, such as Gary Sick, of Columbia University in New York, who served in the National Security Council under Presidents Ford, Carter and Reagan, interpreted it as an extraordinary display of ambition by Mr Ahmadinejad. “At a minimum, [he] is carrying out a direct challenge to Khamenei and the old-guard leadership,” he wrote. “At a maximum, [it is] a slow-motion coup in which he gradually accumulates more and more power to himself and to the presidency.”

Accuser accused

But before Mr Palizdar's allegations could gather momentum, he and several of his friends were arrested on charges of “propagating lies” and “confusing public opinion”. When it became clear that Mr Khamenei was incensed by this public display of alleged dirty laundry, Mr Ahmadinejad and his allies quickly dumped Mr Palizdar, calling him a “corrupt impostor”.

A counter-attack against Mr Ahmadinejad then continued. An influential former foreign minister and confidant of Mr Khamenei, Ali Akbar Velayati, called the president's policies “illogical” and took the unusual step of writing an editorial in a French newspaper, Libération, reportedly with Mr Khamenei's blessing, to state explicitly that the supreme leader was Iran's ultimate decision-maker. The usually combative Mr Ahmadinejad stayed silent. According to a former senior Iranian official who is related to Mr Khamenei and occasionally meets him: “If the leader were to withdraw his support, Ahmadinejad's political future would be finished…He is scared of [Khamenei], like a dog”.

But just as Mr Ahmadinejad seemed to have fallen out of favour with the leader, Mr Khamenei came to his defence. “The responsible party for advancing the nuclear issue is the Supreme National Security Council headed by the honourable president,” he declared. “What is said by the president and authorities is shared by all authorities of the country…” Mr Khamenei may feel obliged to display national unity, especially on nuclear issues. He may also like to let the occasional hostile feelings of ordinary Iranians for the government be deflected towards the president.

So it is unclear how the two top men really feel about each other—or whether, for instance, Mr Khamenei will back Mr Ahmadinejad next summer if he seeks re-election as president. No matter who wins the presidency, a big shift in Iran's domestic and foreign policy seems unlikely while the 69-year-old Mr Khamenei remains the supreme leader.