* Tunisia in "state of war" after double attacks

* Libya providing jihadi shelter across the border

* Crackdown at home may feed militant ranks

By Patrick Markey and Tarek Amara

SOUSSE, Tunisia, July 7(Reuters) - In late 2013, two young Tunisians returned from a jihadi camp in Libya planning to blow themselves up among foreign tourists. It was a spectacular failure - one bomb malfunctioned; the other killed only the bomber on the sands of Tunisia's Sousse resort.

Two years later, just a few months apart, three more Libyan-trained Tunisian militants succeeded in bringing bloodshed to Sousse and the capital Tunis, gunning down 60 people, mainly tourists, in the two deadliest attacks in the country's history.

Hailed as the only "Arab Spring" success, Tunisia now finds its young democracy under siege from a jihadist expansion in North Africa, where militants, radicalised locally and trained in Libya, are exporting their violent ideology back home.

Neither Saif Rezgui, the gunman who killed 38 people in last month's Sousse hotel attack, nor the two attackers who killed 22 people at Tunisia's Bardo National museum in March, revealed clues of their new-found radicalism. Authorities had little idea of their jihadist ties until they struck.

Claims of responsibility are hazy; all three gunmen were killed by police. Reports that another gunman escaped from the Bardo and is still at large have never been confirmed.

But what is clear is the threat to Tunisia from the jihadist haven over its frontier in chaotic Libya, and sleeper cells and extremist recruiters at home.

"This is a war against Tunisia," said Kamel Jendoubi, head of a government task force on Sousse. "It's not just about our tourism, or our economy, but Tunisian democracy."

President Beji Caid Essebsi said security forces had been surprised, and last week imposed a state of emergency. European partners are promising aid in training, border security and intelligence sharing for Tunisia's armed forces.

But balancing tough security with new-found freedoms may come with a price, and rights campaigners worry a crackdown is the slippery slope to oppression that may feed militant ranks in a country where many youth already feel alienated.

Murdering tourists was a deliberate strike at Tunisia's economic heart at a delicate time.

Since the 2011 uprising against autocrat Zine El Abidine Ben Ali - the first of the Arab leaders toppled in that year's wave of revolts across the region - Tunisia has been spared the bloodshed and civil war that hit other Arab countries.

But it has seen political turmoil and occasional unrest between secular and Islamist parties, and is only just now consolidating democratic gains and economic growth.

New freedoms that arrived after Ben Ali's fall allowed Salafist and ultra-conservative Islamists to emerge in one of the Arab world's most secular states. Some have turned violent and gone underground.

Signs of growing militancy have been clear for a while. More than 3,000 Tunisians now are fighting in Syria, Iraq and increasingly in Libya with Islamist militant groups, government officials say. Some fighters have promised to return home to carry out attacks.

"They came to attack the cities for the first time in Bardo. We thought it was the last time, but the big disaster was in Sousse," Essebsi said in a televised address this week to announce the state of emergency. "Another attack like this and the state could collapse."



LIBYA CONNECTION

Tunisian officials and foreign sources say the Bardo and Sousse attackers trained at the same time in a Libya jihadist camp near Sabratha, a town near the Tunisian border in an area where contraband and smuggling have long been a way of life.

Caught in a war between two rival factions and governments, Libya is spiralling deeper into chaos. Islamic State supporters and other militant groups are expanding in the security vacuum.

"Libya is our problem," said one Tunisian security source. "If Libya were stable, with one government and one army, then Tunisia would be safer. All the attacks were planned in Libya."

Even before the Bardo attack, several high-profile North African jihadi commanders were reportedly in Libya, including Algerian Mokhtar Belmokhtar, an al-Qaeda veteran who orchestrated the 2012 hostage seizure at the In Amenas gasfield in Algeria in which at least 67 people died.

U.S. officials say Belmokhtar, and Tunisian Ansar al-Sharia founder and leader Abu Iyad, may have been killed in a June air strike on a jihadist meeting near Benghazi. Militant groups deny that, and neither death has been confirmed.

Senior figures in Abu Iyad's group have largely relocated from Tunisia to Libya, mainly Sabratha, Tunisian officials say.

Tunisian authorities blame Abu Iyad's group for the Sousse attack, and blame the Bardo attack on another home-grown group with foreign ties, Okba Ibn Nafaa, which is aligned to al Qaeda and has fought a low-level insurgency against Tunisian authorities on the Algerian border.

The rise of the Islamic State group, which proclaimed a caliphate in Syria and Iraq last year, has made tracing the loyalties of militants more difficult.

Islamic State claimed responsibilty for both the Bardo and Sousse attacks, although a U.S. source familiar with the investigations said no evidence has emerged proving the attackers were directed by Islamic State's central command.

"The jihadi landscape in Libya and Tunisia is extremely fluid," said Geoff Porter, a North Africa expert at the West Point Center for Combating Terrorism. "What may dictate whether an individual joins this group or that is not whether he is really an adherent to the Islamic State's leadership or to al-Qaeda's but the likelihood of being able to train for jihad and to carry out attacks."

One key Tunisian recruiter for Ansar al-Sharia, Ahmed Rouissi, split to declare allegiance to Islamic State at the start of this year. Tunisian sources say he ran training and recruiting in Libya and was killed in the city of Sirte, now an Islamic State stronghold, just a few days before Bardo.

Tunisian militants have turned up fighting for Islamic State in Benghazi and Sirte. Local residents say one was a leading Islamic State commander in the group's Libyan stronghold of Derna before Islamic State was ousted from that city by rival Islamist fighters.



HOMEFRONT TROUBLES

But despite Tunisian officials pointing across the border, recruiting for Islamist militant ranks starts at home, where radical preachers and online recruiters found fertile ground.

Rezgui, the Sousse gunman, was in touch with jihadists over the internet, but had also attended a illegal mosque in Kairouan, the city where he studied, Tunisian security sources said. The Bardo gunmen were also radicalised at local mosques.

In the turmoil of the early days of the revolution, ultra-conservative imams took over mosques and religious schools to spread their hardline message. Only recently has the government retaken control and banned such preachers.

After Bardo and Sousse, authorities announced they would close 80 mosques. Officials have also banned a well-known salafist preacher from sermons and prohibited the leader of a Salafist political party from preaching.

"These mosques don't have permits, that were opened during the chaos after the revolution. They have extremist preaching or are operating illegally," said religious affairs ministry spokeswoman Najet Hammami.

But after Bardo attack, sweeping arrests, a new anti-terror law and harassment of conservative Islamists by police brought warnings from human rights lawyers and worries about a backlash among conservative youth who felt unfairly targeted.

Human rights groups worry the rights restrictions imposed under the latest 30-day State of Emergency - including possible restrictions on press, assembly and easier detentions - may be open to abuse by authorities under pressure for results.

Last week after police moved to stop one hardliner preaching, hundreds took to the streets in protest in Msaken, his town near Sousse.

In Kalaa Kbira, another nearby down, police fired tear gas during clashes on Thursday night to disperse youths opposed to the forced closure of a local mosque.

"These decisions are only going to create tensions," said Nassreddine, a local Islamist youth. "The government is declaring war on mosques, even moderates. It's just going to create more extremism not less."

(Reporting by Patrick Markey; Editing by Peter Graff) ((pat.markey@thomsonreuters.com; +21620349006; Reuters Messaging: pat.markey.thomsonreuters.com@reuters.net))