Bug 15804 - "samba-tool domain backup offline" hangs
Summary: "samba-tool domain backup offline" hangs
Status: RESOLVED FIXED
Alias: None
Product: Samba 4.1 and newer
Classification: Unclassified
Component: AD: LDB/DSDB/SAMDB (show other bugs)
Version: 4.19.7
Hardware: All All
: P5 normal (vote)
Target Milestone: ---
Assignee: Jule Anger
QA Contact: Samba QA Contact
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2025-02-11 14:42 UTC by Andrea Venturoli
Modified: 2025-03-06 14:08 UTC (History)
3 users (show)

See Also:


Attachments
Patch against master (1.24 KB, patch)
2025-02-19 07:55 UTC, Michael Osipov
no flags Details
Patch against master (1.27 KB, patch)
2025-02-19 07:57 UTC, Michael Osipov
no flags Details
Patch against master (1.32 KB, patch)
2025-02-20 08:18 UTC, Michael Osipov
no flags Details
patch for 4.20 (1.51 KB, patch)
2025-02-27 01:14 UTC, Douglas Bagnall
jsutton: review+
Details
patch for 4.21 (1.51 KB, patch)
2025-02-27 01:15 UTC, Douglas Bagnall
jsutton: review+
Details
patch for 4.22 (1.51 KB, patch)
2025-02-27 01:16 UTC, Douglas Bagnall
jsutton: review+
Details

Note You need to log in before you can comment on or make changes to this bug.
Description Andrea Venturoli 2025-02-11 14:42:11 UTC
Running "samba-tool domain backup offline" hangs almost immidiately for me (since its introduction in Samba 4.10):

#samba-tool domain backup offline --targetdir .
running backup on dirs: /var/db/samba4/private /var/db/samba4 /usr/local/etc
Starting transaction on /var/db/samba4/private/secrets
(...)

What really hangs is a subprocess that samba-tool starts:
/usr/local/bin/tdbbackup -s .copy.tdb /var/db/samba4/private/secrets.ldb -r




The problem seems to lie in python/samba/tdb_util.py, where we find:
    tdbbackup_cmd = [toolpath, "-s", ".copy.tdb", file1]
    if readonly:
        tdbbackup_cmd.append("-r")

Since readonly is required (otherwise the afore mentioned hanging happens), -r is added, resulting in the above command line.

However, tdbbackup uses getopt to parse its arguments and getopt stops at the first token that doesn't start with -, i.e. "/var/db/samba4/private/secrets.ldb", so "-r" is never considered.
tdbbackup then tries to open the databases read/write and hangs since Samba is using them.



It works if I change the above code (in python/samba/tdb_util.py) to something like:
    if readonly:
        tdbbackup_cmd = [toolpath, "-r", "-s", ".copy.tdb", file1]
    else:
        tdbbackup_cmd = [toolpath, "-s", ".copy.tdb", file1]
Comment 1 Michael Osipov 2025-02-18 08:00:57 UTC
Here is the reason why it does not fail on GNU/Linux:

>        By default, getopt() permutes the contents of argv as it scans, so  that  eventually  all  the
>        nonoptions  are  at the end.  Two other modes are also implemented.  If the first character of
>        optstring is '+' or the environment variable POSIXLY_CORRECT is set,  then  option  processing
>        stops  as soon as a nonoption argument is encountered.  If the first character of optstring is
>        '-', then each nonoption argv-element is handled as if it were the argument of an option  with
>        character  code  1.   (This  is used by programs that were written to expect options and other
>        argv-elements in any order and that care about the ordering of the two.)  The special argument
>        "--" forces an end of option-scanning regardless of the scanning mode.

glibc is by default not POSIX compliant that is why it accepts the out of order args. This is a pure portability issue.
Comment 2 Michael Osipov 2025-02-18 16:54:50 UTC
I have applied the patch downstream, but it has to be addressed upstream in a portable (POSIX compliant) way.
Comment 3 Douglas Bagnall 2025-02-18 23:15:15 UTC
Thank you Andrea.

do you want to write that as a patch?
Comment 5 Michael Osipov 2025-02-19 07:55:28 UTC
Created attachment 18575 [details]
Patch against master

Here is a Git-formatted patch. Cherry-picking to previous branches is straight forward.
Comment 6 Michael Osipov 2025-02-19 07:57:40 UTC
Created attachment 18576 [details]
Patch against master
Comment 7 Douglas Bagnall 2025-02-19 22:26:50 UTC
Comment on attachment 18576 [details]
Patch against master

The patch looks good! Can I ask for two more things:

1. add "Signed-off-by: Andrea Venturoli <ml@netfence.it>" at the bottom (`git commit --amend -s` will do it)

2. Read https://www.samba.org/samba/devel/copyright-policy.html and send in the declaration paperwork?
Comment 8 Douglas Bagnall 2025-02-19 22:32:16 UTC
I have had a bit of a poke around to see if we do this in other places, but I haven't found anything. 

I think usually we're OK because
a) we use lib/cmdline or python for parsing arguments, or 
b) we don't often call subprocesses from end-user tools, or
c) we happen to get things in the right order.
Comment 9 Michael Osipov 2025-02-20 08:13:42 UTC
(In reply to Douglas Bagnall from comment #7)

I will amend the patch, I'd also would like to do this with my FreeBSD email address as an individual contributor. Does it make any difference?
Comment 10 Michael Osipov 2025-02-20 08:18:24 UTC
Created attachment 18579 [details]
Patch against master

Updated patch.
Comment 11 Michael Osipov 2025-02-20 08:22:25 UTC
CLA sent from michaelo AT FreeBSD.org
Comment 12 Michael Osipov 2025-02-20 18:00:04 UTC
slow@ has confirmed my CLA today.
Comment 13 Douglas Bagnall 2025-02-20 21:02:30 UTC
(In reply to Michael Osipov from comment #12)
> slow@ has confirmed my CLA today.

That's good, but I have got mixed up, sorry. We really need the CLA from Andrea, not you, to match the patch:

> From: Andrea Venturoli <ml@netfence.it>
Comment 14 Andrea Venturoli 2025-02-21 07:48:33 UTC
(In reply to Douglas Bagnall from comment #13)

Sorry, what's a CLA?
I'd be glad to see this patch committed (I'm already used and it was already committed downstream in FreeBSD).
Comment 15 Michael Osipov 2025-02-21 07:50:13 UTC
(In reply to Douglas Bagnall from comment #13)

Never mind, I will contribute as well, but left Andrea's notice because he's the author, not me.
Comment 16 Michael Osipov 2025-02-21 07:52:21 UTC
(In reply to Andrea Venturoli from comment #14)

It is a legal document that you provide you contribution under a certain license/conditions, see a few comments above. From your your mail account with the content provided and done.
Comment 17 Andrea Venturoli 2025-02-21 09:00:30 UTC
(In reply to Michael Osipov from comment #16)
 
CLA sent.
Thanks.
Comment 18 Michael Osipov 2025-02-25 12:42:24 UTC
Both CLAs have been provided. Is there anything we can do for this patch to land and being backported?
Comment 19 Douglas Bagnall 2025-02-25 19:45:08 UTC
(In reply to Michael Osipov from comment #18)
I'll push it through a gitlab CI pipeline, another team member will review it, we'll attach backport patches for the release branches here, and the release manager will apply those.

The thing you can do is remind me if I forget, which so far you are doing well.
Comment 20 Michael Osipov 2025-02-26 07:43:52 UTC
(In reply to Douglas Bagnall from comment #19)

Thank you Douglas, appreciated!
Comment 21 Samba QA Contact 2025-02-27 01:01:04 UTC
This bug was referenced in samba master:

7e083a6b3a12933b79ef19ccbd4c13bfa0203498
Comment 22 Douglas Bagnall 2025-02-27 01:14:49 UTC
Created attachment 18587 [details]
patch for 4.20
Comment 23 Douglas Bagnall 2025-02-27 01:15:18 UTC
Created attachment 18588 [details]
patch for 4.21
Comment 24 Douglas Bagnall 2025-02-27 01:16:07 UTC
Created attachment 18589 [details]
patch for 4.22

The patches are all exactly the same, apart from the commit hashes.
Comment 25 Jule Anger 2025-03-04 09:13:48 UTC
Pushed to autobuild-v4-{22,21,20}-test.
Comment 26 Samba QA Contact 2025-03-04 11:24:03 UTC
This bug was referenced in samba v4-20-test:

cabe5f89975ddd395abb7d77d89f18da9807762c
Comment 27 Samba QA Contact 2025-03-04 13:36:03 UTC
This bug was referenced in samba v4-21-test:

480f27625dd7eb8af499fe07e5f6d7c01d6bb60a
Comment 28 Samba QA Contact 2025-03-04 14:58:04 UTC
This bug was referenced in samba v4-22-test:

a210c2b2bc53ebae5e0ee64eb51fcd117bd231ee
Comment 29 Jule Anger 2025-03-04 15:00:57 UTC
Closing out bug report.

Thanks!
Comment 30 Michael Osipov 2025-03-04 15:01:48 UTC
(In reply to Jule Anger from comment #29)

Magic, thank you guys. I will try to provide more patches in the upcoming weeks and months.
Comment 31 Samba QA Contact 2025-03-06 14:08:00 UTC
This bug was referenced in samba v4-22-stable (Release samba-4.22.0):

a210c2b2bc53ebae5e0ee64eb51fcd117bd231ee