{"id":127,"date":"2020-06-11T23:24:00","date_gmt":"2020-06-11T22:24:00","guid":{"rendered":"https:\/\/blog.hub47.com\/?p=127"},"modified":"2021-11-20T00:41:56","modified_gmt":"2021-11-20T00:41:56","slug":"build-tor-on-freebsd","status":"publish","type":"post","link":"https:\/\/blog.hub47.com\/?p=127","title":{"rendered":"Build TOR on FreeBSD"},"content":{"rendered":"\n<h1 class=\"wp-block-heading\" id=\"pre-intro\"><strong>Pre-intro<\/strong><\/h1>\n\n\n\n<p>For this article I used an AWS EC2 instance, running a FreeBSD AMI based on FreeBSD 13. I don&#8217;t recommend using this AMI because it has an issue with ncurses (not a huge problem, easily fixed), but instead go for the stable release, eg an AMI based on FreeBSD 12.<br>This means that I already had the binary pkg system in place and to become root I had to only type <code>su -<\/code>.<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"introduction\"><strong>Introduction<\/strong><\/h1>\n\n\n\n<p>Since you are going to be building software on FreeBSD, it&#8217;s fair to assume you are going to have some knowledge about the underlying platform, like installing packages and&#8230; becoming root.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"becoming-root\"><strong>Becoming root<\/strong><\/h2>\n\n\n\n<p>If you are running FreeBSD on a machine you administer, then you already know the root password, so you can use <code>su -<\/code> or you installed sudo. Details on how to install sudo can be found in the <a href=\"https:\/\/www.freebsd.org\/doc\/handbook\/security-sudo.html\">handbook<\/a>.<br><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"installing-packages\"><strong>Installing packages<\/strong><\/h2>\n\n\n\n<p>There are 2 ways to install software on FreeBSD that are more or less painless: <a href=\"https:\/\/www.freebsd.org\/doc\/handbook\/ports-using.html\">ports<\/a> and <a href=\"https:\/\/www.freebsd.org\/doc\/handbook\/pkgng-intro.html\">binary packages<\/a>. Keep in mind that if you add a package via pkg, then all deps not already present on the system will be added via the same method and if you add a package via the ports system, eg by <strong>building<\/strong>, then <strong>all not installed deps<\/strong> will be added via the ports system, eg by <strong>building<\/strong> them. On your machine. You don&#8217;t want that. Unless you have access to huge amounts of computing and disk power and you don&#8217;t care about the carbon footprint. Carbon Aware Software should be a thing.<br><\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"hacking-tor-on-freebsd\"><strong>Hacking TOR on FreeBSD<\/strong><\/h1>\n\n\n\n<p>I am going to use <strong>security\/tor<\/strong> as an example.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"primer-on-freebsd-ports-system\"><strong>Primer on FreeBSD Ports System<\/strong><\/h2>\n\n\n\n<p>The FreeBSD Ports System is one of the best build system I ever worked with. It needs a local database to know how to build things, with files in <strong>PORTSDIR<\/strong> (default \/usr\/ports) and <strong>PORT_DBDIR<\/strong> (default \/var\/db\/ports). As a developer you should check the <a href=\"https:\/\/www.freebsd.org\/doc\/en_US.ISO8859-1\/books\/porters-handbook\/\">Porter&#8217;s Handbook<\/a>.<\/p>\n\n\n\n<p><strong>PORTSDIR<\/strong> is the directory where the ports database itself resides, along with a set of utilities. More detailed <a href=\"https:\/\/www.freebsd.org\/cgi\/man.cgi?ports(7)\">info<\/a>. For each port it contains information about how to build the port (and consequently the binary package), such as port Makefile and whatever patches are required to make the thing work on FreeBSD. This database is managed by <a href=\"https:\/\/www.freebsd.org\/cgi\/man.cgi?portsnap(8)\">portsnap<\/a>.<\/p>\n\n\n\n<p><strong>PORT_DBDIR<\/strong> is the directory where the OPTIONS for each port are stored. Eg, use libzstd, install the docs etc. More detailed <a href=\"https:\/\/www.freebsd.org\/cgi\/man.cgi?ports(7)\">info<\/a>.<\/p>\n\n\n\n<p><strong>DISTDIR<\/strong> (default <strong>PORTSDIR<\/strong>\/distfiles), where the source archives are downloaded at build time.<\/p>\n\n\n\n<p><strong>WRKDIRPREFIX<\/strong> (default <strong>PORTSDIR<\/strong>\/path\/to\/port\/work), where the build artefacts and what not are written.<\/p>\n\n\n\n<p><em>There is a way to have a partial <\/em><strong>PORTSDIR<\/strong><em> and still be able to build things, but is not supported by FreeBSD and might not even save you that much time.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"0-get-update-the-ports-db-and-install-deps\"><strong>0. Get\/update the ports db and install deps<\/strong><\/h2>\n\n\n\n<p><br>Most likely you need to do this only every so often, maybe once a week? See portsnap man page for more info. As root, fetch a new snapshot of the ports database and extract it:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>portsnap fetch &amp;&amp; portsnap auto<\/code><\/pre>\n\n\n\n<p>As root, install deps (both build and run):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>pkg install `make -C \/usr\/ports\/security\/tor run-depends-list build-depends-list | awk -F '\/' '{ print $NF }'`<\/code><\/pre>\n\n\n\n<p>Done.<\/p>\n\n\n\n<p><em>From now on, all the commands should be run in ~\/building_tor or similar, as your regular user.<\/em><\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"1-configure-the-port\"><br><strong>1. Configure the port<\/strong><\/h2>\n\n\n\n<p><br>Let&#8217;s assume we want to hack on tor with lib zstd, both the shared build and static.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>make -C \/usr\/ports\/security\/tor PORT_DBDIR=${PWD}\/options\/non-static config<\/code><\/pre>\n\n\n\n<p>and uncheck everything apart ZSTD.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code><meta charset=\"utf-8\">make -C \/usr\/ports\/security\/tor PORT_DBDIR=${PWD}\/options\/static config<\/code><\/pre>\n\n\n\n<p>and uncheck everything apart STATIC_TOR and ZSTD.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"2-prepare-the-sources\"><strong>2. Prepare the sources<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>make -C \/usr\/ports\/security\/tor DISTDIR=${PWD}\/distfiles WRKDIRPREFIX=${PWD}\/work extract<\/code><\/pre>\n\n\n\n<p>This will download the source tarball in <strong>DISTDIR<\/strong> and unpack it in <code><strong>WRKDIRPREFIX<\/strong>\/<strong>PORTSDIR<\/strong>\/&lt;path_to_port&gt;\/work\/port_name-version<\/code>, eg in my case &#8220;<code>${PWD}work\/usr\/ports\/security\/tor\/work\/tor-0.4.3.5\/<\/code>&#8220;.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\" id=\"3-build-the-package\"><strong>3. Build (the package)<\/strong><\/h2>\n\n\n\n<pre class=\"wp-block-code\"><code>make -C \/usr\/ports\/security\/tor DISTDIR=${PWD}\/distfiles WRKDIRPREFIX=${PWD}\/work PORT_DBDIR=${PWD}\/options\/&lt;variant&gt;\/<\/code><\/pre>\n\n\n\n<p>where <strong>&lt;variant&gt;<\/strong> is <strong>static<\/strong> or <strong>non-static<\/strong> in our case.<\/p>\n\n\n\n<p>The output in this case will be <code>${PWD}\/work\/usr\/ports\/security\/tor\/work\/stage<\/code> (eg the binary itself being <code>${PWD}\/work\/usr\/ports\/security\/tor\/work\/stage\/usr\/local\/bin\/tor)<\/code>.<\/p>\n\n\n\n<p><a href=\"https:\/\/github.com\/freebsd\/freebsd-ports\/blob\/master\/Mk\/bsd.port.mk\">Further reading<\/a> (search for <em>Default targets and their behaviors<\/em>)<\/p>\n\n\n\n<h1 class=\"wp-block-heading\" id=\"closing\"><strong>Closing<\/strong><\/h1>\n\n\n\n<p>I think a jail setup will make things a bit easier, you won&#8217;t need to use so many make variables, but that&#8217;s for another time.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Pre-intro For this article I used an AWS EC2 instance, running a FreeBSD AMI based on FreeBSD 13. I don&#8217;t recommend using this AMI because it has an issue with ncurses (not a huge problem, easily fixed), but instead go for the stable release, eg an AMI based on FreeBSD 12.This means that I already [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10],"tags":[9,11,12],"class_list":["post-127","post","type-post","status-publish","format-standard","hentry","category-tech","tag-devops","tag-freebsd","tag-tor"],"_links":{"self":[{"href":"https:\/\/blog.hub47.com\/index.php?rest_route=\/wp\/v2\/posts\/127","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.hub47.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.hub47.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.hub47.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.hub47.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=127"}],"version-history":[{"count":10,"href":"https:\/\/blog.hub47.com\/index.php?rest_route=\/wp\/v2\/posts\/127\/revisions"}],"predecessor-version":[{"id":143,"href":"https:\/\/blog.hub47.com\/index.php?rest_route=\/wp\/v2\/posts\/127\/revisions\/143"}],"wp:attachment":[{"href":"https:\/\/blog.hub47.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=127"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.hub47.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=127"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.hub47.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=127"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}