In December 2003, Mr. Goldsmith began to question the legality of some aspects of the programs, and he persuaded Mr. Comey — who took over temporarily as attorney general when Mr. Ashcroft became ill the following March — not to sign a document reauthorizing it.

The report says that the Justice Department’s concerns were focused on the Internet metadata program, which was then based on a theory that the N.S.A. did not “acquire,” for legal purposes, the bulk communications when it collected them, but instead only when human analysts selected certain data to examine because it met certain criteria.

In the hospital room confrontation of March 10, 2004, Mr. Ashcroft refused a request by senior White House aides to overrule Mr. Comey and sign off on extending the program. The report says that on March 19, after taking into account the objections of Mr. Comey and his allies, Mr. Bush rescinded his authorization to the N.S.A. to collect bulk Internet metadata and gave the agency a week to stop collecting it and to block access to its existing database.

During the same month, the report says, the Bush administration also ended the secret domestic surveillance programs against Iraq it had begun the year before under a theory that Iraqi spies were “engaged in terrorist activities and presented a threat to U.S. interests in the United States and abroad.” The credibility of Bush administration claims linking Iraq to Al Qaeda and other terrorist groups was, at the time, a subject of deep controversy. It is not clear whether ending the program was a coincidence or connected to the Justice Department’s rebellion against the White House over the administration’s legal theories about surveillance.

After Mr. Bush’s order to end the collection of Internet metadata, administration lawyers looked for a new legal basis to restart the program. Under this theory, the Foreign Intelligence Surveillance Court could authorize such bulk collection under a “pen register/trap trace” statute, which traditionally allows law enforcement officials to keep logs of communications sent and received by particular phone numbers or e-mail accounts.

The chief judge of the national-security court, Colleen Kollar-Kotelly, issued an order on July 15, 2004, authorizing the resumption of the program, according to the report. None of the other judges on the court were apparently told about the programs or the new order; this was the first time one of the N.S.A.’s secret surveillance programs had come under the court’s oversight and authority.

In 2006, according to the report, the N.S.A. was faced with another legal challenge when The New York Times disclosed the existence of the warrantless surveillance program. In response to the disclosures, one of the telephone companies that was secretly providing its customers’ data to the N.S.A. on a voluntary basis asked for a court order compelling it to comply to protect itself legally, forcing the Bush administration to develop new legal theories to support other surveillance.