Under capitalism, where the right to defense is delegated to the government, it is the government’s responsibility to prevent the entrance of terrorists and other rights-violating individuals from entering the country by securing the nation’s borders — similar in principle to how one secures their house and property. The implementation of this principle depends on many factors in a given context (which can change over time). The immigration policies of a nation-state under war would be different from one at peace. The immigration policies towards a citizen of a capitalist country (i.e., we have a valid criminal record from a foreign government we trust) would be different than to a citizen from a communist/socialist/theocratic one (i.e., is the immigrant a spy or a political refugee?).
Similarly, in regards to emigration, a rights-respecting individual is also not prevented from leaving a country (so long as their emigration is not resulting in the violation of the rights of others, or illegally interfering with the government’s ability to protect rights, i.e., smuggling stolen goods outside the country). Generally, it is communist states — like the former Soviet bloc East Germany and Castro’s Cuba — that have to build walls, not to prevent invaders from entering, insomuch as to prevent “masses yearning to breathe free” from leaving these prison states.
Under capitalism, the government’s only concern in regards to immigration (or emigration) is that the migrating individual will respect the laws of the country, i.e., respect individual rights. Under capitalism, the government is not permitted to ban a rights-respecting foreign individual from immigrating into the country — especially if a property owner in that country is welcoming that immigrant onto his property.