The Stored Wire and Electronic Communications and Transactional Records Access, Section 2703(c) outlines exactly five reasons that a phone company can disclose your records to the fed:
a court order
the customer’s consent
for telemarketing enforcement
Keep in mind that the NSA does not have subpoena power and it is easy to see that not only is your federal government breaking the law by stripping you of your expectation of privacy in your papers and effects but that the Telcos are breaking the law by supplying this info to the Fed.