summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorkx <kx@radix.pro>2022-09-28 17:07:19 +0300
committerkx <kx@radix.pro>2022-09-28 17:07:19 +0300
commit5780fdaca5a2cd49c8218aec48cb6647374089ec (patch)
treeef5c36583f70cf1e24d6d3cf257beb0e832c9d98
parentd05678abe9422b72583ab3a90c0c6c7db392ca83 (diff)
downloadsila-shell-5780fdaca5a2cd49c8218aec48cb6647374089ec.tar.xz
users list command implementation
-rw-r--r--meson.build4
-rw-r--r--src/Makefile4
-rw-r--r--src/commands.c157
-rw-r--r--src/commands.h18
-rw-r--r--src/completion.c2
-rw-r--r--src/main.c4
-rw-r--r--src/main.h14
-rw-r--r--src/shell.c152
-rw-r--r--src/shell.h28
-rw-r--r--src/users.c196
-rw-r--r--src/users.h42
-rw-r--r--src/utils.c6
12 files changed, 455 insertions, 172 deletions
diff --git a/meson.build b/meson.build
index 774a871..03350a9 100644
--- a/meson.build
+++ b/meson.build
@@ -7,7 +7,9 @@ src = [
'src/main.c',
'src/commands.c',
'src/completion.c',
- 'src/utils.c'
+ 'src/utils.c',
+ 'src/shell.c',
+ 'src/users.c'
]
readline = dependency('readline')
diff --git a/src/Makefile b/src/Makefile
index 3cd22f3..a0ad936 100644
--- a/src/Makefile
+++ b/src/Makefile
@@ -8,8 +8,8 @@ LDFLAGS = -L/usr/lib64
LIBS = -lasan -lm -lreadline
-SOURCES = main.c commands.c completion.c utils.c
-OBJECTS = main.o commands.o completion.o utils.o
+SOURCES = main.c commands.c completion.c utils.c shell.c users.c
+OBJECTS = main.o commands.o completion.o utils.o shell.o users.o
.SUFFIXES:
diff --git a/src/commands.c b/src/commands.c
index db26130..b52a5a1 100644
--- a/src/commands.c
+++ b/src/commands.c
@@ -18,6 +18,9 @@
#include <completion.h>
#include <utils.h>
+#include <shell.h>
+#include <users.h>
+
/* Function which tells you that you can't do this. */
void too_dangerous( char *caller )
{
@@ -47,82 +50,7 @@ int valid_argument( char *caller, char *arg )
/* String to pass to system (). This is for the LIST, VIEW and RENAME
commands. */
-static char syscom[PATH_MAX];
-
-/* List the file(s) named in arg. */
-int com_ls( char *arg )
-{
- if( !arg )
- arg = "";
-
- sprintf( syscom, "ls -FClg %s", arg );
- return( system( syscom ) );
-}
-
-int com_id( char *arg )
-{
- if( !arg )
- arg = "";
-
- sprintf (syscom, "id %s", arg);
- return( system( syscom ) );
-}
-
-int com_more( char *arg )
-{
- if( !valid_argument( "more", arg ) )
- return( 1 );
-
- sprintf (syscom, "more %s", arg);
- return( system( syscom ) );
-}
-
-int com_vi( char *arg )
-{
- if( !valid_argument( "vi", arg ) )
- return( 1 );
-
- sprintf (syscom, "vi %s", arg);
- return( system( syscom ) );
-}
-
-int com_rename( char *arg )
-{
- too_dangerous( "rename" );
- return( 1 );
-}
-
-int com_stat( char *arg )
-{
- struct stat finfo;
-
- if( !valid_argument( "stat", arg ) )
- return( 1 );
-
- if( stat( arg, &finfo ) == -1 )
- {
- perror( arg );
- return( 1 );
- }
-
- printf( "Statistics for `%s':\n", arg );
-
- printf( "%s has %ld link%s, and is %ld byte%s in length.\n", arg,
- finfo.st_nlink,
- (finfo.st_nlink == 1) ? "" : "s",
- finfo.st_size,
- (finfo.st_size == 1) ? "" : "s");
- printf( "Inode Last Change at: %s", ctime( &finfo.st_ctime ) );
- printf( " Last access at: %s", ctime( &finfo.st_atime ) );
- printf( " Last modified at: %s", ctime( &finfo.st_mtime ) );
- return( 0 );
-}
-
-int com_delete( char *arg )
-{
- too_dangerous( "delete" );
- return( 1 );
-}
+//static char syscom[PATH_MAX];
/* Print out help for ARG, or for all of the commands if ARG is
not present. */
@@ -166,79 +94,6 @@ int com_help( char *arg )
return( 0 );
}
-/* Change to the directory ARG. */
-int com_cd( char *arg )
-{
- if( !strcmp( arg, "~" ) )
- {
- arg = getenv( "HOME" );
- }
-
- if( chdir( arg ) == -1 )
- {
- perror( arg );
- return( 1 );
- }
-
- com_pwd( "" );
- return( 0 );
-}
-
-/* Print out the current working directory. */
-int com_pwd( char *ignore )
-{
- char dir[PATH_MAX], *s;
-
- s = getcwd( dir, PATH_MAX );
- if( s == 0 )
- {
- printf( "Error getting pwd: %s\n", dir );
- return( 1 );
- }
-
- printf( "Current directory is: %s\n", dir );
- return( 0 );
-}
-
-int com_ping( char *arg )
-{
- if( !valid_argument( "ping", arg ) )
- return( 1 );
-
- sprintf (syscom, "ping %s", arg);
- return( system( syscom ) );
-}
-
-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 ) );
-}
-
-int com_userlist( char *arg )
-{
- /* temporary STUB. Should use busctl or REST */
- if( !arg ) arg = "";
-
- sprintf (syscom, "cat /etc/passwd | cut -f1 -d':' %s", arg);
- return( system( syscom ) );
-}
-
-
/* The user wishes to quit using this program. Just set DONE non-zero. */
int com_quit( char *arg )
{
@@ -247,7 +102,9 @@ int com_quit( char *arg )
}
-
+/*
+ Menu:
+ */
int com_shell( char *arg )
{
if( !arg ) arg = "";
diff --git a/src/commands.h b/src/commands.h
index 76058c1..2c16c74 100644
--- a/src/commands.h
+++ b/src/commands.h
@@ -29,23 +29,11 @@ extern COMMAND_LIST users;
extern COMMAND_LIST *current;
+extern void too_dangerous( char *caller );
+extern int valid_argument( char *caller, char *arg );
-extern int com_id( char *arg );
-extern int com_ls( char *arg );
-extern int com_more( char *arg );
-extern int com_vi( char *arg );
-extern int com_rename( char *arg );
-extern int com_stat( char *arg );
-extern int com_delete( char *arg );
-extern int com_help( char *arg );
-extern int com_cd( char *arg );
-extern int com_ping( char *arg );
-extern int com_pwd( char *ignore );
-
-extern int com_useradd( char *arg );
-extern int com_userdel( char *arg );
-extern int com_userlist( char *arg );
+extern int com_help( char *arg );
extern int com_quit( char *arg );
extern int com_shell( char *arg );
diff --git a/src/completion.c b/src/completion.c
index b90f249..ec93a9d 100644
--- a/src/completion.c
+++ b/src/completion.c
@@ -11,6 +11,8 @@
#include <utils.h>
#include <commands.h>
+#include <users.h>
+
/* **************************************************************** */
/* */
/* Interface to Readline Completion */
diff --git a/src/main.c b/src/main.c
index 3e991d1..d00230e 100644
--- a/src/main.c
+++ b/src/main.c
@@ -18,6 +18,8 @@
#include <completion.h>
#include <commands.h>
+#include <shell.h>
+#include <users.h>
char *progname; /* The name of this program, as taken from argv[0]. */
int done; /* When non-zero, this global means the user is done using this program. */
@@ -256,7 +258,7 @@ char *stripwhite( char *string )
if( *s == 0 )
return( s );
- t = s + strlen (s) - 1;
+ t = s + strlen( s ) - 1;
while( t > s && whitespace( *t ) )
t--;
*++t = '\0';
diff --git a/src/main.h b/src/main.h
index f0e017c..331c831 100644
--- a/src/main.h
+++ b/src/main.h
@@ -1,4 +1,18 @@
+#ifndef __MAIN_H
+#define __MAIN_H
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
extern int done; /* When non-zero, this global means the user is done using this program. */
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __MAIN_H */
diff --git a/src/shell.c b/src/shell.c
new file mode 100644
index 0000000..ade4d53
--- /dev/null
+++ b/src/shell.c
@@ -0,0 +1,152 @@
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <sys/errno.h>
+#include <limits.h>
+#include <unistd.h>
+#include <time.h>
+
+#include <readline/readline.h>
+#include <readline/history.h>
+
+#include <main.h>
+#include <commands.h>
+#include <completion.h>
+#include <utils.h>
+
+#include <shell.h>
+#include <users.h>
+
+
+/* **************************************************************** */
+/* */
+/* SILA Shell Commands */
+/* */
+/* **************************************************************** */
+
+/* String to pass to system (). This is for the LIST, VIEW and RENAME
+ commands. */
+static char syscom[PATH_MAX];
+
+/* List the file(s) named in arg. */
+int com_ls( char *arg )
+{
+ if( !arg )
+ arg = "";
+
+ sprintf( syscom, "ls -FClg %s", arg );
+ return( system( syscom ) );
+}
+
+int com_id( char *arg )
+{
+ if( !arg )
+ arg = "";
+
+ sprintf( syscom, "id %s", arg );
+ return( system( syscom ) );
+}
+
+int com_more( char *arg )
+{
+ if( !valid_argument( "more", arg ) )
+ return( 1 );
+
+ sprintf( syscom, "more %s", arg );
+ return( system( syscom ) );
+}
+
+int com_vi( char *arg )
+{
+ if( !valid_argument( "vi", arg ) )
+ return( 1 );
+
+ sprintf( syscom, "vi %s", arg );
+ return( system( syscom ) );
+}
+
+int com_rename( char *arg )
+{
+ too_dangerous( "rename" );
+ return( 1 );
+}
+
+int com_stat( char *arg )
+{
+ struct stat finfo;
+
+ if( !valid_argument( "stat", arg ) )
+ return( 1 );
+
+ if( stat( arg, &finfo ) == -1 )
+ {
+ perror( arg );
+ return( 1 );
+ }
+
+ printf( "Statistics for `%s':\n", arg );
+
+ printf( "%s has %ld link%s, and is %ld byte%s in length.\n", arg,
+ finfo.st_nlink,
+ (finfo.st_nlink == 1) ? "" : "s",
+ finfo.st_size,
+ (finfo.st_size == 1) ? "" : "s");
+ printf( "Inode Last Change at: %s", ctime( &finfo.st_ctime ) );
+ printf( " Last access at: %s", ctime( &finfo.st_atime ) );
+ printf( " Last modified at: %s", ctime( &finfo.st_mtime ) );
+ return( 0 );
+}
+
+int com_delete( char *arg )
+{
+ too_dangerous( "delete" );
+ return( 1 );
+}
+
+
+/* Change to the directory ARG. */
+int com_cd( char *arg )
+{
+ if( !strcmp( arg, "~" ) )
+ {
+ arg = getenv( "HOME" );
+ }
+
+ if( chdir( arg ) == -1 )
+ {
+ perror( arg );
+ return( 1 );
+ }
+
+ com_pwd( "" );
+ return( 0 );
+}
+
+/* Print out the current working directory. */
+int com_pwd( char *ignore )
+{
+ char dir[PATH_MAX], *s;
+
+ s = getcwd( dir, PATH_MAX );
+ if( s == 0 )
+ {
+ printf( "Error getting pwd: %s\n", dir );
+ return( 1 );
+ }
+
+ printf( "Current directory is: %s\n", dir );
+ return( 0 );
+}
+
+int com_ping( char *arg )
+{
+ if( !valid_argument( "ping", arg ) )
+ return( 1 );
+
+ sprintf( syscom, "ping %s", arg );
+ return( system( syscom ) );
+}
diff --git a/src/shell.h b/src/shell.h
new file mode 100644
index 0000000..15b4278
--- /dev/null
+++ b/src/shell.h
@@ -0,0 +1,28 @@
+
+#ifndef __SHELL_H
+#define __SHELL_H
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+extern int com_id( char *arg );
+extern int com_ls( char *arg );
+extern int com_more( char *arg );
+extern int com_vi( char *arg );
+extern int com_rename( char *arg );
+extern int com_stat( char *arg );
+extern int com_delete( char *arg );
+extern int com_cd( char *arg );
+extern int com_ping( char *arg );
+extern int com_pwd( char *ignore );
+
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __SHELL_H */
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 <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <sys/types.h>
+#include <sys/file.h>
+#include <sys/stat.h>
+#include <sys/errno.h>
+#include <limits.h>
+#include <unistd.h>
+#include <time.h>
+
+#include <readline/readline.h>
+#include <readline/history.h>
+
+#define _GNU_SOURCE
+#include <getopt.h>
+
+#include <main.h>
+#include <commands.h>
+#include <completion.h>
+#include <utils.h>
+
+#include <shell.h>
+#include <users.h>
+
+/* **************************************************************** */
+/* */
+/* 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 );
+}
diff --git a/src/users.h b/src/users.h
new file mode 100644
index 0000000..125f13b
--- /dev/null
+++ b/src/users.h
@@ -0,0 +1,42 @@
+
+#ifndef __USERS_H
+#define __USERS_H
+
+
+#define ROLE_ADMIN "admin"
+#define ROLE_OPERATOR "operator"
+#define ROLE_USER "user"
+
+#define GROUP_ADMIN "priv-admin"
+#define GROUP_OPERATOR "priv-operator"
+#define GROUP_USER "priv-user"
+
+
+#define GROUP_IPMI "ipmi"
+#define GROUP_REDFISH "redfish"
+#define GROUP_WEB "web"
+#define GROUP_SSH "ssh"
+
+/* D-Bus entities to manage user accounts */
+#define USERS_DBUS_SERVICE "xyz.openbmc_project.User.Manager"
+#define USERS_DBUS_ROOT_PATH "/xyz/openbmc_project/user"
+#define USERS_DBUS_ATTRIBUTES "xyz.openbmc_project.User.Attributes"
+#define USERS_DBUS_DELETE "xyz.openbmc_project.Object.Delete"
+
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+
+extern int com_useradd( char *arg );
+extern int com_userdel( char *arg );
+extern int com_userlist( char *arg );
+
+
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __USERS_H */
diff --git a/src/utils.c b/src/utils.c
index 05844da..07429fa 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -46,7 +46,7 @@ char *dupstr( char *s )
{
char *r;
- r = xmalloc (strlen (s) + 1);
- strcpy (r, s);
- return (r);
+ r = xmalloc( strlen( s ) + 1 );
+ strcpy( r, s );
+ return( r );
}