“I don’t think I’ll be invited to any more N.S.A. Christmas parties,” Mr. Bamford said, deadpan, sitting down for an interview this week in his Washington town house. At 62, bald and with a salt-and-pepper mustache, Mr. Bamford is so unflamboyant that it comes as a surprise to learn that for most of the 1990s he lived aboard a 60-foot motor yacht called the Safe House, moored in a Potomac River marina.  His relationship with the National Security Agency might be compared to a long and rocky romance, in which fascination with his quarry’s size and capabilities has alternated with horror at its power to invade privacy. But he insists that he is no enemy of the United States’ largest intelligence agency or its mission.  “I have no problem whatsoever with their eavesdropping on terrorists in the U.S.,” Mr. Bamford said. “But the law says they need a court warrant.”  The book depicts the agency before 9/11 under Michael V. Hayden, a now-retired Air Force general who is currently the director of the Central Intelligence Agency, as excessively cautious about legally pursuing terrorists in the United States. He attributes the wariness to the trauma that the agency suffered in the 1970s when a Senate committee exposed the agency’s warrantless surveillance of thousands of Americans, including the Rev. Dr. Martin Luther King Jr. and Jane Fonda.  Mr. Bamford traces what was happening inside the agency as the 9/11 hijackers arrived in the United States and made preparations for the attack. Some were living in Laurel, Md., home to thousands of agency employees. “The terrorists and the eavesdroppers would coexist in the N.S.A.’s close-knit community like unseeing ghosts,” Mr. Bamford writes.  But after the terrorist attacks, Mr. Bamford asserts, General Hayden went to the opposite extreme, bypassing the warrant requirement of the Foreign Intelligence Surveillance Act, which was passed in 1978 in response to the earlier abuses.  Mr. Bamford says he believes General Hayden acted out of ambition and deference to his bosses. “I think he has a desire to please the powerful,” he said.  (Asked for comment, Mark Mansfield, a spokesman for General Hayden at the C.I.A., said that the pre-9/11 failings of the N.S.A. and other agencies had been thoroughly examined by Congress and the national 9/11 commission, and that “an attempt to assign blame to General Hayden — and impugn his conduct and integrity — strikes me as an unseemly effort aimed at peddling a book.” As for the warrantless surveillance, he added that “the law was followed assiduously” and said the suggestion that General Hayden caved in to White House pressure was nonsense.)  When The New York Times reported the warrantless eavesdropping in December 2005, Mr. Bamford recalled, “My phone started ringing at 4 in the morning” as reporters chasing the story looked for an expert.  This time Mr. Bamford’s experience as an N.S.A. sleuth had failed him. “I was extremely shocked,” he said. “I’d always said they had learned their lesson and were obeying the law.”  Mr. Bamford said he felt he had been duped by the agency and by General Hayden, who had granted him several interviews and had invited him to dinner. “The Shadow Factory” is in part a reporter’s mea culpa for his temporary seduction by a powerful agency.  Like many life-defining turns, Mr. Bamford’s decision in the late ’70s to take an interest in the National Security Agency was all but accidental. He had served in the Navy unit that supports the agency and attended Suffolk University law school, in Boston, on the G.I. Bill of Rights, but after graduation he had decided that he would rather be a writer than a lawyer. The eavesdropping giant seemed a worthy target. It took him months of research even to persuade a publisher to give him a contract for “The Puzzle Palace,” which he wrote on a typewriter.  He has always been awed by the scale of the N.S.A., long the world’s largest employer of mathematicians and user of supercomputers. What his new book documents is the agency’s growing partnership with private companies to tap into the fiber-optic cables that now carry most telephone and Internet traffic.  The partners include not only telecommunication giants like AT&T but also, Mr. Bamford found, a number of small companies that sell the agency surveillance software and hardware. He discovered that key figures in at least two of those suppliers have ties to Israeli intelligence — including Israel’s counterpart to the N.S.A., Unit 8200 — raising questions about the security of the United States’ most secret intelligence operations.  “I don’t think Congress knows about these vulnerabilities,” he said.  Mr. Bamford denied that he was obsessed with the N.S.A.: what he would really like to do next, he said, is give fiction a try. But he said he believed that a new inquiry, along the lines of the one headed by Senator Frank Church, Democrat of Idaho, in the ’70s, was called for to examine the warrantless eavesdropping, the agency’s relations with private companies and other issues raised by the role of a powerful surveillance bureaucracy in a democracy.  And if there is such an investigation? “I guess,” he said, “it would be a pretty good subject for a fourth N.S.A. book.”  A version of this article appears in print on , on Page C1 of the New York edition with the headline: Decades on the Trail Of a Shadowy Agency. Order Reprints| Today's Paper|Subscribe
The latest book by James Bamford, an expert on the National Security Agency, reconstructs the agency’s recent history.