From ed02760159a4a777ed0e1b617dafddbc9c62f5a1 Mon Sep 17 00:00:00 2001 From: kx Date: Thu, 29 Sep 2022 01:46:11 +0300 Subject: users: passwd --- src/main.c | 13 +++++++++++++ src/users.c | 63 +++++++++++++++++++++++++++++++++++++++++++++++++++++++------ src/users.h | 6 ++++++ 3 files changed, 76 insertions(+), 6 deletions(-) diff --git a/src/main.c b/src/main.c index 19474de..e186215 100644 --- a/src/main.c +++ b/src/main.c @@ -50,6 +50,7 @@ COMMAND top_user_list[] = { { "help", com_help, "Display this text" }, { "?", com_help, "Synonym for `help'" }, { "shell", com_shell, "Activate submenu shell" }, + { "users", com_users, "Activate submenu users" }, { "quit", com_quit, "Quit using SILA Shell" }, { (char *)NULL, (rl_icpfunc_t *)NULL, (char *)NULL } }; @@ -121,6 +122,7 @@ COMMAND users_admin_list[] = { { "useradd", com_useradd, "Register new user" }, { "userdel", com_userdel, "Delete user" }, { "list", com_userlist, "List users" }, + { "passwd", com_passwd, "Set or change user's password" }, { "..", com_top, "Return to top menu" }, { "quit", com_quit, "Quit using SILA Shell" }, { (char *)NULL, (rl_icpfunc_t *)NULL, (char *)NULL } @@ -132,6 +134,7 @@ COMMAND users_operator_list[] = { { "useradd", com_useradd, "Register new user" }, { "userdel", com_userdel, "Delete user" }, { "list", com_userlist, "List users" }, + { "passwd", com_passwd, "Set or change user's password" }, { "..", com_top, "Return to top menu" }, { (char *)NULL, (rl_icpfunc_t *)NULL, (char *)NULL } }; @@ -140,6 +143,7 @@ COMMAND users_user_list[] = { { "help", com_help, "Display this text" }, { "?", com_help, "Synonym for `help'" }, { "list", com_userlist, "List users" }, + { "passwd", com_passwd, "Set or change user's password" }, { "..", com_top, "Return to top menu" }, { (char *)NULL, (rl_icpfunc_t *)NULL, (char *)NULL } }; @@ -166,6 +170,9 @@ enum priv user_privileges() if( !pw ) return ret; + strncpy( user_name, pw->pw_name, USER_NAME_SIZE - 1 ); + strncpy( user_role, ROLE_USER, USER_ROLE_SIZE - 1 ); + (void)getgrouplist( pw->pw_name, pw->pw_gid, NULL, &ng ); /* allocate groups[] */ if( ng == 0 ) @@ -210,6 +217,12 @@ enum priv user_privileges() free( groups ); + if( ret == OPERATOR ) + strncpy( user_role, ROLE_OPERATOR, USER_ROLE_SIZE - 1 ); + + if( ret == ADMIN ) + strncpy( user_role, ROLE_ADMIN, USER_ROLE_SIZE - 1 ); + return ret; } 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" ); } diff --git a/src/users.h b/src/users.h index 125f13b..8e4ebe6 100644 --- a/src/users.h +++ b/src/users.h @@ -3,6 +3,9 @@ #define __USERS_H +#define USER_NAME_SIZE 64 +#define USER_ROLE_SIZE 16 + #define ROLE_ADMIN "admin" #define ROLE_OPERATOR "operator" #define ROLE_USER "user" @@ -28,10 +31,13 @@ extern "C" { #endif +extern char user_name[]; +extern char user_role[]; extern int com_useradd( char *arg ); extern int com_userdel( char *arg ); extern int com_userlist( char *arg ); +extern int com_passwd( char *arg ); -- cgit v1.2.3