#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]; /* Options: */ #define USER_NAME_SIZE 64 #define USER_ROLE_SIZE 16 static int opt_help = 0; static char opt_name[USER_NAME_SIZE]; static char opt_role[USER_ROLE_SIZE]; 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; } } } void get_args( int argc, char * const *argv ) { const char* short_options = "hn:r:"; const struct option long_options[] = { { "help", no_argument, NULL, 'h' }, { "name", required_argument, NULL, 'n' }, { "role", required_argument, NULL, 'r' }, { NULL, 0, NULL, 0 } }; int ret; int option_index = 0; /* Reset options: */ opt_help = 0; bzero( &opt_name[0], USER_NAME_SIZE ); bzero( &opt_role[0], USER_ROLE_SIZE ); while( (ret = getopt_long( argc, argv, short_options, long_options, &option_index )) != -1 ) { switch( ret ) { case 'h': { opt_help = 1; break; } case 'n': { if( optarg != NULL ) strncpy( opt_name, (const char *)optarg, sizeof( opt_name ) - 1 ); else opt_help = 1; break; } case 'r': { if( optarg != NULL ) strncpy( opt_role, (const char *)optarg, sizeof( opt_role ) - 1 ); else opt_help = 1; break; } case '?': default: { opt_help = 1; break; } } } if( optind < argc ) { opt_help = 1; } /* This is important for call getopt_long() several times! */ optind = 0; } /*************************************************************** Users management commands: ***************************************************************/ static void useradd_usage( char *fname ) { fprintf( stdout, "\n" ); fprintf( stdout, "Usage: %s [options]\n", fname ); fprintf( stdout, "\n" ); fprintf( stdout, "Delete user.\n" ); fprintf( stdout, "\n" ); fprintf( stdout, "Options:\n" ); fprintf( stdout, " -h,--help Display this information.\n" ); fprintf( stdout, " -n,--name User Name.\n" ); fprintf( stdout, " -r,--role User Role (default: %s.\n", ROLE_USER ); fprintf( stdout, "\n" ); } int com_useradd( char *arg ) { split_args( "useradd", arg ); get_args( argc, (char * const *)&argv[0] ); if( opt_help ) { useradd_usage( "useradd" ); } else { /* check args */ if( !opt_name[0] || !opt_role[0] ) { useradd_usage( "useradd" ); } else { /* temporary STUB. Should use busctl or REST */ sprintf( syscom, "echo 'useradd --name %s --role %s (command is not implemented yet).'", opt_name, opt_role ); return( system( syscom ) ); } } return( 0 ); } static void userdel_usage( char *fname ) { fprintf( stdout, "\n" ); fprintf( stdout, "Usage: %s [options]\n", fname ); fprintf( stdout, "\n" ); fprintf( stdout, "Delete user.\n" ); fprintf( stdout, "\n" ); fprintf( stdout, "Options:\n" ); fprintf( stdout, " -h,--help Display this information.\n" ); fprintf( stdout, " -n,--name User Name.\n" ); fprintf( stdout, "\n" ); } int com_userdel( char *arg ) { split_args( "userdel", arg ); get_args( argc, (char * const *)&argv[0] ); if( opt_help ) { userdel_usage( "userdel" ); } else { /* check args */ if( !opt_name[0] ) { useradd_usage( "userdel" ); } else { /* temporary STUB. Should use busctl or REST */ sprintf( syscom, "echo 'userdel --name %s (command is not implemented yet).'", opt_name ); return( system( syscom ) ); } } return( 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" ); } 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; } } */ get_args( argc, (char * const *)&argv[0] ); if( 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 ); }