From 5780fdaca5a2cd49c8218aec48cb6647374089ec Mon Sep 17 00:00:00 2001 From: kx Date: Wed, 28 Sep 2022 17:07:19 +0300 Subject: users list command implementation --- src/users.c | 196 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 196 insertions(+) create mode 100644 src/users.c (limited to 'src/users.c') diff --git a/src/users.c b/src/users.c new file mode 100644 index 0000000..292f9ad --- /dev/null +++ b/src/users.c @@ -0,0 +1,196 @@ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include +#include + +#define _GNU_SOURCE +#include + +#include +#include +#include +#include + +#include +#include + +/* **************************************************************** */ +/* */ +/* SILA Users Commands */ +/* */ +/* **************************************************************** */ + +/* String to pass to system (). This is for the LIST, VIEW and RENAME + commands. */ +static char syscom[PATH_MAX]; + +static int argc; +static char args[PATH_MAX]; +static const char *argv[PATH_MAX]; + +void split_args( const char *name, char *arg ) +{ + char *p = NULL; + bzero( &args[0], PATH_MAX ); + bzero( (void *)argv, sizeof( argv ) ); + argv[0] = name; + argc = 1; + + if( !arg ) + { + return; + } + + strncpy( args, arg, PATH_MAX - 1 ); + p = &args[0]; + while( *p ) + { + while( *p && (*p == '\t' || *p == ' ') ) + ++p; + + if( *p ) + { + argv[argc] = p; + while( *p && *p != '\t' && *p != ' ' ) + ++p; + *p = '\0'; + ++p; + + ++argc; + } + } +} + +int com_useradd( char *arg ) +{ + /* temporary STUB. Should use busctl or REST */ + if( !valid_argument( "useradd", arg ) ) + return( 1 ); + + sprintf (syscom, "useradd %s", arg); + return( system( syscom ) ); +} + +int com_userdel( char *arg ) +{ + /* temporary STUB. Should use busctl or REST */ + if( !valid_argument( "userdel", arg ) ) + return( 1 ); + + sprintf( syscom, "userdel %s", arg ); + return( system( syscom ) ); +} + + + +/*************************************************************** + Users list functions: + ***************************************************************/ + +/* Options: */ +static int userlist_opt_help = 0; + +static void userlist_usage( char *fname ) +{ + fprintf( stdout, "\n" ); + fprintf( stdout, "Usage: %s [options]\n", fname ); + fprintf( stdout, "\n" ); + fprintf( stdout, "List users.\n" ); + fprintf( stdout, "\n" ); + fprintf( stdout, "Options:\n" ); + fprintf( stdout, " -h,--help Display this information.\n" ); + fprintf( stdout, "\n" ); +} + + +void userlist_get_args( int argc, char * const *argv ) +{ + const char* short_options = "h"; + + const struct option long_options[] = + { + { "help", no_argument, NULL, 'h' }, + { NULL, 0, NULL, 0 } + }; + + int ret; + int option_index = 0; + + /* Reset options: */ + userlist_opt_help = 0; + + while( (ret = getopt_long( argc, argv, short_options, long_options, &option_index )) != -1 ) + { + switch( ret ) + { + case 'h': + { + userlist_opt_help = 1; + break; + } + case '?': default: + { + userlist_opt_help = 1; + break; + } + } + } + + if( optind < argc ) + { + userlist_opt_help = 1; + } + + /* This is important for call getopt_long() several times! */ + optind = 0; +} + + + +int com_userlist( char *arg ) +{ + split_args( "list", arg ); + +/* test: + printf( "argc: %d\n", argc ); + if( argc > 0 ) + { + int i = 0; + while( i < argc ) + { + printf( "arg[%d] = '%s';\n", i, argv[i] ); + ++i; + } + } + */ + + userlist_get_args( argc, (char * const *)&argv[0] ); + + if( userlist_opt_help ) + { + userlist_usage( "list" ); + } + else + { + /* + list command: + */ + sprintf( syscom, "busctl --list tree %s | grep %s/ | sed 's,%s/, ,'", + USERS_DBUS_SERVICE, + USERS_DBUS_ROOT_PATH, + USERS_DBUS_ROOT_PATH ); + return( system( syscom ) ); + } + + return( 0 ); +} -- cgit v1.2.3