Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save movalex/9ee4207c52a291d2eec70d3517c879f3 to your computer and use it in GitHub Desktop.
Save movalex/9ee4207c52a291d2eec70d3517c879f3 to your computer and use it in GitHub Desktop.
Compile Samba on Macos

Compile Samba on macos 12 (Monterey)

git clone https://git.samba.org/samba.git

brew install cmake gnutls jansson libarchive openssl pkg-config [email protected]
brew install readline && brew link --force readline

export CPPFLAGS="-I/usr/local/opt/libarchive/include -I/usr/local/opt/readline/include -I/usr/local/opt/openssl@3/include"
export LDFLAGS="-L/usr/local/opt/libarchive/lib -L/usr/local/opt/readline/lib -L/usr/local/opt/openssl@3/lib"

cpan install Parse::Yapp::Driver
cpan install JSON

./configure --prefix=/opt/samba --without-ad-dc --without-acl-support --without-gettext

make
sudo make install

FYR: https://wiki.samba.org/index.php/Build_Samba_from_Source

Compile Samba 4.13.17 on Macos 10.13

This is legacy way for the unsupported macos versions.

$ git clone https://git.samba.org/samba.git
$ git checkout samba-4.13.17
$ brew install jansson
$ brew install readline && brew link --force readline
$ cpan # then w/in cpan install Parse::Yapp module (`install Parse::Yapp::Driver`)
$ brew install libarchive
$ export LDFLAGS="-L/usr/local/opt/libarchive/lib"
$ export CPPFLAGS="-I/usr/local/opt/libarchive/include"
$ ./configure --prefix=/opt/samba --without-ad-dc --without-acl-support

Patch these files

  • source4/torture/libsmbclient/libsmbclient.c
  • source3/lib/system_smbd.c
  • source3/libsmb/libsmb_stat.c
  • samba/libcli/smbreadline/smbreadline.c

as follows:

diff --git a/libcli/smbreadline/smbreadline.c b/libcli/smbreadline/smbreadline.c
index 6929209be20..90a2dea12c5 100644
--- a/libcli/smbreadline/smbreadline.c
+++ b/libcli/smbreadline/smbreadline.c
@@ -53,6 +53,7 @@ static bool smb_rl_done;
  * readline.so has it
  */
 extern int rl_done;
+extern Function *rl_event_hook;
 #endif

 void smb_readline_done(void)
@@ -147,7 +148,11 @@ char *smb_readline(const char *prompt, void (*callback)(void),

 #ifdef HAVE_DECL_RL_EVENT_HOOK
        if (callback)
-               rl_event_hook = (rl_hook_func_t *)callback;
+#if defined(_RL_FUNCTION_TYPEDEF)
+        rl_event_hook = (rl_hook_func_t *)callback;
+#else
+    rl_event_hook = (Function *)callback;
+#endif
 #endif
        ret = readline(prompt);
        if (ret && *ret)
diff --git a/source3/lib/system_smbd.c b/source3/lib/system_smbd.c
index 3b1ac9c1c2a..896105a1ae5 100644
--- a/source3/lib/system_smbd.c
+++ b/source3/lib/system_smbd.c
@@ -205,7 +205,11 @@ bool getgroups_unix_user(TALLOC_CTX *mem_ctx, const char *user,
                         gid_t primary_gid,
                         gid_t **ret_groups, uint32_t *p_ngroups)
 {
+#if defined(DARWINOS)
+    int max_grp = 128;
+#else
        int max_grp = MIN(128, groups_max());
+#endif
        gid_t stack_groups[max_grp];
        uint32_t ngrp;
        gid_t *temp_groups = stack_groups;
diff --git a/source3/libsmb/libsmb_stat.c b/source3/libsmb/libsmb_stat.c
index 790934bd565..fed2c8c63c3 100644
--- a/source3/libsmb/libsmb_stat.c
+++ b/source3/libsmb/libsmb_stat.c
@@ -102,6 +102,11 @@ void setup_stat(struct stat *st,
        }

        st->st_dev = dev;
+    #if defined(__APPLE__) || defined(__NetBSD__)
+        #define st_atim st_atimespec
+        #define st_ctim st_ctimespec
+        #define st_mtim st_mtimespec
+    #endif
        st->st_atim = access_time_ts;
        st->st_ctim = change_time_ts;
        st->st_mtim = write_time_ts;
diff --git a/source4/torture/libsmbclient/libsmbclient.c b/source4/torture/libsmbclient/libsmbclient.c
index 3f3992593f9..6275bad1159 100644
--- a/source4/torture/libsmbclient/libsmbclient.c
+++ b/source4/torture/libsmbclient/libsmbclient.c
@@ -1231,6 +1231,11 @@ static bool torture_libsmbclient_utimes(struct torture_context *tctx)
        ret = smbc_fstat(fhandle, &st);
        torture_assert_int_not_equal(tctx, ret, -1, "smbc_fstat failed");

+    #if defined(__APPLE__) || defined(__NetBSD__)
+        #define st_atim st_atimespec
+        #define st_mtim st_mtimespec
+    #endif
+
        tbuf[0] = convert_timespec_to_timeval(st.st_atim);
        tbuf[1] = convert_timespec_to_timeval(st.st_mtim);

and hopefully it builds for you with just the typical set of warnings..

$ make

and optionally

$ sudo make install

FYR - some reference docs which are actually useful now that you have $ make; working. https://wiki.samba.org/index.php/Build_Samba_from_Source

Create samba launchers plists

/Library/LaunchDaemons/com.samba.smbd.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.samba.smbd</string>
    <key>ProgramArguments</key>
    <array>
        <string>/opt/samba/sbin/smbd</string>
        <string>--foreground</string>
        <string>--no-process-group</string>
        <string>-s</string>
        <string>/opt/samba/etc/smb.conf</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <dict>
        <key>SuccessfulExit</key>
        <false/>
        <key>Crashed</key>
        <true/>
    </dict>
</dict>
</plist>

/Library/LaunchDaemons/com.samba.nmbd.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.samba.nmbd</string>
    <key>ProgramArguments</key>
    <array>
        <string>/opt/samba/sbin/nmbd</string>
        <string>-F</string>
        <string>--no-process-group</string>
    </array>
    <key>KeepAlive</key>
        <false/>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

Create log folder:

sudo mkdir /var/log/samba/

Optionally disable native NETBIOS service:

sudo launchctl unload -w /System/Library/LaunchDaemons/com.apple.netbiosd.plist

see additional info here

Create basic samba config file:

# /opt/samba/etc/smb.conf
[global]
    workgroup = WORKGROUP
    netbios name = server
    debug hires timestamp = No
    debug timestamp = yes
    idmap cache time = 604800
    ldap ssl = start tls
    log file = /var/log/samba/%m.log
    log level = 1
    max log size = 10000
    passdb backend = tdbsam://etc/passdb.tdb
[SHARE_NAME]
    path = /Volumes/DISK_NAME
    public = yes
    browsable = yes
    writable = yes
    directory mask = 0755
    create mask = 0644
    guest ok = no

Add samba directory to the PATH variable:

export PATH=/opt/samba/bin:/opt/samba/sbin:$PATH

Setup username and password for samba share:

sudo smbpasswd -a <username>

Load the services:

sudo launchctl bootstrap system/ /Library/LaunchDaemons/com.samba.smbd.plist
sudo launchctl bootstrap system/ /Library/LaunchDaemons/com.samba.nmbd.plist
sudo launchctl list | grep samba
launchctl print system/com.samba.smbd | grep "state"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment