2020-07-16 16:15:07 +03:00
/*
* Copyright ( c ) 2020 Andreas Schneider < asn @ samba . org >
*
* This program is free software : you can redistribute it and / or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation , either version 3 of the License , or
* ( at your option ) any later version .
*
* This program is distributed in the hope that it will be useful ,
* but WITHOUT ANY WARRANTY ; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE . See the
* GNU General Public License for more details .
*
* You should have received a copy of the GNU General Public License
* along with this program . If not , see < http : //www.gnu.org/licenses/>.
*/
# ifndef _CMDLINE_H
# define _CMDLINE_H
2020-07-27 17:13:53 +03:00
# include "auth/credentials/credentials.h"
2020-08-11 17:37:16 +03:00
# include <popt.h>
# ifndef POPT_TABLEEND
# define POPT_TABLEEND { \
. longName = NULL , \
. shortName = 0 , \
. argInfo = 0 , \
. arg = NULL , \
. val = 0 , \
. descrip = NULL , \
. argDescrip = NULL }
# endif
enum samba_cmdline_config_type {
2021-01-13 11:56:41 +03:00
SAMBA_CMDLINE_CONFIG_NONE = 0 ,
SAMBA_CMDLINE_CONFIG_CLIENT ,
2020-08-11 17:37:16 +03:00
SAMBA_CMDLINE_CONFIG_SERVER ,
} ;
enum smb_cmdline_popt_options {
2021-01-05 16:23:27 +03:00
SAMBA_CMDLINE_POPT_OPT_DEBUG_ONLY = 1 ,
2021-01-13 11:57:19 +03:00
SAMBA_CMDLINE_POPT_OPT_OPTION_ONLY ,
2021-01-13 12:56:41 +03:00
SAMBA_CMDLINE_POPT_OPT_CONFIG_ONLY ,
2021-01-05 16:23:27 +03:00
SAMBA_CMDLINE_POPT_OPT_SAMBA ,
2020-08-11 17:37:16 +03:00
SAMBA_CMDLINE_POPT_OPT_CONNECTION ,
SAMBA_CMDLINE_POPT_OPT_CREDENTIALS ,
SAMBA_CMDLINE_POPT_OPT_VERSION ,
2021-09-03 08:22:18 +03:00
SAMBA_CMDLINE_POPT_OPT_DAEMON ,
2020-08-11 17:37:16 +03:00
SAMBA_CMDLINE_POPT_OPT_SAMBA_LDB ,
2020-08-31 18:42:57 +03:00
SAMBA_CMDLINE_POPT_OPT_LEGACY_S3 ,
SAMBA_CMDLINE_POPT_OPT_LEGACY_S4 ,
2020-08-11 17:37:16 +03:00
} ;
2020-07-27 17:13:53 +03:00
2021-09-03 08:22:18 +03:00
struct samba_cmdline_daemon_cfg {
bool daemon ;
bool interactive ;
bool fork ;
bool no_process_group ;
} ;
2020-07-16 16:15:07 +03:00
/**
* @ brief Initialize the commandline interface for parsing options .
*
2021-05-11 10:13:51 +03:00
* This initializes the interface for parsing options given on the command
2020-07-16 16:15:07 +03:00
* line . It sets up the loadparm and client credentials contexts .
2020-08-11 17:37:16 +03:00
* The function will also setup fault handler , set logging to STDERR by
* default , setup talloc logging and the panic handler .
2020-07-16 16:15:07 +03:00
*
2021-07-21 17:06:15 +03:00
* The function also setups a callback for loading the smb . conf file , the
* config file will be parsed after the commandline options have been parsed
* by popt . This is done by one of the following options parser :
*
* POPT_COMMON_DEBUG_ONLY
* POPT_COMMON_OPTION_ONLY
* POPT_COMMON_CONFIG_ONLY
* POPT_COMMON_SAMBA
*
2020-07-16 16:15:07 +03:00
* @ param [ in ] mem_ctx The talloc memory context to use for allocating memory .
* This should be a long living context till the client
* exits .
*
2021-05-11 10:13:51 +03:00
* @ param [ in ] require_smbconf Wether the smb . conf file is required to be
* present or not ?
2020-07-16 16:15:07 +03:00
*
* @ return true on success , false if an error occured .
*/
2020-08-11 17:37:16 +03:00
bool samba_cmdline_init ( TALLOC_CTX * mem_ctx ,
enum samba_cmdline_config_type config_type ,
bool require_smbconf ) ;
2020-07-16 16:15:07 +03:00
/**
* @ brief Get a pointer of loadparm context used for the command line interface .
*
* @ return The loadparm context .
*/
struct loadparm_context * samba_cmdline_get_lp_ctx ( void ) ;
2020-07-27 17:13:53 +03:00
/**
* @ brief Get the client credentials of the command line interface .
*
* @ return A pointer to the client credentials .
*/
struct cli_credentials * samba_cmdline_get_creds ( void ) ;
2020-08-11 17:37:16 +03:00
/**
* @ brief Get a pointer to the poptOption for the given option section .
*
2021-05-11 10:13:51 +03:00
* You should not directly use this function , but the macros .
*
2020-08-11 17:37:16 +03:00
* @ param [ in ] opt The options to retrieve .
*
* @ return A pointer to the poptOption array .
2021-05-11 10:13:51 +03:00
*
* @ see POPT_COMMON_DEBUG_ONLY
* @ see POPT_COMMON_OPTION_ONLY
* @ see POPT_COMMON_CONFIG_ONLY
* @ see POPT_COMMON_SAMBA
* @ see POPT_COMMON_CONNECTION
* @ see POPT_COMMON_CREDENTIALS
* @ see POPT_COMMON_VERSION
2020-08-11 17:37:16 +03:00
*/
struct poptOption * samba_cmdline_get_popt ( enum smb_cmdline_popt_options opt ) ;
2021-09-03 08:22:18 +03:00
/**
* @ brief Get a pointer to the poptOptions for daemons
*
* @ return A pointer to the daemon options
*
* @ see POPT_COMMON_DAEMON
*/
struct samba_cmdline_daemon_cfg * samba_cmdline_get_daemon_cfg ( void ) ;
2021-11-17 14:25:58 +03:00
void samba_cmdline_set_machine_account_fn (
NTSTATUS ( * fn ) ( struct cli_credentials * cred ,
struct loadparm_context * lp_ctx ) ) ;
2020-08-19 10:07:47 +03:00
/**
* @ brief Burn secrets on the command line .
*
* This function removes secrets from the command line so we don ' t leak e . g .
* passwords on ' ps aux ' output .
*
* It should be called after processing the options and you should pass down
* argv from main ( ) .
*
* @ param [ in ] argc The number of arguments .
*
2021-05-11 10:13:51 +03:00
* @ param [ in ] argv [ ] The argument array we should remove secrets from .
2020-08-19 10:07:47 +03:00
*/
void samba_cmdline_burn ( int argc , char * argv [ ] ) ;
2020-09-02 18:19:00 +03:00
/**
2021-05-11 10:13:51 +03:00
* @ brief Sanity check the command line options .
2020-09-02 18:19:00 +03:00
*
* This checks for duplicates in short and long options .
*
* @ param [ in ] opts The options array to check .
*
* @ return true if valid , false otherwise .
*/
bool samba_cmdline_sanity_check ( const struct poptOption * opts ) ;
/**
* @ brief This is a wrapper for the poptGetContext ( ) which initializes the popt
* context .
*
* If Samba is build in developer mode , this will call
* samba_cmdline_sanity_check ( ) before poptGetContext ( ) .
*
2021-05-11 10:13:51 +03:00
* @ param [ in ] name The context name ( usually argv [ 0 ] program name or
* getprogname ( ) )
*
* @ param [ in ] argc Number of arguments
*
* @ param [ in ] argv The argument array
2020-09-02 18:19:00 +03:00
*
2021-05-11 10:13:51 +03:00
* @ param [ in ] options The address of popt option table
2020-09-02 18:19:00 +03:00
*
2021-05-11 10:13:51 +03:00
* @ param [ in ] flags The OR ' d POPT_CONTEXT_ * bits
2020-09-02 18:19:00 +03:00
*
* @ return The initialized popt context or NULL on error .
*/
poptContext samba_popt_get_context ( const char * name ,
int argc , const char * * argv ,
const struct poptOption * options ,
unsigned int flags ) ;
2021-01-05 16:23:27 +03:00
/**
* @ brief A popt structure for common debug options only .
*/
# define POPT_COMMON_DEBUG_ONLY { \
. longName = NULL , \
. shortName = ' \0 ' , \
. argInfo = POPT_ARG_INCLUDE_TABLE , \
. arg = samba_cmdline_get_popt ( SAMBA_CMDLINE_POPT_OPT_DEBUG_ONLY ) , \
. val = 0 , \
. descrip = " Common debug options: " , \
. argDescrip = NULL } ,
2021-01-13 11:57:19 +03:00
/**
* @ brief A popt structure for - - option only .
*/
# define POPT_COMMON_OPTION_ONLY { \
. longName = NULL , \
. shortName = ' \0 ' , \
. argInfo = POPT_ARG_INCLUDE_TABLE , \
. arg = samba_cmdline_get_popt ( SAMBA_CMDLINE_POPT_OPT_OPTION_ONLY ) , \
. val = 0 , \
. descrip = " Options: " , \
. argDescrip = NULL } ,
2021-01-13 12:56:41 +03:00
/**
* @ brief A popt structure for - - configfile only .
*/
# define POPT_COMMON_CONFIG_ONLY { \
. longName = NULL , \
. shortName = ' \0 ' , \
. argInfo = POPT_ARG_INCLUDE_TABLE , \
. arg = samba_cmdline_get_popt ( SAMBA_CMDLINE_POPT_OPT_CONFIG_ONLY ) , \
. val = 0 , \
. descrip = " Config file: " , \
. argDescrip = NULL } ,
2020-08-11 17:37:16 +03:00
/**
* @ brief A popt structure for common samba options .
*/
# define POPT_COMMON_SAMBA { \
. longName = NULL , \
. shortName = ' \0 ' , \
. argInfo = POPT_ARG_INCLUDE_TABLE , \
. arg = samba_cmdline_get_popt ( SAMBA_CMDLINE_POPT_OPT_SAMBA ) , \
. val = 0 , \
. descrip = " Common Samba options: " , \
. argDescrip = NULL } ,
/**
* @ brief A popt structure for connection options .
*/
# define POPT_COMMON_CONNECTION { \
. longName = NULL , \
. shortName = ' \0 ' , \
. argInfo = POPT_ARG_INCLUDE_TABLE , \
. arg = samba_cmdline_get_popt ( SAMBA_CMDLINE_POPT_OPT_CONNECTION ) , \
. val = 0 , \
. descrip = " Connection options: " , \
. argDescrip = NULL } ,
/**
* @ brief A popt structure for credential options .
*/
# define POPT_COMMON_CREDENTIALS { \
. longName = NULL , \
. shortName = ' \0 ' , \
. argInfo = POPT_ARG_INCLUDE_TABLE , \
. arg = samba_cmdline_get_popt ( SAMBA_CMDLINE_POPT_OPT_CREDENTIALS ) , \
. val = 0 , \
. descrip = " Credential options: " , \
. argDescrip = NULL } ,
/**
* @ brief A popt structure for version options .
*/
# define POPT_COMMON_VERSION { \
. longName = NULL , \
. shortName = ' \0 ' , \
. argInfo = POPT_ARG_INCLUDE_TABLE , \
. arg = samba_cmdline_get_popt ( SAMBA_CMDLINE_POPT_OPT_VERSION ) , \
. val = 0 , \
. descrip = " Version options: " , \
. argDescrip = NULL } ,
2021-09-03 08:22:18 +03:00
/**
* @ brief A popt structure for daemon options .
*/
# define POPT_COMMON_DAEMON { \
. longName = NULL , \
. shortName = ' \0 ' , \
. argInfo = POPT_ARG_INCLUDE_TABLE , \
. arg = samba_cmdline_get_popt ( SAMBA_CMDLINE_POPT_OPT_DAEMON ) , \
. val = 0 , \
. descrip = " Daemon options: " , \
. argDescrip = NULL } ,
2020-08-11 17:37:16 +03:00
/**
* @ brief A popt structure for common samba options .
*/
# define POPT_COMMON_SAMBA_LDB { \
. longName = NULL , \
. shortName = ' \0 ' , \
. argInfo = POPT_ARG_INCLUDE_TABLE , \
. arg = samba_cmdline_get_popt ( SAMBA_CMDLINE_POPT_OPT_SAMBA_LDB ) , \
. val = 0 , \
. descrip = " Common Samba options: " , \
. argDescrip = NULL } ,
2020-08-31 18:42:57 +03:00
/* TODO Get rid of me! */
# define POPT_LEGACY_S3 { \
. longName = NULL , \
. shortName = ' \0 ' , \
. argInfo = POPT_ARG_INCLUDE_TABLE , \
. arg = samba_cmdline_get_popt ( SAMBA_CMDLINE_POPT_OPT_LEGACY_S3 ) , \
. val = 0 , \
. descrip = " Deprecated legcacy options: " , \
. argDescrip = NULL } ,
/* TODO Get rid of me! */
# define POPT_LEGACY_S4 { \
. longName = NULL , \
. shortName = ' \0 ' , \
. argInfo = POPT_ARG_INCLUDE_TABLE , \
. arg = samba_cmdline_get_popt ( SAMBA_CMDLINE_POPT_OPT_LEGACY_S4 ) , \
. val = 0 , \
. descrip = " Deprecated legcacy options: " , \
. argDescrip = NULL } ,
2020-07-16 16:15:07 +03:00
# endif /* _CMDLINE_H */