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 Senate Republican Majority Leader Mitch McConnell.

Glad the Senate finally passed the USA Freedom Act. It protects civil liberties and our national security. I'll sign it as soon as I get it.

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 Communications Inc and AT&T Inc, 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.

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. (midnight GMT) 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.