A Ruger AR-15 semiautomatic rifle, center, the same model, though in gray rather than black, used by the shooter in a Texas church massacre on Nov. 5, shown at a store in Lynnwood, Wash. (Elaine Thompson/AP)

The recent mass shootings in California and Texas that killed a total of more than 30 people were committed by men who should not have been able to obtain firearms, highlighting the difficulty of preventing people who want guns from getting them.

In Sutherland Springs, Tex., Devin Kelley killed 26 people in a church using an assault-style semiautomatic rifle even though he should not have been able to get a gun at all — his domestic assault convictions while in the Air Force were not reported to national background check databases.

And in Rancho Tehama Reserve, Calif., Kevin Neal, who used an assault-style rifle in a spree that left five dead, was prohibited by a court from owning or possessing a gun because of legal troubles.

The cases raise concerns for both gun control advocates and those who lobby for legal gun ownership: No one wants any mass shootings to happen, but how can firearms be kept out of the hands of people who should not have them while preserving Second Amendment rights for law-abiding citizens?

“It’s the million-dollar question,” said James Densley, an associate professor of criminal justice at Metropolitan State University in St. Paul, Minn.

Dozens of people stand in the rain to pray and sing at the memorial crosses near the First Baptist Church of Sutherland Springs, Tex., one week after 26 people were killed there. (Carolyn Van Houten/The Washington Post)

It also has brought about a rare point of agreement between gun rights groups and gun control advocates. Both believe the FBI’s criminal background check system is woefully inadequate and needs to be shored up. The National Instant Criminal Background Check system is known to be missing millions of records of criminal convictions, mental illness diagnoses and other signs that a potentially dangerous person should not be able to purchase a firearm.

“Databases are only as good as the records in them,” said Shannon Watts, founder of Moms Demand Action for Gun Sense in America. “And right now it’s very much a patchwork solution to making sure the right records are in there.”

The FBI has said it does not know the scope of the problem, but the National Rifle Association has said that about 7 million records are missing from the system, based on a 2013 report by the nonprofit National Consortium for Justice Information and Statistics.

“The National Rifle Association has long supported the inclusion of all appropriate records in the National Instant Criminal Background Check System,” Jennifer Baker, the NRA’s director of public affairs, said in a statement this week.

On Wednesday, Sens. John Cornyn (R-Tex.) and Chris Murphy (D-Conn.), who are usually on opposite sides of the gun debate, announced legislation aimed at fixing the database, offering financial incentives to states that report offenses that would legally bar certain people from buying a gun.

The National Shooting Sports Foundation started a campaign called “Fix NICS,” which urges states to submit more records, said Lawrence G. Keane, the organization’s senior vice president and general counsel.

"The focus should be on fixing the NICS database, making it accurate," Keane said. "The retailers rely on it and they're on the front lines."

Randy Morehouse, the maintenance and operations supervisor for the Corning Union Elementary School District, points to one of the bullet holes at the Rancho Tehama Elementary School in California after a mass shooting this week. (Rich Pedroncelli/AP)

But beyond that, the two sides sharply diverge.

The database applies only to purchases made through licensed gun dealers, as federal law exempts private sales, such as those online or at gun shows, from comprehensive checks.

Gun control groups argue that background checks must be extended to all gun sales. Nineteen states have laws that expand checks to private sales, according to the Brady Campaign to Prevent Gun Violence.

When it comes to prohibiting people who should not have guns from obtaining them, “we’ve known how to do that for decades and it starts by having a background check system that covers all gun transactions in America,” said Avery Gardiner, co-president of the Brady Campaign.

Gardiner says there are a number of ways to keep guns from people who should not own them, including eliminating a provision in federal law that allows a dealer to sell a firearm to a person after 72 hours even if a background check is not complete. Gardiner also called for closing the “boyfriend loophole” in federal law for domestic violence cases. The loophole says that to have restricted access to firearms, the domestic violence offense must have been committed by a current or former spouse, parent or guardian of the victim, or someone with whom the offender shares a child.

“The most effective way to stop gun violence in America is making sure we keep guns out of the hands of people who are too dangerous to own then, and that has nothing to do with the rights of law-abiding Americans,” Gardiner said.

But Keane said new laws won’t do anything if the ones currently on the books are not enforced.

“We’re not enforcing the laws we have now,” he said. “Why are we going to pass more laws? So they won’t be enforced?”

Kelley, who attacked the church in Texas, had been convicted at court-martial of charges stemming from a domestic violence case, but officials said the Air Force never notified the FBI of his conviction. That allowed him to pass a background check and to purchase multiple guns in stores.

FBI agents are seen behind yellow crime scene tape outside Rancho Tehama Elementary School after a shooting on Tuesday morning, in Rancho Tehama, Calif. (Elijah Nouvelage/AFP/Getty Images)

Neal was charged with assault with a deadly weapon against a neighbor in January. Court documents show he was subject to a protective order that prohibited him from having firearms. He was charged with six felonies, including assault with a deadly weapon and possession of an assault weapon.

His sister, Sheridan Orr, said Neal “never should have had guns and he should’ve been able to get mental health care.”

Neal was barred from having guns because of a court order issued this year. After Neal was arrested in January and charged with assaulting a neighbor with a deadly weapon, a judge issued a protective order prohibiting him from having, buying or attempting to purchase any firearms, court records show.

According to court documents, the neighbor Neal assaulted sought the protective order and said he “attacked me and my mother-in-law stabbing me with a knife and beating her.” In the request, she described Neal as unpredictable and angry, writing that he physically and verbally attacked multiple members of her household.

Authorities investigating Neal’s rampage recovered two semiautomatic rifles that were “manufactured illegally, we believe by him at his home,” Phil Johnston, an assistant sheriff in Tehama County, Calif., said at a briefing Wednesday. “So they were obtained in an illegal manner, not through a legal process.”

Johnston did not elaborate on how Neal manufactured the guns, but he said: “He can get parts through a number, a variety of sources, and they come together and they can build them in their shop, or they can build them in their garage.”

"Unfinished receivers," the metal pieces that allow guns to fire and can be used to build assault weapons, are not subject to background checks. They are not considered firearms under federal law and do not bear serial numbers that indicate where they were manufactured.

Neal also had a pair of handguns that were not registered to him, Johnston said.

Police in Tehama County said neighbors had alerted them to sounds of gunfire coming from Neal’s home in the past but that he did not open the door for officers and police did not specify whether those calls came before or after the protective order restricting Neal’s gun access.

Michael Hammond, legislative counsel for Gun Owners of America, said the nation is going through “cycles of horrific crimes, followed by mass attention, followed by copycat shootings to get the same amount of attention.” He does not believe that more laws are the answer, noting that one of the shootings occurred in California, which has some of the nation’s strictest gun laws. He said that is evidence that additional legislation won’t stop mass shootings.

“If California, with all this gun control, hasn’t been able to use gun control to stop it, what can you do that California hasn’t done?” he said.

Phil Johnston, assistant sheriff for Tehama County, displays a booking photo from a prior arrest of Kevin Neal, during a news conference on Wednesday. (Rich Pedroncelli/AP)