President Barack Obama speaks to members of the media during his meeting with NATO Secretary General Jens Stoltenberg, Tuesday, May 26, 2015, in the Oval Office of the White House in Washington. Obama began with commenting on flooding in Texas and calling on Senate to act on USA Freedom Act. AP Photo/Pablo Martinez Monsivais WASHINGTON (Reuters) - President Barack Obama signed into law on Tuesday legislation passed by Congress earlier in the day reforming a government surveillance program that swept up millions of Americans' telephone records.

Reversing security policy in place since shortly after the Sept. 11, 2001 attacks, the bill ends a system exposed by former National Security Agency contractor Edward Snowden.

The spy agency collected and searched records of phone calls looking for terrorism leads but was not allowed to listen to their content.

Passage of the USA Freedom Act, the result of an alliance between Senate Democrats and some of the chamber's most conservative Republicans, was a victory for Obama, a Democrat, and a setback for SenateRepublican Majority Leader Mitch McConnell.

After the Senate voted 67-32 on Tuesday to give final congressional approval to the bill, Obama used his Twitter account, @POTUS, to say he was glad it had passed. "I'll sign it as soon as I get it," the tweet said.

Before voting, senators defeated three amendments proposed by Republican leaders after they reversed themselves and ended efforts to block it. The House of Representatives passed the measure overwhelmingly last month.

In the end, 23 Senate Republicans voted for the Freedom Act, joining 196 who backed it in the House. In a rift between Republicans, who control both chambers, House leaders had warned that amendments proposed by McConnell would be a "challenge" for the House that could delay the bill.

A federal appeals court on May 7 ruled the collection of "metadata" illegal.

The new law would require companies such as Verizon and AT&T, to collect and store telephone records the same way that they do now for billing purposes.

But instead of routinely feeding U.S. intelligence agencies such data, the companies would be required to turn it over only in response to a government request approved by the secretive Foreign Intelligence Surveillance Court.

The Freedom Act is the first major legislative reform of U.S. surveillance since Snowden's revelations two years ago this month led to debate over how to balance Americans' distrust of intrusive government with fears of terrorist attacks.

Along with the phone records program, two other domestic surveillance programs authorized under the 2001 USA Patriot Act have been shut down since Sunday.

In this April 7, 2015 file photo, Sen. Rand Paul, R-Ky. holds up his cell phone as he speaks before announcing the start of his presidential campaign, in Louisville, Ky. AP

Missed deadline

After Republican Senator Rand Paul, a 2016 presidential candidate, blocked McConnell's efforts to keep them going temporarily, the Senate missed a deadline to extend legal authorities for certain data collection by the NSA and the FBI.

McConnell made an unusually strong last-ditch argument against the Freedom Act after his amendments failed. "It surely undermines American security by taking one more tool from our war fighters, in my view, at exactly the wrong time," he said in a Senate speech.

Telephone companies had been less than thrilled about potentially overhauling their record-keeping systems to become the repositories of surveillance records.

Together with civil liberties groups, they opposed specific requirements for how long they must retain any data, which were proposed in some amendments that were later defeated. A Verizon official, for instance, spoke in support of the Freedom Act, without such a mandate, in a Senate hearing last year.

After the vote, Microsoft Corp General Counsel Brad Smith praised Congress. "Today's vote by the Senate on the USA Freedom Act will help to restore the balance between protecting public safety and preserving civil liberties," Smith said in a statement.

Democratic Senator Ron Wyden, a leading Senate privacy advocate, voted for the Freedom Act. He pledged that he and his allies would continue pushing for more limits on surveillance.

"This has always been about reforming intelligence policies that do not make America safer and threaten our liberties," Wyden told reporters.

The American Civil Liberties Union said the Freedom Act was a milestone, but did not go far enough. "The passage of the bill is an indication that comprehensive reform is possible, but it is not comprehensive reform in itself," ACLU deputy legal director Jameel Jaffer said in a statement.

A senior U.S. intelligence official said the bulk telephone data collection system had been shut down since shortly before 8 p.m. EDT on Sunday.

It was not immediately clear how soon the NSA program would be restarted. The Freedom Act allows it to continue for six months while the new system is established.

The White House said the administration would move quickly to get it up and running again.

With Obama's signing of the bill, the executive branch will have to apply to the surveillance court for reauthorization.

(Editing by Grant McCool and Peter Cooney)