See also ExternalBackLink.
This is also called referrer tracking. You can't just list all referrers, because not all of them actually link to the page. You have to visit that page and check. If it really links, then link back to it.
This can also be exploited using so-called referrer spam. A porn site can link to your site, relying on your link back to it. The porn site might hide the link using CSS, so machine based link verification might not always work.
Another thing to avoid are search results. You usually don't want to link to the Google query that led to your site. One solution is to maintain a blacklist of regular expressions, perhaps using FileReplacement.
Automating your backlinks can have it's advantages and disadvantages. Make sure if you implement this you are aware that you are opening your site up for spam. [Back Links]
See also BackLink for internal backlinks.
It should be possible to use Google or other search engines for this: