cmd: version command should not call before or after procs Since the original IBM code import, the cmd_Dispatch processor has avoided calling beforeProc and afterProc for the built-in 'help' and 'apropos' commands. This is important, because these procs may rely on application-defined common arguments (e.g., -cell <cell> or -noauth) for proper operation. However, application common arguments are not defined for built-in commands 'help' and 'apropos'. Unfortunately, when a new built-in 'version' command was added (350c140d89198cb7 libcmd-support-version-switch-20060630), it was not exempted from the before and after procs. In order to avoid a segmentation fault in 'libadmin/test/afscp version', modify cmd to also avoid calling beforeProc and afterProc for the 'version' command. Change-Id: Ic899397de808fd6c4a5f66a0f5add102340064e6 Reviewed-on: https://gerrit.openafs.org/14645 Reviewed-by: Benjamin Kaduk <kaduk@mit.edu> Tested-by: BuildBot <buildbot@rampaginggeek.com>
clang-10: use AFS_FALLTHROUGH for case fallthrough Clang-10 will not recognize '/* fallthrough */' as an indicator to turn off the fallthrough diagnostic due to the lack of a 'break' in a case statement. Clang-10 requires the '__attribute__((fallthrough))' statement to disable the diagnostic. In addition clang-10 is finding additional locations where fall throughs occur. Determine if the compiler supports '__attribute__((fallthrough))' to disable the implicit fallthrough diagnostic. Define a new macro 'AFS_FALLTHROUGH' that will disable the fallthrough diagnostic. Set it as a wrapper for the Linux kernel's 'fallthrough' macro if available, otherwise set it as a wrapper macro for '__attribute__((fallthrough))' if the compiler supports it. Update CODING to document the use of AFS_FALLTHROUGH when needing to fallthrough between case statements. Replace the '/* fallthrough */' comments with AFS_FALLTHROUGH, and add AFS_FALLTHROUGH as needed. Replace some fallthroughs with a break (or goto) if the flow was was just to a break (or goto). e.g. case x: case x: somestmt; somestmt; break; case y: case y: break; break; Correct a mis-indented brace '}' in src/WINNT/afsd/smb3.c Note, the clang maintainers have rejected the use of comments as a flag to turn off the fall through warnings. Change-Id: Ia5da10fc14fc1874baca035a3cf471e618e0d5f5 Reviewed-on: https://gerrit.openafs.org/14274 Tested-by: BuildBot <buildbot@rampaginggeek.com> Reviewed-by: Andrew Deason <adeason@sinenomine.net> Reviewed-by: Benjamin Kaduk <kaduk@mit.edu>
Add more 'fall through' switch comments Commit a455452d (LINUX 5.3: Add comments for fallthrough switch cases) added the special /* fall through */ comment to various switch/case blocks, in order to avoid implicit-fallthrough warnings from causing the build to fail when building the Linux kernel module. In this commit, add additional /* fall through */ comments to the rest of the tree where falling through is intentional. Add a "break;" in one place in dumptool.c where falling through seems like a mistake, and flag certain functions as AFS_NORETURN to avoid needing to explicitly break or fallthrough. Check for the availability of the -Wimplicit-fallthrough compiler flag and use it when --enable-checking is set, to prevent additional cases from creeping into the tree. Note: the -Wimplicit-fallthrough compiler flag was added in gcc 7. Change-Id: Iae34e7969606603da8358d7cfa5fd04279b218dc Reviewed-on: https://gerrit.openafs.org/14125 Reviewed-by: Andrew Deason <adeason@sinenomine.net> Reviewed-by: Benjamin Kaduk <kaduk@mit.edu> Tested-by: BuildBot <buildbot@rampaginggeek.com>
cmd: bail if out of memory while printing syntax Bail with an error message to stderr if we are unable to format the command syntax due to a string allocation error. Found via scan-build. [mmeffie: updated commit] Change-Id: Ib3bc7f53c295d8dde6c07b9c4990cd1b3bcee58c Reviewed-on: https://gerrit.openafs.org/13335 Tested-by: BuildBot <buildbot@rampaginggeek.com> Reviewed-by: Benjamin Kaduk <kaduk@mit.edu>
cmd: improve help for programs without subcommands Some programs do not have subcommands (other than the standard "help", and "version" subcommands). The cmd library provides the "noopcode" mechanism for new subcommand-less programs, but older programs take advantage of the optional "initcmd" token to simulate subcommand-less programs. The "initcmd" token is optional to run the command, however it is required to display the command help. For example, running the xstat_cm_test program without any options gives a syntax error: $ xstat_cm_test xstat_cm_test: Missing required parameter '-cmname' ... Retrying with -help (or help, -h, --help), gives the rather unhelpful output: $ xstat_cm_test -help xstat_cm_test: Commands are: apropos search by help text help get help on commands initcmd initialize the program It is not obvious to the user how to get the command usage for the program, nor that the initcmd subcommand to "initialize the program" is actually is a placeholder to run the program. Instead, display the command usage when help is requested and initcmd is the only defined subcommand for a program. For example: $ xstat_cm_test -help Usage: src/xstat/xstat_cm_test [initcmd] -cmname <Cache Manager name(s) to monitor>+ -collID <Collection(s) to fetch>+ [-onceonly] [-frequency <poll frequency, in seconds>] [-period <data collection time, in minutes>] [-debug] [-help] Where: -onceonly Collect results exactly once, then quit -debug turn on debugging output The libcmd library now supports an "noopcode", which should used for future subcommand-less programs, but converting old programs to remove the initcmd opcode could break scripts which actually specify the optional initcmd token. This commit adds a new libcmd flag called CMD_IMPLICIT which is used to denote built-in subcommands such as "version" and "help". Change-Id: Iee9cb2761254543f74166e5c240685f85b6915b6 Reviewed-on: https://gerrit.openafs.org/10983 Reviewed-by: Michael Meffie <mmeffie@sinenomine.net> Tested-by: BuildBot <buildbot@rampaginggeek.com> Reviewed-by: Benjamin Kaduk <kaduk@mit.edu>
Fix unchecked calls to asprintf The return value of asprintf() is the number of bytes printed, or -1 if there was an error allocating a large enough buffer. In the latter case, the value of the result string is undefined, and so it cannot be counted on to be NULL. This change fixes numerous places where the result of asprintf is checked incorrectly (by examining the output pointer and not the return value) or not at all. Change-Id: I9fef14d60c096795d59c42798f3906041fb18c86 Reviewed-on: http://gerrit.openafs.org/9978 Reviewed-by: Benjamin Kaduk <kaduk@mit.edu> Reviewed-by: D Brashear <shadow@your-file-system.com> Tested-by: BuildBot <buildbot@rampaginggeek.com>
cmd: avoid piggy-backing flags in the help string Remove the hack to piggy-back the hidden command option in the help string argument. Change-Id: Iedcb6b96e98b766e3ef2c87cd6e5d41874f2c0b7 Reviewed-on: http://gerrit.openafs.org/10982 Reviewed-by: D Brashear <shadow@your-file-system.com> Reviewed-by: Chas Williams - CONTRACTOR <chas@cmf.nrl.navy.mil> Tested-by: BuildBot <buildbot@rampaginggeek.com>
cmd: add flags argument to create syntax function Add the flags argument to cmd_CreateSyntax() and update all callers. The flags argument will be used to set command options, such as CMD_HIDDEN. Change-Id: Ia51be9635f262516cb084d236a9e0756f608bf16 Reviewed-on: http://gerrit.openafs.org/11430 Tested-by: BuildBot <buildbot@rampaginggeek.com> Reviewed-by: Benjamin Kaduk <kaduk@mit.edu>
remove cmd-suite-option-for-hiding-admin-commands Remove the incomplete and non-functional cmd option for hiding admin commands, introduced in commit 36d02757fd6863a845163daf0d730bdcc0a28343. This patch removes the CMD_ADMIN flag, the non-functional help -admin parameter, and the non-functional cmd_IsAdministratorCommand() function. Thanks to Jeffrey Altman for pointing out this old commit and for suggestions on cleanup. Change-Id: I72c7d2ed7109b1238713fe0d6d177c5af6fc6b7d Reviewed-on: http://gerrit.openafs.org/11429 Tested-by: BuildBot <buildbot@rampaginggeek.com> Reviewed-by: Chas Williams - CONTRACTOR <chas@cmf.nrl.navy.mil> Reviewed-by: Garrett Wollman <wollman@csail.mit.edu> Reviewed-by: Jeffrey Altman <jaltman@your-file-system.com> Reviewed-by: D Brashear <shadow@your-file-system.com>
cmd: List version in help for commands List version in help for commands using cmd library Change-Id: I4da64be11244e64d961e8de47cecb24cbbadce32 Reviewed-on: http://gerrit.openafs.org/10956 Tested-by: BuildBot <buildbot@rampaginggeek.com> Reviewed-by: D Brashear <shadow@your-file-system.com>
Satisfy clang's aggressive strlcpy warnings Passing something related to the length of the source as the length argument to strlcpy triggers a warning, which is converted to an error with --enable-checking (on FreeBSD 10.0). The current code is safe, since it is using the same expression that was used to allocate the destination buffer, but switch to using a separate variable to hold the length and use that variable for both allocation and copying, to appease the compiler. Change-Id: I580083d142fd19a4e7828c915b4846868fa8f917 Reviewed-on: http://gerrit.openafs.org/10818 Tested-by: BuildBot <buildbot@rampaginggeek.com> Reviewed-by: Perry Ruiter <pruiter@sinenomine.net> Reviewed-by: Jeffrey Altman <jaltman@your-file-system.com>
cmd: exit status zero from -help Return of an exit status of zero when running commands with -help, instead of returning an error. By general convention, and in previous versions, tools do not not treat -help as an error. The AFS::Command perl modules, use -help as an introspection technique, and fail when commands run with the -help option returns non-zero. Change-Id: I6ecd95f6ccd07218a2657dbb4dbf1c13599159f4 Reviewed-on: http://gerrit.openafs.org/8087 Reviewed-by: Derrick Brashear <shadow@your-file-system.com> Reviewed-by: Simon Wilkinson <simonxwilkinson@gmail.com> Tested-by: BuildBot <buildbot@rampaginggeek.com>
cmd: Avoid premature OptionAsString free Only free the previously-set string value if we're actually going to change it. This avoids the scenario where a caller does something like: char *str = strdup("defaultval"); /* ... */ cmd_OptionAsString(as, OPT_foo, &str); resulting in 'str' pointing to freed memory when the relevant option is not specified, or there is some other error. Change-Id: If1f94d5eb39cc4dba9a4195ff20850ff81737eb9 Reviewed-on: http://gerrit.openafs.org/7475 Reviewed-by: Jeffrey Altman <jaltman@secure-endpoints.com> Reviewed-by: Derrick Brashear <shadow@dementix.org> Tested-by: BuildBot <buildbot@rampaginggeek.com>
Use strdup to copy strings Where we have newStr = malloc(strlen(oldStr)+1); strcpy(newStr, oldStr); replace these with newStr = strdup(oldStr); It's shorter, clearer, and gets rid of a load of occurences of strcpy, which some compilers are now warning is unsafe (although it isn't in this context) Get rid of a number of custom duplicate string functions and replace them with strdup where the behaviour is identical Change-Id: If800343a7d13b1ba6362d4570a2a324fa3525250 Reviewed-on: http://gerrit.openafs.org/7450 Tested-by: BuildBot <buildbot@rampaginggeek.com> Reviewed-by: Derrick Brashear <shadow@dementix.org>
cmd: Add support for pulling options from files With this change, we gain the ability to set our command line options from krb5.conf configuration files. This is only available for tools which are implemented using the new cmd_OptionAs accessor methods. Callers should load their configuration file using cmd_OpenConfigFile("/path/to/config/file.conf"); (an addition to libauth to return a path to a system wide configuration file will be forthcoming) and then set their command name (for example, "fileserver", "afsd", "vos" and so on) using cmd_SetCommandName("mycommand"); The accessor functions will then populate their return values with either: a) The command line options, if specified b) The contents of the tag matching the option name, in the [command_subcommand] section of the configuration file, if it exists c) The contents of the same tag, in the [command] section of the configuration file, if it that exists. d) The contents of the same tag in the [defaults] section of the configuration file. Callers can also gain access to the entire configuration file by calling cmd_RawFile, or to just the section corresponding to their command line by calling cmd_RawSection. Note that when using the file directly, it is up to callers to preserve consistency by implementing similar inheritance rules as the above. Change-Id: Ic501ab296af3638f961486869af79c9ce47b77b8 Reviewed-on: http://gerrit.openafs.org/7135 Reviewed-by: Derrick Brashear <shadow@dementix.org> Tested-by: BuildBot <buildbot@rampaginggeek.com>
cmd: Don't leak memory when constructing help string It's not critical, because we'll be exiting soon afterwards, but just to be nice, don't leak the name of every command option whilst we're constructing the help string. Caught by clang-analyzer Change-Id: I87fdb2030c6a7c5ad810f2a06820c08cbe2fb18e Reviewed-on: http://gerrit.openafs.org/7107 Tested-by: BuildBot <buildbot@rampaginggeek.com> Reviewed-by: Derrick Brashear <shadow@dementix.org>
cmd: Use strl* rather than strn* to avoid overrun The NName function was using strncat(a, b, sizeof(a)), which doesn't work as you would expect if 'a' already contains data. To avoid the potential buffer overflow, switch to just using strlcat. Caught by clang-analyzer Change-Id: Idd2c630c07a93b27e8d629339589aa6686290eae Reviewed-on: http://gerrit.openafs.org/7092 Tested-by: BuildBot <buildbot@rampaginggeek.com> Reviewed-by: Derrick Brashear <shadow@dementix.org>
libcmd: Don't increment array then discard result We don't need to increment argv here, as we're about to return to the caller. Change-Id: I442fe741e4bda91e12375a6fe72657dc215c4e78 Reviewed-on: http://gerrit.openafs.org/5078 Reviewed-by: Derrick Brashear <shadow@dementia.org> Reviewed-by: Jeffrey Altman <jaltman@openafs.org> Tested-by: Jeffrey Altman <jaltman@openafs.org>
libcmd: Don't store values that we don't need Don't store the results from cmd_CreateSyntax unless we actually need to use them for something. Caught by clang-analyzer Change-Id: I581dcc00a886245a8d117f78c74590111c75e3b0 Reviewed-on: http://gerrit.openafs.org/5077 Reviewed-by: Derrick Brashear <shadow@dementia.org> Reviewed-by: Jeffrey Altman <jaltman@openafs.org> Tested-by: Jeffrey Altman <jaltman@openafs.org>
cmd: Add support for disabling specific abbrevs Sometimes, when adding a new command parameter, it's necessary to prevent it from colliding with an existing abbreviation. This patch adds a new command flag CMD_NOABBRV which can be set on a parameter to indicate that it should not be considered when checking for ambiguous abbreviations. For example, if a command has the existing '-cell' option which is popularly abbreviated to '-c', adding a '-config' option would cause the existing abbreviation to stop working. However, if '-config' is added with the NOABBRV flag set, '-c' will continue to work. Change-Id: I3b6d718f9dd81c44fb1d10c904db6a4a0fd763b8 Reviewed-on: http://gerrit.openafs.org/4810 Tested-by: BuildBot <buildbot@rampaginggeek.com> Reviewed-by: Jeffrey Altman <jaltman@openafs.org> Reviewed-by: Derrick Brashear <shadow@dementia.org>