From ed02760159a4a777ed0e1b617dafddbc9c62f5a1 Mon Sep 17 00:00:00 2001 From: kx Date: Thu, 29 Sep 2022 01:46:11 +0300 Subject: users: passwd --- src/users.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 6 deletions(-) (limited to 'src/users.c') diff --git a/src/users.c b/src/users.c index 338834f..b0f767c 100644 --- a/src/users.c +++ b/src/users.c @@ -24,6 +24,10 @@ #include #include + +char user_name[USER_NAME_SIZE]; +char user_role[USER_ROLE_SIZE]; + /* **************************************************************** */ /* */ /* SILA Users Commands */ @@ -39,9 +43,6 @@ 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]; @@ -132,9 +133,12 @@ void get_args( int argc, char * const *argv ) } } + /* + Assume that the last argument is a user name: + */ if( optind < argc ) { - opt_help = 1; + strncpy( opt_name, (const char *)argv[optind++], sizeof( opt_name ) - 1 ); } /* This is important for call getopt_long() several times! */ @@ -146,17 +150,64 @@ void get_args( int argc, char * const *argv ) Users management commands: ***************************************************************/ +static void passwd_usage( char *fname ) +{ + fprintf( stdout, "\n" ); + fprintf( stdout, "Usage: %s [username]\n", fname ); + fprintf( stdout, "\n" ); + fprintf( stdout, "Set or change user password.\n" ); + fprintf( stdout, "\n" ); + fprintf( stdout, "Options:\n" ); + fprintf( stdout, " -h,--help Display this information.\n" ); + fprintf( stdout, "\n" ); + fprintf( stdout, "Parameter:\n" ); + fprintf( stdout, " [username] User name (default: %s).\n", user_name ); + fprintf( stdout, "\n" ); +} + +int com_passwd( char *arg ) +{ + split_args( "passwd", arg ); + + get_args( argc, (char * const *)&argv[0] ); + + if( opt_help ) + { + passwd_usage( "passwd" ); + } + else + { + /* check args */ + if( !opt_name[0] ) + { + sprintf( syscom, "passwd %s", user_name ); + } + else + { + if( !strncmp( user_role, ROLE_ADMIN, sizeof( ROLE_ADMIN ) ) ) + sprintf( syscom, "passwd %s", opt_name ); + else + sprintf( syscom, "echo \"Only users with role='%s' can change other user's password\"", ROLE_ADMIN ); + } + + return( system( syscom ) ); + } + + return( 0 ); +} + + static void useradd_usage( char *fname ) { fprintf( stdout, "\n" ); - fprintf( stdout, "Usage: %s [options]\n", fname ); + fprintf( stdout, "Usage: %s [options] [user name]\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, " -r,--role User Role (default: %s).\n", ROLE_USER ); fprintf( stdout, "\n" ); } -- cgit v1.2.3