The Central Intelligence Agency's long and close ties to the shah of Iran and his intelligence service effectively prevented the agency from giving the White House a clear warning that public unrest posed a major threat to the shah and U.S. policy.

That conclusion emerges from a series of interviews with officals and analysts of the U.S. intelligence community, which has been severely shaken by President Carter's criticism of its performance in Iran. Some of these officials also have been interviewed by House and Senate committee staff investigators who are preparing reports on this intelligence failure.

Warnings originating in the State Department that the shah faced rising and dangerous popular opposition last summer were kept out of a draft national intelligence estimate because CIA and Defense Department, analysts strongly disagreed with that view, congressional investigators have been told.

A CIA official said yesterday that intelligence director Stansfield Turner had returned the draft estimate on Iran to its agency authors twice "because he didn't think it ws good enough." But the official was unable to say if Turner's objections involved the report's highly optimistic view of the shah's hold on power.

Other knowledgeable sources said that Turner had sent the now-controversial estimate back for more information on military and other matters, but there were no clear indications at the time that he was unhappy with the political reporting.

The bureaucratic wrangling over the estimate-which was overtaken by the explsion of unrest in the streets in Tehran in September-is only part of a much broader problem the CIA faces in trying to work in a country where it has an enormous investment in maintaining an autocratic ruler that it helped to put in power.

"If we wanted information on the opposition leaders and on the military in Iran, we should have turned it into an unfriendly country," said one exasperated analyst. "Then we would have targeted them and developed sources. But we can't do much with opaque regimes headed by friendly authoritarian figures.

The political opposition and officer corps have been off limits for years to the 50 to 75 agents the CIA maintains in Iran. The agency's professional intelligence on domestic Iranian developments has had to come largely from the shah's own secret police, SAVAK, which could hardly be expected to report that the shah was in trouble.

"If we had tried to penetrate the opposition, we would have been caught immediately by SAVAK," a CIA official said. "Iran is an ally. In England, we would not try to penetrate the opposition."

Hovering in the corners of the sharpening disputes over the Iran failure and U.S. choices for the future are ghosts from an era when the CIA could quickly mount covert operations to shore up-or move aside-agency allies who suddenly had become liabilities in their own countries.

Ngo Dinh Diem, the South Vietnamese dictator assassinated by his own troops in 1963 after President Kennedy let his lack of confidence in Diem be known, is conjured up by U.S. policymakers who argue that the United States would touch off disastrous turmoil by doing or saying anything to undercut Shah Mohammad Reza Pahlavi in a moment of crisis.

State Department officials argue that the dangers lie in not disengaging faster. They raise the specters of Libya or Ethiopia, where radicals have swung sharply against the United States after overthrowing regimes with which CIA had strong ties.

In any case, Iran is already a multilevel failure that is likely to haunt the agency for years. Because of its enormous importance and history, Iran is rapidly becoming the Jacob Marley of the intelligence world at this Christmas season, providing the kind of simultaneous visions of ghosts of the CIA past, present and future that Dickens had haunt Ebenezer Scrooge.

The agency has been deeply implanted in Iran since 1953, when Kermit Roosevelt and fewer than 30 American and Iranian agents were able to mount an operation that brought down the leftist government of Mohammed Mossadegh and put the shah back on the throne.

The entire operation cost less than $1 million, which the shah insisted on paying back, according to agency folklore. It is unclear that the agency could produce anything approaching these results for any sum-even if it were "unleashed."

In 1953, Roosevelt mobilized huge proshah crowds through religious Moslem leaders. Today those leaders are spear-heading the massive demonstrations calling for the sha's removel. The efforts to organize pro-shah demonstrations now are headed by some Iranian generals and Iranian ambassador to the U.S. Ardeshir Zehedi, according to intelligence reports reaching Washington. Ardeshir is currently in Tehran.

While honoring the shah's wishes that they totally ignore Iranian developments, CIA personnel have been given free rein in gathering intelligence about the Soviet Union from Iran. The shah permits the United States to operate some of the world's most sophisticated listening and radar equipment on the Soviet border.

The fate of that equipment-should the shah fall suddenly, before it could be relocated-is a major worry for Carter administration planner.

Intelligence analysts report that the agency performs two other major tasks in Iran. Its agents watch terrorist groups, devoting their attention primarily to anti-shah efforts but also coordinating with Israeli Mossad agents in Iran in combating Palestinian terror units. The CIA also is responsible for monitoring oil-field security, according to one source.

"The agency was absolutely accurate in its repeortingn throughout the year that the small radical terror groups represented no threat to the shah," said one U.S. intelligence official. "But those were the only opposition group the agency was reporting on. There was zero in their analysis about the rightist Moslem opposition that was the real threat."

This trend continued even as unrest intensified, and the White House sent an urgent request that a new national intelligence estimate on Iran already in production be accelerated. The estimate is an authoritative summary prepared on request and jointly by the CIA, State, Defense, Treasury and other departments.

Turner appointed one of his national intelligence officers to head the secret Iran project, which quickly produced a sharp and divisive dispute.

State Department analysts who argued that the opposition represented a serious threat to the shah were overruled in the writing of the draft by CIA and Pentagon analysts who insisted that the report should conclude that while some unrest would continue, the shah's hold on power was completely secure.

The draft as sent to Turner excluded the dissent the State Department wanted in the document. While State was preparing its own paper on the subject and Turner was sending the draft back for rewriting twice, the public demonstrations against the shah escalated dramatically and "Iran became a day-to-day problem for us," a CIA official said. The estimate was shelved in October.

But portions of the first draft were published after a note from President Carter expressing unhappiness with intelligence reporting on Iran was also leaked.