summaryrefslogtreecommitdiff
path: root/python/samba/netcmd/__init__.py
AgeCommit message (Collapse)AuthorFilesLines
2025-08-20samba-tool: Command.message() can print multiple linesDouglas Bagnall1-2/+3
Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Gary Lockyer <gary@catalyst.net.nz>
2025-08-20samba-tool: add verbose flag to @exception_to_command_errorDouglas Bagnall1-1/+19
Helpful in development. Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Gary Lockyer <gary@catalyst.net.nz>
2025-08-20samba-tool: add decorator to catch exception typesDouglas Bagnall1-0/+18
Often we [think we] know that all exceptions of a certain type should be formatted as CommandErrors (i.e., the traceback is suppressed, and the message is assumed intelligible). Rather than riddling .run() with try...except blocks to do this, we can @exception_to_command_error(ModelError) def run(...) which makes any ModelError into a CommandError in that samba-tool command. Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Gary Lockyer <gary@catalyst.net.nz>
2025-02-08samba-tool: --version shortcircuits option evaluationDouglas Bagnall1-2/+5
This means in bin/samba-tool spn -h -V the -V takes precedence over the -h, as with the 'net' tool. BUG: https://bugzilla.samba.org/show_bug.cgi?id=15770 Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Jennifer Sutton <jennifersutton@catalyst.net.nz>
2025-02-08samba-tool: all subcommands know --versionDouglas Bagnall1-2/+33
Before `samba-tool -V` would give you the version, but `samba-tool spn -V` would complain. An ad-hoc selection of sub-commands already supported --version, depending on whether VersionOptions was manually added to the takes_options dict. The .run() methods of these subcommands all take a 'versionopts' keyword argument, but never use it. If it was set (i.e., argv contained "--version"), the process never gets to .run(), so the value of versionopts.version is always None in run(). After this commit we can remove VersionOptions/versionopts from sub-commands. BUG: https://bugzilla.samba.org/show_bug.cgi?id=15770 Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Jennifer Sutton <jennifersutton@catalyst.net.nz>
2025-02-08samba-tool: do not complain of no sub-command with '-V'Douglas Bagnall1-1/+2
BUG: https://bugzilla.samba.org/show_bug.cgi?id=15770 Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Jennifer Sutton <jennifersutton@catalyst.net.nz>
2024-12-20samba-tool: no traceback for unauthenticated rootdse accessDouglas Bagnall1-0/+3
Under some circumstances rootdse returns an operations error with "Operation unavailable without authentication" instead of insufficient access rights. Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andreas Schneider <asn@samba.org> Autobuild-User(master): Douglas Bagnall <dbagnall@samba.org> Autobuild-Date(master): Fri Dec 20 00:00:02 UTC 2024 on atb-devel-224
2024-03-04samba-tool: tidy up uncaught insufficient rights LdbErrorDouglas Bagnall1-1/+4
It is likely that many sub-commands will produce a traceback when people go `-H ldap://server -Ubob` when they needed to go `-UAdministrator`. We can catch these and show only the core message. Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2024-03-01netcmd: properly show command name in show helpRob van der Linde1-1/+1
This comes up if a user ends up typing something wrong, and it incorrectly showed only part of the command under Usage: Signed-off-by: Rob van der Linde <rob@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
2024-03-01netcmd: add newline before epilog so there is a space betweenRob van der Linde1-1/+1
Signed-off-by: Rob van der Linde <rob@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz>
2024-03-01samba-tool: don't error if there are no sub-commandsDouglas Bagnall1-1/+1
This is useful when you commit samba-tool tests before you commit the samba-tool code, and you want the tests to fail rather than error. Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2024-02-29samba-tool: with --json, error messages are in JSONDouglas Bagnall1-0/+13
Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2024-02-29samba-tool: instances remember whether --json was requestedDouglas Bagnall1-0/+8
All our subcommands are going to learn --json eventually, and they shouldn't all have to do this individually. The next commit uses this to automatically format CommandErrors as JSON. Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2024-02-29samba-tool: add self.print_json_status() helperDouglas Bagnall1-0/+44
This is a helper to return JSON for simple messages. Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2024-02-29samba-tool: avoid mutable Command class valuesDouglas Bagnall1-2/+2
These values are shared across all instances of the class, which makes no difference in samba-tool itself, because there is one instance per process. But in tests we can have many Command classes at once (due to runcmd()), and if any of them happened to append to takes_args or takes_options rather than replacing it, well, the effect would be subtle. Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2023-11-01samba-tool: try to present diagnostics for SDDL errors.Douglas Bagnall1-0/+28
Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2023-10-24netcmd: auth policy: add OptionGroup classes for user, service and computer ↵Rob van der Linde1-1/+1
options Signed-off-by: Rob van der Linde <rob@catalyst.net.nz> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2023-10-24python: netcmd: make use of HostOptions for claims and sites commandsRob van der Linde1-2/+2
Signed-off-by: Rob van der Linde <rob@catalyst.net.nz> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2023-10-24python: getopt: subclass OptionParser to populate option_classRob van der Linde1-2/+2
The option_class needs to be set correctly for OptionGroups that use self.add_option Override OptionParser `__init__` to change the default Option class to the samba one. Signed-off-by: Rob van der Linde <rob@catalyst.net.nz> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2023-10-24python: netcmd: catch parent exception class OptParseError insteadRob van der Linde1-2/+2
This covers both OptionError and OptionValueError Signed-off-by: Rob van der Linde <rob@catalyst.net.nz> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2023-10-24python: getopt: rename SambaOption to OptionRob van der Linde1-2/+1
* Nothing uses SambaOption directly, everything imports it through samba.netcmd.Option * Avoid an unnecessary rename on import Signed-off-by: Rob van der Linde <rob@catalyst.net.nz> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2023-10-24python: netcmd: remove redundant Option subclassRob van der Linde1-5/+2
Instead, just import SambaOption as Option, so all the existing commands don't need changing. Signed-off-by: Rob van der Linde <rob@catalyst.net.nz> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2023-10-24python: netcmd: SUPPRESS_HELP constant has no effect hereRob van der Linde1-1/+1
Where it is used, on a few options, the constant should be used directly instead. This means that in the following commit, the Option subclass of SambaOption can be removed, as it will become redundant. Signed-off-by: Rob van der Linde <rob@catalyst.net.nz> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2023-10-24python: getopt: move validators logic to parent classRob van der Linde1-17/+0
Signed-off-by: Rob van der Linde <rob@catalyst.net.nz> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2023-10-24netcmd: don't turn exception into CommandError in run_validatorsRob van der Linde1-12/+10
It's the wrong place to do it. Instead, let it raise the original exception, capture it in _run, and call existing show_command_error method. Signed-off-by: Rob van der Linde <rob@catalyst.net.nz> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2023-10-24netcmd: PEP8: minor whitespace fix, file did not pass PEP8Rob van der Linde1-1/+1
Signed-off-by: Rob van der Linde <rob@catalyst.net.nz> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2023-10-24netcmd: move comment above class to docstringRob van der Linde1-3/+2
Signed-off-by: Rob van der Linde <rob@catalyst.net.nz> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2023-10-24netcmd: correctly pass Samba option class to OptionParserRob van der Linde1-1/+3
On OptionGroups it will set option_class and then this gets used by self.add_option Signed-off-by: Rob van der Linde <rob@catalyst.net.nz> Reviewed-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2023-06-25netcmd: move method print_json to command base classRob van der Linde1-0/+11
This is used in quite a few commands, move to base class. This ensures the correct encoder class and settings are always used, and they are only defined in one place. Signed-off-by: Rob van der Linde <rob@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org> Reviewed-by: Joseph Sutton <josephsutton@catalyst.net.nz>
2023-06-25netcmd: fix import sort/grouping as per python standardRob van der Linde1-4/+5
Signed-off-by: Rob van der Linde <rob@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org> Reviewed-by: Joseph Sutton <josephsutton@catalyst.net.nz>
2023-06-25netcmd: move ldb_connect method to base classRob van der Linde1-0/+9
This method is needed by just about every command and moving it here is another step towards elinimanting the base classes in domain/auth and domain/claim. The base classes are almost empty now, since introducing the model layer. The next step is to get rid of these base classes completely. Signed-off-by: Rob van der Linde <rob@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org> Reviewed-by: Joseph Sutton <josephsutton@catalyst.net.nz>
2023-06-25netcmd: PEP257 fix incorrect docstring quotesRob van der Linde1-1/+1
Signed-off-by: Rob van der Linde <rob@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org> Reviewed-by: Joseph Sutton <josephsutton@catalyst.net.nz>
2023-06-25netcmd: add optparse validators and Range validatorRob van der Linde1-0/+28
Add the ability to the add validators to optparse Option fields. The Option class was already subclassed in `netcmd/__init__.py` so adding some functionality to this was relatively easy. Added the ability to add Validator classes to a field so that this can be used for anything else in the future, but for now there is a Range validator required by upcoming auto silo commands. Signed-off-by: Rob van der Linde <rob@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org> Reviewed-by: Joseph Sutton <josephsutton@catalyst.net.nz>
2023-06-23python:samba:netcmd: Fix code spellingAndreas Schneider1-3/+3
Signed-off-by: Andreas Schneider <asn@samba.org> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2022-09-16samba-tool: no stack trace on missing ldb tdbDouglas Bagnall1-0/+4
Now, in a testenv, if you forget to use '-s st/ad_dc/etc/smb.conf', you only see this: $ bin/samba-tool user rename dsadsa ldb: Unable to open tdb '$HERE/st/client/private/secrets.ldb': No such file or directory ldb: Failed to connect to '$HERE/st/client/private/secrets.ldb' with backend 'tdb': Unable to open tdb '$HERE/st/client/private/secrets.ldb': No such file or directory Could not find machine account in secrets database: Failed to fetch machine account password from secrets.ldb: Could not open secrets.ldb and failed to open $HERE/st/client/private/secrets.tdb: NT_STATUS_CANT_ACCESS_DOMAIN_INFO ltdb: tdb($HERE/st/client/private/sam.ldb): tdb_open_ex: could not open file $HERE/st/client/private/sam.ldb: No such file or directory Unable to open tdb '$HERE/st/client/private/sam.ldb': No such file or directory Failed to connect to 'tdb://$HERE/st/client/private/sam.ldb' with backend 'tdb': Unable to open tdb '$HERE/st/client/private/sam.ldb': No such file or directory ERROR(ldb): uncaught exception - Unable to open tdb '$HERE/st/client/private/sam.ldb': No such file or directory rather than all that AND a stack trace. Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2022-09-16samba-tool: write ERROR in red if colour is wantedDouglas Bagnall1-6/+15
Often we'll write something like ERROR: Unable to find user "potato" which can get lost in the jumble of other output. With this patch, we colour the word "ERROR" red but not the rest of the string, unless it is determined that colour is not wanted (due to one of --color=never, NO_COLOR=1, output is not a tty). We choose to redden the word "ERROR" only to maintain legibility in the actual message, while hopefully increasing the noticeability of the line. Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2022-09-16samba-tool: --color=auto looks at stderr and stdoutDouglas Bagnall1-1/+3
More often than not we are using colour in stderr, but are deciding based on stdout's tty-ness. This patch changes to use both, and will affect the following situation: samba-tool 2>/tmp/errors # used to be colour, now not. of course, if you want colour, you can always samba-tool --color=yes 2>/tmp/errors Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2022-09-16samba-tool: save --color choice for subcommandsDouglas Bagnall1-0/+2
In particular, visualize needs it to decide colour for an output file that may or may not be stdout, so it needs to make its own decision for that file. Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2022-09-16samba-tool: make --color a general optionDouglas Bagnall1-0/+10
We don't put --color into options.SambaOptions because we can't handle the 'auto' case in the options module without knowing whether or not self.outf is a tty, and a) this might not be resolved and b) is fiddly to pass through. The .use_colour class flag allows samba-tool subcommands to avoid having --color, and is *also* useful in the short term for visualise and drs commands to avoid having this --color clobber their own bespoke versions (temporarily, during the transition). Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2022-09-16samba-tool: simplify and clarify SuperCommand._run() a littleDouglas Bagnall1-2/+5
Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2022-09-08samba-tool: _resolve() can set outf, errfDouglas Bagnall1-6/+12
We catch output in outf and errf for testing, which we currently do with cmd.outf = self.stringIO() cmd.errf = self.stringIO() on the final resolved commands. But this does not catch the output of the super-commands, of which we normally expect none. Using supercmd._resolve(*args, outf=self.stringIO(), errf=self.stringIO()) will redirect output all the way up the chain. Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2022-09-08samba-tool: more conventional usage of parser.parse_argsDouglas Bagnall1-3/+2
By default parse_args will use sys.argv[1:], which is to say the command-line without the command name. We have always fed it the equivalent of sys.argv, then trimmed the command off the result. That was a bit silly. Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2022-09-08samba-tool: separate ._run() from command resolutionDouglas Bagnall1-32/+55
Prior to this commit, in super-commands, the first half of the _run() is resolving what sub-command to run, and the second half is working out what to print if that failed. Some issues with that are: * it looks a little bit complicated. * the tests can't use the tool's resolution code, because it runs immediately, while the tests first want to fiddle with self.outf and so on. * it makes it harder to subclass and override the resolution code, so instead we do strange things like where we subclass dict as in main.py. So we split it into ._resolve() and ._run(). There are a few tests that break. We mark these as flapping, rather than knownfail, so as to avoid going into extremely fine-grain filters for tests that will be fixed within a few commits. Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2022-09-08samba-tool: do not crash on unimplemented .run()Douglas Bagnall1-2/+2
The run() method is always called with arguments, so it crashes before the NotImplementedError() is ever reached. That's OK, but this is better. Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2022-09-06samba-tool: do not force a traceback on CommandErrorDouglas Bagnall1-1/+0
When a CommandError has an 'inner exception', we have been printing drowning out the error message with a long traceback of the exception we tried to catch. People who really want to see tracebacks can use -d3. Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2022-09-06samba-tool: avoid traceback for NT_STATUS_NETWORK_UNREACHABLEDouglas Bagnall1-0/+3
Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2022-09-06samba-tool: avoid traceback for options errorsDouglas Bagnall1-2/+7
What option? None yet, but see the next two commits. We use a local reference to optparse.OptionValueError, to save typing and make the eventual switch to argparse easier. Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2022-09-06samba-tool: reduce repetitious jargon on credentials failureDouglas Bagnall1-2/+6
We already print the following due to DBG_ERR()s: cli_credentials_failed_kerberos_login: krb5_cc_get_principal failed: No such file or directory Failed to bind - LDAP error 49 LDAP_INVALID_CREDENTIALS - <8009030C: LdapErr: DSID-0C0904DC, comment: AcceptSecurityContext error, data 52e, v1db1> <> Failed to connect to 'ldap://10.53.57.30' with backend 'ldap': LDAP error 49 LDAP_INVALID_CREDENTIALS - <8009030C: LdapErr: DSID-0C0904DC, comment: AcceptSecurityContext error, data 52e, v1db1> <> We don't *really* need to follow that with: ERROR(ldb): LDAP connection to ldap://10.53.57.30 failed - LDAP error 49 LDAP_INVALID_CREDENTIALS - <8009030C: LdapErr: DSID-0C0904DC, comment: AcceptSecurityContext error, data 52e, v1db1> <> rather we can say: Bad username or password. Also, we don't really need to print a traceback, which we seem to do for some commands and not others. Maybe *sometimes* "bad username or password" might be technically incorrect (e.g. --simple-bind-dn), but in those cases the user is already behaving strangely, and they will still see the LDAP_INVALID_CREDENTIALS twice. Kerberos failures don't come this way. BUG: https://bugzilla.samba.org/show_bug.cgi?id=9608 Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org>
2022-09-06samba-tool: respect NO_COLOR env variable and --color optionsDouglas Bagnall1-16/+5
This allows the NO_COLOR environment variable and --color=never to work for samba-tool commands that use this method. So far that means some parts of drs showrepl. Signed-off-by: Douglas Bagnall <douglas.bagnall@catalyst.net.nz> Reviewed-by: Andrew Bartlett <abartlet@samba.org> Reviewed-by: Joseph Sutton <josephsutton@catalyst.net.nz>
2022-05-10python: Remove unnecessary 'pass' statementsJoseph Sutton1-1/+0
Signed-off-by: Joseph Sutton <josephsutton@catalyst.net.nz> Reviewed-by: Andreas Schneider <asn@samba.org>